xref: /haiku/headers/os/interface/Menu.h (revision 6a45488dd93a920bccd174756ad44613d844189c)
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	BSize			MinSize();
117 virtual	BSize			MaxSize();
118 virtual	BSize			PreferredSize();
119 virtual void			GetPreferredSize(float *width, float *height);
120 virtual void			ResizeToPreferred();
121 virtual	void			DoLayout();
122 virtual	void			FrameMoved(BPoint new_position);
123 virtual	void			FrameResized(float new_width, float new_height);
124 		void			InvalidateLayout();
125 virtual	void			InvalidateLayout(bool descendants);
126 
127 virtual BHandler		*ResolveSpecifier(BMessage *msg,
128 										int32 index,
129 										BMessage *specifier,
130 										int32 form,
131 										const char *property);
132 virtual status_t		GetSupportedSuites(BMessage *data);
133 
134 virtual status_t		Perform(perform_code d, void *arg);
135 
136 virtual void		MakeFocus(bool state = true);
137 virtual void		AllAttached();
138 virtual void		AllDetached();
139 
140 protected:
141 
142 					BMenu(	BRect frame,
143 							const char *viewName,
144 							uint32 resizeMask,
145 							uint32 flags,
146 							menu_layout layout,
147 							bool resizeToFit);
148 
149 virtual	BPoint		ScreenLocation();
150 
151 		void		SetItemMargins(	float left,
152 									float top,
153 									float right,
154 									float bottom);
155 		void		GetItemMargins(	float *left,
156 									float *top,
157 									float *right,
158 									float *bottom) const;
159 
160 		menu_layout	Layout() const;
161 
162 virtual	void		Show();
163 		void		Show(bool selectFirstItem);
164 		void		Hide();
165 		BMenuItem	*Track(	bool start_opened = false,
166 							BRect *special_rect = NULL);
167 
168 public:
169 		enum add_state {
170 			B_INITIAL_ADD,
171 			B_PROCESSING,
172 			B_ABORT
173 		};
174 virtual	bool		AddDynamicItem(add_state s);
175 virtual void		DrawBackground(BRect update);
176 
177 		void		SetTrackingHook(menu_tracking_hook func, void *state);
178 
179 /*----- Private or reserved -----------------------------------------*/
180 private:
181 friend class BWindow;
182 friend class BMenuBar;
183 friend class BMenuItem;
184 friend status_t _init_interface_kit_();
185 friend status_t	set_menu_info(menu_info *);
186 friend status_t	get_menu_info(menu_info *);
187 
188 struct LayoutData;
189 
190 virtual	void			_ReservedMenu3();
191 virtual	void			_ReservedMenu4();
192 virtual	void			_ReservedMenu5();
193 virtual	void			_ReservedMenu6();
194 
195 		BMenu			&operator=(const BMenu &);
196 
197 		void		InitData(BMessage *data = NULL);
198 		bool		_show(bool selectFirstItem = false);
199 		void		_hide();
200 		BMenuItem	*_track(int *action, long start = -1);
201 
202 		void		_UpdateStateOpenSelect(BMenuItem *item, bigtime_t &openTime, bigtime_t &closeTime);
203 		void		_UpdateStateClose(BMenuItem *item, const BPoint &where, const uint32 &buttons);
204 
205 		bool		_AddItem(BMenuItem *item, int32 index);
206 		bool		RemoveItems(int32 index,
207 								int32 count,
208 								BMenuItem *item,
209 								bool del = false);
210 		bool		RelayoutIfNeeded();
211 		void		LayoutItems(int32 index);
212 		BSize		_ValidatePreferredSize();
213 		void		ComputeLayout(int32 index, bool bestFit, bool moveItems,
214 								  float* width, float* height);
215 		void		_ComputeColumnLayout(int32 index, bool bestFit, bool moveItems, BRect &outRect);
216 		void		_ComputeRowLayout(int32 index, bool bestFit, bool moveItems, BRect &outRect);
217 		void		_ComputeMatrixLayout(BRect &outRect);
218 
219 		BRect		Bump(BRect current, BPoint extent, int32 index) const;
220 		BPoint		ItemLocInRect(BRect frame) const;
221 		BRect		CalcFrame(BPoint where, bool *scrollOn);
222 		bool		ScrollMenu(BRect bounds, BPoint loc, bool *fast);
223 		void		ScrollIntoView(BMenuItem *item);
224 
225 		void		DrawItems(BRect updateRect);
226 		int			State(BMenuItem **item = NULL) const;
227 		void		InvokeItem(BMenuItem *item, bool now = false);
228 
229 		bool		OverSuper(BPoint loc);
230 		bool		OverSubmenu(BMenuItem *item, BPoint loc);
231 		BPrivate::BMenuWindow* MenuWindow();
232 		void		DeleteMenuWindow();
233 		BMenuItem	*HitTestItems(BPoint where, BPoint slop = B_ORIGIN) const;
234 		BRect		Superbounds() const;
235 		void		CacheFontInfo();
236 
237 		void		ItemMarked(BMenuItem *item);
238 		void		Install(BWindow *target);
239 		void		Uninstall();
240 		void		_SelectItem(BMenuItem* item, bool showSubmenu = true,
241 						bool selectFirstItem = false);
242 		BMenuItem	*CurrentSelection() const;
243 		bool		SelectNextItem(BMenuItem *item, bool forward);
244 		BMenuItem	*NextItem(BMenuItem *item, bool forward) const;
245 		bool		IsItemVisible(BMenuItem *item) const;
246 		void		SetIgnoreHidden(bool on);
247 		void		SetStickyMode(bool on);
248 		bool		IsStickyMode() const;
249 		void		CalcTriggers();
250 		const char	*ChooseTrigger(const char *title, BList *chars);
251 		void		UpdateWindowViewSize(bool upWind = true);
252 		bool		IsStickyPrefOn();
253 		void		RedrawAfterSticky(BRect bounds);
254 		bool		OkToProceed(BMenuItem *);
255 
256 		bool		CustomTrackingWantsToQuit();
257 
258 		void		QuitTracking();
259 
260 
261 		status_t	ParseMsg(BMessage *msg, int32 *sindex, BMessage *spec,
262 						int32 *form, const char **prop,
263 						BMenu **tmenu, BMenuItem **titem, int32 *user_data,
264 						BMessage *reply) const;
265 
266 		status_t	DoMenuMsg(BMenuItem **next, BMenu *tar, BMessage *m,
267 						BMessage *r, BMessage *spec, int32 f) const;
268 		status_t	DoMenuItemMsg(BMenuItem **next, BMenu *tar, BMessage *m,
269 						BMessage *r, BMessage *spec, int32 f) const;
270 
271 		status_t	DoEnabledMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
272 						BMessage *r) const;
273 		status_t	DoLabelMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
274 						BMessage *r) const;
275 		status_t	DoMarkMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
276 						BMessage *r) const;
277 		status_t	DoDeleteMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
278 						BMessage *r) const;
279 		status_t	DoCreateMsg(BMenuItem *ti, BMenu *tm, BMessage *m,
280 						BMessage *r, bool menu) const;
281 
282 static	menu_info	sMenuInfo;
283 static	bool		sAltAsCommandKey;
284 
285 		BMenuItem	*fChosenItem;
286 		BList		fItems;
287 		BRect		fPad;
288 		BMenuItem	*fSelected;
289 		BPrivate::BMenuWindow* fCachedMenuWindow;
290 		BMenu		*fSuper;
291 		BMenuItem	*fSuperitem;
292 		BRect		fSuperbounds;
293 		float		fAscent;
294 		float		fDescent;
295 		float		fFontHeight;
296 		uint32		fState;
297 		menu_layout	fLayout;
298 		BRect		*fExtraRect;
299 		float		fMaxContentWidth;
300 		BPoint		*fInitMatrixSize;
301 		_ExtraMenuData_	*fExtraMenuData;
302 
303 		LayoutData*	fLayoutData;
304 
305 		int32		fSubmenus;
306 
307 		char		fTrigger;
308 		bool		fResizeToFit;
309 		bool		fUseCachedMenuLayout;
310 		bool		fEnabled;
311 		bool		fDynamicName;
312 		bool		fRadioMode;
313 		bool		fTrackNewBounds;
314 		bool		fStickyMode;
315 		bool		fIgnoreHidden;
316 		bool		fTriggerEnabled;
317 		bool		fRedrawAfterSticky;
318 		bool		fAttachAborted;
319 };
320 
321 /*-------------------------------------------------------------*/
322 /*-------------------------------------------------------------*/
323 
324 #endif /* _MENU_H */
325