Page 1 of 1

Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 1:56 am
by Quin
Hi,
I have an app using BindEvent to handle all its events, and it also has multiple child windows that pop out next to the main window. When I close a child window I should be returned to the main window, the app shouldn't exit. Here's a tiny example:

Code: Select all

EnableExplicit

Enumeration Windows
#Window_Main
#Window_Popup
EndEnumeration

Enumeration Gadgets
#Gadget_PopupButton
#Gadget_CloseButton
EndEnumeration

Procedure PopupWindowEvents()
Select EventGadget()
Case #Gadget_CloseButton
CloseWindow(#Window_Popup)
EndSelect
EndProcedure

Procedure CreatePopupWindow()
OpenWindow(#Window_Popup, #PB_Ignore, #PB_Ignore, 600, 400, "Subwindow")
ButtonGadget(#Gadget_CloseButton, 10, 10, 50, 50, "Close")
BindEvent(#PB_Event_Gadget, @PopupWindowEvents())
SetActiveGadget(#Gadget_CloseButton)
EndProcedure

Procedure MainWindowEvents()
Select EventGadget()
Case #Gadget_PopupButton
CreatePopupWindow()
EndSelect
EndProcedure

Procedure MainWindowCloseHandler()
End
EndProcedure

OpenWindow(#Window_Main, #PB_Ignore, #PB_Ignore, 600, 400, "Test")
ButtonGadget(#Gadget_PopupButton, 10, 10, 50, 50, "Show popup")
BindEvent(#PB_Event_Gadget, @MainWindowEvents())
BindEvent(#PB_Event_CloseWindow, @MainWindowCloseHandler())
SetActiveGadget(#Gadget_PopupButton)
Repeat
Define Event = WaitWindowEvent(1)
ForEver
Originally, I had it be Until WaitWindowEvent(1) = #PB_Event_CloseWindow, but that didn't work because it came from any window. I also tried adding a check for And EventWindow() = #Window_Main, but that was unsuccessful, same as the approach shown above, the entire app shuts down upon me closing the child window. Providing a parent ID didn't seem to fix this either.
What am I missing?
Thanks!

Re: Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 2:46 am
by idle
you need to check for the eventwindow

Code: Select all

Procedure MainWindowCloseHandler()
If EventWindow() = #Window_Main 
End
EndIf   
EndProcedure


Re: Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 2:55 am
by Quin
idle wrote: Sun Dec 15, 2024 2:46 am you need to check for the eventwindow

Code: Select all

Procedure MainWindowCloseHandler()
If EventWindow() = #Window_Main 
End
EndIf   
EndProcedure

Oh, I see... BindEvent isn't window-specific. Is it better form to pass a WindowID to it, which also seems to need an ObjectID which is erelivent in this case, or to do it this way?

Re: Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 3:11 am
by idle
better to check for the eventwindow , if you do it with the window number in the bind event the other windows wont close

Re: Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 3:12 am
by RASHAD
Hi Quin
Maybe

Code: Select all

EnableExplicit

Declare MainWindowCloseHandler()

Enumeration Windows
  #Window_Main
  #Window_Popup
EndEnumeration

Enumeration Gadgets
  #Gadget_PopupButton
  #Gadget_CloseButton
EndEnumeration

Procedure PopupWindowEvents()
  Select EventGadget()
    Case #Gadget_CloseButton
      CloseWindow(#Window_Popup)
  EndSelect
EndProcedure

Procedure CreatePopupWindow()
  OpenWindow(#Window_Popup, #PB_Ignore, #PB_Ignore, 600, 400, "Subwindow")
  ButtonGadget(#Gadget_CloseButton, 10, 10, 50, 50, "Close")
  BindEvent(#PB_Event_Gadget, @PopupWindowEvents())
  BindEvent(#PB_Event_CloseWindow, @MainWindowCloseHandler())
  SetActiveGadget(#Gadget_CloseButton)
EndProcedure

Procedure MainWindowEvents()
  Select EventGadget()
    Case #Gadget_PopupButton
      CreatePopupWindow()
  EndSelect
EndProcedure

Procedure MainWindowCloseHandler()
  If GetActiveWindow() = #Window_Popup
    CloseWindow(#Window_Popup)
  Else
    End
  EndIf
EndProcedure

OpenWindow(#Window_Main, #PB_Ignore, #PB_Ignore, 600, 400, "Test")
ButtonGadget(#Gadget_PopupButton, 10, 10, 50, 50, "Show popup")
BindEvent(#PB_Event_Gadget, @MainWindowEvents())
BindEvent(#PB_Event_CloseWindow, @MainWindowCloseHandler())
SetActiveGadget(#Gadget_PopupButton)
Repeat
  Select  WaitWindowEvent(1)
  EndSelect
ForEver


Re: Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 3:20 am
by Quin
Thanks both of you for all your help! I got it to work! Here's my final CloseWindowHandler():

Code: Select all

Procedure CloseWindowHandler()
Select EventWindow()
Case #Window_Main
End
Default
CloseWindow(EventWindow())
EndSelect
EndProcedure
:)

Re: Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 9:49 am
by Fred
You can just specify the 'Window' param to BindEvent() if you want to restrict it to specific window

Code: Select all

BindEvent(#PB_Event_CloseWindow, @MainWindowCloseHandler(), #Window_Main)

Re: Closing child windows without shutting down the entire application?

Posted: Sun Dec 15, 2024 11:29 am
by Quin
Fred wrote: Sun Dec 15, 2024 9:49 am You can just specify the 'Window' param to BindEvent() if you want to restrict it to specific window

Code: Select all

BindEvent(#PB_Event_CloseWindow, @MainWindowCloseHandler(), #Window_Main)
Hi Fred,
Yes, you're right, this does work! However, I think I'll stick to my approach as only binding the event to the main window makes it so I can't close the child windows with alt+f4 at all to return to the parent :twisted:
Thanks for all your help!