-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.mm
570 lines (479 loc) · 16.9 KB
/
ui.mm
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#import "ui.h"
//------------------------------------------------------------------------------
// SVNWindowController
//------------------------------------------------------------------------------
@implementation SVNWindowController
-(NSMenu*)openWithFor:(NSString*)path {
NSMenu *menu = [[[NSMenu alloc] initWithTitle:@"Open with"] autorelease];
NSString *uti = [path getUTI];
NSArray *array = (NSArray*)LSCopyAllRoleHandlersForContentType((CFStringRef)uti, kLSRolesAll);
for (NSString *bundle_id in array) {
if (NSString *path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:bundle_id]) {
NSString *name = [[NSFileManager defaultManager] displayNameAtPath:path];
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
NSMenuItem *mi = [[[NSMenuItem alloc] initWithTitle:name action:@selector(open_with:) keyEquivalent:@""] autorelease];
[mi setImage:icon];
[mi setRepresentedObject:path];
[menu addItem:mi];
}
}
if ([menu numberOfItems] == 0) {
NSMenuItem *mi =[[NSMenuItem new] autorelease];
[mi setTitle:@"<none>"];
[mi setEnabled:NO];
[menu addItem:mi];
}
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:[[[NSMenuItem alloc] initWithTitle:@"Other..." action:@selector(open_with:) keyEquivalent:@""] autorelease]];
return menu;
}
-(void)chooseApplication:(void(^)(NSURL *app))handler {
NSArray *dirs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationDirectory inDomains: NSLocalDomainMask];
NSOpenPanel *dlg = [NSOpenPanel openPanel];
[dlg setMessage: @"Choose an application to open the document"];
// [dlg setPrompt: @"Add Application"];
[dlg setDirectoryURL: [dirs objectAtIndex:0]];
[dlg setAllowsMultipleSelection:NO];
[dlg setCanChooseDirectories: NO];
[dlg beginSheetModalForWindow:[self window] completionHandler: ^(NSInteger result) {
if (result == NSOKButton)
handler([[dlg URLs] objectAtIndex:0]);
}];
}
-(void)setLogo {
NSWindow *window = [self window];
[window setRepresentedFilename:@" "];
[[window standardWindowButton:NSWindowDocumentIconButton] setImage:[NSImage imageNamed:@"logo"]];
}
@end
//------------------------------------------------------------------------------
// Criteria
//------------------------------------------------------------------------------
@implementation Criterion
@synthesize name;
+(id)criterion {
return [[self class] criterionWithName:nil children:nil];
}
+(id)criterionWithName:(NSString*)name {
return [[self class] criterionWithName:name children:nil];
}
+(id)criterionWithName:(NSString*)name children:(id)children, ... {
Criterion *crit = [[[[self class] alloc] init] autorelease];
crit.name = name;
va_list ap;
va_start(ap, children);
for (id next = children; next; next = va_arg(ap, id))
[crit addChild:next];
va_end(ap);
return crit;
}
+(id)boolCriterion {
static Criterion *boolCriterion;
if (!boolCriterion)
boolCriterion = [Criterion criterionWithName:@":" children:
[MatchCriterion criterionWithName:@"no"],
[MatchCriterion criterionWithName:@"yes"],
nil
];
return boolCriterion;
}
-(void)dealloc {
self.name = nil;
[children release];
[super dealloc];
}
-(void)addChild:(id)child {
if (!children)
children = [NSMutableArray new];
[children addObject:child];
}
-(NSUInteger)numberOfChildren {
return [children count];
}
-(id)childAtIndex:(NSUInteger)index {
return [children objectAtIndex:index];
}
-(id)displayValue:(NSString*)input {
return self.name;
}
-(NSString*)output:(id)display after:(NSString*)input last:(bool)last {
return input;
}
-(bool)isComposite {
return false;
}
-(NSString*)matches:(NSString*)input {
return input;
}
@end
//----------------------------------------
@implementation SeparatorCriterion
+(id)shared {
static SeparatorCriterion *shared;
@synchronized(self) {
if (!shared)
shared = [SeparatorCriterion new];
return shared;
}
}
-(id)displayValue:(NSString*)input {
return [NSMenuItem separatorItem];
}
-(NSString*)matches:(NSString*)input {
return nil;
}
@end
//----------------------------------------
@implementation CompositeCriterion
-(id)withSeparators:(NSString*)s {
separators = s;
return self;
}
-(bool)isComposite {
return true;
}
-(NSUInteger)numberOfChildren {
return 0;
}
@end
//----------------------------------------
@implementation TextFieldCriterion
+(id)shared {
static TextFieldCriterion *shared;
@synchronized(self) {
if (!shared)
shared = [TextFieldCriterion new];
return shared;
}
}
-(NSString*)matches:(NSString*)input {
return @"";
}
-(id)displayValue:(NSString*)input withWidth:(float)width {
if (name) {
LabelledTextField *text = [[[LabelledTextField alloc] initWithFrame:NSMakeRect(0, 0, width, 18)] autorelease];
[text setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
[[text cell] setWraps:NO];
[text setStringValue:input];
[text setLabel:name];
return text;
} else {
NSTextField *text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, width, 18)] autorelease];
[text setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
[[text cell] setWraps:NO];
[text setStringValue:input];
return text;
}
}
-(id)displayValue:(NSString*)input {
return [self displayValue:[input stringByReplacingOccurrencesOfString:@"\n" withString:@";"] withWidth:600];
}
-(NSString*)output:(id)display after:(NSString*)input last:(bool)last {
NSString *text = [(NSTextField*)display stringValue];
NSUInteger length = [input length];
if (length == 0)
return text;
if ([[NSCharacterSet alphanumericCharacterSet] characterIsMember:[input characterAtIndex:length - 1]])
input = [input stringByAppendingString:@" "];
return [input stringByAppendingString:text];
}
@end
//----------------------------------------
@implementation MatchCriterion
-(NSString*)matches:(NSString*)input {
NSUInteger length = [name length];
if ([input hasPrefix:name]) {
if ([input length] == length)
return @"";
unichar c = [input characterAtIndex:length];
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == ':' || c == '-' || c == '_')
return nil;
return [input substringFromIndex:length + 1];
}
return nil;
}
-(NSString*)output:(id)display after:(NSString*)input last:(bool)last {
NSUInteger length = [input length];
if (length == 0)
return name;
if ([[NSCharacterSet alphanumericCharacterSet] characterIsMember:[input characterAtIndex:length - 1]])
input = [input stringByAppendingString:@" "];
return [input stringByAppendingString:name];
}
@end
//----------------------------------------
@implementation TokenFieldCriterion
+(id)criterionWithName:(NSString*)name separators:(NSString*)separators width:(float)width children:(id)children, ... {
TokenFieldCriterion *crit = [[[self class] new] autorelease];
crit.name = name;
va_list ap;
va_start(ap, children);
for (id next = children; next; next = va_arg(ap, id))
[crit addChild:next];
va_end(ap);
crit->width = width;
crit->separators = separators;
return crit;
}
-(id)withWidth: (float)w {
width = w;
return self;
}
-(id)withSeparators:(NSString*)s {
separators = s;
return self;
}
-(NSRange)findEnd:(NSString*)input {
return [input rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:separators ? separators : @" \t\n\r;"]];
}
-(NSString*)matches:(NSString*)input {
NSRange r = [self findEnd:input];
return r.location == NSNotFound ? @"" : [input substringFromIndex:r.location + 1];
}
-(id)displayValue:(NSString*)input {
NSRange r = [self findEnd:input];
return [self displayValue:r.location == NSNotFound ? input : [input substringToIndex:r.location] withWidth:width ? width : 100];
}
-(NSString*)output:(id)display after:(NSString*)input last:(bool)last {
input =[super output:display after:input last:last];
if (!separators)
return input;
return [input stringByAppendingString:last
? [separators substringFromIndex:[separators length] - 1]
: [separators substringToIndex:1]
];
}
@end
//------------------------------------------------------------------------------
// MyRuleEditor
//------------------------------------------------------------------------------
@implementation MyRuleEditor
-(void)insertRowAtIndex:(NSInteger)rowIndex withType:(NSRuleEditorRowType)rowType asSubrowOfRow:(NSInteger)parentRow animate:(BOOL)shouldAnimate {
[(id<MyRuleEditorDelegate>)self.delegate ruleEditor:self setParentRow:parentRow];
[super insertRowAtIndex:rowIndex withType:rowType asSubrowOfRow:parentRow animate:shouldAnimate];
#if 0
NSMutableArray *c = [NSMutableArray new];
NSMutableArray *v = [NSMutableArray new];
for (id crit = nil; [self.delegate ruleEditor:self numberOfChildrenForCriterion:crit withRowType:NSRuleEditorRowTypeSimple];) {
crit = [self.delegate ruleEditor:self child:0 forCriterion:crit withRowType:NSRuleEditorRowTypeSimple];
[c addObject:crit];
[v addObject:[self.delegate ruleEditor:self displayValueForCriterion:crit inRow:rowIndex]];
}
[super setCriteria:c andDisplayValues:v forRowAtIndex:rowIndex];
#endif
}
-(void)removeRowsAtIndexes:(NSIndexSet*)rowIndexes includeSubrows:(BOOL)includeSubrows {
[(id<MyRuleEditorDelegate>)self.delegate ruleEditor:self removeRows:rowIndexes];
[super removeRowsAtIndexes:rowIndexes includeSubrows:YES];
}
-(void)setCriteria:(NSArray*)criteria andDisplayValues:(NSArray*)values forRowAtIndex:(NSInteger)rowIndex {
[super setCriteria:criteria andDisplayValues:values forRowAtIndex:rowIndex];
}
-(int)getNewChildRow:(int)parent {
int row = [[self subrowIndexesForRow:parent] lastIndex];
while (row != -1 && [self rowTypeForRow:row] == NSRuleEditorRowTypeCompound) {
parent = row;
row = [[self subrowIndexesForRow:parent] lastIndex];
}
return row == -1 ? parent + 1 : row + 1;
}
-(void)parse:(NSString*)value withCriteria:(Criterion*)criteria toParent:(NSInteger)parent {
NSMutableArray *c = [NSMutableArray new];
NSMutableArray *v = [NSMutableArray new];
struct {
CompositeCriterion *comp;
NSInteger parent;
} stack[8], *sp = stack;
Criterion *crit = criteria;
NSString *input = value;
NSInteger ancestor = parent;
for (;;) {
Criterion *next = 0;
for (Criterion *i in crit->children) {
if (NSString *rem = [i matches:input]) {
[c addObject:i];
[v addObject:[i displayValue:input]];
next = i;
input = rem;
break;
}
}
if (!next) {
NSRange r = [input rangeOfString:@" "];
NSString* s;
if (r.location == NSNotFound) {
s = input;
input = @"";
} else {
s = [input substringToIndex:r.location];
input = [input substringFromIndex:r.location + 1];
}
Criterion *i = [MatchCriterion criterionWithName:s];
[c addObject:i];
[v addObject:s];
[crit addChild:i];
if (crit == criteria) {
TextFieldCriterion *t = [TextFieldCriterion shared];
[i addChild:t];
[c addObject:t];
[v addObject:[t displayValue:value]];
break;
}
next = i;
}
crit = next;
if ([crit isComposite]) {
sp->comp = (CompositeCriterion*)crit;
sp->parent = parent;
++sp;
int row = [self getNewChildRow:parent];
[self insertRowAtIndex:row withType:NSRuleEditorRowTypeCompound asSubrowOfRow:parent animate:FALSE];
[self setCriteria:c andDisplayValues:v forRowAtIndex:row];
[c removeAllObjects];
[v removeAllObjects];
parent = row;
}
if ([crit->children count] == 0) {
if (parent == ancestor || [input length] == 0)
break;
int row = [self getNewChildRow:parent];
[self insertRowAtIndex:row withType:NSRuleEditorRowTypeSimple asSubrowOfRow:parent animate:FALSE];
[self setCriteria:c andDisplayValues:v forRowAtIndex:row];
[c removeAllObjects];
[v removeAllObjects];
if (sp > stack) {
if (sp[-1].comp->separators) {
while ([input length] && [[NSCharacterSet characterSetWithCharactersInString:sp[-1].comp->separators] characterIsMember:[input characterAtIndex:0]])
input = [input substringFromIndex:1];
} else if (![[NSCharacterSet alphanumericCharacterSet] characterIsMember:[input characterAtIndex:0]]) {
--sp;
parent = sp->parent;
while (![[NSCharacterSet alphanumericCharacterSet] characterIsMember:[input characterAtIndex:0]])
input = [input substringFromIndex:1];
}
crit = sp[-1].comp;
}
}
}
if ([c count]) {
int row = [self getNewChildRow:parent];
[self insertRowAtIndex:row withType:NSRuleEditorRowTypeSimple asSubrowOfRow:parent animate:FALSE];
[self setCriteria:c andDisplayValues:v forRowAtIndex:row];
}
}
-(NSString*)deparseRow:(int)row to:(NSString*)output isLast:(bool)last {
NSArray *c = [self criteriaForRow:row];
NSArray *v = [self displayValuesForRow:row];
for (int j = 0, n = [c count]; j < n; j++)
output = [(Criterion*)[c objectAtIndex:j] output:[v objectAtIndex:j] after:output last:last];
if ([self rowTypeForRow:row] == NSRuleEditorRowTypeCompound) {
CompositeCriterion *comp = [c lastObject];
NSIndexSet *set = [self subrowIndexesForRow:row];
for (NSUInteger i = [set firstIndex]; i != NSNotFound; i = [set indexGreaterThanIndex:i]) {
output = [self deparseRow:i to:output isLast:i == [set lastIndex]];
if (comp->separators && i != [set lastIndex])
output = [output stringByAppendingString:comp->separators];
}
}
return output;
}
-(NSString*)deparseRow:(int)row {
return [self deparseRow:row to:@"" isLast:true];
}
@end
//------------------------------------------------------------------------------
// PathCell
//------------------------------------------------------------------------------
@implementation PathCell
-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView withIcon:(NSImage*)icon {
NSRect icon_rect, path_rect;
NSDivideRect(cellFrame, &icon_rect, &path_rect, cellFrame.size.height, NSMinXEdge);
[[self attributedStringValue] drawInRect: path_rect];
icon_rect.size.width = icon_rect.size.height = 16;
[icon drawInRect:icon_rect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1 respectFlipped:YES hints:nil];
}
-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
[self drawInteriorWithFrame:cellFrame inView:controlView
withIcon:[[NSWorkspace sharedWorkspace] iconForFileType:[[self title] pathExtension]]
];
}
@end
//------------------------------------------------------------------------------
// IconTextCell
//------------------------------------------------------------------------------
@implementation IconTextCell
@synthesize icon;
-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
[self drawInteriorWithFrame:cellFrame inView:controlView withIcon:icon];
}
@end
//------------------------------------------------------------------------------
// LabelledTextCell
//------------------------------------------------------------------------------
@implementation LabelledTextCell
-(id)init {
if (self = [super init]) {
label = [[NSCell alloc] initTextCell:@""];
}
return self;
}
-(void)setLabel:(NSString*)s {
[label setObjectValue:s];
}
-(void)setFont:(NSFont*)fontObj {
[super setFont:fontObj];
[label setFont:fontObj];
}
-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
NSRect label_rect, edit_rect;
NSDivideRect(cellFrame, &label_rect, &edit_rect, cellFrame.size.width / 2, NSMinXEdge);
label_rect.origin.y += 2;
[label drawWithFrame:label_rect inView:controlView];
[super drawWithFrame:edit_rect inView:controlView];
}
-(void)editWithFrame:(NSRect)aRect inView:(NSView*)controlView editor:(NSText*)textObj delegate:(id)anObject event:(NSEvent*)theEvent {
aRect.size.width /= 2;
aRect.origin.x += aRect.size.width;
[super editWithFrame:aRect inView:controlView editor:textObj delegate:anObject event:theEvent];
}
-(void)selectWithFrame:(NSRect)aRect inView:(NSView*)controlView editor:(NSText*)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
aRect.size.width /= 2;
aRect.origin.x += aRect.size.width;
[super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}
@end
//------------------------------------------------------------------------------
// LabelledTextField
//------------------------------------------------------------------------------
@implementation LabelledTextField
+(Class)cellClass {
return [LabelledTextCell class];
}
-(void)setLabel:(NSString*)s {
[self.cell setLabel:s];
}
-(void)setFont:(NSFont*)fontObj {
[self.cell setFont:fontObj];
}
@end
//------------------------------------------------------------------------------
// SVNErrorAlert
//------------------------------------------------------------------------------
void SVNErrorAlert(NSWindow *window, svn_error_t *err) {
NSAlert *alert = [NSAlert new];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Subversion Error"];
[alert setInformativeText: SVNcontext::GetErrorMessage(err, true)];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert beginSheetModalForWindow:window
modalDelegate:nil
didEndSelector:nil
contextInfo:nil
];
[alert release];
}
void SVNErrorAlertMainThread(NSWindow *window, svn_error_t *err) {
dispatch_async(dispatch_get_main_queue(), ^ {
SVNErrorAlert(window, err);
});
}