Two Editors
Volume Number: 7
Issue Number: 5
Column Tag: Jörg's Folder
Two Simple Editors
By Jörg Langowski, MacTutor Editorial Board
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
Two simple editors”
If you’ve read enough about drawing shapes in windows under MacApp, here’s a
little break. In fact, the next feature that I wanted to add to the drawing example was to
put text boxes into the document. I soon found out that in order to do that in a clean way,
one needs to understand the view architecture of MacApp a little better. In order to do
that, we’ll diverge from our previous example and look at two small text editor
programs.
Views
A view - in the MacApp perspective - is anything that is displayed on a screen.
Thus, a window by itself is a view. Anything that is displayed in that window is also a
view. Views depend on each other in a hierarchical way; if you display some editable
text, some controls and a list in a window, these items will be subviews of the main
window view. Each subview can again display other views, which will then depend on
that subview, and so on.
In our example, we display editable text in a window. The view class used for
displaying a TextEdit record is called TTEView. Our window will contain one subview of
this class. MacApp provides a function which creates a window with its associated view
hierarchy from a template which is stored in a ‘view’ resource. In our example (see
listing 1&2) NewTemplateWindow (kWindowID, itsDocument) will create a TextEdit
window that responds in the usual way to keyboard input, menu commands like Cut,
Copy and Paste, and changes the cursor to an I-bar when it is over the TextEdit
rectangle in the window. We have set itsDocument to nil because we do not want a
document to be associated with the view. We’ll talk about how to add documents next
time.
We’ll see soon how one creates the view resource, for the moment look at the
program example and see with how little code you can program a functioning text editor
in MacApp. It won’t read or write files yet, neither will it scroll or change the text
style (functions that we’ll add later), but it will print its text.
The listing shows that all the important functionality of the window (and
therefore of the program) is contained in the constructor routine of the TEditor class,
of which our application is an instance (yes, we are finally using constructors in
C++/MacApp, instead of the initialization methods that were inherited from Pascal).
The application’s window is created by NewTemplateWindow, and the TTEView instance
is created from its view hierarchy through the MacApp routine FindSubView, which
finds a subview by its name.
ViewEdit
So how did we generate the template for our window and the TTEView? MacApp
2.0 provides a wonderful tool, ViewEdit. With this program, you generate a view
hierarchy just by drawing it. I cannot go into all the details of that program; the screen
image below shows how a typical dialog looks like that is used to change the parameters
of a view; in this case, our TTEView.
You see from the dialog which parameters can be controlled through the view
template: the upper part of the dialog shows all the attributes of the TTEView, such as
fonts, justification, etc., and the lower part controls the superclass (TView)
parameters. There, we can name the view template (‘edit’), and determine where it is
located in the window and how it responds to resizing of the superview. Through a
similar dialog we can control the initialization of the main window. The view resource
that we created in this way is added to our program.
Other resources that we may need (size, dialogs, etc.) are contained in the file
‘Defaults.rsrc’ in the MacApp library. The editor.r Rez file takes some resources from
that file, and also defines the File menu. The actual C++ program is only about a page
long.
Scrolling the text
The template window created in the first example does not scroll the text. We can
add scrolling through several different routes: there exists a TScroller class, which can
be a subview of our window and in turn contain the TTEView as a subview. We’ll talk
about that class in a later column; for a very simple scrolling editor, we can create a
window that contains one scrollable view using the function NewSimpleWindow (Listing
4). This routine creates a MacApp window from a WIND resource and adds optionally
scrollbars. One parameter to the routine is the view that is to be displayed in the
window.
Thus, for the second editor example, we create a simple TTEView template with
ViewEdit, name it ‘text’ (why not?) and save it in a view ID=1002 resource which will
be copied into our program file. DoCreateViews (Listing 4) will create the view
hierarchy corresponding to that resource ID, in our case only one TTEView. Its
parameters are the associated document (nil here), the superview (also nil), the view
resource ID, and the offset of the view inside its superview (no offset here). gZeroVPt
is a (0,0) point in the 32-bit coordinate system that is used by MacApp views.
We then call NewSimpleWindow, with parameters that indicate the window ID,
whether or not we want horizontal or vertical scrollbars, the document that is supposed
to be displayed in the TTEView (nil, because we are not handling documents in this
example), and the view to be displayed.
With these changes, our editor will also scroll the text. It prints already, so all
we need to add is file handling. We’ll do that in the next column.
Forth news
A reader who downloaded Yerk (the public-domain NEON successor which I wrote
about some months ago), complained about some bugs that seem to have perpetuated
from the original NEON. Walter Kulecz writes:
” Is YERK worth the trouble to learn? Reading the documentation suggested it might
be, having source code is a BIG PLUS!, but playing with the command window suggests it
might not be.