added groups for toolbar items (Cocoa)

Sat, 05 Apr 2014 13:02:37 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 05 Apr 2014 13:02:37 +0200
changeset 25
78ae3efe463f
parent 24
06bceda81a03
child 26
a137277f9173

added groups for toolbar items (Cocoa)

ui/cocoa/menu.m file | annotate | diff | comparison | revisions
ui/cocoa/toolbar.h file | annotate | diff | comparison | revisions
ui/cocoa/toolbar.m file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.m file | annotate | diff | comparison | revisions
ui/cocoa/window.m file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/ui/toolbar.h file | annotate | diff | comparison | revisions
--- a/ui/cocoa/menu.m	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/cocoa/menu.m	Sat Apr 05 13:02:37 2014 +0200
@@ -83,7 +83,7 @@
 
 - (id)initWithTitle:(NSString*)title action:(SEL)action keyEquivalent:(NSString*)s {
     [super initWithTitle:title action:action keyEquivalent:s];
-    groups = [[NSMutableArray alloc]initWithCapacity: 16];
+    groups = [[NSMutableArray alloc]initWithCapacity: 8];
     return self;
 }
 
--- a/ui/cocoa/toolbar.h	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/cocoa/toolbar.h	Sat Apr 05 13:02:37 2014 +0200
@@ -32,7 +32,13 @@
 
 @protocol UiToolItem
 - (NSToolbarItem *) createItem:(NSToolbar*)toolbar
-                    identifier:(NSString*)identifier;
+                    identifier:(NSString*)identifier
+                        object:(UiObject*)obj;
+
+- (void) addGroup:(int)group;
+
+- (UcxList*) groups;
+
 @end
 
 
@@ -42,10 +48,11 @@
  * creates a toolbar item from stock description
  */
 @interface UiToolbarStockItem : NSObject <UiToolItem> {
-    char          *name;
-    char          *stockid;
-    ui_callback   callback;
-    void          *userdata;
+    char           *name;
+    char           *stockid;
+    ui_callback    callback;
+    void           *userdata;
+    UcxList        *groups;
 }
 
 - (UiToolbarStockItem*) initWithIdentifier:(char*)identifier
@@ -53,6 +60,7 @@
                             callback:(ui_callback)f
                             userdata:(void*)data;
 
+
 @end
 
 
@@ -75,5 +83,18 @@
 @end
 
 
+/*
+ * UiToolbar
+ */
+@interface UiToolbar : NSToolbar {
+    UiObject *obj;
+}
+
+- (UiToolbar*) initWithObject:(UiObject*)object;
+
+- (UiObject*) object;
+
+@end
+
 void ui_toolbar_init();
-NSToolbar* ui_create_toolbar();
+NSToolbar* ui_create_toolbar(UiObject *obj);
--- a/ui/cocoa/toolbar.m	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/cocoa/toolbar.m	Sat Apr 05 13:02:37 2014 +0200
@@ -29,8 +29,11 @@
 #import <stdio.h>
 #import <stdlib.h>
 #import <string.h>
+#import <inttypes.h>
+#import <stdarg.h>
 
 #import "toolbar.h"
+#import "window.h"
 #import "stock.h"
 
 
@@ -49,12 +52,18 @@
     stockid = sid;
     callback = f;
     userdata = data;
+    groups = NULL;
     return self;
 }
 
+- (void) addGroup:(int)group {
+    groups = ucx_list_append(groups, (void*)(intptr_t)group);
+}
+
 
 - (NSToolbarItem *) createItem:(NSToolbar*)toolbar
                     identifier:(NSString*)identifier
+                        object:(UiObject*)obj
 {
     UiStockItem *s = ui_get_stock_item(stockid);
     if(s == nil) {
@@ -89,9 +98,17 @@
     [button setAction:@selector(handleEvent:)];
     [button setTarget:event];
     
+    if(groups) {
+        uic_add_group_widget(obj->ctx, item, groups);
+    }
+    
     return item;
 }
 
+- (UcxList*) groups {
+    return groups;
+}
+
 @end
 
 
@@ -156,7 +173,27 @@
 {
     Protocol *item = @protocol(UiToolItem);
     item = [items objectForKey: identifier];
-    return [item createItem:toolbar identifier:identifier];
+    
+    // get UiObject from toolbar
+    UiObject *obj = [(UiToolbar*)toolbar object];
+    
+    // create new NSToolbarItem
+    return [item createItem:toolbar identifier:identifier object:obj];
+}
+
+@end
+
+
+@implementation UiToolbar
+
+- (UiToolbar*) initWithObject:(UiObject*)object {
+    [self initWithIdentifier: @"MainToolbar"];
+    obj = object;
+    return self;
+}
+
+- (UiObject*) object {
+    return obj;
 }
 
 @end
@@ -169,13 +206,26 @@
 }
 
 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata) {
+    ui_toolitem_stgr(name, stockid, f, udata, -1);
+}
+
+void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) {
     UiToolbarStockItem *item = [[UiToolbarStockItem alloc]
                                 initWithIdentifier: name
-                                           stockID: stockid
-                                          callback: f
-                                          userdata: udata];
+                                stockID: stockid
+                                callback: f
+                                userdata: udata];
     NSString *identifier = [[NSString alloc]initWithUTF8String:name];
     [toolbar_delegate addItem: identifier item: item];
+    
+    // add groups
+    va_list ap;
+    va_start(ap, udata);
+    int group;
+    while((group = va_arg(ap, int)) != -1) {
+        [item addGroup: group];
+    }
+    va_end(ap);
 }
 
 void ui_toolbar_add_default(char *name) {
@@ -183,8 +233,8 @@
     [toolbar_delegate addDefault: identifier];
 }
 
-NSToolbar* ui_create_toolbar() {
-    NSToolbar *toolbar = [[NSToolbar alloc]initWithIdentifier: @"MainToolbar"];
+NSToolbar* ui_create_toolbar(UiObject *obj) {
+    UiToolbar *toolbar = [[UiToolbar alloc] initWithObject:obj];
     [toolbar setDelegate: toolbar_delegate];
     [toolbar setAllowsUserCustomization: true];
     return toolbar;
--- a/ui/cocoa/toolkit.m	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/cocoa/toolkit.m	Sat Apr 05 13:02:37 2014 +0200
@@ -62,7 +62,12 @@
     uic_load_app_properties();
 }
 
+char* ui_appname() {
+    return application_name;
+}
+
 void ui_show(UiObject *obj) {
+    uic_check_group_widgets(obj->ctx);
     if([obj->widget class] == [UiCocoaWindow class]) {
         UiCocoaWindow *window = (UiCocoaWindow*)obj->widget;
         [window makeKeyAndOrderFront:nil];
@@ -72,7 +77,7 @@
 }
 
 void ui_set_enabled(UIWIDGET widget, int enabled) {
-    //[widget setEnabled:enabled];
+    [(id)widget setEnabled: enabled];
 }
 
 void ui_main() {
--- a/ui/cocoa/window.m	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/cocoa/window.m	Sat Apr 05 13:02:37 2014 +0200
@@ -179,7 +179,7 @@
     [window setMenuItems: [menuDelegate items]];
     [window setMenuItemLists: [menuDelegate lists]];
     
-    NSToolbar *toolbar = ui_create_toolbar();
+    NSToolbar *toolbar = ui_create_toolbar(obj);
     [window setToolbar: toolbar];
     
     obj->widget = (NSView*)window;
--- a/ui/common/context.c	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/common/context.c	Sat Apr 05 13:02:37 2014 +0200
@@ -217,7 +217,7 @@
     free(groups);
 }
 
-void uic_add_group_widget(UiContext *ctx, UIWIDGET widget, UcxList *groups) {
+void uic_add_group_widget(UiContext *ctx, void *widget, UcxList *groups) {
     UcxMempool *mp = ctx->mempool;
     UiGroupWidget *gw = ucx_mempool_malloc(mp, sizeof(UiGroupWidget));
     
@@ -234,4 +234,3 @@
             ctx->group_widgets,
             gw);
 }
-
--- a/ui/common/context.h	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/common/context.h	Sat Apr 05 13:02:37 2014 +0200
@@ -77,7 +77,7 @@
 void* uic_create_value(UcxAllocator *a, int type);
 
 void uic_check_group_widgets(UiContext *ctx);
-void uic_add_group_widget(UiContext *ctx, UIWIDGET widget, UcxList *groups);
+void uic_add_group_widget(UiContext *ctx, void *widget, UcxList *groups);
 
 #ifdef	__cplusplus
 }
--- a/ui/ui/toolbar.h	Fri Apr 04 17:45:36 2014 +0200
+++ b/ui/ui/toolbar.h	Sat Apr 05 13:02:37 2014 +0200
@@ -37,6 +37,7 @@
 
 void ui_toolitem(char *name, char *label, ui_callback f, void *udata);
 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata);
+void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *udata, ...);
 
 void ui_toolbar_add_default(char *name);
 

mercurial