home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / CBMDevKit3.dms / CBMDevKit3.adf / intuition / nlmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  7.9 KB  |  350 lines

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