diff -r 05a18c56d9ca -r 431dde3c5fbe ui/cocoa/toolbar.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/cocoa/toolbar.m Tue Mar 25 20:25:09 2014 +0100 @@ -0,0 +1,188 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2014 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#import +#import +#import + +#import "toolbar.h" + + +static UiToolbarDelegate* toolbar_delegate; + +/* --------------------- UiToolbarStockItem --------------------- */ + +@implementation UiToolbarStockItem + +- (UiToolbarStockItem*) initWithIdentifier:(char*)identifier + stockID:(char*)sid + callback:(ui_callback)f + userdata:(void*)data +{ + name = identifier; + stockid = sid; + callback = f; + userdata = data; + return self; +} + + +- (NSToolbarItem *) createItem:(NSToolbar*)toolbar + identifier:(NSString*)identifier +{ + /* + UiStockItem *s = ui_get_stock_item(stockid); + if(s == nil) { + printf("cannot find stock item\n"); + return nil; + } + if([s buttonImage] == nil && [s itemImage] == nil) { + return nil; + } + */ + + NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier: + identifier] autorelease]; + //[item setLabel:[s label]]; + //[item setPaletteLabel:[s label]]; + [item setLabel:@"Add"]; + [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]]; + [button setBezelStyle: NSTexturedRoundedBezelStyle]; + [item setView:button]; + + // event + EventWrapper *event = [[EventWrapper alloc] + initWithData:userdata callback:callback]; + [button setAction:@selector(handleEvent:)]; + [button setTarget:event]; + + return item; +} + +@end + + +/* --------------------- UiToolbarDelegate --------------------- */ + +@implementation UiToolbarDelegate + +- (UiToolbarDelegate*) init { + allowedItems = [[NSMutableArray alloc]initWithCapacity: 16]; + defaultItems = [[NSMutableArray alloc]initWithCapacity: 16]; + items = [[NSMutableDictionary alloc] init]; + return self; +} + +- (void) addDefault:(NSString*)identifier { + [defaultItems addObject: identifier]; +} + +- (void) addItem: (NSString*) identifier + item: (NSObject*) item +{ + [allowedItems addObject: identifier]; + [items setObject: item forKey:identifier]; +} + +/* +- (void) addStockItem:(char*)name + stockID:(char*)sid + callback:(ui_callback)f + data:(void*)userdata +{ + UiToolbarStockItem *item = [[UiToolbarStockItem alloc]initWithData:name + stockID:sid callback:f data:userdata]; + + NSString *s = [[NSString alloc]initWithUTF8String:name]; + [allowedItems addObject: s]; + [items setObject: item forKey:s]; +} +*/ + +// implementation of NSToolbarDelegate methods +- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar { + NSMutableArray *i = [[NSMutableArray alloc] + initWithCapacity:[allowedItems count] + 3]; + [i addObject: NSToolbarFlexibleSpaceItemIdentifier]; + [i addObject: NSToolbarSpaceItemIdentifier]; + [i addObject: NSToolbarSeparatorItemIdentifier]; + + return i; +} + +- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar { + return defaultItems; +} + +- (NSToolbarItem *) toolbar:(NSToolbar*)toolbar + itemForItemIdentifier:(NSString*)identifier + willBeInsertedIntoToolbar:(BOOL)flag +{ + Protocol *item = @protocol(UiToolItem); + item = [items objectForKey: identifier]; + return [item createItem:toolbar identifier:identifier]; +} + +@end + + +/* --------------------- functions --------------------- */ + +void ui_toolbar_init() { + toolbar_delegate = [[UiToolbarDelegate alloc]init]; +} + +void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata) { + UiToolbarStockItem *item = [[UiToolbarStockItem alloc] + initWithIdentifier: name + stockID: stockid + callback: f + userdata: udata]; + NSString *identifier = [[NSString alloc]initWithUTF8String:name]; + [toolbar_delegate addItem: identifier item: item]; +} + +void ui_toolbar_add_default(char *name) { + NSString *identifier = [[NSString alloc]initWithUTF8String:name]; + [toolbar_delegate addDefault: identifier]; +} + +NSToolbar* ui_create_toolbar() { + NSToolbar *toolbar = [[NSToolbar alloc]initWithIdentifier: @"MainToolbar"]; + [toolbar setDelegate: toolbar_delegate]; + [toolbar setAllowsUserCustomization: true]; + return toolbar; +} +