home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / nlmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  8.8 KB  |  393 lines

  1. /*
  2.  * nlmenu.c
  3.  *
  4.  * Example showing use of Intuition and GadTools V39 to get NewLook
  5.  * menus.  Shows use of custom font for menu items as well as the
  6.  * simpler default case (using the screen's font).
  7.  *
  8.  * This program works unmodified under V37.
  9.  *
  10.  * (C) Copyright 1991, Commodore-Amiga, Inc.
  11.  *
  12.  * Executables based on this information may be used in software
  13.  * for Commodore Amiga computers.  All other rights reserved.
  14.  * This information is provided "as is"; no warranties are made.  All
  15.  * use is at your own risk. No liability or responsibility is assumed.
  16.  *
  17.  */
  18.  
  19. #include <exec/types.h>
  20. #include <intuition/intuition.h>
  21. #include <intuition/gadgetclass.h>
  22. #include <intuition/imageclass.h>
  23. #include <libraries/gadtools.h>
  24.  
  25. #include <clib/exec_protos.h>
  26. #include <clib/graphics_protos.h>
  27. #include <clib/intuition_protos.h>
  28. #include <clib/diskfont_protos.h>
  29. #include <clib/gadtools_protos.h>
  30.  
  31. void printf(STRPTR,...);
  32. int stcd_l(char *, long *);
  33. void exit(int);
  34.  
  35. /*------------------------------------------------------------------------*/
  36.  
  37. void main(int, char *[]);
  38. void bail_out(int);
  39. BOOL HandleMenuEvent(UWORD);
  40.  
  41. /* These definitions will appear in V39 include files.  Until then,
  42.  * they're available locally
  43.  */
  44.  
  45. #ifndef    WA_NewLookMenus
  46. #define WA_NewLookMenus    (WA_Dummy + 0x30)
  47. #endif
  48.  
  49. #ifndef    WA_AmigaKey
  50. #define WA_AmigaKey    (WA_Dummy + 0x31)
  51. #endif
  52.  
  53. #ifndef SYSIA_ReferenceFont
  54. #define SYSIA_ReferenceFont    (IA_Dummy + 0x19)
  55. #endif
  56.  
  57. #ifndef MENUCHECK
  58. #define    MENUCHECK    (0x10L)
  59. #endif
  60.  
  61. #ifndef AMIGAKEY
  62. #define AMIGAKEY    (0x11L)
  63. #endif
  64.  
  65. #ifndef GTMN_Checkmark
  66. #define GTMN_Checkmark         GT_TagBase+65
  67. #endif
  68.  
  69. #ifndef GTMN_AmigaKey
  70. #define GTMN_AmigaKey         GT_TagBase+66
  71. #endif
  72.  
  73. #ifndef GTMN_NewLookMenus
  74. #define GTMN_NewLookMenus        GT_TagBase+67
  75. #endif
  76.  
  77. /*------------------------------------------------------------------------*/
  78.  
  79. /* Here we specify what we want our menus to contain: */
  80.  
  81. struct NewMenu mynewmenu[] =
  82. {
  83.     { NM_TITLE, "Project",      0 , 0, 0, 0,},
  84.     {  NM_ITEM, "Open...",     "O", 0, 0, 0,},
  85.     {  NM_ITEM, "Save",      0 , 0, 0, 0,},
  86.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  87.     {  NM_ITEM, "Print",      0 , 0, 0, 0,},
  88.     {   NM_SUB, "Draft",      0 , CHECKIT|CHECKED, ~1, 0,},
  89.     {   NM_SUB, "NLQ",      0 , CHECKIT, ~2, 0,},
  90.     {   NM_SUB, "Laser",      0 , CHECKIT, ~4, 0,},
  91.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  92.     {  NM_ITEM, "Quit...",     "Q", 0, 0, 0,},
  93.  
  94.     { NM_TITLE, "Edit",      0 , 0, 0, 0,},
  95.     {  NM_ITEM, "Cut",     "X", 0, 0, 0,},
  96.     {  NM_ITEM, "Copy",     "C", 0, 0, 0,},
  97.     {  NM_ITEM, "Paste",     "V", 0, 0, 0,},
  98.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  99.     {  NM_ITEM, "Undo",     "Z", 0, 0, 0,},
  100.  
  101.     {   NM_END, 0,          0 , 0, 0, 0,},
  102. };
  103.  
  104. /*------------------------------------------------------------------------*/
  105.  
  106. struct TextAttr customtattr;
  107. struct TextAttr *tattr;
  108.  
  109. /*------------------------------------------------------------------------*/
  110.  
  111. extern struct Library *SysBase;
  112. struct GfxBase *GfxBase = NULL;
  113. struct IntuitionBase *IntuitionBase = NULL;
  114. struct Library *GadToolsBase = NULL;
  115. struct Library *DiskfontBase = NULL;
  116. struct Screen *mysc = NULL;
  117. struct Menu *menu = NULL;
  118. struct Window *mywin = NULL;
  119. struct TextFont *customfont = NULL;
  120. void *vi = NULL;
  121. struct DrawInfo *dri = NULL;
  122. struct Image *checkimage = NULL;
  123. struct Image *amigakeyimage = NULL;
  124.  
  125. /*------------------------------------------------------------------------*/
  126.  
  127. BOOL terminated;
  128.  
  129. /*------------------------------------------------------------------------*/
  130.  
  131. void main(argc, argv)
  132.  
  133. int argc;
  134. char *argv[];
  135.  
  136. {
  137.     struct IntuiMessage *imsg;
  138.     ULONG imsgClass;
  139.     UWORD imsgCode;
  140.     struct TagItem moretags[3];
  141.  
  142.     terminated = FALSE;
  143.  
  144.     if (argc == 2)
  145.     {
  146.     printf("Usage:\n\tnlmenu\nor\n\tnlmenu fontname.font fontsize\n");
  147.     printf("Example:\n\tnlmenu courier.font 15\n");
  148.     bail_out(0);
  149.     }
  150.     /* Open all libraries: */
  151.  
  152.     if (!(GfxBase = (struct GfxBase *)
  153.     OpenLibrary("graphics.library", 36L)))
  154.     bail_out(20);
  155.  
  156.     if (!(IntuitionBase = (struct IntuitionBase *)
  157.     OpenLibrary("intuition.library", 36L)))
  158.     bail_out(20);
  159.  
  160.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  161.     bail_out(20);
  162.  
  163.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 36L)))
  164.     bail_out(20);
  165.  
  166.     if (!(mysc = LockPubScreen(NULL)))
  167.     bail_out(20);
  168.  
  169.     if (!(vi = GetVisualInfo(mysc,
  170.     TAG_DONE)))
  171.     bail_out(20);
  172.  
  173.     if (!(dri = GetScreenDrawInfo(mysc)))
  174.     bail_out(20);
  175.  
  176.     if (argc < 3)
  177.     {
  178.     /* Default to screen's font */
  179.     tattr = mysc->Font;
  180.     }
  181.     else
  182.     {
  183.     LONG longval;
  184.  
  185.     customtattr.ta_Style = 0;
  186.     customtattr.ta_Flags = 0;
  187.     /* Attempt to use the font specified on the command line: */
  188.     customtattr.ta_Name = argv[1];
  189.     /* Convert decimal size to long */
  190.     stcd_l(argv[2], &longval);
  191.     customtattr.ta_YSize = longval;
  192.     tattr = &customtattr;
  193.     if (!(customfont = OpenDiskFont(tattr)))
  194.     {
  195.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  196.         customtattr.ta_YSize);
  197.         bail_out(20);
  198.     }
  199.  
  200.     /* Generate a custom checkmark whose size matches
  201.      * our custom font
  202.      */
  203.     if (!( checkimage = NewObject(NULL, "sysiclass",
  204.         SYSIA_DrawInfo, dri,
  205.         SYSIA_Which, MENUCHECK,
  206.         SYSIA_ReferenceFont, customfont, /* If NULL, uses dri_Font */
  207.         TAG_DONE) ))
  208.     {
  209.         bail_out(20);
  210.     }
  211.  
  212.     /* Generate a custom Amiga-key image whose size matches
  213.      * our custom font
  214.      */
  215.     if (!( amigakeyimage = NewObject(NULL, "sysiclass",
  216.         SYSIA_DrawInfo, dri,
  217.         SYSIA_Which, AMIGAKEY,
  218.         SYSIA_ReferenceFont, customfont, /* If NULL, uses dri_Font */
  219.         TAG_DONE) ))
  220.     {
  221.         bail_out(20);
  222.     }
  223.     }
  224.  
  225.     /* Build and layout menus using the right font: */
  226.     if (!(menu = CreateMenus(mynewmenu,
  227.     TAG_DONE)))
  228.     {
  229.     bail_out(20);
  230.     }
  231.  
  232.     /* These are only necessary if a custom font was supplied... */
  233.     moretags[0].ti_Tag = GTMN_Checkmark;
  234.     moretags[0].ti_Data = (ULONG) checkimage;
  235.     moretags[1].ti_Tag = GTMN_AmigaKey;
  236.     moretags[1].ti_Data = (ULONG) amigakeyimage;
  237.     moretags[2].ti_Tag = TAG_DONE;
  238.  
  239.     if (!LayoutMenus(menu, vi,
  240.     GTMN_TextAttr, tattr,
  241.     GTMN_NewLookMenus, TRUE,
  242.     (customfont ? TAG_MORE : TAG_DONE), moretags))
  243.     bail_out(20);
  244.  
  245.     /* These are only necessary if a custom font was supplied...
  246.      * Note: we re-use some of the tag-array initializations from above
  247.      */
  248.     moretags[0].ti_Tag = WA_Checkmark;
  249.     moretags[1].ti_Tag = WA_AmigaKey;
  250.  
  251.     if (!(mywin = OpenWindowTags(NULL,
  252.     WA_Width, 500,
  253.     WA_InnerHeight, 100,
  254.     WA_Top, 50,
  255.  
  256.     WA_Activate, TRUE,
  257.     WA_DragBar, TRUE,
  258.     WA_DepthGadget, TRUE,
  259.     WA_CloseGadget, TRUE,
  260.     WA_SizeGadget, TRUE,
  261.     WA_SmartRefresh, TRUE,
  262.  
  263.     /* NOTE: NOCAREREFRESH is not allowed if you use GadTools Gadgets! */
  264.     WA_NoCareRefresh, TRUE,
  265.  
  266.     WA_IDCMP, CLOSEWINDOW | MENUPICK,
  267.  
  268.     WA_MinWidth, 50,
  269.     WA_MinHeight, 50,
  270.     WA_Title, "GadTools Menu Demo",
  271.     WA_NewLookMenus, TRUE,
  272.     (customfont ? TAG_MORE : TAG_DONE), moretags)))
  273.     bail_out(20);
  274.  
  275.     SetMenuStrip(mywin, menu);
  276.  
  277.     while (!terminated)
  278.     {
  279.     Wait (1 << mywin->UserPort->mp_SigBit);
  280.     /* NOTE:  If you use GadTools gadgets, you must use GT_GetIMsg()
  281.      * and GT_ReplyIMsg() instead of GetMsg() and ReplyMsg().
  282.      * Regular GetMsg() and ReplyMsg() are safe if the only part
  283.      * of GadTools you use are menus...
  284.      */
  285.     while ((!terminated) &&
  286.         (imsg = (struct IntuiMessage *)GetMsg(mywin->UserPort)))
  287.     {
  288.         imsgClass = imsg->Class;
  289.         imsgCode = imsg->Code;
  290.         ReplyMsg((struct Message *)imsg);
  291.         switch (imsgClass)
  292.         {
  293.         case MENUPICK:
  294.             terminated = HandleMenuEvent(imsgCode);
  295.             break;
  296.  
  297.         case CLOSEWINDOW:
  298.             printf("CLOSEWINDOW.\n");
  299.             terminated = TRUE;
  300.             break;
  301.         }
  302.     }
  303.     }
  304.     bail_out(0);
  305. }
  306.  
  307. /*------------------------------------------------------------------------*/
  308.  
  309. /*/ bail_out()
  310.  *
  311.  * Function to close down or free any opened or allocated stuff, and then
  312.  * exit.
  313.  *
  314.  */
  315.  
  316. void bail_out(code)
  317.  
  318. int code;
  319.  
  320. {
  321.     if (mywin)
  322.     {
  323.     ClearMenuStrip(mywin);
  324.     CloseWindow(mywin);
  325.     }
  326.  
  327.     /* None of these two calls mind a NULL parameter, so it's not
  328.      * necessary to check for non-NULL before calling.  If we do that,
  329.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  330.      * or else we would be jumping into outer space:
  331.      */
  332.     if (GadToolsBase)
  333.     {
  334.     FreeMenus(menu);
  335.     FreeVisualInfo(vi);
  336.     CloseLibrary(GadToolsBase);
  337.     }
  338.  
  339.     if (dri)
  340.     {
  341.     FreeScreenDrawInfo( mysc, dri );
  342.     }
  343.  
  344.     if (customfont)
  345.     {
  346.     DisposeObject( amigakeyimage );
  347.     DisposeObject( checkimage );
  348.     CloseFont(customfont);
  349.     }
  350.  
  351.     if (mysc)
  352.     {
  353.     UnlockPubScreen(NULL, mysc);
  354.     }
  355.  
  356.     if (DiskfontBase)
  357.     {
  358.     CloseLibrary(DiskfontBase);
  359.     }
  360.  
  361.     if (IntuitionBase)
  362.     {
  363.     CloseLibrary(IntuitionBase);
  364.     }
  365.  
  366.     if (GfxBase)
  367.     {
  368.     CloseLibrary(GfxBase);
  369.     }
  370.  
  371.     exit(code);
  372. }
  373.  
  374.  
  375. /*------------------------------------------------------------------------*/
  376.  
  377. /*/ HandleMenuEvent()
  378.  *
  379.  * This function handles IntuiMessage events of type MENUPICK.
  380.  *
  381.  */
  382.  
  383. BOOL HandleMenuEvent(code)
  384.  
  385. UWORD code;
  386.  
  387. {
  388.     /* Your code goes here */
  389.     return(FALSE);
  390. }
  391.  
  392. /*------------------------------------------------------------------------*/
  393.