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

  1. //
  2. // $VER: Opus4.c 1.0 (21.08.00)
  3. //
  4. // Popup Menu example program
  5. //
  6. // ⌐1996-2000 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // This example shows how to use MultiSelect, Mutual Exclude items, MenuHandlers
  10. // and how to share Item ID lists among a set of items. (saves memory and typing)
  11. //
  12.  
  13. #include <intuition/intuition.h>
  14.  
  15. #include <proto/intuition.h>
  16. #include <proto/graphics.h>
  17. #include <proto/exec.h>
  18.  
  19. #include <clib/alib_protos.h>
  20.  
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25. #include <libraries/pm.h>
  26. #include <proto/pm.h>
  27.  
  28. struct IntuitionBase    *IntuitionBase;
  29. struct GfxBase        *GfxBase;
  30. struct PopupMenuBase    *PopupMenuBase;
  31.  
  32. struct Window *w;    // This window is only needed to find out when and where the menu should appear.
  33.             // The font in this window's rastport will be used for the menu.
  34.  
  35. void ShowMessage(STRPTR text);    // Shows a message in the window
  36.  
  37. //
  38. // Some ID's for the items
  39. //
  40.  
  41. enum SortBy {
  42.     sbName=1, sbSize, sbProt, sbDate, sbComment, sbType,
  43.  
  44.     sbReverse=0x40000000    // So we can put in the same LONG, as a flag...
  45. };
  46.  
  47. #define ID_SHOWDEBUG    100
  48.  
  49. //
  50. // Here's the menu handler function.
  51. //
  52.  
  53. ULONG __saveds __asm MenuHandlerFunc(register __a0 struct Hook *hook,
  54.                      register __a2 APTR object,
  55.                      register __a1 APTR msg)
  56. {
  57.     struct PopupMenu *pm=(struct PopupMenu *)object;
  58.     static BOOL showdebug=FALSE;
  59.  
  60.     if(showdebug) printf("--- MenuHandler ---\n");
  61.  
  62.     if(pm->ID==sbReverse) {
  63.         if(pm->Flags&PM_CHECKED) {
  64.             *((ULONG *)hook->h_Data)|=sbReverse;    // Set flag
  65.  
  66.             if(showdebug) printf("Reverse order sorting was turned ON!\n");
  67.         } else {
  68.             *((ULONG *)hook->h_Data)&=~sbReverse;    // Clear flag
  69.  
  70.             if(showdebug) printf("Reverse order sorting was turned OFF!\n");
  71.         }
  72.     } else if(pm->ID>=sbName && pm->ID<=sbType) {
  73.         if(pm->Flags & PM_CHECKED) {
  74.             *((ULONG *)hook->h_Data)&=0xFFFF0000;    // We don't need all bits so we
  75.             *((ULONG *)hook->h_Data)|=pm->ID;    // preserve the high ones for flags.
  76.  
  77.             if(showdebug) printf("Sort by = %ld\n", pm->ID);
  78.         }
  79.     } else if(pm->ID==ID_SHOWDEBUG) {
  80.         if(pm->Flags & PM_CHECKED) showdebug=TRUE;
  81.         else showdebug=FALSE;
  82.     }
  83.  
  84.     return 0;
  85. }
  86.  
  87. //
  88. // Array of strings used to print out the selected sort order.
  89. //
  90.  
  91. char *order[] = {"file name", "file size", "protection bits", "creation date", "file comment", "file type"};
  92.  
  93. void main()
  94. {
  95.     BOOL r=TRUE;
  96.     struct IntuiMessage *im,imsg;
  97.     struct PopupMenu *p;
  98.     struct Hook MenuHandler;
  99.     struct PM_IDLst *exlst;
  100.     ULONG sortorder=sbName;        // Holds the current sort order
  101.     ULONG oldorder=sortorder;    // To find out if changes has been made
  102.     char bfr[128];            // Temp string buffer
  103.  
  104.     MenuHandler.h_Entry=(HOOKFUNC)MenuHandlerFunc;
  105.     MenuHandler.h_Data=&sortorder;
  106.  
  107.     PopupMenuBase=(struct PopupMenuBase *)OpenLibrary(POPUPMENU_NAME,POPUPMENU_VERSION);            // Open the library
  108.     if(PopupMenuBase) {
  109.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;    // We let popupmenu.library open the libraries we need
  110.         GfxBase=(struct GfxBase *)PopupMenuBase->pmb_GfxBase;            // They remain valid until the library is closed!
  111.  
  112.         exlst=PM_ExLst(sbName, sbSize, sbProt, sbDate, sbComment, sbType, NULL);
  113.  
  114.         p=PMMenu("Sort by"),
  115.             PMMXItem("File name", sbName),        PM_Exclude, exlst, PM_ExcludeShared, TRUE, PM_Checked, TRUE, End,
  116.             PMMXItem("File size", sbSize),        PM_Exclude, exlst, PM_ExcludeShared, TRUE, End,
  117.             PMMXItem("Protection bits", sbProt),    PM_Exclude, exlst, PM_ExcludeShared, TRUE, End,
  118.             PMMXItem("Creation date", sbDate),    PM_Exclude, exlst, PM_ExcludeShared, TRUE, End,
  119.             PMMXItem("File comment", sbComment),    PM_Exclude, exlst, PM_ExcludeShared, TRUE, End,
  120.             PMMXItem("File type", sbType),        PM_Exclude, exlst, PM_ExcludeShared, TRUE, End,
  121.  
  122.             PMBar,    End,
  123.  
  124.             PMCheckItem("Reverse order", sbReverse), End,
  125.  
  126.             PMBar,    End,
  127.  
  128.             PMCheckItem("Show debug output", ID_SHOWDEBUG), End,
  129.  
  130.         End; /* PMMenu */
  131.  
  132.         if(p) {
  133.             w=OpenWindowTags(NULL,    WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS,    // Open a little window
  134.                     WA_RMBTrap,    TRUE,
  135.                     WA_DragBar,    TRUE,
  136.                     WA_Width,    400,
  137.                     WA_Height,    75,
  138.                     WA_Left,    0,
  139.                     WA_Top,        0,
  140.                     WA_Title,    "DirectoryOpus4 Menu Test",
  141.                     WA_CloseGadget,    TRUE,
  142.                     TAG_DONE);
  143.             if(w) {
  144.  
  145.                 ShowMessage("Click right mouse button!");
  146.  
  147.                 while(r) {
  148.                     WaitPort(w->UserPort);                        // Wait for a message
  149.                     while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {    // Get the message
  150.                         CopyMem(im,&imsg,sizeof(struct IntuiMessage));        // Copy the contents of it
  151.                         ReplyMsg((struct Message *)im);                // Reply the message
  152.  
  153.                         switch(imsg.Class) {
  154.                             case IDCMP_CLOSEWINDOW: r=FALSE; break;
  155.                             case IDCMP_MOUSEBUTTONS:            // The user has hit a mousebutton - time to open the menu!
  156.                                 if(imsg.Code==MENUDOWN) {
  157.                                     PM_OpenPopupMenu(w,
  158.                                         PM_Menu,        p,
  159.                                         PM_MenuHandler,        &MenuHandler,
  160.                                         TAG_DONE);
  161.  
  162.                                     // This is one way of finding out if something has changed
  163.                                     // It could also be managed in the MenuHandler or by checking
  164.                                     // what is returned by PM_OpenPopupMenu(). But this method
  165.                                     // seems appropriate in this case...
  166.                                     if(sortorder!=oldorder) {
  167.                                         int i=(sortorder&0xF)-1;    // Mask out the lower 4 bits
  168.  
  169.                                         i=i>5?0:i;    // Make sure i is inside the array
  170.                                         i=i<0?0:i;    // even if something should fail
  171.  
  172.                                         sprintf(bfr, "Sort by %s %s", order[i],
  173.                                             (sortorder&sbReverse)?"(REVERSE)":"");
  174.                                         ShowMessage(bfr);
  175.                                         oldorder=sortorder;
  176.                                     }
  177.                                 }
  178.                             break;
  179.                         }
  180.                     }
  181.                 }
  182.                 CloseWindow(w);
  183.             } else printf("Window error!\n");
  184.             PM_FreePopupMenu(p);
  185.         } else printf("Menu error!\n");
  186.  
  187.         if(exlst)
  188.             PM_FreeIDList(exlst);
  189.  
  190.         CloseLibrary((struct Library *)PopupMenuBase);
  191.     } else {
  192.         printf("PopupMenu V10+ is required!\n");
  193.     }
  194. }
  195.  
  196. void ShowMessage(STRPTR text)
  197. {
  198.     SetDrMd(w->RPort, 0);
  199.     SetAPen(w->RPort, 0);
  200.     RectFill(w->RPort, w->BorderLeft+5, w->BorderTop+5, w->Width-w->BorderRight-w->BorderLeft,
  201.         w->BorderTop+w->RPort->Font->tf_YSize+5);
  202.     SetAPen(w->RPort, 1);
  203.     Move(w->RPort, w->BorderLeft+5, w->BorderTop+5+w->RPort->Font->tf_Baseline);
  204.     Text(w->RPort, text, strlen(text));
  205. }
  206.