Debian Codegraph @

pTk/mTk/generic/tkSquare.c

raw symbols SCIP
1 /*
2 * tkSquare.c --
3 *
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".
9 *
10 * Copyright (c) 1997 Sun Microsystems, Inc.
11 *
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 *
15 * RCS: @(#) $Id: tkSquare.c,v 1.5 2002/01/17 23:33:53 dgp Exp $
16 */
17
18 #include "tkInt.h"
19
20 /*
21 * A data structure of the following type is kept for each square
22 * widget managed by this file:
23 */
24
25 typedef struct {
26 Tk_Window tkwin; /* Window that embodies the square. NULL
27 * means window has been deleted but
28 * widget record hasn't been cleaned up yet. */
29 Display *display; /* X's token for the window's display. */
30 Tcl_Interp *interp; /* Interpreter associated with widget. */
31 Tcl_Command widgetCmd; /* Token for square's widget command. */
32 Tk_OptionTable optionTable; /* Token representing the configuration
33 * specifications. */
34 Tcl_Obj *xPtr, *yPtr; /* Position of square's upper-left corner
35 * within widget. */
36 int x, y;
37 Tcl_Obj *sizeObjPtr; /* Width and height of square. */
38
39 /*
40 * Information used when displaying widget:
41 */
42
43 Tcl_Obj *borderWidthPtr; /* Width of 3-D border around whole widget. */
44 Tcl_Obj *bgBorderPtr;
45 Tcl_Obj *fgBorderPtr;
46 Tcl_Obj *reliefPtr;
47 GC gc; /* Graphics context for copying from
48 * off-screen pixmap onto screen. */
49 Tcl_Obj *doubleBufferPtr; /* Non-zero means double-buffer redisplay
50 * with pixmap; zero means draw straight
51 * onto the display. */
52 int updatePending; /* Non-zero means a call to SquareDisplay
53 * has already been scheduled. */
54 } Square;
55
56 /*
57 * Information used for argv parsing.
58 */
59
60 static Tk_OptionSpec optionSpecs[] = {
61 {TK_OPTION_BORDER, "-background", "background", "Background",
62 "#d9d9d9", Tk_Offset(Square, bgBorderPtr), -1, 0,
63 (ClientData) "white"},
64 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL,
65 (char *) NULL, 0, -1, 0, (ClientData) "-borderwidth"},
66 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
67 (char *) NULL, 0, -1, 0, (ClientData) "-background"},
68 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
69 "2", Tk_Offset(Square, borderWidthPtr), -1},
70 {TK_OPTION_BOOLEAN, "-dbl", "doubleBuffer", "DoubleBuffer",
71 "1", Tk_Offset(Square, doubleBufferPtr), -1},
72 {TK_OPTION_SYNONYM, "-fg", (char *) NULL, (char *) NULL,
73 (char *) NULL, 0, -1, 0, (ClientData) "-foreground"},
74 {TK_OPTION_BORDER, "-foreground", "foreground", "Foreground",
75 "#b03060", Tk_Offset(Square, fgBorderPtr), -1, 0,
76 (ClientData) "black"},
77 {TK_OPTION_PIXELS, "-posx", "posx", "PosX", "0",
78 Tk_Offset(Square, xPtr), -1},
79 {TK_OPTION_PIXELS, "-posy", "posy", "PosY", "0",
80 Tk_Offset(Square, yPtr), -1},
81 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
82 "raised", Tk_Offset(Square, reliefPtr), -1},
83 {TK_OPTION_PIXELS, "-size", "size", "Size", "20",
84 Tk_Offset(Square, sizeObjPtr), -1},
85 {TK_OPTION_END}
86 };
87
88 /*
89 * Forward declarations for procedures defined later in this file:
90 */
91
92 int SquareObjCmd _ANSI_ARGS_((ClientData clientData,
93 Tcl_Interp *interp, int objc,
94 Tcl_Obj * CONST objv[]));
95 static void SquareDeletedProc _ANSI_ARGS_((
96 ClientData clientData));
97 static int SquareConfigure _ANSI_ARGS_((Tcl_Interp *interp,
98 Square *squarePtr));
99 static void SquareDestroy _ANSI_ARGS_((char *memPtr));
100 static void SquareDisplay _ANSI_ARGS_((ClientData clientData));
101 static void KeepInWindow _ANSI_ARGS_((Square *squarePtr));
102 static void SquareObjEventProc _ANSI_ARGS_((ClientData clientData,
103 XEvent *eventPtr));
104 static int SquareWidgetObjCmd _ANSI_ARGS_((ClientData clientData,
105 Tcl_Interp *, int objc, Tcl_Obj * CONST objv[]));
106
107 /*
108 *--------------------------------------------------------------
109 *
110 * SquareCmd --
111 *
112 * This procedure is invoked to process the "square" Tcl
113 * command. It creates a new "square" widget.
114 *
115 * Results:
116 * A standard Tcl result.
117 *
118 * Side effects:
119 * A new widget is created and configured.
120 *
121 *--------------------------------------------------------------
122 */
123
124 int
125 SquareObjCmd(clientData, interp, objc, objv)
126 ClientData clientData; /* NULL. */
127 Tcl_Interp *interp; /* Current interpreter. */
128 int objc; /* Number of arguments. */
129 Tcl_Obj *CONST objv[]; /* Argument objects. */
130 {
131 Square *squarePtr;
132 Tk_Window tkwin;
133 Tk_OptionTable optionTable;
134
135 if (objc < 2) {
136 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
137 return TCL_ERROR;
138 }
139
140 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
141 Tcl_GetStringFromObj(objv[1], NULL), (char *) NULL);
142 if (tkwin == NULL) {
143 return TCL_ERROR;
144 }
145 Tk_SetClass(tkwin, "Square");
146
147 /*
148 * Create the option table for this widget class. If it has
149 * already been created, the refcount will get bumped and just
150 * the pointer will be returned. The refcount getting bumped
151 * does not concern us, because Tk will ensure the table is
152 * deleted when the interpreter is destroyed.
153 */
154
155 optionTable = Tk_CreateOptionTable(interp, optionSpecs);
156
157 /*
158 * Allocate and initialize the widget record. The memset allows
159 * us to set just the non-NULL/0 items.
160 */
161
162 squarePtr = (Square *) ckalloc(sizeof(Square));
163 memset((void *) squarePtr, 0, (sizeof(Square)));
164
165 squarePtr->tkwin = tkwin;
166 squarePtr->display = Tk_Display(tkwin);
167 squarePtr->interp = interp;
168 squarePtr->widgetCmd = Tcl_CreateObjCommand(interp,
169 Tk_PathName(squarePtr->tkwin), SquareWidgetObjCmd,
170 (ClientData) squarePtr, SquareDeletedProc);
171 squarePtr->gc = None;
172 squarePtr->optionTable = optionTable;
173
174 if (Tk_InitOptions(interp, (char *) squarePtr, optionTable, tkwin)
175 != TCL_OK) {
176 Tk_DestroyWindow(squarePtr->tkwin);
177 ckfree((char *) squarePtr);
178 return TCL_ERROR;
179 }
180
181 Tk_CreateEventHandler(squarePtr->tkwin, ExposureMask|StructureNotifyMask,
182 SquareObjEventProc, (ClientData) squarePtr);
183 if (Tk_SetOptions(interp, (char *) squarePtr, optionTable, objc - 2,
184 objv + 2, tkwin, NULL, (int *) NULL) != TCL_OK) {
185 goto error;
186 }
187 if (SquareConfigure(interp, squarePtr) != TCL_OK) {
188 goto error;
189 }
190
191 Tcl_SetObjResult(interp,
192 Tcl_NewStringObj(Tk_PathName(squarePtr->tkwin), -1));
193 return TCL_OK;
194
195 error:
196 Tk_DestroyWindow(squarePtr->tkwin);
197 return TCL_ERROR;
198 }
199
200 /*
201 *--------------------------------------------------------------
202 *
203 * SquareWidgetObjCmd --
204 *
205 * This procedure is invoked to process the Tcl command
206 * that corresponds to a widget managed by this module.
207 * See the user documentation for details on what it does.
208 *
209 * Results:
210 * A standard Tcl result.
211 *
212 * Side effects:
213 * See the user documentation.
214 *
215 *--------------------------------------------------------------
216 */
217
218 static int
219 SquareWidgetObjCmd(clientData, interp, objc, objv)
220 ClientData clientData; /* Information about square widget. */
221 Tcl_Interp *interp; /* Current interpreter. */
222 int objc; /* Number of arguments. */
223 Tcl_Obj *CONST objv[]; /* Argument objects. */
224 {
225 Square *squarePtr = (Square *) clientData;
226 int result = TCL_OK;
227 static CONST char *squareOptions[] = {"cget", "configure", (char *) NULL};
228 enum {
229 SQUARE_CGET, SQUARE_CONFIGURE
230 };
231 Tcl_Obj *resultObjPtr;
232 int index;
233
234 if (objc < 2) {
235 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg...?");
236 return TCL_ERROR;
237 }
238
239 if (Tcl_GetIndexFromObj(interp, objv[1], squareOptions, "command",
240 0, &index) != TCL_OK) {
241 return TCL_ERROR;
242 }
243
244 Tcl_Preserve((ClientData) squarePtr);
245
246 switch (index) {
247 case SQUARE_CGET: {
248 if (objc != 3) {
249 Tcl_WrongNumArgs(interp, 2, objv, "option");
250 goto error;
251 }
252 resultObjPtr = Tk_GetOptionValue(interp, (char *) squarePtr,
253 squarePtr->optionTable, objv[2], squarePtr->tkwin);
254 if (resultObjPtr == NULL) {
255 result = TCL_ERROR;
256 } else {
257 Tcl_SetObjResult(interp, resultObjPtr);
258 }
259 break;
260 }
261 case SQUARE_CONFIGURE: {
262 resultObjPtr = NULL;
263 if (objc == 2) {
264 resultObjPtr = Tk_GetOptionInfo(interp, (char *) squarePtr,
265 squarePtr->optionTable, (Tcl_Obj *) NULL,
266 squarePtr->tkwin);
267 if (resultObjPtr == NULL) {
268 result = TCL_ERROR;
269 }
270 } else if (objc == 3) {
271 resultObjPtr = Tk_GetOptionInfo(interp, (char *) squarePtr,
272 squarePtr->optionTable, objv[2], squarePtr->tkwin);
273 if (resultObjPtr == NULL) {
274 result = TCL_ERROR;
275 }
276 } else {
277 result = Tk_SetOptions(interp, (char *) squarePtr,
278 squarePtr->optionTable, objc - 2, objv + 2,
279 squarePtr->tkwin, NULL, (int *) NULL);
280 if (result == TCL_OK) {
281 result = SquareConfigure(interp, squarePtr);
282 }
283 if (!squarePtr->updatePending) {
284 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
285 squarePtr->updatePending = 1;
286 }
287 }
288 if (resultObjPtr != NULL) {
289 Tcl_SetObjResult(interp, resultObjPtr);
290 }
291 }
292 }
293 Tcl_Release((ClientData) squarePtr);
294 return result;
295
296 error:
297 Tcl_Release((ClientData) squarePtr);
298 return TCL_ERROR;
299 }
300
301 /*
302 *----------------------------------------------------------------------
303 *
304 * SquareConfigure --
305 *
306 * This procedure is called to process an argv/argc list in
307 * conjunction with the Tk option database to configure (or
308 * reconfigure) a square widget.
309 *
310 * Results:
311 * The return value is a standard Tcl result. If TCL_ERROR is
312 * returned, then the interp's result contains an error message.
313 *
314 * Side effects:
315 * Configuration information, such as colors, border width,
316 * etc. get set for squarePtr; old resources get freed,
317 * if there were any.
318 *
319 *----------------------------------------------------------------------
320 */
321
322 static int
323 SquareConfigure(interp, squarePtr)
324 Tcl_Interp *interp; /* Used for error reporting. */
325 Square *squarePtr; /* Information about widget. */
326 {
327 int borderWidth;
328 Tk_3DBorder bgBorder;
329 int doubleBuffer;
330
331 /*
332 * Set the background for the window and create a graphics context
333 * for use during redisplay.
334 */
335
336 bgBorder = Tk_Get3DBorderFromObj(squarePtr->tkwin,
337 squarePtr->bgBorderPtr);
338 Tk_SetWindowBackground(squarePtr->tkwin,
339 Tk_3DBorderColor(bgBorder)->pixel);
340 Tcl_GetBooleanFromObj(NULL, squarePtr->doubleBufferPtr, &doubleBuffer);
341 if ((squarePtr->gc == None) && (doubleBuffer)) {
342 XGCValues gcValues;
343 gcValues.function = GXcopy;
344 gcValues.graphics_exposures = False;
345 squarePtr->gc = Tk_GetGC(squarePtr->tkwin,
346 GCFunction|GCGraphicsExposures, &gcValues);
347 }
348
349 /*
350 * Register the desired geometry for the window. Then arrange for
351 * the window to be redisplayed.
352 */
353
354 Tk_GeometryRequest(squarePtr->tkwin, 200, 150);
355 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->borderWidthPtr,
356 &borderWidth);
357 Tk_SetInternalBorder(squarePtr->tkwin, borderWidth);
358 if (!squarePtr->updatePending) {
359 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
360 squarePtr->updatePending = 1;
361 }
362 KeepInWindow(squarePtr);
363 return TCL_OK;
364 }
365
366 /*
367 *--------------------------------------------------------------
368 *
369 * SquareObjEventProc --
370 *
371 * This procedure is invoked by the Tk dispatcher for various
372 * events on squares.
373 *
374 * Results:
375 * None.
376 *
377 * Side effects:
378 * When the window gets deleted, internal structures get
379 * cleaned up. When it gets exposed, it is redisplayed.
380 *
381 *--------------------------------------------------------------
382 */
383
384 static void
385 SquareObjEventProc(clientData, eventPtr)
386 ClientData clientData; /* Information about window. */
387 XEvent *eventPtr; /* Information about event. */
388 {
389 Square *squarePtr = (Square *) clientData;
390
391 if (eventPtr->type == Expose) {
392 if (!squarePtr->updatePending) {
393 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
394 squarePtr->updatePending = 1;
395 }
396 } else if (eventPtr->type == ConfigureNotify) {
397 KeepInWindow(squarePtr);
398 if (!squarePtr->updatePending) {
399 Tcl_DoWhenIdle(SquareDisplay, (ClientData) squarePtr);
400 squarePtr->updatePending = 1;
401 }
402 } else if (eventPtr->type == DestroyNotify) {
403 if (squarePtr->tkwin != NULL) {
404 Tk_FreeConfigOptions((char *) squarePtr, squarePtr->optionTable,
405 squarePtr->tkwin);
406 if (squarePtr->gc != None) {
407 Tk_FreeGC(squarePtr->display, squarePtr->gc);
408 }
409 squarePtr->tkwin = NULL;
410 Tcl_DeleteCommandFromToken(squarePtr->interp,
411 squarePtr->widgetCmd);
412 }
413 if (squarePtr->updatePending) {
414 Tcl_CancelIdleCall(SquareDisplay, (ClientData) squarePtr);
415 }
416 Tcl_EventuallyFree((ClientData) squarePtr, SquareDestroy);
417 }
418 }
419
420 /*
421 *----------------------------------------------------------------------
422 *
423 * SquareDeletedProc --
424 *
425 * This procedure is invoked when a widget command is deleted. If
426 * the widget isn't already in the process of being destroyed,
427 * this command destroys it.
428 *
429 * Results:
430 * None.
431 *
432 * Side effects:
433 * The widget is destroyed.
434 *
435 *----------------------------------------------------------------------
436 */
437
438 static void
439 SquareDeletedProc(clientData)
440 ClientData clientData; /* Pointer to widget record for widget. */
441 {
442 Square *squarePtr = (Square *) clientData;
443 Tk_Window tkwin = squarePtr->tkwin;
444
445 /*
446 * This procedure could be invoked either because the window was
447 * destroyed and the command was then deleted (in which case tkwin
448 * is NULL) or because the command was deleted, and then this procedure
449 * destroys the widget.
450 */
451
452 if (tkwin != NULL) {
453 Tk_DestroyWindow(tkwin);
454 }
455 }
456
457 /*
458 *--------------------------------------------------------------
459 *
460 * SquareDisplay --
461 *
462 * This procedure redraws the contents of a square window.
463 * It is invoked as a do-when-idle handler, so it only runs
464 * when there's nothing else for the application to do.
465 *
466 * Results:
467 * None.
468 *
469 * Side effects:
470 * Information appears on the screen.
471 *
472 *--------------------------------------------------------------
473 */
474
475 static void
476 SquareDisplay(clientData)
477 ClientData clientData; /* Information about window. */
478 {
479 Square *squarePtr = (Square *) clientData;
480 Tk_Window tkwin = squarePtr->tkwin;
481 Pixmap pm = None;
482 Drawable d;
483 int borderWidth, size, relief;
484 Tk_3DBorder bgBorder, fgBorder;
485 int doubleBuffer;
486
487 squarePtr->updatePending = 0;
488 if (!Tk_IsMapped(tkwin)) {
489 return;
490 }
491
492 /*
493 * Create a pixmap for double-buffering, if necessary.
494 */
495
496 Tcl_GetBooleanFromObj(NULL, squarePtr->doubleBufferPtr, &doubleBuffer);
497 if (doubleBuffer) {
498 pm = Tk_GetPixmap(Tk_Display(tkwin), Tk_WindowId(tkwin),
499 Tk_Width(tkwin), Tk_Height(tkwin),
500 DefaultDepthOfScreen(Tk_Screen(tkwin)));
501 d = pm;
502 } else {
503 d = Tk_WindowId(tkwin);
504 }
505
506 /*
507 * Redraw the widget's background and border.
508 */
509
510 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->borderWidthPtr,
511 &borderWidth);
512 bgBorder = Tk_Get3DBorderFromObj(squarePtr->tkwin,
513 squarePtr->bgBorderPtr);
514 Tk_GetReliefFromObj(NULL, squarePtr->reliefPtr, &relief);
515 Tk_Fill3DRectangle(tkwin, d, bgBorder, 0, 0, Tk_Width(tkwin),
516 Tk_Height(tkwin), borderWidth, relief);
517
518 /*
519 * Display the square.
520 */
521
522 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->sizeObjPtr, &size);
523 fgBorder = Tk_Get3DBorderFromObj(squarePtr->tkwin,
524 squarePtr->fgBorderPtr);
525 Tk_Fill3DRectangle(tkwin, d, fgBorder, squarePtr->x, squarePtr->y, size,
526 size, borderWidth, TK_RELIEF_RAISED);
527
528 /*
529 * If double-buffered, copy to the screen and release the pixmap.
530 */
531
532 if (doubleBuffer) {
533 XCopyArea(Tk_Display(tkwin), pm, Tk_WindowId(tkwin), squarePtr->gc,
534 0, 0, (unsigned) Tk_Width(tkwin), (unsigned) Tk_Height(tkwin),
535 0, 0);
536 Tk_FreePixmap(Tk_Display(tkwin), pm);
537 }
538 }
539
540 /*
541 *----------------------------------------------------------------------
542 *
543 * SquareDestroy --
544 *
545 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
546 * to clean up the internal structure of a square at a safe time
547 * (when no-one is using it anymore).
548 *
549 * Results:
550 * None.
551 *
552 * Side effects:
553 * Everything associated with the square is freed up.
554 *
555 *----------------------------------------------------------------------
556 */
557
558 static void
559 SquareDestroy(memPtr)
560 char *memPtr; /* Info about square widget. */
561 {
562 Square *squarePtr = (Square *) memPtr;
563
564 ckfree((char *) squarePtr);
565 }
566
567 /*
568 *----------------------------------------------------------------------
569 *
570 * KeepInWindow --
571 *
572 * Adjust the position of the square if necessary to keep it in
573 * the widget's window.
574 *
575 * Results:
576 * None.
577 *
578 * Side effects:
579 * The x and y position of the square are adjusted if necessary
580 * to keep the square in the window.
581 *
582 *----------------------------------------------------------------------
583 */
584
585 static void
586 KeepInWindow(squarePtr)
587 register Square *squarePtr; /* Pointer to widget record. */
588 {
589 int i, bd, relief;
590 int borderWidth, size;
591
592 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->borderWidthPtr,
593 &borderWidth);
594 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->xPtr,
595 &squarePtr->x);
596 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->yPtr,
597 &squarePtr->y);
598 Tk_GetPixelsFromObj(NULL, squarePtr->tkwin, squarePtr->sizeObjPtr, &size);
599 Tk_GetReliefFromObj(NULL, squarePtr->reliefPtr, &relief);
600 bd = 0;
601 if (relief != TK_RELIEF_FLAT) {
602 bd = borderWidth;
603 }
604 i = (Tk_Width(squarePtr->tkwin) - bd) - (squarePtr->x + size);
605 if (i < 0) {
606 squarePtr->x += i;
607 }
608 i = (Tk_Height(squarePtr->tkwin) - bd) - (squarePtr->y + size);
609 if (i < 0) {
610 squarePtr->y += i;
611 }
612 if (squarePtr->x < bd) {
613 squarePtr->x = bd;
614 }
615 if (squarePtr->y < bd) {
616 squarePtr->y = bd;
617 }
618 }