MyGUI 3.2.2

MyGUI_MenuControl.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_MenuControl.h"
9 #include "MyGUI_ResourceSkin.h"
10 #include "MyGUI_MenuItem.h"
11 #include "MyGUI_ImageBox.h"
12 #include "MyGUI_MenuBar.h"
13 #include "MyGUI_WidgetManager.h"
14 #include "MyGUI_LayerManager.h"
15 #include "MyGUI_ControllerManager.h"
16 #include "MyGUI_InputManager.h"
17 #include "MyGUI_Gui.h"
18 #include "MyGUI_RenderManager.h"
19
20 namespace MyGUI
21 {
22
23 const float POPUP_MENU_SPEED_COEF = 3.0f;
24
25 MenuControl::MenuControl() :
26 mHideByAccept(true),
27 mMenuDropMode(false),
28 mIsMenuDrop(true),
29 mHideByLostKey(false),
30 mResizeToContent(true),
31 mShutdown(false),
32 mVerticalAlignment(true),
33 mDistanceButton(0),
34 mPopupAccept(false),
35 mOwner(nullptr),
36 mAnimateSmooth(false),
37 mChangeChildSkin(false),
38 mClient(nullptr),
39 mInternalCreateChild(false)
40 {
41 }
42
43 void MenuControl::initialiseOverride()
44 {
45 Base::initialiseOverride();
46
47 // инициализируем овнера
48 Widget* parent = getParent();
49 if (parent)
50 {
51 mOwner = parent->castType<MenuItem>(false);
52 if (!mOwner)
53 {
54 Widget* client = parent;
55 parent = client->getParent();
56 if (parent && parent->getClientWidget())
57 {
58 mOwner = parent->castType<MenuItem>(false);
59 }
60 }
61 }
62
63 // FIXME нам нужен фокус клавы
64 setNeedKeyFocus(true);
65
67 assignWidget(mClient, "Client");
68 if (mClient != nullptr)
69 {
70 setWidgetClient(mClient);
71 }
72
73 //OBSOLETE
74 if (isUserString("SkinLine"))
75 {
76 mItemNormalSkin = getUserString("SkinLine");
77 mItemPopupSkin = mItemNormalSkin;
78 }
79
80 if (isUserString("SeparatorSkin"))
81 mItemSeparatorSkin = getUserString("SeparatorSkin");
82
83 if (isUserString("NormalSkin"))
84 mItemNormalSkin = getUserString("NormalSkin");
85
86 if (isUserString("PopupSkin"))
87 mItemPopupSkin = getUserString("PopupSkin");
88
89 if (isUserString("DistanceButton"))
90 mDistanceButton = utility::parseValue<int>(getUserString("DistanceButton"));
91
92 if (isUserString("SubMenuSkin"))
93 mSubMenuSkin = getUserString("SubMenuSkin");
94
95 if (isUserString("SubMenuLayer"))
96 mSubMenuLayer = getUserString("SubMenuLayer");
97
98 // FIXME добавленно, так как шетдаун вызывается и при смене скина
99 mShutdown = false;
100 }
101
102 void MenuControl::shutdownOverride()
103 {
104 mShutdown = true;
105
106 if (mOwner != nullptr)
107 mOwner->getMenuCtrlParent()->_notifyDeletePopup(mOwner);
108
109 Base::shutdownOverride();
110 }
111
112 void MenuControl::onWidgetCreated(Widget* _widget)
113 {
114 Base::onWidgetCreated(_widget);
115
116 MenuItem* child = _widget->castType<MenuItem>(false);
117 if (child != nullptr && !mInternalCreateChild)
118 {
119 _wrapItem(child, mItemsInfo.size(), "", MenuItemType::Normal, "", Any::Null);
120 }
121 }
122
123 MenuItem* MenuControl::insertItemAt(size_t _index, const UString& _name, MenuItemType _type, const std::string& _id, Any _data)
124 {
125 MYGUI_ASSERT_RANGE_INSERT(_index, mItemsInfo.size(), "MenuControl::insertItemAt");
126 if (_index == ITEM_NONE) _index = mItemsInfo.size();
127
128 mInternalCreateChild = true;
129 MenuItem* item = _getClientWidget()->createWidget<MenuItem>(getSkinByType(_type), IntCoord(), Align::Default);
130 mInternalCreateChild = false;
131 _wrapItem(item, _index, _name, _type, _id, _data);
132
133 return item;
134 }
135
136 void MenuControl::removeItemAt(size_t _index)
137 {
138 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::removeItemAt");
139
140 if (mItemsInfo[_index].submenu)
141 {
142 WidgetManager::getInstance().destroyWidget(mItemsInfo[_index].submenu);
143 mItemsInfo[_index].submenu = nullptr;
144 }
145 WidgetManager::getInstance().destroyWidget(mItemsInfo[_index].item);
146 }
147
148 void MenuControl::removeAllItems()
149 {
150 while (!mItemsInfo.empty())
151 {
152 if (mItemsInfo.back().submenu)
153 {
154 WidgetManager::getInstance().destroyWidget(mItemsInfo.back().submenu);
155 mItemsInfo.back().submenu = nullptr;
156 }
157 WidgetManager::getInstance().destroyWidget(mItemsInfo.back().item);
158 }
159 }
160
161 const UString& MenuControl::getItemNameAt(size_t _index)
162 {
163 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::getItemNameAt");
164 return mItemsInfo[_index].name;
165 }
166
167 void MenuControl::update()
168 {
169 IntSize size;
170
171 if (mVerticalAlignment)
172 {
173 for (VectorMenuItemInfo::iterator iter = mItemsInfo.begin(); iter != mItemsInfo.end(); ++iter)
174 {
175 IntSize contentSize = iter->item->_getContentSize();
176 iter->item->setCoord(0, size.height, _getClientWidget()->getWidth(), contentSize.height);
177 size.height += contentSize.height + mDistanceButton;
178
179 if (contentSize.width > size.width)
180 size.width = contentSize.width;
181 }
182 if (!mItemsInfo.empty())
183 size.height -= mDistanceButton;
184 }
185 else
186 {
187 int maxHeight = 0;
188 for (VectorMenuItemInfo::iterator iter = mItemsInfo.begin(); iter != mItemsInfo.end(); ++iter)
189 {
190 IntSize contentSize = iter->item->_getContentSize();
191 if (maxHeight < contentSize.height)
192 maxHeight = contentSize.height;
193 }
194
195 for (VectorMenuItemInfo::iterator iter = mItemsInfo.begin(); iter != mItemsInfo.end(); ++iter)
196 {
197 IntSize contentSize = iter->item->_getContentSize();
198 iter->item->setCoord(size.width, 0, contentSize.width, maxHeight);
199 size.width += contentSize.width + mDistanceButton;
200 }
201
202 if (!mItemsInfo.empty())
203 size.width -= mDistanceButton;
204 }
205
206 if (mResizeToContent)
207 setSize(size + mCoord.size() - _getClientWidget()->getSize());
208 }
209
210 void MenuControl::setItemDataAt(size_t _index, Any _data)
211 {
212 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::setItemDataAt");
213 mItemsInfo[_index].data = _data;
214 }
215
216 MenuControl* MenuControl::getItemChildAt(size_t _index)
217 {
218 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::getItemChildAt");
219 return mItemsInfo[_index].submenu;
220 }
221
222 void MenuControl::removeItemChildAt(size_t _index)
223 {
224 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::removeItemChildAt");
225
226 if (mItemsInfo[_index].submenu != nullptr)
227 {
228 WidgetManager::getInstance().destroyWidget(mItemsInfo[_index].submenu);
229 mItemsInfo[_index].submenu = nullptr;
230 }
231
232 update();
233 }
234
235 void MenuControl::setItemNameAt(size_t _index, const UString& _name)
236 {
237 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::setItemNameAt");
238
239 mItemsInfo[_index].name = _name;
240 MenuItem* item = mItemsInfo[_index].item;
241 item->setCaption(_name);
242
243 update();
244 }
245
246 void MenuControl::setItemIdAt(size_t _index, const std::string& _id)
247 {
248 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::setItemIdAt");
249 mItemsInfo[_index].id = _id;
250 }
251
252 const std::string& MenuControl::getItemIdAt(size_t _index)
253 {
254 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::getItemIdAt");
255 return mItemsInfo[_index].id;
256 }
257
258 void MenuControl::_notifyDeleteItem(MenuItem* _item)
259 {
260 // дитю меняем скин
261 if (mChangeChildSkin)
262 return;
263
264 // общий шутдаун виджета
265 if (mShutdown)
266 return;
267
268 size_t index = getItemIndex(_item);
269 mItemsInfo.erase(mItemsInfo.begin() + index);
270 update();
271 }
272
273 void MenuControl::_notifyDeletePopup(MenuItem* _item)
274 {
275 size_t index = getItemIndex(_item);
276 mItemsInfo[index].submenu = nullptr;
277 }
278
279 void MenuControl::_notifyUpdateName(MenuItem* _item)
280 {
281 size_t index = getItemIndex(_item);
282 mItemsInfo[index].name = _item->getCaption();
283
284 ISubWidgetText* text = _item->getSubWidgetText();
285 mItemsInfo[index].width = text ? (text->getTextSize().width + _item->getSize().width - text->getWidth()) : 0;
286 update();
287 }
288
289 MenuItemType MenuControl::getItemTypeAt(size_t _index)
290 {
291 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::getItemTypeAt");
292 return mItemsInfo[_index].type;
293 }
294
295 void MenuControl::setItemTypeAt(size_t _index, MenuItemType _type)
296 {
297 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::setItemTypeAt");
298 ItemInfo& info = mItemsInfo[_index];
299 if (info.type == _type)
300 return;
301
302 // сохраняем данные
303 info.type = _type;
304
305 // при смене скина дите отпишется
306 mChangeChildSkin = true;
307 info.item->changeWidgetSkin(getSkinByType(_type));
308 mChangeChildSkin = false;
309
310 info.item->setImageName(getIconIndexByType(_type ));
311 info.item->setCaption(info.name);
312
313 update();
314 }
315
316 void MenuControl::notifyMenuCtrlAccept(MenuItem* _item)
317 {
318 if (mHideByAccept)
319 {
320 setVisibleSmooth(false);
321 }
322 else
323 {
324 InputManager::getInstance().setKeyFocusWidget(nullptr);
325 }
326
327 MenuItem* parent_item = getMenuItemParent();
328 if (parent_item)
329 {
330 MenuControl* parent_ctrl = parent_item->getMenuCtrlParent();
331 if (parent_ctrl)
332 {
333 parent_ctrl->notifyMenuCtrlAccept(_item);
334 }
335 }
336
337 eventMenuCtrlAccept(this, _item);
338 }
339
340 void MenuControl::setItemChildVisibleAt(size_t _index, bool _visible)
341 {
342 _setItemChildVisibleAt(_index, _visible, true);
343 }
344
345 void MenuControl::_setItemChildVisibleAt(size_t _index, bool _visible, bool _smooth)
346 {
347 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::setItemChildVisibleAt");
348
349 if (_visible)
350 {
351 if (mItemsInfo[_index].submenu && mItemsInfo[_index].submenu->getItemCount())
352 {
353 int offset = mItemsInfo[0].item->getAbsoluteTop() - getAbsoluteTop();
354
355 const IntCoord& coord = mItemsInfo[_index].item->getAbsoluteCoord();
356 IntPoint point(getAbsoluteRect().right, coord.top - offset);
357
358 MenuControl* menu = mItemsInfo[_index].submenu;
359
360 if (mVerticalAlignment)
361 {
362 // too wide
363 if (point.left + menu->getWidth() > menu->getParentSize().width)
364 {
365 // move to the left side if possible
366 if (point.left - menu->getWidth() - getWidth() > 0)
367 point.left -= menu->getWidth() + getWidth();
368 // or put near right parent border (window) if too wide for left side too
369 else
370 point.left = menu->getParentSize().width - menu->getWidth();
371 }
372 // too high (same logic as for too wide)
373 if (point.top + menu->getHeight() > menu->getParentSize().height)
374 {
375 // move to the top side if possible
376 if (point.top - menu->getHeight() - getHeight() > 0)
377 point.top -= menu->getHeight() + getHeight();
378 // or put near bottom parent border (window) if too high for top side too
379 else
380 point.top = menu->getParentSize().height - menu->getHeight();
381 }
382 }
383 else
384 {
385 point.set(coord.left, getAbsoluteRect().bottom);
386 }
387
388 menu->setPosition(point);
389 if (_smooth)
390 menu->setVisibleSmooth(true);
391 else
392 menu->setVisible(true);
393
394 MyGUI::LayerManager::getInstance().upLayerItem(menu);
395 }
396 }
397 else
398 {
399 if (mItemsInfo[_index].submenu)
400 {
401 if (_smooth)
402 mItemsInfo[_index].submenu->setVisibleSmooth(false);
403 else
404 mItemsInfo[_index].submenu->setVisible(false);
405 }
406 }
407 }
408
409 void MenuControl::notifyRootKeyChangeFocus(Widget* _sender, bool _focus)
410 {
411 MenuItem* item = _sender->castType<MenuItem>();
412 if (item->getItemType() == MenuItemType::Popup)
413 {
414 if (_focus)
415 {
416 if (!mMenuDropMode || mIsMenuDrop)
417 {
418 item->setItemChildVisible(true);
419 item->setStateSelected(true);
420 }
421 }
422 else
423 {
424 item->setItemChildVisible(false);
425 item->setStateSelected(false);
426 }
427 }
428 }
429
430 Widget* MenuControl::createItemChildByType(size_t _index, const std::string& _type)
431 {
432 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::createItemChildByType");
433 removeItemChildAt(_index);
434 Widget* child = mItemsInfo[_index].item->createWidgetT(WidgetStyle::Popup, _type, mSubMenuSkin, IntCoord(), Align::Default, mSubMenuLayer);
435 MYGUI_ASSERT(child->isType<MenuControl>(), "child must have MenuControl base type");
436 return child;
437 }
438
439 void MenuControl::notifyMouseButtonClick(Widget* _sender)
440 {
441 MenuItem* item = _sender->castType<MenuItem>();
442 if (mMenuDropMode)
443 {
444 if (mIsMenuDrop)
445 {
446 if (item->getItemType() == MenuItemType::Popup)
447 {
448 item->setStateSelected(false);
449 item->setItemChildVisible(false);
450 mIsMenuDrop = false;
451 }
452 }
453 else
454 {
455 if (item->getItemType() == MenuItemType::Popup)
456 {
457 mIsMenuDrop = true;
458 item->setStateSelected(true);
459 item->setItemChildVisible(true);
460 InputManager::getInstance().setKeyFocusWidget(item);
461 }
462 }
463 }
464 else
465 {
466 if ((item->getItemType() == MenuItemType::Popup && mPopupAccept) ||
467 item->getItemType() == MenuItemType::Normal)
468 {
469 notifyMenuCtrlAccept(item);
470 }
471 }
472 }
473
474 void MenuControl::onKeyChangeRootFocus(bool _focus)
475 {
476 if (mMenuDropMode)
477 {
478 mIsMenuDrop = false;
479 }
480 if (!_focus && mHideByLostKey)
481 {
482 setVisibleSmooth(false);
483 eventMenuCtrlClose(this);
484 }
485 Base::onKeyChangeRootFocus(_focus);
486 }
487
488 void MenuControl::notifyMouseSetFocus(Widget* _sender, Widget* _new)
489 {
490 InputManager::getInstance().setKeyFocusWidget(_sender);
491 }
492
493 void MenuControl::_wrapItemChild(MenuItem* _item, MenuControl* _widget)
494 {
495 // заменяем
496 size_t index = getItemIndex(_item);
497 if (mItemsInfo[index].submenu != nullptr)
498 {
499 WidgetManager::getInstance().destroyWidget(mItemsInfo[index].submenu);
500 mItemsInfo[index].submenu = nullptr;
501 }
502 mItemsInfo[index].submenu = _widget;
503 // скрываем менюшку
504 mItemsInfo[index].submenu->setVisible(false);
505
506 update();
507 }
508
509 void MenuControl::_wrapItem(MenuItem* _item, size_t _index, const UString& _name, MenuItemType _type, const std::string& _id, Any _data)
510 {
511 _item->setAlign(mVerticalAlignment ? Align::Top | Align::HStretch : Align::Default);
512 _item->eventRootKeyChangeFocus += newDelegate(this, &MenuControl::notifyRootKeyChangeFocus);
513 _item->eventMouseButtonClick += newDelegate(this, &MenuControl::notifyMouseButtonClick);
514 _item->eventMouseSetFocus += newDelegate(this, &MenuControl::notifyMouseSetFocus);
515
516 _item->setImageName(getIconIndexByType(_type ));
517
518 MenuControl* submenu = nullptr;
519
520 ItemInfo info = ItemInfo(_item, _name, _type, submenu, _id, _data);
521
522 mItemsInfo.insert(mItemsInfo.begin() + _index, info);
523
524 mChangeChildSkin = true;
525 _item->changeWidgetSkin(getSkinByType(_type));
526 mChangeChildSkin = false;
527
528 // его сет капшен, обновит размер
529 _item->setCaption(_name);
530
531 update();
532 }
533
534 void MenuControl::setVisible(bool _visible)
535 {
536 if (mAnimateSmooth)
537 {
538 ControllerManager::getInstance().removeItem(this);
539 setAlpha(ALPHA_MAX);
540 setEnabledSilent(true);
541 mAnimateSmooth = false;
542 }
543
544 if (_visible)
545 {
546 if (mOwner == nullptr && mHideByLostKey)
547 {
548 MyGUI::InputManager::getInstance().setKeyFocusWidget(this);
549 }
550 }
551
552 Base::setVisible(_visible);
553 }
554
555 void MenuControl::setVisibleSmooth(bool _visible)
556 {
557 mAnimateSmooth = true;
558 ControllerManager::getInstance().removeItem(this);
559
560 if (_visible)
561 {
562 setEnabledSilent(true);
563 if (!getVisible())
564 {
565 setAlpha(ALPHA_MIN);
566 Base::setVisible(true);
567 }
568
569 ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MAX, POPUP_MENU_SPEED_COEF, true);
570 controller->eventPostAction += newDelegate(action::actionWidgetShow);
571 ControllerManager::getInstance().addItem(this, controller);
572 }
573 else
574 {
575 setEnabledSilent(false);
576
577 ControllerFadeAlpha* controller = createControllerFadeAlpha(ALPHA_MIN, POPUP_MENU_SPEED_COEF, false);
578 controller->eventPostAction += newDelegate(action::actionWidgetHide);
579 ControllerManager::getInstance().addItem(this, controller);
580 }
581 }
582
583 ControllerFadeAlpha* MenuControl::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
584 {
585 ControllerItem* item = ControllerManager::getInstance().createItem(ControllerFadeAlpha::getClassTypeName());
586 ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
587
588 controller->setAlpha(_alpha);
589 controller->setCoef(_coef);
590 controller->setEnabled(_enable);
591
592 return controller;
593 }
594
595 MenuItem* MenuControl::insertItem(MenuItem* _to, const UString& _name, MenuItemType _type, const std::string& _id, Any _data)
596 {
597 return insertItemAt(getItemIndex(_to), _name, _type, _id, _data);
598 }
599
600 MenuItem* MenuControl::addItem(const UString& _name, MenuItemType _type, const std::string& _id, Any _data)
601 {
602 return insertItemAt(ITEM_NONE, _name, _type, _id, _data);
603 }
604
605 void MenuControl::removeItem(MenuItem* _item)
606 {
607 removeItemAt(getItemIndex(_item));
608 }
609
610 MenuItem* MenuControl::getItemAt(size_t _index)
611 {
612 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuControl::getItemAt");
613 return mItemsInfo[_index].item;
614 }
615
616 size_t MenuControl::getItemIndex(MenuItem* _item)
617 {
618 for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
619 {
620 if (mItemsInfo[pos].item == _item)
621 return pos;
622 }
623 MYGUI_EXCEPT("item (" << _item << ") not found, source 'MenuControl::getItemIndex'");
624 }
625
626 MenuItem* MenuControl::findItemWith(const UString& _name)
627 {
628 for (size_t pos = 0; pos < mItemsInfo.size(); pos++)
629 {
630 if (mItemsInfo[pos].name == _name)
631 return mItemsInfo[pos].item;
632 }
633 return nullptr;
634 }
635
636 MenuItem* MenuControl::getItemById(const std::string& _id)
637 {
638 for (size_t index = 0; index < mItemsInfo.size(); index++)
639 {
640 if (mItemsInfo[index].id == _id)
641 return mItemsInfo[index].item;
642 }
643 MYGUI_EXCEPT("item id (" << _id << ") not found, source 'MenuControl::getItemById'");
644 }
645
646 size_t MenuControl::getItemIndexById(const std::string& _id)
647 {
648 for (size_t index = 0; index < mItemsInfo.size(); index++)
649 {
650 if (mItemsInfo[index].id == _id)
651 return index;
652 }
653 MYGUI_EXCEPT("item id (" << _id << ") not found, source 'MenuControl::getItemById'");
654 }
655
656 MenuItem* MenuControl::findItemById(const std::string& _id, bool _recursive)
657 {
658 for (size_t index = 0; index < mItemsInfo.size(); index++)
659 {
660 if (mItemsInfo[index].id == _id)
661 return mItemsInfo[index].item;
662
663 if (_recursive && mItemsInfo[index].submenu != nullptr)
664 {
665 MenuItem* find = mItemsInfo[index].submenu->findItemById(_id, _recursive);
666 if (find != nullptr)
667 return find;
668 }
669 }
670 return nullptr;
671 }
672
673 size_t MenuControl::findItemIndexWith(const UString& _name)
674 {
675 for (size_t index = 0; index < mItemsInfo.size(); index++)
676 {
677 if (mItemsInfo[index].name == _name)
678 return index;
679 }
680 return ITEM_NONE;
681 }
682
683 size_t MenuControl::findItemIndex(MenuItem* _item)
684 {
685 for (size_t index = 0; index < mItemsInfo.size(); index++)
686 {
687 if (mItemsInfo[index].item == _item)
688 return index;
689 }
690 return ITEM_NONE;
691 }
692
693 Widget* MenuControl::_getClientWidget()
694 {
695 return mClient == nullptr ? this : mClient;
696 }
697
698 size_t MenuControl::getItemCount() const
699 {
700 return mItemsInfo.size();
701 }
702
703 void MenuControl::setItemData(MenuItem* _item, Any _data)
704 {
705 setItemDataAt(getItemIndex(_item), _data);
706 }
707
708 void MenuControl::clearItemDataAt(size_t _index)
709 {
710 setItemDataAt(_index, Any::Null);
711 }
712
713 void MenuControl::clearItemData(MenuItem* _item)
714 {
715 clearItemDataAt(getItemIndex(_item));
716 }
717
718 void MenuControl::setItemId(MenuItem* _item, const std::string& _id)
719 {
720 setItemIdAt(getItemIndex(_item), _id);
721 }
722
723 const std::string& MenuControl::getItemId(MenuItem* _item)
724 {
725 return getItemIdAt(getItemIndex(_item));
726 }
727
728 void MenuControl::setItemName(MenuItem* _item, const UString& _name)
729 {
730 setItemNameAt(getItemIndex(_item), _name);
731 }
732
733 const UString& MenuControl::getItemName(MenuItem* _item)
734 {
735 return getItemNameAt(getItemIndex(_item));
736 }
737
738 void MenuControl::setItemChildVisible(MenuItem* _item, bool _visible)
739 {
740 setItemChildVisibleAt(getItemIndex(_item), _visible);
741 }
742
743 MenuControl* MenuControl::getItemChild(MenuItem* _item)
744 {
745 return getItemChildAt(getItemIndex(_item));
746 }
747
748 MenuControl* MenuControl::createItemChildAt(size_t _index)
749 {
750 return createItemChildTAt<MenuControl>(_index);
751 }
752
753 MenuControl* MenuControl::createItemChild(MenuItem* _item)
754 {
755 return createItemChildAt(getItemIndex(_item));
756 }
757
758 void MenuControl::removeItemChild(MenuItem* _item)
759 {
760 removeItemChildAt(getItemIndex(_item));
761 }
762
763 MenuItemType MenuControl::getItemType(MenuItem* _item)
764 {
765 return getItemTypeAt(getItemIndex(_item));
766 }
767
768 void MenuControl::setItemType(MenuItem* _item, MenuItemType _type)
769 {
770 setItemTypeAt(getItemIndex(_item), _type);
771 }
772
773 void MenuControl::setPopupAccept(bool _value)
774 {
775 mPopupAccept = _value;
776 }
777
778 bool MenuControl::getPopupAccept() const
779 {
780 return mPopupAccept;
781 }
782
783 MenuItem* MenuControl::getMenuItemParent()
784 {
785 return mOwner;
786 }
787
788 const std::string& MenuControl::getSkinByType(MenuItemType _type) const
789 {
790 if (_type == MenuItemType::Popup)
791 return mItemPopupSkin;
792 else if (_type == MenuItemType::Separator)
793 return mItemSeparatorSkin;
794 return mItemNormalSkin;
795 }
796
797 std::string MenuControl::getIconIndexByType(MenuItemType _type) const
798 {
799 if (_type == MenuItemType::Popup)
800 return "Popup";
801 return "None";
802 }
803
804 MenuItemType MenuControl::getItemType(bool _submenu, bool _separator) const
805 {
806 if (_submenu)
807 return MenuItemType::Popup;
808 else if (_separator)
809 return MenuItemType::Separator;
810 return MenuItemType::Normal;
811 }
812
813 size_t MenuControl::_getItemCount()
814 {
815 return getItemCount();
816 }
817
818 void MenuControl::_addItem(const MyGUI::UString& _name)
819 {
820 addItem(_name, MenuItemType::Normal);
821 }
822
823 void MenuControl::_removeItemAt(size_t _index)
824 {
825 removeItemAt(_index);
826
827 _updateSizeForEmpty();
828 }
829
830 Widget* MenuControl::_getItemAt(size_t _index)
831 {
832 return getItemAt(_index);
833 }
834
835 void MenuControl::_setItemNameAt(size_t _index, const UString& _name)
836 {
837 setItemNameAt(_index, _name);
838 }
839
840 const UString& MenuControl::_getItemNameAt(size_t _index)
841 {
842 return getItemNameAt(_index);
843 }
844
845 void MenuControl::_setItemSelected(IItem* _item)
846 {
847 MenuItem* item = static_cast<MenuItem*>(_item);
848 for (VectorMenuItemInfo::iterator iter = mItemsInfo.begin(); iter != mItemsInfo.end(); ++iter)
849 {
850 if ((*iter).type == MenuItemType::Popup)
851 {
852 (*iter).item->setStateSelected(false);
853
854 if ((*iter).submenu != nullptr)
855 (*iter).submenu->setVisible(false);
856 }
857 }
858
859 if (item->getItemType() == MenuItemType::Popup)
860 {
861 item->setStateSelected(true);
862 size_t index = getItemIndex(item);
863
864 _setItemChildVisibleAt(index, true, false);
865
866 _updateItems(index);
867 }
868 }
869
870 void MenuControl::_updateItems(size_t _index)
871 {
872 if (mItemsInfo[_index].submenu != nullptr)
873 mItemsInfo[_index].submenu->_updateSizeForEmpty();
874 }
875
876 void MenuControl::_updateSizeForEmpty()
877 {
878 if (mItemsInfo.empty())
879 setSize(100, 100);
880 }
881
882 void MenuControl::setVerticalAlignment(bool _value)
883 {
884 mVerticalAlignment = _value;
885
886 update();
887 }
888
889 bool MenuControl::getVerticalAlignment() const
890 {
891 return mVerticalAlignment;
892 }
893
894 void MenuControl::setPropertyOverride(const std::string& _key, const std::string& _value)
895 {
897 if (_key == "VerticalAlignment")
898 setVerticalAlignment(utility::parseValue<bool>(_value));
899
900 else
901 {
902 Base::setPropertyOverride(_key, _value);
903 return;
904 }
905
906 eventChangeProperty(this, _key, _value);
907 }
908
909 } // namespace MyGUI
MyGUI::ControllerManager::addItem
void addItem(Widget *_widget, ControllerItem *_item)
Definition: MyGUI_ControllerManager.cpp:78
MyGUI::Widget::getParentSize
IntSize getParentSize() const
Definition: MyGUI_Widget.cpp:1025
MyGUI::types::TCoord< int >
MyGUI::ALPHA_MIN
const float ALPHA_MIN
Definition: MyGUI_Macros.h:20
MyGUI::Widget
Widget properties. Widget_skin_childs Skin childs. Widget widget description should be here...
Definition: MyGUI_Widget.h:29
MyGUI::types::TPoint< int >
MyGUI::ICroppedRectangle::getSize
IntSize getSize() const
Definition: MyGUI_ICroppedRectangle.h:56
MyGUI_LayerManager.h
MyGUI_Precompiled.h
MyGUI::WidgetInput::eventRootKeyChangeFocus
EventHandle_WidgetBool eventRootKeyChangeFocus
Definition: MyGUI_WidgetInput.h:220
MyGUI::WidgetInput::eventMouseButtonClick
EventHandle_WidgetVoid eventMouseButtonClick
Definition: MyGUI_WidgetInput.h:169
MyGUI::Widget::getClientWidget
Widget * getClientWidget()
Definition: MyGUI_Widget.cpp:1290
MyGUI::types::TSize< int >
MyGUI::newDelegate
delegates::IDelegate0 * newDelegate(void(*_func)())
Definition: MyGUI_Delegate.h:118
MyGUI::Singleton< WidgetManager >::getInstance
static WidgetManager & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI_WidgetManager.h
MyGUI::action::actionWidgetHide
void actionWidgetHide(Widget *_widget, ControllerItem *_controller)
Definition: MyGUI_ActionController.cpp:18
MyGUI::Widget::isType
virtual bool isType(const std::type_info &_type) const
Definition: MyGUI_Widget.h:41
MyGUI::Widget::assignWidget
void assignWidget(T *&_widget, const std::string &_name)
Definition: MyGUI_Widget.h:328
MyGUI::UserData::getUserString
const std::string & getUserString(const std::string &_key) const
Definition: MyGUI_WidgetUserData.cpp:28
MyGUI::WidgetManager::destroyWidget
void destroyWidget(Widget *_widget)
Definition: MyGUI_WidgetManager.cpp:118
MyGUI::Any
Definition: MyGUI_Any.h:63
MyGUI::ITEM_NONE
const size_t ITEM_NONE
Definition: MyGUI_Macros.h:17
MyGUI::Align::HStretch
Definition: MyGUI_Align.h:29
MyGUI::ICroppedRectangle::getAbsoluteTop
int getAbsoluteTop() const
Definition: MyGUI_ICroppedRectangle.h:88
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI::types::TPoint::top
T top
Definition: MyGUI_TPoint.h:21
MyGUI::Widget::setSize
virtual void setSize(const IntSize &_value)
Definition: MyGUI_Widget.cpp:652
MyGUI::SkinItem::getSubWidgetText
ISubWidgetText * getSubWidgetText()
Definition: MyGUI_SkinItem.cpp:155
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:35
MyGUI::ICroppedRectangle::mCoord
IntCoord mCoord
Definition: MyGUI_ICroppedRectangle.h:245
MyGUI::Widget::getVisible
bool getVisible() const
Definition: MyGUI_Widget.cpp:1250
MyGUI::Align::Default
Definition: MyGUI_Align.h:36
MyGUI::Align::Top
Definition: MyGUI_Align.h:31
MyGUI::types::TCoord::top
T top
Definition: MyGUI_TCoord.h:23
MyGUI::TextBox::getCaption
virtual const UString & getCaption()
Definition: MyGUI_TextBox.cpp:83
MyGUI::ICroppedRectangle::getWidth
int getWidth() const
Definition: MyGUI_ICroppedRectangle.h:114
MyGUI::ControllerItem::eventPostAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventPostAction
Definition: MyGUI_ControllerItem.h:54
MyGUI_RenderManager.h
MyGUI::Widget::setPosition
virtual void setPosition(const IntPoint &_value)
Definition: MyGUI_Widget.cpp:635
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::InputManager::setKeyFocusWidget
void setKeyFocusWidget(Widget *_widget)
Definition: MyGUI_InputManager.cpp:382
MyGUI::ALPHA_MAX
const float ALPHA_MAX
Definition: MyGUI_Macros.h:19
MYGUI_EXCEPT
#define MYGUI_EXCEPT(dest)
Definition: MyGUI_Diagnostic.h:33
MyGUI::ControllerFadeAlpha::getClassTypeName
static const std::string & getClassTypeName()
Definition: MyGUI_ControllerFadeAlpha.h:21
MyGUI::Widget::eventChangeProperty
EventHandle_WidgetStringString eventChangeProperty
Definition: MyGUI_Widget.h:266
MyGUI::types::TPoint::left
T left
Definition: MyGUI_TPoint.h:20
MyGUI::Widget::getParent
Widget * getParent() const
Definition: MyGUI_Widget.cpp:1275
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:20
MyGUI::ControllerFadeAlpha::setAlpha
void setAlpha(float _value)
Definition: MyGUI_ControllerFadeAlpha.cpp:96
MyGUI::ControllerFadeAlpha::setCoef
void setCoef(float _value)
Definition: MyGUI_ControllerFadeAlpha.cpp:101
MyGUI_ControllerManager.h
MyGUI::UserData::isUserString
bool isUserString(const std::string &_key) const
Definition: MyGUI_WidgetUserData.cpp:52
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:42
MyGUI::ControllerFadeAlpha::setEnabled
void setEnabled(bool _value)
Definition: MyGUI_ControllerFadeAlpha.cpp:106
MyGUI::Widget::setWidgetClient
void setWidgetClient(Widget *_widget)
Definition: MyGUI_Widget.cpp:1134
MyGUI::Widget::createWidget
T * createWidget(const std::string &_skin, const IntCoord &_coord, Align _align, const std::string &_name="")
Definition: MyGUI_Widget.h:67
MyGUI_InputManager.h
MyGUI_ResourceSkin.h
MyGUI::Widget::setEnabledSilent
void setEnabledSilent(bool _value)
Definition: MyGUI_Widget.cpp:1280
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition: MyGUI_IObject.h:18
MyGUI::Button::setImageName
void setImageName(const std::string &_name)
Definition: MyGUI_Button.cpp:169
MyGUI::ControllerManager::removeItem
void removeItem(Widget *_widget)
Definition: MyGUI_ControllerManager.cpp:105
MyGUI::Widget::setAlign
virtual void setAlign(Align _value)
Definition: MyGUI_Widget.cpp:734
MYGUI_ASSERT_RANGE
#define MYGUI_ASSERT_RANGE(index, size, owner)
Definition: MyGUI_Diagnostic.h:54
MyGUI::action::actionWidgetShow
void actionWidgetShow(Widget *_widget, ControllerItem *_controller)
Definition: MyGUI_ActionController.cpp:23
MyGUI::ControllerFadeAlpha
Definition: MyGUI_ControllerFadeAlpha.h:18
MyGUI_ImageBox.h
MyGUI::WidgetInput::eventMouseSetFocus
EventHandle_WidgetWidget eventMouseSetFocus
Definition: MyGUI_WidgetInput.h:122
MyGUI::UString
A UTF-16 string with implicit conversion to/from std::string and std::wstring.
Definition: MyGUI_UString.h:168
MyGUI::ISubWidgetText
Definition: MyGUI_ISubWidgetText.h:18
MyGUI::ControllerItem
Definition: MyGUI_ControllerItem.h:25
MyGUI_Gui.h
MyGUI::types::TCoord::size
TSize< T > size() const
Definition: MyGUI_TCoord.h:190
MyGUI::POPUP_MENU_SPEED_COEF
const float POPUP_MENU_SPEED_COEF
Definition: MyGUI_MenuControl.cpp:23
MyGUI::WidgetStyle::Popup
Definition: MyGUI_WidgetStyle.h:21
MyGUI::IItem
Definition: MyGUI_IItem.h:17
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:21
MyGUI::Widget::changeWidgetSkin
void changeWidgetSkin(const std::string &_skinName)
Definition: MyGUI_Widget.cpp:154
MyGUI::WidgetInput::setNeedKeyFocus
void setNeedKeyFocus(bool _value)
Definition: MyGUI_WidgetInput.cpp:156
MyGUI::types::TCoord::left
T left
Definition: MyGUI_TCoord.h:22
MyGUI::ICroppedRectangle::getAbsoluteRect
IntRect getAbsoluteRect() const
Definition: MyGUI_ICroppedRectangle.h:72
MyGUI::Widget::setAlpha
void setAlpha(float _value)
Definition: MyGUI_Widget.cpp:435
MyGUI::ISubWidgetText::getTextSize
virtual IntSize getTextSize()
Definition: MyGUI_ISubWidgetText.h:111
MYGUI_ASSERT_RANGE_INSERT
#define MYGUI_ASSERT_RANGE_INSERT(index, size, owner)
Definition: MyGUI_Diagnostic.h:56
MyGUI::Any::Null
static AnyEmpty Null
Definition: MyGUI_Any.h:67
MyGUI::types::TPoint::set
void set(T const &_left, T const &_top)
Definition: MyGUI_TPoint.h:95
MyGUI::ICroppedRectangle::getHeight
int getHeight() const
Definition: MyGUI_ICroppedRectangle.h:119
MyGUI::LayerManager::upLayerItem
void upLayerItem(Widget *_item)
Definition: MyGUI_LayerManager.cpp:148
MyGUI::ControllerManager::createItem
ControllerItem * createItem(const std::string &_type)
Definition: MyGUI_ControllerManager.cpp:72
MyGUI::Button::setStateSelected
void setStateSelected(bool _value)
Set button selected state.
Definition: MyGUI_Button.cpp:111