home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / sviluppo / popupmenudeveloper / c / demos / menuverify.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-07  |  4.2 KB  |  142 lines

  1. //
  2. // $VER: MenuVerify.c 1.2 (07.01.00)
  3. //
  4. // Popup Menu example program
  5. //
  6. // ©1996-1997 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // Run and click the mouse in the window!
  10. //
  11.  
  12. //
  13. // Note! Read the RKRMs for information about how to use MenuVerify
  14. // correctly. I have done it the easy way.
  15. //
  16.  
  17. #include <intuition/intuition.h>
  18.  
  19. #include <proto/intuition.h>
  20. #include <proto/gadtools.h>
  21. #include <proto/exec.h>
  22.  
  23. #include <clib/alib_protos.h>
  24.  
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28.  
  29. #include <libraries/pm.h>
  30. #include <proto/pm.h>
  31.  
  32. struct IntuitionBase    *IntuitionBase;
  33. struct GfxBase        *GfxBase;
  34. struct PopupMenuBase    *PopupMenuBase;
  35. struct Window        *w;
  36. struct Menu        *Intui_Menu=NULL;
  37. APTR VisualInfo=NULL;
  38.  
  39. struct NewMenu MyNewMenu[]=
  40. {
  41.     NM_TITLE, "Project", NULL, NULL, NULL, NULL,
  42.     NM_ITEM, "Load", "L", NULL, NULL, NULL,
  43.     NM_ITEM, "Save", "S", NULL, NULL, NULL,
  44.     NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL,
  45.     NM_ITEM, "Quit", "Q", NULL, NULL, NULL,
  46.     NM_END, NULL, NULL, NULL, NULL, NULL
  47. };
  48.  
  49. BOOL CreateIntuiMenu(void)
  50. {
  51.     if(!(VisualInfo=GetVisualInfo(w->WScreen,TAG_DONE))) return FALSE;
  52.     if(!(Intui_Menu=CreateMenus(MyNewMenu, GTMN_FrontPen, 0, TAG_DONE))) return FALSE;
  53.     LayoutMenus(Intui_Menu, VisualInfo, TAG_DONE);
  54.  
  55.     return TRUE;
  56. }
  57.  
  58. void FreeIntuiMenu(void)
  59. {
  60.     if(VisualInfo) FreeVisualInfo(VisualInfo);
  61.     if(Intui_Menu) FreeMenus(Intui_Menu);
  62. }
  63.  
  64. void main()
  65. {
  66.     BOOL r=TRUE, menu=FALSE;
  67.     struct IntuiMessage *im,imsg;
  68.     struct PopupMenu *p;
  69.  
  70.     PopupMenuBase=(struct PopupMenuBase *)OpenLibrary(POPUPMENU_NAME,POPUPMENU_VERSION);            // Open the library
  71.     if(PopupMenuBase) {
  72.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;    // We let popupmenu.library open the libraries we need
  73.         GfxBase=(struct GfxBase *)PopupMenuBase->pmb_GfxBase;            // They remain valid until the library is closed!
  74.  
  75.         p=PMMenu("Inside the window!"),
  76.             PMItem("This example shows"),    PM_NoSelect,    TRUE,    PMEnd,
  77.             PMItem("how to use popup"),    PM_NoSelect,    TRUE,    PMEnd,
  78.             PMItem("menus and intuition"),    PM_NoSelect,    TRUE,    PMEnd,
  79.             PMItem("menus in the same"),    PM_NoSelect,    TRUE,    PMEnd,
  80.             PMItem("window!"),        PM_NoSelect,    TRUE,    PMEnd,
  81.             PMTitleBar,    PMEnd,
  82.             PMItem("Quit"),    PM_UserData,    5,    PMEnd,
  83.             End;
  84.  
  85.         if(p) {
  86.             w=OpenWindowTags(NULL,    WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_MENUVERIFY|IDCMP_MENUPICK,
  87.                     WA_DragBar,    TRUE,
  88.                     WA_Width,    150,
  89.                     WA_Height,    100,
  90.                     WA_Left,    150,
  91.                     WA_Top,        0,
  92.                     WA_Title,    "MenuVerify Demo",
  93.                     WA_CloseGadget,    TRUE,
  94.                     TAG_DONE);
  95.             if(w) {
  96.                 if(CreateIntuiMenu()) {
  97.                     SetMenuStrip(w, Intui_Menu);
  98.  
  99.                     while(r) {
  100.                         WaitPort(w->UserPort);                        // Wait for a message
  101.                         while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {    // Get the message
  102.                             CopyMem(im,&imsg,sizeof(struct IntuiMessage));        // Copy the contents of it
  103.  
  104.                             if(im->Class==IDCMP_MENUVERIFY) {            // Check if the user wants to see the menu
  105.                                 if(im->MouseY>0 && im->MouseY<w->Height &&
  106.                                    im->MouseX>0 && im->MouseX<w->Width  &&    // Check if the mouse is in our window
  107.                                    im->Code==MENUHOT) {                // Read the RKRMs
  108.                                
  109.                                    im->Code=MENUCANCEL;                // Cancel the intuition menu
  110.                                
  111.                                    imsg.Class=IDCMP_MOUSEBUTTONS;        // Make sure the menu gets opened...
  112.                                    imsg.Code=MENUDOWN;                // (we fake a IDCMP_MOUSEBUTTONS message)
  113.                                 }
  114.                             }
  115.  
  116.                             ReplyMsg((struct Message *)im);                // Reply the message
  117.  
  118.                             switch(imsg.Class) {
  119.                                 case IDCMP_CLOSEWINDOW: r=FALSE; break;
  120.                                 case IDCMP_MOUSEBUTTONS:            // The user has hit a mousebutton - time to open the menu!
  121.                                     if(imsg.Code==MENUUP ||
  122.                                        imsg.Code==MENUDOWN)    {        // Open only if the right (that is the right button, in this case :) ) button was hit
  123.                                         r=(BOOL)((ULONG)(PM_OpenPopupMenu(w,
  124.                                                 PM_Menu,        p,
  125.                                                 PM_Code,        imsg.Code,    // This will ensure that the user can change
  126.                                                 TAG_DONE))-5);                // the time of apperance for the menu.
  127.                                     }
  128.                                 break;
  129.                             }
  130.                         }
  131.                     }
  132.                     ClearMenuStrip(w);
  133.                 } else printf("Can't create intuition menus!\n");
  134.                 FreeIntuiMenu();
  135.                 CloseWindow(w);
  136.             } else printf("Window error!\n");
  137.             PM_FreePopupMenu(p);
  138.         } else printf("Menu error!\n");
  139.         CloseLibrary((struct Library *)PopupMenuBase);
  140.     }
  141. }
  142.