4 * This module implements "square" widgets that are object
5 * based. A "square" is a widget that displays a single square that can
6 * be moved around and resized. This file is intended as an example
7 * of how to build a widget; it isn't included in the
8 * normal wish, but it is included in "tktest".
10 * Copyright (c) 1997 Sun Microsystems, Inc.
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
19 #define __NO_OLD_CONFIG
24 * A data structure of the following type is kept for each square
25 * widget managed by this file:
29 Tk_Window tkwin; /* Window that embodies the square. NULL
30 * means window has been deleted but
31 * widget record hasn't been cleaned up yet. */
32 Display *display; /* X's token for the window's display. */
33 Tcl_Interp *interp; /* Interpreter associated with widget. */
34 Tcl_Command widgetCmd; /* Token for square's widget command. */
35 Tk_OptionTable optionTable; /* Token representing the configuration
37 Tcl_Obj *xPtr, *yPtr; /* Position of square's upper-left corner
40 Tcl_Obj *sizeObjPtr; /* Width and height of square. */
43 * Information used when displaying widget:
46 Tcl_Obj *borderWidthPtr; /* Width of 3-D border around whole widget. */
50 GC gc; /* Graphics context for copying from
51 * off-screen pixmap onto screen. */
52 Tcl_Obj *doubleBufferPtr; /* Non-zero means double-buffer redisplay
53 * with pixmap; zero means draw straight
54 * onto the display. */
55 int updatePending; /* Non-zero means a call to SquareDisplay
56 * has already been scheduled. */
60 * Information used for argv parsing.
63 static Tk_OptionSpec optionSpecs[] = {
64 {TK_OPTION_BORDER, "-background", "background", "Background",
65 "#d9d9d9", Tk_Offset(Square, bgBorderPtr), -1, 0,
66 (ClientData) "white"},
67 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL,
68 (char *) NULL, 0, -1, 0, (ClientData) "-borderwidth"},
69 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
70 (char *) NULL, 0, -1, 0, (ClientData) "-background"},
71 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
72 "2", Tk_Offset(Square, borderWidthPtr), -1},
73 {TK_OPTION_BOOLEAN, "-dbl", "doubleBuffer", "DoubleBuffer",
74 "1", Tk_Offset(Square, doubleBufferPtr), -1},
75 {TK_OPTION_SYNONYM, "-fg", (char *) NULL, (char *) NULL,
76 (char *) NULL, 0, -1, 0, (ClientData) "-foreground"},
77 {TK_OPTION_BORDER, "-foreground", "foreground", "Foreground",
78 "#b03060", Tk_Offset(Square, fgBorderPtr), -1, 0,
79 (ClientData) "black"},
80 {TK_OPTION_PIXELS, "-posx", "posx", "PosX", "0",
81 Tk_Offset(Square, xPtr), -1},
82 {TK_OPTION_PIXELS, "-posy", "posy", "PosY", "0",
83 Tk_Offset(Square, yPtr), -1},
84 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
85 "raised", Tk_Offset(Square, reliefPtr), -1},
86 {TK_OPTION_PIXELS, "-size", "size", "Size", "20",
87 Tk_Offset(Square, sizeObjPtr), -1},
92 * Forward declarations for procedures defined later in this file:
95 int SquareObjCmd _ANSI_ARGS_((ClientData clientData,
96 Tcl_Interp *interp, int objc,
97 Tcl_Obj * CONST objv[]));
98 static void SquareDeletedProc _ANSI_ARGS_((
99 ClientData clientData));
100 static int SquareConfigure _ANSI_ARGS_((Tcl_Interp *interp,
102 static void SquareDestroy _ANSI_ARGS_((char *memPtr));
103 static void SquareDisplay _ANSI_ARGS_((ClientData clientData));
104 static void KeepInWindow _ANSI_ARGS_((Square *squarePtr));
105 static void SquareObjEventProc _ANSI_ARGS_((ClientData clientData,
107 static int SquareWidgetObjCmd _ANSI_ARGS_((ClientData clientData,
108 Tcl_Interp *, int objc, Tcl_Obj * CONST objv[]));
111 *--------------------------------------------------------------
115 * This procedure is invoked to process the "square" Tcl
116 * command. It creates a new "square" widget.
119 * A standard Tcl result.
122 * A new widget is created and configured.
124 *--------------------------------------------------------------
128 SquareObjCmd(clientData, interp, objc, objv)
129 ClientData clientData; /* NULL. */
130 Tcl_Interp *interp; /* Current interpreter. */
131 int objc; /* Number of arguments. */
132 Tcl_Obj * CONST objv[]; /* Argument objects. */
136 Tk_OptionTable optionTable;
139 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
143 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
144 Tcl_GetStringFromObj(objv[1], NULL), (char *) NULL);
148 Tk_SetClass(tkwin, "Square");
151 * Create the option table for this widget class. If it has
152 * already been created, the refcount will get bumped and just
153 * the pointer will be returned. The refcount getting bumped
154 * does not concern us, because Tk will ensure the table is
155 * deleted when the interpreter is destroyed.
158 optionTable = Tk_CreateOptionTable(interp, optionSpecs);
161 * Allocate and initialize the widget record. The memset allows
162 * us to set just the non-NULL/0 items.
165 squarePtr = (Square *) ckalloc(sizeof(Square));
166 memset((void *) squarePtr, 0, (sizeof(Square)));
168 squarePtr->tkwin = tkwin;
169 squarePtr->display = Tk_Display(tkwin);
170 squarePtr->interp = interp;
171 squarePtr->widgetCmd = Tcl_CreateObjCommand(interp,
172 Tk_PathName(squarePtr->tkwin), SquareWidgetObjCmd,
173 (ClientData) squarePtr, SquareDeletedProc);
174 squarePtr->gc = None;
175 squarePtr->optionTable = optionTable;
177 if (Tk_InitOptions(interp, (char *) squarePtr, optionTable, tkwin)
179 Tk_DestroyWindow(squarePtr->tkwin);
180 ckfree((char *) squarePtr);
184 Tk_CreateEventHandler(squarePtr->tkwin, ExposureMask|StructureNotifyMask,
185 SquareObjEventProc, (ClientData) squarePtr);
186 if (Tk_SetOptions(interp, (char *) squarePtr, optionTable, objc - 2,
187 objv + 2, tkwin, NULL, (int *) NULL) != TCL_OK) {
190 if (SquareConfigure(interp, squarePtr) != TCL_OK) {
194 Tcl_SetObjResult(interp,
195 Tcl_NewStringObj(Tk_PathName(squarePtr->tkwin), -1));
199 Tk_DestroyWindow(squarePtr->tkwin);
204 *--------------------------------------------------------------
206 * SquareWidgetObjCmd --
208 * This procedure is invoked to process the Tcl command
209 * that corresponds to a widget managed by this module.
210 * See the user documentation for details on what it does.
213 * A standard Tcl result.
216 * See the user documentation.
218 *--------------------------------------------------------------
222 SquareWidgetObjCmd(clientData, interp, objc, objv)
223 ClientData clientData; /* Information about square widget. */
224 Tcl_Interp *interp; /* Current interpreter. */
225 int objc; /* Number of arguments. */
226 Tcl_Obj * CONST objv[]; /* Argument objects. */
228 Square *squarePtr = (Square *) clientData;
230 static CONST char *squareOptions[] = {"cget", "configure", (char *) NULL};
232 SQUARE_CGET, SQUARE_CONFIGURE
234 Tcl_Obj *resultObjPtr;
238 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg...?");
242 if (Tcl_GetIndexFromObj(interp, objv[1], squareOptions, "command",
243 0, &index) != TCL_OK) {
247 Tcl_Preserve((ClientData) squarePtr);
252 Tcl_WrongNumArgs(interp, 2, objv, "option");
255 resultObjPtr = Tk_GetOptionValue(interp, (char *) squarePtr,
256 squarePtr->optionTable, objv[2], squarePtr->tkwin);
257 if (resultObjPtr == NULL) {
260 Tcl_SetObjResult(interp, resultObjPtr);
264 case SQUARE_CONFIGURE: {
267 resultObjPtr = Tk_GetOptionInfo(interp, (char *) squarePtr,
268 squarePtr->optionTable, (Tcl_Obj *) NULL,
270 if (resultObjPtr == NULL) {
273 } else if (objc == 3) {
274 resultObjPtr = Tk_GetOptionInfo(interp, (char *) squarePtr,
275 squarePtr->optionTable, objv[2], squarePtr->tkwin);
276 if (resultObjPtr == NULL) {
280 result = Tk_SetOptions(interp, (char *) squarePtr,
281 squarePtr->optionTable, objc - 2, objv + 2,
282 squarePtr->tkwin, NULL, (int *) NULL);
283 if (result == TCL_OK) {
284 result = SquareConfigure(interp, squarePtr);
286 if (!squarePtr->updatePending) {
287 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
288 squarePtr->updatePending = 1;
291 if (resultObjPtr != NULL) {
292 Tcl_SetObjResult(interp, resultObjPtr);
296 Tcl_Release((ClientData) squarePtr);
300 Tcl_Release((ClientData) squarePtr);
305 *----------------------------------------------------------------------
309 * This procedure is called to process an argv/argc list in
310 * conjunction with the Tk option database to configure (or
311 * reconfigure) a square widget.
314 * The return value is a standard Tcl result. If TCL_ERROR is
315 * returned, then the interp's result contains an error message.
318 * Configuration information, such as colors, border width,
319 * etc. get set for squarePtr; old resources get freed,
322 *----------------------------------------------------------------------
326 SquareConfigure(interp, squarePtr)
327 Tcl_Interp *interp; /* Used for error reporting. */
328 Square *squarePtr; /* Information about widget. */
331 Tk_3DBorder bgBorder;
335 * Set the background for the window and create a graphics context
336 * for use during redisplay.
339 bgBorder = Tk_Get3DBorderFromObj(squarePtr->tkwin,
340 squarePtr->bgBorderPtr);
341 Tk_SetWindowBackground(squarePtr->tkwin,
342 Tk_3DBorderColor(bgBorder)->pixel);
343 Tcl_GetBooleanFromObj(NULL, squarePtr->doubleBufferPtr, &doubleBuffer);
344 if ((squarePtr->gc == None) && (doubleBuffer)) {
346 gcValues.function = GXcopy;
347 gcValues.graphics_exposures = False;
348 squarePtr->gc = Tk_GetGC(squarePtr->tkwin,
349 GCFunction|GCGraphicsExposures, &gcValues);
353 * Register the desired geometry for the window. Then arrange for
354 * the window to be redisplayed.
357 Tk_GeometryRequest(squarePtr->tkwin, 200, 150);
358 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->borderWidthPtr,
360 Tk_SetInternalBorder(squarePtr->tkwin, borderWidth);
361 if (!squarePtr->updatePending) {
362 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
363 squarePtr->updatePending = 1;
365 KeepInWindow(squarePtr);
370 *--------------------------------------------------------------
372 * SquareObjEventProc --
374 * This procedure is invoked by the Tk dispatcher for various
381 * When the window gets deleted, internal structures get
382 * cleaned up. When it gets exposed, it is redisplayed.
384 *--------------------------------------------------------------
388 SquareObjEventProc(clientData, eventPtr)
389 ClientData clientData; /* Information about window. */
390 XEvent *eventPtr; /* Information about event. */
392 Square *squarePtr = (Square *) clientData;
394 if (eventPtr->type == Expose) {
395 if (!squarePtr->updatePending) {
396 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
397 squarePtr->updatePending = 1;
399 } else if (eventPtr->type == ConfigureNotify) {
400 KeepInWindow(squarePtr);
401 if (!squarePtr->updatePending) {
402 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
403 squarePtr->updatePending = 1;
405 } else if (eventPtr->type == DestroyNotify) {
406 if (squarePtr->tkwin != NULL) {
407 Tk_FreeConfigOptions((char *) squarePtr, squarePtr->optionTable,
409 if (squarePtr->gc != None) {
410 Tk_FreeGC(squarePtr->display, squarePtr->gc);
412 squarePtr->tkwin = NULL;
413 Tcl_DeleteCommandFromToken(squarePtr->interp,
414 squarePtr->widgetCmd);
416 if (squarePtr->updatePending) {
417 Tcl_CancelIdleCall(SquareDisplay, (ClientData) squarePtr);
419 Tcl_EventuallyFree((ClientData) squarePtr, SquareDestroy);
424 *----------------------------------------------------------------------
426 * SquareDeletedProc --
428 * This procedure is invoked when a widget command is deleted. If
429 * the widget isn't already in the process of being destroyed,
430 * this command destroys it.
436 * The widget is destroyed.
438 *----------------------------------------------------------------------
442 SquareDeletedProc(clientData)
443 ClientData clientData; /* Pointer to widget record for widget. */
445 Square *squarePtr = (Square *) clientData;
446 Tk_Window tkwin = squarePtr->tkwin;
449 * This procedure could be invoked either because the window was
450 * destroyed and the command was then deleted (in which case tkwin
451 * is NULL) or because the command was deleted, and then this procedure
452 * destroys the widget.
456 Tk_DestroyWindow(tkwin);
461 *--------------------------------------------------------------
465 * This procedure redraws the contents of a square window.
466 * It is invoked as a do-when-idle handler, so it only runs
467 * when there's nothing else for the application to do.
473 * Information appears on the screen.
475 *--------------------------------------------------------------
479 SquareDisplay(clientData)
480 ClientData clientData; /* Information about window. */
482 Square *squarePtr = (Square *) clientData;
483 Tk_Window tkwin = squarePtr->tkwin;
486 int borderWidth, size, relief;
487 Tk_3DBorder bgBorder, fgBorder;
490 squarePtr->updatePending = 0;
491 if (!Tk_IsMapped(tkwin)) {
496 * Create a pixmap for double-buffering, if necessary.
499 Tcl_GetBooleanFromObj(NULL, squarePtr->doubleBufferPtr, &doubleBuffer);
501 pm = Tk_GetPixmap(Tk_Display(tkwin), Tk_WindowId(tkwin),
502 Tk_Width(tkwin), Tk_Height(tkwin),
503 DefaultDepthOfScreen(Tk_Screen(tkwin)));
506 d = Tk_WindowId(tkwin);
510 * Redraw the widget's background and border.
513 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->borderWidthPtr,
515 bgBorder = Tk_Get3DBorderFromObj(squarePtr->tkwin,
516 squarePtr->bgBorderPtr);
517 Tk_GetReliefFromObj(NULL, squarePtr->reliefPtr, &relief);
518 Tk_Fill3DRectangle(tkwin, d, bgBorder, 0, 0, Tk_Width(tkwin),
519 Tk_Height(tkwin), borderWidth, relief);
522 * Display the square.
525 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->sizeObjPtr, &size);
526 fgBorder = Tk_Get3DBorderFromObj(squarePtr->tkwin,
527 squarePtr->fgBorderPtr);
528 Tk_Fill3DRectangle(tkwin, d, fgBorder, squarePtr->x, squarePtr->y, size,
529 size, borderWidth, TK_RELIEF_RAISED);
532 * If double-buffered, copy to the screen and release the pixmap.
536 XCopyArea(Tk_Display(tkwin), pm, Tk_WindowId(tkwin), squarePtr->gc,
537 0, 0, (unsigned) Tk_Width(tkwin), (unsigned) Tk_Height(tkwin),
539 Tk_FreePixmap(Tk_Display(tkwin), pm);
544 *----------------------------------------------------------------------
548 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
549 * to clean up the internal structure of a square at a safe time
550 * (when no-one is using it anymore).
556 * Everything associated with the square is freed up.
558 *----------------------------------------------------------------------
562 SquareDestroy(memPtr)
563 char *memPtr; /* Info about square widget. */
565 Square *squarePtr = (Square *) memPtr;
567 ckfree((char *) squarePtr);
571 *----------------------------------------------------------------------
575 * Adjust the position of the square if necessary to keep it in
576 * the widget's window.
582 * The x and y position of the square are adjusted if necessary
583 * to keep the square in the window.
585 *----------------------------------------------------------------------
589 KeepInWindow(squarePtr)
590 register Square *squarePtr; /* Pointer to widget record. */
593 int borderWidth, size;
595 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->borderWidthPtr,
597 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->xPtr,
599 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->yPtr,
601 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->sizeObjPtr, &size);
602 Tk_GetReliefFromObj(NULL, squarePtr->reliefPtr, &relief);
604 if (relief != TK_RELIEF_FLAT) {
607 i = (Tk_Width(squarePtr->tkwin) - bd) - (squarePtr->x + size);
611 i = (Tk_Height(squarePtr->tkwin) - bd) - (squarePtr->y + size);
615 if (squarePtr->x < bd) {
618 if (squarePtr->y < bd) {