MyGUI 3.5.3
MyGUI_ListBox.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_ListBox.h"
9#include "MyGUI_Button.h"
10#include "MyGUI_ScrollBar.h"
11#include "MyGUI_ResourceSkin.h"
12#include "MyGUI_InputManager.h"
13#include "MyGUI_WidgetManager.h"
15
16namespace MyGUI
17{
18
19 void ListBox::initialiseOverride()
20 {
21 Base::initialiseOverride();
22
23 // FIXME we need key focus
24 setNeedKeyFocus(true);
25
26 if (isUserString("SkinLine"))
27 mSkinLine = getUserString("SkinLine");
28
29 if (isUserString("HeightLine"))
30 mHeightLine = utility::parseInt(getUserString("HeightLine"));
31
32 if (mHeightLine < 1)
33 mHeightLine = 1;
34
35 if (getClientWidget() != nullptr)
36 {
37 getClientWidget()->eventMouseButtonPressed += newDelegate(this, &ListBox::notifyMousePressed);
38 getClientWidget()->eventMouseButtonReleased += newDelegate(this, &ListBox::notifyMouseButtonReleased);
39 getClientWidget()->eventKeyButtonPressed += newDelegate(this, &ListBox::notifyKeyButtonPressed);
40 getClientWidget()->eventKeyButtonReleased += newDelegate(this, &ListBox::notifyKeyButtonReleased);
41 }
42
44 assignWidget(mWidgetScroll, "VScroll");
45 if (mWidgetScroll != nullptr)
46 {
47 mWidgetScroll->eventScrollChangePosition += newDelegate(this, &ListBox::notifyScrollChangePosition);
48 mWidgetScroll->setScrollPage((size_t)mHeightLine);
49 }
50
51 updateScroll();
52 updateLine();
53 }
54
55 void ListBox::shutdownOverride()
56 {
57 mWidgetScroll = nullptr;
58
59 Base::shutdownOverride();
60 }
61
62 void ListBox::onMouseWheel(int _rel)
63 {
64 notifyMouseWheel(nullptr, _rel);
65
66 Base::onMouseWheel(_rel);
67 }
68
69 void ListBox::onKeyButtonPressed(KeyCode _key, Char _char)
70 {
71 if (getItemCount() == 0)
72 {
73 Base::onKeyButtonPressed(_key, _char);
74 eventNotifyItem(this, IBNotifyItemData(ITEM_NONE, IBNotifyItemData::KeyPressed, _key, _char));
75 return;
76 }
77
78 // very secret patented cursor movement mechanism
79 size_t sel = mIndexSelect;
80
81 if (_key == KeyCode::ArrowUp)
82 {
83 if (sel != 0)
84 {
85 if (sel == ITEM_NONE)
86 sel = 0;
87 else
88 sel--;
89 }
90 }
91 else if (_key == KeyCode::ArrowDown)
92 {
93 if (sel == ITEM_NONE)
94 sel = 0;
95 else
96 sel++;
97
98 if (sel >= getItemCount())
99 {
100 sel = mIndexSelect;
101 }
102 }
103 else if (_key == KeyCode::Home)
104 {
105 sel = 0;
106 }
107 else if (_key == KeyCode::End)
108 {
109 sel = getItemCount() - 1;
110 }
111 else if (_key == KeyCode::PageUp)
112 {
113 if (sel != 0)
114 {
115 if (sel == ITEM_NONE)
116 {
117 sel = 0;
118 }
119 else
120 {
121 size_t page = _getClientWidget()->getHeight() / mHeightLine;
122 if (sel <= page)
123 sel = 0;
124 else
125 sel -= page;
126 }
127 }
128 }
129 else if (_key == KeyCode::PageDown)
130 {
131 if (sel != (getItemCount() - 1))
132 {
133 if (sel == ITEM_NONE)
134 {
135 sel = 0;
136 }
137 else
138 {
139 sel += _getClientWidget()->getHeight() / mHeightLine;
140 if (sel >= getItemCount())
141 sel = getItemCount() - 1;
142 }
143 }
144 }
145 else if ((_key == KeyCode::Return) || (_key == KeyCode::NumpadEnter))
146 {
147 if (sel != ITEM_NONE)
148 {
149 //FIXME we may be deleted
150 eventListSelectAccept(this, sel);
151
152 Base::onKeyButtonPressed(_key, _char);
153
154 eventNotifyItem(this, IBNotifyItemData(ITEM_NONE, IBNotifyItemData::KeyPressed, _key, _char));
155 // exit because number of lines changed
156 return;
157 }
158 }
159
160 if (sel != mIndexSelect)
161 {
162 _resetContainer(true);
163
164 if (!isItemVisibleAt(sel))
165 {
166 beginToItemAt(sel);
167 if (mWidgetScroll != nullptr)
168 _sendEventChangeScroll(mWidgetScroll->getScrollPosition());
169 }
170 setIndexSelected(sel);
171
172 // position changed
173 // FIXME we may be deleted
174 eventListChangePosition(this, mIndexSelect);
175 }
176
177 Base::onKeyButtonPressed(_key, _char);
178 eventNotifyItem(this, IBNotifyItemData(ITEM_NONE, IBNotifyItemData::KeyPressed, _key, _char));
179 }
180
181 void ListBox::notifyMouseWheel(Widget* _sender, int _rel)
182 {
183 if (mRangeIndex <= 0)
184 return;
185
186 if (mWidgetScroll == nullptr)
187 return;
188
189 int offset = (int)mWidgetScroll->getScrollPosition();
190 if (_rel < 0)
191 offset += mHeightLine;
192 else
193 offset -= mHeightLine;
194
195 if (offset >= mRangeIndex)
196 offset = mRangeIndex;
197 else if (offset < 0)
198 offset = 0;
199
200 if ((int)mWidgetScroll->getScrollPosition() == offset)
201 return;
202
203 mWidgetScroll->setScrollPosition(offset);
204 _setScrollView(offset);
205 _sendEventChangeScroll(offset);
206
207 _resetContainer(true);
208 }
209
210 void ListBox::notifyScrollChangePosition(ScrollBar* _sender, size_t _position)
211 {
212 _setScrollView(_position);
213 _sendEventChangeScroll(_position);
214 }
215
216 void ListBox::notifyMousePressed(Widget* _sender, int _left, int _top, MouseButton _id)
217 {
218 if (MouseButton::Left == _id && !mActivateOnClick)
219 _activateItem(_sender);
220
221 eventNotifyItem(
222 this,
223 IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::MousePressed, _left, _top, _id));
224 }
225
226 void ListBox::notifyMouseClick(MyGUI::Widget* _sender)
227 {
228 if (mActivateOnClick)
229 _activateItem(_sender);
230 }
231
232 void ListBox::notifyMouseDoubleClick(Widget* _sender)
233 {
234 if (mIndexSelect != ITEM_NONE)
235 eventListSelectAccept(this, mIndexSelect);
236 }
237
238 void ListBox::setPosition(const IntPoint& _point)
239 {
240 Base::setPosition(_point);
241 }
242
243 void ListBox::setSize(const IntSize& _size)
244 {
245 Base::setSize(_size);
246
247 updateScroll();
248 updateLine();
249 }
250
251 void ListBox::setCoord(const IntCoord& _coord)
252 {
253 Base::setCoord(_coord);
254
255 updateScroll();
256 updateLine();
257 }
258
259 void ListBox::updateScroll()
260 {
261 mRangeIndex = (mHeightLine * (int)mItemsInfo.size()) - _getClientWidget()->getHeight();
262
263 if (mWidgetScroll == nullptr)
264 return;
265
266 if ((!mNeedVisibleScroll) || (mRangeIndex < 1) || (mWidgetScroll->getLeft() <= _getClientWidget()->getLeft()))
267 {
268 if (mWidgetScroll->getVisible())
269 {
270 mWidgetScroll->setVisible(false);
271 // increase client area by scroll width
272 if (getClientWidget() != nullptr)
273 getClientWidget()->setSize(
274 getClientWidget()->getWidth() + mWidgetScroll->getWidth(),
275 getClientWidget()->getHeight());
276 }
277 }
278 else if (!mWidgetScroll->getVisible())
279 {
280 if (getClientWidget() != nullptr)
281 getClientWidget()->setSize(
282 getClientWidget()->getWidth() - mWidgetScroll->getWidth(),
283 getClientWidget()->getHeight());
284 mWidgetScroll->setVisible(true);
285 }
286
287 mWidgetScroll->setScrollRange(std::max(0, mRangeIndex) + 1);
288 mWidgetScroll->setScrollViewPage(_getClientWidget()->getHeight());
289 if (!mItemsInfo.empty())
290 mWidgetScroll->setTrackSize(
291 mWidgetScroll->getLineSize() * _getClientWidget()->getHeight() / mHeightLine / (int)mItemsInfo.size());
292 }
293
294 void ListBox::updateLine(bool _reset)
295 {
296 if (_reset)
297 {
298 mOldSize.clear();
299 mLastRedrawLine = 0;
300 _resetContainer(false);
301 }
302
303 size_t position = _getScrollPosition();
304
305 if (mOldSize.height < mCoord.height)
306 {
307 int height = (int)mWidgetLines.size() * mHeightLine - mOffsetTop;
308
309 // until max count reached, always one more
310 while ((height <= (_getClientWidget()->getHeight() + mHeightLine)) &&
311 (mWidgetLines.size() < mItemsInfo.size()))
312 {
313 Widget* widget = _getClientWidget()->createWidgetT(
314 "Button",
315 mSkinLine,
316 0,
317 height,
318 _getClientWidget()->getWidth(),
319 mHeightLine,
320 Align::Top | Align::HStretch);
321 Button* line = widget->castType<Button>();
322 line->eventMouseButtonPressed += newDelegate(this, &ListBox::notifyMousePressed);
323 line->eventMouseButtonReleased += newDelegate(this, &ListBox::notifyMouseButtonReleased);
324 line->eventMouseButtonClick += newDelegate(this, &ListBox::notifyMouseClick);
325 line->eventMouseButtonDoubleClick += newDelegate(this, &ListBox::notifyMouseDoubleClick);
326 line->eventMouseWheel += newDelegate(this, &ListBox::notifyMouseWheel);
327 line->eventKeyButtonPressed += newDelegate(this, &ListBox::notifyKeyButtonPressed);
328 line->eventKeyButtonReleased += newDelegate(this, &ListBox::notifyKeyButtonReleased);
329 line->eventMouseSetFocus += newDelegate(this, &ListBox::notifyMouseSetFocus);
330 line->eventMouseLostFocus += newDelegate(this, &ListBox::notifyMouseLostFocus);
331 line->_setContainer(this);
332 line->_setInternalData((size_t)mWidgetLines.size());
333 mWidgetLines.push_back(line);
334 height += mHeightLine;
335 }
336
337 // check if list position should stay unchanged
338 if (mRangeIndex <= 0 || position >= static_cast<size_t>(mRangeIndex))
339 {
340 if (mRangeIndex <= 0)
341 {
342 if (position || mOffsetTop || mTopIndex)
343 {
344 position = 0;
345 mTopIndex = 0;
346 mOffsetTop = 0;
347 mLastRedrawLine = 0;
348
349 int offset = 0;
350 for (auto& widgetLine : mWidgetLines)
351 {
352 widgetLine->setPosition(0, offset);
353 offset += mHeightLine;
354 }
355 }
356 }
357 else
358 {
359 // push list to bottom border
360 int count = _getClientWidget()->getHeight() / mHeightLine;
361 mOffsetTop = mHeightLine - (_getClientWidget()->getHeight() % mHeightLine);
362
363 if (mOffsetTop == mHeightLine)
364 {
365 mOffsetTop = 0;
366 count--;
367 }
368
369 int top = (int)mItemsInfo.size() - count - 1;
370
371 int offset = 0 - mOffsetTop;
372 for (auto& widgetLine : mWidgetLines)
373 {
374 widgetLine->setPosition(0, offset);
375 offset += mHeightLine;
376 }
377
378 // calculate position, should be maximum
379 position = top * mHeightLine + mOffsetTop;
380
381 if (top != mTopIndex)
382 {
383 mTopIndex = top;
384 _redrawItemRange();
385 }
386 }
387 }
388 }
389
390 // Resizing can expose or hide existing rows without changing the scroll
391 // position. Refresh them all so reused widgets have current contents and state.
392 _redrawItemRange();
393
394 if (mWidgetScroll != nullptr)
395 mWidgetScroll->setScrollPosition(position);
396
397 mOldSize.width = mCoord.width;
398 mOldSize.height = mCoord.height;
399 }
400
401 void ListBox::_redrawItemRange(size_t _start)
402 {
403 // redraw only visible lines
404 size_t pos = _start;
405 for (; pos < mWidgetLines.size(); pos++)
406 {
407 size_t index = pos + (size_t)mTopIndex;
408
409 if (index >= mItemsInfo.size())
410 {
411 mLastRedrawLine = pos;
412 break;
413 }
414 if (mWidgetLines[pos]->getTop() > _getClientWidget()->getHeight())
415 {
416 mLastRedrawLine = pos;
417 break;
418 }
419
420 mWidgetLines[pos]->setVisible(true);
421 mWidgetLines[pos]->setCaption(mItemsInfo[index].first);
422
423 static_cast<Button*>(mWidgetLines[pos])->setStateSelected(index == mIndexSelect);
424 }
425
426 if (pos >= mWidgetLines.size())
427 {
428 mLastRedrawLine = pos;
429 }
430 else
431 {
432 //Widget* focus = InputManager::getInstance().getMouseFocusWidget();
433 for (; pos < mWidgetLines.size(); pos++)
434 {
435 static_cast<Button*>(mWidgetLines[pos])->setStateSelected(false);
436 static_cast<Button*>(mWidgetLines[pos])->setVisible(false);
437 //if (focus == mWidgetLines[pos]) InputManager::getInstance()._unlinkWidget(focus);
438 }
439 }
440 }
441
442 void ListBox::_redrawItem(size_t _index)
443 {
444 if (_index < (size_t)mTopIndex)
445 return;
446 _index -= (size_t)mTopIndex;
447 if (_index >= mLastRedrawLine)
448 return;
449
450 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::_redrawItem");
451 mWidgetLines[_index]->setCaption(mItemsInfo[_index + mTopIndex].first);
452 }
453
454 void ListBox::insertItemAt(size_t _index, const UString& _name, Any _data)
455 {
456 MYGUI_ASSERT_RANGE_INSERT(_index, mItemsInfo.size(), "ListBox::insertItemAt");
457 if (_index == ITEM_NONE)
458 _index = mItemsInfo.size();
459
460 mItemsInfo.insert(mItemsInfo.begin() + _index, PairItem(_name, _data));
461
462 if ((mIndexSelect != ITEM_NONE) && (_index <= mIndexSelect))
463 mIndexSelect++;
464
465 if ((_index <= (size_t)mTopIndex) && (mRangeIndex > 0))
466 {
467 mTopIndex++;
468 if (mWidgetScroll != nullptr)
469 {
470 mWidgetScroll->setScrollRange(mWidgetScroll->getScrollRange() + mHeightLine);
471 if (!mItemsInfo.empty())
472 mWidgetScroll->setTrackSize(
473 mWidgetScroll->getLineSize() * _getClientWidget()->getHeight() / mHeightLine /
474 (int)mItemsInfo.size());
475 mWidgetScroll->setScrollPosition(_getScrollPosition());
476 }
477 mRangeIndex += mHeightLine;
478 updateLine(true);
479 }
480 else
481 {
482 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
483
484 // line after last visible element, plus one (scroll needs one more)
485 if (_getClientWidget()->getHeight() < (offset - mHeightLine))
486 {
487 if (mWidgetScroll != nullptr)
488 {
489 mWidgetScroll->setScrollRange(mWidgetScroll->getScrollRange() + mHeightLine);
490 if (!mItemsInfo.empty())
491 mWidgetScroll->setTrackSize(
492 mWidgetScroll->getLineSize() * _getClientWidget()->getHeight() / mHeightLine /
493 (int)mItemsInfo.size());
494 mWidgetScroll->setScrollPosition(_getScrollPosition());
495 }
496 mRangeIndex += mHeightLine;
497 }
498 else
499 {
500 updateScroll();
501 updateLine(true);
502
503 // later add optimization for number of redraws
504 }
505 }
506 }
507
508 void ListBox::removeItemAt(size_t _index)
509 {
510 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::removeItemAt");
511
512 mItemsInfo.erase(mItemsInfo.begin() + _index);
513
514 if (mItemsInfo.empty())
515 mIndexSelect = ITEM_NONE;
516 else if (mIndexSelect != ITEM_NONE)
517 {
518 if (_index < mIndexSelect)
519 mIndexSelect--;
520 else if ((_index == mIndexSelect) && (mIndexSelect == (mItemsInfo.size())))
521 mIndexSelect--;
522 }
523
524 // if more widgets than items, hide the last one
525 if (mWidgetLines.size() > mItemsInfo.size())
526 {
527 mWidgetLines[mItemsInfo.size()]->setVisible(false);
528 }
529
530 if (_index < (size_t)mTopIndex)
531 {
532 mTopIndex--;
533 if (mWidgetScroll != nullptr)
534 {
535 mWidgetScroll->setScrollRange(mWidgetScroll->getScrollRange() - mHeightLine);
536 if (!mItemsInfo.empty())
537 mWidgetScroll->setTrackSize(
538 mWidgetScroll->getLineSize() * _getClientWidget()->getHeight() / mHeightLine /
539 (int)mItemsInfo.size());
540 mWidgetScroll->setScrollPosition(_getScrollPosition());
541 }
542 mRangeIndex -= mHeightLine;
543 }
544 else
545 {
546 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
547
548 if (_getClientWidget()->getHeight() < offset)
549 {
550 if (mWidgetScroll != nullptr)
551 {
552 mWidgetScroll->setScrollRange(mWidgetScroll->getScrollRange() - mHeightLine);
553 if (!mItemsInfo.empty())
554 mWidgetScroll->setTrackSize(
555 mWidgetScroll->getLineSize() * _getClientWidget()->getHeight() / mHeightLine /
556 (int)mItemsInfo.size());
557 mWidgetScroll->setScrollPosition(_getScrollPosition());
558 }
559 mRangeIndex -= mHeightLine;
560 }
561 else
562 {
563 updateScroll();
564 updateLine(true);
565
566 // later add optimization for number of redraws
567 }
568 }
569
570 // Removing the selected item can select its neighbour. The neighbour may
571 // already have a reused row even when the removed item was outside the view.
572 _redrawItemRange();
573 }
574
575 void ListBox::setIndexSelected(size_t _index)
576 {
577 MYGUI_ASSERT_RANGE_AND_NONE(_index, mItemsInfo.size(), "ListBox::setIndexSelected");
578 if (mIndexSelect != _index)
579 {
580 _selectIndex(mIndexSelect, false);
581 _selectIndex(_index, true);
582 mIndexSelect = _index;
583 }
584 }
585
586 void ListBox::_selectIndex(size_t _index, bool _select)
587 {
588 if (_index == ITEM_NONE)
589 return;
590 if (_index < (size_t)mTopIndex)
591 return;
592 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
593 if (_getClientWidget()->getHeight() < offset)
594 return;
595
596 size_t index = _index - mTopIndex;
597 if (index < mWidgetLines.size())
598 static_cast<Button*>(mWidgetLines[index])->setStateSelected(_select);
599 }
600
601 void ListBox::beginToItemAt(size_t _index)
602 {
603 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::beginToItemAt");
604 if (mRangeIndex <= 0)
605 return;
606
607 int offset = (int)_index * mHeightLine;
608 if (offset >= mRangeIndex)
609 offset = mRangeIndex;
610
611 if (mWidgetScroll != nullptr)
612 {
613 if ((int)mWidgetScroll->getScrollPosition() == offset)
614 return;
615 mWidgetScroll->setScrollPosition(offset);
616 }
617 notifyScrollChangePosition(nullptr, offset);
618 }
619
620 // whether item is fully or partially visible
621 bool ListBox::isItemVisibleAt(size_t _index, bool _fill)
622 {
623 if (_index >= mItemsInfo.size())
624 return false;
625 if (mRangeIndex <= 0)
626 return true;
627
628 if (_index < (size_t)mTopIndex)
629 return false;
630
631 if (_index == (size_t)mTopIndex)
632 {
633 return mOffsetTop == 0 || !_fill;
634 }
635
636 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
637
638 if (_getClientWidget()->getHeight() < offset)
639 return false;
640
641 if (_getClientWidget()->getHeight() < (offset + mHeightLine) && _fill)
642 return false;
643
644 return true;
645 }
646
647 void ListBox::removeAllItems()
648 {
649 mTopIndex = 0;
650 mIndexSelect = ITEM_NONE;
651 mOffsetTop = 0;
652
653 mItemsInfo.clear();
654
655 int offset = 0;
656 for (auto& widgetLine : mWidgetLines)
657 {
658 widgetLine->setVisible(false);
659 widgetLine->setPosition(0, offset);
660 offset += mHeightLine;
661 }
662
663 updateScroll();
664 updateLine(true);
665 }
666
667 void ListBox::setItemNameAt(size_t _index, const UString& _name)
668 {
669 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::setItemNameAt");
670 mItemsInfo[_index].first = _name;
671 _redrawItem(_index);
672 }
673
674 void ListBox::setItemDataAt(size_t _index, Any _data)
675 {
676 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::setItemDataAt");
677 mItemsInfo[_index].second = std::move(_data);
678 _redrawItem(_index);
679 }
680
681 const UString& ListBox::getItemNameAt(size_t _index) const
682 {
683 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "ListBox::getItemNameAt");
684 return mItemsInfo[_index].first;
685 }
686
687 void ListBox::notifyMouseSetFocus(Widget* _sender, Widget* _old)
688 {
689#if MYGUI_DEBUG_MODE == 1
690 MYGUI_ASSERT_RANGE(*_sender->_getInternalData<size_t>(), mWidgetLines.size(), "ListBox::notifyMouseSetFocus");
691#endif
692
693 mLineActive = *_sender->_getInternalData<size_t>();
694 eventListMouseItemFocus(this, mLineActive);
695 }
696
697 void ListBox::notifyMouseLostFocus(Widget* _sender, Widget* _new)
698 {
699 if ((nullptr == _new) || (_new->getParent() != _getClientWidget()))
700 {
701 mLineActive = ITEM_NONE;
702 eventListMouseItemFocus(this, ITEM_NONE);
703 }
704 }
705
706 void ListBox::_setItemFocus(size_t _index, bool _focus)
707 {
708 MYGUI_ASSERT_RANGE(_index, mWidgetLines.size(), "ListBox::_setItemFocus");
709 static_cast<Button*>(mWidgetLines[_index])->_setMouseFocus(_focus);
710 }
711
712 void ListBox::setScrollVisible(bool _visible)
713 {
714 if (mNeedVisibleScroll == _visible)
715 return;
716 mNeedVisibleScroll = _visible;
717 updateScroll();
718 }
719
720 void ListBox::setScrollPosition(size_t _position)
721 {
722 if (mWidgetScroll != nullptr)
723 {
724 if (mWidgetScroll->getScrollRange() > _position)
725 {
726 mWidgetScroll->setScrollPosition(_position);
727 _setScrollView(_position);
728 }
729 }
730 }
731
732 void ListBox::_setScrollView(size_t _position)
733 {
734 mOffsetTop = ((int)_position % mHeightLine);
735
736 // offset from negative side
737 int offset = 0 - mOffsetTop;
738
739 for (auto& widgetLine : mWidgetLines)
740 {
741 widgetLine->setPosition(IntPoint(0, offset));
742 offset += mHeightLine;
743 }
744
745 int top = ((int)_position / mHeightLine);
746 if (top != mTopIndex)
747 {
748 mTopIndex = top;
749 _redrawItemRange();
750 }
751
752 // render all bottom lines if they appeared
753 _redrawItemRange(mLastRedrawLine);
754 }
755
756 size_t ListBox::_getScrollPosition() const
757 {
758 return mTopIndex * mHeightLine + mOffsetTop;
759 }
760
761 void ListBox::_sendEventChangeScroll(size_t _position)
762 {
763 eventListChangeScroll(this, _position);
764 if (ITEM_NONE != mLineActive)
765 eventListMouseItemFocus(this, mLineActive);
766 }
767
768 void ListBox::swapItemsAt(size_t _index1, size_t _index2)
769 {
770 MYGUI_ASSERT_RANGE(_index1, mItemsInfo.size(), "ListBox::swapItemsAt");
771 MYGUI_ASSERT_RANGE(_index2, mItemsInfo.size(), "ListBox::swapItemsAt");
772
773 if (_index1 == _index2)
774 return;
775
776 std::swap(mItemsInfo[_index1], mItemsInfo[_index2]);
777
778 _redrawItem(_index1);
779 _redrawItem(_index2);
780 }
781
782 void ListBox::_checkAlign()
783 {
784 int max_height = mItemsInfo.size() * mHeightLine;
785 int visible_height = _getClientWidget()->getHeight();
786
787 if (visible_height >= max_height)
788 {
789 MYGUI_ASSERT(mTopIndex == 0, "mTopIndex == 0");
790 MYGUI_ASSERT(mOffsetTop == 0, "mOffsetTop == 0");
791 int height = 0;
792 for (size_t pos = 0; pos < mWidgetLines.size(); pos++)
793 {
794 if (pos >= mItemsInfo.size())
795 break;
796 MYGUI_ASSERT(mWidgetLines[pos]->getTop() == height, "mWidgetLines[pos]->getTop() == height");
797 height += mWidgetLines[pos]->getHeight();
798 }
799 }
800 }
801
802 size_t ListBox::findItemIndexWith(const UString& _name)
803 {
804 for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
805 {
806 if (mItemsInfo[pos].first == _name)
807 return pos;
808 }
809 return ITEM_NONE;
810 }
811
812 void ListBox::setItemHeight(int _height)
813 {
814 _height = std::max(1, _height);
815 if (mHeightLine == _height)
816 return;
817
818 int position = static_cast<int>(_getScrollPosition());
819 mHeightLine = _height;
820
821 if (mWidgetScroll != nullptr)
822 mWidgetScroll->setScrollPage((size_t)mHeightLine);
823 updateScroll();
824
825 for (auto& widgetLine : mWidgetLines)
826 widgetLine->setSize(_getClientWidget()->getWidth(), mHeightLine);
827
828 _setScrollView(std::clamp(position, 0, std::max(0, mRangeIndex)));
829 updateLine(true);
830 }
831
832 int ListBox::getItemHeight() const
833 {
834 return mHeightLine;
835 }
836
837 int ListBox::getOptimalHeight() const
838 {
839 return (int)((mCoord.height - _getClientWidget()->getHeight()) + (mItemsInfo.size() * mHeightLine));
840 }
841
842 size_t ListBox::getItemCount() const
843 {
844 return mItemsInfo.size();
845 }
846
847 void ListBox::addItem(const UString& _name, Any _data)
848 {
849 insertItemAt(ITEM_NONE, _name, _data);
850 }
851
852 size_t ListBox::getIndexSelected() const
853 {
854 return mIndexSelect;
855 }
856
857 void ListBox::clearIndexSelected()
858 {
859 setIndexSelected(ITEM_NONE);
860 }
861
862 void ListBox::clearItemDataAt(size_t _index)
863 {
864 setItemDataAt(_index, Any::Null);
865 }
866
867 void ListBox::beginToItemFirst()
868 {
869 if (getItemCount())
870 beginToItemAt(0);
871 }
872
873 void ListBox::beginToItemLast()
874 {
875 if (getItemCount())
876 beginToItemAt(getItemCount() - 1);
877 }
878
879 void ListBox::beginToItemSelected()
880 {
881 if (getIndexSelected() != ITEM_NONE)
882 beginToItemAt(getIndexSelected());
883 }
884
885 bool ListBox::isItemSelectedVisible(bool _fill)
886 {
887 return isItemVisibleAt(mIndexSelect, _fill);
888 }
889
890 size_t ListBox::_getItemIndex(Widget* _item) const
891 {
892 for (const auto& line : mWidgetLines)
893 {
894 if (line == _item)
895 return *line->_getInternalData<size_t>() + mTopIndex;
896 }
897 return ITEM_NONE;
898 }
899
900 void ListBox::_resetContainer(bool _update)
901 {
902 Base::_resetContainer(_update);
903
904 if (!_update)
905 {
906 WidgetManager& instance = WidgetManager::getInstance();
907 for (const auto& line : mWidgetLines)
908 instance.unlinkFromUnlinkers(line);
909 }
910 }
911
912 void ListBox::setPropertyOverride(std::string_view _key, std::string_view _value)
913 {
914 if (_key == "AddItem")
915 addItem(LanguageManager::getInstance().replaceTags(UString(_value)));
916 else if (_key == "ActivateOnClick")
917 mActivateOnClick = utility::parseBool(_value);
919 else if (_key == "ItemHeight")
920 setItemHeight(utility::parseInt(_value));
921 else
922 {
923 Base::setPropertyOverride(_key, _value);
924 return;
925 }
926
927 eventChangeProperty(this, _key, _value);
928 }
929
930 void ListBox::_activateItem(MyGUI::Widget* _sender)
931 {
932 if (_sender == _getClientWidget())
933 {
934 if (mIndexSelect != ITEM_NONE)
935 {
936 _selectIndex(mIndexSelect, false);
937 mIndexSelect = ITEM_NONE;
938 eventListChangePosition(this, mIndexSelect);
939 }
940 eventListMouseItemActivate(this, mIndexSelect);
941 }
942 // cell may be hidden
943 else if (_sender->getVisible())
944 {
945#if MYGUI_DEBUG_MODE == 1
947 *_sender->_getInternalData<size_t>(),
948 mWidgetLines.size(),
949 "ListBox::notifyMousePressed");
951 *_sender->_getInternalData<size_t>() + mTopIndex,
952 mItemsInfo.size(),
953 "ListBox::notifyMousePressed");
954#endif
955
956 size_t index = *_sender->_getInternalData<size_t>() + mTopIndex;
957
958 if (mIndexSelect != index)
959 {
960 _selectIndex(mIndexSelect, false);
961 _selectIndex(index, true);
962 mIndexSelect = index;
963 eventListChangePosition(this, mIndexSelect);
964 }
965 eventListMouseItemActivate(this, mIndexSelect);
966 }
967
968 _resetContainer(true);
969 }
970
971 size_t ListBox::_getItemCount() const
972 {
973 return getItemCount();
974 }
975
976 void ListBox::_addItem(const MyGUI::UString& _name)
977 {
978 addItem(_name);
979 }
980
981 void ListBox::_removeItemAt(size_t _index)
982 {
983 removeItemAt(_index);
984 }
985
986 void ListBox::_setItemNameAt(size_t _index, const UString& _name)
987 {
988 setItemNameAt(_index, _name);
989 }
990
991 const UString& ListBox::_getItemNameAt(size_t _index) const
992 {
993 return getItemNameAt(_index);
994 }
995
996 size_t ListBox::getIndexByWidget(Widget* _widget) const
997 {
998 if (_widget == getClientWidget())
999 return ITEM_NONE;
1000 return *_widget->_getInternalData<size_t>() + mTopIndex;
1001 }
1002
1003 void ListBox::notifyKeyButtonPressed(Widget* _sender, KeyCode _key, Char _char)
1004 {
1005 eventNotifyItem(this, IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::KeyPressed, _key, _char));
1006 }
1007
1008 void ListBox::notifyKeyButtonReleased(Widget* _sender, KeyCode _key)
1009 {
1010 eventNotifyItem(this, IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::KeyReleased, _key));
1011 }
1012
1013 void ListBox::notifyMouseButtonReleased(Widget* _sender, int _left, int _top, MouseButton _id)
1014 {
1015 eventNotifyItem(
1016 this,
1017 IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::MouseReleased, _left, _top, _id));
1018 }
1019
1020 void ListBox::onKeyButtonReleased(KeyCode _key)
1021 {
1022 Base::onKeyButtonReleased(_key);
1023
1024 eventNotifyItem(this, IBNotifyItemData(ITEM_NONE, IBNotifyItemData::KeyReleased, _key));
1025 }
1026
1027 void ListBox::setActivateOnClick(bool activateOnClick)
1028 {
1029 mActivateOnClick = activateOnClick;
1030 }
1031
1032 Widget* ListBox::getWidgetByIndex(size_t _index)
1033 {
1034 if (_index == MyGUI::ITEM_NONE)
1035 return nullptr;
1036
1037 size_t index = _index - (size_t)mTopIndex;
1038
1039 if (index < mWidgetLines.size())
1040 return mWidgetLines[index];
1041 return nullptr;
1042 }
1043
1044} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_ASSERT_RANGE_INSERT(index, size, owner)
#define MYGUI_ASSERT_RANGE(index, size, owner)
#define MYGUI_ASSERT_RANGE_AND_NONE(index, size, owner)
bool parseBool(std::string_view _value)
int parseInt(std::string_view _value)
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
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
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))