The $canvas$ widget

The Tk canvas widget offers a powerful means for creating structured graphics. The hush class canvas provides merely a simplified interface to the corresponding Tk widget. }
  class move_handler : public handler { 
\fbox{move_handler}
public: move_handler( canvas* cv ) : c(cv) { dragging = 0; } void press( event& e ) { x = e.x(); y = e.y(); id = c->overlapping(x, y); if (id) dragging = 1; } void motion( event& e ) { if (dragging) { id.move( e.x() - x, e.y() - y ); x = e.x(); y = e.y(); } } void release( event& ) { dragging = 0; } protected: canvas* c; int dragging; item id; int x,y; };

slide: The {\em move\_handler} class

As an example of the use of a canvas, consider the definition of the move_handler class in slide draw-move. The move_handler class is derived from the class handler. It makes use of the dispatch and operator() function defined for handler, but redefines the (virtual) functions press, motion and release. When creating an instance of move_handler, a pointer to the canvas must be given to the constructor. In addition, the class has data members to record position coordinates and whether a particular item is being moved. Actually, moving an item occurs by pressing the (left) mouse button on an item and dragging the item along. When the mouse button is released, moving stops. To identify the item, the function overlapping is used. The movement is determined by the distance between the last recorded position and the current position of the cursor. \fslide{hyper}{Hypertext help}{ \epsfbox{help.eps} } In an analogous manner, a box_handler may be defined. The box_handler sets dragging to true when the button is pressed and creates a rectangle of zero width and height. Each time the function motion is called, the item created in the previous round is deleted and a new rectangle is created by calling
   c->rectangle(x,y,e.x(),e.y());
  
where c is a pointer to the canvas and x and y the button pointer coordinates stored when dragging began. For circles and lines, it suffices to replace the call to rectangle with a call to the appropriate figure creation function.