TUTORIAL T203 - Window's Events - part 1

File : Examples/T203 - WindowEvents-1.hws



In this tutorial we will see how we can attach callback functions to window's events. This way you will be able to detect and automatically execute your custom functions when a window's event occurs.

Since we want to attach callback functions to the window's events we have to define them. Each window can react on the following events:

  • OnClose : Occurs when the window is closed
  • OnMove : Occurs when the window is moved
  • OnActivate : Occurs when the window get the focus
  • OnDeactivate : Occurs when the window loose the focus
  • OnHide : Occurs when the window is hided/iconified
  • OnShow : Occurs when the window is showed/deiconified
  • OnSize : Occurs when the window is resized

All callback functions can be defined at creation time using the options table where you can specify to track the window events. Note that all callback functions will be called with two arguments, the first one is a table describing the occurred event and the second one is the window object that have raised the event.

Note that in this tutorial the debug output has been turned off because we need to print some stuff on the console and we don't want to mess up the screen with the library messages.

Now let's proceed with the source code: as always we need to include the HGui library:

@INCLUDE "../../+Includes.hws"
@INCLUDE #INC_HGUI

Now, before we build the window we have to define the callback functions we will use later, for simplicity in this tutorial we are going to track only two event : OnMove and OnSize. The forst one is raised when the window is moved, the second one is raised when the window is resized.

First callback function:

Function windowMoved(msg, win)
   ; This one will be attached to the <OnMove> event and will be called every
   ; time the linked window will be moved around.
   DebugPrint("::::::: C A L L B A C K   F U N C T I O N :::::::::::::::::::::::")
   DebugPrint(":::: ", GetTime(True), "The window was moved! -> " .. win.name)
   DebugPrint("---- Message Contents ----")
   ForEach(msg, DebugPrint)
   DebugPrint("--------------------------\n")
EndFunction

Second callback function:

Function windowResized(msg, win)
   ; This one will be attached to the <OnSize> event and will be called every
   ; time the linked window will be resized.
   DebugPrint("::::::: C A L L B A C K   F U N C T I O N :::::::::::::::::::::::")
   DebugPrint(":::: ", GetTime(True), "The window was resized! -> " .. win.name)
   DebugPrint("---- Message Contents ----")
   ForEach(msg, DebugPrint)
   DebugPrint("--------------------------\n")
EndFunction

And now let's create the window specifying the callback functions on the events we want to track:

; Window creation
Local myWin0 = HGui.Window:new(
                 { title = "My First Window!",
                   name  = "win0",
                   position = { x = 20, y = 20 },
                   size     = { w = 400, h = 100 },
                   Events   = ;  In this table you have to specify all supported
                              ;  window's events you wish to track.
                     { OnMove = windowMoved,
                       OnSize = windowResized
                       }
                   })


Repeat
   WaitEvent()
Forever

Now execute the script and try to move and/or resize the window, you will see that the callback functions attached to the OnMove and OnSize events are executed when the events are raised by your actions.

Screenshot

hollywoodlibs/hgui/tutorials/t203.txt · Ultima modifica: 2017/09/28 18:28 da admin
CC Attribution-Noncommercial 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0