Debian Codegraph @

pTk/mTk/win/tkWinMenu.c

raw
1 /*
2 * tkWinMenu.c --
3 *
4 * This module implements the Windows platform-specific features of menus.
5 *
6 * Copyright (c) 1996-1998 by Sun Microsystems, Inc.
7 * Copyright (c) 1998-1999 by Scriptics Corporation.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkWinMenu.c,v 1.21 2002/08/08 01:42:57 hobbs Exp $
13 */
14
15 #define OEMRESOURCE
16 #include "tkWinInt.h"
17 /* Mingw32 has not caught up yet with MT_* yet ... */
18 #ifndef MT_RIGHTJUSTIFY
19 #define MT_RIGHTJUSTIFY MF_HELP
20 #endif
21 #include "tkMenu.h"
22 #include <string.h>
23
24 /*
25 * The class of the window for popup menus.
26 */
27
28 #define MENU_CLASS_NAME "MenuWindowClass"
29
30 /*
31 * Used to align a windows bitmap inside a rectangle
32 */
33
34 #define ALIGN_BITMAP_LEFT 0x00000001
35 #define ALIGN_BITMAP_RIGHT 0x00000002
36 #define ALIGN_BITMAP_TOP 0x00000004
37 #define ALIGN_BITMAP_BOTTOM 0x00000008
38
39 /*
40 * Platform-specific menu flags:
41 *
42 * MENU_SYSTEM_MENU Non-zero means that the Windows menu handle
43 * was retrieved with GetSystemMenu and needs
44 * to be disposed of specially.
45 * MENU_RECONFIGURE_PENDING
46 * Non-zero means that an idle handler has
47 * been set up to reconfigure the Windows menu
48 * handle for this menu.
49 */
50
51 #define MENU_SYSTEM_MENU MENU_PLATFORM_FLAG1
52 #define MENU_RECONFIGURE_PENDING MENU_PLATFORM_FLAG2
53
54 static int indicatorDimensions[2];
55 /* The dimensions of the indicator space
56 * in a menu entry. Calculated at init
57 * time to save time. */
58
59 typedef struct ThreadSpecificData {
60 Tcl_HashTable commandTable;
61 /* A map of command ids to menu entries */
62 int inPostMenu; /* We cannot be re-entrant like X Windows. */
63 WORD lastCommandID; /* The last command ID we allocated. */
64 HWND menuHWND; /* A window to service popup-menu messages
65 * in. */
66 int oldServiceMode; /* Used while processing a menu; we need
67 * to set the event mode specially when we
68 * enter the menu processing modal loop
69 * and reset it when menus go away. */
70 TkMenu *modalMenuPtr; /* The menu we are processing inside the modal
71 * loop. We need this to reset all of the
72 * active items when menus go away since
73 * Windows does not see fit to give this
74 * to us when it sends its WM_MENUSELECT. */
75 Tcl_HashTable winMenuTable;
76 /* Need this to map HMENUs back to menuPtrs */
77 } ThreadSpecificData;
78 static Tcl_ThreadDataKey dataKey;
79
80 /*
81 * The following are default menu value strings.
82 */
83
84 static int defaultBorderWidth; /* The windows default border width. */
85 static Tcl_DString menuFontDString;
86 /* A buffer to store the default menu font
87 * string. */
88 /*
89 * Forward declarations for procedures defined later in this file:
90 */
91
92 static void DrawMenuEntryAccelerator _ANSI_ARGS_((
93 TkMenu *menuPtr, TkMenuEntry *mePtr,
94 Drawable d, GC gc, Tk_Font tkfont,
95 CONST Tk_FontMetrics *fmPtr,
96 Tk_3DBorder activeBorder, int x, int y,
97 int width, int height, int drawArrow));
98 static void DrawMenuEntryBackground _ANSI_ARGS_((
99 TkMenu *menuPtr, TkMenuEntry *mePtr,
100 Drawable d, Tk_3DBorder activeBorder,
101 Tk_3DBorder bgBorder, int x, int y,
102 int width, int heigth));
103 static void DrawMenuEntryIndicator _ANSI_ARGS_((
104 TkMenu *menuPtr, TkMenuEntry *mePtr,
105 Drawable d, GC gc, GC indicatorGC,
106 Tk_Font tkfont,
107 CONST Tk_FontMetrics *fmPtr, int x, int y,
108 int width, int height));
109 static void DrawMenuEntryLabel _ANSI_ARGS_((
110 TkMenu * menuPtr, TkMenuEntry *mePtr, Drawable d,
111 GC gc, Tk_Font tkfont,
112 CONST Tk_FontMetrics *fmPtr, int x, int y,
113 int width, int height));
114 static void DrawMenuSeparator _ANSI_ARGS_((TkMenu *menuPtr,
115 TkMenuEntry *mePtr, Drawable d, GC gc,
116 Tk_Font tkfont, CONST Tk_FontMetrics *fmPtr,
117 int x, int y, int width, int height));
118 static void DrawTearoffEntry _ANSI_ARGS_((TkMenu *menuPtr,
119 TkMenuEntry *mePtr, Drawable d, GC gc,
120 Tk_Font tkfont, CONST Tk_FontMetrics *fmPtr,
121 int x, int y, int width, int height));
122 static void DrawMenuUnderline _ANSI_ARGS_((TkMenu *menuPtr,
123 TkMenuEntry *mePtr, Drawable d, GC gc,
124 Tk_Font tkfont, CONST Tk_FontMetrics *fmPtr, int x,
125 int y, int width, int height));
126 static void DrawWindowsSystemBitmap _ANSI_ARGS_((
127 Display *display, Drawable drawable,
128 GC gc, CONST RECT *rectPtr, int bitmapID,
129 int alignFlags));
130 static void FreeID _ANSI_ARGS_((int commandID));
131 static TCHAR * GetEntryText _ANSI_ARGS_((TkMenuEntry *mePtr));
132 static void GetMenuAccelGeometry _ANSI_ARGS_((TkMenu *menuPtr,
133 TkMenuEntry *mePtr, Tk_Font tkfont,
134 CONST Tk_FontMetrics *fmPtr, int *widthPtr,
135 int *heightPtr));
136 static void GetMenuLabelGeometry _ANSI_ARGS_((TkMenuEntry *mePtr,
137 Tk_Font tkfont, CONST Tk_FontMetrics *fmPtr,
138 int *widthPtr, int *heightPtr));
139 static void GetMenuIndicatorGeometry _ANSI_ARGS_((
140 TkMenu *menuPtr, TkMenuEntry *mePtr,
141 Tk_Font tkfont, CONST Tk_FontMetrics *fmPtr,
142 int *widthPtr, int *heightPtr));
143 static void GetMenuSeparatorGeometry _ANSI_ARGS_((
144 TkMenu *menuPtr, TkMenuEntry *mePtr,
145 Tk_Font tkfont, CONST Tk_FontMetrics *fmPtr,
146 int *widthPtr, int *heightPtr));
147 static void GetTearoffEntryGeometry _ANSI_ARGS_((TkMenu *menuPtr,
148 TkMenuEntry *mePtr, Tk_Font tkfont,
149 CONST Tk_FontMetrics *fmPtr, int *widthPtr,
150 int *heightPtr));
151 static int GetNewID _ANSI_ARGS_((TkMenuEntry *mePtr,
152 int *menuIDPtr));
153 static int MenuKeyBindProc _ANSI_ARGS_((
154 ClientData clientData,
155 Tcl_Interp *interp, XEvent *eventPtr,
156 Tk_Window tkwin, KeySym keySym));
157 static void MenuSelectEvent _ANSI_ARGS_((TkMenu *menuPtr));
158 static void ReconfigureWindowsMenu _ANSI_ARGS_((
159 ClientData clientData));
160 static void RecursivelyClearActiveMenu _ANSI_ARGS_((
161 TkMenu *menuPtr));
162 static void SetDefaults _ANSI_ARGS_((int firstTime));
163 static LRESULT CALLBACK TkWinMenuProc _ANSI_ARGS_((HWND hwnd,
164 UINT message, WPARAM wParam,
165 LPARAM lParam));
166
167
168
169 /*
170 *----------------------------------------------------------------------
171 *
172 * GetNewID --
173 *
174 * Allocates a new menu id and marks it in use.
175 *
176 * Results:
177 * Returns TCL_OK if succesful; TCL_ERROR if there are no more
178 * ids of the appropriate type to allocate. menuIDPtr contains
179 * the new id if succesful.
180 *
181 * Side effects:
182 * An entry is created for the menu in the command hash table,
183 * and the hash entry is stored in the appropriate field in the
184 * menu data structure.
185 *
186 *----------------------------------------------------------------------
187 */
188
189 static int
190 GetNewID(mePtr, menuIDPtr)
191 TkMenuEntry *mePtr; /* The menu we are working with */
192 int *menuIDPtr; /* The resulting id */
193 {
194 int found = 0;
195 int newEntry;
196 Tcl_HashEntry *commandEntryPtr;
197 WORD returnID;
198 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
199 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
200
201 WORD curID = tsdPtr->lastCommandID + 1;
202
203 /*
204 * The following code relies on WORD wrapping when the highest value is
205 * incremented.
206 */
207
208 while (curID != tsdPtr->lastCommandID) {
209 commandEntryPtr = Tcl_CreateHashEntry(&tsdPtr->commandTable,
210 (char *) curID, &newEntry);
211 if (newEntry == 1) {
212 found = 1;
213 returnID = curID;
214 break;
215 }
216 curID++;
217 }
218
219 if (found) {
220 Tcl_SetHashValue(commandEntryPtr, (char *) mePtr);
221 *menuIDPtr = (int) returnID;
222 tsdPtr->lastCommandID = returnID;
223 return TCL_OK;
224 } else {
225 return TCL_ERROR;
226 }
227 }
228
229 /*
230 *----------------------------------------------------------------------
231 *
232 * FreeID --
233 *
234 * Marks the itemID as free.
235 *
236 * Results:
237 * None.
238 *
239 * Side effects:
240 * The hash table entry for the ID is cleared.
241 *
242 *----------------------------------------------------------------------
243 */
244
245 static void
246 FreeID(commandID)
247 int commandID;
248 {
249 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
250 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
251
252 Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(&tsdPtr->commandTable,
253 (char *) commandID);
254
255 if (entryPtr != NULL) {
256 Tcl_DeleteHashEntry(entryPtr);
257 }
258 }
259
260 /*
261 *----------------------------------------------------------------------
262 *
263 * TkpNewMenu --
264 *
265 * Gets a new blank menu. Only the platform specific options are filled
266 * in.
267 *
268 * Results:
269 * Standard TCL error.
270 *
271 * Side effects:
272 * Allocates a Windows menu handle and places it in the platformData
273 * field of the menuPtr.
274 *
275 *----------------------------------------------------------------------
276 */
277
278 int
279 TkpNewMenu(menuPtr)
280 TkMenu *menuPtr; /* The common structure we are making the
281 * platform structure for. */
282 {
283 HMENU winMenuHdl;
284 Tcl_HashEntry *hashEntryPtr;
285 int newEntry;
286 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
287 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
288
289 winMenuHdl = CreatePopupMenu();
290
291 if (winMenuHdl == NULL) {
292 Tcl_AppendResult(menuPtr->interp, "No more menus can be allocated.",
293 (char *) NULL);
294 return TCL_ERROR;
295 }
296
297 /*
298 * We hash all of the HMENU's so that we can get their menu ptrs
299 * back when dispatch messages.
300 */
301
302 hashEntryPtr = Tcl_CreateHashEntry(&tsdPtr->winMenuTable, (char *) winMenuHdl,
303 &newEntry);
304 Tcl_SetHashValue(hashEntryPtr, (char *) menuPtr);
305
306 menuPtr->platformData = (TkMenuPlatformData) winMenuHdl;
307 return TCL_OK;
308 }
309
310 /*
311 *----------------------------------------------------------------------
312 *
313 * TkpDestroyMenu --
314 *
315 * Destroys platform-specific menu structures.
316 *
317 * Results:
318 * None.
319 *
320 * Side effects:
321 * All platform-specific allocations are freed up.
322 *
323 *----------------------------------------------------------------------
324 */
325
326 void
327 TkpDestroyMenu(menuPtr)
328 TkMenu *menuPtr; /* The common menu structure */
329 {
330 HMENU winMenuHdl = (HMENU) menuPtr->platformData;
331 char *searchName;
332 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
333 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
334
335 if (menuPtr->menuFlags & MENU_RECONFIGURE_PENDING) {
336 Tcl_CancelIdleCall(ReconfigureWindowsMenu, (ClientData) menuPtr);
337 }
338
339 if (winMenuHdl == NULL) {
340 return;
341 }
342
343 if (menuPtr->menuFlags & MENU_SYSTEM_MENU) {
344 TkMenuEntry *searchEntryPtr;
345 Tcl_HashTable *tablePtr = TkGetMenuHashTable(menuPtr->interp);
346 char *menuName = Tcl_GetHashKey(tablePtr,
347 menuPtr->menuRefPtr->hashEntryPtr);
348
349 /*
350 * Search for the menu in the menubar, if it is present, get the
351 * wrapper window associated with the toplevel and reset its
352 * system menu to the default menu.
353 */
354
355 for (searchEntryPtr = menuPtr->menuRefPtr->parentEntryPtr;
356 searchEntryPtr != NULL;
357 searchEntryPtr = searchEntryPtr->nextCascadePtr) {
358 searchName = Tcl_GetStringFromObj(searchEntryPtr->namePtr, NULL);
359 if (strcmp(searchName, menuName) == 0) {
360 Tk_Window parentTopLevelPtr = searchEntryPtr
361 ->menuPtr->parentTopLevelPtr;
362 if (parentTopLevelPtr != NULL) {
363 GetSystemMenu(TkWinGetWrapperWindow(parentTopLevelPtr),
364 TRUE);
365 }
366 break;
367 }
368 }
369 } else {
370 Tcl_HashEntry *hashEntryPtr;
371
372 /*
373 * Remove the menu from the menu hash table, then destroy the handle.
374 */
375
376 hashEntryPtr = Tcl_FindHashEntry(&tsdPtr->winMenuTable,
377 (char *) winMenuHdl);
378 if (hashEntryPtr != NULL) {
379 Tcl_DeleteHashEntry(hashEntryPtr);
380 }
381 DestroyMenu(winMenuHdl);
382 }
383 menuPtr->platformData = NULL;
384
385 if (menuPtr == tsdPtr->modalMenuPtr) {
386 tsdPtr->modalMenuPtr = NULL;
387 }
388 }
389
390 /*
391 *----------------------------------------------------------------------
392 *
393 * TkpDestroyMenuEntry --
394 *
395 * Cleans up platform-specific menu entry items.
396 *
397 * Results:
398 * None
399 *
400 * Side effects:
401 * All platform-specific allocations are freed up.
402 *
403 *----------------------------------------------------------------------
404 */
405
406 void
407 TkpDestroyMenuEntry(mePtr)
408 TkMenuEntry *mePtr; /* The entry to destroy */
409 {
410 TkMenu *menuPtr = mePtr->menuPtr;
411 HMENU winMenuHdl = (HMENU) menuPtr->platformData;
412
413 if (NULL != winMenuHdl) {
414 if (!(menuPtr->menuFlags & MENU_RECONFIGURE_PENDING)) {
415 menuPtr->menuFlags |= MENU_RECONFIGURE_PENDING;
416 Tcl_DoWhenIdle(ReconfigureWindowsMenu, (ClientData) menuPtr);
417 }
418 }
419 FreeID((int) mePtr->platformEntryData);
420 mePtr->platformEntryData = NULL;
421 }
422
423 /*
424 *----------------------------------------------------------------------
425 *
426 * GetEntryText --
427 *
428 * Given a menu entry, gives back the text that should go in it.
429 * Separators should be done by the caller, as they have to be
430 * handled specially. Allocates the memory with alloc. The caller
431 * should free the memory.
432 *
433 * Results:
434 * itemText points to the new text for the item.
435 *
436 * Side effects:
437 * None.
438 *
439 *----------------------------------------------------------------------
440 */
441
442 static char *
443 GetEntryText(mePtr)
444 TkMenuEntry *mePtr; /* A pointer to the menu entry. */
445 {
446 char *itemText;
447
448 if (mePtr->type == TEAROFF_ENTRY) {
449 itemText = ckalloc(sizeof("(Tear-off)"));
450 strcpy(itemText, "(Tear-off)");
451 } else if (mePtr->imagePtr != NULL) {
452 itemText = ckalloc(sizeof("(Image)"));
453 strcpy(itemText, "(Image)");
454 } else if (mePtr->bitmapPtr != NULL) {
455 itemText = ckalloc(sizeof("(Pixmap)"));
456 strcpy(itemText, "(Pixmap)");
457 } else if (mePtr->labelPtr == NULL || mePtr->labelLength == 0) {
458 itemText = ckalloc(sizeof("( )"));
459 strcpy(itemText, "( )");
460 } else {
461 int i;
462 char *label = (mePtr->labelPtr == NULL) ? ""
463 : Tcl_GetStringFromObj(mePtr->labelPtr, NULL);
464 char *accel = (mePtr->accelPtr == NULL) ? ""
465 : Tcl_GetStringFromObj(mePtr->accelPtr, NULL);
466 CONST char *p, *next;
467 Tcl_DString itemString;
468
469 /*
470 * We have to construct the string with an ampersand
471 * preceeding the underline character, and a tab seperating
472 * the text and the accel text. We have to be careful with
473 * ampersands in the string.
474 */
475
476 Tcl_DStringInit(&itemString);
477
478 for (p = label, i = 0; *p != '\0'; i++, p = next) {
479 if (i == mePtr->underline) {
480 Tcl_DStringAppend(&itemString, "&", 1);
481 }
482 if (*p == '&') {
483 Tcl_DStringAppend(&itemString, "&", 1);
484 }
485 next = Tcl_UtfNext(p);
486 Tcl_DStringAppend(&itemString, p, (int) (next - p));
487 }
488 if (mePtr->accelLength > 0) {
489 Tcl_DStringAppend(&itemString, "\t", 1);
490 for (p = accel, i = 0; *p != '\0'; i++, p = next) {
491 if (*p == '&') {
492 Tcl_DStringAppend(&itemString, "&", 1);
493 }
494 next = Tcl_UtfNext(p);
495 Tcl_DStringAppend(&itemString, p, (int) (next - p));
496 }
497 }
498
499 itemText = ckalloc(Tcl_DStringLength(&itemString) + 1);
500 strcpy(itemText, Tcl_DStringValue(&itemString));
501 Tcl_DStringFree(&itemString);
502 }
503 return itemText;
504 }
505
506 /*
507 *----------------------------------------------------------------------
508 *
509 * ReconfigureWindowsMenu --
510 *
511 * Tears down and rebuilds the platform-specific part of this menu.
512 *
513 * Results:
514 * None.
515 *
516 * Side effects:
517 * Configuration information get set for mePtr; old resources
518 * get freed, if any need it.
519 *
520 *----------------------------------------------------------------------
521 */
522
523 static void
524 ReconfigureWindowsMenu(
525 ClientData clientData) /* The menu we are rebuilding */
526 {
527 TkMenu *menuPtr = (TkMenu *) clientData;
528 TkMenuEntry *mePtr;
529 HMENU winMenuHdl = (HMENU) menuPtr->platformData;
530 TCHAR *itemText = NULL;
531 const TCHAR *lpNewItem;
532 UINT flags;
533 UINT itemID;
534 int i, count, systemMenu = 0, base;
535 int width, height;
536 Tcl_DString translatedText;
537
538 if (NULL == winMenuHdl) {
539 return;
540 }
541
542 /*
543 * Reconstruct the entire menu. Takes care of nasty system menu and index
544 * problem.
545 *
546 */
547
548 if ((menuPtr->menuType == MENUBAR)
549 && (menuPtr->parentTopLevelPtr != NULL)) {
550 width = Tk_Width(menuPtr->parentTopLevelPtr);
551 height = Tk_Height(menuPtr->parentTopLevelPtr);
552 }
553
554 base = (menuPtr->menuFlags & MENU_SYSTEM_MENU) ? 7 : 0;
555 count = GetMenuItemCount(winMenuHdl);
556 for (i = base; i < count; i++) {
557 RemoveMenu(winMenuHdl, base, MF_BYPOSITION);
558 }
559
560 count = menuPtr->numEntries;
561 for (i = 0; i < count; i++) {
562 mePtr = menuPtr->entries[i];
563 lpNewItem = NULL;
564 flags = MF_BYPOSITION;
565 itemID = 0;
566 Tcl_DStringInit(&translatedText);
567
568 if ((menuPtr->menuType == MENUBAR) && (mePtr->type == TEAROFF_ENTRY)) {
569 continue;
570 }
571
572 itemText = GetEntryText(mePtr);
573 if ((menuPtr->menuType == MENUBAR)
574 || (menuPtr->menuFlags & MENU_SYSTEM_MENU)) {
575 Tcl_WinUtfToTChar(itemText, -1, &translatedText);
576 lpNewItem = Tcl_DStringValue(&translatedText);
577 } else {
578 lpNewItem = (LPCTSTR) mePtr;
579 flags |= MF_OWNERDRAW;
580 }
581
582 /*
583 * Set enabling and disabling correctly.
584 */
585
586 if (mePtr->state == ENTRY_DISABLED) {
587 flags |= MF_DISABLED | MF_GRAYED;
588 }
589
590 /*
591 * Set the check mark for check entries and radio entries.
592 */
593
594 if (((mePtr->type == CHECK_BUTTON_ENTRY)
595 || (mePtr->type == RADIO_BUTTON_ENTRY))
596 && (mePtr->entryFlags & ENTRY_SELECTED)) {
597 flags |= MF_CHECKED;
598 }
599
600 /*
601 * Set the SEPARATOR bit for separator entries. This bit is not
602 * used by our internal drawing functions, but it is used by the
603 * system when drawing the system menu (we do not draw the system menu
604 * ourselves). If this bit is not set, separator entries on the system
605 * menu will not be drawn correctly.
606 */
607
608 if (mePtr->type == SEPARATOR_ENTRY) {
609 flags |= MF_SEPARATOR;
610 }
611
612 if (mePtr->columnBreak) {
613 flags |= MF_MENUBREAK;
614 }
615
616 itemID = (int) mePtr->platformEntryData;
617 if ((mePtr->type == CASCADE_ENTRY)
618 && (mePtr->childMenuRefPtr != NULL)
619 && (mePtr->childMenuRefPtr->menuPtr != NULL)) {
620 HMENU childMenuHdl = (HMENU) mePtr->childMenuRefPtr->menuPtr
621 ->platformData;
622 if (childMenuHdl != NULL) {
623 itemID = (UINT) childMenuHdl;
624 flags |= MF_POPUP;
625 }
626 if ((menuPtr->menuType == MENUBAR)
627 && !(mePtr->childMenuRefPtr->menuPtr->menuFlags
628 & MENU_SYSTEM_MENU)) {
629 Tcl_DString ds;
630 TkMenuReferences *menuRefPtr;
631 TkMenu *systemMenuPtr = mePtr->childMenuRefPtr->menuPtr;
632
633 Tcl_DStringInit(&ds);
634 Tcl_DStringAppend(&ds,
635 Tk_PathName(menuPtr->masterMenuPtr->tkwin), -1);
636 Tcl_DStringAppend(&ds, ".system", 7);
637
638 menuRefPtr = TkFindMenuReferences(menuPtr->interp,
639 Tcl_DStringValue(&ds));
640
641 Tcl_DStringFree(&ds);
642
643 if ((menuRefPtr != NULL)
644 && (menuRefPtr->menuPtr != NULL)
645 && (menuPtr->parentTopLevelPtr != NULL)
646 && (systemMenuPtr->masterMenuPtr
647 == menuRefPtr->menuPtr)) {
648 HMENU systemMenuHdl =
649 (HMENU) systemMenuPtr->platformData;
650 HWND wrapper = TkWinGetWrapperWindow(menuPtr
651 ->parentTopLevelPtr);
652 if (wrapper != NULL) {
653 DestroyMenu(systemMenuHdl);
654 systemMenuHdl = GetSystemMenu(wrapper, FALSE);
655 systemMenuPtr->menuFlags |= MENU_SYSTEM_MENU;
656 systemMenuPtr->platformData =
657 (TkMenuPlatformData) systemMenuHdl;
658 if (!(systemMenuPtr->menuFlags
659 & MENU_RECONFIGURE_PENDING)) {
660 systemMenuPtr->menuFlags
661 |= MENU_RECONFIGURE_PENDING;
662 Tcl_DoWhenIdle(ReconfigureWindowsMenu,
663 (ClientData) systemMenuPtr);
664 }
665 }
666 }
667 }
668 if (mePtr->childMenuRefPtr->menuPtr->menuFlags
669 & MENU_SYSTEM_MENU) {
670 systemMenu++;
671 }
672 }
673 if (!systemMenu) {
674 (*tkWinProcs->insertMenu)(winMenuHdl, 0xFFFFFFFF, flags,
675 itemID, lpNewItem);
676 }
677 Tcl_DStringFree(&translatedText);
678 if (itemText != NULL) {
679 ckfree(itemText);
680 itemText = NULL;
681 }
682 }
683
684
685 if ((menuPtr->menuType == MENUBAR)
686 && (menuPtr->parentTopLevelPtr != NULL)) {
687 DrawMenuBar(TkWinGetWrapperWindow(menuPtr->parentTopLevelPtr));
688 Tk_GeometryRequest(menuPtr->parentTopLevelPtr, width, height);
689 }
690
691 menuPtr->menuFlags &= ~(MENU_RECONFIGURE_PENDING);
692 }
693
694 /*
695 *----------------------------------------------------------------------
696 *
697 * TkpPostMenu --
698 *
699 * Posts a menu on the screen
700 *
701 * Results:
702 * None.
703 *
704 * Side effects:
705 * The menu is posted and handled.
706 *
707 *----------------------------------------------------------------------
708 */
709
710 int
711 TkpPostMenu(interp, menuPtr, x, y)
712 Tcl_Interp *interp;
713 TkMenu *menuPtr;
714 int x;
715 int y;
716 {
717 HMENU winMenuHdl = (HMENU) menuPtr->platformData;
718 int result, flags;
719 RECT noGoawayRect;
720 POINT point;
721 Tk_Window parentWindow = Tk_Parent(menuPtr->tkwin);
722 int oldServiceMode = Tcl_GetServiceMode();
723 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
724 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
725
726 tsdPtr->inPostMenu++;
727
728 if (menuPtr->menuFlags & MENU_RECONFIGURE_PENDING) {
729 Tcl_CancelIdleCall(ReconfigureWindowsMenu, (ClientData) menuPtr);
730 ReconfigureWindowsMenu((ClientData) menuPtr);
731 }
732
733 result = TkPreprocessMenu(menuPtr);
734 if (result != TCL_OK) {
735 tsdPtr->inPostMenu--;
736 return result;
737 }
738
739 /*
740 * The post commands could have deleted the menu, which means
741 * we are dead and should go away.
742 */
743
744 if (menuPtr->tkwin == NULL) {
745 tsdPtr->inPostMenu--;
746 return TCL_OK;
747 }
748
749 if (NULL == parentWindow) {
750 noGoawayRect.top = y - 50;
751 noGoawayRect.bottom = y + 50;
752 noGoawayRect.left = x - 50;
753 noGoawayRect.right = x + 50;
754 } else {
755 int left, top;
756 Tk_GetRootCoords(parentWindow, &left, &top);
757 noGoawayRect.left = left;
758 noGoawayRect.top = top;
759 noGoawayRect.bottom = noGoawayRect.top + Tk_Height(parentWindow);
760 noGoawayRect.right = noGoawayRect.left + Tk_Width(parentWindow);
761 }
762
763 Tcl_SetServiceMode(TCL_SERVICE_NONE);
764
765 /*
766 * Make an assumption here. If the right button is down,
767 * then we want to track it. Otherwise, track the left mouse button.
768 */
769
770 flags = TPM_LEFTALIGN;
771 if (GetSystemMetrics(SM_SWAPBUTTON)) {
772 if (GetAsyncKeyState(VK_LBUTTON) < 0) {
773 flags |= TPM_RIGHTBUTTON;
774 } else {
775 flags |= TPM_LEFTBUTTON;
776 }
777 } else {
778 if (GetAsyncKeyState(VK_RBUTTON) < 0) {
779 flags |= TPM_RIGHTBUTTON;
780 } else {
781 flags |= TPM_LEFTBUTTON;
782 }
783 }
784
785 TrackPopupMenu(winMenuHdl, flags, x, y, 0,
786 tsdPtr->menuHWND, &noGoawayRect);
787 Tcl_SetServiceMode(oldServiceMode);
788
789 GetCursorPos(&point);
790 Tk_PointerEvent(NULL, point.x, point.y);
791
792 if (tsdPtr->inPostMenu) {
793 tsdPtr->inPostMenu = 0;
794 }
795 return TCL_OK;
796 }
797
798 /*
799 *----------------------------------------------------------------------
800 *
801 * TkpMenuNewEntry --
802 *
803 * Adds a pointer to a new menu entry structure with the platform-
804 * specific fields filled in.
805 *
806 * Results:
807 * Standard TCL error.
808 *
809 * Side effects:
810 * A new command ID is allocated and stored in the platformEntryData
811 * field of mePtr.
812 *
813 *----------------------------------------------------------------------
814 */
815
816 int
817 TkpMenuNewEntry(mePtr)
818 TkMenuEntry *mePtr;
819 {
820 int commandID;
821 TkMenu *menuPtr = mePtr->menuPtr;
822
823 if (GetNewID(mePtr, &commandID) != TCL_OK) {
824 return TCL_ERROR;
825 }
826
827 if (!(menuPtr->menuFlags & MENU_RECONFIGURE_PENDING)) {
828 menuPtr->menuFlags |= MENU_RECONFIGURE_PENDING;
829 Tcl_DoWhenIdle(ReconfigureWindowsMenu, (ClientData) menuPtr);
830 }
831
832 mePtr->platformEntryData = (TkMenuPlatformEntryData) commandID;
833
834 return TCL_OK;
835 }
836
837 /*
838 *----------------------------------------------------------------------
839 *
840 * TkWinMenuProc --
841 *
842 * The window proc for the dummy window we put popups in. This allows
843 * is to post a popup whether or not we know what the parent window
844 * is.
845 *
846 * Results:
847 * Returns whatever is appropriate for the message in question.
848 *
849 * Side effects:
850 * Normal side-effect for windows messages.
851 *
852 *----------------------------------------------------------------------
853 */
854
855 static LRESULT CALLBACK
856 TkWinMenuProc(hwnd, message, wParam, lParam)
857 HWND hwnd;
858 UINT message;
859 WPARAM wParam;
860 LPARAM lParam;
861 {
862 LRESULT lResult;
863
864 if (!TkWinHandleMenuEvent(&hwnd, &message, &wParam, &lParam, &lResult)) {
865 lResult = DefWindowProc(hwnd, message, wParam, lParam);
866 }
867 return lResult;
868 }
869
870 /*
871 *----------------------------------------------------------------------
872 *
873 * TkWinHandleMenuEvent --
874 *
875 * Filters out menu messages from messages passed to a top-level.
876 * Will respond appropriately to WM_COMMAND, WM_MENUSELECT,
877 * WM_MEASUREITEM, WM_DRAWITEM
878 *
879 * Result:
880 * Returns 1 if this handled the message; 0 if it did not.
881 *
882 * Side effects:
883 * All of the parameters may be modified so that the caller can
884 * think it is getting a different message. plResult points to
885 * the result that should be returned to windows from this message.
886 *
887 *----------------------------------------------------------------------
888 */
889
890 int
891 TkWinHandleMenuEvent(phwnd, pMessage, pwParam, plParam, plResult)
892 HWND *phwnd;
893 UINT *pMessage;
894 WPARAM *pwParam;
895 LPARAM *plParam;
896 LRESULT *plResult;
897 {
898 Tcl_HashEntry *hashEntryPtr;
899 int returnResult = 0;
900 TkMenu *menuPtr;
901 TkMenuEntry *mePtr;
902 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
903 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
904
905 switch (*pMessage) {
906 case WM_INITMENU:
907 TkMenuInit();
908 hashEntryPtr = Tcl_FindHashEntry(&tsdPtr->winMenuTable,
909 (char *) *pwParam);
910 if (hashEntryPtr != NULL) {
911 tsdPtr->oldServiceMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
912 menuPtr = (TkMenu *) Tcl_GetHashValue(hashEntryPtr);
913 tsdPtr->modalMenuPtr = menuPtr;
914 if (menuPtr->menuFlags & MENU_RECONFIGURE_PENDING) {
915 Tcl_CancelIdleCall(ReconfigureWindowsMenu,
916 (ClientData) menuPtr);
917 ReconfigureWindowsMenu((ClientData) menuPtr);
918 }
919 RecursivelyClearActiveMenu(menuPtr);
920 if (!tsdPtr->inPostMenu) {
921 Tcl_Interp *interp;
922 int code;
923
924 interp = menuPtr->interp;
925 Tcl_Preserve((ClientData)interp);
926 code = TkPreprocessMenu(menuPtr);
927 if ((code != TCL_OK) && (code != TCL_CONTINUE)
928 && (code != TCL_BREAK)) {
929 Tcl_AddErrorInfo(interp, "\n (menu preprocess)");
930 Tcl_BackgroundError(interp);
931 }
932 Tcl_Release((ClientData)interp);
933 }
934 TkActivateMenuEntry(menuPtr, -1);
935 *plResult = 0;
936 returnResult = 1;
937 } else {
938 tsdPtr->modalMenuPtr = NULL;
939 }
940 break;
941
942 case WM_SYSCOMMAND:
943 case WM_COMMAND: {
944 TkMenuInit();
945 if (HIWORD(*pwParam) != 0) {
946 break;
947 }
948 hashEntryPtr = Tcl_FindHashEntry(&tsdPtr->commandTable,
949 (char *)LOWORD(*pwParam));
950 if (hashEntryPtr == NULL) {
951 break;
952 }
953 mePtr = (TkMenuEntry *) Tcl_GetHashValue(hashEntryPtr);
954 if (mePtr != NULL) {
955 TkMenuReferences *menuRefPtr;
956 TkMenuEntry *parentEntryPtr;
957 Tcl_Interp *interp;
958 int code;
959
960 /*
961 * We have to set the parent of this menu to be active
962 * if this is a submenu so that tearoffs will get the
963 * correct title.
964 */
965
966 menuPtr = mePtr->menuPtr;
967 menuRefPtr = TkFindMenuReferences(menuPtr->interp,
968 Tk_PathName(menuPtr->tkwin));
969 if ((menuRefPtr != NULL)
970 && (menuRefPtr->parentEntryPtr != NULL)) {
971 char *name;
972
973 for (parentEntryPtr = menuRefPtr->parentEntryPtr;
974 ;
975 parentEntryPtr =
976 parentEntryPtr->nextCascadePtr) {
977 name = Tcl_GetStringFromObj(
978 parentEntryPtr->namePtr, NULL);
979 if (strcmp(name, Tk_PathName(menuPtr->tkwin))
980 == 0) {
981 break;
982 }
983 }
984 if (parentEntryPtr->menuPtr->entries[parentEntryPtr->index]
985 ->state != ENTRY_DISABLED) {
986 TkActivateMenuEntry(parentEntryPtr->menuPtr,
987 parentEntryPtr->index);
988 }
989 }
990
991 interp = menuPtr->interp;
992 Tcl_Preserve((ClientData)interp);
993 code = TkInvokeMenu(interp, menuPtr, mePtr->index);
994 if (code != TCL_OK && code != TCL_CONTINUE
995 && code != TCL_BREAK) {
996 Tcl_AddErrorInfo(interp, "\n (menu invoke)");
997 Tcl_BackgroundError(interp);
998 }
999 Tcl_Release((ClientData)interp);
1000 }
1001 *plResult = 0;
1002 returnResult = 1;
1003 break;
1004 }
1005
1006
1007 case WM_MENUCHAR: {
1008 unsigned char menuChar = (unsigned char) LOWORD(*pwParam);
1009 hashEntryPtr = Tcl_FindHashEntry(&tsdPtr->winMenuTable,
1010 (char *) *plParam);
1011 if (hashEntryPtr != NULL) {
1012 int i;
1013
1014 *plResult = 0;
1015 menuPtr = (TkMenu *) Tcl_GetHashValue(hashEntryPtr);
1016 for (i = 0; i < menuPtr->numEntries; i++) {
1017 int underline;
1018 char *label;
1019
1020 underline = menuPtr->entries[i]->underline;
1021 if (menuPtr->entries[i]->labelPtr != NULL) {
1022 label = Tcl_GetStringFromObj(
1023 menuPtr->entries[i]->labelPtr, NULL);
1024 }
1025 if ((-1 != underline)
1026 && (NULL != menuPtr->entries[i]->labelPtr)
1027 && (CharUpper((LPTSTR) menuChar)
1028 == CharUpper((LPTSTR) (unsigned char)
1029 label[underline]))) {
1030 *plResult = (2 << 16) | i;
1031 returnResult = 1;
1032 break;
1033 }
1034 }
1035 }
1036 break;
1037 }
1038
1039 case WM_MEASUREITEM: {
1040 LPMEASUREITEMSTRUCT itemPtr = (LPMEASUREITEMSTRUCT) *plParam;
1041
1042 if (itemPtr != NULL) {
1043 mePtr = (TkMenuEntry *) itemPtr->itemData;
1044 menuPtr = mePtr->menuPtr;
1045
1046 TkRecomputeMenu(menuPtr);
1047 itemPtr->itemHeight = mePtr->height;
1048 itemPtr->itemWidth = mePtr->width;
1049 if (mePtr->hideMargin) {
1050 itemPtr->itemWidth += 2 - indicatorDimensions[1];
1051 } else {
1052 int activeBorderWidth;
1053
1054 Tk_GetPixelsFromObj(menuPtr->interp, menuPtr->tkwin,
1055 menuPtr->activeBorderWidthPtr,
1056 &activeBorderWidth);
1057 itemPtr->itemWidth += 2 * activeBorderWidth;
1058 }
1059 *plResult = 1;
1060 returnResult = 1;
1061 }
1062 break;
1063 }
1064
1065 case WM_DRAWITEM: {
1066 TkWinDrawable *twdPtr;
1067 LPDRAWITEMSTRUCT itemPtr = (LPDRAWITEMSTRUCT) *plParam;
1068 Tk_FontMetrics fontMetrics;
1069
1070 if (itemPtr != NULL) {
1071 Tk_Font tkfont;
1072
1073 mePtr = (TkMenuEntry *) itemPtr->itemData;
1074 menuPtr = mePtr->menuPtr;
1075 twdPtr = (TkWinDrawable *) ckalloc(sizeof(TkWinDrawable));
1076 twdPtr->type = TWD_WINDC;
1077 twdPtr->winDC.hdc = itemPtr->hDC;
1078
1079 if (mePtr->state != ENTRY_DISABLED) {
1080 if (itemPtr->itemState & ODS_SELECTED) {
1081 TkActivateMenuEntry(menuPtr, mePtr->index);
1082 } else {
1083 TkActivateMenuEntry(menuPtr, -1);
1084 }
1085 } else {
1086 /* On windows, menu entries should highlight even if they
1087 ** are disabled. (I know this seems dumb, but it is the way
1088 ** native windows menus works so we ought to mimic it.)
1089 ** The ENTRY_PLATFORM_FLAG1 flag will indicate that the
1090 ** entry should be highlighted even though it is disabled.
1091 */
1092 if (itemPtr->itemState & ODS_SELECTED) {
1093 mePtr->entryFlags |= ENTRY_PLATFORM_FLAG1;
1094 } else {
1095 mePtr->entryFlags &= ~ENTRY_PLATFORM_FLAG1;
1096 }
1097 }
1098
1099 tkfont = Tk_GetFontFromObj(menuPtr->tkwin, menuPtr->fontPtr);
1100 Tk_GetFontMetrics(tkfont, &fontMetrics);
1101 TkpDrawMenuEntry(mePtr, (Drawable) twdPtr, tkfont,
1102 &fontMetrics, itemPtr->rcItem.left,
1103 itemPtr->rcItem.top, itemPtr->rcItem.right
1104 - itemPtr->rcItem.left, itemPtr->rcItem.bottom
1105 - itemPtr->rcItem.top, 0, 0);
1106
1107 ckfree((char *) twdPtr);
1108 *plResult = 1;
1109 returnResult = 1;
1110 }
1111 break;
1112 }
1113
1114 case WM_MENUSELECT: {
1115 UINT flags = HIWORD(*pwParam);
1116
1117 TkMenuInit();
1118
1119 if ((flags == 0xFFFF) && (*plParam == 0)) {
1120 if (tsdPtr->modalMenuPtr != NULL) {
1121 Tcl_SetServiceMode(tsdPtr->oldServiceMode);
1122 RecursivelyClearActiveMenu(tsdPtr->modalMenuPtr);
1123 }
1124 } else {
1125 menuPtr = NULL;
1126 if (*plParam != 0) {
1127 hashEntryPtr = Tcl_FindHashEntry(&tsdPtr->winMenuTable,
1128 (char *) *plParam);
1129 if (hashEntryPtr != NULL) {
1130 menuPtr = (TkMenu *) Tcl_GetHashValue(hashEntryPtr);
1131 }
1132 }
1133
1134 if (menuPtr != NULL) {
1135 mePtr = NULL;
1136 if (flags != 0xFFFF) {
1137 if (flags & MF_POPUP) {
1138 int index = LOWORD(*pwParam);
1139 if ((menuPtr->menuType == MENUBAR) && (menuPtr->tearoff)) {
1140 /* Code above has skipped the tearoff entry on a menubar
1141 so Windows's index differs from Tk's
1142 */
1143 index++;
1144 }
1145 mePtr = menuPtr->entries[index];
1146 } else {
1147 hashEntryPtr = Tcl_FindHashEntry(
1148 &tsdPtr->commandTable,
1149 (char *) LOWORD(*pwParam));
1150 if (hashEntryPtr != NULL) {
1151 mePtr = (TkMenuEntry *)
1152 Tcl_GetHashValue(hashEntryPtr);
1153 }
1154 }
1155 }
1156
1157 if ((mePtr == NULL) || (mePtr->state == ENTRY_DISABLED)) {
1158 TkActivateMenuEntry(menuPtr, -1);
1159 } else {
1160 TkActivateMenuEntry(menuPtr, mePtr->index);
1161 }
1162 MenuSelectEvent(menuPtr);
1163 Tcl_ServiceAll();
1164 }
1165 }
1166 break;
1167 }
1168 }
1169 return returnResult;
1170 }
1171
1172 /*
1173 *----------------------------------------------------------------------
1174 *
1175 * RecursivelyClearActiveMenu --
1176 *
1177 * Recursively clears the active entry in the menu's cascade hierarchy.
1178 *
1179 * Results:
1180 * None.
1181 *
1182 * Side effects:
1183 * Generates <<MenuSelect>> virtual events.
1184 *
1185 *----------------------------------------------------------------------
1186 */
1187
1188 void
1189 RecursivelyClearActiveMenu(
1190 TkMenu *menuPtr) /* The menu to reset. */
1191 {
1192 int i;
1193 TkMenuEntry *mePtr;
1194
1195 TkActivateMenuEntry(menuPtr, -1);
1196 MenuSelectEvent(menuPtr);
1197 for (i = 0; i < menuPtr->numEntries; i++) {
1198 mePtr = menuPtr->entries[i];
1199 if (mePtr->state == ENTRY_ACTIVE) {
1200 mePtr->state = ENTRY_NORMAL;
1201 }
1202 mePtr->entryFlags &= ~ENTRY_PLATFORM_FLAG1;
1203 if (mePtr->type == CASCADE_ENTRY) {
1204 if ((mePtr->childMenuRefPtr != NULL)
1205 && (mePtr->childMenuRefPtr->menuPtr != NULL)) {
1206 RecursivelyClearActiveMenu(mePtr->childMenuRefPtr->menuPtr);
1207 }
1208 }
1209 }
1210 }
1211
1212 /*
1213 *----------------------------------------------------------------------
1214 *
1215 * TkpSetWindowMenuBar --
1216 *
1217 * Associates a given menu with a window.
1218 *
1219 * Results:
1220 * None.
1221 *
1222 * Side effects:
1223 * On Windows and UNIX, associates the platform menu with the
1224 * platform window.
1225 *
1226 *----------------------------------------------------------------------
1227 */
1228
1229 void
1230 TkpSetWindowMenuBar(tkwin, menuPtr)
1231 Tk_Window tkwin; /* The window we are putting the menubar into.*/
1232 TkMenu *menuPtr; /* The menu we are inserting */
1233 {
1234 HMENU winMenuHdl;
1235 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1236 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1237
1238 if (menuPtr != NULL) {
1239 Tcl_HashEntry *hashEntryPtr;
1240 int newEntry;
1241
1242 winMenuHdl = (HMENU) menuPtr->platformData;
1243 hashEntryPtr = Tcl_FindHashEntry(&tsdPtr->winMenuTable,
1244 (char *) winMenuHdl);
1245 Tcl_DeleteHashEntry(hashEntryPtr);
1246 DestroyMenu(winMenuHdl);
1247 winMenuHdl = CreateMenu();
1248 hashEntryPtr = Tcl_CreateHashEntry(&tsdPtr->winMenuTable,
1249 (char *) winMenuHdl, &newEntry);
1250 Tcl_SetHashValue(hashEntryPtr, (char *) menuPtr);
1251 menuPtr->platformData = (TkMenuPlatformData) winMenuHdl;
1252 TkWinSetMenu(tkwin, winMenuHdl);
1253 if (!(menuPtr->menuFlags & MENU_RECONFIGURE_PENDING)) {
1254 menuPtr->menuFlags |= MENU_RECONFIGURE_PENDING;
1255 Tcl_DoWhenIdle(ReconfigureWindowsMenu, (ClientData) menuPtr);
1256 }
1257 } else {
1258 TkWinSetMenu(tkwin, NULL);
1259 }
1260 }
1261
1262 /*
1263 *----------------------------------------------------------------------
1264 *
1265 * TkpSetMainMenubar --
1266 *
1267 * Puts the menu associated with a window into the menubar. Should
1268 * only be called when the window is in front.
1269 *
1270 * Results:
1271 * None.
1272 *
1273 * Side effects:
1274 * The menubar is changed.
1275 *
1276 *----------------------------------------------------------------------
1277 */
1278 void
1279 TkpSetMainMenubar(
1280 Tcl_Interp *interp, /* The interpreter of the application */
1281 Tk_Window tkwin, /* The frame we are setting up */
1282 char *menuName) /* The name of the menu to put in front.
1283 * If NULL, use the default menu bar.
1284 */
1285 {
1286 /*
1287 * Nothing to do.
1288 */
1289 }
1290
1291 /*
1292 *----------------------------------------------------------------------
1293 *
1294 * GetMenuIndicatorGeometry --
1295 *
1296 * Gets the width and height of the indicator area of a menu.
1297 *
1298 * Results:
1299 * widthPtr and heightPtr are set.
1300 *
1301 * Side effects:
1302 * None.
1303 *
1304 *----------------------------------------------------------------------
1305 */
1306
1307 void
1308 GetMenuIndicatorGeometry (
1309 TkMenu *menuPtr, /* The menu we are measuring */
1310 TkMenuEntry *mePtr, /* The entry we are measuring */
1311 Tk_Font tkfont, /* Precalculated font */
1312 CONST Tk_FontMetrics *fmPtr, /* Precalculated font metrics */
1313 int *widthPtr, /* The resulting width */
1314 int *heightPtr) /* The resulting height */
1315 {
1316 *heightPtr = indicatorDimensions[0];
1317 if (mePtr->hideMargin) {
1318 *widthPtr = 0;
1319 } else {
1320 int borderWidth;
1321
1322 Tk_GetPixelsFromObj(menuPtr->interp, menuPtr->tkwin,
1323 menuPtr->borderWidthPtr, &borderWidth);
1324 *widthPtr = indicatorDimensions[1] - borderWidth;
1325 }
1326 }
1327
1328 /*
1329 *----------------------------------------------------------------------
1330 *
1331 * GetMenuAccelGeometry --
1332 *
1333 * Gets the width and height of the indicator area of a menu.
1334 *
1335 * Results:
1336 * widthPtr and heightPtr are set.
1337 *
1338 * Side effects:
1339 * None.
1340 *
1341 *----------------------------------------------------------------------
1342 */
1343
1344 void
1345 GetMenuAccelGeometry (
1346 TkMenu *menuPtr, /* The menu we are measuring */
1347 TkMenuEntry *mePtr, /* The entry we are measuring */
1348 Tk_Font tkfont, /* The precalculated font */
1349 CONST Tk_FontMetrics *fmPtr, /* The precalculated font metrics */
1350 int *widthPtr, /* The resulting width */
1351 int *heightPtr) /* The resulting height */
1352 {
1353 *heightPtr = fmPtr->linespace;
1354 if (mePtr->type == CASCADE_ENTRY) {
1355 *widthPtr = 0;
1356 } else if (mePtr->accelPtr == NULL) {
1357 *widthPtr = 0;
1358 } else {
1359 char *accel = Tcl_GetStringFromObj(mePtr->accelPtr, NULL);
1360 *widthPtr = Tk_TextWidth(tkfont, accel, mePtr->accelLength);
1361 }
1362 }
1363
1364 /*
1365 *----------------------------------------------------------------------
1366 *
1367 * GetTearoffEntryGeometry --
1368 *
1369 * Gets the width and height of the indicator area of a menu.
1370 *
1371 * Results:
1372 * widthPtr and heightPtr are set.
1373 *
1374 * Side effects:
1375 * None.
1376 *
1377 *----------------------------------------------------------------------
1378 */
1379
1380 void
1381 GetTearoffEntryGeometry (
1382 TkMenu *menuPtr, /* The menu we are measuring */
1383 TkMenuEntry *mePtr, /* The entry we are measuring */
1384 Tk_Font tkfont, /* The precalculated font */
1385 CONST Tk_FontMetrics *fmPtr, /* The precalculated font metrics */
1386 int *widthPtr, /* The resulting width */
1387 int *heightPtr) /* The resulting height */
1388 {
1389 if (menuPtr->menuType != MASTER_MENU) {
1390 *heightPtr = 0;
1391 } else {
1392 *heightPtr = fmPtr->linespace;
1393 }
1394 *widthPtr = 0;
1395 }
1396
1397 /*
1398 *----------------------------------------------------------------------
1399 *
1400 * GetMenuSeparatorGeometry --
1401 *
1402 * Gets the width and height of the indicator area of a menu.
1403 *
1404 * Results:
1405 * widthPtr and heightPtr are set.
1406 *
1407 * Side effects:
1408 * None.
1409 *
1410 *----------------------------------------------------------------------
1411 */
1412
1413 void
1414 GetMenuSeparatorGeometry (
1415 TkMenu *menuPtr, /* The menu we are measuring */
1416 TkMenuEntry *mePtr, /* The entry we are measuring */
1417 Tk_Font tkfont, /* The precalculated font */
1418 CONST Tk_FontMetrics *fmPtr, /* The precalcualted font metrics */
1419 int *widthPtr, /* The resulting width */
1420 int *heightPtr) /* The resulting height */
1421 {
1422 *widthPtr = 0;
1423 *heightPtr = fmPtr->linespace - (2 * fmPtr->descent);
1424 }
1425
1426 /*
1427 *----------------------------------------------------------------------
1428 *
1429 * DrawWindowsSystemBitmap --
1430 *
1431 * Draws the windows system bitmap given by bitmapID into the rect
1432 * given by rectPtr in the drawable. The bitmap is centered in the
1433 * rectangle. It is not clipped, so if the bitmap is bigger than
1434 * the rect it will bleed.
1435 *
1436 * Results:
1437 * None.
1438 *
1439 * Side effects:
1440 * Drawing occurs. Some storage is allocated and released.
1441 *
1442 *----------------------------------------------------------------------
1443 */
1444
1445 static void
1446 DrawWindowsSystemBitmap(display, drawable, gc, rectPtr, bitmapID, alignFlags)
1447 Display *display; /* The display we are drawing into */
1448 Drawable drawable; /* The drawable we are working with */
1449 GC gc; /* The GC to draw with */
1450 CONST RECT *rectPtr; /* The rectangle to draw into */
1451 int bitmapID; /* The windows id of the system
1452 * bitmap to draw. */
1453 int alignFlags; /* How to align the bitmap inside the
1454 * rectangle. */
1455 {
1456 TkWinDCState state;
1457 HDC hdc = TkWinGetDrawableDC(display, drawable, &state);
1458 HDC scratchDC;
1459 HBITMAP bitmap;
1460 BITMAP bm;
1461 POINT ptSize;
1462 POINT ptOrg;
1463 int topOffset, leftOffset;
1464
1465 SetBkColor(hdc, gc->background);
1466 SetTextColor(hdc, gc->foreground);
1467
1468 scratchDC = CreateCompatibleDC(hdc);
1469 bitmap = LoadBitmap(NULL, MAKEINTRESOURCE(bitmapID));
1470
1471 SelectObject(scratchDC, bitmap);
1472 SetMapMode(scratchDC, GetMapMode(hdc));
1473 GetObject(bitmap, sizeof(BITMAP), &bm);
1474 ptSize.x = bm.bmWidth;
1475 ptSize.y = bm.bmHeight;
1476 DPtoLP(hdc, &ptSize, 1);
1477
1478 ptOrg.y = ptOrg.x = 0;
1479 DPtoLP(scratchDC, &ptOrg, 1);
1480
1481 if (alignFlags & ALIGN_BITMAP_TOP) {
1482 topOffset = 0;
1483 } else if (alignFlags & ALIGN_BITMAP_BOTTOM) {
1484 topOffset = (rectPtr->bottom - rectPtr->top) - ptSize.y;
1485 } else {
1486 topOffset = (rectPtr->bottom - rectPtr->top) / 2 - (ptSize.y / 2);
1487 }
1488
1489 if (alignFlags & ALIGN_BITMAP_LEFT) {
1490 leftOffset = 0;
1491 } else if (alignFlags & ALIGN_BITMAP_RIGHT) {
1492 leftOffset = (rectPtr->right - rectPtr->left) - ptSize.x;
1493 } else {
1494 leftOffset = (rectPtr->right - rectPtr->left) / 2 - (ptSize.x / 2);
1495 }
1496
1497 BitBlt(hdc, rectPtr->left + leftOffset, rectPtr->top + topOffset, ptSize.x,
1498 ptSize.y, scratchDC, ptOrg.x, ptOrg.y, SRCCOPY);
1499 DeleteDC(scratchDC);
1500 DeleteObject(bitmap);
1501
1502 TkWinReleaseDrawableDC(drawable, hdc, &state);
1503 }
1504
1505 /*
1506 *----------------------------------------------------------------------
1507 *
1508 * DrawMenuEntryIndicator --
1509 *
1510 * This procedure draws the indicator part of a menu.
1511 *
1512 * Results:
1513 * None.
1514 *
1515 * Side effects:
1516 * Commands are output to X to display the menu in its
1517 * current mode.
1518 *
1519 *----------------------------------------------------------------------
1520 */
1521 void
1522 DrawMenuEntryIndicator(menuPtr, mePtr, d, gc, indicatorGC, tkfont, fmPtr, x,
1523 y, width, height)
1524 TkMenu *menuPtr; /* The menu we are drawing */
1525 TkMenuEntry *mePtr; /* The entry we are drawing */
1526 Drawable d; /* What we are drawing into */
1527 GC gc; /* The gc we are drawing with */
1528 GC indicatorGC; /* The gc for indicator objects */
1529 Tk_Font tkfont; /* The precalculated font */
1530 CONST Tk_FontMetrics *fmPtr; /* The precalculated font metrics */
1531 int x; /* Left edge */
1532 int y; /* Top edge */
1533 int width;
1534 int height;
1535 {
1536 if ((mePtr->type == CHECK_BUTTON_ENTRY)
1537 || (mePtr->type == RADIO_BUTTON_ENTRY)) {
1538 if (mePtr->indicatorOn && (mePtr->entryFlags & ENTRY_SELECTED)) {
1539 RECT rect;
1540 GC whichGC;
1541 int borderWidth, activeBorderWidth;
1542 if (mePtr->state != ENTRY_NORMAL) {
1543 whichGC = gc;
1544 } else {
1545 whichGC = indicatorGC;
1546 }
1547
1548 rect.top = y;
1549 rect.bottom = y + mePtr->height;
1550 Tk_GetPixelsFromObj(menuPtr->interp, menuPtr->tkwin,
1551 menuPtr->borderWidthPtr, &borderWidth);
1552 Tk_GetPixelsFromObj(menuPtr->interp, menuPtr->tkwin,
1553 menuPtr->activeBorderWidthPtr, &activeBorderWidth);
1554 rect.left = borderWidth + activeBorderWidth + x;
1555 rect.right = mePtr->indicatorSpace + x;
1556
1557 if ((mePtr->state == ENTRY_DISABLED)
1558 && (menuPtr->disabledFgPtr != NULL)) {
1559 RECT hilightRect;
1560 COLORREF oldFgColor = whichGC->foreground;
1561
1562 whichGC->foreground = GetSysColor(COLOR_3DHILIGHT);
1563 hilightRect.top = rect.top + 1;
1564 hilightRect.bottom = rect.bottom + 1;
1565 hilightRect.left = rect.left + 1;
1566 hilightRect.right = rect.right + 1;
1567 DrawWindowsSystemBitmap(menuPtr->display, d, whichGC,
1568 &hilightRect, OBM_CHECK, 0);
1569 whichGC->foreground = oldFgColor;
1570 }
1571
1572 DrawWindowsSystemBitmap(menuPtr->display, d, whichGC, &rect,
1573 OBM_CHECK, 0);
1574 }
1575 }
1576 }
1577
1578 /*
1579 *----------------------------------------------------------------------
1580 *
1581 * DrawMenuEntryAccelerator --
1582 *
1583 * This procedure draws the accelerator part of a menu. We
1584 * need to decide what to draw here. Should we replace strings
1585 * like "Control", "Command", etc?
1586 *
1587 * Results:
1588 * None.
1589 *
1590 * Side effects:
1591 * Commands are output to X to display the menu in its
1592 * current mode.
1593 *
1594 *----------------------------------------------------------------------
1595 */
1596
1597 void
1598 DrawMenuEntryAccelerator(menuPtr, mePtr, d, gc, tkfont, fmPtr,
1599 activeBorder, x, y, width, height, drawArrow)
1600 TkMenu *menuPtr; /* The menu we are drawing */
1601 TkMenuEntry *mePtr; /* The entry we are drawing */
1602 Drawable d; /* What we are drawing into */
1603 GC gc; /* The gc we are drawing with */
1604 Tk_Font tkfont; /* The precalculated font */
1605 CONST Tk_FontMetrics *fmPtr; /* The precalculated font metrics */
1606 Tk_3DBorder activeBorder; /* The border when an item is active */
1607 int x; /* left edge */
1608 int y; /* top edge */
1609 int width; /* Width of menu entry */
1610 int height; /* Height of menu entry */
1611 int drawArrow; /* For cascade menus, whether of not
1612 * to draw the arraw. I cannot figure
1613 * out Windows' algorithm for where
1614 * to draw this. */
1615 {
1616 int baseline;
1617 int leftEdge = x + mePtr->indicatorSpace + mePtr->labelWidth;
1618 char *accel;
1619
1620 if (mePtr->accelPtr != NULL) {
1621 accel = Tcl_GetStringFromObj(mePtr->accelPtr, NULL);
1622 }
1623
1624 baseline = y + (height + fmPtr->ascent - fmPtr->descent) / 2;
1625
1626 if ((mePtr->state == ENTRY_DISABLED) && (menuPtr->disabledFgPtr != NULL)
1627 && ((mePtr->accelPtr != NULL)
1628 || ((mePtr->type == CASCADE_ENTRY) && drawArrow))) {
1629 COLORREF oldFgColor = gc->foreground;
1630
1631 gc->foreground = GetSysColor(COLOR_3DHILIGHT);
1632 if (mePtr->accelPtr != NULL) {
1633 Tk_DrawChars(menuPtr->display, d, gc, tkfont, accel,
1634 mePtr->accelLength, leftEdge + 1, baseline + 1);
1635 }
1636
1637 if (mePtr->type == CASCADE_ENTRY) {
1638 RECT rect;
1639
1640 rect.top = y + GetSystemMetrics(SM_CYBORDER) + 1;
1641 rect.bottom = y + height - GetSystemMetrics(SM_CYBORDER) + 1;
1642 rect.left = x + mePtr->indicatorSpace + mePtr->labelWidth + 1;
1643 rect.right = x + width;
1644 DrawWindowsSystemBitmap(menuPtr->display, d, gc, &rect,
1645 OBM_MNARROW, ALIGN_BITMAP_RIGHT);
1646 }
1647 gc->foreground = oldFgColor;
1648 }
1649
1650 if (mePtr->accelPtr != NULL) {
1651 Tk_DrawChars(menuPtr->display, d, gc, tkfont, accel,
1652 mePtr->accelLength, leftEdge, baseline);
1653 }
1654
1655 if ((mePtr->type == CASCADE_ENTRY) && drawArrow) {
1656 RECT rect;
1657
1658 rect.top = y + GetSystemMetrics(SM_CYBORDER);
1659 rect.bottom = y + height - GetSystemMetrics(SM_CYBORDER);
1660 rect.left = x + mePtr->indicatorSpace + mePtr->labelWidth;
1661 rect.right = x + width - 1;
1662 DrawWindowsSystemBitmap(menuPtr->display, d, gc, &rect, OBM_MNARROW,
1663 ALIGN_BITMAP_RIGHT);
1664 }
1665 }
1666
1667 /*
1668 *----------------------------------------------------------------------
1669 *
1670 * DrawMenuSeparator --
1671 *
1672 * The menu separator is drawn.
1673 *
1674 * Results:
1675 * None.
1676 *
1677 * Side effects:
1678 * Commands are output to X to display the menu in its
1679 * current mode.
1680 *
1681 *----------------------------------------------------------------------
1682 */
1683 void
1684 DrawMenuSeparator(menuPtr, mePtr, d, gc, tkfont, fmPtr, x, y, width, height)
1685 TkMenu *menuPtr; /* The menu we are drawing */
1686 TkMenuEntry *mePtr; /* The entry we are drawing */
1687 Drawable d; /* What we are drawing into */
1688 GC gc; /* The gc we are drawing with */
1689 Tk_Font tkfont; /* The precalculated font */
1690 CONST Tk_FontMetrics *fmPtr; /* The precalculated font metrics */
1691 int x; /* left edge */
1692 int y; /* top edge */
1693 int width; /* width of item */
1694 int height; /* height of item */
1695 {
1696 XPoint points[2];
1697 Tk_3DBorder border;
1698
1699 points[0].x = x;
1700 points[0].y = y + height / 2;
1701 points[1].x = x + width - 1;
1702 points[1].y = points[0].y;
1703 border = Tk_Get3DBorderFromObj(menuPtr->tkwin, menuPtr->borderPtr);
1704 Tk_Draw3DPolygon(menuPtr->tkwin, d, border, points, 2, 1,
1705 TK_RELIEF_RAISED);
1706 }
1707
1708 /*
1709 *----------------------------------------------------------------------
1710 *
1711 * DrawMenuUnderline --
1712 *
1713 * On appropriate platforms, draw the underline character for the
1714 * menu.
1715 *
1716 * Results:
1717 * None.
1718 *
1719 * Side effects:
1720 * Commands are output to X to display the menu in its
1721 * current mode.
1722 *
1723 *----------------------------------------------------------------------
1724 */
1725 static void
1726 DrawMenuUnderline(
1727 TkMenu *menuPtr, /* The menu to draw into */
1728 TkMenuEntry *mePtr, /* The entry we are drawing */
1729 Drawable d, /* What we are drawing into */
1730 GC gc, /* The gc to draw into */
1731 Tk_Font tkfont, /* The precalculated font */
1732 CONST Tk_FontMetrics *fmPtr, /* The precalculated font metrics */
1733 int x, /* Left Edge */
1734 int y, /* Top Edge */
1735 int width, /* Width of entry */
1736 int height) /* Height of entry */
1737 {
1738 if (mePtr->underline >= 0) {
1739 char *label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL);
1740 CONST char *start = Tcl_UtfAtIndex(label, mePtr->underline);
1741 CONST char *end = Tcl_UtfNext(start);
1742
1743 Tk_UnderlineChars(menuPtr->display, d,
1744 gc, tkfont, label, x + mePtr->indicatorSpace,
1745 y + (height + fmPtr->ascent - fmPtr->descent) / 2,
1746 (int) (start - label), (int) (end - label));
1747 }
1748 }
1749
1750 /*
1751 *--------------------------------------------------------------
1752 *
1753 * MenuKeyBindProc --
1754 *
1755 * This procedure is invoked when keys related to pulling
1756 * down menus is pressed. The corresponding Windows events
1757 * are generated and passed to DefWindowProc if appropriate.
1758 *
1759 * Results:
1760 * Always returns TCL_OK.
1761 *
1762 * Side effects:
1763 * The menu system may take over and process user events
1764 * for menu input.
1765 *
1766 *--------------------------------------------------------------
1767 */
1768
1769 static int
1770 MenuKeyBindProc(clientData, interp, eventPtr, tkwin, keySym)
1771 ClientData clientData; /* not used in this proc */
1772 Tcl_Interp *interp; /* The interpreter of the receiving window. */
1773 XEvent *eventPtr; /* The XEvent to process */
1774 Tk_Window tkwin; /* The window receiving the event */
1775 KeySym keySym; /* The key sym that is produced. */
1776 {
1777 UINT scanCode;
1778 UINT virtualKey;
1779 TkWindow *winPtr = (TkWindow *)tkwin;
1780 int i;
1781
1782 if (eventPtr->type == KeyPress) {
1783 switch (keySym) {
1784 case XK_Alt_L:
1785 scanCode = MapVirtualKey(VK_LMENU, 0);
1786 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1787 WM_SYSKEYDOWN, VK_MENU, (scanCode << 16)
1788 | (1 << 29));
1789 break;
1790 case XK_Alt_R:
1791 scanCode = MapVirtualKey(VK_RMENU, 0);
1792 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1793 WM_SYSKEYDOWN, VK_MENU, (scanCode << 16)
1794 | (1 << 29) | (1 << 24));
1795 break;
1796 case XK_F10:
1797 scanCode = MapVirtualKey(VK_F10, 0);
1798 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1799 WM_SYSKEYDOWN, VK_F10, (scanCode << 16));
1800 break;
1801 default:
1802 virtualKey = XKeysymToKeycode(winPtr->display, keySym);
1803 scanCode = MapVirtualKey(virtualKey, 0);
1804 if (0 != scanCode) {
1805 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1806 WM_SYSKEYDOWN, virtualKey, ((scanCode << 16)
1807 | (1 << 29)));
1808 if (eventPtr->xkey.nbytes > 0) {
1809 for (i = 0; i < eventPtr->xkey.nbytes; i++) {
1810 CallWindowProc(DefWindowProc,
1811 Tk_GetHWND(Tk_WindowId(tkwin)),
1812 WM_SYSCHAR,
1813 eventPtr->xkey.trans_chars[i],
1814 ((scanCode << 16) | (1 << 29)));
1815 }
1816 }
1817 }
1818 }
1819 } else if (eventPtr->type == KeyRelease) {
1820 switch (keySym) {
1821 case XK_Alt_L:
1822 scanCode = MapVirtualKey(VK_LMENU, 0);
1823 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1824 WM_SYSKEYUP, VK_MENU, (scanCode << 16)
1825 | (1 << 29) | (1 << 30) | (1 << 31));
1826 break;
1827 case XK_Alt_R:
1828 scanCode = MapVirtualKey(VK_RMENU, 0);
1829 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1830 WM_SYSKEYUP, VK_MENU, (scanCode << 16) | (1 << 24)
1831 | (0x111 << 29) | (1 << 30) | (1 << 31));
1832 break;
1833 case XK_F10:
1834 scanCode = MapVirtualKey(VK_F10, 0);
1835 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1836 WM_SYSKEYUP, VK_F10, (scanCode << 16)
1837 | (1 << 30) | (1 << 31));
1838 break;
1839 default:
1840 virtualKey = XKeysymToKeycode(winPtr->display, keySym);
1841 scanCode = MapVirtualKey(virtualKey, 0);
1842 if (0 != scanCode) {
1843 CallWindowProc(DefWindowProc, Tk_GetHWND(Tk_WindowId(tkwin)),
1844 WM_SYSKEYUP, virtualKey, ((scanCode << 16)
1845 | (1 << 29) | (1 << 30) | (1 << 31)));
1846 }
1847 }
1848 }
1849 return TCL_OK;
1850 }
1851
1852 /*
1853 *--------------------------------------------------------------
1854 *
1855 * TkpInitializeMenuBindings --
1856 *
1857 * For every interp, initializes the bindings for Windows
1858 * menus. Does nothing on Mac or XWindows.
1859 *
1860 * Results:
1861 * None.
1862 *
1863 * Side effects:
1864 * C-level bindings are setup for the interp which will
1865 * handle Alt-key sequences for menus without beeping
1866 * or interfering with user-defined Alt-key bindings.
1867 *
1868 *--------------------------------------------------------------
1869 */
1870
1871 void
1872 TkpInitializeMenuBindings(interp, bindingTable)
1873 Tcl_Interp *interp; /* The interpreter to set. */
1874 Tk_BindingTable bindingTable; /* The table to add to. */
1875 {
1876 Tk_Uid uid = Tk_GetUid("all");
1877
1878 /*
1879 * We need to set up the bindings for menubars. These have to
1880 * recreate windows events, so we need to have a C-level
1881 * binding for this. We have to generate the WM_SYSKEYDOWNS
1882 * and WM_SYSKEYUPs appropriately.
1883 */
1884
1885 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1886 "<Alt_L>", MenuKeyBindProc, NULL, NULL);
1887 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1888 "<KeyRelease-Alt_L>", MenuKeyBindProc, NULL, NULL);
1889 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1890 "<Alt_R>", MenuKeyBindProc, NULL, NULL);
1891 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1892 "<KeyRelease-Alt_R>", MenuKeyBindProc, NULL, NULL);
1893 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1894 "<Alt-KeyPress>", MenuKeyBindProc, NULL, NULL);
1895 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1896 "<Alt-KeyRelease>", MenuKeyBindProc, NULL, NULL);
1897 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1898 "<KeyPress-F10>", MenuKeyBindProc, NULL, NULL);
1899 TkCreateBindingProcedure(interp, bindingTable, (ClientData)uid,
1900 "<KeyRelease-F10>", MenuKeyBindProc, NULL, NULL);
1901 }
1902
1903 /*
1904 *----------------------------------------------------------------------
1905 *
1906 * DrawMenuEntryLabel --
1907 *
1908 * This procedure draws the label part of a menu.
1909 *
1910 * Results:
1911 * None.
1912 *
1913 * Side effects:
1914 * Commands are output to X to display the menu in its
1915 * current mode.
1916 *
1917 *----------------------------------------------------------------------
1918 */
1919 static void
1920 DrawMenuEntryLabel(
1921 TkMenu *menuPtr, /* The menu we are drawing */
1922 TkMenuEntry *mePtr, /* The entry we are drawing */
1923 Drawable d, /* What we are drawing into */
1924 GC gc, /* The gc we are drawing into */
1925 Tk_Font tkfont, /* The precalculated font */
1926 CONST Tk_FontMetrics *fmPtr, /* The precalculated font metrics */
1927 int x, /* left edge */
1928 int y, /* right edge */
1929 int width, /* width of entry */
1930 int height) /* height of entry */
1931 {
1932 int indicatorSpace = mePtr->indicatorSpace;
1933 int activeBorderWidth;
1934 int leftEdge;
1935 int imageHeight, imageWidth;
1936 int textHeight, textWidth;
1937 int haveImage = 0, haveText = 0;
1938 int imageXOffset = 0, imageYOffset = 0;
1939 int textXOffset = 0, textYOffset = 0;
1940
1941 Tk_GetPixelsFromObj(menuPtr->interp, menuPtr->tkwin,
1942 menuPtr->activeBorderWidthPtr, &activeBorderWidth);
1943 leftEdge = x + indicatorSpace + activeBorderWidth;
1944
1945 /*
1946 * Work out what we will need to draw first.
1947 */
1948
1949 if (mePtr->image != NULL) {
1950 Tk_SizeOfImage(mePtr->image, &imageWidth, &imageHeight);
1951 haveImage = 1;
1952 } else if (mePtr->bitmapPtr != NULL) {
1953 Pixmap bitmap = Tk_GetBitmapFromObj(menuPtr->tkwin, mePtr->bitmapPtr);
1954 Tk_SizeOfBitmap(menuPtr->display, bitmap, &imageWidth, &imageHeight);
1955 haveImage = 1;
1956 }
1957 if (!haveImage || (mePtr->compound != COMPOUND_NONE)) {
1958 if (mePtr->labelLength > 0) {
1959 char *label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL);
1960 textWidth = Tk_TextWidth(tkfont, label, mePtr->labelLength);
1961 textHeight = fmPtr->linespace;
1962 haveText = 1;
1963 }
1964 }
1965
1966 /*
1967 * Now work out what the relative positions are.
1968 */
1969
1970 if (haveImage && haveText) {
1971 int fullWidth = (imageWidth > textWidth ? imageWidth : textWidth);
1972 switch ((enum compound) mePtr->compound) {
1973 case COMPOUND_TOP: {
1974 textXOffset = (fullWidth - textWidth)/2;
1975 textYOffset = imageHeight/2 + 2;
1976 imageXOffset = (fullWidth - imageWidth)/2;
1977 imageYOffset = -textHeight/2;
1978 break;
1979 }
1980 case COMPOUND_BOTTOM: {
1981 textXOffset = (fullWidth - textWidth)/2;
1982 textYOffset = -imageHeight/2;
1983 imageXOffset = (fullWidth - imageWidth)/2;
1984 imageYOffset = textHeight/2 + 2;
1985 break;
1986 }
1987 case COMPOUND_LEFT: {
1988 textXOffset = imageWidth + 2;
1989 textYOffset = 0;
1990 imageXOffset = 0;
1991 imageYOffset = 0;
1992 break;
1993 }
1994 case COMPOUND_RIGHT: {
1995 textXOffset = 0;
1996 textYOffset = 0;
1997 imageXOffset = textWidth + 2;
1998 imageYOffset = 0;
1999 break;
2000 }
2001 case COMPOUND_CENTER: {
2002 textXOffset = (fullWidth - textWidth)/2;
2003 textYOffset = 0;
2004 imageXOffset = (fullWidth - imageWidth)/2;
2005 imageYOffset = 0;
2006 break;
2007 }
2008 case COMPOUND_NONE: {break;}
2009 }
2010 } else {
2011 textXOffset = 0;
2012 textYOffset = 0;
2013 imageXOffset = 0;
2014 imageYOffset = 0;
2015 }
2016
2017 /*
2018 * Draw label and/or bitmap or image for entry.
2019 */
2020
2021 if (mePtr->image != NULL) {
2022 if ((mePtr->selectImage != NULL)
2023 && (mePtr->entryFlags & ENTRY_SELECTED)) {
2024 Tk_RedrawImage(mePtr->selectImage, 0, 0,
2025 imageWidth, imageHeight, d, leftEdge + imageXOffset,
2026 (int) (y + (mePtr->height - imageHeight)/2 + imageYOffset));
2027 } else {
2028 Tk_RedrawImage(mePtr->image, 0, 0, imageWidth,
2029 imageHeight, d, leftEdge + imageXOffset,
2030 (int) (y + (mePtr->height - imageHeight)/2 + imageYOffset));
2031 }
2032 } else if (mePtr->bitmapPtr != NULL) {
2033 Pixmap bitmap = Tk_GetBitmapFromObj(menuPtr->tkwin, mePtr->bitmapPtr);
2034 XCopyPlane(menuPtr->display, bitmap, d, gc, 0, 0,
2035 (unsigned) imageWidth, (unsigned) imageHeight,
2036 leftEdge + imageXOffset,
2037 (int) (y + (mePtr->height - imageHeight)/2 + imageYOffset), 1);
2038 }
2039 if ((mePtr->compound != COMPOUND_NONE) || !haveImage) {
2040 if (mePtr->labelLength > 0) {
2041 int baseline = y + (height + fmPtr->ascent - fmPtr->descent) / 2;
2042 char *label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL);
2043 Tk_DrawChars(menuPtr->display, d, gc, tkfont, label,
2044 mePtr->labelLength, leftEdge + textXOffset,
2045 baseline + textYOffset);
2046 DrawMenuUnderline(menuPtr, mePtr, d, gc, tkfont, fmPtr,
2047 x + textXOffset, y + textYOffset,
2048 width, height);
2049 }
2050 }
2051
2052 if (mePtr->state == ENTRY_DISABLED) {
2053 if (menuPtr->disabledFgPtr == NULL) {
2054 XFillRectangle(menuPtr->display, d, menuPtr->disabledGC, x, y,
2055 (unsigned) width, (unsigned) height);
2056 } else if ((mePtr->image != NULL)
2057 && (menuPtr->disabledImageGC != None)) {
2058 XFillRectangle(menuPtr->display, d, menuPtr->disabledImageGC,
2059 leftEdge + imageXOffset,
2060 (int) (y + (mePtr->height - imageHeight)/2 + imageYOffset),
2061 (unsigned) imageWidth, (unsigned) imageHeight);
2062 }
2063 }
2064 }
2065
2066 /*
2067 *--------------------------------------------------------------
2068 *
2069 * TkpComputeMenubarGeometry --
2070 *
2071 * This procedure is invoked to recompute the size and
2072 * layout of a menu that is a menubar clone.
2073 *
2074 * Results:
2075 * None.
2076 *
2077 * Side effects:
2078 * Fields of menu entries are changed to reflect their
2079 * current positions, and the size of the menu window
2080 * itself may be changed.
2081 *
2082 *--------------------------------------------------------------
2083 */
2084
2085 void
2086 TkpComputeMenubarGeometry(menuPtr)
2087 TkMenu *menuPtr; /* Structure describing menu. */
2088 {
2089 TkpComputeStandardMenuGeometry(menuPtr);
2090 }
2091
2092 /*
2093 *----------------------------------------------------------------------
2094 *
2095 * DrawTearoffEntry --
2096 *
2097 * This procedure draws the background part of a menu.
2098 *
2099 * Results:
2100 * None.
2101 *
2102 * Side effects:
2103 * Commands are output to X to display the menu in its
2104 * current mode.
2105 *
2106 *----------------------------------------------------------------------
2107 */
2108
2109 void
2110 DrawTearoffEntry(menuPtr, mePtr, d, gc, tkfont, fmPtr, x, y, width, height)
2111 TkMenu *menuPtr; /* The menu we are drawing */
2112 TkMenuEntry *mePtr; /* The entry we are drawing */
2113 Drawable d; /* The drawable we are drawing into */
2114 GC gc; /* The gc we are drawing with */
2115 Tk_Font tkfont; /* The font we are drawing with */
2116 CONST Tk_FontMetrics *fmPtr; /* The metrics we are drawing with */
2117 int x;
2118 int y;
2119 int width;
2120 int height;
2121 {
2122 XPoint points[2];
2123 int segmentWidth, maxX;
2124 Tk_3DBorder border;
2125
2126 if (menuPtr->menuType != MASTER_MENU) {
2127 return;
2128 }
2129
2130 points[0].x = x;
2131 points[0].y = y + height/2;
2132 points[1].y = points[0].y;
2133 segmentWidth = 6;
2134 maxX = width - 1;
2135 border = Tk_Get3DBorderFromObj(menuPtr->tkwin, menuPtr->borderPtr);
2136
2137 while (points[0].x < maxX) {
2138 points[1].x = points[0].x + segmentWidth;
2139 if (points[1].x > maxX) {
2140 points[1].x = maxX;
2141 }
2142 Tk_Draw3DPolygon(menuPtr->tkwin, d, border, points, 2, 1,
2143 TK_RELIEF_RAISED);
2144 points[0].x += 2*segmentWidth;
2145 }
2146 }
2147
2148 /*
2149 *----------------------------------------------------------------------
2150 *
2151 * TkpConfigureMenuEntry --
2152 *
2153 * Processes configurations for menu entries.
2154 *
2155 * Results:
2156 * Returns standard TCL result. If TCL_ERROR is returned, then
2157 * the interp's result contains an error message.
2158 *
2159 * Side effects:
2160 * Configuration information get set for mePtr; old resources
2161 * get freed, if any need it.
2162 *
2163 *----------------------------------------------------------------------
2164 */
2165
2166 int
2167 TkpConfigureMenuEntry(mePtr)
2168 register TkMenuEntry *mePtr; /* Information about menu entry; may
2169 * or may not already have values for
2170 * some fields. */
2171 {
2172 TkMenu *menuPtr = mePtr->menuPtr;
2173
2174 if (!(menuPtr->menuFlags & MENU_RECONFIGURE_PENDING)) {
2175 menuPtr->menuFlags |= MENU_RECONFIGURE_PENDING;
2176 Tcl_DoWhenIdle(ReconfigureWindowsMenu, (ClientData) menuPtr);
2177 }
2178 return TCL_OK;
2179 }
2180
2181 /*
2182 *----------------------------------------------------------------------
2183 *
2184 * TkpDrawMenuEntry --
2185 *
2186 * Draws the given menu entry at the given coordinates with the
2187 * given attributes.
2188 *
2189 * Results:
2190 * None.
2191 *
2192 * Side effects:
2193 * X Server commands are executed to display the menu entry.
2194 *
2195 *----------------------------------------------------------------------
2196 */
2197
2198 void
2199 TkpDrawMenuEntry(mePtr, d, tkfont, menuMetricsPtr, x, y, width, height,
2200 strictMotif, drawArrow)
2201 TkMenuEntry *mePtr; /* The entry to draw */
2202 Drawable d; /* What to draw into */
2203 Tk_Font tkfont; /* Precalculated font for menu */
2204 CONST Tk_FontMetrics *menuMetricsPtr;
2205 /* Precalculated metrics for menu */
2206 int x; /* X-coordinate of topleft of entry */
2207 int y; /* Y-coordinate of topleft of entry */
2208 int width; /* Width of the entry rectangle */
2209 int height; /* Height of the current rectangle */
2210 int strictMotif; /* Boolean flag */
2211 int drawArrow; /* Whether or not to draw the cascade
2212 * arrow for cascade items. Only applies
2213 * to Windows. */
2214 {
2215 GC gc, indicatorGC;
2216 TkMenu *menuPtr = mePtr->menuPtr;
2217 Tk_3DBorder bgBorder, activeBorder;
2218 CONST Tk_FontMetrics *fmPtr;
2219 Tk_FontMetrics entryMetrics;
2220 int padY = (menuPtr->menuType == MENUBAR) ? 3 : 0;
2221 int adjustedY = y + padY;
2222 int adjustedHeight = height - 2 * padY;
2223
2224 /*
2225 * Choose the gc for drawing the foreground part of the entry.
2226 */
2227
2228 if ((mePtr->state == ENTRY_ACTIVE) && !strictMotif) {
2229 gc = mePtr->activeGC;
2230 if (gc == NULL) {
2231 gc = menuPtr->activeGC;
2232 }
2233 } else {
2234 TkMenuEntry *cascadeEntryPtr;
2235 int parentDisabled = 0;
2236 char *name;
2237
2238 for (cascadeEntryPtr = menuPtr->menuRefPtr->parentEntryPtr;
2239 cascadeEntryPtr != NULL;
2240 cascadeEntryPtr = cascadeEntryPtr->nextCascadePtr) {
2241 name = Tcl_GetStringFromObj(cascadeEntryPtr->namePtr, NULL);
2242 if (strcmp(name, Tk_PathName(menuPtr->tkwin)) == 0) {
2243 if (mePtr->state == ENTRY_DISABLED) {
2244 parentDisabled = 1;
2245 }
2246 break;
2247 }
2248 }
2249
2250 if (((parentDisabled || (mePtr->state == ENTRY_DISABLED)))
2251 && (menuPtr->disabledFgPtr != NULL)) {
2252 gc = mePtr->disabledGC;
2253 if (gc == NULL) {
2254 gc = menuPtr->disabledGC;
2255 }
2256 } else {
2257 gc = mePtr->textGC;
2258 if (gc == NULL) {
2259 gc = menuPtr->textGC;
2260 }
2261 }
2262 }
2263 indicatorGC = mePtr->indicatorGC;
2264 if (indicatorGC == NULL) {
2265 indicatorGC = menuPtr->indicatorGC;
2266 }
2267
2268 bgBorder = Tk_Get3DBorderFromObj(menuPtr->tkwin,
2269 (mePtr->borderPtr == NULL) ? menuPtr->borderPtr
2270 : mePtr->borderPtr);
2271 if (strictMotif) {
2272 activeBorder = bgBorder;
2273 } else {
2274 activeBorder = Tk_Get3DBorderFromObj(menuPtr->tkwin,
2275 (mePtr->activeBorderPtr == NULL) ? menuPtr->activeBorderPtr
2276 : mePtr->activeBorderPtr);
2277 }
2278
2279 if (mePtr->fontPtr == NULL) {
2280 fmPtr = menuMetricsPtr;
2281 } else {
2282 tkfont = Tk_GetFontFromObj(menuPtr->tkwin, mePtr->fontPtr);
2283 Tk_GetFontMetrics(tkfont, &entryMetrics);
2284 fmPtr = &entryMetrics;
2285 }
2286
2287 /*
2288 * Need to draw the entire background, including padding. On Unix,
2289 * for menubars, we have to draw the rest of the entry taking
2290 * into account the padding.
2291 */
2292
2293 DrawMenuEntryBackground(menuPtr, mePtr, d, activeBorder,
2294 bgBorder, x, y, width, height);
2295
2296 if (mePtr->type == SEPARATOR_ENTRY) {
2297 DrawMenuSeparator(menuPtr, mePtr, d, gc, tkfont,
2298 fmPtr, x, adjustedY, width, adjustedHeight);
2299 } else if (mePtr->type == TEAROFF_ENTRY) {
2300 DrawTearoffEntry(menuPtr, mePtr, d, gc, tkfont, fmPtr, x, adjustedY,
2301 width, adjustedHeight);
2302 } else {
2303 DrawMenuEntryLabel(menuPtr, mePtr, d, gc, tkfont, fmPtr, x, adjustedY,
2304 width, adjustedHeight);
2305 DrawMenuEntryAccelerator(menuPtr, mePtr, d, gc, tkfont, fmPtr,
2306 activeBorder, x, adjustedY, width, adjustedHeight, drawArrow);
2307 if (!mePtr->hideMargin) {
2308 DrawMenuEntryIndicator(menuPtr, mePtr, d, gc, indicatorGC, tkfont,
2309 fmPtr, x, adjustedY, width, adjustedHeight);
2310 }
2311 }
2312 }
2313
2314 /*
2315 *----------------------------------------------------------------------
2316 *
2317 * GetMenuLabelGeometry --
2318 *
2319 * Figures out the size of the label portion of a menu item.
2320 *
2321 * Results:
2322 * widthPtr and heightPtr are filled in with the correct geometry
2323 * information.
2324 *
2325 * Side effects:
2326 * None.
2327 *
2328 *----------------------------------------------------------------------
2329 */
2330
2331 static void
2332 GetMenuLabelGeometry(mePtr, tkfont, fmPtr, widthPtr, heightPtr)
2333 TkMenuEntry *mePtr; /* The entry we are computing */
2334 Tk_Font tkfont; /* The precalculated font */
2335 CONST Tk_FontMetrics *fmPtr; /* The precalculated metrics */
2336 int *widthPtr; /* The resulting width of the label
2337 * portion */
2338 int *heightPtr; /* The resulting height of the label
2339 * portion */
2340 {
2341 TkMenu *menuPtr = mePtr->menuPtr;
2342 int haveImage = 0, haveText = 0;
2343
2344 if (mePtr->image != NULL) {
2345 Tk_SizeOfImage(mePtr->image, widthPtr, heightPtr);
2346 haveImage = 1;
2347 } else if (mePtr->bitmapPtr != NULL) {
2348 Pixmap bitmap = Tk_GetBitmapFromObj(menuPtr->tkwin, mePtr->bitmapPtr);
2349 Tk_SizeOfBitmap(menuPtr->display, bitmap, widthPtr, heightPtr);
2350 haveImage = 1;
2351 } else {
2352 *heightPtr = 0;
2353 *widthPtr = 0;
2354 }
2355
2356 if (haveImage && (mePtr->compound == COMPOUND_NONE)) {
2357 /* We don't care about the text in this case */
2358 } else {
2359 /* Either it is compound or we don't have an image */
2360 if (mePtr->labelPtr != NULL) {
2361 int textWidth;
2362 char *label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL);
2363 textWidth = Tk_TextWidth(tkfont, label, mePtr->labelLength);
2364
2365 if ((mePtr->compound != COMPOUND_NONE) && haveImage) {
2366 switch ((enum compound) mePtr->compound) {
2367 case COMPOUND_TOP:
2368 case COMPOUND_BOTTOM: {
2369 if (textWidth > *widthPtr) {
2370 *widthPtr = textWidth;
2371 }
2372 /* Add text and padding */
2373 *heightPtr += fmPtr->linespace + 2;
2374 break;
2375 }
2376 case COMPOUND_LEFT:
2377 case COMPOUND_RIGHT: {
2378 if (fmPtr->linespace > *heightPtr) {
2379 *heightPtr = fmPtr->linespace;
2380 }
2381 /* Add text and padding */
2382 *widthPtr += textWidth + 2;
2383 break;
2384 }
2385 case COMPOUND_CENTER: {
2386 if (fmPtr->linespace > *heightPtr) {
2387 *heightPtr = fmPtr->linespace;
2388 }
2389 if (textWidth > *widthPtr) {
2390 *widthPtr = textWidth;
2391 }
2392 break;
2393 }
2394 case COMPOUND_NONE: {break;}
2395 }
2396 } else {
2397 /* We don't have an image or we're not compound */
2398 *heightPtr = fmPtr->linespace;
2399 *widthPtr = textWidth;
2400 }
2401 } else {
2402 /* An empty entry still has this height */
2403 *heightPtr = fmPtr->linespace;
2404 }
2405 }
2406 *heightPtr += 1;
2407 }
2408
2409 /*
2410 *----------------------------------------------------------------------
2411 *
2412 * DrawMenuEntryBackground --
2413 *
2414 * This procedure draws the background part of a menu.
2415 *
2416 * Results:
2417 * None.
2418 *
2419 * Side effects:
2420 * Commands are output to X to display the menu in its
2421 * current mode.
2422 *
2423 *----------------------------------------------------------------------
2424 */
2425
2426 static void
2427 DrawMenuEntryBackground(
2428 TkMenu *menuPtr, /* The menu we are drawing. */
2429 TkMenuEntry *mePtr, /* The entry we are drawing. */
2430 Drawable d, /* What we are drawing into */
2431 Tk_3DBorder activeBorder, /* Border for active items */
2432 Tk_3DBorder bgBorder, /* Border for the background */
2433 int x, /* left edge */
2434 int y, /* top edge */
2435 int width, /* width of rectangle to draw */
2436 int height) /* height of rectangle to draw */
2437 {
2438 if (mePtr->state == ENTRY_ACTIVE
2439 || (mePtr->entryFlags & ENTRY_PLATFORM_FLAG1)!=0 ) {
2440 bgBorder = activeBorder;
2441 }
2442 Tk_Fill3DRectangle(menuPtr->tkwin, d, bgBorder,
2443 x, y, width, height, 0, TK_RELIEF_FLAT);
2444 }
2445
2446 /*
2447 *--------------------------------------------------------------
2448 *
2449 * TkpComputeStandardMenuGeometry --
2450 *
2451 * This procedure is invoked to recompute the size and
2452 * layout of a menu that is not a menubar clone.
2453 *
2454 * Results:
2455 * None.
2456 *
2457 * Side effects:
2458 * Fields of menu entries are changed to reflect their
2459 * current positions, and the size of the menu window
2460 * itself may be changed.
2461 *
2462 *--------------------------------------------------------------
2463 */
2464
2465 void
2466 TkpComputeStandardMenuGeometry(
2467 TkMenu *menuPtr) /* Structure describing menu. */
2468 {
2469 Tk_Font menuFont, tkfont;
2470 Tk_FontMetrics menuMetrics, entryMetrics, *fmPtr;
2471 int x, y, height, width, indicatorSpace, labelWidth, accelWidth;
2472 int windowWidth, windowHeight, accelSpace;
2473 int i, j, lastColumnBreak = 0;
2474 int activeBorderWidth, borderWidth;
2475
2476 if (menuPtr->tkwin == NULL) {
2477 return;
2478 }
2479
2480 Tk_GetPixelsFromObj(menuPtr->interp, menuPtr->tkwin,
2481 menuPtr->borderWidthPtr, &borderWidth);
2482 x = y = borderWidth;
2483 indicatorSpace = labelWidth = accelWidth = 0;
2484 windowHeight = 0;
2485
2486 /*
2487 * On the Mac especially, getting font metrics can be quite slow,
2488 * so we want to do it intelligently. We are going to precalculate
2489 * them and pass them down to all of the measuring and drawing
2490 * routines. We will measure the font metrics of the menu once.
2491 * If an entry does not have its own font set, then we give
2492 * the geometry/drawing routines the menu's font and metrics.
2493 * If an entry has its own font, we will measure that font and
2494 * give all of the geometry/drawing the entry's font and metrics.
2495 */
2496
2497 menuFont = Tk_GetFontFromObj(menuPtr->tkwin, menuPtr->fontPtr);
2498 Tk_GetFontMetrics(menuFont, &menuMetrics);
2499 accelSpace = Tk_TextWidth(menuFont, "M", 1);
2500 Tk_GetPixelsFromObj(menuPtr->interp, menuPtr->tkwin,
2501 menuPtr->activeBorderWidthPtr, &activeBorderWidth);
2502
2503 for (i = 0; i < menuPtr->numEntries; i++) {
2504 if (menuPtr->entries[i]->fontPtr == NULL) {
2505 tkfont = menuFont;
2506 fmPtr = &menuMetrics;
2507 } else {
2508 tkfont = Tk_GetFontFromObj(menuPtr->tkwin,
2509 menuPtr->entries[i]->fontPtr);
2510 Tk_GetFontMetrics(tkfont, &entryMetrics);
2511 fmPtr = &entryMetrics;
2512 }
2513 if ((i > 0) && menuPtr->entries[i]->columnBreak) {
2514 if (accelWidth != 0) {
2515 labelWidth += accelSpace;
2516 }
2517 for (j = lastColumnBreak; j < i; j++) {
2518 menuPtr->entries[j]->indicatorSpace = indicatorSpace;
2519 menuPtr->entries[j]->labelWidth = labelWidth;
2520 menuPtr->entries[j]->width = indicatorSpace + labelWidth
2521 + accelWidth + 2 * activeBorderWidth;
2522 menuPtr->entries[j]->x = x;
2523 menuPtr->entries[j]->entryFlags &= ~ENTRY_LAST_COLUMN;
2524 }
2525 x += indicatorSpace + labelWidth + accelWidth
2526 + 2 * borderWidth;
2527 indicatorSpace = labelWidth = accelWidth = 0;
2528 lastColumnBreak = i;
2529 y = borderWidth;
2530 }
2531
2532 if (menuPtr->entries[i]->type == SEPARATOR_ENTRY) {
2533 GetMenuSeparatorGeometry(menuPtr, menuPtr->entries[i], tkfont,
2534 fmPtr, &width, &height);
2535 menuPtr->entries[i]->height = height;
2536 } else if (menuPtr->entries[i]->type == TEAROFF_ENTRY) {
2537 GetTearoffEntryGeometry(menuPtr, menuPtr->entries[i], tkfont,
2538 fmPtr, &width, &height);
2539 menuPtr->entries[i]->height = height;
2540 } else {
2541
2542 /*
2543 * For each entry, compute the height required by that
2544 * particular entry, plus three widths: the width of the
2545 * label, the width to allow for an indicator to be displayed
2546 * to the left of the label (if any), and the width of the
2547 * accelerator to be displayed to the right of the label
2548 * (if any). These sizes depend, of course, on the type
2549 * of the entry.
2550 */
2551
2552 GetMenuLabelGeometry(menuPtr->entries[i], tkfont, fmPtr, &width,
2553 &height);
2554 menuPtr->entries[i]->height = height;
2555 if (width > labelWidth) {
2556 labelWidth = width;
2557 }
2558
2559 GetMenuAccelGeometry(menuPtr, menuPtr->entries[i], tkfont,
2560 fmPtr, &width, &height);
2561 if (height > menuPtr->entries[i]->height) {
2562 menuPtr->entries[i]->height = height;
2563 }
2564 if (width > accelWidth) {
2565 accelWidth = width;
2566 }
2567
2568 GetMenuIndicatorGeometry(menuPtr, menuPtr->entries[i], tkfont,
2569 fmPtr, &width, &height);
2570 if (height > menuPtr->entries[i]->height) {
2571 menuPtr->entries[i]->height = height;
2572 }
2573 if (width > indicatorSpace) {
2574 indicatorSpace = width;
2575 }
2576
2577 menuPtr->entries[i]->height += 2 * activeBorderWidth + 1;
2578 }
2579 menuPtr->entries[i]->y = y;
2580 y += menuPtr->entries[i]->height;
2581 if (y > windowHeight) {
2582 windowHeight = y;
2583 }
2584 }
2585
2586 if (accelWidth != 0) {
2587 labelWidth += accelSpace;
2588 }
2589 for (j = lastColumnBreak; j < menuPtr->numEntries; j++) {
2590 menuPtr->entries[j]->indicatorSpace = indicatorSpace;
2591 menuPtr->entries[j]->labelWidth = labelWidth;
2592 menuPtr->entries[j]->width = indicatorSpace + labelWidth
2593 + accelWidth + 2 * activeBorderWidth;
2594 menuPtr->entries[j]->x = x;
2595 menuPtr->entries[j]->entryFlags |= ENTRY_LAST_COLUMN;
2596 }
2597 windowWidth = x + indicatorSpace + labelWidth + accelWidth + accelSpace
2598 + 2 * activeBorderWidth + 2 * borderWidth;
2599
2600
2601 windowHeight += borderWidth;
2602
2603 /*
2604 * The X server doesn't like zero dimensions, so round up to at least
2605 * 1 (a zero-sized menu should never really occur, anyway).
2606 */
2607
2608 if (windowWidth <= 0) {
2609 windowWidth = 1;
2610 }
2611 if (windowHeight <= 0) {
2612 windowHeight = 1;
2613 }
2614 menuPtr->totalWidth = windowWidth;
2615 menuPtr->totalHeight = windowHeight;
2616 }
2617
2618 /*
2619 *----------------------------------------------------------------------
2620 *
2621 * MenuSelectEvent --
2622 *
2623 * Generates a "MenuSelect" virtual event. This can be used to
2624 * do context-sensitive menu help.
2625 *
2626 * Results:
2627 * None.
2628 *
2629 * Side effects:
2630 * Places a virtual event on the event queue.
2631 *
2632 *----------------------------------------------------------------------
2633 */
2634
2635 static void
2636 MenuSelectEvent(
2637 TkMenu *menuPtr) /* the menu we have selected. */
2638 {
2639 XVirtualEvent event;
2640 POINTS rootPoint;
2641 DWORD msgPos;
2642
2643 event.type = VirtualEvent;
2644 event.serial = menuPtr->display->request;
2645 event.send_event = 0;
2646 event.display = menuPtr->display;
2647 Tk_MakeWindowExist(menuPtr->tkwin);
2648 event.event = Tk_WindowId(menuPtr->tkwin);
2649 event.root = XRootWindow(menuPtr->display, 0);
2650 event.subwindow = None;
2651 event.time = TkpGetMS();
2652
2653 msgPos = GetMessagePos();
2654 rootPoint = MAKEPOINTS(msgPos);
2655 event.x_root = rootPoint.x;
2656 event.y_root = rootPoint.y;
2657 event.state = TkWinGetModifierState();
2658 event.same_screen = 1;
2659 event.name = Tk_GetUid("MenuSelect");
2660 Tk_QueueWindowEvent((XEvent *) &event, TCL_QUEUE_TAIL);
2661 }
2662
2663 /*
2664 *----------------------------------------------------------------------
2665 *
2666 * TkpMenuNotifyToplevelCreate --
2667 *
2668 * This routine reconfigures the menu and the clones indicated by
2669 * menuName becuase a toplevel has been created and any system
2670 * menus need to be created.
2671 *
2672 * Results:
2673 * None.
2674 *
2675 * Side effects:
2676 * An idle handler is set up to do the reconfiguration.
2677 *
2678 *----------------------------------------------------------------------
2679 */
2680
2681 void
2682 TkpMenuNotifyToplevelCreate(
2683 Tcl_Interp *interp, /* The interp the menu lives in. */
2684 char *menuName) /* The name of the menu to
2685 * reconfigure. */
2686 {
2687 TkMenuReferences *menuRefPtr;
2688 TkMenu *menuPtr;
2689
2690 if ((menuName != NULL) && (menuName[0] != '\0')) {
2691 menuRefPtr = TkFindMenuReferences(interp, menuName);
2692 if ((menuRefPtr != NULL) && (menuRefPtr->menuPtr != NULL)) {
2693 for (menuPtr = menuRefPtr->menuPtr->masterMenuPtr; menuPtr != NULL;
2694 menuPtr = menuPtr->nextInstancePtr) {
2695 if ((menuPtr->menuType == MENUBAR)
2696 && !(menuPtr->menuFlags & MENU_RECONFIGURE_PENDING)) {
2697 menuPtr->menuFlags |= MENU_RECONFIGURE_PENDING;
2698 Tcl_DoWhenIdle(ReconfigureWindowsMenu,
2699 (ClientData) menuPtr);
2700 }
2701 }
2702 }
2703 }
2704 }
2705
2706 /*
2707 *----------------------------------------------------------------------
2708 *
2709 * MenuExitHandler --
2710 *
2711 * Throws away the utility window needed for menus and unregisters
2712 * the class.
2713 *
2714 * Results:
2715 * None.
2716 *
2717 * Side effects:
2718 * Menus have to be reinitialized next time.
2719 *
2720 *----------------------------------------------------------------------
2721 */
2722
2723 static void
2724 MenuExitHandler(
2725 ClientData clientData) /* Not used */
2726 {
2727 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
2728 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
2729
2730 DestroyWindow(tsdPtr->menuHWND);
2731 UnregisterClass(MENU_CLASS_NAME, Tk_GetHINSTANCE());
2732 }
2733
2734 /*
2735 *----------------------------------------------------------------------
2736 *
2737 * TkWinGetMenuSystemDefault --
2738 *
2739 * Gets the Windows specific default value for a given X resource
2740 * database name.
2741 *
2742 * Results:
2743 * Returns a Tcl_Obj * with the default value. If there is no
2744 * Windows-specific default for this attribute, returns NULL.
2745 * This object has a ref count of 0.
2746 *
2747 * Side effects:
2748 * Storage is allocated.
2749 *
2750 *----------------------------------------------------------------------
2751 */
2752
2753 Tcl_Obj *
2754 TkWinGetMenuSystemDefault(
2755 Tk_Window tkwin, /* A window to use. */
2756 CONST char *dbName, /* The option database name. */
2757 CONST char *className) /* The name of the option class. */
2758 {
2759 Tcl_Obj *valuePtr = NULL;
2760
2761 if ((strcmp(dbName, "activeBorderWidth") == 0) ||
2762 (strcmp(dbName, "borderWidth") == 0)) {
2763 valuePtr = Tcl_NewIntObj(defaultBorderWidth);
2764 } else if (strcmp(dbName, "font") == 0) {
2765 valuePtr = Tcl_NewStringObj(Tcl_DStringValue(&menuFontDString),
2766 -1);
2767 }
2768
2769 return valuePtr;
2770 }
2771
2772 /*
2773 *----------------------------------------------------------------------
2774 *
2775 * TkWinMenuSetDefaults --
2776 *
2777 * Sets up the hash tables and the variables used by the menu package.
2778 *
2779 * Results:
2780 * None.
2781 *
2782 * Side effects:
2783 * lastMenuID gets initialized, and the parent hash and the command hash
2784 * are allocated.
2785 *
2786 *----------------------------------------------------------------------
2787 */
2788
2789 void
2790 SetDefaults(
2791 int firstTime) /* Is this the first time this
2792 * has been called? */
2793 {
2794 char sizeString[TCL_INTEGER_SPACE];
2795 char faceName[LF_FACESIZE];
2796 HDC scratchDC;
2797 Tcl_DString boldItalicDString;
2798 int bold = 0;
2799 int italic = 0;
2800 TEXTMETRIC tm;
2801 int pointSize;
2802 HFONT menuFont;
2803 NONCLIENTMETRICS ncMetrics;
2804
2805 /*
2806 * Set all of the default options. The loop will terminate when we run
2807 * out of options via a break statement.
2808 */
2809
2810 defaultBorderWidth = GetSystemMetrics(SM_CXBORDER);
2811 if (GetSystemMetrics(SM_CYBORDER) > defaultBorderWidth) {
2812 defaultBorderWidth = GetSystemMetrics(SM_CYBORDER);
2813 }
2814
2815 scratchDC = CreateDC("DISPLAY", NULL, NULL, NULL);
2816 if (!firstTime) {
2817 Tcl_DStringFree(&menuFontDString);
2818 }
2819 Tcl_DStringInit(&menuFontDString);
2820
2821 ncMetrics.cbSize = sizeof(ncMetrics);
2822 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncMetrics),
2823 &ncMetrics, 0);
2824 menuFont = CreateFontIndirect(&ncMetrics.lfMenuFont);
2825 SelectObject(scratchDC, menuFont);
2826 GetTextMetrics(scratchDC, &tm);
2827 GetTextFace(scratchDC, LF_FACESIZE, faceName);
2828 pointSize = MulDiv(tm.tmHeight - tm.tmInternalLeading,
2829 72, GetDeviceCaps(scratchDC, LOGPIXELSY));
2830 if (tm.tmWeight >= 700) {
2831 bold = 1;
2832 }
2833 if (tm.tmItalic) {
2834 italic = 1;
2835 }
2836
2837 SelectObject(scratchDC, GetStockObject(SYSTEM_FONT));
2838 DeleteDC(scratchDC);
2839
2840 DeleteObject(menuFont);
2841
2842 Tcl_DStringAppendElement(&menuFontDString, faceName);
2843 sprintf(sizeString, "%d", pointSize);
2844 Tcl_DStringAppendElement(&menuFontDString, sizeString);
2845
2846 if (bold == 1 || italic == 1) {
2847 Tcl_DStringInit(&boldItalicDString);
2848 if (bold == 1) {
2849 Tcl_DStringAppendElement(&boldItalicDString, "bold");
2850 }
2851 if (italic == 1) {
2852 Tcl_DStringAppendElement(&boldItalicDString, "italic");
2853 }
2854 Tcl_DStringAppendElement(&menuFontDString,
2855 Tcl_DStringValue(&boldItalicDString));
2856 }
2857
2858 /*
2859 * Now we go ahead and get the dimensions of the check mark and the
2860 * appropriate margins. Since this is fairly hairy, we do it here
2861 * to save time when traversing large sets of menu items.
2862 *
2863 * The code below was given to me by Microsoft over the phone. It
2864 * is the only way to insure menu items lining up, and is not
2865 * documented.
2866 */
2867
2868 if (TkWinGetPlatformId() >= VER_PLATFORM_WIN32_WINDOWS) {
2869 indicatorDimensions[0] = GetSystemMetrics(SM_CYMENUCHECK);
2870 indicatorDimensions[1] = ((GetSystemMetrics(SM_CXFIXEDFRAME) +
2871 GetSystemMetrics(SM_CXBORDER)
2872 + GetSystemMetrics(SM_CXMENUCHECK) + 7) & 0xFFF8)
2873 - GetSystemMetrics(SM_CXFIXEDFRAME);
2874 } else {
2875 DWORD dimensions = GetMenuCheckMarkDimensions();
2876 indicatorDimensions[0] = HIWORD(dimensions);
2877 indicatorDimensions[1] = LOWORD(dimensions);
2878 }
2879 }
2880
2881 /*
2882 *----------------------------------------------------------------------
2883 *
2884 * TkpMenuInit --
2885 *
2886 * Sets up the process-wide variables used by the menu package.
2887 *
2888 * Results:
2889 * None.
2890 *
2891 * Side effects:
2892 * lastMenuID gets initialized.
2893 *
2894 *----------------------------------------------------------------------
2895 */
2896
2897 void
2898 TkpMenuInit()
2899 {
2900 WNDCLASS wndClass;
2901 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
2902 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
2903
2904 wndClass.style = CS_OWNDC;
2905 wndClass.lpfnWndProc = TkWinMenuProc;
2906 wndClass.cbClsExtra = 0;
2907 wndClass.cbWndExtra = 0;
2908 wndClass.hInstance = Tk_GetHINSTANCE();
2909 wndClass.hIcon = NULL;
2910 wndClass.hCursor = NULL;
2911 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2912 wndClass.lpszMenuName = NULL;
2913 wndClass.lpszClassName = MENU_CLASS_NAME;
2914 RegisterClass(&wndClass);
2915
2916 tsdPtr->menuHWND = CreateWindow(MENU_CLASS_NAME, "MenuWindow", WS_POPUP,
2917 0, 0, 10, 10, NULL, NULL, Tk_GetHINSTANCE(), NULL);
2918
2919 Tcl_CreateExitHandler(MenuExitHandler, (ClientData) NULL);
2920 SetDefaults(1);
2921 }
2922
2923 /*
2924 *----------------------------------------------------------------------
2925 *
2926 * TkpMenuThreadInit --
2927 *
2928 * Sets up the thread-local hash tables used by the menu module.
2929 *
2930 * Results:
2931 * None.
2932 *
2933 * Side effects:
2934 * Hash tables winMenuTable and commandTable are initialized.
2935 *
2936 *----------------------------------------------------------------------
2937 */
2938
2939 void
2940 TkpMenuThreadInit()
2941 {
2942 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
2943 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
2944
2945 Tcl_InitHashTable(&tsdPtr->winMenuTable, TCL_ONE_WORD_KEYS);
2946 Tcl_InitHashTable(&tsdPtr->commandTable, TCL_ONE_WORD_KEYS);
2947 }
2948