Debian Codegraph @

pTk/mTk/generic/tkMenubutton.c

raw symbols SCIP
1 /*
2 * tkMenubutton.c --
3 *
4 * This module implements button-like widgets that are used
5 * to invoke pull-down menus.
6 *
7 * Copyright (c) 1990-1994 The Regents of the University of California.
8 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkMenubutton.c,v 1.12.2.1 2003/11/17 23:29:36 hobbs Exp $
14 */
15
16 #include "tkPort.h"
17 #include "default.h"
18 #include "tkInt.h"
19 #include "tkMenubutton.h"
20 #include "tkVMacro.h"
21
22 /*
23 * The following table defines the legal values for the -direction
24 * option. It is used together with the "enum direction" declaration
25 * in tkMenubutton.h.
26 */
27
28 static char *directionStrings[] = {
29 "above", "below", "flush", "left", "right", (char *) NULL
30 };
31
32 /*
33 * The following table defines the legal values for the -state option.
34 * It is used together with the "enum state" declaration in tkMenubutton.h.
35 */
36
37 static char *stateStrings[] = {
38 "active", "disabled", "normal", (char *) NULL
39 };
40
41 /*
42 * The following table defines the legal values for the -compound option.
43 * It is used with the "enum compound" declaration in tkMenuButton.h
44 */
45
46 static char *compoundStrings[] = {
47 "bottom", "center", "left", "none", "right", "top", (char *) NULL
48 };
49
50 /*
51 * Information used for parsing configuration specs:
52 */
53
54 static Tk_OptionSpec optionSpecs[] = {
55 {TK_OPTION_BORDER, "-activebackground", "activeBackground", "Foreground",
56 DEF_MENUBUTTON_ACTIVE_BG_COLOR, -1,
57 Tk_Offset(TkMenuButton, activeBorder), 0,
58 (ClientData) DEF_MENUBUTTON_ACTIVE_BG_MONO, 0},
59 {TK_OPTION_COLOR, "-activeforeground", "activeForeground", "Background",
60 DEF_MENUBUTTON_ACTIVE_FG_COLOR, -1,
61 Tk_Offset(TkMenuButton, activeFg),
62 0, (ClientData) DEF_MENUBUTTON_ACTIVE_FG_MONO, 0},
63 {TK_OPTION_ANCHOR, "-anchor", "anchor", "Anchor",
64 DEF_MENUBUTTON_ANCHOR, -1,
65 Tk_Offset(TkMenuButton, anchor), 0, 0, 0},
66 {TK_OPTION_BORDER, "-background", "background", "Background",
67 DEF_MENUBUTTON_BG_COLOR, -1, Tk_Offset(TkMenuButton, normalBorder),
68 0, (ClientData) DEF_MENUBUTTON_BG_MONO, 0},
69 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL,
70 (char *) NULL, 0, -1, 0, (ClientData) "-borderwidth", 0},
71 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
72 (char *) NULL, 0, -1, 0, (ClientData) "-background", 0},
73 {TK_OPTION_BITMAP, "-bitmap", "bitmap", "Bitmap",
74 DEF_MENUBUTTON_BITMAP, -1, Tk_Offset(TkMenuButton, bitmap),
75 TK_OPTION_NULL_OK, 0, 0},
76 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
77 DEF_MENUBUTTON_BORDER_WIDTH, -1,
78 Tk_Offset(TkMenuButton, borderWidth), 0, 0, 0},
79 {TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
80 DEF_MENUBUTTON_CURSOR, -1, Tk_Offset(TkMenuButton, cursor),
81 TK_OPTION_NULL_OK, 0, 0},
82 {TK_OPTION_STRING_TABLE, "-direction", "direction", "Direction",
83 DEF_MENUBUTTON_DIRECTION, -1, Tk_Offset(TkMenuButton, direction),
84 0, (ClientData) directionStrings, 0},
85 {TK_OPTION_COLOR, "-disabledforeground", "disabledForeground",
86 "DisabledForeground", DEF_MENUBUTTON_DISABLED_FG_COLOR,
87 -1, Tk_Offset(TkMenuButton, disabledFg), TK_OPTION_NULL_OK,
88 (ClientData) DEF_MENUBUTTON_DISABLED_FG_MONO, 0},
89 {TK_OPTION_SYNONYM, "-fg", "foreground", (char *) NULL,
90 (char *) NULL, 0, -1, 0, (ClientData) "-foreground", 0},
91 {TK_OPTION_FONT, "-font", "font", "Font",
92 DEF_MENUBUTTON_FONT, -1, Tk_Offset(TkMenuButton, tkfont), 0, 0, 0},
93 {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
94 DEF_MENUBUTTON_FG, -1, Tk_Offset(TkMenuButton, normalFg), 0, 0, 0},
95 {TK_OPTION_OBJ, "-height", "height", "Height",
96 DEF_MENUBUTTON_HEIGHT, -1, Tk_Offset(TkMenuButton, heightString),
97 0, 0, 0},
98 {TK_OPTION_COLOR, "-highlightbackground", "highlightBackground",
99 "HighlightBackground", DEF_MENUBUTTON_HIGHLIGHT_BG_COLOR,
100 -1, Tk_Offset(TkMenuButton, highlightBgColorPtr), 0, 0, 0},
101 {TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
102 DEF_MENUBUTTON_HIGHLIGHT, -1,
103 Tk_Offset(TkMenuButton, highlightColorPtr), 0, 0, 0},
104 {TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness",
105 "HighlightThickness", DEF_MENUBUTTON_HIGHLIGHT_WIDTH,
106 -1, Tk_Offset(TkMenuButton, highlightWidth), 0, 0, 0},
107 {TK_OPTION_STRING, "-image", "image", "Image",
108 DEF_MENUBUTTON_IMAGE, -1, Tk_Offset(TkMenuButton, imageString),
109 TK_OPTION_NULL_OK, 0, 0},
110 {TK_OPTION_BOOLEAN, "-indicatoron", "indicatorOn", "IndicatorOn",
111 DEF_MENUBUTTON_INDICATOR, -1, Tk_Offset(TkMenuButton, indicatorOn),
112 0, 0, 0},
113 {TK_OPTION_JUSTIFY, "-justify", "justify", "Justify",
114 DEF_BUTTON_JUSTIFY, -1, Tk_Offset(TkMenuButton, justify), 0, 0, 0},
115 {TK_OPTION_WINDOW, "-menu", "menu", "Menu",
116 DEF_MENUBUTTON_MENU, -1, Tk_Offset(TkMenuButton, menuName),
117 TK_OPTION_NULL_OK, 0, 0},
118 {TK_OPTION_PIXELS, "-padx", "padX", "Pad",
119 DEF_MENUBUTTON_PADX, -1, Tk_Offset(TkMenuButton, padX),
120 0, 0, 0},
121 {TK_OPTION_PIXELS, "-pady", "padY", "Pad",
122 DEF_MENUBUTTON_PADY, -1, Tk_Offset(TkMenuButton, padY),
123 0, 0, 0},
124 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
125 DEF_MENUBUTTON_RELIEF, -1, Tk_Offset(TkMenuButton, relief),
126 0, 0, 0},
127 {TK_OPTION_STRING_TABLE, "-compound", "compound", "Compound",
128 DEF_BUTTON_COMPOUND, -1, Tk_Offset(TkMenuButton, compound), 0,
129 (ClientData) compoundStrings, 0},
130 {TK_OPTION_STRING_TABLE, "-state", "state", "State",
131 DEF_MENUBUTTON_STATE, -1, Tk_Offset(TkMenuButton, state),
132 0, (ClientData) stateStrings, 0},
133 {TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
134 DEF_MENUBUTTON_TAKE_FOCUS, -1,
135 Tk_Offset(TkMenuButton, takeFocus), TK_OPTION_NULL_OK, 0, 0},
136 {TK_OPTION_STRING, "-text", "text", "Text",
137 DEF_MENUBUTTON_TEXT, -1, Tk_Offset(TkMenuButton, text), 0, 0, 0},
138 {TK_OPTION_OBJ, "-textvariable", "textVariable", "Variable",
139 DEF_MENUBUTTON_TEXT_VARIABLE,
140 -1, Tk_Offset(TkMenuButton, textVarName), TK_OPTION_NULL_OK, 0, 0},
141 {TK_OPTION_INT, "-underline", "underline", "Underline",
142 DEF_MENUBUTTON_UNDERLINE, -1, Tk_Offset(TkMenuButton, underline),
143 0, 0, 0},
144 {TK_OPTION_OBJ, "-width", "width", "Width",
145 DEF_MENUBUTTON_WIDTH, -1, Tk_Offset(TkMenuButton, widthString),
146 0, 0, 0},
147 {TK_OPTION_PIXELS, "-wraplength", "wrapLength", "WrapLength",
148 DEF_MENUBUTTON_WRAP_LENGTH, -1, Tk_Offset(TkMenuButton, wrapLength),
149 0, 0, 0},
150 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
151 (char *) NULL, 0, 0}
152 };
153
154 /*
155 * The following tables define the menubutton widget commands and map the
156 * indexes into the string tables into a single enumerated type used
157 * to dispatch the scale widget command.
158 */
159
160 static CONST char *commandNames[] = {
161 "cget", "configure", (char *) NULL
162 };
163
164 enum command {
165 COMMAND_CGET, COMMAND_CONFIGURE
166 };
167
168 /*
169 * Forward declarations for procedures defined later in this file:
170 */
171
172 static void MenuButtonCmdDeletedProc _ANSI_ARGS_((
173 ClientData clientData));
174 static void MenuButtonEventProc _ANSI_ARGS_((ClientData clientData,
175 XEvent *eventPtr));
176 static void MenuButtonImageProc _ANSI_ARGS_((ClientData clientData,
177 int x, int y, int width, int height, int imgWidth,
178 int imgHeight));
179 static char * MenuButtonTextVarProc _ANSI_ARGS_((
180 ClientData clientData, Tcl_Interp *interp,
181 Var name1, CONST char *name2, int flags));
182 static int MenuButtonWidgetObjCmd _ANSI_ARGS_((
183 ClientData clientData, Tcl_Interp *interp,
184 int objc, Tcl_Obj *CONST objv[]));
185 static int ConfigureMenuButton _ANSI_ARGS_((Tcl_Interp *interp,
186 TkMenuButton *mbPtr, int objc,
187 Tcl_Obj *CONST objv[]));
188 static void DestroyMenuButton _ANSI_ARGS_((char *memPtr));
189
190 /*
191 *--------------------------------------------------------------
192 *
193 * Tk_MenubuttonObjCmd --
194 *
195 * This procedure is invoked to process the "button", "label",
196 * "radiobutton", and "checkbutton" Tcl commands. See the
197 * user documentation for details on what it does.
198 *
199 * Results:
200 * A standard Tcl result.
201 *
202 * Side effects:
203 * See the user documentation.
204 *
205 *--------------------------------------------------------------
206 */
207
208 int
209 Tk_MenubuttonObjCmd(clientData, interp, objc, objv)
210 ClientData clientData; /* NULL. */
211 Tcl_Interp *interp; /* Current interpreter. */
212 int objc; /* Number of arguments. */
213 Tcl_Obj *CONST objv[]; /* Argument objects. */
214 {
215 register TkMenuButton *mbPtr;
216 Tk_OptionTable optionTable;
217 Tk_Window tkwin;
218
219 if (objc < 2) {
220 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
221 return TCL_ERROR;
222 }
223
224 /*
225 * Create the new window.
226 */
227
228 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
229 Tcl_GetString(objv[1]), (char *) NULL);
230 if (tkwin == NULL) {
231 return TCL_ERROR;
232 }
233
234 /*
235 * Create the option table for this widget class. If it has already
236 * been created, the cached pointer will be returned.
237 */
238
239 optionTable = Tk_CreateOptionTable(interp, optionSpecs);
240
241 Tk_SetClass(tkwin, "Menubutton");
242 mbPtr = TkpCreateMenuButton(tkwin);
243
244 Tk_SetClassProcs(tkwin, &tkpMenubuttonClass, (ClientData) mbPtr);
245
246 /*
247 * Initialize the data structure for the button.
248 */
249
250 mbPtr->tkwin = tkwin;
251 mbPtr->display = Tk_Display (tkwin);
252 mbPtr->interp = interp;
253 mbPtr->widgetCmd = Tcl_CreateObjCommand(interp,
254 Tk_PathName(mbPtr->tkwin), MenuButtonWidgetObjCmd,
255 (ClientData) mbPtr, MenuButtonCmdDeletedProc);
256 mbPtr->optionTable = optionTable;
257 mbPtr->menuName = NULL;
258 mbPtr->text = NULL;
259 mbPtr->underline = -1;
260 mbPtr->textVarName = NULL;
261 mbPtr->bitmap = None;
262 mbPtr->imageString = NULL;
263 mbPtr->image = NULL;
264 mbPtr->state = STATE_NORMAL;
265 mbPtr->normalBorder = NULL;
266 mbPtr->activeBorder = NULL;
267 mbPtr->borderWidth = 0;
268 mbPtr->relief = TK_RELIEF_FLAT;
269 mbPtr->highlightWidth = 0;
270 mbPtr->highlightBgColorPtr = NULL;
271 mbPtr->highlightColorPtr = NULL;
272 mbPtr->inset = 0;
273 mbPtr->tkfont = NULL;
274 mbPtr->normalFg = NULL;
275 mbPtr->activeFg = NULL;
276 mbPtr->disabledFg = NULL;
277 mbPtr->normalTextGC = None;
278 mbPtr->activeTextGC = None;
279 mbPtr->gray = None;
280 mbPtr->disabledGC = None;
281 mbPtr->stippleGC = None;
282 mbPtr->leftBearing = 0;
283 mbPtr->rightBearing = 0;
284 mbPtr->widthString = NULL;
285 mbPtr->heightString = NULL;
286 mbPtr->width = 0;
287 mbPtr->width = 0;
288 mbPtr->wrapLength = 0;
289 mbPtr->padX = 0;
290 mbPtr->padY = 0;
291 mbPtr->anchor = TK_ANCHOR_CENTER;
292 mbPtr->justify = TK_JUSTIFY_CENTER;
293 mbPtr->textLayout = NULL;
294 mbPtr->indicatorOn = 0;
295 mbPtr->indicatorWidth = 0;
296 mbPtr->indicatorHeight = 0;
297 mbPtr->direction = DIRECTION_FLUSH;
298 mbPtr->cursor = None;
299 mbPtr->takeFocus = NULL;
300 mbPtr->flags = 0;
301
302 Tk_CreateEventHandler(mbPtr->tkwin,
303 ExposureMask|StructureNotifyMask|FocusChangeMask,
304 MenuButtonEventProc, (ClientData) mbPtr);
305
306 if (Tk_InitOptions(interp, (char *) mbPtr, optionTable, tkwin) != TCL_OK) {
307 Tk_DestroyWindow(mbPtr->tkwin);
308 return TCL_ERROR;
309 }
310
311 if (ConfigureMenuButton(interp, mbPtr, objc-2, objv+2) != TCL_OK) {
312 Tk_DestroyWindow(mbPtr->tkwin);
313 return TCL_ERROR;
314 }
315
316 Tcl_SetStringObj(Tcl_GetObjResult(interp), Tk_PathName(mbPtr->tkwin), -1);
317 return TCL_OK;
318 }
319
320 /*
321 *--------------------------------------------------------------
322 *
323 * MenuButtonWidgetObjCmd --
324 *
325 * This procedure is invoked to process the Tcl command
326 * that corresponds to a widget managed by this module.
327 * See the user documentation for details on what it does.
328 *
329 * Results:
330 * A standard Tcl result.
331 *
332 * Side effects:
333 * See the user documentation.
334 *
335 *--------------------------------------------------------------
336 */
337
338 static int
339 MenuButtonWidgetObjCmd(clientData, interp, objc, objv)
340 ClientData clientData; /* Information about button widget. */
341 Tcl_Interp *interp; /* Current interpreter. */
342 int objc; /* Number of arguments. */
343 Tcl_Obj *CONST objv[]; /* Argument objects. */
344 {
345 register TkMenuButton *mbPtr = (TkMenuButton *) clientData;
346 int result, index;
347 Tcl_Obj *objPtr;
348
349 if (objc < 2) {
350 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
351 return TCL_ERROR;
352 }
353 result = Tcl_GetIndexFromObj(interp, objv[1],
354 commandNames, "option", 0, &index);
355 if (result != TCL_OK) {
356 return result;
357 }
358 Tcl_Preserve((ClientData) mbPtr);
359
360 switch (index) {
361 case COMMAND_CGET: {
362 if (objc != 3) {
363 Tcl_WrongNumArgs(interp, 1, objv, "cget option");
364 goto error;
365 }
366
367 objPtr = Tk_GetOptionValue(interp, (char *) mbPtr,
368 mbPtr->optionTable, objv[2], mbPtr->tkwin);
369 if (objPtr == NULL) {
370 goto error;
371 } else {
372 Tcl_SetObjResult(interp, objPtr);
373 }
374 break;
375 }
376
377 case COMMAND_CONFIGURE: {
378 if (objc <= 3) {
379 objPtr = Tk_GetOptionInfo(interp, (char *) mbPtr,
380 mbPtr->optionTable,
381 (objc == 3) ? objv[2] : (Tcl_Obj *) NULL,
382 mbPtr->tkwin);
383 if (objPtr == NULL) {
384 goto error;
385 } else {
386 Tcl_SetObjResult(interp, objPtr);
387 }
388 } else {
389 result = ConfigureMenuButton(interp, mbPtr, objc-2,
390 objv+2);
391 }
392 break;
393 }
394 }
395 Tcl_Release((ClientData) mbPtr);
396 return result;
397
398 error:
399 Tcl_Release((ClientData) mbPtr);
400 return TCL_ERROR;
401 }
402
403 /*
404 *----------------------------------------------------------------------
405 *
406 * DestroyMenuButton --
407 *
408 * This procedure is invoked to recycle all of the resources
409 * associated with a menubutton widget. It is invoked as a
410 * when-idle handler in order to make sure that there is no
411 * other use of the menubutton pending at the time of the deletion.
412 *
413 * Results:
414 * None.
415 *
416 * Side effects:
417 * Everything associated with the widget is freed up.
418 *
419 *----------------------------------------------------------------------
420 */
421
422 static void
423 DestroyMenuButton(memPtr)
424 char *memPtr; /* Info about button widget. */
425 {
426 register TkMenuButton *mbPtr = (TkMenuButton *) memPtr;
427 TkpDestroyMenuButton(mbPtr);
428
429 if (mbPtr->flags & REDRAW_PENDING) {
430 Tcl_CancelIdleCall(TkpDisplayMenuButton, (ClientData) mbPtr);
431 }
432
433 /*
434 * Free up all the stuff that requires special handling, then
435 * let Tk_FreeOptions handle all the standard option-related
436 * stuff.
437 */
438
439 Tcl_DeleteCommandFromToken(mbPtr->interp, mbPtr->widgetCmd);
440 if (mbPtr->textVarName != NULL) {
441 Lang_UntraceVar(mbPtr->interp, mbPtr->textVarName,
442 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
443 MenuButtonTextVarProc, (ClientData) mbPtr);
444 }
445 if (mbPtr->image != NULL) {
446 Tk_FreeImage(mbPtr->image);
447 }
448 if (mbPtr->normalTextGC != None) {
449 Tk_FreeGC(mbPtr->display, mbPtr->normalTextGC);
450 }
451 if (mbPtr->activeTextGC != None) {
452 Tk_FreeGC(mbPtr->display, mbPtr->activeTextGC);
453 }
454 if (mbPtr->disabledGC != None) {
455 Tk_FreeGC(mbPtr->display, mbPtr->disabledGC);
456 }
457 if (mbPtr->stippleGC != None) {
458 Tk_FreeGC(mbPtr->display, mbPtr->stippleGC);
459 }
460 if (mbPtr->gray != None) {
461 Tk_FreeBitmap(mbPtr->display, mbPtr->gray);
462 }
463 if (mbPtr->textLayout != NULL) {
464 Tk_FreeTextLayout(mbPtr->textLayout);
465 }
466 Tk_FreeConfigOptions((char *) mbPtr, mbPtr->optionTable, mbPtr->tkwin);
467 mbPtr->tkwin = NULL;
468 Tcl_EventuallyFree((ClientData) mbPtr, TCL_DYNAMIC);
469 }
470
471 /*
472 *----------------------------------------------------------------------
473 *
474 * ConfigureMenuButton --
475 *
476 * This procedure is called to process an argv/argc list, plus
477 * the Tk option database, in order to configure (or
478 * reconfigure) a menubutton widget.
479 *
480 * Results:
481 * The return value is a standard Tcl result. If TCL_ERROR is
482 * returned, then the interp's result contains an error message.
483 *
484 * Side effects:
485 * Configuration information, such as text string, colors, font,
486 * etc. get set for mbPtr; old resources get freed, if there
487 * were any. The menubutton is redisplayed.
488 *
489 *----------------------------------------------------------------------
490 */
491
492 static int
493 ConfigureMenuButton(interp, mbPtr, objc, objv)
494 Tcl_Interp *interp; /* Used for error reporting. */
495 register TkMenuButton *mbPtr;
496 /* Information about widget; may or may
497 * not already have values for some
498 * fields. */
499 int objc; /* Number of valid entries in objv. */
500 Tcl_Obj *CONST objv[]; /* Arguments. */
501 {
502 Tk_SavedOptions savedOptions;
503 Tcl_Obj *errorResult = NULL;
504 int error;
505 Tk_Image image;
506
507 /*
508 * Eliminate any existing trace on variables monitored by the
509 * menubutton.
510 */
511
512 if (mbPtr->textVarName != NULL) {
513 Lang_UntraceVar(interp, mbPtr->textVarName,
514 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
515 MenuButtonTextVarProc, (ClientData) mbPtr);
516 }
517
518 /*
519 * The following loop is potentially executed twice. During the
520 * first pass configuration options get set to their new values.
521 * If there is an error in this pass, we execute a second pass
522 * to restore all the options to their previous values.
523 */
524
525 for (error = 0; error <= 1; error++) {
526 if (!error) {
527 /*
528 * First pass: set options to new values.
529 */
530
531 if (Tk_SetOptions(interp, (char *) mbPtr,
532 mbPtr->optionTable, objc, objv,
533 mbPtr->tkwin, &savedOptions, (int *) NULL) != TCL_OK) {
534 continue;
535 }
536 } else {
537 /*
538 * Second pass: restore options to old values.
539 */
540
541 errorResult = Tcl_GetObjResult(interp);
542 Tcl_IncrRefCount(errorResult);
543 Tk_RestoreSavedOptions(&savedOptions);
544 }
545
546 /*
547 * A few options need special processing, such as setting the
548 * background from a 3-D border, or filling in complicated
549 * defaults that couldn't be specified to Tk_SetOptions.
550 */
551
552 if ((mbPtr->state == STATE_ACTIVE)
553 && !Tk_StrictMotif(mbPtr->tkwin)) {
554 Tk_SetBackgroundFromBorder(mbPtr->tkwin, mbPtr->activeBorder);
555 } else {
556 Tk_SetBackgroundFromBorder(mbPtr->tkwin, mbPtr->normalBorder);
557 }
558
559 if (mbPtr->highlightWidth < 0) {
560 mbPtr->highlightWidth = 0;
561 }
562
563 if (mbPtr->padX < 0) {
564 mbPtr->padX = 0;
565 }
566 if (mbPtr->padY < 0) {
567 mbPtr->padY = 0;
568 }
569
570 /*
571 * Get the image for the widget, if there is one. Allocate the
572 * new image before freeing the old one, so that the reference
573 * count doesn't go to zero and cause image data to be discarded.
574 */
575
576 if (mbPtr->imageString != NULL) {
577 image = Tk_GetImage(mbPtr->interp, mbPtr->tkwin,
578 mbPtr->imageString, MenuButtonImageProc,
579 (ClientData) mbPtr);
580 if (image == NULL) {
581 return TCL_ERROR;
582 }
583 } else {
584 image = NULL;
585 }
586 if (mbPtr->image != NULL) {
587 Tk_FreeImage(mbPtr->image);
588 }
589 mbPtr->image = image;
590
591 /*
592 * Recompute the geometry for the button.
593 */
594
595 if ((mbPtr->bitmap != None) || (mbPtr->image != NULL)) {
596 if (Tk_GetPixelsFromObj(interp, mbPtr->tkwin, mbPtr->widthString,
597 &mbPtr->width) != TCL_OK) {
598 widthError:
599 Tcl_AddErrorInfo(interp, "\n (processing -width option)");
600 continue;
601 }
602 if (Tk_GetPixelsFromObj(interp, mbPtr->tkwin, mbPtr->heightString,
603 &mbPtr->height) != TCL_OK) {
604 heightError:
605 Tcl_AddErrorInfo(interp, "\n (processing -height option)");
606 continue;
607 }
608 } else {
609 if (Tcl_GetIntFromObj(interp, mbPtr->widthString, &mbPtr->width)
610 != TCL_OK) {
611 goto widthError;
612 }
613 if (Tcl_GetIntFromObj(interp, mbPtr->heightString, &mbPtr->height)
614 != TCL_OK) {
615 goto heightError;
616 }
617 }
618 break;
619 }
620
621 if (!error) {
622 Tk_FreeSavedOptions(&savedOptions);
623 }
624
625 if ((mbPtr->image == NULL) && (mbPtr->bitmap == None)
626 && (mbPtr->textVarName != NULL)) {
627
628 /*
629 * The menubutton displays the value of a variable.
630 * Set up a trace to watch for any changes in it, create
631 * the variable if it doesn't exist, and fetch its
632 * current value.
633 */
634
635 CONST char *value;
636
637 value = Tcl_GetString(Tcl_ObjGetVar2(interp, mbPtr->textVarName, NULL, TCL_GLOBAL_ONLY));
638 if (value == NULL) {
639 Tcl_Obj *temp = Tcl_NewStringObj(mbPtr->text,-1);
640 Tcl_ObjSetVar2(interp, mbPtr->textVarName, NULL, temp,
641 TCL_GLOBAL_ONLY);
642 Tcl_DecrRefCount(temp);
643 } else {
644 if (mbPtr->text != NULL) {
645 ckfree(mbPtr->text);
646 }
647 mbPtr->text = (char *) ckalloc((unsigned) (strlen(value) + 1));
648 strcpy(mbPtr->text, value);
649 }
650 Lang_TraceVar(interp, mbPtr->textVarName,
651 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
652 MenuButtonTextVarProc, (ClientData) mbPtr);
653 }
654
655 TkMenuButtonWorldChanged((ClientData) mbPtr);
656 if (error) {
657 Tcl_SetObjResult(interp, errorResult);
658 Tcl_DecrRefCount(errorResult);
659 return TCL_ERROR;
660 } else {
661 return TCL_OK;
662 }
663 }
664
665 /*
666 *---------------------------------------------------------------------------
667 *
668 * TkMenuButtonWorldChanged --
669 *
670 * This procedure is called when the world has changed in some
671 * way and the widget needs to recompute all its graphics contexts
672 * and determine its new geometry.
673 *
674 * Results:
675 * None.
676 *
677 * Side effects:
678 * TkMenuButton will be relayed out and redisplayed.
679 *
680 *---------------------------------------------------------------------------
681 */
682
683 void
684 TkMenuButtonWorldChanged(instanceData)
685 ClientData instanceData; /* Information about widget. */
686 {
687 XGCValues gcValues;
688 GC gc;
689 unsigned long mask;
690 TkMenuButton *mbPtr;
691
692 mbPtr = (TkMenuButton *) instanceData;
693
694 gcValues.font = Tk_FontId(mbPtr->tkfont);
695 gcValues.foreground = mbPtr->normalFg->pixel;
696 gcValues.background = Tk_3DBorderColor(mbPtr->normalBorder)->pixel;
697
698 /*
699 * Note: GraphicsExpose events are disabled in GC's because they're
700 * used to copy stuff from an off-screen pixmap onto the screen (we know
701 * that there's no problem with obscured areas).
702 */
703
704 gcValues.graphics_exposures = False;
705 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
706 gc = Tk_GetGC(mbPtr->tkwin, mask, &gcValues);
707 if (mbPtr->normalTextGC != None) {
708 Tk_FreeGC(mbPtr->display, mbPtr->normalTextGC);
709 }
710 mbPtr->normalTextGC = gc;
711
712 gcValues.foreground = mbPtr->activeFg->pixel;
713 gcValues.background = Tk_3DBorderColor(mbPtr->activeBorder)->pixel;
714 mask = GCForeground | GCBackground | GCFont;
715 gc = Tk_GetGC(mbPtr->tkwin, mask, &gcValues);
716 if (mbPtr->activeTextGC != None) {
717 Tk_FreeGC(mbPtr->display, mbPtr->activeTextGC);
718 }
719 mbPtr->activeTextGC = gc;
720
721 gcValues.background = Tk_3DBorderColor(mbPtr->normalBorder)->pixel;
722
723 /*
724 * Create the GC that can be used for stippling
725 */
726
727 if (mbPtr->stippleGC == None) {
728 gcValues.foreground = gcValues.background;
729 mask = GCForeground;
730 if (mbPtr->gray == None) {
731 mbPtr->gray = Tk_GetBitmap(NULL, mbPtr->tkwin, "gray50");
732 }
733 if (mbPtr->gray != None) {
734 gcValues.fill_style = FillStippled;
735 gcValues.stipple = mbPtr->gray;
736 mask |= GCFillStyle | GCStipple;
737 }
738 mbPtr->stippleGC = Tk_GetGC(mbPtr->tkwin, mask, &gcValues);
739 }
740
741 /*
742 * Allocate the disabled graphics context, for drawing text in
743 * its disabled state.
744 */
745
746 mask = GCForeground | GCBackground | GCFont;
747 if (mbPtr->disabledFg != NULL) {
748 gcValues.foreground = mbPtr->disabledFg->pixel;
749 } else {
750 gcValues.foreground = gcValues.background;
751 }
752 gc = Tk_GetGC(mbPtr->tkwin, mask, &gcValues);
753 if (mbPtr->disabledGC != None) {
754 Tk_FreeGC(mbPtr->display, mbPtr->disabledGC);
755 }
756 mbPtr->disabledGC = gc;
757
758 TkpComputeMenuButtonGeometry(mbPtr);
759
760 /*
761 * Lastly, arrange for the button to be redisplayed.
762 */
763
764 if (Tk_IsMapped(mbPtr->tkwin) && !(mbPtr->flags & REDRAW_PENDING)) {
765 Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
766 mbPtr->flags |= REDRAW_PENDING;
767 }
768 }
769
770 /*
771 *--------------------------------------------------------------
772 *
773 * MenuButtonEventProc --
774 *
775 * This procedure is invoked by the Tk dispatcher for various
776 * events on buttons.
777 *
778 * Results:
779 * None.
780 *
781 * Side effects:
782 * When the window gets deleted, internal structures get
783 * cleaned up. When it gets exposed, it is redisplayed.
784 *
785 *--------------------------------------------------------------
786 */
787
788 static void
789 MenuButtonEventProc(clientData, eventPtr)
790 ClientData clientData; /* Information about window. */
791 XEvent *eventPtr; /* Information about event. */
792 {
793 TkMenuButton *mbPtr = (TkMenuButton *) clientData;
794 if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
795 goto redraw;
796 } else if (eventPtr->type == ConfigureNotify) {
797 /*
798 * Must redraw after size changes, since layout could have changed
799 * and borders will need to be redrawn.
800 */
801
802 goto redraw;
803 } else if (eventPtr->type == DestroyNotify) {
804 DestroyMenuButton((char *) mbPtr);
805 } else if (eventPtr->type == FocusIn) {
806 if (eventPtr->xfocus.detail != NotifyInferior) {
807 mbPtr->flags |= GOT_FOCUS;
808 if (mbPtr->highlightWidth > 0) {
809 goto redraw;
810 }
811 }
812 } else if (eventPtr->type == FocusOut) {
813 if (eventPtr->xfocus.detail != NotifyInferior) {
814 mbPtr->flags &= ~GOT_FOCUS;
815 if (mbPtr->highlightWidth > 0) {
816 goto redraw;
817 }
818 }
819 }
820 return;
821
822 redraw:
823 if ((mbPtr->tkwin != NULL) && !(mbPtr->flags & REDRAW_PENDING)) {
824 Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
825 mbPtr->flags |= REDRAW_PENDING;
826 }
827 }
828
829 /*
830 *----------------------------------------------------------------------
831 *
832 * MenuButtonCmdDeletedProc --
833 *
834 * This procedure is invoked when a widget command is deleted. If
835 * the widget isn't already in the process of being destroyed,
836 * this command destroys it.
837 *
838 * Results:
839 * None.
840 *
841 * Side effects:
842 * The widget is destroyed.
843 *
844 *----------------------------------------------------------------------
845 */
846
847 static void
848 MenuButtonCmdDeletedProc(clientData)
849 ClientData clientData; /* Pointer to widget record for widget. */
850 {
851 TkMenuButton *mbPtr = (TkMenuButton *) clientData;
852 Tk_Window tkwin = mbPtr->tkwin;
853
854 /*
855 * This procedure could be invoked either because the window was
856 * destroyed and the command was then deleted (in which case tkwin
857 * is NULL) or because the command was deleted, and then this procedure
858 * destroys the widget.
859 */
860
861 if (tkwin != NULL) {
862 Tk_DestroyWindow(tkwin);
863 }
864 }
865
866 /*
867 *--------------------------------------------------------------
868 *
869 * MenuButtonTextVarProc --
870 *
871 * This procedure is invoked when someone changes the variable
872 * whose contents are to be displayed in a menu button.
873 *
874 * Results:
875 * NULL is always returned.
876 *
877 * Side effects:
878 * The text displayed in the menu button will change to match the
879 * variable.
880 *
881 *--------------------------------------------------------------
882 */
883
884 /* ARGSUSED */
885 static char *
886 MenuButtonTextVarProc(clientData, interp, name1, name2, flags)
887 ClientData clientData; /* Information about button. */
888 Tcl_Interp *interp; /* Interpreter containing variable. */
889 Var name1; /* Name of variable. */
890 CONST char *name2; /* Second part of variable name. */
891 int flags; /* Information about what happened. */
892 {
893 register TkMenuButton *mbPtr = (TkMenuButton *) clientData;
894 CONST char *value;
895
896 /*
897 * If the variable is unset, then immediately recreate it unless
898 * the whole interpreter is going away.
899 */
900
901 if (flags & TCL_TRACE_UNSETS) {
902 if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
903 Tcl_Obj *temp = Tcl_NewStringObj(mbPtr->text, -1);
904 Tcl_ObjSetVar2(interp, mbPtr->textVarName, NULL, temp,
905 TCL_GLOBAL_ONLY);
906 Tcl_DecrRefCount(temp);
907 Lang_TraceVar(interp, mbPtr->textVarName,
908 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
909 MenuButtonTextVarProc, clientData);
910 }
911 return (char *) NULL;
912 }
913
914 value = Tcl_GetString(Tcl_ObjGetVar2(interp, mbPtr->textVarName, NULL, TCL_GLOBAL_ONLY));
915 if (value == NULL) {
916 value = "";
917 }
918 if (mbPtr->text != NULL) {
919 ckfree(mbPtr->text);
920 }
921 mbPtr->text = (char *) ckalloc((unsigned) (strlen(value) + 1));
922 strcpy(mbPtr->text, value);
923 TkpComputeMenuButtonGeometry(mbPtr);
924
925 if ((mbPtr->tkwin != NULL) && Tk_IsMapped(mbPtr->tkwin)
926 && !(mbPtr->flags & REDRAW_PENDING)) {
927 Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
928 mbPtr->flags |= REDRAW_PENDING;
929 }
930 return (char *) NULL;
931 }
932
933 /*
934 *----------------------------------------------------------------------
935 *
936 * MenuButtonImageProc --
937 *
938 * This procedure is invoked by the image code whenever the manager
939 * for an image does something that affects the size of contents
940 * of an image displayed in a button.
941 *
942 * Results:
943 * None.
944 *
945 * Side effects:
946 * Arranges for the button to get redisplayed.
947 *
948 *----------------------------------------------------------------------
949 */
950
951 static void
952 MenuButtonImageProc(clientData, x, y, width, height, imgWidth, imgHeight)
953 ClientData clientData; /* Pointer to widget record. */
954 int x, y; /* Upper left pixel (within image)
955 * that must be redisplayed. */
956 int width, height; /* Dimensions of area to redisplay
957 * (may be <= 0). */
958 int imgWidth, imgHeight; /* New dimensions of image. */
959 {
960 register TkMenuButton *mbPtr = (TkMenuButton *) clientData;
961
962 if (mbPtr->tkwin != NULL) {
963 TkpComputeMenuButtonGeometry(mbPtr);
964 if (Tk_IsMapped(mbPtr->tkwin) && !(mbPtr->flags & REDRAW_PENDING)) {
965 Tcl_DoWhenIdle(TkpDisplayMenuButton, (ClientData) mbPtr);
966 mbPtr->flags |= REDRAW_PENDING;
967 }
968 }
969 }
970
971