How to set window close event to match each window?

Just starting out? Need help? Post your questions and find answers here.
hdt888
User
User
Posts: 56
Joined: Sun Jul 07, 2024 8:42 am

How to set window close event to match each window?

Post by hdt888 »

I have 3 windows: exit, welcome and main.

Nothing happened until I tried put the #PB_Event_CloseWindow event for each Windows. It didn't work as expected.

How to set window close event to match each window?

That means I want to separate the #PB_Event_CloseWindow event into each window's own module. Not put it in the "main.pbi" module.

And so, the "main.pbi" module is left with just the following simple line of code:

Code: Select all

Repeat : WaitWindowEvent() : ForEver
1. declare_program.pbi

Code: Select all

DeclareModule declare_program
  
  Enumeration windows
  EndEnumeration
  
  Enumeration gadgets
  EndEnumeration
  
  Structure my_data
    welcome.i
  EndStructure
  
  Global.my_data *mydata = AllocateStructure(my_data)
  
EndDeclareModule

Module declare_program
  *mydata\welcome = #False
EndModule

DeclareModule exit_window
  UseModule declare_program
  
  Enumeration windows
    #ui_exit_window
  EndEnumeration
  
  Enumeration gadgets
    #exit_btn_yes
    #exit_btn_no
  EndEnumeration
  
  Declare open_exit_window()
  
EndDeclareModule

DeclareModule welcome_window
  UseModule declare_program
  
  Enumeration windows
    #ui_welcome_window
  EndEnumeration
  
  Enumeration gadgets
    #welcome_btn_start
  EndEnumeration
  
  Declare open_welcome_window()
  
EndDeclareModule

DeclareModule main_window
  UseModule declare_program
  
  Enumeration windows
    #ui_main_window
  EndEnumeration
  
  Enumeration gadgets
    #main_btn_1
    #main_btn_2
  EndEnumeration
  
  Declare open_main_window()
  
EndDeclareModule
2. main.pbi (main to build/run)

Code: Select all

XIncludeFile "declare_program.pbi"
XIncludeFile "ui_exit.pbi"
XIncludeFile "ui_welcome.pbi"
XIncludeFile "ui_main.pbi"

UseModule declare_program
UseModule exit_window
UseModule welcome_window
UseModule main_window

If *mydata\welcome = #True
  open_welcome_window()
Else
  open_main_window()
EndIf

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Select EventWindow()
        Case #ui_welcome_window
          DisableWindow(#ui_welcome_window, #True)
          open_exit_window()
        Case #ui_main_window
          DisableWindow(#ui_main_window, #True)
          open_exit_window()
        Case #ui_exit_window
          If IsWindow(#ui_welcome_window)
            DisableWindow(#ui_welcome_window, #False)
          EndIf
          If IsWindow(#ui_main_window)
            DisableWindow(#ui_main_window, #False)
          EndIf
          CloseWindow(#ui_exit_window)
      EndSelect 
  EndSelect
ForEver
3. ui_exit.pbi

Code: Select all

Module exit_window
  
  UseModule welcome_window
  UseModule main_window
  
  Procedure events_exit_window()
    Select Event()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #exit_btn_no : PostEvent(#PB_Event_CloseWindow, #ui_exit_window, #PB_Ignore)
          Case #exit_btn_yes : End
        EndSelect
    EndSelect
  EndProcedure
  
  Procedure open_exit_window()
    OpenWindow(#ui_exit_window, #PB_Ignore, #PB_Ignore, 190, 100, "Exit ?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#exit_btn_no, 10, 50, 80, 45, "No")
    ButtonGadget(#exit_btn_yes, 100, 50, 80, 45, "Yes")
    BindEvent(#PB_Event_Menu, @events_exit_window())
    BindEvent(#PB_Event_Gadget, @events_exit_window())
  EndProcedure
EndModule
4. ui_welcome.pbi

Code: Select all

Module welcome_window
  
  UseModule exit_window
  UseModule main_window
  
  Procedure welcome_btn_start_handler()
    HideWindow(#ui_welcome_window, #True)
    open_main_window()
  EndProcedure
  
  Procedure events_welcome_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #welcome_btn_start : CloseWindow(#ui_welcome_window) : open_main_window()
          EndSelect
      EndSelect
  EndProcedure
  
  Procedure open_welcome_window()
    OpenWindow(#ui_welcome_window, #PB_Ignore, #PB_Ignore, 300, 400, "Welcome Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#welcome_btn_start, 10, 360, 280, 30, "Start")
    BindEvent(#PB_Event_Menu, @events_welcome_window())
    BindEvent(#PB_Event_Gadget, @events_welcome_window())
  EndProcedure

EndModule
5. ui_main.pbi

Code: Select all

Module main_window
  
  UseModule exit_window
  
  Procedure events_main_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #main_btn_1 : Debug "#main_btn_1"
            Case #main_btn_2 : Debug "#main_btn_2"
          EndSelect
      EndSelect
  EndProcedure
  
  Procedure open_main_window()
    OpenWindow(#ui_main_window, #PB_Ignore, #PB_Ignore, 600, 400, "Main Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#main_btn_1, 10, 360, 280, 30, "Button 1")
    ButtonGadget(#main_btn_2, 300, 360, 280, 30, "Button 2")
    BindEvent(#PB_Event_Menu, @events_main_window())
    BindEvent(#PB_Event_Gadget, @events_main_window())
  EndProcedure
EndModule
Thanks for support.
PB 5.x + 6.x + Win10. Feel the ...Pure... Power.
User avatar
STARGÅTE
Addict
Addict
Posts: 2258
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to set window close event to match each window?

Post by STARGÅTE »

You can bind the event on each window separately:

Code: Select all

BindEvent(#PB_Event_CloseWindow, @Callback(), #Window) 
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
ChrisR
Addict
Addict
Posts: 1484
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: How to set window close event to match each window?

Post by ChrisR »

Yes, I was writing...
I don't have time to test it right now. but just by looking at the shared codes

Code: Select all

BindEvent(#PB_Event_Menu, @events_main_window())
BindEvent(#PB_Event_Gadget, @events_main_window())
Is used multiple times, with different procedures and without specifying the window
it would be better with:

Code: Select all

BindGadgetEvent(#main_btn_1, @event_main_btn_1())
BindGadgetEvent(#main_btn_2, @event_main_btn_2())
BindEvent(#PB_Event_Menu, @event_menu_main_window(), #ui_main_window)
BindEvent(#PB_Event_CloseWindow, @Event_close_main_window(), #ui_main_window)
with the event procedures for each window, Gadget

Code: Select all

Procedure Event_close_main_window()
  Quit = #True
EndProcedure
To facilitate testing, you could concatenate the different *.pbi files into a single one, just copy the code in place of XIncludeFile
User avatar
TI-994A
Addict
Addict
Posts: 2747
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to set window close event to match each window?

Post by TI-994A »

Like @STARGÅTE and @ChrisR had said, bind the close window events to each window expressly. Here's the integrated, working example of your code with these amendments:

Code: Select all

; declare_program.pbi

DeclareModule declare_program
  
  Enumeration windows
  EndEnumeration
  
  Enumeration gadgets
  EndEnumeration
  
  Structure my_data
    welcome.i
  EndStructure
  
  Global.my_data *mydata = AllocateStructure(my_data)
  
EndDeclareModule

Module declare_program
  *mydata\welcome = #True
EndModule

DeclareModule exit_window
  UseModule declare_program
  
  Enumeration windows
    #ui_exit_window
  EndEnumeration
  
  Enumeration gadgets
    #exit_btn_yes
    #exit_btn_no
  EndEnumeration
  
  Declare open_exit_window()
  
EndDeclareModule

DeclareModule welcome_window
  UseModule declare_program
  
  Enumeration windows
    #ui_welcome_window
  EndEnumeration
  
  Enumeration gadgets
    #welcome_btn_start
  EndEnumeration
  
  Declare open_welcome_window()
  
EndDeclareModule

DeclareModule main_window
  UseModule declare_program
  
  Enumeration windows
    #ui_main_window
  EndEnumeration
  
  Enumeration gadgets
    #main_btn_1
    #main_btn_2
  EndEnumeration
  
  Declare open_main_window()
  
EndDeclareModule



; ui_exit.pbi

Module exit_window
  
  UseModule welcome_window
  UseModule main_window
    
  Procedure events_exit_window()
    Select Event()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #exit_btn_no : PostEvent(#PB_Event_CloseWindow, #ui_exit_window, #PB_Ignore)
          Case #exit_btn_yes : End
        EndSelect
      Case #PB_Event_CloseWindow
        CloseWindow(#ui_exit_window)
        If IsWindow(#ui_welcome_window)
          DisableWindow(#ui_welcome_window, #False)
        Else
          DisableWindow(#ui_main_window, #False)
        EndIf        
    EndSelect
  EndProcedure
     
  Procedure open_exit_window()
    OpenWindow(#ui_exit_window, #PB_Ignore, #PB_Ignore, 190, 100, "Exit ?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#exit_btn_no, 10, 50, 80, 45, "No")
    ButtonGadget(#exit_btn_yes, 100, 50, 80, 45, "Yes")
    BindEvent(#PB_Event_Menu, @events_exit_window(), #ui_exit_window)
    BindEvent(#PB_Event_Gadget, @events_exit_window(), #ui_exit_window)
    BindEvent(#PB_Event_CloseWindow, @events_exit_window(), #ui_exit_window)
  EndProcedure
EndModule



; ui_welcome.pbi

Module welcome_window
  
  UseModule exit_window
  UseModule main_window
  
  Procedure welcome_btn_start_handler()
    HideWindow(#ui_welcome_window, #True)
    open_main_window()
  EndProcedure
  
  Procedure events_welcome_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #welcome_btn_start : CloseWindow(#ui_welcome_window) : open_main_window()
          EndSelect
        Case #PB_Event_CloseWindow
          DisableWindow(#ui_welcome_window, #True)
          open_exit_window()
      EndSelect
  EndProcedure
  
  Procedure open_welcome_window()
    OpenWindow(#ui_welcome_window, #PB_Ignore, #PB_Ignore, 300, 400, "Welcome Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#welcome_btn_start, 10, 360, 280, 30, "Start")
    BindEvent(#PB_Event_Menu, @events_welcome_window(), #ui_welcome_window)
    BindEvent(#PB_Event_Gadget, @events_welcome_window(), #ui_welcome_window)
    BindEvent(#PB_Event_CloseWindow, @events_welcome_window(), #ui_welcome_window)
  EndProcedure

EndModule



; ui_main.pbi

Module main_window
  
  UseModule exit_window
  
  Procedure events_main_window()
    Select Event()
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #main_btn_1 : Debug "#main_btn_1"
            Case #main_btn_2 : Debug "#main_btn_2"
          EndSelect
        Case #PB_Event_CloseWindow
          DisableWindow(#ui_main_window, #True)
          open_exit_window()
      EndSelect
  EndProcedure
  
  Procedure open_main_window()
    OpenWindow(#ui_main_window, #PB_Ignore, #PB_Ignore, 600, 400, "Main Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(#main_btn_1, 10, 360, 280, 30, "Button 1")
    ButtonGadget(#main_btn_2, 300, 360, 280, 30, "Button 2")
    BindEvent(#PB_Event_Menu, @events_main_window(), #ui_main_window)
    BindEvent(#PB_Event_Gadget, @events_main_window(), #ui_main_window)
    BindEvent(#PB_Event_CloseWindow, @events_main_window(), #ui_main_window)
  EndProcedure
EndModule

UseModule declare_program
UseModule exit_window
UseModule welcome_window
UseModule main_window

If *mydata\welcome = #True
  open_welcome_window()
Else
  open_main_window()
EndIf

Repeat : WaitWindowEvent() : ForEver
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