add_callback(+Source, +EventName, +Destination, +DestinationMethod)

This built-in predicate creates an event callback between a source and a destination object. The source object is the object which generates the event and the EventName must be the name of a documented event like onSize or onCreate. The DestinationMethod method must be the name of a public method of the class of the destination instance. The predicate add_callback/4 is part of the Windows API of Trinc-Prolog. A nice small example of using this type of callbacks is the 'hello world' example program. In the second table on this page the relevant part of that example program is shown

With add_callback/3 is it possible to send an event from an object to a normal Prolog clause (thus not to a destination instance).

see also: add_callback/3 callbacks class_name/2 clear_var/1 del_obj/1 get_instance/2 instance/1 new_obj/2 new_obja/2  ispresent_callback/4 object-oriented prolog remove_callback/4 <-/2 ::<-/1 ::<-/2 this/1

Examples
this(T),
add_callback(T, onCreate, T, doOnCreate),
the 'onCreate' event of an instance is connected to the method 'doOnCreate' of the same instance
add_callback(Instance, onBeforeSwitch, T, beforeSwitch) the event 'onBeforeSwitch' of a control window is connected to the method 'beforeSwitch' of another window

 

Hello world example program
class helloFrame.
  inherit frame.
  public.
    doOnCreate/1. %Method that will respond to creation of frame
    doOnSize/1. %Method that responds to resizing of panel
    doOnClose/1. %Responds to closing frame window
endclass helloFrame.

helloFrame::doOnCreate( _ ) :-
    putText('Hello World Example'),
    this(T),
    resize(point(150, 120), false),
    move(point(50, 50), false),
    createSinglePanel(Panel, Result),
    add_callback(Panel, onSize, T, doOnSize), %Add a callback for resize event
    add_callback(T, onClose, T, doOnClose), %Add a callback for close event
    L new_obj label,
    L<-createCenter(Panel, 10, 10, 200, 20, Result),
    L<-putText('hello world'), !.

helloFrame::doOnSize( onSize(_, point(X,Y)) ) :-
    NewX is (X-200) // 2,
    NewY is (Y-20) // 2,
    label<-move(point(NewX, NewY), true).

helloFrame::doOnClose( _ ) :-
    prolog_vm,
    app<-startQuit.

 

Exceptions
Source and/or Destination are empty variables an instantiation_error exception is thrown
Source and/or Destination not refer to an instance of a class a type_error(class_instance, Var) exception is thrown
EventName and/or DestinationMethod are not atoms or variables containing atoms a type_error(atom, Name) exception is thrown
No event with the same name as EventName existence_error(class_method, Var) is thrown
No method with the same name as DestinationMethod that has 1 parameter existence_error(class_method, Var) is thrown

 

 

info@trinc-prolog.com