Text Display
Volume Number: 2
Issue Number: 11
Column Tag: ABC's of C
Text Display from Quickdraw
By Bob Gordon, Apropos Publications, MacTutor Contributing Editor
Text Display on the Mac
Like last month's, this month's column deals with using QuickDraw routines for
writing text to a window. Last month we only needed a couple of routines to get
printw() working; this time I tried to use as many of the QuickDraw text functions as I
could. [Note that Quickdraw is the only way text can be drawn to the screen. Ultimately,
all programs no matter how sophisticated must use Quickdraw to get text on the screen.
-Ed]
Those who have been reading along since the ABC's of C started will realize we
have almost covered all of C. This does not mean we are done, but does reflect the nature
of C as a language. C is a relatively small language. If you count keywords, for
example, it has fewer than many dialects of Basic. On the other hand, much of the
language's capabilities are not in the language itself but are in one or another libraries.
Many capabilities included in other languages (such as file access and I/O) are in
libraries. In learning to use C on the Macintosh, the main part of the problem is to
learn to use the various Toolbox functions. We have done menus and started windows.
The plan is to now follow the chapters in Using the Macintosh Toolbox with C and focus
on the Toolbox. This will result in a distorted view of C, in that we will not generally
discuss the standard C functions if there is an analogous Macintosh function.
Short Personal Digression
You may have noticed the note in the September issue. I came down with an
exotic, flu-like disease that lasted about two weeks and had me bedridden for one of
them. It must have been reasonably serious because my doctor threatened to stick an IV
in my arm and put me in the hospital. I convinced her that such measures were not
necessary and seem to have survived.
But then over Labor Day weekend, my Mac died. I stepped out of the room to
check on the state of the children, and when I returned the screen was dark and there
was the unmistakeable smell of fried electrons. I quickly turned everything off, but it
was clear that some component had expired. My diagnosis was that the analog board had
failed. I called around to get estimates for replacing the analog board. These ranged
from just over $100 to over $200. If you are unlucky enough to have your Mac fail,
be sure to get estimates. I figure I saved $90.00 to $100.00 just by spending some
time on the phone.
Fig. 1 Text from Quickdraw
Text on the Macintosh
Anyone who has ever seen a Mac realizes that unlike conventional computers, the
Macintosh displays text in different sizes, styles, and fonts. While the Macintosh
Toolbox contains a set of text-editing functions (TextEdit), they are limited to
displaying only one font/size/style at a time. By directly calling the QuickDraw
functions, we can display text almost any way we want. In order to do this, I first set
up menus to allow user control over the font, style, and size, and then placed a routine
in the event loop to capture keys and write them to a window. That is all this month's
program does. It does not word wrap, scroll, backspace, or do any other useful editing
operation.
Text Menus
Like the Desk Accessories, the font menu is acquired with AddResMenu().
AddResMenu() searches resource files for resources of the type specified and appends
them to the menu. So only two lines are needed to create the font menu:
menuFont = NewMenu(Mfont), CtoPstr("Font"));
AddResMenu (menuFont, 'FONT');
This creates the menu, but a program cannot use the information directly. The
program needs to be able to use the font number that identifies the font. When a user
pulls down a menu and selects an item, the program receives a number indicating which
menu and which item. Since the program has no idea of the available fonts or their
position in the menus, we need a way to determine the font number from the item
number. Two functions accomplish this. The first supplies the item text given the
number, and the second supplies the font number given the font name. These functions
are called in dofont():
GetItem(menuFont,item,&itemS);
GetFNum(&itemS,&cfont);
The parameter itemS is a Pascal string. There is no need to call CtoPstr()
because both routines are from the Toolbox and both expect Pascal strings. cfont
receives the font number which is passed to the editor routines with ed_cset(). (I am
taking the liberty of calling them editor routines even though they do no editing.
Perhaps someday they will.)
The operation of the size menu is simpler. The sizes displayed in the menu are
9, 10, 12, 14, 18, 24, 28, and 36 points. To get the correct size from the menu item,
a global array called sizes is initialized to those values. In C, all arrays start at zero so
the zero element is not used, at least not directly from the menu. You will note it is
initialized to 8 points. One of the styles supported is “Small Caps,” and the 8 point
size is used to make small caps for the 9 point size.
One of the features of size menus is that they let the user know which sizes are
available and which must be scaled. This varies with the font and so is included in
dofont(). A function from the font manager, RealFont() returns a true value if the font
in a given size actually exists. The following little loop provides this feature.
for (i = 1; i < Nsize; i++)
if (RealFont(cfont, sizes[i]))
SetItemStyle(menuSize,i,OutlineStye);
else
SetItemStyle(menuSize,i,0);
This also shows a for loop. The statement consists of five components:
for ( initialize ; test ; increment )
statement;

This directly translates into the following while loop:
initialize;
while ( test )