How to retrieve text from dynamically created menu

Just starting out? Need help? Post your questions and find answers here.
superjacent
User
User
Posts: 27
Joined: Mon Oct 01, 2007 1:38 pm
Location: Melbourne, Australia
Contact:

How to retrieve text from dynamically created menu

Post by superjacent »

Hope someone can help. I have the following function which builds a menu structure and adds from zero 'recent folders' (directory paths) to many items. The directory paths are retrieved from a linked list.

Code: Select all

Procedure BuildMenu()
  If CreateMenu(#MENU_BAR, WindowID(#WIN_MAIN))
    MenuTitle("File")
    OpenSubMenu("Recent Folders")
      ForEach strFolder()
        MenuItem(#PB_Any, strFolder()\strPath)
      Next
    CloseSubMenu()
    MenuItem(#M_QUIT, "Quit")
    MenuTitle("Help")
    MenuItem(#M_ABOUT, "About")
EndIf
EndProcedure
In the main program code, the menu trapping section is as follows:

Code: Select all

Case #PB_Event_Menu
      Select EventMenu()
        Case #M_ABOUT
          MessageRequester("About","A program by Steve")
        Case #M_QUIT
          Quit = #True
My question is, how do I detect if a 'recent folder' menu item has been clicked and then retrieve the value. I'm a little confused as there could be anything from zero to say 10, 20 or 30 items.

Any help appreciated.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

You need a reserved ID range, e. g. 33 to 43 or somthing. To don't interfere with other Menu ID's you need to assign the ID's after all other menu items.

Code: Select all

Enumeration
  #IDM_MenuItem1
  #IDM_MenuItem2
  #IDM_MenuItem3
EndEnumeration

#IDM_RecentStart = #PB_Compiler_EnumerationValue
#IDM_RecentMax = 10
Then you can check events like this:

Code: Select all

	If EventID = #PB_Event_Menu
		Select EventMenu()
			Case #IDM_MenuItem1			
			Case #IDM_MenuItem2
			Case #IDM_MenuItem3
			Case #IDM_RecentStart To #IDM_RecentStart + #IDM_RecentMax
			; do stuff with your recent menu ...			
		EndSelect
	EndIf
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
superjacent
User
User
Posts: 27
Joined: Mon Oct 01, 2007 1:38 pm
Location: Melbourne, Australia
Contact:

Post by superjacent »

Thank you, looks like that makes sense, after visiting the Enumeration help page. I'll fiddle with this over the weekend.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Fluid Byte wrote:You need a reserved ID range, e. g. 33 to 43 or somthing. To don't interfere with other Menu ID's you need to assign the ID's after all other menu items.

Code: Select all

Enumeration
  #IDM_MenuItem1
  #IDM_MenuItem2
  #IDM_MenuItem3
EndEnumeration

#IDM_RecentStart = #PB_Compiler_EnumerationValue
#IDM_RecentMax = 10
Then you can check events like this:

Code: Select all

	If EventID = #PB_Event_Menu
		Select EventMenu()
			Case #IDM_MenuItem1			
			Case #IDM_MenuItem2
			Case #IDM_MenuItem3
			Case #IDM_RecentStart To #IDM_RecentStart + #IDM_RecentMax
			; do stuff with your recent menu ...			
		EndSelect
	EndIf
But thats not dynamic.
--Kale

Image
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

Code: Select all

  
If EventID = #PB_Event_Menu 
      Select EventMenu() 
        Case #M_ABOUT 
          MessageRequester("About","A program by Steve") 
        Case #M_QUIT 
          Quit = #True 
        Default
           RecentFilePath.s=GetMenuItemText(#MEMU_BAR, EventMenu())
      EndSelect 
EndIf
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

But thats not dynamic.
What's not dynamic about it? You have to reserve ID's anyway.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Fluid Byte wrote:
But thats not dynamic.
What's not dynamic about it? You have to reserve ID's anyway.
Using predefined constants make it static.

A dynamic way would be (thanks eddy):

Code: Select all

;======================================================================
;-CONSTANTS
;======================================================================

#APP_NAME = "Dynamic Menu Test"

Enumeration
	#WINDOW_ROOT
	#MENUBAR_ROOT
	#MENU_QUIT
EndEnumeration

#WINDOW_FLAGS = #PB_Window_SystemMenu | #PB_Window_ScreenCentered

;======================================================================
;-GLOBAL FLAGS / VARIABLES / STRUCTURES / ARRAYS
;======================================================================

NewList MenuItems.s()
	AddElement(MenuItems()) : MenuItems() = "Menu Item 1"
	AddElement(MenuItems()) : MenuItems() = "Menu Item 2"
	AddElement(MenuItems()) : MenuItems() = "Menu Item 3"

;======================================================================
;-PROCEDURES
;======================================================================

;Handle an error
Procedure HandleError(Result.l, Text.s)
	If Result = 0
		MessageRequester("Error", Text, #PB_MessageRequester_Ok)
		End
	EndIf
EndProcedure

;======================================================================
;-GEOMETRY
;======================================================================

HandleError(OpenWindow(#WINDOW_ROOT, 0, 0, 400, 300, #APP_NAME, #WINDOW_FLAGS), "Main window could not be created.")
HandleError(CreateGadgetList(WindowID(#WINDOW_ROOT)), "Gadget list for the main window could not be created.")

HandleError(CreateMenu(#MENUBAR_ROOT, WindowID(#WINDOW_ROOT)), "Menu for the main window could not be created.")
	MenuTitle("File")
		
		ForEach MenuItems()
			MenuItem(#PB_Any, MenuItems())
		Next

		MenuBar()

		MenuItem(#MENU_QUIT, "Quit")

;======================================================================
;-MAIN LOOP
;======================================================================

Repeat
	EventID.l = WaitWindowEvent()
	Select EventID
	
		Case #PB_Event_Menu

			MenuItemID.l = EventMenu()
			Select MenuItemID.l
			
				Case #MENU_QUIT
					Debug GetMenuItemText(#MENUBAR_ROOT, MenuItemID.l)
					End
					
				Default
					Debug GetMenuItemText(#MENUBAR_ROOT, MenuItemID.l)

			EndSelect
	
	EndSelect
Until EventID = #PB_Event_CloseWindow
End

;======================================================================
;-END
;======================================================================
Except it doesn't work. For some reason using #PB_Any as a MenuID makes GetMenuItemText() not work!!! Bug? Me too drunk? :?
--Kale

Image
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Err, MenuItem() doesn't support #PB_Any ...
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Fluid Byte wrote:Err, MenuItem() doesn't support #PB_Any ...
Really? Then why is there no error? and how then are true dynamic menus created? Maybe it should be added?

Image
--Kale

Image
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Really? Then why is there no error?
You are simply passing -1 as the ID and since MenuItem() doesn't support handles negative indexes are valid. Some commands support handles, some don't. And if a certain lib does, it's stated in the manual.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Fluid Byte wrote:
Really? Then why is there no error?
You are simply passing -1 as the ID and since MenuItem() doesn't support handles negative indexes are valid. Some commands support handles, some don't. And if a certain lib does, it's stated in the manual.
It should also be stated that it doesn't then.

Also, how then are true dynamic menus created? Maybe it should be added?
--Kale

Image
superjacent
User
User
Posts: 27
Joined: Mon Oct 01, 2007 1:38 pm
Location: Melbourne, Australia
Contact:

Post by superjacent »

Thanks all, there's enough info here to enable me to progress my first PB application. I like Eddy's one-liner but that's not dismissing the other info put forward, I'll have to study it to make sense of it.
Post Reply