-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTNStackView.j
155 lines (124 loc) · 4.15 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
/*
* 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:(CPRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
_dataSource = [CPArray array];
_stackedViews = [CPArray array];
_padding = 0;
_reversed = NO;
[self setAutoresizingMask:CPViewWidthSizable];
}
return self;
}
/*! @ignore
*/
- (CPRect)_nextPosition
{
var lastStackedView = [_stackedViews lastObject],
position;
if (lastStackedView)
{
position = [lastStackedView frame];
position.origin.y = CPRectGetMaxY(position) + _padding;
position.origin.x = _padding;
}
else
position = CGRectMake(_padding, _padding, CPRectGetWidth([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; i < [_dataSource count]; i++)
{
var view = [_dataSource 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; i < [workingArray count]; i++)
{
var currentView = [workingArray objectAtIndex: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 IBAction
*/
- (IBAction)removeAllViews:(id)aSender
{
for (var i = 0; i < [_dataSource count]; i++)
[[_dataSource objectAtIndex:i] removeFromSuperview];
[_dataSource removeAllObjects];
[self reload];
}
/*! reverse the display of the view (but not in the Datasource) as an @action
*/
- (IBAction)reverse:(id)sender
{
_reversed = !_reversed;
[self reload];
}
@end