forked from glebd/bwtoolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BWSplitViewInspectorAutosizingView.m
171 lines (142 loc) · 4.3 KB
/
BWSplitViewInspectorAutosizingView.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
//
// BWSplitViewInspectorAutosizingView.m
// BWToolkit
//
// Created by Brandon Walkin (www.brandonwalkin.com)
// All code is provided under the New BSD license.
//
#import "BWSplitViewInspectorAutosizingView.h"
#import "BWSplitViewInspectorAutosizingButtonCell.h"
@implementation BWSplitViewInspectorAutosizingView
@synthesize splitView;
- (id)initWithFrame:(NSRect)frameRect
{
if (self = [super initWithFrame:frameRect])
{
buttons = [[NSMutableArray alloc] init];
}
return self;
}
- (void)drawRect:(NSRect)aRect
{
aRect = self.bounds;
if ([[self subviews] count] > 0)
{
[[NSColor windowBackgroundColor] set];
NSRectFill(aRect);
}
}
- (BOOL)isFlipped
{
return YES;
}
- (BOOL)isVertical
{
return [splitView isVertical];
}
- (void)layoutButtons
{
// Remove existing buttons
[buttons removeAllObjects];
while ([[self subviews] count] > 0)
{
[[[self subviews] objectAtIndex:0] removeFromSuperview];
}
// Create new buttons and draw them
float x, y;
int numberOfSubviews = [[splitView subviews] count];
for (int i = 0; i < numberOfSubviews; i++)
{
NSRect buttonRect = NSZeroRect;
if ([splitView isVertical])
{
if (i != numberOfSubviews - 1)
buttonRect = NSMakeRect(x, 0, floorf((self.bounds.size.width + numberOfSubviews) / numberOfSubviews), self.bounds.size.height);
else
buttonRect = NSMakeRect(x, 0, self.bounds.size.width - x, self.bounds.size.height);
}
if ([splitView isVertical] == NO)
{
if (i != numberOfSubviews - 1)
buttonRect = NSMakeRect(0, y, self.bounds.size.width, floorf((self.bounds.size.height + numberOfSubviews) / numberOfSubviews));
else
buttonRect = NSMakeRect(0, y, self.bounds.size.width, self.bounds.size.height - y);
}
NSButton *subviewButton = [[[NSButton alloc] initWithFrame:buttonRect] autorelease];
[subviewButton setCell:[[[BWSplitViewInspectorAutosizingButtonCell alloc] initTextCell:@""] autorelease]];
[subviewButton setTarget:self];
[subviewButton setAction:@selector(updateValues:)];
[subviewButton setTag:i];
// Make the new buttons represent whether the subviews are set to resize or not
if ([splitView isVertical])
{
if ([[[splitView subviews] objectAtIndex:i] autoresizingMask] & NSViewWidthSizable)
[subviewButton setIntValue:1];
}
else
{
if ([[[splitView subviews] objectAtIndex:i] autoresizingMask] & NSViewHeightSizable)
[subviewButton setIntValue:1];
}
if ([splitView isVertical] && numberOfSubviews < 6 || ![splitView isVertical] && numberOfSubviews < 4)
[self addSubview:subviewButton];
[buttons addObject:subviewButton];
x += buttonRect.size.width - 1;
y += buttonRect.size.height - 1;
}
// At least 1 subview must be resizable, so if none of the subviews are set to resize, then we'll set all subviews to resize (which will make it the default state)
BOOL resizableViewExists = NO;
for (NSButton *button in buttons)
{
if ([button intValue] == 1)
resizableViewExists = YES;
}
if (resizableViewExists == NO)
{
for (NSButton *button in buttons)
{
[button setIntValue:1];
NSView *subviewForButton = [[splitView subviews] objectAtIndex:[button tag]];
int mask = [subviewForButton autoresizingMask];
if ([splitView isVertical])
[subviewForButton setAutoresizingMask:(mask | NSViewWidthSizable)];
else
[subviewForButton setAutoresizingMask:(mask | NSViewHeightSizable)];
}
}
}
- (void)updateValues:(id)sender
{
// Make sure there is always at least one resizable view
NSView *subviewForSender = [[splitView subviews] objectAtIndex:[sender tag]];
BOOL resizableViewExists = NO;
for (NSButton *button in buttons)
{
if ([button intValue] == 1)
resizableViewExists = YES;
}
if (resizableViewExists == NO)
[sender setIntValue:1];
// Set the autorezising mask on the subview according to the button state
int mask = [subviewForSender autoresizingMask];
if ([splitView isVertical])
{
if ([sender intValue] == 1)
[subviewForSender setAutoresizingMask:(mask | NSViewWidthSizable)];
else
[subviewForSender setAutoresizingMask:(mask & ~NSViewWidthSizable)];
}
else
{
if ([sender intValue] == 1)
[subviewForSender setAutoresizingMask:(mask | NSViewHeightSizable)];
else
[subviewForSender setAutoresizingMask:(mask & ~NSViewHeightSizable)];
}
}
- (void)dealloc
{
[buttons release];
[super dealloc];
}
@end