UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2014 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #import <stdio.h> 30 #import <stdlib.h> 31 #import <string.h> 32 #import <inttypes.h> 33 #import <stdarg.h> 34 35 #import "toolbar.h" 36 #import "window.h" 37 #import "stock.h" 38 39 40 static UiToolbarDelegate* toolbar_delegate; 41 42 /* --------------------- UiToolbarStockItem --------------------- */ 43 44 @implementation UiToolbarStockItem 45 46 - (UiToolbarStockItem*) initWithIdentifier:(char*)identifier 47 stockID:(char*)sid 48 callback:(ui_callback)f 49 userdata:(void*)data 50 { 51 name = identifier; 52 stockid = sid; 53 callback = f; 54 userdata = data; 55 groups = NULL; 56 isToggleButton = NO; 57 return self; 58 } 59 60 - (void) setIsToggleButton:(BOOL)t { 61 isToggleButton = t; 62 } 63 64 - (void) addGroup:(int)group { 65 groups = ucx_list_append(groups, (void*)(intptr_t)group); 66 } 67 68 69 - (NSToolbarItem *) createItem:(NSToolbar*)toolbar 70 identifier:(NSString*)identifier 71 object:(UiObject*)obj 72 { 73 UiStockItem *s = ui_get_stock_item(stockid); 74 if(s == nil) { 75 printf("cannot find stock item\n"); 76 return nil; 77 } 78 79 NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier: 80 identifier] autorelease]; 81 //[item setLabel:[s label]]; 82 //[item setPaletteLabel:[s label]]; 83 [item setLabel:s->label]; 84 [item setPaletteLabel:@"Operation"]; 85 86 // create button ... 87 NSRect frame = NSMakeRect(0, 0, 40, 22); 88 //NSSearchField *sf = [[NSSearchField alloc]initWithFrame:frame]; 89 NSButton *button = [[NSButton alloc]initWithFrame:frame]; 90 //[button setImage:[s buttonImage]]; 91 //[button setImage:[NSImage imageNamed: NSImageNameAddTemplate]]; 92 if(s->image) { 93 [button setImage:s->image]; 94 } else { 95 [button setImage:[NSImage imageNamed: NSImageNameRemoveTemplate]]; 96 } 97 [button setBezelStyle: NSTexturedRoundedBezelStyle]; 98 99 // event 100 EventWrapper *event = [[EventWrapper alloc] 101 initWithData:userdata callback:callback]; 102 if(isToggleButton) { 103 [button setButtonType: NSPushOnPushOffButton]; 104 [button setAction:@selector(handleToggleEvent:)]; 105 } else { 106 [button setAction:@selector(handleEvent:)]; 107 } 108 [button setTarget:event]; 109 110 if(groups) { 111 uic_add_group_widget(obj->ctx, item, groups); 112 } 113 114 [item setView:button]; 115 return item; 116 } 117 118 - (UcxList*) groups { 119 return groups; 120 } 121 122 @end 123 124 125 /* --------------------- UiToolbarItem --------------------- */ 126 127 @implementation UiToolbarItem 128 129 - (UiToolbarItem*) initWithIdentifier:(char*)identifier 130 label:(char*)lbl 131 callback:(ui_callback)f 132 userdata:(void*)data 133 { 134 name = identifier; 135 label = lbl; 136 callback = f; 137 userdata = data; 138 groups = NULL; 139 isToggleButton = NO; 140 return self; 141 } 142 143 - (void) setIsToggleButton:(BOOL)t { 144 isToggleButton = t; 145 } 146 147 - (void) addGroup:(int)group { 148 groups = ucx_list_append(groups, (void*)(intptr_t)group); 149 } 150 151 152 - (NSToolbarItem *) createItem:(NSToolbar*)toolbar 153 identifier:(NSString*)identifier 154 object:(UiObject*)obj 155 { 156 NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier: 157 identifier] autorelease]; 158 //[item setLabel:[s label]]; 159 //[item setPaletteLabel:[s label]]; 160 NSString *l = [[NSString alloc]initWithUTF8String:label]; 161 [item setLabel:l]; 162 [item setPaletteLabel:@"Operation"]; 163 164 // create button ... 165 NSRect frame = NSMakeRect(0, 0, 40, 22); 166 //NSSearchField *sf = [[NSSearchField alloc]initWithFrame:frame]; 167 NSButton *button = [[NSButton alloc]initWithFrame:frame]; 168 //[button setImage:[s buttonImage]]; 169 //[button setImage:[NSImage imageNamed: NSImageNameAddTemplate]]; 170 171 // TODO: image 172 [button setImage:[NSImage imageNamed: NSImageNameRemoveTemplate]]; 173 174 [button setBezelStyle: NSTexturedRoundedBezelStyle]; 175 176 // event 177 EventWrapper *event = [[EventWrapper alloc] 178 initWithData:userdata callback:callback]; 179 if(isToggleButton) { 180 [button setButtonType: NSPushOnPushOffButton]; 181 [button setAction:@selector(handleToggleEvent:)]; 182 } else { 183 [button setAction:@selector(handleEvent:)]; 184 } 185 [button setTarget:event]; 186 187 if(groups) { 188 uic_add_group_widget(obj->ctx, item, groups); 189 } 190 191 [item setView:button]; 192 return item; 193 } 194 195 - (UcxList*) groups { 196 return groups; 197 } 198 199 @end 200 201 202 /* --------------------- UiToolbarDelegate --------------------- */ 203 204 @implementation UiToolbarDelegate 205 206 - (UiToolbarDelegate*) init { 207 allowedItems = [[NSMutableArray alloc]initWithCapacity: 16]; 208 defaultItems = [[NSMutableArray alloc]initWithCapacity: 16]; 209 items = [[NSMutableDictionary alloc] init]; 210 return self; 211 } 212 213 - (void) addDefault:(NSString*)identifier { 214 [defaultItems addObject: identifier]; 215 } 216 217 - (void) addItem: (NSString*) identifier 218 item: (NSObject<UiToolItem>*) item 219 { 220 [allowedItems addObject: identifier]; 221 [items setObject: item forKey:identifier]; 222 } 223 224 /* 225 - (void) addStockItem:(char*)name 226 stockID:(char*)sid 227 callback:(ui_callback)f 228 data:(void*)userdata 229 { 230 UiToolbarStockItem *item = [[UiToolbarStockItem alloc]initWithData:name 231 stockID:sid callback:f data:userdata]; 232 233 NSString *s = [[NSString alloc]initWithUTF8String:name]; 234 [allowedItems addObject: s]; 235 [items setObject: item forKey:s]; 236 } 237 */ 238 239 // implementation of NSToolbarDelegate methods 240 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar { 241 NSMutableArray *i = [[NSMutableArray alloc] 242 initWithCapacity:[allowedItems count] + 3]; 243 [i addObject: NSToolbarFlexibleSpaceItemIdentifier]; 244 [i addObject: NSToolbarSpaceItemIdentifier]; 245 [i addObject: NSToolbarSeparatorItemIdentifier]; 246 for(id item in allowedItems) { 247 [i addObject: item]; 248 } 249 250 return i; 251 } 252 253 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar { 254 return defaultItems; 255 } 256 257 - (NSToolbarItem *) toolbar:(NSToolbar*)toolbar 258 itemForItemIdentifier:(NSString*)identifier 259 willBeInsertedIntoToolbar:(BOOL)flag 260 { 261 Protocol *item = @protocol(UiToolItem); 262 item = [items objectForKey: identifier]; 263 264 // get UiObject from toolbar 265 UiObject *obj = [(UiToolbar*)toolbar object]; 266 267 // create new NSToolbarItem 268 return [item createItem:toolbar identifier:identifier object:obj]; 269 } 270 271 @end 272 273 274 @implementation UiToolbar 275 276 - (UiToolbar*) initWithObject:(UiObject*)object { 277 [self initWithIdentifier: @"MainToolbar"]; 278 obj = object; 279 return self; 280 } 281 282 - (UiObject*) object { 283 return obj; 284 } 285 286 @end 287 288 289 /* --------------------- functions --------------------- */ 290 291 void ui_toolbar_init() { 292 toolbar_delegate = [[UiToolbarDelegate alloc]init]; 293 } 294 295 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) { 296 UiToolbarItem *item = [[UiToolbarItem alloc] 297 initWithIdentifier: name 298 label: label 299 callback: f 300 userdata: udata]; 301 302 NSString *identifier = [[NSString alloc]initWithUTF8String:name]; 303 [toolbar_delegate addItem: identifier item: item]; 304 } 305 306 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata) { 307 ui_toolitem_stgr(name, stockid, f, udata, -1); 308 } 309 310 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) { 311 va_list ap; 312 va_start(ap, udata); 313 ui_toolbar_stock_button(name, stockid, NO, f, udata, ap); 314 va_end(ap); 315 } 316 317 void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata) { 318 ui_toolitem_toggle_stgr(name, stockid, f, udata, -1); 319 } 320 321 void ui_toolitem_toggle_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) { 322 va_list ap; 323 va_start(ap, udata); 324 ui_toolbar_stock_button(name, stockid, YES, f, udata, ap); 325 va_end(ap); 326 } 327 328 329 void ui_toolbar_stock_button(char *name, char *stockid, BOOL toggle, ui_callback f, void *udata, va_list ap) { 330 UiToolbarStockItem *item = [[UiToolbarStockItem alloc] 331 initWithIdentifier: name 332 stockID: stockid 333 callback: f 334 userdata: udata]; 335 [item setIsToggleButton: toggle]; 336 NSString *identifier = [[NSString alloc]initWithUTF8String:name]; 337 [toolbar_delegate addItem: identifier item: item]; 338 339 // add groups 340 int group; 341 while((group = va_arg(ap, int)) != -1) { 342 [item addGroup: group]; 343 } 344 } 345 346 347 void ui_toolbar_add_default(char *name) { 348 NSString *identifier = [[NSString alloc]initWithUTF8String:name]; 349 [toolbar_delegate addDefault: identifier]; 350 } 351 352 NSToolbar* ui_create_toolbar(UiObject *obj) { 353 UiToolbar *toolbar = [[UiToolbar alloc] initWithObject:obj]; 354 [toolbar setDelegate: toolbar_delegate]; 355 [toolbar setAllowsUserCustomization: true]; 356 return toolbar; 357 } 358 359