home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Demos / C Demos / Hello / Hello.c next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  4.5 KB  |  226 lines  |  [TEXT/KAHL]

  1. /*
  2.  * TransSkel version of "hello, world."
  3.  *
  4.  * Presents a window with "Hello, world." displayed centered in the window
  5.  * when the window is active, and "Goodbye, world." when the window is
  6.  * inactive (e.g., when the application is suspended, or when the "About"
  7.  * alert comes up).
  8.  *
  9.  * 04 Feb 94 Version 1.00, Paul DuBois
  10.  * 21 Feb 94
  11.  * - Updated for TransSkel 3.11.
  12.  */
  13.  
  14. # include    "TransSkel.h"
  15.  
  16. /* strings that appear in window */
  17.  
  18. # define    hello    "\pHello, world."
  19. # define    goodbye    "\pGoodbye, world."
  20.  
  21. /* window positioning ratios */
  22.  
  23. # define    hRatio    FixRatio (1, 2)
  24. # define    vRatio    FixRatio (1, 5)
  25.  
  26. /*
  27.  * Resource numbers
  28.  */
  29.  
  30. # define    aboutAlrtRes    1000                    /* About box */
  31. # define    fileMenuNum        (skelAppleMenuID + 1)    /* File menu */
  32.  
  33.  
  34. /* file menu item numbers */
  35.  
  36. typedef enum
  37. {
  38.     quit = 1
  39. };
  40.  
  41.  
  42. static MenuHandle    fileMenu;
  43.  
  44. /* current phrase */
  45.  
  46. static StringPtr    phrase = hello;
  47.  
  48.  
  49. /* -------------------------------------------------------------------- */
  50. /*                        Menu handling procedures                        */
  51. /* -------------------------------------------------------------------- */
  52.  
  53.  
  54. /*
  55.  * Handle selection of "About Hello..." item from Apple menu
  56.  */
  57.  
  58. static pascal void
  59. DoAppleMenu (short item)
  60. {
  61.     (void) SkelAlert (aboutAlrtRes, SkelDlogFilter (nil, true),
  62.                                         skelPositionOnParentDevice);
  63.     SkelRmveDlogFilter ();
  64. }
  65.  
  66.  
  67. /*
  68.  * Process selection from File menu.
  69.  */
  70.  
  71. static pascal void
  72. DoFileMenu (short item)
  73. {
  74.     switch (item)
  75.     {
  76.     case quit:
  77.         SkelStopEventLoop ();
  78.         break;
  79.     }
  80. }
  81.  
  82.  
  83. /*
  84.  * Initialize menus.  Tell TransSkel to process the Apple menu
  85.  * automatically, and associate the proper procedure with the
  86.  * File menu.
  87.  *
  88.  * \311 is the ellipsis character.
  89.  */
  90.  
  91. static void
  92. SetupMenus (void)
  93. {
  94.     SkelApple ("\pAbout Hello\311", DoAppleMenu);
  95.     fileMenu = NewMenu (fileMenuNum, "\pFile");
  96.     AppendMenu (fileMenu, "\pQuit/Q");
  97.     (void) SkelMenu (fileMenu, DoFileMenu, nil, false, false);
  98.  
  99.     DrawMenuBar ();
  100. }
  101.  
  102.  
  103. /* -------------------------------------------------------------------- */
  104. /*                    Window handling procedures                            */
  105. /* -------------------------------------------------------------------- */
  106.  
  107.  
  108. /*
  109.  * Draw grow box of window in lower right hand corner
  110.  */
  111.  
  112.  
  113. static void
  114. DrawGrowBox (WindowPtr w)
  115. {
  116. RgnHandle    oldClip;
  117. Rect        r;
  118.  
  119.     r = w->portRect;
  120.     r.left = r.right - 15;        /* draw only in corner */
  121.     r.top = r.bottom - 15;
  122.     oldClip = NewRgn ();
  123.     GetClip (oldClip);
  124.     ClipRect (&r);
  125.     DrawGrowIcon (w);
  126.     SetClip (oldClip);
  127.     DisposeRgn (oldClip);
  128. }
  129.  
  130.  
  131. static pascal void
  132. Update (Boolean resized)
  133. {
  134. WindowPtr    w;
  135. Rect        r;
  136. short        h, v;
  137.  
  138.     GetPort (&w);
  139.     r = w->portRect;
  140.     EraseRect (&r);
  141.     h = (r.left + r.right - StringWidth (phrase)) / 2;
  142.     v = (r.top + r.bottom) / 2;
  143.     MoveTo (h, v);
  144.     DrawString (phrase);
  145.     DrawGrowBox (w);
  146. }
  147.  
  148.  
  149. /*
  150.  * Normally the grow icon is drawn on a change of activation state, but since
  151.  * the entire portRect is invalidated here, everything will be redrawn by
  152.  * Update(), anyway, including the grow box.  So don't bother here.
  153.  */
  154.  
  155. static pascal void
  156. Activate (Boolean active)
  157. {
  158. WindowPtr    w;
  159.  
  160.     GetPort (&w);
  161.     phrase = (active ? hello : goodbye);
  162.     InvalRect (&w->portRect);
  163. }
  164.  
  165.  
  166. static pascal void
  167. Clobber (void)
  168. {
  169. WindowPtr    w;
  170.  
  171.     GetPort (&w);
  172.     DisposeWindow (w);
  173. }
  174.  
  175.  
  176. /*
  177.  * Read window from resource file and install handler for it.  Mouse
  178.  * and key clicks are ignored.  There is no close proc since the window
  179.  * doesn't have a close box.  There is no idle proc since nothing is
  180.  * done while the window is in front (all the things that are done are
  181.  * handled by TransSkel).
  182.  */
  183.  
  184. static void
  185. WindInit (void)
  186. {
  187. WindowPtr    w;
  188. Rect    bounds;
  189.  
  190.     SetRect (&bounds, 0, 0, 200, 100);
  191.     if (SkelQuery (skelQHasColorQD))
  192.     {
  193.         w = NewCWindow (nil, &bounds, "\pHowdy", false,
  194.                         documentProc+8, (WindowPtr) -1, false, 0L);
  195.     }
  196.     else
  197.     {
  198.         w = NewWindow (nil, &bounds, "\pHowdy", false,
  199.                         documentProc+8, (WindowPtr) -1, false, 0L);
  200.     }
  201.     SkelPositionWindow (w, skelPositionOnMainDevice,
  202.                                     FixRatio (1, 2), FixRatio (1,5));
  203.     (void) SkelWindow (w, nil, nil, Update, Activate, nil,
  204.                     Clobber, nil, false);
  205.     TextFont (0);    /* select system font in default size */
  206.     TextSize (0);
  207.     SelectWindow (w);
  208.     ShowWindow (w);
  209. }
  210.  
  211.  
  212. /* -------------------------------------------------------------------- */
  213. /*                                    Main                                */
  214. /* -------------------------------------------------------------------- */
  215.  
  216.  
  217. int
  218. main (void)
  219. {
  220.     SkelInit ((SkelInitParamsPtr) nil);    /* initialize */
  221.     SetupMenus ();                        /* install menu handlers */
  222.     WindInit();                            /* install window handler */
  223.     SkelEventLoop ();                    /* loop 'til Quit selected */
  224.     SkelCleanup ();                        /* clean up */
  225. }
  226.