home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / simplemenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  6.9 KB  |  228 lines

  1. ;/* simplemenu.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 simplemenu.c
  3. Blink FROM LIB:c.o,simplemenu.o TO simplemenu LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. ** simplemenu.c: how to use the menu system with a window under all OS versions.
  7. */
  8. #define INTUI_V36_NAMES_ONLY
  9.  
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <graphics/text.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/intuitionbase.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/graphics_protos.h>
  18. #include <clib/intuition_protos.h>
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #ifdef LATTICE
  24. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  25. int chkabort(void) { return(0); }  /* really */
  26. #endif
  27.  
  28. /*  These values are based on the ROM font Topaz8. Adjust these  */
  29. /*  values to correctly handle the screen's current font.        */
  30. #define MENWIDTH  (56+8)  /* Longest menu item name * font width */
  31.                           /* + 8 pixels for trim                 */
  32. #define MENHEIGHT (10)    /* Font height + 2 pixels              */
  33.  
  34. struct Library *GfxBase;
  35. struct Library *IntuitionBase;
  36.  
  37. /* To keep this example simple, we'll hard-code the font used for menu */
  38. /* items.  Algorithmic layout can be used to handle arbitrary fonts.   */
  39. /* Under Release 2, GadTools provides font-sensitive menu layout.      */
  40. /* Note that we still must handle fonts for the menu headers.          */
  41. struct TextAttr Topaz80 =
  42. {
  43.     "topaz.font", 8, 0, 0
  44. };
  45.  
  46. struct IntuiText menuIText[] =
  47. {
  48.     { 0, 1, JAM2, 0, 1, &Topaz80, "Open...",  NULL },
  49.     { 0, 1, JAM2, 0, 1, &Topaz80, "Save",     NULL },
  50.     { 0, 1, JAM2, 0, 1, &Topaz80, "Print \273",  NULL },
  51.     { 0, 1, JAM2, 0, 1, &Topaz80, "Draft",    NULL },
  52.     { 0, 1, JAM2, 0, 1, &Topaz80, "NLQ",      NULL },
  53.     { 0, 1, JAM2, 0, 1, &Topaz80, "Quit",     NULL }
  54. };
  55.  
  56. struct MenuItem submenu1[] =
  57. {
  58.     { /* Draft  */
  59.     &submenu1[1], MENWIDTH-2,  -2 ,            MENWIDTH, MENHEIGHT,
  60.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  61.     0, (APTR)&menuIText[3], NULL, NULL, NULL, NULL
  62.     },
  63.     { /* NLQ    */
  64.     NULL,         MENWIDTH-2, MENHEIGHT-2, MENWIDTH, MENHEIGHT,
  65.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  66.     0, (APTR)&menuIText[4], NULL, NULL, NULL, NULL
  67.     }
  68. };
  69.  
  70. struct MenuItem menu1[] =
  71. {
  72.     { /* Open... */
  73.     &menu1[1], 0, 0,            MENWIDTH, MENHEIGHT,
  74.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  75.     0, (APTR)&menuIText[0], NULL, NULL, NULL, NULL
  76.     },
  77.     { /* Save    */
  78.     &menu1[2], 0,  MENHEIGHT ,  MENWIDTH, MENHEIGHT,
  79.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  80.     0, (APTR)&menuIText[1], NULL, NULL, NULL, NULL
  81.     },
  82.     { /* Print   */
  83.     &menu1[3], 0, 2*MENHEIGHT , MENWIDTH, MENHEIGHT,
  84.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  85.     0, (APTR)&menuIText[2], NULL, NULL, &submenu1[0] , NULL
  86.     },
  87.     { /* Quit    */
  88.     NULL, 0, 3*MENHEIGHT , MENWIDTH, MENHEIGHT,
  89.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  90.     0, (APTR)&menuIText[5], NULL, NULL, NULL, NULL
  91.     },
  92. };
  93.  
  94. /* We only use a single menu, but the code is generalizable to */
  95. /* more than one menu.                                         */
  96. #define NUM_MENUS 1
  97.  
  98. STRPTR menutitle[NUM_MENUS] =  {   "Project"   };
  99.  
  100. struct Menu menustrip[NUM_MENUS] =
  101. {
  102.     {
  103.     NULL,                    /* Next Menu          */
  104.     0, 0,                    /* LeftEdge, TopEdge, */
  105.     0, MENHEIGHT,            /* Width, Height,     */
  106.     MENUENABLED,             /* Flags              */
  107.     NULL,                    /* Title              */
  108.     &menu1[0]                /* First item         */
  109.     }
  110. };
  111.  
  112. struct NewWindow mynewWindow =
  113. {
  114. 40,40, 300,100, 0,1, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
  115. WFLG_DRAGBAR | WFLG_ACTIVATE | WFLG_CLOSEGADGET, NULL,NULL,
  116. "Menu Test Window", NULL,NULL,0,0,0,0,WBENCHSCREEN
  117. };
  118.  
  119. /* our function prototypes */
  120. VOID handleWindow(struct Window *win, struct Menu *menuStrip);
  121.  
  122. /*      Main routine.         */
  123. /*                            */
  124. VOID main(int argc, char **argv)
  125. {
  126. struct Window *win=NULL;
  127. UWORD left, m;
  128.  
  129. /* Open the Graphics Library */
  130. GfxBase = OpenLibrary("graphics.library",33);
  131. if (GfxBase)
  132.     {
  133.     /* Open the Intuition Library */
  134.     IntuitionBase = OpenLibrary("intuition.library", 33);
  135.     if (IntuitionBase)
  136.         {
  137.         if ( win = OpenWindow(&mynewWindow) )
  138.             {
  139.             left = 2;
  140.             for (m = 0; m < NUM_MENUS; m++)
  141.                 {
  142.                 menustrip[m].LeftEdge = left;
  143.                 menustrip[m].MenuName = menutitle[m];
  144.                 menustrip[m].Width = TextLength(&win->WScreen->RastPort,
  145.                     menutitle[m], strlen(menutitle[m])) + 8;
  146.                 left += menustrip[m].Width;
  147.                 }
  148.             if (SetMenuStrip(win, menustrip))
  149.                 {
  150.                 handleWindow(win, menustrip);
  151.                 ClearMenuStrip(win);
  152.                 }
  153.             CloseWindow(win);
  154.             }
  155.         CloseLibrary(IntuitionBase);
  156.         }
  157.     CloseLibrary(GfxBase);
  158.     }
  159. }
  160.  
  161. /*
  162. **   Wait for the user to select the close gadget.
  163. */
  164. VOID handleWindow(struct Window *win, struct Menu *menuStrip)
  165. {
  166. struct IntuiMessage *msg;
  167. SHORT done;
  168. ULONG class;
  169. UWORD menuNumber;
  170. UWORD menuNum;
  171. UWORD itemNum;
  172. UWORD subNum;
  173. struct MenuItem *item;
  174.  
  175. done = FALSE;
  176. while (FALSE == done)
  177.     {
  178.     /* we only have one signal bit, so we do not have to check which
  179.     ** bit broke the Wait().
  180.     */
  181.     Wait(1L << win->UserPort->mp_SigBit);
  182.  
  183.     while ( (FALSE == done) &&
  184.             (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  185.         {
  186.         class = msg->Class;
  187.         if(class == IDCMP_MENUPICK)   menuNumber = msg->Code;
  188.  
  189.         switch (class)
  190.             {
  191.             case IDCMP_CLOSEWINDOW:
  192.                 done = TRUE;
  193.                 break;
  194.             case IDCMP_MENUPICK:
  195.                 while ((menuNumber != MENUNULL) && (!done))
  196.                     {
  197.                     item = ItemAddress(menuStrip, menuNumber);
  198.  
  199.                     /* process this item
  200.                     ** if there were no sub-items attached to that item,
  201.                     ** SubNumber will equal NOSUB.
  202.                     */
  203.                     menuNum = MENUNUM(menuNumber);
  204.                     itemNum = ITEMNUM(menuNumber);
  205.                     subNum  = SUBNUM(menuNumber);
  206.  
  207.                     /* Note that we are printing all values, even things
  208.                     ** like NOMENU, NOITEM and NOSUB.  An application should
  209.                     ** check for these cases.
  210.                     */
  211.                     printf("IDCMP_MENUPICK: menu %d, item %d, sub %d\n",
  212.                         menuNum, itemNum, subNum);
  213.  
  214.                     /* This one is the quit menu selection...
  215.                     ** stop if we get it, and don't process any more.
  216.                     */
  217.                     if ((menuNum == 0) && (itemNum == 4))
  218.                         done = TRUE;
  219.  
  220.                     menuNumber = item->NextSelect;
  221.                     }
  222.                 break;
  223.             }
  224.         ReplyMsg((struct Message *)msg);
  225.         }
  226.     }
  227. }
  228.