Successfully upgraded to Vanilla Forums v2.6. Please report any issue you may find.

2.6-The Events of the Window: Part 3

edited May 2016 in English
Let's continue with the callback functions in the detection of the events related to the windows: Now we'll see how you can change the callback functions during the execution of the program by introducing the method :set() of the Window object.

The example of this tutorial we have seen before but have added two commands at the end of the callback function that just deal with exchanging callback functions themselves.

;Include the HGui library in our application
@INCLUDE "yourpath/+Includes.hws"
@INCLUDE #INC_HGUI

; For now we disable debugging commenting on the following lines
; DBG.Console.Enable(True, False)
; DBG.Console.AddChannel(HGui.DebugChannel)
Pay attention to the final part of the callback functions:

Function windowMoved1(msg, win)
  ; This function will be executed each time the window will be moved
  DebugPrint("::: CALLBACK FUNCTION :::")
  DebugPrint(GetTime(True), "OnMove event for window " .. win.Name)
  DebugPrint("--- message contents ---")
  ForEach(msg, DebugPrint)
  DebugPrint("")

  ; We change the callback function with the method :set()
  win:set({ events = { OnMove = windowMoved2 }})
EndFunction

Function windowMoved2(msg, win)
  ; This function will be executed each time the window will be moved
  DebugPrint("::: CALLBACK FUNCTION :::")
  DebugPrint(GetTime(True), "OnMove event for window " .. win.Name)
  DebugPrint("--- message contents ---")
  ForEach(msg, DebugPrint)
  DebugPrint("")

  ; We change the callback function with the method :set()
  win:set({ events = { OnMove = windowMoved1 }})
EndFunction
As you can guess, the callback functions will alternate with each other by dynamically changing the callback function assigned to the event OnMove itself.

The method :set() allows you to modify any object parameter of a Window. It is therefore a very powerful tool that you will find often in our examples and not just associated with the windows but also with the various gadgets.

Local myWin0 = HGui.Window:new(
  { title = "My first window",
    name = "win0",
    position = { x = 20, y = 20 },
    size = { w = 400, h = 100 },
    ; define the callback below
    events = 
      { OnMove = windowMoved1 }
  })

Repeat
  WaitEvent()
Forever
Try to get more events to use the :set() method in the callback functions for other purposes.
Sign In or Register to comment.