/
HTMDSplitView.m
250 lines (206 loc) · 6.94 KB
/
HTMDSplitView.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
//
// HTMDSplitView.m
// MissingDrawer
#import "HTMDSplitView.h"
@implementation HTMDSplitView
#pragma mark -
#pragma mark Original Methods
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
[self setVertical:YES];
[self setDelegate:self];
}
return self;
}
- (IBAction)adjustSubviews:(id)sender
{
[self adjustSubviews];
}
//setup
- (void)addSubview:(NSView *)aView
{
[super addSubview:aView];
if([[self subviews]count]==1){
[aView setAutoresizingMask:NSViewHeightSizable];
_sideView=aView;
}else{
_mainView=aView;
}
}
//cleanup
- (void)windowWillCloseWillCall
{
//NSLog(@"windowWillCloseWillCall");
NSWindow* window=[self window];
NSDrawer* drawer=[[window drawers]objectAtIndex:0];
NSView* sideView=[_sideView retain];
NSView* contentView=[[window contentView]retain];
NSView* leftView=[[contentView subviews]objectAtIndex:0];
NSRect leftFrame=[leftView frame];
if (leftFrame.size.width==0) {
NSLog(@"save only when frame not collapsed");
leftFrame.size.width = 122;
[leftView setFrame:leftFrame];
[contentView adjustSubviews];
}
[contentView storeLayoutWithName:@"Panels"];
if(sideView){
_sideView=nil;
[sideView removeFromSuperview];
[drawer setContentView:sideView];
[sideView release];
}
}
#pragma mark -
#pragma mark Overridden from NSSplitView
- (float)dividerThickness
{
return 1;
}
- (void)drawDividerInRect:(NSRect)aRect
{
[[NSColor colorWithDeviceWhite:.625 alpha:1] setFill];
[NSBezierPath fillRect:aRect];
}
#pragma mark -
#pragma mark NSSplitView delegate methods
- (BOOL)splitView:(NSSplitView *)sender canCollapseSubview:(NSView *)subview
{
return NO;
}
- (float)splitView:(NSSplitView *)sender constrainMinCoordinate:(float)proposedMin ofSubviewAt:(int)offset
{
return 110.0; //Min width
}
- (float)splitView:(NSSplitView *)sender constrainMaxCoordinate:(float)proposedMax ofSubviewAt:(int)offset
{
return 350.0; //Max width
}
- (void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
NSRect newFrame = [sender frame];
NSView *left = [[sender subviews] objectAtIndex:0];
NSRect leftFrame = [left frame];
NSView *right = [[sender subviews] objectAtIndex:1];
NSRect rightFrame = [right frame];
float dividerThickness = [sender dividerThickness];
leftFrame.size.height = newFrame.size.height;
leftFrame.origin.x = 0;
leftFrame.origin.y = 0;
rightFrame.size.width = newFrame.size.width - leftFrame.size.width - dividerThickness;
rightFrame.size.height = newFrame.size.height;
rightFrame.origin.x = leftFrame.size.width + dividerThickness;
[left setFrame:leftFrame];
[right setFrame:rightFrame];
}
#pragma mark -
#pragma mark Sidebar resize area
- (void)resetCursorRects
{
[super resetCursorRects];
NSRect location = [resizeSlider frame];
location.origin.y = [self frame].size.height - location.size.height;
[self addCursorRect:location cursor:[NSCursor resizeLeftRightCursor]];
}
- (void)mouseDown:(NSEvent *)theEvent
{
//NSLog(@"mouseDown in splitView");
NSPoint clickLocation = [theEvent locationInWindow];
NSView *clickReceiver = [self hitTest:clickLocation];
if ([[clickReceiver className] isEqualToString:@"HTMDResizer"]) {
//NSLog(@"Entering drag");
inResizeMode = YES;
} else {
//NSLog([clickReceiver className]);
inResizeMode = NO;
[super mouseDown:theEvent];
}
//NSLog(@"mouseDown in splitView done");
}
- (void)mouseUp:(NSEvent *)theEvent
{
//NSLog(@"Exiting drag");
inResizeMode = NO;
}
- (void)mouseDragged:(NSEvent *)theEvent
{
//NSLog(@"mouseDragged in splitView");
if (inResizeMode == NO) {
[super mouseDragged:theEvent];
return;
}
[[NSNotificationCenter defaultCenter] postNotificationName:NSSplitViewWillResizeSubviewsNotification object:self];
NSPoint clickLocation = [theEvent locationInWindow];
NSRect newFrame = [_sideView frame];
newFrame.size.width = clickLocation.x;
//NSLog(@"new width: %f", newFrame.size.width);
id delegate = [self delegate];
if(delegate && [delegate respondsToSelector:@selector(splitView:constrainSplitPosition:ofSubviewAt:)]) {
float new = [delegate splitView:self constrainSplitPosition:newFrame.size.width ofSubviewAt:0];
newFrame.size.width = new;
//NSLog(@"Constrained width to: %f", new);
}
if(delegate && [delegate respondsToSelector:@selector(splitView:constrainMinCoordinate:ofSubviewAt:)]) {
float min = [delegate splitView:self constrainMinCoordinate:0. ofSubviewAt:0];
newFrame.size.width = MAX(min, newFrame.size.width);
//NSLog(@"Constrained min width to: %f", newFrame.size.width);
}
if(delegate && [delegate respondsToSelector:@selector(splitView:constrainMaxCoordinate:ofSubviewAt:)]) {
float max = [delegate splitView:self constrainMaxCoordinate:0. ofSubviewAt:0];
newFrame.size.width = MIN(max, newFrame.size.width);
//NSLog(@"Constrained max width to: %f", newFrame.size.width);
}
[_sideView setFrame:newFrame];
[self setNeedsDisplay:YES];
[self adjustSubviews];
[[NSNotificationCenter defaultCenter] postNotificationName:NSSplitViewDidResizeSubviewsNotification object:self];
}
#pragma mark -
#pragma mark Position save support
- (NSString*)htmd__keyForLayoutName: (NSString*)name
{
return [NSString stringWithFormat: @"HTMDSplitViewLayout%@", name];
}
- (void)storeLayoutWithName: (NSString*)name
{
NSString* key = [self htmd__keyForLayoutName: name];
NSMutableArray* viewRects = [NSMutableArray array];
NSEnumerator* viewEnum = [[self subviews] objectEnumerator];
NSView* view;
NSRect frame;
while((view=[viewEnum nextObject])!= nil)
{
if([self isSubviewCollapsed: view])
frame = NSZeroRect;
else
frame = [view frame];
[viewRects addObject: NSStringFromRect(frame)];
}
[[NSUserDefaults standardUserDefaults] setObject: viewRects forKey: key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)loadLayoutWithName: (NSString*)name
{
NSString* key = [self htmd__keyForLayoutName: name];
NSMutableArray* viewRects = [[NSUserDefaults standardUserDefaults] objectForKey: key];
NSArray* views = [self subviews];
int i, count;
NSRect frame;
count = MIN([viewRects count], [views count]);
for(i=0; i<count; i++)
{
frame = NSRectFromString([viewRects objectAtIndex: i]);
if(NSIsEmptyRect(frame))
{
frame = [[views objectAtIndex: i] frame];
if([self isVertical])
frame.size.width = 0;
else
frame.size.height = 0;
}
[[views objectAtIndex: i] setFrame: frame];
}
}
@end