TweetFollow Us on Twitter

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 )
 {
 statement;
 increment;
 }

This translation shows how the loop is constructed. The initialization takes place once before the loop begins. The test occurs at the top of the loop, and the increment at the bottom. The primary advantage of using a for over a while is that all the loop components are available on one line. You may omit any of the components on the for line but the semicolons must be there as place holders.

It is also possible to have several statements in the for:

 for (i = 0, j = 0; i < 10; i++, j++)
 statement;

Commas separate statements within each line section.

The final text menu handles the style. I have included all the styles I have seen, which include:

Plain

Bold

Italic

Underline

Outline

Shadow

Condensed

Extended

Small Caps

Superscript

Subscript

The first eight are standard Macintosh styles; the others are in MacWrite or Word. As anyone familiar with the Mac is aware, the styles generally may be combined. I assumed that it makes no sense to combine Condensed and Extended or Superscript and Subscript. This complicates the menu arrangement a bit, as selection of an item does not mean just setting or removing a check mark. The total style is collected in a short, in which each bit represents one style. Since only ten bits are in use so far (Plain is all bits off) suggestions for additional styles are open (would it, for example, be nice to set a block of text to all upper case or lower case as in MacDraw?). Where possible, the style menu illustrates the style through the use of the menu meta-characters.

Getting the Text

The eventloop() detects keyDown events. If the command key is not down, it calls ed_key() to send the character to the window. The event loop does not call ed_key() unless the window has been opened to prevent writing to an unopened window. At the moment it does not detect auto-key events. This would be something to add.

The Edit Functions

There are several Edit functions, but only two of them are of real importance. Two, ed_init() and ed_new() do some simple initialization. For this program they probably could have been combined. The most important thing that ed_new() does is to call each of the text-setting functions, dofont(), dosize(), and dostyle(), to establish initial conditions and set the menus accordingly. Each of those functions contains special code to handle the situation where the item equals zero. Since the item cannot equal zero when it is the result of mouse input, I used it for initialization.

Any time there is a change in font, size, or style, the relevant function calls ed_cset() where the change is noted and stored. ed_cset does not effect the change; it just keeps it around. This allows users to change their minds by making repeated changes through the menu without affecting anything. The change is put into effect when ed_key() is called from the event loop.

The function ed_key() does most of the work. It calls the various QuickDraw routines for changing font, size, and style, uses DrawChar() to write the character. For each kind of change it checks the flag in edbuf.cesc, and if the flag is set, it gets the value and does the change.

The styles not built into QuickDraw require special handling. To deal with the super and subscripts, ed_key() retrieves the pen position and adjusts it vertically. It does this for every character written. It probably could be done only when the the styles involved changed, but this logic is simpler.

Prior to writing each character, ed_key() checks if small caps is on. If it is, and the character is a lower case letter, it sets the size to the next smaller size (in the menu), converts the character to upper case, draws it and sets the size back to the original size. The actual character itself is not affected. Two library functions, islower() and toupper(), provide the test of lower case and change to upper case. These are part of a set of character functions that most C compilers include. I have not used the library, but included the functions right in the program. This is not due to any aversion to using libraries, but because the functions in the Lightspeed library use a C construct we have not used before:

 toupper(c)
 char c;
 {
 return( c>='a')&&(c<='z') ? (c-32) : c);
 }

This construction may be read:

 test ? do if true : do if false

As can be seen in toupper(), this form can be rather cryptic at first. toupper() simply asks if the character, c, is greater or equal to 'a' and less than or equal to 'z.' If it is, subtract 32 (converts to upper case in ASCII) and return that value; if not, return c unchanged.

Organization of the Program

The edit functions are in a separate file, and there is a separate header file, ed.h, that is included in the main program and in ed.c.

You will notice several lines in ed_key() that serve no apparent purpose. The structure edbuf contains a buffer in all characters and font/size/style changes are written (though the line that writes to the buffer is commented out). This is to allow eventual updating of the window.

printw()

There have been two changes to printw(). First, I fixed a bug. Previously it determined the line size on each call from the Grafport that was open when it was called. This resulted in rather odd interaction between the window being written by ed_key(), the printw() window and the menus. At one time I had menus with text in one or two point type. I moved the determination of line size inside the if-statement that is done the first time printw() is called.

The other change is the addition of code to print strings. This prints only C strings. We probably should add the ability to print Pascal strings and points and rectangles. Some of these changes may happen for next month.

Next Month

I hope to get my Mac upgraded with the 128K ROMs and the 800K drive. I also hope to stay healthy. We will continue to explore QuickDraw. I intend to draw various shapes on the screen.


/* QD1.C
 *    Sending text to window with
 *    function that accepts a variable
 * number of parameters
 *
 * Compiled with LightspeedC
 *
 * Important note for Mac C users:
 * Every place you see event->where,
 * replace it with &event->where
 */
 
 
 #include "abc.h"
 #include "Quickdraw.h"
 #include "EventMgr.h"
 #include "WindowMgr.h"
 #include "MenuMgr.h"
 #include "FontMgr.h"
 #include "ed.h" 
 
 /* defines for menu ID's */
 
 #defineMdesk    100
 #defineMfile    101
 #defineMedit    102
 #defineMfont    103
 #defineMstyl    104
 #defineMsize    105
 
 /* Window items */
 /* File */
 #defineiNew1
 #defineiClose 2
 #defineiQuit  3
 
 /* Edit */
 #defineiUndo  1
 #defineiCut3
 #defineiCopy  4
 #defineiPaste 5
 
 /* Style */
 #defineiPlain 1
 #defineiBold  2
 #defineiItalic  3
 #defineiUline 4
 #defineiOline 5
 #defineiShado 6
 #defineiCon7
 #defineiExt8
 #defineiScaps 9
 #defineiSuper 10
 #defineiSub11
 
 #definePlainStyle 0
 #defineBoldStyle1
 #defineItalicStyle2
 #defineUnderStyle 4
 #defineOutlineStyle 8
 #defineShadowStyle16
 #defineCondStyle32
 #defineExtStyle 64
 
 
 
 /* Global variables */
 
 MenuHandle menuDesk;/* menu handles */
 MenuHandle menuFile;
 MenuHandle menuEdit;
 MenuHandle menuFont;
 MenuHandle menuStyl;
 MenuHandle menuSize;
 
 
 WindowPtrtheWindow;
 WindowRecord    windowRec;
 Rect   dragbound;
 Rect   limitRect;
 
 #defineNsize  9
 
 uchar  sizes[Nsize] = {8,9,10,12,14,18,24,28,36};
 
 
main()
{
 initsys(); /* system initialization */
 initapp(); /* application initialization */
 eventloop();
}


/* system initialization 
 * note use of hard coded screen sizes
 * with LightspeedC.  This will work
 * with other compilers but is not
 * good practice
 */
initsys() 
{
 InitGraf(&thePort); /* these two lines done */
 InitFonts();    /* automatically by Mac C */
 InitWindows();
 InitCursor();
 InitMenus();
 theWindow = Nil;/*indicates no window */
 SetRect(&dragbound,0,0,512,250);
 SetRect(&limitRect,60,40,508,244);
}


/*
 * application initialization
 * Sets up menus.
 * Initialize ed
 */
initapp()
{
 setupmenu();
 ed_init();
}

/*
 * set up application's menus
 * Each menu is a separate group
 * of lines.  
 */
setupmenu()
{
 menuDesk = NewMenu(Mdesk,CtoPstr("\24"));
 AddResMenu (menuDesk, 'DRVR');
 InsertMenu (menuDesk, 0);
 
 menuFile = NewMenu(Mfile, CtoPstr("File"));
 AppendMenu (menuFile, CtoPstr("New;Close;Quit/Q"));
 InsertMenu (menuFile, 0);
 
 menuEdit = NewMenu(Medit, CtoPstr("Edit"));
 AppendMenu (menuEdit, CtoPstr( "(Undo/Z;(-;(Cut/X;(Copy/C;(Paste/V;(Clear"));
 InsertMenu (menuEdit, 0);
 
 menuFont = NewMenu(Mfont, CtoPstr("Font"));
 AddResMenu (menuFont, 'FONT');
 InsertMenu (menuFont, 0);
 
 menuStyl = NewMenu(Mstyl, CtoPstr("Style"));
 AppendMenu (menuStyl, 
 CtoPstr("Plain/P;<BBold/B;<IItalic/I;<UUnderline/U;"));
 AppendMenu (menuStyl,CtoPstr( "<OOutline/O;<SShadow/S;Condensed;Extended"));
 AppendMenu (menuStyl,CtoPstr( "SmallCaps;Superscript/H;Subscript/L"));
 InsertMenu (menuStyl, 0);
 
 menuSize = NewMenu(Msize, CtoPstr("Size"));
 AppendMenu (menuSize,CtoPstr( "09 Point;10 Point;12 Point;14 Point"));
 AppendMenu (menuSize,CtoPstr( "18 Point;24 Point;28 Point;36 Point"));
 InsertMenu (menuSize, 0);
 
 DrawMenuBar();
}
 
 
/* Event Loop 
 * Loop forever until Quit
 */
eventloop()
{
 EventRecordtheEvent;
 char   c;
 short  windowcode;
 WindowPtrww;
 
 while(True)
 {
 if (theWindow)      /* this code is here to */
 { /* prevent closing an already */
 EnableItem(menuFile,2);  /* closed window */
 DisableItem(menuFile,1);
 }
 else   
 { 
 EnableItem(menuFile,1);
 DisableItem(menuFile,2);
 }
 if (GetNextEvent(everyEvent,&theEvent))
 switch(theEvent.what)    
 { /* only check key and */
 case keyDown:   /* mouse down events */
 c = theEvent.message & charCodeMask;
 if (theEvent.modifiers & cmdKey)
 domenu(MenuKey(c));
 else if (theWindow)
 ed_key(c);
 break;
 case mouseDown:
 domouse(&theEvent);
 break;
 default:
 break;
 }
 }
}


/* domouse
 * handle mouse down events
 */
domouse(er)
 EventRecord*er;
{
 short  windowcode;
 WindowPtrwhichWindow;
 short  ingo;
 long   size;
 long   newsize;
 
 
 windowcode = FindWindow(er->where, 
 &whichWindow);
 switch (windowcode)
 {
 case inDesk:
 if (theWindow notequal 0)
 {
 HiliteWindow(theWindow, False);
 DrawGrowIcon(theWindow);
 }
 break;
 case inMenuBar:
 domenu(MenuSelect(er->where));
 break;
 case inSysWindow:
 SysBeep(1);
 break;
 case inContent:
 if (whichWindow equals theWindow)
 {
 HiliteWindow(whichWindow,True);
 DrawGrowIcon(theWindow);
 }
 break;
 case inDrag:
 DragWindow(whichWindow, 
   er->where, &dragbound);
 DrawGrowIcon(theWindow);
 break;
 case inGrow:
 break;
 case inGoAway:
 ingo = TrackGoAway(whichWindow,er->where);
 if (ingo)
 {
 CloseWindow(whichWindow);
 theWindow = Nil;
 }
 break;
 }
}

/* domenu
 * handles menu activity
 * simply a dispatcher for each
 * menu.
 */
domenu(mc)
 long   mc; /* menu result */
{
 short  menuId;
 short  menuitem;
 
 menuId = HiWord(mc);
 menuitem = LoWord(mc);
 
 switch (menuId)
 {
 case Mdesk : break; 
 /*apple menu not handling DA's */
 case Mfile : dofile(menuitem);
  break;
 case Medit : /* all disabled */
  break;
 case Mfont : dofont(menuitem);
  break;
 case Mstyl : dostyle(menuitem);
  break;
 case Msize : dosize(menuitem);
  break;
 }
 HiliteMenu(0);
}

/* dofont
 * marks selected font and obtains
 * font number.  modifies size 
 * menu to reflect available sizes
 * If item is zero, sets item to 3
 * (geneva, the application default)
 */
 
 dofont(item)
 short  item;
 {
 Str255 itemS;
 short  cfont; /* actual font number */
 short  i;
 static short  lastitem = 0;
 
 if (item equals 0)
 do
   {
   item++;
   GetItem(menuFont,item,&itemS);
   GetFNum(&itemS,&cfont);
   }
 while (cfont notequal 3);
 CheckItem (menuFont,lastitem,False);
 CheckItem (menuFont,item,True);
 lastitem = item;
 GetItem(menuFont,item,&itemS);
 GetFNum(&itemS,&cfont);
 for (i = 1; i < Nsize; i++)
 {
 if (RealFont(cfont,sizes[i]))
 SetItemStyle(menuSize,i,OutlineStyle);
 else
 SetItemStyle(menuSize,i,0);
 }
 ed_cset(edFont,cfont);
 }
 
 
 /*
  * sets size menu, if item
  * is 0, sets item to 3 (12pt)
  */
 dosize (item)
 short  item;
 {
 static short  lastitem = 0;
 
 if (item equals 0)
   item = 3;
 CheckItem (menuSize,lastitem,False);
 CheckItem (menuSize,item,True);
 lastitem = item;
 ed_cset(edSize,item);
 }
 
 dostyle(item)
 short  item;
 {
 static short  thestyle = 0;
 short  mitems;
 short  i;
 short  style;
 short  styleflag;
 short  offflag;
 
 if (item equals 0)
 item = 1;
 style = item - 1; /* move item to style range */
 if (style) /* if not plain */
 {
 styleflag = 1;
 styleflag = styleflag << (style - 1);
 CheckItem (menuStyl,1,False); /* unmark plain */
 if (thestyle & styleflag) /* if style is marked */
 { /* un mark it */
 CheckItem (menuStyl,item,False);
 thestyle = thestyle & ~styleflag;
 }
 else
 {
 offflag = 0;
 switch (item)
 {
 case iCon :
 CheckItem(menuStyl,iExt,False);
 offflag = 64;
 break;
 case iExt :
 CheckItem(menuStyl,iCon,False);
 offflag = 32;
 break;
 case iSuper :
 CheckItem(menuStyl,iSub,False);
 offflag = 512;
 break;
 case iSub :
 CheckItem(menuStyl,iSuper,False);
 offflag = 256;
 break;
 } 
 CheckItem (menuStyl,item,True);
 thestyle &= ~offflag;
 thestyle ^= styleflag;
 }
 }
 else
 {
 thestyle = 0;
 mitems = CountMItems(menuStyl);
 for (i = 2; i <= mitems; i++)
 CheckItem (menuStyl,i,False);
 CheckItem (menuStyl,1,True);
 }
 ed_cset(edStyle,thestyle);
 }
  

/* dofile
 * handles file menu
 */
dofile(item)
 short  item;
{
 char   *title1; /* first title for window */
 Rect   boundsRect;
 
 switch (item)
 {
 case iNew :/* open the window */
 title1 = "ABC Window";
 SetRect(&boundsRect,50,50,400,200);
 theWindow = NewWindow(&windowRec, &boundsRect,
 CtoPstr( title1),True,documentProc,
 (WindowPtr) -1, True, 0);
 DrawGrowIcon(theWindow);
 PtoCstr(title1);
 DisableItem(menuFile,1);
 EnableItem(menuFile,2);
 ed_new(theWindow);
 break;
 
 case iClose :   /* close the window */
 CloseWindow(theWindow);
 theWindow = Nil;
 DisableItem(menuFile,2);
 EnableItem(menuFile,1);
 break;
 
 case iQuit :    /* Quit */
 ExitToShell();
 break; 
 }
}


/* printw()
 *
 * Displays strings and numbers in a 
 * special window
 *
 * This function is designed to receive
 * a variable number of parameters. The 
 * number is computed by the number of
 * percent signs in the control string.
 * If the number of parameters following the 
 * control string does not match the 
 * number of percent signs, expect 
 * the unexpected.
 */
printw(cs)
 char   *cs;/* the control string */
{
#define Bufsz  14/* size of buffer to hold */
 /* converted numbers */  

 static RectboundsRect; /* variables for */
 static RectwindowRect; /* defining printw */
 static WindowRecord wrc; /* window, pw is */
 static WindowPtrpw = 0;  /* initialized to 0 */
 static short    linesz;  /* size of line */
 
 WindowPtroldport; /* save grafport here */
 FontInfo info;  
 short  nl;
 Point  pt;
 RgnHandleupdrgn;/* needed for scrolling */
 char   numAsStr[Bufsz];  /* number conversion */
 short  nsz;/* size of numbers (2 or 4) */
 char   **ts;  /* ptr to ptr to ctrl string */
 char   *ps;/* ptr to parameters */
 ulong  num;/* for number conversion */
 short  convchar;/* found conversion char */
 short  islong;  /* number is a long */
 char   c;/* char parameter */
 char   *s; /* string pointer parameter */
 long   tcs;
 char   *tps;
 
/* Window rectancgle coordinates */

#define wl0
#define wr512
#define wt250
#define wb342
 
 
 GetPort(&oldport);/* save current graph port */
 if (pw equals 0)/* if window does not exist, */
 { /*  open it */
 SetRect(&boundsRect,wl,wt,wr,wb);
 pw = NewWindow(&wrc, &boundsRect,
 CtoPstr(""),True,plainDBox,
 (WindowPtr) -1, True, 0);
 GetFontInfo(&info); /* compute line height */
 linesz = info.ascent + info.descent;
 nl = linesz;    /* move down one line as */
 } /*  writing will be above */
 else   /*  boundary.  No need to  */
 nl = 0;/*  move line if already open */
 SetPort(pw);    /* Set graf port to this window */
 Move(0,nl);/* Move (relative) */
 
 ts = &cs;/* get address of control string ptr */
 ps = (char *)ts;/* convert to pointer to params */
 ps += sizeof(long); /* skip over control string ptr */
 tcs = (long)cs;
 tps = ps;
 while (*cs)/* loop until end of control string */
 {
 switch (*cs)    /* check each character */
 {
 case '%' : /* percent sign: check conversion */
 cs++;  /* point to next char */
 convchar = False; /* init for conv loop */
 islong = False;
 do {   /* loop til reach conversion char */
 switch (*cs)
 {
 case 'l' : /* indicates a long */
 islong = True;
 cs++;
 break;
 case 'u' : /* unsigned decimal */
 case 'd' : /* signed decimal */
 if (islong)   /* extract numb */
 {
 num = *(ulong*)ps;
 nsz = sizeof(long);
 }
 else
 {
 num = *(ushort*)ps;
 nsz = sizeof(short);
 }
 ps += nsz; /* point to next */
 ntoa(num,nsz,'u' - *cs,numAsStr); /* convert & write number */
 DrawString(CtoPstr(numAsStr));
 convchar = True;
 break;
 case 's' :
 num = *(ulong*)ps;
 DrawString(CtoPstr(num));
 PtoCstr(num);
 ps += sizeof(char*);
 convchar = True;
 break; 
 case 'c' :
 c = *(ushort*)ps;
 DrawChar(c);
 nsz = sizeof(short);
 convchar = True;
 ps += nsz;
 break;
 default: /* all other char */
 DrawChar(*cs); /* write char */
 convchar = True;
 } 
 } while (not convchar);
 break;
 case '\n' :/* newline ('\n') control string */
 GetPen(&pt);  /* find current pen position */
 if (pt.v+linesz > wb-wt)
 /* if it goes off window, */
 { /* scroll the window */
 updrgn = NewRgn();
 ScrollRect(&(pw->portRect), 0,-linesz,updrgn);
 DisposeRgn(updrgn); /* no update */
 Move(0,-linesz);/* move onto window */
 }
 Move(-pt.h,linesz); /* move beg of next line */
 break;
 default :/* any other character just gets */
 DrawChar(*cs);  /*  written on the window */
 }
 cs++;  /* move pointer to next char */
 } /*  in control string and continue */
 
 SetPort(oldport); /* restore orignal graf port */
}

/* Convert numbers to ascii strings
 * Handles signed and unsigned
 * short and long values
 * Note:Length of string returned 
 * must be large enough to 
 * hold -2G (12 bytes)
 */
ntoa(n,len,issigned,s)
 ulong  n;/* number to convert */
 short  len;/* size of n (2 or 4)*/
 short  issigned;/* signed flag */
 char   *s; /* string to return */
{
 char   ts[12];  /* temporary string */
 int    i = 0; /* counter, initialized */
 ulong  m;/* working copy of */
 long   sm; /* to convert signed values */
 
 if (n equals 0) /* if n is zero, place '0' */
 ts[i++] = '0';  /*  in temporary string */
 else
 {
 if (issigned)   /* if sign flag is set, */
 { /*  convert to signed value */
 if (len equals sizeof(long))
 sm = (long)n;
 else
 sm = (short)n;
 if (issigned = sm < 0) /* Check if value is */
 n = -sm; /*  negative. If so, */
 } /*  keep the flag and */
 /*  get the absolute value */
 while (n)/* Convert number into ascii */
 { /*  by repeatedly taking mod */
 ts[i++] = n % 10 + '0';  /*  and dividing.  This */
 n /= 10; /*  gives a string in */
 } /*  reverse order */
 if (issigned)   /* If number was negative, */
 ts[i++] = '-';  /*  stick a minus sign in */
 } /*  the string.*/
 
 do{    /* Reverse the string */
 *s++ = ts[--i]; /*  to the correct direction*/
 } 
 while (i);

 *s = '\0'; /* Place null terminator on */
}/*  string */



/*
 *
 *  ed.c source
 * 
 */
#include"abc.h"
#include"ed.h"

extern  charsizes[];

edrec edbuf;

/*
 * Initialize some parts of the
 * edbuf structure.  This coulb
 * easily be combined with ed_new()
 */
ed_init()
{
 edbuf.cesc = 0;
 edbuf.cndx = 0;
 edbuf.supsub = 0;
 edbuf.chars[0] = 0;
 edbuf.linenum = 0;
}

/*
 * save grafport, moves to
 * top of port, initializes
 * font, style, and size.
 * Initialize edbuf values so
 * the calls to dofont will
 * find different values than
 * are being set.
 */
ed_new(aport)
 GrafPtraport;
{
 GrafPtrgp;
 
 edbuf.edport = aport;
 GetPort(&gp);
 SetPort(aport);
 MoveTo(0,0);
 edbuf.face[edFont] = -1;
 edbuf.face[edStyle] = -1;
 edbuf.face[edSize] = -1;
 dofont(0);
 dostyle(0);
 dosize(0);
 SetPort(gp);
}


/*
 * Allows setting or getting
 * a value (size,font,style).
 * Is not used.
 */
ed_set(what,value)
 short  what;
 short  value;
{
 if (value >= 0)
 edbuf.face[what] = value;
 return(edbuf.face[what]);
}

/*
 * Called by any routine wishing
 * to change the size, style or font.
 * The change is indicated in the
 * cesc element of the edbuf structure, 
 * and the value stored in the cface 
 * element.  The change is not applied
 * until a new character is written.
 */
ed_cset(what,value)
 short  what;
 short  value;
{
 char wbit = 1;
 
 wbit <<= what;
 if (edbuf.face[what] equals value)
 edbuf.cesc &= ~wbit;
 else
 {
 edbuf.cesc |= wbit;
 edbuf.cface[what] = value;
 }
}


/*
 * Writes the character c to the
 * window.  Before writing any 
 * character, it checks to see if 
 * size/font/style have changed and
 * effects the necessary changes.
 *
 * Since sub and super scripts and 
 * small caps styles are not supported
 * by quick draw, they are handled
 * here.
 *
 * Small Caps is implemented by using
 * the font size one smaller on the 
 * menu for the lower case letters.
 *
 * Sub and superscripts are generated
 * by shifting the position up or down
 * one third of a line of the the current
 * size.
 */
ed_key(c)
 char c;
{
 short  *sp;
 FontInfo info;
 static short  linesz;
 GrafPtrgp;
 Point  pt;
 short  lineinc;
 short  linedec;
 
 GetPort(&gp);
 SetPort(edbuf.edport);
 lineinc = linedec = 0;
 if (edbuf.cesc)
 {
 if (edbuf.face[edStyle] & Supbit + Subbit)
 linedec = -edbuf.supsub;
 edbuf.chars[edbuf.cndx++] = edbuf.cesc;
 sp = (short *)&edbuf.chars[edbuf.cndx];
 if (edbuf.cesc & 1) /* Font */
 {
 *sp = edbuf.cface[edFont];
 edbuf.face[edFont] = *sp;
 TextFont(*sp);
 sp++;
 edbuf.cndx +=2;
 }
 if (edbuf.cesc & 2) /* Size */
 {
 *sp = edbuf.cface[edSize];
 edbuf.face[edSize] = *sp;
 TextSize(sizes[*sp]);
 sp++;
 edbuf.cndx +=2;
 }
 if (edbuf.cesc & 4) /* Style */
 {
 *sp = edbuf.cface[edStyle];
 edbuf.face[edStyle] = *sp;
 TextFace((Style)*sp & 0x7F);
 sp++;
 edbuf.cndx += 2;
 }
 edbuf.chars[edbuf.cndx++] = edbuf.cesc;
 edbuf.cesc = 0;
 GetFontInfo(&info);
 linesz = info.ascent + info.descent;
 if (edbuf.face[edStyle] & Subbit)
 edbuf.supsub = linesz / 3;
 else if (edbuf.face[edStyle] & Supbit)
   edbuf.supsub = -linesz/3;
 else
 edbuf.supsub = 0;
 lineinc = edbuf.supsub;
 } 
 /*edbuf.chars[edbuf.cndx++] = c;*/
  if (c equals '\r')
   {
   GetPen(&pt);
   Move(-pt.h,linesz + lineinc + linedec);
   edbuf.linenum++;
   }
  else
   {
   GetPen(&pt);
   Move (0,linedec + lineinc);
   if (edbuf.face[edStyle] & Capbit and islower(c))
   {
   TextSize(sizes[edbuf.face[edSize] - 1]);
   DrawChar(toupper(c));
   TextSize(sizes[edbuf.face[edSize]]);
   }
   else
   DrawChar(c);
   }
 SetPort(gp);
}
 
inttoupper(c)
 char c;
{
 return( (c>='a')&&(c<='z') ? (c-32) : c );
}
 
intislower(c)
 char c;
{
 return( (c >= 'a') and (c <= 'z') ? True : False);
}
 
 
/* abc.h 
 *
 * Local definitions to improve readability
 *
 */
 
#define True1
#define False  0
#define Nil 0
#define and &&
#define or||
#define not !
#define equals ==
#define notequal !=

/* unsigned longs and shorts
 * (unsigned longs may not be 
 *  available with all compilers
 */
#define ushort   unsigned short
#define ulong    unsigned long
#define uchar    unsigned char

/* General purpose external routines 
 * String conversion routines 
 * return a pointer to a char 
 */
extern  char*CtoPstr();
extern  char*PtoCstr(); 

/*
 * ed.h
 * definitions for edit functions
 */
 
#include"quickdraw.h"
 
#define edFont 0
#define edSize 1
#define edStyle  2

#define Capbit 128
#define Supbit 256
#define Subbit 512

struct edstruct
 {
 GrafPtredport;
 Rect   edrect;
 short  face[3];
 short  cface[3];
 short  supsub;
 short  linenum;
 char   cesc;
 char   chars[1000];
 short  cndx;
 };
 
typedef struct edstruct edrec;
typedef edrec *edpointer;
 
AAPL
$98.15
Apple Inc.
-0.23
MSFT
$43.58
Microsoft Corpora
-0.31
GOOG
$587.42
Google Inc.
+1.81

MacTech Search:
Community Search:

Software Updates via MacUpdate

Knock 1.1.7 - Unlock your Mac by knockin...
Knock is a faster, safer way to sign in. You keep your iPhone with you all the time. Now you can use it as a password. You never have to open the app -- just knock on your phone twice, even when it's... Read more
Mellel 3.3.6 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
LibreOffice 4.3.0.4 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
Freeway Pro 7.0 - Drag-and-drop Web desi...
Freeway Pro lets you build websites with speed and precision... without writing a line of code! With it's user-oriented drag-and-drop interface, Freeway Pro helps you piece together the website of... Read more
Drive Genius 3.2.4 - Powerful system uti...
Drive Genius is an OS X utility designed to provide unsurpassed storage management. Featuring an easy-to-use interface, Drive Genius is packed with powerful tools such as a drive optimizer, a... Read more
Vitamin-R 2.15 - Personal productivity t...
Vitamin-R creates the optimal conditions for your brain to work at its best by structuring your work into short bursts of distraction-free, highly focused activity alternating with opportunities for... Read more
Toast Titanium 12.0 - The ultimate media...
Toast Titanium goes way beyond the very basic burning in the Mac OS and iLife software, and sets the standard for burning CDs, DVDs, and now Blu-ray discs on the Mac. Create superior sounding audio... Read more
OS X Yosemite Wallpaper 1.0 - Desktop im...
OS X Yosemite Wallpaper is the gorgeous new background image for Apple's upcoming OS X 10.10 Yosemite. This wallpaper is available for all screen resolutions with a source file that measures 5,418... Read more
Acorn 4.4 - Bitmap image editor. (Demo)
Acorn is a new image editor built with one goal in mind - simplicity. Fast, easy, and fluid, Acorn provides the options you'll need without any overhead. Acorn feels right, and won't drain your bank... Read more
Bartender 1.2.20 - Organize your menu ba...
Bartender lets you organize your menu bar apps. Features: Lets you tidy your menu bar apps how you want. See your menu bar apps when you want. Hide the apps you need to run, but do not need to... Read more

Latest Forum Discussions

See All

Ice Wings Plus (Games)
Ice Wings Plus 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: THE GREAT ENDLESS RUNNER OF COMBAT JETS IS BACK !! With more than 680.000 downloads in the App Store, Ice Wings: Skies of Steel... | Read more »
Murl the Squirrel (Games)
Murl the Squirrel 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Meet Murl. He is teased by a group of flying squirrels because he can't fly. Determined to show them he's can fly, he meets... | Read more »
Celleste (Games)
Celleste 0.1 Device: iOS Universal Category: Games Price: $2.99, Version: 0.1 (iTunes) Description: Lots of cute action with amazing 3D graphics and a new type of gameplay! Take control over the forces of the universe to help a group... | Read more »
Super Heavy Sword (Games)
Super Heavy Sword 0.0.1 Device: iOS Universal Category: Games Price: $.99, Version: 0.0.1 (iTunes) Description: Get Ready to Get HEAVY! Monster Robot Studios presents SUPER Heavy Sword! The sequel to the smash hit HEAVY sword which... | Read more »
Angels In The Sky (Games)
Angels In The Sky 1.00 Device: iOS Universal Category: Games Price: $6.99, Version: 1.00 (iTunes) Description: - A.I.S will only run smoothly on iPhone 5s. It's NOT compatible with iPad, iPhone 5 or earlier devices.- In order to... | Read more »
80 Days (Games)
80 Days 1.0.2 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.2 (iTunes) Description: 1872, with a steampunk twist. Phileas Fogg has wagered he can circumnavigate the world in just eighty days. Choose your own route... | Read more »
Micromon (Games)
Micromon 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: 130+ Animated Monsters to Catch & Battle! No waiting, play at your own pace! Embark on an epic monster capture RPG like none... | Read more »
Empire Manager (Games)
Empire Manager 1.0 Device: iOS iPhone Category: Games Price: $3.99, Version: 1.0 (iTunes) Description: Become ruler of an empire. Manage your economy, develop technology, hire an army and conquer the world in this addictive turn-... | Read more »
Empire Manager HD (Games)
Empire Manager HD 1.0 Device: iOS Universal Category: Games Price: $7.99, Version: 1.0 (iTunes) Description: Become ruler of an empire. Manage your economy, develop technology, hire an army and conquer the world in this addictive... | Read more »
Star Admiral Review
Star Admiral Review By Rob Thomas on July 30th, 2014 Our Rating: :: ADMIRABLE ADMIRALSUniversal App - Designed for iPhone and iPad While this new digital CCG may feel a bit familiar, Star Admiral offers a sci-fi twist and galaxy’s... | Read more »

Price Scanner via MacPrices.net

iPad Cannibalization Threat “Overblown”
Seeking Alpha’s Kevin Greenhalgh observes that while many commentators think Apple’s forthcoming 5.5-inch panel iPhone 6 will cannibalize iPad sales, in his estimation, these concerns are being... Read more
Primate Labs Releases July 2014 MacBook Pro P...
Primate Labs’ John Poole has posted Geekbench 3 results for most of the new MacBook Pro models that Apple released on Tuesday. Poole observes that overall performance improvements for the new MacBook... Read more
Apple Re-Releases Bugfixed MacBook Air EFI Fi...
Apple has posted a bugfixed version EFI Firmware Update 2.9 a for MacBook Air (Mid 2011) models. The update addresses an issue where systems may take longer to wake from sleep than expected, and... Read more
Save $50 on the 2.5GHz Mac mini, plus free sh...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Save up to $140 on an iPad Air with Apple ref...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more
$250 price drop on leftover 15-inch Retina Ma...
B&H Photo has dropped prices on 2013 15″ Retina MacBook Pros by $250 off original MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.3GHz Retina MacBook Pro: $2249, $250 off... Read more
More iPad Upgrade Musings – The ‘Book Mystiqu...
Much discussed recently, what with Apple reporting iPad sales shrinkage over two consecutive quarters, is that it had apparently been widely assumed that tablet users would follow a two-year hardware... Read more
13-inch 2.5GHz MacBook Pro on sale for $999,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $999.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $100 off MSRP. Price is... Read more
Save up to $300 on an iMac with Apple refurbi...
The Apple Store has Apple Certified Refurbished iMacs available for up to $300 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. These are the best prices on... Read more
WaterField Unveils 15″ Outback Solo & 13″...
Hard on the heels of Apple’s refreshed MacBook Pro Retina laptops announcement, WaterField Designs has unveiled a 15-inch version of the waxed-canvas and leather Outback Solo and a 13-inch version of... Read more

Jobs Board

Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.