-
Notifications
You must be signed in to change notification settings - Fork 13
/
TNStackView.j
182 lines (146 loc) · 4.45 KB
/
TNStackView.j
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
/*
* TNStackView.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPView.j>
/*! @ingroup messageboard
This class allows to create a view that can stack different subviews.
It will resize if in width to fill completely the view, but keeps the height
It is also possible to set padding between views and reverse it.
when the positionning of the view is done, it will call eventual selector [aStackedView layout]
of each subviews. The subview can then, if needed adjust its height, it's content
call it's mom or whatever
*/
@implementation TNStackView : CPView
{
CPArray _dataSource @accessors(property=dataSource);
int _padding @accessors(property=padding);
BOOL _reversed @accessors(getter=isReversed, setter=setReversed:);
CPArray _stackedViews;
}
/*! initialize the TNStackView
@param aFrame the frame
@return a instancied TNStackView
*/
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
[self _init];
}
return self;
}
- (void)_init
{
_dataSource = [CPArray array];
_stackedViews = [CPArray array];
_padding = 0;
_reversed = NO;
}
/*! @ignore
*/
- (CGRect)_nextPosition
{
var lastStackedView = [_stackedViews lastObject],
position;
if (lastStackedView)
{
position = [lastStackedView frame];
position.origin.y = CGRectGetMaxY(position) + _padding;
position.origin.x = _padding;
}
else
position = CGRectMake(_padding, _padding, CGRectGetWidth([self bounds]) - (_padding * 2), 0);
return position
}
/*! reload the content of the datasource
*/
- (void)reload
{
var frame = [self frame];
frame.size.height = 0;
[self setFrame:frame];
for (var i = 0, c = [_stackedViews count]; i < c; i++)
{
var view = [_stackedViews objectAtIndex:i];
if ([view superview])
[view removeFromSuperview];
}
[_stackedViews removeAllObjects];
[self layout];
}
/*! @position the different subviews in the daasource
*/
- (void)layout
{
var stackViewFrame = [self frame],
workingArray = _reversed ? [_dataSource copy].reverse() : _dataSource;
stackViewFrame.size.height = 0;
for (var i = 0, c = [workingArray count]; i < c; i++)
{
var currentView = workingArray[i],
position = [self _nextPosition];
position.size.height = [currentView frameSize].height;
[currentView setAutoresizingMask:CPViewWidthSizable];
[currentView setFrame:position];
if ([currentView respondsToSelector:@selector(layout)])
[currentView layout];
[self addSubview:currentView];
[_stackedViews addObject:currentView];
stackViewFrame.size.height += [currentView frame].size.height + _padding;
}
stackViewFrame.size.height += _padding;
[self setFrame:stackViewFrame];
}
/*! remove all items as an @action
*/
- (@action)removeAllViews:(id)aSender
{
for (var i = 0, c = [_dataSource count]; i < c; i++)
[[_dataSource objectAtIndex:i] removeFromSuperview];
[_dataSource removeAllObjects];
[self reload];
}
/*! reverse the display of the view (but not in the Datasource) as an @action
*/
- (@action)reverse:(id)sender
{
_reversed = !_reversed;
[self reload];
}
#pragma mark -
#pragma mark CPCoding compliance
/*! CPCoder compliance
*/
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
if (self)
{
[self _init];
}
return self;
}
/*! CPCoder compliance
*/
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
}
@end