forked from danielkleinert/EFLaceView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEFView.m
489 lines (412 loc) · 17 KB
/
EFView.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
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
//
// EFView.m
// EFLaceView
//
// Created by MacBook Pro ef on 25/07/06.
// Copyright 2006 Edouard FISCHER. All rights reserved.
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name of Edouard FISCHER nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import "EFView.h"
#import "EFLaceView.h"
#import "Utils.h"
static void *_inoutputObservationContext = (void *)1094;
@implementation EFView
#pragma mark -
#pragma mark *** init routines ***
- (id) init {
return [self initWithFrame:NSMakeRect(0,0,10,10)];
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
_inputs = [[NSMutableSet alloc] init];
_outputs = [[NSMutableSet alloc] init];
_stringAttributes = [[NSMutableDictionary alloc] init];
[_stringAttributes setObject:[NSFont messageFontOfSize:10] forKey:NSFontAttributeName];
[_stringAttributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
_title = @"Title bar";
NSSize titleSize = [[self title] sizeWithAttributes:_stringAttributes];
_titleColor = [NSColor greenColor];
_verticalOffset = titleSize.height/2;
[self setFrameSize:[self minimalSize]];
[self setNeedsDisplay:YES];
// need to update view when labels or positions are changed in inputs or ouputs
[self addObserver:self forKeyPath:@"inputs" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:_inoutputObservationContext];
[self addObserver:self forKeyPath:@"outputs" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:_inoutputObservationContext];
}
return self;
}
- (void)removeFromSuperview {
for (id anObject in _inputs) {
[anObject removeObserver:self forKeyPath:@"label"];
[anObject removeObserver:self forKeyPath:@"position"];
}
for (id anObject in _outputs) {
[anObject removeObserver:self forKeyPath:@"label"];
[anObject removeObserver:self forKeyPath:@"position"];
}
[super removeFromSuperview];
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"inputs"];
[self removeObserver:self forKeyPath:@"outputs"];
}
#pragma mark -
#pragma mark *** setters and accessors ***
//vertical Offset
- (float)verticalOffset {
return _verticalOffset;
}
- (void)setVerticalOffset:(float)aValue {
_verticalOffset = MAX(aValue,0);
[self setHeight:MAX([self minimalSize].height,[self height])];
[[self superview] setNeedsDisplay:YES];
}
- (BOOL)isSelected{
return [[(EFLaceView*)[self superview] selectedSubViews] containsObject:self];
}
// title color
- (NSColor *)titleColor {
return _titleColor;
}
- (void)setTitleColor:(NSColor *)aColor {
if (aColor != [self titleColor]) {
_titleColor = aColor;
}
[self setNeedsDisplay:YES];
}
// title
- (NSString *)title {
return (_title == nil)?@"":_title;
}
- (void)setTitle:(NSString *)aTitle {
if (aTitle != _title) {
_title = aTitle;
[self setWidth:MAX([self minimalSize].width,[self width])];
[self setNeedsDisplay:YES];
}
}
- (float) originX {
return [self frame].origin.x;
}
- (float) originY {
return [self frame].origin.y;
}
- (float) width {
return [self frame].size.width;
}
- (float) height {
return [self frame].size.height;
}
-(void) setOriginX:(float)aFloat {
if (aFloat != [self originX]) {
NSRect frame = [self frame];
frame.origin.x = aFloat;
[self setFrame:frame];
[[self superview] setNeedsDisplay:YES];
}
}
-(void) setOriginY:(float)aFloat {
if (aFloat != [self originY]) {
NSRect frame = [self frame];
frame.origin.y = aFloat;
[self setFrame:frame];
[[self superview] setNeedsDisplay:YES];
}
}
-(void) setWidth:(float)aFloat {
if (aFloat != [self width]) {
NSRect frame = [self frame];
frame.size.width = MAX(aFloat,[self minimalSize].width);
[self setFrame:frame];
[[self superview] setNeedsDisplay:YES];
}
}
-(void) setHeight:(float)aFloat {
if (aFloat != [self height]) {
NSRect frame = [self frame];
frame.size.height = MAX(aFloat,[self minimalSize].height);
[self setFrame:frame];
[[self superview] setNeedsDisplay:YES];
}
}
#pragma mark inputs
- (NSMutableSet *)inputs {
return _inputs;
}
- (void)setInputs:(NSMutableSet *)aSet {
if (aSet != _inputs) {
_inputs = aSet;
}
}
- (NSArray *)orderedInputs {
return [self orderedHoles:[self inputs]];
}
- (NSArray *)orderedHoles:(NSSet *)aSet {
NSSortDescriptor* sort = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES];
NSArray* result = [[aSet allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
return result;
}
#pragma mark outputs
- (NSMutableSet *)outputs {
return _outputs;
}
- (void)setOutputs:(NSMutableSet *)aSet {
if (aSet != _outputs) {
_outputs = aSet;
}
}
- (NSArray *)orderedOutputs {
return [self orderedHoles:[self outputs]];
}
#pragma mark -
#pragma mark *** geometry ***
- (id) endHole:(NSPoint)aPoint {
NSPoint mousePos = [self convertPoint:aPoint fromView:[self superview]];
NSSize stringSize = [[self title] sizeWithAttributes:_stringAttributes];
float heightOfText = stringSize.height;
if ((mousePos.x>0) && (mousePos.x <15)) {
int hole = (-mousePos.y + [self bounds].origin.y + [self bounds].size.height - [self verticalOffset] - heightOfText*0.5 ) / heightOfText;
id res = ((hole >0)&&(hole <=[[self inputs] count]))?[[self orderedInputs] objectAtIndex:hole-1]:nil;
if (res) {
[res setValue:_data forKey:@"data"];
}
return res;
}
return nil;
}
- (id) startHole:(NSPoint)aPoint {
NSPoint mousePos = [self convertPoint:aPoint fromView:[self superview]];
NSSize stringSize = [[self title] sizeWithAttributes:_stringAttributes];
float heightOfText = stringSize.height;
if ((mousePos.x>[self bounds].origin.x+[self bounds].size.width-15) && (mousePos.x <[self bounds].origin.x+[self bounds].size.width)) {
int hole = (-mousePos.y + [self bounds].origin.y + [self bounds].size.height - [self verticalOffset] - heightOfText*0.5) / heightOfText;
id res = ((hole >0)&&(hole <=[[self outputs] count]))?[[self orderedOutputs] objectAtIndex:hole-1]:nil;
if (res) {
[res setValue:_data forKey:@"data"];
}
return res;
}
return nil;
}
- (NSPoint)endHolePoint:(id)aEndHole {
NSSize stringSize = [[self title] sizeWithAttributes:_stringAttributes];
float heightOfText = stringSize.height;
int hole = [[self orderedHoles:[self inputs]] indexOfObject:aEndHole]+1;
NSAssert( (hole <= [[self inputs] count]),@"hole should be within Inputs range in endholePoint:");
return [self convertPoint:NSMakePoint(5+4,[self bounds].origin.y+[self bounds].size.height - [self verticalOffset] - heightOfText * (hole+1.0)) toView:[self superview]];
}
- (NSPoint)startHolePoint:(id) aStartHole {
NSSize stringSize = [[self title] sizeWithAttributes:_stringAttributes];
float heightOfText = stringSize.height;
int hole = [[self orderedHoles:[self outputs]] indexOfObject:aStartHole]+1;
NSAssert( (hole <= [[self outputs] count]),@"hole should be within Outputs range in startholePoint:");
return [self convertPoint:NSMakePoint([self bounds].origin.x+[self bounds].size.width-5-4, [self bounds].origin.y+[self bounds].size.height - [self verticalOffset] - heightOfText * (hole+1.0)) toView:[self superview]];
}
- (NSSize) minimalSize {
NSSize titleSize = [[self title] sizeWithAttributes:_stringAttributes];
float maxInputWidth = 0;
int i;
for (i=0; i<[[self inputs] count]; i++) {
NSString* inputLabel = [[[self orderedInputs] objectAtIndex:(unsigned)i] valueForKey:@"label"];
float inputWidth = 10+4+[inputLabel sizeWithAttributes:_stringAttributes].width + 5;
maxInputWidth = MAX(inputWidth,maxInputWidth);
}
float maxOutputWidth = 0;
int j;
for (j=0; j<[[self outputs] count]; j++) {
NSString* outputLabel = [[[self orderedOutputs] objectAtIndex:(unsigned)j] valueForKey:@"label"];
float outputWidth =10+4+[outputLabel sizeWithAttributes:_stringAttributes].width + 5;
maxOutputWidth = MAX(outputWidth,maxOutputWidth);
}
NSSize result;
result.width = MAX(titleSize.width+16,maxInputWidth+maxOutputWidth);
result.height = (titleSize.height)*(2.0+(([[self inputs] count]>[[self outputs] count])?[[self inputs] count]:[[self outputs] count])) + [self verticalOffset]+12;
return result;
}
#pragma mark -
#pragma mark *** drawing ***
- (void)drawRect:(NSRect)rect {
NSRect bounds = NSInsetRect([self bounds],4,4);
const float backgroundAlpha = 0.7;
NSSize stringSize = [[self title] sizeWithAttributes:_stringAttributes];
//draw body background
[[[[self titleColor] blendedColorWithFraction:0.8 ofColor:[NSColor controlBackgroundColor]]colorWithAlphaComponent:backgroundAlpha] setFill];
[[NSBezierPath bezierPathWithBottomRoundedRect:NSMakeRect(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height - stringSize.height) radius:8] fill];
//draw title background
[[NSBezierPath bezierPathWithTopRoundedRect:NSMakeRect(bounds.origin.x, bounds.origin.y + bounds.size.height - stringSize.height, bounds.size.width, stringSize.height) radius:8] gradientFillWithColor:[[self titleColor] colorWithAlphaComponent:backgroundAlpha]];
//draw title
[[self title] drawAtPoint:NSMakePoint(bounds.origin.x + (bounds.size.width -stringSize.width)/2,bounds.origin.y + bounds.size.height - stringSize.height) withAttributes:_stringAttributes];
// draw end of lace
for (NSDictionary *aDict in [self inputs]) {
NSBezierPath *path = [NSBezierPath bezierPath];
[path setLineWidth:1];
[[NSColor grayColor] set];
NSPoint end = [self convertPoint: [self endHolePoint:aDict] fromView:[self superview]];
[path appendBezierPathWithOvalInRect:NSMakeRect(end.x-3,end.y-3,6,6)];
[path stroke];
NSPoint labelOrigin;
NSString *inputLabel = [aDict valueForKey:@"label"];
labelOrigin.x = end.x +5;
labelOrigin.y = end.y - stringSize.height/2;
[[NSColor blackColor] set];
[inputLabel drawAtPoint:labelOrigin withAttributes:_stringAttributes];
}
// draw start of lace
for (NSDictionary *aDict in [self outputs]) {
NSBezierPath *path = [NSBezierPath bezierPath];
[path setLineWidth:1];
[[NSColor grayColor] set];
NSPoint start = [self convertPoint: [self startHolePoint:aDict] fromView:[self superview]];
[path appendBezierPathWithOvalInRect:NSMakeRect(start.x-3,start.y-3,6,6)];
[path stroke];
NSPoint labelOrigin;
NSString *outputLabel = [aDict valueForKey:@"label"];
labelOrigin.x = start.x - 5 - [outputLabel sizeWithAttributes:_stringAttributes].width;
labelOrigin.y = start.y - stringSize.height/2;
[[NSColor blackColor] set];
[outputLabel drawAtPoint:labelOrigin withAttributes:_stringAttributes];
}
//draw outline
[(([self isSelected])&&([NSGraphicsContext currentContextDrawingToScreen]))?[NSColor selectedControlColor]:[NSColor controlShadowColor] /*_titleColor*/ setStroke];
float lineWidth = (([self isSelected])&&([NSGraphicsContext currentContextDrawingToScreen]))?2.0:1.0;
NSBezierPath *shape = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(bounds,-lineWidth/2+0.15,-lineWidth/2+0.15) radius:8]; //0.15 to be perfect on a zoomed printing
[shape setLineWidth:lineWidth];
[shape stroke];
}
-(void)setFrame:(NSRect)aRect {
NSRect orFrame = [self frame];
if (orFrame.origin.x != aRect.origin.x) {
[self willChangeValueForKey:@"originX"];
[self willChangeValueForKey:@"drawingBounds"];
}
if (orFrame.origin.y != aRect.origin.y) {
[self willChangeValueForKey:@"originY"];
[self willChangeValueForKey:@"drawingBounds"];
}
if (orFrame.size.height != aRect.size.height) {
[self willChangeValueForKey:@"height"];
[self willChangeValueForKey:@"drawingBounds"];
}
if (orFrame.size.width != aRect.size.width) {
[self willChangeValueForKey:@"width"];
[self willChangeValueForKey:@"drawingBounds"];
}
[super setFrame:aRect];
if (orFrame.origin.x != aRect.origin.x) {
[self didChangeValueForKey:@"originX"];
[self didChangeValueForKey:@"drawingBounds"];
}
if (orFrame.origin.y != aRect.origin.y) {
[self didChangeValueForKey:@"originY"];
[self didChangeValueForKey:@"drawingBounds"];
}
if (orFrame.size.height != aRect.size.height) {
[self didChangeValueForKey:@"height"];
[self didChangeValueForKey:@"drawingBounds"];
}
if (orFrame.size.width != aRect.size.width) {
[self didChangeValueForKey:@"width"];
[self didChangeValueForKey:@"drawingBounds"];
}
}
#pragma mark -
#pragma mark *** events ***
- (BOOL) acceptsFirstResponder {
return YES;
}
- (NSView *)hitTest:(NSPoint)aPoint {
return (([self startHole:aPoint] != nil) || ([self endHole:aPoint] != nil))?nil:[super hitTest:aPoint];
}
- (void)mouseDown:(NSEvent*)theEvent {
EFLaceView* sView = (EFLaceView*)[self superview];
if ([theEvent modifierFlags] & NSShiftKeyMask){
// add to selection
[sView selectView:self state:YES];
} else if ([theEvent modifierFlags] & NSCommandKeyMask){
// inverse selection
[sView selectView:self state:!self.isSelected];
} else if (!self.isSelected) {
[sView deselectViews];
[sView selectView:self state:YES];
}
BOOL keepOn = YES;
NSPoint mouseLoc;
NSPoint lastMouseLoc = [[self superview] convertPoint:[theEvent locationInWindow] fromView:nil];;
NSRect initialFrame = [self frame];
while (keepOn) {
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSPeriodicMask ];
switch ([theEvent type]) {
case NSLeftMouseDragged: {
[[NSCursor closedHandCursor] set];
mouseLoc = [[self superview] convertPoint:[theEvent locationInWindow] fromView:nil];
for (NSView* view in [sView selectedSubViews]) {
[view setFrame: NSOffsetRect([view frame],mouseLoc.x-lastMouseLoc.x,mouseLoc.y-lastMouseLoc.y)];
}
lastMouseLoc = mouseLoc;
[self autoscroll:theEvent];
[sView setNeedsDisplay:YES];
break;
}
case NSLeftMouseUp:
[[NSCursor arrowCursor] set];
if (!NSContainsRect([sView bounds],[self frame])) {
// revert to original frame if not inside superview
[self setFrame:initialFrame];
[sView setNeedsDisplay:YES];
}
keepOn = NO;
[sView setNeedsDisplay:YES];
break;
default:
/* Ignore any other kind of event. */
break;
}
};
return;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (((keyPath == @"inputs") || keyPath == @"outputs") && (context == _inoutputObservationContext)) {
NSSet *new = [change valueForKey:@"new"];
NSSet *old = [change valueForKey:@"old"];
//compute inserted labels
NSMutableSet *inserted = [new mutableCopy];
[inserted minusSet:old];
//compute removed labels
NSMutableSet *removed = [old mutableCopy];
[removed minusSet:new];
//make label observed by the view for changes on label or on position
for (id anObject in inserted) {
[anObject addObserver:self forKeyPath:@"label" options:0 context:_inoutputObservationContext];
[anObject addObserver:self forKeyPath:@"position" options:0 context:_inoutputObservationContext];
[anObject addObserver:self forKeyPath:@"laces" options:0 context:_inoutputObservationContext];
}
for (id anObject in removed) {
[anObject removeObserver:self forKeyPath:@"label"];
[anObject removeObserver:self forKeyPath:@"position"];
[anObject removeObserver:self forKeyPath:@"laces"];
}
//update size and redraw
[self setWidth:MAX([self minimalSize].width,[self width])];
[self setHeight:MAX([self minimalSize].height,[self height])];
[[self superview] setNeedsDisplay:YES];
}
if ((keyPath == @"label") && (context == _inoutputObservationContext) ) {
//update size and redraw
[self setWidth:MAX([self minimalSize].width,[self width])];
[self setHeight:MAX([self minimalSize].height,[self height])];
}
if ((keyPath == @"position") && (context == _inoutputObservationContext) ) {
//redraw superview (laces may have changed because of positions of labels)
[[self superview] setNeedsDisplay:YES];
}
if ([keyPath isEqualToString:@"laces"]) {
//redraw laces because of undos
[[self superview] setNeedsDisplay:YES];
}
}
@end