ui/cocoa/menu.m

changeset 10
6f263196f916
parent 7
431dde3c5fbe
child 13
2dbc56c2323b
--- a/ui/cocoa/menu.m	Wed Mar 26 15:53:43 2014 +0100
+++ b/ui/cocoa/menu.m	Fri Mar 28 20:03:01 2014 +0100
@@ -31,9 +31,47 @@
 #import <string.h>
 
 #import "menu.h"
+#import "window.h"
+
+@implementation UiMenuDelegate 
+
+- (UiMenuDelegate*) init {
+    items = NULL;
+    return self;
+}
+
+- (void) menuNeedsUpdate:(NSMenu *) menu {
+    NSWindow *activeWindow = [NSApp keyWindow];
+    [(UiCocoaWindow*)activeWindow updateMenu: menu];
+}
+
+- (void) addItem:(NSMenuItem*) item var: (char*)name {
+    UiStateItem *i = malloc(sizeof(UiStateItem));
+    i->item = item;
+    i->var = name;
+    items = ucx_list_append(items, i);
+}
+
+- (UcxList*) items {
+    return items;
+}
+
+@end
+
+
 
 
 static NSMenu *currentMenu = NULL;
+static UiMenuDelegate *delegate;
+
+void ui_menu_init() {
+    delegate = [[UiMenuDelegate alloc]init];
+}
+
+UiMenuDelegate* ui_menu_delegate() {
+    return delegate;
+}
+
 
 void ui_menu(char *title) {
     NSString *str = [[NSString alloc] initWithUTF8String:title];
@@ -41,6 +79,7 @@
     NSMenu *menu = [[NSMenu alloc] initWithTitle: str];
     NSMenuItem *menuItem = [[NSApp mainMenu] addItemWithTitle:str
                                                        action:nil keyEquivalent:@""];
+    [menu setDelegate: delegate];
     
     [[NSApp mainMenu] setSubmenu:menu forItem:menuItem];
     currentMenu = menu;
@@ -53,14 +92,30 @@
     NSMenuItem *item = [currentMenu addItemWithTitle:str
                                               action:@selector(handleEvent:) keyEquivalent:@""];
     [item setTarget:event];
+    
+    //[delegate addItem: item];
 }
 
-void ui_checkitem(char *label, ui_callback f, void *userdata) {
+void ui_checkitem(char *label, ui_callback f, void *data) {
+    EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f];
+    NSString *str = [[NSString alloc] initWithUTF8String:label];
     
+    NSMenuItem *item = [currentMenu addItemWithTitle:str
+                                              action:@selector(handleStateEvent:) keyEquivalent:@""];
+    [item setTarget:event];
+    
+    [delegate addItem: item var:NULL];
 }
 
 void ui_checkitem_nv(char *label, char *vname) {
+    EventWrapper *event = [[EventWrapper alloc]initWithData:NULL callback:NULL];
+    NSString *str = [[NSString alloc] initWithUTF8String:label];
     
+    NSMenuItem *item = [currentMenu addItemWithTitle:str
+                                              action:@selector(handleStateEvent:) keyEquivalent:@""];
+    [item setTarget:event];
+    
+    [delegate addItem: item var:vname];
 }
 
 void ui_menuseparator() {
@@ -68,3 +123,15 @@
 }
 
 
+
+int ui_menuitem_get(UiInteger *i) {
+    UiMenuItem *item = i->obj;
+    i->value = [item->item state];
+    return i->value;
+}
+
+void ui_menuitem_set(UiInteger *i, int value) {
+    UiMenuItem *item = i->obj;
+    [item->item setState: value];
+    i->value = value;
+}

mercurial