Skip to content
Newer
Older
100644 272 lines (211 sloc) 5.21 KB
dc5be1c Initial check-in.
uli authored
1 //
2 // UKViewListView.m
3 // UKViewListView
4 //
5 // Created by Uli Kusterer on 14.10.06.
6 // Copyright 2006 M. Uli Kusterer. All rights reserved.
7 //
8
9 #import "UKViewListView.h"
10 #import "NSView+SetFrameSizePinnedToTopLeft.h"
11
12
13 #define HIDDEN_PROP_NAME @"isHidden"
14
15
16 @implementation UKViewListView
17
18 - (id)initWithFrame:(NSRect)frame
19 {
20 self = [super initWithFrame:frame];
21 if( self )
22 {
23 // Initialization code here.
24 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(viewFrameDidChange:) name: NSViewFrameDidChangeNotification object: self];
25 interViewSpacing = -1;
26 forceToContentHeight = YES;
27 }
28 return self;
29 }
30
31
32 - (id)initWithCoder:(NSCoder*)coder
33 {
34 self = [super initWithCoder: coder];
35 if( self )
36 {
37 // Initialization code here.
38 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(viewFrameDidChange:) name: NSViewFrameDidChangeNotification object: self];
39 interViewSpacing = -1;
40 forceToContentHeight = YES;
41 }
42 return self;
43 }
44
45
46 -(void) dealloc
47 {
48 NSEnumerator* enny = [[self subviews] objectEnumerator];
49 NSView* currView = nil;
50 while(( currView = [enny nextObject] ))
51 [currView removeObserver: self forKeyPath: HIDDEN_PROP_NAME];
52
53 [super dealloc];
54 }
55
56
57 -(void) awakeFromNib
58 {
59 [self performSelector: @selector(observeAllSubviewsVisibilityChanges:) withObject: nil afterDelay: 0];
60 [self reLayoutViewListViews];
61 }
62
63
64 -(void) observeAllSubviewsVisibilityChanges: (id)sender
65 {
66 NSEnumerator* enny = [[self subviews] objectEnumerator];
67 NSView* currView = nil;
68 while(( currView = [enny nextObject] ))
69 [currView addObserver: self forKeyPath: HIDDEN_PROP_NAME options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context: nil];
70 }
71
72 -(void) drawRect: (NSRect)rect
73 {
74 //NSDrawGrayBezel( [self bounds], [self bounds] ); // Debugging only.
75 }
76
77
78 -(void) didAddSubview: (NSView*)subview
79 {
80 [super didAddSubview: subview];
81
82 [self reLayoutViewListViews]; // Invalidates new box.
83
84 [subview addObserver: self forKeyPath: HIDDEN_PROP_NAME options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context: nil];
85 }
86
87
88 -(void) willRemoveSubview: (NSView *)subview
89 {
90 [subview removeObserver: self forKeyPath: HIDDEN_PROP_NAME];
91 }
92
93
94 -(void) viewDidMoveToWindow
95 {
96 [self reLayoutViewListViews];
97 }
98
99
100 -(void) observeValueForKeyPath: (NSString*)propName ofObject: (NSView*)subview
101 change: (NSDictionary*)change context: (void*)refCon
102 {
103 if( [propName isEqualToString: HIDDEN_PROP_NAME] )
104 {
105 [self reLayoutViewListViews];
106 }
107 }
108
109
110 -(void) reLayoutViewListViewsAndAdjustFrame: (BOOL)adjustFrame
111 {
112 if( !isInReLayout )
113 {
114 isInReLayout = YES;
115 if( forceToContentHeight && adjustFrame )
116 {
117 if( [[self window] contentView] == self )
118 [[self window] setContentSize: [self bestSize]];
119 else
120 [self setFrameSizePinnedToTopLeft: [self bestSize]];
121 }
122
123 NSRect myBounds = [self bounds];
124 NSPoint viewPos = NSMakePoint( leftMargin, NSMaxY( myBounds ) -topMargin );
125 NSArray* subs = [[[self subviews] copy] autorelease];
126 NSEnumerator* enny = [subs objectEnumerator];
127 NSView* currSubview = nil;
128
129 while( (currSubview = [enny nextObject]) )
130 {
131 if( ![currSubview isHidden] )
132 {
133 NSRect currViewBox = [currSubview bounds];
134 viewPos.y -= currViewBox.size.height;
135 currViewBox.origin = viewPos;
136 currViewBox.size.width = myBounds.size.width -leftMargin -rightMargin;
137 [currSubview setFrame: currViewBox];
138 viewPos.y -= interViewSpacing;
139 }
140 }
141
142 [self setNeedsDisplay: YES];
143 isInReLayout = NO;
144 }
145 }
146
147
148 -(void) reLayoutViewListViews
149 {
150 [self reLayoutViewListViewsAndAdjustFrame: YES];
151 }
152
153
154 -(NSSize) bestSize
155 {
156 NSSize bSiz = NSZeroSize;
157
158 bSiz.width = [self bounds].size.width;
159 bSiz.height = topMargin +bottomMargin;
160
161 NSEnumerator* enny = [[self subviews] objectEnumerator];
162 NSView* currSubview = nil;
163 BOOL isFirst = YES;
164
165 while( (currSubview = [enny nextObject]) )
166 {
167 if( ![currSubview isHidden] )
168 {
169 NSRect currViewBox = [currSubview bounds];
170
171 if( isFirst )
172 isFirst = NO;
173 else
174 bSiz.height += interViewSpacing;
175
176 bSiz.height += currViewBox.size.height;
177 }
178 }
179
180 return bSiz;
181 }
182
183
184 -(void) viewFrameDidChange: (NSNotification*)notif
185 {
186 //NSLog(@"viewFrameDidChange:");
187 [self reLayoutViewListViewsAndAdjustFrame: NO];
188 }
189
190
191 -(void) setForceToContentHeight: (BOOL)doForce
192 {
193 forceToContentHeight = doForce;
194 if( doForce )
195 [self reLayoutViewListViews];
196 }
197
198
199 -(BOOL) forceToContentHeight
200 {
201 return forceToContentHeight;
202 }
203
204
205 - (float)leftMargin
206 {
207 return leftMargin;
208 }
209
210 - (void)setLeftMargin:(float)value
211 {
212 if (leftMargin != value)
213 {
214 leftMargin = value;
215 }
216 }
217
218 - (float)rightMargin
219 {
220 return rightMargin;
221 }
222
223 - (void)setRightMargin:(float)value
224 {
225 if (rightMargin != value)
226 {
227 rightMargin = value;
228 }
229 }
230
231 - (float)topMargin
232 {
233 return topMargin;
234 }
235
236 - (void)setTopMargin:(float)value
237 {
238 if (topMargin != value)
239 {
240 topMargin = value;
241 }
242 }
243
244 - (float)bottomMargin
245 {
246 return bottomMargin;
247 }
248
249 - (void)setBottomMargin:(float)value
250 {
251 if (bottomMargin != value)
252 {
253 bottomMargin = value;
254 }
255 }
256
257 - (float)interViewSpacing
258 {
259 return interViewSpacing;
260 }
261
262 - (void)setInterViewSpacing:(float)value
263 {
264 if (interViewSpacing != value)
265 {
266 interViewSpacing = value;
267 }
268 }
269
270
271 @end
Something went wrong with that request. Please try again.