19 void ListBox::initialiseOverride()
21 Base::initialiseOverride();
24 setNeedKeyFocus(
true);
26 if (isUserString(
"SkinLine"))
27 mSkinLine = getUserString(
"SkinLine");
29 if (isUserString(
"HeightLine"))
35 if (getClientWidget() !=
nullptr)
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);
44 assignWidget(mWidgetScroll,
"VScroll");
45 if (mWidgetScroll !=
nullptr)
47 mWidgetScroll->eventScrollChangePosition +=
newDelegate(
this, &ListBox::notifyScrollChangePosition);
48 mWidgetScroll->setScrollPage((
size_t)mHeightLine);
55 void ListBox::shutdownOverride()
57 mWidgetScroll =
nullptr;
59 Base::shutdownOverride();
62 void ListBox::onMouseWheel(
int _rel)
64 notifyMouseWheel(
nullptr, _rel);
66 Base::onMouseWheel(_rel);
69 void ListBox::onKeyButtonPressed(KeyCode _key,
Char _char)
71 if (getItemCount() == 0)
73 Base::onKeyButtonPressed(_key, _char);
74 eventNotifyItem(
this, IBNotifyItemData(
ITEM_NONE, IBNotifyItemData::KeyPressed, _key, _char));
79 size_t sel = mIndexSelect;
81 if (_key == KeyCode::ArrowUp)
91 else if (_key == KeyCode::ArrowDown)
98 if (sel >= getItemCount())
103 else if (_key == KeyCode::Home)
107 else if (_key == KeyCode::End)
109 sel = getItemCount() - 1;
111 else if (_key == KeyCode::PageUp)
121 size_t page = _getClientWidget()->getHeight() / mHeightLine;
129 else if (_key == KeyCode::PageDown)
131 if (sel != (getItemCount() - 1))
139 sel += _getClientWidget()->getHeight() / mHeightLine;
140 if (sel >= getItemCount())
141 sel = getItemCount() - 1;
145 else if ((_key == KeyCode::Return) || (_key == KeyCode::NumpadEnter))
150 eventListSelectAccept(
this, sel);
152 Base::onKeyButtonPressed(_key, _char);
154 eventNotifyItem(
this, IBNotifyItemData(
ITEM_NONE, IBNotifyItemData::KeyPressed, _key, _char));
160 if (sel != mIndexSelect)
162 _resetContainer(
true);
164 if (!isItemVisibleAt(sel))
167 if (mWidgetScroll !=
nullptr)
168 _sendEventChangeScroll(mWidgetScroll->getScrollPosition());
170 setIndexSelected(sel);
174 eventListChangePosition(
this, mIndexSelect);
177 Base::onKeyButtonPressed(_key, _char);
178 eventNotifyItem(
this, IBNotifyItemData(
ITEM_NONE, IBNotifyItemData::KeyPressed, _key, _char));
181 void ListBox::notifyMouseWheel(Widget* _sender,
int _rel)
183 if (mRangeIndex <= 0)
186 if (mWidgetScroll ==
nullptr)
189 int offset = (int)mWidgetScroll->getScrollPosition();
191 offset += mHeightLine;
193 offset -= mHeightLine;
195 if (offset >= mRangeIndex)
196 offset = mRangeIndex;
200 if ((
int)mWidgetScroll->getScrollPosition() == offset)
203 mWidgetScroll->setScrollPosition(offset);
204 _setScrollView(offset);
205 _sendEventChangeScroll(offset);
207 _resetContainer(
true);
210 void ListBox::notifyScrollChangePosition(ScrollBar* _sender,
size_t _position)
212 _setScrollView(_position);
213 _sendEventChangeScroll(_position);
216 void ListBox::notifyMousePressed(Widget* _sender,
int _left,
int _top, MouseButton _id)
218 if (MouseButton::Left == _id && !mActivateOnClick)
219 _activateItem(_sender);
223 IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::MousePressed, _left, _top, _id));
226 void ListBox::notifyMouseClick(MyGUI::Widget* _sender)
228 if (mActivateOnClick)
229 _activateItem(_sender);
232 void ListBox::notifyMouseDoubleClick(Widget* _sender)
235 eventListSelectAccept(
this, mIndexSelect);
238 void ListBox::setPosition(
const IntPoint& _point)
240 Base::setPosition(_point);
243 void ListBox::setSize(
const IntSize& _size)
245 Base::setSize(_size);
251 void ListBox::setCoord(
const IntCoord& _coord)
253 Base::setCoord(_coord);
259 void ListBox::updateScroll()
261 mRangeIndex = (mHeightLine * (int)mItemsInfo.size()) - _getClientWidget()->getHeight();
263 if (mWidgetScroll ==
nullptr)
266 if ((!mNeedVisibleScroll) || (mRangeIndex < 1) || (mWidgetScroll->getLeft() <= _getClientWidget()->getLeft()))
268 if (mWidgetScroll->getVisible())
270 mWidgetScroll->setVisible(
false);
272 if (getClientWidget() !=
nullptr)
273 getClientWidget()->setSize(
274 getClientWidget()->getWidth() + mWidgetScroll->getWidth(),
275 getClientWidget()->getHeight());
278 else if (!mWidgetScroll->getVisible())
280 if (getClientWidget() !=
nullptr)
281 getClientWidget()->setSize(
282 getClientWidget()->getWidth() - mWidgetScroll->getWidth(),
283 getClientWidget()->getHeight());
284 mWidgetScroll->setVisible(
true);
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());
294 void ListBox::updateLine(
bool _reset)
300 _resetContainer(
false);
303 size_t position = _getScrollPosition();
305 if (mOldSize.height < mCoord.height)
307 int height = (int)mWidgetLines.size() * mHeightLine - mOffsetTop;
310 while ((height <= (_getClientWidget()->getHeight() + mHeightLine)) &&
311 (mWidgetLines.size() < mItemsInfo.size()))
313 Widget* widget = _getClientWidget()->createWidgetT(
318 _getClientWidget()->getWidth(),
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;
338 if (mRangeIndex <= 0 || position >=
static_cast<size_t>(mRangeIndex))
340 if (mRangeIndex <= 0)
342 if (position || mOffsetTop || mTopIndex)
350 for (
auto& widgetLine : mWidgetLines)
352 widgetLine->setPosition(0, offset);
353 offset += mHeightLine;
360 int count = _getClientWidget()->getHeight() / mHeightLine;
361 mOffsetTop = mHeightLine - (_getClientWidget()->getHeight() % mHeightLine);
363 if (mOffsetTop == mHeightLine)
369 int top = (int)mItemsInfo.size() - count - 1;
371 int offset = 0 - mOffsetTop;
372 for (
auto& widgetLine : mWidgetLines)
374 widgetLine->setPosition(0, offset);
375 offset += mHeightLine;
379 position = top * mHeightLine + mOffsetTop;
381 if (top != mTopIndex)
394 if (mWidgetScroll !=
nullptr)
395 mWidgetScroll->setScrollPosition(position);
397 mOldSize.width = mCoord.width;
398 mOldSize.height = mCoord.height;
401 void ListBox::_redrawItemRange(
size_t _start)
405 for (; pos < mWidgetLines.size(); pos++)
407 size_t index = pos + (size_t)mTopIndex;
409 if (index >= mItemsInfo.size())
411 mLastRedrawLine = pos;
414 if (mWidgetLines[pos]->getTop() > _getClientWidget()->getHeight())
416 mLastRedrawLine = pos;
420 mWidgetLines[pos]->setVisible(
true);
421 mWidgetLines[pos]->setCaption(mItemsInfo[index].first);
423 static_cast<Button*
>(mWidgetLines[pos])->setStateSelected(index == mIndexSelect);
426 if (pos >= mWidgetLines.size())
428 mLastRedrawLine = pos;
433 for (; pos < mWidgetLines.size(); pos++)
435 static_cast<Button*
>(mWidgetLines[pos])->setStateSelected(
false);
436 static_cast<Button*
>(mWidgetLines[pos])->setVisible(
false);
442 void ListBox::_redrawItem(
size_t _index)
444 if (_index < (
size_t)mTopIndex)
446 _index -= (size_t)mTopIndex;
447 if (_index >= mLastRedrawLine)
451 mWidgetLines[_index]->setCaption(mItemsInfo[_index + mTopIndex].first);
454 void ListBox::insertItemAt(
size_t _index,
const UString& _name, Any _data)
458 _index = mItemsInfo.size();
460 mItemsInfo.insert(mItemsInfo.begin() + _index, PairItem(_name, _data));
462 if ((mIndexSelect !=
ITEM_NONE) && (_index <= mIndexSelect))
465 if ((_index <= (
size_t)mTopIndex) && (mRangeIndex > 0))
468 if (mWidgetScroll !=
nullptr)
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());
477 mRangeIndex += mHeightLine;
482 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
485 if (_getClientWidget()->getHeight() < (offset - mHeightLine))
487 if (mWidgetScroll !=
nullptr)
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());
496 mRangeIndex += mHeightLine;
508 void ListBox::removeItemAt(
size_t _index)
512 mItemsInfo.erase(mItemsInfo.begin() + _index);
514 if (mItemsInfo.empty())
518 if (_index < mIndexSelect)
520 else if ((_index == mIndexSelect) && (mIndexSelect == (mItemsInfo.size())))
525 if (mWidgetLines.size() > mItemsInfo.size())
527 mWidgetLines[mItemsInfo.size()]->setVisible(
false);
530 if (_index < (
size_t)mTopIndex)
533 if (mWidgetScroll !=
nullptr)
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());
542 mRangeIndex -= mHeightLine;
546 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
548 if (_getClientWidget()->getHeight() < offset)
550 if (mWidgetScroll !=
nullptr)
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());
559 mRangeIndex -= mHeightLine;
575 void ListBox::setIndexSelected(
size_t _index)
578 if (mIndexSelect != _index)
580 _selectIndex(mIndexSelect,
false);
581 _selectIndex(_index,
true);
582 mIndexSelect = _index;
586 void ListBox::_selectIndex(
size_t _index,
bool _select)
590 if (_index < (
size_t)mTopIndex)
592 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
593 if (_getClientWidget()->getHeight() < offset)
596 size_t index = _index - mTopIndex;
597 if (index < mWidgetLines.size())
598 static_cast<Button*
>(mWidgetLines[index])->setStateSelected(_select);
601 void ListBox::beginToItemAt(
size_t _index)
604 if (mRangeIndex <= 0)
607 int offset = (int)_index * mHeightLine;
608 if (offset >= mRangeIndex)
609 offset = mRangeIndex;
611 if (mWidgetScroll !=
nullptr)
613 if ((
int)mWidgetScroll->getScrollPosition() == offset)
615 mWidgetScroll->setScrollPosition(offset);
617 notifyScrollChangePosition(
nullptr, offset);
621 bool ListBox::isItemVisibleAt(
size_t _index,
bool _fill)
623 if (_index >= mItemsInfo.size())
625 if (mRangeIndex <= 0)
628 if (_index < (
size_t)mTopIndex)
631 if (_index == (
size_t)mTopIndex)
633 return mOffsetTop == 0 || !_fill;
636 int offset = ((int)_index - mTopIndex) * mHeightLine - mOffsetTop;
638 if (_getClientWidget()->getHeight() < offset)
641 if (_getClientWidget()->getHeight() < (offset + mHeightLine) && _fill)
647 void ListBox::removeAllItems()
656 for (
auto& widgetLine : mWidgetLines)
658 widgetLine->setVisible(
false);
659 widgetLine->setPosition(0, offset);
660 offset += mHeightLine;
667 void ListBox::setItemNameAt(
size_t _index,
const UString& _name)
670 mItemsInfo[_index].first = _name;
674 void ListBox::setItemDataAt(
size_t _index, Any _data)
677 mItemsInfo[_index].second = std::move(_data);
681 const UString& ListBox::getItemNameAt(
size_t _index)
const
684 return mItemsInfo[_index].first;
687 void ListBox::notifyMouseSetFocus(Widget* _sender, Widget* _old)
689#if MYGUI_DEBUG_MODE == 1
690 MYGUI_ASSERT_RANGE(*_sender->_getInternalData<
size_t>(), mWidgetLines.size(),
"ListBox::notifyMouseSetFocus");
693 mLineActive = *_sender->_getInternalData<
size_t>();
694 eventListMouseItemFocus(
this, mLineActive);
697 void ListBox::notifyMouseLostFocus(Widget* _sender, Widget* _new)
699 if ((
nullptr == _new) || (_new->getParent() != _getClientWidget()))
702 eventListMouseItemFocus(
this,
ITEM_NONE);
706 void ListBox::_setItemFocus(
size_t _index,
bool _focus)
709 static_cast<Button*
>(mWidgetLines[_index])->_setMouseFocus(_focus);
712 void ListBox::setScrollVisible(
bool _visible)
714 if (mNeedVisibleScroll == _visible)
716 mNeedVisibleScroll = _visible;
720 void ListBox::setScrollPosition(
size_t _position)
722 if (mWidgetScroll !=
nullptr)
724 if (mWidgetScroll->getScrollRange() > _position)
726 mWidgetScroll->setScrollPosition(_position);
727 _setScrollView(_position);
732 void ListBox::_setScrollView(
size_t _position)
734 mOffsetTop = ((int)_position % mHeightLine);
737 int offset = 0 - mOffsetTop;
739 for (
auto& widgetLine : mWidgetLines)
741 widgetLine->setPosition(
IntPoint(0, offset));
742 offset += mHeightLine;
745 int top = ((int)_position / mHeightLine);
746 if (top != mTopIndex)
753 _redrawItemRange(mLastRedrawLine);
756 size_t ListBox::_getScrollPosition()
const
758 return mTopIndex * mHeightLine + mOffsetTop;
761 void ListBox::_sendEventChangeScroll(
size_t _position)
763 eventListChangeScroll(
this, _position);
765 eventListMouseItemFocus(
this, mLineActive);
768 void ListBox::swapItemsAt(
size_t _index1,
size_t _index2)
773 if (_index1 == _index2)
776 std::swap(mItemsInfo[_index1], mItemsInfo[_index2]);
778 _redrawItem(_index1);
779 _redrawItem(_index2);
782 void ListBox::_checkAlign()
784 int max_height = mItemsInfo.size() * mHeightLine;
785 int visible_height = _getClientWidget()->getHeight();
787 if (visible_height >= max_height)
792 for (
size_t pos = 0; pos < mWidgetLines.size(); pos++)
794 if (pos >= mItemsInfo.size())
796 MYGUI_ASSERT(mWidgetLines[pos]->getTop() == height,
"mWidgetLines[pos]->getTop() == height");
797 height += mWidgetLines[pos]->getHeight();
802 size_t ListBox::findItemIndexWith(
const UString& _name)
804 for (
size_t pos = 0; pos < mItemsInfo.size(); pos++)
806 if (mItemsInfo[pos].first == _name)
812 void ListBox::setItemHeight(
int _height)
814 _height = std::max(1, _height);
815 if (mHeightLine == _height)
818 int position =
static_cast<int>(_getScrollPosition());
819 mHeightLine = _height;
821 if (mWidgetScroll !=
nullptr)
822 mWidgetScroll->setScrollPage((
size_t)mHeightLine);
825 for (
auto& widgetLine : mWidgetLines)
826 widgetLine->setSize(_getClientWidget()->getWidth(), mHeightLine);
828 _setScrollView(std::clamp(position, 0, std::max(0, mRangeIndex)));
832 int ListBox::getItemHeight()
const
837 int ListBox::getOptimalHeight()
const
839 return (
int)((mCoord.height - _getClientWidget()->getHeight()) + (mItemsInfo.size() * mHeightLine));
842 size_t ListBox::getItemCount()
const
844 return mItemsInfo.size();
847 void ListBox::addItem(
const UString& _name, Any _data)
852 size_t ListBox::getIndexSelected()
const
857 void ListBox::clearIndexSelected()
862 void ListBox::clearItemDataAt(
size_t _index)
864 setItemDataAt(_index, Any::Null);
867 void ListBox::beginToItemFirst()
873 void ListBox::beginToItemLast()
876 beginToItemAt(getItemCount() - 1);
879 void ListBox::beginToItemSelected()
882 beginToItemAt(getIndexSelected());
885 bool ListBox::isItemSelectedVisible(
bool _fill)
887 return isItemVisibleAt(mIndexSelect, _fill);
890 size_t ListBox::_getItemIndex(Widget* _item)
const
892 for (
const auto& line : mWidgetLines)
895 return *line->_getInternalData<
size_t>() + mTopIndex;
900 void ListBox::_resetContainer(
bool _update)
902 Base::_resetContainer(_update);
906 WidgetManager& instance = WidgetManager::getInstance();
907 for (
const auto& line : mWidgetLines)
908 instance.unlinkFromUnlinkers(line);
912 void ListBox::setPropertyOverride(std::string_view _key, std::string_view _value)
914 if (_key ==
"AddItem")
915 addItem(LanguageManager::getInstance().replaceTags(UString(_value)));
916 else if (_key ==
"ActivateOnClick")
919 else if (_key ==
"ItemHeight")
923 Base::setPropertyOverride(_key, _value);
927 eventChangeProperty(
this, _key, _value);
930 void ListBox::_activateItem(MyGUI::Widget* _sender)
932 if (_sender == _getClientWidget())
936 _selectIndex(mIndexSelect,
false);
938 eventListChangePosition(
this, mIndexSelect);
940 eventListMouseItemActivate(
this, mIndexSelect);
943 else if (_sender->getVisible())
945#if MYGUI_DEBUG_MODE == 1
947 *_sender->_getInternalData<
size_t>(),
949 "ListBox::notifyMousePressed");
951 *_sender->_getInternalData<
size_t>() + mTopIndex,
953 "ListBox::notifyMousePressed");
956 size_t index = *_sender->_getInternalData<
size_t>() + mTopIndex;
958 if (mIndexSelect != index)
960 _selectIndex(mIndexSelect,
false);
961 _selectIndex(index,
true);
962 mIndexSelect = index;
963 eventListChangePosition(
this, mIndexSelect);
965 eventListMouseItemActivate(
this, mIndexSelect);
968 _resetContainer(
true);
971 size_t ListBox::_getItemCount()
const
973 return getItemCount();
976 void ListBox::_addItem(
const MyGUI::UString& _name)
981 void ListBox::_removeItemAt(
size_t _index)
983 removeItemAt(_index);
986 void ListBox::_setItemNameAt(
size_t _index,
const UString& _name)
988 setItemNameAt(_index, _name);
991 const UString& ListBox::_getItemNameAt(
size_t _index)
const
993 return getItemNameAt(_index);
996 size_t ListBox::getIndexByWidget(Widget* _widget)
const
998 if (_widget == getClientWidget())
1000 return *_widget->_getInternalData<
size_t>() + mTopIndex;
1003 void ListBox::notifyKeyButtonPressed(Widget* _sender, KeyCode _key,
Char _char)
1005 eventNotifyItem(
this, IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::KeyPressed, _key, _char));
1008 void ListBox::notifyKeyButtonReleased(Widget* _sender, KeyCode _key)
1010 eventNotifyItem(
this, IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::KeyReleased, _key));
1013 void ListBox::notifyMouseButtonReleased(Widget* _sender,
int _left,
int _top, MouseButton _id)
1017 IBNotifyItemData(getIndexByWidget(_sender), IBNotifyItemData::MouseReleased, _left, _top, _id));
1020 void ListBox::onKeyButtonReleased(KeyCode _key)
1022 Base::onKeyButtonReleased(_key);
1024 eventNotifyItem(
this, IBNotifyItemData(
ITEM_NONE, IBNotifyItemData::KeyReleased, _key));
1027 void ListBox::setActivateOnClick(
bool activateOnClick)
1029 mActivateOnClick = activateOnClick;
1032 Widget* ListBox::getWidgetByIndex(
size_t _index)
1037 size_t index = _index - (size_t)mTopIndex;
1039 if (index < mWidgetLines.size())
1040 return mWidgetLines[index];
#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
constexpr size_t ITEM_NONE
types::TCoord< int > IntCoord
types::TSize< int > IntSize
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))