xref: /haiku/headers/os/interface/Menu.h (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
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 			void				_Hide();
188 			BMenuItem*			_Track(int* action, long start = -1);
189 
190 			void				_UpdateNavigationArea(BPoint position,
191 									BRect& navAreaRectAbove,
192 									BRect& navAreaBelow);
193 
194 			void				_UpdateStateOpenSelect(BMenuItem* item,
195 									BPoint position, BRect& navAreaRectAbove,
196 									BRect& navAreaBelow,
197 									bigtime_t& selectedTime,
198 									bigtime_t& navigationAreaTime);
199 			void				_UpdateStateClose(BMenuItem* item,
200 									const BPoint& where,
201 									const uint32& buttons);
202 
203 			bool				_AddItem(BMenuItem* item, int32 index);
204 			bool				_RemoveItems(int32 index, int32 count,
205 									BMenuItem* item, bool deleteItems = false);
206 			bool				_RelayoutIfNeeded();
207 			void				_LayoutItems(int32 index);
208 			BSize				_ValidatePreferredSize();
209 			void				_ComputeLayout(int32 index, bool bestFit,
210 									bool moveItems, float* width,
211 									float* height);
212 			void				_ComputeColumnLayout(int32 index, bool bestFit,
213 									bool moveItems, BRect& outRect);
214 			void				_ComputeRowLayout(int32 index, bool bestFit,
215 									bool moveItems, BRect& outRect);
216 			void				_ComputeMatrixLayout(BRect& outRect);
217 
218 			BRect				_CalcFrame(BPoint where, bool* scrollOn);
219 
220 protected:
221 			void				_DrawItems(BRect updateRect);
222 
223 private:
224 			bool				_OverSuper(BPoint loc);
225 			bool				_OverSubmenu(BMenuItem* item, BPoint loc);
226 			BPrivate::BMenuWindow* _MenuWindow();
227 			void				_DeleteMenuWindow();
228 			BMenuItem*			_HitTestItems(BPoint where,
229 									BPoint slop = B_ORIGIN) const;
230 			BRect				_Superbounds() const;
231 			void				_CacheFontInfo();
232 
233 			void				_ItemMarked(BMenuItem* item);
234 			void				_Install(BWindow* target);
235 			void				_Uninstall();
236 			void				_SelectItem(BMenuItem* item,
237 									bool showSubmenu = true,
238 									bool selectFirstItem = false);
239 			bool				_SelectNextItem(BMenuItem* item, bool forward);
240 			BMenuItem*			_NextItem(BMenuItem* item, bool forward) const;
241 			void				_SetIgnoreHidden(bool on);
242 			void				_SetStickyMode(bool on);
243 			bool				_IsStickyMode() const;
244 			void				_CalcTriggers();
245 			bool				_ChooseTrigger(const char* title, int32& index,
246 									uint32& trigger,
247 									BPrivate::TriggerList& triggers);
248 			void				_UpdateWindowViewSize(
249 									bool updatePosition = true);
250 			bool				_OkToProceed(BMenuItem* item);
251 
252 			bool				_CustomTrackingWantsToQuit();
253 
254 			int					_State(BMenuItem** _item = NULL) const;
255 			void				_InvokeItem(BMenuItem* item, bool now = false);
256 			void				_QuitTracking(bool onlyThis = true);
257 
258 	static	menu_info			sMenuInfo;
259 	static	bool				sAltAsCommandKey;
260 
261 			BMenuItem*			fChosenItem;
262 			BList				fItems;
263 			BRect				fPad;
264 			BMenuItem*			fSelected;
265 			BPrivate::BMenuWindow* fCachedMenuWindow;
266 			BMenu*				fSuper;
267 			BMenuItem*			fSuperitem;
268 			BRect				fSuperbounds;
269 			float				fAscent;
270 			float				fDescent;
271 			float				fFontHeight;
272 			uint32				fState;
273 			menu_layout			fLayout;
274 			BRect*				fExtraRect;
275 			float				fMaxContentWidth;
276 			BPoint*				fInitMatrixSize;
277 			BPrivate::ExtraMenuData* fExtraMenuData;
278 
279 			LayoutData*			fLayoutData;
280 
281 			int32				_reserved;
282 
283 			char				fTrigger;
284 			bool				fResizeToFit;
285 			bool				fUseCachedMenuLayout;
286 			bool				fEnabled;
287 			bool				fDynamicName;
288 			bool				fRadioMode;
289 			bool				fTrackNewBounds;
290 			bool				fStickyMode;
291 			bool				fIgnoreHidden;
292 			bool				fTriggerEnabled;
293 			bool				fRedrawAfterSticky;
294 			bool				fAttachAborted;
295 };
296 
297 #endif // _MENU_H
298