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