CGUIContextMenu.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __C_GUI_CONTEXT_MENU_H_INCLUDED__
  5. #define __C_GUI_CONTEXT_MENU_H_INCLUDED__
  6. #include "IrrCompileConfig.h"
  7. #ifdef _IRR_COMPILE_WITH_GUI_
  8. #include "IGUIContextMenu.h"
  9. #include "irrString.h"
  10. #include "irrArray.h"
  11. #include "IGUIFont.h"
  12. namespace irr
  13. {
  14. namespace gui
  15. {
  16. //! GUI Context menu interface.
  17. class CGUIContextMenu : public IGUIContextMenu
  18. {
  19. public:
  20. //! constructor
  21. CGUIContextMenu(IGUIEnvironment* environment,
  22. IGUIElement* parent, s32 id, core::rect<s32> rectangle,
  23. bool getFocus = true, bool allowFocus = true);
  24. //! destructor
  25. virtual ~CGUIContextMenu();
  26. //! set behavior when menus are closed
  27. virtual void setCloseHandling(ECONTEXT_MENU_CLOSE onClose);
  28. //! get current behavior when the menue will be closed
  29. virtual ECONTEXT_MENU_CLOSE getCloseHandling() const;
  30. //! Returns amount of menu items
  31. virtual u32 getItemCount() const;
  32. //! Adds a menu item.
  33. virtual u32 addItem(const wchar_t* text, s32 commandid,
  34. bool enabled, bool hasSubMenu, bool checked, bool autoChecking);
  35. //! Insert a menu item at specified position.
  36. virtual u32 insertItem(u32 idx, const wchar_t* text, s32 commandId, bool enabled,
  37. bool hasSubMenu, bool checked, bool autoChecking);
  38. //! Find a item which has the given CommandId starting from given index
  39. virtual s32 findItemWithCommandId(s32 commandId, u32 idxStartSearch) const;
  40. //! Adds a separator item to the menu
  41. virtual void addSeparator();
  42. //! Returns text of the menu item.
  43. virtual const wchar_t* getItemText(u32 idx) const;
  44. //! Sets text of the menu item.
  45. virtual void setItemText(u32 idx, const wchar_t* text);
  46. //! Returns if a menu item is enabled
  47. virtual bool isItemEnabled(u32 idx) const;
  48. //! Sets if the menu item should be enabled.
  49. virtual void setItemEnabled(u32 idx, bool enabled);
  50. //! Returns if a menu item is checked
  51. virtual bool isItemChecked(u32 idx) const;
  52. //! Sets if the menu item should be checked.
  53. virtual void setItemChecked(u32 idx, bool enabled);
  54. //! Removes a menu item
  55. virtual void removeItem(u32 idx);
  56. //! Removes all menu items
  57. virtual void removeAllItems();
  58. //! called if an event happened.
  59. virtual bool OnEvent(const SEvent& event);
  60. //! draws the element and its children
  61. virtual void draw();
  62. //! Returns the selected item in the menu
  63. virtual s32 getSelectedItem() const;
  64. //! Returns a pointer to the submenu of an item.
  65. //! \return Pointer to the submenu of an item.
  66. virtual IGUIContextMenu* getSubMenu(u32 idx) const;
  67. //! Sets the visible state of this element.
  68. virtual void setVisible(bool visible);
  69. //! should the element change the checked status on clicking
  70. virtual void setItemAutoChecking(u32 idx, bool autoChecking);
  71. //! does the element change the checked status on clicking
  72. virtual bool getItemAutoChecking(u32 idx) const;
  73. //! Returns command id of a menu item
  74. virtual s32 getItemCommandId(u32 idx) const;
  75. //! Sets the command id of a menu item
  76. virtual void setItemCommandId(u32 idx, s32 id);
  77. //! Adds a sub menu from an element that already exists.
  78. virtual void setSubMenu(u32 index, CGUIContextMenu* menu);
  79. //! When an eventparent is set it receives events instead of the usual parent element
  80. virtual void setEventParent(IGUIElement *parent);
  81. //! Writes attributes of the element.
  82. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
  83. //! Reads attributes of the element
  84. virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
  85. protected:
  86. void closeAllSubMenus();
  87. bool hasOpenSubMenu() const;
  88. struct SItem
  89. {
  90. core::stringw Text;
  91. bool IsSeparator;
  92. bool Enabled;
  93. bool Checked;
  94. bool AutoChecking;
  95. core::dimension2d<u32> Dim;
  96. s32 PosY;
  97. CGUIContextMenu* SubMenu;
  98. s32 CommandId;
  99. };
  100. virtual void recalculateSize();
  101. //! returns true, if an element was highlighted
  102. virtual bool highlight(const core::position2d<s32>& p, bool canOpenSubMenu);
  103. //! sends a click Returns:
  104. //! 0 if click went outside of the element,
  105. //! 1 if a valid button was clicked,
  106. //! 2 if a nonclickable element was clicked
  107. virtual u32 sendClick(const core::position2d<s32>& p);
  108. //! returns the item highlight-area
  109. virtual core::rect<s32> getHRect(const SItem& i, const core::rect<s32>& absolute) const;
  110. //! Gets drawing rect of Item
  111. virtual core::rect<s32> getRect(const SItem& i, const core::rect<s32>& absolute) const;
  112. core::array<SItem> Items;
  113. core::position2d<s32> Pos;
  114. IGUIElement* EventParent;
  115. IGUIFont *LastFont;
  116. ECONTEXT_MENU_CLOSE CloseHandling;
  117. s32 HighLighted;
  118. u32 ChangeTime;
  119. bool AllowFocus;
  120. };
  121. } // end namespace gui
  122. } // end namespace irr
  123. #endif // _IRR_COMPILE_WITH_GUI_
  124. #endif // __C_GUI_CONTEXT_MENU_H_INCLUDED__