comparison ext/guichan-0.8.1/include/guichan/widgets/dropdown.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) 2004 - 2008 Olof Naessén and Per Larsson
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_DROPDOWN_HPP
45 #define GCN_DROPDOWN_HPP
46
47 #include "guichan/actionlistener.hpp"
48 #include "guichan/basiccontainer.hpp"
49 #include "guichan/deathlistener.hpp"
50 #include "guichan/focushandler.hpp"
51 #include "guichan/focuslistener.hpp"
52 #include "guichan/keylistener.hpp"
53 #include "guichan/listmodel.hpp"
54 #include "guichan/mouselistener.hpp"
55 #include "guichan/platform.hpp"
56 #include "guichan/selectionlistener.hpp"
57 #include "guichan/widgets/listbox.hpp"
58 #include "guichan/widgets/scrollarea.hpp"
59
60 namespace gcn
61 {
62 /**
63 * An implementation of a drop downable list from which an item can be selected.
64 * The drop down consists of an internal ScrollArea and an internal ListBox.
65 * The drop down also uses an internal FocusHandler to handle the focus of the
66 * internal ScollArea and the internal ListBox. The scroll area and the list box
67 * can be passed to the drop down if a custom scroll area and or a custom list box
68 * is preferable.
69 *
70 * To be able display a list the drop down uses a user provided list model.
71 * A list model can be any class that implements the ListModel interface.
72 *
73 * If an item is selected in the drop down a select event will be sent to all selection
74 * listeners of the drop down. If an item is selected by using a mouse click or by using
75 * the enter or space key an action event will be sent to all action listeners of the
76 * drop down.
77 */
78 class GCN_CORE_DECLSPEC DropDown :
79 public ActionListener,
80 public BasicContainer,
81 public KeyListener,
82 public MouseListener,
83 public FocusListener,
84 public SelectionListener
85 {
86 public:
87 /**
88 * Contructor.
89 *
90 * @param listModel the ListModel to use.
91 * @param scrollArea the ScrollArea to use.
92 * @param listBox the listBox to use.
93 * @see ListModel, ScrollArea, ListBox.
94 */
95 DropDown(ListModel *listModel = NULL,
96 ScrollArea *scrollArea = NULL,
97 ListBox *listBox = NULL);
98
99 /**
100 * Destructor.
101 */
102 virtual ~DropDown();
103
104 /**
105 * Gets the selected item as an index in the list model.
106 *
107 * @return the selected item as an index in the list model.
108 * @see setSelected
109 */
110 int getSelected() const;
111
112 /**
113 * Sets the selected item. The selected item is represented by
114 * an index from the list model.
115 *
116 * @param selected the selected item as an index from the list model.
117 * @see getSelected
118 */
119 void setSelected(int selected);
120
121 /**
122 * Sets the list model to use when displaying the list.
123 *
124 * @param listModel the list model to use.
125 * @see getListModel
126 */
127 void setListModel(ListModel *listModel);
128
129 /**
130 * Gets the list model used.
131 *
132 * @return the ListModel used.
133 * @see setListModel
134 */
135 ListModel *getListModel();
136
137 /**
138 * Adjusts the height of the drop down to fit the height of the
139 * drop down's parent's height. It's used to not make the drop down
140 * draw itself outside of it's parent if folded down.
141 */
142 void adjustHeight();
143
144 /**
145 * Adds a selection listener to the drop down. When the selection
146 * changes an event will be sent to all selection listeners of the
147 * drop down.
148 *
149 * @param selectionListener the selection listener to add.
150 * @since 0.8.0
151 */
152 void addSelectionListener(SelectionListener* selectionListener);
153
154 /**
155 * Removes a selection listener from the drop down.
156 *
157 * @param selectionListener the selection listener to remove.
158 * @since 0.8.0
159 */
160 void removeSelectionListener(SelectionListener* selectionListener);
161
162
163 // Inherited from Widget
164
165 virtual void draw(Graphics* graphics);
166
167 void setBaseColor(const Color& color);
168
169 void setBackgroundColor(const Color& color);
170
171 void setForegroundColor(const Color& color);
172
173 void setFont(Font *font);
174
175 void setSelectionColor(const Color& color);
176
177
178 // Inherited from BasicContainer
179
180 virtual Rectangle getChildrenArea();
181
182
183 // Inherited from FocusListener
184
185 virtual void focusLost(const Event& event);
186
187
188 // Inherited from ActionListener
189
190 virtual void action(const ActionEvent& actionEvent);
191
192
193 // Inherited from DeathListener
194
195 virtual void death(const Event& event);
196
197
198 // Inherited from KeyListener
199
200 virtual void keyPressed(KeyEvent& keyEvent);
201
202
203 // Inherited from MouseListener
204
205 virtual void mousePressed(MouseEvent& mouseEvent);
206
207 virtual void mouseReleased(MouseEvent& mouseEvent);
208
209 virtual void mouseWheelMovedUp(MouseEvent& mouseEvent);
210
211 virtual void mouseWheelMovedDown(MouseEvent& mouseEvent);
212
213 virtual void mouseDragged(MouseEvent& mouseEvent);
214
215
216 // Inherited from SelectionListener
217
218 virtual void valueChanged(const SelectionEvent& event);
219
220 protected:
221 /**
222 * Draws the button of the drop down.
223 *
224 * @param graphics a Graphics object to draw with.
225 */
226 virtual void drawButton(Graphics *graphics);
227
228 /**
229 * Sets the drop down to be dropped down.
230 */
231 virtual void dropDown();
232
233 /**
234 * Sets the drop down to be folded up.
235 */
236 virtual void foldUp();
237
238 /**
239 * Distributes a value changed event to all selection listeners
240 * of the drop down.
241 *
242 * @since 0.8.0
243 */
244 void distributeValueChangedEvent();
245
246 /**
247 * True if the drop down is dropped down, false otherwise.
248 */
249 bool mDroppedDown;
250
251 /**
252 * True if the drop down has been pushed with the mouse, false
253 * otherwise.
254 */
255 bool mPushed;
256
257 /**
258 * Holds what the height is if the drop down is folded up. Used when
259 * checking if the list of the drop down was clicked or if the upper part
260 * of the drop down was clicked on a mouse click
261 */
262 int mFoldedUpHeight;
263
264 /**
265 * The scroll area used.
266 */
267 ScrollArea* mScrollArea;
268
269 /**
270 * The list box used.
271 */
272 ListBox* mListBox;
273
274 /**
275 * The internal focus handler used to keep track of focus for the
276 * internal list box.
277 */
278 FocusHandler mInternalFocusHandler;
279
280 /**
281 * True if an internal scroll area is used, false if a scroll area
282 * has been passed to the drop down which the drop down should not
283 * deleted in it's destructor.
284 */
285 bool mInternalScrollArea;
286
287 /**
288 * True if an internal list box is used, false if a list box
289 * has been passed to the drop down which the drop down should not
290 * deleted in it's destructor.
291 */
292 bool mInternalListBox;
293
294 /**
295 * True if the drop down is dragged.
296 */
297 bool mIsDragged;
298
299 /**
300 * Typedef.
301 */
302 typedef std::list<SelectionListener*> SelectionListenerList;
303
304 /**
305 * The selection listener's of the drop down.
306 */
307 SelectionListenerList mSelectionListeners;
308
309 /**
310 * Typedef.
311 */
312 typedef SelectionListenerList::iterator SelectionListenerIterator;
313 };
314 }
315
316 #endif // end GCN_DROPDOWN_HPP