MyGUI  3.0.1
MyGUI_MenuCtrl.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_MENU_CTRL_H__
24 #define __MYGUI_MENU_CTRL_H__
25 
26 #include "MyGUI_Prerequest.h"
27 #include "MyGUI_Types.h"
28 #include "MyGUI_MenuItemType.h"
29 #include "MyGUI_Widget.h"
30 #include "MyGUI_Any.h"
31 #include "MyGUI_EventPair.h"
32 #include "MyGUI_MenuItemType.h"
34 
35 namespace MyGUI
36 {
37 
40 
42  public Widget
43  {
45 
46  public:
47  MenuCtrl();
48 
49  enum ItemImage
50  {
52  ItemImagePopup
53  };
54 
55  struct ItemInfo
56  {
57  ItemInfo(MenuItem* _item, const UString& _name, MenuItemType _type, MenuCtrl* _submenu, const std::string& _id, Any _data) :
58  item(_item),
59  name(_name),
60  type(_type),
61  submenu(_submenu),
62  id(_id),
63  data(_data),
64  width(0)
65  {
66  }
67 
77  std::string id;
81  int width;
82  };
83 
84  typedef std::vector<ItemInfo> VectorMenuItemInfo;
85 
86  public:
88  virtual void setVisible(bool _value);
89 
91  void setVisibleSmooth(bool _value);
92 
93  //------------------------------------------------------------------------------//
94  // манипуляции айтемами
95 
97  size_t getItemCount() const { return mItemsInfo.size(); }
98 
100  MenuItem* insertItemAt(size_t _index, const UString& _name, MenuItemType _type = MenuItemType::Normal, const std::string& _id = "", Any _data = Any::Null);
102  MenuItem* insertItem(MenuItem* _to, const UString& _name, MenuItemType _type = MenuItemType::Normal, const std::string& _id = "", Any _data = Any::Null);
103 
105  MenuItem* addItem(const UString& _name, MenuItemType _type = MenuItemType::Normal, const std::string& _id = "", Any _data = Any::Null);
106 
108  void removeItemAt(size_t _index);
110  void removeItem(MenuItem* _item);
111 
113  void removeAllItems();
114 
115 
117  MenuItem* getItemAt(size_t _index);
118 
120  size_t getItemIndex(MenuItem* _item);
121 
123  size_t findItemIndex(MenuItem* _item);
124 
126  MenuItem* findItemWith(const UString& _name);
127 
128  //------------------------------------------------------------------------------//
129  // манипуляции данными
130 
132  void setItemDataAt(size_t _index, Any _data);
134  void setItemData(MenuItem* _item, Any _data) { setItemDataAt(getItemIndex(_item), _data); }
135 
137  void clearItemDataAt(size_t _index) { setItemDataAt(_index, Any::Null); }
139  void clearItemData(MenuItem* _item) { clearItemDataAt(getItemIndex(_item)); }
140 
142  template <typename ValueType>
143  ValueType * getItemDataAt(size_t _index, bool _throw = true)
144  {
145  MYGUI_ASSERT_RANGE(_index, mItemsInfo.size(), "MenuCtrl::getItemDataAt");
146  return mItemsInfo[_index].data.castType<ValueType>(_throw);
147  }
149  template <typename ValueType>
150  ValueType * getItemData(MenuItem* _item, bool _throw = true)
151  {
152  return getItemDataAt<ValueType>(getItemIndex(_item), _throw);
153  }
154 
156  void setItemIdAt(size_t _index, const std::string& _id);
158  void setItemId(MenuItem* _item, const std::string& _id) { setItemIdAt(getItemIndex(_item), _id); }
159 
161  const std::string& getItemIdAt(size_t _index);
163  const std::string& getItemId(MenuItem* _item) { return getItemIdAt(getItemIndex(_item)); }
164 
166  MenuItem* getItemById(const std::string& _id);
167 
169  size_t getItemIndexById(const std::string& _id);
170  //------------------------------------------------------------------------------//
171  // манипуляции отображением
172 
174  void setItemNameAt(size_t _index, const UString& _name);
176  void setItemName(MenuItem* _item, const UString& _name) { setItemNameAt(getItemIndex(_item), _name); }
177 
179  const UString& getItemNameAt(size_t _index);
181  const UString& getItemName(MenuItem* _item) { return getItemNameAt(getItemIndex(_item)); }
182 
184  size_t findItemIndexWith(const UString& _name);
185 
187  void setItemChildVisibleAt(size_t _index, bool _visible);
189  void setItemChildVisible(MenuItem* _item, bool _visible) { setItemChildVisibleAt(getItemIndex(_item), _visible); }
190 
191  //------------------------------------------------------------------------------//
192  // остальные манипуляции
193 
195  template <typename Type>
196  Type * createItemChildTAt(size_t _index)
197  {
198  return static_cast<Type*>(createItemChildByType(_index, Type::getClassTypeName()));
199  }
200 
202  template <typename Type>
203  Type * createItemChildT(MenuItem* _item) { return createItemChildTAt<Type>(getItemIndex(_item)); }
204 
206  MenuCtrl* getItemChildAt(size_t _index);
207 
209  MenuCtrl* getItemChild(MenuItem* _item) { return getItemChildAt(getItemIndex(_item)); }
210 
212  MenuCtrl* createItemChildAt(size_t _index) { return createItemChildTAt<MenuCtrl>(_index); }
213 
215  MenuCtrl* createItemChild(MenuItem* _item) { return createItemChildAt(getItemIndex(_item)); }
216 
218  void removeItemChildAt(size_t _index);
219 
221  void removeItemChild(MenuItem* _item) { removeItemChildAt(getItemIndex(_item)); }
222 
223 
225  MenuItemType getItemTypeAt(size_t _index);
226 
228  MenuItemType getItemType(MenuItem* _item) { return getItemTypeAt(getItemIndex(_item)); }
229 
231  void setItemTypeAt(size_t _index, MenuItemType _type);
233  void setItemType(MenuItem* _item, MenuItemType _type) { setItemTypeAt(getItemIndex(_item), _type); }
234 
236  void setPopupAccept(bool _value) { mPopupAccept = _value; }
238  bool getPopupAccept() { return mPopupAccept; }
239 
241  MenuItem* getMenuItemParent() { return mOwner; }
242 
243 
244  /*event:*/
251 
257 
258 
259  /*internal:*/
260  void _notifyDeleteItem(MenuItem* _item);
261  void _notifyUpdateName(MenuItem* _item);
262  void _wrapItemChild(MenuItem* _item, MenuCtrl* _widget);
263 
264  virtual void _initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, Widget* _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name);
265 
266  /*obsolete:*/
267 #ifndef MYGUI_DONT_USE_OBSOLETE
268 
269  MYGUI_OBSOLETE("use : void Widget::setVisible(bool _value)")
270  void showMenu() { setVisible(true); }
271  MYGUI_OBSOLETE("use : void Widget::setVisible(bool _value)")
272  void hideMenu() { setVisible(false); }
273  MYGUI_OBSOLETE("use : bool Widget::isVisible()")
274  bool isShowMenu() { return isVisible(); }
275 
276  MYGUI_OBSOLETE("use : void setItemChildVisibleAt(size_t _index, bool _visible)")
277  void showItemChildAt(size_t _index) { setItemChildVisibleAt(_index, true); }
278  MYGUI_OBSOLETE("use : void setItemChildVisible(MenuItem* _item, bool _visible)")
279  void showItemChild(MenuItem* _item) { setItemChildVisible(_item, true); }
280  MYGUI_OBSOLETE("use : void setItemChildVisibleAt(size_t _index, bool _visible)")
281  void hideItemChildAt(size_t _index) { setItemChildVisibleAt(_index, false); }
282  MYGUI_OBSOLETE("use : void setItemChildVisible(MenuItem* _item, bool _visible)")
283  void hideItemChild(MenuItem* _item) { setItemChildVisible(_item, false); }
284 
285 #endif // MYGUI_DONT_USE_OBSOLETE
286 
287  protected:
288  virtual ~MenuCtrl();
289 
290  void baseChangeWidgetSkin(ResourceSkin* _info);
291 
292  // переопределяем для особого обслуживания
293  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);
294 
295  virtual void onKeyChangeRootFocus(bool _focus);
296 
297  private:
298  void initialiseWidgetSkin(ResourceSkin* _info);
299  void shutdownWidgetSkin();
300 
301  void notifyRootKeyChangeFocus(Widget* _sender, bool _focus);
302  void notifyMouseButtonClick(Widget* _sender);
303  void notifyMouseSetFocus(Widget* _sender, Widget* _new);
304 
305  const std::string& getSkinByType(MenuItemType _type)
306  {
307  return _type == MenuItemType::Separator ? mSeparatorSkin : mSkinLine;
308  }
309 
310  size_t getIconIndexByType(MenuItemType _type)
311  {
312  return _type == MenuItemType::Popup ? ItemImagePopup : ItemImageNone;
313  }
314 
315  void update();
316 
317  void setButtonImageIndex(Button* _button, size_t _index);
318 
319  MenuItemType getItemType(bool _submenu, bool _separator)
320  {
321  if (_submenu) return MenuItemType::Popup;
322  else if (_separator) return MenuItemType::Separator;
323  return MenuItemType::Normal;
324  }
325 
326  void notifyMenuCtrlAccept(MenuItem* _item);
327 
328  Widget* createItemChildByType(size_t _index, const std::string& _type);
329 
330  void _wrapItem(MenuItem* _item, size_t _index, const UString& _name, MenuItemType _type, const std::string& _id, Any _data);
331 
332  ControllerFadeAlpha* createControllerFadeAlpha(float _alpha, float _coef, bool _enable);
333 
334  Widget* _getClientWidget();
335  const Widget* _getClientWidget() const;
336 
337  protected:
339  // нужно ли выбрасывать по нажатию
343 
344  private:
345  VectorMenuItemInfo mItemsInfo;
346 
347  int mHeightLine;
348  std::string mSkinLine;
349 
350  int mSubmenuImageSize;
351 
352  std::string mSubMenuSkin;
353  std::string mSubMenuLayer;
354 
355  // флаг, чтобы отсеч уведомления от айтемов, при общем шутдауне виджета
356  bool mShutdown;
357 
358  int mSeparatorHeight;
359  std::string mSeparatorSkin;
360 
361  bool mAlignVert;
362  int mDistanceButton;
363  bool mPopupAccept;
364  MenuItem* mOwner;
365  bool mAnimateSmooth;
366 
367  };
368 
369 } // namespace MyGUI
370 
371 #endif // __MYGUI_MENU_CTRL_H__