Page 1 of 1

Using PopUpMenus

Posted: Tue Oct 20, 2009 6:36 pm
by WilliamL
I saw a thread recently that got me thinking of ways to use the PopUpMenu. I'm having a few problems and I see that some others have tried to solve them but they won't work on a Mac. I want the popupmenu to show if you hover over the gadget and go away if I move off of the gadget.

Problems:
1) The popupmenu won't go away if I move off of the gadget. Events stop until a menu selection is made. (as reported before)
2) The menu event is not... fixed code it works now!
3) Using the optional position, in PopUpMenu, puts the menu in the upper left corner of the desktop and not the window.
[edit] This is correct behavior. See below...

Code: Select all

Procedure ScanGadgets(x,y,gadget)
    If y>GadgetY(gadget) And y<GadgetY(gadget)+GadgetHeight(gadget)
        If x>GadgetX(gadget) And x<GadgetX(gadget)+GadgetWidth(gadget)
            ProcedureReturn 1
        EndIf
    EndIf
    ProcedureReturn 0
EndProcedure

#Window1=1
#Gadget1=1
#Gadget2=2
Define x.l,y.l,oldx.l,oldy.l

OpenWindow(#Window1,0,0,400,100,"Example",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
    If CreatePopupMenu(0)
        MenuItem(1,"none")
        MenuItem(2,"50%")
        MenuItem(3,"25%")
    EndIf
    If CreatePopupMenu(10)
        MenuItem(11,"none")
        MenuItem(12,"200%")
        MenuItem(13,"400%")
    EndIf
    TextGadget(#Gadget1,10,10,200,20,"Popup menu 1",#PB_Text_Border)
    TextGadget(#Gadget2,10,40,200,20,"Popup menu 2",#PB_Text_Border)

Repeat
    event=WaitWindowEvent()
    If event=#PB_Event_Menu
        MessageRequester("Menu",Str(EventMenu()))
    Else
        x=WindowMouseX(#Window1) ; present mouse position
        y=WindowMouseY(#Window1)
        If (oldx<>x) Or (oldy<>y)
            If ScanGadgets(x,y,#Gadget1)
                DisplayPopupMenu(0,WindowID(#Window1))
            EndIf
            If ScanGadgets(x,y,#Gadget2)
               DisplayPopupMenu(10,WindowID(#Window1))
            EndIf
            oldx=x
            oldy=y
        EndIf
    EndIf
Until event=#PB_Event_CloseWindow

Re: Using PopUpMenus

Posted: Wed Oct 21, 2009 10:59 am
by Fred
For 3), warning GadgetX/Y() is relative to the window border, while displaypopupmenu() expect absolute screen coordinate.

Re: Using PopUpMenus

Posted: Wed Oct 21, 2009 5:04 pm
by WilliamL
I can see why, once the PopUpMenu() is open, the event loop waits for a menu event. Maybe this will not be possible (at this time). Actually, the code works pretty well as it is. I just wanted to get the same effect as I get on web pages where you move over a link and you get a menu of choices.

It was just an idea.

PS. Hi Fred!

Re: Using PopUpMenus

Posted: Sun Jul 18, 2010 3:55 pm
by Krisko
I want to ask you how can I use popup menu only in a specific gadget. I mean if in my form I have listicon gadget I want to have popup menu only in this gadget :? Thank you :)

Re: Using PopUpMenus

Posted: Sun Jul 18, 2010 7:42 pm
by WilliamL
Isn't that what my first posting of code does? As I understand it, a pop-up menu is a menu and doesn't have anything to do with gadgets so you will have to program accordingly (like my code - which is copied from someone else's code). I haven't played with this for awhile and I don't know how the pop-up menu might behave differently than when I posted.

I you get this to work, post your solution. :)

Re: Using PopUpMenus

Posted: Wed Jul 28, 2010 5:06 pm
by WilliamL
Hey Krisko,

This is a better solution! Well, different, on mine the menu shows immediately and on vera's you have to click on the gadget. Take your choice.

http://www.purebasic.fr/english/viewtop ... 19&t=43052

I tried it and it works.

Thanks vera :D

Re: Using PopUpMenus

Posted: Wed Jul 28, 2010 11:22 pm
by Vera
Hi William,

that's a nice surprise :)

While trying to find out a bit more about 'Using PopUpMenus' I made another small example which I think is more convenient to show how popupmenus may be implemented, especially applying towards beginners. And I find your thread would be a good place to share it.
It runs on Linux and Windows, and hopefully on MAC too - if you can have look.

I've also tested your fine procedure (I'm glad I came across) and it works well with PB 4.50 on Linux and on Win - so it's fully crossplattform Image

thanks for sharing ~ Vera

Code: Select all

If OpenWindow(0, 260, 130, 300, 120, "PopupMenu Example 3")
     ButtonGadget(1, 40, 10, 100, 30, "click to popup")
     ListIconGadget(2, 160, 10, 110, 72, "Listing", 82)
      AddGadgetItem(2, -1, "click")
      AddGadgetItem(2, -1, "right mouse")

  If CreateMenu(0, WindowID(0))               ; building a window menu, to show it's items 
     MenuTitle("Menu-1")                      ; are also regarded by the EventMenu()
       MenuItem(1, "T&ab 1"  +Chr(9) + "A")
       MenuItem(2, "Tab 2"  +Chr(9)+"Ctrl+B")
       MenuItem(3, "Tab 3"  +Chr(9)+"Cmd+C")  ; used on MAC
       MenuItem(0, "quit")
  EndIf
  If CreatePopupMenu(1)                        ; building one popup menu
       MenuItem(4, "save as") 
       MenuItem(5, "Item 2" + Chr(9) + "Ctrl+B")
       MenuItem(6, "debug me")
  EndIf
  If CreatePopupImageMenu(2, #PB_Menu_ModernLook) ; building another popup menu
       MenuItem(7, " ONE ? ")
       MenuItem(8, " + TWO ")
       MenuItem(0, " quit ")
  EndIf
  
 Repeat
  Select WaitWindowEvent()                       ; starting the event request
    Case #PB_Event_Gadget                        ; in case of gadgets, request which one
      Select EventGadget()
          Case 1
            DisplayPopupMenu(1, WindowID(0))     ; shows the first Popup-Menu 
          Case 2 
            If EventType() = #PB_EventType_RightClick   ; only on specific event
              DisplayPopupMenu(2, WindowID(0))   ; shows the second Popup-Menu
            EndIf 
            
       EndSelect 
            
     Case #PB_Event_Menu                         ; in case of menus, request which item
       Select EventMenu()  
           Case 1 : Debug GetGadgetText(1)
           Case 2 : SetGadgetText(1, "come here")
           Case 3 : SetGadgetText(1, "click to popup")
           Case 4 : Debug "Menu: Save as"
           Case 5 : Debug "Item 2 clicked"
           Case 6 : Debug "I'm  debuged"
           Case 7 : Debug "no: " + Str(CountGadgetItems(2)) + " Items"
           Case 8 : Debug "= THREE"
           Case 0 : Quit = 1                     ; note: the same case can be used in several menus
        EndSelect
        
      Case #PB_Event_CloseWindow                 ; in case of window close button
        Quit = 1 ; Break
    EndSelect
 Until Quit = 1 
 
EndIf

Re: Using PopUpMenus

Posted: Thu Jul 29, 2010 3:58 am
by WilliamL
Hi vera! It nice hearing from you. I've read many of your posts in other forums.

Your code works fine and is a good outline of how to use events and events with types. On the Mac, #PB_Menu_ModernLook causes problems for the popup menu (no menus, just lines in the popup box! - this looks like a 'bug' in the Mac version), take that constant out and all is fine.

Re: Using PopUpMenus

Posted: Thu Jul 29, 2010 7:46 am
by jamirokwai
Vera wrote:I've also tested your fine procedure (I'm glad I came across) and it works well with PB 4.50 on Linux and on Win - so it's fully crossplattform.
Nice. Thanks for the example. Works on my Mac.
I tried to get the PopUpMenu-code from one of my tools, but I couldn't narrow it down. It looks similar to your approach, but the program depended on some includes...
WilliamL wrote:Your code works fine and is a good outline of how to use events and events with types. On the Mac, #PB_Menu_ModernLook causes problems for the popup menu (no menus, just lines in the popup box! - this looks like a 'bug' in the Mac version), take that constant out and all is fine.
On my Mac, even without the #PB_Menu_ModernLook-option, the menu will have no entries, just lines. Seems, CreatePopupImageMenu() is buggy.

Edit: It's not CreatePopupImageMenu() but MenuItem... The error for me was: no "-" as a first letter in your menu-description...
This works:
MenuItem(7, "ONE")
This won't:
MenuItem(7, "- ONE")

I filled in a bug-report - see http://www.purebasic.fr/english/viewtop ... 24&t=43062

Re: Using PopUpMenus

Posted: Thu Jul 29, 2010 11:47 am
by Vera
Hello,

thank you for your encouraging replies :D

As for those dashes - I only used them to make that popup look more pleasing (a bit wider), but removed them now (not exchanging them with other fancy characters ;) )
I've kept the #PB_Menu_ModernLook-parameter as obvious hint that it's a different kind of popupmenu, though it doesn't seem to do anything on Linux either, still it doesn't harm the function.

@ jamirokwai
Concerning your bugreport. It already proves that it's not due to the gagdet. But could you also crosscheck if it happens with the standard popupmenu as well?
Also it might be valuable to know if it's only the very first dash in the menu, or if any (later) leading dash will too corrupt the (whole or rest of the) menu. And if you're passionate - test it with the one or other uncommon character, there might be some more.
Not meant to make you any extra work but to support the team 8)

greetings ~ Vera

Re: Using PopUpMenus

Posted: Thu Jul 29, 2010 2:46 pm
by jamirokwai
Vera wrote:@ jamirokwai
Concerning your bugreport. It already proves that it's not due to the gagdet. But could you also crosscheck if it happens with the standard popupmenu as well?
Also it might be valuable to know if it's only the very first dash in the menu, or if any (later) leading dash will too corrupt the (whole or rest of the) menu. And if you're passionate - test it with the one or other uncommon character, there might be some more.
Not meant to make you any extra work but to support the team 8)

greetings ~ Vera
Hi Vera,

the problem lies in the MenuItem()-command. Every try with an "-" as the first letter fails. Your other fancy characters work...
Looks like MenuItem(0,"-") creates a MenuBar()...

Re: Using PopUpMenus

Posted: Thu Jul 29, 2010 5:10 pm
by WilliamL
Yes, if you take out the beginning '-'s it works fine. I think jamirokwai hit on the solution and it does appear to be MenuBars and the dashes effect any type of MenuItems. I don't know if you'd call this a bug? The #PB_Menu_ModernLook constant doesn't seem to have any effect on the Mac but doesn't cause a problem.

vera, I ran your edited code and it seems to works fine. Who woulda guessed, you try to show pop-up menus and end up finding something else?! :?

Re: Using PopUpMenus

Posted: Thu Jul 29, 2010 5:33 pm
by Vera
WilliamL wrote:Who woulda guessed, you try to show pop-up menus and end up finding something else?! :?
well - you're never safe
'cause THE UNEXPECTED always shows up when you dare to leave the mainstream :lol:

ps: thanks for retesting

Re: Using PopUpMenus

Posted: Thu Jul 29, 2010 5:41 pm
by jamirokwai
WilliamL wrote:Yes, if you take out the beginning '-'s it works fine. I think jamirokwai hit on the solution and it does appear to be MenuBars and the dashes effect any type of MenuItems. I don't know if you'd call this a bug? The #PB_Menu_ModernLook constant doesn't seem to have any effect on the Mac but doesn't cause a problem.
Hm. Maybe I should play Lotto this weekend!?
Probably PureBasic uses MenuBar() and MenuItem("-") in a similar, magically connected way... :mrgreen:
So, if it's not a bug, it's a feature...