popup menu without right_click

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

popup menu without right_click

Post by Columbo »

Is it possible to create a popup like menu and have it display in the center of the window but not have to right_click to call it? I just want it to appear similar to how a MessageRequester would.

Thanks
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: popup menu without right_click

Post by spikey »

See CreatePopupMenu and DisplayPopupMenu.

DisplayPopupMenu has optional co-ordinate parameters to display the menu at a position you choose rather than the current mouse position.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: popup menu without right_click

Post by mk-soft »

Without create own dialog (Like MessageRequester)

Code: Select all

If OpenWindow(0, 100, 100, 600, 400, "Popup-Menu Example")
  ButtonGadget(0, 5, 5, 100, 30, "Click")
  
  If CreatePopupMenu(0)      ; creation of the pop-up menu begins...
    MenuItem(1, "Open")      ; You can use all commands for creating a menu
    MenuItem(2, "Save")      ; just like in a normal menu...
    MenuItem(3, "Save as")
    MenuItem(4, "Quit")
    MenuBar()
    OpenSubMenu("Recent files")
    MenuItem(5, "PureBasic.exe")
    MenuItem(6, "Test.txt")
    CloseSubMenu()
  EndIf
  
  Repeat
    Event = WaitWindowEvent()     ; check for window events
    
    Select Event
      Case #PB_Event_Menu        ; an item of the popup-menu was clicked
        Select EventMenu()       ; get the clicked menu item...
          Case 1 : Debug "Menu: Open"
          Case 2 : Debug "Menu: Save"
          Case 3 : Debug "Menu: Save as"
          Case 4 : End
          Case 5 : Debug "Menu: PureBasic.exe"
          Case 6 : Debug "Menu: Text.txt"
        EndSelect
        
      Case #PB_Event_Gadget
        If EventGadget() = 0
          x = WindowWidth(0) / 2 + WindowX(0) - 80
          y = WindowHeight(0) / 2 + WindowY(0) - 80
          DisplayPopupMenu(0, WindowID(0), x, y)  ; now display the popup-menu
        EndIf
        
    EndSelect
    
  Until Event = #PB_Event_CloseWindow
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: popup menu without right_click

Post by Columbo »

Thanks mk-soft. I'll give that a try. Much appreciated!
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: popup menu without right_click

Post by Columbo »

Hi mk-soft,

Your code works well. There is just one problem,... I have to click the button to make the menu appear on the screen. What I am trying to do is this,.. In my program I am using a couple of InputRequesters to get some user data. When the user clicks the "OK" button on the first InputRequester the second InputRequester appears. When the user clicks the "OK" button on the second InputRequester I want the menu to automatically appear on the screen. (without having to click on a separate button). I made a few attempts to do this but I can't get it to work.


Thank for you help.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: popup menu without right_click

Post by Marc56us »

A popup menu can appear whenever and wherever you want, even without user intervention.
It is simply the DisplayPopUpMenu() command that does this, even outside the main loop.

Code: Select all

; Popupmenu as main choice menu

OpenWindow(0, 200, 200, 600, 300, "Popup-Menu Example")

If CreatePopupMenu(0)
    MenuItem(1, "Open")
    MenuItem(2, "Save")
    MenuItem(3, "Save as")
    MenuItem(4, "Quit")
EndIf

DisplayPopupMenu(0, WindowID(0), WindowWidth(0)/2 + 140, WindowHeight(0)/2 + 150)

Repeat
    Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
            End
            
        Case #PB_Event_Menu
            Select EventMenu()
                Case 1 : Debug "Menu: Open"
                Case 2 : Debug "Menu: Save"
                Case 3 : Debug "Menu: Save as"
                Case 4 : End
                Case 5 : Debug "Menu: PureBasic.exe"
                Case 6 : Debug "Menu: Text.txt"
            EndSelect     
    EndSelect
ForEver
There is no function to close it since it disappears as soon as a command is used. So you just have to call it up again if you want to use it as the main menu.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: popup menu without right_click

Post by Columbo »

Thank you Marc56us. I was able to get the menu to popup ok by moving it into a procedure but the problem is that I am not getting any return from the EventMenu.

Here is the code that I was testing it with:

Code: Select all

Global month.s

Procedure showMonth()
     Debug month
EndProcedure

Procedure showMenu()
If CreatePopupMenu(0)      ; creation of the pop-up menu begins...
      MenuItem(1, "January")      ; You can use all commands for creating a menu
      MenuItem(2, "February")      ; just like in a normal menu...
      MenuItem(3, "March")
      MenuItem(4, "April")
      MenuItem(5, "May")
      MenuItem(6, "June")
               
    DisplayPopupMenu(0, WindowID(0))  ; now display the popup-menu
  EndIf
EndProcedure

  
  If OpenWindow(0, 200, 200, 200, 120, "Popup-Menu Example")
    
    showMenu()
    showMonth()
    
    Repeat
      Event = WaitWindowEvent()     ; check for window events
      
      Select Event
        ;Case #PB_Event_RightClick       ; right mouse button was clicked =>
        ;  DisplayPopupMenu(0, WindowID(0))  ; now display the popup-menu
          
        Case #PB_Event_Menu        ; an item of the popup-menu was clicked
          Select EventMenu()           ; get the clicked menu item...
            Case 1 : month =  "January"
            Case 2 : month =  "February"
            Case 3 : month =  "March"
            Case 4 : month =  "April"
            Case 5 : month =  "May"
            Case 6 : month =  "June"
          EndSelect
                 <----------[ If I put a Debug month here, it gives me the month that was selected but if I put the Debug month into the showMonth() procedure 
                               I get an empty Debug window.
      EndSelect
      
    Until Event = #PB_Event_CloseWindow
  EndIf
The variable month only contains the selected menu item if the debug is in the Select EventMenu(). If I Debug the variable month in the showMonth procedure the value seems to be lost. It displays an empty Debug window.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: popup menu without right_click

Post by Marc56us »

Columbo wrote:...the problem is that I am not getting any return from the EventMenu.
Just call your Procedure after EndSelect.
This works.

Code: Select all

        Case #PB_Event_Menu        
          Select EventMenu()          
            Case 1 : month =  "January"
            Case 2 : month =  "February"
            Case 3 : month =  "March"
            Case 4 : month =  "April"
            Case 5 : month =  "May"
            Case 6 : month =  "June"
        EndSelect
        showMonth() ; <--- Here
Example of clean code.

Code: Select all

EnableExplicit

Procedure showMonth(month.s)
    Debug month
EndProcedure

Procedure showMenu()
    Global X_PopMenu = WindowWidth(0)  / 2 + 140
    Global Y_PopMenu = WindowHeight(0) / 2 + 100
    If CreatePopupMenu(0)      
        MenuItem(1, "January") 
        MenuItem(2, "February")
        MenuItem(3, "March")
        MenuItem(4, "April")
        MenuItem(5, "May")
        MenuItem(6, "June")
        MenuBar()
        MenuItem(7, "Exit")
        DisplayPopupMenu(0, WindowID(0), X_PopMenu, Y_PopMenu)  
    EndIf
EndProcedure

OpenWindow(0, 200, 200, 600, 300, "Popup-Menu Example")
showMenu()

Repeat
    Select WaitWindowEvent()
            
        Case #PB_Event_Menu        
            Select EventMenu()         
                Case 1 : showMonth("January") 
                Case 2 : showMonth("February")
                Case 3 : showMonth( "March")
                Case 4 : showMonth("April")
                Case 5 : showMonth("May")
                Case 6 : showMonth("June")
                Case 7 : End
            EndSelect
            DisplayPopupMenu(0, WindowID(0), X_PopMenu, Y_PopMenu)  
            
        Case #PB_Event_CloseWindow
            End
            
    EndSelect
ForEver

End
:wink:
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: popup menu without right_click

Post by WilliamL »

Marc56us,

I ran your 'clean code' and I see the pop-up menu flash then the window covers it up. I can't see the pop-up menu or make a selection. It seems like it should work...

I never thought of defining a global in a procedure. :)

I'm wondering if the event loop is redrawing the window. This works but it is activated in the loop by the left click.

Code: Select all

EnableExplicit

Procedure showMonth(month.s)
    Debug month
EndProcedure

Procedure showMenu()
    Global X_PopMenu = WindowWidth(0)  / 2 + 140
    Global Y_PopMenu = WindowHeight(0) / 2 + 100
    If CreatePopupMenu(0)      
        MenuItem(1, "January") 
        MenuItem(2, "February")
        MenuItem(3, "March")
        MenuItem(4, "April")
        MenuItem(5, "May")
        MenuItem(6, "June")
        MenuBar()
        MenuItem(7, "Exit")
        DisplayPopupMenu(0, WindowID(0), X_PopMenu, Y_PopMenu)  
    EndIf
EndProcedure

OpenWindow(0, 200, 200, 600, 300, "Popup-Menu Example")
showMenu()

Repeat
    Select WaitWindowEvent()
        Case #PB_Event_LeftClick       ; left mouse button was clicked =>
            DisplayPopupMenu(0, WindowID(0))  ; now display the popup-menu
        Case #PB_Event_Menu        
            Select EventMenu()         
                Case 1 : showMonth("January") 
                Case 2 : showMonth("February")
                Case 3 : showMonth( "March")
                Case 4 : showMonth("April")
                Case 5 : showMonth("May")
                Case 6 : showMonth("June")
                Case 7 : End
            EndSelect
            DisplayPopupMenu(0, WindowID(0), X_PopMenu, Y_PopMenu)  
        Case #PB_Event_CloseWindow
            End
    EndSelect
ForEver
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: popup menu without right_click

Post by RASHAD »

I hope that it will suit your need

Code: Select all

ExamineDesktops()

If OpenWindow(0, -300, 0, 0, 0, "")
 
  If CreatePopupMenu(0)      ; creation of the pop-up menu begins...
    MenuItem(1, "Open")      ; You can use all commands for creating a menu
    MenuItem(2, "Save")      ; just like in a normal menu...
    MenuItem(3, "Save as")
    MenuItem(4, "Quit")
    MenuBar()
    OpenSubMenu("Recent files")
    MenuItem(5, "PureBasic.exe")
    MenuItem(6, "Test.txt")
    CloseSubMenu()
  EndIf
  Input$ = InputRequester("Title", "Please make your input:", "I'm the default input.")
  If Input$ > ""    
    Input$ = InputRequester("Title #2", "Make sure of your input:", "I'm the default input.")
    If Input$ > ""
      DisplayPopupMenu(0, WindowID(0),DesktopWidth(0)/2-100,DesktopHeight(0)/2-100)
    EndIf  
  EndIf

  Repeat
    Event = WaitWindowEvent()     ; check for window events
   
    Select Event
      Case #PB_Event_Menu        ; an item of the popup-menu was clicked
        Select EventMenu()       ; get the clicked menu item...
          Case 1 : Debug "Menu: Open"
          Case 2 : Debug "Menu: Save"
          Case 3 : Debug "Menu: Save as"
          Case 4 : End
          Case 5 : Debug "Menu: PureBasic.exe"
          Case 6 : Debug "Menu: Text.txt"
        EndSelect
       
      Case #PB_Event_Gadget
       
    EndSelect
   
  Until Event = #PB_Event_CloseWindow
EndIf
Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: popup menu without right_click

Post by mk-soft »

Window on the left screen :!: :mrgreen:

Popup work with hide window ...

Code: Select all

ExamineDesktops()

If OpenWindow(0, 10, 10, 400, 300, "Popup", #PB_Window_SystemMenu | #PB_Window_Invisible)
 
  If CreatePopupMenu(0)      ; creation of the pop-up menu begins...
    MenuItem(1, "Open")      ; You can use all commands for creating a menu
    MenuItem(2, "Save")      ; just like in a normal menu...
    MenuItem(3, "Save as")
    MenuItem(4, "Quit")
    MenuBar()
    OpenSubMenu("Recent files")
    MenuItem(5, "PureBasic.exe")
    MenuItem(6, "Test.txt")
    CloseSubMenu()
  EndIf
  Input$ = InputRequester("Title", "Please make your input:", "I'm the default input.")
  If Input$ > ""    
    Input$ = InputRequester("Title #2", "Make sure of your input:", "I'm the default input.")
    If Input$ > ""
      DisplayPopupMenu(0, WindowID(0),DesktopWidth(0)/2-100,DesktopHeight(0)/2-100)
    EndIf  
  EndIf

  Repeat
    Event = WaitWindowEvent()     ; check for window events
   
    Select Event
      Case #PB_Event_Menu        ; an item of the popup-menu was clicked
        Select EventMenu()       ; get the clicked menu item...
          Case 1 : Debug "Menu: Open"
          Case 2 : Debug "Menu: Save"
          Case 3 : Debug "Menu: Save as"
          Case 4 : End
          Case 5 : Debug "Menu: PureBasic.exe"
          Case 6 : Debug "Menu: Text.txt"
        EndSelect
        HideWindow(0, 0)
      Case #PB_Event_Gadget
       
    EndSelect
   
  Until Event = #PB_Event_CloseWindow
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: popup menu without right_click

Post by Columbo »

Thanks for the help guys. I think I have it working now.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: popup menu without right_click

Post by Columbo »

I thought that I had it working but its not.

So I’m still playing around with this popup menu. I am able to display it where I want to have it displayed but retrieving the selected item is giving me problems. If I call getUserInfo() from the #PB_Event_Menu and do a debug birthmonth in the getUserInfo procedure, it will display the selected month.

I have birthmonth set as a Global variable and the variable month also as a global variable. In the getUserInfo procedure I set month = birthmonth but if I try to use month or birthmonth in any other part of the program they are empty. A debug of these variables anywhere else in the program displays nothing. Totally confused!

Here is the testing code:

Code: Select all

Enumeration
  #MainWindow
EndEnumeration

Global Fname.s
Global  Lname.s

Global birthmonth.s
Global month.s
Global birthday.s
Global birthyear.s

Procedure makeMenu()
    If CreatePopupMenu(0)      ; creation of the pop-up menu begins...
      MenuItem(1, "January")      
      MenuItem(2, "February")      
      MenuItem(3, "March")
      MenuItem(4, "April")
      MenuItem(5, "May")
      MenuItem(6, "June")      
      MenuItem(7, "July")      
      MenuItem(8, "August")      
      MenuItem(9, "September")
      MenuItem(10, "October")
      MenuItem(11, "November")
      MenuItem(12, "December")
   EndIf
 EndProcedure    
 
 ;-------- Get Birth Month ---------
Procedure getmonth()
  x = WindowWidth( #MainWindow) / 2 + WindowX( #MainWindow) - 80
  y = WindowHeight( #MainWindow) / 2 + WindowY( #MainWindow) - 80 
  DisplayPopupMenu( #MainWindow, WindowID( #MainWindow),x,y)  ; now display the popup-menu
EndProcedure 

;-------- Get User Info ---------
Procedure getUserInfo()
  Fname = InputRequester("", "Please enter your First Name:  ", "")
  Lname = InputRequester("", "Please enter your Last Name:  ", "")
  birthday = InputRequester("", "Enter Birth Day:  ", "")
  birthyear = InputRequester("", "Enter Year Of Birth:  ", "")  
  byear = Val(birthyear)
  bday = Val(birthday)
  Debug birthmonth ;<------ this displays the selected month in this debug window
  month = birthmonth    ;<------ Here I assign birthmonth to the variable month
EndProcedure

Procedure getData()
  Debug birthmonth   ;<------ This displays nothing in the debug window
  Debug month         ;<------ This displays nothing in the debug window
EndProcedure

wFlags = #PB_Window_BorderLess | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_Maximize
If OpenWindow(#MainWindow, 0, 0, 1250, 700, "",wFlags)
  SetWindowColor(#Mainwindow, RGB(60,86,138))      ; Was RGB(12,73,139)  RGB(0,57,166)
  
  makeMenu()
  getmonth()
  getData()
  
  Repeat
Select WaitWindowEvent()                         ;Wait for an event
       ;  Case #PB_Event_CloseWindow          ;Close window if "X" is clicked.
       ;    run = 1                           ;Set run flag To 1   
           
    ;Select Event
         Case #PB_Event_CloseWindow          ;Close window if "X" is clicked.
           run = 1                           ;Set run flag To 1   
        
       Case #PB_Event_Menu        ; an item of the popup-menu was clicked
         Select EventMenu()           ; get the clicked menu item...
           Case 1 : birthmonth = "January"            
           Case 2 : birthmonth =  "February"
           Case 3 : birthmonth = "March"
           Case 4 : birthmonth = "April"         
           Case 5 : birthmonth = "May"            
           Case 6 : birthmonth = "June"           
           Case 7 : birthmonth = "July"           
           Case 8 : birthmonth = "August"
           Case 9 : birthmonth = "September"            
           Case 10 :  birthmonth = "October"           
           Case 11 : birthmonth = "November"            
           Case 12 : birthmonth = "December" 
         EndSelect
         getUserInfo()    <------ I call getUserInfo() here.
      Case #PB_Event_Gadget
       EndSelect
       
;-------- Exit program---------
Until run = 1    ;If run = 1 close the program
CloseDatabase(#PB_All)    ;close all databases
EndIf

http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: popup menu without right_click

Post by blueb »

Looks like this is getting complicated...
Why not have the user plug the information in a DateGadget() and parse the results?

Here's a sample from RSBasic that gets around the 1970 limits.... :idea:

Code: Select all

;==================================================================
;
; Author:     RSBasic    
; Date:       April 17, 2018
; Explain:    64-bit time (removes 1970 - 2037 limits)       
;==================================================================
EnableExplicit

Procedure WinCallback(hWnd, uMsg, wParam, lParam) 
  Protected *NMHDR.NMHDR
  Protected SYSTEMTIME.SYSTEMTIME
  
  Select uMsg
    Case #WM_NOTIFY
      *NMHDR = lParam
      Select *NMHDR\hwndFrom
        Case GadgetID(1)
          Select *NMHDR\code
            Case #DTN_DATETIMECHANGE
              SendMessage_(GadgetID(1), #MCM_GETCURSEL, 0, SYSTEMTIME)
              Debug Str(SYSTEMTIME\wMonth) + "/" + Str(SYSTEMTIME\wDay) + "/" +  Str(SYSTEMTIME\wYear)
          EndSelect
      EndSelect
  EndSelect
  
  ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure 

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  DateGadget(1, 10, 10, 100, 20)
  
  SetWindowCallback(@WinCallback())
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: popup menu without right_click

Post by Columbo »

Thanks blueb. I tried your code and, while it works, it might be a bit confusing to a user. For example, it comes up with the current date, ie: June 6, 2020, and if the user was looking for the month of December they may think that they have to click the arrow on the calendar 6 times to get to December. That's what I thought initially but then I realized that I could click on the month and get a popup with all of the months in it. Also, if it is just the month that we are looking for, the user may get confused by the fact that the DateGadget is looking for a complete date, day, month and year as opposed to simply clicking on a month from a popup menu. Your suggestion does however, stir my brain and gives me another idea that I will experiment with. Thank you so very much. Your help is very much appreciated.

Have a great day and be safe!
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Post Reply