home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / VerticalString 1.0.2 / VerticalString.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  8.0 KB  |  345 lines  |  [TEXT/CWIE]

  1. // VerticalString
  2. // version 1.0.2
  3. // ported to CodeWarrior by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951201
  5.  
  6. #define over qd.screenBits.bounds.right
  7. #define down qd.screenBits.bounds.bottom
  8.  
  9. MenuHandle    appleMenu, fileMenu, editMenu;
  10.  
  11. enum {
  12.     appleID = 1,
  13.     fileID,
  14.     editID
  15. };
  16.  
  17. enum {
  18.     openItem = 1,
  19.     closeItem,
  20.     quitItem = 4
  21. };
  22.  
  23.  
  24. WindowPtr    shell_window, about_window;
  25. Rect        dragRect;
  26. Rect        windowBounds, about_bounds; 
  27. Rect        strRect ={ 5, 20, 364, 40 };
  28. short            width = 1;
  29.  
  30. // Prototypes.
  31.  
  32. void SetUpWindow (void);
  33. void DoAbout (void);
  34. void SetUpMenus (void);
  35. void VerticalString (Rect word_rect, Str255 word_string);
  36. void StyleTheText (short whatFont, short whatStyle, short whatSize);
  37. void DrawTheStrings (short active);
  38. void AdjustMenus (void);
  39. static enable (MenuHandle menu, short item, short ok);
  40. void HandleMenu (long mSelect);
  41. void InitMacintosh (void);
  42. void HandleMouseDown (EventRecord *theEvent);
  43. void HandleEvent (void);
  44. void main (void);
  45.  
  46. void SetUpWindow (void)
  47. {
  48.     dragRect = qd.screenBits.bounds;
  49.     
  50.     SetRect (&windowBounds, 2, 22, over - 4, down - 4);
  51.     
  52.     shell_window = NewWindow (0L, &windowBounds, "\pKen's Shell", true, 
  53.                               plainDBox, (WindowPtr) -1L, true, 0);
  54.     SetPort (shell_window);
  55. }
  56.  
  57. void SetUpMenus (void)
  58. {
  59.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  60.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  61.     InsertMenu (editMenu = NewMenu (editID, "\pEdit"), 0);
  62.     DrawMenuBar ();
  63.     AppendMenu (appleMenu, "\pAbout VerticalString");
  64.     AddResMenu (appleMenu, 'DRVR');
  65.     AppendMenu (fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
  66.     AppendMenu (editMenu, "\pUndo/Z; (-;Cut/X;Copy/C;Paste/V;Clear");
  67. }
  68.  
  69. void VerticalString (Rect word_rect, Str255 word_string)
  70. {
  71.     #define center 5    // center of label is 'center' pixels from left of rect.
  72.     
  73.     short numChars, leading, stringCenter, x, curVert;
  74.     FontInfo fInfo;
  75.     
  76.     GetFontInfo (&fInfo);
  77.     leading = fInfo.ascent + fInfo.descent + fInfo.leading;
  78.     stringCenter = word_rect.left - center;
  79.  
  80. //    ForeColor (redColor);    //• Color here and not elsewhere for same.
  81.     curVert = word_rect.top + leading;    // primed for first character.
  82.     numChars = strlen (word_string);
  83.     for (x = 1; x < numChars; x++)
  84.     {
  85.         MoveTo (stringCenter - (CharWidth (word_string[x]) / 2), 
  86.                 curVert);
  87.         DrawChar ((char) word_string[x]);
  88.         curVert = curVert + leading;
  89.     }
  90. }
  91.  
  92. void StyleTheText (short whatFont, short whatStyle, short whatSize)
  93. {
  94.     TextFont (whatFont);
  95.     TextFace (whatStyle);
  96.     TextSize (whatSize);
  97. }
  98.  
  99. void DrawTheStrings (short active)
  100. {
  101.     short        color = true;
  102.     
  103.     SetPort (shell_window);
  104.     
  105.     ForeColor (blueColor);    // oldStyle color.
  106.     StyleTheText (monaco, 0, 9);
  107.     VerticalString (strRect, "\pMy name is Long but this is not\0");
  108.     OffsetRect (&strRect, 20, 0);
  109.     VerticalString (strRect, "\pChinese writing.");
  110.     
  111.     ForeColor (redColor);
  112.     StyleTheText (newYork, outline, 14);
  113.     OffsetRect (&strRect, 40, 0);
  114.     VerticalString (strRect, "\pSuperman\0");
  115.     
  116.     ForeColor (blackColor);
  117.     StyleTheText (monaco, 0, 9);
  118.     OffsetRect (&strRect, 20, 0);
  119.     VerticalString (strRect, "\pStrange visitor from another\0");
  120.     OffsetRect (&strRect, 20, 0);
  121.     VerticalString (strRect, "\pplanet who came to Earth with\0");
  122.     OffsetRect (&strRect, 20, 0);
  123.     VerticalString (strRect, "\ppowers and abilities far beyond\0");
  124.     OffsetRect (&strRect, 20, 0);
  125.     VerticalString (strRect, "\pthose of mortal men. Then went\0");
  126.     StyleTheText (0, 0, 12);
  127.     OffsetRect (&strRect, 20, 0);
  128.     VerticalString (strRect, "\pto Chicago!\0");
  129.     
  130.     ForeColor (magentaColor);
  131.     StyleTheText (monaco, 0, 9);
  132.     OffsetRect (&strRect, 40, 0);
  133.     VerticalString (strRect, "\pThe VerticalString routine came\0");
  134.     OffsetRect (&strRect, 20, 0);
  135.     VerticalString (strRect, "\pfrom SplatMaster Pascal source,\0");
  136.     OffsetRect (&strRect, 20, 0);
  137.     VerticalString (strRect, "\pby Jon Benton, and ported to C\0");
  138.     OffsetRect (&strRect, 20, 0);
  139.     VerticalString (strRect, "\pby Kenneth A. Long in mid 1992.\0");
  140.     OffsetRect (&strRect, 20, 0);
  141.     VerticalString (strRect, "\pIn May, 1994 it was put in this\0");
  142.     OffsetRect (&strRect, 20, 0);
  143.     VerticalString (strRect, "\pdemo, for YOUR enjoyment as an.\0");
  144.  
  145.     StyleTheText (monaco, bold, 9);
  146.     OffsetRect (&strRect, 20, 0);
  147.     VerticalString (strRect, "\pitty bitty bytes(tm) product.\0");
  148.     
  149.     StyleTheText (monaco, 0, 9);
  150.     ForeColor (greenColor);
  151.     OffsetRect (&strRect, 40, 0);
  152.     VerticalString (strRect, "\pVertical strings are excellent\0");
  153.     OffsetRect (&strRect, 20, 0);
  154.     VerticalString (strRect, "\pfor labeling vertical scrolls.\0");
  155.     
  156.     ForeColor (blackColor);
  157.     OffsetRect (&strRect, 40, 0);
  158.     VerticalString (strRect, "\pBullseye was used for the shell.\0");
  159.     OffsetRect (&strRect, 20, 0);
  160.     VerticalString (strRect, "\pJon Benton wrote SplatMaster.\0");
  161.     OffsetRect (&strRect, 20, 0);
  162.     VerticalString (strRect, "\pVertical String Demo is PD.\0");
  163.     StyleTheText (0, 0, 12);
  164. }
  165.  
  166. void AdjustMenus (void)
  167. {
  168.     register WindowPeek wp = (WindowPeek) FrontWindow ();
  169.     short kind = wp ? wp->windowKind : 0;
  170.     Boolean DA = kind < 0;
  171.     
  172.     enable (editMenu, 1, DA);
  173.     enable (editMenu, 3, DA);
  174.     enable (editMenu, 4, DA);
  175.     enable (editMenu, 5, DA);
  176.     enable (editMenu, 6, DA);
  177.     
  178.     enable (fileMenu, openItem, ! ((WindowPeek) shell_window)->visible);
  179.     enable (fileMenu, closeItem, DA || ((WindowPeek) shell_window)->visible);
  180. }
  181.  
  182. static enable (MenuHandle menu, short item, short ok)
  183. {
  184.     if (ok)
  185.         EnableItem (menu, item);
  186.     else
  187.         DisableItem (menu, item);
  188. }
  189.  
  190. void HandleMenu (long mSelect)
  191. {
  192.     short        menuID = HiWord (mSelect);
  193.     short        menuItem = LoWord (mSelect);
  194.     Str255        name;
  195.     GrafPtr        savePort;
  196.     WindowPeek    frontWindow;
  197.     
  198.     switch (menuID)
  199.     {
  200.         case    appleID:
  201.             if (menuItem == 1)
  202.                 ;    //• Put your DoAboutBox call here.
  203.             else
  204.                 {
  205.                     GetPort (&savePort);
  206.                     GetItem (appleMenu, menuItem, name);
  207.                     OpenDeskAcc (name);
  208.                     SetPort (savePort);
  209.             }
  210.         break;
  211.         
  212.         case    fileID:
  213.             switch (menuItem)
  214.             {
  215.                 case    openItem:
  216.                     ShowWindow (shell_window);
  217.                     SelectWindow (shell_window);
  218.                 break;
  219.                               
  220.                 case    closeItem:
  221.                     if ((frontWindow = (WindowPeek) FrontWindow ()) == 0L)
  222.                 break;
  223.             
  224.                 if (frontWindow->windowKind < 0)
  225.                     CloseDeskAcc (frontWindow->windowKind);
  226.                 else 
  227.                     if (frontWindow = (WindowPeek) shell_window)
  228.                         HideWindow (shell_window);
  229.                       break;
  230.                       
  231.                 case    quitItem:
  232.                     ExitToShell ();
  233.                 break;
  234.             }
  235.         break;
  236.                   
  237.         case    editID:
  238.             if (!SystemEdit (menuItem-1))
  239.                 SysBeep (5);
  240.         break;
  241.     }
  242. }
  243.  
  244. void InitMacintosh (void)
  245. {
  246.     MaxApplZone ();
  247.     
  248.     InitGraf (& (qd.thePort));
  249.     InitFonts ();
  250.     FlushEvents (everyEvent, 0);
  251.     InitWindows ();
  252.     InitMenus ();
  253.     TEInit ();
  254.     InitDialogs (0L);
  255.     InitCursor ();
  256. }
  257.  
  258. void HandleMouseDown (EventRecord    *theEvent)
  259. {
  260.     WindowPtr    theWindow;
  261.     short            windowCode = FindWindow (theEvent->where, &theWindow);
  262.     
  263.     switch (windowCode)
  264.     {
  265.         case inSysWindow: 
  266.             SystemClick (theEvent, theWindow);
  267.         break;
  268.     
  269.         case inMenuBar:
  270.             AdjustMenus ();
  271.             HandleMenu (MenuSelect (theEvent->where));
  272.         break;
  273.     
  274.         case inDrag:
  275.             if (theWindow == shell_window)
  276.                 DragWindow (shell_window, theEvent->where, &dragRect);
  277.         break;
  278.         
  279.         case inContent:
  280. //            if (theWindow == shell_window)
  281. //            {
  282. //                if (theWindow != FrontWindow ())
  283. //                    SelectWindow (shell_window);
  284. //                else
  285. //                    InvalRect (&shell_window->portRect);
  286. //            }
  287.         break;
  288.         
  289.         case inGoAway:
  290.             if (theWindow == shell_window && 
  291.                 TrackGoAway (shell_window, theEvent->where))
  292.                 HideWindow (shell_window);
  293.         break;
  294.     }
  295. }
  296.  
  297. void HandleEvent (void)
  298. {
  299.     short            ok;
  300.     EventRecord    theEvent;
  301.  
  302.     HiliteMenu (0);
  303.     SystemTask ();        /* Handle desk accessories */
  304.     
  305.     ok = GetNextEvent (everyEvent, &theEvent);
  306.     if (ok)
  307.     switch (theEvent.what)
  308.     {
  309.         case mouseDown:
  310.             HandleMouseDown (&theEvent);
  311.         break;
  312.             
  313.         case keyDown: 
  314.         case autoKey:
  315.         if ((theEvent.modifiers & cmdKey) != 0)
  316.         {
  317.             AdjustMenus ();
  318.             HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
  319.         }
  320.         break;
  321.             
  322.         case updateEvt:
  323.             BeginUpdate (shell_window);
  324.                 EraseRect (&windowBounds);
  325.                 DrawTheStrings (((WindowPeek) shell_window)->hilited);
  326.             EndUpdate (shell_window);
  327.         break;
  328.         
  329.         case activateEvt:
  330.             InvalRect (&shell_window->portRect);
  331.         break;
  332.     }
  333. }
  334.  
  335. void main ()
  336. {
  337.     InitMacintosh ();
  338.     SetUpMenus ();
  339.     SetUpWindow ();
  340.     
  341.     for (;;)
  342.         HandleEvent ();
  343. }
  344.  
  345.