# HG changeset patch # User Olaf Wintermann # Date 1468416959 -7200 # Node ID f190d03dce0f3e8feb9177a4423ad4da7a382e06 # Parent 3335268a8073a072618403e6c14430fc0d72ab13 implemented simple toolbar items (Cocoa) diff -r 3335268a8073 -r f190d03dce0f application/main.c --- a/application/main.c Wed Jul 13 15:14:09 2016 +0200 +++ b/application/main.c Wed Jul 13 15:35:59 2016 +0200 @@ -49,7 +49,7 @@ } void action_button(UiEvent *event, void *data) { - printf("radio: %d\n", ui_getval(radio)); + //printf("radio: %d\n", ui_getval(radio)); } void action_button2(UiEvent *event, void *data) { @@ -142,11 +142,11 @@ ui_submenu_end(); ui_menuitem("item4", NULL, NULL); - //ui_toolitem("button1", "Test", action_button, NULL); - //ui_toolitem("button2", "OK", action_button, 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("button1"); + ui_toolbar_add_default("button2"); //ui_toolbar_add_default("combo"); UiObject *obj = ui_window("Test", NULL); diff -r 3335268a8073 -r f190d03dce0f ui/cocoa/toolbar.h --- a/ui/cocoa/toolbar.h Wed Jul 13 15:14:09 2016 +0200 +++ b/ui/cocoa/toolbar.h Wed Jul 13 15:35:59 2016 +0200 @@ -67,6 +67,32 @@ @end +/* + * UiToolbarItem + * + * toolbar item with label and icon + */ +@interface UiToolbarItem : NSObject { + char *name; + char *label; + // icon + ui_callback callback; + void *userdata; + UcxList *groups; + BOOL isToggleButton; +} + +- (UiToolbarItem*) initWithIdentifier:(char*)identifier + label:(char*)lbl + callback:(ui_callback)f + userdata:(void*)data; + +- (void) setIsToggleButton:(BOOL)t; + + +@end + + /* * UiToolbarDelegate diff -r 3335268a8073 -r f190d03dce0f ui/cocoa/toolbar.m --- a/ui/cocoa/toolbar.m Wed Jul 13 15:14:09 2016 +0200 +++ b/ui/cocoa/toolbar.m Wed Jul 13 15:35:59 2016 +0200 @@ -122,6 +122,83 @@ @end +/* --------------------- UiToolbarItem --------------------- */ + +@implementation UiToolbarItem + +- (UiToolbarItem*) initWithIdentifier:(char*)identifier + label:(char*)lbl + callback:(ui_callback)f + userdata:(void*)data +{ + name = identifier; + label = lbl; + 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); +} + + +- (NSToolbarItem *) createItem:(NSToolbar*)toolbar + identifier:(NSString*)identifier + object:(UiObject*)obj +{ + NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier: + identifier] autorelease]; + //[item setLabel:[s label]]; + //[item setPaletteLabel:[s label]]; + NSString *l = [[NSString alloc]initWithUTF8String:label]; + [item setLabel:l]; + [item setPaletteLabel:@"Operation"]; + + // create button ... + NSRect frame = NSMakeRect(0, 0, 40, 22); + //NSSearchField *sf = [[NSSearchField alloc]initWithFrame:frame]; + NSButton *button = [[NSButton alloc]initWithFrame:frame]; + //[button setImage:[s buttonImage]]; + //[button setImage:[NSImage imageNamed: NSImageNameAddTemplate]]; + + // TODO: image + [button setImage:[NSImage imageNamed: NSImageNameRemoveTemplate]]; + + [button setBezelStyle: NSTexturedRoundedBezelStyle]; + + // event + EventWrapper *event = [[EventWrapper alloc] + initWithData:userdata callback:callback]; + 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; +} + +- (UcxList*) groups { + return groups; +} + +@end + + /* --------------------- UiToolbarDelegate --------------------- */ @implementation UiToolbarDelegate @@ -215,6 +292,17 @@ toolbar_delegate = [[UiToolbarDelegate alloc]init]; } +void ui_toolitem(char *name, char *label, ui_callback f, void *udata) { + UiToolbarItem *item = [[UiToolbarItem alloc] + initWithIdentifier: name + label: label + callback: f + userdata: udata]; + + NSString *identifier = [[NSString alloc]initWithUTF8String:name]; + [toolbar_delegate addItem: identifier item: item]; +} + void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata) { ui_toolitem_stgr(name, stockid, f, udata, -1); }