comparison ext/guichan-0.8.1/include/guichan/contrib/widgets/adjustingcontainer.hpp @ 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 #ifndef GCN_CONTRIB_ADJUSTINGCONTAINER_HPP
45 #define GCN_CONTRIB_ADJUSTINGCONTAINER_HPP
46
47 #include <vector>
48
49 namespace gcn
50 {
51 namespace contrib
52 {
53 /**
54 * Self-adjusting Container class. AdjustingContainers are an easy way to
55 * have Guichan position a group of widgets for you. It organizes elements
56 * in a table layout, with fixed columns and variable rows. The user specifies
57 *
58 * @verbitam
59 * <ul>
60 * <li>the number of columns</li>
61 * <li>horizontal spacing between columns</li>
62 * <li>vertical spacing between rows</li>
63 * <li>padding around the sides of the container</li>
64 * <li>each column's alignment</li>
65 * </ul>
66 * @endverbitam
67 *
68 * These properties give the user a lot of flexibility to make the
69 * widgets look just right.
70 * @code
71 * AdjustingContainer *adjust = new AdjustingContainer;
72 * adjust->setPadding(5, 5, 5, 5); //left, right, top, bottom
73 * adjust->setHorizontalSpacing(3);
74 * adjust->setVerticalSpacing(3);
75 * adjust->setColumns(3);
76 * adjust->setColumnAlignment(0, AdjustingContainer::LEFT);
77 * adjust->setColumnAlignment(1, AdjustingContainer::CENTER);
78 * adjust->setColumnAlignment(2, AdjustingContainer::RIGHT);
79 * top->add(adjust);
80 *
81 * for(int j = 0; j < 9; j++)
82 * {
83 * gcn::Label *l;
84 * int r = rand() % 3;
85 * if(r == 0)
86 * l = new gcn::Label("Short");
87 * else if(r == 1)
88 * l = new gcn::Label("A longer phrase");
89 * else
90 * l = new gcn::Label("Extravagent and wordy text");
91 * adjust->add(l);
92 * @endcode
93 *
94 * Output:
95 * @verbitam
96 * <pre>
97 *+---------------------------------------------------------------------------+
98 *| |
99 *| A longer phrase Short Extravagent and wordy text |
100 *| |
101 *| Short Extravagent and wordy text Short |
102 *| |
103 *| Short A longer phrase A longer phrase |
104 *| |
105 *+---------------------------------------------------------------------------+
106 * </pre>
107 * @endverbitam
108 * As you can see, each column is only as big as its largest element.
109 * The AdjustingContainer will resize itself and rearrange its contents
110 * based on whatever widgets it contains, allowing dynamic addition and
111 * removal while the program is running. It also plays nicely with ScrollAreas,
112 * allowing you to show a fixed, maximum size while not limiting the actual
113 * container.
114 *
115 * For more help with using AdjustingContainers, try the Guichan forums
116 * (http://guichan.sourceforge.net/forum/) or email mrlachatte@gmail.com.
117 *
118 * @author Josh Matthews
119 */
120 class AdjustingContainer : public gcn::Container
121 {
122 public:
123 /**
124 * Constructor.
125 */
126 AdjustingContainer();
127
128 /**
129 * Destructor.
130 */
131 virtual ~AdjustingContainer();
132
133 /**
134 * Set the number of columns to divide the widgets into.
135 * The number of rows is derived automatically from the number
136 * of widgets based on the number of columns. Default column
137 * alignment is left.
138 *
139 * @param numberOfColumns the number of columns.
140 */
141 virtual void setNumberOfColumns(unsigned int numberOfColumns);
142
143 /**
144 * Set a specific column's alignment.
145 *
146 * @param column the column number, starting from 0.
147 * @param alignment the column's alignment. See enum with alignments.
148 */
149 virtual void setColumnAlignment(unsigned int column, unsigned int alignment);
150
151 /**
152 * Set the padding for the sides of the container.
153 *
154 * @param paddingLeft left padding.
155 * @param paddingRight right padding.
156 * @param paddingTop top padding.
157 * @param paddingBottom bottom padding.
158 */
159 virtual void setPadding(unsigned int paddingLeft,
160 unsigned int paddingRight,
161 unsigned int paddingTop,
162 unsigned int paddingBottom);
163
164 /**
165 * Set the spacing between rows.
166 *
167 * @param verticalSpacing spacing in pixels.
168 */
169 virtual void setVerticalSpacing(unsigned int verticalSpacing);
170
171 /**
172 * Set the horizontal spacing between columns.
173 *
174 * @param horizontalSpacing spacing in pixels.
175 */
176 virtual void setHorizontalSpacing(unsigned int horizontalSpacing);
177
178 /**
179 * Rearrange the widgets and resize the container.
180 */
181 virtual void adjustContent();
182
183
184 // Inherited from Container
185
186 virtual void logic();
187
188 virtual void add(Widget *widget);
189
190 virtual void add(Widget *widget, int x, int y);
191
192 virtual void remove(Widget *widget);
193
194 virtual void clear();
195
196 /**
197 * Possible alignment values for each column.
198 *
199 * LEFT - Align content to the left of the column.
200 * MIDDLE - Align content to the middle of the column.
201 * RIGHT - Align content to the right of the column.
202 */
203 enum
204 {
205 LEFT = 0,
206 CENTER,
207 RIGHT
208 };
209
210 protected:
211
212 /**
213 * Adjust the size of the container to fit all the widgets.
214 */
215 virtual void adjustSize();
216
217 std::vector<Widget*> mContainedWidgets;
218 std::vector<unsigned int> mColumnWidths;
219 std::vector<unsigned int> mColumnAlignment;
220 std::vector<unsigned int> mRowHeights;
221 unsigned int mWidth;
222 unsigned int mHeight;
223 unsigned int mNumberOfColumns;
224 unsigned int mNumberOfRows;
225 unsigned int mPaddingLeft;
226 unsigned int mPaddingRight;
227 unsigned int mPaddingTop;
228 unsigned int mPaddingBottom;
229 unsigned int mVerticalSpacing;
230 unsigned int mHorizontalSpacing;
231 };
232 }
233 }
234
235 #endif