forked from Kentzo/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigBrowserView.m
More file actions
483 lines (401 loc) · 17.1 KB
/
BigBrowserView.m
File metadata and controls
483 lines (401 loc) · 17.1 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// BigBrowserView.m Copyright 2000 Philippe Mougin.
// This software is Open Source. See the license.
#import "build_config.h"
#ifdef BUILD_WITH_APPKIT
//-----------------------------------------------------------------------
// BigBowserView is compiled only if we are using AppKit
#import "BigBrowserView.h"
#import <objc/objc-class.h>
#import "Compiler.h"
#import "ExecEngine.h"
#import "NSBrowserBugFix.h"
#import "FSInterpreter.h"
@implementation BigBrowserView
- (void) dealloc
{
//NSLog(@"BigBrowserView dealloc");
[rootObject release];
[interpreter release];
[argumentsWindow release];
[b1 release];
[b2 release];
[super dealloc];
}
- (void)disable { isDisabled = YES; [[[self subviews] objectAtIndex:0] replacementForBuggySetEnabled:NO]; }
- (void)enable { isDisabled = NO; [[[self subviews] objectAtIndex:0] replacementForBuggySetEnabled:YES];}
- (id)initWithFrame:(NSRect)frameRect
{
if ([super initWithFrame:frameRect])
{
CGFloat baseWidth = NSWidth([self bounds]);
CGFloat baseHeight = NSHeight([self bounds]);
twoLevelsEnabled = YES;
b1 = [[NSBrowser alloc] initWithFrame:NSMakeRect(0,baseHeight/2,baseWidth,baseHeight/2)];
b2 = [[NSBrowser alloc] initWithFrame:NSMakeRect(0,0,baseWidth,baseHeight/2)];
[b1 setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable | NSViewMinYMargin];
[b2 setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable | NSViewMaxYMargin];
[self addSubview:b1];
[self addSubview:b2];
[b1 setHasHorizontalScroller:YES];
[b1 setMinColumnWidth:130];
// [b setAcceptsArrowKeys:YES];
[b1 setDelegate:self];
[b2 setHasHorizontalScroller:YES];
[b2 setMinColumnWidth:130];
// [b setAcceptsArrowKeys:YES];
[b2 setDelegate:self];
isDisabled = NO;
return self;
}
return nil;
}
- (void)fillMatrix:(NSMatrix *)matrix withMethodsForObject:(id)object
{
Class cls = [object class];
while (cls)
{
void *iterator = 0;
struct objc_method_list *mlist;
NSInteger i;
NSBrowserCell *cell;
NSColor *txtColor = [NSColor blueColor];
NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName, nil];
NSMutableAttributedString *attrStr = [[[NSMutableAttributedString alloc] initWithString:[cls description] attributes:txtDict] autorelease];
[attrStr setAlignment:NSCenterTextAlignment range:NSMakeRange(0,[attrStr length])];
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setLeaf:YES];
[cell setEnabled:NO];
[cell setAttributedStringValue:attrStr];
while ( mlist = class_nextMethodList( cls, &iterator ) )
{
for (i = 0; i < mlist->method_count; i++)
{
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setStringValue:[Compiler stringFromSelector:mlist->method_list[i].method_name]];
}
}
// add a blank line
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setLeaf:YES];
[cell setEnabled:NO];
cls = [cls superclass];
}
}
- (void)fillMatrix:(NSMatrix *)matrix withObject:(id)object
{
NSBrowserCell *cell;
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setRepresentedObject:object];
if (object == nil)
{
[cell setStringValue:@"nil"];
[cell setLeaf:YES];
}
else
{
NSString *description = [object description];
if ([description length] > 410) description = [description substringWithRange:NSMakeRange(0,400)];
[cell setStringValue:description];
}
if ([object respondsToSelector:@selector(objectEnumerator)])
{
NSEnumerator *enumerator = [object objectEnumerator];
id elem;
// add a blank line
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setLeaf:YES];
[cell setEnabled:NO];
while ((elem = [enumerator nextObject]))
{
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setRepresentedObject:elem];
if (elem == nil)
{
[cell setStringValue:@"nil"];
[cell setLeaf:YES];
}
else
{
NSString *description = [elem description];
if ([description length] > 410) description = [description substringWithRange:NSMakeRange(0,400)];
[cell setStringValue:description];
}
}
}
}
- (void) sendMessageTo:(id)receiver selectorString:(NSString *)selectorStr arguments:(NSArray *)arguments fillThisMatrixWithResult:(NSMatrix *)matrix
{
NSInteger nbarg = [arguments count];
id args[nbarg+2];
SEL selector = [Compiler selectorFromString:selectorStr];
NSInteger i;
id result;
NSString *errorString = nil;
args[0] = receiver;
args[1] = (id)selector;
for (i = 0; i < nbarg; i++) args[i+2] = [arguments objectAtIndex:i];
NS_DURING
result = sendMsgNoPattern(receiver, selector, selectorStr, nbarg+2, args,[MsgContext msgContext]);
NS_HANDLER
// errorString = [NSString stringWithFormat:@"%@ : %@",[localException name], [localException reason]];
errorString = [localException reason];
NS_ENDHANDLER
if (errorString) NSRunAlertPanel(@"ERROR", errorString, @"Ok", nil, nil,nil);
else [self fillMatrix:matrix withObject:result];
}
- (void)browser:(NSBrowser *)sender createRowsForColumn:(NSInteger)column inMatrix:(NSMatrix *)matrix // We are our own delegate !
{
if (twoLevelsEnabled)
{
NSInteger i, nb;
NSMatrix *m;
if (sender == b1)
{
if (column == 0)
{
#warning 64BIT: Check formatting arguments
NSLog(@"b1 column 0");
[self fillMatrix:matrix withObject:rootObject];
}
else
{
[b2 setLastColumn:column-1];
for (i = 0, m = [b2 matrixInColumn:column-1], nb = [m numberOfRows]; i < nb; i++)
[m removeRow:0];
[self fillMatrix:m withMethodsForObject:[[b1 selectedCell] representedObject]];
[b2 tile];
}
}
else // sender == b2
{
if (column != 0)
{
NSString *selectedString = [[b2 selectedCell] stringValue];
SEL selector = [Compiler selectorFromString:selectedString];
NSArray *selectorComponents = [NSStringFromSelector(selector) componentsSeparatedByString:@":"];
NSInteger nbarg = [selectorComponents count]-1;
id selectedObject = [[b1 selectedCellInColumn:column-1] representedObject];
[b1 setLastColumn:column];
for (i = 0, m = [b1 matrixInColumn:column], nb = [m numberOfRows]; i < nb; i++)
[m removeRow:0];
if (nbarg == 0)
{
[self sendMessageTo:selectedObject selectorString:selectedString arguments:[NSArray array] fillThisMatrixWithResult:[b1 matrixInColumn:column]];
[b1 tile];
}
else
{
NSInteger i;
NSInteger baseWidth = 300;
NSInteger baseHeight = nbarg*30+55;
NSButton *button;
NSForm *f;
argumentsWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(100,100,baseWidth,baseHeight) styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask backing:NSBackingStoreBuffered defer:false];
[argumentsWindow setDelegate:self];
[argumentsWindow setTitle:@"Arguments"];
[argumentsWindow setMinSize:NSMakeSize(130,80)];
[argumentsWindow setLevel:NSFloatingWindowLevel];
f = [[[NSForm alloc] initWithFrame:NSMakeRect(10,55,baseWidth-20,baseHeight-65)] autorelease];
[f setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[[argumentsWindow contentView] addSubview:f]; // The form must be the first subview
// (this is used by method sendMessageButton:)
button = [[[NSButton alloc] initWithFrame:NSMakeRect(baseWidth/2-40,5,110,30)] autorelease];
[button setBezelStyle:1];
[button setTitle:@"Send message"];
[button setAction:@selector(sendMessageButton:)];
[button setTarget:self];
[[argumentsWindow contentView] addSubview:button];
if (nbarg == 1 && [[selectorComponents objectAtIndex:0] hasPrefix:@"operator_"]) [f addEntry:selectedString];
else for (i = 0; i < nbarg; i++) [f addEntry:[[selectorComponents objectAtIndex:i] stringByAppendingString:@":"]];
[f setAutosizesCells:YES];
[argumentsWindow makeKeyAndOrderFront:nil];
[self disable];
}
}
}
}
else
{
if (column == 0)
{
[self fillMatrix:matrix withObject:rootObject];
}
else if ( ((CGFloat)column)/2 != (NSInteger)(column/2) )
{
id selectedObject = [[[[self subviews] objectAtIndex:0] selectedCell] representedObject];
Class cls = [selectedObject class];
while (cls)
{
void *iterator = 0;
struct objc_method_list *mlist;
NSInteger i;
NSBrowserCell *cell;
NSColor *txtColor = [NSColor blueColor];
NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName, nil];
NSMutableAttributedString *attrStr = [[[NSMutableAttributedString alloc] initWithString:[cls description] attributes:txtDict] autorelease];
[attrStr setAlignment:NSCenterTextAlignment range:NSMakeRange(0,[attrStr length])];
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setLeaf:YES];
[cell setEnabled:NO];
[cell setAttributedStringValue:attrStr];
while ( mlist = class_nextMethodList( cls, &iterator ) )
{
for (i = 0; i < mlist->method_count; i++)
{
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setStringValue:[Compiler stringFromSelector:mlist->method_list[i].method_name]];
}
}
// add a blank line
[matrix addRow];
cell = [matrix cellAtRow:[matrix numberOfRows]-1 column:0];
[cell setLeaf:YES];
[cell setEnabled:NO];
cls = [cls superclass];
}
}
else
{
NSString *selectedString = [[[[self subviews] objectAtIndex:0] selectedCell] stringValue];
SEL selector = [Compiler selectorFromString:selectedString];
NSArray *selectorComponents = [NSStringFromSelector(selector) componentsSeparatedByString:@":"];
NSInteger nbarg = [selectorComponents count]-1;
id selectedObject = [[[[self subviews] objectAtIndex:0] selectedCellInColumn:column-2] representedObject];
if (nbarg == 0)
[self sendMessageTo:selectedObject selectorString:selectedString arguments:[NSArray array] fillThisMatrixWithResult:matrix];
else
{
NSInteger i;
NSInteger baseWidth = 300;
NSInteger baseHeight = nbarg*30+55;
NSButton *button;
NSForm *f;
argumentsWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(100,100,baseWidth,baseHeight) styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask backing:NSBackingStoreBuffered defer:false];
[argumentsWindow setDelegate:self];
[argumentsWindow setTitle:@"Arguments"];
[argumentsWindow setMinSize:NSMakeSize(130,80)];
[argumentsWindow setLevel:NSFloatingWindowLevel];
f = [[[NSForm alloc] initWithFrame:NSMakeRect(10,55,baseWidth-20,baseHeight-65)] autorelease];
[f setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[[argumentsWindow contentView] addSubview:f]; // The form must be the first subview
// (this is used by method sendMessageButton:)
button = [[[NSButton alloc] initWithFrame:NSMakeRect(baseWidth/2-40,5,110,30)] autorelease];
[button setBezelStyle:1];
[button setTitle:@"Send message"];
[button setAction:@selector(sendMessageButton:)];
[button setTarget:self];
[[argumentsWindow contentView] addSubview:button];
if (nbarg == 1 && [[selectorComponents objectAtIndex:0] hasPrefix:@"operator_"]) [f addEntry:selectedString];
else for (i = 0; i < nbarg; i++) [f addEntry:[[selectorComponents objectAtIndex:i] stringByAppendingString:@":"]];
[f setAutosizesCells:YES];
[argumentsWindow makeKeyAndOrderFront:nil];
[self disable];
}
}
}
}
- (void)sendMessageButton:(NSButton *)button
{
if (twoLevelsEnabled)
{
NSString *selectedString = [[b2 selectedCell] stringValue];
id selectedObject = [[b1 selectedCellInColumn:[b1 lastColumn]-1] representedObject];
NSForm *f = [[[[button window] contentView] subviews] objectAtIndex:0];
NSInteger nbarg = [f numberOfRows];
NSMutableArray *arguments = [NSMutableArray arrayWithCapacity:nbarg];
NSInteger i;
for (i = 0; i < nbarg; i++)
{
NSFormCell *cell = [f cellAtIndex:i];
NSString *argumentString = [cell stringValue];
FSInterpreterResult *result = [interpreter execute:argumentString];
if ([result isOk])
[arguments addObject:[result result]];
else
{
#warning 64BIT: Check formatting arguments
NSMutableString *errorArgumentString = [NSString stringWithFormat:@"Argument %d %@", i+1, [result errorMessage]];
[f selectTextAtIndex:i];
NSRunAlertPanel(@"ERROR", errorArgumentString, @"Ok", nil, nil,nil);
// An alternative for displaying the error message. Not yet functionnal.
/*
NSMutableString *errorArgumentString = [NSMutableString stringWithString:argumentString];
NSBeep();
[errorArgumentString insertString:[result errorMessage] atIndex:[result errorRange].location];
[cell setStringValue:errorArgumentString];
[cell selectWithFrame:NSMakeRect(0,0,[cell cellSize].width,[cell cellSize].height) inView:[cell controlView] editor:[f currentEditor] delegate:self start:[result errorRange].location length:[result errorRange].length]; */
break;
}
}
if (i == nbarg) // There were no error evaluating the arguments
{
[self sendMessageTo:selectedObject selectorString:selectedString arguments:arguments fillThisMatrixWithResult:[b1 matrixInColumn:[b1 lastColumn]]];
[b1 tile];
[[button window] close];
}
}
else
{
NSBrowser *browser = [[self subviews] objectAtIndex:0];
NSString *selectedString = [[browser selectedCell] stringValue];
id selectedObject = [[browser selectedCellInColumn:[browser selectedColumn]-1] representedObject];
NSForm *f = [[[[button window] contentView] subviews] objectAtIndex:0];
NSInteger nbarg = [f numberOfRows];
NSMutableArray *arguments = [NSMutableArray arrayWithCapacity:nbarg];
NSInteger i;
for (i = 0; i < nbarg; i++)
{
NSFormCell *cell = [f cellAtIndex:i];
NSString *argumentString = [cell stringValue];
FSInterpreterResult *result = [interpreter execute:argumentString];
if ([result isOk])
[arguments addObject:[result result]];
else
{
#warning 64BIT: Check formatting arguments
NSMutableString *errorArgumentString = [NSString stringWithFormat:@"Argument %d %@", i+1, [result errorMessage]];
[f selectTextAtIndex:i];
NSRunAlertPanel(@"ERROR", errorArgumentString, @"Ok", nil, nil,nil);
// An alternative for displaying the error message. Not yet functionnal.
/*
NSMutableString *errorArgumentString = [NSMutableString stringWithString:argumentString];
NSBeep();
[errorArgumentString insertString:[result errorMessage] atIndex:[result errorRange].location];
[cell setStringValue:errorArgumentString];
[cell selectWithFrame:NSMakeRect(0,0,[cell cellSize].width,[cell cellSize].height) inView:[cell controlView] editor:[f currentEditor] delegate:self start:[result errorRange].location length:[result errorRange].length]; */
break;
}
}
if (i == nbarg) // There were no error evaluating the arguments
{
[self sendMessageTo:selectedObject selectorString:selectedString arguments:arguments fillThisMatrixWithResult:[browser matrixInColumn:[browser lastColumn]]];
[browser tile];
[[button window] close];
}
}
}
-(void) setInterpreter:(FSInterpreter *)theInterpreter
{
interpreter = [theInterpreter retain];
}
-(void) setRootObject:(NSArray *)theRootObject
{
rootObject = [theRootObject retain];
}
- (void)windowWillClose:(NSNotification *)aNotification
// Invoked when argumentsWindow is about to close
{
argumentsWindow = nil;
// Note: We don't send "release" because the window will automatically release itself (TODO: test this)
[self enable];
}
@end
#endif