home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / ansicdemo.lha / ANSI-C / gadtools / menu1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  8.4 KB  |  369 lines

  1. /*
  2. ** menu1.c:  Menu example with font-sensitivity.
  3. **
  4. ** (C) Copyright 1990, Commodore-Amiga, Inc.
  5. **
  6. ** Executables based on this information may be used in software
  7. ** for Commodore Amiga computers.  All other rights reserved.
  8. ** This information is provided "as is"; no warranties are made.  All
  9. ** use is at your own risk. No liability or responsibility is assumed.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <exec/types.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/gadgetclass.h>
  17. #include <libraries/gadtools.h>
  18.  
  19. #include <pragma/exec_lib.h>
  20. #include <pragma/graphics_lib.h>
  21. #include <pragma/intuition_lib.h>
  22. #include <pragma/diskfont_lib.h>
  23. #include <pragma/gadtools_lib.h>
  24.  
  25. //void printf(STRPTR,...);
  26. //int stcd_l(char *, long *);
  27. //void exit(int);
  28.  
  29. /*------------------------------------------------------------------------*/
  30.  
  31. int main(int, char *[]);
  32. void bail_out(int);
  33. BOOL HandleMenuEvent(UWORD);
  34. BOOL OpenFunc(UWORD);
  35. BOOL SaveFunc(UWORD);
  36. BOOL PrintFunc(UWORD);
  37. BOOL QuitFunc(UWORD);
  38.  
  39. /*------------------------------------------------------------------------*/
  40.  
  41. /* Here we specify what we want our menus to contain: */
  42.  
  43. struct NewMenu mynewmenu[] =
  44. {
  45.     { NM_TITLE, "Project",      0 , 0, 0, 0,},
  46.     {  NM_ITEM, "Open...",     "O", 0, 0, OpenFunc,},
  47.     {  NM_ITEM, "Save",      0 , 0, 0, SaveFunc,},
  48.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  49.     {  NM_ITEM, "Print",      0 , 0, 0, 0,},
  50.     {   NM_SUB, "Draft",      0 , 0, 0, PrintFunc,},
  51.     {   NM_SUB, "NLQ",      0 , 0, 0, PrintFunc,},
  52.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  53.     {  NM_ITEM, "Quit...",     "Q", 0, 0, QuitFunc,},
  54.  
  55.     { NM_TITLE, "Edit",      0 , 0, 0, 0,},
  56.     {  NM_ITEM, "Cut",     "X", 0, 0, 0,},
  57.     {  NM_ITEM, "Copy",     "C", 0, 0, 0,},
  58.     {  NM_ITEM, "Paste",     "V", 0, 0, 0,},
  59.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  60.     {  NM_ITEM, "Undo",     "Z", 0, 0, 0,},
  61.  
  62.     {   NM_END, 0,          0 , 0, 0, 0,},
  63. };
  64.  
  65. /*------------------------------------------------------------------------*/
  66.  
  67. struct TextAttr customtattr;
  68. struct TextAttr *tattr;
  69.  
  70. /*------------------------------------------------------------------------*/
  71.  
  72. extern struct Library *SysBase;
  73. struct GfxBase *GfxBase = NULL;
  74. struct IntuitionBase *IntuitionBase = NULL;
  75. struct Library *GadToolsBase = NULL;
  76. struct Library *DiskfontBase = NULL;
  77. struct Screen *mysc = NULL;
  78. struct Menu *menu = NULL;
  79. struct Window *mywin = NULL;
  80. struct TextFont *customfont = NULL;
  81. void *vi = NULL;
  82.  
  83. /*------------------------------------------------------------------------*/
  84.  
  85. BOOL terminated;
  86.  
  87. /*------------------------------------------------------------------------*/
  88.  
  89. int main( int argc, char *argv[])
  90. {
  91.     struct IntuiMessage *imsg;
  92.     ULONG imsgClass;
  93.     UWORD imsgCode;
  94.     ULONG errorcode = 0;
  95.  
  96.     terminated = FALSE;
  97.  
  98.     if (argc == 2)
  99.     {
  100.     printf("Usage:\n\tmenu1\nor\n\tmenu1 fontname.font fontsize\n");
  101.     printf("Example:\n\tmenu1 courier.font 15\n");
  102.     bail_out(0);
  103.     }
  104.     /* Open all libraries: */
  105.  
  106.     if (!(GfxBase = (struct GfxBase *)
  107.     OpenLibrary("graphics.library", 36L)))
  108.     bail_out(20);
  109.  
  110.     if (!(IntuitionBase = (struct IntuitionBase *)
  111.     OpenLibrary("intuition.library", 36L)))
  112.     bail_out(20);
  113.  
  114.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  115.     bail_out(20);
  116.  
  117.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 36L)))
  118.     bail_out(20);
  119.  
  120.     if (!(mysc = LockPubScreen(NULL)))
  121.     bail_out(20);
  122.  
  123.     if (!(vi = GetVisualInfo(mysc,
  124.     TAG_DONE)))
  125.     bail_out(20);
  126.  
  127.     customtattr.ta_Style = 0;
  128.     customtattr.ta_Flags = 0;
  129.     if (argc < 3)
  130.     {
  131.     /* Default to screen's font */
  132.     tattr = mysc->Font;
  133.     }
  134.     else
  135.     {
  136.     LONG longval;
  137.  
  138.     /* Attempt to use the font specified on the command line: */
  139.     customtattr.ta_Name = argv[1];
  140.     /* Convert decimal size to long */
  141.     longval = atol( argv[2]);
  142.     customtattr.ta_YSize = longval;
  143.     tattr = &customtattr;
  144.     if (!(customfont = OpenDiskFont(tattr)))
  145.     {
  146.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  147.         customtattr.ta_YSize);
  148.         bail_out(20);
  149.     }
  150.     }
  151.  
  152.     /* Build and layout menus using the right font: */
  153.     if (!(menu = CreateMenus(mynewmenu,
  154.     GTMN_FrontPen, 0,
  155.     GTMN_SecondaryError, &errorcode,
  156.     TAG_DONE)))
  157.     {
  158.     switch ( errorcode )
  159.     {
  160.         case GTMENU_INVALID:
  161.         /* Bad ordering of menu elements in the NewMenu structure. */
  162.         printf("Invalid menu structure!\n");
  163.         break;
  164.  
  165.         case GTMENU_NOMEM:
  166.         printf("Out of memory in CreateMenus()!\n");
  167.         break;
  168.  
  169.         default:
  170.         printf("Failed to create menus for unknown reason\n");
  171.         break;
  172.     }
  173.     bail_out(20);
  174.     }
  175.  
  176.     if (errorcode == GTMENU_TRIMMED)
  177.     {
  178.     /* This condition does not cause failure.  Your menu will
  179.      * be trimmed instead.  (This won't happen in the example, since
  180.      * the maximum number of menus, items, or sub-items is not
  181.      * exceeded.  The test is included to demonstrate its use.
  182.      */
  183.     printf(" Too many menu elements -- some have been trimmed off\n");
  184.     }
  185.  
  186.     if (!LayoutMenus(menu, vi,
  187.     GTMN_TextAttr, tattr,
  188.     TAG_DONE))
  189.     bail_out(20);
  190.  
  191.     if (!(mywin = OpenWindowTags(NULL,
  192.     WA_Width, 500,
  193.     WA_InnerHeight, 100,
  194.  
  195.     WA_Activate, TRUE,
  196.     WA_DragBar, TRUE,
  197.     WA_DepthGadget, TRUE,
  198.     WA_CloseGadget, TRUE,
  199.     WA_SizeGadget, TRUE,
  200.     WA_SimpleRefresh, TRUE,
  201.  
  202.     /* NOTE: NOCAREREFRESH is not allowed if you use GadTools Gadgets! */
  203.     WA_NoCareRefresh, TRUE,
  204.  
  205.     WA_IDCMP, CLOSEWINDOW | MENUPICK,
  206.  
  207.     WA_MinWidth, 50,
  208.     WA_MinHeight, 50,
  209.     WA_Title, "GadTools Menu Demo",
  210.  
  211.     TAG_DONE)))
  212.     bail_out(20);
  213.  
  214.     SetMenuStrip(mywin, menu);
  215.  
  216.     while (!terminated)
  217.     {
  218.     Wait (1 << mywin->UserPort->mp_SigBit);
  219.     /* NOTE:  If you use GadTools gadgets, you must use GT_GetIMsg()
  220.      * and GT_ReplyIMsg() instead of GetMsg() and ReplyMsg().
  221.      */
  222.     while ((!terminated) && (imsg = (struct IntuiMessage *)GetMsg(mywin->UserPort)))
  223.     {
  224.         imsgClass = imsg->Class;
  225.         imsgCode = imsg->Code;
  226.         ReplyMsg( ( struct Message *)imsg);
  227.         switch (imsgClass)
  228.         {
  229.         case MENUPICK:
  230.             terminated = HandleMenuEvent(imsgCode);
  231.             break;
  232.  
  233.         case CLOSEWINDOW:
  234.             printf("CLOSEWINDOW.\n");
  235.             terminated = TRUE;
  236.             break;
  237.         }
  238.     }
  239.     }
  240.     bail_out(0);
  241. return 0;
  242. }
  243.  
  244. /*------------------------------------------------------------------------*/
  245.  
  246. /*/ bail_out()
  247.  *
  248.  * Function to close down or free any opened or allocated stuff, and then
  249.  * exit.
  250.  *
  251.  */
  252.  
  253. void bail_out( int code)
  254. {
  255.     if (mywin)
  256.     {
  257.     ClearMenuStrip(mywin);
  258.     CloseWindow(mywin);
  259.     }
  260.  
  261.     /* None of these two calls mind a NULL parameter, so it's not
  262.      * necessary to check for non-NULL before calling.  If we do that,
  263.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  264.      * or else we would be jumping into outer space:
  265.      */
  266.     if (GadToolsBase)
  267.     {
  268.     FreeMenus(menu);
  269.     FreeVisualInfo(vi);
  270.     CloseLibrary(GadToolsBase);
  271.     }
  272.  
  273.     if (customfont)
  274.     {
  275.     CloseFont(customfont);
  276.     }
  277.  
  278.     if (mysc)
  279.     {
  280.     UnlockPubScreen(NULL, mysc);
  281.     }
  282.  
  283.     if (DiskfontBase)
  284.     {
  285.     CloseLibrary( ( struct Library *)DiskfontBase);
  286.     }
  287.  
  288.     if (IntuitionBase)
  289.     {
  290.     CloseLibrary( ( struct Library *)IntuitionBase);
  291.     }
  292.  
  293.     if (GfxBase)
  294.     {
  295.     CloseLibrary( ( struct Library *)GfxBase);
  296.     }
  297.  
  298.     exit(code);
  299. }
  300.  
  301.  
  302. /*------------------------------------------------------------------------*/
  303.  
  304. /*/ HandleMenuEvent()
  305.  *
  306.  * This function handles IntuiMessage events of type MENUPICK.  It
  307.  * demonstrates one of the best uses for the MenuItem UserData field
  308.  * provided by GadTools, namely a place to store pointers-to-functions.
  309.  *
  310.  */
  311.  
  312. BOOL HandleMenuEvent( UWORD code)
  313. {
  314.     struct MenuItem *item;
  315.     BOOL terminated = FALSE;
  316.  
  317.     printf("MENUPICK:  ");
  318.     /* Get all menu events including NextEvents until a terminating
  319.      * selection is made (such as Quit)
  320.      */
  321.     while ((code != MENUNULL) && (!terminated))
  322.     {
  323.     item = ItemAddress(menu, code);
  324.     code = item->NextSelect;
  325.     }
  326.     printf("\n");
  327.  
  328.     return(terminated);
  329. }
  330.  
  331. /*------------------------------------------------------------------------*/
  332.  
  333. BOOL OpenFunc( UWORD code)
  334. {
  335.     printf("OpenFunc called.  ");
  336.     return(FALSE);
  337. }
  338.  
  339. /*------------------------------------------------------------------------*/
  340.  
  341. BOOL SaveFunc( UWORD code)
  342. {
  343.     printf("SaveFunc called.  ");
  344.     return(FALSE);
  345. }
  346. /*------------------------------------------------------------------------*/
  347.  
  348. BOOL PrintFunc( UWORD code)
  349. {
  350.     printf("PrintFunc called ");
  351.     if (code == FULLMENUNUM(0, 3, 0))
  352.     {
  353.     printf("(Draft).  ");
  354.     }
  355.     else
  356.     {
  357.     printf("(NLQ).  ");
  358.     }
  359.     return(FALSE);
  360. }
  361. /*------------------------------------------------------------------------*/
  362.  
  363. BOOL QuitFunc( UWORD code)
  364. {
  365.     printf("QuitFunc called.  ");
  366.     return(TRUE);
  367. }
  368. /*------------------------------------------------------------------------*/
  369.