/
NSSplitView.m
executable file
·602 lines (470 loc) · 19.5 KB
/
NSSplitView.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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
2009 Markus Hitter <mah@jump-ing.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <AppKit/NSSplitView.h>
#import <AppKit/NSColor.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSEvent.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSGraphicsContext.h>
#import <AppKit/NSImage.h>
#import <AppKit/NSCursor.h>
#import <Foundation/NSKeyedArchiver.h>
#import <AppKit/NSRaise.h>
NSString * const NSSplitViewDidResizeSubviewsNotification = @"NSSplitViewDidResizeSubviewsNotification";
NSString * const NSSplitViewWillResizeSubviewsNotification = @"NSSplitViewWillResizeSubviewsNotification";
@implementation NSSplitView
-(void)encodeWithCoder:(NSCoder *)coder {
NSUnimplementedMethod();
}
-initWithCoder:(NSCoder *)coder {
[super initWithCoder:coder];
if([coder allowsKeyedCoding]){
NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
_isVertical=[keyed decodeBoolForKey:@"NSIsVertical"];
// The divider thickness in the nib may not be the same as ours
[self resizeSubviewsWithOldSize:[self bounds].size];
}
else {
[NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
}
return self;
}
-(id)delegate {
return _delegate;
}
-(BOOL)isVertical {
return _isVertical;
}
-(void)_postNoteWillResize {
[[NSNotificationCenter defaultCenter] postNotificationName:NSSplitViewWillResizeSubviewsNotification object:self];
}
-(void)_postNoteDidResize {
[[NSNotificationCenter defaultCenter] postNotificationName:NSSplitViewDidResizeSubviewsNotification object:self];
}
-(void)setDelegate:(id)delegate {
if ([_delegate respondsToSelector:@selector(splitViewDidResizeSubviews:)])
[[NSNotificationCenter defaultCenter] removeObserver:_delegate name:NSSplitViewDidResizeSubviewsNotification object:self];
if ([_delegate respondsToSelector:@selector(splitViewWillResizeSubviews:)])
[[NSNotificationCenter defaultCenter] removeObserver:_delegate name:NSSplitViewWillResizeSubviewsNotification object:self];
_delegate=delegate;
if ([_delegate respondsToSelector:@selector(splitViewDidResizeSubviews:)])
[[NSNotificationCenter defaultCenter] addObserver:_delegate
selector:@selector(splitViewDidResizeSubviews:)
name:NSSplitViewDidResizeSubviewsNotification
object:self];
if ([_delegate respondsToSelector:@selector(splitViewWillResizeSubviews:)])
[[NSNotificationCenter defaultCenter] addObserver:_delegate
selector:@selector(splitViewWillResizeSubviews:)
name:NSSplitViewWillResizeSubviewsNotification
object:self];
}
-(void)setVertical:(BOOL)flag {
if (_isVertical == flag) {
// Don't do unneccessary work
return;
}
_isVertical=flag;
// Now get the split axis sorted
[self adjustSubviews];
}
-(BOOL)isFlipped {
return YES;
}
-(BOOL)isSubviewCollapsed:(NSView *)subview {
/* From Apple's header comments:
* Collapsed subviews are hidden but retained by the split view.
* Collapsing of a subview will not change its bounds, but may set its frame
* to zero pixels high (in horizontal split views) or zero pixels wide (vertical).
*/
return [subview isHidden];
}
-(void)setDividerStyle:(NSSplitViewDividerStyle)style {
_dividerStyle = style;
[self setNeedsDisplay: YES];
}
- (NSSplitViewDividerStyle)dividerStyle
{
return _dividerStyle;
}
/** adjust all the non-collapsed subviews so that they are equally spaced horizontally within the splitview */
- (void)_adjustSubviewWidths
{
// Set all the subview heights to the bounds height of the split view
float height = NSHeight([self bounds]);
int i,count=[_subviews count];
float totalWidthBefore=0.;
// The available width to the subviews
float totalWidthAfter=[self bounds].size.width-[self dividerThickness]*(count-1);
for(i = 0; i < count; i++) {
NSView *subview = [_subviews objectAtIndex: i];
if ([self isSubviewCollapsed: subview] == NO) {
totalWidthBefore += NSWidth([subview frame]);
}
}
float delta = totalWidthAfter / totalWidthBefore;
NSRect frame = [self bounds];
for(i = 0; i < count; i++){
NSView *subview = [_subviews objectAtIndex: i];
if ([self isSubviewCollapsed: subview] == NO) {
frame.size.width= NSWidth([subview frame]) * delta;
frame.size.width=floor(frame.size.width);
frame.size.height = height;
NSSize oldSize = [subview frame].size;
[subview setFrame:frame];
frame.origin.x+= NSWidth(frame);
frame.origin.x+= [self dividerThickness];
}
}
}
- (void)_adjustSubviewHeights
{
// Set all the subview widths to the bounds width of the split view
float width = NSWidth([self bounds]);
int i,count=[_subviews count];
// We've got to figure out how much the delta is between the old and new heights and multiply
// all the heights to the new delta to get them to fit (or something like that...) Apple says
// They resize proportionally
float totalHeightBefore=0.;
float totalHeightAfter=[self bounds].size.height-[self dividerThickness]*(count-1);
for(i=0;i<count;i++) {
NSView *subview = [_subviews objectAtIndex: i];
if ([self isSubviewCollapsed: subview] == NO) {
NSRect subviewFrame = [subview frame];
totalHeightBefore += NSHeight(subviewFrame);
}
}
float delta = totalHeightAfter / totalHeightBefore;
NSRect frame=[self bounds];
for(i=0;i<count;i++){
NSView *subview = [_subviews objectAtIndex: i];
if ([self isSubviewCollapsed: subview] == NO) {
frame.size.height= NSHeight([subview frame]) * delta;
frame.size.height=floor(frame.size.height);
frame.size.width = width;
NSSize oldSize = [subview frame].size;
[subview setFrame:frame];
frame.origin.y+= NSHeight(frame);
frame.origin.y+=[self dividerThickness];
}
}
}
/**
* Apple says this method sets the frames of the split view's subviews so that they, plus the dividers,
* fill the split view. The default implementation of this method resizes all of the subviews
* proportionally so that the ratio of heights (in the horizontal split view case) or widths
* (in the vertical split view case) doesn't change, even though the absolute sizes of the
* subviews do change. This message should be sent to split views from which subviews have been
* added or removed, to reestablish the consistency of subview placement.
*/
-(void)adjustSubviews {
if ([_subviews count] < 2) {
return;
}
[self _postNoteWillResize];
if ([self isVertical]){
[self _adjustSubviewWidths];
} else {
[self _adjustSubviewHeights];
}
[self setNeedsDisplay: YES];
[self _postNoteDidResize];
}
-(float)dividerThickness {
if (_dividerStyle == NSSplitViewDividerStyleThick) {
return 10;
} else {
return 5;
}
}
-(NSImage *)dimpleImage {
if([self isVertical])
return [NSImage imageNamed:@"NSSplitViewVDimple"];
else
return [NSImage imageNamed:@"NSSplitViewHDimple"];
}
-(void)drawDividerInRect:(NSRect)rect {
if (_dividerStyle != NSSplitViewDividerStylePaneSplitter) {
// Fill in the view - pane splitter means just draw the dimple
[[NSColor controlColor] setFill];
NSRectFill(rect);
}
NSImage *image=[self dimpleImage];
NSSize imageSize = [image size];
NSPoint point = rect.origin;
if([self isVertical]){
point.x += floor((NSWidth(rect) - imageSize.width)/2);
point.y += floor((NSHeight(rect) - imageSize.height)/2);
} else {
point.x += floor((NSWidth(rect) - imageSize.width)/2);
point.y += floor((NSHeight(rect) - imageSize.height)/2);
}
[image drawAtPoint: point fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
}
-(void)addSubview:(NSView *)view {
[super addSubview:view];
[self adjustSubviews];
}
-(void)resizeSubviewsWithOldSize:(NSSize)oldSize {
NSSize size=[self bounds].size;
NSPoint origin=[self bounds].origin;
int i,count=[_subviews count];
if(size.width<1) size.width=1;
if(size.height<1) size.height=1;
if(oldSize.width<1) oldSize.width=1;
if(oldSize.height<1) oldSize.height=1;
if([_delegate respondsToSelector:@selector(splitView:resizeSubviewsWithOldSize:)]) {
[_delegate splitView:self resizeSubviewsWithOldSize:oldSize];
} else {
// Apple docs say just call adjustSubviews
[self adjustSubviews];
}
}
-(NSRect)dividerRectAtIndex:(unsigned)index {
NSRect rect=[[_subviews objectAtIndex:index] frame];
if([self isVertical]){
rect.origin.x=rect.origin.x+rect.size.width;
rect.size.width=[self dividerThickness];
}
else {
rect.origin.y=rect.origin.y+rect.size.height;
rect.size.height=[self dividerThickness];
}
return rect;
}
-(void)drawRect:(NSRect)rect {
int i,count=[_subviews count];
for(i=0;i<count-1;i++){
if ([self dividerThickness] > 0)
[self drawDividerInRect:[self dividerRectAtIndex:i]];
}
}
-(unsigned)dividerIndexAtPoint:(NSPoint)point {
int i,count=[[self subviews] count];
for(i=0;i<count-1;i++){
NSRect rect=[self dividerRectAtIndex:i];
if(NSMouseInRect(point,rect,[self isFlipped]))
return i;
}
return NSNotFound;
}
static float constrainTo(float value,float min,float max){
if(value<min)
value=min;
if(value>max)
value=max;
return value;
}
-(void)mouseDown:(NSEvent *)event {
NSPoint firstPoint=[self convertPoint:[event locationInWindow] fromView:nil];
unsigned divider=[self dividerIndexAtPoint:firstPoint];
if (divider == NSNotFound) {
return;
}
NSEventType eventType;
[self _postNoteWillResize];
do{
NSAutoreleasePool *pool=[NSAutoreleasePool new];
NSPoint point;
event=[[self window] nextEventMatchingMask:NSLeftMouseUpMask|
NSLeftMouseDraggedMask];
eventType=[event type];
point=[self convertPoint:[event locationInWindow] fromView:nil];
if([self isVertical]){
[self setPosition: point.x ofDividerAtIndex: divider];
} else {
[self setPosition: point.y ofDividerAtIndex: divider];
}
[pool release];
}while(eventType!=NSLeftMouseUp);
if ([self dividerThickness] > 0)
[[self window] invalidateCursorRectsForView:self];
[self _postNoteDidResize];
}
-(void)resetCursorRects {
if ([self dividerThickness] <= 0)
return;
int i,count=[_subviews count];
NSCursor *cursor;
if([self isVertical])
cursor=[NSCursor resizeLeftRightCursor];
else
cursor=[NSCursor resizeUpDownCursor];
for(i=0;i<count-1;i++){
NSRect rect=[self dividerRectAtIndex:i];
// FIX
// The cursor is activated one pixel past NSMaxY(rect) if we don't do
// this, not sure where the problem is.
rect.origin.y-=1.;
[self addCursorRect:rect cursor:cursor];
}
}
- (float)minPossiblePositionOfDividerAtIndex:(int)index
{
NSUnimplementedMethod();
return 0;
}
- (float)maxPossiblePositionOfDividerAtIndex:(int)index
{
NSUnimplementedMethod();
return 0;
}
/** adjusts the subviews on either side of the divider while
* honoring the constraints imposed by the delegate (if any)
*/
- (void)setPosition:(float)position ofDividerAtIndex:(int)index
{
NSAssert(index >= 0 && index < [[self subviews] count] - 1, @"divider index out of range");
NSView *subview0 = [[self subviews] objectAtIndex: index];
NSView *subview1 = [[self subviews] objectAtIndex: index + 1];
BOOL subview0Expanded = [self isSubviewCollapsed: subview0] == NO;
BOOL subview1Expanded = [self isSubviewCollapsed: subview1] == NO;
float minPosition = 0;
float maxPosition = 0;
NSRect frame0 = NSZeroRect;
NSRect frame1 = NSZeroRect;
if (subview0Expanded) {
frame0 = [subview0 frame];
}
if (subview1Expanded) {
frame1 = [subview1 frame];
}
// Determine the minimum position
if (subview0Expanded) {
if ([self isVertical]) {
minPosition = NSMinX(frame0);
} else {
minPosition = NSMinY(frame0);
}
} else {
NSAssert(subview1Expanded, @"both are collapsed??");
if ([self isVertical]) {
minPosition = NSMinX(frame1);
} else {
minPosition = NSMinY(frame1);
}
}
// Determine the maximum position
if (subview1Expanded) {
if ([self isVertical]) {
maxPosition = NSMaxX(frame1);
} else {
maxPosition = NSMaxY(frame1);
}
} else {
NSAssert(subview0Expanded, @"both are collapsed??");
if ([self isVertical]) {
maxPosition = NSMaxX(frame0);
} else {
maxPosition = NSMaxY(frame0);
}
}
// Check in with the delegate and see if it wants to tweak the min and max
if ([_delegate respondsToSelector: @selector(splitView:constrainMinCoordinate:ofSubviewAt:)] ||
[_delegate respondsToSelector: @selector(splitView:constrainMaxCoordinate:ofSubviewAt:)]) {
// Use the modern API
if ([_delegate respondsToSelector: @selector(splitView:constrainMinCoordinate:ofSubviewAt:)]) {
minPosition = [_delegate splitView: self constrainMinCoordinate: minPosition ofSubviewAt: index];
}
if ([_delegate respondsToSelector: @selector(splitView:constrainMaxCoordinate:ofSubviewAt:)]) {
maxPosition = [_delegate splitView: self constrainMaxCoordinate: maxPosition ofSubviewAt: index];
}
}
else if([_delegate respondsToSelector:@selector(splitView:constrainMinCoordinate:maxCoordinate:ofSubviewAt:)]) {
// Use the deprecated API
[_delegate splitView:self constrainMinCoordinate:&minPosition maxCoordinate:&maxPosition ofSubviewAt: index];
}
// And if it wants to constrain the divider position
BOOL delegateWantsTrackConstraining = [_delegate respondsToSelector:@selector(splitView:constrainSplitPosition:ofSubviewAt:)];
if(delegateWantsTrackConstraining) {
position = [_delegate splitView:self constrainSplitPosition: position ofSubviewAt: index];
}
// OK we're ready to figure out where the divider can be positioned
NSRect resize0=frame0;
NSRect resize1=frame1;
BOOL subviewsWereCollapsedOrExpanded = NO;
BOOL checkWithDelegateAboutCollapsingViews = [_delegate respondsToSelector:@selector(splitView:canCollapseSubview:)];
if([self isVertical]){
float lastPosition = NSMaxX(resize0);
float delta = floor(position - lastPosition);
resize0.size.width += delta;
resize1.size.width -= delta;
if (checkWithDelegateAboutCollapsingViews) {
if (position < minPosition) {
if ([_delegate splitView: self canCollapseSubview: subview0]) {
[subview0 setHidden: YES];
subviewsWereCollapsedOrExpanded = YES;
}
} else if (position > maxPosition) {
if ([_delegate splitView: self canCollapseSubview: subview1]) {
[subview1 setHidden: YES];
subviewsWereCollapsedOrExpanded = YES;
}
}
}
// But make sure collapsed views can reappear
if (position > minPosition && [subview0 isHidden]) {
[subview0 setHidden: NO];
subviewsWereCollapsedOrExpanded = YES;
} else if (position < maxPosition && [subview1 isHidden]) {
[subview1 setHidden: NO];
subviewsWereCollapsedOrExpanded = YES;
}
// Figure out the adjusted widths
resize0.size.width = constrainTo(NSWidth(resize0), minPosition, maxPosition);
resize1.size.width = constrainTo(NSWidth(resize1), minPosition, maxPosition);
resize1.origin.x = (NSMinX(frame1) + NSWidth(frame1)) - NSWidth(resize1);
}
else {
float lastPosition = NSMaxY(resize0);
float delta = floor(position - lastPosition);
resize0.size.height += delta;
resize1.size.height -= delta;
if (checkWithDelegateAboutCollapsingViews) {
if (position < minPosition) {
if ([_delegate splitView: self canCollapseSubview: subview0]) {
[subview0 setHidden: YES];
subviewsWereCollapsedOrExpanded = YES;
}
} else if (position > maxPosition) {
if ([_delegate splitView: self canCollapseSubview: subview1]) {
[subview1 setHidden: YES];
subviewsWereCollapsedOrExpanded = YES;
}
}
}
// But make sure collapsed views can reappear
if (position > minPosition && [subview0 isHidden]) {
[subview0 setHidden: NO];
subviewsWereCollapsedOrExpanded = YES;
} else if (position < maxPosition && [subview1 isHidden]) {
[subview1 setHidden: NO];
subviewsWereCollapsedOrExpanded = YES;
}
// Figure out the adjusted heights
resize0.size.height = constrainTo(NSHeight(resize0), minPosition, maxPosition);
resize1.size.height = constrainTo(NSHeight(resize1), minPosition, maxPosition);
resize1.origin.y = (NSMinY(frame1) + NSHeight(frame1)) - NSHeight(resize1);
}
if (subviewsWereCollapsedOrExpanded) {
// It doesn't really matter what happened with the divider because we need
// to get the views re-laid out - so fall back to adjustSubviews and bail
[self adjustSubviews];
return;
}
// Nothing special happened so just resize the subviews as expected
if ([subview0 isHidden] == NO) {
[subview0 setFrame: resize0];
// Tell the view to redisplay otherwise there are drawing artifacts
[subview0 setNeedsDisplay: YES];
}
if ([subview1 isHidden] == NO) {
[subview1 setFrame: resize1];
// Tell the view to redisplay otherwise there are drawing artifacts
[subview1 setNeedsDisplay: YES];
}
[self setNeedsDisplay:YES];
}
@end