Debian Codegraph @

pTk/mTk/generic/tkMenu.c

raw symbols SCIP
1 /*
2 * tkMenu.c --
3 *
4 * This file contains most of the code for implementing menus in Tk. It takes
5 * care of all of the generic (platform-independent) parts of menus, and
6 * is supplemented by platform-specific files. The geometry calculation
7 * and drawing code for menus is in the file tkMenuDraw.c
8 *
9 * Copyright (c) 1990-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1998 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: tkMenu.c,v 1.20.2.1 2003/07/15 13:59:06 vincentdarley Exp $
16 */
17
18 /*
19 * Notes on implementation of menus:
20 *
21 * Menus can be used in three ways:
22 * - as a popup menu, either as part of a menubutton or standalone.
23 * - as a menubar. The menu's cascade items are arranged according to
24 * the specific platform to provide the user access to the menus at all
25 * times
26 * - as a tearoff palette. This is a window with the menu's items in it.
27 *
28 * The goal is to provide the Tk developer with a way to use a common
29 * set of menus for all of these tasks.
30 *
31 * In order to make the bindings for cascade menus work properly under Unix,
32 * the cascade menus' pathnames must be proper children of the menu that
33 * they are cascade from. So if there is a menu .m, and it has two
34 * cascades labelled "File" and "Edit", the cascade menus might have
35 * the pathnames .m.file and .m.edit. Another constraint is that the menus
36 * used for menubars must be children of the toplevel widget that they
37 * are attached to. And on the Macintosh, the platform specific menu handle
38 * for cascades attached to a menu bar must have a title that matches the
39 * label for the cascade menu.
40 *
41 * To handle all of the constraints, Tk menubars and tearoff menus are
42 * implemented using menu clones. Menu clones are full menus in their own
43 * right; they have a Tk window and pathname associated with them; they have
44 * a TkMenu structure and array of entries. However, they are linked with the
45 * original menu that they were cloned from. The reflect the attributes of
46 * the original, or "master", menu. So if an item is added to a menu, and
47 * that menu has clones, then the item must be added to all of its clones
48 * also. Menus are cloned when a menu is torn-off or when a menu is assigned
49 * as a menubar using the "-menu" option of the toplevel's pathname configure
50 * subcommand. When a clone is destroyed, only the clone is destroyed, but
51 * when the master menu is destroyed, all clones are also destroyed. This
52 * allows the developer to just deal with one set of menus when creating
53 * and destroying.
54 *
55 * Clones are rather tricky when a menu with cascade entries is cloned (such
56 * as a menubar). Not only does the menu have to be cloned, but each cascade
57 * entry's corresponding menu must also be cloned. This maintains the pathname
58 * parent-child hierarchy necessary for menubars and toplevels to work.
59 * This leads to several special cases:
60 *
61 * 1. When a new menu is created, and it is pointed to by cascade entries in
62 * cloned menus, the new menu has to be cloned to parallel the cascade
63 * structure.
64 * 2. When a cascade item is added to a menu that has been cloned, and the
65 * menu that the cascade item points to exists, that menu has to be cloned.
66 * 3. When the menu that a cascade entry points to is changed, the old
67 * cloned cascade menu has to be discarded, and the new one has to be cloned.
68 *
69 */
70
71 #if 0
72
73 /*
74 * used only to test for old config code
75 */
76
77 #define __NO_OLD_CONFIG
78 #endif
79
80 #include "tkPort.h"
81 #include "tkMenu.h"
82
83 #define MENU_HASH_KEY "tkMenus"
84
85 typedef struct ThreadSpecificData {
86 int menusInitialized; /* Flag indicates whether thread-specific
87 * elements of the Windows Menu module
88 * have been initialized. */
89 } ThreadSpecificData;
90 static Tcl_ThreadDataKey dataKey;
91
92 /*
93 * The following flag indicates whether the process-wide state for
94 * the Menu module has been intialized. The Mutex protects access to
95 * that flag.
96 */
97
98 static int menusInitialized;
99 TCL_DECLARE_MUTEX(menuMutex)
100
101 /*
102 * Configuration specs for individual menu entries. If this changes, be sure
103 * to update code in TkpMenuInit that changes the font string entry.
104 */
105
106 char *tkMenuStateStrings[] = {"active", "normal", "disabled", (char *) NULL};
107
108 static CONST char *menuEntryTypeStrings[] = {
109 "cascade", "checkbutton", "command", "radiobutton", "separator",
110 (char *) NULL
111 };
112
113 /*
114 * The following table defines the legal values for the -compound option.
115 * It is used with the "enum compound" declaration in tkMenu.h
116 */
117
118 static char *compoundStrings[] = {
119 "bottom", "center", "left", "none", "right", "top", (char *) NULL
120 };
121
122 Tk_OptionSpec tkBasicMenuEntryConfigSpecs[] = {
123 {TK_OPTION_BORDER, "-activebackground", (char *) NULL, (char *) NULL,
124 DEF_MENU_ENTRY_ACTIVE_BG, Tk_Offset(TkMenuEntry, activeBorderPtr), -1,
125 TK_OPTION_NULL_OK},
126 {TK_OPTION_COLOR, "-activeforeground", (char *) NULL, (char *) NULL,
127 DEF_MENU_ENTRY_ACTIVE_FG,
128 Tk_Offset(TkMenuEntry, activeFgPtr), -1, TK_OPTION_NULL_OK},
129 {TK_OPTION_STRING, "-accelerator", (char *) NULL, (char *) NULL,
130 DEF_MENU_ENTRY_ACCELERATOR,
131 Tk_Offset(TkMenuEntry, accelPtr), -1, TK_OPTION_NULL_OK},
132 {TK_OPTION_BORDER, "-background", (char *) NULL, (char *) NULL,
133 DEF_MENU_ENTRY_BG,
134 Tk_Offset(TkMenuEntry, borderPtr), -1, TK_OPTION_NULL_OK},
135 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
136 (char *) NULL, 0, -1, 0, (ClientData) "-background"},
137 {TK_OPTION_BITMAP, "-bitmap", (char *) NULL, (char *) NULL,
138 DEF_MENU_ENTRY_BITMAP,
139 Tk_Offset(TkMenuEntry, bitmapPtr), -1, TK_OPTION_NULL_OK},
140 {TK_OPTION_BOOLEAN, "-columnbreak", (char *) NULL, (char *) NULL,
141 DEF_MENU_ENTRY_COLUMN_BREAK,
142 -1, Tk_Offset(TkMenuEntry, columnBreak)},
143 {TK_OPTION_CALLBACK, "-command", (char *) NULL, (char *) NULL,
144 DEF_MENU_ENTRY_COMMAND,
145 -1, Tk_Offset(TkMenuEntry, commandPtr), TK_OPTION_NULL_OK},
146 {TK_OPTION_STRING_TABLE, "-compound", "compound", "Compound",
147 DEF_MENU_ENTRY_COMPOUND, -1, Tk_Offset(TkMenuEntry, compound), 0,
148 (ClientData) compoundStrings, 0},
149 {TK_OPTION_FONT, "-font", (char *) NULL, (char *) NULL,
150 DEF_MENU_ENTRY_FONT,
151 Tk_Offset(TkMenuEntry, fontPtr), -1, TK_OPTION_NULL_OK},
152 {TK_OPTION_COLOR, "-foreground", (char *) NULL, (char *) NULL,
153 DEF_MENU_ENTRY_FG,
154 Tk_Offset(TkMenuEntry, fgPtr), -1, TK_OPTION_NULL_OK},
155 {TK_OPTION_BOOLEAN, "-hidemargin", (char *) NULL, (char *) NULL,
156 DEF_MENU_ENTRY_HIDE_MARGIN,
157 -1, Tk_Offset(TkMenuEntry, hideMargin)},
158 {TK_OPTION_STRING, "-image", (char *) NULL, (char *) NULL,
159 DEF_MENU_ENTRY_IMAGE,
160 Tk_Offset(TkMenuEntry, imagePtr), -1, TK_OPTION_NULL_OK},
161 {TK_OPTION_STRING, "-label", (char *) NULL, (char *) NULL,
162 DEF_MENU_ENTRY_LABEL,
163 Tk_Offset(TkMenuEntry, labelPtr), -1, 0},
164 {TK_OPTION_STRING_TABLE, "-state", (char *) NULL, (char *) NULL,
165 DEF_MENU_ENTRY_STATE,
166 -1, Tk_Offset(TkMenuEntry, state), 0,
167 (ClientData) tkMenuStateStrings},
168 {TK_OPTION_INT, "-underline", (char *) NULL, (char *) NULL,
169 DEF_MENU_ENTRY_UNDERLINE, -1, Tk_Offset(TkMenuEntry, underline)},
170 {TK_OPTION_END}
171 };
172
173 Tk_OptionSpec tkSeparatorEntryConfigSpecs[] = {
174 {TK_OPTION_BORDER, "-background", (char *) NULL, (char *) NULL,
175 DEF_MENU_ENTRY_BG,
176 Tk_Offset(TkMenuEntry, borderPtr), -1, TK_OPTION_NULL_OK},
177 {TK_OPTION_END}
178 };
179
180 Tk_OptionSpec tkCheckButtonEntryConfigSpecs[] = {
181 {TK_OPTION_BOOLEAN, "-indicatoron", (char *) NULL, (char *) NULL,
182 DEF_MENU_ENTRY_INDICATOR,
183 -1, Tk_Offset(TkMenuEntry, indicatorOn)},
184 {TK_OPTION_STRING, "-offvalue", (char *) NULL, (char *) NULL,
185 DEF_MENU_ENTRY_OFF_VALUE,
186 Tk_Offset(TkMenuEntry, offValuePtr), -1},
187 {TK_OPTION_STRING, "-onvalue", (char *) NULL, (char *) NULL,
188 DEF_MENU_ENTRY_ON_VALUE,
189 Tk_Offset(TkMenuEntry, onValuePtr), -1},
190 {TK_OPTION_COLOR, "-selectcolor", (char *) NULL, (char *) NULL,
191 DEF_MENU_ENTRY_SELECT,
192 Tk_Offset(TkMenuEntry, indicatorFgPtr), -1, TK_OPTION_NULL_OK},
193 {TK_OPTION_STRING, "-selectimage", (char *) NULL, (char *) NULL,
194 DEF_MENU_ENTRY_SELECT_IMAGE,
195 Tk_Offset(TkMenuEntry, selectImagePtr), -1, TK_OPTION_NULL_OK},
196 {TK_OPTION_SCALARVAR, "-variable", (char *) NULL, (char *) NULL,
197 DEF_MENU_ENTRY_CHECK_VARIABLE,
198 -1, Tk_Offset(TkMenuEntry, namePtr), TK_OPTION_NULL_OK},
199 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
200 (char *) NULL, 0, -1, 0, (ClientData) tkBasicMenuEntryConfigSpecs}
201 };
202
203 Tk_OptionSpec tkRadioButtonEntryConfigSpecs[] = {
204 {TK_OPTION_BOOLEAN, "-indicatoron", (char *) NULL, (char *) NULL,
205 DEF_MENU_ENTRY_INDICATOR,
206 -1, Tk_Offset(TkMenuEntry, indicatorOn)},
207 {TK_OPTION_COLOR, "-selectcolor", (char *) NULL, (char *) NULL,
208 DEF_MENU_ENTRY_SELECT,
209 Tk_Offset(TkMenuEntry, indicatorFgPtr), -1, TK_OPTION_NULL_OK},
210 {TK_OPTION_STRING, "-selectimage", (char *) NULL, (char *) NULL,
211 DEF_MENU_ENTRY_SELECT_IMAGE,
212 Tk_Offset(TkMenuEntry, selectImagePtr), -1, TK_OPTION_NULL_OK},
213 {TK_OPTION_STRING, "-value", (char *) NULL, (char *) NULL,
214 DEF_MENU_ENTRY_VALUE,
215 Tk_Offset(TkMenuEntry, onValuePtr), -1, TK_OPTION_NULL_OK},
216 {TK_OPTION_SCALARVAR, "-variable", (char *) NULL, (char *) NULL,
217 DEF_MENU_ENTRY_RADIO_VARIABLE,
218 -1, Tk_Offset(TkMenuEntry, namePtr), 0},
219 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
220 (char *) NULL, 0, -1, 0, (ClientData) tkBasicMenuEntryConfigSpecs}
221 };
222
223 Tk_OptionSpec tkCascadeEntryConfigSpecs[] = {
224 {TK_OPTION_STRING, "-menu", (char *) NULL, (char *) NULL,
225 DEF_MENU_ENTRY_MENU,
226 Tk_Offset(TkMenuEntry, namePtr), -1, TK_OPTION_NULL_OK},
227 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
228 (char *) NULL, 0, -1, 0, (ClientData) tkBasicMenuEntryConfigSpecs}
229 };
230
231 Tk_OptionSpec tkTearoffEntryConfigSpecs[] = {
232 {TK_OPTION_BORDER, "-background", (char *) NULL, (char *) NULL,
233 DEF_MENU_ENTRY_BG,
234 Tk_Offset(TkMenuEntry, borderPtr), -1, TK_OPTION_NULL_OK},
235 {TK_OPTION_STRING_TABLE, "-state", (char *) NULL, (char *) NULL,
236 DEF_MENU_ENTRY_STATE, -1, Tk_Offset(TkMenuEntry, state), 0,
237 (ClientData) tkMenuStateStrings},
238 {TK_OPTION_END}
239 };
240
241 static Tk_OptionSpec *specsArray[] = {
242 tkCascadeEntryConfigSpecs, tkCheckButtonEntryConfigSpecs,
243 tkBasicMenuEntryConfigSpecs, tkRadioButtonEntryConfigSpecs,
244 tkSeparatorEntryConfigSpecs, tkTearoffEntryConfigSpecs};
245
246 /*
247 * Menu type strings for use with Tcl_GetIndexFromObj.
248 */
249
250 static CONST char *menuTypeStrings[] = {"normal", "tearoff", "menubar",
251 (char *) NULL};
252
253 Tk_OptionSpec tkMenuConfigSpecs[] = {
254 {TK_OPTION_BORDER, "-activebackground", "activeBackground",
255 "Foreground", DEF_MENU_ACTIVE_BG_COLOR,
256 Tk_Offset(TkMenu, activeBorderPtr), -1, 0,
257 (ClientData) DEF_MENU_ACTIVE_BG_MONO},
258 {TK_OPTION_PIXELS, "-activeborderwidth", "activeBorderWidth",
259 "BorderWidth", DEF_MENU_ACTIVE_BORDER_WIDTH,
260 Tk_Offset(TkMenu, activeBorderWidthPtr), -1},
261 {TK_OPTION_COLOR, "-activeforeground", "activeForeground",
262 "Background", DEF_MENU_ACTIVE_FG_COLOR,
263 Tk_Offset(TkMenu, activeFgPtr), -1, 0,
264 (ClientData) DEF_MENU_ACTIVE_FG_MONO},
265 {TK_OPTION_BORDER, "-background", "background", "Background",
266 DEF_MENU_BG_COLOR, Tk_Offset(TkMenu, borderPtr), -1, 0,
267 (ClientData) DEF_MENU_BG_MONO},
268 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL,
269 (char *) NULL, 0, -1, 0, (ClientData) "-borderwidth"},
270 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
271 (char *) NULL, 0, -1, 0, (ClientData) "-background"},
272 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
273 DEF_MENU_BORDER_WIDTH,
274 Tk_Offset(TkMenu, borderWidthPtr), -1, 0},
275 {TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
276 DEF_MENU_CURSOR,
277 Tk_Offset(TkMenu, cursorPtr), -1, TK_OPTION_NULL_OK},
278 {TK_OPTION_COLOR, "-disabledforeground", "disabledForeground",
279 "DisabledForeground", DEF_MENU_DISABLED_FG_COLOR,
280 Tk_Offset(TkMenu, disabledFgPtr), -1, TK_OPTION_NULL_OK,
281 (ClientData) DEF_MENU_DISABLED_FG_MONO},
282 {TK_OPTION_SYNONYM, "-fg", (char *) NULL, (char *) NULL,
283 (char *) NULL, 0, -1, 0, (ClientData) "-foreground"},
284 {TK_OPTION_FONT, "-font", "font", "Font",
285 DEF_MENU_FONT, Tk_Offset(TkMenu, fontPtr), -1},
286 {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
287 DEF_MENU_FG, Tk_Offset(TkMenu, fgPtr), -1, 0},
288 {TK_OPTION_CALLBACK, "-postcommand", "postCommand", "Command",
289 DEF_MENU_POST_COMMAND,
290 -1, Tk_Offset(TkMenu, postCommandPtr), TK_OPTION_NULL_OK},
291 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
292 DEF_MENU_RELIEF, Tk_Offset(TkMenu, reliefPtr), -1},
293 {TK_OPTION_COLOR, "-selectcolor", "selectColor", "Background",
294 DEF_MENU_SELECT_COLOR, Tk_Offset(TkMenu, indicatorFgPtr), -1, 0,
295 (ClientData) DEF_MENU_SELECT_MONO},
296 {TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
297 DEF_MENU_TAKE_FOCUS,
298 Tk_Offset(TkMenu, takeFocusPtr), -1, TK_OPTION_NULL_OK},
299 {TK_OPTION_BOOLEAN, "-tearoff", "tearOff", "TearOff",
300 DEF_MENU_TEAROFF, -1, Tk_Offset(TkMenu, tearoff)},
301 {TK_OPTION_CALLBACK, "-tearoffcommand", "tearOffCommand",
302 "TearOffCommand", DEF_MENU_TEAROFF_CMD,
303 -1, Tk_Offset(TkMenu, tearoffCommandPtr), TK_OPTION_NULL_OK},
304 {TK_OPTION_STRING, "-title", "title", "Title",
305 DEF_MENU_TITLE, Tk_Offset(TkMenu, titlePtr), -1,
306 TK_OPTION_NULL_OK},
307 {TK_OPTION_STRING_TABLE, "-type", "type", "Type",
308 DEF_MENU_TYPE, Tk_Offset(TkMenu, menuTypePtr), -1, TK_OPTION_NULL_OK,
309 (ClientData) menuTypeStrings},
310 {TK_OPTION_END}
311 };
312
313 /*
314 * Command line options. Put here because MenuCmd has to look at them
315 * along with MenuWidgetObjCmd.
316 */
317
318 static CONST char *menuOptions[] = {
319 "activate", "add", "cget", "clone", "configure", "delete", "entrycget",
320 "entryconfigure", "index", "insert", "invoke", "post", "postcascade",
321 "type", "unpost", "yposition", (char *) NULL
322 };
323 enum options {
324 MENU_ACTIVATE, MENU_ADD, MENU_CGET, MENU_CLONE, MENU_CONFIGURE,
325 MENU_DELETE, MENU_ENTRYCGET, MENU_ENTRYCONFIGURE, MENU_INDEX,
326 MENU_INSERT, MENU_INVOKE, MENU_POST, MENU_POSTCASCADE, MENU_TYPE,
327 MENU_UNPOST, MENU_YPOSITION
328 };
329
330 /*
331 * Prototypes for static procedures in this file:
332 */
333
334 static int CloneMenu _ANSI_ARGS_((TkMenu *menuPtr,
335 Tcl_Obj *newMenuName, Tcl_Obj *newMenuTypeString));
336 static int ConfigureMenu _ANSI_ARGS_((Tcl_Interp *interp,
337 TkMenu *menuPtr, int objc, Tcl_Obj *CONST objv[]));
338 static int ConfigureMenuCloneEntries _ANSI_ARGS_((
339 Tcl_Interp *interp, TkMenu *menuPtr, int index,
340 int objc, Tcl_Obj *CONST objv[]));
341 static int ConfigureMenuEntry _ANSI_ARGS_((TkMenuEntry *mePtr,
342 int objc, Tcl_Obj *CONST objv[]));
343 static void DeleteMenuCloneEntries _ANSI_ARGS_((TkMenu *menuPtr,
344 int first, int last));
345 static void DestroyMenuHashTable _ANSI_ARGS_((
346 ClientData clientData, Tcl_Interp *interp));
347 static void DestroyMenuInstance _ANSI_ARGS_((TkMenu *menuPtr));
348 static void DestroyMenuEntry _ANSI_ARGS_((char *memPtr));
349 static int GetIndexFromCoords
350 _ANSI_ARGS_((Tcl_Interp *interp, TkMenu *menuPtr,
351 char *string, int *indexPtr));
352 static int MenuDoYPosition _ANSI_ARGS_((Tcl_Interp *interp,
353 TkMenu *menuPtr, Tcl_Obj *objPtr));
354 static int MenuAddOrInsert _ANSI_ARGS_((Tcl_Interp *interp,
355 TkMenu *menuPtr, Tcl_Obj *indexPtr, int objc,
356 Tcl_Obj *CONST objv[]));
357 static int MenuCmd _ANSI_ARGS_((ClientData clientData,
358 Tcl_Interp *interp, int objc,
359 Tcl_Obj *CONST objv[]));
360 static void MenuCmdDeletedProc _ANSI_ARGS_((
361 ClientData clientData));
362 static TkMenuEntry * MenuNewEntry _ANSI_ARGS_((TkMenu *menuPtr, int index,
363 int type));
364 static char * MenuVarProc _ANSI_ARGS_((ClientData clientData,
365 Tcl_Interp *interp, Var name1,
366 CONST char *name2, int flags));
367 static int MenuWidgetObjCmd _ANSI_ARGS_((ClientData clientData,
368 Tcl_Interp *interp, int objc,
369 Tcl_Obj *CONST objv[]));
370 static void MenuWorldChanged _ANSI_ARGS_((
371 ClientData instanceData));
372 static int PostProcessEntry _ANSI_ARGS_((TkMenuEntry *mePtr));
373 static void RecursivelyDeleteMenu _ANSI_ARGS_((TkMenu *menuPtr));
374 static void UnhookCascadeEntry _ANSI_ARGS_((TkMenuEntry *mePtr));
375 static void TkMenuCleanup _ANSI_ARGS_((ClientData unused));
376
377 /*
378 * The structure below is a list of procs that respond to certain window
379 * manager events. One of these includes a font change, which forces
380 * the geometry proc to be called.
381 */
382
383 static Tk_ClassProcs menuClass = {
384 sizeof(Tk_ClassProcs), /* size */
385 MenuWorldChanged /* worldChangedProc */
386 };
387
388 /*
389 *--------------------------------------------------------------
390 *
391 * TkCreateMenuCmd --
392 *
393 * Called by Tk at initialization time to create the menu
394 * command.
395 *
396 * Results:
397 * A standard Tcl result.
398 *
399 * Side effects:
400 * See the user documentation.
401 *
402 *--------------------------------------------------------------
403 */
404
405 int
406 TkCreateMenuCmd(interp)
407 Tcl_Interp *interp; /* Interpreter we are creating the
408 * command in. */
409 {
410 TkMenuOptionTables *optionTablesPtr =
411 (TkMenuOptionTables *) ckalloc(sizeof(TkMenuOptionTables));
412
413 optionTablesPtr->menuOptionTable =
414 Tk_CreateOptionTable(interp, tkMenuConfigSpecs);
415 optionTablesPtr->entryOptionTables[TEAROFF_ENTRY] =
416 Tk_CreateOptionTable(interp, specsArray[TEAROFF_ENTRY]);
417 optionTablesPtr->entryOptionTables[COMMAND_ENTRY] =
418 Tk_CreateOptionTable(interp, specsArray[COMMAND_ENTRY]);
419 optionTablesPtr->entryOptionTables[CASCADE_ENTRY] =
420 Tk_CreateOptionTable(interp, specsArray[CASCADE_ENTRY]);
421 optionTablesPtr->entryOptionTables[SEPARATOR_ENTRY] =
422 Tk_CreateOptionTable(interp, specsArray[SEPARATOR_ENTRY]);
423 optionTablesPtr->entryOptionTables[RADIO_BUTTON_ENTRY] =
424 Tk_CreateOptionTable(interp, specsArray[RADIO_BUTTON_ENTRY]);
425 optionTablesPtr->entryOptionTables[CHECK_BUTTON_ENTRY] =
426 Tk_CreateOptionTable(interp, specsArray[CHECK_BUTTON_ENTRY]);
427
428 Tcl_CreateObjCommand(interp, "menu", MenuCmd,
429 (ClientData) optionTablesPtr, NULL);
430
431 if (Tcl_IsSafe(interp)) {
432 Tcl_HideCommand(interp, "menu", "menu");
433 }
434
435 return TCL_OK;
436 }
437
438 /*
439 *--------------------------------------------------------------
440 *
441 * MenuCmd --
442 *
443 * This procedure is invoked to process the "menu" Tcl
444 * command. See the user documentation for details on
445 * what it does.
446 *
447 * Results:
448 * A standard Tcl result.
449 *
450 * Side effects:
451 * See the user documentation.
452 *
453 *--------------------------------------------------------------
454 */
455
456 #ifndef _LANG
457 static
458 #endif
459 int
460 MenuCmd(clientData, interp, objc, objv)
461 ClientData clientData; /* Main window associated with
462 * interpreter. */
463 Tcl_Interp *interp; /* Current interpreter. */
464 int objc; /* Number of arguments. */
465 Tcl_Obj *CONST objv[]; /* Argument strings. */
466 {
467 Tk_Window tkwin = Tk_MainWindow(interp);
468 Tk_Window new;
469 register TkMenu *menuPtr = NULL;
470 TkMenuReferences *menuRefPtr;
471 int i, index;
472 int toplevel;
473 char *windowName;
474 static CONST char *typeStringList[] = {"-type", (char *) NULL};
475 TkMenuOptionTables *optionTablesPtr = (TkMenuOptionTables *) clientData;
476 Tcl_Obj *menuObj;
477
478 if (objc < 2) {
479 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
480 return TCL_ERROR;
481 }
482
483 TkMenuInit();
484
485 toplevel = 1;
486 for (i = 2; i < (objc - 1); i++) {
487 if (Tcl_GetIndexFromObj(NULL, objv[i], typeStringList, NULL, 0, &index)
488 != TCL_ERROR) {
489 if ((Tcl_GetIndexFromObj(NULL, objv[i + 1], menuTypeStrings, NULL,
490 0, &index) == TCL_OK) && (index == MENUBAR)) {
491 toplevel = 0;
492 }
493 break;
494 }
495 }
496
497 windowName = Tcl_GetStringFromObj(objv[1], NULL);
498 new = Tk_CreateWindowFromPath(interp, tkwin, windowName, toplevel ? ""
499 : NULL);
500 if (new == NULL) {
501 return TCL_ERROR;
502 }
503
504 /*
505 * Initialize the data structure for the menu. Note that the
506 * menuPtr is eventually freed in 'TkMenuEventProc' in tkMenuDraw.c,
507 * when Tcl_EventuallyFree is called.
508 */
509
510 menuPtr = (TkMenu *) ckalloc(sizeof(TkMenu));
511 menuPtr->tkwin = new;
512 menuPtr->display = Tk_Display(new);
513 menuPtr->interp = interp;
514 menuPtr->widgetCmd = Tcl_CreateObjCommand(interp,
515 Tk_PathName(menuPtr->tkwin), MenuWidgetObjCmd,
516 (ClientData) menuPtr, MenuCmdDeletedProc);
517 menuPtr->entries = NULL;
518 menuPtr->numEntries = 0;
519 menuPtr->active = -1;
520 menuPtr->borderPtr = NULL;
521 menuPtr->borderWidthPtr = NULL;
522 menuPtr->reliefPtr = NULL;
523 menuPtr->activeBorderPtr = NULL;
524 menuPtr->activeBorderWidthPtr = NULL;
525 menuPtr->fontPtr = NULL;
526 menuPtr->fgPtr = NULL;
527 menuPtr->disabledFgPtr = NULL;
528 menuPtr->activeFgPtr = NULL;
529 menuPtr->indicatorFgPtr = NULL;
530 menuPtr->tearoff = 0;
531 menuPtr->tearoffCommandPtr = NULL;
532 menuPtr->cursorPtr = None;
533 menuPtr->takeFocusPtr = NULL;
534 menuPtr->postCommandPtr = NULL;
535 menuPtr->postCommandGeneration = 0;
536 menuPtr->postedCascade = NULL;
537 menuPtr->nextInstancePtr = NULL;
538 menuPtr->masterMenuPtr = menuPtr;
539 menuPtr->menuType = UNKNOWN_TYPE;
540 menuPtr->menuFlags = 0;
541 menuPtr->parentTopLevelPtr = NULL;
542 menuPtr->menuTypePtr = NULL;
543 menuPtr->titlePtr = NULL;
544 menuPtr->errorStructPtr = NULL;
545 menuPtr->optionTablesPtr = optionTablesPtr;
546 TkMenuInitializeDrawingFields(menuPtr);
547
548 Tk_SetClass(menuPtr->tkwin, "Menu");
549 Tk_SetClassProcs(menuPtr->tkwin, &menuClass, (ClientData) menuPtr);
550 if (Tk_InitOptions(interp, (char *) menuPtr,
551 menuPtr->optionTablesPtr->menuOptionTable, menuPtr->tkwin)
552 != TCL_OK) {
553 Tk_DestroyWindow(menuPtr->tkwin);
554 ckfree((char *) menuPtr);
555 return TCL_ERROR;
556 }
557
558
559 menuRefPtr = TkCreateMenuReferences(menuPtr->interp,
560 Tk_PathName(menuPtr->tkwin));
561 menuRefPtr->menuPtr = menuPtr;
562 menuPtr->menuRefPtr = menuRefPtr;
563 if (TCL_OK != TkpNewMenu(menuPtr)) {
564 Tk_DestroyWindow(menuPtr->tkwin);
565 ckfree((char *) menuPtr);
566 return TCL_ERROR;
567 }
568
569 Tk_CreateEventHandler(new, ExposureMask|StructureNotifyMask|ActivateMask,
570 TkMenuEventProc, (ClientData) menuPtr);
571 if (ConfigureMenu(interp, menuPtr, objc - 2, objv + 2) != TCL_OK) {
572 Tk_DestroyWindow(menuPtr->tkwin);
573 return TCL_ERROR;
574 }
575
576 /*
577 * If a menu has a parent menu pointing to it as a cascade entry, the
578 * parent menu needs to be told that this menu now exists so that
579 * the platform-part of the menu is correctly updated.
580 *
581 * If a menu has an instance and has cascade entries, then each cascade
582 * menu must also have a parallel instance. This is especially true on
583 * the Mac, where each menu has to have a separate title everytime it is in
584 * a menubar. For instance, say you have a menu .m1 with a cascade entry
585 * for .m2, where .m2 does not exist yet. You then put .m1 into a menubar.
586 * This creates a menubar instance for .m1, but since .m2 is not there,
587 * nothing else happens. When we go to create .m2, we hook it up properly
588 * with .m1. However, we now need to clone .m2 and assign the clone of .m2
589 * to be the cascade entry for the clone of .m1. This is special case
590 * #1 listed in the introductory comment.
591 */
592
593 if (menuRefPtr->parentEntryPtr != NULL) {
594 TkMenuEntry *cascadeListPtr = menuRefPtr->parentEntryPtr;
595 TkMenuEntry *nextCascadePtr;
596 Tcl_Obj *newMenuName;
597 Tcl_Obj *newObjv[2];
598
599 while (cascadeListPtr != NULL) {
600
601 nextCascadePtr = cascadeListPtr->nextCascadePtr;
602
603 /*
604 * If we have a new master menu, and an existing cloned menu
605 * points to this menu in a cascade entry, we have to clone
606 * the new menu and point the entry to the clone instead
607 * of the menu we are creating. Otherwise, ConfigureMenuEntry
608 * will hook up the platform-specific cascade linkages now
609 * that the menu we are creating exists.
610 */
611
612 if ((menuPtr->masterMenuPtr != menuPtr)
613 || ((menuPtr->masterMenuPtr == menuPtr)
614 && ((cascadeListPtr->menuPtr->masterMenuPtr
615 == cascadeListPtr->menuPtr)))) {
616 newObjv[0] = Tcl_NewStringObj("-menu", -1);
617 newObjv[1] = Tcl_NewStringObj(Tk_PathName(menuPtr->tkwin), -1);
618 Tcl_IncrRefCount(newObjv[0]);
619 Tcl_IncrRefCount(newObjv[1]);
620 ConfigureMenuEntry(cascadeListPtr, 2, newObjv);
621 Tcl_DecrRefCount(newObjv[0]);
622 Tcl_DecrRefCount(newObjv[1]);
623 } else {
624 Tcl_Obj *normalPtr = Tcl_NewStringObj("normal", -1);
625 Tcl_Obj *windowNamePtr = Tcl_NewStringObj(
626 Tk_PathName(cascadeListPtr->menuPtr->tkwin), -1);
627
628 Tcl_IncrRefCount(normalPtr);
629 Tcl_IncrRefCount(windowNamePtr);
630 newMenuName = TkNewMenuName(menuPtr->interp,
631 windowNamePtr, menuPtr);
632 Tcl_IncrRefCount(newMenuName);
633 CloneMenu(menuPtr, newMenuName, normalPtr);
634
635 /*
636 * Now we can set the new menu instance to be the cascade entry
637 * of the parent's instance.
638 */
639
640 newObjv[0] = Tcl_NewStringObj("-menu", -1);
641 newObjv[1] = newMenuName;
642 Tcl_IncrRefCount(newObjv[0]);
643 ConfigureMenuEntry(cascadeListPtr, 2, newObjv);
644 Tcl_DecrRefCount(normalPtr);
645 Tcl_DecrRefCount(newObjv[0]);
646 Tcl_DecrRefCount(newObjv[1]);
647 Tcl_DecrRefCount(windowNamePtr);
648 }
649 cascadeListPtr = nextCascadePtr;
650 }
651 }
652
653 /*
654 * If there already exist toplevel widgets that refer to this menu,
655 * find them and notify them so that they can reconfigure their
656 * geometry to reflect the menu.
657 */
658
659 menuObj = LangWidgetObj(menuPtr->interp,menuPtr->tkwin);
660 if (menuRefPtr->topLevelListPtr != NULL) {
661 TkMenuTopLevelList *topLevelListPtr = menuRefPtr->topLevelListPtr;
662 TkMenuTopLevelList *nextPtr;
663 Tk_Window listtkwin;
664 while (topLevelListPtr != NULL) {
665
666 /*
667 * Need to get the next pointer first. TkSetWindowMenuBar
668 * changes the list, so that the next pointer is different
669 * after calling it.
670 */
671
672 nextPtr = topLevelListPtr->nextPtr;
673 listtkwin = topLevelListPtr->tkwin;
674
675 TkSetWindowMenuBar(menuPtr->interp, listtkwin,
676 menuObj, menuObj);
677 topLevelListPtr = nextPtr;
678 }
679 }
680 Tcl_SetObjResult(interp,menuObj);
681 return TCL_OK;
682 }
683
684 /*
685 *--------------------------------------------------------------
686 *
687 * MenuWidgetObjCmd --
688 *
689 * This procedure is invoked to process the Tcl command
690 * that corresponds to a widget managed by this module.
691 * See the user documentation for details on what it does.
692 *
693 * Results:
694 * A standard Tcl result.
695 *
696 * Side effects:
697 * See the user documentation.
698 *
699 *--------------------------------------------------------------
700 */
701
702 static int
703 MenuWidgetObjCmd(clientData, interp, objc, objv)
704 ClientData clientData; /* Information about menu widget. */
705 Tcl_Interp *interp; /* Current interpreter. */
706 int objc; /* Number of arguments. */
707 Tcl_Obj *CONST objv[]; /* Argument strings. */
708 {
709 register TkMenu *menuPtr = (TkMenu *) clientData;
710 register TkMenuEntry *mePtr;
711 int result = TCL_OK;
712 int option;
713
714 if (objc < 2) {
715 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
716 return TCL_ERROR;
717 }
718 if (Tcl_GetIndexFromObj(interp, objv[1], menuOptions, "option", 0,
719 &option) != TCL_OK) {
720 return TCL_ERROR;
721 }
722 Tcl_Preserve((ClientData) menuPtr);
723
724 switch ((enum options) option) {
725 case MENU_ACTIVATE: {
726 int index;
727
728 if (objc != 3) {
729 Tcl_WrongNumArgs(interp, 1, objv, "activate index");
730 goto error;
731 }
732 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &index)
733 != TCL_OK) {
734 goto error;
735 }
736 if (menuPtr->active == index) {
737 goto done;
738 }
739 if ((index >= 0)
740 && ((menuPtr->entries[index]->type == SEPARATOR_ENTRY)
741 || (menuPtr->entries[index]->state
742 == ENTRY_DISABLED))) {
743 index = -1;
744 }
745 result = TkActivateMenuEntry(menuPtr, index);
746 break;
747 }
748 case MENU_ADD:
749 if (objc < 3) {
750 Tcl_WrongNumArgs(interp, 1, objv, "add type ?options?");
751 goto error;
752 }
753
754 if (MenuAddOrInsert(interp, menuPtr, (Tcl_Obj *) NULL,
755 objc - 2, objv + 2) != TCL_OK) {
756 goto error;
757 }
758 break;
759 case MENU_CGET: {
760 Tcl_Obj *resultPtr;
761
762 if (objc != 3) {
763 Tcl_WrongNumArgs(interp, 1, objv, "cget option");
764 goto error;
765 }
766 resultPtr = Tk_GetOptionValue(interp, (char *) menuPtr,
767 menuPtr->optionTablesPtr->menuOptionTable, objv[2],
768 menuPtr->tkwin);
769 if (resultPtr == NULL) {
770 goto error;
771 }
772 Tcl_SetObjResult(interp, resultPtr);
773 break;
774 }
775 case MENU_CLONE:
776 if ((objc < 3) || (objc > 4)) {
777 Tcl_WrongNumArgs(interp, 1, objv,
778 "clone newMenuName ?menuType?");
779 goto error;
780 }
781 result = CloneMenu(menuPtr, objv[2], (objc == 3) ? NULL : objv[3]);
782 if (result == TCL_OK) {
783 Tcl_SetObjResult(interp, objv[2]);
784 }
785 break;
786 case MENU_CONFIGURE: {
787 Tcl_Obj *resultPtr;
788
789 if (objc == 2) {
790 resultPtr = Tk_GetOptionInfo(interp, (char *) menuPtr,
791 menuPtr->optionTablesPtr->menuOptionTable,
792 (Tcl_Obj *) NULL, menuPtr->tkwin);
793 if (resultPtr == NULL) {
794 result = TCL_ERROR;
795 } else {
796 result = TCL_OK;
797 Tcl_SetObjResult(interp, resultPtr);
798 }
799 } else if (objc == 3) {
800 resultPtr = Tk_GetOptionInfo(interp, (char *) menuPtr,
801 menuPtr->optionTablesPtr->menuOptionTable,
802 objv[2], menuPtr->tkwin);
803 if (resultPtr == NULL) {
804 result = TCL_ERROR;
805 } else {
806 result = TCL_OK;
807 Tcl_SetObjResult(interp, resultPtr);
808 }
809 } else {
810 result = ConfigureMenu(interp, menuPtr, objc - 2, objv + 2);
811 }
812 if (result != TCL_OK) {
813 goto error;
814 }
815 break;
816 }
817 case MENU_DELETE: {
818 int first, last;
819
820 if ((objc != 3) && (objc != 4)) {
821 Tcl_WrongNumArgs(interp, 1, objv, "delete first ?last?");
822 goto error;
823 }
824 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &first)
825 != TCL_OK) {
826 goto error;
827 }
828 if (objc == 3) {
829 last = first;
830 } else {
831 if (TkGetMenuIndex(interp, menuPtr, objv[3], 0, &last)
832 != TCL_OK) {
833 goto error;
834 }
835 }
836 if (menuPtr->tearoff && (first == 0)) {
837
838 /*
839 * Sorry, can't delete the tearoff entry; must reconfigure
840 * the menu.
841 */
842
843 first = 1;
844 }
845 if ((first < 0) || (last < first)) {
846 goto done;
847 }
848 DeleteMenuCloneEntries(menuPtr, first, last);
849 break;
850 }
851 case MENU_ENTRYCGET: {
852 int index;
853 Tcl_Obj *resultPtr;
854
855 if (objc != 4) {
856 Tcl_WrongNumArgs(interp, 1, objv, "entrycget index option");
857 goto error;
858 }
859 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &index)
860 != TCL_OK) {
861 goto error;
862 }
863 if (index < 0) {
864 goto done;
865 }
866 mePtr = menuPtr->entries[index];
867 Tcl_Preserve((ClientData) mePtr);
868 resultPtr = Tk_GetOptionValue(interp, (char *) mePtr,
869 mePtr->optionTable, objv[3], menuPtr->tkwin);
870 Tcl_Release((ClientData) mePtr);
871 if (resultPtr == NULL) {
872 goto error;
873 }
874 Tcl_SetObjResult(interp, resultPtr);
875 break;
876 }
877 case MENU_ENTRYCONFIGURE: {
878 int index;
879 Tcl_Obj *resultPtr;
880
881 if (objc < 3) {
882 Tcl_WrongNumArgs(interp, 1, objv,
883 "entryconfigure index ?option value ...?");
884 goto error;
885 }
886 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &index)
887 != TCL_OK) {
888 goto error;
889 }
890 if (index < 0) {
891 goto done;
892 }
893 mePtr = menuPtr->entries[index];
894 Tcl_Preserve((ClientData) mePtr);
895 if (objc == 3) {
896 resultPtr = Tk_GetOptionInfo(interp, (char *) mePtr,
897 mePtr->optionTable, (Tcl_Obj *) NULL, menuPtr->tkwin);
898 if (resultPtr == NULL) {
899 result = TCL_ERROR;
900 } else {
901 result = TCL_OK;
902 Tcl_SetObjResult(interp, resultPtr);
903 }
904 } else if (objc == 4) {
905 resultPtr = Tk_GetOptionInfo(interp, (char *) mePtr,
906 mePtr->optionTable, objv[3], menuPtr->tkwin);
907 if (resultPtr == NULL) {
908 result = TCL_ERROR;
909 } else {
910 result = TCL_OK;
911 Tcl_SetObjResult(interp, resultPtr);
912 }
913 } else {
914 result = ConfigureMenuCloneEntries(interp, menuPtr, index,
915 objc - 3, objv + 3);
916 }
917 Tcl_Release((ClientData) mePtr);
918 break;
919 }
920 case MENU_INDEX: {
921 int index;
922
923 if (objc != 3) {
924 Tcl_WrongNumArgs(interp, 1, objv, "index string");
925 goto error;
926 }
927 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &index)
928 != TCL_OK) {
929 goto error;
930 }
931 if (index < 0) {
932 Tcl_SetResult(interp, "none", TCL_STATIC);
933 } else {
934 Tcl_SetIntObj(Tcl_GetObjResult(interp), index);
935 }
936 break;
937 }
938 case MENU_INSERT:
939 if (objc < 4) {
940 Tcl_WrongNumArgs(interp, 1, objv,
941 "insert index type ?options?");
942 goto error;
943 }
944 if (MenuAddOrInsert(interp, menuPtr, objv[2], objc - 3,
945 objv + 3) != TCL_OK) {
946 goto error;
947 }
948 break;
949 case MENU_INVOKE: {
950 int index;
951
952 if (objc != 3) {
953 Tcl_WrongNumArgs(interp, 1, objv, "invoke index");
954 goto error;
955 }
956 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &index)
957 != TCL_OK) {
958 goto error;
959 }
960 if (index < 0) {
961 goto done;
962 }
963 result = TkInvokeMenu(interp, menuPtr, index);
964 break;
965 }
966 case MENU_POST: {
967 int x, y;
968
969 if (objc != 4) {
970 Tcl_WrongNumArgs(interp, 1, objv, "post x y");
971 goto error;
972 }
973 if ((Tcl_GetIntFromObj(interp, objv[2], &x) != TCL_OK)
974 || (Tcl_GetIntFromObj(interp, objv[3], &y) != TCL_OK)) {
975 goto error;
976 }
977
978 /*
979 * Tearoff menus are posted differently on Mac and Windows than
980 * non-tearoffs. TkpPostMenu does not actually map the menu's
981 * window on those platforms, and popup menus have to be
982 * handled specially.
983 */
984
985 if (menuPtr->menuType != TEAROFF_MENU) {
986 result = TkpPostMenu(interp, menuPtr, x, y);
987 } else {
988 result = TkPostTearoffMenu(interp, menuPtr, x, y);
989 }
990 break;
991 }
992 case MENU_POSTCASCADE: {
993 int index;
994
995 if (objc != 3) {
996 Tcl_WrongNumArgs(interp, 1, objv, "postcascade index");
997 goto error;
998 }
999
1000 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &index)
1001 != TCL_OK) {
1002 goto error;
1003 }
1004 if ((index < 0) || (menuPtr->entries[index]->type
1005 != CASCADE_ENTRY)) {
1006 result = TkPostSubmenu(interp, menuPtr, (TkMenuEntry *) NULL);
1007 } else {
1008 result = TkPostSubmenu(interp, menuPtr,
1009 menuPtr->entries[index]);
1010 }
1011 break;
1012 }
1013 case MENU_TYPE: {
1014 int index;
1015
1016 if (objc != 3) {
1017 Tcl_WrongNumArgs(interp, 1, objv, "type index");
1018 goto error;
1019 }
1020 if (TkGetMenuIndex(interp, menuPtr, objv[2], 0, &index)
1021 != TCL_OK) {
1022 goto error;
1023 }
1024 if (index < 0) {
1025 goto done;
1026 }
1027 if (menuPtr->entries[index]->type == TEAROFF_ENTRY) {
1028 Tcl_SetResult(interp, "tearoff", TCL_STATIC);
1029 } else {
1030 Tcl_SetStringObj(Tcl_GetObjResult(interp),
1031 menuEntryTypeStrings[menuPtr->entries[index]->type],
1032 -1);
1033 }
1034 break;
1035 }
1036 case MENU_UNPOST:
1037 if (objc != 2) {
1038 Tcl_WrongNumArgs(interp, 1, objv, "unpost");
1039 goto error;
1040 }
1041 Tk_UnmapWindow(menuPtr->tkwin);
1042 result = TkPostSubmenu(interp, menuPtr, (TkMenuEntry *) NULL);
1043 break;
1044 case MENU_YPOSITION:
1045 if (objc != 3) {
1046 Tcl_WrongNumArgs(interp, 1, objv, "yposition index");
1047 goto error;
1048 }
1049 result = MenuDoYPosition(interp, menuPtr, objv[2]);
1050 break;
1051 }
1052 done:
1053 Tcl_Release((ClientData) menuPtr);
1054 return result;
1055
1056 error:
1057 Tcl_Release((ClientData) menuPtr);
1058 return TCL_ERROR;
1059 }
1060
1061 /*
1062 *----------------------------------------------------------------------
1063 *
1064 * TkInvokeMenu --
1065 *
1066 * Given a menu and an index, takes the appropriate action for the
1067 * entry associated with that index.
1068 *
1069 * Results:
1070 * Standard Tcl result.
1071 *
1072 * Side effects:
1073 * Commands may get excecuted; variables may get set; sub-menus may
1074 * get posted.
1075 *
1076 *----------------------------------------------------------------------
1077 */
1078
1079 int
1080 TkInvokeMenu(interp, menuPtr, index)
1081 Tcl_Interp *interp; /* The interp that the menu lives in. */
1082 TkMenu *menuPtr; /* The menu we are invoking. */
1083 int index; /* The zero based index of the item we
1084 * are invoking */
1085 {
1086 int result = TCL_OK;
1087 TkMenuEntry *mePtr;
1088
1089 if (index < 0) {
1090 goto done;
1091 }
1092 mePtr = menuPtr->entries[index];
1093 if (mePtr->state == ENTRY_DISABLED) {
1094 goto done;
1095 }
1096 Tcl_Preserve((ClientData) mePtr);
1097 if (mePtr->type == TEAROFF_ENTRY) {
1098 #if 0
1099 Tcl_DString ds;
1100 Tcl_DStringInit(&ds);
1101 Tcl_DStringAppend(&ds, "tk::TearOffMenu ", -1);
1102 Tcl_DStringAppend(&ds, Tk_PathName(menuPtr->tkwin), -1);
1103 result = Tcl_Eval(interp, Tcl_DStringValue(&ds));
1104 Tcl_DStringFree(&ds);
1105 #else
1106 Tcl_Obj *obj = LangWidgetObj(interp,menuPtr->tkwin);
1107 result = LangMethodCall(interp, obj, "tearOffMenu", 0, 0);
1108 Tcl_DecrRefCount(obj);
1109 #endif
1110 } else if ((mePtr->type == CHECK_BUTTON_ENTRY)
1111 && (mePtr->namePtr != NULL)) {
1112 Tcl_Obj *valuePtr;
1113
1114 if (mePtr->entryFlags & ENTRY_SELECTED) {
1115 valuePtr = mePtr->offValuePtr;
1116 } else {
1117 valuePtr = mePtr->onValuePtr;
1118 }
1119 if (valuePtr == NULL) {
1120 valuePtr = Tcl_NewObj();
1121 }
1122 Tcl_IncrRefCount(valuePtr);
1123 if (Tcl_ObjSetVar2(interp, mePtr->namePtr, NULL, valuePtr,
1124 TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG) == NULL) {
1125 result = TCL_ERROR;
1126 }
1127 Tcl_DecrRefCount(valuePtr);
1128 } else if ((mePtr->type == RADIO_BUTTON_ENTRY)
1129 && (mePtr->namePtr != NULL)) {
1130 Tcl_Obj *valuePtr = mePtr->onValuePtr;
1131
1132 if (valuePtr == NULL) {
1133 valuePtr = Tcl_NewObj();
1134 }
1135 Tcl_IncrRefCount(valuePtr);
1136 if (Tcl_ObjSetVar2(interp, mePtr->namePtr, NULL, valuePtr,
1137 TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG) == NULL) {
1138 result = TCL_ERROR;
1139 }
1140 Tcl_DecrRefCount(valuePtr);
1141 }
1142 /*
1143 * We check numEntries in addition to whether the menu entry
1144 * has a command because that goes to zero if the menu gets
1145 * deleted (e.g., during command evaluation).
1146 */
1147 if ((menuPtr->numEntries != 0) && (result == TCL_OK)
1148 && (mePtr->commandPtr != NULL)) {
1149 Tcl_Obj *commandPtr = mePtr->commandPtr;
1150
1151 Tcl_IncrRefCount(commandPtr);
1152 result = Tcl_EvalObjEx(interp, commandPtr, TCL_EVAL_GLOBAL);
1153 Tcl_DecrRefCount(commandPtr);
1154 }
1155 Tcl_Release((ClientData) mePtr);
1156 done:
1157 return result;
1158 }
1159
1160 /*
1161 *----------------------------------------------------------------------
1162 *
1163 * DestroyMenuInstance --
1164 *
1165 * This procedure is invoked by TkDestroyMenu
1166 * to clean up the internal structure of a menu at a safe time
1167 * (when no-one is using it anymore). Only takes care of one instance
1168 * of the menu.
1169 *
1170 * Results:
1171 * None.
1172 *
1173 * Side effects:
1174 * Everything associated with the menu is freed up.
1175 *
1176 *----------------------------------------------------------------------
1177 */
1178
1179 static void
1180 DestroyMenuInstance(menuPtr)
1181 TkMenu *menuPtr; /* Info about menu widget. */
1182 {
1183 int i;
1184 TkMenu *menuInstancePtr;
1185 TkMenuEntry *cascadePtr, *nextCascadePtr;
1186 Tcl_Obj *newObjv[2];
1187 TkMenu *parentMasterMenuPtr;
1188 TkMenuEntry *parentMasterEntryPtr;
1189
1190 /*
1191 * If the menu has any cascade menu entries pointing to it, the cascade
1192 * entries need to be told that the menu is going away. We need to clear
1193 * the menu ptr field in the menu reference at this point in the code
1194 * so that everything else can forget about this menu properly. We also
1195 * need to reset -menu field of all entries that are not master menus
1196 * back to this entry name if this is a master menu pointed to by another
1197 * master menu. If there is a clone menu that points to this menu,
1198 * then this menu is itself a clone, so when this menu goes away,
1199 * the -menu field of the pointing entry must be set back to this
1200 * menu's master menu name so that later if another menu is created
1201 * the cascade hierarchy can be maintained.
1202 */
1203
1204 TkpDestroyMenu(menuPtr);
1205 cascadePtr = menuPtr->menuRefPtr->parentEntryPtr;
1206 menuPtr->menuRefPtr->menuPtr = NULL;
1207 if (TkFreeMenuReferences(menuPtr->menuRefPtr)) {
1208 menuPtr->menuRefPtr = NULL;
1209 }
1210
1211 for (; cascadePtr != NULL; cascadePtr = nextCascadePtr) {
1212 nextCascadePtr = cascadePtr->nextCascadePtr;
1213
1214 if (menuPtr->masterMenuPtr != menuPtr) {
1215 Tcl_Obj *menuNamePtr = Tcl_NewStringObj("-menu", -1);
1216
1217 parentMasterMenuPtr = cascadePtr->menuPtr->masterMenuPtr;
1218 parentMasterEntryPtr =
1219 parentMasterMenuPtr->entries[cascadePtr->index];
1220 newObjv[0] = menuNamePtr;
1221 newObjv[1] = parentMasterEntryPtr->namePtr;
1222 /*
1223 * It is possible that the menu info is out of sync, and
1224 * these things point to NULL, so verify existence [Bug: 3402]
1225 */
1226 if (newObjv[0] && newObjv[1]) {
1227 Tcl_IncrRefCount(newObjv[0]);
1228 Tcl_IncrRefCount(newObjv[1]);
1229 ConfigureMenuEntry(cascadePtr, 2, newObjv);
1230 Tcl_DecrRefCount(newObjv[0]);
1231 Tcl_DecrRefCount(newObjv[1]);
1232 }
1233 } else {
1234 ConfigureMenuEntry(cascadePtr, 0, (Tcl_Obj **) NULL);
1235 }
1236 }
1237
1238 if (menuPtr->masterMenuPtr != menuPtr) {
1239 for (menuInstancePtr = menuPtr->masterMenuPtr;
1240 menuInstancePtr != NULL;
1241 menuInstancePtr = menuInstancePtr->nextInstancePtr) {
1242 if (menuInstancePtr->nextInstancePtr == menuPtr) {
1243 menuInstancePtr->nextInstancePtr =
1244 menuInstancePtr->nextInstancePtr->nextInstancePtr;
1245 break;
1246 }
1247 }
1248 } else if (menuPtr->nextInstancePtr != NULL) {
1249 panic("Attempting to delete master menu when there are still clones.");
1250 }
1251
1252 /*
1253 * Free up all the stuff that requires special handling, then
1254 * let Tk_FreeConfigOptions handle all the standard option-related
1255 * stuff.
1256 */
1257
1258 for (i = menuPtr->numEntries; --i >= 0; ) {
1259 /*
1260 * As each menu entry is deleted from the end of the array of
1261 * entries, decrement menuPtr->numEntries. Otherwise, the act of
1262 * deleting menu entry i will dereference freed memory attempting
1263 * to queue a redraw for menu entries (i+1)...numEntries.
1264 */
1265
1266 DestroyMenuEntry((char *) menuPtr->entries[i]);
1267 menuPtr->numEntries = i;
1268 }
1269 if (menuPtr->entries != NULL) {
1270 ckfree((char *) menuPtr->entries);
1271 }
1272 TkMenuFreeDrawOptions(menuPtr);
1273 Tk_FreeConfigOptions((char *) menuPtr,
1274 menuPtr->optionTablesPtr->menuOptionTable, menuPtr->tkwin);
1275 if (menuPtr->tkwin != NULL) {
1276 Tk_Window tkwin = menuPtr->tkwin;
1277 menuPtr->tkwin = NULL;
1278 Tk_DestroyWindow(tkwin);
1279 }
1280 }
1281
1282 /*
1283 *----------------------------------------------------------------------
1284 *
1285 * TkDestroyMenu --
1286 *
1287 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
1288 * to clean up the internal structure of a menu at a safe time
1289 * (when no-one is using it anymore). If called on a master instance,
1290 * destroys all of the slave instances. If called on a non-master
1291 * instance, just destroys that instance.
1292 *
1293 * Results:
1294 * None.
1295 *
1296 * Side effects:
1297 * Everything associated with the menu is freed up.
1298 *
1299 *----------------------------------------------------------------------
1300 */
1301
1302 void
1303 TkDestroyMenu(menuPtr)
1304 TkMenu *menuPtr; /* Info about menu widget. */
1305 {
1306 TkMenu *menuInstancePtr;
1307 TkMenuTopLevelList *topLevelListPtr, *nextTopLevelPtr;
1308
1309 if (menuPtr->menuFlags & MENU_DELETION_PENDING) {
1310 return;
1311 }
1312
1313 Tcl_Preserve(menuPtr);
1314
1315 /*
1316 * Now destroy all non-tearoff instances of this menu if this is a
1317 * parent menu. Is this loop safe enough? Are there going to be
1318 * destroy bindings on child menus which kill the parent? If not,
1319 * we have to do a slightly more complex scheme.
1320 */
1321
1322 menuPtr->menuFlags |= MENU_DELETION_PENDING;
1323 if (menuPtr->menuRefPtr != NULL) {
1324 /*
1325 * If any toplevel widgets have this menu as their menubar,
1326 * the geometry of the window may have to be recalculated.
1327 */
1328 topLevelListPtr = menuPtr->menuRefPtr->topLevelListPtr;
1329 while (topLevelListPtr != NULL) {
1330 nextTopLevelPtr = topLevelListPtr->nextPtr;
1331 TkpSetWindowMenuBar(topLevelListPtr->tkwin, NULL);
1332 topLevelListPtr = nextTopLevelPtr;
1333 }
1334 }
1335 if (menuPtr->masterMenuPtr == menuPtr) {
1336 while (menuPtr->nextInstancePtr != NULL) {
1337 menuInstancePtr = menuPtr->nextInstancePtr;
1338 menuPtr->nextInstancePtr = menuInstancePtr->nextInstancePtr;
1339 if (menuInstancePtr->tkwin != NULL) {
1340 Tk_Window tkwin = menuInstancePtr->tkwin;
1341 /*
1342 * Note: it may be desirable to NULL out the tkwin
1343 * field of menuInstancePtr here:
1344 * menuInstancePtr->tkwin = NULL;
1345 */
1346 Tk_DestroyWindow(tkwin);
1347 }
1348 }
1349 }
1350
1351 DestroyMenuInstance(menuPtr);
1352
1353 Tcl_Release(menuPtr);
1354 }
1355
1356 /*
1357 *----------------------------------------------------------------------
1358 *
1359 * UnhookCascadeEntry --
1360 *
1361 * This entry is removed from the list of entries that point to the
1362 * cascade menu. This is done in preparation for changing the menu
1363 * that this entry points to.
1364 *
1365 * At the end of this function, the menu entry no longer contains
1366 * a reference to a 'TkMenuReferences' structure, and therefore
1367 * no such structure contains a reference to this menu entry either.
1368 *
1369 * Results:
1370 * None
1371 *
1372 * Side effects:
1373 * The appropriate lists are modified.
1374 *
1375 *----------------------------------------------------------------------
1376 */
1377
1378 static void
1379 UnhookCascadeEntry(mePtr)
1380 TkMenuEntry *mePtr; /* The cascade entry we are removing
1381 * from the cascade list. */
1382 {
1383 TkMenuEntry *cascadeEntryPtr;
1384 TkMenuEntry *prevCascadePtr;
1385 TkMenuReferences *menuRefPtr;
1386
1387 menuRefPtr = mePtr->childMenuRefPtr;
1388 if (menuRefPtr == NULL) {
1389 return;
1390 }
1391
1392 cascadeEntryPtr = menuRefPtr->parentEntryPtr;
1393 if (cascadeEntryPtr == NULL) {
1394 TkFreeMenuReferences(menuRefPtr);
1395 mePtr->childMenuRefPtr = NULL;
1396 return;
1397 }
1398
1399 /*
1400 * Singularly linked list deletion. The two special cases are
1401 * 1. one element; 2. The first element is the one we want.
1402 */
1403
1404 if (cascadeEntryPtr == mePtr) {
1405 if (cascadeEntryPtr->nextCascadePtr == NULL) {
1406
1407 /*
1408 * This is the last menu entry which points to this
1409 * menu, so we need to clear out the list pointer in the
1410 * cascade itself.
1411 */
1412
1413 menuRefPtr->parentEntryPtr = NULL;
1414 /*
1415 * The original field is set to zero below, after it is
1416 * freed.
1417 */
1418 TkFreeMenuReferences(menuRefPtr);
1419 } else {
1420 menuRefPtr->parentEntryPtr = cascadeEntryPtr->nextCascadePtr;
1421 }
1422 mePtr->nextCascadePtr = NULL;
1423 } else {
1424 for (prevCascadePtr = cascadeEntryPtr,
1425 cascadeEntryPtr = cascadeEntryPtr->nextCascadePtr;
1426 cascadeEntryPtr != NULL;
1427 prevCascadePtr = cascadeEntryPtr,
1428 cascadeEntryPtr = cascadeEntryPtr->nextCascadePtr) {
1429 if (cascadeEntryPtr == mePtr){
1430 prevCascadePtr->nextCascadePtr =
1431 cascadeEntryPtr->nextCascadePtr;
1432 cascadeEntryPtr->nextCascadePtr = NULL;
1433 break;
1434 }
1435 }
1436 mePtr->nextCascadePtr = NULL;
1437 }
1438 mePtr->childMenuRefPtr = NULL;
1439 }
1440
1441 /*
1442 *----------------------------------------------------------------------
1443 *
1444 * DestroyMenuEntry --
1445 *
1446 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
1447 * to clean up the internal structure of a menu entry at a safe time
1448 * (when no-one is using it anymore).
1449 *
1450 * Results:
1451 * None.
1452 *
1453 * Side effects:
1454 * Everything associated with the menu entry is freed.
1455 *
1456 *----------------------------------------------------------------------
1457 */
1458
1459 static void
1460 DestroyMenuEntry(memPtr)
1461 char *memPtr; /* Pointer to entry to be freed. */
1462 {
1463 register TkMenuEntry *mePtr = (TkMenuEntry *) memPtr;
1464 TkMenu *menuPtr = mePtr->menuPtr;
1465
1466 if (menuPtr->postedCascade == mePtr) {
1467
1468 /*
1469 * Ignore errors while unposting the menu, since it's possible
1470 * that the menu has already been deleted and the unpost will
1471 * generate an error.
1472 */
1473
1474 TkPostSubmenu(menuPtr->interp, menuPtr, (TkMenuEntry *) NULL);
1475 }
1476
1477 /*
1478 * Free up all the stuff that requires special handling, then
1479 * let Tk_FreeConfigOptions handle all the standard option-related
1480 * stuff.
1481 */
1482
1483 if (mePtr->type == CASCADE_ENTRY) {
1484 if (menuPtr->masterMenuPtr != menuPtr) {
1485 TkMenu *destroyThis = NULL;
1486 /*
1487 * The menu as a whole is a clone. We must delete the clone
1488 * of the cascaded menu for the particular entry we are
1489 * destroying.
1490 */
1491 TkMenuReferences *menuRefPtr = mePtr->childMenuRefPtr;
1492 if (menuRefPtr != NULL) {
1493 destroyThis = menuRefPtr->menuPtr;
1494 /*
1495 * But only if it is a clone. What can happen is that
1496 * we are in the middle of deleting a menu and this
1497 * menu pointer has already been reset to point to the
1498 * original menu. In that case we have nothing special
1499 * to do.
1500 */
1501 if ((destroyThis != NULL)
1502 && (destroyThis->masterMenuPtr == destroyThis)) {
1503 destroyThis = NULL;
1504 }
1505 }
1506 UnhookCascadeEntry(mePtr);
1507 if (menuRefPtr != NULL) {
1508 if (menuRefPtr->menuPtr == destroyThis) {
1509 menuRefPtr->menuPtr = NULL;
1510 }
1511 if (destroyThis != NULL) {
1512 TkDestroyMenu(destroyThis);
1513 }
1514 }
1515 } else {
1516 UnhookCascadeEntry(mePtr);
1517 }
1518 }
1519 if (mePtr->image != NULL) {
1520 Tk_FreeImage(mePtr->image);
1521 }
1522 if (mePtr->selectImage != NULL) {
1523 Tk_FreeImage(mePtr->selectImage);
1524 }
1525 if (((mePtr->type == CHECK_BUTTON_ENTRY)
1526 || (mePtr->type == RADIO_BUTTON_ENTRY))
1527 && (mePtr->namePtr != NULL)) {
1528 Lang_UntraceVar(menuPtr->interp, mePtr->namePtr,
1529 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1530 MenuVarProc, (ClientData) mePtr);
1531 }
1532 TkpDestroyMenuEntry(mePtr);
1533 TkMenuEntryFreeDrawOptions(mePtr);
1534 Tk_FreeConfigOptions((char *) mePtr, mePtr->optionTable, menuPtr->tkwin);
1535 ckfree((char *) mePtr);
1536 }
1537
1538 /*
1539 *---------------------------------------------------------------------------
1540 *
1541 * MenuWorldChanged --
1542 *
1543 * This procedure is called when the world has changed in some
1544 * way (such as the fonts in the system changing) and the widget needs
1545 * to recompute all its graphics contexts and determine its new geometry.
1546 *
1547 * Results:
1548 * None.
1549 *
1550 * Side effects:
1551 * Menu will be relayed out and redisplayed.
1552 *
1553 *---------------------------------------------------------------------------
1554 */
1555
1556 static void
1557 MenuWorldChanged(instanceData)
1558 ClientData instanceData; /* Information about widget. */
1559 {
1560 TkMenu *menuPtr = (TkMenu *) instanceData;
1561 int i;
1562
1563 TkMenuConfigureDrawOptions(menuPtr);
1564 for (i = 0; i < menuPtr->numEntries; i++) {
1565 TkMenuConfigureEntryDrawOptions(menuPtr->entries[i],
1566 menuPtr->entries[i]->index);
1567 TkpConfigureMenuEntry(menuPtr->entries[i]);
1568 }
1569 }
1570
1571 /*
1572 *----------------------------------------------------------------------
1573 *
1574 * ConfigureMenu --
1575 *
1576 * This procedure is called to process an argv/argc list, plus
1577 * the Tk option database, in order to configure (or
1578 * reconfigure) a menu widget.
1579 *
1580 * Results:
1581 * The return value is a standard Tcl result. If TCL_ERROR is
1582 * returned, then the interp's result contains an error message.
1583 *
1584 * Side effects:
1585 * Configuration information, such as colors, font, etc. get set
1586 * for menuPtr; old resources get freed, if there were any.
1587 *
1588 *----------------------------------------------------------------------
1589 */
1590
1591 static int
1592 ConfigureMenu(interp, menuPtr, objc, objv)
1593 Tcl_Interp *interp; /* Used for error reporting. */
1594 register TkMenu *menuPtr; /* Information about widget; may or may
1595 * not already have values for some fields. */
1596 int objc; /* Number of valid entries in argv. */
1597 Tcl_Obj *CONST objv[]; /* Arguments. */
1598 {
1599 int i;
1600 TkMenu *menuListPtr, *cleanupPtr;
1601 int result;
1602
1603 for (menuListPtr = menuPtr->masterMenuPtr; menuListPtr != NULL;
1604 menuListPtr = menuListPtr->nextInstancePtr) {
1605 menuListPtr->errorStructPtr = (Tk_SavedOptions *)
1606 ckalloc(sizeof(Tk_SavedOptions));
1607 result = Tk_SetOptions(interp, (char *) menuListPtr,
1608 menuListPtr->optionTablesPtr->menuOptionTable, objc, objv,
1609 menuListPtr->tkwin, menuListPtr->errorStructPtr, (int *) NULL);
1610 if (result != TCL_OK) {
1611 for (cleanupPtr = menuPtr->masterMenuPtr;
1612 cleanupPtr != menuListPtr;
1613 cleanupPtr = cleanupPtr->nextInstancePtr) {
1614 Tk_RestoreSavedOptions(cleanupPtr->errorStructPtr);
1615 ckfree((char *) cleanupPtr->errorStructPtr);
1616 cleanupPtr->errorStructPtr = NULL;
1617 }
1618 if (menuListPtr->errorStructPtr != NULL) {
1619 Tk_RestoreSavedOptions(menuListPtr->errorStructPtr);
1620 ckfree((char *) menuListPtr->errorStructPtr);
1621 menuListPtr->errorStructPtr = NULL;
1622 }
1623 return TCL_ERROR;
1624 }
1625
1626 /*
1627 * When a menu is created, the type is in all of the arguments
1628 * to the menu command. Let Tk_ConfigureWidget take care of
1629 * parsing them, and then set the type after we can look at
1630 * the type string. Once set, a menu's type cannot be changed
1631 */
1632
1633 if (menuListPtr->menuType == UNKNOWN_TYPE) {
1634 Tcl_GetIndexFromObj(NULL, menuListPtr->menuTypePtr,
1635 menuTypeStrings, NULL, 0, &menuListPtr->menuType);
1636
1637 /*
1638 * Configure the new window to be either a pop-up menu
1639 * or a tear-off menu.
1640 * We don't do this for menubars since they are not toplevel
1641 * windows. Also, since this gets called before CloneMenu has
1642 * a chance to set the menuType field, we have to look at the
1643 * menuTypeName field to tell that this is a menu bar.
1644 */
1645
1646 if (menuListPtr->menuType == MASTER_MENU) {
1647 TkpMakeMenuWindow(menuListPtr->tkwin, 1);
1648 } else if (menuListPtr->menuType == TEAROFF_MENU) {
1649 TkpMakeMenuWindow(menuListPtr->tkwin, 0);
1650 }
1651 }
1652
1653
1654 /*
1655 * Depending on the -tearOff option, make sure that there is or
1656 * isn't an initial tear-off entry at the beginning of the menu.
1657 */
1658
1659 if (menuListPtr->tearoff) {
1660 if ((menuListPtr->numEntries == 0)
1661 || (menuListPtr->entries[0]->type != TEAROFF_ENTRY)) {
1662 if (MenuNewEntry(menuListPtr, 0, TEAROFF_ENTRY) == NULL) {
1663 for (cleanupPtr = menuPtr->masterMenuPtr;
1664 cleanupPtr != menuListPtr;
1665 cleanupPtr = cleanupPtr->nextInstancePtr) {
1666 Tk_RestoreSavedOptions(cleanupPtr->errorStructPtr);
1667 ckfree((char *) cleanupPtr->errorStructPtr);
1668 cleanupPtr->errorStructPtr = NULL;
1669 }
1670 if (menuListPtr->errorStructPtr != NULL) {
1671 Tk_RestoreSavedOptions(menuListPtr->errorStructPtr);
1672 ckfree((char *) menuListPtr->errorStructPtr);
1673 menuListPtr->errorStructPtr = NULL;
1674 }
1675 return TCL_ERROR;
1676 }
1677 }
1678 } else if ((menuListPtr->numEntries > 0)
1679 && (menuListPtr->entries[0]->type == TEAROFF_ENTRY)) {
1680 int i;
1681
1682 Tcl_EventuallyFree((ClientData) menuListPtr->entries[0],
1683 DestroyMenuEntry);
1684
1685 for (i = 0; i < menuListPtr->numEntries - 1; i++) {
1686 menuListPtr->entries[i] = menuListPtr->entries[i + 1];
1687 menuListPtr->entries[i]->index = i;
1688 }
1689 menuListPtr->numEntries--;
1690 if (menuListPtr->numEntries == 0) {
1691 ckfree((char *) menuListPtr->entries);
1692 menuListPtr->entries = NULL;
1693 }
1694 }
1695
1696 TkMenuConfigureDrawOptions(menuListPtr);
1697
1698 /*
1699 * After reconfiguring a menu, we need to reconfigure all of the
1700 * entries in the menu, since some of the things in the children
1701 * (such as graphics contexts) may have to change to reflect changes
1702 * in the parent.
1703 */
1704
1705 for (i = 0; i < menuListPtr->numEntries; i++) {
1706 TkMenuEntry *mePtr;
1707
1708 mePtr = menuListPtr->entries[i];
1709 ConfigureMenuEntry(mePtr, 0, (Tcl_Obj **) NULL);
1710 }
1711
1712 TkEventuallyRecomputeMenu(menuListPtr);
1713 }
1714
1715 for (cleanupPtr = menuPtr->masterMenuPtr; cleanupPtr != NULL;
1716 cleanupPtr = cleanupPtr->nextInstancePtr) {
1717 Tk_FreeSavedOptions(cleanupPtr->errorStructPtr);
1718 ckfree((char *) cleanupPtr->errorStructPtr);
1719 cleanupPtr->errorStructPtr = NULL;
1720 }
1721
1722 return TCL_OK;
1723 }
1724
1725
1726 /*
1727 *----------------------------------------------------------------------
1728 *
1729 * PostProcessEntry --
1730 *
1731 * This is called by ConfigureMenuEntry to do all of the configuration
1732 * after Tk_SetOptions is called. This is separate
1733 * so that error handling is easier.
1734 *
1735 * Results:
1736 * The return value is a standard Tcl result. If TCL_ERROR is
1737 * returned, then the interp's result contains an error message.
1738 *
1739 * Side effects:
1740 * Configuration information such as label and accelerator get
1741 * set for mePtr; old resources get freed, if there were any.
1742 *
1743 *----------------------------------------------------------------------
1744 */
1745
1746 static int
1747 PostProcessEntry(mePtr)
1748 TkMenuEntry *mePtr; /* The entry we are configuring. */
1749 {
1750 TkMenu *menuPtr = mePtr->menuPtr;
1751 int index = mePtr->index;
1752 char *name;
1753 Tk_Image image;
1754
1755 /*
1756 * The code below handles special configuration stuff not taken
1757 * care of by Tk_ConfigureWidget, such as special processing for
1758 * defaults, sizing strings, graphics contexts, etc.
1759 */
1760
1761 if (mePtr->labelPtr == NULL) {
1762 mePtr->labelLength = 0;
1763 } else {
1764 Tcl_GetStringFromObj(mePtr->labelPtr, &mePtr->labelLength);
1765 }
1766 if (mePtr->accelPtr == NULL) {
1767 mePtr->accelLength = 0;
1768 } else {
1769 Tcl_GetStringFromObj(mePtr->accelPtr, &mePtr->accelLength);
1770 }
1771
1772 /*
1773 * If this is a cascade entry, the platform-specific data of the child
1774 * menu has to be updated. Also, the links that point to parents and
1775 * cascades have to be updated.
1776 */
1777
1778 if ((mePtr->type == CASCADE_ENTRY) && (mePtr->namePtr != NULL)) {
1779 TkMenuEntry *cascadeEntryPtr;
1780 int alreadyThere;
1781 TkMenuReferences *menuRefPtr;
1782 char *oldHashKey = NULL; /* Initialization only needed to
1783 * prevent compiler warning. */
1784
1785 /*
1786 * This is a cascade entry. If the menu that the cascade entry
1787 * is pointing to has changed, we need to remove this entry
1788 * from the list of entries pointing to the old menu, and add a
1789 * cascade reference to the list of entries pointing to the
1790 * new menu.
1791 *
1792 * BUG: We are not recloning for special case #3 yet.
1793 */
1794
1795 name = Tcl_GetStringFromObj(mePtr->namePtr, NULL);
1796 if (mePtr->childMenuRefPtr != NULL) {
1797 oldHashKey = Tcl_GetHashKey(TkGetMenuHashTable(menuPtr->interp),
1798 mePtr->childMenuRefPtr->hashEntryPtr);
1799 if (strcmp(oldHashKey, name) != 0) {
1800 UnhookCascadeEntry(mePtr);
1801 }
1802 }
1803
1804 if ((mePtr->childMenuRefPtr == NULL)
1805 || (strcmp(oldHashKey, name) != 0)) {
1806 menuRefPtr = TkCreateMenuReferences(menuPtr->interp, name);
1807 mePtr->childMenuRefPtr = menuRefPtr;
1808
1809 if (menuRefPtr->parentEntryPtr == NULL) {
1810 menuRefPtr->parentEntryPtr = mePtr;
1811 } else {
1812 alreadyThere = 0;
1813 for (cascadeEntryPtr = menuRefPtr->parentEntryPtr;
1814 cascadeEntryPtr != NULL;
1815 cascadeEntryPtr =
1816 cascadeEntryPtr->nextCascadePtr) {
1817 if (cascadeEntryPtr == mePtr) {
1818 alreadyThere = 1;
1819 break;
1820 }
1821 }
1822
1823 /*
1824 * Put the item at the front of the list.
1825 */
1826
1827 if (!alreadyThere) {
1828 mePtr->nextCascadePtr = menuRefPtr->parentEntryPtr;
1829 menuRefPtr->parentEntryPtr = mePtr;
1830 }
1831 }
1832 }
1833 }
1834
1835 if (TkMenuConfigureEntryDrawOptions(mePtr, index) != TCL_OK) {
1836 return TCL_ERROR;
1837 }
1838
1839 if (TkpConfigureMenuEntry(mePtr) != TCL_OK) {
1840 return TCL_ERROR;
1841 }
1842
1843 /*
1844 * Get the images for the entry, if there are any. Allocate the
1845 * new images before freeing the old ones, so that the reference
1846 * counts don't go to zero and cause image data to be discarded.
1847 */
1848
1849 if (mePtr->imagePtr != NULL) {
1850 char *imageString = Tcl_GetStringFromObj(mePtr->imagePtr, NULL);
1851 image = Tk_GetImage(menuPtr->interp, menuPtr->tkwin, imageString,
1852 TkMenuImageProc, (ClientData) mePtr);
1853 if (image == NULL) {
1854 return TCL_ERROR;
1855 }
1856 } else {
1857 image = NULL;
1858 }
1859 if (mePtr->image != NULL) {
1860 Tk_FreeImage(mePtr->image);
1861 }
1862 mePtr->image = image;
1863 if (mePtr->selectImagePtr != NULL) {
1864 char *selectImageString = Tcl_GetStringFromObj(
1865 mePtr->selectImagePtr, NULL);
1866 image = Tk_GetImage(menuPtr->interp, menuPtr->tkwin, selectImageString,
1867 TkMenuSelectImageProc, (ClientData) mePtr);
1868 if (image == NULL) {
1869 return TCL_ERROR;
1870 }
1871 } else {
1872 image = NULL;
1873 }
1874 if (mePtr->selectImage != NULL) {
1875 Tk_FreeImage(mePtr->selectImage);
1876 }
1877 mePtr->selectImage = image;
1878
1879 if ((mePtr->type == CHECK_BUTTON_ENTRY)
1880 || (mePtr->type == RADIO_BUTTON_ENTRY)) {
1881 Tcl_Obj *valuePtr;
1882 char *name;
1883
1884 if (mePtr->namePtr == NULL) {
1885 if (mePtr->labelPtr == NULL) {
1886 mePtr->namePtr = NULL;
1887 } else {
1888 mePtr->namePtr = Tcl_DuplicateObj(mePtr->labelPtr);
1889 Tcl_IncrRefCount(mePtr->namePtr);
1890 }
1891 }
1892 if (mePtr->onValuePtr == NULL) {
1893 if (mePtr->labelPtr == NULL) {
1894 mePtr->onValuePtr = NULL;
1895 } else {
1896 mePtr->onValuePtr = Tcl_DuplicateObj(mePtr->labelPtr);
1897 Tcl_IncrRefCount(mePtr->onValuePtr);
1898 }
1899 }
1900
1901 /*
1902 * Select the entry if the associated variable has the
1903 * appropriate value, initialize the variable if it doesn't
1904 * exist, then set a trace on the variable to monitor future
1905 * changes to its value.
1906 */
1907
1908 if (mePtr->namePtr != NULL) {
1909 valuePtr = Tcl_ObjGetVar2(menuPtr->interp, mePtr->namePtr, NULL,
1910 TCL_GLOBAL_ONLY);
1911 } else {
1912 valuePtr = NULL;
1913 }
1914 mePtr->entryFlags &= ~ENTRY_SELECTED;
1915 if (valuePtr != NULL) {
1916 if (mePtr->onValuePtr != NULL) {
1917 char *value = Tcl_GetStringFromObj(valuePtr, NULL);
1918 char *onValue = Tcl_GetStringFromObj(mePtr->onValuePtr,
1919 NULL);
1920
1921
1922 if (strcmp(value, onValue) == 0) {
1923 mePtr->entryFlags |= ENTRY_SELECTED;
1924 }
1925 }
1926 } else {
1927 if (mePtr->namePtr != NULL) {
1928 Tcl_ObjSetVar2(menuPtr->interp, mePtr->namePtr, NULL,
1929 (mePtr->type == CHECK_BUTTON_ENTRY)
1930 ? mePtr->offValuePtr
1931 : Tcl_NewObj(),
1932 TCL_GLOBAL_ONLY);
1933 }
1934 }
1935 if (mePtr->namePtr != NULL) {
1936 Lang_TraceVar(menuPtr->interp, mePtr->namePtr,
1937 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1938 MenuVarProc, (ClientData) mePtr);
1939 }
1940 }
1941
1942 return TCL_OK;
1943 }
1944
1945 /*
1946 *----------------------------------------------------------------------
1947 *
1948 * ConfigureMenuEntry --
1949 *
1950 * This procedure is called to process an argv/argc list in order
1951 * to configure (or reconfigure) one entry in a menu.
1952 *
1953 * Results:
1954 * The return value is a standard Tcl result. If TCL_ERROR is
1955 * returned, then the interp's result contains an error message.
1956 *
1957 * Side effects:
1958 * Configuration information such as label and accelerator get
1959 * set for mePtr; old resources get freed, if there were any.
1960 *
1961 *----------------------------------------------------------------------
1962 */
1963
1964 static int
1965 ConfigureMenuEntry(mePtr, objc, objv)
1966 register TkMenuEntry *mePtr; /* Information about menu entry; may
1967 * or may not already have values for
1968 * some fields. */
1969 int objc; /* Number of valid entries in argv. */
1970 Tcl_Obj *CONST objv[]; /* Arguments. */
1971 {
1972 TkMenu *menuPtr = mePtr->menuPtr;
1973 Tk_SavedOptions errorStruct;
1974 int result;
1975
1976 /*
1977 * If this entry is a check button or radio button, then remove
1978 * its old trace procedure.
1979 */
1980
1981 if ((mePtr->namePtr != NULL)
1982 && ((mePtr->type == CHECK_BUTTON_ENTRY)
1983 || (mePtr->type == RADIO_BUTTON_ENTRY))) {
1984 Lang_UntraceVar(menuPtr->interp, mePtr->namePtr,
1985 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1986 MenuVarProc, (ClientData) mePtr);
1987 }
1988
1989 result = TCL_OK;
1990 if (menuPtr->tkwin != NULL) {
1991 if (Tk_SetOptions(menuPtr->interp, (char *) mePtr,
1992 mePtr->optionTable, objc, objv, menuPtr->tkwin,
1993 &errorStruct, (int *) NULL) != TCL_OK) {
1994 return TCL_ERROR;
1995 }
1996 result = PostProcessEntry(mePtr);
1997 if (result != TCL_OK) {
1998 Tk_RestoreSavedOptions(&errorStruct);
1999 PostProcessEntry(mePtr);
2000 }
2001 Tk_FreeSavedOptions(&errorStruct);
2002 }
2003
2004 TkEventuallyRecomputeMenu(menuPtr);
2005
2006 return result;
2007 }
2008
2009 /*
2010 *----------------------------------------------------------------------
2011 *
2012 * ConfigureMenuCloneEntries --
2013 *
2014 * Calls ConfigureMenuEntry for each menu in the clone chain.
2015 *
2016 * Results:
2017 * The return value is a standard Tcl result. If TCL_ERROR is
2018 * returned, then the interp's result contains an error message.
2019 *
2020 * Side effects:
2021 * Configuration information such as label and accelerator get
2022 * set for mePtr; old resources get freed, if there were any.
2023 *
2024 *----------------------------------------------------------------------
2025 */
2026
2027 static int
2028 ConfigureMenuCloneEntries(interp, menuPtr, index, objc, objv)
2029 Tcl_Interp *interp; /* Used for error reporting. */
2030 TkMenu *menuPtr; /* Information about whole menu. */
2031 int index; /* Index of mePtr within menuPtr's
2032 * entries. */
2033 int objc; /* Number of valid entries in argv. */
2034 Tcl_Obj *CONST objv[]; /* Arguments. */
2035 {
2036 TkMenuEntry *mePtr;
2037 TkMenu *menuListPtr;
2038 int cascadeEntryChanged = 0;
2039 TkMenuReferences *oldCascadeMenuRefPtr, *cascadeMenuRefPtr = NULL;
2040 Tcl_Obj *oldCascadePtr = NULL;
2041 char *newCascadeName;
2042
2043 /*
2044 * Cascades are kind of tricky here. This is special case #3 in the comment
2045 * at the top of this file. Basically, if a menu is the master menu of a
2046 * clone chain, and has an entry with a cascade menu, the clones of
2047 * the menu will point to clones of the cascade menu. We have
2048 * to destroy the clones of the cascades, clone the new cascade
2049 * menu, and configure the entry to point to the new clone.
2050 */
2051
2052 mePtr = menuPtr->masterMenuPtr->entries[index];
2053 if (mePtr->type == CASCADE_ENTRY) {
2054 oldCascadePtr = mePtr->namePtr;
2055 if (oldCascadePtr != NULL) {
2056 Tcl_IncrRefCount(oldCascadePtr);
2057 }
2058 }
2059
2060 if (ConfigureMenuEntry(mePtr, objc, objv) != TCL_OK) {
2061 return TCL_ERROR;
2062 }
2063
2064 if (mePtr->type == CASCADE_ENTRY) {
2065 char *oldCascadeName;
2066
2067 if (mePtr->namePtr != NULL) {
2068 newCascadeName = Tcl_GetStringFromObj(mePtr->namePtr, NULL);
2069 } else {
2070 newCascadeName = NULL;
2071 }
2072
2073 if ((oldCascadePtr == NULL) && (mePtr->namePtr == NULL)) {
2074 cascadeEntryChanged = 0;
2075 } else if (((oldCascadePtr == NULL) && (mePtr->namePtr != NULL))
2076 || ((oldCascadePtr != NULL)
2077 && (mePtr->namePtr == NULL))) {
2078 cascadeEntryChanged = 1;
2079 } else {
2080 oldCascadeName = Tcl_GetStringFromObj(oldCascadePtr,
2081 NULL);
2082 cascadeEntryChanged = (strcmp(oldCascadeName, newCascadeName)
2083 != 0);
2084 }
2085 if (oldCascadePtr != NULL) {
2086 Tcl_DecrRefCount(oldCascadePtr);
2087 }
2088 }
2089
2090 if (cascadeEntryChanged) {
2091 if (mePtr->namePtr != NULL) {
2092 newCascadeName = Tcl_GetStringFromObj(mePtr->namePtr, NULL);
2093 cascadeMenuRefPtr = TkFindMenuReferences(menuPtr->interp,
2094 newCascadeName);
2095 }
2096 }
2097
2098 for (menuListPtr = menuPtr->masterMenuPtr->nextInstancePtr;
2099 menuListPtr != NULL;
2100 menuListPtr = menuListPtr->nextInstancePtr) {
2101
2102 mePtr = menuListPtr->entries[index];
2103
2104 if (cascadeEntryChanged && (mePtr->namePtr != NULL)) {
2105 oldCascadeMenuRefPtr = TkFindMenuReferencesObj(menuPtr->interp,
2106 mePtr->namePtr);
2107
2108 if ((oldCascadeMenuRefPtr != NULL)
2109 && (oldCascadeMenuRefPtr->menuPtr != NULL)) {
2110 RecursivelyDeleteMenu(oldCascadeMenuRefPtr->menuPtr);
2111 }
2112 }
2113
2114 if (ConfigureMenuEntry(mePtr, objc, objv) != TCL_OK) {
2115 return TCL_ERROR;
2116 }
2117
2118 if (cascadeEntryChanged && (mePtr->namePtr != NULL)) {
2119 if (cascadeMenuRefPtr->menuPtr != NULL) {
2120 Tcl_Obj *newObjv[2];
2121 Tcl_Obj *newCloneNamePtr;
2122 Tcl_Obj *pathNamePtr = Tcl_NewStringObj(
2123 Tk_PathName(menuListPtr->tkwin), -1);
2124 Tcl_Obj *normalPtr = Tcl_NewStringObj("normal", -1);
2125 Tcl_Obj *menuObjPtr = Tcl_NewStringObj("-menu", -1);
2126
2127 Tcl_IncrRefCount(pathNamePtr);
2128 newCloneNamePtr = TkNewMenuName(menuPtr->interp,
2129 pathNamePtr,
2130 cascadeMenuRefPtr->menuPtr);
2131 Tcl_IncrRefCount(newCloneNamePtr);
2132 Tcl_IncrRefCount(normalPtr);
2133 CloneMenu(cascadeMenuRefPtr->menuPtr, newCloneNamePtr,
2134 normalPtr);
2135
2136 newObjv[0] = menuObjPtr;
2137 newObjv[1] = newCloneNamePtr;
2138 Tcl_IncrRefCount(menuObjPtr);
2139 ConfigureMenuEntry(mePtr, 2, newObjv);
2140 Tcl_DecrRefCount(newCloneNamePtr);
2141 Tcl_DecrRefCount(pathNamePtr);
2142 Tcl_DecrRefCount(normalPtr);
2143 Tcl_DecrRefCount(menuObjPtr);
2144 }
2145 }
2146 }
2147 return TCL_OK;
2148 }
2149
2150 /*
2151 *--------------------------------------------------------------
2152 *
2153 * TkGetMenuIndex --
2154 *
2155 * Parse a textual index into a menu and return the numerical
2156 * index of the indicated entry.
2157 *
2158 * Results:
2159 * A standard Tcl result. If all went well, then *indexPtr is
2160 * filled in with the entry index corresponding to string
2161 * (ranges from -1 to the number of entries in the menu minus
2162 * one). Otherwise an error message is left in the interp's result.
2163 *
2164 * Side effects:
2165 * None.
2166 *
2167 *--------------------------------------------------------------
2168 */
2169
2170 int
2171 TkGetMenuIndex(interp, menuPtr, objPtr, lastOK, indexPtr)
2172 Tcl_Interp *interp; /* For error messages. */
2173 TkMenu *menuPtr; /* Menu for which the index is being
2174 * specified. */
2175 Tcl_Obj *objPtr; /* Specification of an entry in menu. See
2176 * manual entry for valid .*/
2177 int lastOK; /* Non-zero means its OK to return index
2178 * just *after* last entry. */
2179 int *indexPtr; /* Where to store converted index. */
2180 {
2181 int i;
2182 char *string = Tcl_GetStringFromObj(objPtr, NULL);
2183
2184 if ((string[0] == 'a') && (strcmp(string, "active") == 0)) {
2185 *indexPtr = menuPtr->active;
2186 goto success;
2187 }
2188
2189 if (((string[0] == 'l') && (strcmp(string, "last") == 0))
2190 || ((string[0] == 'e') && (strcmp(string, "end") == 0))) {
2191 *indexPtr = menuPtr->numEntries - ((lastOK) ? 0 : 1);
2192 goto success;
2193 }
2194
2195 if ((string[0] == 'n') && (strcmp(string, "none") == 0)) {
2196 *indexPtr = -1;
2197 goto success;
2198 }
2199
2200 if (string[0] == '@') {
2201 if (GetIndexFromCoords(interp, menuPtr, string, indexPtr)
2202 == TCL_OK) {
2203 goto success;
2204 }
2205 }
2206
2207 if (isdigit(UCHAR(string[0]))) {
2208 if (Tcl_GetIntFromObj(interp, objPtr, &i) == TCL_OK) {
2209 if (i >= menuPtr->numEntries) {
2210 if (lastOK) {
2211 i = menuPtr->numEntries;
2212 } else {
2213 i = menuPtr->numEntries-1;
2214 }
2215 } else if (i < 0) {
2216 i = -1;
2217 }
2218 *indexPtr = i;
2219 goto success;
2220 }
2221 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
2222 }
2223
2224 for (i = 0; i < menuPtr->numEntries; i++) {
2225 Tcl_Obj *labelPtr = menuPtr->entries[i]->labelPtr;
2226 char *label = (labelPtr == NULL) ? NULL
2227 : Tcl_GetStringFromObj(labelPtr, NULL);
2228
2229 if ((label != NULL)
2230 && (Tcl_StringMatch(label, string))) {
2231 *indexPtr = i;
2232 goto success;
2233 }
2234 }
2235
2236 Tcl_AppendResult(interp, "bad menu entry index \"",
2237 string, "\"", (char *) NULL);
2238 return TCL_ERROR;
2239
2240 success:
2241 return TCL_OK;
2242 }
2243
2244 /*
2245 *----------------------------------------------------------------------
2246 *
2247 * MenuCmdDeletedProc --
2248 *
2249 * This procedure is invoked when a widget command is deleted. If
2250 * the widget isn't already in the process of being destroyed,
2251 * this command destroys it.
2252 *
2253 * Results:
2254 * None.
2255 *
2256 * Side effects:
2257 * The widget is destroyed.
2258 *
2259 *----------------------------------------------------------------------
2260 */
2261
2262 static void
2263 MenuCmdDeletedProc(clientData)
2264 ClientData clientData; /* Pointer to widget record for widget. */
2265 {
2266 TkMenu *menuPtr = (TkMenu *) clientData;
2267 Tk_Window tkwin = menuPtr->tkwin;
2268
2269 /*
2270 * This procedure could be invoked either because the window was
2271 * destroyed and the command was then deleted (in which case tkwin
2272 * is NULL) or because the command was deleted, and then this procedure
2273 * destroys the widget.
2274 */
2275
2276 if (tkwin != NULL) {
2277 /*
2278 * Note: it may be desirable to NULL out the tkwin
2279 * field of menuPtr here:
2280 * menuPtr->tkwin = NULL;
2281 */
2282 Tk_DestroyWindow(tkwin);
2283 }
2284 }
2285
2286 /*
2287 *----------------------------------------------------------------------
2288 *
2289 * MenuNewEntry --
2290 *
2291 * This procedure allocates and initializes a new menu entry.
2292 *
2293 * Results:
2294 * The return value is a pointer to a new menu entry structure,
2295 * which has been malloc-ed, initialized, and entered into the
2296 * entry array for the menu.
2297 *
2298 * Side effects:
2299 * Storage gets allocated.
2300 *
2301 *----------------------------------------------------------------------
2302 */
2303
2304 static TkMenuEntry *
2305 MenuNewEntry(menuPtr, index, type)
2306 TkMenu *menuPtr; /* Menu that will hold the new entry. */
2307 int index; /* Where in the menu the new entry is to
2308 * go. */
2309 int type; /* The type of the new entry. */
2310 {
2311 TkMenuEntry *mePtr;
2312 TkMenuEntry **newEntries;
2313 int i;
2314
2315 /*
2316 * Create a new array of entries with an empty slot for the
2317 * new entry.
2318 */
2319
2320 newEntries = (TkMenuEntry **) ckalloc((unsigned)
2321 ((menuPtr->numEntries+1)*sizeof(TkMenuEntry *)));
2322 for (i = 0; i < index; i++) {
2323 newEntries[i] = menuPtr->entries[i];
2324 }
2325 for ( ; i < menuPtr->numEntries; i++) {
2326 newEntries[i+1] = menuPtr->entries[i];
2327 newEntries[i+1]->index = i + 1;
2328 }
2329 if (menuPtr->numEntries != 0) {
2330 ckfree((char *) menuPtr->entries);
2331 }
2332 menuPtr->entries = newEntries;
2333 menuPtr->numEntries++;
2334 mePtr = (TkMenuEntry *) ckalloc(sizeof(TkMenuEntry));
2335 menuPtr->entries[index] = mePtr;
2336 mePtr->type = type;
2337 mePtr->optionTable = menuPtr->optionTablesPtr->entryOptionTables[type];
2338 mePtr->menuPtr = menuPtr;
2339 mePtr->labelPtr = NULL;
2340 mePtr->labelLength = 0;
2341 mePtr->underline = -1;
2342 mePtr->bitmapPtr = NULL;
2343 mePtr->imagePtr = NULL;
2344 mePtr->image = NULL;
2345 mePtr->selectImagePtr = NULL;
2346 mePtr->selectImage = NULL;
2347 mePtr->accelPtr = NULL;
2348 mePtr->accelLength = 0;
2349 mePtr->state = ENTRY_DISABLED;
2350 mePtr->borderPtr = NULL;
2351 mePtr->fgPtr = NULL;
2352 mePtr->activeBorderPtr = NULL;
2353 mePtr->activeFgPtr = NULL;
2354 mePtr->fontPtr = NULL;
2355 mePtr->indicatorOn = 0;
2356 mePtr->indicatorFgPtr = NULL;
2357 mePtr->columnBreak = 0;
2358 mePtr->hideMargin = 0;
2359 mePtr->commandPtr = NULL;
2360 mePtr->namePtr = NULL;
2361 mePtr->childMenuRefPtr = NULL;
2362 mePtr->onValuePtr = NULL;
2363 mePtr->offValuePtr = NULL;
2364 mePtr->entryFlags = 0;
2365 mePtr->index = index;
2366 mePtr->nextCascadePtr = NULL;
2367 if (Tk_InitOptions(menuPtr->interp, (char *) mePtr,
2368 mePtr->optionTable, menuPtr->tkwin) != TCL_OK) {
2369 ckfree((char *) mePtr);
2370 return NULL;
2371 }
2372 TkMenuInitializeEntryDrawingFields(mePtr);
2373 if (TkpMenuNewEntry(mePtr) != TCL_OK) {
2374 Tk_FreeConfigOptions((char *) mePtr, mePtr->optionTable,
2375 menuPtr->tkwin);
2376 ckfree((char *) mePtr);
2377 return NULL;
2378 }
2379
2380 return mePtr;
2381 }
2382
2383 /*
2384 *----------------------------------------------------------------------
2385 *
2386 * MenuAddOrInsert --
2387 *
2388 * This procedure does all of the work of the "add" and "insert"
2389 * widget commands, allowing the code for these to be shared.
2390 *
2391 * Results:
2392 * A standard Tcl return value.
2393 *
2394 * Side effects:
2395 * A new menu entry is created in menuPtr.
2396 *
2397 *----------------------------------------------------------------------
2398 */
2399
2400 static int
2401 MenuAddOrInsert(interp, menuPtr, indexPtr, objc, objv)
2402 Tcl_Interp *interp; /* Used for error reporting. */
2403 TkMenu *menuPtr; /* Widget in which to create new
2404 * entry. */
2405 Tcl_Obj *indexPtr; /* Object describing index at which
2406 * to insert. NULL means insert at
2407 * end. */
2408 int objc; /* Number of elements in objv. */
2409 Tcl_Obj *CONST objv[]; /* Arguments to command: first arg
2410 * is type of entry, others are
2411 * config options. */
2412 {
2413 int type, index;
2414 TkMenuEntry *mePtr;
2415 TkMenu *menuListPtr;
2416
2417 if (indexPtr != NULL) {
2418 if (TkGetMenuIndex(interp, menuPtr, indexPtr, 1, &index)
2419 != TCL_OK) {
2420 return TCL_ERROR;
2421 }
2422 } else {
2423 index = menuPtr->numEntries;
2424 }
2425 if (index < 0) {
2426 char *indexString = Tcl_GetStringFromObj(indexPtr, NULL);
2427 Tcl_AppendResult(interp, "bad index \"", indexString, "\"",
2428 (char *) NULL);
2429 return TCL_ERROR;
2430 }
2431 if (menuPtr->tearoff && (index == 0)) {
2432 index = 1;
2433 }
2434
2435 /*
2436 * Figure out the type of the new entry.
2437 */
2438
2439 if (Tcl_GetIndexFromObj(interp, objv[0], menuEntryTypeStrings,
2440 "menu entry type", 0, &type) != TCL_OK) {
2441 return TCL_ERROR;
2442 }
2443
2444 /*
2445 * Now we have to add an entry for every instance related to this menu.
2446 */
2447
2448 for (menuListPtr = menuPtr->masterMenuPtr; menuListPtr != NULL;
2449 menuListPtr = menuListPtr->nextInstancePtr) {
2450
2451 mePtr = MenuNewEntry(menuListPtr, index, type);
2452 if (mePtr == NULL) {
2453 return TCL_ERROR;
2454 }
2455 if (ConfigureMenuEntry(mePtr, objc - 1, objv + 1) != TCL_OK) {
2456 TkMenu *errorMenuPtr;
2457 int i;
2458
2459 for (errorMenuPtr = menuPtr->masterMenuPtr;
2460 errorMenuPtr != NULL;
2461 errorMenuPtr = errorMenuPtr->nextInstancePtr) {
2462 Tcl_EventuallyFree((ClientData) errorMenuPtr->entries[index],
2463 DestroyMenuEntry);
2464 for (i = index; i < errorMenuPtr->numEntries - 1; i++) {
2465 errorMenuPtr->entries[i] = errorMenuPtr->entries[i + 1];
2466 errorMenuPtr->entries[i]->index = i;
2467 }
2468 errorMenuPtr->numEntries--;
2469 if (errorMenuPtr->numEntries == 0) {
2470 ckfree((char *) errorMenuPtr->entries);
2471 errorMenuPtr->entries = NULL;
2472 }
2473 if (errorMenuPtr == menuListPtr) {
2474 break;
2475 }
2476 }
2477 return TCL_ERROR;
2478 }
2479
2480 /*
2481 * If a menu has cascades, then every instance of the menu has
2482 * to have its own parallel cascade structure. So adding an
2483 * entry to a menu with clones means that the menu that the
2484 * entry points to has to be cloned for every clone the
2485 * master menu has. This is special case #2 in the comment
2486 * at the top of this file.
2487 */
2488
2489 if ((menuPtr != menuListPtr) && (type == CASCADE_ENTRY)) {
2490 if ((mePtr->namePtr != NULL)
2491 && (mePtr->childMenuRefPtr != NULL)
2492 && (mePtr->childMenuRefPtr->menuPtr != NULL)) {
2493 TkMenu *cascadeMenuPtr =
2494 mePtr->childMenuRefPtr->menuPtr->masterMenuPtr;
2495 Tcl_Obj *newCascadePtr;
2496 Tcl_Obj *menuNamePtr = Tcl_NewStringObj("-menu", -1);
2497 Tcl_Obj *windowNamePtr =
2498 Tcl_NewStringObj(Tk_PathName(menuListPtr->tkwin), -1);
2499 Tcl_Obj *normalPtr = Tcl_NewStringObj("normal", -1);
2500 Tcl_Obj *newObjv[2];
2501 TkMenuReferences *menuRefPtr;
2502
2503 Tcl_IncrRefCount(windowNamePtr);
2504 newCascadePtr = TkNewMenuName(menuListPtr->interp,
2505 windowNamePtr, cascadeMenuPtr);
2506 Tcl_IncrRefCount(newCascadePtr);
2507 Tcl_IncrRefCount(normalPtr);
2508 CloneMenu(cascadeMenuPtr, newCascadePtr, normalPtr);
2509
2510 menuRefPtr = TkFindMenuReferencesObj(menuListPtr->interp,
2511 newCascadePtr);
2512 if (menuRefPtr == NULL) {
2513 panic("CloneMenu failed inside of MenuAddOrInsert.");
2514 }
2515 newObjv[0] = menuNamePtr;
2516 newObjv[1] = newCascadePtr;
2517 Tcl_IncrRefCount(menuNamePtr);
2518 Tcl_IncrRefCount(newCascadePtr);
2519 ConfigureMenuEntry(mePtr, 2, newObjv);
2520 Tcl_DecrRefCount(newCascadePtr);
2521 Tcl_DecrRefCount(menuNamePtr);
2522 Tcl_DecrRefCount(windowNamePtr);
2523 Tcl_DecrRefCount(normalPtr);
2524 }
2525 }
2526 }
2527 return TCL_OK;
2528 }
2529
2530 /*
2531 *--------------------------------------------------------------
2532 *
2533 * MenuVarProc --
2534 *
2535 * This procedure is invoked when someone changes the
2536 * state variable associated with a radiobutton or checkbutton
2537 * menu entry. The entry's selected state is set to match
2538 * the value of the variable.
2539 *
2540 * Results:
2541 * NULL is always returned.
2542 *
2543 * Side effects:
2544 * The menu entry may become selected or deselected.
2545 *
2546 *--------------------------------------------------------------
2547 */
2548
2549 static char *
2550 MenuVarProc(clientData, interp, name1, name2, flags)
2551 ClientData clientData; /* Information about menu entry. */
2552 Tcl_Interp *interp; /* Interpreter containing variable. */
2553 Var name1; /* First part of variable's name. */
2554 CONST char *name2; /* Second part of variable's name. */
2555 int flags; /* Describes what just happened. */
2556 {
2557 TkMenuEntry *mePtr = (TkMenuEntry *) clientData;
2558 TkMenu *menuPtr;
2559 CONST char *value;
2560 char *name = Tcl_GetStringFromObj(mePtr->namePtr, NULL);
2561 char *onValue;
2562
2563 menuPtr = mePtr->menuPtr;
2564
2565 /*
2566 * If the variable is being unset, then re-establish the
2567 * trace unless the whole interpreter is going away.
2568 */
2569
2570 if (flags & TCL_TRACE_UNSETS) {
2571 mePtr->entryFlags &= ~ENTRY_SELECTED;
2572 if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
2573 Lang_TraceVar(interp, mePtr->namePtr,
2574 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
2575 MenuVarProc, clientData);
2576 }
2577 TkpConfigureMenuEntry(mePtr);
2578 TkEventuallyRedrawMenu(menuPtr, (TkMenuEntry *) NULL);
2579 return (char *) NULL;
2580 }
2581
2582 /*
2583 * Use the value of the variable to update the selected status of
2584 * the menu entry.
2585 */
2586
2587 value = Tcl_GetString(Tcl_ObjGetVar2(interp, mePtr->namePtr, NULL, TCL_GLOBAL_ONLY));
2588 if (value == NULL) {
2589 value = "";
2590 }
2591 if (mePtr->onValuePtr != NULL) {
2592 onValue = Tcl_GetStringFromObj(mePtr->onValuePtr, NULL);
2593 if (strcmp(value, onValue) == 0) {
2594 if (mePtr->entryFlags & ENTRY_SELECTED) {
2595 return (char *) NULL;
2596 }
2597 mePtr->entryFlags |= ENTRY_SELECTED;
2598 } else if (mePtr->entryFlags & ENTRY_SELECTED) {
2599 mePtr->entryFlags &= ~ENTRY_SELECTED;
2600 } else {
2601 return (char *) NULL;
2602 }
2603 } else {
2604 return (char *) NULL;
2605 }
2606 TkpConfigureMenuEntry(mePtr);
2607 TkEventuallyRedrawMenu(menuPtr, mePtr);
2608 return (char *) NULL;
2609 }
2610
2611 /*
2612 *----------------------------------------------------------------------
2613 *
2614 * TkActivateMenuEntry --
2615 *
2616 * This procedure is invoked to make a particular menu entry
2617 * the active one, deactivating any other entry that might
2618 * currently be active.
2619 *
2620 * Results:
2621 * The return value is a standard Tcl result (errors can occur
2622 * while posting and unposting submenus).
2623 *
2624 * Side effects:
2625 * Menu entries get redisplayed, and the active entry changes.
2626 * Submenus may get posted and unposted.
2627 *
2628 *----------------------------------------------------------------------
2629 */
2630
2631 int
2632 TkActivateMenuEntry(menuPtr, index)
2633 register TkMenu *menuPtr; /* Menu in which to activate. */
2634 int index; /* Index of entry to activate, or
2635 * -1 to deactivate all entries. */
2636 {
2637 register TkMenuEntry *mePtr;
2638 int result = TCL_OK;
2639
2640 if (menuPtr->active >= 0) {
2641 mePtr = menuPtr->entries[menuPtr->active];
2642
2643 /*
2644 * Don't change the state unless it's currently active (state
2645 * might already have been changed to disabled).
2646 */
2647
2648 if (mePtr->state == ENTRY_ACTIVE) {
2649 mePtr->state = ENTRY_NORMAL;
2650 }
2651 TkEventuallyRedrawMenu(menuPtr, menuPtr->entries[menuPtr->active]);
2652 }
2653 menuPtr->active = index;
2654 if (index >= 0) {
2655 mePtr = menuPtr->entries[index];
2656 mePtr->state = ENTRY_ACTIVE;
2657 TkEventuallyRedrawMenu(menuPtr, mePtr);
2658 }
2659 return result;
2660 }
2661
2662 /*
2663 *----------------------------------------------------------------------
2664 *
2665 * TkPostCommand --
2666 *
2667 * Execute the postcommand for the given menu.
2668 *
2669 * Results:
2670 * The return value is a standard Tcl result (errors can occur
2671 * while the postcommands are being processed).
2672 *
2673 * Side effects:
2674 * Since commands can get executed while this routine is being executed,
2675 * the entire world can change.
2676 *
2677 *----------------------------------------------------------------------
2678 */
2679
2680 int
2681 TkPostCommand(menuPtr)
2682 TkMenu *menuPtr;
2683 {
2684 int result;
2685
2686 /*
2687 * If there is a command for the menu, execute it. This
2688 * may change the size of the menu, so be sure to recompute
2689 * the menu's geometry if needed.
2690 */
2691
2692 if (menuPtr->postCommandPtr != NULL) {
2693 Tcl_Obj *postCommandPtr = menuPtr->postCommandPtr;
2694
2695 Tcl_IncrRefCount(postCommandPtr);
2696 result = Tcl_EvalObjEx(menuPtr->interp, postCommandPtr,
2697 TCL_EVAL_GLOBAL);
2698 Tcl_DecrRefCount(postCommandPtr);
2699 if (result != TCL_OK) {
2700 return result;
2701 }
2702 TkRecomputeMenu(menuPtr);
2703 }
2704 return TCL_OK;
2705 }
2706
2707 /*
2708 *--------------------------------------------------------------
2709 *
2710 * CloneMenu --
2711 *
2712 * Creates a child copy of the menu. It will be inserted into
2713 * the menu's instance chain. All attributes and entry
2714 * attributes will be duplicated.
2715 *
2716 * Results:
2717 * A standard Tcl result.
2718 *
2719 * Side effects:
2720 * Allocates storage. After the menu is created, any
2721 * configuration done with this menu or any related one
2722 * will be reflected in all of them.
2723 *
2724 *--------------------------------------------------------------
2725 */
2726
2727
2728 static int
2729 CloneMenu(menuPtr, newMenuNamePtr, newMenuTypePtr)
2730 TkMenu *menuPtr; /* The menu we are going to clone */
2731 Tcl_Obj *newMenuNamePtr; /* The name to give the new menu */
2732 Tcl_Obj *newMenuTypePtr; /* What kind of menu is this, a normal menu
2733 * a menubar, or a tearoff? */
2734 {
2735 int returnResult;
2736 int menuType, i;
2737 TkMenuReferences *menuRefPtr;
2738 Tcl_Obj *menuDupCommandArray[4];
2739
2740 if (newMenuTypePtr == NULL) {
2741 menuType = MASTER_MENU;
2742 } else {
2743 if (Tcl_GetIndexFromObj(menuPtr->interp, newMenuTypePtr,
2744 menuTypeStrings, "menu type", 0, &menuType) != TCL_OK) {
2745 return TCL_ERROR;
2746 }
2747 }
2748
2749 menuDupCommandArray[0] = Tcl_NewStringObj("tkMenuDup", -1);
2750 menuDupCommandArray[1] = LangWidgetObj(menuPtr->interp,menuPtr->tkwin);
2751 Tcl_IncrRefCount(newMenuNamePtr);
2752 menuDupCommandArray[2] = newMenuNamePtr;
2753 if (newMenuTypePtr == NULL) {
2754 menuDupCommandArray[3] = Tcl_NewStringObj("normal", -1);
2755 } else {
2756 menuDupCommandArray[3] = newMenuTypePtr;
2757 }
2758 for (i = 0; i < 4; i++) {
2759 Tcl_IncrRefCount(menuDupCommandArray[i]);
2760 }
2761 Tcl_Preserve((ClientData) menuPtr);
2762 returnResult = Tcl_EvalObjv(menuPtr->interp, 4, menuDupCommandArray, 0);
2763 for (i = 0; i < 4; i++) {
2764 Tcl_DecrRefCount(menuDupCommandArray[i]);
2765 }
2766
2767 /*
2768 * Make sure the tcl command actually created the clone.
2769 */
2770
2771 if ((returnResult == TCL_OK) &&
2772 ((menuRefPtr = TkFindMenuReferencesObj(menuPtr->interp,
2773 newMenuNamePtr)) != (TkMenuReferences *) NULL)
2774 && (menuPtr->numEntries == menuRefPtr->menuPtr->numEntries)) {
2775 TkMenu *newMenuPtr = menuRefPtr->menuPtr;
2776 Tcl_Obj *newObjv[3];
2777 int i, numElements;
2778
2779 /*
2780 * Now put this newly created menu into the parent menu's instance
2781 * chain.
2782 */
2783
2784 if (menuPtr->nextInstancePtr == NULL) {
2785 menuPtr->nextInstancePtr = newMenuPtr;
2786 newMenuPtr->masterMenuPtr = menuPtr->masterMenuPtr;
2787 } else {
2788 TkMenu *masterMenuPtr;
2789
2790 masterMenuPtr = menuPtr->masterMenuPtr;
2791 newMenuPtr->nextInstancePtr = masterMenuPtr->nextInstancePtr;
2792 masterMenuPtr->nextInstancePtr = newMenuPtr;
2793 newMenuPtr->masterMenuPtr = masterMenuPtr;
2794 }
2795
2796 /*
2797 * Add the master menu's window to the bind tags for this window
2798 * after this window's tag. This is so the user can bind to either
2799 * this clone (which may not be easy to do) or the entire menu
2800 * clone structure.
2801 */
2802
2803 newObjv[0] = Tcl_NewStringObj("bindtags", -1);
2804 newObjv[1] = Tcl_NewStringObj(Tk_PathName(newMenuPtr->tkwin), -1);
2805 Tcl_IncrRefCount(newObjv[0]);
2806 Tcl_IncrRefCount(newObjv[1]);
2807 if (Tk_BindtagsObjCmd((ClientData)newMenuPtr->tkwin,
2808 newMenuPtr->interp, 2, newObjv) == TCL_OK) {
2809 char *windowName;
2810 Tcl_Obj *bindingsPtr =
2811 Tcl_DuplicateObj(Tcl_GetObjResult(newMenuPtr->interp));
2812 Tcl_Obj *elementPtr;
2813
2814 Tcl_IncrRefCount(bindingsPtr);
2815 Tcl_ListObjLength(newMenuPtr->interp, bindingsPtr, &numElements);
2816 for (i = 0; i < numElements; i++) {
2817 Tcl_ListObjIndex(newMenuPtr->interp, bindingsPtr, i,
2818 &elementPtr);
2819 windowName = Tcl_GetStringFromObj(elementPtr, NULL);
2820 if (strcmp(windowName, Tk_PathName(newMenuPtr->tkwin))
2821 == 0) {
2822 Tcl_Obj *newElementPtr = Tcl_NewStringObj(
2823 Tk_PathName(newMenuPtr->masterMenuPtr->tkwin), -1);
2824 /*
2825 * The newElementPtr will have its refCount incremented
2826 * here, so we don't need to worry about it any more.
2827 */
2828 Tcl_ListObjReplace(menuPtr->interp, bindingsPtr,
2829 i + 1, 0, 1, &newElementPtr);
2830 newObjv[2] = bindingsPtr;
2831 Tk_BindtagsObjCmd((ClientData)newMenuPtr->tkwin,
2832 menuPtr->interp, 3, newObjv);
2833 break;
2834 }
2835 }
2836 Tcl_DecrRefCount(bindingsPtr);
2837 }
2838 Tcl_DecrRefCount(newObjv[0]);
2839 Tcl_DecrRefCount(newObjv[1]);
2840 Tcl_ResetResult(menuPtr->interp);
2841
2842 /*
2843 * Clone all of the cascade menus that this menu points to.
2844 */
2845
2846 for (i = 0; i < menuPtr->numEntries; i++) {
2847 TkMenuReferences *cascadeRefPtr;
2848 TkMenu *oldCascadePtr;
2849
2850 if ((menuPtr->entries[i]->type == CASCADE_ENTRY)
2851 && (menuPtr->entries[i]->namePtr != NULL)) {
2852 cascadeRefPtr =
2853 TkFindMenuReferencesObj(menuPtr->interp,
2854 menuPtr->entries[i]->namePtr);
2855 if ((cascadeRefPtr != NULL) && (cascadeRefPtr->menuPtr)) {
2856 Tcl_Obj *windowNamePtr =
2857 Tcl_NewStringObj(Tk_PathName(newMenuPtr->tkwin),
2858 -1);
2859 Tcl_Obj *newCascadePtr;
2860
2861 oldCascadePtr = cascadeRefPtr->menuPtr;
2862
2863 Tcl_IncrRefCount(windowNamePtr);
2864 newCascadePtr = TkNewMenuName(menuPtr->interp,
2865 windowNamePtr, oldCascadePtr);
2866 Tcl_IncrRefCount(newCascadePtr);
2867 CloneMenu(oldCascadePtr, newCascadePtr, NULL);
2868
2869 newObjv[0] = Tcl_NewStringObj("-menu", -1);
2870 newObjv[1] = newCascadePtr;
2871 Tcl_IncrRefCount(newObjv[0]);
2872 ConfigureMenuEntry(newMenuPtr->entries[i], 2, newObjv);
2873 Tcl_DecrRefCount(newObjv[0]);
2874 Tcl_DecrRefCount(newCascadePtr);
2875 Tcl_DecrRefCount(windowNamePtr);
2876 }
2877 }
2878 }
2879
2880 returnResult = TCL_OK;
2881 } else {
2882 returnResult = TCL_ERROR;
2883 }
2884 Tcl_Release((ClientData) menuPtr);
2885 return returnResult;
2886 }
2887
2888 /*
2889 *----------------------------------------------------------------------
2890 *
2891 * MenuDoYPosition --
2892 *
2893 * Given arguments from an option command line, returns the Y position.
2894 *
2895 * Results:
2896 * Returns TCL_OK or TCL_Error
2897 *
2898 * Side effects:
2899 * yPosition is set to the Y-position of the menu entry.
2900 *
2901 *----------------------------------------------------------------------
2902 */
2903
2904 static int
2905 MenuDoYPosition(interp, menuPtr, objPtr)
2906 Tcl_Interp *interp;
2907 TkMenu *menuPtr;
2908 Tcl_Obj *objPtr;
2909 {
2910 int index;
2911
2912 TkRecomputeMenu(menuPtr);
2913 if (TkGetMenuIndex(interp, menuPtr, objPtr, 0, &index) != TCL_OK) {
2914 goto error;
2915 }
2916 Tcl_ResetResult(interp);
2917 if (index < 0) {
2918 Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
2919 } else {
2920 Tcl_SetObjResult(interp, Tcl_NewIntObj(menuPtr->entries[index]->y));
2921 }
2922
2923 return TCL_OK;
2924
2925 error:
2926 return TCL_ERROR;
2927 }
2928
2929 /*
2930 *----------------------------------------------------------------------
2931 *
2932 * GetIndexFromCoords --
2933 *
2934 * Given a string of the form "@int", return the menu item corresponding
2935 * to int.
2936 *
2937 * Results:
2938 * If int is a valid number, *indexPtr will be the number of the menuentry
2939 * that is the correct height. If int is invaled, *indexPtr will be
2940 * unchanged. Returns appropriate Tcl error number.
2941 *
2942 * Side effects:
2943 * If int is invalid, interp's result will set to NULL.
2944 *
2945 *----------------------------------------------------------------------
2946 */
2947
2948 static int
2949 GetIndexFromCoords(interp, menuPtr, string, indexPtr)
2950 Tcl_Interp *interp; /* interp of menu */
2951 TkMenu *menuPtr; /* the menu we are searching */
2952 char *string; /* The @string we are parsing */
2953 int *indexPtr; /* The index of the item that matches */
2954 {
2955 int x, y, i;
2956 char *p, *end;
2957
2958 TkRecomputeMenu(menuPtr);
2959 p = string + 1;
2960 y = strtol(p, &end, 0);
2961 if (end == p) {
2962 goto error;
2963 }
2964 if (*end == ',') {
2965 x = y;
2966 p = end + 1;
2967 y = strtol(p, &end, 0);
2968 if (end == p) {
2969 goto error;
2970 }
2971 } else {
2972 Tk_GetPixelsFromObj(interp, menuPtr->tkwin,
2973 menuPtr->borderWidthPtr, &x);
2974 }
2975
2976 for (i = 0; i < menuPtr->numEntries; i++) {
2977 if ((x >= menuPtr->entries[i]->x) && (y >= menuPtr->entries[i]->y)
2978 && (x < (menuPtr->entries[i]->x + menuPtr->entries[i]->width))
2979 && (y < (menuPtr->entries[i]->y
2980 + menuPtr->entries[i]->height))) {
2981 break;
2982 }
2983 }
2984 if (i >= menuPtr->numEntries) {
2985 /* i = menuPtr->numEntries - 1; */
2986 i = -1;
2987 }
2988 *indexPtr = i;
2989 return TCL_OK;
2990
2991 error:
2992 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
2993 return TCL_ERROR;
2994 }
2995
2996 /*
2997 *----------------------------------------------------------------------
2998 *
2999 * RecursivelyDeleteMenu --
3000 *
3001 * Deletes a menu and any cascades underneath it. Used for deleting
3002 * instances when a menu is no longer being used as a menubar,
3003 * for instance.
3004 *
3005 * Results:
3006 * None.
3007 *
3008 * Side effects:
3009 * Destroys the menu and all cascade menus underneath it.
3010 *
3011 *----------------------------------------------------------------------
3012 */
3013
3014 static void
3015 RecursivelyDeleteMenu(menuPtr)
3016 TkMenu *menuPtr; /* The menubar instance we are deleting */
3017 {
3018 int i;
3019 TkMenuEntry *mePtr;
3020
3021 /*
3022 * It is not 100% clear that this preserve/release pair is
3023 * required, but we have added them for safety in this
3024 * very complex code.
3025 */
3026 Tcl_Preserve(menuPtr);
3027
3028 for (i = 0; i < menuPtr->numEntries; i++) {
3029 mePtr = menuPtr->entries[i];
3030 if ((mePtr->type == CASCADE_ENTRY)
3031 && (mePtr->childMenuRefPtr != NULL)
3032 && (mePtr->childMenuRefPtr->menuPtr != NULL)) {
3033 RecursivelyDeleteMenu(mePtr->childMenuRefPtr->menuPtr);
3034 }
3035 }
3036 if (menuPtr->tkwin != NULL) {
3037 Tk_DestroyWindow(menuPtr->tkwin);
3038 }
3039
3040 Tcl_Release(menuPtr);
3041 }
3042
3043 /*
3044 *----------------------------------------------------------------------
3045 *
3046 * TkNewMenuName --
3047 *
3048 * Makes a new unique name for a cloned menu. Will be a child
3049 * of oldName.
3050 *
3051 * Results:
3052 * Returns a char * which has been allocated; caller must free.
3053 *
3054 * Side effects:
3055 * Memory is allocated.
3056 *
3057 *----------------------------------------------------------------------
3058 */
3059
3060 Tcl_Obj *
3061 TkNewMenuName(interp, parentPtr, menuPtr)
3062 Tcl_Interp *interp; /* The interp the new name has to live in.*/
3063 Tcl_Obj *parentPtr; /* The prefix path of the new name. */
3064 TkMenu *menuPtr; /* The menu we are cloning. */
3065 {
3066 Tcl_Obj *resultPtr = NULL; /* Initialization needed only to prevent
3067 * compiler warning. */
3068 Tcl_Obj *childPtr;
3069 char *destString;
3070 int i;
3071 int doDot;
3072 Tcl_CmdInfo cmdInfo;
3073 Tcl_HashTable *nameTablePtr = NULL;
3074 TkWindow *winPtr = (TkWindow *) menuPtr->tkwin;
3075 char *parentName = Tcl_GetStringFromObj(parentPtr, NULL);
3076
3077 if (winPtr->mainPtr != NULL) {
3078 nameTablePtr = &(winPtr->mainPtr->nameTable);
3079 }
3080
3081 doDot = parentName[strlen(parentName) - 1] != '.';
3082
3083 childPtr = Tcl_NewStringObj(Tk_PathName(menuPtr->tkwin), -1);
3084 for (destString = Tcl_GetStringFromObj(childPtr, NULL);
3085 *destString != '\0'; destString++) {
3086 if (*destString == '.') {
3087 *destString = '#';
3088 }
3089 }
3090
3091 for (i = 0; ; i++) {
3092 if (i == 0) {
3093 resultPtr = Tcl_DuplicateObj(parentPtr);
3094 if (doDot) {
3095 Tcl_AppendToObj(resultPtr, ".", -1);
3096 }
3097 Tcl_AppendObjToObj(resultPtr, childPtr);
3098 } else {
3099 Tcl_Obj *intPtr;
3100
3101 Tcl_DecrRefCount(resultPtr);
3102 resultPtr = Tcl_DuplicateObj(parentPtr);
3103 if (doDot) {
3104 Tcl_AppendToObj(resultPtr, ".", -1);
3105 }
3106 Tcl_AppendObjToObj(resultPtr, childPtr);
3107 intPtr = Tcl_NewIntObj(i);
3108 Tcl_AppendObjToObj(resultPtr, intPtr);
3109 Tcl_DecrRefCount(intPtr);
3110 }
3111 destString = Tcl_GetStringFromObj(resultPtr, NULL);
3112 if ((Tcl_GetCommandInfo(interp, destString, &cmdInfo) == 0)
3113 && ((nameTablePtr == NULL)
3114 || (Tcl_FindHashEntry(nameTablePtr, destString) == NULL))) {
3115 break;
3116 }
3117 }
3118 Tcl_DecrRefCount(childPtr);
3119 return resultPtr;
3120 }
3121
3122 /*
3123 *----------------------------------------------------------------------
3124 *
3125 * TkSetWindowMenuBar --
3126 *
3127 * Associates a menu with a window. Called by ConfigureFrame in
3128 * in response to a "-menu .foo" configuration option for a top
3129 * level.
3130 *
3131 * Results:
3132 * None.
3133 *
3134 * Side effects:
3135 * The old menu clones for the menubar are thrown away, and a
3136 * handler is set up to allocate the new ones.
3137 *
3138 *----------------------------------------------------------------------
3139 */
3140 void
3141 TkSetWindowMenuBar(interp, tkwin, oldMenuName, menuName)
3142 Tcl_Interp *interp; /* The interpreter the toplevel lives in. */
3143 Tk_Window tkwin; /* The toplevel window */
3144 Tcl_Obj *oldMenuName; /* The name of the menubar previously set in
3145 * this toplevel. NULL means no menu was
3146 * set previously. */
3147 Tcl_Obj *menuName; /* The name of the new menubar that the
3148 * toplevel needs to be set to. NULL means
3149 * that their is no menu now. */
3150 {
3151 TkMenuTopLevelList *topLevelListPtr, *prevTopLevelPtr;
3152 TkMenu *menuPtr;
3153 TkMenuReferences *menuRefPtr;
3154
3155 TkMenuInit();
3156
3157 /*
3158 * Destroy the menubar instances of the old menu. Take this window
3159 * out of the old menu's top level reference list.
3160 */
3161
3162 if (oldMenuName != NULL) {
3163 menuRefPtr = TkFindMenuReferences(interp, Tcl_GetString(oldMenuName));
3164 if (menuRefPtr != NULL) {
3165
3166 /*
3167 * Find the menubar instance that is to be removed. Destroy
3168 * it and all of the cascades underneath it.
3169 */
3170
3171 if (menuRefPtr->menuPtr != NULL) {
3172 TkMenu *instancePtr;
3173
3174 menuPtr = menuRefPtr->menuPtr;
3175
3176 for (instancePtr = menuPtr->masterMenuPtr;
3177 instancePtr != NULL;
3178 instancePtr = instancePtr->nextInstancePtr) {
3179 if (instancePtr->menuType == MENUBAR
3180 && instancePtr->parentTopLevelPtr == tkwin) {
3181 RecursivelyDeleteMenu(instancePtr);
3182 break;
3183 }
3184 }
3185 }
3186
3187 /*
3188 * Now we need to remove this toplevel from the list of toplevels
3189 * that reference this menu.
3190 */
3191
3192 topLevelListPtr = menuRefPtr->topLevelListPtr;
3193 prevTopLevelPtr = NULL;
3194
3195 while ((topLevelListPtr != NULL)
3196 && (topLevelListPtr->tkwin != tkwin)) {
3197 prevTopLevelPtr = topLevelListPtr;
3198 topLevelListPtr = topLevelListPtr->nextPtr;
3199 }
3200
3201 /*
3202 * Now we have found the toplevel reference that matches the
3203 * tkwin; remove this reference from the list.
3204 */
3205
3206 if (topLevelListPtr != NULL) {
3207 if (prevTopLevelPtr == NULL) {
3208 menuRefPtr->topLevelListPtr =
3209 menuRefPtr->topLevelListPtr->nextPtr;
3210 } else {
3211 prevTopLevelPtr->nextPtr = topLevelListPtr->nextPtr;
3212 }
3213 ckfree((char *) topLevelListPtr);
3214 TkFreeMenuReferences(menuRefPtr);
3215 }
3216 }
3217 }
3218
3219 /*
3220 * Now, add the clone references for the new menu.
3221 */
3222
3223 if (menuName != NULL && Tcl_GetString(menuName)[0] != 0) {
3224 TkMenu *menuBarPtr = NULL;
3225
3226 menuRefPtr = TkCreateMenuReferences(interp, Tcl_GetString(menuName));
3227
3228 menuPtr = menuRefPtr->menuPtr;
3229 if (menuPtr != NULL) {
3230 Tcl_Obj *cloneMenuPtr;
3231 TkMenuReferences *cloneMenuRefPtr;
3232 Tcl_Obj *newObjv[4];
3233 Tcl_Obj *windowNamePtr = Tcl_NewStringObj(Tk_PathName(tkwin),
3234 -1);
3235 Tcl_Obj *menubarPtr = Tcl_NewStringObj("menubar", -1);
3236
3237 /*
3238 * Clone the menu and all of the cascades underneath it.
3239 */
3240
3241 Tcl_IncrRefCount(windowNamePtr);
3242 cloneMenuPtr = TkNewMenuName(interp, windowNamePtr,
3243 menuPtr);
3244 Tcl_IncrRefCount(cloneMenuPtr);
3245 Tcl_IncrRefCount(menubarPtr);
3246 CloneMenu(menuPtr, cloneMenuPtr, menubarPtr);
3247
3248 cloneMenuRefPtr = TkFindMenuReferencesObj(interp, cloneMenuPtr);
3249 if ((cloneMenuRefPtr != NULL)
3250 && (cloneMenuRefPtr->menuPtr != NULL)) {
3251 Tcl_Obj *cursorPtr = Tcl_NewStringObj("-cursor", -1);
3252 Tcl_Obj *nullPtr = Tcl_NewObj();
3253 cloneMenuRefPtr->menuPtr->parentTopLevelPtr = tkwin;
3254 menuBarPtr = cloneMenuRefPtr->menuPtr;
3255 newObjv[0] = cursorPtr;
3256 newObjv[1] = nullPtr;
3257 Tcl_IncrRefCount(cursorPtr);
3258 Tcl_IncrRefCount(nullPtr);
3259 ConfigureMenu(menuPtr->interp, cloneMenuRefPtr->menuPtr,
3260 2, newObjv);
3261 Tcl_DecrRefCount(cursorPtr);
3262 Tcl_DecrRefCount(nullPtr);
3263 }
3264
3265 TkpSetWindowMenuBar(tkwin, menuBarPtr);
3266 Tcl_DecrRefCount(cloneMenuPtr);
3267 Tcl_DecrRefCount(menubarPtr);
3268 Tcl_DecrRefCount(windowNamePtr);
3269 } else {
3270 TkpSetWindowMenuBar(tkwin, NULL);
3271 }
3272
3273
3274 /*
3275 * Add this window to the menu's list of windows that refer
3276 * to this menu.
3277 */
3278
3279 topLevelListPtr = (TkMenuTopLevelList *)
3280 ckalloc(sizeof(TkMenuTopLevelList));
3281 topLevelListPtr->tkwin = tkwin;
3282 topLevelListPtr->nextPtr = menuRefPtr->topLevelListPtr;
3283 menuRefPtr->topLevelListPtr = topLevelListPtr;
3284 } else {
3285 TkpSetWindowMenuBar(tkwin, NULL);
3286 }
3287 TkpSetMainMenubar(interp, tkwin, Tcl_GetString(menuName));
3288 }
3289
3290 /*
3291 *----------------------------------------------------------------------
3292 *
3293 * DestroyMenuHashTable --
3294 *
3295 * Called when an interp is deleted and a menu hash table has
3296 * been set in it.
3297 *
3298 * Results:
3299 * None.
3300 *
3301 * Side effects:
3302 * The hash table is destroyed.
3303 *
3304 *----------------------------------------------------------------------
3305 */
3306
3307 static void
3308 DestroyMenuHashTable(clientData, interp)
3309 ClientData clientData; /* The menu hash table we are destroying */
3310 Tcl_Interp *interp; /* The interpreter we are destroying */
3311 {
3312 Tcl_DeleteHashTable((Tcl_HashTable *) clientData);
3313 ckfree((char *) clientData);
3314 }
3315
3316 /*
3317 *----------------------------------------------------------------------
3318 *
3319 * TkGetMenuHashTable --
3320 *
3321 * For a given interp, give back the menu hash table that goes with
3322 * it. If the hash table does not exist, it is created.
3323 *
3324 * Results:
3325 * Returns a hash table pointer.
3326 *
3327 * Side effects:
3328 * A new hash table is created if there were no table in the interp
3329 * originally.
3330 *
3331 *----------------------------------------------------------------------
3332 */
3333
3334 Tcl_HashTable *
3335 TkGetMenuHashTable(interp)
3336 Tcl_Interp *interp; /* The interp we need the hash table in.*/
3337 {
3338 Tcl_HashTable *menuTablePtr;
3339
3340 menuTablePtr = (Tcl_HashTable *) Tcl_GetAssocData(interp, MENU_HASH_KEY,
3341 NULL);
3342 if (menuTablePtr == NULL) {
3343 menuTablePtr = (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
3344 Tcl_InitHashTable(menuTablePtr, TCL_STRING_KEYS);
3345 Tcl_SetAssocData(interp, MENU_HASH_KEY, DestroyMenuHashTable,
3346 (ClientData) menuTablePtr);
3347 }
3348 return menuTablePtr;
3349 }
3350
3351 /*
3352 *----------------------------------------------------------------------
3353 *
3354 * TkCreateMenuReferences --
3355 *
3356 * Given a pathname, gives back a pointer to a TkMenuReferences structure.
3357 * If a reference is not already in the hash table, one is created.
3358 *
3359 * Results:
3360 * Returns a pointer to a menu reference structure. Should not
3361 * be freed by calller; when a field of the reference is cleared,
3362 * TkFreeMenuReferences should be called.
3363 *
3364 * Side effects:
3365 * A new hash table entry is created if there were no references
3366 * to the menu originally.
3367 *
3368 *----------------------------------------------------------------------
3369 */
3370
3371 TkMenuReferences *
3372 TkCreateMenuReferences(interp, pathName)
3373 Tcl_Interp *interp;
3374 char *pathName; /* The path of the menu widget */
3375 {
3376 Tcl_HashEntry *hashEntryPtr;
3377 TkMenuReferences *menuRefPtr;
3378 int newEntry;
3379 Tcl_HashTable *menuTablePtr = TkGetMenuHashTable(interp);
3380
3381 hashEntryPtr = Tcl_CreateHashEntry(menuTablePtr, pathName, &newEntry);
3382 if (newEntry) {
3383 menuRefPtr = (TkMenuReferences *) ckalloc(sizeof(TkMenuReferences));
3384 menuRefPtr->menuPtr = NULL;
3385 menuRefPtr->topLevelListPtr = NULL;
3386 menuRefPtr->parentEntryPtr = NULL;
3387 menuRefPtr->hashEntryPtr = hashEntryPtr;
3388 Tcl_SetHashValue(hashEntryPtr, (char *) menuRefPtr);
3389 } else {
3390 menuRefPtr = (TkMenuReferences *) Tcl_GetHashValue(hashEntryPtr);
3391 }
3392 return menuRefPtr;
3393 }
3394
3395 /*
3396 *----------------------------------------------------------------------
3397 *
3398 * TkFindMenuReferences --
3399 *
3400 * Given a pathname, gives back a pointer to the TkMenuReferences
3401 * structure.
3402 *
3403 * Results:
3404 * Returns a pointer to a menu reference structure. Should not
3405 * be freed by calller; when a field of the reference is cleared,
3406 * TkFreeMenuReferences should be called. Returns NULL if no reference
3407 * with this pathname exists.
3408 *
3409 * Side effects:
3410 * None.
3411 *
3412 *----------------------------------------------------------------------
3413 */
3414
3415 TkMenuReferences *
3416 TkFindMenuReferences(interp, pathName)
3417 Tcl_Interp *interp; /* The interp the menu is living in. */
3418 char *pathName; /* The path of the menu widget */
3419 {
3420 Tcl_HashEntry *hashEntryPtr;
3421 TkMenuReferences *menuRefPtr = NULL;
3422 Tcl_HashTable *menuTablePtr;
3423
3424 menuTablePtr = TkGetMenuHashTable(interp);
3425 hashEntryPtr = Tcl_FindHashEntry(menuTablePtr, pathName);
3426 if (hashEntryPtr != NULL) {
3427 menuRefPtr = (TkMenuReferences *) Tcl_GetHashValue(hashEntryPtr);
3428 }
3429 return menuRefPtr;
3430 }
3431
3432 /*
3433 *----------------------------------------------------------------------
3434 *
3435 * TkFindMenuReferencesObj --
3436 *
3437 * Given a pathname, gives back a pointer to the TkMenuReferences
3438 * structure.
3439 *
3440 * Results:
3441 * Returns a pointer to a menu reference structure. Should not
3442 * be freed by calller; when a field of the reference is cleared,
3443 * TkFreeMenuReferences should be called. Returns NULL if no reference
3444 * with this pathname exists.
3445 *
3446 * Side effects:
3447 * None.
3448 *
3449 *----------------------------------------------------------------------
3450 */
3451
3452 TkMenuReferences *
3453 TkFindMenuReferencesObj(interp, objPtr)
3454 Tcl_Interp *interp; /* The interp the menu is living in. */
3455 Tcl_Obj *objPtr; /* The path of the menu widget */
3456 {
3457 char *pathName = Tcl_GetStringFromObj(objPtr, NULL);
3458 return TkFindMenuReferences(interp, pathName);
3459 }
3460
3461 /*
3462 *----------------------------------------------------------------------
3463 *
3464 * TkFreeMenuReferences --
3465 *
3466 * This is called after one of the fields in a menu reference
3467 * is cleared. It cleans up the ref if it is now empty.
3468 *
3469 * Results:
3470 * Returns 1 if the references structure was freed, and 0
3471 * otherwise.
3472 *
3473 * Side effects:
3474 * If this is the last field to be cleared, the menu ref is
3475 * taken out of the hash table.
3476 *
3477 *----------------------------------------------------------------------
3478 */
3479
3480 int
3481 TkFreeMenuReferences(menuRefPtr)
3482 TkMenuReferences *menuRefPtr; /* The menu reference to
3483 * free */
3484 {
3485 if ((menuRefPtr->menuPtr == NULL)
3486 && (menuRefPtr->parentEntryPtr == NULL)
3487 && (menuRefPtr->topLevelListPtr == NULL)) {
3488 Tcl_DeleteHashEntry(menuRefPtr->hashEntryPtr);
3489 ckfree((char *) menuRefPtr);
3490 return 1;
3491 }
3492 return 0;
3493 }
3494
3495 /*
3496 *----------------------------------------------------------------------
3497 *
3498 * DeleteMenuCloneEntries --
3499 *
3500 * For every clone in this clone chain, delete the menu entries
3501 * given by the parameters.
3502 *
3503 * Results:
3504 * None.
3505 *
3506 * Side effects:
3507 * The appropriate entries are deleted from all clones of this menu.
3508 *
3509 *----------------------------------------------------------------------
3510 */
3511
3512 static void
3513 DeleteMenuCloneEntries(menuPtr, first, last)
3514 TkMenu *menuPtr; /* the menu the command was issued with */
3515 int first; /* the zero-based first entry in the set
3516 * of entries to delete. */
3517 int last; /* the zero-based last entry */
3518 {
3519
3520 TkMenu *menuListPtr;
3521 int numDeleted, i;
3522
3523 numDeleted = last + 1 - first;
3524 for (menuListPtr = menuPtr->masterMenuPtr; menuListPtr != NULL;
3525 menuListPtr = menuListPtr->nextInstancePtr) {
3526 for (i = last; i >= first; i--) {
3527 Tcl_EventuallyFree((ClientData) menuListPtr->entries[i],
3528 DestroyMenuEntry);
3529 }
3530 for (i = last + 1; i < menuListPtr->numEntries; i++) {
3531 menuListPtr->entries[i - numDeleted] = menuListPtr->entries[i];
3532 menuListPtr->entries[i - numDeleted]->index = i - numDeleted;
3533 }
3534 menuListPtr->numEntries -= numDeleted;
3535 if (menuListPtr->numEntries == 0) {
3536 ckfree((char *) menuListPtr->entries);
3537 menuListPtr->entries = NULL;
3538 }
3539 if ((menuListPtr->active >= first)
3540 && (menuListPtr->active <= last)) {
3541 menuListPtr->active = -1;
3542 } else if (menuListPtr->active > last) {
3543 menuListPtr->active -= numDeleted;
3544 }
3545 TkEventuallyRecomputeMenu(menuListPtr);
3546 }
3547 }
3548
3549 /*
3550 *----------------------------------------------------------------------
3551 *
3552 * TkMenuCleanup --
3553 *
3554 * Resets menusInitialized to allow Tk to be finalized and reused
3555 * without the DLL being unloaded.
3556 *
3557 * Results:
3558 * None.
3559 *
3560 * Side effects:
3561 * None.
3562 *
3563 *----------------------------------------------------------------------
3564 */
3565
3566 static void
3567 TkMenuCleanup(ClientData unused)
3568 {
3569 menusInitialized = 0;
3570 }
3571
3572 /*
3573 *----------------------------------------------------------------------
3574 *
3575 * TkMenuInit --
3576 *
3577 * Sets up the hash tables and the variables used by the menu package.
3578 *
3579 * Results:
3580 * None.
3581 *
3582 * Side effects:
3583 * lastMenuID gets initialized, and the parent hash and the command hash
3584 * are allocated.
3585 *
3586 *----------------------------------------------------------------------
3587 */
3588
3589 void
3590 TkMenuInit()
3591 {
3592 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
3593 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
3594
3595 if (!menusInitialized) {
3596 Tcl_MutexLock(&menuMutex);
3597 if (!menusInitialized) {
3598 TkpMenuInit();
3599 menusInitialized = 1;
3600 }
3601 /*
3602 * Make sure we cleanup on finalize.
3603 */
3604 Tcl_CreateExitHandler((Tcl_ExitProc *) TkMenuCleanup, NULL);
3605 Tcl_MutexUnlock(&menuMutex);
3606 }
3607 if (!tsdPtr->menusInitialized) {
3608 TkpMenuThreadInit();
3609 tsdPtr->menusInitialized = 1;
3610 }
3611 }
3612
3613