diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CGUIComboBox.cpp | 1046 |
1 files changed, 523 insertions, 523 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CGUIComboBox.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CGUIComboBox.cpp index 44d92e4..910177e 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CGUIComboBox.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CGUIComboBox.cpp | |||
@@ -1,523 +1,523 @@ | |||
1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt | 1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt |
2 | // This file is part of the "Irrlicht Engine". | 2 | // This file is part of the "Irrlicht Engine". |
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | 3 | // For conditions of distribution and use, see copyright notice in irrlicht.h |
4 | 4 | ||
5 | #include "CGUIComboBox.h" | 5 | #include "CGUIComboBox.h" |
6 | #ifdef _IRR_COMPILE_WITH_GUI_ | 6 | #ifdef _IRR_COMPILE_WITH_GUI_ |
7 | 7 | ||
8 | #include "IGUIEnvironment.h" | 8 | #include "IGUIEnvironment.h" |
9 | #include "IVideoDriver.h" | 9 | #include "IVideoDriver.h" |
10 | #include "IGUISkin.h" | 10 | #include "IGUISkin.h" |
11 | #include "IGUIEnvironment.h" | 11 | #include "IGUIEnvironment.h" |
12 | #include "IGUIFont.h" | 12 | #include "IGUIFont.h" |
13 | #include "IGUIButton.h" | 13 | #include "IGUIButton.h" |
14 | #include "CGUIListBox.h" | 14 | #include "CGUIListBox.h" |
15 | #include "os.h" | 15 | #include "os.h" |
16 | 16 | ||
17 | namespace irr | 17 | namespace irr |
18 | { | 18 | { |
19 | namespace gui | 19 | namespace gui |
20 | { | 20 | { |
21 | 21 | ||
22 | //! constructor | 22 | //! constructor |
23 | CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent, | 23 | CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent, |
24 | s32 id, core::rect<s32> rectangle) | 24 | s32 id, core::rect<s32> rectangle) |
25 | : IGUIComboBox(environment, parent, id, rectangle), | 25 | : IGUIComboBox(environment, parent, id, rectangle), |
26 | ListButton(0), SelectedText(0), ListBox(0), LastFocus(0), | 26 | ListButton(0), SelectedText(0), ListBox(0), LastFocus(0), |
27 | Selected(-1), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER), MaxSelectionRows(5), HasFocus(false) | 27 | Selected(-1), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER), MaxSelectionRows(5), HasFocus(false) |
28 | { | 28 | { |
29 | #ifdef _DEBUG | 29 | #ifdef _DEBUG |
30 | setDebugName("CGUIComboBox"); | 30 | setDebugName("CGUIComboBox"); |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | IGUISkin* skin = Environment->getSkin(); | 33 | IGUISkin* skin = Environment->getSkin(); |
34 | 34 | ||
35 | s32 width = 15; | 35 | s32 width = 15; |
36 | if (skin) | 36 | if (skin) |
37 | width = skin->getSize(EGDS_WINDOW_BUTTON_WIDTH); | 37 | width = skin->getSize(EGDS_WINDOW_BUTTON_WIDTH); |
38 | 38 | ||
39 | core::rect<s32> r; | 39 | core::rect<s32> r; |
40 | r.UpperLeftCorner.X = rectangle.getWidth() - width - 2; | 40 | r.UpperLeftCorner.X = rectangle.getWidth() - width - 2; |
41 | r.LowerRightCorner.X = rectangle.getWidth() - 2; | 41 | r.LowerRightCorner.X = rectangle.getWidth() - 2; |
42 | 42 | ||
43 | r.UpperLeftCorner.Y = 2; | 43 | r.UpperLeftCorner.Y = 2; |
44 | r.LowerRightCorner.Y = rectangle.getHeight() - 2; | 44 | r.LowerRightCorner.Y = rectangle.getHeight() - 2; |
45 | 45 | ||
46 | ListButton = Environment->addButton(r, this, -1, L""); | 46 | ListButton = Environment->addButton(r, this, -1, L""); |
47 | if (skin && skin->getSpriteBank()) | 47 | if (skin && skin->getSpriteBank()) |
48 | { | 48 | { |
49 | ListButton->setSpriteBank(skin->getSpriteBank()); | 49 | ListButton->setSpriteBank(skin->getSpriteBank()); |
50 | ListButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL)); | 50 | ListButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL)); |
51 | ListButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL)); | 51 | ListButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL)); |
52 | } | 52 | } |
53 | ListButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT); | 53 | ListButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT); |
54 | ListButton->setSubElement(true); | 54 | ListButton->setSubElement(true); |
55 | ListButton->setTabStop(false); | 55 | ListButton->setTabStop(false); |
56 | 56 | ||
57 | r.UpperLeftCorner.X = 2; | 57 | r.UpperLeftCorner.X = 2; |
58 | r.UpperLeftCorner.Y = 2; | 58 | r.UpperLeftCorner.Y = 2; |
59 | r.LowerRightCorner.X = RelativeRect.getWidth() - (ListButton->getAbsolutePosition().getWidth() + 2); | 59 | r.LowerRightCorner.X = RelativeRect.getWidth() - (ListButton->getAbsolutePosition().getWidth() + 2); |
60 | r.LowerRightCorner.Y = RelativeRect.getHeight() - 2; | 60 | r.LowerRightCorner.Y = RelativeRect.getHeight() - 2; |
61 | 61 | ||
62 | SelectedText = Environment->addStaticText(L"", r, false, false, this, -1, false); | 62 | SelectedText = Environment->addStaticText(L"", r, false, false, this, -1, false); |
63 | SelectedText->setSubElement(true); | 63 | SelectedText->setSubElement(true); |
64 | SelectedText->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT); | 64 | SelectedText->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT); |
65 | SelectedText->setTextAlignment(EGUIA_UPPERLEFT, EGUIA_CENTER); | 65 | SelectedText->setTextAlignment(EGUIA_UPPERLEFT, EGUIA_CENTER); |
66 | if (skin) | 66 | if (skin) |
67 | SelectedText->setOverrideColor(skin->getColor(EGDC_BUTTON_TEXT)); | 67 | SelectedText->setOverrideColor(skin->getColor(EGDC_BUTTON_TEXT)); |
68 | SelectedText->enableOverrideColor(true); | 68 | SelectedText->enableOverrideColor(true); |
69 | 69 | ||
70 | // this element can be tabbed to | 70 | // this element can be tabbed to |
71 | setTabStop(true); | 71 | setTabStop(true); |
72 | setTabOrder(-1); | 72 | setTabOrder(-1); |
73 | } | 73 | } |
74 | 74 | ||
75 | 75 | ||
76 | void CGUIComboBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical) | 76 | void CGUIComboBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical) |
77 | { | 77 | { |
78 | HAlign = horizontal; | 78 | HAlign = horizontal; |
79 | VAlign = vertical; | 79 | VAlign = vertical; |
80 | SelectedText->setTextAlignment(horizontal, vertical); | 80 | SelectedText->setTextAlignment(horizontal, vertical); |
81 | } | 81 | } |
82 | 82 | ||
83 | 83 | ||
84 | //! Set the maximal number of rows for the selection listbox | 84 | //! Set the maximal number of rows for the selection listbox |
85 | void CGUIComboBox::setMaxSelectionRows(u32 max) | 85 | void CGUIComboBox::setMaxSelectionRows(u32 max) |
86 | { | 86 | { |
87 | MaxSelectionRows = max; | 87 | MaxSelectionRows = max; |
88 | 88 | ||
89 | // force recalculation of open listbox | 89 | // force recalculation of open listbox |
90 | if (ListBox) | 90 | if (ListBox) |
91 | { | 91 | { |
92 | openCloseMenu(); | 92 | openCloseMenu(); |
93 | openCloseMenu(); | 93 | openCloseMenu(); |
94 | } | 94 | } |
95 | } | 95 | } |
96 | 96 | ||
97 | //! Get the maximimal number of rows for the selection listbox | 97 | //! Get the maximimal number of rows for the selection listbox |
98 | u32 CGUIComboBox::getMaxSelectionRows() const | 98 | u32 CGUIComboBox::getMaxSelectionRows() const |
99 | { | 99 | { |
100 | return MaxSelectionRows; | 100 | return MaxSelectionRows; |
101 | } | 101 | } |
102 | 102 | ||
103 | 103 | ||
104 | //! Returns amount of items in box | 104 | //! Returns amount of items in box |
105 | u32 CGUIComboBox::getItemCount() const | 105 | u32 CGUIComboBox::getItemCount() const |
106 | { | 106 | { |
107 | return Items.size(); | 107 | return Items.size(); |
108 | } | 108 | } |
109 | 109 | ||
110 | 110 | ||
111 | //! returns string of an item. the idx may be a value from 0 to itemCount-1 | 111 | //! returns string of an item. the idx may be a value from 0 to itemCount-1 |
112 | const wchar_t* CGUIComboBox::getItem(u32 idx) const | 112 | const wchar_t* CGUIComboBox::getItem(u32 idx) const |
113 | { | 113 | { |
114 | if (idx >= Items.size()) | 114 | if (idx >= Items.size()) |
115 | return 0; | 115 | return 0; |
116 | 116 | ||
117 | return Items[idx].Name.c_str(); | 117 | return Items[idx].Name.c_str(); |
118 | } | 118 | } |
119 | 119 | ||
120 | //! returns string of an item. the idx may be a value from 0 to itemCount-1 | 120 | //! returns string of an item. the idx may be a value from 0 to itemCount-1 |
121 | u32 CGUIComboBox::getItemData(u32 idx) const | 121 | u32 CGUIComboBox::getItemData(u32 idx) const |
122 | { | 122 | { |
123 | if (idx >= Items.size()) | 123 | if (idx >= Items.size()) |
124 | return 0; | 124 | return 0; |
125 | 125 | ||
126 | return Items[idx].Data; | 126 | return Items[idx].Data; |
127 | } | 127 | } |
128 | 128 | ||
129 | //! Returns index based on item data | 129 | //! Returns index based on item data |
130 | s32 CGUIComboBox::getIndexForItemData(u32 data ) const | 130 | s32 CGUIComboBox::getIndexForItemData(u32 data ) const |
131 | { | 131 | { |
132 | for ( u32 i = 0; i < Items.size (); ++i ) | 132 | for ( u32 i = 0; i < Items.size (); ++i ) |
133 | { | 133 | { |
134 | if ( Items[i].Data == data ) | 134 | if ( Items[i].Data == data ) |
135 | return i; | 135 | return i; |
136 | } | 136 | } |
137 | return -1; | 137 | return -1; |
138 | } | 138 | } |
139 | 139 | ||
140 | 140 | ||
141 | //! Removes an item from the combo box. | 141 | //! Removes an item from the combo box. |
142 | void CGUIComboBox::removeItem(u32 idx) | 142 | void CGUIComboBox::removeItem(u32 idx) |
143 | { | 143 | { |
144 | if (idx >= Items.size()) | 144 | if (idx >= Items.size()) |
145 | return; | 145 | return; |
146 | 146 | ||
147 | if (Selected == (s32)idx) | 147 | if (Selected == (s32)idx) |
148 | setSelected(-1); | 148 | setSelected(-1); |
149 | 149 | ||
150 | Items.erase(idx); | 150 | Items.erase(idx); |
151 | } | 151 | } |
152 | 152 | ||
153 | 153 | ||
154 | //! Returns caption of this element. | 154 | //! Returns caption of this element. |
155 | const wchar_t* CGUIComboBox::getText() const | 155 | const wchar_t* CGUIComboBox::getText() const |
156 | { | 156 | { |
157 | return getItem(Selected); | 157 | return getItem(Selected); |
158 | } | 158 | } |
159 | 159 | ||
160 | 160 | ||
161 | //! adds an item and returns the index of it | 161 | //! adds an item and returns the index of it |
162 | u32 CGUIComboBox::addItem(const wchar_t* text, u32 data) | 162 | u32 CGUIComboBox::addItem(const wchar_t* text, u32 data) |
163 | { | 163 | { |
164 | Items.push_back( SComboData ( text, data ) ); | 164 | Items.push_back( SComboData ( text, data ) ); |
165 | 165 | ||
166 | if (Selected == -1) | 166 | if (Selected == -1) |
167 | setSelected(0); | 167 | setSelected(0); |
168 | 168 | ||
169 | return Items.size() - 1; | 169 | return Items.size() - 1; |
170 | } | 170 | } |
171 | 171 | ||
172 | 172 | ||
173 | //! deletes all items in the combo box | 173 | //! deletes all items in the combo box |
174 | void CGUIComboBox::clear() | 174 | void CGUIComboBox::clear() |
175 | { | 175 | { |
176 | Items.clear(); | 176 | Items.clear(); |
177 | setSelected(-1); | 177 | setSelected(-1); |
178 | } | 178 | } |
179 | 179 | ||
180 | 180 | ||
181 | //! returns id of selected item. returns -1 if no item is selected. | 181 | //! returns id of selected item. returns -1 if no item is selected. |
182 | s32 CGUIComboBox::getSelected() const | 182 | s32 CGUIComboBox::getSelected() const |
183 | { | 183 | { |
184 | return Selected; | 184 | return Selected; |
185 | } | 185 | } |
186 | 186 | ||
187 | 187 | ||
188 | //! sets the selected item. Set this to -1 if no item should be selected | 188 | //! sets the selected item. Set this to -1 if no item should be selected |
189 | void CGUIComboBox::setSelected(s32 idx) | 189 | void CGUIComboBox::setSelected(s32 idx) |
190 | { | 190 | { |
191 | if (idx < -1 || idx >= (s32)Items.size()) | 191 | if (idx < -1 || idx >= (s32)Items.size()) |
192 | return; | 192 | return; |
193 | 193 | ||
194 | Selected = idx; | 194 | Selected = idx; |
195 | if (Selected == -1) | 195 | if (Selected == -1) |
196 | SelectedText->setText(L""); | 196 | SelectedText->setText(L""); |
197 | else | 197 | else |
198 | SelectedText->setText(Items[Selected].Name.c_str()); | 198 | SelectedText->setText(Items[Selected].Name.c_str()); |
199 | } | 199 | } |
200 | 200 | ||
201 | 201 | ||
202 | //! called if an event happened. | 202 | //! called if an event happened. |
203 | bool CGUIComboBox::OnEvent(const SEvent& event) | 203 | bool CGUIComboBox::OnEvent(const SEvent& event) |
204 | { | 204 | { |
205 | if (isEnabled()) | 205 | if (isEnabled()) |
206 | { | 206 | { |
207 | switch(event.EventType) | 207 | switch(event.EventType) |
208 | { | 208 | { |
209 | 209 | ||
210 | case EET_KEY_INPUT_EVENT: | 210 | case EET_KEY_INPUT_EVENT: |
211 | if (ListBox && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE) | 211 | if (ListBox && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE) |
212 | { | 212 | { |
213 | // hide list box | 213 | // hide list box |
214 | openCloseMenu(); | 214 | openCloseMenu(); |
215 | return true; | 215 | return true; |
216 | } | 216 | } |
217 | else | 217 | else |
218 | if (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE) | 218 | if (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE) |
219 | { | 219 | { |
220 | if (!event.KeyInput.PressedDown) | 220 | if (!event.KeyInput.PressedDown) |
221 | { | 221 | { |
222 | openCloseMenu(); | 222 | openCloseMenu(); |
223 | } | 223 | } |
224 | 224 | ||
225 | ListButton->setPressed(ListBox == 0); | 225 | ListButton->setPressed(ListBox == 0); |
226 | 226 | ||
227 | return true; | 227 | return true; |
228 | } | 228 | } |
229 | else | 229 | else |
230 | if (event.KeyInput.PressedDown) | 230 | if (event.KeyInput.PressedDown) |
231 | { | 231 | { |
232 | s32 oldSelected = Selected; | 232 | s32 oldSelected = Selected; |
233 | bool absorb = true; | 233 | bool absorb = true; |
234 | switch (event.KeyInput.Key) | 234 | switch (event.KeyInput.Key) |
235 | { | 235 | { |
236 | case KEY_DOWN: | 236 | case KEY_DOWN: |
237 | setSelected(Selected+1); | 237 | setSelected(Selected+1); |
238 | break; | 238 | break; |
239 | case KEY_UP: | 239 | case KEY_UP: |
240 | setSelected(Selected-1); | 240 | setSelected(Selected-1); |
241 | break; | 241 | break; |
242 | case KEY_HOME: | 242 | case KEY_HOME: |
243 | case KEY_PRIOR: | 243 | case KEY_PRIOR: |
244 | setSelected(0); | 244 | setSelected(0); |
245 | break; | 245 | break; |
246 | case KEY_END: | 246 | case KEY_END: |
247 | case KEY_NEXT: | 247 | case KEY_NEXT: |
248 | setSelected((s32)Items.size()-1); | 248 | setSelected((s32)Items.size()-1); |
249 | break; | 249 | break; |
250 | default: | 250 | default: |
251 | absorb = false; | 251 | absorb = false; |
252 | } | 252 | } |
253 | 253 | ||
254 | if (Selected <0) | 254 | if (Selected <0) |
255 | setSelected(0); | 255 | setSelected(0); |
256 | 256 | ||
257 | if (Selected >= (s32)Items.size()) | 257 | if (Selected >= (s32)Items.size()) |
258 | setSelected((s32)Items.size() -1); | 258 | setSelected((s32)Items.size() -1); |
259 | 259 | ||
260 | if (Selected != oldSelected) | 260 | if (Selected != oldSelected) |
261 | { | 261 | { |
262 | sendSelectionChangedEvent(); | 262 | sendSelectionChangedEvent(); |
263 | return true; | 263 | return true; |
264 | } | 264 | } |
265 | 265 | ||
266 | if (absorb) | 266 | if (absorb) |
267 | return true; | 267 | return true; |
268 | } | 268 | } |
269 | break; | 269 | break; |
270 | 270 | ||
271 | case EET_GUI_EVENT: | 271 | case EET_GUI_EVENT: |
272 | 272 | ||
273 | switch(event.GUIEvent.EventType) | 273 | switch(event.GUIEvent.EventType) |
274 | { | 274 | { |
275 | case EGET_ELEMENT_FOCUS_LOST: | 275 | case EGET_ELEMENT_FOCUS_LOST: |
276 | if (ListBox && | 276 | if (ListBox && |
277 | (Environment->hasFocus(ListBox) || ListBox->isMyChild(event.GUIEvent.Caller) ) && | 277 | (Environment->hasFocus(ListBox) || ListBox->isMyChild(event.GUIEvent.Caller) ) && |
278 | event.GUIEvent.Element != this && | 278 | event.GUIEvent.Element != this && |
279 | !isMyChild(event.GUIEvent.Element) && | 279 | !isMyChild(event.GUIEvent.Element) && |
280 | !ListBox->isMyChild(event.GUIEvent.Element)) | 280 | !ListBox->isMyChild(event.GUIEvent.Element)) |
281 | { | 281 | { |
282 | openCloseMenu(); | 282 | openCloseMenu(); |
283 | } | 283 | } |
284 | break; | 284 | break; |
285 | case EGET_BUTTON_CLICKED: | 285 | case EGET_BUTTON_CLICKED: |
286 | if (event.GUIEvent.Caller == ListButton) | 286 | if (event.GUIEvent.Caller == ListButton) |
287 | { | 287 | { |
288 | openCloseMenu(); | 288 | openCloseMenu(); |
289 | return true; | 289 | return true; |
290 | } | 290 | } |
291 | break; | 291 | break; |
292 | case EGET_LISTBOX_SELECTED_AGAIN: | 292 | case EGET_LISTBOX_SELECTED_AGAIN: |
293 | case EGET_LISTBOX_CHANGED: | 293 | case EGET_LISTBOX_CHANGED: |
294 | if (event.GUIEvent.Caller == ListBox) | 294 | if (event.GUIEvent.Caller == ListBox) |
295 | { | 295 | { |
296 | setSelected(ListBox->getSelected()); | 296 | setSelected(ListBox->getSelected()); |
297 | if (Selected <0 || Selected >= (s32)Items.size()) | 297 | if (Selected <0 || Selected >= (s32)Items.size()) |
298 | setSelected(-1); | 298 | setSelected(-1); |
299 | openCloseMenu(); | 299 | openCloseMenu(); |
300 | 300 | ||
301 | sendSelectionChangedEvent(); | 301 | sendSelectionChangedEvent(); |
302 | } | 302 | } |
303 | return true; | 303 | return true; |
304 | default: | 304 | default: |
305 | break; | 305 | break; |
306 | } | 306 | } |
307 | break; | 307 | break; |
308 | case EET_MOUSE_INPUT_EVENT: | 308 | case EET_MOUSE_INPUT_EVENT: |
309 | 309 | ||
310 | switch(event.MouseInput.Event) | 310 | switch(event.MouseInput.Event) |
311 | { | 311 | { |
312 | case EMIE_LMOUSE_PRESSED_DOWN: | 312 | case EMIE_LMOUSE_PRESSED_DOWN: |
313 | { | 313 | { |
314 | core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y); | 314 | core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y); |
315 | 315 | ||
316 | // send to list box | 316 | // send to list box |
317 | if (ListBox && ListBox->isPointInside(p) && ListBox->OnEvent(event)) | 317 | if (ListBox && ListBox->isPointInside(p) && ListBox->OnEvent(event)) |
318 | return true; | 318 | return true; |
319 | 319 | ||
320 | return true; | 320 | return true; |
321 | } | 321 | } |
322 | case EMIE_LMOUSE_LEFT_UP: | 322 | case EMIE_LMOUSE_LEFT_UP: |
323 | { | 323 | { |
324 | core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y); | 324 | core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y); |
325 | 325 | ||
326 | // send to list box | 326 | // send to list box |
327 | if (!(ListBox && | 327 | if (!(ListBox && |
328 | ListBox->getAbsolutePosition().isPointInside(p) && | 328 | ListBox->getAbsolutePosition().isPointInside(p) && |
329 | ListBox->OnEvent(event))) | 329 | ListBox->OnEvent(event))) |
330 | { | 330 | { |
331 | openCloseMenu(); | 331 | openCloseMenu(); |
332 | } | 332 | } |
333 | return true; | 333 | return true; |
334 | } | 334 | } |
335 | case EMIE_MOUSE_WHEEL: | 335 | case EMIE_MOUSE_WHEEL: |
336 | { | 336 | { |
337 | s32 oldSelected = Selected; | 337 | s32 oldSelected = Selected; |
338 | setSelected( Selected + ((event.MouseInput.Wheel < 0) ? 1 : -1)); | 338 | setSelected( Selected + ((event.MouseInput.Wheel < 0) ? 1 : -1)); |
339 | 339 | ||
340 | if (Selected <0) | 340 | if (Selected <0) |
341 | setSelected(0); | 341 | setSelected(0); |
342 | 342 | ||
343 | if (Selected >= (s32)Items.size()) | 343 | if (Selected >= (s32)Items.size()) |
344 | setSelected((s32)Items.size() -1); | 344 | setSelected((s32)Items.size() -1); |
345 | 345 | ||
346 | if (Selected != oldSelected) | 346 | if (Selected != oldSelected) |
347 | { | 347 | { |
348 | sendSelectionChangedEvent(); | 348 | sendSelectionChangedEvent(); |
349 | return true; | 349 | return true; |
350 | } | 350 | } |
351 | } | 351 | } |
352 | default: | 352 | default: |
353 | break; | 353 | break; |
354 | } | 354 | } |
355 | break; | 355 | break; |
356 | default: | 356 | default: |
357 | break; | 357 | break; |
358 | } | 358 | } |
359 | } | 359 | } |
360 | 360 | ||
361 | return IGUIElement::OnEvent(event); | 361 | return IGUIElement::OnEvent(event); |
362 | } | 362 | } |
363 | 363 | ||
364 | 364 | ||
365 | void CGUIComboBox::sendSelectionChangedEvent() | 365 | void CGUIComboBox::sendSelectionChangedEvent() |
366 | { | 366 | { |
367 | if (Parent) | 367 | if (Parent) |
368 | { | 368 | { |
369 | SEvent event; | 369 | SEvent event; |
370 | 370 | ||
371 | event.EventType = EET_GUI_EVENT; | 371 | event.EventType = EET_GUI_EVENT; |
372 | event.GUIEvent.Caller = this; | 372 | event.GUIEvent.Caller = this; |
373 | event.GUIEvent.Element = 0; | 373 | event.GUIEvent.Element = 0; |
374 | event.GUIEvent.EventType = EGET_COMBO_BOX_CHANGED; | 374 | event.GUIEvent.EventType = EGET_COMBO_BOX_CHANGED; |
375 | Parent->OnEvent(event); | 375 | Parent->OnEvent(event); |
376 | } | 376 | } |
377 | } | 377 | } |
378 | 378 | ||
379 | 379 | ||
380 | //! draws the element and its children | 380 | //! draws the element and its children |
381 | void CGUIComboBox::draw() | 381 | void CGUIComboBox::draw() |
382 | { | 382 | { |
383 | if (!IsVisible) | 383 | if (!IsVisible) |
384 | return; | 384 | return; |
385 | 385 | ||
386 | IGUISkin* skin = Environment->getSkin(); | 386 | IGUISkin* skin = Environment->getSkin(); |
387 | IGUIElement *currentFocus = Environment->getFocus(); | 387 | IGUIElement *currentFocus = Environment->getFocus(); |
388 | if (currentFocus != LastFocus) | 388 | if (currentFocus != LastFocus) |
389 | { | 389 | { |
390 | HasFocus = currentFocus == this || isMyChild(currentFocus); | 390 | HasFocus = currentFocus == this || isMyChild(currentFocus); |
391 | LastFocus = currentFocus; | 391 | LastFocus = currentFocus; |
392 | } | 392 | } |
393 | 393 | ||
394 | // set colors each time as skin-colors can be changed | 394 | // set colors each time as skin-colors can be changed |
395 | SelectedText->setBackgroundColor(skin->getColor(EGDC_HIGH_LIGHT)); | 395 | SelectedText->setBackgroundColor(skin->getColor(EGDC_HIGH_LIGHT)); |
396 | if(isEnabled()) | 396 | if(isEnabled()) |
397 | { | 397 | { |
398 | SelectedText->setDrawBackground(HasFocus); | 398 | SelectedText->setDrawBackground(HasFocus); |
399 | SelectedText->setOverrideColor(skin->getColor(HasFocus ? EGDC_HIGH_LIGHT_TEXT : EGDC_BUTTON_TEXT)); | 399 | SelectedText->setOverrideColor(skin->getColor(HasFocus ? EGDC_HIGH_LIGHT_TEXT : EGDC_BUTTON_TEXT)); |
400 | } | 400 | } |
401 | else | 401 | else |
402 | { | 402 | { |
403 | SelectedText->setDrawBackground(false); | 403 | SelectedText->setDrawBackground(false); |
404 | SelectedText->setOverrideColor(skin->getColor(EGDC_GRAY_TEXT)); | 404 | SelectedText->setOverrideColor(skin->getColor(EGDC_GRAY_TEXT)); |
405 | } | 405 | } |
406 | ListButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL)); | 406 | ListButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL)); |
407 | ListButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL)); | 407 | ListButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_DOWN), skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL)); |
408 | 408 | ||
409 | 409 | ||
410 | core::rect<s32> frameRect(AbsoluteRect); | 410 | core::rect<s32> frameRect(AbsoluteRect); |
411 | 411 | ||
412 | // draw the border | 412 | // draw the border |
413 | 413 | ||
414 | skin->draw3DSunkenPane(this, skin->getColor(EGDC_3D_HIGH_LIGHT), | 414 | skin->draw3DSunkenPane(this, skin->getColor(EGDC_3D_HIGH_LIGHT), |
415 | true, true, frameRect, &AbsoluteClippingRect); | 415 | true, true, frameRect, &AbsoluteClippingRect); |
416 | 416 | ||
417 | // draw children | 417 | // draw children |
418 | IGUIElement::draw(); | 418 | IGUIElement::draw(); |
419 | } | 419 | } |
420 | 420 | ||
421 | 421 | ||
422 | void CGUIComboBox::openCloseMenu() | 422 | void CGUIComboBox::openCloseMenu() |
423 | { | 423 | { |
424 | if (ListBox) | 424 | if (ListBox) |
425 | { | 425 | { |
426 | // close list box | 426 | // close list box |
427 | Environment->setFocus(this); | 427 | Environment->setFocus(this); |
428 | ListBox->remove(); | 428 | ListBox->remove(); |
429 | ListBox = 0; | 429 | ListBox = 0; |
430 | } | 430 | } |
431 | else | 431 | else |
432 | { | 432 | { |
433 | if (Parent) | 433 | if (Parent) |
434 | Parent->bringToFront(this); | 434 | Parent->bringToFront(this); |
435 | 435 | ||
436 | IGUISkin* skin = Environment->getSkin(); | 436 | IGUISkin* skin = Environment->getSkin(); |
437 | u32 h = Items.size(); | 437 | u32 h = Items.size(); |
438 | 438 | ||
439 | if (h > getMaxSelectionRows()) | 439 | if (h > getMaxSelectionRows()) |
440 | h = getMaxSelectionRows(); | 440 | h = getMaxSelectionRows(); |
441 | if (h == 0) | 441 | if (h == 0) |
442 | h = 1; | 442 | h = 1; |
443 | 443 | ||
444 | IGUIFont* font = skin->getFont(); | 444 | IGUIFont* font = skin->getFont(); |
445 | if (font) | 445 | if (font) |
446 | h *= (font->getDimension(L"A").Height + 4); | 446 | h *= (font->getDimension(L"A").Height + 4); |
447 | 447 | ||
448 | // open list box | 448 | // open list box |
449 | core::rect<s32> r(0, AbsoluteRect.getHeight(), | 449 | core::rect<s32> r(0, AbsoluteRect.getHeight(), |
450 | AbsoluteRect.getWidth(), AbsoluteRect.getHeight() + h); | 450 | AbsoluteRect.getWidth(), AbsoluteRect.getHeight() + h); |
451 | 451 | ||
452 | ListBox = new CGUIListBox(Environment, this, -1, r, false, true, true); | 452 | ListBox = new CGUIListBox(Environment, this, -1, r, false, true, true); |
453 | ListBox->setSubElement(true); | 453 | ListBox->setSubElement(true); |
454 | ListBox->setNotClipped(true); | 454 | ListBox->setNotClipped(true); |
455 | ListBox->drop(); | 455 | ListBox->drop(); |
456 | 456 | ||
457 | // ensure that list box is always completely visible | 457 | // ensure that list box is always completely visible |
458 | if (ListBox->getAbsolutePosition().LowerRightCorner.Y > Environment->getRootGUIElement()->getAbsolutePosition().getHeight()) | 458 | if (ListBox->getAbsolutePosition().LowerRightCorner.Y > Environment->getRootGUIElement()->getAbsolutePosition().getHeight()) |
459 | ListBox->setRelativePosition( core::rect<s32>(0, -ListBox->getAbsolutePosition().getHeight(), AbsoluteRect.getWidth(), 0) ); | 459 | ListBox->setRelativePosition( core::rect<s32>(0, -ListBox->getAbsolutePosition().getHeight(), AbsoluteRect.getWidth(), 0) ); |
460 | 460 | ||
461 | for (s32 i=0; i<(s32)Items.size(); ++i) | 461 | for (s32 i=0; i<(s32)Items.size(); ++i) |
462 | ListBox->addItem(Items[i].Name.c_str()); | 462 | ListBox->addItem(Items[i].Name.c_str()); |
463 | 463 | ||
464 | ListBox->setSelected(Selected); | 464 | ListBox->setSelected(Selected); |
465 | 465 | ||
466 | // set focus | 466 | // set focus |
467 | Environment->setFocus(ListBox); | 467 | Environment->setFocus(ListBox); |
468 | } | 468 | } |
469 | } | 469 | } |
470 | 470 | ||
471 | 471 | ||
472 | //! Writes attributes of the element. | 472 | //! Writes attributes of the element. |
473 | void CGUIComboBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const | 473 | void CGUIComboBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const |
474 | { | 474 | { |
475 | IGUIComboBox::serializeAttributes(out,options); | 475 | IGUIComboBox::serializeAttributes(out,options); |
476 | 476 | ||
477 | out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames); | 477 | out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames); |
478 | out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames); | 478 | out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames); |
479 | out->addInt("MaxSelectionRows", (s32)MaxSelectionRows ); | 479 | out->addInt("MaxSelectionRows", (s32)MaxSelectionRows ); |
480 | 480 | ||
481 | out->addInt ("Selected", Selected ); | 481 | out->addInt ("Selected", Selected ); |
482 | out->addInt ("ItemCount", Items.size()); | 482 | out->addInt ("ItemCount", Items.size()); |
483 | for (u32 i=0; i < Items.size(); ++i) | 483 | for (u32 i=0; i < Items.size(); ++i) |
484 | { | 484 | { |
485 | core::stringc s = "Item"; | 485 | core::stringc s = "Item"; |
486 | s += i; | 486 | s += i; |
487 | s += "Text"; | 487 | s += "Text"; |
488 | out->addString(s.c_str(), Items[i].Name.c_str()); | 488 | out->addString(s.c_str(), Items[i].Name.c_str()); |
489 | } | 489 | } |
490 | } | 490 | } |
491 | 491 | ||
492 | 492 | ||
493 | //! Reads attributes of the element | 493 | //! Reads attributes of the element |
494 | void CGUIComboBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) | 494 | void CGUIComboBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) |
495 | { | 495 | { |
496 | IGUIComboBox::deserializeAttributes(in,options); | 496 | IGUIComboBox::deserializeAttributes(in,options); |
497 | 497 | ||
498 | setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames), | 498 | setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames), |
499 | (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames)); | 499 | (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames)); |
500 | setMaxSelectionRows( (u32)(in->getAttributeAsInt("MaxSelectionRows")) ); | 500 | setMaxSelectionRows( (u32)(in->getAttributeAsInt("MaxSelectionRows")) ); |
501 | 501 | ||
502 | // clear the list | 502 | // clear the list |
503 | clear(); | 503 | clear(); |
504 | // get item count | 504 | // get item count |
505 | u32 c = in->getAttributeAsInt("ItemCount"); | 505 | u32 c = in->getAttributeAsInt("ItemCount"); |
506 | // add items | 506 | // add items |
507 | for (u32 i=0; i < c; ++i) | 507 | for (u32 i=0; i < c; ++i) |
508 | { | 508 | { |
509 | core::stringc s = "Item"; | 509 | core::stringc s = "Item"; |
510 | s += i; | 510 | s += i; |
511 | s += "Text"; | 511 | s += "Text"; |
512 | addItem(in->getAttributeAsStringW(s.c_str()).c_str(), 0); | 512 | addItem(in->getAttributeAsStringW(s.c_str()).c_str(), 0); |
513 | } | 513 | } |
514 | 514 | ||
515 | setSelected(in->getAttributeAsInt("Selected")); | 515 | setSelected(in->getAttributeAsInt("Selected")); |
516 | } | 516 | } |
517 | 517 | ||
518 | } // end namespace gui | 518 | } // end namespace gui |
519 | } // end namespace irr | 519 | } // end namespace irr |
520 | 520 | ||
521 | 521 | ||
522 | #endif // _IRR_COMPILE_WITH_GUI_ | 522 | #endif // _IRR_COMPILE_WITH_GUI_ |
523 | 523 | ||