next up previous contents index
Next: 21. Undoing operations Up: II. Widget Commands Previous: 19. XML support   Contents   Index


20. Notifications

It is sometimes necessary to react on certain changes to the structure of an SGML document in a flexible manner that depends on the exact type of change and the element to which it occurs. To deal with these situations, the sgml widgets allows you to define notification commands that are invoked when certain edit events occur in the sgml widget.

You may be familiar with this usage of notification commands from other Tk widgets where you specify a Tcl command that should be evaluated by the widget in response to certain events or user actions. For example, the Tk scrollbar widgets accept a -command option that defines which command should be invoked when the user manipulates the scrollbar.

Edit event are alterations related to the document structure. The sgml widget knows about the following predefined edit events:

insert an element has been inserted by the user
attribute an attribute value has changed
delete an element has been deleted
parent the parent of an element has changed
valid the status of an element has changed
context the context of the insert mark has changed
select the status of the selection has changed

Depending on the type of the edit event, a notification command will be invoked that can react on the edit event and perform any required actions. Notification commands are installed using the configure widget command. The relevant configuration options are described below:

A notification command is a Tcl script that must accept additional parameters depending that are appended to the command when it is invoked:

The following example procedure keeps track of the number of elements that have been inserted (or deleted) by the user. This is accomplished by storing a count for each element type in a global hash ecount that is indexed by the element name.

To utilize this example, the sgml widget must be instructed to invoke the procedure every time an element is deleted or inserted, i.e. the the procedure update_ecount must be installed as a notification command.

Notification commands for the sgml widget are specified as configuration options; either when the widget is created or by using the configure widget command. The following configuration options deal with notifications:

To install the example above as a notification command for delete and insert operations in an existing sgml widget .s, the following Tcl commands would be used:

.s configure -insertnotify update_ecount
.s configure -deletenotify update_ecount

After a notification command has been configured, it will be invoked every time the associated edit event occurs in the sgml widget.


next up previous contents index
Next: 21. Undoing operations Up: II. Widget Commands Previous: 19. XML support   Contents   Index
TkSGML Reference Manual