Graphics and hypertext

\c{ The Tk toolkit offers powerful facilities for graphics and (hyper)text (see Ousterhout, 1993).\index{Ousterhout (1993)} In this section we will discuss only the canvas widget offered by Tk. Instead of looking at the text widget provided by Tk, we will (briefly) look at the hypertext widget, which presents an alternative approach to defining hyperstructures. } .so sli-item \c{

The $item$ class

The canvas widget allows the programmer to create a number of built-in graphic items. Items are given a numerical index when created and, in addition, they may be given a (string) tag. Tags allow items to be manipulated in a group-wise fashion. To deal with items in a C++ context, the hush library contains a class item of which the functionality is shown in slide class-item. } \c{ Instances of item may not be created directly by the user, but instead are created by the canvas widget. For an item, its index may be obtained by casting the item to int. If the index does not identify an existing item, it will be zero. Existing items may be moved, in a relative way, by the move function. } \c{ In a similar way as for widgets, items may be associated with events, either explicitly by using item::bind, or implicitly by using item::handler. The default bindings for items are identical to the default bindings for the canvas widget, but these may be overridden by descendant classes. Similar to the widget class, the item class is derived from the handler class. This allows the user to define possibly compound shapes defining their own handler. }

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. \c{

The $hypertext$ widget

Both the Tk canvas and text widget allow the binding of actions to particular items and hence defining dynamically what we may call hyperstructures. A different, in a way more static, approach is offered by the hypertext widget originally developed by George Howlett. %%george.howlett@att.com. } .so sli-help \c{ The hypertext widget may be used to display text files containing embedded Tcl code. The Tcl code must be placed between escapes, that take the form of {\tt %%} for both the start and end of the code. A screen shot of a fragment of the online help for drawtool is given in slide hyper. Notice that the online help provides a replica of the drawtool application, surrounded by text. When looking at (again a fragment of) the hypertext file specifying the contents of the online help, given in slide help-text, you see that the drawtool command defined in section new is employed to create the embedded widget. } \c{ When specifying the hypertext file, widgets may be given a pathname relative to the pathname of the hypertext widget by using the variable this. In addition, the hypertext widget offers the variables thisline and thisfile to identify the current line number and current file name. } \c{ Any of the widgets and commands offered by Tcl/Tk or supported by hush may be included in a hypertext file, including the ones defined by the program itself. }