xref: /haiku/headers/os/interface/Menu.h (revision f09ba8ea46ba2f4e482d7cd03e8eb77f37a60663)
1 /*******************************************************************************
2 /
3 /	File:			Menu.h
4 /
5 /   Description:    BMenu display a menu of selectable items.
6 /
7 /	Copyright 1994-98, Be Incorporated, All Rights Reserved
8 /
9 *******************************************************************************/
10 
11 #ifndef _MENU_H
12 #define _MENU_H
13 
14 #include <BeBuild.h>
15 #include <InterfaceDefs.h>
16 #include <List.h>
17 #include <View.h>
18 
19 /*----------------------------------------------------------------*/
20 /*----- Menu declarations and structures -------------------------*/
21 
22 class BMenuItem;
23 class BMenuBar;
24 
25 namespace BPrivate {
26 	class BMenuWindow;
27 }
28 
29 class _ExtraMenuData_;
30 
31 enum menu_layout {
32 	B_ITEMS_IN_ROW = 0,
33 	B_ITEMS_IN_COLUMN,
34 	B_ITEMS_IN_MATRIX
35 };
36 
37 struct menu_info {
38 	float		font_size;
39 	font_family	f_family;
40 	font_style	f_style;
41 	rgb_color	background_color;
42 	int32		separator;
43 	bool		click_to_open;
44 	bool		triggers_always_shown;
45 };
46 
47 _IMPEXP_BE status_t	set_menu_info(menu_info *info);
48 _IMPEXP_BE status_t	get_menu_info(menu_info *info);
49 
50 typedef bool (* menu_tracking_hook )(BMenu *, void *);
51 
52 /*----------------------------------------------------------------*/
53 /*----- BMenu class ----------------------------------------------*/
54 
55 class BMenu : public BView
56 {
57 public:
58 						BMenu(	const char *title,
59 								menu_layout layout = B_ITEMS_IN_COLUMN);
60 						BMenu(const char *title, float width, float height);
61 virtual					~BMenu();
62 
63 						BMenu(BMessage *data);
64 static	BArchivable		*Instantiate(BMessage *data);
65 virtual	status_t		Archive(BMessage *data, bool deep = true) const;
66 
67 virtual void			AttachedToWindow();
68 virtual void			DetachedFromWindow();
69 
70 		bool			AddItem(BMenuItem *item);
71 		bool			AddItem(BMenuItem *item, int32 index);
72 		bool			AddItem(BMenuItem *item, BRect frame);
73 		bool			AddItem(BMenu *menu);
74 		bool			AddItem(BMenu *menu, int32 index);
75 		bool			AddItem(BMenu *menu, BRect frame);
76 		bool			AddList(BList *list, int32 index);
77 		bool			AddSeparatorItem();
78 		bool			RemoveItem(BMenuItem *item);
79 		BMenuItem		*RemoveItem(int32 index);
80 		bool			RemoveItems(int32 index,
81 									int32 count,
82 									bool del = false);
83 		bool			RemoveItem(BMenu *menu);
84 
85 		BMenuItem		*ItemAt(int32 index) const;
86 		BMenu			*SubmenuAt(int32 index) const;
87 		int32			CountItems() const;
88 		int32			IndexOf(BMenuItem *item) const;
89 		int32			IndexOf(BMenu *menu) const;
90 		BMenuItem		*FindItem(uint32 command) const;
91 		BMenuItem		*FindItem(const char *name) const;
92 
93 virtual	status_t		SetTargetForItems(BHandler *target);
94 virtual	status_t		SetTargetForItems(BMessenger messenger);
95 virtual	void			SetEnabled(bool state);
96 virtual	void			SetRadioMode(bool state);
97 virtual	void			SetTriggersEnabled(bool state);
98 virtual void			SetMaxContentWidth(float max);
99 
100 		void			SetLabelFromMarked(bool on);
101 		bool			IsLabelFromMarked();
102 		bool			IsEnabled() const;
103 		bool			IsRadioMode() const;
104 		bool			AreTriggersEnabled() const;
105 		bool			IsRedrawAfterSticky() const;
106 		float			MaxContentWidth() const;
107 
108 		BMenuItem		*FindMarked();
109 
110 		BMenu			*Supermenu() const;
111 		BMenuItem		*Superitem() const;
112 
113 virtual void			MessageReceived(BMessage *msg);
114 virtual	void			KeyDown(const char *bytes, int32 numBytes);
115 virtual void			Draw(BRect updateRect);
116 virtual void			GetPreferredSize(float *width, float *height);
117 virtual void			ResizeToPreferred();
118 virtual	void			FrameMoved(BPoint new_position);
119 virtual	void			FrameResized(float new_width, float new_height);
120 		void			InvalidateLayout();
121 
122 virtual BHandler		*ResolveSpecifier(BMessage *msg,
123 										int32 index,
124 										BMessage *specifier,
125 										int32 form,
126 										const char *property);
127 virtual status_t		GetSupportedSuites(BMessage *data);
128 
129 virtual status_t		Perform(perform_code d, void *arg);
130 
131 virtual void		MakeFocus(bool state = true);
132 virtual void		AllAttached();
133 virtual void		AllDetached();
134 
135 protected:
136 
137 					BMenu(	BRect frame,
138 							const char *viewName,
139 							uint32 resizeMask,
140 							uint32 flags,
141 							menu_layout layout,
142 							bool resizeToFit);
143 
144 virtual	BPoint		ScreenLocation();
145 
146 		void		SetItemMargins(	float left,
147 									float top,
148 									float right,
149 									float bottom);
150 		void		GetItemMargins(	float *left,
151 									float *top,
152 									float *right,
153 									float *bottom) const;
154 
155 		menu_layout	Layout() const;
156 
157 virtual	void		Show();
158 		void		Show(bool selectFirstItem);
159 		void		Hide();
160 		BMenuItem	*Track(	bool start_opened = false,
161 							BRect *special_rect = NULL);
162 
163 public:
164 		enum add_state {
165 			B_INITIAL_ADD,
166 			B_PROCESSING,
167 			B_ABORT
168 		};
169 virtual	bool		AddDynamicItem(add_state s);
170 virtual void		DrawBackground(BRect update);
171 
172 		void		SetTrackingHook(menu_tracking_hook func, void *state);
173 
174 /*----- Private or reserved -----------------------------------------*/
175 private:
176 friend class BWindow;
177 friend class BMenuBar;
178 friend class BMenuItem;
179 friend status_t _init_interface_kit_();
180 friend status_t	set_menu_info(menu_info *);
181 friend status_t	get_menu_info(menu_info *);
182 
183 virtual	void			_ReservedMenu3();
184 virtual	void			_ReservedMenu4();
185 virtual	void			_ReservedMenu5();
186 virtual	void			_ReservedMenu6();
187 
188 		BMenu			&operator=(const BMenu &);
189 
190 		void		InitData(BMessage *data = NULL);
191 		bool		_show(bool selectFirstItem = false);
192 		void		_hide();
193 		BMenuItem	*_track(int *action, long start = -1);
194 
195 		void		_UpdateStateOpenSelect(BMenuItem *item, bigtime_t &openTime, bigtime_t &closeTime);
196 		void		_UpdateStateClose(BMenuItem *item, const BPoint &where, const uint32 &buttons);
197 
198 		bool		_AddItem(BMenuItem *item, int32 index);
199 		bool		RemoveItems(int32 index,
200 								int32 count,
201 								BMenuItem *item,
202 								bool del = false);
203 		bool		RelayoutIfNeeded();
204 		void		LayoutItems(int32 index);
205 		void		ComputeLayout(int32 index, bool bestFit, bool moveItems,
206 								  float* width, float* height);
207 		void		_ComputeColumnLayout(int32 index, bool bestFit, bool moveItems, BRect &outRect);
208 		void		_ComputeRowLayout(int32 index, bool bestFit, bool moveItems, BRect &outRect);
209 		void		_ComputeMatrixLayout(BRect &outRect);
210 
211 		BRect		Bump(BRect current, BPoint extent, int32 index) const;
212 		BPoint		ItemLocInRect(BRect frame) const;
213 		BRect		CalcFrame(BPoint where, bool *scrollOn);
214 		bool		ScrollMenu(BRect bounds, BPoint loc, bool *fast);
215 		void		ScrollIntoView(BMenuItem *item);
216 
217 		void		DrawItems(BRect updateRect);
218 		int			State(BMenuItem **item = NULL) const;
219 		void		InvokeItem(BMenuItem *item, bool now = false);
220 
221 		bool		OverSuper(BPoint loc);
222 		bool		OverSubmenu(BMenuItem *item, BPoint loc);
223 		BPrivate::BMenuWindow* MenuWindow();
224 		void		DeleteMenuWindow();
225 		BMenuItem	*HitTestItems(BPoint where, BPoint slop = B_ORIGIN) const;
226 		BRect		Superbounds() const;
227 		void		CacheFontInfo();
228 
229 		void		ItemMarked(BMenuItem *item);
230 		void		Install(BWindow *target);
231 		void		Uninstall();
232 		void		_SelectItem(BMenuItem* item, bool showSubmenu = true,
233 						bool selectFirstItem = false);
234 		BMenuItem	*CurrentSelection() const;
235 		bool		SelectNextItem(BMenuItem *item, bool forward);
236 		BMenuItem	*NextItem(BMenuItem *item, bool forward) const;
237 		bool		IsItemVisible(BMenuItem *item) const;
238 		void		SetIgnoreHidden(bool on);
239 		void		SetStickyMode(bool on);
240 		bool		IsStickyMode() const;
241 		void		CalcTriggers();
242 		const char	*ChooseTrigger(const char *title, BList *chars);
243 		void		UpdateWindowViewSize(bool upWind = true);
244 		bool		IsStickyPrefOn();
245 		void		RedrawAfterSticky(BRect bounds);
246 		bool		OkToProceed(BMenuItem *);
247 
248 		bool		CustomTrackingWantsToQuit();
249 
250 		void		QuitTracking();
251 
252 
253 		status_t	ParseMsg(BMessage *msg, int32 *sindex, BMessage *spec,
254 						int32 *form, const char **prop,
255 						BMenu **tmenu, BMenuItem **titem, int32 *user_data,
256 						BMessage *reply) const;
257 
258 		status_t	DoMenuMsg(BMenuItem **next, BMenu *tar, BMessage *m,
259 						BMessage *r, BMessage *spec, int32 f) const;
260 		status_t	DoMenuItemMsg(BMenuItem **next, BMenu *tar, BMessage *m,
261 						BMessage *r, BMessage *spec, int32 f) const;
262 
263 		status_t	DoEnabledMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
264 						BMessage *r) const;
265 		status_t	DoLabelMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
266 						BMessage *r) const;
267 		status_t	DoMarkMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
268 						BMessage *r) const;
269 		status_t	DoDeleteMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
270 						BMessage *r) const;
271 		status_t	DoCreateMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
272 						BMessage *r, bool menu) const;
273 
274 static	menu_info	sMenuInfo;
275 static	bool		sAltAsCommandKey;
276 
277 		BMenuItem	*fChosenItem;
278 		BList		fItems;
279 		BRect		fPad;
280 		BMenuItem	*fSelected;
281 		BPrivate::BMenuWindow* fCachedMenuWindow;
282 		BMenu		*fSuper;
283 		BMenuItem	*fSuperitem;
284 		BRect		fSuperbounds;
285 		float		fAscent;
286 		float		fDescent;
287 		float		fFontHeight;
288 		uint32		fState;
289 		menu_layout	fLayout;
290 		BRect		*fExtraRect;
291 		float		fMaxContentWidth;
292 		BPoint		*fInitMatrixSize;
293 		_ExtraMenuData_	*fExtraMenuData;
294 
295 		uint32		_reserved[1];
296 
297 		int32		fSubmenus;
298 
299 		char		fTrigger;
300 		bool		fResizeToFit;
301 		bool		fUseCachedMenuLayout;
302 		bool		fEnabled;
303 		bool		fDynamicName;
304 		bool		fRadioMode;
305 		bool		fTrackNewBounds;
306 		bool		fStickyMode;
307 		bool		fIgnoreHidden;
308 		bool		fTriggerEnabled;
309 		bool		fRedrawAfterSticky;
310 		bool		fAttachAborted;
311 };
312 
313 /*-------------------------------------------------------------*/
314 /*-------------------------------------------------------------*/
315 
316 #endif /* _MENU_H */
317