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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
|
/*
* tkMenuDraw.c --
*
* This module implements the platform-independent drawing and geometry
* calculations of menu widgets.
*
* Copyright © 1996-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
#include "tkMenu.h"
/*
* Forward declarations for functions defined later in this file:
*/
static void AdjustMenuCoords(TkMenu *menuPtr, TkMenuEntry *mePtr,
int *xPtr, int *yPtr);
static void ComputeMenuGeometry(ClientData clientData);
static void DisplayMenu(ClientData clientData);
/*
*----------------------------------------------------------------------
*
* TkMenuInitializeDrawingFields --
*
* Fills in drawing fields of a new menu. Called when new menu is created
* by MenuCmd.
*
* Results:
* None.
*
* Side effects:
* menuPtr fields are initialized.
*
*----------------------------------------------------------------------
*/
void
TkMenuInitializeDrawingFields(
TkMenu *menuPtr) /* The menu we are initializing. */
{
menuPtr->textGC = NULL;
menuPtr->gray = None;
menuPtr->disabledGC = NULL;
menuPtr->activeGC = NULL;
menuPtr->indicatorGC = NULL;
menuPtr->disabledImageGC = NULL;
menuPtr->totalWidth = menuPtr->totalHeight = 0;
}
/*
*----------------------------------------------------------------------
*
* TkMenuInitializeEntryDrawingFields --
*
* Fills in drawing fields of a new menu entry. Called when an entry is
* created.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
TkMenuInitializeEntryDrawingFields(
TkMenuEntry *mePtr) /* The menu we are initializing. */
{
mePtr->width = 0;
mePtr->height = 0;
mePtr->x = 0;
mePtr->y = 0;
mePtr->indicatorSpace = 0;
mePtr->labelWidth = 0;
mePtr->textGC = NULL;
mePtr->activeGC = NULL;
mePtr->disabledGC = NULL;
mePtr->indicatorGC = NULL;
}
/*
*----------------------------------------------------------------------
*
* TkMenuFreeDrawOptions --
*
* Frees up any structures allocated for the drawing of a menu. Called
* when menu is deleted.
*
* Results:
* None.
*
* Side effects:
* Storage is released.
*
*----------------------------------------------------------------------
*/
void
TkMenuFreeDrawOptions(
TkMenu *menuPtr)
{
if (menuPtr->textGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->textGC);
}
if (menuPtr->disabledImageGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->disabledImageGC);
}
if (menuPtr->gray != None) {
Tk_FreeBitmap(menuPtr->display, menuPtr->gray);
}
if (menuPtr->disabledGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->disabledGC);
}
if (menuPtr->activeGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->activeGC);
}
if (menuPtr->indicatorGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->indicatorGC);
}
}
/*
*----------------------------------------------------------------------
*
* TkMenuEntryFreeDrawOptions --
*
* Frees up drawing structures for a menu entry. Called when menu entry
* is freed.
*
* RESULTS:
* None.
*
* Side effects:
* Storage is freed.
*
*----------------------------------------------------------------------
*/
void
TkMenuEntryFreeDrawOptions(
TkMenuEntry *mePtr)
{
if (mePtr->textGC != NULL) {
Tk_FreeGC(mePtr->menuPtr->display, mePtr->textGC);
}
if (mePtr->disabledGC != NULL) {
Tk_FreeGC(mePtr->menuPtr->display, mePtr->disabledGC);
}
if (mePtr->activeGC != NULL) {
Tk_FreeGC(mePtr->menuPtr->display, mePtr->activeGC);
}
if (mePtr->indicatorGC != NULL) {
Tk_FreeGC(mePtr->menuPtr->display, mePtr->indicatorGC);
}
}
/*
*----------------------------------------------------------------------
*
* TkMenuConfigureDrawOptions --
*
* Sets the menu's drawing attributes in preparation for drawing the
* menu.
*
* RESULTS:
* None.
*
* Side effects:
* Storage is allocated.
*
*----------------------------------------------------------------------
*/
void
TkMenuConfigureDrawOptions(
TkMenu *menuPtr) /* The menu we are configuring. */
{
XGCValues gcValues;
GC newGC;
unsigned long mask;
Tk_3DBorder border, activeBorder;
Tk_Font tkfont;
XColor *fg, *activeFg, *indicatorFg;
/*
* A few options need special processing, such as setting the background
* from a 3-D border, or filling in complicated defaults that couldn't be
* specified to Tk_ConfigureWidget.
*/
border = Tk_Get3DBorderFromObj(menuPtr->tkwin, menuPtr->borderPtr);
Tk_SetBackgroundFromBorder(menuPtr->tkwin, border);
tkfont = Tk_GetFontFromObj(menuPtr->tkwin, menuPtr->fontPtr);
gcValues.font = Tk_FontId(tkfont);
fg = Tk_GetColorFromObj(menuPtr->tkwin, menuPtr->fgPtr);
gcValues.foreground = fg->pixel;
gcValues.background = Tk_3DBorderColor(border)->pixel;
newGC = Tk_GetGC(menuPtr->tkwin, GCForeground|GCBackground|GCFont,
&gcValues);
if (menuPtr->textGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->textGC);
}
menuPtr->textGC = newGC;
gcValues.font = Tk_FontId(tkfont);
gcValues.background = Tk_3DBorderColor(border)->pixel;
if (menuPtr->disabledFgPtr != NULL) {
XColor *disabledFg;
disabledFg = Tk_GetColorFromObj(menuPtr->tkwin,
menuPtr->disabledFgPtr);
gcValues.foreground = disabledFg->pixel;
mask = GCForeground|GCBackground|GCFont;
} else {
gcValues.foreground = gcValues.background;
mask = GCForeground;
if (menuPtr->gray == None) {
menuPtr->gray = Tk_GetBitmap(menuPtr->interp, menuPtr->tkwin,
"gray50");
}
if (menuPtr->gray != None) {
gcValues.fill_style = FillStippled;
gcValues.stipple = menuPtr->gray;
mask = GCForeground|GCFillStyle|GCStipple;
}
}
newGC = Tk_GetGC(menuPtr->tkwin, mask, &gcValues);
if (menuPtr->disabledGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->disabledGC);
}
menuPtr->disabledGC = newGC;
gcValues.foreground = Tk_3DBorderColor(border)->pixel;
if (menuPtr->gray == None) {
menuPtr->gray = Tk_GetBitmap(menuPtr->interp, menuPtr->tkwin,
"gray50");
}
if (menuPtr->gray != None) {
gcValues.fill_style = FillStippled;
gcValues.stipple = menuPtr->gray;
newGC = Tk_GetGC(menuPtr->tkwin,
GCForeground|GCFillStyle|GCStipple, &gcValues);
}
if (menuPtr->disabledImageGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->disabledImageGC);
}
menuPtr->disabledImageGC = newGC;
gcValues.font = Tk_FontId(tkfont);
activeFg = Tk_GetColorFromObj(menuPtr->tkwin, menuPtr->activeFgPtr);
gcValues.foreground = activeFg->pixel;
activeBorder = Tk_Get3DBorderFromObj(menuPtr->tkwin,
menuPtr->activeBorderPtr);
gcValues.background = Tk_3DBorderColor(activeBorder)->pixel;
newGC = Tk_GetGC(menuPtr->tkwin, GCForeground|GCBackground|GCFont,
&gcValues);
if (menuPtr->activeGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->activeGC);
}
menuPtr->activeGC = newGC;
indicatorFg = Tk_GetColorFromObj(menuPtr->tkwin,
menuPtr->indicatorFgPtr);
gcValues.foreground = indicatorFg->pixel;
gcValues.background = Tk_3DBorderColor(border)->pixel;
newGC = Tk_GetGC(menuPtr->tkwin, GCForeground|GCBackground|GCFont,
&gcValues);
if (menuPtr->indicatorGC != NULL) {
Tk_FreeGC(menuPtr->display, menuPtr->indicatorGC);
}
menuPtr->indicatorGC = newGC;
}
/*
*----------------------------------------------------------------------
*
* TkMenuConfigureEntryDrawOptions --
*
* Calculates any entry-specific draw options for the given menu entry.
*
* Results:
* Returns a standard Tcl error.
*
* Side effects:
* Storage may be allocated.
*
*----------------------------------------------------------------------
*/
int
TkMenuConfigureEntryDrawOptions(
TkMenuEntry *mePtr,
TkSizeT index)
{
XGCValues gcValues;
GC newGC, newActiveGC, newDisabledGC, newIndicatorGC;
unsigned long mask;
Tk_Font tkfont;
TkMenu *menuPtr = mePtr->menuPtr;
tkfont = Tk_GetFontFromObj(menuPtr->tkwin,
(mePtr->fontPtr != NULL) ? mePtr->fontPtr : menuPtr->fontPtr);
if (mePtr->state == ENTRY_ACTIVE) {
if (index != menuPtr->active) {
TkActivateMenuEntry(menuPtr, index);
}
} else {
if (index == menuPtr->active) {
TkActivateMenuEntry(menuPtr, -1);
}
}
if ((mePtr->fontPtr != NULL)
|| (mePtr->borderPtr != NULL)
|| (mePtr->fgPtr != NULL)
|| (mePtr->activeBorderPtr != NULL)
|| (mePtr->activeFgPtr != NULL)
|| (mePtr->indicatorFgPtr != NULL)) {
XColor *fg, *indicatorFg, *activeFg;
Tk_3DBorder border, activeBorder;
fg = Tk_GetColorFromObj(menuPtr->tkwin, (mePtr->fgPtr != NULL)
? mePtr->fgPtr : menuPtr->fgPtr);
gcValues.foreground = fg->pixel;
border = Tk_Get3DBorderFromObj(menuPtr->tkwin,
(mePtr->borderPtr != NULL) ? mePtr->borderPtr
: menuPtr->borderPtr);
gcValues.background = Tk_3DBorderColor(border)->pixel;
gcValues.font = Tk_FontId(tkfont);
/*
* Note: disable GraphicsExpose events; we know there won't be
* obscured areas when copying from an off-screen pixmap to the screen
* and this gets rid of unnecessary events.
*/
gcValues.graphics_exposures = False;
newGC = Tk_GetGC(menuPtr->tkwin,
GCForeground|GCBackground|GCFont|GCGraphicsExposures,
&gcValues);
indicatorFg = Tk_GetColorFromObj(menuPtr->tkwin,
(mePtr->indicatorFgPtr != NULL) ? mePtr->indicatorFgPtr
: menuPtr->indicatorFgPtr);
gcValues.foreground = indicatorFg->pixel;
newIndicatorGC = Tk_GetGC(menuPtr->tkwin,
GCForeground|GCBackground|GCGraphicsExposures,
&gcValues);
if ((menuPtr->disabledFgPtr != NULL) || (mePtr->image != NULL)) {
XColor *disabledFg;
disabledFg = Tk_GetColorFromObj(menuPtr->tkwin,
menuPtr->disabledFgPtr);
gcValues.foreground = disabledFg->pixel;
mask = GCForeground|GCBackground|GCFont|GCGraphicsExposures;
} else {
gcValues.foreground = gcValues.background;
gcValues.fill_style = FillStippled;
gcValues.stipple = menuPtr->gray;
mask = GCForeground|GCFillStyle|GCStipple;
}
newDisabledGC = Tk_GetGC(menuPtr->tkwin, mask, &gcValues);
activeFg = Tk_GetColorFromObj(menuPtr->tkwin,
(mePtr->activeFgPtr != NULL) ? mePtr->activeFgPtr
: menuPtr->activeFgPtr);
activeBorder = Tk_Get3DBorderFromObj(menuPtr->tkwin,
(mePtr->activeBorderPtr != NULL) ? mePtr->activeBorderPtr
: menuPtr->activeBorderPtr);
gcValues.foreground = activeFg->pixel;
gcValues.background = Tk_3DBorderColor(activeBorder)->pixel;
newActiveGC = Tk_GetGC(menuPtr->tkwin,
GCForeground|GCBackground|GCFont|GCGraphicsExposures,
&gcValues);
} else {
newGC = NULL;
newActiveGC = NULL;
newDisabledGC = NULL;
newIndicatorGC = NULL;
}
if (mePtr->textGC != NULL) {
Tk_FreeGC(menuPtr->display, mePtr->textGC);
}
mePtr->textGC = newGC;
if (mePtr->activeGC != NULL) {
Tk_FreeGC(menuPtr->display, mePtr->activeGC);
}
mePtr->activeGC = newActiveGC;
if (mePtr->disabledGC != NULL) {
Tk_FreeGC(menuPtr->display, mePtr->disabledGC);
}
mePtr->disabledGC = newDisabledGC;
if (mePtr->indicatorGC != NULL) {
Tk_FreeGC(menuPtr->display, mePtr->indicatorGC);
}
mePtr->indicatorGC = newIndicatorGC;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkEventuallyRecomputeMenu --
*
* Tells Tcl to redo the geometry because this menu has changed.
*
* Results:
* None.
*
* Side effects:
* Menu geometry is recomputed at idle time, and the menu will be
* redisplayed.
*
*----------------------------------------------------------------------
*/
void
TkEventuallyRecomputeMenu(
TkMenu *menuPtr)
{
if (!(menuPtr->menuFlags & RESIZE_PENDING)) {
menuPtr->menuFlags |= RESIZE_PENDING;
Tcl_DoWhenIdle(ComputeMenuGeometry, menuPtr);
}
}
/*
*----------------------------------------------------------------------
*
* TkRecomputeMenu --
*
* Tells Tcl to redo the geometry because this menu has changed. Does it
* now; removes any ComputeMenuGeometries from the idler.
*
* Results:
* None.
*
* Side effects:
* Menu geometry is immediately reconfigured.
*
*----------------------------------------------------------------------
*/
void
TkRecomputeMenu(
TkMenu *menuPtr)
{
if (menuPtr->menuFlags & RESIZE_PENDING) {
Tcl_CancelIdleCall(ComputeMenuGeometry, menuPtr);
ComputeMenuGeometry(menuPtr);
}
}
/*
*----------------------------------------------------------------------
*
* TkEventuallyRedrawMenu --
*
* Arrange for an entry of a menu, or the whole menu, to be redisplayed
* at some point in the future.
*
* Results:
* None.
*
* Side effects:
* A when-idle handler is scheduled to do the redisplay, if there isn't
* one already scheduled.
*
*----------------------------------------------------------------------
*/
void
TkEventuallyRedrawMenu(
TkMenu *menuPtr, /* Information about menu to redraw. */
TkMenuEntry *mePtr)/* Entry to redraw. NULL means redraw all the
* entries in the menu. */
{
TkSizeT i;
if (menuPtr->tkwin == NULL) {
return;
}
if (mePtr != NULL) {
mePtr->entryFlags |= ENTRY_NEEDS_REDISPLAY;
} else {
for (i = 0; i < menuPtr->numEntries; i++) {
menuPtr->entries[i]->entryFlags |= ENTRY_NEEDS_REDISPLAY;
}
}
if (!Tk_IsMapped(menuPtr->tkwin)
|| (menuPtr->menuFlags & REDRAW_PENDING)) {
return;
}
Tcl_DoWhenIdle(DisplayMenu, menuPtr);
menuPtr->menuFlags |= REDRAW_PENDING;
}
/*
*--------------------------------------------------------------
*
* ComputeMenuGeometry --
*
* This function is invoked to recompute the size and layout of a menu.
* It is called as a when-idle handler so that it only gets done once,
* even if a group of changes is made to the menu.
*
* Results:
* None.
*
* Side effects:
* Fields of menu entries are changed to reflect their current positions,
* and the size of the menu window itself may be changed.
*
*--------------------------------------------------------------
*/
static void
ComputeMenuGeometry(
ClientData clientData) /* Structure describing menu. */
{
TkMenu *menuPtr = (TkMenu *)clientData;
if (menuPtr->tkwin == NULL) {
return;
}
if (menuPtr->menuType == MENUBAR) {
TkpComputeMenubarGeometry(menuPtr);
} else {
TkpComputeStandardMenuGeometry(menuPtr);
}
if ((menuPtr->totalWidth != Tk_ReqWidth(menuPtr->tkwin)) ||
(menuPtr->totalHeight != Tk_ReqHeight(menuPtr->tkwin))) {
Tk_GeometryRequest(menuPtr->tkwin, menuPtr->totalWidth,
menuPtr->totalHeight);
}
/*
* Must always force a redisplay here if the window is mapped (even if the
* size didn't change, something else might have changed in the menu, such
* as a label or accelerator). The resize will force a redisplay above.
*/
TkEventuallyRedrawMenu(menuPtr, NULL);
menuPtr->menuFlags &= ~RESIZE_PENDING;
}
/*
*----------------------------------------------------------------------
*
* TkMenuSelectImageProc --
*
* This function is invoked by the image code whenever the manager for an
* image does something that affects the size of contents of an image
* displayed in a menu entry when it is selected.
*
* Results:
* None.
*
* Side effects:
* Arranges for the menu to get redisplayed.
*
*----------------------------------------------------------------------
*/
void
TkMenuSelectImageProc(
ClientData clientData, /* Pointer to widget record. */
int x, int y, /* Upper left pixel (within image) that must
* be redisplayed. */
int width, int height, /* Dimensions of area to redisplay (may be
* <=0). */
int imgWidth, int imgHeight)/* New dimensions of image. */
{
TkMenuEntry *mePtr = (TkMenuEntry *)clientData;
(void)x;
(void)y;
(void)width;
(void)height;
(void)imgWidth;
(void)imgHeight;
if ((mePtr->entryFlags & ENTRY_SELECTED)
&& !(mePtr->menuPtr->menuFlags & REDRAW_PENDING)) {
mePtr->menuPtr->menuFlags |= REDRAW_PENDING;
Tcl_DoWhenIdle(DisplayMenu, mePtr->menuPtr);
}
}
/*
*----------------------------------------------------------------------
*
* DisplayMenu --
*
* This function is invoked to display a menu widget.
*
* Results:
* None.
*
* Side effects:
* Commands are output to X to display the menu in its current mode.
*
*----------------------------------------------------------------------
*/
static void
DisplayMenu(
ClientData clientData) /* Information about widget. */
{
TkMenu *menuPtr = (TkMenu *)clientData;
TkMenuEntry *mePtr;
Tk_Window tkwin = menuPtr->tkwin;
TkSizeT index;
int strictMotif;
Tk_Font tkfont;
Tk_FontMetrics menuMetrics;
int width;
int borderWidth;
Tk_3DBorder border;
int relief;
menuPtr->menuFlags &= ~REDRAW_PENDING;
if ((menuPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
return;
}
Tk_GetPixelsFromObj(NULL, menuPtr->tkwin, menuPtr->borderWidthPtr,
&borderWidth);
border = Tk_Get3DBorderFromObj(menuPtr->tkwin, menuPtr->borderPtr);
if (menuPtr->menuType == MENUBAR) {
Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin), border, borderWidth,
borderWidth, Tk_Width(tkwin) - 2 * borderWidth,
Tk_Height(tkwin) - 2 * borderWidth, 0, TK_RELIEF_FLAT);
}
strictMotif = Tk_StrictMotif(menuPtr->tkwin);
/*
* See note in ComputeMenuGeometry. We don't want to be doing font metrics
* all of the time.
*/
tkfont = Tk_GetFontFromObj(menuPtr->tkwin, menuPtr->fontPtr);
Tk_GetFontMetrics(tkfont, &menuMetrics);
/*
* Loop through all of the entries, drawing them one at a time.
*/
for (index = 0; index < menuPtr->numEntries; index++) {
mePtr = menuPtr->entries[index];
if (menuPtr->menuType != MENUBAR) {
if (!(mePtr->entryFlags & ENTRY_NEEDS_REDISPLAY)) {
continue;
}
}
mePtr->entryFlags &= ~ENTRY_NEEDS_REDISPLAY;
TkpDrawMenuEntry(mePtr, Tk_WindowId(menuPtr->tkwin), tkfont,
&menuMetrics, mePtr->x, mePtr->y, mePtr->width,
mePtr->height, strictMotif, 1);
if (mePtr->entryFlags & ENTRY_LAST_COLUMN) {
/*
* Paint the area at the right of an entry in the last column.
* This has zero width except after menu resizing.
*/
Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin), border,
mePtr->x + mePtr->width, mePtr->y,
Tk_Width(tkwin) - mePtr->x - mePtr->width - borderWidth,
mePtr->height, 0, TK_RELIEF_FLAT);
}
if ((index > 0) && (menuPtr->menuType != MENUBAR)
&& mePtr->columnBreak) {
/*
* Paint the area under the last entry in a column.
*/
mePtr = menuPtr->entries[index - 1];
Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin), border,
mePtr->x, mePtr->y + mePtr->height, mePtr->width,
Tk_Height(tkwin) - mePtr->y - mePtr->height - borderWidth,
0, TK_RELIEF_FLAT);
}
}
if (menuPtr->menuType != MENUBAR) {
int x, y, height;
if (menuPtr->numEntries == 0) {
x = y = borderWidth;
width = Tk_Width(tkwin) - 2 * borderWidth;
height = Tk_Height(tkwin) - 2 * borderWidth;
} else {
mePtr = menuPtr->entries[menuPtr->numEntries - 1];
/*
* Paint the area under the last entry of the menu.
*/
Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin),
border, mePtr->x, mePtr->y + mePtr->height, mePtr->width,
Tk_Height(tkwin) - mePtr->y - mePtr->height - borderWidth,
0, TK_RELIEF_FLAT);
x = mePtr->x + mePtr->width;
y = mePtr->y + mePtr->height;
width = Tk_Width(tkwin) - x - borderWidth;
height = Tk_Height(tkwin) - y - borderWidth;
}
/*
* Paint the area at the bottom right of the last entry.
* This has zero width except after menu resizing.
*/
Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin), border, x, y,
width, height, 0, TK_RELIEF_FLAT);
}
Tk_GetReliefFromObj(NULL, menuPtr->reliefPtr, &relief);
Tk_Draw3DRectangle(menuPtr->tkwin, Tk_WindowId(tkwin),
border, 0, 0, Tk_Width(tkwin), Tk_Height(tkwin), borderWidth,
relief);
}
/*
*--------------------------------------------------------------
*
* TkMenuEventProc --
*
* This function is invoked by the Tk dispatcher for various events on
* menus.
*
* Results:
* None.
*
* Side effects:
* When the window gets deleted, internal structures get cleaned up. When
* it gets exposed, it is redisplayed.
*
*--------------------------------------------------------------
*/
void
TkMenuEventProc(
ClientData clientData, /* Information about window. */
XEvent *eventPtr) /* Information about event. */
{
TkMenu *menuPtr = (TkMenu *)clientData;
if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
TkEventuallyRedrawMenu(menuPtr, NULL);
} else if (eventPtr->type == ConfigureNotify) {
TkEventuallyRecomputeMenu(menuPtr);
TkEventuallyRedrawMenu(menuPtr, NULL);
} else if (eventPtr->type == ActivateNotify) {
if (menuPtr->menuType == TEAROFF_MENU) {
Tk_SetMainMenubar(menuPtr->interp, menuPtr->tkwin, NULL);
}
} else if (eventPtr->type == DestroyNotify) {
if (menuPtr->tkwin != NULL) {
if (!(menuPtr->menuFlags & MENU_DELETION_PENDING)) {
TkDestroyMenu(menuPtr);
}
menuPtr->tkwin = NULL;
}
if (menuPtr->menuFlags & MENU_WIN_DESTRUCTION_PENDING) {
return;
}
menuPtr->menuFlags |= MENU_WIN_DESTRUCTION_PENDING;
if (menuPtr->widgetCmd != NULL) {
Tcl_DeleteCommandFromToken(menuPtr->interp, menuPtr->widgetCmd);
menuPtr->widgetCmd = NULL;
}
if (menuPtr->menuFlags & REDRAW_PENDING) {
Tcl_CancelIdleCall(DisplayMenu, menuPtr);
menuPtr->menuFlags &= ~REDRAW_PENDING;
}
if (menuPtr->menuFlags & RESIZE_PENDING) {
Tcl_CancelIdleCall(ComputeMenuGeometry, menuPtr);
menuPtr->menuFlags &= ~RESIZE_PENDING;
}
Tcl_EventuallyFree(menuPtr, TCL_DYNAMIC);
}
}
/*
*----------------------------------------------------------------------
*
* TkMenuImageProc --
*
* This function is invoked by the image code whenever the manager for an
* image does something that affects the size of contents of an image
* displayed in a menu entry.
*
* Results:
* None.
*
* Side effects:
* Arranges for the menu to get redisplayed.
*
*----------------------------------------------------------------------
*/
void
TkMenuImageProc(
ClientData clientData, /* Pointer to widget record. */
int x, int y, /* Upper left pixel (within image) that must
* be redisplayed. */
int width, int height, /* Dimensions of area to redisplay (may be
* <=0). */
int imgWidth, int imgHeight)/* New dimensions of image. */
{
TkMenu *menuPtr = (TkMenu *)((TkMenuEntry *) clientData)->menuPtr;
(void)x;
(void)y;
(void)width;
(void)height;
(void)imgWidth;
(void)imgHeight;
if ((menuPtr->tkwin != NULL) && !(menuPtr->menuFlags & RESIZE_PENDING)) {
menuPtr->menuFlags |= RESIZE_PENDING;
Tcl_DoWhenIdle(ComputeMenuGeometry, menuPtr);
}
}
/*
*----------------------------------------------------------------------
*
* TkPostTearoffMenu --
*
* Posts a tearoff menu on the screen. 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
TkPostTearoffMenu(
Tcl_Interp *interp, /* The interpreter of the menu */
TkMenu *menuPtr, /* The menu we are posting */
int x, int y) /* The root X,Y coordinates where we are
* posting */
{
return TkpPostTearoffMenu(interp, menuPtr, x, y, -1);
}
/*
*--------------------------------------------------------------
*
* TkPostSubmenu --
*
* This function arranges for a particular submenu (i.e. the menu
* corresponding to a given cascade entry) to be posted.
*
* Results:
* A standard Tcl return result. Errors may occur in the Tcl commands
* generated to post and unpost submenus.
*
* Side effects:
* If there is already a submenu posted, it is unposted. The new submenu
* is then posted.
*
*--------------------------------------------------------------
*/
int
TkPostSubmenu(
Tcl_Interp *interp, /* Used for invoking sub-commands and
* reporting errors. */
TkMenu *menuPtr, /* Information about menu as a whole. */
TkMenuEntry *mePtr)/* Info about submenu that is to be posted.
* NULL means make sure that no submenu is
* posted. */
{
int result, x, y;
Tcl_Obj *subary[4];
if (mePtr == menuPtr->postedCascade) {
return TCL_OK;
}
if (menuPtr->postedCascade != NULL) {
/*
* Note: when unposting a submenu, we have to redraw the entire parent
* menu. This is because of a combination of the following things:
* (a) the submenu partially overlaps the parent.
* (b) the submenu specifies "save under", which causes the X server
* to make a copy of the information under it when it is posted.
* When the submenu is unposted, the X server copies this data
* back and doesn't generate any Expose events for the parent.
* (c) the parent may have redisplayed itself after the submenu was
* posted, in which case the saved information is no longer
* correct.
* The simplest solution is just force a complete redisplay of the
* parent.
*/
subary[0] = menuPtr->postedCascade->namePtr;
subary[1] = Tcl_NewStringObj("unpost", -1);
Tcl_IncrRefCount(subary[1]);
TkEventuallyRedrawMenu(menuPtr, NULL);
result = Tcl_EvalObjv(interp, 2, subary, 0);
Tcl_DecrRefCount(subary[1]);
menuPtr->postedCascade = NULL;
if (result != TCL_OK) {
return result;
}
}
if ((mePtr != NULL) && (mePtr->namePtr != NULL)
&& Tk_IsMapped(menuPtr->tkwin)) {
/*
* Position the cascade with its upper left corner slightly below and
* to the left of the upper right corner of the menu entry (this is an
* attempt to match Motif behavior).
*
* The menu has to redrawn so that the entry can change relief.
*
* Set postedCascade early to ensure tear-off submenus work on
* Windows. [Bug 873613]
*/
Tk_GetRootCoords(menuPtr->tkwin, &x, &y);
AdjustMenuCoords(menuPtr, mePtr, &x, &y);
menuPtr->postedCascade = mePtr;
subary[0] = mePtr->namePtr;
subary[1] = Tcl_NewStringObj("post", -1);
subary[2] = Tcl_NewWideIntObj(x);
subary[3] = Tcl_NewWideIntObj(y);
Tcl_IncrRefCount(subary[1]);
Tcl_IncrRefCount(subary[2]);
Tcl_IncrRefCount(subary[3]);
result = Tcl_EvalObjv(interp, 4, subary, 0);
Tcl_DecrRefCount(subary[1]);
Tcl_DecrRefCount(subary[2]);
Tcl_DecrRefCount(subary[3]);
if (result != TCL_OK) {
menuPtr->postedCascade = NULL;
return result;
}
TkEventuallyRedrawMenu(menuPtr, mePtr);
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* AdjustMenuCoords --
*
* Adjusts the given coordinates down and the left to give a Motif look.
*
* Results:
* None.
*
* Side effects:
* The menu is eventually redrawn if necessary.
*
*----------------------------------------------------------------------
*/
static void
AdjustMenuCoords(
TkMenu *menuPtr,
TkMenuEntry *mePtr,
int *xPtr,
int *yPtr)
{
if (menuPtr->menuType == MENUBAR) {
*xPtr += mePtr->x;
*yPtr += mePtr->y + mePtr->height;
} else {
int borderWidth, activeBorderWidth;
Tk_GetPixelsFromObj(NULL, menuPtr->tkwin, menuPtr->borderWidthPtr,
&borderWidth);
Tk_GetPixelsFromObj(NULL, menuPtr->tkwin,
menuPtr->activeBorderWidthPtr, &activeBorderWidth);
*xPtr += Tk_Width(menuPtr->tkwin) - borderWidth - activeBorderWidth
- 2;
*yPtr += mePtr->y + activeBorderWidth + 2;
}
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|