-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtkWinMenu.c
More file actions
3549 lines (3178 loc) · 97.7 KB
/
Copy pathtkWinMenu.c
File metadata and controls
3549 lines (3178 loc) · 97.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tkWinMenu.c --
*
* This module implements the Windows platform-specific features of
* menus.
*
* Copyright © 1996-1998 Sun Microsystems, Inc.
* Copyright © 1998-1999 Scriptics Corporation.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#define OEMRESOURCE
#include "tkWinInt.h"
#include "tkMenu.h"
/*
* The class of the window for popup menus.
*/
#define MENU_CLASS_NAME L"MenuWindowClass"
#define EMBEDDED_MENU_CLASS_NAME L"EmbeddedMenuWindowClass"
/*
* Used to align a windows bitmap inside a rectangle
*/
#define ALIGN_BITMAP_LEFT 0x00000001
#define ALIGN_BITMAP_RIGHT 0x00000002
#define ALIGN_BITMAP_TOP 0x00000004
#define ALIGN_BITMAP_BOTTOM 0x00000008
/*
* Platform-specific menu flags:
*
* MENU_SYSTEM_MENU Non-zero means that the Windows menu handle was
* retrieved with GetSystemMenu and needs to be disposed
* of specially.
* MENU_RECONFIGURE_PENDING
* Non-zero means that an idle handler has been set up to
* reconfigure the Windows menu handle for this menu.
*/
#define MENU_SYSTEM_MENU MENU_PLATFORM_FLAG1
#define MENU_RECONFIGURE_PENDING MENU_PLATFORM_FLAG2
/*
* ODS_NOACCEL flag forbids drawing accelerator cues (i.e. underlining labels)
* on Windows 2000 and above. The ODS_NOACCEL define is missing from mingw32
* headers and undefined for _WIN32_WINNT < 0x0500 in Microsoft SDK. We might
* check for _WIN32_WINNT here, but I think it's not needed, as checking for
* this flag does no harm on even on NT: reserved bits should be zero, and in
* fact they are.
*/
#ifndef ODS_NOACCEL
#define ODS_NOACCEL 0x100
#endif
#ifndef SPI_GETKEYBOARDCUES
#define SPI_GETKEYBOARDCUES 0x100A
#endif
#ifndef WM_UPDATEUISTATE
#define WM_UPDATEUISTATE 0x0128
#endif
#ifndef UIS_SET
#define UIS_SET 1
#endif
#ifndef UIS_CLEAR
#define UIS_CLEAR 2
#endif
#ifndef UISF_HIDEACCEL
#define UISF_HIDEACCEL 2
#endif
#ifndef WM_UNINITMENUPOPUP
#define WM_UNINITMENUPOPUP 0x0125
#endif
static int indicatorDimensions[2];
/* The dimensions of the indicator space in a
* menu entry. Calculated at init time to save
* time. */
static BOOL showMenuAccelerators;
typedef struct {
int inPostMenu; /* We cannot be re-entrant like X Windows. */
WORD lastCommandID; /* The last command ID we allocated. */
HWND menuHWND; /* A window to service popup-menu messages
* in. */
HWND embeddedMenuHWND; /* A window to service embedded menu
* messages */
int oldServiceMode; /* Used while processing a menu; we need to
* set the event mode specially when we enter
* the menu processing modal loop and reset it
* when menus go away. */
TkMenu *modalMenuPtr; /* The menu we are processing inside the modal
* loop. We need this to reset all of the
* active items when menus go away since
* Windows does not see fit to give this to us
* when it sends its WM_MENUSELECT. */
Tcl_HashTable commandTable; /* A map of command ids to menu entries */
Tcl_HashTable winMenuTable; /* Need this to map HMENUs back to menuPtrs */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* The following are default menu value strings.
*/
static int defaultBorderWidth; /* The windows default border width. */
static Tcl_DString menuFontDString;
/* A buffer to store the default menu font
* string. */
/*
* Forward declarations for functions defined later in this file:
*/
static void DrawMenuEntryAccelerator(TkMenu *menuPtr,
TkMenuEntry *mePtr, Drawable d, GC gc,
Tk_Font tkfont, const Tk_FontMetrics *fmPtr,
Tk_3DBorder activeBorder, int x, int y,
int width, int height);
static void DrawMenuEntryArrow(TkMenu *menuPtr, TkMenuEntry *mePtr,
Drawable d, GC gc, Tk_3DBorder activeBorder,
int x,int y, int width, int height, int drawArrow);
static void DrawMenuEntryBackground(TkMenu *menuPtr,
TkMenuEntry *mePtr, Drawable d,
Tk_3DBorder activeBorder, Tk_3DBorder bgBorder,
int x, int y, int width, int heigth);
static void DrawMenuEntryIndicator(TkMenu *menuPtr,
TkMenuEntry *mePtr, Drawable d, GC gc,
GC indicatorGC, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr, int x, int y,
int width, int height);
static void DrawMenuEntryLabel(TkMenu *menuPtr, TkMenuEntry *mePtr,
Drawable d, GC gc, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr, int x, int y,
int width, int height, int underline);
static void DrawMenuSeparator(TkMenu *menuPtr, TkMenuEntry *mePtr,
Drawable d, GC gc, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr,
int x, int y, int width, int height);
static void DrawTearoffEntry(TkMenu *menuPtr, TkMenuEntry *mePtr,
Drawable d, GC gc, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr, int x, int y,
int width, int height);
static void DrawMenuUnderline(TkMenu *menuPtr, TkMenuEntry *mePtr,
Drawable d, GC gc, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr, int x, int y,
int width, int height);
static void DrawWindowsSystemBitmap(Display *display,
Drawable drawable, GC gc, const RECT *rectPtr,
int bitmapID, int alignFlags);
static void FreeID(WORD commandID);
static char * GetEntryText(TkMenu *menuPtr, TkMenuEntry *mePtr);
static void GetMenuAccelGeometry(TkMenu *menuPtr,
TkMenuEntry *mePtr, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr, int *widthPtr,
int *heightPtr);
static void GetMenuLabelGeometry(TkMenuEntry *mePtr,
Tk_Font tkfont, const Tk_FontMetrics *fmPtr,
int *widthPtr, int *heightPtr);
static void GetMenuIndicatorGeometry(TkMenu *menuPtr,
TkMenuEntry *mePtr, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr,
int *widthPtr, int *heightPtr);
static void GetMenuSeparatorGeometry(TkMenu *menuPtr,
TkMenuEntry *mePtr, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr,
int *widthPtr, int *heightPtr);
static void GetTearoffEntryGeometry(TkMenu *menuPtr,
TkMenuEntry *mePtr, Tk_Font tkfont,
const Tk_FontMetrics *fmPtr, int *widthPtr,
int *heightPtr);
static int GetNewID(TkMenuEntry *mePtr, WORD *menuIDPtr);
static Tcl_ObjCmdProc TkWinMenuKeyObjCmd;
static void MenuSelectEvent(TkMenu *menuPtr);
static void ReconfigureWindowsMenu(void *clientData);
static void RecursivelyClearActiveMenu(TkMenu *menuPtr);
static void SetDefaults(int firstTime);
static LRESULT CALLBACK TkWinMenuProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam);
static LRESULT CALLBACK TkWinEmbeddedMenuProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam);
static inline void
ScheduleMenuReconfigure(
TkMenu *menuPtr)
{
if (!(menuPtr->menuFlags & MENU_RECONFIGURE_PENDING)) {
menuPtr->menuFlags |= MENU_RECONFIGURE_PENDING;
Tcl_DoWhenIdle(ReconfigureWindowsMenu, menuPtr);
}
}
static inline void
CallPendingReconfigureImmediately(
TkMenu *menuPtr)
{
if (menuPtr->menuFlags & MENU_RECONFIGURE_PENDING) {
Tcl_CancelIdleCall(ReconfigureWindowsMenu, menuPtr);
ReconfigureWindowsMenu(menuPtr);
}
}
/*
*----------------------------------------------------------------------
*
* GetNewID --
*
* Allocates a new menu id and marks it in use.
*
* Results:
* Returns TCL_OK if succesful; TCL_ERROR if there are no more ids of the
* appropriate type to allocate. menuIDPtr contains the new id if
* succesful.
*
* Side effects:
* An entry is created for the menu in the command hash table, and the
* hash entry is stored in the appropriate field in the menu data
* structure.
*
*----------------------------------------------------------------------
*/
static int
GetNewID(
TkMenuEntry *mePtr, /* The menu we are working with. */
WORD *menuIDPtr) /* The resulting id. */
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
WORD curID = tsdPtr->lastCommandID;
while (1) {
Tcl_HashEntry *commandEntryPtr;
int isNew;
/*
* Try the next ID number, taking care to wrap rather than stray
* into the system menu IDs. [Bug 3235256]
*/
if (++curID >= 0xF000) {
curID = 1;
}
/* Return error when we've checked all IDs without success. */
if (curID == tsdPtr->lastCommandID) {
return TCL_ERROR;
}
commandEntryPtr = Tcl_CreateHashEntry(&tsdPtr->commandTable,
INT2PTR(curID), &isNew);
if (isNew) {
Tcl_SetHashValue(commandEntryPtr, mePtr);
*menuIDPtr = curID;
tsdPtr->lastCommandID = curID;
return TCL_OK;
}
}
}
/*
*----------------------------------------------------------------------
*
* FreeID --
*
* Marks the itemID as free.
*
* Results:
* None.
*
* Side effects:
* The hash table entry for the ID is cleared.
*
*----------------------------------------------------------------------
*/
static void
FreeID(
WORD commandID)
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
/*
* If the menuHWND is NULL, this table has been finalized already.
*/
if (tsdPtr->menuHWND != NULL) {
Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(&tsdPtr->commandTable,
INT2PTR(commandID));
if (entryPtr != NULL) {
Tcl_DeleteHashEntry(entryPtr);
}
}
}
/*
*----------------------------------------------------------------------
*
* TkpNewMenu --
*
* Gets a new blank menu. Only the platform specific options are filled
* in.
*
* Results:
* Standard TCL error.
*
* Side effects:
* Allocates a Windows menu handle and places it in the platformData
* field of the menuPtr.
*
*----------------------------------------------------------------------
*/
int
TkpNewMenu(
TkMenu *menuPtr) /* The common structure we are making the
* platform structure for. */
{
HMENU winMenuHdl;
Tcl_HashEntry *hashEntryPtr;
int newEntry;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
winMenuHdl = CreatePopupMenu();
if (winMenuHdl == NULL) {
Tcl_SetObjResult(menuPtr->interp, Tcl_NewStringObj(
"No more menus can be allocated.", TCL_INDEX_NONE));
Tcl_SetErrorCode(menuPtr->interp, "TK", "MENU", "SYSTEM_RESOURCES", (char *)NULL);
return TCL_ERROR;
}
/*
* We hash all of the HMENU's so that we can get their menu ptrs back when
* dispatch messages.
*/
hashEntryPtr = Tcl_CreateHashEntry(&tsdPtr->winMenuTable,
(char *) winMenuHdl, &newEntry);
Tcl_SetHashValue(hashEntryPtr, menuPtr);
menuPtr->platformData = (TkMenuPlatformData) winMenuHdl;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkpDestroyMenu --
*
* Destroys platform-specific menu structures.
*
* Results:
* None.
*
* Side effects:
* All platform-specific allocations are freed up.
*
*----------------------------------------------------------------------
*/
void
TkpDestroyMenu(
TkMenu *menuPtr) /* The common menu structure */
{
HMENU winMenuHdl = (HMENU) menuPtr->platformData;
const char *searchName;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
if (menuPtr->menuFlags & MENU_RECONFIGURE_PENDING) {
Tcl_CancelIdleCall(ReconfigureWindowsMenu, menuPtr);
}
if (winMenuHdl == NULL) {
return;
}
if (menuPtr->menuFlags & MENU_SYSTEM_MENU) {
TkMenuEntry *searchEntryPtr;
Tcl_HashTable *tablePtr = TkGetMenuHashTable(menuPtr->interp);
char *menuName = (char *)Tcl_GetHashKey(tablePtr,
menuPtr->menuRefPtr->hashEntryPtr);
/*
* Search for the menu in the menubar, if it is present, get the
* wrapper window associated with the toplevel and reset its
* system menu to the default menu.
*/
for (searchEntryPtr = menuPtr->menuRefPtr->parentEntryPtr;
searchEntryPtr != NULL;
searchEntryPtr = searchEntryPtr->nextCascadePtr) {
searchName = Tcl_GetString(searchEntryPtr->namePtr);
if (strcmp(searchName, menuName) == 0) {
Tk_Window parentTopLevelPtr = searchEntryPtr
->menuPtr->parentTopLevelPtr;
if (parentTopLevelPtr != NULL) {
GetSystemMenu(
TkWinGetWrapperWindow(parentTopLevelPtr), TRUE);
}
break;
}
}
} else {
/*
* Remove the menu from the menu hash table, then destroy the handle.
* If the menuHWND is NULL, this table has been finalized already.
*/
if (tsdPtr->menuHWND != NULL) {
Tcl_HashEntry *hashEntryPtr =
Tcl_FindHashEntry(&tsdPtr->winMenuTable, winMenuHdl);
if (hashEntryPtr != NULL) {
Tcl_DeleteHashEntry(hashEntryPtr);
}
}
DestroyMenu(winMenuHdl);
}
menuPtr->platformData = NULL;
if (menuPtr == tsdPtr->modalMenuPtr) {
tsdPtr->modalMenuPtr = NULL;
}
}
/*
*----------------------------------------------------------------------
*
* TkpDestroyMenuEntry --
*
* Cleans up platform-specific menu entry items.
*
* Results:
* None
*
* Side effects:
* All platform-specific allocations are freed up.
*
*----------------------------------------------------------------------
*/
void
TkpDestroyMenuEntry(
TkMenuEntry *mePtr) /* The entry to destroy */
{
TkMenu *menuPtr = mePtr->menuPtr;
HMENU winMenuHdl = (HMENU) menuPtr->platformData;
if (NULL != winMenuHdl) {
ScheduleMenuReconfigure(menuPtr);
}
FreeID((WORD) PTR2INT(mePtr->platformEntryData));
mePtr->platformEntryData = NULL;
}
/*
*----------------------------------------------------------------------
*
* GetEntryText --
*
* Given a menu entry, gives back the text that should go in it.
* Separators should be done by the caller, as they have to be handled
* specially. Allocates the memory with alloc. The caller should free the
* memory.
*
* Results:
* itemText points to the new text for the item.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static char *
GetEntryText(
TkMenu *menuPtr, /* The menu considered. */
TkMenuEntry *mePtr) /* A pointer to the menu entry. */
{
char *itemText;
if (mePtr->type == TEAROFF_ENTRY) {
itemText = (char *)ckalloc(sizeof("(Tear-off)"));
strcpy(itemText, "(Tear-off)");
} else if (mePtr->imagePtr != NULL) {
itemText = (char *)ckalloc(sizeof("(Image)"));
strcpy(itemText, "(Image)");
} else if (mePtr->bitmapPtr != NULL) {
itemText = (char *)ckalloc(sizeof("(Pixmap)"));
strcpy(itemText, "(Pixmap)");
} else if (mePtr->labelPtr == NULL || mePtr->labelLength == 0) {
itemText = (char *)ckalloc(sizeof("( )"));
strcpy(itemText, "( )");
} else {
int i;
const char *label = (mePtr->labelPtr == NULL) ? ""
: Tcl_GetString(mePtr->labelPtr);
const char *accel = ((menuPtr->menuType == MENUBAR) || (mePtr->accelPtr == NULL)) ? ""
: Tcl_GetString(mePtr->accelPtr);
const char *p, *next;
Tcl_DString itemString;
Tcl_UniChar ch = 0;
/*
* We have to construct the string with an ampersand preceding the
* underline character, and a tab separating the text and the accel
* text. We have to be careful with ampersands in the string.
*/
Tcl_DStringInit(&itemString);
for (p = label, i = 0; *p != '\0'; i++, p = next) {
if (i == mePtr->underline) {
Tcl_DStringAppend(&itemString, "&", 1);
}
if (*p == '&') {
Tcl_DStringAppend(&itemString, "&", 1);
}
next = p + Tcl_UtfToUniChar(p, &ch);
Tcl_DStringAppend(&itemString, p, (int) (next - p));
}
ch = 0;
if (mePtr->accelLength > 0) {
Tcl_DStringAppend(&itemString, "\t", 1);
for (p = accel, i = 0; *p != '\0'; i++, p = next) {
if (*p == '&') {
Tcl_DStringAppend(&itemString, "&", 1);
}
next = p + Tcl_UtfToUniChar(p, &ch);
Tcl_DStringAppend(&itemString, p, (int) (next - p));
}
}
itemText = (char *)ckalloc(Tcl_DStringLength(&itemString) + 1);
strcpy(itemText, Tcl_DStringValue(&itemString));
Tcl_DStringFree(&itemString);
}
return itemText;
}
/*
*----------------------------------------------------------------------
*
* ReconfigureWindowsMenu --
*
* Tears down and rebuilds the platform-specific part of this menu.
*
* Results:
* None.
*
* Side effects:
* Configuration information get set for mePtr; old resources get freed,
* if any need it.
*
*----------------------------------------------------------------------
*/
static void
ReconfigureWindowsMenu(
void *clientData) /* The menu we are rebuilding */
{
TkMenu *menuPtr = (TkMenu *)clientData;
TkMenuEntry *mePtr;
HMENU winMenuHdl = (HMENU) menuPtr->platformData;
char *itemText = NULL;
LPCWSTR lpNewItem;
UINT flags;
UINT itemID;
int i, count, systemMenu = 0, base;
Tcl_DString translatedText;
if (NULL == winMenuHdl) {
return;
}
/*
* Reconstruct the entire menu. Takes care of nasty system menu and index
* problem.
*/
base = (menuPtr->menuFlags & MENU_SYSTEM_MENU) ? 7 : 0;
count = GetMenuItemCount(winMenuHdl);
for (i = base; i < count; i++) {
RemoveMenu(winMenuHdl, base, MF_BYPOSITION);
}
count = menuPtr->numEntries;
for (i = 0; i < count; i++) {
mePtr = menuPtr->entries[i];
lpNewItem = NULL;
flags = MF_BYPOSITION;
itemID = 0;
Tcl_DStringInit(&translatedText);
if ((menuPtr->menuType == MENUBAR) && (mePtr->type == TEAROFF_ENTRY)) {
continue;
}
itemText = GetEntryText(menuPtr, mePtr);
if ((menuPtr->menuType == MENUBAR)
|| (menuPtr->menuFlags & MENU_SYSTEM_MENU)) {
Tcl_DStringInit(&translatedText);
Tcl_UtfToWCharDString(itemText, TCL_INDEX_NONE, &translatedText);
lpNewItem = (LPCWSTR) Tcl_DStringValue(&translatedText);
flags |= MF_STRING;
} else {
lpNewItem = (LPCWSTR) mePtr;
flags |= MF_OWNERDRAW;
}
/*
* Set enabling and disabling correctly.
*/
if (mePtr->state == ENTRY_DISABLED) {
flags |= MF_DISABLED | MF_GRAYED;
}
/*
* Set the check mark for check entries and radio entries.
*/
if (((mePtr->type == CHECK_BUTTON_ENTRY)
|| (mePtr->type == RADIO_BUTTON_ENTRY))
&& (mePtr->entryFlags & ENTRY_SELECTED)) {
flags |= MF_CHECKED;
}
/*
* Set the SEPARATOR bit for separator entries. This bit is not used
* by our internal drawing functions, but it is used by the system
* when drawing the system menu (we do not draw the system menu
* ourselves). If this bit is not set, separator entries on the system
* menu will not be drawn correctly.
*/
if (mePtr->type == SEPARATOR_ENTRY) {
flags |= MF_SEPARATOR;
}
if (mePtr->columnBreak) {
flags |= MF_MENUBREAK;
}
itemID = PTR2INT(mePtr->platformEntryData);
if ((mePtr->type == CASCADE_ENTRY)
&& (mePtr->childMenuRefPtr != NULL)
&& (mePtr->childMenuRefPtr->menuPtr != NULL)) {
HMENU childMenuHdl = (HMENU) mePtr->childMenuRefPtr->menuPtr
->platformData;
if (childMenuHdl != NULL) {
/*
* Win32 draws the popup arrow in the wrong color for a
* disabled cascade menu, so do it by hand. Given it is
* disabled, there's no need for it to be connected to its
* child.
*/
if (mePtr->state != ENTRY_DISABLED) {
flags |= MF_POPUP;
/*
* If the MF_POPUP flag is set, then the id is interpreted
* as the handle of a submenu.
*/
itemID = PTR2INT(childMenuHdl);
}
}
if ((menuPtr->menuType == MENUBAR)
&& !(mePtr->childMenuRefPtr->menuPtr->menuFlags
& MENU_SYSTEM_MENU)) {
Tcl_DString ds;
TkMenuReferences *menuRefPtr;
TkMenu *systemMenuPtr = mePtr->childMenuRefPtr->menuPtr;
Tcl_DStringInit(&ds);
Tcl_DStringAppend(&ds,
Tk_PathName(menuPtr->mainMenuPtr->tkwin), TCL_INDEX_NONE);
Tcl_DStringAppend(&ds, ".system", 7);
menuRefPtr = TkFindMenuReferences(menuPtr->interp,
Tcl_DStringValue(&ds));
Tcl_DStringFree(&ds);
if ((menuRefPtr != NULL)
&& (menuRefPtr->menuPtr != NULL)
&& (menuPtr->parentTopLevelPtr != NULL)
&& (systemMenuPtr->mainMenuPtr
== menuRefPtr->menuPtr)) {
HMENU systemMenuHdl = (HMENU) systemMenuPtr->platformData;
HWND wrapper = TkWinGetWrapperWindow(menuPtr
->parentTopLevelPtr);
if (wrapper != NULL) {
DestroyMenu(systemMenuHdl);
systemMenuHdl = GetSystemMenu(wrapper, FALSE);
systemMenuPtr->menuFlags |= MENU_SYSTEM_MENU;
systemMenuPtr->platformData =
(TkMenuPlatformData) systemMenuHdl;
ScheduleMenuReconfigure(systemMenuPtr);
}
}
}
if (mePtr->childMenuRefPtr->menuPtr->menuFlags
& MENU_SYSTEM_MENU) {
systemMenu++;
}
}
if (!systemMenu) {
InsertMenuW(winMenuHdl, 0xFFFFFFFF, flags, itemID, lpNewItem);
}
Tcl_DStringFree(&translatedText);
if (itemText != NULL) {
ckfree(itemText);
itemText = NULL;
}
}
if ((menuPtr->menuType == MENUBAR)
&& (menuPtr->parentTopLevelPtr != NULL)) {
HWND bar = TkWinGetWrapperWindow(menuPtr->parentTopLevelPtr);
if (bar) {
DrawMenuBar(bar);
}
}
menuPtr->menuFlags &= ~(MENU_RECONFIGURE_PENDING);
}
/*
*----------------------------------------------------------------------
*
* TkpPostMenu --
*
* Posts a menu on the screen so that the top left corner of the
* specified entry is located at the point (x, y) in screen coordinates.
* If the entry parameter is negative, the upper left corner of the
* menu itself is placed at the point.
*
* Results:
* None.
*
* Side effects:
* The menu is posted and handled.
*
*----------------------------------------------------------------------
*/
int
TkpPostMenu(
TCL_UNUSED(Tcl_Interp *),
TkMenu *menuPtr,
int x, int y, Tcl_Size index)
{
HMENU winMenuHdl = (HMENU) menuPtr->platformData;
int result, flags;
RECT noGoawayRect;
POINT point;
Tk_Window parentWindow = Tk_Parent(menuPtr->tkwin);
int oldServiceMode = Tcl_GetServiceMode();
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
tsdPtr->inPostMenu++;
CallPendingReconfigureImmediately(menuPtr);
result = TkPreprocessMenu(menuPtr);
if (result != TCL_OK) {
tsdPtr->inPostMenu--;
return result;
}
if (index >= menuPtr->numEntries) {
index = menuPtr->numEntries - 1;
}
if (index >= 0) {
y -= menuPtr->entries[index]->y;
}
/*
* The post commands could have deleted the menu, which means
* we are dead and should go away.
*/
if (menuPtr->tkwin == NULL) {
tsdPtr->inPostMenu--;
return TCL_OK;
}
if (NULL == parentWindow) {
noGoawayRect.top = y - 50;
noGoawayRect.bottom = y + 50;
noGoawayRect.left = x - 50;
noGoawayRect.right = x + 50;
} else {
int left, top;
Tk_GetRootCoords(parentWindow, &left, &top);
noGoawayRect.left = left;
noGoawayRect.top = top;
noGoawayRect.bottom = noGoawayRect.top + Tk_Height(parentWindow);
noGoawayRect.right = noGoawayRect.left + Tk_Width(parentWindow);
}
Tcl_SetServiceMode(TCL_SERVICE_NONE);
/*
* Make an assumption here. If the right button is down,
* then we want to track it. Otherwise, track the left mouse button.
*/
flags = TPM_LEFTALIGN;
if (GetSystemMetrics(SM_SWAPBUTTON)) {
if (GetAsyncKeyState(VK_LBUTTON) < 0) {
flags |= TPM_RIGHTBUTTON;
} else {
flags |= TPM_LEFTBUTTON;
}
} else {
if (GetAsyncKeyState(VK_RBUTTON) < 0) {
flags |= TPM_RIGHTBUTTON;
} else {
flags |= TPM_LEFTBUTTON;
}
}
TrackPopupMenu(winMenuHdl, flags, x, y, 0,
tsdPtr->menuHWND, &noGoawayRect);
Tcl_SetServiceMode(oldServiceMode);
GetCursorPos(&point);
TkWinPointerEvent(NULL, point.x, point.y);
if (tsdPtr->inPostMenu) {
tsdPtr->inPostMenu = 0;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkpPostTearoffMenu --
*
* Posts a tearoff menu on the screen so that the top left corner of the
* specified entry is located at the point (x, y) in screen coordinates.
* If the index parameter is negative, the upper left corner of the menu
* itself is placed at the point. Adjusts the menu's position so that it
* fits on the screen, and maps and raises the menu.
*
* Results:
* Returns a standard Tcl Error.
*
* Side effects:
* The menu is posted.
*
*----------------------------------------------------------------------
*/
int
TkpPostTearoffMenu(
TCL_UNUSED(Tcl_Interp *), /* The interpreter of the menu */
TkMenu *menuPtr, /* The menu we are posting */
int x, int y, Tcl_Size index) /* The root X,Y coordinates where we are
* posting */
{
int vRootX, vRootY, vRootWidth, vRootHeight;
int result;
TkActivateMenuEntry(menuPtr, TCL_INDEX_NONE);
TkRecomputeMenu(menuPtr);
result = TkPostCommand(menuPtr);
if (result != TCL_OK) {
return result;
}
/*
* The post commands could have deleted the menu, which means we are dead
* and should go away.
*/
if (menuPtr->tkwin == NULL) {
return TCL_OK;
}
/*
* Adjust the menu y position so that the specified entry will be located
* at the given coordinates.
*/
if (index >= menuPtr->numEntries) {
index = menuPtr->numEntries - 1;
}
if (index >= 0) {
y -= menuPtr->entries[index]->y;
}
/*
* Adjust the position of the menu if necessary to keep it visible on the
* screen. There are two special tricks to make this work right:
*
* 1. If a virtual root window manager is being used then the coordinates
* are in the virtual root window of menuPtr's parent; since the menu
* uses override-redirect mode it will be in the *real* root window for
* the screen, so we have to map the coordinates from the virtual root
* (if any) to the real root. Can't get the virtual root from the menu
* itself (it will never be seen by the wm) so use its parent instead
* (it would be better to have an an option that names a window to use
* for this...).
* 2. The menu may not have been mapped yet, so its current size might be
* the default 1x1. To compute how much space it needs, use its
* requested size, not its actual size.
*/
Tk_GetVRootGeometry(Tk_Parent(menuPtr->tkwin), &vRootX, &vRootY,
&vRootWidth, &vRootHeight);
vRootWidth -= Tk_ReqWidth(menuPtr->tkwin);
if (x > vRootX + vRootWidth) {
x = vRootX + vRootWidth;
}
if (x < vRootX) {
x = vRootX;
}
vRootHeight -= Tk_ReqHeight(menuPtr->tkwin);
if (y > vRootY + vRootHeight) {
y = vRootY + vRootHeight;
}
if (y < vRootY) {
y = vRootY;
}
Tk_MoveToplevelWindow(menuPtr->tkwin, x, y);
if (!Tk_IsMapped(menuPtr->tkwin)) {
Tk_MapWindow(menuPtr->tkwin);
}
TkWmRestackToplevel((TkWindow *) menuPtr->tkwin, Above, NULL);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkpMenuNewEntry --
*
* Adds a pointer to a new menu entry structure with the platform-
* specific fields filled in.
*
* Results:
* Standard TCL error.
*
* Side effects:
* A new command ID is allocated and stored in the platformEntryData
* field of mePtr.
*
*----------------------------------------------------------------------
*/
int
TkpMenuNewEntry(
TkMenuEntry *mePtr)
{
WORD commandID;
TkMenu *menuPtr = mePtr->menuPtr;
if (GetNewID(mePtr, &commandID) != TCL_OK) {
return TCL_ERROR;
}
ScheduleMenuReconfigure(menuPtr);
mePtr->platformEntryData = (TkMenuPlatformEntryData) INT2PTR(commandID);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkWinMenuProc --
*
* The window proc for the dummy window we put popups in. This allows
* is to post a popup whether or not we know what the parent window
* is.
*
* Results:
* Returns whatever is appropriate for the message in question.
*
* Side effects:
* Normal side-effect for windows messages.
*
*----------------------------------------------------------------------
*/