home *** CD-ROM | disk | FTP | other *** search
/ back2roots/filegate / filegate.zip / filegate / ads / adsutils / PMDEV.LHA / PopupMenuDeveloper / C / Demos / MenuIcons.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-09  |  5.5 KB  |  236 lines

  1. //
  2. // $VER: MenuIcons.c 1.1 (09.09.00)
  3. //
  4. // Popup Menu example program
  5. //
  6. // ⌐1996-1997 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // This simple example shows how to use standard BOOPSI images as menu icons.
  10. //
  11.  
  12. #include <intuition/intuition.h>
  13. #include <intuition/imageclass.h>
  14. #include <proto/intuition.h>
  15. #include <proto/exec.h>
  16. #include <clib/alib_protos.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #include <images/bitmap.h>
  21. #include <images/bevel.h>
  22. #include <images/glyph.h>
  23. #include <proto/bitmap.h>
  24. #include <proto/bevel.h>
  25. #include <proto/glyph.h>
  26.  
  27. #include <libraries/pm.h>
  28. #include <proto/pm.h>
  29.  
  30. //
  31. // Declarations for the popup menu
  32. //
  33. struct PopupMenuBase    *PopupMenuBase=NULL;
  34.  
  35. struct PopupMenu    *popupmenu=NULL;
  36.  
  37. //
  38. // Declarations for the image objects
  39. //
  40. struct Library        *BitMapBase=NULL;
  41. struct Library        *BevelBase=NULL;
  42. struct Library        *GlyphBase=NULL;
  43.  
  44. Class *bitmapclass=NULL;
  45. Class *bevelclass=NULL;
  46. Class *glyphclass=NULL;
  47.  
  48. APTR    image1=NULL;
  49. APTR    image2=NULL;
  50. APTR    image3=NULL;
  51. APTR    image4=NULL;
  52.  
  53. //
  54. // Declarations for the window
  55. //
  56. struct Window        *wnd=NULL;
  57.  
  58.  
  59. BOOL Init(void);    // Initialise everything
  60. void Cleanup(void);    // Tidy up a bit afterwards
  61. void MainLoop(void);    // Main loop
  62.  
  63. void ShowMessage(STRPTR text);    // Show a message in the window
  64.  
  65. void main()
  66. {
  67.     if(Init()) {
  68.         MainLoop();
  69.         Cleanup();
  70.     }
  71. }
  72.  
  73. struct Window *OpenMyWindow(void);
  74.  
  75. BOOL Init(void)
  76. {
  77.     if(OPEN_PM_LIB) {    /* A little macro from libraries/pm.h. */
  78.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;
  79.  
  80.         wnd=OpenMyWindow();    /* Open a little window. (declared below) */
  81.         if(wnd) {
  82.  
  83.             ShowMessage("Loading images...");
  84.  
  85.             BevelBase=OpenLibrary("images/bevel.image", 0);
  86.             if(BevelBase) {
  87.                 bevelclass=BEVEL_GetClass();
  88.                 if(bevelclass) {
  89.                     image1=NewObject(bevelclass, NULL,
  90.                         BEVEL_LabelPlace,    BVJ_IN_CENTER,
  91.                         BEVEL_Style,        BVS_BUTTON,
  92.                         BEVEL_Transparent,    TRUE,
  93.                         IA_Height,        18,
  94.                         IA_Width,        20,
  95.                         TAG_DONE);
  96.                 }
  97.             } else printf("Could not locate bevel.image!\n");
  98.  
  99.             BitMapBase=OpenLibrary("images/bitmap.image", 0);
  100.             if(BitMapBase) {
  101.                 bitmapclass=BITMAP_GetClass();
  102.                 if(bitmapclass) {
  103.                     image2=NewObject(bitmapclass, NULL,
  104.                         BITMAP_SourceFile,    "popupmenu.bsh",
  105.                         BITMAP_Screen,        wnd->WScreen,
  106.                         BITMAP_Masking,        TRUE,
  107.                         TAG_DONE);
  108.                     image4=NewObject(bitmapclass, NULL,
  109.                         BITMAP_SourceFile,    "boing.bsh",
  110.                         BITMAP_SelectSourceFile, "boingsel.bsh",
  111.                         BITMAP_Screen,        wnd->WScreen,
  112.                         BITMAP_Masking,        TRUE,
  113.                         TAG_DONE);
  114.                 }
  115.             } else printf("Could not locate bitmap.image!\n");
  116.  
  117.             GlyphBase=OpenLibrary("images/glyph.image", 0);
  118.             if(GlyphBase) {
  119.                 glyphclass=GLYPH_GetClass();
  120.                 if(glyphclass) {
  121.                     image3=NewObject(glyphclass, NULL,
  122.                         GLYPH_Glyph, GLYPH_RETURNARROW,
  123.                         IA_Height, 20,
  124.                         TAG_DONE);
  125.                 }
  126.             } else printf("Could not locate glyph.image!\n");
  127.  
  128.             if(image1 && image2 && image3 && image4)
  129.                 ShowMessage("Done.");
  130.             else
  131.                 ShowMessage("Some images could not be loaded.");
  132.  
  133.             popupmenu=PM_MakeMenu(
  134.                 PM_Item, PM_MakeItem(PM_Hidden, TRUE, TAG_DONE),
  135.                 PMItem(""),    PM_ImageUnselected,    image2,        PM_Center, TRUE, PM_NoSelect, TRUE, PMEnd,
  136.                 PM_Item, PM_MakeItem(PM_WideTitleBar, TRUE, TAG_DONE),
  137.                 PMInfo("This example shows how to use"), PMEnd,
  138.                 PMInfo("BOOPSI image objects."), PMEnd,
  139.                 PMBar,    PMEnd,
  140.                 PMItem("SYS:Classes/images/bevel.image"),    PM_IconUnselected,    image1,        PMEnd,
  141.                 PMItem("SYS:Classes/images/glyph.image"),    PM_IconUnselected,     image3,        PMEnd,
  142.                 PMItem("SYS:Classes/images/bitmap.image"),    PM_IconUnselected,     image4,        PMEnd,
  143.                 PMBar,    PMEnd,
  144.                 PMItem("Quit"),    PM_UserData, 5,    PMEnd,
  145.               PMEnd;
  146.  
  147.             if(popupmenu) {
  148.                 return TRUE;
  149.             }
  150.  
  151.         }
  152.  
  153.     }
  154.  
  155.     return FALSE;
  156. }
  157.  
  158. void Cleanup(void)
  159. {
  160.     if(PopupMenuBase) {
  161.         if(popupmenu) {
  162.             PM_FreePopupMenu(popupmenu);
  163.         }
  164.         CLOSE_PM_LIB;
  165.     }
  166.     if(wnd) CloseWindow(wnd);
  167.     if(image1) DisposeObject(image1);
  168.     if(image2) DisposeObject(image2);
  169.     if(image2) DisposeObject(image3);
  170.     if(image4) DisposeObject(image4);
  171.     if(BitMapBase) CloseLibrary(BitMapBase);
  172.     if(BevelBase) CloseLibrary(BevelBase);
  173.     if(GlyphBase) CloseLibrary(GlyphBase);
  174. }
  175.  
  176. struct Window *OpenMyWindow(void)
  177. {
  178.  
  179.     /* Open the window. I'm keeping this code here just to make the other */
  180.     /* code more readable. */
  181.  
  182.     return OpenWindowTags(NULL,
  183.             WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS,    // Open a little window
  184.             WA_RMBTrap,    TRUE,
  185.             WA_DragBar,    TRUE,
  186.             WA_Width,    350,
  187.             WA_Height,    50,
  188.             WA_Left,    0,
  189.             WA_Top,        15,
  190.             WA_Title,    "SimpleMenu",
  191.             WA_CloseGadget,    TRUE,
  192.         TAG_DONE);
  193. }
  194.  
  195. void MainLoop(void)
  196. {
  197.     BOOL            r=TRUE;    /* Keep running until this gets FALSE */
  198.     struct IntuiMessage    *im,imsg;
  199.  
  200.     /* A standard event loop follows: */
  201.     while(r) {
  202.         WaitPort(wnd->UserPort);
  203.         while((im=(struct IntuiMessage *)GetMsg(wnd->UserPort))) {
  204.             CopyMem(im,&imsg,sizeof(struct IntuiMessage));
  205.             ReplyMsg((struct Message *)im);
  206.  
  207.             switch(imsg.Class) {
  208.                 case IDCMP_CLOSEWINDOW: r=FALSE; break;
  209.                 case IDCMP_MOUSEBUTTONS:
  210.                     if(imsg.Code==MENUDOWN) {
  211.                         r=PM_OpenPopupMenu(wnd,
  212.                             PM_Menu,    popupmenu,
  213.                             TAG_DONE);
  214.  
  215.                         /* If r is 5, we want to quit: */
  216.                             
  217.                         if(r==5) r=0;
  218.                         else r=1;
  219.                     }
  220.                     break;
  221.             }
  222.         }
  223.     }
  224. }
  225.  
  226. void ShowMessage(STRPTR text)
  227. {
  228.     SetDrMd(wnd->RPort, 0);
  229.     SetAPen(wnd->RPort, 0);
  230.     RectFill(wnd->RPort, wnd->BorderLeft+5, wnd->BorderTop+5, wnd->Width-wnd->BorderRight-wnd->BorderLeft,
  231.         wnd->BorderTop+wnd->RPort->Font->tf_YSize+5);
  232.     SetAPen(wnd->RPort, 1);
  233.     Move(wnd->RPort, wnd->BorderLeft+5, wnd->BorderTop+5+wnd->RPort->Font->tf_Baseline);
  234.     Text(wnd->RPort, text, strlen(text));
  235. }
  236.