Previous section   Next section

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

Table of Contents
Chapter 46.  Writing a Tk Widget in C

The Widget Data Structure

Each widget is associated with a data structure that describes it. Any widget structure will need a pointer to the Tcl interpreter, the Tk window, and the display. The interpreter is used in most of the Tcl and Tk library calls, and it provides a way to call out to the script or query and set Tcl variables. The Tk window is needed for various Tk operations, and the display is used when doing low-level graphic operations. The rest of the information in the data structure depends on the widget. The different types will be explained as they are used in the rest of the code. The structure for the clock widget follows:

Example 46-2 The Clock widget data structure.
#include "tk.h"
#include <sys/time.h>

typedef struct {
   Tk_Window tkwin;       /* The window for the widget */
   Display *display;      /* Tk's handle on the display */
   Tcl_Interp *interp;    /* Interpreter of the widget */
   Tcl_Command widgetCmd; /* clock instance command. */
   Tk_OptionTable optionTable; /* Used to parse options */
    * Clock-specific attributes.
   int borderWidth;       /* Size of 3-D border */
   Tcl_Obj *borderWidthPtr;/* Original string value */
   int relief;             /* Style of 3-D border */
   Tk_3DBorder background; /* Color for border & background */
   XColor *foreground;     /* Color for the text */
   XColor *highlight;      /* Color for active highlight */
   XColor *highlightBg;    /* Color for neutral highlight */
   int highlightWidth;     /* Thickness of highlight rim */
   Tcl_Obj *highlightWidthPtr; /* Original string value */
   Tk_Font tkfont;         /* Font info for the text */
   char *format;           /* Format for time string */
    * Graphic contexts and other support.
   GC textGC;              /* Text graphics context */
   Tk_TimerToken token;    /* Periodic callback handle*/
   char *clock;            /* Pointer to the clock string */
   int numChars;           /* length of the text */
   int textWidth;          /* in pixels */
   Tcl_Obj *widthPtr;      /* The original width string value*/
   int textHeight;         /* in pixels */
   Tcl_Obj *heightPtr;     /* The original height string value*/
   int padX;               /* Horizontal padding */
   Tcl_Obj *padXPtr;       /* The original padX string value*/
   int padY;               /* Vertical padding */
   Tcl_Obj *padYPtr;       /* The original padY string value */
   int flags;              /* Flags defined below */
 * Flag bit definitions.
#define REDRAW_PENDING 0x1
#define GOT_FOCUS      0x2
#define TICKING        0x4

      Previous section   Next section