handle_events and Terminating a Dispatch Loop

The last thing we need to learn to complete the example is how to wait for a ProXL condition. The example we have so far will keep our window refreshed indefinitely, but to make this a stand-alone demo we need to wait, handling refresh events, until the user indicates he is finished with this demo. Let's say when you click a mouse button in the window, we destroy the window and exit.

This is the role of the handle_events/[0,1,2] predicates. The simplest, handle_events/0, will wait, handling all ProXL events, until all windows with callbacks are destroyed. handle_events/[1,2] may be made to return before all windows have been destroyed, may be used to get information back from callbacks, and to pass context information to a callback (so it can behave differently in different contexts). But for this simple example, handle_events/0 is perfectly adequate.

So all we need to do is arrange for our window to be destroyed when a mouse button is pressed in it:

     | ?- win(Window),
          put_window_attributes(Window,
                  [callback(button_press,
                         [],
                         destroy_window(Win))]).
     
     Window = window(2376568)
     

Upon receiving a button_press event, the ProXL procedure destroy_window/1 is invoked, destroying our window. At this point, handle_events/0 will return.

Let's try it out:

     | ?- handle_events.
     

Now press a mouse button while the cursor is in our window. The window should go away and handle_events/0 should return.