home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Demo Folder / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  4.6 KB  |  224 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Blob Manager Demonstration:  Handlers for standard menus (those
  3.  * that stick around all the time).  Menus that exist only when
  4.  * are particular window is in front are installed and removed by
  5.  * the handler for that window.
  6.  *
  7.  * 12 July 1986    Paul DuBois
  8.  */
  9.  
  10.  
  11. # include    "TransSkel.h"
  12.  
  13. # include    "BlobMgr.h"
  14. # include    "BlobDemo.h"
  15.  
  16.  
  17. static MenuHandle    fileMenu;
  18. static MenuHandle    editMenu;
  19. static MenuHandle    windMenu;
  20.  
  21.  
  22. /*
  23.  * wPtr is used to map Windows menu items onto the windows associated
  24.  * with each item.
  25.  */
  26.  
  27. static WindowPtr    wPtr[lastWindRes-firstWindRes];
  28. static short        wCount = 0;
  29.  
  30.  
  31. static pascal void
  32. DoAbout (short item)
  33. {
  34.     (void) SkelAlert (aboutAlrtRes, SkelDlogFilter (nil, true),
  35.                                         skelPositionOnParentDevice);
  36.     SkelRmveDlogFilter ();
  37. }
  38.  
  39.  
  40. /*
  41.  * Close all the windows, from the back forward for better esthetics.
  42.  */
  43.  
  44. void
  45. CloseAllWindows (void)
  46. {
  47. WindowPeek    w;
  48. WindowPtr    lastVis;
  49.  
  50.     for (;;)
  51.     {
  52.         lastVis = (WindowPtr) nil;
  53.         for (w = (WindowPeek) FrontWindow (); w != (WindowPeek) nil; w = w->nextWindow)
  54.         {
  55.             if (w->visible)
  56.                 lastVis = (WindowPtr) w;
  57.         }
  58.         if (lastVis == (WindowPtr) nil)
  59.             break;
  60.         SkelClose (lastVis);
  61.     }
  62. }
  63.  
  64.  
  65. static pascal void
  66. DoFileMenu (short item)
  67. {
  68. Handle        h;
  69.  
  70.     switch (item)
  71.     {
  72.         case closeWind:
  73.             SkelClose (FrontWindow ());
  74.             break;
  75.         case closeAll:
  76.             CloseAllWindows ();
  77.             break;
  78.         case getInfo:
  79.             h = GetResource ('TEXT', helpTextRes);
  80.             if (h != (Handle) nil)
  81.             {
  82.                 HLock (h);
  83.                 TextDialog (textDlogRes, h, 0, 0, true);
  84.                 HUnlock (h);
  85.                 ReleaseResource (h);
  86.             }
  87.             break;
  88.         case quit:
  89.             SkelStopEventLoop ();    /* tell SkelEventLoop () to quit */
  90.             break;
  91.     }
  92. }
  93.  
  94.  
  95. static pascal void
  96. DoEditMenu (short item)
  97. {
  98.     (void) SystemEdit (item - 1);    /* route to DA if appropriate */
  99. }
  100.  
  101.  
  102. /*
  103.  * The Windows menu is used to select a given window and bring it
  104.  * to the front.  It also makes it visible if it wasn't already.
  105.  */
  106.  
  107. static pascal void
  108. DoWindMenu (short item)
  109. {
  110.     SelectWindow (wPtr[item-1]);
  111.     ShowWindow (wPtr[item-1]);
  112. }
  113.  
  114.  
  115. /*
  116.  * Add title of each window to the Windows menu.  Save the WindowPtr for
  117.  * use later if the window is selected from the menu.
  118.  */
  119.  
  120. void
  121. AddWindowTitles (void)
  122. {
  123. WindowPeek    theWind;
  124. Str255        title;
  125.  
  126.     theWind = (WindowPeek) FrontWindow ();
  127.     while (theWind != (WindowPeek) nil)
  128.     {
  129.         GetWTitle ((WindowPtr) theWind, (StringPtr) &title);
  130.         AppendMenu (windMenu, title);
  131.         wPtr[wCount++] = (WindowPtr) theWind;
  132.         theWind = theWind->nextWindow;
  133.     }
  134. }
  135.  
  136.  
  137. /*
  138.  * Clobber proc for menu handlers
  139.  */
  140.  
  141. pascal void
  142. DoMClobber (MenuHandle theMenu)
  143. {
  144.     ReleaseResource ((Handle) theMenu);
  145. }
  146.  
  147.  
  148. /*
  149.  * Menu hook, called when mouse is clicked in menu bar.  Sets menu items
  150.  * so they are enabled/disabled properly when Menu Manager shows them to
  151.  * user.
  152.  * - Enable File/Close Window if a window is visible, disable it otherwise.
  153.  * - Enable File/Close All if a window is visible, disable it otherwise.
  154.  * - Enable Edit menu items when a DA window is in front.  Disable them
  155.  * when a demo window is in front.
  156.  * - In Windows menu, check the window that's frontmost, if any.
  157.  */
  158.  
  159. static pascal void
  160. MenuHookProc (void)
  161. {
  162. WindowPeek    wPeek;
  163. short        i;
  164.  
  165.     if ( (wPeek = (WindowPeek) FrontWindow ()) != (WindowPeek) nil)
  166.     {
  167.         EnableItem (fileMenu, closeWind);
  168.         EnableItem (fileMenu, closeAll);
  169.     }
  170.     else
  171.     {
  172.         DisableItem (fileMenu, closeWind);
  173.         DisableItem (fileMenu, closeAll);
  174.     }
  175.  
  176.     if ( (wPeek = (WindowPeek) FrontWindow ()) != (WindowPeek) nil
  177.         && wPeek->windowKind < 0)    /* DA window in front */
  178.     {
  179.         EnableItem (editMenu, editUndo);
  180.         EnableItem (editMenu, editCut);
  181.         EnableItem (editMenu, editCopy);
  182.         EnableItem (editMenu, editPaste);
  183.         EnableItem (editMenu, editClear);
  184.     }
  185.     else                            /* DA not in front */
  186.     {
  187.         DisableItem (editMenu, editUndo);
  188.         DisableItem (editMenu, editCut);
  189.         DisableItem (editMenu, editCopy);
  190.         DisableItem (editMenu, editPaste);
  191.         DisableItem (editMenu, editClear);
  192.     }
  193.  
  194.     for (i = 0; i < wCount; i++)
  195.     {
  196.         if (FrontWindow () == wPtr[i])
  197.             SetItemMark (windMenu, i+1, checkMark);
  198.         else
  199.             SetItemMark (windMenu, i+1, noMark);
  200.     }
  201. }
  202.  
  203.  
  204. /*
  205.  * Set up menus:  Tell TransSkel to make a default Apple menu handler,
  206.  * create handlers for the File, Edit and Windows menus.  Install menu
  207.  * hook to enable/disable menu items.
  208.  *
  209.  * \311 is the ellipsis character.
  210.  */
  211.  
  212. void
  213. SetupMenus (void)
  214. {
  215.     SkelApple ("\pAbout BlobMgr Demo\311", DoAbout);
  216.     fileMenu = GetMenu (fileMenuRes);
  217.     SkelMenu (fileMenu, DoFileMenu, DoMClobber, false, false);
  218.     editMenu = GetMenu (editMenuRes);
  219.     SkelMenu (editMenu, DoEditMenu, DoMClobber, false, false);
  220.     windMenu = GetMenu (windMenuRes);
  221.     SkelMenu (windMenu, DoWindMenu, DoMClobber, false, true);
  222.     SkelSetMenuHook (MenuHookProc);
  223. }
  224.