comparison ext/guichan-0.8.1/src/contrib/widgets/adjustingcontainer.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /* _______ __ __ __ ______ __ __ _______ __ __
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8 *
9 * Copyright (c) 2007 - 2008 Josh Matthews and Olof Naessén
10 *
11 *
12 * Per Larsson a.k.a finalman
13 * Olof Naessén a.k.a jansem/yakslem
14 *
15 * Visit: http://guichan.sourceforge.net
16 *
17 * License: (BSD)
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * 3. Neither the name of Guichan nor the names of its contributors may
28 * be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /*
45 * For comments regarding functions please see the header file.
46 */
47
48 #include "adjustingcontainer.hpp"
49
50 #include <guichan.hpp>
51
52 namespace gcn
53 {
54 namespace contrib
55 {
56 AdjustingContainer::AdjustingContainer()
57 : mWidth(0),
58 mHeight(0),
59 mNumberOfColumns(1),
60 mNumberOfRows(1),
61 mPaddingLeft(0),
62 mPaddingRight(0),
63 mPaddingTop(0),
64 mPaddingBottom(0),
65 mVerticalSpacing(0),
66 mHorizontalSpacing(0)
67
68
69 {
70 mColumnWidths.push_back(0);
71 mRowHeights.push_back(0);
72 }
73
74 AdjustingContainer::~AdjustingContainer()
75 {
76
77 }
78
79 void AdjustingContainer::setNumberOfColumns(unsigned int numberOfColumns)
80 {
81 mNumberOfColumns = numberOfColumns;
82
83 if (mColumnAlignment.size() < numberOfColumns)
84 {
85 while (mColumnAlignment.size() < numberOfColumns)
86 {
87 mColumnAlignment.push_back(LEFT);
88 }
89 }
90 else
91 {
92 while (mColumnAlignment.size() > numberOfColumns)
93 {
94 mColumnAlignment.pop_back();
95 }
96 }
97 }
98
99 void AdjustingContainer::setColumnAlignment(unsigned int column,
100 unsigned int alignment)
101 {
102 if (column < mColumnAlignment.size())
103 {
104 mColumnAlignment[column] = alignment;
105 }
106 }
107
108 void AdjustingContainer::setPadding(unsigned int paddingLeft,
109 unsigned int paddingRight,
110 unsigned int paddingTop,
111 unsigned int paddingBottom)
112 {
113 mPaddingLeft = paddingLeft;
114 mPaddingRight = paddingRight;
115 mPaddingTop = paddingTop;
116 mPaddingBottom = paddingBottom;
117 }
118
119 void AdjustingContainer::setVerticalSpacing(unsigned int verticalSpacing)
120 {
121 mVerticalSpacing = verticalSpacing;
122 }
123
124 void AdjustingContainer::setHorizontalSpacing(unsigned int horizontalSpacing)
125 {
126 mHorizontalSpacing = horizontalSpacing;
127 }
128
129 void AdjustingContainer::logic()
130 {
131 Container::logic();
132 adjustContent();
133 }
134
135 void AdjustingContainer::add(Widget *widget)
136 {
137 Container::add(widget);
138 mContainedWidgets.push_back(widget);
139 }
140
141 void AdjustingContainer::add(Widget *widget, int x, int y)
142 {
143 add(widget);
144 }
145
146 void AdjustingContainer::clear()
147 {
148 Container::clear();
149 mContainedWidgets.clear();
150 }
151
152 void AdjustingContainer::remove(Widget *widget)
153 {
154 Container::remove(widget);
155 std::vector<gcn::Widget *>::iterator it;
156 for(it = mContainedWidgets.begin(); it != mContainedWidgets.end(); it++)
157 {
158 if(*it == widget)
159 {
160 mContainedWidgets.erase(it);
161 break;
162 }
163 }
164 }
165
166 void AdjustingContainer::adjustSize()
167 {
168 mNumberOfRows = mContainedWidgets.size()
169 / mNumberOfColumns + mContainedWidgets.size() % mNumberOfColumns;
170
171 mColumnWidths.clear();
172
173 unsigned int i;
174 for (i = 0; i < mNumberOfColumns; i++)
175 {
176 mColumnWidths.push_back(0);
177 }
178
179 mRowHeights.clear();
180
181 for (i = 0; i < mNumberOfRows; i++)
182 {
183 mRowHeights.push_back(0);
184 }
185
186 for (i = 0; i < mNumberOfColumns; i++)
187 {
188 unsigned int j;
189 for (j = 0; j < mNumberOfRows && mNumberOfColumns * j + i < mContainedWidgets.size(); j++)
190 {
191 if ((unsigned int)mContainedWidgets[mNumberOfColumns * j + i]->getWidth() > mColumnWidths[i])
192 {
193 mColumnWidths[i] = mContainedWidgets[mNumberOfColumns * j + i]->getWidth();
194 }
195 if ((unsigned int)mContainedWidgets[mNumberOfColumns * j + i]->getHeight() > mRowHeights[j])
196 {
197 mRowHeights[j] = mContainedWidgets[mNumberOfColumns * j + i]->getHeight();
198 }
199 }
200 }
201
202 mWidth = mPaddingLeft;
203
204 for (i = 0; i < mColumnWidths.size(); i++)
205 {
206 mWidth += mColumnWidths[i] + mHorizontalSpacing;
207 }
208
209 mWidth -= mHorizontalSpacing;
210 mWidth += mPaddingRight;
211
212 mHeight = mPaddingTop;
213
214 for (i = 0; i < mRowHeights.size(); i++)
215 {
216 mHeight += mRowHeights[i] + mVerticalSpacing;
217 }
218
219 mHeight -= mVerticalSpacing;
220 mHeight += mPaddingBottom;
221
222 setHeight(mHeight);
223 setWidth(mWidth);
224 }
225
226 void AdjustingContainer::adjustContent()
227 {
228 adjustSize();
229
230 unsigned int columnCount = 0;
231 unsigned int rowCount = 0;
232 unsigned int y = mPaddingTop;
233
234 for (unsigned int i = 0; i < mContainedWidgets.size(); i++)
235 {
236 unsigned basex;
237 if (columnCount % mNumberOfColumns)
238 {
239 basex = mPaddingLeft;
240 unsigned int j;
241
242 for (j = 0; j < columnCount; j++)
243 {
244 basex += mColumnWidths[j] + mHorizontalSpacing;
245 }
246 }
247 else
248 {
249 basex = mPaddingLeft;
250 }
251
252 switch (mColumnAlignment[columnCount])
253 {
254 case LEFT:
255 mContainedWidgets[i]->setX(basex);
256 break;
257 case CENTER:
258 mContainedWidgets[i]->setX(basex + (mColumnWidths[columnCount] - mContainedWidgets[i]->getWidth()) / 2);
259 break;
260 case RIGHT:
261 mContainedWidgets[i]->setX(basex + mColumnWidths[columnCount] - mContainedWidgets[i]->getWidth());
262 break;
263 default:
264 throw GCN_EXCEPTION("Unknown alignment.");
265 }
266
267 mContainedWidgets[i]->setY(y);
268 columnCount++;
269
270 if (columnCount == mNumberOfColumns)
271 {
272 columnCount = 0;
273 y += mRowHeights[rowCount] + mVerticalSpacing;
274 rowCount++;
275 }
276 }
277 }
278 }
279 }