MyGUI 3.5.3
MyGUI_TabControl.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_TabControl.h"
10#include "MyGUI_WidgetManager.h"
11#include "MyGUI_Button.h"
12#include "MyGUI_TabItem.h"
13#include "MyGUI_ResourceSkin.h"
14
15namespace MyGUI
16{
17
18 const float TAB_SPEED_FADE_COEF = 5.0f;
19
20 void TabControl::initialiseOverride()
21 {
22 Base::initialiseOverride();
23
24 if (isUserString("ButtonSkin"))
25 mButtonSkinName = getUserString("ButtonSkin");
26
27 // OBSOLETE
28 if (isUserString("OffsetBar"))
29 mOffsetTab = utility::parseValue<int>(getUserString("OffsetBar"));
30
31 // OBSOLETE
32 if (isUserString("EmptyBarSkin"))
33 mEmptySkinName = getUserString("EmptyBarSkin");
34
35 // OBSOLETE
36 assignWidget(mWidgetBar, "Bar");
37 if (mWidgetBar != nullptr)
38 {
39 mWidgetBar->setSize(mWidgetBar->getWidth() - mOffsetTab, mWidgetBar->getHeight());
40 }
41
43 assignWidget(mButtonLeft, "Left");
44 if (mButtonLeft != nullptr)
45 {
46 mButtonLeft->eventMouseButtonClick += newDelegate(this, &TabControl::notifyPressedButtonEvent);
47 }
48
50 assignWidget(mButtonRight, "Right");
51 if (mButtonRight != nullptr)
52 {
53 mButtonRight->eventMouseButtonClick += newDelegate(this, &TabControl::notifyPressedButtonEvent);
54 }
55
56 // OBSOLETE
57 assignWidget(mButtonDecor, "ButtonDecor");
58 if (mButtonDecor != nullptr)
59 {
60 mButtonDecor->setVisible(false);
61 }
62
64 assignWidget(mItemTemplate, "TabItem");
65 if (mItemTemplate != nullptr)
66 {
67 mItemTemplate->setVisible(false);
68 }
69
70#ifndef MYGUI_DONT_USE_OBSOLETE
71 if (mItemTemplate == nullptr)
72 {
73 assignWidget(mItemTemplate, "Sheet");
74 if (mItemTemplate != nullptr)
75 {
76 mItemTemplate->setVisible(false);
77 }
78 }
79#endif // MYGUI_DONT_USE_OBSOLETE
80
81 // OBSOLETE
82 Widget* showPatch = nullptr;
83 assignWidget(showPatch, "ShowPatch");
84 if (showPatch != nullptr)
85 {
86 mWidgetsPatch.push_back(showPatch);
87 showPatch->setVisible(false);
88 }
89
91 assignWidget(mHeaderPlace, "HeaderPlace");
92
94 assignWidget(mControls, "Controls");
95
97 assignWidget(mEmpty, "Empty");
98
99 if (mEmpty == nullptr)
100 {
101 // create widget bearing the empty bar skin
102 // OBSOLETE
103 mEmptyBarWidget =
104 _getWidgetBar()->createWidget<Widget>(mEmptySkinName, IntCoord(), Align::Left | Align::Top);
105 }
106
107 updateBar();
108
109 // FIXME added because shutdown is called on skin change
110 mShutdown = false;
111 }
112
113 void TabControl::shutdownOverride()
114 {
115 mWidgetsPatch.clear();
116 mWidgetBar = nullptr;
117 mButtonLeft = nullptr;
118 mButtonRight = nullptr;
119 mButtonDecor = nullptr;
120 mItemTemplate = nullptr;
121 mEmptyBarWidget = nullptr;
122
123 mHeaderPlace = nullptr;
124 mControls = nullptr;
125 mEmpty = nullptr;
126
127 // FIXME moved from destructor, may break on skin change
128 mShutdown = true;
129
130 Base::shutdownOverride();
131 }
132
133 void TabControl::onWidgetCreated(Widget* _widget)
134 {
135 Base::onWidgetCreated(_widget);
136
137 TabItem* child = _widget->castType<TabItem>(false);
138 if (child != nullptr)
139 {
140 child->setCoord(
141 _getWidgetTemplate()->getAbsoluteLeft() - getAbsoluteLeft(),
142 _getWidgetTemplate()->getAbsoluteTop() - getAbsoluteTop(),
143 _getWidgetTemplate()->getWidth(),
144 _getWidgetTemplate()->getHeight());
145 child->setAlign(_getWidgetTemplate()->getAlign());
146
147 _insertItem(ITEM_NONE, UString(), child, Any::Null);
148 }
149 }
150
151 TabItem* TabControl::insertItemAt(size_t _index, const UString& _name, Any _data)
152 {
153 MYGUI_ASSERT_RANGE_INSERT(_index, mItemsInfo.size(), "TabControl::insertItem");
154
155 Widget* widget = Base::baseCreateWidget(
156 WidgetStyle::Child,
157 TabItem::getClassTypeName(),
158 "Default",
159 _getWidgetTemplate()->getCoord(),
160 _getWidgetTemplate()->getAlign(),
161 std::string_view{},
162 std::string_view{},
163 false);
164
165 size_t lastIndex = mItemsInfo.size() - 1;
166 setItemNameAt(lastIndex, _name);
167 setItemDataAt(lastIndex, _data);
168
169 swapItems(_index == ITEM_NONE ? lastIndex : _index, lastIndex);
170
171 return widget->castType<TabItem>();
172 }
173
174 void TabControl::swapItems(size_t _index1, size_t _index2)
175 {
176 MYGUI_ASSERT_RANGE(_index1, mItemsInfo.size(), "TabControl::swapItems");
177 MYGUI_ASSERT_RANGE(_index2, mItemsInfo.size(), "TabControl::swapItems");
178
179 if (_index1 != _index2)
180 {
181 std::swap(mItemsInfo[_index1], mItemsInfo[_index2]);
182 updateBar();
183 }
184 }
185
186 void TabControl::setPosition(const IntPoint& _point)
187 {
188 Base::setPosition(_point);
189
190 updateBar();
191 }
192
193 void TabControl::setSize(const IntSize& _size)
194 {
195 Base::setSize(_size);
196
197 updateBar();
198 }
199
200 void TabControl::setCoord(const IntCoord& _coord)
201 {
202 Base::setCoord(_coord);
203
204 updateBar();
205 }
206
207 void TabControl::notifyPressedButtonEvent(MyGUI::Widget* _sender)
208 {
209 if (_sender == mButtonLeft)
210 {
211 if (mStartIndex > 0)
212 {
213 mStartIndex--;
214 updateBar();
215 }
216 }
217 else if (_sender == mButtonRight)
218 {
219 if ((mStartIndex + 1) < mItemsInfo.size())
220 {
221 mStartIndex++;
222 // will be corrected in updateBar() if needed
223 updateBar();
224 }
225 }
226 }
227
228 void TabControl::notifyPressedBarButtonEvent(MyGUI::Widget* _sender)
229 {
230 size_t select = *_sender->_getInternalData<size_t>() + mStartIndex;
231 if (select == mIndexSelect)
232 {
233 beginToItemSelected();
234 return;
235 }
236 size_t old = mIndexSelect;
237 mIndexSelect = select;
238
239 size_t count = 0;
240 for (size_t pos = 0; pos < mItemButton.size(); pos++)
241 {
242 Button* button = mItemButton[count]->castType<Button>();
243 if (button->getVisible())
244 {
245 button->setStateSelected((pos + mStartIndex) == mIndexSelect);
246 }
247 count++;
248 }
249
250 beginToItemSelected();
251
252 // raise page for picking
253 _forcePick(mItemsInfo[mIndexSelect].item);
254
255 _showItem(mItemsInfo[mIndexSelect].item, true, mSmoothShow);
256 _showItem(mItemsInfo[old].item, false, mSmoothShow);
257
258 eventTabChangeSelect(this, mIndexSelect);
259 }
260
261 void TabControl::beginToItemAt(size_t _index)
262 {
263 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::beginToItemAt");
264
265 int widthBar = _getWidgetBar()->getWidth();
266 if (mHeaderPlace != nullptr)
267 {
268 widthBar = mHeaderPlace->getWidth();
269 if (mButtonShow && mControls != nullptr)
270 widthBar -= mControls->getWidth();
271 }
272
273 if (widthBar < 1)
274 return;
275
276 if (_index == mStartIndex)
277 return;
278 if (_index < mStartIndex)
279 {
280 mStartIndex = _index;
281 updateBar();
282 }
283 else
284 {
285 // bar length from start index to target inclusive
286 int width = 0;
287 for (size_t pos = mStartIndex; pos <= _index; pos++)
288 {
289 width += mItemsInfo[pos].width;
290 }
291
292 // decrease start index until target appears
293 bool change = false;
294 while ((mStartIndex < _index) && (width > widthBar))
295 {
296 width -= mItemsInfo[mStartIndex].width;
297 mStartIndex++;
298 change = true;
299 }
300 if (change)
301 updateBar();
302 }
303 }
304
305 void TabControl::setButtonDefaultWidth(int _width)
306 {
307 mButtonDefaultWidth = _width;
308 if (mButtonDefaultWidth < 1)
309 mButtonDefaultWidth = 1;
310 setButtonAutoWidth(false);
311 }
312
313 void TabControl::setButtonAutoWidth(bool _auto)
314 {
315 mButtonAutoWidth = _auto;
316
317 for (auto& info : mItemsInfo)
318 {
319 int width;
320 if (mButtonAutoWidth)
321 width = _getTextWidth(info.name);
322 else
323 width = mButtonDefaultWidth;
324
325 mWidthBar += width - info.width;
326 info.width = width;
327 }
328
329 updateBar();
330 }
331
332 void TabControl::setButtonWidthAt(size_t _index, int _width)
333 {
334 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setButtonWidthAt");
335
336 if (_width <= 0)
337 {
338 if (mButtonAutoWidth)
339 _width = _getTextWidth(mItemsInfo[_index].name);
340 else
341 _width = mButtonDefaultWidth;
342 }
343
344 mWidthBar += _width - mItemsInfo[_index].width;
345 mItemsInfo[_index].width = _width;
346
347 updateBar();
348 }
349
350 void TabControl::setItemNameAt(size_t _index, const UString& _name)
351 {
352 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setItemNameAt");
353 mItemsInfo[_index].name = _name;
354
355 int width;
356 if (mButtonAutoWidth)
357 width = _getTextWidth(_name);
358 else
359 width = mButtonDefaultWidth;
360
361 mWidthBar += width - mItemsInfo[_index].width;
362 mItemsInfo[_index].width = width;
363
364 updateBar();
365 }
366
367 void TabControl::setIndexSelected(size_t _index)
368 {
369 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setIndexSelected");
370 if (mIndexSelect == _index)
371 return;
372 size_t old = mIndexSelect;
373 mIndexSelect = _index;
374 updateBar();
375
376 // raise page for picking
377 if (mSmoothShow)
378 _forcePick(mItemsInfo[mIndexSelect].item);
379
380 _showItem(mItemsInfo[mIndexSelect].item, true, mSmoothShow);
381 _showItem(mItemsInfo[old].item, false, mSmoothShow);
382
383 beginToItemSelected();
384 }
385
386 void TabControl::actionWidgetHide(Widget* _widget, ControllerItem* _controller)
387 {
388 _widget->setVisible(false);
389 _widget->setEnabled(true);
390 }
391
392 void TabControl::_showItem(TabItem* _item, bool _show, bool _smooth)
393 {
394 if (!_smooth)
395 {
396 ControllerManager::getInstance().removeItem(_item);
397 _item->setAlpha(ALPHA_MAX);
398
399 _item->setVisible(_show);
400
401 return;
402 }
403
404 if (_show)
405 {
406 // enable _item in case actionWidgetHide was skipped due to rapid click
407 _item->setEnabled(true);
408 ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MAX, TAB_SPEED_FADE_COEF, true);
409 ControllerManager::getInstance().addItem(_item, controller);
410 }
411 else
412 {
413 ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MIN, TAB_SPEED_FADE_COEF, false);
414 controller->eventPostAction += newDelegate(this, &TabControl::actionWidgetHide);
415 ControllerManager::getInstance().addItem(_item, controller);
416 }
417 }
418
419 Button* TabControl::createButton()
420 {
421 Widget* parent = this;
422 if (mWidgetBar != nullptr)
423 parent = mWidgetBar;
424 else if (mHeaderPlace != nullptr)
425 parent = mHeaderPlace;
426
427 return parent->createWidget<Button>(mButtonSkinName, IntCoord(), Align::Left | Align::Top);
428 }
429
430 void TabControl::_createItemButton()
431 {
432 Button* button = createButton();
433 button->eventMouseButtonClick += newDelegate(this, &TabControl::notifyPressedBarButtonEvent);
434 button->_setInternalData(mItemButton.size()); // sequence number
435 mItemButton.push_back(button);
436 }
437
438 int TabControl::_getTextWidth(const UString& _text)
439 {
440 if (mItemButton.empty())
441 _createItemButton();
442
443 UString save = mItemButton[0]->getCaption();
444 mItemButton[0]->setCaption(_text);
445
446 ISubWidgetText* text = mItemButton[0]->getSubWidgetText();
447 const IntSize& size = text ? text->getTextSize() : IntSize();
448 const IntCoord& coord = text ? text->getCoord() : IntCoord();
449
450 mItemButton[0]->setCaption(save);
451
452 return size.width + mItemButton[0]->getWidth() - coord.width;
453 }
454
455 void TabControl::_notifyDeleteItem(TabItem* _sheet)
456 {
457 // general widget shutdown
458 if (mShutdown)
459 return;
460
461 size_t index = getItemIndex(_sheet);
462
463 mWidthBar -= mItemsInfo[index].width;
464 mItemsInfo.erase(mItemsInfo.begin() + index);
465
466 if (mItemsInfo.empty())
467 mIndexSelect = ITEM_NONE;
468 else
469 {
470 if (index < mIndexSelect)
471 mIndexSelect--;
472 else if (index == mIndexSelect)
473 {
474 if (mIndexSelect == mItemsInfo.size())
475 mIndexSelect--;
476 mItemsInfo[mIndexSelect].item->setVisible(true);
477 mItemsInfo[mIndexSelect].item->setAlpha(ALPHA_MAX);
478 }
479 }
480
481 updateBar();
482 }
483
484 void TabControl::_insertItem(size_t _index, const UString& _name, TabItem* _sheet, Any _data)
485 {
486 if (_index == ITEM_NONE)
487 _index = mItemsInfo.size();
488
489 int width = (mButtonAutoWidth ? _getTextWidth(_name) : mButtonDefaultWidth);
490 mWidthBar += width;
491
492 mItemsInfo.insert(mItemsInfo.begin() + _index, TabItemInfo(width, _name, _sheet, _data));
493
494 if (1 == mItemsInfo.size())
495 mIndexSelect = 0;
496 else
497 {
498 _sheet->setVisible(false);
499 if (_index <= mIndexSelect)
500 mIndexSelect++;
501 }
502
503 updateBar();
504 }
505
506 void TabControl::setItemDataAt(size_t _index, Any _data)
507 {
508 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setItemDataAt");
509 mItemsInfo[_index].data = std::move(_data);
510 }
511
512 int TabControl::getButtonWidthAt(size_t _index) const
513 {
514 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::getButtonWidthAt");
515 return mItemsInfo[_index].width;
516 }
517
518 const UString& TabControl::getItemNameAt(size_t _index) const
519 {
520 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::getItemNameAt");
521 return mItemsInfo[_index].name;
522 }
523
524 TabItem* TabControl::getItemAt(size_t _index) const
525 {
526 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::getItemAt");
527 return mItemsInfo[_index].item;
528 }
529
530 void TabControl::removeItemAt(size_t _index)
531 {
532 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::removeItemAt");
533 this->_destroyChildWidget(mItemsInfo[_index].item);
534 }
535
536 void TabControl::removeAllItems()
537 {
538 while (!mItemsInfo.empty())
539 {
540 _destroyChildWidget(mItemsInfo.back().item);
541 }
542 }
543
544 ControllerFadeAlpha* TabControl::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
545 {
546 ControllerItem* item = ControllerManager::getInstance().createItem(ControllerFadeAlpha::getClassTypeName());
547 ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
548
549 controller->setAlpha(_alpha);
550 controller->setCoef(_coef);
551 controller->setEnabled(_enable);
552
553 return controller;
554 }
555
556 size_t TabControl::getItemIndex(const TabItem* _item) const
557 {
558 for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
559 {
560 if (mItemsInfo[pos].item == _item)
561 return pos;
562 }
563 MYGUI_EXCEPT("item (" << _item << ") not found, source 'TabControl::getItemIndex'");
564 }
565
566 size_t TabControl::findItemIndex(const TabItem* _item)
567 {
568 for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
569 {
570 if (mItemsInfo[pos].item == _item)
571 return pos;
572 }
573 return ITEM_NONE;
574 }
575
576 size_t TabControl::findItemIndexWith(const UString& _name)
577 {
578 for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
579 {
580 if (mItemsInfo[pos].name == _name)
581 return pos;
582 }
583 return ITEM_NONE;
584 }
585
586 TabItem* TabControl::findItemWith(const UString& _name)
587 {
588 for (auto& info : mItemsInfo)
589 {
590 if (info.name == _name)
591 return info.item;
592 }
593 return nullptr;
594 }
595
596 TabItem* TabControl::getItemSelected() const
597 {
598 return getIndexSelected() != ITEM_NONE ? getItemAt(getIndexSelected()) : nullptr;
599 }
600
601 Widget* TabControl::_getWidgetTemplate()
602 {
603 return mItemTemplate == nullptr ? this : mItemTemplate;
604 }
605
606 Widget* TabControl::_getWidgetBar()
607 {
608 return mWidgetBar == nullptr ? this : mWidgetBar;
609 }
610
611 void TabControl::setPropertyOverride(std::string_view _key, std::string_view _value)
612 {
614 if (_key == "ButtonWidth")
615 setButtonDefaultWidth(utility::parseValue<int>(_value));
616
618 else if (_key == "ButtonAutoWidth")
619 setButtonAutoWidth(utility::parseValue<bool>(_value));
620
622 else if (_key == "SmoothShow")
623 setSmoothShow(utility::parseValue<bool>(_value));
624
625 else if (_key == "SelectItem")
626 setIndexSelected(utility::parseValue<size_t>(_value));
627
628 else
629 {
630 Base::setPropertyOverride(_key, _value);
631 return;
632 }
633
634 eventChangeProperty(this, _key, _value);
635 }
636
637 size_t TabControl::getItemCount() const
638 {
639 return mItemsInfo.size();
640 }
641
642 TabItem* TabControl::insertItem(TabItem* _to, const UString& _name, Any _data)
643 {
644 return insertItemAt(getItemIndex(_to), _name, _data);
645 }
646
647 TabItem* TabControl::addItem(const UString& _name, Any _data)
648 {
649 return insertItemAt(ITEM_NONE, _name, _data);
650 }
651
652 void TabControl::removeItem(TabItem* _item)
653 {
654 removeItemAt(getItemIndex(_item));
655 }
656
657 size_t TabControl::getIndexSelected() const
658 {
659 return mIndexSelect;
660 }
661
662 void TabControl::setItemSelected(TabItem* _item)
663 {
664 setIndexSelected(getItemIndex(_item));
665 }
666
667 void TabControl::setItemData(TabItem* _item, Any _data)
668 {
669 setItemDataAt(getItemIndex(_item), _data);
670 }
671
672 void TabControl::clearItemDataAt(size_t _index)
673 {
674 setItemDataAt(_index, Any::Null);
675 }
676
677 void TabControl::clearItemData(TabItem* _item)
678 {
679 clearItemDataAt(getItemIndex(_item));
680 }
681
682 void TabControl::setItemName(TabItem* _item, const UString& _name)
683 {
684 setItemNameAt(getItemIndex(_item), _name);
685 }
686
687 const UString& TabControl::getItemName(const TabItem* _item) const
688 {
689 return getItemNameAt(getItemIndex(_item));
690 }
691
692 void TabControl::beginToItem(const TabItem* _item)
693 {
694 beginToItemAt(getItemIndex(_item));
695 }
696
697 void TabControl::beginToItemFirst()
698 {
699 if (getItemCount())
700 beginToItemAt(0);
701 }
702
703 void TabControl::beginToItemLast()
704 {
705 if (getItemCount())
706 beginToItemAt(getItemCount() - 1);
707 }
708
709 void TabControl::beginToItemSelected()
710 {
711 if (getIndexSelected() != ITEM_NONE)
712 beginToItemAt(getIndexSelected());
713 }
714
715 void TabControl::setButtonWidth(TabItem* _item, int _width)
716 {
717 setButtonWidthAt(getItemIndex(_item), _width);
718 }
719
720 int TabControl::getButtonWidth(TabItem* _item) const
721 {
722 return getButtonWidthAt(getItemIndex(_item));
723 }
724
725 int TabControl::getButtonDefaultWidth() const
726 {
727 return mButtonDefaultWidth;
728 }
729
730 bool TabControl::getButtonAutoWidth() const
731 {
732 return mButtonAutoWidth;
733 }
734
735 void TabControl::setSmoothShow(bool _value)
736 {
737 mSmoothShow = _value;
738 }
739
740 bool TabControl::getSmoothShow() const
741 {
742 return mSmoothShow;
743 }
744
745 size_t TabControl::_getItemCount() const
746 {
747 return getItemCount();
748 }
749
750 void TabControl::_addItem(const MyGUI::UString& _name)
751 {
752 addItem(_name);
753 }
754
755 void TabControl::_removeItemAt(size_t _index)
756 {
757 removeItemAt(_index);
758 }
759
760 Widget* TabControl::_getItemAt(size_t _index) const
761 {
762 return getItemAt(_index);
763 }
764
765 void TabControl::_setItemNameAt(size_t _index, const UString& _name)
766 {
767 setItemNameAt(_index, _name);
768 }
769
770 const UString& TabControl::_getItemNameAt(size_t _index) const
771 {
772 return getItemNameAt(_index);
773 }
774
775 void TabControl::updateBar()
776 {
777 if (mHeaderPlace != nullptr)
778 updateBarNew();
779 else
780 updateBarOld();
781 }
782
783 void TabControl::updateBarOld()
784 {
785 if (_getWidgetBar()->getWidth() < 1)
786 return;
787
788 if ((_getWidgetBar()->getWidth() < mWidthBar) && (1 < mItemsInfo.size()))
789 {
790 if (!mButtonShow)
791 {
792 mButtonShow = true;
793
794 if (nullptr != mButtonLeft)
795 mButtonLeft->setVisible(true);
796 if (nullptr != mButtonRight)
797 mButtonRight->setVisible(true);
798 if (nullptr != mButtonDecor)
799 mButtonDecor->setVisible(true);
800 for (auto& iter : mWidgetsPatch)
801 iter->setVisible(true);
802 if (mWidgetBar != nullptr)
803 mWidgetBar->setSize(mWidgetBar->getWidth() - mOffsetTab, mWidgetBar->getHeight());
804 }
805 }
806 else
807 {
808 if (mButtonShow)
809 {
810 mButtonShow = false;
811 if (nullptr != mButtonLeft)
812 mButtonLeft->setVisible(false);
813 if (nullptr != mButtonRight)
814 mButtonRight->setVisible(false);
815 if (nullptr != mButtonDecor)
816 mButtonDecor->setVisible(false);
817 for (auto& iter : mWidgetsPatch)
818 iter->setVisible(false);
819 if (mWidgetBar != nullptr)
820 mWidgetBar->setSize(mWidgetBar->getWidth() + mOffsetTab, mWidgetBar->getHeight());
821 }
822 }
823
824 // check start index validity
825 if (mStartIndex > 0)
826 {
827 int width = 0;
828 for (size_t pos = mStartIndex; pos < mItemsInfo.size(); pos++)
829 width += mItemsInfo[pos].width;
830
831 // decrease index until button before index fully fits in bar
832 while ((mStartIndex > 0) && ((width + mItemsInfo[mStartIndex - 1].width) <= _getWidgetBar()->getWidth()))
833 {
834 mStartIndex--;
835 width += mItemsInfo[mStartIndex].width;
836 }
837 }
838
839 int width = 0;
840 size_t count = 0;
841 size_t pos = mStartIndex;
842 for (; pos < mItemsInfo.size(); pos++)
843 {
844 if (width > _getWidgetBar()->getWidth())
845 break;
846
847 TabItemInfo& info = mItemsInfo[pos];
848 if ((width + info.width) > _getWidgetBar()->getWidth())
849 {
850 break;
851 }
852
853 // check button physical existence
854 if (count >= mItemButton.size())
855 _createItemButton();
856
857 Button* button = mItemButton[count]->castType<Button>();
858 button->setVisible(true);
859
860 button->setStateSelected(pos == mIndexSelect);
861
862 if (button->getCaption() != info.name)
863 button->setCaption(info.name);
864 IntCoord coord(width, 0, info.width, _getWidgetBar()->getHeight());
865 if (coord != button->getCoord())
866 button->setCoord(coord);
867
868 width += info.width;
869 count++;
870 }
871
872 // hide buttons that were created but not visible
873 while (count < mItemButton.size())
874 {
875 mItemButton[count]->setVisible(false);
876 count++;
877 }
878
879 bool right = true;
880 if (pos == mItemsInfo.size())
881 right = false;
882
883 // crashes in editor for some reason, though this skin is always created
884 if (mEmptyBarWidget != nullptr)
885 {
886 if (width < _getWidgetBar()->getWidth())
887 {
888 mEmptyBarWidget->setVisible(true);
889 mEmptyBarWidget->setCoord(width, 0, _getWidgetBar()->getWidth() - width, _getWidgetBar()->getHeight());
890 }
891 else
892 {
893 mEmptyBarWidget->setVisible(false);
894 }
895 }
896
897 // adjust arrow availability
898 if (mStartIndex == 0)
899 {
900 if (nullptr != mButtonLeft)
901 mButtonLeft->setEnabled(false);
902 }
903 else
904 {
905 if (nullptr != mButtonLeft)
906 mButtonLeft->setEnabled(true);
907 }
908
909 if (right)
910 {
911 if (nullptr != mButtonRight)
912 mButtonRight->setEnabled(true);
913 }
914 else
915 {
916 if (nullptr != mButtonRight)
917 mButtonRight->setEnabled(false);
918 }
919 }
920
921 void TabControl::updateBarNew()
922 {
923 if (mHeaderPlace == nullptr)
924 return;
925
926 if (mHeaderPlace->getWidth() < 1)
927 return;
928
929 int widthControls = 0;
930 if (mControls != nullptr)
931 widthControls = mControls->getWidth();
932
933 if ((mHeaderPlace->getWidth() < mWidthBar) && (1 < mItemsInfo.size()) &&
934 (mHeaderPlace->getWidth() >= widthControls))
935 {
936 if (!mButtonShow)
937 {
938 mButtonShow = true;
939
940 if (nullptr != mControls)
941 mControls->setVisible(true);
942 }
943
944 if (mControls != nullptr)
945 mControls->setCoord(
946 mHeaderPlace->getWidth() - mControls->getWidth(),
947 0,
948 mControls->getWidth(),
949 mHeaderPlace->getHeight());
950 }
951 else
952 {
953 if (mButtonShow)
954 {
955 mButtonShow = false;
956
957 if (nullptr != mControls)
958 mControls->setVisible(false);
959 }
960
961 widthControls = 0;
962 }
963
964 // check start index validity
965 if (mStartIndex > 0)
966 {
967 int width = 0;
968 for (size_t pos = mStartIndex; pos < mItemsInfo.size(); pos++)
969 width += mItemsInfo[pos].width;
970
971 // decrease index until button before index fully fits in bar
972 while ((mStartIndex > 0) &&
973 ((width + mItemsInfo[mStartIndex - 1].width) <= (mHeaderPlace->getWidth() - widthControls)))
974 {
975 mStartIndex--;
976 width += mItemsInfo[mStartIndex].width;
977 }
978 }
979
980 int width = 0;
981 size_t count = 0;
982 size_t pos = mStartIndex;
983 for (; pos < mItemsInfo.size(); pos++)
984 {
985 if (width > (mHeaderPlace->getWidth() - widthControls))
986 break;
987
988 TabItemInfo& info = mItemsInfo[pos];
989 if ((width + info.width) > (mHeaderPlace->getWidth() - widthControls))
990 {
991 break;
992 }
993
994 // check button physical existence
995 if (count >= mItemButton.size())
996 _createItemButton();
997 Button* button = mItemButton[count];
998 button->setVisible(true);
999
1000 button->setStateSelected(pos == mIndexSelect);
1001
1002 if (button->getCaption() != info.name)
1003 button->setCaption(info.name);
1004 IntCoord coord(width, 0, info.width, mHeaderPlace->getHeight());
1005 if (coord != button->getCoord())
1006 button->setCoord(coord);
1007
1008 width += info.width;
1009 count++;
1010 }
1011
1012 // hide buttons that were created but not visible
1013 while (count < mItemButton.size())
1014 {
1015 mItemButton[count]->setVisible(false);
1016 count++;
1017 }
1018
1019 bool right = true;
1020 if (pos == mItemsInfo.size())
1021 right = false;
1022
1023 if (mEmpty != nullptr)
1024 {
1025 mEmpty->setCoord(width, 0, mHeaderPlace->getWidth() - width - widthControls, mHeaderPlace->getHeight());
1026 }
1027
1028 // adjust arrow availability
1029 if (mStartIndex == 0)
1030 {
1031 if (nullptr != mButtonLeft)
1032 mButtonLeft->setEnabled(false);
1033 }
1034 else
1035 {
1036 if (nullptr != mButtonLeft)
1037 mButtonLeft->setEnabled(true);
1038 }
1039
1040 if (right)
1041 {
1042 if (nullptr != mButtonRight)
1043 mButtonRight->setEnabled(true);
1044 }
1045 else
1046 {
1047 if (nullptr != mButtonRight)
1048 mButtonRight->setEnabled(false);
1049 }
1050 }
1051
1052} // namespace MyGUI
#define MYGUI_EXCEPT(dest)
#define MYGUI_ASSERT_RANGE_INSERT(index, size, owner)
#define MYGUI_ASSERT_RANGE(index, size, owner)
template MYGUI_EXPORT int parseValue< int >(std::string_view)
T parseValue(std::string_view value)
const float TAB_SPEED_FADE_COEF
constexpr float ALPHA_MAX
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
constexpr float ALPHA_MIN
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))