2 windows

Just starting out? Need help? Post your questions and find answers here.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

2 windows

Post by t57042 »

Following code opens a window with 2 buttons. When clicking EXECUTE or pushing F5 a second window opens.
But only briefly! It Should remain visible until the same button is clicked again, which reopens and displays the second window over and over again.
What is wrong?
Richard

Code: Select all

;test
Global Window_0,Window_1
Global Editor_0, Button_1, Button_3, ListIcon_0
;==============================================================================

Procedure otherwin(txt$)
   OpenWindow(1,20, 180, 1000, 420,  "SQLviewer", #PB_Window_MinimizeGadget)
    ListIconGadget(11, 10, 5, 980, 410, "", 120, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect)
    SetGadgetColor(11, #PB_Gadget_BackColor, $DEC4B0)
    SetGadgetFont(11, FontID(1))   
EndProcedure
    
;==============================================================================

Procedure InitWindow_0()
  LoadFont(0, "tohamo", 12)
  LoadFont(1, "tohamo", 10)
  Window_0 = OpenWindow(#PB_Any, 0, 0, 1040, 640,  "SQLviewer", #PB_Window_MinimizeGadget)
  Editor_0 = EditorGadget(#PB_Any, 10, 10, 860, 130)
  Button_1 = ButtonGadget(#PB_Any, 890, 10, 130, 50, "Execute (F5)")
  Button_3 = ButtonGadget(#PB_Any, 890, 90, 130, 50, "Clear")
  CreateStatusBar(0, WindowID(Window_0 ))
  AddStatusBarField(290)
  SetWindowColor(Window_0, $DCDCDC) 
  SetGadgetFont(Editor_0, FontID(0))
  AddKeyboardShortcut(Window_0,  #PB_Shortcut_F5, 50)  ; enter
  SetActiveGadget(Editor_0) ; set focus on editor
  
  ;EVENTS
  Repeat
    Event = WaitWindowEvent()
    
    Select event
      Case #PB_Event_CloseWindow 
        If EventWindow()=Window_0
          quit=1
          ProcedureReturn #False
        Else
          CloseWindow(Window_1)
        EndIf  


      Case #PB_Event_Gadget
        Select EventGadget()
          Case Button_1
            inh$ = GetGadgetText(Editor_0)                          
            otherwin(inh$)
            SetActiveGadget(Editor_0) ; set focus on editor
          Case Button_3
            ClearGadgetItems(Editor_0)
            SetActiveGadget(Editor_0) ; set focus on editor
        EndSelect
      Case #PB_Event_Menu 
        If EventMenu() = 50 
            inh$ = GetGadgetText(Editor_0)
            otherwin(inh$)
            SetActiveGadget(Editor_0) ; set focus on editor
        EndIf  
    EndSelect
   Until quit=1  
  ProcedureReturn #True
EndProcedure
;==============================================================================
;MAIN
 InitWindow_0()

End


Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: 2 windows

Post by Julian »

I cant really tell what you want the windows to do from your explanation, however:

SetActiveGadget(Editor_0) ; set focus on editor

brings the first window to the front, if you want the 2nd window to stay on top all the time, try using

StickyWindow(#Window, State)

or dont call SetActiveWindow right after the OpenWindow
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: 2 windows

Post by TI-994A »

t57042 wrote:Following code opens a window with 2 buttons. When clicking EXECUTE or pushing F5 a second window opens.
But only briefly! It Should remain visible until the same button is clicked again, which reopens and displays the second window over and over again.
Hi Richard. I've made the following modifications:

1. the SetActiveGadget(Editor_0) in the EXECUTE button event has been removed
2. it's not recommended for the main event loop to be in a procedure; moved out
3. Window_1 has been assigned to the second window with #PB_Any
4. the EXECUTE button event now determines whether to open the second window or just activate it if already opened
5. the screen-centered flag has been assigned to both windows

Code: Select all

;test
Global Window_0,Window_1
Global Editor_0, Button_1, Button_3, ListIcon_0
;==============================================================================

Procedure otherwin(txt$)
  wFlags = #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
  Window_1 = OpenWindow(#PB_Any, 20, 180, 1000, 420,  "SQLviewer", wFlags)
  ListIconGadget(11, 10, 5, 980, 410, "", 120, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect)
  SetGadgetColor(11, #PB_Gadget_BackColor, $DEC4B0)
  SetGadgetFont(11, FontID(1))   
EndProcedure

;==============================================================================

Procedure InitWindow_0()
  LoadFont(0, "tohamo", 12)
  LoadFont(1, "tohamo", 10)
  wFlags = #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
  Window_0 = OpenWindow(#PB_Any, 0, 0, 1040, 640,  "SQLviewer", wFlags)
  Editor_0 = EditorGadget(#PB_Any, 10, 10, 860, 130)
  Button_1 = ButtonGadget(#PB_Any, 890, 10, 130, 50, "Execute (F5)")
  Button_3 = ButtonGadget(#PB_Any, 890, 90, 130, 50, "Clear")
  CreateStatusBar(0, WindowID(Window_0 ))
  AddStatusBarField(290)
  SetWindowColor(Window_0, $DCDCDC) 
  SetGadgetFont(Editor_0, FontID(0))
  AddKeyboardShortcut(Window_0,  #PB_Shortcut_F5, 50)  ; enter
  SetActiveGadget(Editor_0)                            ; set focus on editor
EndProcedure

;==============================================================================
;MAIN
InitWindow_0()

;EVENTS
Repeat
  Event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow 
      If EventWindow() = Window_0
        quit=1
      Else
        CloseWindow(Window_1)
      EndIf  
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_1
          inh$ = GetGadgetText(Editor_0)                          
          If IsWindow(Window_1)
            SetActiveWindow(Window_1)
          Else
            otherwin(inh$)
          EndIf
          ;SetActiveGadget(Editor_0) ; set focus on editor
        Case Button_3
          ClearGadgetItems(Editor_0)
          SetActiveGadget(Editor_0) ; set focus on editor
      EndSelect
    Case #PB_Event_Menu 
      If EventMenu() = 50 
        inh$ = GetGadgetText(Editor_0)
        otherwin(inh$)
        SetActiveGadget(Editor_0) ; set focus on editor
      EndIf  
  EndSelect
Until quit=1
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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: 2 windows

Post by RASHAD »

Code: Select all


;test
Global Window_0,Window_1,Run
Global Editor_0, Button_1, Button_3, ListIcon_0
;==============================================================================

Procedure otherwin(txt$)
   Window_1 = OpenWindow(#PB_Any,20, 180, 1000, 420,  "SQLviewer", #PB_Window_MinimizeGadget)
    ListIconGadget(11, 10, 5, 980, 410, "", 120, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect)
    SetGadgetColor(11, #PB_Gadget_BackColor, $DEC4B0)
    SetGadgetFont(11, FontID(1))   
EndProcedure
    
;==============================================================================

Procedure InitWindow_0()
  LoadFont(0, "tohamo", 12)
  LoadFont(1, "tohamo", 10)
  Window_0 = OpenWindow(#PB_Any, 0, 0, 1040, 640,  "SQLviewer", #PB_Window_MinimizeGadget)
  Editor_0 = EditorGadget(#PB_Any, 10, 10, 860, 130)
  Button_1 = ButtonGadget(#PB_Any, 890, 10, 130, 50, "Execute (F5)")
  Button_3 = ButtonGadget(#PB_Any, 890, 90, 130, 50, "Clear")
  CreateStatusBar(0, WindowID(Window_0 ))
  AddStatusBarField(290)
  SetWindowColor(Window_0, $DCDCDC) 
  SetGadgetFont(Editor_0, FontID(0))
  AddKeyboardShortcut(Window_0,  #PB_Shortcut_F5, 50)  ; enter
  SetActiveGadget(Editor_0) ; set focus on editor
  
  ;EVENTS
  Repeat
    Event = WaitWindowEvent()
    
    Select event
      Case #PB_Event_CloseWindow 
        If EventWindow()=Window_0
          quit=1
          ProcedureReturn #False
        Else
          CloseWindow(Window_1)
        EndIf  


      Case #PB_Event_Gadget
        Select EventGadget()
          Case Button_1
            Run ! 1
            inh$ = GetGadgetText(Editor_0)
            If Run = 1                          
              otherwin(inh$)
            Else
              CloseWindow(Window_1)
            EndIf
            SetActiveGadget(Editor_0) ; set focus on editor
          Case Button_3
            ClearGadgetItems(Editor_0)
            SetActiveGadget(Editor_0) ; set focus on editor
        EndSelect
      Case #PB_Event_Menu 
        If EventMenu() = 50
            Run ! 1 
            inh$ = GetGadgetText(Editor_0)
            If Run = 1
              otherwin(inh$)
            Else
              CloseWindow(Window_1)
            EndIf
            SetActiveGadget(Editor_0) ; set focus on editor
        EndIf  
    EndSelect
   Until quit=1  
  ProcedureReturn #True
EndProcedure
;==============================================================================
;MAIN
 InitWindow_0()

End

Egypt my love
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: 2 windows

Post by TI-994A »

RASHAD wrote:

Code: Select all

Case #PB_Event_Gadget
  Select EventGadget()
    Case Button_1
      Run ! 1
      inh$ = GetGadgetText(Editor_0)
      If Run = 1                          
        otherwin(inh$)
      Else
        CloseWindow(Window_1)
      EndIf
If the second window is closed manually, subsequent presses of the EXECUTE button would crash the program. :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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: 2 windows

Post by RASHAD »

Hi TI
I posted what the man asked for (Close - Reopen)
In the matter of fact I do not like it that way
He should use Hide - Show and clear all the item for each new show
That will be much better
Egypt my love
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: 2 windows

Post by TI-994A »

RASHAD wrote:I posted what the man asked for (Close - Reopen)
In the matter of fact I do not like it that way
He should use Hide - Show and clear all the item for each new show
That will be much better
I'm sure you're right, Rashad. :)

Nonetheless, resetting the Run flag, whenever the window is closed manually, would easily remedy the error:

Code: Select all

Select event
  Case #PB_Event_CloseWindow 
    If EventWindow()=Window_0
      quit=1
      ProcedureReturn #False
    Else
      Run = 0
      CloseWindow(Window_1)
    EndIf 
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