Use one menu for multiple windows
- Distorted Pixel
- Enthusiast
- Posts: 303
- Joined: Sun Aug 29, 2021 4:34 am
Use one menu for multiple windows
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)
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? 
No one cares how much you know until they know how much you care

No one cares how much you know until they know how much you care
Re: Use one menu for multiple windows
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
- Distorted Pixel
- Enthusiast
- Posts: 303
- Joined: Sun Aug 29, 2021 4:34 am
Re: Use one menu for multiple windows
Thank you AZJIO,AZJIO wrote: Sun Mar 24, 2024 4:54 pmCode: 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
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? 
No one cares how much you know until they know how much you care

No one cares how much you know until they know how much you care
Re: Use one menu for multiple windows
To do this, you need to close the window and see what happens. You can conduct the tests yourself.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
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
- Distorted Pixel
- Enthusiast
- Posts: 303
- Joined: Sun Aug 29, 2021 4:34 am
Re: Use one menu for multiple windows
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? 
No one cares how much you know until they know how much you care

No one cares how much you know until they know how much you care
Re: Use one menu for multiple windows
The help manual may be the answer!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. [...]

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...
Except on this sentence...
Re: Use one menu for multiple windows
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
- Distorted Pixel
- Enthusiast
- Posts: 303
- Joined: Sun Aug 29, 2021 4:34 am
Re: Use one menu for multiple windows
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 numbersFips 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
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? 
No one cares how much you know until they know how much you care

No one cares how much you know until they know how much you care
Re: Use one menu for multiple windows
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: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
This procedure is what you call instead of copy-pastingFips wrote: Mon Mar 25, 2024 9:28 pmCode: 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

- Distorted Pixel
- Enthusiast
- Posts: 303
- Joined: Sun Aug 29, 2021 4:34 am
Re: Use one menu for multiple windows
Thank you Fips, I will give it a tryQuin wrote: Tue Mar 26, 2024 3:43 pmNo, 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: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 numbersThis procedure is what you call instead of copy-pastingFips wrote: Mon Mar 25, 2024 9:28 pmCode: 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
just pass the window's number.
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? 
No one cares how much you know until they know how much you care

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