.so draw-new
\c{
Creating new widgets
Having taken care of the basic components
of the drawing tool, that is the toolbox,
{\em menu_bar} and tablet widgets, all that remains
to be done is to define
a suitable {\em file_handler},
appropriate handlers for the
various drawing modes
and a {\em help_handler}.
We will skip the {\em file_handler}, but look at the
latter two issues in
sections [canvas] and
[hyper], respectively.
However, before that it will be shown how we may grant
the drawtool the status of a veritable
Tk widget, by defining a
drawtool handler class
and a corresponding drawtool widget command.
See slide [draw-new].
}
\c{
Defining a widget command involves three steps: (I)
the declaration of the binding between
a command and a handler,
(II) the definition of the
action function, which actually defines a mini-interpreter,
and (III) the definition of the actual
creation of the widget and its declaration
as a Tcl/Tk command.
Step (I) is straightforward.
We need to define an empty handler, which will
be associated with the drawtool command
when starting the application. See slide [new-command] (a).
The functionality offered by the
interpreter defined by the
function in (II) is kept quite simple,
but may easily be extended.
When the first argument of the call is drawtool,
a new drawtool widget is created as specified in (III),
except when the second argument is self.
In that case,
the virtual
path of the widget is returned, which is actually
the path of the tablet's canvas.
It is the responsibility of the writer of the script
that the self command is not addressed to the
empty handler.
If neither of these cases apply,
the function is invoked
for , with the remaining arguments flattened
to a string.
This allows for using the drawtool almost as an ordinary
canvas. See the example hypertext script shown in
section [hyper].
}
\c{
The creation of the actual widget and
declaration of the corresponding Tcl command,
according to the Tk convention,
is somewhat more involved (III).
Recall that each Tk widget is identified by
its path, which simultaneously
defines a command that may be used
to configure the widget or,
as for a canvas,
to draw figures on the screen.
Hence, the function create must
create a new widget and declare the widget
to be the handler of the command
corresponding to its path name.
}
.so new-applic
\c{
The application class depicted in slide [new-command]
will by now look familiar,
except for the function prelude.
In the body of the prelude function,
the Tcl command drawtool is declared,
with an instance of drawtool as its handler (a).
In this way, the drawtool widget
is made available as a command when
the program is used as an interpreter.
However, in the main function
this declaration is overridden (b).
Instead, the actual drawtool widget
is made the handler of the command,
to allow for a script to
address the drawtool by calling
drawtool self.
}
.so discussion