www.gibmonks.com


Бесплатный семинар управление строительными проектами.

  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Part VI.  C Programming


Chapter 46. Writing a Tk Widget in C

This chapter describes the implementation of a simple clock widget. Two implementations are shown: the original string-based command interface and the Tcl_Obj command interface.

A custom widget implemented in C has the advantage of being efficient and flexible. However, it requires more work, too. This chapter illustrates the effort by explaining the implementation of a clock widget. It is a digital clock that displays the current time according to a format string. This is something you could implement in several lines of Tcl using a label widget, the clock command, and after for periodic updates. However, the point of the example is to show the basic structure for a Tk widget implemented in C, not how much easier Tcl programming is :-). The implementation of a widget includes:

  • A data structure to describe one instance of the widget.

  • A class procedure to create a new instance of the widget.

  • An instance procedure to operate on an instance of the widget.

  • A set of configuration options for the widget.

  • A configuration procedure used when creating and reconfiguring the widget.

  • An event handling procedure.

  • A display procedure.

  • Other widget-specific procedures.

Two implementations are compared: string-based and Tcl_Obj based. The version that uses Tcl_Obj values can interpret command line options more efficiently. A new option parsing package hides most of the details. The string-based version of each procedure is shown first, and then the Tcl_Obj version is shown for comparison. The display portion of the code is the same in the two versions.


      Previous section   Next section
    Top