home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / FontShow 1.1.4 / Fontshow.c < prev    next >
Encoding:
Text File  |  1995-07-12  |  21.4 KB  |  833 lines  |  [TEXT/CWIE]

  1. // FontShow version 1.1.3, updated 950712 for CodeWarrior 6
  2. //•-----------------------------------------------------------------------o//
  3. //•    Note from Kenneth A. Long:
  4. //•     I found this source on the net, but it was only the .c and .h file.
  5. //• Then after a time, I found the built application in a different place
  6. //• and was able to get the resources out of it.  Then I made the .r file
  7. //• from it, for general principles.
  8. //• The two includes I excluded caused link errors I couldn't fix, but it
  9. //• seems to run okay at first glance.  I've done hardly any testing, and
  10. //• I'm sure there could be improvements, refinements and more thorough
  11. //• code added.  But HEY!  It's C source!
  12.  
  13. //•      6. March      1994 (v1.1.2) Compiles/runs - Think C v5.0.4/6.0.1
  14. //•-----------------------------------------------------------------------o//
  15. //o The FontShow
  16.  
  17. //o (c) 1989 Rainer Fuchs, Am Weingarten 13, 6000 Frankfurt 90, FRG
  18.  
  19. //o History:
  20. //•     23. November  1994 (v1.1.2) Runs on Code Warrior C.
  21. //•     06. March      1994 (v1.1.2) Runs on Think C.
  22. //o     17. Oktober      1989 (v1.1.1) Compatible with EZ-Menu 
  23. //o     10. Oktober      1989 (v1.1)    Font number added; Fontsize added
  24. //o      3. Oktober      1989 (v1.0)    multiple sample strings
  25. //o      2. Oktober      1989 (v1.0b3) changed print format 
  26. //o     26. September 1989 (v1.0b2) changed print format
  27. //o     25. September 1989 (v1.0b1) added character table 
  28. //o     24. September 1989 (v0.0d1) first hack
  29. //o-----------------------------------------------------------------------o//
  30.  
  31. #include "fontshow.h"
  32.  
  33. //o-----------------------------------------------------------------------o//
  34. //o Error and information handling ---------------------------------------o//
  35.  
  36. void ErrorMsg (short index)
  37. {
  38.     Str255 theString;
  39.  
  40.     InitCursor ();
  41.     GetIndString (theString, 128, index);//o get error msg from rsrc.
  42.     ParamText (theString, "\p", "\p", "\p");
  43.     StopAlert (128, NIL);            //o show error dialog.
  44. }
  45.  
  46. //o-----------------------------------------------------------------------o//
  47.  
  48. void ShowInfo ()                    //o About FontShow….
  49. {
  50.     DialogPtr     myDialog;
  51.     EventRecord    myEvent;
  52.  
  53.     myDialog=GetNewDialog (ABOUT, NIL, (WindowPtr)-1);//o get dialog from rsrc.
  54.     DrawDialog (myDialog);                    //o draw it.
  55.     while (!GetNextEvent (keyDownMask|autoKeyMask|mDownMask, &myEvent))
  56.         SystemTask ();                //o wait until event.
  57.     DisposDialog (myDialog);        //o dispose of dialog.
  58. }
  59.  
  60. //o-----------------------------------------------------------------------o//
  61. //o Sample text output ---------------------------------------------------o//
  62. //o-----------------------------------------------------------------------o//
  63.  
  64. Boolean BreakCheck ()                //o check for break after each page.
  65. {
  66.     EventRecord theEvent;
  67.     WindowPtr     whichWindow;
  68.  
  69.      for (;;) 
  70.      {    //o mouse clicks in menu bar ignored for comp. with EZ-Menu 
  71.          //o *sigh*.
  72.          while (!GetNextEvent (keyDownMask|autoKeyMask|mDownMask, &theEvent))
  73.             SystemTask ();                //o wait until event.
  74.          if ((theEvent.what == keyDown) || (theEvent.what == autoKey)) //o keyDown?.
  75.             return ((char)BitAnd (theEvent.message, charCodeMask) == '.' &&
  76.                         BitAnd (theEvent.modifiers, cmdKey) != 0);
  77.             //o if Cmd-dot    then TRUE else FALSE.
  78.          else
  79.              if (theEvent.what == mouseDown && FindWindow (theEvent.where, &whichWindow)!=inMenuBar)
  80.                      return (FALSE);//o click not in MenuBar.
  81.      }
  82. }
  83.  
  84. //o-----------------------------------------------------------------------o//
  85.  
  86. void DrawPage (short i)//o draw sample page.
  87. {
  88.     myFontInfo     *myFont;
  89.     short             lineNr=0;
  90.     char             *pos;
  91.     short from, to, nr;
  92.     short c, x, y;
  93.     short            lineH;
  94.     short            wMax;
  95.     Str255        myStr;
  96.     char            buf[4];
  97.  
  98.     myFont= *fontHdl;                //o Block is already locked !.
  99.     lineH=myFont[i].lineHeight;
  100.     wMax=myFont[i].widMax;
  101.  
  102.     TextFont (0);                    //o draw font name in system font.
  103.     TextSize (12);                    //o and 12 pt size.
  104.     MoveTo (TOPH, TOPF);            //o    goto title line.
  105.     DrawString (myFont[i].fontName);//o draw font name.
  106.     DrawString ("\p (ID=");
  107. //    stci_d (buf, myFont[i].fontNum, 3);    //o KAL toox it out - couldn't...
  108.     DrawString (CtoPstr (buf));
  109.     DrawString ("\p) ");
  110. //    stci_d (buf, fontSize, 2);            //o find in headers.
  111.     DrawString (CtoPstr (buf));
  112.     DrawString ("\p point.");
  113.  
  114.     TextFont (myFont[i].fontNum);//o set font.
  115.     TextSize (fontSize);
  116.  
  117.     if (RealFont (myFont[i].fontNum, fontSize)) 
  118.     {
  119.         //o sample text.
  120.         if (opt == SAMPLE || prFlag) 
  121.         {
  122.             lineNr++;
  123.             HLock (myText);
  124.             from=nr=to=0;
  125.             pos=*myText;
  126.             
  127.             //o break text into single lines.
  128.             while (to<textLength) 
  129.             {    
  130.                 to++;
  131.                 
  132.                 //o search for CR.
  133.                 if ((*pos++ == 0x0D) || (to == textLength)) 
  134.                 {
  135.                     MoveTo (TOPH, TOPV + lineH*lineNr++ );//o go to next line.
  136.                     DrawText (*myText, from, to-from);//o draw line.
  137.                     from = to;
  138.                 }
  139.             }
  140.             HUnlock (myText);
  141.         }
  142.  
  143.         //o sample string.
  144.         if (opt == STRING || prFlag ) 
  145.         {
  146.             for (i=1;i;i++) 
  147.             {
  148.                 lineNr++;
  149.                 GetIndString (myStr, STRINGS, i);
  150.                 if (*myStr) 
  151.                 {
  152.                     MoveTo (TOPH, TOPV + lineH*lineNr++ );
  153.                     DrawString (myStr);
  154.                 }
  155.                 else break;
  156.             }
  157.         }
  158.         //o character map.
  159.         if (opt == MAP || prFlag) 
  160.         {
  161.             lineNr++;
  162.             
  163.             //o only printable characters.
  164.             for (c = 32; c < 256; c++) 
  165.             {    
  166.                 //o 32 char per row.
  167.                 if (c % 32 == 0) 
  168.                 {
  169.                     x = TOPH;
  170.                     y = TOPV + lineH*lineNr++;    //o new line.
  171.                 }
  172.                 MoveTo (x, y);
  173.                 if (isprint (c))                //o skip 127.
  174.                     DrawChar ((char)c);
  175.                 else
  176.                     DrawChar (32);
  177.                 x += wMax;                    //o makes a pretty output.
  178.             }
  179.         }
  180.     }
  181.     else 
  182.         {
  183.             TextSize (12);
  184.             TextFont (0);
  185.             DrawString ("\p Font size not available.");
  186.     }
  187. }
  188.  
  189. //o-----------------------------------------------------------------------o//
  190.  
  191. void ShowFonts ()                    //o Display fonts.
  192. {
  193.     short i;
  194.     DialogPtr    myDialog;
  195.     WindowPtr     myWindow;
  196.  
  197.     prFlag = FALSE;
  198.  
  199.     if (SelectFonts ()) 
  200.     {            //o only when select dialog is ok.
  201.         myWindow = GetNewWindow (FONTWINDOW, NIL, (WindowPtr)-1L); //o open a window.
  202.         myDialog = GetNewDialog (SCREENBREAK, NIL, myWindow);//o show break information.
  203.         DrawDialog (myDialog);
  204.         SetPort (myWindow);
  205.         HLock ((Handle) fontHdl);
  206.         for (i = 0; i < fontCount; i++) 
  207.         {
  208.             //o draw only selected fonts.
  209.             if ((*fontHdl)[i].flag)
  210.             {
  211.                 EraseRect (&myWindow->portRect);    //o    clear page.
  212.                 DrawPage (i);                        //o draw sample page.
  213.                 if (BreakCheck ())                    //o check for break.
  214.                     break;
  215.             }
  216.         }
  217.         HUnlock ((Handle) fontHdl);
  218.         DisposeWindow (myWindow);//o close window and dialog.
  219.         DisposDialog (myDialog);
  220.     }
  221. }
  222.  
  223. //o-----------------------------------------------------------------------o//
  224.  
  225. short HowMany ()
  226. {
  227.     return (((**prRecHdl).prJob.bJDocLoop == bDraftLoop) ? 
  228.                 (**prRecHdl).prJob.iCopies : 1 );
  229. }
  230.  
  231. //o-----------------------------------------------------------------------o//
  232.  
  233. void PrintFonts ()                    //o print fonts.
  234. {
  235.     TPPrPort        myPrPort;
  236.     TPrStatus    myPrStatus;
  237.     DialogPtr     mySpoolDlg, myPrintDlg;
  238.     GrafPtr        savePort;
  239.     short i;
  240.     short copies;
  241.     short             curs=0;
  242.  
  243.     prFlag=TRUE;                    //o indicate printing.
  244.  
  245.     GetPort (&savePort);            //o save old grafPort.
  246.     PrOpen ();                        //o open PrintMgr.
  247.     
  248.     //o get print job infos.
  249.     if (SelectFonts () && PrJobDialog (prRecHdl)) 
  250.     { 
  251.         //o just in case last printout was cancelled by user:.
  252.         PrintErr=noErr;
  253.         for (copies = HowMany (); copies > 0 && PrintErr == noErr; copies--) 
  254.         {
  255.             //o show status information:.
  256.             if ((**prRecHdl).prJob.bJDocLoop == bSpoolLoop) 
  257.                 DrawDialog (mySpoolDlg=GetNewDialog (SPOOLINFO, NIL, (WindowPtr)-1));
  258.             else
  259.                 DrawDialog (myPrintDlg=GetNewDialog (PRINTINFO, NIL, (WindowPtr)-1));
  260.  
  261.             SetCursor (*myCursor[curs++]);//o we´re busy (rotating watch.
  262.             if (curs == 4) curs = 0;
  263.  
  264.             myPrPort = PrOpenDoc (prRecHdl, NIL, NIL); //o open printPort.
  265.             SetPort ((GrafPtr) myPrPort);
  266.             TextSize (12);
  267.             HLock ((Handle) fontHdl);
  268.             for (i = 0; i < fontCount; i++) 
  269.             {
  270.  
  271.                 SetCursor (*myCursor[curs++]);//o still busy.
  272.                 if (curs == 4) curs = 0;
  273.  
  274.                 if (PrintErr == noErr) 
  275.                 {
  276.                     //o print only selected fonts.
  277.                     if ((*fontHdl)[i].flag)
  278.                     {
  279.                         PrOpenPage (myPrPort, NIL);
  280.                         if (PrintErr == noErr)
  281.                             DrawPage (i);            //o print sample page.
  282.                         PrClosePage (myPrPort);
  283.                     }
  284.                 }
  285.             }
  286.             HUnlock ((Handle) fontHdl);
  287.             PrCloseDoc (myPrPort);//o close printPort.
  288.  
  289.             //o now print spool file.
  290.             if ((**prRecHdl).prJob.bJDocLoop == bSpoolLoop && PrintErr == noErr) 
  291.             {
  292.                 DisposDialog (mySpoolDlg);
  293.                 DrawDialog (myPrintDlg=GetNewDialog (PRINTINFO, NIL, (WindowPtr)-1));
  294.                 PrPicFile (prRecHdl, NIL, NIL, NIL, &myPrStatus); //o print spool file.
  295.             }
  296.  
  297.             DisposDialog (myPrintDlg);
  298.         }
  299.  
  300.         InitCursor ();
  301.         if (PrintErr!=noErr)
  302.                 ErrorMsg (ERR_PRINT);//o something went wrong.
  303.         else
  304.             SysBeep (10);            //o notify user.
  305.         PrClose ();                    //o close printing manager.
  306.         SetPort (savePort);        //o reset old grafPort.
  307.     }
  308. }
  309.  
  310. //o-----------------------------------------------------------------------o//
  311. //o Font dialog handling -------------------------------------------------o//
  312. //o-----------------------------------------------------------------------o//
  313.  
  314. pascal void    DrawList(WindowPtr theWindow, short itemNo)
  315. {
  316.     Rect         rView;
  317.     RgnHandle     theRgn;
  318.     short         itemType;
  319.     Handle         item;
  320.     Rect         box;
  321.         
  322.     //o get rect of OK button.
  323.     GetDItem (theWindow, OK, &itemType, &item, &box); 
  324.     PenSize (3, 3);
  325.     InsetRect (&box, -4, -4);
  326.     FrameRoundRect (&box, 16, 16);    //o draw bold.
  327.     PenSize (1, 1);
  328.  
  329.     HLock ((Handle) myList);
  330.     rView= (**myList).rView;
  331.     InsetRect (&rView, -1, -1);
  332.     FrameRect (&rView);                //o frame around list.
  333.     theRgn= (*theWindow).visRgn;
  334.     LUpdate (theRgn, myList);        //o update list.
  335.     HUnlock ((Handle) myList);
  336. }
  337.  
  338. //o-----------------------------------------------------------------------o//
  339.  
  340. pascal Boolean    myFilter(DialogPtr theDialog, EventRecord *theEvent, 
  341.                                 short *itemHit)
  342. {
  343.     char     charCode;
  344.     GrafPtr savePort;
  345.     Cell     theCell;
  346.     Boolean flag;
  347.  
  348.     if ((theEvent->what == keyDown) || (theEvent->what == autoKey)) 
  349.     { 
  350.         charCode= (char)BitAnd (theEvent->message, charCodeMask);
  351.         
  352.         //o OK button is default.
  353.         if ((charCode == 13) || (charCode == 3)) 
  354.         { 
  355.             *itemHit=1;
  356.             return (TRUE);
  357.         }
  358.         else if ((charCode == '.') && (BitAnd (theEvent->modifiers, cmdKey) != 0)) 
  359.         {                                //o Cmd-dot cancels dialog.
  360.             *itemHit=2;
  361.             return (TRUE);
  362.         }
  363.         else return (FALSE);
  364.     }
  365.  
  366.     else 
  367.         {
  368.             if (*itemHit == FLIST) 
  369.             {    //o list handling.
  370.                     GetPort (&savePort);
  371.                     SetPort ((**myList).port);
  372.  
  373.                     //o convert to local coordinates.
  374.                     GlobalToLocal (& (theEvent->where));
  375.  
  376.                     //o simulate cmd key to fool default list
  377.                     //o definition routine !!!.
  378.                     flag = LClick (theEvent->where, 
  379.                                    theEvent->modifiers|cmdKey, 
  380.                                    myList);
  381.                                    
  382.                     SetPort (savePort);
  383.             }
  384.  
  385.             if (flag == TRUE) 
  386.             {        //o double click is equal to ok.
  387.                     *itemHit=1;
  388.                     return (TRUE);
  389.                 }
  390.                 else
  391.                     return (FALSE);
  392.     }
  393. }
  394.  
  395. //o-----------------------------------------------------------------------o//
  396. //o Initialize font list. ------------------------------------------------o//
  397. //o-----------------------------------------------------------------------o//
  398.  
  399. void InitList (WindowPtr theWindow)
  400. {
  401.     short             itemType;
  402.     Handle         item;
  403.     Point         csize, theCell;
  404.     Rect             rView, dataBounds;
  405.     short i;
  406.     myFontInfo     *myFont;
  407.  
  408.     GetDItem (theWindow, FLIST, &itemType, &item, &rView);
  409.     SetRect (&dataBounds, 0, 0, 1, fontCount);//o set borders of list.
  410.     SetPt (&csize, 0, 0);
  411.     rView.right-=15;                //o leave room for scroll bar.
  412.     myList=LNew (&rView, &dataBounds, csize, 0, theWindow, TRUE, FALSE, FALSE, TRUE);
  413.  
  414.     HLock ((Handle) fontHdl);
  415.     myFont= *fontHdl;
  416.  
  417.     //o fill list with entries from myFontInfo array.
  418.     for (i=0; i<fontCount;i++) 
  419.     {
  420.         SetPt (&theCell, 0, i);
  421.         LSetCell (myFont[i].fontName+1, *myFont[i].fontName, theCell, myList);
  422.         LSetSelect (myFont[i].flag, theCell, myList);//o preselect entries.
  423.     }
  424.     HUnlock ((Handle) fontHdl);
  425. }
  426.  
  427. //o-----------------------------------------------------------------------o//
  428.  
  429. Boolean SelectFonts ()            //o handle font select dialog.
  430.     short         i;
  431.     short        button;
  432.     short         itemType;
  433.     Handle         item;
  434.     Rect         box;
  435.     Cell         theCell;
  436.     Boolean     stop=FALSE;
  437.     myFontInfo     *myFont;
  438.     DialogPtr     myDialog = GetNewDialog (FONTLIST, NIL, (WindowPtr)-1);
  439.                                             //o Auswahl-Dialog.
  440.     
  441.     GetDialogItem(myDialog, FLIST, &itemType, &item, &box);//o get rect of list button.
  442.     SetDialogItem(myDialog, FLIST, userItem, (Handle)DrawList, &box); //o install user item.
  443.     InitList (myDialog);             //o initialize list.
  444.  
  445.     ShowWindow (myDialog);        //o make dialog visible.
  446.     while (!stop) 
  447.     {
  448.         SystemTask ();
  449.         ModalDialog (myFilter, &button);//o Dialog anzeigen.
  450.  
  451.         switch (button) 
  452.         {
  453.             case EXALL:                //o deselect all fonts.
  454.                 for (i=0; i<fontCount;i++) 
  455.                 {
  456.                         SetPt (&theCell, 0, i);
  457.                         LSetSelect (FALSE, theCell, myList);
  458.                 }
  459.             break;
  460.  
  461.             case INALL:                //o select all fonts.
  462.                 for (i=0; i<fontCount;i++) 
  463.                 {
  464.                         SetPt (&theCell, 0, i);
  465.                         LSetSelect (TRUE, theCell, myList);
  466.                 }
  467.             break;
  468.  
  469.             case REVERT:            //o revert to old. 
  470.                 HLock ((Handle) fontHdl);
  471.                 myFont= *fontHdl;
  472.                 for (i=0; i<fontCount;i++) 
  473.                 {
  474.                         SetPt (&theCell, 0, i);
  475.                         LSetSelect (myFont[i].flag, theCell, myList);
  476.                 }
  477.                 HUnlock ((Handle) fontHdl);
  478.             break;
  479.  
  480.             case Cancel:            //o dialog cancelled.
  481.                 stop=TRUE;
  482.             break;
  483.  
  484.             case OK:
  485.                  HLock ((Handle) fontHdl);
  486.                 myFont= *fontHdl;
  487.                 for (i=0; i<fontCount;i++) 
  488.                 {//o update myFontInfo array.
  489.                         SetPt (&theCell, 0, i);
  490.                         myFont[i].flag=LGetSelect (FALSE, &theCell, myList);
  491.                 }
  492.                 HUnlock ((Handle) fontHdl);
  493.                 stop=TRUE;
  494.                 break;
  495.         } //o end case.
  496.     }
  497.  
  498.     LDispose (myList);                //o dispose of list.
  499.     DisposDialog (myDialog);                    //o dispose of dialog.
  500.     return ((button == OK)?TRUE:FALSE);
  501. }
  502.  
  503. //o-----------------------------------------------------------------------o//
  504. //o Event handling -------------------------------------------------------o//
  505. //o-----------------------------------------------------------------------o//
  506.  
  507. void HandleMenu (sel)                //o handle menu bar.
  508. long sel;
  509. {
  510.     short     theItem=LoWord (sel);
  511.     Str255 name;
  512.     GrafPtr savePort;
  513.     short i;
  514.     myFontInfo *myFont;
  515.     FontInfo    fInfo;
  516.  
  517.     switch (HiWord (sel)) 
  518.     {
  519.         case APPLE_M:
  520.             GetItem (myMenus[APPLE], theItem, (StringPtr) &name);
  521.             if (theItem == INFO)        //o About….
  522.                 ShowInfo ();
  523.             else 
  524.                 {
  525.                     GetPort (&savePort);//o save old grafPort (just in case…).
  526.                     OpenDeskAcc ((StringPtr) &name);//o open DA.
  527.                     InitCursor ();        //o DA may have changed cursor.
  528.                     SetPort (savePort);    //o    DA may have changed grafPort.
  529.             }
  530.         break;
  531.  
  532.         case FILE_M:
  533.             switch (theItem) 
  534.             {
  535.                 case SETUP:
  536.                     PrOpen ();        //o open/close PrintMgr acc. to TN.
  537.                     PrStlDialog (prRecHdl);//o get page setup.
  538.                     PrClose ();
  539.                 break;
  540.  
  541.                 case DISPLAY:
  542.                     ShowFonts ();
  543.                 break;
  544.  
  545.                 case PRINT:
  546.                     PrintFonts ();
  547.                 break;
  548.  
  549.                 case QUIT:
  550.                     quit=TRUE;
  551.                 break;
  552.             }
  553.         break;
  554.  
  555.         case EDIT_M: 
  556.             SystemEdit (theItem-1); //o our application doesn´t handle EDIT menu.
  557.         break;
  558.  
  559.         case OPTIONS_M:
  560.             CheckItem (myMenus[OPTIONS], opt, FALSE); //o deselect menu item.
  561.             CheckItem (myMenus[OPTIONS], opt=theItem, TRUE); //o select new Item.
  562.         break;
  563.  
  564.         case SIZE_M:
  565.             if (theItem!=oldItem) 
  566.             {
  567.                 SetCursor (*myCursor[0]);
  568.                 CheckItem (myMenus[SIZE], oldItem, FALSE);
  569.                 CheckItem (myMenus[SIZE], oldItem=theItem, TRUE);
  570.                 switch (oldItem) 
  571.                 {
  572.                     case SIZE9:
  573.                         fontSize=9;
  574.                     break;
  575.  
  576.                     case SIZE10:
  577.                         fontSize=10;
  578.                     break;
  579.  
  580.                     case SIZE12:
  581.                         fontSize=12;
  582.                     break;
  583.  
  584.                     case SIZE14:
  585.                         fontSize=14;
  586.                     break;
  587.  
  588.                     case SIZE18:
  589.                         fontSize=18;
  590.                     break;
  591.  
  592.                     case SIZE20:
  593.                         fontSize=20;
  594.                     break;
  595.  
  596.                     case SIZE24:
  597.                         fontSize=24;
  598.                     break;
  599.                 }
  600.                 TextSize (fontSize);
  601.                 HLock ((Handle) fontHdl);
  602.                 myFont= *fontHdl;
  603.                 for (i = 0; i < fontCount; i++) 
  604.                 {
  605.                 TextFont (myFont[i].fontNum);                //o set font.
  606.                 GetFontInfo (&fInfo);                            //o and get font info.
  607.                 myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; //o calc. line height.
  608.                 myFont[i].widMax=fInfo.widMax;            //o calc. char width.
  609.             }
  610.             HUnlock ((Handle) fontHdl);
  611.             TextSize (12);
  612.             TextFont (0);
  613.             InitCursor ();
  614.         }
  615.         break;
  616.     }
  617.     HiliteMenu (0);
  618. }
  619.  
  620. //o-----------------------------------------------------------------------o//
  621.  
  622. void HandleMouseDown (theEvent)//o handle mouseDown events.
  623. EventRecord theEvent;
  624. {
  625.     WindowPtr whichWindow;
  626.  
  627.     switch (FindWindow (theEvent.where, &whichWindow )) 
  628.     {
  629.         case inDesk:
  630.             SysBeep (10);
  631.         break;
  632.  
  633.         case inMenuBar:
  634.             HandleMenu (MenuSelect (theEvent.where) ) ;
  635.         break;
  636.  
  637.         case inSysWindow:
  638.             SystemClick (&theEvent, whichWindow );
  639.         break;
  640.     } //o end switch.
  641. }
  642.  
  643. //o-----------------------------------------------------------------------o//
  644. //o MainLoop () -----------------------------------------------------------o//
  645. //o-----------------------------------------------------------------------o//
  646.  
  647. void MainLoop ()
  648. {
  649.     EventRecord theEvent;
  650.  
  651.     quit = FALSE;
  652.     while (!quit)
  653.     {
  654.         SystemTask ();
  655.         if (GetNextEvent (everyEvent, &theEvent))
  656.             switch (theEvent.what) 
  657.             {
  658.                 case mouseDown:    //o mouse click.
  659.                     HandleMouseDown (theEvent);
  660.                 break;
  661.  
  662.                 case keyDown:         //o key click.
  663.                 case autoKey:
  664.                     if ((theEvent.modifiers & cmdKey) != 0) //o we handle only Cmd-key.
  665.                         HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
  666.                 break;
  667.             } //o end switch (and if).
  668.     }//o end while.
  669. }
  670.  
  671. //o-----------------------------------------------------------------------o//
  672. //o Initialization and main () --------------------------------------------o//
  673. //o-----------------------------------------------------------------------o//
  674.  
  675. void InitMenu ()                    //o Draw menu bar.
  676. {
  677.     short i;
  678.  
  679.     myMenus[APPLE]=GetMenu (APPLE_M);
  680.     AddResMenu (myMenus[APPLE], 'DRVR');//o insert DAs in Apple-menu.
  681.  
  682.     myMenus[FILE]=GetMenu (FILE_M);
  683.     myMenus[EDIT]=GetMenu (EDIT_M);
  684.     myMenus[OPTIONS]=GetMenu (OPTIONS_M);
  685.     myMenus[SIZE]=GetMenu (SIZE_M);
  686.  
  687.     CheckItem (myMenus[OPTIONS], opt = SAMPLE, TRUE); //o preselect "sample text".
  688.     CheckItem (myMenus[SIZE], oldItem=SIZE12, TRUE); //o preselect font size 12.
  689.     fontSize=12;
  690.  
  691.     for (i = 0; i < 5; InsertMenu (myMenus[i++], 0))
  692.         ;
  693.     DrawMenuBar ();
  694. }
  695.  
  696. //o-----------------------------------------------------------------------o//
  697.  
  698. void InitText ()                    //o get sample text from rsrc.
  699. {
  700.     myText = GetResource ('TEXT', 128);
  701.     textLength= (short)SizeResource (myText);
  702. }
  703.  
  704. //o-----------------------------------------------------------------------o//
  705.  
  706. void InitPrint ()
  707. {
  708.     PrOpen ();
  709.     prRecHdl= (THPrint)NewHandle (sizeof (TPrint));//o get new print record.
  710.     if (prRecHdl)
  711.         PrintDefault (prRecHdl);    //o validate it.
  712.     else
  713.         ErrorMsg (ERR_NOROOM);
  714.     PrClose ();
  715. }
  716.  
  717. //o-----------------------------------------------------------------------o//
  718.  
  719. void BuildList ()                    //o build font list.
  720. {
  721.     register     short i;
  722.     myFontInfo    *myFont;
  723.     FontInfo     fInfo;
  724.     MenuHandle    tempMenu;
  725.  
  726.     //o Get a fake menu; use the Menu Manager to fill it with font names - 
  727.     //o the Menu Manager sorts entries alphabetically !.
  728.  
  729.     tempMenu = NewMenu (TEMP_M, "\px");
  730.     AddResMenu (tempMenu, 'FONT');
  731.     fontCount=CountMItems (tempMenu);
  732.  
  733.     //o dynamic allocation of heap space for myFontInfo array.
  734.     if (fontHdl= (myFontInfo **)NewHandle (fontCount*sizeof (myFontInfo))) 
  735.     {
  736.         HLock ((Handle) fontHdl);
  737.         TextSize (12);
  738.         myFont= *fontHdl;
  739.         for (i=0;i<fontCount;i++) 
  740.         {
  741.                 GetItem (tempMenu, i+1, myFont[i].fontName);//o get font names.
  742.             GetFNum (myFont[i].fontName, &myFont[i].fontNum);//o get font numbers.
  743.             TextFont (myFont[i].fontNum);                //o set font.
  744.             GetFontInfo (&fInfo);                            //o and get font info.
  745.             myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; //o calc. line height.
  746.             myFont[i].widMax=fInfo.widMax;            //o calc. char width.
  747.             myFont[i].flag=TRUE;                            //o preselect font.
  748.         }
  749.         HUnlock ((Handle) fontHdl);
  750.     }
  751.     else
  752.         ErrorMsg (ERR_NOROOM);
  753.  
  754.     DisposeMenu (tempMenu);//o We’re done with the menu; dispose of it.
  755.     TextFont (0);    //o and reset system font.
  756. }
  757.  
  758. //o-----------------------------------------------------------------------o//
  759.  
  760. void InitThings ()
  761. {
  762.     DialogPtr myDialog;
  763.     short i;
  764.  
  765.     InitGraf (&qd.thePort); 
  766.     MaxApplZone ();                        //o we want it all, and we want it NOW !.
  767.     MoreMasters ();                    //o extra pointer blocks at the bottom of the heap.
  768.     MoreMasters ();                    //o this is 5 X 64 master pointers.
  769.     MoreMasters ();
  770.     MoreMasters ();
  771.     MoreMasters ();
  772.  
  773.     InitFonts ();            //o startup the font manager.
  774.     InitWindows ();                        //o startup the window manager.
  775.     InitMenus ();                    //o startup the menu manager.
  776.     TEInit ();                        //o startup the text edit manager.
  777.     InitDialogs (0);                //o startup the dialog manager.
  778.  
  779.     FlushEvents (everyEvent, 0);
  780.  
  781. //o get the cursors we use and lock them down - no clutter.
  782.  
  783.     myCursor[0] = GetCursor (watchCursor);
  784.     myCursor[1] = GetCursor (128);
  785.     myCursor[2] = GetCursor (129);
  786.     myCursor[3] = GetCursor (130);
  787.  
  788.     for (i = 0; i < 4; i++) 
  789.     {
  790.         MoveHHi ((Handle)myCursor[i]);
  791.         HLock ((Handle)myCursor[i]);
  792.     }
  793.  
  794.     //o tell user we´re busy.
  795.     myDialog = GetNewDialog (WAIT, NIL, (WindowPtr)-1); 
  796.     DrawDialog (myDialog);
  797.     InitMenu ();                        //o build menus.
  798.     InitText ();                        //o get sample text.
  799.     InitPrint ();                    //o initialize printing.
  800.     BuildList ();                    //o build font name list.
  801.     DisposDialog (myDialog);
  802.     InitCursor ();                    //o show arrow cursor.
  803. }
  804.  
  805. //o-----------------------------------------------------------------------o//
  806.  
  807. void CleanUp ()
  808. {
  809.     short i;
  810.  
  811.     if (fontHdl)
  812.         DisposHandle ((Handle) fontHdl);
  813.     if (prRecHdl)
  814.         DisposHandle ((Handle) prRecHdl);
  815.     for (i=0;i<4;i++)
  816.         HUnlock ((Handle)myCursor[i]);
  817. }
  818.  
  819. //o-----------------------------------------------------------------------o//
  820.  
  821. void main ()
  822. {
  823.     InitThings ();                            //o initializations.
  824.     if (fontHdl && prRecHdl)
  825.         MainLoop ();                            //o main routine.
  826.     CleanUp ();                                //o clean up.
  827. }
  828.  
  829. //o-----------------------------------------------------------------------o//
  830. //o Show's over, folks!
  831. //o-----------------------------------------------------------------------o//
  832.