ui/cocoa/toolbar.m

changeset 7
431dde3c5fbe
child 14
e2fd132ab781
equal deleted inserted replaced
6:05a18c56d9ca 7:431dde3c5fbe
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
33 #import "toolbar.h"
34
35
36 static UiToolbarDelegate* toolbar_delegate;
37
38 /* --------------------- UiToolbarStockItem --------------------- */
39
40 @implementation UiToolbarStockItem
41
42 - (UiToolbarStockItem*) initWithIdentifier:(char*)identifier
43 stockID:(char*)sid
44 callback:(ui_callback)f
45 userdata:(void*)data
46 {
47 name = identifier;
48 stockid = sid;
49 callback = f;
50 userdata = data;
51 return self;
52 }
53
54
55 - (NSToolbarItem *) createItem:(NSToolbar*)toolbar
56 identifier:(NSString*)identifier
57 {
58 /*
59 UiStockItem *s = ui_get_stock_item(stockid);
60 if(s == nil) {
61 printf("cannot find stock item\n");
62 return nil;
63 }
64 if([s buttonImage] == nil && [s itemImage] == nil) {
65 return nil;
66 }
67 */
68
69 NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:
70 identifier] autorelease];
71 //[item setLabel:[s label]];
72 //[item setPaletteLabel:[s label]];
73 [item setLabel:@"Add"];
74 [item setPaletteLabel:@"Operation"];
75
76 // create button ...
77 NSRect frame = NSMakeRect(0, 0, 40, 22);
78 //NSSearchField *sf = [[NSSearchField alloc]initWithFrame:frame];
79 NSButton *button = [[NSButton alloc]initWithFrame:frame];
80 //[button setImage:[s buttonImage]];
81 [button setImage:[NSImage imageNamed: NSImageNameAddTemplate]];
82 [button setBezelStyle: NSTexturedRoundedBezelStyle];
83 [item setView:button];
84
85 // event
86 EventWrapper *event = [[EventWrapper alloc]
87 initWithData:userdata callback:callback];
88 [button setAction:@selector(handleEvent:)];
89 [button setTarget:event];
90
91 return item;
92 }
93
94 @end
95
96
97 /* --------------------- UiToolbarDelegate --------------------- */
98
99 @implementation UiToolbarDelegate
100
101 - (UiToolbarDelegate*) init {
102 allowedItems = [[NSMutableArray alloc]initWithCapacity: 16];
103 defaultItems = [[NSMutableArray alloc]initWithCapacity: 16];
104 items = [[NSMutableDictionary alloc] init];
105 return self;
106 }
107
108 - (void) addDefault:(NSString*)identifier {
109 [defaultItems addObject: identifier];
110 }
111
112 - (void) addItem: (NSString*) identifier
113 item: (NSObject<UiToolItem>*) item
114 {
115 [allowedItems addObject: identifier];
116 [items setObject: item forKey:identifier];
117 }
118
119 /*
120 - (void) addStockItem:(char*)name
121 stockID:(char*)sid
122 callback:(ui_callback)f
123 data:(void*)userdata
124 {
125 UiToolbarStockItem *item = [[UiToolbarStockItem alloc]initWithData:name
126 stockID:sid callback:f data:userdata];
127
128 NSString *s = [[NSString alloc]initWithUTF8String:name];
129 [allowedItems addObject: s];
130 [items setObject: item forKey:s];
131 }
132 */
133
134 // implementation of NSToolbarDelegate methods
135 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
136 NSMutableArray *i = [[NSMutableArray alloc]
137 initWithCapacity:[allowedItems count] + 3];
138 [i addObject: NSToolbarFlexibleSpaceItemIdentifier];
139 [i addObject: NSToolbarSpaceItemIdentifier];
140 [i addObject: NSToolbarSeparatorItemIdentifier];
141
142 return i;
143 }
144
145 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
146 return defaultItems;
147 }
148
149 - (NSToolbarItem *) toolbar:(NSToolbar*)toolbar
150 itemForItemIdentifier:(NSString*)identifier
151 willBeInsertedIntoToolbar:(BOOL)flag
152 {
153 Protocol *item = @protocol(UiToolItem);
154 item = [items objectForKey: identifier];
155 return [item createItem:toolbar identifier:identifier];
156 }
157
158 @end
159
160
161 /* --------------------- functions --------------------- */
162
163 void ui_toolbar_init() {
164 toolbar_delegate = [[UiToolbarDelegate alloc]init];
165 }
166
167 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata) {
168 UiToolbarStockItem *item = [[UiToolbarStockItem alloc]
169 initWithIdentifier: name
170 stockID: stockid
171 callback: f
172 userdata: udata];
173 NSString *identifier = [[NSString alloc]initWithUTF8String:name];
174 [toolbar_delegate addItem: identifier item: item];
175 }
176
177 void ui_toolbar_add_default(char *name) {
178 NSString *identifier = [[NSString alloc]initWithUTF8String:name];
179 [toolbar_delegate addDefault: identifier];
180 }
181
182 NSToolbar* ui_create_toolbar() {
183 NSToolbar *toolbar = [[NSToolbar alloc]initWithIdentifier: @"MainToolbar"];
184 [toolbar setDelegate: toolbar_delegate];
185 [toolbar setAllowsUserCustomization: true];
186 return toolbar;
187 }
188

mercurial