Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: pgplotwidget.deactivatecallback - Function Up: pgplotwidget - Tool Previous: pgplotwidget.shortnametofullname - Function


pgplotwidget.setcallback - Function



Package display
Module plotter
Tool pgplotwidget


set a function to be called for mouse or keyboard events


Synopsis
setcallback(name, callback)


Description
This function allows a function of yours to be called whenever a certain event happens. It is a simplification of the event-binding described in the Glish/PGPLOT chapter of the Glish manual.

The function returns an index which you can use to deactivate the callback.

At present, the events for which you may set a callback are:

motion
Called whnenver the mouse moves in the plot area.
button
Called whenever any button goes down.
button1
Called whenever button 1 goes down.
button2
Called whenever button 2 goes down.
button3
Called whenever button 3 goes down.
buttonup
Called whenever a button goes up.
key
Called whenever a key is pressed on the keyboard.

The callback functions take a single argument, which will be a record of the type described in the manual. Or of course you could just pass in the callback function:

   callback := function(rec) {print rec;}
to discover the record structure. Note that to follow a drag you merely follow the motion after a button down and before a button up.



Example
For example, here is how you could implement a little ``scribble'' application.
include 'pgplotwidget.g'
f := frame();
pg := pgplotwidget(f);
pos := [dragging=F];

# Drag starts
downcallback := function(rec) {
    global pos;
    pos := [dragging=T, xlast=rec.world[1], ylast=rec.world[2]];
}

# Draw the new line segment if we are dragging
motioncallback := function(rec) {
    global pos;
    if (pos.dragging) {
        x := rec.world[1]; y := rec.world[2];
        pg.line([pos.xlast, x], [pos.ylast, y]);
        pos.xlast := x; pos.ylast := y;
    }
}

# Drag ends
upcallback := function(rec) {
    global pos;
    pos.dragging := F;
}

pg.env(0, 1, 0, 1, 0, -1);
pg.setcallback('button', downcallback);
pg.setcallback('motion', motioncallback);
pg.setcallback('buttonup', upcallback);
pg.message('Start scribbling! Drag the mouse with a button');
The amount of code could have been reduced somewhat by having only one callback for both button and buttonup, and using the ``ButtonPress'' field of the record to distinguish up from down events.



Arguments

name   events for which to call supply callback
    Allowed: motion,key,button,button[1-3],buttonup
callback   function to call on event
    Allowed: single argument function


Returns
integer index (for deactivatecallback)




next up previous contents index
Next: pgplotwidget.deactivatecallback - Function Up: pgplotwidget - Tool Previous: pgplotwidget.shortnametofullname - Function   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15