I googled and came up with this (see below-this is googles code). It all look pretty straightforward but I'm confused as to whether there needs to be a separate event loop in the modal procedure? The comments below the code were also interesting.
I think a few years ago I made a 'modal window' that used the main event loop but I didn't like how it was coded. Having the event loop in the modal window is much simpler but I was told that you should only have one event loop,
Code: Select all
; Main Window
Enumeration
#MainWindow
#OpenModalButton
EndEnumeration
If OpenWindow(#MainWindow, 100, 100, 400, 300, "Main Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#OpenModalButton, 100, 100, 200, 30, "Open Modal Window")
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
If EventGadget() = #OpenModalButton
; Open the modal window
ModalWindow()
EndIf
Case #PB_Event_CloseWindow
If EventWindow() = #MainWindow
End
EndIf
EndSelect
Forever
EndIf
Procedure ModalWindow()
; Modal Window
Enumeration
#ModalWindow
#CloseModalButton
EndEnumeration
If OpenWindow(#ModalWindow, 200, 200, 300, 150, "Modal Dialog", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Dialog)
ButtonGadget(#CloseModalButton, 100, 80, 100, 30, "Close")
; Disable the main window while the modal is open
DisableWindow(#MainWindow, #True)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
If EventGadget() = #CloseModalButton
Break ; Exit the modal loop
EndIf
Case #PB_Event_CloseWindow
If EventWindow() = #ModalWindow
Break ; Exit the modal loop
EndIf
EndSelect
ForEver
CloseWindow(#ModalWindow)
; Re-enable the main window
DisableWindow(#MainWindow, #False)
EndIf
EndProcedureMain Window: An initial window is created with a button to trigger the modal.
ModalWindow() Procedure:
A new window (#ModalWindow) is opened. The #PB_Window_Dialog flag is important here as it gives the window a typical dialog appearance.
DisableWindow(#MainWindow, #True) is used to prevent interaction with the main window while the modal is active, enforcing its "modality."
A Repeat...Forever loop handles events specifically for the modal window. The loop continues until the close button is clicked or the window is closed.
CloseWindow(#ModalWindow) closes the modal window.
DisableWindow(#MainWindow, #False) re-enables the main window, allowing user interaction again.
This approach ensures that the user must interact with and close the modal window before they can return to the main application window.

