home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / gadtoolsmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  4.9 KB  |  162 lines

  1. /* The functions used to set up and control GadTools menus are discussed
  2.  * in the next section.  Before looking at these functions in detail, it
  3.  * may be helpful to look at a brief example.
  4.  */
  5.  
  6. /* gadtoolsmenu.c
  7. ** Example showing the basic usage of the menu system with a window.
  8. ** Menu layout is done with GadTools, as is recommended for applications.
  9. **
  10. ** Compiled with SAS C v5.10a
  11. ** lc -b1 -cfistq -v -y gadtoolsmenu
  12. ** blink FROM LIB:c.o gadtoolsmenu.o TO gadtoolsmenu LIB LIB:lc.lib LIB:amiga.lib
  13. */
  14.  
  15. #define INTUI_V36_NAMES_ONLY
  16.  
  17. #include <exec/types.h>
  18. #include <intuition/intuition.h>
  19. #include <intuition/intuitionbase.h>
  20. #include <libraries/gadtools.h>
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/gadtools_protos.h>
  24. #include <clib/intuition_protos.h>
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef LATTICE
  29. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  30. int chkabort(void) { return(0); }  /* really */
  31. #endif
  32.  
  33. struct Library *GadToolsBase;
  34. struct IntuitionBase *IntuitionBase;
  35.  
  36. struct NewMenu mynewmenu[] =
  37.     {
  38.         { NM_TITLE, "Project",    0 , 0, 0, 0,},
  39.         {  NM_ITEM, "Open...",   "O", 0, 0, 0,},
  40.         {  NM_ITEM, "Save",      "S", 0, 0, 0,},
  41.         {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  42.         {  NM_ITEM, "Print",      0 , 0, 0, 0,},
  43.         {   NM_SUB, "Draft",      0 , 0, 0, 0,},
  44.         {   NM_SUB, "NLQ",        0 , 0, 0, 0,},
  45.         {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  46.         {  NM_ITEM, "Quit...",   "Q", 0, 0, 0,},
  47.  
  48.         { NM_TITLE, "Edit",       0 , 0, 0, 0,},
  49.         {  NM_ITEM, "Cut",       "X", 0, 0, 0,},
  50.         {  NM_ITEM, "Copy",      "C", 0, 0, 0,},
  51.         {  NM_ITEM, "Paste",     "V", 0, 0, 0,},
  52.         {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  53.         {  NM_ITEM, "Undo",      "Z", 0, 0, 0,},
  54.  
  55.         {   NM_END, NULL,         0 , 0, 0, 0,},
  56.     };
  57.  
  58.  
  59. /*
  60. ** Watch the menus and wait for the user to select the close gadget
  61. ** or quit from the menus.
  62. */
  63. VOID handle_window_events(struct Window *win, struct Menu *menuStrip)
  64. {
  65. struct IntuiMessage *msg;
  66. SHORT done;
  67. UWORD menuNumber;
  68. UWORD menuNum;
  69. UWORD itemNum;
  70. UWORD subNum;
  71. struct MenuItem *item;
  72.  
  73. done = FALSE;
  74. while (FALSE == done)
  75.     {
  76.     /* we only have one signal bit, so we do not have to check which
  77.     ** bit broke the Wait().
  78.     */
  79.     Wait(1L << win->UserPort->mp_SigBit);
  80.  
  81.     while ( (FALSE == done) &&
  82.             (NULL != (msg = (struct IntuiMessage *)GetMsg(win->UserPort))))
  83.         {
  84.         switch (msg->Class)
  85.             {
  86.             case IDCMP_CLOSEWINDOW:
  87.                 done = TRUE;
  88.                 break;
  89.             case IDCMP_MENUPICK:
  90.                 menuNumber = msg->Code;
  91.                 while ((menuNumber != MENUNULL) && (!done))
  92.                     {
  93.                     item = ItemAddress(menuStrip, menuNumber);
  94.  
  95.                     /* process the item here! */
  96.                     menuNum = MENUNUM(menuNumber);
  97.                     itemNum = ITEMNUM(menuNumber);
  98.                     subNum  = SUBNUM(menuNumber);
  99.  
  100.                     /* stop if quit is selected. */
  101.                     if ((menuNum == 0) && (itemNum == 5))
  102.                         done = TRUE;
  103.  
  104.                     menuNumber = item->NextSelect;
  105.                     }
  106.                 break;
  107.             }
  108.         ReplyMsg((struct Message *)msg);
  109.         }
  110.     }
  111. }
  112.  
  113.  
  114. /*
  115. ** Open all of the required libraries and set-up the menus.
  116. */
  117. VOID main(int argc, char *argv[])
  118. {
  119. struct Window *win;
  120. APTR *my_VisualInfo;
  121. struct Menu *menuStrip;
  122.  
  123. /* Open the Intuition Library */
  124. IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37);
  125. if (IntuitionBase != NULL)
  126.     {
  127.     /* Open the gadtools Library */
  128.     GadToolsBase = OpenLibrary("gadtools.library", 37);
  129.     if (GadToolsBase != NULL)
  130.         {
  131.         if (NULL != (win = OpenWindowTags(NULL,
  132.                             WA_Width,  400,       WA_Activate,    TRUE,
  133.                             WA_Height, 100,       WA_CloseGadget, TRUE,
  134.                             WA_Title,  "Menu Test Window",
  135.                             WA_IDCMP,  IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
  136.                             TAG_END)))
  137.             {
  138.             if (NULL != (my_VisualInfo = GetVisualInfo(win->WScreen, TAG_END)))
  139.                 {
  140.                 if (NULL != (menuStrip = CreateMenus(mynewmenu, TAG_END)))
  141.                     {
  142.                     if (LayoutMenus(menuStrip, my_VisualInfo, TAG_END))
  143.                         {
  144.                         if (SetMenuStrip(win, menuStrip))
  145.                             {
  146.                             handle_window_events(win,menuStrip);
  147.  
  148.                             ClearMenuStrip(win);
  149.                             }
  150.                         FreeMenus(menuStrip);
  151.                         }
  152.                     }
  153.                 FreeVisualInfo(my_VisualInfo);
  154.                 }
  155.             CloseWindow(win);
  156.             }
  157.         CloseLibrary((struct Library *)GadToolsBase);
  158.         }
  159.     CloseLibrary((struct Library *)IntuitionBase);
  160.     }
  161. }
  162.