[-<--%--^-->-]
The Tk toolkit offers powerful facilities
for graphics and (hyper)text. See Ousterhout93.
In this section we discuss 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.
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 below.
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 function move.
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 as the widget class, the item class
is derived from the handler class.
This allows the user to define possibly compound
shapes that have their own handlers.
The Tk canvas widget offers powerful means for
doing structured graphics.
The hush class $canvas$ provides merely
a simplified interface to
the corresponding Tk widget.
As an example of the use of a canvas, consider the
definition of the move_handler
below.
The move_handler defines one of the modes of the
tablet and allows for moving graphical items
on the canvas.
Both the Tk canvas and text widget allow to bind
actions to particular items
and hence to define 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.
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 %%
for both the begin and end of the code.
A screen shot of a fragment of the on-line help
for drawtool is given in
figure Help.
Notice, that the on-line 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 on-line help,
given below,
you see that the drawtool command defined
in section new
is employed to create the embedded widget:
Rubber banding: press the left mouse button
and release when the rectangle is of appropriate
size
%%
drawtool $this.draw
$this append $this.draw
$this.draw create rectangle 20 20 80 80
$this.draw create rectangle 10 30 70 90
$this.draw create oval 40 40 90 90
$this append $this.draw
%%
For additional information click on the %%
button $this.goto -text instruction -command end-of-text
$this append $this.goto
%%
button. Press %%
button $this.quit -command { destroy . } -text quit -bg pink
$this append quit
%% to remove the window.
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.
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.
eliens@cs.vu.nl