Project

General

Profile

Actions

Define the callbacks in Scol

When you code a graphic user interface (GUI), you should use a event-driven programming.

The developer doesn't need to program the main loop. Scol does it for him. However, he defines which events must be detected (or selected if any) and, for each defined event what Scol must do.

<defined_event> => <function_to_call> => <action_to_do>

The function to call is the callback.

In Scol, the generic manner is :

<defined_event> : _CBxxxyyy where xxx indicates the object type (win, text, tree, ...) and yyy is an event
<object> : the handler (this window, this text, htis tree ...)
<function_to_call> : the callback, the function called when the event occurs. A callback has always the prefix @ (see also Function_object_in_Scol).
<user_parameter> : a parameter at your convenience. It will be passed to the callback

By example, to define the event "window closed by the user" :

_CBwinDestroy window_object @cbWinDestroy parameter

Depending on the event, each callback must have a specific typing. The Scol reference language provides these typing.

Example

We create a simple window. If the object exists, we define two callback :
  1. the window is destroyed (typically, the user closes it) : *_CBwinDestroy*. In this case, the application exists (the VM is also destroyed after the window).
  2. the window moves on the screen : *_CBwinMove*. In this second case, the console displays the new coordinates.

Note : sprintf is a Syspack function

// function executed when the close event occurs
fun cbDestroy (object, our_parameter)=
    _fooS our_parameter;
    _closemachine;;

// function executed when the move event occurs
fun cbMove (object, our_parameter, new_x, new_y)=
    _fooS sprintf "%s : %d %d" [our_paramter new_x new_y];
    0;;

fun createGui ()=
    // a new window should be created
    let _CRwindow _channel nil 0 0 320 240 WN_NORMAL "Tutorial callback" -> window in
    if window == nil then
    (
        _fooS "Unable to create a window";
        1
    )
    else
    (
        _fooS "Window created successfully";
        // define a callback on the close event
        _CBwinDestroy window @cbDestroy "Closed event";
        // define a callback on the move event
        _CBwinMove window @cbMove "New position";
        0
    );;

fun main ()=
    _showconsole;
    createGui;
    0;;

Updated by iri about 12 years ago · 1 revisions