Page 1 of 1

WindowClose

Posted: Wed Jan 28, 2015 10:04 pm
by mestnyi

Code: Select all

   CompilerIf Not Defined(IsCloseWindow, #PB_Function) And
           Not Defined(IsCloseWindow, #PB_Procedure) 
    EnableExplicit
  
  CompilerIf Not Defined(PB_Object_Count, #PB_Function) And
             Not Defined(PB_Object_Count, #PB_Procedure) And 
             Not Defined(CountWindow, #PB_Function) And
             Not Defined(CountWindow, #PB_Procedure)
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      Import ""
      CompilerElse
        ImportC ""
        CompilerEndIf
        PB_Object_Count( Objects )
        PB_Window_Objects.l
      EndImport
    CompilerEndIf
    
    ProcedureDLL IsCloseWindow2( Window = #PB_All) 
      If Window ! #PB_All
        If ((Not IsWindow( EventWindow() )) And EventWindow() = Window )
          ProcedureReturn #True
        EndIf
      Else
        If Not PB_Object_Count( PB_Window_Objects )
          ProcedureReturn #True
        EndIf
      EndIf
    EndProcedure
    ProcedureDLL IsCloseWindow( ) ;Returns TRUE is window close
      ProcedureReturn IsCloseWindow2( #PB_All ) 
    EndProcedure
    Macro IsCloseWindow( Window = #PB_All) ;Returns TRUE is window close
      IsCloseWindow2( Window )  
    EndMacro
    
    DisableExplicit
  CompilerEndIf
  
  CompilerIf Not Defined(WindowClose, #PB_Function) And
             Not Defined(WindowClose, #PB_Procedure) 
    Procedure Event()
      SetEnvironmentVariable("Window",Str(EventWindow()+1))
      SetEnvironmentVariable("Event",Str(#PB_Event_CloseWindow))
      If PB_Object_Count( PB_Window_Objects ) = 1
        UnbindEvent(#PB_Event_CloseWindow,@event())
      EndIf
    EndProcedure
    
    ProcedureDLL WindowClose4(MainWindow =-1,Message =-1,Text$="Exit?",Title$="")
      Static Run
      Protected Window = Val(GetEnvironmentVariable("Window"))-1
      Protected Event = Val(GetEnvironmentVariable("Event"))
      
      If Run = 0 
        Run = 1
        BindEvent(#PB_Event_CloseWindow,@event())
      EndIf
      
      If IsWindow(Window) And 
         Event = #PB_Event_CloseWindow
        If IsWindow(Window) And Title$ = ""
          Title$ = GetWindowTitle(Window)
        EndIf
        
        If MainWindow = Window :Window = -1 :EndIf
        
        Select Message 
          Case Window
            If MessageRequester( Title$, Text$, #PB_MessageRequester_YesNo ) ! #PB_MessageRequester_Yes
              SetEnvironmentVariable("Window",Str(0))
              SetEnvironmentVariable("Event",Str(0))
              ProcedureReturn #False
            EndIf
        EndSelect
        
        CloseWindow( Window )
        If IsCloseWindow( MainWindow ) 
          ProcedureReturn #True
        EndIf
      EndIf
    EndProcedure
    ProcedureDLL WindowClose3(MainWindow,Message,Text$)
      ProcedureReturn WindowClose4(MainWindow,Message,Text$,"")
    EndProcedure
    ProcedureDLL WindowClose2(MainWindow,Message)
      ProcedureReturn WindowClose3(MainWindow,Message,"Exit?")
    EndProcedure
    ProcedureDLL WindowClose(MainWindow)
      ProcedureReturn WindowClose2(MainWindow,#PB_All)
    EndProcedure
    
    Macro WindowClose(MainWindow =-1,Message =-1,Text="Exit?",Title="") ;Returns TRUE is window close
      WindowClose4(MainWindow,Message,Text,Title)
    EndMacro
  CompilerEndIf

  CompilerIf #PB_Compiler_IsMainFile
    X = 100
    
    For i = 0 To 4
      OpenWindow(i, X, 200, 50, 50, "Window" + Trim(Str(i)),#PB_Window_SystemMenu|#PB_Window_Invisible) :HideWindow(i,0)
      X + 200
    Next
    
    Repeat
      ;no close
      If Event = #PB_Event_CloseWindow
        If Not IsCloseWindow(EventWindow())
          Debug "no exit"
        EndIf
      EndIf   
      
      Event = WaitWindowEvent( )
      Select Event
        Case #PB_Event_ActivateWindow
          Debug "ActivateWindow "+EventWindow()
      EndSelect
      
    Until WindowClose( 4 )

    ;close
    Debug "exit"
    
  CompilerEndIf