Multiple window problem

Just starting out? Need help? Post your questions and find answers here.
ElementE
Enthusiast
Enthusiast
Posts: 139
Joined: Sun Feb 22, 2015 2:33 am

Re: Multiple window problem

Post by ElementE »

This article from the PureBasic User Guide contains a code example showing the management of multiple windows.
The code consists of 708 lines of code!

UserGuide - Managing multiple windows with different content
http://www.purebasic.com/documentation/ ... _any2.html
In the previous article we examined one way in which a program can support multiple instances of a single type of window. In this one we are going to extend this concept further – developing a program that can support multiple instances of several different types of window, in this case three:

- The Button window – contains a list view and two buttons labelled 'Add' and 'Remove'. When the 'Add' button is clicked a random integer is added to the list view, when the 'Remove' button is clicked the currently highlighted entry in the list view is removed.
- The Date window – contains a list view and two buttons in the same way as the Button window but also contains a calendar gadget too, the window layout is altered to accommodate this additional control. When the 'Add' button is clicked, it is the currently selected date that is added to the list view.
- The Track window – contains two track bars, with a value between 0 and 100, and a string gadget. When the track bars are moved the string gadget is updated with the value of the second track bar subtracted from the first.
Think Unicode!
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Multiple window problem

Post by collectordave »

Hi ElementE

Read that and run.

Definately found it to be bigger and more complex.

After learning here I have posted an example of using multiple windows where you can add as many windows as you like, each doing what you want them to do, up to the limits imposed by the OS or PB simply by defining your window and adding two lines of code to the main message loop and a variable definition. 143 lines of code in main window with as many lines as you like in all other windows.

I assume a novice will understand it as a novice wrote it.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Multiple window problem

Post by collectordave »

Thanks ElementE.

Rehashed the example you quoted.

Could not understand why anyone would want to open lots of windows doing the same thing, made no sense to me.

The program I have posted here:-

http://www.codeinbasic.com/index.php?topic=266.0

Opens the same three windows with the same funcionality in a few less lines of code and no pointers etc.

If someone needs to open lots of the same windows no problem but just need to introduce some planning, if they know how many windows they need to open it is quite simple without pointers etc.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Multiple window problem

Post by TI-994A »

A simple multi-window example:

Code: Select all

Enumeration Windows
  #Window1
  #Window2
  #Window3
EndEnumeration

Enumeration Gadgets
  #Button1a
  #Button1b
  #Button2
  #Button3
  #Text1a
  #Text1b
  #Text2
  #Text3
  #Label1
  #Label2
EndEnumeration

Enumeration Timers
  #Timer1
  #Timer2
  #Timer3
EndEnumeration

Procedure Open_Window2()
  wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
  OpenWindow(#Window2, #PB_Any, #PB_Any, 400, 200, "Window 2", wFlags)
  EditorGadget(#Text2, 10, 10, 380, 130)
  ButtonGadget(#Button2, 10, 150, 380, 40, "Close Window 2")
  AddWindowTimer(#Window2, #Timer2, 1000)
  ResizeWindow(#Window2, WindowX(#Window2) - (WindowWidth(#Window2) / 2) - 15, 
               #PB_Ignore, #PB_Ignore, #PB_Ignore)
  DisableGadget(#Button1a, 1)
  ProcedureReturn
EndProcedure 

Procedure Window2_EventHandler(event.i)
  Select event
    Case #PB_Event_CloseWindow
      windowClosed = #True
    Case #PB_Event_Timer
      SetWindowTitle(#Window2, 
                     "Window 2 - " + FormatDate("%hh:%ii:%ss", Date()))      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button2
          windowClosed = #True
        Case #Text2
          Select EventType()
            Case #PB_EventType_Focus
              SetGadgetText(#Text2, "")              
            Case #PB_EventType_Change
              SetGadgetText(#Text1a, GetGadgetText(#Text2))
          EndSelect
      EndSelect
  EndSelect
  
  If windowClosed
    DisableGadget(#Button1a, 0)
    RemoveWindowTimer(#Window2, #Timer2)
    CloseWindow(#Window2)
  EndIf
  
  ProcedureReturn
EndProcedure 

Procedure Open_Window3()
  wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
  OpenWindow(#Window3, #PB_Any, #PB_Any, 400, 200, "Window 3", wFlags)
  EditorGadget(#Text3, 10, 10, 380, 130)
  ButtonGadget(#Button3, 10, 150, 380, 40, "Close Window 3")
  AddWindowTimer(#Window3, #Timer3, 1000)
  ResizeWindow(#Window3, WindowX(#Window3) + (WindowWidth(#Window3) / 2) + 15, 
               #PB_Ignore, #PB_Ignore, #PB_Ignore)
  DisableGadget(#Button1b, 1)
  ProcedureReturn
EndProcedure 

Procedure Window3_EventHandler(event.i)
  Select event
    Case #PB_Event_CloseWindow
      windowClosed = #True
    Case #PB_Event_Timer
      SetWindowTitle(#Window3, 
                     "Window 3 - " + FormatDate("%hh:%ii:%ss", Date()))
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button3
          windowClosed = #True
        Case #Text3
          Select EventType()
            Case #PB_EventType_Focus
              SetGadgetText(#Text3, "")              
            Case #PB_EventType_Change
              SetGadgetText(#Text1b, GetGadgetText(#Text3))
          EndSelect
      EndSelect
  EndSelect
  
  If windowClosed
    DisableGadget(#Button1b, 0)
    RemoveWindowTimer(#Window3, #Timer3)
    CloseWindow(#Window3)
  EndIf
  
  ProcedureReturn
EndProcedure 

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#Window1, #PB_Any, #PB_Any, 400, 250, "A Multi-Window Example", wFlags)
TextGadget(#Label1, 10, 10, 200, 20, "Window 2 mirror:")
TextGadget(#Label2, 10, 105, 200, 20, "Window 3 mirror:")
EditorGadget(#Text1a, 10, 30, 380, 65, #PB_Editor_WordWrap)
EditorGadget(#Text1b, 10, 125, 380, 65, #PB_Editor_WordWrap)
ButtonGadget(#Button1a, 10, 200, 185, 40, "Open Window 2")
ButtonGadget(#Button1b, 205, 200, 185, 40, "Open Window 3")
AddWindowTimer(#Window1, #Timer1, 1000)
ResizeWindow(#Window1, #PB_Ignore, WindowY(#Window1) - WindowHeight(#Window1) - 30,
             #PB_Ignore, #PB_Ignore)
Repeat
  event = WaitWindowEvent()
  Select EventWindow()
    Case #Window2
      Window2_EventHandler(event)
    Case #Window3
      Window3_EventHandler(event)
    Case #Window1
      Select event
        Case #PB_Event_CloseWindow
          appQuit = 1
        Case #PB_Event_Timer
          SetWindowTitle(#Window1, "A Multi-Window Example - " + 
                                   FormatDate("%hh:%ii:%ss", Date()))          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #Button1a
              If Not IsWindow(#Window2)
                Open_Window2()
              EndIf
            Case #Button1b
              If Not IsWindow(#Window3)
                Open_Window3()
              EndIf
            Case #Text1a
              Select EventType()
                Case #PB_EventType_Focus
                  SetGadgetText(#Text1a, "")
                Case #PB_EventType_Change  
                  If IsGadget(#Text2)
                    SetGadgetText(#Text2, GetGadgetText(#Text1a))   
                  EndIf
              EndSelect
            Case #Text1B
              Select EventType()
                Case #PB_EventType_Focus
                  SetGadgetText(#Text1b, "")
                Case #PB_EventType_Change  
                  If IsGadget(#Text3)
                    SetGadgetText(#Text3, GetGadgetText(#Text1b))   
                  EndIf
              EndSelect
          EndSelect
      EndSelect
  EndSelect    
Until appQuit = 1
Each window's events are handled exclusively by its own event handler, working gracefully without any issues.

No crossfires. No misfires. Just the simple beauty of PureBasic! :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply