ui/cocoa/menu.m

changeset 14
e2fd132ab781
parent 13
2dbc56c2323b
child 15
7a36f91c22f7
equal deleted inserted replaced
13:2dbc56c2323b 14:e2fd132ab781
35 35
36 @implementation UiMenuDelegate 36 @implementation UiMenuDelegate
37 37
38 - (UiMenuDelegate*) init { 38 - (UiMenuDelegate*) init {
39 items = NULL; 39 items = NULL;
40 itemlists = NULL;
40 return self; 41 return self;
41 } 42 }
42 43
43 - (void) menuNeedsUpdate:(NSMenu *) menu { 44 - (void) menuNeedsUpdate:(NSMenu *) menu {
44 NSWindow *activeWindow = [NSApp keyWindow]; 45 NSWindow *activeWindow = [NSApp keyWindow];
50 i->item = item; 51 i->item = item;
51 i->var = name; 52 i->var = name;
52 items = ucx_list_append(items, i); 53 items = ucx_list_append(items, i);
53 } 54 }
54 55
56 - (void) addList:(UiList*) list menu:(NSMenu*)menu index: (int)i callback: (ui_callback)f data:(void*) data {
57 UiMenuItemList *itemList = malloc(sizeof(UiMenuItemList));
58 itemList->list = list;
59 itemList->menu = menu;
60 itemList->first = NULL;
61 itemList->index = i;
62 itemList->oldcount = 0;
63 itemList->callback = f;
64 itemList->data = data;
65 itemlists = ucx_list_append(itemlists, itemList);
66 }
67
55 - (UcxList*) items { 68 - (UcxList*) items {
56 return items; 69 return items;
57 } 70 }
58 71
72 - (UcxList*) lists {
73 return itemlists;
74
75 }
76
59 @end 77 @end
60 78
61 79
62 80
63 81
64 static NSMenu *currentMenu = NULL; 82 static NSMenu *currentMenu = NULL;
83 static int currentItemIndex = 0;
65 static UiMenuDelegate *delegate; 84 static UiMenuDelegate *delegate;
66 85
67 void ui_menu_init() { 86 void ui_menu_init() {
68 delegate = [[UiMenuDelegate alloc]init]; 87 delegate = [[UiMenuDelegate alloc]init];
69 } 88 }
81 action:nil keyEquivalent:@""]; 100 action:nil keyEquivalent:@""];
82 [menu setDelegate: delegate]; 101 [menu setDelegate: delegate];
83 102
84 [[NSApp mainMenu] setSubmenu:menu forItem:menuItem]; 103 [[NSApp mainMenu] setSubmenu:menu forItem:menuItem];
85 currentMenu = menu; 104 currentMenu = menu;
105 currentItemIndex = 0;
86 } 106 }
87 107
88 void ui_menuitem(char *label, ui_callback f, void *data) { 108 void ui_menuitem(char *label, ui_callback f, void *data) {
89 EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f]; 109 EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f];
90 NSString *str = [[NSString alloc] initWithUTF8String:label]; 110 NSString *str = [[NSString alloc] initWithUTF8String:label];
92 NSMenuItem *item = [currentMenu addItemWithTitle:str 112 NSMenuItem *item = [currentMenu addItemWithTitle:str
93 action:@selector(handleEvent:) keyEquivalent:@""]; 113 action:@selector(handleEvent:) keyEquivalent:@""];
94 [item setTarget:event]; 114 [item setTarget:event];
95 115
96 //[delegate addItem: item]; 116 //[delegate addItem: item];
117 currentItemIndex++;
97 } 118 }
98 119
99 void ui_checkitem(char *label, ui_callback f, void *data) { 120 void ui_checkitem(char *label, ui_callback f, void *data) {
100 EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f]; 121 EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f];
101 NSString *str = [[NSString alloc] initWithUTF8String:label]; 122 NSString *str = [[NSString alloc] initWithUTF8String:label];
103 NSMenuItem *item = [currentMenu addItemWithTitle:str 124 NSMenuItem *item = [currentMenu addItemWithTitle:str
104 action:@selector(handleStateEvent:) keyEquivalent:@""]; 125 action:@selector(handleStateEvent:) keyEquivalent:@""];
105 [item setTarget:event]; 126 [item setTarget:event];
106 127
107 [delegate addItem: item var:NULL]; 128 [delegate addItem: item var:NULL];
129 currentItemIndex++;
108 } 130 }
109 131
110 void ui_checkitem_nv(char *label, char *vname) { 132 void ui_checkitem_nv(char *label, char *vname) {
111 EventWrapper *event = [[EventWrapper alloc]initWithData:NULL callback:NULL]; 133 EventWrapper *event = [[EventWrapper alloc]initWithData:NULL callback:NULL];
112 NSString *str = [[NSString alloc] initWithUTF8String:label]; 134 NSString *str = [[NSString alloc] initWithUTF8String:label];
114 NSMenuItem *item = [currentMenu addItemWithTitle:str 136 NSMenuItem *item = [currentMenu addItemWithTitle:str
115 action:@selector(handleStateEvent:) keyEquivalent:@""]; 137 action:@selector(handleStateEvent:) keyEquivalent:@""];
116 [item setTarget:event]; 138 [item setTarget:event];
117 139
118 [delegate addItem: item var:vname]; 140 [delegate addItem: item var:vname];
141 currentItemIndex++;
119 } 142 }
120 143
121 void ui_menuseparator() { 144 void ui_menuseparator() {
122 145 [currentMenu addItem: [NSMenuItem separatorItem]];
146 currentItemIndex++;
147 }
148
149 void ui_menuitem_list (UiList *items, ui_callback f, void *data) {
150 [delegate addList:items menu:currentMenu index:currentItemIndex callback:f data:data];
123 } 151 }
124 152
125 153
126 154
127 int ui_menuitem_get(UiInteger *i) { 155 int ui_menuitem_get(UiInteger *i) {
134 UiMenuItem *item = i->obj; 162 UiMenuItem *item = i->obj;
135 [item->item setState: value]; 163 [item->item setState: value];
136 i->value = value; 164 i->value = value;
137 item->state = value; 165 item->state = value;
138 } 166 }
167
168
169 int ui_update_item(UiCocoaWindow *window, void *data) {
170 UiMenuItem *item = data;
171 [item->item setState: item->state];
172 return 0;
173 }
174
175 int ui_update_item_list(UiCocoaWindow *window, void *data) {
176 printf("update list\n");
177
178 UiMenuItemList *itemList = data;
179 UiList *list = itemList->list;
180
181 printf("oldcount: %d\n", itemList->oldcount);
182 for(int r=0;r<itemList->oldcount;r++) {
183 [itemList->menu removeItemAtIndex:itemList->index];
184 }
185
186 char *str = ui_list_first(list);
187 int i = itemList->index;
188 [itemList->menu insertItem: [NSMenuItem separatorItem] atIndex: i];
189 i++;
190 do {
191 NSString *title = [[NSString alloc] initWithUTF8String:str];
192 NSMenuItem *item = [[NSMenuItem alloc]initWithTitle:title action:@selector(handleStateEvent:) keyEquivalent:@""];
193
194 [itemList->menu insertItem:item atIndex:i];
195 i++;
196 } while ((str = ui_list_next(list)));
197
198 itemList->oldcount = i - itemList->index;
199
200 return 0;
201 }

mercurial