forked from fyne-io/fyne
/
menu_darwin.m
190 lines (159 loc) · 5.18 KB
/
menu_darwin.m
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
// +build !no_native_menus
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
const int menuTagMin = 5000;
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
NSControlStateValue STATE_ON = NSControlStateValueOn;
NSControlStateValue STATE_OFF = NSControlStateValueOff;
#else
NSControlStateValue STATE_ON = NSOnState;
NSControlStateValue STATE_OFF = NSOffState;
#endif
extern void menuCallback(int);
extern BOOL menuEnabled(int);
extern BOOL menuChecked(int);
extern void exceptionCallback(const char*);
@interface FyneMenuHandler : NSObject {
}
@end
@implementation FyneMenuHandler
+ (void) tapped:(NSMenuItem*) item {
menuCallback([item tag]-menuTagMin);
}
+ (BOOL) validateMenuItem:(NSMenuItem*) item {
BOOL checked = menuChecked([item tag]-menuTagMin);
if (checked) {
[item setState:STATE_ON];
} else {
[item setState:STATE_OFF];
}
return menuEnabled([item tag]-menuTagMin);
}
@end
// forward declaration … we want methods to be ordered alphabetically
NSMenu* nativeMainMenu();
void assignDarwinSubmenu(const void* i, const void* m) {
NSMenu* menu = (NSMenu*)m; // this menu is created in the createDarwinMenu() function
NSMenuItem *item = (NSMenuItem*)i;
[item setSubmenu:menu]; // this retains the menu
[menu release]; // release the menu
}
void completeDarwinMenu(const void* m, bool prepend) {
NSMenu* main = nativeMainMenu();
NSMenuItem* top = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[top setTag:menuTagMin];
if (prepend) {
[main insertItem:top atIndex:1];
} else {
[main addItem:top];
}
assignDarwinSubmenu(top, m);
}
const void* createDarwinMenu(const char* label) {
return (void*)[[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:label]];
}
const void* darwinAppMenu() {
return [[nativeMainMenu() itemAtIndex:0] submenu];
}
void handleException(const char* m, id e) {
exceptionCallback([[NSString stringWithFormat:@"%s failed: %@", m, e] UTF8String]);
}
const void* insertDarwinMenuItem(const void* m, const char* label, int id, int index, bool isSeparator) {
NSMenu* menu = (NSMenu*)m;
NSMenuItem* item;
if (isSeparator) {
item = [NSMenuItem separatorItem];
} else {
item = [[NSMenuItem alloc]
initWithTitle:[NSString stringWithUTF8String:label]
action:@selector(tapped:)
keyEquivalent:@""];
[item setTarget:[FyneMenuHandler class]];
[item setTag:id+menuTagMin];
}
if (index > -1) {
[menu insertItem:item atIndex:index];
} else {
[menu addItem:item];
}
[item release]; // retained by the menu
return item;
}
NSMenu* nativeMainMenu() {
NSApplication* app = [NSApplication sharedApplication];
return [app mainMenu];
}
void resetDarwinMenu() {
NSMenu *root = nativeMainMenu();
NSEnumerator *items = [[root itemArray] objectEnumerator];
id object;
while (object = [items nextObject]) {
NSMenuItem *item = object;
if ([item tag] < menuTagMin) {
// check for inserted items (like Settings...)
NSMenu *menu = [item submenu];
NSEnumerator *subItems = [[menu itemArray] objectEnumerator];
id sub;
while (sub = [subItems nextObject]) {
NSMenuItem *item = sub;
if ([item tag] >= menuTagMin) {
[menu removeItem: item];
}
}
continue;
}
[root removeItem: item];
}
}
const void* test_darwinMainMenu() {
return nativeMainMenu();
}
const void* test_NSMenu_itemAtIndex(const void* m, NSInteger i) {
NSMenu* menu = (NSMenu*)m;
@try {
return [menu itemAtIndex: i];
} @catch(NSException* e) {
handleException("test_NSMenu_itemAtIndex", e);
return NULL;
}
}
NSInteger test_NSMenu_numberOfItems(const void* m) {
NSMenu* menu = (NSMenu*)m;
return [menu numberOfItems];
}
void test_NSMenu_performActionForItemAtIndex(const void* m, NSInteger i) {
NSMenu* menu = (NSMenu*)m;
@try {
// Using performActionForItemAtIndex: would be better but sadly it crashes.
// We simulate the relevant effect for now.
// [menu performActionForItemAtIndex:i];
NSMenuItem* item = [menu itemAtIndex:i];
[[item target] performSelector:[item action] withObject:item];
} @catch(NSException* e) {
handleException("test_NSMenu_performActionForItemAtIndex", e);
}
}
void test_NSMenu_removeItemAtIndex(const void* m, NSInteger i) {
NSMenu* menu = (NSMenu*)m;
@try {
[menu removeItemAtIndex: i];
} @catch(NSException* e) {
handleException("test_NSMenu_removeItemAtIndex", e);
}
}
const char* test_NSMenu_title(const void* m) {
NSMenu* menu = (NSMenu*)m;
return [[menu title] UTF8String];
}
bool test_NSMenuItem_isSeparatorItem(const void* i) {
NSMenuItem* item = (NSMenuItem*)i;
return [item isSeparatorItem];
}
const void* test_NSMenuItem_submenu(const void* i) {
NSMenuItem* item = (NSMenuItem*)i;
return [item submenu];
}
const char* test_NSMenuItem_title(const void* i) {
NSMenuItem* item = (NSMenuItem*)i;
return [[item title] UTF8String];
}