In an event-driven environment like Microsoft Windows, a program must be able to respond to actions of the user, for instance: the user presses a button and another window must appear to enter more information. The initiator of the action is the user and a Prolog program must be able to respond to (asynchronous) events, events like the showing of a window, pressing of a button, resizing of a window, etc...
It is possible to respond to events by adding a so called callback (a sort of link) between an event of a window and a piece of Prolog source code that will be executed if the event occurs. While registering a callback a Prolog program can indicate in which events of which class instance it is interested and to where a message must be send. It is like telling a button: 'if you are clicked then I want you to call method 'MyMethod' of the class instance called MyInstance'.
In this example the problem is that a piece of Prolog code must be executed when the user presses a button. The figure below displays this situation.
![]() |
After the button is created the button instance must be instructed that if it is clicked it must execute a piece of Prolog code, this is done by using the predicate add_callback/4.
The complete command to create the callback looks like:
![]() |
The method that must respond to an event always has 1 parameter, this parameter is a compound structure with more information about the event, like the sender of the event. After the callback was added the situation is shown in the picture below.
![]() |
If the event is executed the argument of the event gives extra information about it. For the 'onClick' event the variable X (in the example above) will be unified with the compound structure onClick(Sender), the variable Sender refers to the instance that generated the event, in this example is it the button that was clicked.
For other types of events like the 'onActivate' event more arguments are in the compound structure, the event structure for 'onActivate' is:
| onActivate(Sender, Bool) |
The 'Sender' variable will refer to the originator of the event, the 'Bool' variable will be true if the window is activated or false if the window will no longer be the active window.
Because the name of the event is the functor of the compound event structure it is possible to use Prolog matching for selecting the appropriate method for handling an event.
![]() |
With callbacks it is also possible to have more than one method executed if an event occurs, for instance: two different prolog instances can react to the same onClick of the same button!
Each class that is derived from the window class has a collection of events to which can be responded. A callback can be added to respond to any of these events by using the built-in predicate add_callback/4.
| With the built-in predicate add_callback/3 an event can be send to a normal Prolog clause, not just to a destination instance like with add_callback/4. |
Each class derived from the window class can generate a variety of events, below there is an overview of all events of the window class:
| onCreate/1 | : | is executed when a visible window is being created |
| onClose/2 | : | is executed when a window is being closed, it is possible to prevent the window from closing |
| onEraseBackground/1 | : | when the background of a window must be erased this event is executed |
| onPaint/2 | : | when a window must be re-painted this event is executed |
| onShow/2 | : | is executed when a window is becoming visible or is being hidden |
| onEnable/1 | : | when a window is being disabled or enabled this event is executed |
| onActivate/2 | : | this event is executed when the user switches to another window of the same application |
| onKBFocus/3 | : | when a window receives the keyboard focus this event is executed |
| onSetCursor/3 | : | the window wants to know what cursor it must display for the mouse pointer |
| onMouseMove/5 | : | the mouse is moved over a window |
| onMouseDown/5 | : | when a mouse button is pressed this event is executed |
| onMouseUp/5 | : | a mouse button is released |
| onMouseDoubleClick/5 | : | a mouse button was double clicked |
| onClick/1 | : | the user clicks on a window |
| onKeyDown/3 | : | a key on the keyboard is pressed |
| onKeyUp/3 | : | a key on the keyboard is released |
| onChar/3 | : | during the processing of a key this event is executed with the ASCII value of the key pressed |
| onCommand/2 | : | a menu command was selected |
| onMenuSelect/2 | : | a menu item is selected |
| onSize/2 | : | a window was resized |
| onMove/2 | : | a window is moved to a new position |
| onInitPopupMenu/2 | : | the user wants to view a floating popup menu for the window |
| onVScroll/3 | : | an event from the vertical scrollbar of the window |
| onHScroll/3 | : | an event from the horizontal scrollbar of the window |
| onDrop/5 | : | one or more dragged files was dropped on a window |
For more information about these events and their parameters see the reference of the window class.
see also: add_callback/3 add_callback/4 ispresent_callback/4 remove_callback/4