MyGUI 3.5.3
MyGUI_ComboBox.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_ComboBox.h"
10#include "MyGUI_InputManager.h"
11#include "MyGUI_WidgetManager.h"
12#include "MyGUI_ListBox.h"
13#include "MyGUI_Button.h"
14#include "MyGUI_ResourceSkin.h"
15#include "MyGUI_LayerManager.h"
17
18namespace MyGUI
19{
20
23 const float COMBO_ALPHA_COEF = 4.0f;
24
25 void ComboBox::initialiseOverride()
26 {
27 Base::initialiseOverride();
28
30 assignWidget(mButton, "Button");
31 if (mButton != nullptr)
32 {
33 mButton->eventMouseButtonPressed += newDelegate(this, &ComboBox::notifyButtonPressed);
34 }
35
37 assignWidget(mList, "List");
38
39 if (mList == nullptr)
40 {
41 std::string_view list_skin = getUserString("ListSkin");
42 std::string_view list_layer = getUserString("ListLayer");
43
44 mList = static_cast<ListBox*>(_createSkinWidget(
45 WidgetStyle::Popup,
46 ListBox::getClassTypeName(),
47 list_skin,
48 IntCoord(),
49 Align::Default,
50 list_layer));
51 }
52
53 if (mList != nullptr)
54 {
55 mList->setActivateOnClick(true);
56
57 mList->setVisible(false);
58 mList->eventKeyLostFocus += newDelegate(this, &ComboBox::notifyListLostFocus);
59 mList->eventListSelectAccept += newDelegate(this, &ComboBox::notifyListSelectAccept);
60 mList->eventListMouseItemActivate += newDelegate(this, &ComboBox::notifyListMouseItemActivate);
61 mList->eventListChangePosition += newDelegate(this, &ComboBox::notifyListChangePosition);
62
63 mList->setNeedToolTip(true);
64 mList->eventToolTip += newDelegate(this, &ComboBox::notifyToolTip);
65 }
66
67 // subscribe child classes to scroll
68 if (mScrollViewClient != nullptr)
69 {
70 mScrollViewClient->eventMouseWheel += newDelegate(this, &ComboBox::notifyMouseWheel);
71 mScrollViewClient->eventMouseButtonPressed += newDelegate(this, &ComboBox::notifyMousePressed);
72
73 mScrollViewClient->setNeedToolTip(true);
74 mScrollViewClient->eventToolTip += newDelegate(this, &ComboBox::notifyToolTip);
75 }
76
77 // subscribe to text changes
78 eventEditTextChange += newDelegate(this, &ComboBox::notifyEditTextChange);
79 }
80
81 void ComboBox::shutdownOverride()
82 {
83 mListShow = false;
84 eventChangeAbsoluteCoord -= newDelegate(this, &ComboBox::notifyChangeCoord);
85
86 mList = nullptr;
87 mButton = nullptr;
88
89 Base::shutdownOverride();
90 }
91
92 void ComboBox::notifyButtonPressed(Widget* _sender, int _left, int _top, MouseButton _id)
93 {
94 if (MouseButton::Left != _id)
95 return;
96
97 mDropMouse = true;
98
99 if (mListShow)
100 hideList();
101 else
102 showList();
103 }
104
105 void ComboBox::notifyListLostFocus(Widget* _sender, Widget* _new)
106 {
107 if (mDropMouse)
108 {
109 mDropMouse = false;
110 Widget* focus = InputManager::getInstance().getMouseFocusWidget();
111
112 // button itself will hide the list
113 if (focus == mButton)
114 return;
115
116 // in drop mode all windows participate
117 if (mModeDrop && focus == mScrollViewClient)
118 return;
119 }
120
121 hideList();
122 }
123
124 void ComboBox::notifyListSelectAccept(ListBox* _widget, size_t _position)
125 {
126 mItemIndex = _position;
127 Base::setCaption(mItemIndex != ITEM_NONE ? mList->getItemNameAt(mItemIndex) : UString());
128
129 mDropMouse = false;
130 InputManager::getInstance().setKeyFocusWidget(this);
131
132 if (mModeDrop)
133 {
134 _resetContainer(false);
135
136 eventComboAccept(this, mItemIndex);
137 }
138 }
139
140 void ComboBox::notifyListChangePosition(ListBox* _widget, size_t _position)
141 {
142 mItemIndex = _position;
143
144 _resetContainer(false);
145
146 eventComboChangePosition(this, _position);
147 }
148
149 void ComboBox::onKeyButtonPressed(KeyCode _key, Char _char)
150 {
151 Base::onKeyButtonPressed(_key, _char);
152
153 if (_key == KeyCode::ArrowDown)
154 {
155 // show list only if mouse is free
156 if (!InputManager::getInstance().isCaptureMouse())
157 {
158 showList();
159 }
160 }
161 else if ((_key == KeyCode::Return) || (_key == KeyCode::NumpadEnter))
162 {
163 _resetContainer(false);
164
165 eventComboAccept(this, mItemIndex);
166 }
167 }
168
169 void ComboBox::notifyListMouseItemActivate(ListBox* _widget, size_t _position)
170 {
171 mItemIndex = _position;
172 Base::setCaption(mItemIndex != ITEM_NONE ? mList->getItemNameAt(mItemIndex) : UString());
173
174 InputManager::getInstance().setKeyFocusWidget(this);
175
176 if (mModeDrop)
177 {
178 _resetContainer(false);
179
180 eventComboAccept(this, mItemIndex);
181 }
182 }
183
184 void ComboBox::notifyMouseWheel(Widget* _sender, int _rel)
185 {
186 if (mList->getItemCount() == 0)
187 return;
188 if (InputManager::getInstance().getKeyFocusWidget() != this)
189 return;
190 if (InputManager::getInstance().isCaptureMouse())
191 return;
192
193 if (_rel > 0)
194 {
195 if (mItemIndex != 0)
196 {
197 if (mItemIndex == ITEM_NONE)
198 mItemIndex = 0;
199 else
200 mItemIndex--;
201 Base::setCaption(mList->getItemNameAt(mItemIndex));
202 mList->setIndexSelected(mItemIndex);
203 mList->beginToItemAt(mItemIndex);
204
205 _resetContainer(false);
206
207 eventComboChangePosition(this, mItemIndex);
208 }
209 }
210 else if (_rel < 0)
211 {
212 if ((mItemIndex + 1) < mList->getItemCount())
213 {
214 if (mItemIndex == ITEM_NONE)
215 mItemIndex = 0;
216 else
217 mItemIndex++;
218 Base::setCaption(mList->getItemNameAt(mItemIndex));
219 mList->setIndexSelected(mItemIndex);
220 mList->beginToItemAt(mItemIndex);
221
222 _resetContainer(false);
223
224 eventComboChangePosition(this, mItemIndex);
225 }
226 }
227 }
228
229 void ComboBox::notifyMousePressed(Widget* _sender, int _left, int _top, MouseButton _id)
230 {
231 // must pass to parent, otherwise we stole focus from it
232 Base::notifyMousePressed(_sender, _left, _top, _id);
233
234 mDropMouse = true;
235
236 if (mModeDrop)
237 notifyButtonPressed(nullptr, _left, _top, _id);
238 }
239
240 void ComboBox::notifyEditTextChange(EditBox* _sender)
241 {
242 // reset selected item
243 if (ITEM_NONE != mItemIndex)
244 {
245 mItemIndex = ITEM_NONE;
246 mList->setIndexSelected(mItemIndex);
247 mList->beginToItemFirst();
248
249 _resetContainer(false);
250
251 eventComboChangePosition(this, mItemIndex);
252 }
253 }
254
255 void ComboBox::showList()
256 {
257 // don't show empty list
258 if (mList->getItemCount() == 0)
259 return;
260
261 if (mListShow)
262 return;
263 mListShow = true;
264
265 IntCoord coord = calculateListPosition();
266 mList->setCoord(coord);
267 eventChangeAbsoluteCoord += newDelegate(this, &ComboBox::notifyChangeCoord);
268
269 if (mShowSmooth)
270 {
271 ControllerFadeAlpha* controller = createControllerFadeAlpha(COMBO_ALPHA_MAX, COMBO_ALPHA_COEF, true);
272 ControllerManager::getInstance().addItem(mList, controller);
273 }
274 else
275 {
276 mList->setVisible(true);
277 }
278
279 InputManager::getInstance().setKeyFocusWidget(mList);
280 }
281
282 void ComboBox::actionWidgetHide(Widget* _widget, ControllerItem* _controller)
283 {
284 _widget->setVisible(false);
285 _widget->setEnabled(true);
286 }
287
288 void ComboBox::hideList()
289 {
290 if (!mListShow)
291 return;
292 mListShow = false;
293 eventChangeAbsoluteCoord -= newDelegate(this, &ComboBox::notifyChangeCoord);
294
295 if (mShowSmooth)
296 {
297 ControllerFadeAlpha* controller = createControllerFadeAlpha(COMBO_ALPHA_MIN, COMBO_ALPHA_COEF, false);
298 controller->eventPostAction += newDelegate(this, &ComboBox::actionWidgetHide);
299 ControllerManager::getInstance().addItem(mList, controller);
300 }
301 else
302 {
303 mList->setVisible(false);
304 }
305 }
306
307 void ComboBox::notifyChangeCoord(Widget* _sender)
308 {
309 // Popup coordinates are absolute, so follow movement of the combo box and its parents.
310 IntCoord coord = calculateListPosition();
311 if (mList->getCoord() != coord)
312 mList->setCoord(coord);
313 }
314
315 void ComboBox::setIndexSelected(size_t _index)
316 {
317 MYGUI_ASSERT_RANGE_AND_NONE(_index, mList->getItemCount(), "ComboBox::setIndexSelected");
318 mItemIndex = _index;
319 mList->setIndexSelected(_index);
320 if (_index == ITEM_NONE)
321 {
322 Base::setCaption(UString());
323 return;
324 }
325 Base::setCaption(mList->getItemNameAt(_index));
326 Base::updateView(); // hook for update
327 }
328
329 void ComboBox::setItemNameAt(size_t _index, const UString& _name)
330 {
331 mList->setItemNameAt(_index, _name);
332 mItemIndex = ITEM_NONE; //FIXME
333 mList->setIndexSelected(mItemIndex); //FIXME
334 }
335
336 void ComboBox::setItemDataAt(size_t _index, Any _data)
337 {
338 mList->setItemDataAt(_index, _data);
339 mItemIndex = ITEM_NONE; //FIXME
340 mList->setIndexSelected(mItemIndex); //FIXME
341 }
342
343 void ComboBox::insertItemAt(size_t _index, const UString& _item, Any _data)
344 {
345 mList->insertItemAt(_index, _item, _data);
346 mItemIndex = ITEM_NONE; //FIXME
347 mList->setIndexSelected(mItemIndex); //FIXME
348 }
349
350 void ComboBox::removeItemAt(size_t _index)
351 {
352 mList->removeItemAt(_index);
353 mItemIndex = ITEM_NONE; //FIXME
354 mList->clearIndexSelected(); //FIXME
355 }
356
357 void ComboBox::removeAllItems()
358 {
359 mItemIndex = ITEM_NONE;
360 mList->removeAllItems();
361 }
362
363 void ComboBox::setComboModeDrop(bool _drop)
364 {
365 mModeDrop = _drop;
366 setEditStatic(mModeDrop);
367 }
368
369 ControllerFadeAlpha* ComboBox::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
370 {
371 ControllerItem* item = ControllerManager::getInstance().createItem(ControllerFadeAlpha::getClassTypeName());
372 ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
373
374 controller->setAlpha(_alpha);
375 controller->setCoef(_coef);
376 controller->setEnabled(_enable);
377
378 return controller;
379 }
380
381 size_t ComboBox::findItemIndexWith(const UString& _name)
382 {
383 return mList->findItemIndexWith(_name);
384 }
385
386 void ComboBox::setFlowDirection(FlowDirection _value)
387 {
388 mFlowDirection = _value;
389 }
390
391 IntCoord ComboBox::calculateListPosition()
392 {
393 int length = 0;
394 if (mFlowDirection.isVertical())
395 length = mList->getOptimalHeight();
396 else
397 length = mMaxListLength;
398
399 if (mMaxListLength > 0 && length > mMaxListLength)
400 length = mMaxListLength;
401
402 IntCoord coord = getAbsoluteCoord();
403 // layer size
404 IntSize sizeView = mList->getParentSize();
405
406 if (mFlowDirection == FlowDirection::TopToBottom)
407 {
408 if ((coord.bottom() + length) <= sizeView.height)
409 coord.top += coord.height;
410 else
411 coord.top -= length;
412 coord.height = length;
413 }
414 else if (mFlowDirection == FlowDirection::BottomToTop)
415 {
416 if ((coord.top - length) >= 0)
417 coord.top -= length;
418 else
419 coord.top += coord.height;
420 coord.height = length;
421 }
422 else if (mFlowDirection == FlowDirection::LeftToRight)
423 {
424 if ((coord.right() + length) <= sizeView.width)
425 coord.left += coord.width;
426 else
427 coord.left -= length;
428 coord.width = length;
429 }
430 else if (mFlowDirection == FlowDirection::RightToLeft)
431 {
432 if ((coord.left - length) >= 0)
433 coord.left -= length;
434 else
435 coord.left += coord.width;
436 coord.width = length;
437 }
438
439 return coord;
440 }
441
442 void ComboBox::setPropertyOverride(std::string_view _key, std::string_view _value)
443 {
445 if (_key == "ModeDrop")
446 setComboModeDrop(utility::parseValue<bool>(_value));
447
449 else if (_key == "FlowDirection")
450 setFlowDirection(utility::parseValue<FlowDirection>(_value));
451
453 else if (_key == "MaxListLength")
454 setMaxListLength(utility::parseValue<int>(_value));
455
457 else if (_key == "ItemHeight")
458 setItemHeight(utility::parseValue<int>(_value));
459
461 else if (_key == "SmoothShow")
462 setSmoothShow(utility::parseValue<bool>(_value));
463
464 // @wproperty{ComboBox, AddItem, string} Adds list item
465 else if (_key == "AddItem")
466 addItem(LanguageManager::getInstance().replaceTags(UString(_value)));
467
468 else
469 {
470 Base::setPropertyOverride(_key, _value);
471 return;
472 }
473
474 eventChangeProperty(this, _key, _value);
475 }
476
477 size_t ComboBox::getItemCount() const
478 {
479 return mList->getItemCount();
480 }
481
482 void ComboBox::addItem(const UString& _name, Any _data)
483 {
484 return insertItemAt(ITEM_NONE, _name, _data);
485 }
486
487 size_t ComboBox::getIndexSelected() const
488 {
489 return mItemIndex;
490 }
491
492 void ComboBox::clearIndexSelected()
493 {
494 setIndexSelected(ITEM_NONE);
495 }
496
497 void ComboBox::clearItemDataAt(size_t _index)
498 {
499 setItemDataAt(_index, Any::Null);
500 }
501
502 const UString& ComboBox::getItemNameAt(size_t _index) const
503 {
504 return mList->getItemNameAt(_index);
505 }
506
507 void ComboBox::beginToItemAt(size_t _index)
508 {
509 mList->beginToItemAt(_index);
510 }
511
512 void ComboBox::beginToItemFirst()
513 {
514 if (getItemCount())
515 beginToItemAt(0);
516 }
517
518 void ComboBox::beginToItemLast()
519 {
520 if (getItemCount())
521 beginToItemAt(getItemCount() - 1);
522 }
523
524 void ComboBox::beginToItemSelected()
525 {
526 if (getIndexSelected() != ITEM_NONE)
527 beginToItemAt(getIndexSelected());
528 }
529
530 bool ComboBox::getComboModeDrop() const
531 {
532 return mModeDrop;
533 }
534
535 void ComboBox::setSmoothShow(bool _value)
536 {
537 mShowSmooth = _value;
538 }
539
540 bool ComboBox::getSmoothShow() const
541 {
542 return mShowSmooth;
543 }
544
545 void ComboBox::setItemHeight(int _height)
546 {
547 _height = std::max(1, _height);
548 if (mList->getItemHeight() == _height)
549 return;
550
551 mList->setItemHeight(_height);
552 if (mListShow)
553 notifyChangeCoord(this);
554 }
555
556 int ComboBox::getItemHeight() const
557 {
558 return mList->getItemHeight();
559 }
560
561 void ComboBox::setMaxListLength(int _value)
562 {
563 mMaxListLength = _value;
564 }
565
566 int ComboBox::getMaxListLength() const
567 {
568 return mMaxListLength;
569 }
570
571 FlowDirection ComboBox::getFlowDirection() const
572 {
573 return mFlowDirection;
574 }
575
576 void ComboBox::notifyToolTip(Widget* _sender, const ToolTipInfo& _info)
577 {
578 if (getNeedToolTip())
579 eventToolTip(this, _info);
580 }
581
582 size_t ComboBox::_getItemCount() const
583 {
584 return getItemCount();
585 }
586
587 void ComboBox::_addItem(const MyGUI::UString& _name)
588 {
589 addItem(_name);
590 }
591
592 void ComboBox::_removeItemAt(size_t _index)
593 {
594 removeItemAt(_index);
595 }
596
597 void ComboBox::_setItemNameAt(size_t _index, const UString& _name)
598 {
599 setItemNameAt(_index, _name);
600 }
601
602 const UString& ComboBox::_getItemNameAt(size_t _index) const
603 {
604 return getItemNameAt(_index);
605 }
606
607 void ComboBox::_resetContainer(bool _update)
608 {
609 Base::_resetContainer(_update);
610 if (mList != nullptr)
611 mList->_resetContainer(_update);
612 }
613
614} // namespace MyGUI
#define MYGUI_ASSERT_RANGE_AND_NONE(index, size, owner)
T parseValue(std::string_view value)
constexpr float ALPHA_MAX
constexpr float ALPHA_MIN
const float COMBO_ALPHA_MAX
const float COMBO_ALPHA_MIN
unsigned int Char
Definition MyGUI_Types.h:49
constexpr size_t ITEM_NONE
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
const float COMBO_ALPHA_COEF
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))