comparison ext/guichan-0.8.1/include/guichan/widgets/listbox.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_LISTBOX_HPP
45 #define GCN_LISTBOX_HPP
46
47 #include <list>
48
49 #include "guichan/keylistener.hpp"
50 #include "guichan/listmodel.hpp"
51 #include "guichan/mouselistener.hpp"
52 #include "guichan/platform.hpp"
53 #include "guichan/widget.hpp"
54
55 namespace gcn
56 {
57 class SelectionListener;
58
59 /**
60 * An implementation of a list box where an item can be selected.
61 *
62 * To be able display a list the list box uses a user provided list model.
63 * A list model can be any class that implements the ListModel interface.
64 *
65 * If an item is selected in the list box a select event will be sent to all selection
66 * listeners of the list box. If an item is selected by using a mouse click or by using
67 * the enter or space key an action event will be sent to all action listeners of the
68 * list box.
69 */
70 class GCN_CORE_DECLSPEC ListBox :
71 public Widget,
72 public MouseListener,
73 public KeyListener
74 {
75 public:
76 /**
77 * Constructor.
78 */
79 ListBox();
80
81 /**
82 * Constructor.
83 *
84 * @param listModel the list model to use.
85 */
86 ListBox(ListModel *listModel);
87
88 /**
89 * Destructor.
90 */
91 virtual ~ListBox() { }
92
93 /**
94 * Gets the selected item as an index in the list model.
95 *
96 * @return the selected item as an index in the list model.
97 * @see setSelected
98 */
99 int getSelected() const;
100
101 /**
102 * Sets the selected item. The selected item is represented by
103 * an index from the list model.
104 *
105 * @param selected the selected item as an index from the list model.
106 * @see getSelected
107 */
108 void setSelected(int selected);
109
110 /**
111 * Sets the list model to use.
112 *
113 * @param listModel the list model to use.
114 * @see getListModel
115 */
116 void setListModel(ListModel *listModel);
117
118 /**
119 * Gets the list model used.
120 *
121 * @return the list model used.
122 * @see setListModel
123 */
124 ListModel *getListModel();
125
126 /**
127 * Adjusts the size of the list box to fit it's list model.
128 */
129 void adjustSize();
130
131 /**
132 * Checks whether the list box wraps when selecting items with a keyboard.
133 *
134 * Wrapping means that the selection of items will be wrapped. That is, if
135 * the first item is selected and up is pressed, the last item will get
136 * selected. If the last item is selected and down is pressed, the first item
137 * will get selected.
138 *
139 * @return true if wrapping is enabled, fasle otherwise.
140 * @see setWrappingEnabled
141 */
142 bool isWrappingEnabled() const;
143
144 /**
145 * Sets the list box to wrap or not when selecting items with a keyboard.
146 *
147 * Wrapping means that the selection of items will be wrapped. That is, if
148 * the first item is selected and up is pressed, the last item will get
149 * selected. If the last item is selected and down is pressed, the first item
150 * will get selected.
151 *
152 * @see isWrappingEnabled
153 */
154 void setWrappingEnabled(bool wrappingEnabled);
155
156 /**
157 * Adds a selection listener to the list box. When the selection
158 * changes an event will be sent to all selection listeners of the
159 * list box.
160 *
161 * @param selectionListener The selection listener to add.
162 * @since 0.8.0
163 */
164 void addSelectionListener(SelectionListener* selectionListener);
165
166 /**
167 * Removes a selection listener from the list box.
168 *
169 * @param selectionListener The selection listener to remove.
170 * @since 0.8.0
171 */
172 void removeSelectionListener(SelectionListener* selectionListener);
173
174 /**
175 * Gets the height of a row. Should be overridden if another row
176 * height than the font height is preferred.
177 *
178 * @return The height of a row.
179 * @since 0.8.0
180 */
181 virtual unsigned int getRowHeight() const;
182
183
184 // Inherited from Widget
185
186 virtual void draw(Graphics* graphics);
187
188 virtual void logic();
189
190
191 // Inherited from KeyListener
192
193 virtual void keyPressed(KeyEvent& keyEvent);
194
195
196 // Inherited from MouseListener
197
198 virtual void mousePressed(MouseEvent& mouseEvent);
199
200 virtual void mouseWheelMovedUp(MouseEvent& mouseEvent);
201
202 virtual void mouseWheelMovedDown(MouseEvent& mouseEvent);
203
204 virtual void mouseDragged(MouseEvent& mouseEvent);
205
206
207 protected:
208 /**
209 * Distributes a value changed event to all selection listeners
210 * of the list box.
211 *
212 * @since 0.8.0
213 */
214 void distributeValueChangedEvent();
215
216 /**
217 * The selected item as an index in the list model.
218 */
219 int mSelected;
220
221 /**
222 * The list model to use.
223 */
224 ListModel *mListModel;
225
226 /**
227 * True if wrapping is enabled, false otherwise.
228 */
229 bool mWrappingEnabled;
230
231 /**
232 * Typdef.
233 */
234 typedef std::list<SelectionListener*> SelectionListenerList;
235
236 /**
237 * The selection listeners of the list box.
238 */
239 SelectionListenerList mSelectionListeners;
240
241 /**
242 * Typedef.
243 */
244 typedef SelectionListenerList::iterator SelectionListenerIterator;
245 };
246 }
247
248 #endif // end GCN_LISTBOX_HPP