added toolbar toggle button (Cocoa)

Tue, 20 May 2014 13:29:53 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 20 May 2014 13:29:53 +0200
changeset 49
a80ba8741be6
parent 48
0b8ac9d6d473
child 50
22b5adeb371f

added toolbar toggle button (Cocoa)

application/main.c 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.h file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.m file | annotate | diff | comparison | revisions
ui/gtk/toolbar.c file | annotate | diff | comparison | revisions
ui/ui/toolbar.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon May 19 21:15:43 2014 +0200
+++ b/application/main.c	Tue May 20 13:29:53 2014 +0200
@@ -65,6 +65,10 @@
     printf("\n");
 }
 
+void action_button(UiEvent *event, void *data) {
+    printf("button: %d\n", event->intval);
+}
+
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
     ui_locales_dir("/opt/app1/locales");
@@ -75,6 +79,10 @@
     
     ui_menu("File");
     ui_menuitem_st(UI_STOCK_CLOSE, action_close, NULL);
+    ui_checkitem("Check", action_button, NULL);
+    
+    ui_toolitem_toggle_st("button", UI_STOCK_GO_BACK, action_button, NULL);
+    ui_toolbar_add_default("button");
     
     printf("create window\n");
     UiObject *window = ui_window("Mod0", NULL);
--- a/ui/cocoa/toolbar.h	Mon May 19 21:15:43 2014 +0200
+++ b/ui/cocoa/toolbar.h	Tue May 20 13:29:53 2014 +0200
@@ -28,6 +28,7 @@
 
 #import "../ui/toolbar.h"
 #import "toolkit.h"
+#import <stdarg.h>
 
 
 @protocol UiToolItem
@@ -53,6 +54,7 @@
     ui_callback    callback;
     void           *userdata;
     UcxList        *groups;
+    BOOL           isToggleButton;
 }
 
 - (UiToolbarStockItem*) initWithIdentifier:(char*)identifier
@@ -60,6 +62,8 @@
                             callback:(ui_callback)f
                             userdata:(void*)data;
 
+- (void) setIsToggleButton:(BOOL)t;
+
 
 @end
 
@@ -97,4 +101,5 @@
 @end
 
 void ui_toolbar_init();
+void ui_toolbar_stock_button(char *name, char *stockid, BOOL toggle, ui_callback f, void *udata, va_list ap);
 NSToolbar* ui_create_toolbar(UiObject *obj);
--- a/ui/cocoa/toolbar.m	Mon May 19 21:15:43 2014 +0200
+++ b/ui/cocoa/toolbar.m	Tue May 20 13:29:53 2014 +0200
@@ -53,9 +53,14 @@
     callback = f;
     userdata = data;
     groups = NULL;
+    isToggleButton = NO;
     return self;
 }
 
+- (void) setIsToggleButton:(BOOL)t {
+    isToggleButton = t;
+}
+
 - (void) addGroup:(int)group {
     groups = ucx_list_append(groups, (void*)(intptr_t)group);
 }
@@ -90,18 +95,23 @@
         [button setImage:[NSImage imageNamed: NSImageNameRemoveTemplate]];
     }
     [button setBezelStyle: NSTexturedRoundedBezelStyle];
-    [item setView:button];
     
     // event
     EventWrapper *event = [[EventWrapper alloc]
                            initWithData:userdata callback:callback];
-    [button setAction:@selector(handleEvent:)];
+    if(isToggleButton) {
+        [button setButtonType: NSPushOnPushOffButton];
+        [button setAction:@selector(handleToggleEvent:)];
+    } else {
+        [button setAction:@selector(handleEvent:)];
+    }
     [button setTarget:event];
     
     if(groups) {
         uic_add_group_widget(obj->ctx, item, groups);
     }
     
+    [item setView:button];
     return item;
 }
 
@@ -210,24 +220,42 @@
 }
 
 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) {
+    va_list ap;
+    va_start(ap, udata);
+    ui_toolbar_stock_button(name, stockid, NO, f, udata, ap);
+    va_end(ap);
+}
+
+void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata) {
+    ui_toolitem_toggle_stgr(name, stockid, f, udata, -1);
+}
+
+void ui_toolitem_toggle_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) {
+    va_list ap;
+    va_start(ap, udata);
+    ui_toolbar_stock_button(name, stockid, YES, f, udata, ap);
+    va_end(ap);
+}
+
+
+void ui_toolbar_stock_button(char *name, char *stockid, BOOL toggle, ui_callback f, void *udata, va_list ap) {
     UiToolbarStockItem *item = [[UiToolbarStockItem alloc]
                                 initWithIdentifier: name
                                 stockID: stockid
                                 callback: f
                                 userdata: udata];
+    [item setIsToggleButton: toggle];
     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) {
     NSString *identifier = [[NSString alloc]initWithUTF8String:name];
     [toolbar_delegate addDefault: identifier];
--- a/ui/cocoa/toolkit.h	Mon May 19 21:15:43 2014 +0200
+++ b/ui/cocoa/toolkit.h	Tue May 20 13:29:53 2014 +0200
@@ -61,6 +61,7 @@
 
 - (BOOL)handleEvent:(id)sender;
 - (BOOL)handleStateEvent:(id)sender;
+- (BOOL)handleToggleEvent:(id)sender;
 
 @end
 
--- a/ui/cocoa/toolkit.m	Mon May 19 21:15:43 2014 +0200
+++ b/ui/cocoa/toolkit.m	Tue May 20 13:29:53 2014 +0200
@@ -238,8 +238,7 @@
 
 - (BOOL)handleStateEvent:(id)sender {
     NSWindow *activeWindow = [NSApp keyWindow];
-    NSMenuItem *item = (NSMenuItem*)sender;
-    int state = [item state] ? NSOffState : NSOnState;
+    int state = [sender state] ? NSOffState : NSOnState;
     
     UiEvent event;
     event.intval = state;
@@ -248,9 +247,13 @@
         event.obj = [(UiCocoaWindow*)activeWindow object];
         event.window = event.obj->window;
         event.document = event.obj->ctx->document;
-        // update state in window data
+        // if the sender is a menu item, we have to save the state for this
+        // window
         UiMenuItem *wmi = [(UiCocoaWindow*)activeWindow getMenuItem: sender];
-        wmi->state = state;
+        if(wmi) {
+            // update state in window data
+            wmi->state = state;
+        }
     } else {
         event.window = NULL;
         event.document = NULL;
@@ -258,7 +261,28 @@
     if(callback) {
         callback(&event, data);
     }
-    [item setState: state];
+    [sender setState: state];
+    
+    return true;
+}
+
+- (BOOL)handleToggleEvent:(id)sender {
+    NSWindow *activeWindow = [NSApp keyWindow];
+    
+    UiEvent event;
+    event.intval = [sender state];
+    event.eventdata = NULL;
+    if([activeWindow class] == [UiCocoaWindow class]) {
+        event.obj = [(UiCocoaWindow*)activeWindow object];
+        event.window = event.obj->window;
+        event.document = event.obj->ctx->document;
+    } else {
+        event.window = NULL;
+        event.document = NULL;
+    }
+    if(callback) {
+        callback(&event, data);
+    }
     
     return true;
 }
--- a/ui/gtk/toolbar.c	Mon May 19 21:15:43 2014 +0200
+++ b/ui/gtk/toolbar.c	Tue May 20 13:29:53 2014 +0200
@@ -107,7 +107,7 @@
     ucx_map_cstr_put(toolbar_items, name, item);
 }
 
-void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata, ...) {
+void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata) {
     ui_toolitem_toggle_stgr(name, stockid, f, udata, -1);
 }
 
--- a/ui/ui/toolbar.h	Mon May 19 21:15:43 2014 +0200
+++ b/ui/ui/toolbar.h	Tue May 20 13:29:53 2014 +0200
@@ -42,7 +42,7 @@
 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *udata, ...);
 void ui_toolitem_stgri(char *name, char *stockid, ui_callback f, void *userdata, ...);
 void ui_toolitem_img(char *name, char *label, char *img, ui_callback f, void *udata);
-void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata, ...);
+void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata);
 void ui_toolitem_toggle_stgr(char *name, char *stockid, ui_callback f, void *udata, ...);
 void ui_toolitem_toggle_imggr(char *name, char *label, char *img, ui_callback f, void *udata, ...);
 

mercurial