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

  1. //
  2. // $VER: EndlessMenu.c 1.1 (25.08.00)
  3. //
  4. // Popup Menu library test program
  5. //
  6. // ⌐1996-2000 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // Run and click the mouse in the window!
  10. // This little hack is intended to test the submenus.
  11. //
  12. // Changes:
  13. //
  14. // 1.1    Displays remaining stack and free RAM now
  15. //
  16.  
  17. #include <intuition/intuition.h>
  18. #include <exec/memory.h>
  19.  
  20. #include <proto/intuition.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.  
  36. struct Window *w;    // This window is only needed to find out when and where the menu should appear.
  37.             // The font in this window's rastport will be used for the menu.
  38.  
  39. struct Hook SubConstruct;
  40. struct Hook SubDestruct;
  41.  
  42. ULONG SubNum=1;
  43.  
  44. struct PopupMenu * __saveds __asm SubConstructFunc(register __a0 struct Hook *hook,
  45.                      register __a2 struct PopupMenu *selected, register __a1 APTR *handle)
  46. {
  47.     struct PopupMenu *newpm;
  48.     char bfr[50], stack[50], mem[50];
  49.     struct Task *tsk=FindTask(NULL);
  50.  
  51.     // Don't try to open windows, requesters or print text etc from this
  52.     // hook, wich is called while the menu is still open.
  53.     // The display may be locked! (If window mode is not used)
  54.  
  55.     sprintf(bfr, "SubMenu #%ld", SubNum++);
  56.     sprintf(stack, "Stack: %ld b", (ULONG)tsk->tc_SPReg-(ULONG)tsk->tc_SPLower);
  57.     sprintf(mem, "Mem: %ld kb", AvailMem(0)/1024);
  58.  
  59.     newpm=PM_MakeMenu(
  60.         PMInfo(bfr), End,
  61.         PMBar, End,
  62.         PMItem("Next SubMenu"),
  63.             PM_SubConstruct,    &SubConstruct,
  64.             PM_SubDestruct,        &SubDestruct,
  65.             PM_Sub, PM_MakeMenu(
  66.                 PMBar, PMEnd,
  67.             PMEnd,
  68.         PMEnd,
  69.         PMBar, End,
  70.         PMInfo(stack), End,
  71.         PMInfo(mem), End,
  72.     PMEnd;        
  73.  
  74.     if(selected->Sub) {
  75.         PM_FreePopupMenu(selected->Sub);    // free the old one (if there is one)
  76.     }
  77.  
  78.     selected->Sub=newpm;
  79.  
  80.     return selected->Sub;    // If you return NULL, no menu will be opened.
  81. }
  82.  
  83. void __saveds __asm SubDestructFunc(register __a0 struct Hook *hook,
  84.                      register __a2 struct PopupMenu *parent)
  85. {
  86.     // Don't try to open windows, requesters or print text from this
  87.     // hook, wich is called while the menu is still open.
  88.     // The display might be locked, and input definitively is.
  89.     
  90.     SubNum--;
  91. }
  92.  
  93. struct PopupMenu *MakeTestMenu(void);
  94.  
  95. void main()
  96. {
  97.     struct IntuiMessage *im,imsg;
  98.     struct PopupMenu *p;
  99.     BOOL r=TRUE;
  100.  
  101.     SubConstruct.h_Entry=(HOOKFUNC)SubConstructFunc;
  102.     SubConstruct.h_Data=NULL;    // User Data - you can put anything you like to here.
  103.  
  104.     SubDestruct.h_Entry=(HOOKFUNC)SubDestructFunc;
  105.     SubDestruct.h_Data=NULL;    // User Data - you can put anything you like to here.
  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.         p=MakeTestMenu(); // Declared at the end of this file.
  113.  
  114.         if(p) {
  115.             w=OpenWindowTags(NULL,    WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_VANILLAKEY,    // Open a little window
  116.                     WA_RMBTrap,    TRUE,
  117.                     WA_DragBar,    TRUE,
  118.                     WA_Width,    150,
  119.                     WA_Height,    100,
  120.                     WA_Left,    0,
  121.                     WA_Top,        100,
  122.                     WA_Title,    "Endless Menus",
  123.                     WA_CloseGadget,    TRUE,
  124.                     TAG_DONE);
  125.             if(w) {
  126.                 while(r) {
  127.                     WaitPort(w->UserPort);                        // Wait for a message
  128.                     while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {    // Get the message
  129.                         CopyMem(im,&imsg,sizeof(struct IntuiMessage));        // Copy the contents of it
  130.                         ReplyMsg((struct Message *)im);                // Reply the message
  131.  
  132.                         if(imsg.Class==IDCMP_MOUSEBUTTONS) {
  133.                             if(imsg.Code==MENUDOWN || imsg.Code==MENUUP) {
  134.                                 if(PM_OpenPopupMenu(w, PM_Menu, p, TAG_DONE)==5) r=0;
  135.                             }
  136.                         }
  137.                         if(imsg.Class==IDCMP_CLOSEWINDOW) r=FALSE;        // See if the user wants to quit
  138.                     }
  139.                 }
  140.                 CloseWindow(w);
  141.             } else printf("Window error!\n");
  142.             PM_FreePopupMenu(p);
  143.         } else printf("Menu error!\n");
  144.         CloseLibrary((struct Library *)PopupMenuBase);
  145.     }
  146. }
  147.  
  148. struct PopupMenu *MakeTestMenu()
  149. {
  150.     struct PopupMenu *p;
  151.  
  152.     p=PMMenu("Endless Menu"),
  153.         PMInfo("Welcome to the neverending"), End,
  154.         PMInfo("submenu example."), End,
  155.         PMInfo("This time it's for real..."), End,
  156.         PMInfo("Note! The menu stops when it"), End,
  157.         PMInfo("runs out of stack or memory."), End,
  158.         PMBar, End,
  159.         PMItem("Submenu"),
  160.             PM_SubConstruct,    &SubConstruct,
  161.             PM_SubDestruct,        &SubDestruct,
  162.             PM_Sub, PMMenu("Error!"),
  163.                 PMBar, End,
  164.             PMEnd,
  165.         End,
  166.         PMBar,    End,
  167.         PMItem("Quit"),    PM_UserData,    5,    End,
  168.     End;
  169.  
  170.     return p;
  171. }