Ramses800 wrote:...is it possible to use a OpenWindow in a Module and get the events for that seconday windows gadgets to work?
...selfcontained module(with its own window)...
...to use variable names in the module without conflicting with the main window...
That's exactly what modules are for.
This example implements two modules, both with exactly the same gadget variables, used side by side without any naming conflicts. It utilises the
UseModule/UnuseModule function to isolate their respective namespaces:
Code: Select all
DeclareModule Win1
Define.i window, text, string, button
Declare Init()
EndDeclareModule
DeclareModule Win2
Define.i window, text, string, button
Global.s forWin2_andBeyond
Declare Init()
EndDeclareModule
Module Win1
;this GLOBAL variable IS NOT DECLARED in the module declaration
;so it is only available within the Win1 module (Private)
Global.s forWin1_Only = "Win1 String (Private)"
Procedure Init()
Shared window, string, button
window = OpenWindow(#PB_Any, 100, 200, 300, 200, "Win1 Window", #PB_Window_SystemMenu)
text = TextGadget(#PB_Any, 50, 30, 250, 30, "Closing this window ends the app...")
string = StringGadget(#PB_Any, 50, 80, 200, 30, forWin1_Only)
button = ButtonGadget(#PB_Any, 50, 130, 200, 30, "Win1 Button")
EndProcedure
EndModule
Module Win2
;this GLOBAL variable IS DECLARED in the module declaration
;so it is available within & without the Win2 module (Public)
forWin2_andBeyond = "Win2 String (Public)"
Procedure Init()
Shared window, string, button
window = OpenWindow(#PB_Any, 440, 200, 300, 200, "Win2 Window", #PB_Window_SystemMenu)
text = TextGadget(#PB_Any, 50, 30, 250, 30, "Closing this window does not end the app...")
string = StringGadget(#PB_Any, 50, 80, 200, 30, forWin2_andBeyond)
button = ButtonGadget(#PB_Any, 50, 130, 200, 30, "Press to see Win2 variable...")
EndProcedure
EndModule
Win1::Init()
Win2::Init()
Repeat
event = WaitWindowEvent()
Select EventWindow()
Case Win1::window
UseModule Win1
Select event
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case string
Select EventType()
Case #PB_EventType_Change
SetGadgetText(button, "Typing...")
EndSelect
Case button
SetGadgetText(string, "You pressed Win1::button!")
EndSelect
UnuseModule Win1
EndSelect
Case Win2::window
UseModule Win2
Select event
Case #PB_Event_CloseWindow
CloseWindow(window)
Case #PB_Event_Gadget
Select EventGadget()
Case string
Select EventType()
Case #PB_EventType_Change
SetGadgetText(button, "Typing...")
EndSelect
Case button
If Not seenOnce
MessageRequester("Win2::Module", forWin2_andBeyond)
SetGadgetText(button, "Win2 Button")
seenOnce = #True
Else
SetGadgetText(string, "You pressed Win2::button!")
EndIf
EndSelect
EndSelect
UnuseModule Win2
EndSelect
Until appQuit = 1
This next example adds another window in the global scope, also using the same gadget variables. However, in such instances, the
UseModule/UnuseModule function would cause name conflicts, so the module elements are referenced directly with the module separators
(::) instead:
Code: Select all
DeclareModule Win1
Define.i window, text, string, button
Declare Init()
EndDeclareModule
DeclareModule Win2
Define.i window, text, string, button
Global.s forWin2_andBeyond
Declare Init()
EndDeclareModule
Module Win1
;this GLOBAL variable IS NOT DECLARED in the module declaration
;so it is only available within the Win1 module (Private)
Global.s forWin1_Only = "Win1 String (Private)"
Procedure Init()
Shared window, string, button
window = OpenWindow(#PB_Any, 100, 460, 300, 200, "Win1 Window", #PB_Window_SystemMenu)
text = TextGadget(#PB_Any, 50, 30, 250, 30, "Closing this window ends the app...")
string = StringGadget(#PB_Any, 50, 80, 200, 30, forWin1_Only)
button = ButtonGadget(#PB_Any, 50, 130, 200, 30, "Win1 Button")
EndProcedure
EndModule
Module Win2
;this GLOBAL variable IS DECLARED in the module declaration
;so it is available within & without the Win2 module (Public)
forWin2_andBeyond = "Win2 String (Public)"
Procedure Init()
Shared window, string, button
window = OpenWindow(#PB_Any, 440, 460, 300, 200, "Win2 Window", #PB_Window_SystemMenu)
text = TextGadget(#PB_Any, 50, 30, 250, 30, "Closing this window does not end the app...")
string = StringGadget(#PB_Any, 50, 80, 200, 30, forWin2_andBeyond)
button = ButtonGadget(#PB_Any, 50, 130, 200, 30, "Press to see Win2 variable...")
EndProcedure
EndModule
Win1::Init()
Win2::Init()
Define.s forMainWindowOnly = "MainWindow String"
Define.i window = OpenWindow(#PB_Any, 250, 200, 300, 200, "MainWindow", #PB_Window_SystemMenu)
Define.i text = TextGadget(#PB_Any, 50, 30, 250, 30, "Closing this window ends the app...")
Define.i string = StringGadget(#PB_Any, 50, 80, 200, 30, forMainWindowOnly)
Define.i button = ButtonGadget(#PB_Any, 50, 130, 200, 30, "MainWindow Button")
Repeat
event = WaitWindowEvent()
Select EventWindow()
Case window
Select event
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case string
Select EventType()
Case #PB_EventType_Change
SetGadgetText(button, "Typing...")
EndSelect
Case button
SetGadgetText(string, "You pressed MainWindow button!")
EndSelect
EndSelect
Case Win1::window
Select event
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case Win1::string
Select EventType()
Case #PB_EventType_Change
SetGadgetText(Win1::button, "Typing...")
EndSelect
Case Win1::button
SetGadgetText(Win1::string, "You pressed Win1::button!")
EndSelect
EndSelect
Case Win2::window
Select event
Case #PB_Event_CloseWindow
CloseWindow(Win2::window)
Case #PB_Event_Gadget
Select EventGadget()
Case Win2::string
Select EventType()
Case #PB_EventType_Change
SetGadgetText(Win2::button, "Typing...")
EndSelect
Case Win2::button
If Not seenOnce
MessageRequester("Win2::Module", Win2::forWin2_andBeyond)
SetGadgetText(Win2::button, "Win2 Button")
seenOnce = #True
Else
SetGadgetText(Win2::string, "You pressed Win2::button!")
EndIf
EndSelect
EndSelect
EndSelect
Until appQuit = 1
I threw in the
forWin1_Only and
forWin2_andBeyond variables simply to illustrate module scopes.
Hope it helps.
