This built-in predicate creates an event callback between a source object and a normal Prolog clause. 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 DestinationClause parameter must be the name of a public Prolog clause with arity 1. The predicate add_callback/3 is part of the Windows API of Trinc-Prolog. A nice small example of using this type of callbacks is the 'hello world 2' example program. In the second table on this page the relevant part of that example program is shown
With add_callback/4 is it possible to send an event from an object to another instance.
see also: add_callback/4 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, doOnCreate), |
the 'onCreate' event of an instance is connected to the clause with the name 'doOnCreate' and arity 1 |
| add_callback(Instance, onBeforeSwitch, beforeSwitch) | the event 'onBeforeSwitch' of a control window is connected to the Prolog clause with the name beforeSwitch and arity 1. |
| Hello world 2 example program |
| class helloFrame. inherit frame. public. doOnCreate/1. %Method that will respond to creation of frame endclass helloFrame. helloFrame::doOnCreate( _ ) :- putText('Hello World Example'), this(T), resize(point(150, 120), false), move(point(50, 50), false), createSinglePanel(Panel, Result), % %Use add_callback/3 to route events from "this" instance to a normal %Prolog clause -> not to a method of a class % add_callback(Panel, onSize, doOnSize), %Add a callback for resize event add_callback(T, onClose, doOnClose), %Add a callback for close event % L new_obj label, L<-createCenter(Panel, 10, 10, 200, 20, Result), L<-putText('hello world'), !. doOnSize( onSize(_, point(X,Y)) ) :- NewX is (X-200) // 2, NewY is (Y-20) // 2, label<-move(point(NewX, NewY), true). %Move control to new position, control redraws itself doOnClose( _ ) :- prolog_vm, %Exits if Virtual Machine is executing prolog program app<-startQuit. %If virtual machine then stop it |
| Exceptions | |
| Source is empty variable | an instantiation_error exception is thrown |
| Source does refer to an instance of a class | a type_error(class_instance, Var) exception is thrown |
| EventName or DestinationClause 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 |