WaitCloseWindow()

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

WaitCloseWindow()

Post by mestnyi »

Do you think that's such a function would be useful?

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Import ""
  CompilerElse
    ImportC ""
    CompilerEndIf
    PB_Window_Objects.i
    PB_Object_Count( Object )
  EndImport
  
  Procedure __IsWindow( Window = #PB_All )
    If Window = #PB_All
      ProcedureReturn PB_Object_Count( PB_Window_Objects.i )
    EndIf
    ProcedureReturn IsWindow( Window )
  EndProcedure :Macro IsWindow( Window = #PB_All ) : __IsWindow( Window ) : EndMacro
  
  Procedure WaitWindowClose( Window = #PB_All )
    While IsWindow( Window )
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow 
          CloseWindow( EventWindow() )
      EndSelect
    Wend
    
    CloseWindow( #PB_All )
  EndProcedure
  
  CompilerIf #PB_Compiler_IsMainFile
    OpenWindow(1, 0, 0, 322, 150, "window 1", #PB_Window_SystemMenu )
    OpenWindow(2, 0, 230, 322, 150, "window 2", #PB_Window_SystemMenu )
    OpenWindow(3, 0, 460, 322, 150, "window 3", #PB_Window_SystemMenu )
    OpenWindow(4, 0, 0, 322, 150, "window 4", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    ; WaitWindowClose( 4 ) ; uncomment to close the application, by closing specified window
    WaitWindowClose( )
  CompilerEndIf
 
Feel free to leave your comments, very interested in your opinion.

Code: Select all

Enumeration #PB_Event_FirstCustomValue
  #PB_Event_Window
  #PB_Event_Quit
EndEnumeration

Structure EventStruct
  Event.i
  Window.i
  CallBack.i
  MainWindow.i
EndStructure

Global NewList WindowEventList.EventStruct()

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Import ""
  CompilerElse
    ImportC ""
    CompilerEndIf
    PB_Object_EnumerateStart(Object)
    PB_Object_EnumerateNext(Object,*ID.Integer)
    PB_Object_EnumerateAbort(Object)
    PB_Object_Count( Objects )
    PB_Window_Objects.l
  EndImport
  Procedure CountWindow( ) 
    ProcedureReturn PB_Object_Count( PB_Window_Objects )
  EndProcedure
  Procedure.i CurrentWindow()
   Protected Window =- 1
   If PB_Window_Objects
    PB_Object_EnumerateStart(PB_Window_Objects)
     While PB_Object_EnumerateNext(PB_Window_Objects, @Window)
       If WindowID(Window) = UseGadgetList(0) 
         ProcedureReturn Window
       EndIf
     Wend
      PB_Object_EnumerateAbort(PB_Window_Objects) 
   EndIf
  EndProcedure
  
  ; Предлогаю изменить на следующее поведение
  Procedure CloseWindowAll()
    CloseWindow(#PB_All)
  EndProcedure
  Procedure __CloseWindow( Window )
    If Window = #PB_All
      PostEvent( #PB_Event_Quit, EventWindow(), #PB_Ignore ) ; - 1 bug
      BindEvent( #PB_Event_Quit, @CloseWindowAll(), EventWindow())
    Else
      If EventWindow() = Window
        ProcedureReturn CloseWindow( Window )
      Else
        PostEvent( #PB_Event_CloseWindow, Window, #PB_Ignore ) ; - 1 bug
        ProcedureReturn WindowID(Window)
     EndIf
    EndIf
  EndProcedure 
  Macro CloseWindow( Window ) : __CloseWindow( Window ) : EndMacro
  
  ;
  Procedure Event( )
    If ListSize(WindowEventList())
      FirstElement( WindowEventList() )
      If WindowEventList()\Window = EventWindow()
        ProcedureReturn WindowEventList()\Event
      EndIf
    EndIf
  EndProcedure
  
  Procedure IsBindEvent( Window )
    Protected Result
    If IsWindow(Window) 
      If ListSize(WindowEventList())
        ForEach WindowEventList()
          If ListIndex(WindowEventList()) ; Исключаем первый элемент
            If WindowEventList()\Window = Window
              Result = WindowEventList()\Event
              Debug "IsBindEvent "+Result
              ProcedureReturn Result
            EndIf
          EndIf
        Next
      EndIf
    EndIf
  EndProcedure
  
  Declare WindowEvents( Event )
  Procedure Event_Menu_Window()               : WindowEvents( #PB_Event_Menu )                : EndProcedure
  Procedure Event_Move_Window()               : WindowEvents( #PB_Event_MoveWindow )         : EndProcedure
  Procedure Event_Size_Window()               : WindowEvents( #PB_Event_SizeWindow )         : EndProcedure
  Procedure Event_Timer_Window()              : WindowEvents( #PB_Event_Timer )              : EndProcedure
  Procedure Event_Close_Window()              : WindowEvents( #PB_Event_CloseWindow )        : EndProcedure
  Procedure Event_Gadget_Window()             : WindowEvents( #PB_Event_Gadget )             : EndProcedure
  Procedure Event_SysTray_Window()            : WindowEvents( #PB_Event_SysTray )            : EndProcedure
  Procedure Event_Restore_Window()            : WindowEvents( #PB_Event_RestoreWindow )      : EndProcedure
  Procedure Event_Repaint_Window()            : WindowEvents( #PB_Event_Repaint )            : EndProcedure
  Procedure Event_Maximize_Window()           : WindowEvents( #PB_Event_MaximizeWindow )     : EndProcedure
  Procedure Event_Minimize_Window()           : WindowEvents( #PB_Event_MinimizeWindow )     : EndProcedure
  Procedure Event_Activate_Window()           : WindowEvents( #PB_Event_ActivateWindow )     : EndProcedure
  Procedure Event_LeftClick_Window()          : WindowEvents( #PB_Event_LeftClick )          : EndProcedure
  Procedure Event_GadgetDrop_Window()         : WindowEvents( #PB_Event_GadgetDrop )         : EndProcedure
  Procedure Event_WindowDrop_Window()         : WindowEvents( #PB_Event_WindowDrop )         : EndProcedure
  Procedure Event_Deactivate_Window()         : WindowEvents( #PB_Event_DeactivateWindow )   : EndProcedure
  Procedure Event_RightClick_Window()         : WindowEvents( #PB_Event_RightClick )         : EndProcedure
  Procedure Event_LeftDoubleClick_Window()    : WindowEvents( #PB_Event_LeftDoubleClick )    : EndProcedure
  
  Procedure Event_FileReceived_Window()       : WindowEvents( #PB_Event_FileReceived )       : EndProcedure
  Procedure Event_ClientConnected_Window()    : WindowEvents( #PB_Event_ClientConnected )    : EndProcedure
  Procedure Event_ClientDisconnected_Window() : WindowEvents( #PB_Event_ClientDisconnected ) : EndProcedure
  
  Procedure Event_Quit_Window()               : WindowEvents( #PB_Event_Quit )               : EndProcedure
  
  Procedure EventsWindow( Window ) ; 
    BindEvent( #PB_Event_Gadget, @Event_Gadget_Window(), Window)
    BindEvent( #PB_Event_DeactivateWindow, @Event_Deactivate_Window(), Window)
    BindEvent( #PB_Event_ActivateWindow, @Event_Activate_Window(), Window)
    BindEvent( #PB_Event_CloseWindow, @Event_Close_Window(), Window)
    BindEvent( #PB_Event_GadgetDrop, @Event_GadgetDrop_Window(), Window)
    BindEvent( #PB_Event_LeftClick, @Event_LeftClick_Window(), Window)
    BindEvent( #PB_Event_LeftDoubleClick, @Event_LeftDoubleClick_Window(), Window)
    BindEvent( #PB_Event_MaximizeWindow, @Event_Maximize_Window(), Window)
    BindEvent( #PB_Event_Menu, @Event_Menu_Window(), Window)
    BindEvent( #PB_Event_MinimizeWindow, @Event_Minimize_Window(), Window)
    BindEvent( #PB_Event_MoveWindow, @Event_Move_Window(), Window)
    BindEvent( #PB_Event_Repaint, @Event_Repaint_Window(), Window)
    BindEvent( #PB_Event_RestoreWindow, @Event_Restore_Window(), Window)
    BindEvent( #PB_Event_RightClick, @Event_RightClick_Window(), Window)
    BindEvent( #PB_Event_SizeWindow, @Event_Size_Window(), Window)
    BindEvent( #PB_Event_SysTray, @Event_SysTray_Window(), Window)
    BindEvent( #PB_Event_Timer, @Event_Timer_Window(), Window)
    BindEvent( #PB_Event_WindowDrop, @Event_WindowDrop_Window(), Window)
    
;     CompilerIf #PB_Compiler_OS = #PB_OS_Windows ; антивирус ругается
;       BindEvent( #PB_Event_ClientConnected, @Event_ClientConnected_Window(), Window)
;       BindEvent( #PB_Event_ClientDisconnected, @Event_ClientDisconnected_Window(), Window)
;       BindEvent( #PB_Event_FileReceived, @Event_FileReceived_Window(), Window)
;     CompilerEndIf   
    
    BindEvent( #PB_Event_Quit, @Event_Quit_Window(), Window)
  EndProcedure
  Procedure UnEventsWindow( Window ) ; 
    UnbindEvent( #PB_Event_Gadget, @Event_Gadget_Window(), Window)
    UnbindEvent( #PB_Event_DeactivateWindow, @Event_Deactivate_Window(), Window)
    UnbindEvent( #PB_Event_ActivateWindow, @Event_Activate_Window(), Window)
    UnbindEvent( #PB_Event_CloseWindow, @Event_Close_Window(), Window)
    UnbindEvent( #PB_Event_GadgetDrop, @Event_GadgetDrop_Window(), Window)
    UnbindEvent( #PB_Event_LeftClick, @Event_LeftClick_Window(), Window)
    UnbindEvent( #PB_Event_LeftDoubleClick, @Event_LeftDoubleClick_Window(), Window)
    UnbindEvent( #PB_Event_MaximizeWindow, @Event_Maximize_Window(), Window)
    UnbindEvent( #PB_Event_Menu, @Event_Menu_Window(), Window)
    UnbindEvent( #PB_Event_MinimizeWindow, @Event_Minimize_Window(), Window)
    UnbindEvent( #PB_Event_MoveWindow, @Event_Move_Window(), Window)
    UnbindEvent( #PB_Event_Repaint, @Event_Repaint_Window(), Window)
    UnbindEvent( #PB_Event_RestoreWindow, @Event_Restore_Window(), Window)
    UnbindEvent( #PB_Event_RightClick, @Event_RightClick_Window(), Window)
    UnbindEvent( #PB_Event_SizeWindow, @Event_Size_Window(), Window)
    UnbindEvent( #PB_Event_SysTray, @Event_SysTray_Window(), Window)
    UnbindEvent( #PB_Event_Timer, @Event_Timer_Window(), Window)
    UnbindEvent( #PB_Event_WindowDrop, @Event_WindowDrop_Window(), Window)
    
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      UnbindEvent( #PB_Event_ClientConnected, @Event_ClientConnected_Window(), Window)
      UnbindEvent( #PB_Event_ClientDisconnected, @Event_ClientDisconnected_Window(), Window)
      UnbindEvent( #PB_Event_FileReceived, @Event_FileReceived_Window(), Window)
    CompilerEndIf   
    
    UnbindEvent( #PB_Event_Quit, @Event_Quit_Window(), Window)
  EndProcedure
  Procedure InitEvents()
    Protected Window =- 1
    PB_Object_EnumerateStart(PB_Window_Objects)
    If PB_Window_Objects
      While PB_Object_EnumerateNext(PB_Window_Objects, @Window)
        EventsWindow( Window ) 
      Wend
      PB_Object_EnumerateAbort(PB_Window_Objects) 
    EndIf
    EventsWindow( #PB_All )
  EndProcedure
  
  Procedure WindowEvents( Event )
    Protected Window =- 1
    If IsWindow(EventWindow()) 
      FirstElement(WindowEventList()) 
      WindowEventList()\Event = Event
      WindowEventList()\Window = EventWindow()
    EndIf
    
    Select Event 
      Case #PB_Event_Quit
        UnEventsWindow( #PB_All )
        FreeList(WindowEventList())
    EndSelect 
  EndProcedure
  
  ; Если вышли из петли посылаем сообшение "#PB_Event_Quit"
  Procedure QuitCallBack(); 
    Static Window =- 1
    If Window =- 1
      Window = OpenWindow( #PB_Any, 0,0,0,0, "", #PB_Window_Invisible )
      BindEvent( #PB_Event_Quit, @QuitCallBack() )
      While IsWindow( Window ) : WaitWindowEvent() : Wend  
    Else
      CloseWindow(Window)
    EndIf
  EndProcedure
  Procedure InitQuit( Window = #PB_All ); 
    PostEvent( #PB_Event_Quit, Window, #PB_Ignore ) ; - 1 bug
    QuitCallBack( )
  EndProcedure
  
  Procedure WaitWindowClose( Window = #PB_All )
    AddElement( WindowEventList() )
    WindowEventList()\Window =- 1 
    WindowEventList()\MainWindow = Window
    MoveElement( WindowEventList(), #PB_List_First )
    
    InitEvents()
    
    While CountWindow()
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          If  IsWindow(EventWindow())
            If IsBindEvent(EventWindow()) : Continue : EndIf 
            If ( EventWindow() = Window Or Window =- CountWindow() ) 
              CloseWindow( #PB_All )
            Else
              CloseWindow( EventWindow() )
            EndIf
          EndIf
      EndSelect    
    Wend
    
    InitQuit( WindowEventList()\Window ) 
  EndProcedure
  
 Procedure BindEventAll( Event, *EventCallBack, Window, Object, EventType ) ;Returns TRUE is purebasic event
    If Event = #PB_All
      BindEvent( #PB_Event_DeactivateWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_ActivateWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_CloseWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_FirstCustomValue, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_Gadget, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_GadgetDrop, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_LeftClick, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_LeftDoubleClick, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_MaximizeWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_Menu, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_MinimizeWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_MoveWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_Repaint, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_RestoreWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_RightClick, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_SizeWindow, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_SysTray, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_Timer, *EventCallBack, Window, Object, EventType)
      BindEvent( #PB_Event_WindowDrop, *EventCallBack, Window, Object, EventType)
      CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        BindEvent( #PB_Event_ClientConnected, *EventCallBack, Window, Object, EventType)
        BindEvent( #PB_Event_ClientDisconnected, *EventCallBack, Window, Object, EventType)
        BindEvent( #PB_Event_FileReceived, *EventCallBack, Window, Object, EventType)
      CompilerEndIf   
    Else
      BindEvent( Event, *EventCallBack, Window, Object, EventType)
    EndIf
    
    If ListSize(WindowEventList())
      ForEach WindowEventList()
        If WindowEventList()\Window = Window
          Window =- 1
          Break
        EndIf
      Next
    EndIf
    
    If IsWindow(Window)
      AddElement(WindowEventList()) 
      WindowEventList()\Event = Event
      WindowEventList()\Window = Window
      WindowEventList()\CallBack = *EventCallBack
    EndIf
  EndProcedure
  Macro BindEvent( Event, EventCallBack, Window =-1, Object =-1, EventType =-1) :BindEventAll( Event, EventCallBack, Window, Object, EventType ) :EndMacro
  
 
   

  
  
  CompilerIf #PB_Compiler_IsMainFile
    ;Debug UseGadgetList(0)
    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")
    ;ContainerGadget(-1,10,10,100,30) : CloseGadgetList()
    ;Debug UseGadgetList(0)
    ;Debug CurrentWindow()
    
    
    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()
      Protected Event = Event()
      Select Event
        Case #PB_Event_CloseWindow
          If MessageRequester("Warning!!!","Close application "+Str(EventWindow())+"?",#PB_MessageRequester_YesNo ) = #PB_MessageRequester_Yes 
            CloseWindow( #PB_All ) 
          EndIf
          
        Case #PB_Event_Gadget
          Debug "Gadget"
          ;UnEventWindows( )
          
          Debug CloseWindow(1)
          
      EndSelect
     If Event
        Debug #PB_Compiler_Procedure+" - "+EventWindow() +" - "+Event
        EndIf
     EndProcedure
    
    Procedure Event_Windows()
      Protected Event = Event()
      Select Event
        Case #PB_Event_Gadget
          Debug "Gadget555"
          
      EndSelect
      If Event
        Debug #PB_Compiler_Procedure+" - "+EventWindow() +" - "+Event
        EndIf
    EndProcedure
    
    
    
;       BindEvent( #PB_Event_CloseWindow, @Event_Window_1(), 1)
     BindEvent( #PB_Event_CloseWindow, @Event_Window_4(), 4)
    BindEvent( #PB_Event_Gadget, @Event_Window_4(), 4)
; ; ;     
; ; ; ;     BindEvent( #PB_Event_ActivateWindow, @Event_Windows())
; ; ; ;     BindEvent( #PB_Event_DeactivateWindow, @Event_Windows())
; ; ; ;     BindEvent( #PB_Event_CloseWindow, @Event_Windows())
; ; ; ;     BindEvent( #PB_Event_Gadget, @Event_Windows())
  ;  BindEvent( #PB_All, @Event_Window_4(), 4)
  ; BindEvent( #PB_All, @Event_Windows(),4)
;     
    Procedure Quit()
      Debug "Quit "+CountWindow()+" Window"+EventWindow()
      End
    EndProcedure
    BindEvent( #PB_Event_Quit, @Quit())
    
    WaitWindowClose( 4 ) 
  CompilerEndIf
; IDE Options = PureBasic 5.31 (Windows - x86)
; CursorPosition = 241
; FirstLine = 229
; Folding = -------------
; EnableUnicode
; EnableXP
Last edited by mestnyi on Fri Jan 15, 2016 7:22 pm, edited 6 times in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: WaitCloseWindow()

Post by IdeasVacuum »

;You can also specify the main window after closing to close all the windows
That's already automatically handled by PB.
If you want your app to do something before closing the Main Window, that action is in your loop:

Code: Select all

iExit = #False
Repeat
              iEvent = WaitWindowEvent()
       Select iEvent

                   Case #PB_Event_CloseWindow

                         MakeCoffee()
                        EatDoughnut()
                        iExit = #True
       EndSelect

Until iExit = #True
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: WaitCloseWindow()

Post by mestnyi »

What do you want to say, do not really understand :|

Code: Select all

EnableExplicit

DeclareModule Close
  Declare.i Event()
  Declare.i CountWindow()
  Declare.i WaitWindowClose( Window = #PB_All )
EndDeclareModule
Module Close
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows 
    Import "" : CompilerElse : ImportC "" : CompilerEndIf
      PB_Object_EnumerateStart(Object)
      PB_Object_EnumerateNext(Object,*ID.Integer)
      PB_Object_EnumerateAbort(Object)
      PB_Object_Count( Object )
      PB_Window_Objects.I
    EndImport
    
    Procedure CountWindow()
      ProcedureReturn PB_Object_Count( PB_Window_Objects )
    EndProcedure
    
    Procedure Event()
      If PB_Object_Count( PB_Window_Objects )
        ProcedureReturn WaitWindowEvent()
      EndIf
    EndProcedure
    
    Procedure WaitWindowClose( Window = #PB_All )
      If IsWindow( Window ) 
        Protected Event
        Protected MainWindow = Window
        
        While IsWindow( Window ) 
          Select Event()
            Case #PB_Event_CloseWindow 
              If IsWindow( EventWindow() )
                SendMessage_( WindowID( EventWindow() ), #WM_CLOSE, 0, 0) 
                Select Event() 
                  Case #PB_Event_CloseWindow 
                     CloseWindow( EventWindow() )
                EndSelect
              EndIf
          EndSelect
        Wend
        
         PostEvent( #PB_Event_CloseWindow, #PB_Ignore, #PB_Ignore ) : Event()
         
         Debug "CountWindow "+CountWindow()
        CloseWindow( #PB_All )
     Else
        While PB_Object_Count( PB_Window_Objects )
          Select WaitWindowEvent()
            Case #PB_Event_CloseWindow 
              CloseWindow( EventWindow() )
          EndSelect
        Wend
      EndIf
    EndProcedure
  EndModule
  
  
  DeclareModule CanvasWindow
    Declare.i new(X.i,Y.i,Width.i,Height.i,Title.s)
  EndDeclareModule
  
  Module CanvasWindow
    Declare.i Event_close()
    Declare.i Event_resize()
    Declare.i Event_draw()
    
    Structure Type_S
      IdWindow.i
      IdCanvas.i 
      Width.i
      Height.i
      leftButtonDown.i
      rightButtonDown.i
      lastX.i
      LastY.i
    EndStructure
    
    Global NewMap *windowData()
    
    Procedure _setWindowData(windowid, *data)
      *windowData(Str(windowid)) = *data
    EndProcedure
    
    Procedure _getWindowData(windowid)
      If MapSize(*windowData()) 
        ProcedureReturn *windowData(Str(windowid))
        ;Else
        ; CloseWindow(WindowID)
      EndIf
    EndProcedure
    
    Procedure.i CloseMain()
      Debug "CloseMainWindow"
        FreeMap(*windowData())
    EndProcedure
    
    Procedure.i Event_close()
      Debug "Close "+GetWindowTitle(EventWindow())
      Protected *This.Type_S = _getWindowData(EventWindow())
      If *This
        FreeGadget(*This\IdCanvas)
        CloseWindow( *This\idWindow )
        DeleteMapElement(*windowData(), Str(*This\idWindow))
        FreeMemory(*This)
        Debug "FreeMemory "+*This
        *This = #False
      EndIf
    EndProcedure
    
    Procedure.i Event_resize()
      Protected *This.Type_S = _getWindowData(EventWindow())
      If *This
        With *This
          \Width = WindowWidth(\IdWindow)
          \Height = WindowHeight(\IdWindow)
          ResizeGadget(\IdCanvas,#PB_Ignore,#PB_Ignore,\Width,\Height)
        EndWith
      EndIf
    EndProcedure
    
    Procedure.i Event_draw()
      Protected draw.i = #False
      Protected *This.Type_S = _getWindowData(EventWindow())
      With *This
        Protected X.i = GetGadgetAttribute(\IdCanvas,#PB_Canvas_MouseX)
        Protected Y.i = GetGadgetAttribute(\IdCanvas,#PB_Canvas_MouseY)
        
        Select EventType()
          Case #PB_EventType_LeftButtonDown
            \leftButtonDown = #True
            draw = #True
            \lastX = X.i
            \LastY = Y.i
          Case #PB_EventType_LeftButtonUp
            \leftButtonDown = #False
          Case #PB_EventType_RightButtonDown
            \rightButtonDown = #True
            draw = #True
            \lastX = X.i
            \LastY = Y.i
          Case #PB_EventType_LeftButtonUp
            \rightButtonDown = #False
          Case #PB_EventType_MouseMove
            If (\leftButtonDown Or \rightButtonDown)
              draw = #True
            EndIf
        EndSelect 
        
        If (draw)
          If StartDrawing(CanvasOutput(\IdCanvas))
            Protected Color.i = $000000
            If (\rightButtonDown)
              Color = $ffffff
            EndIf
            
            LineXY(\lastX,\LastY,X,Y,Color)
            \lastX = X.i
            \LastY = Y.i
            
            ;Plot(X,Y,Color)
            StopDrawing()
          EndIf
        EndIf 
      EndWith
    EndProcedure
    
    Procedure.i New(X.i,Y.i,Width.i,Height.i,Title.s)
      *This.Type_S = AllocateMemory(SizeOf(Type_S))
      If *This
        With *This
          \Width = Width
          \Height = Height
          \idWindow = OpenWindow(#PB_Any,X.i,Y.i,Width.i,Height.i,Title.s,#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_Invisible)
          \leftButtonDown = #False
          \rightButtonDown = #False
          
          If (Not \IdWindow)
            FreeMemory(*This)
            ProcedureReturn #False
          EndIf
          
          \IdCanvas = CanvasGadget(#PB_Any,0,0,Width,Height)
          If (Not \IdCanvas)
            CloseWindow(\IdWindow)
            FreeMemory(*This)
            ProcedureReturn #False
          EndIf
          
          
          _setWindowData(\IdWindow, *This)
          BindEvent(#PB_Event_CloseWindow,@Event_Close(),\idWindow)
          
          UnbindEvent(#PB_Event_CloseWindow,@CloseMain(), #PB_Ignore) ; bug
          BindEvent(#PB_Event_CloseWindow,@CloseMain(), #PB_Ignore)
          
;           UnbindEvent(#PB_Event_FirstCustomValue,@CloseMain(), #PB_Ignore) ; bug
;           BindEvent(#PB_Event_FirstCustomValue,@CloseMain(), #PB_Ignore)
          
          BindEvent(#PB_Event_SizeWindow,@Event_resize(),\idWindow)
          BindGadgetEvent(\IdCanvas,@Event_draw())
          
          HideWindow(\IdWindow,#False)
        EndWith
        
        ProcedureReturn *This
      EndIf
    EndProcedure
  EndModule
  
  
  
  UseModule Close
  Define Window = OpenWindow(#PB_Any,55,55,140,40,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
 Define Window = OpenWindow(#PB_Any,55,55,140,40,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  StickyWindow( Window, 1 )
  
  Procedure Close_Event()
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow 
        If MessageRequester("Предупреждение!!!","Закрыть приложение?",#PB_MessageRequester_YesNo ) = #PB_MessageRequester_Yes 
          CloseWindow( EventWindow() )
        EndIf
    EndSelect
    Debug EventWindow()
  EndProcedure
  
  Procedure Button_Event()
    Static i
    Select EventType() 
      Case #PB_EventType_LeftClick
        UseModule CanvasWindow
        New(0,0, 300, 200,"CanvasWindow_"+Str(i))
        UnuseModule CanvasWindow
        i+1
    EndSelect
  EndProcedure
  
  BindGadgetEvent(ButtonGadget(-1,5,5,130,30,"new"), @Button_Event() )
  ;BindEvent(#PB_Event_CloseWindow, @Close_Event(), Window ) ; Uncoment 
  WaitWindowClose( Window )
  UnuseModule Close
  End
  
  
  
  ; IDE Options = PureBasic 5.31 (Linux - x86)
  ; CursorPosition = 40
  ; FirstLine = 23
  ; Folding = --------
  ; EnableUnicode
  ; EnableXP
Last edited by mestnyi on Thu Dec 17, 2015 10:36 am, edited 2 times in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: WaitCloseWindow()

Post by IdeasVacuum »

What I'm saying is that a WaitCloseWindow() function does not seem necessary.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply