WaitWindowClose()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

WaitWindowClose()

Post by mestnyi »

I propose to make the following options, what would you say?
some of the benefits:
1) do not need to write each time a lot of code.
2) you can specify the main window.
3) you can duplicate the event the gadget.

Code: Select all

;Name = WaitWindowClose.pbi

Enumeration #PB_Event_FirstCustomValue
   #PB_Event_Quit
EndEnumeration

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
   Import ""
   CompilerElse
      ImportC ""
      CompilerEndIf
      PB_Object_Count( Objects )
      PB_Window_Objects.i
   EndImport
   Procedure CountWindow( ) 
      ProcedureReturn PB_Object_Count( PB_Window_Objects )
   EndProcedure
   
   Procedure CloseWindowAll()
      CloseWindow(#PB_All)
   EndProcedure
   Procedure __CloseWindow( Window )
      If Window = #PB_All
         BindEvent( #PB_Event_Quit, @CloseWindowAll(), EventWindow())
         PostEvent( #PB_Event_Quit, EventWindow(), #PB_Ignore ) ; - 1 bug
      Else
         If EventWindow() = Window
            ProcedureReturn CloseWindow( Window )
         Else
            PostEvent( #PB_Event_CloseWindow, Window, #PB_Ignore ) ; - 1 bug
         EndIf
      EndIf
   EndProcedure 
   Macro CloseWindow( Window ) 
      __CloseWindow( Window ) 
   EndMacro
   
   Procedure IsBindEvent( Window )
      If IsWindow(Window) 
         CompilerSelect #PB_Compiler_OS 
            CompilerCase #PB_OS_Windows
               ProcedureReturn GetProp_(WindowID(Window), "BindWindowEvent")
            CompilerCase #PB_OS_MacOS
               ProcedureReturn objc_getAssociatedObject_(WindowID(Window), "BindWindowEvent" )
         CompilerEndSelect
      EndIf
   EndProcedure
   
   Procedure __BindEvent( Event, *CallBack, Window, Object, EventType)
      If IsWindow(Window) 
         CompilerSelect #PB_Compiler_OS 
            CompilerCase #PB_OS_Windows
               SetProp_(WindowID(Window), "BindWindowEvent", #True)
            CompilerCase #PB_OS_MacOS
               objc_setAssociatedObject_( WindowID(Window), "BindWindowEvent", #True, 0 ) 
         CompilerEndSelect
      EndIf
      BindEvent( Event, *CallBack, Window, Object, EventType)
   EndProcedure
   Macro BindEvent( Event, CallBack, Window = -1, Object = -1, EventType = -1)
      __BindEvent( Event, CallBack, Window, Object, EventType)
   EndMacro
   
   Procedure WaitWindowClose( Window = #PB_All )
      While Event <> #PB_Event_Quit 
         Event = WaitWindowEvent()
         If IsWindow( EventWindow() ) 
            If IsBindEvent( EventWindow() ) : Continue : EndIf 
            Select Event
               Case #PB_Event_CloseWindow 
                  If ( EventWindow() = Window Or Window = - CountWindow() ) 
                     CloseWindow( #PB_All )
                  Else
                     CloseWindow( EventWindow() )
                  EndIf
                  
               Case #PB_Event_Gadget
                  Debug "Event_Gadget"
            EndSelect
         EndIf
      Wend
   EndProcedure
   
   
   CompilerIf #PB_Compiler_IsMainFile
      OpenWindow(1, 0, 0, 322, 150, "window 1", #PB_Window_SystemMenu )
      ButtonGadget(-1,10,10,100,30,"Button_window_1")
      OpenWindow(2, 0, 200, 322, 150, "Window 2", #PB_Window_SystemMenu )
      ButtonGadget(-1,10,10,100,30,"Button_window_2")
      OpenWindow(4, 0, 0, 322, 150, "window 4", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
      ButtonGadget(-1,10,10,100,30,"Close a window 1")
      
      
      Procedure Event_Window_1()
         Debug "Close a window"
         If MessageRequester("Warning!!!","Close a window "+Str(EventWindow())+"?",#PB_MessageRequester_YesNo ) = #PB_MessageRequester_Yes 
            CloseWindow( EventWindow() ) 
         EndIf
      EndProcedure
      
      Procedure Event_Window_4()
         If MessageRequester("Warning!!!","Close application "+Str(EventWindow())+"?",#PB_MessageRequester_YesNo ) = #PB_MessageRequester_Yes 
            CloseWindow( #PB_All ) 
         EndIf
      EndProcedure
      
      Procedure EventGadget_Windows()
         Debug "Gadget"
         CloseWindow(1)
      EndProcedure
      
      BindEvent( #PB_Event_CloseWindow, @Event_Window_1(), 1)
      BindEvent( #PB_Event_CloseWindow, @Event_Window_4(), 4)
      BindEvent( #PB_Event_Gadget, @EventGadget_Windows(), 4)
      
      
      Procedure Quit()
         Debug "Quit "+EventWindow()
         End
      EndProcedure
      
      BindEvent( #PB_Event_Quit, @Quit())
      
      WaitWindowClose( 4 ) 
   CompilerEndIf