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