forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBigBrowserToolbar.m
321 lines (274 loc) · 12.7 KB
/
BigBrowserToolbar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// BigBrowserToolbar.m Copyright (c) 2002 Joerg Garbers.
// This software is open source. See the license.
#import "BigBrowserToolbar.h"
#import "Block.h"
#import "FSInterpreter.h"
#import "BigBrowserToolbarButton.h"
#import "System.h"
#import "FSNSString.h"
@implementation BigBrowserView (ToolbarController)
static NSMutableArray *customButtons; // Added by PM for f-script 1.2.3
+ (NSArray *)customButtons // Added by PM for f-script 1.2.3
{
return customButtons;
}
/*+(void) initialize // Added by PM for f-script 1.2.3
{
static BOOL tooLate = NO;
if ( !tooLate )
{
int i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
customButtons = [[NSMutableArray alloc] init];
for (i = 1; i < 11; i++)
{
BigBrowserToolbarButton *button = [[BigBrowserToolbarButton alloc] initWithFrame:NSMakeRect(0,0,93,30)];
NSString *buttonName = [defaults stringForKey:[NSString stringWithFormat:@"BigBrowserToolbarButtonCustom%dName",i]];
NSData *blockData = [defaults objectForKey:[NSString stringWithFormat:@"BigBrowserToolbarButtonCustom%dBlock",i]];
Block *block = nil;
block = [NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:[NSString stringWithFormat:@"BigBrowserToolbarButtonCustom%dBlock",i]]];
if (blockData)
{
//NS_DURING
block = [NSUnarchiver unarchiveObjectWithData:blockData];
//NS_HANDLER
// NSLog([NSString stringWithFormat:@"Syntax error found while loading a block for an F-Script object browser custom button: %@", [localException reason]]);
// blockSource = nil; // We will fall back to the default block template
//NS_ENDHANDLER
}
if (!buttonName) buttonName = [NSString stringWithFormat:@"Custom%d", i];
if (!block)
{
NSString *blockSource = @"[:selectedObject| selectedObject \"Define your custom block here.\"]";
block = [blockSource asBlock];
}
[button setIdentifier:[NSString stringWithFormat:@"Custom%d", i]];
[button setName:buttonName];
[button setBlock:block];
[button setAction:@selector(applyBlockAction:)];
[customButtons addObject:button];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCustomButtonsSettings:) name:NSApplicationWillTerminateNotification object:nil];
}
}*/
+ (void)saveCustomButtonsSettings // Added by PM for F-Scrip 1.2.3
{
NSInteger i;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (i = 0; i < 10; i++)
{
#warning 64BIT: Check formatting arguments
[defaults setObject:[[customButtons objectAtIndex:i] name] forKey:[NSString stringWithFormat:@"BigBrowserToolbarButtonCustom%dName",i+1] inDomain:@"NSGlobalDomain"]; // This is an undocumented Cocoa API in Mac OS X 10.1
//[defaults setObject:[NSArchiver archivedDataWithRootObject:[[customButtons objectAtIndex:i] block]] forKey:[NSString stringWithFormat:@"BigBrowserToolbarButtonCustom%dBlock",i+1] inDomain:@"NSGlobalDomain"]; // This is an undocumented Cocoa API in Mac OS X 10.1
}
[defaults synchronize];
}
+ (void)saveCustomButtonsSettings:(NSNotification *)aNotification // Added by PM for F-Scrip 1.2.3
{
[self saveCustomButtonsSettings];
}
- (BOOL)doSetupToolbar;
{
static BOOL yn=YES;
return yn;
}
- (BOOL)doSetupOldToolbar;
{
static BOOL yn=NO;
return yn;
}
- (void) setupToolbarWithWindow:(NSWindow *)window;
{
// Create a new toolbar instance, and attach it to our document window
NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier: @"BigBrowserToolbar"] autorelease];
// Set up toolbar properties: Allow customization, give a default display mode, and remember state in user defaults
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
[toolbar setDisplayMode:NSToolbarDisplayModeIconOnly]; // NSToolbarDisplayModeIconAndLabel
// We are the delegate
[toolbar setDelegate: self];
// Attach the toolbar to the document window
[window setToolbar: toolbar];
}
- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted // Modified by PM for f-script 1.2.3
{
// Required delegate method Given an item identifier, self method returns an item
// The toolbar will use self method to obtain toolbar items that can be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem;
if (![[toolbar identifier] isEqualToString:@"BigBrowserToolbar"]) return nil;
if ([itemIdent hasPrefix:@"Custom"])
{
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
NSMenuItem *menuFormRep;
BigBrowserToolbarButton *button;
NSInteger i = 0;
while (![[[customButtons objectAtIndex:i] identifier] isEqualToString:itemIdent])
i++;
button = [[[customButtons objectAtIndex:i] copy] autorelease];
[button setTarget:self];
[toolbarItem setView:button];
[button setToolbarItem:toolbarItem];
menuFormRep = [[[NSMenuItem alloc] init] autorelease];
[menuFormRep setAction:@selector(selectedFromMenuItem:)];
[menuFormRep setTarget:button];
[menuFormRep setTitle:[button name]];
[toolbarItem setMenuFormRepresentation: menuFormRep];
[toolbarItem setTarget:self];
[toolbarItem setLabel:[button name]];
[toolbarItem setPaletteLabel:[button name]];
[toolbarItem setMinSize:[[button cell] cellSize].width < 93 ? NSMakeSize(93, [button frame].size.height) : [[button cell] cellSize]];
[toolbarItem setMaxSize:([button frame].size.width < 93 ? NSMakeSize(93, [button frame].size.height) : [[button cell] cellSize])];
//[toolbarItem setMinSize:[button frame].size];
//[toolbarItem setMaxSize:[button frame].size];
}
else
{
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
NSString *buttonTitle=nil;
NSString *toolTip=nil;
SEL action=nil;
// defaults
buttonTitle=itemIdent;
if ([itemIdent isEqual: @"Classes"]) {
action=@selector(classesAction:);
toolTip=@"Browse loaded classes";
}
if ([itemIdent isEqual: @"Workspace"]) {
action=@selector(workspaceAction:);
toolTip=@"Browse workspace";
}
if ([itemIdent isEqual: @"Update"]) {
action=@selector(updateAction:);
toolTip=@"Update the diplay of objects";
}
if ([itemIdent isEqual: @"Name"]) {
action=@selector(nameObjectAction:);
toolTip=@"Assign the selected object to an identifier in the F-Script environment";
}
if ([itemIdent isEqual: @"Self"]) {
action=@selector(selfAction:);
toolTip=@"Send the \"self\" message to the selected object";
}
if ([itemIdent isEqual: @"Inspect"]) {
action=@selector(inspectObjectAction:);
toolTip=@"Inspect the selected object";
}
if ([itemIdent isEqual: @"Key-value"]) {
action=@selector(browseKVAction:);
toolTip=@"Browse the selected object relations";
}
if ([itemIdent isEqual: @"Select view"]){
action=@selector(selectViewAction:);
toolTip=@"Lets you select a view with the mouse and browse it";
}
// if it is a button of the above, action is set to non nil.
if (action)
{
NSMenuItem *menuFormRep = nil;
NSButton *button = [[[NSButton alloc] initWithFrame:NSMakeRect(0,0,93,30)] autorelease];
[button setBezelStyle:NSRoundedBezelStyle];
[button setTitle:buttonTitle];
[button setAction:action];
[button setTarget:self];
[button setToolTip:toolTip];
[toolbarItem setLabel: buttonTitle];
[toolbarItem setPaletteLabel: buttonTitle];
[toolbarItem setLabel: buttonTitle];
[toolbarItem setPaletteLabel: buttonTitle];
[toolbarItem setTarget:self];
[toolbarItem setView:button];
[toolbarItem setMinSize:[[button cell] cellSize].width < 93 ? NSMakeSize(93, [button frame].size.height) : [[button cell] cellSize]];
[toolbarItem setMaxSize:([button frame].size.width < 93 ? NSMakeSize(93, [button frame].size.height) : [[button cell] cellSize])];
//[toolbarItem setMinSize:[button frame].size];
//[toolbarItem setMaxSize:[button frame].size];
[toolbarItem setToolTip:toolTip];
menuFormRep = [[[NSMenuItem alloc] init] autorelease];
[menuFormRep setTitle: buttonTitle];
[menuFormRep setAction:action];
[menuFormRep setTarget:[toolbarItem target]];
[toolbarItem setMenuFormRepresentation: menuFormRep];
} /*else if([itemIdent isEqual: @"ApplyBlock"]) {
NSMenu *submenu = [[self class] customBlockMenu]; // always the same instance (allows us to add to submenu in one place only)
NSMenuItem *menuFormRep = nil;
NSRect r=NSMakeRect(0.0,0.0,150.0,30.0);
NSPopUpButton *popUpButton=[[[NSPopUpButton alloc] initWithFrame:r pullsDown:NO] autorelease];
[popUpButton setMenu:submenu];
[popUpButton setTitle:itemIdent];
[toolbarItem setToolTip: @"Run Selection with User defined Block."];
[toolbarItem setView:popUpButton];
[toolbarItem setMinSize:NSMakeSize(150,NSHeight([popUpButton frame]))];
[toolbarItem setMaxSize:NSMakeSize(250,NSHeight([popUpButton frame]))];
// By default, in text only mode, a custom items label will be shown as disabled text, but you can provide a
// custom menu of your own by using <item> setMenuFormRepresentation]
menuFormRep = [[[NSMenuItem alloc] init] autorelease];
[menuFormRep setSubmenu: submenu];
[menuFormRep setTitle: [toolbarItem label]];
[toolbarItem setMenuFormRepresentation: menuFormRep];
}*/ else {
// itemIdent refered to a toolbar item that is not provide or supported by us or cocoa
// Returning nil will inform the toolbar self kind of item is not supported
toolbarItem = nil;
}
}
return toolbarItem;
}
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar;
{
// Required delegate method Returns the ordered list of items to be shown in the toolbar by default
// If during the toolbar's initialization, no overriding values are found in the user defaults, or if the
// user chooses to revert to the default items self set will be used
return [NSArray arrayWithObjects: @"Workspace", @"Classes", @"Select view", @"Name", @"Self", @"Inspect", nil];
}
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar;
{
// Required delegate method Returns the list of all allowed items by identifier By default, the toolbar
// does not assume any items are allowed, even the separator So, every allowed item must be explicitly listed
// The set of allowed items is used to construct the customization palette
return [NSArray arrayWithObjects: @"Workspace",@"Classes",@"Select view",@"Name",@"Self",@"Inspect",
@"Update",@"Key-value", @"Custom1", @"Custom2", @"Custom3", @"Custom4", @"Custom5", @"Custom6", @"Custom7", @"Custom8", @"Custom9", @"Custom10", NSToolbarCustomizeToolbarItemIdentifier,
NSToolbarFlexibleSpaceItemIdentifier, NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];
/* return [NSArray arrayWithObjects: @"Classes",@"Workspace",@"Name",@"Self",@"Inspect",
@"Update",@"Key-value", NSToolbarCustomizeToolbarItemIdentifier,
NSToolbarFlexibleSpaceItemIdentifier, NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];*/
}
+ (NSMenu *) customBlockMenu;
{
static NSMenu *menu=nil;
if (!menu) {
menu=[[NSMenu alloc] initWithTitle:@"ApplyBlock"];
}
return menu;
}
+ (void) addCustomBlockMenuIdentifier:(NSString *)blockIdentifier;
{
NSMenu *menu=[self customBlockMenu];
NSMenuItem *item=[[NSMenuItem alloc] initWithTitle:blockIdentifier action:@selector(customBlockMenuAction:) keyEquivalent:@""];
[menu addItem:item];
}
+ (void) removeCustomBlockMenuIdentifier:(NSString *)blockIdentifier;
{
NSMenu *menu=[self customBlockMenu];
NSInteger idx=[menu indexOfItemWithTitle:blockIdentifier];
if (idx!=NSNotFound)
[menu removeItemAtIndex:idx];
else
#warning 64BIT: Check formatting arguments
NSLog(@"Warning: removeCustomBlockMenuIdentifier called with invalid blockIdentifier %@",blockIdentifier);
}
- (void) customBlockMenuAction:(id)sender;
{
BOOL found;
NSString *title=[sender title];
id selectedObject = [self selectedObject];
id block=[interpreter objectForIdentifier:title found:&found];
if (!found) {
#warning 64BIT: Check formatting arguments
NSInteger choice=NSRunAlertPanel(@"Undefined Block", [NSString stringWithFormat:@"Could not find Block with name %@", title], @"Cancel", @"Remove Menu Entry", nil);
if (choice) {
[[self class] removeCustomBlockMenuIdentifier:title];
}
} else {
[block value:selectedObject];
}
}
@end