managing multiple windows

Just starting out? Need help? Post your questions and find answers here.
Jacobite
User
User
Posts: 16
Joined: Fri Nov 30, 2012 7:21 pm

managing multiple windows

Post by Jacobite »

I'm just getting started with PB and want to create a window which opens another window when you click a button. I've got it working but when you close the 2nd window using the default "x" in the top right corner, both the windows are closed and not just the 2nd one. Can anyone tell me what I'm doing wrong? Thanks.

Code: Select all

Enumeration
  #window1
  #window2
  #button1_1
  #button1_2
  #button2_1
  #button2_2
EndEnumeration

OpenWindow(#window1, 0, 0, 230, 90, "Window 1", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#button1_1, 10, 10, 200, 25, "open window 2")
ButtonGadget(#button1_2, 10, 50, 200, 25, "Test")
      
OpenWindow(#window2, 0, 0, 230, 90, "Window 2", #PB_Window_SystemMenu)
ButtonGadget(#button2_1, 10, 10, 200, 25, "Test")
ButtonGadget(#button2_2, 10, 50, 200, 25, "Close this window")
HideWindow(#window2,1)   

Repeat
 Event = WaitWindowEvent()
 Window = EventWindow()
 Gadget = EventGadget()
 Select Window
   Case #window1
     Select Event
       Case #PB_Event_Gadget
         Select Gadget
           Case #button1_1 : HideWindow(#window2, 0) ; show window 2
           Case #button1_2 : Debug "window 1 Test button clicked"
         EndSelect
       Case #PB_Event_CloseWindow
         End
     EndSelect
   Case #window2
     Select Event
       Case #PB_Event_Gadget
         Select Gadget
           Case #button2_1 : Debug "window 2 Test button clicked"
           Case #button2_2 : HideWindow(#window2, 1) ; hide window 2
         EndSelect
       Case #PB_Event_CloseWindow
         HideWindow(#window2, 1) ; hide window 2
     EndSelect
 EndSelect

Until Event = #PB_Event_CloseWindow

edit: I've just been looking at the help file and I see that there might be an easier way to do this using #PB_Window_Invisible and ParentWindowID in the OpenWindow() command. Not sure how to do this though so any help gratefully received.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: managing multiple windows

Post by spikey »

Your event loop terminates when a #PB_Event_CloseWindow is received - from either window - then the end of the program is reached and PB tidies up after you automatically.

I normally use a flag variable something like the following. If you just hide the window it will continue to occupy all the resources it needs to exist - not a problem with small windows but in larger windows with lots of memory usage this might be a concern - closing the window completely frees up its resources.

Doing it this way also allows you to provide any clean up code you need to do before the program ends too...

Code: Select all

Enumeration
  #window1
  #window2
  #button1_1
  #button1_2
  #button2_1
  #button2_2
EndEnumeration

Define.I ReadyToQuit

OpenWindow(#window1, 0, 0, 230, 90, "Window 1", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#button1_1, 10, 10, 200, 25, "open window 2")
ButtonGadget(#button1_2, 10, 50, 200, 25, "Test")
      
OpenWindow(#window2, 0, 0, 230, 90, "Window 2", #PB_Window_SystemMenu)
ButtonGadget(#button2_1, 10, 10, 200, 25, "Test")
ButtonGadget(#button2_2, 10, 50, 200, 25, "Close this window")
HideWindow(#window2,1)   

Repeat
 Event = WaitWindowEvent()
 Window = EventWindow()
 Gadget = EventGadget()
 Select Window
   Case #window1
     Select Event
       Case #PB_Event_Gadget
         Select Gadget
           Case #button1_1 : HideWindow(#window2, 0) ; show window 2
           Case #button1_2 : Debug "window 1 Test button clicked"
         EndSelect
       Case #PB_Event_CloseWindow
          ; Can tidy up here too
         ReadyToQuit = #True
     EndSelect
   Case #window2
     Select Event
       Case #PB_Event_Gadget
         Select Gadget
           Case #button2_1 : Debug "window 2 Test button clicked"
           Case #button2_2 : HideWindow(#window2, 1) ; hide window 2
         EndSelect
       Case #PB_Event_CloseWindow
         HideWindow(#window2, 1) ; hide window 2
     EndSelect
 EndSelect

Until ReadyToQuit

End
Jacobite
User
User
Posts: 16
Joined: Fri Nov 30, 2012 7:21 pm

Re: managing multiple windows

Post by Jacobite »

Thanks spikey. The application I have in mind should not allow any controls on window 1 to be used when window 2 is open, I think this is called a "modal" window?
In that case I think I need to use "parent" and "child" windows for this purpose. Not quite sure how to do this yet but if I get stuck I'll be back. Thanks again.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: managing multiple windows

Post by spikey »

Use DisableWindow(#window1, 1) when you create/show #window2 then DisableWindow(#window1, 0) when you're finished with it.
Jacobite
User
User
Posts: 16
Joined: Fri Nov 30, 2012 7:21 pm

Re: managing multiple windows

Post by Jacobite »

Ah, simple! thanks again. :)
Jacobite
User
User
Posts: 16
Joined: Fri Nov 30, 2012 7:21 pm

Re: managing multiple windows

Post by Jacobite »

Just playing around, here's another way of doing it using procedures. The child window doesn't have a border so you can't use the X button to close it.

Code: Select all

Enumeration
  #parent
  #child
  #parentButton1
  #parentButton2
  #childButton1
  #childButton2
EndEnumeration

Procedure OpenChild()
  If OpenWindow(#child, 0, 0, 230, 90, "window 2", #PB_Window_BorderLess|#PB_Window_WindowCentered, #parent)
    ButtonGadget(#childButton1, 10, 10, 200, 25, "Test")
    ButtonGadget(#childButton2, 10, 50, 200, 25, "Close this window")
  EndIf
EndProcedure

Procedure OpenParent()
If OpenWindow(#parent, 0, 0, 230, 90, "Window 1", #PB_Window_ScreenCentered)
  ButtonGadget(#parentButton1, 10, 10, 200, 25, "open window 2")
  ButtonGadget(#parentButton2, 10, 50, 200, 25, "Test")
EndIf
EndProcedure

OpenParent()

Repeat
  Event = WaitWindowEvent()
  Gadget = EventGadget()
  Select Event
    Case #PB_Event_Gadget
      Select Gadget
        Case #parentButton1
          DisableWindow(#parent,#True)
          OpenChild()
        Case #parentButton2
          Debug "window 1 Test button clicked"
        Case #childButton1
          Debug "window 2 Test button clicked"
        Case #childButton2
          CloseWindow(#child)
          DisableWindow(#parent, #False)
      EndSelect
  EndSelect 
    
Until Event = #PB_Event_CloseWindow

End
Post Reply