ui/cocoa/menu.m

changeset 20
2dda1ad6dc7a
parent 19
f8f22eb0b575
child 21
012418e7dc90
equal deleted inserted replaced
19:f8f22eb0b575 20:2dda1ad6dc7a
27 */ 27 */
28 28
29 #import <stdio.h> 29 #import <stdio.h>
30 #import <stdlib.h> 30 #import <stdlib.h>
31 #import <string.h> 31 #import <string.h>
32 #import <stdarg.h>
32 33
33 #import "menu.h" 34 #import "menu.h"
34 #import "window.h" 35 #import "window.h"
35 #import "stock.h" 36 #import "stock.h"
36 37
76 } 77 }
77 78
78 @end 79 @end
79 80
80 81
82 @implementation UiGroupMenuItem
83
84 - (id)initWithTitle:(NSString*)title action:(SEL)action keyEquivalent:(NSString*)s {
85 [super initWithTitle:title action:action keyEquivalent:s];
86 groups = [[NSMutableArray alloc]initWithCapacity: 16];
87 return self;
88 }
89
90 - (void) addGroup:(int)group {
91 NSNumber *groupNumber = [NSNumber numberWithInteger:group];
92 [groups addObject:groupNumber];
93 }
94
95 - (void) checkGroups:(int*)g count:(int)n {
96 int c = [groups count];
97
98 char *check = calloc(1, c);
99 for(int i=0;i<n;i++) {
100 for(int k=0;k<c;k++) {
101 NSNumber *groupNumber = [groups objectAtIndex:k];
102 if([groupNumber intValue] == g[i]) {
103 check[k] = 1;
104 break;
105 }
106 }
107 }
108
109 for(int j=0;j<c;j++) {
110 if(check[j] == 0) {
111 [self setEnabled:NO];
112 return;
113 }
114 }
115 [self setEnabled:YES];
116 }
117
118 @end
81 119
82 120
83 static NSMenu *currentMenu = NULL; 121 static NSMenu *currentMenu = NULL;
84 static int currentItemIndex = 0; 122 static int currentItemIndex = 0;
85 static UiMenuDelegate *delegate; 123 static UiMenuDelegate *delegate;
98 136
99 NSMenu *menu = [[NSMenu alloc] initWithTitle: str]; 137 NSMenu *menu = [[NSMenu alloc] initWithTitle: str];
100 NSMenuItem *menuItem = [[NSApp mainMenu] addItemWithTitle:str 138 NSMenuItem *menuItem = [[NSApp mainMenu] addItemWithTitle:str
101 action:nil keyEquivalent:@""]; 139 action:nil keyEquivalent:@""];
102 [menu setDelegate: delegate]; 140 [menu setDelegate: delegate];
141 [menu setAutoenablesItems:NO];
103 142
104 [[NSApp mainMenu] setSubmenu:menu forItem:menuItem]; 143 [[NSApp mainMenu] setSubmenu:menu forItem:menuItem];
105 currentMenu = menu; 144 currentMenu = menu;
106 currentItemIndex = 0; 145 currentItemIndex = 0;
107 } 146 }
108 147
109 void ui_menuitem(char *label, ui_callback f, void *data) { 148 void ui_menuitem(char *label, ui_callback f, void *data) {
110 EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f]; 149 ui_menuitem_gr(label, f, data, -1);
111 NSString *str = [[NSString alloc] initWithUTF8String:label];
112
113 NSMenuItem *item = [currentMenu addItemWithTitle:str
114 action:@selector(handleEvent:) keyEquivalent:@""];
115 [item setTarget:event];
116
117 currentItemIndex++;
118 } 150 }
119 151
120 void ui_menuitem_st(char *stockid, ui_callback f, void *data) { 152 void ui_menuitem_st(char *stockid, ui_callback f, void *data) {
121 EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f]; 153 ui_menuitem_stgr(stockid, f, data, -1);
154 }
155
156 void ui_menuitem_gr(char *label, ui_callback f, void *userdata, ...) {
157 // create menu item
158 EventWrapper *event = [[EventWrapper alloc]initWithData:userdata callback:f];
159 NSString *title = [[NSString alloc] initWithUTF8String:label];
160 UiGroupMenuItem *item = [[UiGroupMenuItem alloc]initWithTitle:title action:@selector(handleEvent:) keyEquivalent:@""];
161 [item setTarget:event];
162
163 // add groups
164 va_list ap;
165 va_start(ap, userdata);
166 int group;
167 while((group = va_arg(ap, int)) != -1) {
168 [item addGroup: group];
169 }
170
171
172 [currentMenu addItem:item];
173
174 currentItemIndex++;
175 }
176
177 void ui_menuitem_stgr(char *stockid, ui_callback f, void *userdata, ...) {
178 // create menu item
179 EventWrapper *event = [[EventWrapper alloc]initWithData:userdata callback:f];
122 UiStockItem *si = ui_get_stock_item(stockid); 180 UiStockItem *si = ui_get_stock_item(stockid);
123 181 UiGroupMenuItem *item = [[UiGroupMenuItem alloc]initWithTitle:si->label
124 NSMenuItem *item = [currentMenu addItemWithTitle:si->label 182 action:@selector(handleEvent:)
125 action:@selector(handleEvent:) keyEquivalent:si->keyEquivalent]; 183 keyEquivalent:si->keyEquivalent];
126 [item setTarget:event]; 184 [item setTarget:event];
185
186 // add groups
187 va_list ap;
188 va_start(ap, userdata);
189 int group;
190 while((group = va_arg(ap, int)) != -1) {
191 [item addGroup: group];
192 }
193
194
195 [currentMenu addItem:item];
127 196
128 currentItemIndex++; 197 currentItemIndex++;
129 } 198 }
130 199
131 void ui_checkitem(char *label, ui_callback f, void *data) { 200 void ui_checkitem(char *label, ui_callback f, void *data) {

mercurial