Use one menu for multiple windows

Just starting out? Need help? Post your questions and find answers here.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 303
Joined: Sun Aug 29, 2021 4:34 am

Use one menu for multiple windows

Post by Distorted Pixel »

Hi,

Is it possible to use the same menu for multiple windows. I have searched for an answer, but have not found an answer for my specific question, all I have found is stuff about managing multiple windows and nothing about the same menu used across multiple windows. I want to use literally one menu for all windows. Only 1 - 2 windows will be loaded or seen at any given time, but I want to use the same menu for all but the first 6 -7 windows (The first 6 -7 windows are the game setup windows. No need for the menu in them.)

After the setup windows I want to have the menu available in all other windows except the game windows itself where the 2 teams will be playing against each other. The game window will have it's own menu.

[Edit] - I would like to not have to create the same menu many times in the code for all windows that need it. I would be senseless, time consuming and a lot of typing. I have even thought about using PureVision for all creating all the windows and the menus (Only 2 menus in the game. One main menu for multiple windows and the other is for during the game action)
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Use one menu for multiple windows

Post by AZJIO »

Code: Select all

If OpenWindow(0, 200, 200, 200, 100, "Example Menu")
	If CreateMenu(0, WindowID(0))
		MenuTitle("Project")
		MenuItem(1, "Open" + Chr(9) + "Ctrl+O")
		MenuItem(2, "Save" + Chr(9) + "Ctrl+S")
		MenuItem(3, "Save as" + Chr(9) + "Ctrl+A")
		MenuItem(4, "Close" + Chr(9) + "Ctrl+C")
	EndIf
EndIf

If OpenWindow(1, 400, 200, 200, 100, "Example Menu")
	SetMenu_(WindowID(1), MenuID(0))
EndIf

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
			EndSelect
	EndSelect
ForEver
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 303
Joined: Sun Aug 29, 2021 4:34 am

Re: Use one menu for multiple windows

Post by Distorted Pixel »

AZJIO wrote: Sun Mar 24, 2024 4:54 pm

Code: Select all

If OpenWindow(0, 200, 200, 200, 100, "Example Menu")
	If CreateMenu(0, WindowID(0))
		MenuTitle("Project")
		MenuItem(1, "Open" + Chr(9) + "Ctrl+O")
		MenuItem(2, "Save" + Chr(9) + "Ctrl+S")
		MenuItem(3, "Save as" + Chr(9) + "Ctrl+A")
		MenuItem(4, "Close" + Chr(9) + "Ctrl+C")
	EndIf
EndIf

If OpenWindow(1, 400, 200, 200, 100, "Example Menu")
	SetMenu_(WindowID(1), MenuID(0))
EndIf

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
			EndSelect
	EndSelect
ForEver
Thank you AZJIO,
One more question I have is if the first window where the creation of the menu is done is closed, can you still use the the menu throughout the rest of the game in other windows? I ask this because I will have all windows using the menu in their own "Include" file
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Use one menu for multiple windows

Post by AZJIO »

Distorted Pixel wrote: Sun Mar 24, 2024 5:18 pm if the first window where the creation of the menu is done is closed, can you still use the the menu
To do this, you need to close the window and see what happens. You can conduct the tests yourself.

Code: Select all

If OpenWindow(0, 200, 200, 200, 100, "Example Menu")
	If CreateMenu(0, WindowID(0))
		MenuTitle("Project")
		MenuItem(1, "Open" + Chr(9) + "Ctrl+O")
		MenuItem(2, "Save" + Chr(9) + "Ctrl+S")
		MenuItem(3, "Save as" + Chr(9) + "Ctrl+A")
		MenuItem(4, "Close" + Chr(9) + "Ctrl+C")
	EndIf
EndIf

If OpenWindow(1, 400, 200, 200, 100, "Example Menu")
	SetMenu_(WindowID(1), MenuID(0))
EndIf

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			Select EventWindow()
				Case 0 : CloseWindow(0)
				Case 1 : End
			EndSelect
		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
			EndSelect
	EndSelect
ForEver
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 303
Joined: Sun Aug 29, 2021 4:34 am

Re: Use one menu for multiple windows

Post by Distorted Pixel »

AZJIO wrote: Sun Mar 24, 2024 5:33 pm You can conduct the tests yourself.
Yes, as soon as you posted your example code I started to test things. Currently I have been trying to implement your example into my project and it is failing to show the menu in the second window.

Currently I am using PureVision to create the interface of the game, but so far I have failed to get your example working. I will keep working on it. If I can't get it to work with PureVision files, I will code the entire game manually. I do have a project without using PureVision also.
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Use one menu for multiple windows

Post by boddhi »

Distorted Pixel wrote: [...] Currently I have been trying to implement your example into my project and it is failing to show the menu in the second window. [...]
The help manual may be the answer! :wink:

Technically, a menu must be attached to a window. Therefore, the same menu cannot be "shared" by several windows. And, consequently, when you close the window, you also release the menu associated with it.

If you want to have the same menu (i.e. with the same items) on several windows, one way is to create, for example, a procedure - which will have the task of creating each menu - with at least two arguments, one of which will receive the menu number (i.e. the PB Id) and the other, the number of the window to which it will be attached.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Fips
User
User
Posts: 35
Joined: Sun Feb 20, 2022 1:03 pm

Re: Use one menu for multiple windows

Post by Fips »

Maybe you don't really need the same menue but could instead create several alike menues via a procedure like this:

Code: Select all

EnableExplicit

Enumeration Windows
  #Win_0
  #Win_1
  #Win_2
EndEnumeration

Enumeration Menues
  #Menue_Close
  #Menue_Test
EndEnumeration


Procedure onCloseWindow()
  Protected.i winNr = EventWindow()
  
  CloseWindow(winNr)
  
  If IsWindow(#Win_0) = #False And IsWindow(#Win_1) = #False And IsWindow(#Win_2) = #False
    End  
  EndIf
  
EndProcedure

Procedure onClickedTest()
  Protected.i winNr = EventWindow()
  
  Debug "Menue-Item 'Test' was clicked on window " + Str(winNr) + ". The MenueNr is " + Str(GetWindowData(winNr)) + "." 
  
EndProcedure


Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure


Procedure OpenTestWindow(winNr.i)
  
  If OpenWindow(winNr, Random(400), Random(400), 400, 400, "Window " + Str (winNr))
    CreateTestMenue(winNr)
    
    BindEvent(#PB_Event_CloseWindow, @onCloseWindow(), winNr)
    
  EndIf
  
  
  
EndProcedure

OpenTestWindow(#Win_0)
OpenTestWindow(#Win_1)
OpenTestWindow(#Win_2)


Repeat
  WaitWindowEvent()
  
ForEver

User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 303
Joined: Sun Aug 29, 2021 4:34 am

Re: Use one menu for multiple windows

Post by Distorted Pixel »

Fips wrote: Mon Mar 25, 2024 9:28 pm Maybe you don't really need the same menue but could instead create several alike menues via a procedure like this:

Code: Select all

EnableExplicit

Enumeration Windows
  #Win_0
  #Win_1
  #Win_2
EndEnumeration

Enumeration Menues
  #Menue_Close
  #Menue_Test
EndEnumeration


Procedure onCloseWindow()
  Protected.i winNr = EventWindow()
  
  CloseWindow(winNr)
  
  If IsWindow(#Win_0) = #False And IsWindow(#Win_1) = #False And IsWindow(#Win_2) = #False
    End  
  EndIf
  
EndProcedure

Procedure onClickedTest()
  Protected.i winNr = EventWindow()
  
  Debug "Menue-Item 'Test' was clicked on window " + Str(winNr) + ". The MenueNr is " + Str(GetWindowData(winNr)) + "." 
  
EndProcedure


Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure


Procedure OpenTestWindow(winNr.i)
  
  If OpenWindow(winNr, Random(400), Random(400), 400, 400, "Window " + Str (winNr))
    CreateTestMenue(winNr)
    
    BindEvent(#PB_Event_CloseWindow, @onCloseWindow(), winNr)
    
  EndIf
  
  
  
EndProcedure

OpenTestWindow(#Win_0)
OpenTestWindow(#Win_1)
OpenTestWindow(#Win_2)


Repeat
  WaitWindowEvent()
  
ForEver

Actually I do need the exact same menu for all screens other than the game screen where the 2 teams are playing against each other. If I have to I'll just copy and paste the same menu code into each window and set the correct window and menu numbers
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Use one menu for multiple windows

Post by Quin »

Distorted Pixel wrote: Tue Mar 26, 2024 2:45 pm Actually I do need the exact same menu for all screens other than the game screen where the 2 teams are playing against each other. If I have to I'll just copy and paste the same menu code into each window and set the correct window and menu numbers
No, you don't need to do that, the procedure way can work for that. In fact, that's what it was designed for. Look here:
Fips wrote: Mon Mar 25, 2024 9:28 pm

Code: Select all

Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure
This procedure is what you call instead of copy-pasting :wink: just pass the window's number.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 303
Joined: Sun Aug 29, 2021 4:34 am

Re: Use one menu for multiple windows

Post by Distorted Pixel »

Quin wrote: Tue Mar 26, 2024 3:43 pm
Distorted Pixel wrote: Tue Mar 26, 2024 2:45 pm Actually I do need the exact same menu for all screens other than the game screen where the 2 teams are playing against each other. If I have to I'll just copy and paste the same menu code into each window and set the correct window and menu numbers
No, you don't need to do that, the procedure way can work for that. In fact, that's what it was designed for. Look here:
Fips wrote: Mon Mar 25, 2024 9:28 pm

Code: Select all

Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure
This procedure is what you call instead of copy-pasting :wink: just pass the window's number.
Thank you Fips, I will give it a try
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Post Reply