/
tkMacOSXMenu.c
1763 lines (1580 loc) · 45.8 KB
/
tkMacOSXMenu.c
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
/*
* tkMacOSXMenu.c --
*
* This module implements the Mac-platform specific features of menus.
*
* Copyright (c) 1996-1997 by Sun Microsystems, Inc.
* Copyright 2001-2009, Apple Inc.
* Copyright (c) 2005-2009 Daniel A. Steffen <das@users.sourceforge.net>
* Copyright (c) 2012 Adrian Robert.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkMacOSXPrivate.h"
#include "tkMenubutton.h"
#include "tkMenu.h"
#include "tkColor.h"
#include "tkFont.h"
#include "tkMacOSXWm.h"
#include "tkMacOSXDebug.h"
/*
#ifdef TK_MAC_DEBUG
#define TK_MAC_DEBUG_MENUS
#endif
*/
#define ENTRY_HELP_MENU ENTRY_PLATFORM_FLAG1
#define ENTRY_APPLE_MENU ENTRY_PLATFORM_FLAG2
#define ENTRY_WINDOWS_MENU ENTRY_PLATFORM_FLAG3
#define sl(s) ((int) (sizeof(s "") - 1))
#define SPECIALMENU(n, f) {.name = "." #n, .len = sl(#n) + 1, \
.flag = ENTRY_##f##_MENU }
static const struct {
const char *name; const size_t len; const int flag;
} specialMenus[] = {
SPECIALMENU(help, HELP),
SPECIALMENU(apple, APPLE),
SPECIALMENU(window, WINDOWS),
{NULL}
};
#undef SPECIALMENU
#define MODIFIER(n, f) {.name = #n, .len = sl(#n), .mask = f }
static const struct {
const char *name; const size_t len; const NSUInteger mask;
} modifiers[] = {
MODIFIER(Control, NSControlKeyMask),
MODIFIER(Ctrl, NSControlKeyMask),
MODIFIER(Option, NSAlternateKeyMask),
MODIFIER(Opt, NSAlternateKeyMask),
MODIFIER(Alt, NSAlternateKeyMask),
MODIFIER(Shift, NSShiftKeyMask),
MODIFIER(Command, NSCommandKeyMask),
MODIFIER(Cmd, NSCommandKeyMask),
MODIFIER(Meta, NSCommandKeyMask),
{NULL}
};
#undef MODIFIER
#define ACCEL(n, c) {.name = #n, .len = sl(#n), .ch = c }
static const struct {
const char *name; const size_t len; const UniChar ch;
} specialAccelerators[] = {
ACCEL(PageUp, NSPageUpFunctionKey),
ACCEL(PageDown, NSPageDownFunctionKey),
ACCEL(Left, NSLeftArrowFunctionKey),
ACCEL(Right, NSRightArrowFunctionKey),
ACCEL(Up, NSUpArrowFunctionKey),
ACCEL(Down, NSDownArrowFunctionKey),
ACCEL(Escape, 0x001b),
ACCEL(Clear, NSClearDisplayFunctionKey),
ACCEL(Enter, NSEnterCharacter),
ACCEL(Backspace, NSBackspaceCharacter),
ACCEL(Space, ' '),
ACCEL(Tab, NSTabCharacter),
ACCEL(BackTab, NSBackTabCharacter),
ACCEL(Delete, NSDeleteCharacter),
ACCEL(Home, NSHomeFunctionKey),
ACCEL(End, NSEndFunctionKey),
ACCEL(Return, NSCarriageReturnCharacter),
ACCEL(Help, NSHelpFunctionKey),
ACCEL(Power, 0x233d),
ACCEL(Eject, 0xf804),
{NULL}
};
#undef ACCEL
#undef sl
static int gNoTkMenus = 0; /* This is used by Tk_MacOSXTurnOffMenus as
* the flag that Tk is not to draw any
* menus. */
static int inPostMenu = 0;
static unsigned long defaultBg = 0, defaultFg = 0;
static SInt32 menuMarkColumnWidth = 0, menuIconTrailingEdgeMargin = 0;
static SInt32 menuTextLeadingEdgeMargin = 0, menuTextTrailingEdgeMargin = 0;
static SInt16 menuItemExtraHeight = 0, menuItemExtraWidth = 0;
static SInt16 menuSeparatorHeight = 0;
static void CheckForSpecialMenu(TkMenu *menuPtr);
static NSString *ParseAccelerator(const char *accel, NSUInteger *maskPtr);
static int GenerateMenuSelectEvent(TKMenu *menu, NSMenuItem *menuItem);
static void MenuSelectEvent(TkMenu *menuPtr);
static void RecursivelyClearActiveMenu(TkMenu *menuPtr);
static int ModifierCharWidth(Tk_Font tkfont);
#pragma mark TKMenu
@interface TKMenu(TKMenuPrivate)
- (id) initWithTkMenu: (TkMenu *) tkMenu;
- (TkMenu *) tkMenu;
- (int) tkIndexOfItem: (NSMenuItem *) menuItem;
- (void) insertItem: (NSMenuItem *) newItem atTkIndex: (NSInteger) index;
@end
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
#define TKMenu_NSMenuDelegate <NSMenuDelegate>
#else
#define TKMenu_NSMenuDelegate
#endif
@interface TKMenu(TKMenuDelegate) TKMenu_NSMenuDelegate
@end
@implementation TKMenu
- (void) setSpecial: (NSUInteger) special
{
NSAssert(!_tkSpecial, @"Cannot change specialness of a special menu");
_tkSpecial = special;
}
- (BOOL) isSpecial: (NSUInteger) special
{
return (_tkSpecial == special);
}
@end
@implementation TKMenu(TKMenuPrivate)
- (id) initWithTitle: (NSString *) aTitle
{
self = [super initWithTitle:aTitle];
if (self) {
_tkMenu = NULL;
_tkOffset = 0;
_tkItemCount = 0;
_tkSpecial = 0;
[self setDelegate:self];
}
return self;
}
- (id) initWithTkMenu: (TkMenu *) tkMenu
{
NSString *title = [[NSString alloc] initWithUTF8String:
Tk_PathName(tkMenu->tkwin)];
self = [self initWithTitle:title];
[title release];
if (self) {
_tkMenu = tkMenu;
}
return self;
}
- (id) copyWithZone: (NSZone *) zone
{
TKMenu *copy = [super copyWithZone:zone];
NSAssert(_tkMenu == nil, @"Cannot copy tkMenu");
copy->_tkMenu = _tkMenu;
copy->_tkOffset = _tkOffset;
copy->_tkItemCount = _tkItemCount;
copy->_tkSpecial = _tkSpecial;
return copy;
}
- (TkMenu *) tkMenu
{
return _tkMenu;
}
- (int) tkIndexOfItem: (NSMenuItem *) menuItem
{
return [self indexOfItem:menuItem] - _tkOffset;
}
- (void) insertItem: (NSMenuItem *) newItem atTkIndex: (NSInteger) index
{
[super insertItem:newItem atIndex:index + _tkOffset];
_tkItemCount++;
}
- (void) insertItem: (NSMenuItem *) newItem atIndex: (NSInteger) index
{
if (_tkMenu && index >= 0) {
if ((NSUInteger)index <= _tkOffset) {
_tkOffset++;
} else {
NSAssert((NSUInteger)index >= _tkItemCount + _tkOffset,
@"Cannot insert in the middle of Tk menu");
}
}
[super insertItem:newItem atIndex:index];
}
- (void) removeItemAtIndex: (NSInteger) index
{
if (_tkMenu && index >= 0) {
if ((NSUInteger)index < _tkOffset) {
_tkOffset--;
} else if ((NSUInteger)index < _tkItemCount + _tkOffset) {
_tkItemCount--;
}
}
[super removeItemAtIndex:index];
}
- (NSMenuItem *) newTkMenuItem: (TkMenuEntry *) mePtr
{
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@""
action:@selector(tkMenuItemInvoke:) keyEquivalent:@""];
[menuItem setTarget:self];
[menuItem setTag:(NSInteger)mePtr];
return menuItem;
}
@end
@implementation TKMenu(TKMenuActions)
// target methods
- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
{
return [menuItem isEnabled];
}
// Workaround for bug 3572016; leaves menu items enabled during modal dialog.
- (BOOL)worksWhenModal
{
return YES;
}
- (void) tkMenuItemInvoke: (id) sender
{
/*
* With the delegate matching key equivalents, when a menu action is sent
* in response to a key equivalent, sender is the whole menu and not the
* the specific menu item, use this to ignore key equivalents for our
* menus (as Tk handles them directly via bindings).
*/
if ([sender isKindOfClass:[NSMenuItem class]]) {
NSMenuItem *menuItem = (NSMenuItem *)sender;
TkMenu *menuPtr = (TkMenu *)_tkMenu;
TkMenuEntry *mePtr = (TkMenuEntry *)[menuItem tag];
if (menuPtr && mePtr) {
Tcl_Interp *interp = menuPtr->interp;
/*Add time for errors to fire if necessary. This is sub-optimal but avoids issues with Tcl/Cocoa event loop integration.*/
Tcl_Sleep(100);
Tcl_Preserve(interp);
Tcl_Preserve(menuPtr);
int result = TkInvokeMenu(interp, menuPtr, mePtr->index);
if (result != TCL_OK && result != TCL_CONTINUE &&
result != TCL_BREAK) {
Tcl_AddErrorInfo(interp, "\n (menu invoke)");
Tcl_BackgroundException(interp, result);
}
Tcl_Release(menuPtr);
Tcl_Release(interp);
}
}
}
@end
@implementation TKMenu(TKMenuDelegate)
#define keyEquivModifiersMatch(km, m) (( \
((km) & NSCommandKeyMask) != ((m) & NSCommandKeyMask) || \
((km) & NSAlternateKeyMask) != ((m) & NSAlternateKeyMask) || \
((km) & NSControlKeyMask) != ((m) & NSControlKeyMask) || \
(((km) & NSShiftKeyMask) != ((m) & NSShiftKeyMask) && \
((m) & NSFunctionKeyMask))) ? NO : YES)
- (BOOL) menuHasKeyEquivalent: (NSMenu *) menu forEvent: (NSEvent *) event
target: (id *) target action: (SEL *) action
{
/*Use lowercaseString to keep "shift" from firing twice if bound to different procedure.*/
NSString *key = [[event charactersIgnoringModifiers] lowercaseString];
NSUInteger modifiers = [event modifierFlags] &
NSDeviceIndependentModifierFlagsMask;
if (modifiers == (NSCommandKeyMask | NSShiftKeyMask) &&
[key compare:@"?"] == NSOrderedSame) {
return NO;
}
// For command key, take input manager's word so things
// like dvorak / qwerty layout work.
if (([event modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask) {
key = [event characters];
}
NSArray *itemArray = [self itemArray];
for (NSMenuItem *item in itemArray) {
if ([item isEnabled] && [[item keyEquivalent] compare:key] ==
NSOrderedSame) {
NSUInteger keyEquivModifiers = [item keyEquivalentModifierMask];
if (keyEquivModifiersMatch(keyEquivModifiers, modifiers)) {
*target = [item target];
*action = [item action];
return YES;
}
}
}
return NO;
}
- (void) menuWillOpen: (NSMenu *) menu
{
if (_tkMenu) {
//RecursivelyClearActiveMenu(_tkMenu);
GenerateMenuSelectEvent((TKMenu *)[self supermenu],
[self itemInSupermenu]);
}
}
- (void) menuDidClose: (NSMenu *) menu
{
if (_tkMenu) {
RecursivelyClearActiveMenu(_tkMenu);
}
}
- (void) menu: (NSMenu *) menu willHighlightItem: (NSMenuItem *) item
{
if (_tkMenu) {
GenerateMenuSelectEvent(self, item);
}
}
- (void) menuNeedsUpdate: (NSMenu *) menu
{
TkMenu *menuPtr = (TkMenu *) _tkMenu;
if (menuPtr) {
Tcl_Interp *interp = menuPtr->interp;
Tcl_Preserve(interp);
Tcl_Preserve(menuPtr);
int result = TkPostCommand(_tkMenu);
if (result!=TCL_OK && result!=TCL_CONTINUE && result!=TCL_BREAK) {
Tcl_AddErrorInfo(interp, "\n (menu preprocess)");
Tcl_BackgroundException(interp, result);
}
Tcl_Release(menuPtr);
Tcl_Release(interp);
}
}
@end
#pragma mark TKApplication(TKMenu)
@interface NSApplication(TKMenu)
- (void) setAppleMenu: (NSMenu *) menu;
@end
@implementation TKApplication(TKMenu)
- (void) menuBeginTracking: (NSNotification *) notification
{
#ifdef TK_MAC_DEBUG_NOTIFICATIONS
TKLog(@"-[%@(%p) %s] %@", [self class], self, _cmd, notification);
#endif
//TkMacOSXClearMenubarActive();
//TkMacOSXPreprocessMenu();
}
- (void) menuEndTracking: (NSNotification *) notification
{
#ifdef TK_MAC_DEBUG_NOTIFICATIONS
TKLog(@"-[%@(%p) %s] %@", [self class], self, _cmd, notification);
#endif
if (!inPostMenu) {
TkMacOSXClearMenubarActive();
}
}
- (void) tkSetMainMenu: (TKMenu *) menu
{
if (gNoTkMenus) {
return;
}
TKMenu *applicationMenu = nil;
if (menu) {
NSMenuItem *applicationMenuItem = [menu numberOfItems] ?
[menu itemAtIndex:0] : nil;
if (![menu isSpecial:tkMainMenu]) {
TkMenuEntry *mePtr = (TkMenuEntry *)[applicationMenuItem tag];
if (!mePtr || !(mePtr->entryFlags & ENTRY_APPLE_MENU)) {
applicationMenuItem = [NSMenuItem itemWithSubmenu:
[[_defaultApplicationMenu copy] autorelease]];
[menu insertItem:applicationMenuItem atIndex:0];
}
[menu setSpecial:tkMainMenu];
}
applicationMenu = (TKMenu *)[applicationMenuItem submenu];
if (![applicationMenu isSpecial:tkApplicationMenu]) {
for (NSMenuItem *item in _defaultApplicationMenuItems) {
[applicationMenu addItem:[[item copy] autorelease]];
}
[applicationMenu setSpecial:tkApplicationMenu];
}
NSArray *itemArray = [menu itemArray];
for (NSMenuItem *item in itemArray) {
TkMenuEntry *mePtr = (TkMenuEntry *)[item tag];
TKMenu *submenu = (TKMenu *)[item submenu];
if (mePtr && submenu) {
if ((mePtr->entryFlags & ENTRY_WINDOWS_MENU) &&
![submenu isSpecial:tkWindowsMenu]) {
NSInteger index = 0;
for (NSMenuItem *i in _defaultWindowsMenuItems) {
[submenu insertItem:[[i copy] autorelease] atIndex:
index++];
}
[self setWindowsMenu:submenu];
[submenu setSpecial:tkWindowsMenu];
} else if ((mePtr->entryFlags & ENTRY_HELP_MENU) &&
![submenu isSpecial:tkHelpMenu]) {
NSInteger index = 0;
for (NSMenuItem *i in _defaultHelpMenuItems) {
[submenu insertItem:[[i copy] autorelease] atIndex:
index++];
}
[submenu setSpecial:tkHelpMenu];
}
}
}
} else {
menu = _defaultMainMenu;
applicationMenu = _defaultApplicationMenu;
}
NSMenuItem *servicesMenuItem =
[applicationMenu itemWithTitle:@"Services"];
if (servicesMenuItem && [servicesMenuItem submenu] != _servicesMenu) {
[[_servicesMenu itemInSupermenu] setSubmenu:nil];
[servicesMenuItem setSubmenu:_servicesMenu];
}
[self setAppleMenu:applicationMenu];
[self setMainMenu:menu];
}
@end
#pragma mark -
/*
*----------------------------------------------------------------------
*
* TkpNewMenu --
*
* Gets a new blank menu. Only the platform specific options are filled
* in.
*
* Results:
* Returns a standard Tcl error.
*
* Side effects:
* Allocates a NSMenu and puts it into the platformData field of the
* menuPtr.
*
*----------------------------------------------------------------------
*/
int
TkpNewMenu(
TkMenu *menuPtr) /* The common structure we are making the
* platform structure for. */
{
TKMenu *menu = [[TKMenu alloc] initWithTkMenu:menuPtr];
menuPtr->platformData = (TkMenuPlatformData)
TkMacOSXMakeUncollectable(menu);
CheckForSpecialMenu(menuPtr);
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 */
{
TkMacOSXMakeCollectableAndRelease(menuPtr->platformData);
}
/*
*----------------------------------------------------------------------
*
* TkpMenuNewEntry --
*
* Adds a pointer to a new menu entry structure with the platform-
* specific fields filled in. The Macintosh uses the platformEntryData
* field of the TkMenuEntry record.
*
* Results:
* Standard TCL error.
*
* Side effects:
* Storage gets allocated. New menu entry data is put into the
* platformEntryData field of the mePtr.
*
*----------------------------------------------------------------------
*/
int
TkpMenuNewEntry(
TkMenuEntry *mePtr) /* The menu we are adding an entry to */
{
TKMenu *menu = (TKMenu *) mePtr->menuPtr->platformData;
NSMenuItem *menuItem;
if (mePtr->type == SEPARATOR_ENTRY || mePtr->type == TEAROFF_ENTRY) {
menuItem = [[NSMenuItem separatorItem] retain];
} else {
menuItem = [menu newTkMenuItem:mePtr];
}
mePtr->platformEntryData = (TkMenuPlatformEntryData)
TkMacOSXMakeUncollectable(menuItem);
/*
* Caller TkMenuEntry() already did this same insertion into the generic
* TkMenu so we just match it for the platform menu.
*/
[menu insertItem:menuItem atTkIndex:mePtr->index];
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkpConfigureMenuEntry --
*
* Processes configurations for menu entries.
*
* Results:
* Returns standard TCL result. If TCL_ERROR is returned, then the
* interp's result contains an error message.
*
* Side effects:
* Configuration information get set for mePtr; old resources get freed,
* if any need it.
*
*----------------------------------------------------------------------
*/
int
TkpConfigureMenuEntry(
TkMenuEntry *mePtr) /* Information about menu entry; may or may
* not already have values for some fields. */
{
NSMenuItem *menuItem = (NSMenuItem *) mePtr->platformEntryData;
NSString *title = @"";
NSAttributedString *attributedTitle = nil;
NSImage *image = nil;
NSString *keyEquivalent = @"";
NSUInteger modifierMask = NSCommandKeyMask;
NSMenu *submenu = nil;
NSDictionary *attributes;
int imageWidth, imageHeight;
GC gc = (mePtr->textGC ? mePtr->textGC : mePtr->menuPtr->textGC);
Tcl_Obj *fontPtr = (mePtr->fontPtr ? mePtr->fontPtr :
mePtr->menuPtr->fontPtr);
if (mePtr->image) {
Tk_SizeOfImage(mePtr->image, &imageWidth, &imageHeight);
image = TkMacOSXGetNSImageWithTkImage(mePtr->menuPtr->display,
mePtr->image, imageWidth, imageHeight);
} else if (mePtr->bitmapPtr != None) {
Pixmap bitmap = Tk_GetBitmapFromObj(mePtr->menuPtr->tkwin,
mePtr->bitmapPtr);
Tk_SizeOfBitmap(mePtr->menuPtr->display, bitmap, &imageWidth,
&imageHeight);
image = TkMacOSXGetNSImageWithBitmap(mePtr->menuPtr->display, bitmap,
gc, imageWidth, imageHeight);
}
[menuItem setImage:image];
if ((!image || mePtr->compound != COMPOUND_NONE) && mePtr->labelPtr &&
mePtr->labelLength) {
title = [[[NSString alloc] initWithBytes:Tcl_GetString(mePtr->labelPtr)
length:mePtr->labelLength encoding:NSUTF8StringEncoding]
autorelease];
if ([title hasSuffix:@"..."]) {
title = [NSString stringWithFormat:@"%@%C",
[title substringToIndex:[title length] - 3], 0x2026];
}
}
[menuItem setTitle:title];
if (strcmp(Tcl_GetString(fontPtr), "menu") || gc->foreground != defaultFg
|| gc->background != defaultBg) {
attributes = TkMacOSXNSFontAttributesForFont(Tk_GetFontFromObj(
mePtr->menuPtr->tkwin, fontPtr));
if (gc->foreground != defaultFg || gc->background != defaultBg) {
NSColor *color = TkMacOSXGetNSColor(gc,
gc->foreground!=defaultFg? gc->foreground:gc->background);
attributes = [[attributes mutableCopy] autorelease];
[(NSMutableDictionary *)attributes setObject:color
forKey:NSForegroundColorAttributeName];
}
if (attributes) {
attributedTitle = [[[NSAttributedString alloc]
initWithString:title attributes:attributes] autorelease];
}
}
[menuItem setAttributedTitle:attributedTitle];
[menuItem setEnabled:!(mePtr->state == ENTRY_DISABLED)];
[menuItem setState:((mePtr->type == CHECK_BUTTON_ENTRY ||
mePtr->type == RADIO_BUTTON_ENTRY) && mePtr->indicatorOn &&
(mePtr->entryFlags & ENTRY_SELECTED) ? NSOnState : NSOffState)];
if (mePtr->type != CASCADE_ENTRY && mePtr->accelPtr && mePtr->accelLength) {
keyEquivalent = ParseAccelerator(Tcl_GetString(mePtr->accelPtr),
&modifierMask);
}
[menuItem setKeyEquivalent:keyEquivalent];
[menuItem setKeyEquivalentModifierMask:modifierMask];
if (mePtr->type == CASCADE_ENTRY && mePtr->namePtr) {
TkMenuReferences *menuRefPtr;
menuRefPtr = TkFindMenuReferencesObj(mePtr->menuPtr->interp,
mePtr->namePtr);
if (menuRefPtr && menuRefPtr->menuPtr) {
CheckForSpecialMenu(menuRefPtr->menuPtr);
submenu = (TKMenu *) menuRefPtr->menuPtr->platformData;
if ([submenu supermenu] && [menuItem submenu] != submenu) {
/*
* This happens during a clone, where the parent menu is
* cloned before its children, so just ignore this temprary
* setting, it will be changed shortly (c.f. tkMenu.c
* CloneMenu())
*/
submenu = nil;
} else {
[submenu setTitle:title];
if ([menuItem isEnabled]) {
/* This menuItem might have been previously disabled (XXX:
track this), which would have disabled entries; we must
re-enable the entries here. */
int i = 0;
NSArray *itemArray = [submenu itemArray];
for (NSMenuItem *item in itemArray) {
TkMenuEntry *submePtr = menuRefPtr->menuPtr->entries[i];
[item setEnabled: !(submePtr->state == ENTRY_DISABLED)];
i++;
}
}
}
}
}
[menuItem setSubmenu:submenu];
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkpDestroyMenuEntry --
*
* Cleans up platform-specific menu entry items.
*
* Results:
* None
*
* Side effects:
* All platform-specific allocations are freed up.
*
*----------------------------------------------------------------------
*/
void
TkpDestroyMenuEntry(
TkMenuEntry *mePtr)
{
if (mePtr->platformEntryData && mePtr->menuPtr->platformData) {
TKMenu *menu = (TKMenu *) mePtr->menuPtr->platformData;
NSMenuItem *menuItem = (NSMenuItem *) mePtr->platformEntryData;
NSInteger index = [menu indexOfItem:menuItem];
if (index > -1) {
[menu removeItemAtIndex:index];
}
}
TkMacOSXMakeCollectableAndRelease(mePtr->platformEntryData);
}
/*
*----------------------------------------------------------------------
*
* TkpPostMenu --
*
* Posts a menu on the screen
*
* Results:
* None.
*
* Side effects:
* The menu is posted and handled.
*
*----------------------------------------------------------------------
*/
int
TkpPostMenu(
Tcl_Interp *interp, /* The interpreter this menu lives in */
TkMenu *menuPtr, /* The menu we are posting */
int x, /* The global x-coordinate of the top, left-
* hand corner of where the menu is supposed
* to be posted. */
int y) /* The global y-coordinate */
{
NSWindow *win = [NSApp keyWindow];
if (!win) {
return TCL_ERROR;
}
inPostMenu = 1;
int oldMode = Tcl_SetServiceMode(TCL_SERVICE_NONE);
NSView *view = [win contentView];
NSRect frame = NSMakeRect(x + 9, tkMacOSXZeroScreenHeight - y - 9, 1, 1);
frame.origin = [view convertPoint:
[win convertScreenToBase:frame.origin] fromView:nil];
NSMenu *menu = (NSMenu *) menuPtr->platformData;
NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc]
initTextCell:@"" pullsDown:NO];
[popUpButtonCell setAltersStateOfSelectedItem:NO];
[popUpButtonCell setMenu:menu];
[popUpButtonCell selectItem:nil];
[popUpButtonCell performClickWithFrame:frame inView:view];
[popUpButtonCell release];
Tcl_SetServiceMode(oldMode);
inPostMenu = 0;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkpSetWindowMenuBar --
*
* Associates a given menu with a window.
*
* Results:
* None.
*
* Side effects:
* On Windows and UNIX, associates the platform menu with the platform
* window.
*
*----------------------------------------------------------------------
*/
void
TkpSetWindowMenuBar(
Tk_Window tkwin, /* The window we are setting the menu in */
TkMenu *menuPtr) /* The menu we are setting */
{
TkWindow *winPtr = (TkWindow *) tkwin;
if (winPtr->wmInfoPtr) {
winPtr->wmInfoPtr->menuPtr = menuPtr;
}
}
/*
*----------------------------------------------------------------------
*
* TkpSetMainMenubar --
*
* Puts the menu associated with a window into the menubar. Should only
* be called when the window is in front.
*
* Results:
* None.
*
* Side effects:
* The menubar is changed.
*
*----------------------------------------------------------------------
*/
void
TkpSetMainMenubar(
Tcl_Interp *interp, /* The interpreter of the application */
Tk_Window tkwin, /* The frame we are setting up */
const char *menuName) /* The name of the menu to put in front. If
* NULL, use the default menu bar. */
{
static Tcl_Interp *currentInterp = NULL;
TKMenu *menu = nil;
if (menuName) {
TkWindow *winPtr = (TkWindow *) tkwin;
if (winPtr->wmInfoPtr && winPtr->wmInfoPtr->menuPtr &&
winPtr->wmInfoPtr->menuPtr->masterMenuPtr &&
winPtr->wmInfoPtr->menuPtr->masterMenuPtr->tkwin &&
!strcmp(menuName, Tk_PathName(
winPtr->wmInfoPtr->menuPtr->masterMenuPtr->tkwin))) {
menu = (TKMenu *) winPtr->wmInfoPtr->menuPtr->platformData;
} else {
TkMenuReferences *menuRefPtr = TkFindMenuReferences(interp,
menuName);
if (menuRefPtr && menuRefPtr->menuPtr &&
menuRefPtr->menuPtr->platformData) {
menu = (TKMenu *) menuRefPtr->menuPtr->platformData;
}
}
}
if (menu || interp != currentInterp) {
[NSApp tkSetMainMenu:menu];
}
currentInterp = interp;
}
/*
*----------------------------------------------------------------------
*
* CheckForSpecialMenu --
*
* Given a menu, check to see whether or not it is a cascade in a menubar
* with one of the special names .apple, .help or .window If it is, the
* entry that points to this menu will be marked.
*
* Results:
* None.
*
* Side effects:
* Will set entryFlags appropriately.
*
*----------------------------------------------------------------------
*/
static void
CheckForSpecialMenu(
TkMenu *menuPtr) /* The menu we are checking */
{
if (!menuPtr->masterMenuPtr->tkwin) {
return;
}
for (TkMenuEntry *cascadeEntryPtr = menuPtr->menuRefPtr->parentEntryPtr;
cascadeEntryPtr;
cascadeEntryPtr = cascadeEntryPtr->nextCascadePtr) {
if (cascadeEntryPtr->menuPtr->menuType == MENUBAR
&& cascadeEntryPtr->menuPtr->masterMenuPtr->tkwin) {
TkMenu *masterMenuPtr = cascadeEntryPtr->menuPtr->masterMenuPtr;
int i = 0;
Tcl_DString ds;
Tcl_DStringInit(&ds);
Tcl_DStringAppend(&ds, Tk_PathName(masterMenuPtr->tkwin), -1);
while (specialMenus[i].name) {
Tcl_DStringAppend(&ds, specialMenus[i].name,
specialMenus[i].len);
if (strcmp(Tcl_DStringValue(&ds),
Tk_PathName(menuPtr->masterMenuPtr->tkwin)) == 0) {
cascadeEntryPtr->entryFlags |= specialMenus[i].flag;
} else {
cascadeEntryPtr->entryFlags &= ~specialMenus[i].flag;
}
Tcl_DStringSetLength(&ds, Tcl_DStringLength(&ds) -
specialMenus[i].len);
i++;
}
Tcl_DStringFree(&ds);
}
}
}
/*
*----------------------------------------------------------------------
*
* ParseAccelerator --
*
* Parse accelerator string.
*
* Results:
* Accelerator string & flags.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static NSString *
ParseAccelerator(
const char *accel,
NSUInteger *maskPtr)
{
unichar ch = 0;
size_t len;
int i;
*maskPtr = 0;
while (1) {
i = 0;
while (modifiers[i].name) {
int l = modifiers[i].len;
if (!strncasecmp(accel, modifiers[i].name, l) &&
(accel[l] == '-' || accel[l] == '+')) {
*maskPtr |= modifiers[i].mask;
accel += l+1;
break;
}
i++;
}
if (!modifiers[i].name || !*accel) {
break;
}
}
len = strlen(accel);
if (len > 1) {
i = 0;
if (accel[0] == 'F' && len < 4 && accel[1] > '0' && accel[1] <= '9') {
int fkey = accel[1] - '0';
if (len == 3) {
if (accel[2] >= '0' && accel[2] <= '9') {
fkey = 10 * fkey + (accel[2] - '0');
} else {
fkey = 0;
}
}
if (fkey >= 1 && fkey <= 15) {
ch = NSF1FunctionKey + fkey - 1;
}
} else while (specialAccelerators[i].name) {
if (accel[0] == specialAccelerators[i].name[0] &&
len == specialAccelerators[i].len && !strncasecmp(accel,
specialAccelerators[i].name, specialAccelerators[i].len)) {
ch = specialAccelerators[i].ch;
break;
}
i++;
}
}
if (ch) {
return [[[NSString alloc] initWithCharacters:&ch length:1] autorelease];
} else {
return [[[[NSString alloc] initWithUTF8String:accel] autorelease]
lowercaseString];
}
}
/*
*--------------------------------------------------------------
*
* ModifierCharWidth --