Event Handling Loop

At some point the application must start handling events. The procedure for obtaining these events is xtAppNextEvent/2 and the procedure for dispatching them to the widgets is xtDispatchEvent/1. The call to xtAppMainLoop/1 starts an endless loop where these two procedures are called one after another. Of course, the programmer can always write her own event loop, specifying some exit conditions, etc. The following example is a slight modification of the first program, which exits the event loop when the button is pressed.

     :- dynamic exit_loop/1.
     
     exit_loop(no).
     
     exit_callback(_Widget,_CLientData,_CallData) :-
             retract(exit_loop(no)),
             assert(exit_loop(yes)).
     
     create_button :-
             xtAppInitialize(App,'Test',[],Shell),
             xmCreatePushButton(Shell,push_button,
                                [xmNwidth(100), xmNheight(100)],
                                Button),
             xtManageChild(Button),
             xtAddCallback(Button,xmNactivateCallback,exit_callback,_),
             xtRealizeWidget(Shell),
             main_loop(App).
     
     main_loop(App) :-
             (   exit_loop(yes)
             ->  write('Exiting...'), nl
             ;   xtAppNextEvent(App,Event),
                 xtDispatchEvent(Event),
                 main_loop(App)
             ).