implemented sub menus (Cocoa)

Wed, 13 Jul 2016 15:14:09 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 13 Jul 2016 15:14:09 +0200
changeset 125
3335268a8073
parent 124
80609f9675f1
child 126
f190d03dce0f

implemented sub menus (Cocoa)

application/main.c file | annotate | diff | comparison | revisions
ui/cocoa/menu.m file | annotate | diff | comparison | revisions
ui/cocoa/window.m file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon May 23 12:28:32 2016 +0200
+++ b/application/main.c	Wed Jul 13 15:14:09 2016 +0200
@@ -39,13 +39,13 @@
 
 void action_menu(UiEvent *event, void *data) {
     printf("action_menu test: {%s}\n", data);
-    printf("text: {%s}\n", ui_gettext(event->obj, "text"));
-    fflush(stdout);
-    
-    char *file = ui_openfiledialog(event->obj);
-    printf("file: %s\n", file);
-    fflush(stdout);
-    free(file);
+    //printf("text: {%s}\n", ui_gettext(event->obj, "text"));
+    //fflush(stdout);
+    //
+    //char *file = ui_openfiledialog(event->obj);
+    ////printf("file: %s\n", file);
+    //fflush(stdout);
+    //free(file);
 }
 
 void action_button(UiEvent *event, void *data) {
@@ -59,10 +59,11 @@
 UiTextLayout *text;
 
 void action_scroll(UiEvent *event, void *data) {
-    ui_drawingarea_redraw(drawingarea);
+    //ui_drawingarea_redraw(drawingarea);
     printf("scroll\n");
 }
 
+/*
 void draw(UiEvent *event, UiGraphics *g, void *data) {
     double adjust = range.get(&range);
     
@@ -95,6 +96,7 @@
     
     //ui_text_free(text);
     //*/
+    /*
 }
 
 void* model_getval(void *obj, int col) {
@@ -120,6 +122,7 @@
 void window_close(UiEvent *event, void *data) {
     printf("window close\n");
 }
+*/
 
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
@@ -139,21 +142,16 @@
     ui_submenu_end();
     ui_menuitem("item4", NULL, NULL);
     
-    ui_toolitem("button1", "Test", action_button, NULL);
-    ui_toolitem("button2", "OK", action_button, NULL);
-    ui_toolbar_combobox_str("combo", list, NULL, NULL);
-    ui_toolbar_add_default("button1");
-    ui_toolbar_add_default("button2");
-    ui_toolbar_add_default("combo");
+    //ui_toolitem("button1", "Test", action_button, NULL);
+    //ui_toolitem("button2", "OK", action_button, NULL);
+    //ui_toolbar_combobox_str("combo", list, NULL, NULL);
+    //ui_toolbar_add_default("button1");
+    //ui_toolbar_add_default("button2");
+    //ui_toolbar_add_default("combo");
     
     UiObject *obj = ui_window("Test", NULL);
-    ui_context_closefunc(obj->ctx, window_close, NULL);
-    
-///*
-    UiModelInfo *modeldesc = ui_model_info(obj->ctx, UI_ICON_TEXT, "Name", UI_STRING, "Mail", -1);
-    modeldesc->getvalue = model_getval;
-    ui_table(obj, list, modeldesc);
-//*/
+    //ui_context_closefunc(obj->ctx, window_close, NULL);
+
     
     ui_show(obj);
     ui_main();
--- a/ui/cocoa/menu.m	Mon May 23 12:28:32 2016 +0200
+++ b/ui/cocoa/menu.m	Wed Jul 13 15:14:09 2016 +0200
@@ -118,7 +118,10 @@
 @end
 
 
-static NSMenu *currentMenu = NULL;
+//static NSMenu *currentMenu = NULL;
+
+static UcxList *current;
+
 static int currentItemIndex = 0;
 static UiMenuDelegate *delegate;
 
@@ -141,8 +144,34 @@
     [menu setAutoenablesItems:NO];
     
     [[NSApp mainMenu] setSubmenu:menu forItem:menuItem];
-    currentMenu = menu;
+    //currentMenu = menu;
     currentItemIndex = 0;
+    
+    current = ucx_list_prepend(NULL, menu);
+}
+
+void ui_submenu(char *title) {
+    NSString *str = [[NSString alloc] initWithUTF8String:title];
+    NSMenu *currentMenu = current->data;
+    
+    NSMenu *menu = [[NSMenu alloc] initWithTitle: str];
+    NSMenuItem *menuItem = [currentMenu addItemWithTitle:str
+                                                       action:nil keyEquivalent:@""];
+    [menu setDelegate: delegate];
+    [menu setAutoenablesItems:NO];
+    
+    [currentMenu setSubmenu:menu forItem:menuItem];
+    //currentMenu = menu;
+    currentItemIndex = 0;
+    
+    current = ucx_list_prepend(current, menu);
+}
+
+void ui_submenu_end() {
+    if(ucx_list_size(current) < 2) {
+        return;
+    }
+    current = ucx_list_remove(current, current);
 }
 
 void ui_menuitem(char *label, ui_callback f, void *data) {
@@ -169,6 +198,7 @@
     }
     va_end(ap);
     
+    NSMenu *currentMenu = current->data;
     [currentMenu addItem:item];
     
     currentItemIndex++;
@@ -192,6 +222,7 @@
     }
     va_end(ap);
     
+    NSMenu *currentMenu = current->data;
     [currentMenu addItem:item];
     
     currentItemIndex++;
@@ -201,6 +232,7 @@
     EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f];
     NSString *str = [[NSString alloc] initWithUTF8String:label];
     
+    NSMenu *currentMenu = current->data;
     NSMenuItem *item = [currentMenu addItemWithTitle:str
                                               action:@selector(handleStateEvent:) keyEquivalent:@""];
     [item setTarget:event];
@@ -213,6 +245,7 @@
     EventWrapper *event = [[EventWrapper alloc]initWithData:NULL callback:NULL];
     NSString *str = [[NSString alloc] initWithUTF8String:label];
     
+    NSMenu *currentMenu = current->data;
     NSMenuItem *item = [currentMenu addItemWithTitle:str
                                               action:@selector(handleStateEvent:) keyEquivalent:@""];
     [item setTarget:event];
@@ -222,11 +255,13 @@
 }
 
 void ui_menuseparator() {
+    NSMenu *currentMenu = current->data;
     [currentMenu addItem: [NSMenuItem separatorItem]];
     currentItemIndex++;
 }
 
 void ui_menuitem_list (UiList *items, ui_callback f, void *data) {
+    NSMenu *currentMenu = current->data;
     [delegate addList:items menu:currentMenu index:currentItemIndex callback:f data:data];
 }
 
--- a/ui/cocoa/window.m	Mon May 23 12:28:32 2016 +0200
+++ b/ui/cocoa/window.m	Wed Jul 13 15:14:09 2016 +0200
@@ -186,7 +186,6 @@
     
     obj->widget = (NSView*)window;
     obj->window = window_data;
-    obj->document = NULL;
     obj->container = ui_window_container(obj, window);
     
     
--- a/ui/ui/toolkit.h	Mon May 23 12:28:32 2016 +0200
+++ b/ui/ui/toolkit.h	Wed Jul 13 15:14:09 2016 +0200
@@ -34,8 +34,10 @@
 #ifdef __OBJC__
 #import <Cocoa/Cocoa.h>
 #define UIWIDGET NSView*
+#define UIMENU   NSMenu*
 #else
 typedef void* UIWIDGET;
+typedef void* UIMENU;
 #endif
 
 #elif UI_GTK2 || UI_GTK3

mercurial