MyGUI  3.2.1
MyGUI_TabControl.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_TabControl.h"
00009 #include "MyGUI_ControllerManager.h"
00010 #include "MyGUI_WidgetManager.h"
00011 #include "MyGUI_Button.h"
00012 #include "MyGUI_TabItem.h"
00013 #include "MyGUI_ResourceSkin.h"
00014 
00015 namespace MyGUI
00016 {
00017 
00018     const float TAB_SPEED_FADE_COEF = 5.0f;
00019 
00020     TabControl::TabControl() :
00021         mOffsetTab(0),
00022         mButtonShow(true),
00023         mWidthBar(0),
00024         mWidgetBar(nullptr),
00025         mButtonLeft(nullptr),
00026         mButtonRight(nullptr),
00027         mButtonDecor(nullptr),
00028         mEmptyBarWidget(nullptr),
00029         mItemTemplate(nullptr),
00030         mStartIndex(0),
00031         mIndexSelect(ITEM_NONE),
00032         mButtonDefaultWidth(1),
00033         mSmoothShow(true),
00034         mButtonAutoWidth(true),
00035         mShutdown(false),
00036         mHeaderPlace(nullptr),
00037         mControls(nullptr),
00038         mEmpty(nullptr)
00039     {
00040     }
00041 
00042     void TabControl::initialiseOverride()
00043     {
00044         Base::initialiseOverride();
00045 
00046         if (isUserString("ButtonSkin"))
00047             mButtonSkinName = getUserString("ButtonSkin");
00048 
00049         // OBSOLETE
00050         if (isUserString("OffsetBar"))
00051             mOffsetTab = utility::parseValue<int>(getUserString("OffsetBar"));
00052 
00053         // OBSOLETE
00054         if (isUserString("EmptyBarSkin"))
00055             mEmptySkinName = getUserString("EmptyBarSkin");
00056 
00057         // OBSOLETE
00058         assignWidget(mWidgetBar, "Bar");
00059         if (mWidgetBar != nullptr)
00060         {
00061             mWidgetBar->setSize(mWidgetBar->getWidth() - mOffsetTab, mWidgetBar->getHeight());
00062         }
00063 
00065         assignWidget(mButtonLeft, "Left");
00066         if (mButtonLeft != nullptr)
00067         {
00068             mButtonLeft->eventMouseButtonClick += newDelegate(this, &TabControl::notifyPressedButtonEvent);
00069         }
00070 
00072         assignWidget(mButtonRight, "Right");
00073         if (mButtonRight != nullptr)
00074         {
00075             mButtonRight->eventMouseButtonClick += newDelegate(this, &TabControl::notifyPressedButtonEvent);
00076         }
00077 
00078         // OBSOLETE
00079         assignWidget(mButtonDecor, "ButtonDecor");
00080         if (mButtonDecor != nullptr)
00081         {
00082             mButtonDecor->setVisible(false);
00083         }
00084 
00086         assignWidget(mItemTemplate, "TabItem");
00087         if (mItemTemplate != nullptr)
00088         {
00089             mItemTemplate->setVisible(false);
00090         }
00091 
00092 #ifndef MYGUI_DONT_USE_OBSOLETE
00093         if (mItemTemplate == nullptr)
00094         {
00095             assignWidget(mItemTemplate, "Sheet");
00096             if (mItemTemplate != nullptr)
00097             {
00098                 mItemTemplate->setVisible(false);
00099             }
00100         }
00101 #endif // MYGUI_DONT_USE_OBSOLETE
00102 
00103         // OBSOLETE
00104         Widget* showPatch = nullptr;
00105         assignWidget(showPatch, "ShowPatch");
00106         if (showPatch != nullptr)
00107         {
00108             mWidgetsPatch.push_back(showPatch);
00109             showPatch->setVisible(false);
00110         }
00111 
00113         assignWidget(mHeaderPlace, "HeaderPlace");
00114 
00116         assignWidget(mControls, "Controls");
00117 
00119         assignWidget(mEmpty, "Empty");
00120 
00121         if (mEmpty == nullptr)
00122         {
00123             // создаем виджет, носитель скина пустоты бара
00124             // OBSOLETE
00125             mEmptyBarWidget = _getWidgetBar()->createWidget<Widget>(mEmptySkinName, IntCoord(), Align::Left | Align::Top);
00126         }
00127 
00128         updateBar();
00129 
00130         // FIXME добавленно, так как шетдаун вызывается и при смене скина
00131         mShutdown = false;
00132     }
00133 
00134     void TabControl::shutdownOverride()
00135     {
00136         mWidgetsPatch.clear();
00137         mWidgetBar = nullptr;
00138         mButtonLeft = nullptr;
00139         mButtonRight = nullptr;
00140         mButtonDecor = nullptr;
00141         mItemTemplate = nullptr;
00142         mEmptyBarWidget = nullptr;
00143 
00144         mHeaderPlace = nullptr;
00145         mControls = nullptr;
00146         mEmpty = nullptr;
00147 
00148         // FIXME перенесенно из деструктора, может косячить при смене скина
00149         mShutdown = true;
00150 
00151         Base::shutdownOverride();
00152     }
00153 
00154     void TabControl::onWidgetCreated(Widget* _widget)
00155     {
00156         Base::onWidgetCreated(_widget);
00157 
00158         TabItem* child = _widget->castType<TabItem>(false);
00159         if (child != nullptr)
00160         {
00161             child->setCoord(_getWidgetTemplate()->getAbsoluteLeft() - getAbsoluteLeft(), _getWidgetTemplate()->getAbsoluteTop() - getAbsoluteTop(), _getWidgetTemplate()->getWidth(), _getWidgetTemplate()->getHeight());
00162             child->setAlign(_getWidgetTemplate()->getAlign());
00163 
00164             _insertItem(ITEM_NONE, "", child, Any::Null);
00165         }
00166     }
00167 
00168     TabItem* TabControl::insertItemAt(size_t _index, const UString& _name, Any _data)
00169     {
00170         MYGUI_ASSERT_RANGE_INSERT(_index, mItemsInfo.size(), "TabControl::insertItem");
00171 
00172         Widget* widget = Base::baseCreateWidget(WidgetStyle::Child, TabItem::getClassTypeName(), "Default", _getWidgetTemplate()->getCoord(), _getWidgetTemplate()->getAlign(), "", "", false);
00173 
00174         size_t lastIndex = mItemsInfo.size() - 1;
00175         setItemNameAt(lastIndex, _name);
00176         setItemDataAt(lastIndex, _data);
00177 
00178         swapItems(_index == ITEM_NONE ? lastIndex : _index, lastIndex);
00179 
00180         return widget->castType<TabItem>();
00181     }
00182 
00183     void TabControl::swapItems(size_t _index1, size_t _index2)
00184     {
00185         MYGUI_ASSERT_RANGE(_index1, mItemsInfo.size(), "TabControl::swapItems");
00186         MYGUI_ASSERT_RANGE(_index2, mItemsInfo.size(), "TabControl::swapItems");
00187 
00188         if (_index1 != _index2)
00189         {
00190             std::swap(mItemsInfo[_index1], mItemsInfo[_index2]);
00191             updateBar();
00192         }
00193     }
00194 
00195     void TabControl::setPosition(const IntPoint& _point)
00196     {
00197         Base::setPosition(_point);
00198 
00199         updateBar();
00200     }
00201 
00202     void TabControl::setSize(const IntSize& _size)
00203     {
00204         Base::setSize(_size);
00205 
00206         updateBar();
00207     }
00208 
00209     void TabControl::setCoord(const IntCoord& _coord)
00210     {
00211         Base::setCoord(_coord);
00212 
00213         updateBar();
00214     }
00215 
00216     void TabControl::notifyPressedButtonEvent(MyGUI::Widget* _sender)
00217     {
00218         if (_sender == mButtonLeft)
00219         {
00220             if (mStartIndex > 0)
00221             {
00222                 mStartIndex --;
00223                 updateBar();
00224             }
00225         }
00226         else if (_sender == mButtonRight)
00227         {
00228             if ((mStartIndex + 1) < mItemsInfo.size())
00229             {
00230                 mStartIndex ++;
00231                 // в updateBar() будет подкорректированно если что
00232                 updateBar();
00233             }
00234         }
00235     }
00236 
00237     void TabControl::notifyPressedBarButtonEvent(MyGUI::Widget* _sender)
00238     {
00239         size_t select = *_sender->_getInternalData<size_t>() + mStartIndex;
00240         // щелкнули по той же кнопке
00241         if (select == mIndexSelect)
00242         {
00243             // стараемся показать выделенную кнопку
00244             beginToItemSelected();
00245             return;
00246         }
00247         size_t old = mIndexSelect;
00248         mIndexSelect = select;
00249 
00250         size_t count = 0;
00251         for (size_t pos = 0; pos < mItemButton.size(); pos++)
00252         {
00253             Button* button = mItemButton[count]->castType<Button>();
00254             if (button->getVisible())
00255             {
00256                 // корректируем нажатость кнопки
00257                 button->setStateSelected((pos + mStartIndex) == mIndexSelect);
00258             }
00259             count ++;
00260         }
00261 
00262         // стараемся показать выделенную кнопку
00263         beginToItemSelected();
00264 
00265         // поднимаем страницу для пикинга
00266         _forcePick(mItemsInfo[mIndexSelect].item);
00267 
00268         _showItem(mItemsInfo[mIndexSelect].item, true, mSmoothShow);
00269         _showItem(mItemsInfo[old].item, false, mSmoothShow);
00270 
00271         eventTabChangeSelect(this, mIndexSelect);
00272     }
00273 
00274     void TabControl::beginToItemAt(size_t _index)
00275     {
00276         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::beginToItemAt");
00277 
00278         // подстраховка
00279         if (_getWidgetBar()->getWidth() < 1)
00280             return;
00281 
00282         if (_index == mStartIndex)
00283             return;
00284         else if (_index < mStartIndex)
00285         {
00286             mStartIndex = _index;
00287             updateBar();
00288         }
00289         else
00290         {
00291             // длинна бара от старт индекса до нужной включительно
00292             int width = 0;
00293             for (size_t pos = mStartIndex; pos <= _index; pos++)
00294             {
00295                 width += mItemsInfo[pos].width;
00296             }
00297 
00298             // уменьшем старт индекс пока не появиться нужная
00299             bool change = false;
00300             while ((mStartIndex < _index) && (width > _getWidgetBar()->getWidth()))
00301             {
00302                 width -= mItemsInfo[mStartIndex].width;
00303                 mStartIndex ++;
00304                 change = true;
00305             }
00306             if (change)
00307                 updateBar();
00308         }
00309     }
00310 
00311     void TabControl::setButtonDefaultWidth(int _width)
00312     {
00313         mButtonDefaultWidth = _width;
00314         if (mButtonDefaultWidth < 1)
00315             mButtonDefaultWidth = 1;
00316         setButtonAutoWidth(false);
00317     }
00318 
00319     void TabControl::setButtonAutoWidth(bool _auto)
00320     {
00321         mButtonAutoWidth = _auto;
00322 
00323         for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
00324         {
00325             int width;
00326             if (mButtonAutoWidth)
00327                 width = _getTextWidth(mItemsInfo[pos].name);
00328             else
00329                 width = mButtonDefaultWidth;
00330 
00331             mWidthBar += width - mItemsInfo[pos].width;
00332             mItemsInfo[pos].width = width;
00333         }
00334 
00335         updateBar();
00336     }
00337 
00338     void TabControl::setButtonWidthAt(size_t _index, int _width)
00339     {
00340         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setButtonWidthAt");
00341 
00342         if (_width <= 0)
00343         {
00344             if (mButtonAutoWidth)
00345                 _width = _getTextWidth(mItemsInfo[_index].name);
00346             else
00347                 _width = mButtonDefaultWidth;
00348         }
00349 
00350         mWidthBar += _width - mItemsInfo[_index].width;
00351         mItemsInfo[_index].width = _width;
00352 
00353         updateBar();
00354     }
00355 
00356     void TabControl::setItemNameAt(size_t _index, const UString& _name)
00357     {
00358         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setItemNameAt");
00359         mItemsInfo[_index].name = _name;
00360 
00361         int width;
00362         if (mButtonAutoWidth)
00363             width = _getTextWidth(_name);
00364         else
00365             width = mButtonDefaultWidth;
00366 
00367         mWidthBar += width - mItemsInfo[_index].width;
00368         mItemsInfo[_index].width = width;
00369 
00370         updateBar();
00371     }
00372 
00373     void TabControl::setIndexSelected(size_t _index)
00374     {
00375         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setIndexSelected");
00376         if (mIndexSelect == _index)
00377             return;
00378         size_t old = mIndexSelect;
00379         mIndexSelect = _index;
00380         updateBar();
00381 
00382         // поднимаем страницу для пикинга
00383         if (mSmoothShow)
00384             _forcePick(mItemsInfo[mIndexSelect].item);
00385 
00386         _showItem(mItemsInfo[mIndexSelect].item, true, mSmoothShow);
00387         _showItem(mItemsInfo[old].item, false, mSmoothShow);
00388 
00389         beginToItemSelected();
00390     }
00391 
00392     void TabControl::actionWidgetHide(Widget* _widget, ControllerItem* _controller)
00393     {
00394         _widget->setVisible(false);
00395         _widget->setEnabled(true);
00396     }
00397 
00398     void TabControl::_showItem(TabItem* _item, bool _show, bool _smooth)
00399     {
00400         if (!_smooth)
00401         {
00402             ControllerManager::getInstance().removeItem(_item);
00403             _item->setAlpha(ALPHA_MAX);
00404 
00405             _item->setVisible(_show);
00406 
00407             return;
00408         }
00409 
00410         if (_show)
00411         {
00412             ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MAX, TAB_SPEED_FADE_COEF, true);
00413             ControllerManager::getInstance().addItem(_item, controller);
00414         }
00415         else
00416         {
00417             ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MIN, TAB_SPEED_FADE_COEF, false);
00418             controller->eventPostAction += newDelegate(this, &TabControl::actionWidgetHide);
00419             ControllerManager::getInstance().addItem(_item, controller);
00420         }
00421     }
00422 
00423     Button* TabControl::createButton()
00424     {
00425         Widget* parent = this;
00426         if (mWidgetBar != nullptr)
00427             parent = mWidgetBar;
00428         else if (mHeaderPlace != nullptr)
00429             parent = mHeaderPlace;
00430 
00431         return parent->createWidget<Button>(mButtonSkinName, IntCoord(), Align::Left | Align::Top);
00432     }
00433 
00434     void TabControl::_createItemButton()
00435     {
00436         Button* button = createButton();
00437         button->eventMouseButtonClick += newDelegate(this, &TabControl::notifyPressedBarButtonEvent);
00438         button->_setInternalData(mItemButton.size()); // порядковый номер
00439         mItemButton.push_back(button);
00440     }
00441 
00442     int TabControl::_getTextWidth(const UString& _text)
00443     {
00444         if (mItemButton.empty())
00445             _createItemButton();
00446 
00447         UString save = mItemButton[0]->getCaption();
00448         mItemButton[0]->setCaption(_text);
00449 
00450         ISubWidgetText* text = mItemButton[0]->getSubWidgetText();
00451         const IntSize& size = text ? text->getTextSize() : IntSize();
00452         const IntCoord& coord = text ? text->getCoord() : IntCoord();
00453 
00454         mItemButton[0]->setCaption(save);
00455 
00456         return size.width + mItemButton[0]->getWidth() - coord.width;
00457     }
00458 
00459     void TabControl::_notifyDeleteItem(TabItem* _sheet)
00460     {
00461         // общий шутдаун виджета
00462         if (mShutdown)
00463             return;
00464 
00465         size_t index = getItemIndex(_sheet);
00466 
00467         mWidthBar -= mItemsInfo[index].width;
00468         mItemsInfo.erase(mItemsInfo.begin() + index);
00469 
00470         if (mItemsInfo.empty())
00471             mIndexSelect = ITEM_NONE;
00472         else
00473         {
00474             if (index < mIndexSelect)
00475                 mIndexSelect --;
00476             else if (index == mIndexSelect)
00477             {
00478                 if (mIndexSelect == mItemsInfo.size())
00479                     mIndexSelect --;
00480                 mItemsInfo[mIndexSelect].item->setVisible(true);
00481                 mItemsInfo[mIndexSelect].item->setAlpha(ALPHA_MAX);
00482             }
00483         }
00484 
00485         updateBar();
00486     }
00487 
00488     void TabControl::_insertItem(size_t _index, const UString& _name, TabItem* _sheet, Any _data)
00489     {
00490         if (_index == ITEM_NONE)
00491             _index = mItemsInfo.size();
00492 
00493         // добавляем инфу о вкладке
00494         int width = (mButtonAutoWidth ? _getTextWidth(_name) : mButtonDefaultWidth);
00495         mWidthBar += width;
00496 
00497         mItemsInfo.insert(mItemsInfo.begin() + _index, TabItemInfo(width, _name, _sheet, _data));
00498 
00499         // первая вкладка
00500         if (1 == mItemsInfo.size())
00501             mIndexSelect = 0;
00502         else
00503         {
00504             _sheet->setVisible(false);
00505             if (_index <= mIndexSelect)
00506                 mIndexSelect ++;
00507         }
00508 
00509         updateBar();
00510     }
00511 
00512     void TabControl::setItemDataAt(size_t _index, Any _data)
00513     {
00514         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::setItemDataAt");
00515         mItemsInfo[_index].data = _data;
00516     }
00517 
00518     int TabControl::getButtonWidthAt(size_t _index)
00519     {
00520         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::getButtonWidthAt");
00521         return mItemsInfo[_index].width;
00522     }
00523 
00524     const UString& TabControl::getItemNameAt(size_t _index)
00525     {
00526         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::getItemNameAt");
00527         return mItemsInfo[_index].name;
00528     }
00529 
00530     TabItem* TabControl::getItemAt(size_t _index)
00531     {
00532         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::getItemAt");
00533         return mItemsInfo[_index].item;
00534     }
00535 
00536     void TabControl::removeItemAt(size_t _index)
00537     {
00538         MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "TabControl::removeItemAt");
00539         this->_destroyChildWidget(mItemsInfo[_index].item);
00540     }
00541 
00542     void TabControl::removeAllItems()
00543     {
00544         while (!mItemsInfo.empty())
00545         {
00546             _destroyChildWidget(mItemsInfo.back().item);
00547         }
00548     }
00549 
00550     ControllerFadeAlpha* TabControl::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
00551     {
00552         ControllerItem* item = ControllerManager::getInstance().createItem(ControllerFadeAlpha::getClassTypeName());
00553         ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
00554 
00555         controller->setAlpha(_alpha);
00556         controller->setCoef(_coef);
00557         controller->setEnabled(_enable);
00558 
00559         return controller;
00560     }
00561 
00562     size_t TabControl::getItemIndex(TabItem* _item)
00563     {
00564         for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
00565         {
00566             if (mItemsInfo[pos].item == _item)
00567                 return pos;
00568         }
00569         MYGUI_EXCEPT("item (" << _item << ") not found, source 'TabControl::getItemIndex'");
00570     }
00571 
00572     size_t TabControl::findItemIndex(TabItem* _item)
00573     {
00574         for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
00575         {
00576             if (mItemsInfo[pos].item == _item)
00577                 return pos;
00578         }
00579         return ITEM_NONE;
00580     }
00581 
00582     size_t TabControl::findItemIndexWith(const UString& _name)
00583     {
00584         for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
00585         {
00586             if (mItemsInfo[pos].name == _name)
00587                 return pos;
00588         }
00589         return ITEM_NONE;
00590     }
00591 
00592     TabItem* TabControl::findItemWith(const UString& _name)
00593     {
00594         for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
00595         {
00596             if (mItemsInfo[pos].name == _name)
00597                 return mItemsInfo[pos].item;
00598         }
00599         return nullptr;
00600     }
00601 
00602     TabItem* TabControl::getItemSelected()
00603     {
00604         return getIndexSelected() != ITEM_NONE ? getItemAt(getIndexSelected()) : nullptr;
00605     }
00606 
00607     Widget* TabControl::_getWidgetTemplate()
00608     {
00609         return mItemTemplate == nullptr ? this : mItemTemplate;
00610     }
00611 
00612     Widget* TabControl::_getWidgetBar()
00613     {
00614         return mWidgetBar == nullptr ? this : mWidgetBar;
00615     }
00616 
00617     void TabControl::setPropertyOverride(const std::string& _key, const std::string& _value)
00618     {
00620         if (_key == "ButtonWidth")
00621             setButtonDefaultWidth(utility::parseValue<int>(_value));
00622 
00624         else if (_key == "ButtonAutoWidth")
00625             setButtonAutoWidth(utility::parseValue<bool>(_value));
00626 
00628         else if (_key == "SmoothShow")
00629             setSmoothShow(utility::parseValue<bool>(_value));
00630 
00631         // не коментировать
00632         else if (_key == "SelectItem")
00633             setIndexSelected(utility::parseValue<size_t>(_value));
00634 
00635         else
00636         {
00637             Base::setPropertyOverride(_key, _value);
00638             return;
00639         }
00640 
00641         eventChangeProperty(this, _key, _value);
00642     }
00643 
00644     void TabControl::setPosition(int _left, int _top)
00645     {
00646         setPosition(IntPoint(_left, _top));
00647     }
00648 
00649     void TabControl::setSize(int _width, int _height)
00650     {
00651         setSize(IntSize(_width, _height));
00652     }
00653 
00654     void TabControl::setCoord(int _left, int _top, int _width, int _height)
00655     {
00656         setCoord(IntCoord(_left, _top, _width, _height));
00657     }
00658 
00659     size_t TabControl::getItemCount() const
00660     {
00661         return mItemsInfo.size();
00662     }
00663 
00664     TabItem* TabControl::insertItem(TabItem* _to, const UString& _name, Any _data)
00665     {
00666         return insertItemAt(getItemIndex(_to), _name, _data);
00667     }
00668 
00669     TabItem* TabControl::addItem(const UString& _name, Any _data)
00670     {
00671         return insertItemAt(ITEM_NONE, _name, _data);
00672     }
00673 
00674     void TabControl::removeItem(TabItem* _item)
00675     {
00676         removeItemAt(getItemIndex(_item));
00677     }
00678 
00679     size_t TabControl::getIndexSelected() const
00680     {
00681         return mIndexSelect;
00682     }
00683 
00684     void TabControl::setItemSelected(TabItem* _item)
00685     {
00686         setIndexSelected(getItemIndex(_item));
00687     }
00688 
00689     void TabControl::setItemData(TabItem* _item, Any _data)
00690     {
00691         setItemDataAt(getItemIndex(_item), _data);
00692     }
00693 
00694     void TabControl::clearItemDataAt(size_t _index)
00695     {
00696         setItemDataAt(_index, Any::Null);
00697     }
00698 
00699     void TabControl::clearItemData(TabItem* _item)
00700     {
00701         clearItemDataAt(getItemIndex(_item));
00702     }
00703 
00704     void TabControl::setItemName(TabItem* _item, const UString& _name)
00705     {
00706         setItemNameAt(getItemIndex(_item), _name);
00707     }
00708 
00709     const UString& TabControl::getItemName(TabItem* _item)
00710     {
00711         return getItemNameAt(getItemIndex(_item));
00712     }
00713 
00714     void TabControl::beginToItem(TabItem* _item)
00715     {
00716         beginToItemAt(getItemIndex(_item));
00717     }
00718 
00719     void TabControl::beginToItemFirst()
00720     {
00721         if (getItemCount())
00722             beginToItemAt(0);
00723     }
00724 
00725     void TabControl::beginToItemLast()
00726     {
00727         if (getItemCount())
00728             beginToItemAt(getItemCount() - 1);
00729     }
00730 
00731     void TabControl::beginToItemSelected()
00732     {
00733         if (getIndexSelected() != ITEM_NONE)
00734             beginToItemAt(getIndexSelected());
00735     }
00736 
00737     void TabControl::setButtonWidth(TabItem* _item, int _width)
00738     {
00739         setButtonWidthAt(getItemIndex(_item), _width);
00740     }
00741 
00742     int TabControl::getButtonWidth(TabItem* _item)
00743     {
00744         return getButtonWidthAt(getItemIndex(_item));
00745     }
00746 
00747     int TabControl::getButtonDefaultWidth() const
00748     {
00749         return mButtonDefaultWidth;
00750     }
00751 
00752     bool TabControl::getButtonAutoWidth() const
00753     {
00754         return mButtonAutoWidth;
00755     }
00756 
00757     void TabControl::setSmoothShow(bool _value)
00758     {
00759         mSmoothShow = _value;
00760     }
00761 
00762     bool TabControl::getSmoothShow() const
00763     {
00764         return mSmoothShow;
00765     }
00766 
00767     size_t TabControl::_getItemCount()
00768     {
00769         return getItemCount();
00770     }
00771 
00772     void TabControl::_addItem(const MyGUI::UString& _name)
00773     {
00774         addItem(_name);
00775     }
00776 
00777     void TabControl::_removeItemAt(size_t _index)
00778     {
00779         removeItemAt(_index);
00780     }
00781 
00782     Widget* TabControl::_getItemAt(size_t _index)
00783     {
00784         return getItemAt(_index);
00785     }
00786 
00787     void TabControl::_setItemNameAt(size_t _index, const UString& _name)
00788     {
00789         setItemNameAt(_index, _name);
00790     }
00791 
00792     const UString& TabControl::_getItemNameAt(size_t _index)
00793     {
00794         return getItemNameAt(_index);
00795     }
00796 
00797     void TabControl::updateBar()
00798     {
00799         if (mHeaderPlace != nullptr)
00800             updateBarNew();
00801         else
00802             updateBarOld();
00803     }
00804 
00805     void TabControl::updateBarOld()
00806     {
00807         // подстраховка
00808         if (_getWidgetBar()->getWidth() < 1)
00809             return;
00810 
00811         if ((_getWidgetBar()->getWidth() < mWidthBar) && (1 < mItemsInfo.size()))
00812         {
00813             if (!mButtonShow)
00814             {
00815                 mButtonShow = true;
00816 
00817                 if (nullptr != mButtonLeft)
00818                     mButtonLeft->setVisible(true);
00819                 if (nullptr != mButtonRight)
00820                     mButtonRight->setVisible(true);
00821                 if (nullptr != mButtonDecor)
00822                     mButtonDecor->setVisible(true);
00823                 for (VectorWidgetPtr::iterator iter = mWidgetsPatch.begin(); iter != mWidgetsPatch.end(); ++iter)
00824                     (*iter)->setVisible(true);
00825                 if (mWidgetBar != nullptr)
00826                     mWidgetBar->setSize(mWidgetBar->getWidth() - mOffsetTab, mWidgetBar->getHeight());
00827             }
00828         }
00829         else
00830         {
00831             if (mButtonShow)
00832             {
00833                 mButtonShow = false;
00834                 if (nullptr != mButtonLeft)
00835                     mButtonLeft->setVisible(false);
00836                 if (nullptr != mButtonRight)
00837                     mButtonRight->setVisible(false);
00838                 if (nullptr != mButtonDecor)
00839                     mButtonDecor->setVisible(false);
00840                 for (VectorWidgetPtr::iterator iter = mWidgetsPatch.begin(); iter != mWidgetsPatch.end(); ++iter)
00841                     (*iter)->setVisible(false);
00842                 if (mWidgetBar != nullptr)
00843                     mWidgetBar->setSize(mWidgetBar->getWidth() + mOffsetTab, mWidgetBar->getHeight());
00844             }
00845         }
00846 
00847         // проверяем правильность стартового индекса
00848         if (mStartIndex > 0)
00849         {
00850             // считаем длинну видимых кнопок
00851             int width = 0;
00852             for (size_t pos = mStartIndex; pos < mItemsInfo.size(); pos++)
00853                 width += mItemsInfo[pos].width;
00854 
00855             // уменьшаем индекс до тех пор пока кнопка до индекста полностью не влезет в бар
00856             while ((mStartIndex > 0) && ((width + mItemsInfo[mStartIndex - 1].width) <= _getWidgetBar()->getWidth()))
00857             {
00858                 mStartIndex--;
00859                 width += mItemsInfo[mStartIndex].width;
00860             }
00861         }
00862 
00863         // проверяем и обновляем бар
00864         int width = 0;
00865         size_t count = 0;
00866         size_t pos = mStartIndex;
00867         for (; pos < mItemsInfo.size(); pos++)
00868         {
00869             // текущая кнопка не влазиет
00870             if (width > _getWidgetBar()->getWidth())
00871                 break;
00872 
00873             // следующая не влазиет
00874             TabItemInfo& info = mItemsInfo[pos];
00875             if ((width + info.width) > _getWidgetBar()->getWidth())
00876             {
00877                 break;
00878             }
00879 
00880             // проверяем физическое наличие кнопки
00881             if (count >= mItemButton.size())
00882                 _createItemButton();
00883 
00884             // если кнопка не соответствует, то изменяем ее
00885             Button* button = mItemButton[count]->castType<Button>();
00886             button->setVisible(true);
00887 
00888             // корректируем нажатость кнопки
00889             button->setStateSelected(pos == mIndexSelect);
00890 
00891             if (button->getCaption() != info.name)
00892                 button->setCaption(info.name);
00893             // положение кнопки
00894             IntCoord coord(width, 0, info.width, _getWidgetBar()->getHeight());
00895             if (coord != button->getCoord())
00896                 button->setCoord(coord);
00897 
00898             width += info.width;
00899             count ++;
00900         }
00901 
00902         // скрываем кнопки что были созданны, но не видны
00903         while (count < mItemButton.size())
00904         {
00905             mItemButton[count]->setVisible(false);
00906             count ++;
00907         }
00908 
00909         bool right = true;
00910         if (pos == mItemsInfo.size())
00911             right = false;
00912 
00913         // в редакторе падает почему то, хотя этот скин создается всегда
00914         if (mEmptyBarWidget != nullptr)
00915         {
00916             // корректируем виджет для пустоты
00917             if (width < _getWidgetBar()->getWidth())
00918             {
00919                 mEmptyBarWidget->setVisible(true);
00920                 mEmptyBarWidget->setCoord(width, 0, _getWidgetBar()->getWidth() - width, _getWidgetBar()->getHeight());
00921             }
00922             else
00923             {
00924                 mEmptyBarWidget->setVisible(false);
00925             }
00926         }
00927 
00928         // корректируем доступность стрелок
00929         if (mStartIndex == 0)
00930         {
00931             if (nullptr != mButtonLeft)
00932                 mButtonLeft->setEnabled(false);
00933         }
00934         else
00935         {
00936             if (nullptr != mButtonLeft)
00937                 mButtonLeft->setEnabled(true);
00938         }
00939 
00940         if (right)
00941         {
00942             if (nullptr != mButtonRight)
00943                 mButtonRight->setEnabled(true);
00944         }
00945         else
00946         {
00947             if (nullptr != mButtonRight)
00948                 mButtonRight->setEnabled(false);
00949         }
00950     }
00951 
00952     void TabControl::updateBarNew()
00953     {
00954         if (mHeaderPlace == nullptr)
00955             return;
00956 
00957         // подстраховка
00958         if (mHeaderPlace->getWidth() < 1)
00959             return;
00960 
00961         int widthControls = 0;
00962         if (mControls != nullptr)
00963             widthControls = mControls->getWidth();
00964 
00965         if ((mHeaderPlace->getWidth() < mWidthBar) && (1 < mItemsInfo.size()) && (mHeaderPlace->getWidth() >= widthControls))
00966         {
00967             if (!mButtonShow)
00968             {
00969                 mButtonShow = true;
00970 
00971                 if (nullptr != mControls)
00972                     mControls->setVisible(true);
00973             }
00974 
00975             if (mControls != nullptr)
00976                 mControls->setCoord(mHeaderPlace->getWidth() - mControls->getWidth(), 0, mControls->getWidth(), mHeaderPlace->getHeight());
00977         }
00978         else
00979         {
00980             if (mButtonShow)
00981             {
00982                 mButtonShow = false;
00983 
00984                 if (nullptr != mControls)
00985                     mControls->setVisible(false);
00986             }
00987 
00988             widthControls = 0;
00989         }
00990 
00991         // проверяем правильность стартового индекса
00992         if (mStartIndex > 0)
00993         {
00994             // считаем длинну видимых кнопок
00995             int width = 0;
00996             for (size_t pos = mStartIndex; pos < mItemsInfo.size(); pos++)
00997                 width += mItemsInfo[pos].width;
00998 
00999             // уменьшаем индекс до тех пор пока кнопка до индекста полностью не влезет в бар
01000             while ((mStartIndex > 0) && ((width + mItemsInfo[mStartIndex - 1].width) <= (mHeaderPlace->getWidth() - widthControls)))
01001             {
01002                 mStartIndex--;
01003                 width += mItemsInfo[mStartIndex].width;
01004             }
01005         }
01006 
01007         // проверяем и обновляем бар
01008         int width = 0;
01009         size_t count = 0;
01010         size_t pos = mStartIndex;
01011         for (; pos < mItemsInfo.size(); pos++)
01012         {
01013             // текущая кнопка не влазиет
01014             if (width > (mHeaderPlace->getWidth() - widthControls))
01015                 break;
01016 
01017             // следующая не влазиет
01018             TabItemInfo& info = mItemsInfo[pos];
01019             if ((width + info.width) > (mHeaderPlace->getWidth() - widthControls))
01020             {
01021                 break;
01022             }
01023 
01024             // проверяем физическое наличие кнопки
01025             if (count >= mItemButton.size())
01026                 _createItemButton();
01027 
01028             // если кнопка не соответствует, то изменяем ее
01029             Button* button = mItemButton[count];
01030             button->setVisible(true);
01031 
01032             // корректируем нажатость кнопки
01033             button->setStateSelected(pos == mIndexSelect);
01034 
01035             if (button->getCaption() != info.name)
01036                 button->setCaption(info.name);
01037             // положение кнопки
01038             IntCoord coord(width, 0, info.width, mHeaderPlace->getHeight());
01039             if (coord != button->getCoord())
01040                 button->setCoord(coord);
01041 
01042             width += info.width;
01043             count ++;
01044         }
01045 
01046         // скрываем кнопки что были созданны, но не видны
01047         while (count < mItemButton.size())
01048         {
01049             mItemButton[count]->setVisible(false);
01050             count ++;
01051         }
01052 
01053         bool right = true;
01054         if (pos == mItemsInfo.size())
01055             right = false;
01056 
01057         if (mEmpty != nullptr)
01058         {
01059             // корректируем виджет для пустоты
01060             mEmpty->setCoord(width, 0, mHeaderPlace->getWidth() - width - widthControls, mHeaderPlace->getHeight());
01061         }
01062 
01063         // корректируем доступность стрелок
01064         if (mStartIndex == 0)
01065         {
01066             if (nullptr != mButtonLeft)
01067                 mButtonLeft->setEnabled(false);
01068         }
01069         else
01070         {
01071             if (nullptr != mButtonLeft)
01072                 mButtonLeft->setEnabled(true);
01073         }
01074 
01075         if (right)
01076         {
01077             if (nullptr != mButtonRight)
01078                 mButtonRight->setEnabled(true);
01079         }
01080         else
01081         {
01082             if (nullptr != mButtonRight)
01083                 mButtonRight->setEnabled(false);
01084         }
01085     }
01086 
01087 } // namespace MyGUI