Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Nudgy
User
User
Posts: 32
Joined: Mon May 27, 2024 8:11 pm

Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?

Post by Nudgy »

I am playing around with using "Generate event procedures" for automatic event procedure generation, with two forms:
  • Window_1
  • Window_Editor
A simple program loop works fine for programs with one window:

Code: Select all

Repeat
  event = WaitWindowEvent()
Until Window_1_Events(event) = #False ;Window_1 is the main form
But after the app shows the subwindow (called Window_Editor), via "OpenWindow_Editor()", closing the subwindow will end the entire program, and not just the subwindow. I am guessing that closing the subwindow is somehow triggering "Window_1_Events(event) = #False", and thereby ending the loop, but the mechanics of the event handling here are not clear to me.

What is the best way to handle the closing of a subwindow correctly, assuming that I want to use PureBasic's automatic event procedure generation?
SMaag
Enthusiast
Enthusiast
Posts: 385
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?

Post by SMaag »

I don't know what "Generate event procedures" is!

But I guess you try to use 2 Main Loops. 1 for the Main Window and 1 for the Child Window.

That's a problem. Only one event Loop per Application!

You have to use BindEvent or selcect your events of all windows in a single
Repeat

Until
User avatar
Nudgy
User
User
Posts: 32
Joined: Mon May 27, 2024 8:11 pm

Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?

Post by Nudgy »

Thanks. I am only using one event loop.

With "Generate event procedure", I am referring to the built-in functionality in PureBasic and its form designer to create event procedures within the form file's code itself, referring to the relevant procedures, that have been manually created in the main code. If "generate event procedure" is enabled in PureBasic settings, each form will contain something like the following in its code:

Code: Select all

Procedure Window_1_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
        Case #Toolbar_MyButton
          MyButton_Clicked(EventMenu())
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?

Post by infratec »

You need

Code: Select all

Select EventWindow()
To detect which window generates the event.
Then you can use CloseWindow() to close the sub window.
User avatar
Nudgy
User
User
Posts: 32
Joined: Mon May 27, 2024 8:11 pm

Re: Closing sub-windows correctly, when "Generate event procedure" is enabled (without closing entire app)?

Post by Nudgy »

Thanks. With this and a few additional things, it seems to work:
  1. In the last part of the event loop, the "Until" must include a check of which window is sending the event, e.g. "And Window = #Window_1" (although I still don't understand why "Window_1_Events(event) = #False" is fulfilled, when closing the sub-window.
  2. Even if the form gadgets themselves have event procedures specified via the form designer, the form itself should let the event loop handle its events, when using multiple forms.
The event loop code below works for opening sub-windows/modal windows with forms that use "generate event procedure". There's probably a more elegant solution than this, though :D

Code: Select all

   Define WindowMainID = OpenWindow_1()
   Define Window, Gadget, Menu
  
  ; Keep the window open and capture events:
  Repeat
    
    ; Get the events:
    Event = WaitWindowEvent()
    Window = EventWindow() 
    Gadget = EventGadget() 
    Menu = EventMenu()     
    
    Select Window
        
      ; ---------- MAIN WINDOW ---------
      Case #Window_1
        Select Event
          Case #PB_Event_CloseWindow
            Debug "Window1-CloseWindow"
            End
          Case #PB_Event_SizeWindow
            Debug "Resized Window1"
        EndSelect
        

      ; ---------- SUB-WINDOW ---------
      Case #Window_Editor
        Select Event  
          Case #PB_Event_CloseWindow 
            Debug "Window_Editor-CloseWindow"
            CloseWindow(#Window_Editor)
            DisableWindow(#Window_1, #False)
          Case #PB_Event_SizeWindow
            Debug "Resized WindowEditor"
        EndSelect
            
    EndSelect

  Until Window_1_Events(event) = #False And Window = #Window_1
Post Reply