forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKFSplitView.m
executable file
·1137 lines (961 loc) · 39.1 KB
/
KFSplitView.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// KFSplitView.m
// KFSplitView v. 1.3, 11/27/2004
//
// Copyright (c) 2003-2004 Ken Ferry. Some rights reserved.
// http://homepage.mac.com/kenferry/software.html
//
// Other contributors: Kirk Baker, John Pannell
//
// This work is licensed under a Creative Commons license:
// http://creativecommons.org/licenses/by-nc/1.0/
//
// Send me an email if you have any problems (after you've read what there is to read).
//
// You can reach me at kenferry at the domain mac.com.
//
// On this whole major axis, minor axis thing:
//
// The 'major' axis refers to the direction in which dividers can move.
// It's the y-axis when [self isVertical] returns NO, and the x-axis otherwise.
// Pretty much everything that uses coordinates or dimensions in this file works
// more comfortably in that coordinate system.
//
// Other
//
// This class is a basically a complete reimplementation of NSSplitView. The
// underlying NSSplitView is mostly used for drawing dividers.
#import "KFSplitView.h"
#pragma mark File-level and global vars:
const NSPoint KFOffScreenPoint = {1000000.0,1000000.0};
static NSMutableSet *kfInUsePositionNames;
#pragma mark Utility:
// these are macros (instead of inlines) so that we can use the instance variable kfIsVertical
// they're undef'd at the bottom of the file
#define KFMAJORCOORDOFPOINT(point) (kfIsVertical ? (point).x : (point).y)
#define KFMINORCOORDOFPOINT(point) (kfIsVertical ? (point).y : (point).x)
#define KFMAJORDIMOFSIZE(size) (kfIsVertical ? (size).width : (size).height)
#define KFMINORDIMOFSIZE(size) (kfIsVertical ? (size).height : (size).width)
#define KFPOINTWITHMAJMIN(major, minor) (kfIsVertical ? NSMakePoint((major), (minor)) : NSMakePoint((minor), (major)))
#define KFSIZEWITHMAJMIN(major, minor) (kfIsVertical ? NSMakeSize((major), (minor)) : NSMakeSize((minor), (major)))
#define KFMAX(a,b) ((a)>(b)?(a):(b))
// proportionally scale a list of integers so that the sum of the resulting list is targetTotal
// Will fail (return NO) if all integers are zero
// Favors not completely zeroing out a nonzero int
static BOOL kfScaleUInts(unsigned *integers, int numInts, unsigned targetTotal)
{
unsigned total;
float scalingFactor;
int i, numNonZeroInts;
// compute total
total = 0;
numNonZeroInts = 0;
for (i = 0; i < numInts; i++)
{
if (integers[i] != 0)
{
total += integers[i];
numNonZeroInts++;
}
}
if (numNonZeroInts == 0) // fail
{
return NO;
}
// compute scalingFactor
scalingFactor = (float)targetTotal / total;
// scale all ints and recompute total (which may not equal targetTotal due to roundoff error)
total = 0;
for (i = 0; i < numInts; i++)
{
if (integers[i] != 0)
{
// this is preferable to rounding when used for subviews - helps
// prevent a subview getting stuck at thickness 1 during a drag resize
integers[i] = MAX(floor(scalingFactor*integers[i]), 1);
total += integers[i];
}
}
// Each non-zero integer may be as much as 1 off of its "proper" floating point value due to roundoff,
// so abs(targetTotal - total) might be as much as numNonZero. We randomly choose integers to increment (or decrement)
// to make up the gap, and we choose only from the non-zero values.
int gap = abs(targetTotal - total);
int closeGapIncrement = (targetTotal > total) ? 1 : -1;
int numRemainingNonZeroInts = numNonZeroInts;
for (i = 0; i < numInts && gap > 0; i++)
{
if (integers[i] > 0)
{
BOOL shouldIncrementInt = (gap == numRemainingNonZeroInts) || (rand() < (float) gap / numRemainingNonZeroInts * RAND_MAX);
if (shouldIncrementInt)
{
integers[i] += closeGapIncrement;
gap--;
}
numRemainingNonZeroInts--;
}
}
return YES;
}
@interface KFSplitView (kfPrivate)
+ (NSString *)kfDefaultsKeyForName:(NSString *)name;
- (void)kfSetup;
- (void)kfSetupResizeCursors;
- (int)kfGetDividerAtMajCoord:(float)coord;
- (void)kfPutDivider:(int)offset atMajCoord:(float)coord;
- (void)kfRecalculateDividerRects;
- (void)kfMoveCollapsedSubviewsOffScreen;
- (void)kfSavePositionUsingAutosaveName:(id)sender;
- (void)kfLayoutSubviewsUsingThicknesses:(unsigned *)subviewThicknesses;
@end
@implementation KFSplitView
/*****************
* Initialization
*****************/
#pragma mark Setup/teardown:
+ (void)initialize
{
kfInUsePositionNames = [[NSMutableSet alloc] init];
}
- initWithFrame:(NSRect)frameRect
{
if (self = [super initWithFrame:frameRect])
{
[self kfSetup];
}
return self;
}
- initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder])
{
[self kfSetup];
}
return self;
}
- (void)kfSetup
{
// be sure to setup cursors before calling setVertical:
[self kfSetupResizeCursors];
kfCollapsedSubviews = [[NSMutableSet alloc] init];
kfDividerRects = [[NSMutableArray alloc] init];
kfDefaults = [NSUserDefaults standardUserDefaults];
kfNotificationCenter = [NSNotificationCenter defaultCenter];
[self setVertical:[self isVertical]];
}
// Attempts to find cursors to use as kfIsVerticalResizeCursor and kfNotIsVerticalResizeCursor.
// These cursors are eventually released, so make sure each receives a retain message now.
// If no good cursors can be found, an error is printed and the arrow cursor is used.
- (void)kfSetupResizeCursors
{
NSImage *isVerticalImage, *isNotVerticalImage;
if ((isVerticalImage = [NSImage imageNamed:@"NSTruthHorizontalResizeCursor"])); // standard Jaguar NSSplitView resize cursor
else if((isVerticalImage = [NSImage imageNamed:@"NSTruthHResizeCursor"]))
{
}
if (isVerticalImage)
{
kfIsVerticalResizeCursor = [[NSCursor alloc] initWithImage:isVerticalImage
hotSpot:NSMakePoint(8,8)];
}
if((isNotVerticalImage = [NSImage imageNamed:@"NSTruthVerticalResizeCursor"])); // standard Jaguar NSSplitView resize cursor
else if( (isNotVerticalImage = [NSImage imageNamed:@"NSTruthVResizeCursor"]))
{
}
if (isNotVerticalImage)
{
kfNotIsVerticalResizeCursor = [[NSCursor alloc] initWithImage:isNotVerticalImage
hotSpot:NSMakePoint(8,8)];
}
if (kfIsVerticalResizeCursor == nil)
{
kfIsVerticalResizeCursor = [[NSCursor arrowCursor] retain];
NSLog(@"Warning - no horizontal resizing cursor located. Please report this as a bug.");
}
if (kfNotIsVerticalResizeCursor == nil)
{
kfNotIsVerticalResizeCursor = [[NSCursor arrowCursor] retain];
NSLog(@"Warning - no vertical resizing cursor located. Please report this as a bug.");
}
}
- (void)awakeFromNib
{
[self kfRecalculateDividerRects];
}
- (void)dealloc
{
[self setDelegate:nil];
[self setPositionAutosaveName:@""];
[kfCollapsedSubviews release];
[kfDividerRects release];
[kfIsVerticalResizeCursor release];
[kfNotIsVerticalResizeCursor release];
[super dealloc];
}
/******************
* Main processing
******************/
#pragma mark Main processing:
- (void)mouseDown:(NSEvent *)theEvent
{
// All coordinates are major axis coordinates unless otherwise specified. See the top of the file
// for an explanation of major and minor axes.
float minorDim; // common dimension of all subviews
NSInteger divider; // index of a divider being dragged
float mouseCoord, mouseToDividerOffset; // the mouse holds on to whatever part of the divider it grabs onto
float dividerThickness;
float dividerCoord, prevDividerCoord;
float hardMinCoord, hardMaxCoord; // absolute boundaries for dividerCoord
float delMinCoord, delMaxCoord; // boundaries for dividerCoord according to the delegate (not absolute)
NSView *firstSubview, *secondSubview; // subviews above and below the divider (if !isVertical)
float firstSubviewMinCoord, secondSubviewMaxCoord; // top of the first, bottom of the second (if !isVertical)
BOOL firstSubviewCanCollapse, secondSubviewCanCollapse;
NSDate *distantFuture;
float (*splitPosConstraintFunc)(id, SEL, ...); // delegate supplied function to constrain dividerCoord
// setup
minorDim = KFMINORDIMOFSIZE([self frame].size);
dividerThickness = [self dividerThickness];
distantFuture = [NSDate distantFuture];
// PRECOMPUTATION - we do as much as we can before starting the event loop.
// figure out which divider is being dragged
mouseCoord = KFMAJORCOORDOFPOINT([self convertPoint:[theEvent locationInWindow] fromView:nil]);
divider = [self kfGetDividerAtMajCoord:mouseCoord];
if (divider == NSNotFound)
{
return;
}
// if the event is a double click we let the delegate deal with it
if ([theEvent clickCount] > 1)
{
if ([kfDelegate respondsToSelector:@selector(splitView:didDoubleClickInDivider:)])
{
[kfDelegate splitView:self didDoubleClickInDivider:divider];
return;
}
}
// firstSubview is the subview above (left) of the divider
// secondSubview is the subview below (right) of the divider
firstSubview = [[self subviews] objectAtIndex:divider];
secondSubview = [[self subviews] objectAtIndex:divider+1];
// set firstSubviewMinCoord and secondSubviewMaxCoord. Here's a little diagram:
// ------------ <- firstSubviewMinCoord
//
//
//
//
// ------------ <- dividerCoord (not set yet)
// ------------
//
//
// ------------ <- secondSubviewMaxCoord
if (![self isSubviewCollapsed:firstSubview])
{
firstSubviewMinCoord = KFMAJORCOORDOFPOINT([firstSubview frame].origin);
}
else
{
firstSubviewMinCoord = KFMAJORCOORDOFPOINT([[kfDividerRects objectAtIndex:divider] rectValue].origin);
}
if (![self isSubviewCollapsed:secondSubview])
{
secondSubviewMaxCoord = KFMAJORCOORDOFPOINT([secondSubview frame].origin) + KFMAJORDIMOFSIZE([secondSubview frame].size);
}
else
{
secondSubviewMaxCoord = KFMAJORCOORDOFPOINT([[kfDividerRects objectAtIndex:divider] rectValue].origin) + dividerThickness;
}
// hardMinCoord and hardMaxCoord are the absolute minimum and maximum values that may be
// assigned to dividerCoord. delMinCoord and delMaxCoord are minimum and maximum values
// for dividerCoord that are supplied by the delegate. These last are _not_ absolute: if the
// delegate allows collapsing of subviews then dividerCoord can snap from delMinCoord to
// hardMinCoord if the user drags the divider more than halfway across the region between them.
// See Apple's NSSplitView documenation under - splitView:canCollapseSubview:.
hardMinCoord = firstSubviewMinCoord;
hardMaxCoord = secondSubviewMaxCoord - dividerThickness;
delMinCoord = hardMinCoord;
delMaxCoord = hardMaxCoord;
if ([kfDelegate respondsToSelector:@selector(splitView:constrainMinCoordinate:ofSubviewAt:)])
{
delMinCoord = [kfDelegate splitView:self
constrainMinCoordinate:delMinCoord
ofSubviewAt:divider];
}
if ([kfDelegate respondsToSelector:@selector(splitView:constrainMaxCoordinate:ofSubviewAt:)])
{
delMaxCoord = [kfDelegate splitView:self
constrainMaxCoordinate:delMaxCoord
ofSubviewAt:divider];
}
delMinCoord = (delMinCoord < hardMinCoord) ? hardMinCoord : delMinCoord;
delMaxCoord = (delMaxCoord > hardMaxCoord) ? hardMaxCoord : delMaxCoord;
if (delMinCoord > delMaxCoord)
{
// this follows apple's implementation. It says that if the delegate does
// not supply any zone where the divider can sit without collapsing a subview then
// ignore the delegate. The other option would be to always collapse to one subview
// or the other, if one or both of the subviews are collasible. That could be a bit of a UI
// problem, because the user could try to drag a subview and have nothing happen.
delMinCoord = hardMinCoord;
delMaxCoord = hardMaxCoord;
}
firstSubviewCanCollapse = NO;
secondSubviewCanCollapse = NO;
if ([kfDelegate respondsToSelector:@selector(splitView:canCollapseSubview:)])
{
firstSubviewCanCollapse = [kfDelegate splitView:self canCollapseSubview:firstSubview];
secondSubviewCanCollapse = [kfDelegate splitView:self canCollapseSubview:secondSubview];
}
// The delegate may constrain the possible values for dividerCoord.
// Since this method will be called repeatedly we cache a pointer to it.
splitPosConstraintFunc = NULL;
if ([kfDelegate respondsToSelector:@selector(splitView:constrainSplitPosition:ofSubviewAt:)])
{
splitPosConstraintFunc = (float (*)(id, SEL, ...))[kfDelegate methodForSelector:@selector(splitView:constrainSplitPosition:ofSubviewAt:)];
}
// When the user grabs and drags the divider he holds onto that
// particular spot while dragging.
// mouseToDividerOffset is the difference between dividerCoord (the top of
// the divider) and mouseCoord.
mouseToDividerOffset = KFMAJORCOORDOFPOINT([[kfDividerRects objectAtIndex:divider] rectValue].origin) - mouseCoord;
// EVENT-LOOP
prevDividerCoord = 1000000; // something non-sensical
do
{
mouseCoord = KFMAJORCOORDOFPOINT([self convertPoint:[theEvent locationInWindow] fromView:nil]);
dividerCoord = mouseCoord + mouseToDividerOffset;
if (splitPosConstraintFunc != NULL)
{
dividerCoord = (*splitPosConstraintFunc)(kfDelegate,
@selector(splitView:constrainSplitPosition:ofSubviewAt:),
self,
dividerCoord,
divider);
}
// There are five regions where user may have dragged the divider:
// collapse first subview
// stick the divider to delMinCoord
// move freely
// stick the divider to delMaxCoord
// collapse the second subview
if ( hardMinCoord == hardMaxCoord )
{
// special case: divider is pinned. It is possible to collapse both subviews.
[self setSubview:firstSubview isCollapsed:firstSubviewCanCollapse];
[self setSubview:secondSubview isCollapsed:secondSubviewCanCollapse];
dividerCoord = hardMinCoord;
}
else if ( firstSubviewCanCollapse &&
dividerCoord < hardMinCoord + (delMinCoord - hardMinCoord)/2)
{
// collapse first subview
[self setSubview:secondSubview isCollapsed:NO];
[self setSubview:firstSubview isCollapsed:YES];
dividerCoord = hardMinCoord;
}
else if ( dividerCoord < delMinCoord )
{
// stick to delMinCoord
[self setSubview:firstSubview isCollapsed:NO];
[self setSubview:secondSubview isCollapsed:NO];
dividerCoord = delMinCoord;
}
else if ( dividerCoord < delMaxCoord )
{
// move freely
[self setSubview:firstSubview isCollapsed:NO];
[self setSubview:secondSubview isCollapsed:NO];
}
else if ( !secondSubviewCanCollapse ||
dividerCoord < hardMaxCoord - (hardMaxCoord - delMaxCoord)/2 )
{
// stick to delMaxCoord
[self setSubview:firstSubview isCollapsed:NO];
[self setSubview:secondSubview isCollapsed:NO];
dividerCoord = delMaxCoord;
}
else
{
// collapse second subview
[self setSubview:firstSubview isCollapsed:NO];
[self setSubview:secondSubview isCollapsed:YES];
dividerCoord = hardMaxCoord;
}
if (prevDividerCoord != dividerCoord)
{
// Position and resize elements. A collapsing subview's frame size doesn't change,
// the subview just gets moved way offscreen (as in NSSplitView).
// The diagram may help:
//
// ------------ <- firstSubviewMinCoord
//
//
//
// ------------ <- dividerCoord
// ------------ <- dividerCoord + dividerThickness
//
//
// ------------ <- secondSubviewMaxCoord
[kfNotificationCenter postNotificationName:NSSplitViewWillResizeSubviewsNotification object:self];
// divider
[self kfPutDivider:divider atMajCoord:dividerCoord];
// firstSubview
if (![self isSubviewCollapsed:firstSubview])
{
NSRect newFrame;
newFrame.origin = KFPOINTWITHMAJMIN(firstSubviewMinCoord,0);
newFrame.size = KFSIZEWITHMAJMIN(dividerCoord - firstSubviewMinCoord, minorDim);
if (!NSEqualRects([firstSubview frame],newFrame))
{
[firstSubview setFrame:newFrame];
[firstSubview setNeedsDisplay:YES];
}
}
else
{
[firstSubview setFrameOrigin:KFOffScreenPoint];
}
// secondSubview
if (![self isSubviewCollapsed:secondSubview])
{
NSRect newFrame;
newFrame.origin = KFPOINTWITHMAJMIN(dividerCoord + dividerThickness, 0);
newFrame.size = KFSIZEWITHMAJMIN(secondSubviewMaxCoord - (dividerCoord + dividerThickness), minorDim);
if (!NSEqualRects([secondSubview frame],newFrame))
{
[secondSubview setFrame:newFrame];
[secondSubview setNeedsDisplay:YES];
}
}
else
{
[secondSubview setFrameOrigin:KFOffScreenPoint];
}
[kfNotificationCenter postNotificationName:NSSplitViewDidResizeSubviewsNotification object:self];
prevDividerCoord = dividerCoord;
}
// get the next relevant event
theEvent = [NSApp nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask
untilDate:distantFuture
inMode:NSEventTrackingRunLoopMode
dequeue:YES];
} while ([theEvent type] == NSLeftMouseDragged);
// inform delegate that user has finished dragging divider
if ([kfDelegate respondsToSelector:@selector(splitView:didFinishDragInDivider:)])
{
[kfDelegate splitView:self didFinishDragInDivider:divider];
}
}
// Call this method to retile the subviews, not adjustSubviews.
// It 1) dispatches will and did resize subviews notifications
// 2) calls the appropriate method to do the retiling. That's a method of
// the delegate if it has one and the default adjustSubviews otherwise.
// 3) cleans up some other layout, like divider positions
- (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize
{
NSDisableScreenUpdates();
[kfNotificationCenter postNotificationName:NSSplitViewWillResizeSubviewsNotification object:self];
if ([kfDelegate respondsToSelector:@selector(splitView:resizeSubviewsWithOldSize:)])
{
[kfDelegate splitView:self resizeSubviewsWithOldSize:oldBoundsSize];
}
else
{
[self adjustSubviews];
}
[self kfRecalculateDividerRects];
[self kfMoveCollapsedSubviewsOffScreen];
[kfNotificationCenter postNotificationName:NSSplitViewDidResizeSubviewsNotification object:self];
NSEnableScreenUpdates();
}
// See Apple's NSSplitView docs. However, note that in general you want to call
// resizeSubviewsWithOldSize:, not this method. The exception is that you might
// want to call adjustSubviews from splitView:resizeSubviewsWithOldSize: in the
// the delegate
- (void)adjustSubviews
{
int i, numSubviews;
NSArray *subviews;
// The 'thickness' of a subview will mean the amount of space along
// the major axis that the subview occupies in the splitview.
// We work in integral values, though actual thicknesses are floats.
// In the current OS, the floats actually have integral values.
//
// Ex 1: The thickness of a collapsed subview is 0.
// Ex 2: For an uncollapsed subview in a horizontal (standard direction) splitview,
// thickness means height.
unsigned *subviewThicknesses;
// setup
subviews = [self subviews];
numSubviews = [subviews count];
if (numSubviews == 0)
{
return;
}
subviewThicknesses = malloc(sizeof(unsigned)*numSubviews);
// Fill out subviewThicknesses array.
// Also keep track of the total thickness of all subviews, and
// of the first expanded subview
unsigned totalSubviewThicknesses = 0;
NSInteger firstExpandedSubviewIndex = NSNotFound;
for (i = 0; i < numSubviews; i++)
{
NSView *subview = [subviews objectAtIndex:i];
if (![self isSubviewCollapsed:subview])
{
subviewThicknesses[i] = floor(KFMAJORDIMOFSIZE([subview frame].size));
totalSubviewThicknesses += subviewThicknesses[i];
if (firstExpandedSubviewIndex == NSNotFound) { firstExpandedSubviewIndex = i; }
}
else
{
subviewThicknesses[i] = 0;
}
}
// Compute new thicknesses for subviews.
// In the end, the subview thicknesses should sum to the thickness of the splitview minus the space occupied by dividers.
unsigned targetTotalSubviewsThickness = KFMAX(floor(KFMAJORDIMOFSIZE([self frame].size) - [self dividerThickness]*(numSubviews - 1)), 0);
// If at least one of the subviews has positive thickness
if (totalSubviewThicknesses != 0)
{
// then we can scale all the thicknesses
kfScaleUInts(subviewThicknesses, numSubviews, targetTotalSubviewsThickness);
}
else // otherwise we'll have to expand one of the subviews to fill the entire space
{
if (firstExpandedSubviewIndex != NSNotFound)
{
subviewThicknesses[firstExpandedSubviewIndex] = targetTotalSubviewsThickness;
}
else
{
subviewThicknesses[0] = targetTotalSubviewsThickness;
}
}
// layout subviews
[self kfLayoutSubviewsUsingThicknesses:subviewThicknesses];
// cleanup
free(subviewThicknesses);
}
// Required: Sum of all subviewThicknesses <= splitViewThickness - dividersThickness.
// If the splitview has positive available space for subviews, then one of the supplied subview thicknesses
// must also be positive. Extra space will be dumped into the last subview with positive thickness.
// See adjustSubviews for the definition of 'thickness'.
//
// Does not currently put collapsed subviews off screen or do divider placement.
// Could be done efficiently here, but would duplicate functionality of other methods.
- (void)kfLayoutSubviewsUsingThicknesses:(unsigned *)subviewThicknesses
{
NSInteger i, lastPositiveThicknessSubviewIndex, numSubviews;
float minorDimOfSplitViewSize;
float curMajAxisPos, dividerThickness;
NSArray *subviews;
// setup
subviews = [self subviews];
numSubviews = [subviews count];
minorDimOfSplitViewSize = KFMINORDIMOFSIZE([self frame].size);
dividerThickness = [self dividerThickness];
// Compute lastPositiveThicknessSubviewIndex.
lastPositiveThicknessSubviewIndex = NSNotFound;
for (i = numSubviews - 1; i >= 0; i--)
{
if (subviewThicknesses[i] != 0)
{
lastPositiveThicknessSubviewIndex = i;
break;
}
}
// We walk down the major axis, setting subview frames as we go.
curMajAxisPos = 0;
for (i = 0; i < numSubviews; i++)
{
NSView *subview = [subviews objectAtIndex:i];
float newSubviewThickness = -1; // sentinel value, meaning "do not change"
if (subviewThicknesses[i] == 0) // If subview should have no thickness
{
// then shrink its frame if it is uncollapsed.
if (![self isSubviewCollapsed:subview])
{
newSubviewThickness = 0;
}
}
else // If supplied thickness is positive
{
// make sure the subview isn't collapsed.
if ([self isSubviewCollapsed:subview])
{
[self setSubview:subview isCollapsed:NO];
}
// If this is the last subview that we're going to give a positive thickness
if (i == lastPositiveThicknessSubviewIndex)
{
// we overrule the given the given value and just fill all available area.
float remainingDividersThickness = (numSubviews - 1 - i)*dividerThickness;
float splitViewThickness = KFMAJORDIMOFSIZE([self frame].size);
newSubviewThickness = KFMAX(splitViewThickness - curMajAxisPos - remainingDividersThickness, 0);
}
else // If this isn't the last subview that we're going to set to a positive thickness
{
// use the supplied thickness.
newSubviewThickness = subviewThicknesses[i];
}
}
// If we found a new subview thickness
if (newSubviewThickness != -1)
{
// set the subview's frame accordingly
NSPoint newSubviewOrigin = KFPOINTWITHMAJMIN(curMajAxisPos, 0);
NSSize newSubviewSize = KFSIZEWITHMAJMIN(newSubviewThickness, minorDimOfSplitViewSize);
NSRect newFrame = NSMakeRect(newSubviewOrigin.x, newSubviewOrigin.y,
newSubviewSize.width, newSubviewSize.height);
if (!NSEqualRects([subview frame],newFrame))
{
[subview setFrame:newFrame];
[subview setNeedsDisplay:YES];
}
// and advance down the major axis.
curMajAxisPos += newSubviewThickness;
}
// Account for divider thickness.
if (i < numSubviews - 1)
{
curMajAxisPos += dividerThickness;
}
}
}
- (void)kfMoveCollapsedSubviewsOffScreen
{
NSView *subview;
for (subview in kfCollapsedSubviews)
{
[subview setFrameOrigin:KFOffScreenPoint];
}
}
- (void)drawRect:(NSRect)rect
{
int i, numDividers;
numDividers = [kfDividerRects count];
for (i = 0; i < numDividers; i++)
{
[self drawDividerInRect:[[kfDividerRects objectAtIndex:i] rectValue]];
}
}
// returns the index ('offset' in Apple's docs) of the divider under the
// given coordinate, or NSNotFound if there isn't a divider there.
- (int)kfGetDividerAtMajCoord:(float)coord
{
NSInteger i, numDividers, result;
float curDividerMinimumMajorCoord, dividerThickness;
numDividers = [kfDividerRects count];
result = NSNotFound;
dividerThickness = [self dividerThickness];
for (i = 0; i < numDividers; i++)
{
curDividerMinimumMajorCoord = KFMAJORCOORDOFPOINT([[kfDividerRects objectAtIndex:i] rectValue].origin);
if (curDividerMinimumMajorCoord <= coord && coord < curDividerMinimumMajorCoord + dividerThickness)
{
result = i;
break;
}
}
return result;
}
- (void)kfPutDivider:(int)offset atMajCoord:(float)coord
{
NSPoint newOrigin;
NSSize newSize;
NSRect newFrame;
while ([kfDividerRects count] <= offset)
{
[kfDividerRects addObject:[NSValue valueWithRect:NSZeroRect]];
}
newOrigin = KFPOINTWITHMAJMIN(coord,0);
newSize = KFSIZEWITHMAJMIN([self dividerThickness], KFMINORDIMOFSIZE([self frame].size));
newFrame = NSMakeRect(newOrigin.x, newOrigin.y, newSize.width, newSize.height);
if (!NSEqualRects([[kfDividerRects objectAtIndex:offset] rectValue], newFrame))
{
[kfDividerRects replaceObjectAtIndex:offset withObject:[NSValue valueWithRect:newFrame]];
[self setNeedsDisplayInRect:newFrame];
[[[self subviews] objectAtIndex:offset] setNeedsDisplay:YES];
[[[self subviews] objectAtIndex:offset+1] setNeedsDisplay:YES];
}
}
// positions all dividers based on the current location of the subviews
- (void)kfRecalculateDividerRects
{
float curMajAxisPos, dividerThickness;
id subview, subviews;
int numSubviews, i;
dividerThickness = [self dividerThickness];
subviews = [self subviews];
numSubviews = [subviews count];
curMajAxisPos = 0;
for (i = 0; i < numSubviews - 1; i++)
{
subview = [subviews objectAtIndex:i];
if (![self isSubviewCollapsed:subview])
{
curMajAxisPos += KFMAJORDIMOFSIZE([subview frame].size);
}
[self kfPutDivider:i atMajCoord:curMajAxisPos];
curMajAxisPos += dividerThickness;
}
int numDividerRects = [kfDividerRects count];
if (numDividerRects > numSubviews - 1)
{
[kfDividerRects removeObjectsInRange:NSMakeRange(numSubviews-1,numDividerRects - numSubviews + 1)];
}
[[self window] invalidateCursorRectsForView:self];
}
- (void)resetCursorRects
{
int i, numDividers;
numDividers = [kfDividerRects count];
for (i = 0; i < numDividers; i++)
{
[self addCursorRect:[[kfDividerRects objectAtIndex:i] rectValue]
cursor:kfCurrentResizeCursor];
}
}
/******************
* Accessors
******************/
- (void)setVertical:(BOOL)flag
{
[super setVertical:flag];
kfIsVertical = flag;
if (kfIsVertical)
{
kfCurrentResizeCursor = kfIsVerticalResizeCursor;
}
else
{
kfCurrentResizeCursor = kfNotIsVerticalResizeCursor;
}
}
- (id)delegate
{
return kfDelegate;
}
// automatically registers the delegate for relevant notifications, and unregisters
// the old delegate for those same notifications.
- (void)setDelegate:(id)delegate
{
id delegateAutoRegNotifications, delegateMethodNames;
int i, numAutoRegNotifications;
SEL methodSelector;
delegateAutoRegNotifications = [NSArray arrayWithObjects:
NSSplitViewWillResizeSubviewsNotification,
NSSplitViewDidResizeSubviewsNotification,
KFSplitViewDidCollapseSubviewNotification,
KFSplitViewDidExpandSubviewNotification, nil];
delegateMethodNames = [NSArray arrayWithObjects:
@"splitViewWillResizeSubviews:",
@"splitViewDidResizeSubviews:",
@"splitViewDidCollapseSubview:",
@"splitViewDidExpandSubview:", nil];
numAutoRegNotifications = [delegateAutoRegNotifications count];
if (kfDelegate)
{
for (i = 0; i < numAutoRegNotifications; i++)
{
[kfNotificationCenter removeObserver:kfDelegate
name:[delegateAutoRegNotifications objectAtIndex:i]
object:self];
}
}
kfDelegate = delegate;
if (kfDelegate)
{
for (i = 0; i < numAutoRegNotifications; i++)
{
methodSelector = sel_registerName([[delegateMethodNames objectAtIndex:i] cString]);
if ([kfDelegate respondsToSelector:methodSelector])
{
[kfNotificationCenter addObserver:kfDelegate
selector:methodSelector
name:[delegateAutoRegNotifications objectAtIndex:i]
object:self];
}
}
}
}
- (BOOL)isSubviewCollapsed:(NSView *)subview
{
return [kfCollapsedSubviews containsObject:subview];
}
- (void)setSubview:(NSView *)subview isCollapsed:(BOOL)flag
{
NSDictionary *subviewDictionary;
if (flag != [self isSubviewCollapsed:subview])
{
subviewDictionary = [NSDictionary dictionaryWithObject:subview forKey:@"subview"];
if (flag)
{
[kfCollapsedSubviews addObject:subview];
[kfNotificationCenter postNotificationName:KFSplitViewDidCollapseSubviewNotification
object:self
userInfo:subviewDictionary];
}
else
{
[kfCollapsedSubviews removeObject:subview];
[kfNotificationCenter postNotificationName:KFSplitViewDidExpandSubviewNotification
object:self
userInfo:subviewDictionary];
}
}
}
/**********************
* Position saving
**********************/
#pragma mark Position saving:
// FOR DOCUMENTATION OF POSITION SAVING METHODS SEE APPLE'S NSWINDOW DOCS
+ (void)removePositionUsingName:(NSString *)name
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[[self class] kfDefaultsKeyForName:name]];
}
- (void)savePositionUsingName:(NSString *)name
{
NSString *key = [[self class] kfDefaultsKeyForName:name];
NSString *prop = [self plistObjectWithSavedPosition];
[kfDefaults setObject:prop forKey:key];
}
- (BOOL)setPositionUsingName:(NSString *)name
{
BOOL result;
id object;
object = [kfDefaults objectForKey:[[self class] kfDefaultsKeyForName:name]];
if (object)
{
[self setPositionFromPlistObject:object];
result = YES;
}
else
{
result = NO;
}
return result;
}
- (BOOL)setPositionAutosaveName:(NSString *)name
{
if ([name isEqualToString:@""])
{
name = nil;
}
if ([kfInUsePositionNames containsObject:name])
{
return NO;
}
if (kfPositionAutosaveName)
{
[kfInUsePositionNames removeObject:kfPositionAutosaveName];
[kfPositionAutosaveName autorelease];
}