xref: /haiku/headers/os/interface/Menu.h (revision e0ef64750f3169cd634bb2f7a001e22488b05231)
1 /*
2  * Copyright 2007-2009, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _MENU_H
6 #define _MENU_H
7 
8 
9 #include <InterfaceDefs.h>
10 #include <List.h>
11 #include <View.h>
12 
13 class BMenu;
14 class BMenuBar;
15 class BMenuItem;
16 
17 
18 namespace BPrivate {
19 	class BMenuWindow;
20 	class ExtraMenuData;
21 	class TriggerList;
22 	class MenuPrivate;
23 }
24 
25 enum menu_layout {
26 	B_ITEMS_IN_ROW = 0,
27 	B_ITEMS_IN_COLUMN,
28 	B_ITEMS_IN_MATRIX
29 };
30 
31 struct menu_info {
32 	float		font_size;
33 	font_family	f_family;
34 	font_style	f_style;
35 	rgb_color	background_color;
36 	int32		separator;
37 	bool		click_to_open;
38 	bool		triggers_always_shown;
39 };
40 
41 status_t get_menu_info(menu_info* info);
42 status_t set_menu_info(menu_info* info);
43 
44 typedef bool (*menu_tracking_hook)(BMenu* menu, void* state);
45 
46 
47 class BMenu : public BView {
48 public:
49 								BMenu(const char* title,
50 									menu_layout layout = B_ITEMS_IN_COLUMN);
51 								BMenu(const char* title, float width,
52 									float height);
53 								BMenu(BMessage* archive);
54 
55 	virtual						~BMenu();
56 
57 	static	BArchivable*		Instantiate(BMessage* archive);
58 	virtual	status_t			Archive(BMessage* archive,
59 									bool deep = true) const;
60 
61 	virtual void				AttachedToWindow();
62 	virtual void				DetachedFromWindow();
63 	virtual void				AllAttached();
64 	virtual void				AllDetached();
65 
66 	virtual void				Draw(BRect updateRect);
67 
68 	virtual void				MessageReceived(BMessage* message);
69 	virtual	void				KeyDown(const char* bytes, int32 numBytes);
70 
71 	virtual	BSize				MinSize();
72 	virtual	BSize				MaxSize();
73 	virtual	BSize				PreferredSize();
74 	virtual void				GetPreferredSize(float* _width,
75 									float* _height);
76 	virtual void				ResizeToPreferred();
77 	virtual	void				DoLayout();
78 	virtual	void				FrameMoved(BPoint newPosition);
79 	virtual	void				FrameResized(float newWidth, float newHeight);
80 			void				InvalidateLayout();
81 	virtual	void				InvalidateLayout(bool descendants);
82 
83 	virtual void				MakeFocus(bool focus = true);
84 
85 			bool				AddItem(BMenuItem* item);
86 			bool				AddItem(BMenuItem* item, int32 index);
87 			bool				AddItem(BMenuItem* item, BRect frame);
88 			bool				AddItem(BMenu* menu);
89 			bool				AddItem(BMenu* menu, int32 index);
90 			bool				AddItem(BMenu* menu, BRect frame);
91 			bool				AddList(BList* list, int32 index);
92 
93 			bool				AddSeparatorItem();
94 
95 			bool				RemoveItem(BMenuItem* item);
96 			BMenuItem*			RemoveItem(int32 index);
97 			bool				RemoveItems(int32 index, int32 count,
98 									bool deleteItems = false);
99 			bool				RemoveItem(BMenu* menu);
100 
101 			BMenuItem*			ItemAt(int32 index) const;
102 			BMenu*				SubmenuAt(int32 index) const;
103 			int32				CountItems() const;
104 			int32				IndexOf(BMenuItem* item) const;
105 			int32				IndexOf(BMenu* menu) const;
106 			BMenuItem*			FindItem(uint32 command) const;
107 			BMenuItem*			FindItem(const char* name) const;
108 
109 	virtual	status_t			SetTargetForItems(BHandler* target);
110 	virtual	status_t			SetTargetForItems(BMessenger messenger);
111 	virtual	void				SetEnabled(bool state);
112 	virtual	void				SetRadioMode(bool state);
113 	virtual	void				SetTriggersEnabled(bool state);
114 	virtual	void				SetMaxContentWidth(float maxWidth);
115 
116 			void				SetLabelFromMarked(bool state);
117 			bool				IsLabelFromMarked();
118 			bool				IsEnabled() const;
119 			bool				IsRadioMode() const;
120 			bool				AreTriggersEnabled() const;
121 			bool				IsRedrawAfterSticky() const;
122 			float				MaxContentWidth() const;
123 
124 			BMenuItem*			FindMarked();
125 
126 			BMenu*				Supermenu() const;
127 			BMenuItem*			Superitem() const;
128 
129 
130 	virtual BHandler*			ResolveSpecifier(BMessage* message,
131 									int32 index, BMessage* specifier,
132 									int32 form, const char* property);
133 	virtual status_t			GetSupportedSuites(BMessage* data);
134 
135 	virtual status_t			Perform(perform_code d, void* arg);
136 
137 protected:
138 								BMenu(BRect frame, const char* name,
139 									uint32 resizeMask, uint32 flags,
140 									menu_layout layout, bool resizeToFit);
141 
142 	virtual	BPoint				ScreenLocation();
143 
144 			void				SetItemMargins(float left, float top,
145 									float right, float bottom);
146 			void				GetItemMargins(float* _left, float* _top,
147 									float* _right, float* _bottom) const;
148 
149 			menu_layout			Layout() const;
150 
151 	virtual	void				Show();
152 			void				Show(bool selectFirstItem);
153 			void				Hide();
154 			BMenuItem*			Track(bool startOpened = false,
155 									BRect* specialRect = NULL);
156 
157 public:
158 	enum add_state {
159 		B_INITIAL_ADD,
160 		B_PROCESSING,
161 		B_ABORT
162 	};
163 	virtual	bool				AddDynamicItem(add_state state);
164 	virtual void				DrawBackground(BRect update);
165 
166 			void				SetTrackingHook(menu_tracking_hook hook,
167 									void* state);
168 
169 private:
170 	friend class BMenuBar;
171 	friend class BPrivate::MenuPrivate;
172 	friend status_t _init_interface_kit_();
173 	friend status_t	set_menu_info(menu_info* info);
174 	friend status_t	get_menu_info(menu_info* info);
175 
176 	struct LayoutData;
177 
178 	virtual	void				_ReservedMenu3();
179 	virtual	void				_ReservedMenu4();
180 	virtual	void				_ReservedMenu5();
181 	virtual	void				_ReservedMenu6();
182 
183 			BMenu&				operator=(const BMenu& other);
184 
185 			void				_InitData(BMessage* archive);
186 			bool				_Show(bool selectFirstItem = false,
187 									bool keyDown = false);
188 			void				_Hide();
189 			BMenuItem*			_Track(int* action, long start = -1);
190 
191 			void				_UpdateNavigationArea(BPoint position,
192 									BRect& navAreaRectAbove,
193 									BRect& navAreaBelow);
194 
195 			void				_UpdateStateOpenSelect(BMenuItem* item,
196 									BPoint position, BRect& navAreaRectAbove,
197 									BRect& navAreaBelow,
198 									bigtime_t& selectedTime,
199 									bigtime_t& navigationAreaTime);
200 			void				_UpdateStateClose(BMenuItem* item,
201 									const BPoint& where,
202 									const uint32& buttons);
203 
204 			bool				_AddItem(BMenuItem* item, int32 index);
205 			bool				_RemoveItems(int32 index, int32 count,
206 									BMenuItem* item, bool deleteItems = false);
207 			bool				_RelayoutIfNeeded();
208 			void				_LayoutItems(int32 index);
209 			BSize				_ValidatePreferredSize();
210 			void				_ComputeLayout(int32 index, bool bestFit,
211 									bool moveItems, float* width,
212 									float* height);
213 			void				_ComputeColumnLayout(int32 index, bool bestFit,
214 									bool moveItems, BRect& outRect);
215 			void				_ComputeRowLayout(int32 index, bool bestFit,
216 									bool moveItems, BRect& outRect);
217 			void				_ComputeMatrixLayout(BRect& outRect);
218 
219 			BRect				_CalcFrame(BPoint where, bool* scrollOn);
220 
221 protected:
222 			void				_DrawItems(BRect updateRect);
223 
224 private:
225 			bool				_OverSuper(BPoint loc);
226 			bool				_OverSubmenu(BMenuItem* item, BPoint loc);
227 			BPrivate::BMenuWindow* _MenuWindow();
228 			void				_DeleteMenuWindow();
229 			BMenuItem*			_HitTestItems(BPoint where,
230 									BPoint slop = B_ORIGIN) const;
231 			BRect				_Superbounds() const;
232 			void				_CacheFontInfo();
233 
234 			void				_ItemMarked(BMenuItem* item);
235 			void				_Install(BWindow* target);
236 			void				_Uninstall();
237 			void				_SelectItem(BMenuItem* item,
238 									bool showSubmenu = true,
239 									bool selectFirstItem = false,
240 									bool keyDown = false);
241 			bool				_SelectNextItem(BMenuItem* item, bool forward);
242 			BMenuItem*			_NextItem(BMenuItem* item, bool forward) const;
243 			void				_SetIgnoreHidden(bool on);
244 			void				_SetStickyMode(bool on);
245 			bool				_IsStickyMode() const;
246 			void				_GetIsAltCommandKey(bool &value) const;
247 			void				_CalcTriggers();
248 			bool				_ChooseTrigger(const char* title, int32& index,
249 									uint32& trigger,
250 									BPrivate::TriggerList& triggers);
251 			void				_UpdateWindowViewSize(const bool &updatePosition);
252 			bool				_AddDynamicItems(bool keyDown = false);
253 			bool				_OkToProceed(BMenuItem* item,
254 									bool keyDown = false);
255 
256 			bool				_CustomTrackingWantsToQuit();
257 
258 			int					_State(BMenuItem** _item = NULL) const;
259 			void				_InvokeItem(BMenuItem* item, bool now = false);
260 			void				_QuitTracking(bool onlyThis = true);
261 
262 	static	menu_info			sMenuInfo;
263 	static	bool				sAltAsCommandKey;
264 
265 			BMenuItem*			fChosenItem;
266 			BList				fItems;
267 			BRect				fPad;
268 			BMenuItem*			fSelected;
269 			BPrivate::BMenuWindow* fCachedMenuWindow;
270 			BMenu*				fSuper;
271 			BMenuItem*			fSuperitem;
272 			BRect				fSuperbounds;
273 			float				fAscent;
274 			float				fDescent;
275 			float				fFontHeight;
276 			uint32				fState;
277 			menu_layout			fLayout;
278 			BRect*				fExtraRect;
279 			float				fMaxContentWidth;
280 			BPoint*				fInitMatrixSize;
281 			BPrivate::ExtraMenuData* fExtraMenuData;
282 
283 			LayoutData*			fLayoutData;
284 
285 			int32				_reserved;
286 
287 			char				fTrigger;
288 			bool				fResizeToFit;
289 			bool				fUseCachedMenuLayout;
290 			bool				fEnabled;
291 			bool				fDynamicName;
292 			bool				fRadioMode;
293 			bool				fTrackNewBounds;
294 			bool				fStickyMode;
295 			bool				fIgnoreHidden;
296 			bool				fTriggerEnabled;
297 			bool				fRedrawAfterSticky;
298 			bool				fAttachAborted;
299 };
300 
301 #endif // _MENU_H
302