MyGUI
3.0.1
|
00001 00007 /* 00008 This file is part of MyGUI. 00009 00010 MyGUI is free software: you can redistribute it and/or modify 00011 it under the terms of the GNU Lesser General Public License as published by 00012 the Free Software Foundation, either version 3 of the License, or 00013 (at your option) any later version. 00014 00015 MyGUI is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU Lesser General Public License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public License 00021 along with MyGUI. If not, see <http://www.gnu.org/licenses/>. 00022 */ 00023 #ifndef __MYGUI_MENU_CTRL_H__ 00024 #define __MYGUI_MENU_CTRL_H__ 00025 00026 #include "MyGUI_Prerequest.h" 00027 #include "MyGUI_Types.h" 00028 #include "MyGUI_MenuItemType.h" 00029 #include "MyGUI_Widget.h" 00030 #include "MyGUI_Any.h" 00031 #include "MyGUI_EventPair.h" 00032 #include "MyGUI_MenuItemType.h" 00033 #include "MyGUI_ControllerFadeAlpha.h" 00034 00035 namespace MyGUI 00036 { 00037 00038 typedef delegates::CDelegate2<MenuCtrl*, MenuItem*> EventHandle_MenuCtrlPtrMenuItemPtr; 00039 typedef delegates::CDelegate1<MenuCtrl*> EventHandle_MenuCtrlPtr; 00040 00041 class MYGUI_EXPORT MenuCtrl : 00042 public Widget 00043 { 00044 MYGUI_RTTI_DERIVED( MenuCtrl ) 00045 00046 public: 00047 MenuCtrl(); 00048 00049 enum ItemImage 00050 { 00051 ItemImageNone, 00052 ItemImagePopup 00053 }; 00054 00055 struct ItemInfo 00056 { 00057 ItemInfo(MenuItem* _item, const UString& _name, MenuItemType _type, MenuCtrl* _submenu, const std::string& _id, Any _data) : 00058 item(_item), 00059 name(_name), 00060 type(_type), 00061 submenu(_submenu), 00062 id(_id), 00063 data(_data), 00064 width(0) 00065 { 00066 } 00067 00069 MenuItem* item; 00071 UString name; 00073 MenuItemType type; 00075 MenuCtrl* submenu; 00077 std::string id; 00079 Any data; 00081 int width; 00082 }; 00083 00084 typedef std::vector<ItemInfo> VectorMenuItemInfo; 00085 00086 public: 00088 virtual void setVisible(bool _value); 00089 00091 void setVisibleSmooth(bool _value); 00092 00093 //------------------------------------------------------------------------------// 00094 // манипуляции айтемами 00095 00097 size_t getItemCount() const { return mItemsInfo.size(); } 00098 00100 MenuItem* insertItemAt(size_t _index, const UString& _name, MenuItemType _type = MenuItemType::Normal, const std::string& _id = "", Any _data = Any::Null); 00102 MenuItem* insertItem(MenuItem* _to, const UString& _name, MenuItemType _type = MenuItemType::Normal, const std::string& _id = "", Any _data = Any::Null); 00103 00105 MenuItem* addItem(const UString& _name, MenuItemType _type = MenuItemType::Normal, const std::string& _id = "", Any _data = Any::Null); 00106 00108 void removeItemAt(size_t _index); 00110 void removeItem(MenuItem* _item); 00111 00113 void removeAllItems(); 00114 00115 00117 MenuItem* getItemAt(size_t _index); 00118 00120 size_t getItemIndex(MenuItem* _item); 00121 00123 size_t findItemIndex(MenuItem* _item); 00124 00126 MenuItem* findItemWith(const UString& _name); 00127 00128 //------------------------------------------------------------------------------// 00129 // манипуляции данными 00130 00132 void setItemDataAt(size_t _index, Any _data); 00134 void setItemData(MenuItem* _item, Any _data) { setItemDataAt(getItemIndex(_item), _data); } 00135 00137 void clearItemDataAt(size_t _index) { setItemDataAt(_index, Any::Null); } 00139 void clearItemData(MenuItem* _item) { clearItemDataAt(getItemIndex(_item)); } 00140 00142 template <typename ValueType> 00143 ValueType * getItemDataAt(size_t _index, bool _throw = true) 00144 { 00145 MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuCtrl::getItemDataAt"); 00146 return mItemsInfo[_index].data.castType<ValueType>(_throw); 00147 } 00149 template <typename ValueType> 00150 ValueType * getItemData(MenuItem* _item, bool _throw = true) 00151 { 00152 return getItemDataAt<ValueType>(getItemIndex(_item), _throw); 00153 } 00154 00156 void setItemIdAt(size_t _index, const std::string& _id); 00158 void setItemId(MenuItem* _item, const std::string& _id) { setItemIdAt(getItemIndex(_item), _id); } 00159 00161 const std::string& getItemIdAt(size_t _index); 00163 const std::string& getItemId(MenuItem* _item) { return getItemIdAt(getItemIndex(_item)); } 00164 00166 MenuItem* getItemById(const std::string& _id); 00167 00169 size_t getItemIndexById(const std::string& _id); 00170 //------------------------------------------------------------------------------// 00171 // манипуляции отображением 00172 00174 void setItemNameAt(size_t _index, const UString& _name); 00176 void setItemName(MenuItem* _item, const UString& _name) { setItemNameAt(getItemIndex(_item), _name); } 00177 00179 const UString& getItemNameAt(size_t _index); 00181 const UString& getItemName(MenuItem* _item) { return getItemNameAt(getItemIndex(_item)); } 00182 00184 size_t findItemIndexWith(const UString& _name); 00185 00187 void setItemChildVisibleAt(size_t _index, bool _visible); 00189 void setItemChildVisible(MenuItem* _item, bool _visible) { setItemChildVisibleAt(getItemIndex(_item), _visible); } 00190 00191 //------------------------------------------------------------------------------// 00192 // остальные манипуляции 00193 00195 template <typename Type> 00196 Type * createItemChildTAt(size_t _index) 00197 { 00198 return static_cast<Type*>(createItemChildByType(_index, Type::getClassTypeName())); 00199 } 00200 00202 template <typename Type> 00203 Type * createItemChildT(MenuItem* _item) { return createItemChildTAt<Type>(getItemIndex(_item)); } 00204 00206 MenuCtrl* getItemChildAt(size_t _index); 00207 00209 MenuCtrl* getItemChild(MenuItem* _item) { return getItemChildAt(getItemIndex(_item)); } 00210 00212 MenuCtrl* createItemChildAt(size_t _index) { return createItemChildTAt<MenuCtrl>(_index); } 00213 00215 MenuCtrl* createItemChild(MenuItem* _item) { return createItemChildAt(getItemIndex(_item)); } 00216 00218 void removeItemChildAt(size_t _index); 00219 00221 void removeItemChild(MenuItem* _item) { removeItemChildAt(getItemIndex(_item)); } 00222 00223 00225 MenuItemType getItemTypeAt(size_t _index); 00226 00228 MenuItemType getItemType(MenuItem* _item) { return getItemTypeAt(getItemIndex(_item)); } 00229 00231 void setItemTypeAt(size_t _index, MenuItemType _type); 00233 void setItemType(MenuItem* _item, MenuItemType _type) { setItemTypeAt(getItemIndex(_item), _type); } 00234 00236 void setPopupAccept(bool _value) { mPopupAccept = _value; } 00238 bool getPopupAccept() { return mPopupAccept; } 00239 00241 MenuItem* getMenuItemParent() { return mOwner; } 00242 00243 00244 /*event:*/ 00250 EventHandle_MenuCtrlPtrMenuItemPtr eventMenuCtrlAccept; 00251 00256 EventHandle_MenuCtrlPtr eventMenuCtrlClose; 00257 00258 00259 /*internal:*/ 00260 void _notifyDeleteItem(MenuItem* _item); 00261 void _notifyUpdateName(MenuItem* _item); 00262 void _wrapItemChild(MenuItem* _item, MenuCtrl* _widget); 00263 00264 virtual void _initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, Widget* _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name); 00265 00266 /*obsolete:*/ 00267 #ifndef MYGUI_DONT_USE_OBSOLETE 00268 00269 MYGUI_OBSOLETE("use : void Widget::setVisible(bool _value)") 00270 void showMenu() { setVisible(true); } 00271 MYGUI_OBSOLETE("use : void Widget::setVisible(bool _value)") 00272 void hideMenu() { setVisible(false); } 00273 MYGUI_OBSOLETE("use : bool Widget::isVisible()") 00274 bool isShowMenu() { return isVisible(); } 00275 00276 MYGUI_OBSOLETE("use : void setItemChildVisibleAt(size_t _index, bool _visible)") 00277 void showItemChildAt(size_t _index) { setItemChildVisibleAt(_index, true); } 00278 MYGUI_OBSOLETE("use : void setItemChildVisible(MenuItem* _item, bool _visible)") 00279 void showItemChild(MenuItem* _item) { setItemChildVisible(_item, true); } 00280 MYGUI_OBSOLETE("use : void setItemChildVisibleAt(size_t _index, bool _visible)") 00281 void hideItemChildAt(size_t _index) { setItemChildVisibleAt(_index, false); } 00282 MYGUI_OBSOLETE("use : void setItemChildVisible(MenuItem* _item, bool _visible)") 00283 void hideItemChild(MenuItem* _item) { setItemChildVisible(_item, false); } 00284 00285 #endif // MYGUI_DONT_USE_OBSOLETE 00286 00287 protected: 00288 virtual ~MenuCtrl(); 00289 00290 void baseChangeWidgetSkin(ResourceSkin* _info); 00291 00292 // переопределяем для особого обслуживания 00293 virtual Widget* baseCreateWidget(WidgetStyle _style, const std::string& _type, const std::string& _skin, const IntCoord& _coord, Align _align, const std::string& _layer, const std::string& _name); 00294 00295 virtual void onKeyChangeRootFocus(bool _focus); 00296 00297 private: 00298 void initialiseWidgetSkin(ResourceSkin* _info); 00299 void shutdownWidgetSkin(); 00300 00301 void notifyRootKeyChangeFocus(Widget* _sender, bool _focus); 00302 void notifyMouseButtonClick(Widget* _sender); 00303 void notifyMouseSetFocus(Widget* _sender, Widget* _new); 00304 00305 const std::string& getSkinByType(MenuItemType _type) 00306 { 00307 return _type == MenuItemType::Separator ? mSeparatorSkin : mSkinLine; 00308 } 00309 00310 size_t getIconIndexByType(MenuItemType _type) 00311 { 00312 return _type == MenuItemType::Popup ? ItemImagePopup : ItemImageNone; 00313 } 00314 00315 void update(); 00316 00317 void setButtonImageIndex(Button* _button, size_t _index); 00318 00319 MenuItemType getItemType(bool _submenu, bool _separator) 00320 { 00321 if (_submenu) return MenuItemType::Popup; 00322 else if (_separator) return MenuItemType::Separator; 00323 return MenuItemType::Normal; 00324 } 00325 00326 void notifyMenuCtrlAccept(MenuItem* _item); 00327 00328 Widget* createItemChildByType(size_t _index, const std::string& _type); 00329 00330 void _wrapItem(MenuItem* _item, size_t _index, const UString& _name, MenuItemType _type, const std::string& _id, Any _data); 00331 00332 ControllerFadeAlpha* createControllerFadeAlpha(float _alpha, float _coef, bool _enable); 00333 00334 Widget* _getClientWidget(); 00335 const Widget* _getClientWidget() const; 00336 00337 protected: 00338 bool mHideByAccept; 00339 // нужно ли выбрасывать по нажатию 00340 bool mMenuDropMode; 00341 bool mIsMenuDrop; 00342 bool mHideByLostKey; 00343 00344 private: 00345 VectorMenuItemInfo mItemsInfo; 00346 00347 int mHeightLine; 00348 std::string mSkinLine; 00349 00350 int mSubmenuImageSize; 00351 00352 std::string mSubMenuSkin; 00353 std::string mSubMenuLayer; 00354 00355 // флаг, чтобы отсеч уведомления от айтемов, при общем шутдауне виджета 00356 bool mShutdown; 00357 00358 int mSeparatorHeight; 00359 std::string mSeparatorSkin; 00360 00361 bool mAlignVert; 00362 int mDistanceButton; 00363 bool mPopupAccept; 00364 MenuItem* mOwner; 00365 bool mAnimateSmooth; 00366 00367 }; 00368 00369 } // namespace MyGUI 00370 00371 #endif // __MYGUI_MENU_CTRL_H__