Multiple SetWindowCallback

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Multiple SetWindowCallback

Post by mk-soft »

Multiple SetWindowCallback as a list.
The last assigned callback is executed first. If the return value is not changed, the previous return value is used.

If a window is specified, it works like the original with only one callback.

v1.01.0

Code: Select all

;-TOP by mk-soft, v1.01.0, 08.05.2022

; Multiple SetWindowCallback.
; The last assigned callback is executed first. If the return value is not changed, the previous return value is used.
; If a window is specified, it works like the original with only one callback.

Prototype _Invoke4(hWnd, uMsg, wParam, lParam)

Structure _udtWindowCallback
  *Callback._Invoke4
EndStructure

Macro _PB_(Function)
  Function
EndMacro

Global NewList _ListWindowCallback._udtWindowCallback()

Procedure _DoWindowCallback(hWnd, uMsg, wParam, lParam) 
  Protected result, result_save
  
  result = #PB_ProcessPureBasicEvents
  ForEach _ListWindowCallback()
    result_save = result
    result = _ListWindowCallback()\Callback(hWnd, uMsg, wParam, lParam)
    If result = #PB_ProcessPureBasicEvents
      result = result_save
    EndIf
  Next
  ProcedureReturn result
EndProcedure 

Procedure AddWindowCallback(*ProcedureName, Window = -1)
  If Window >= 0
    _PB_(SetWindowCallback)(*ProcedureName, Window)
  Else
    If *ProcedureName
      FirstElement(_ListWindowCallback())
      InsertElement(_ListWindowCallback())
      _ListWindowCallback()\Callback = *ProcedureName
    EndIf
  EndIf
EndProcedure

Procedure RemoveWindowCallback(*ProcedureName)
  ForEach _ListWindowCallback()
    If _ListWindowCallback()\Callback = *ProcedureName
      DeleteElement(_ListWindowCallback())
    EndIf
  Next
EndProcedure

SetWindowCallback(@_DoWindowCallback())

Macro SetWindowCallback(ProcedureName, Window = -1)
  AddWindowCallback(ProcedureName, Window)
EndMacro

; *********************************************************

CompilerIf #PB_Compiler_IsMainFile
  
  ;- Example
  
  Enumeration Windows
    #Main
  EndEnumeration
  
  Enumeration MenuBar
    #MainMenu
  EndEnumeration
  
  Enumeration MenuItems
    
  EndEnumeration
  
  Enumeration Gadgets
    
  EndEnumeration
  
  Enumeration StatusBar
    #MainStatusBar
  EndEnumeration
  
  Procedure WinCallback_1(hWnd, uMsg, wParam, lParam) 
    
    If uMsg = #WM_SIZE 
      Select wParam 
        Case #SIZE_MINIMIZED 
          Debug ("Size minimized")
        Case #SIZE_RESTORED 
          Debug ("Size restored")
        Case #SIZE_MAXIMIZED 
          Debug ("Size maximized")
      EndSelect 
    EndIf 
    
    ProcedureReturn #PB_ProcessPureBasicEvents 
  EndProcedure 
  
  Procedure WinCallback_2(hWnd, uMsg, wParam, lParam) 
    
    If uMsg = #WM_MOVE
      Debug ("Move")
    EndIf 
    
    ProcedureReturn #PB_ProcessPureBasicEvents 
  EndProcedure 
  
  SetWindowCallback(@WinCallback_1())
  SetWindowCallback(@WinCallback_2())
  
  Procedure UpdateWindow()
    Protected dx, dy
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar)
    ; Resize gadgets
  EndProcedure
  
  Procedure Main()
    Protected dx, dy
    
    #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
    
    If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, "Window" , #MainStyle)
      ; Menu
      CreateMenu(#MainMenu, WindowID(#Main))
      
      ; StatusBar
      CreateStatusBar(#MainStatusBar, WindowID(#Main))
      AddStatusBarField(#PB_Ignore)
      
      ; Gadgets
      dx = WindowWidth(#Main)
      dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar)
      
      ; Bind Events
      BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
      
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Break
            
          Case #PB_Event_Menu
            Select EventMenu()
                CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
                Case #PB_Menu_About
                  
                Case #PB_Menu_Preferences
                  
                Case #PB_Menu_Quit
                  PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                  
                CompilerEndIf
                
            EndSelect
            
          Case #PB_Event_Gadget
            Select EventGadget()
                
            EndSelect
            
        EndSelect
      ForEver
      
    EndIf
    
  EndProcedure : Main()
  
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive