xref: /haiku/docs/user/interface/Control.dox (revision 819863d8a90d53ecfd6ba71d7a46075a8f41f450)
1/*
2 * Copyright 2011-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		John Scipione, jscipione@gmail.com
7 *
8 * Corresponds to:
9 *		headers/os/interface/Control.h	 hrev47274
10 *		src/kits/interface/Control.cpp	 hrev47274
11 */
12
13
14/*!
15	\file Control.h
16	\ingroup interface
17	\ingroup libbe
18	\brief BControl class definition and support enums.
19*/
20
21
22/*!
23	\var B_CONTROL_ON
24
25	Control on. Value equal to 1.
26*/
27
28
29/*!
30	\var B_CONTROL_OFF
31
32	Control off. Value equal to 0.
33*/
34
35
36/*!
37	\class BControl
38	\ingroup interface
39	\ingroup libbe
40	\brief BControl is the base class for user-event handling objects.
41
42	Simple controls such as BCheckBox and BButton deviate only a bit from
43	BControl, whereas more complicated controls such as BColorControl and
44	BSlider re-implement much more functionality. Whether you are building
45	a simple control or something more complicated you should inherit from
46	BControl as it provides a common set of methods for intercepting
47	received messages from mouse and keyboard events.
48
49	Controls have state which they keep in their value. The value of a
50	control, stored as an int32, is read and set by the virtual Value() and
51	SetValue() methods. BControl defines \c B_CONTROL_ON and \c B_CONTROL_OFF
52	values that you can use as a convenience if your control has a simple
53	on/off state. If your BControl derived class stores a larger set of
54	states then you should define your own integer values instead.
55*/
56
57
58/*!
59	\fn BControl::BControl(BRect frame, const char* name, const char* label,
60		BMessage* message, uint32 resizingMode, uint32 flags)
61	\brief Construct a control in the \a frame with a \a name, \a label,
62	       model \a message, \a resizingMode, and creation \a flags.
63
64	The initial value of the control is set to 0 (\c B_CONTROL_OFF).
65	The \a label and the \a message parameters can be set to \c NULL.
66
67	\param frame The \a frame to draw the control in.
68	\param name The \a name of the control.
69	\param label The \a label displayed along with the control.
70	\param message The \a message to send when the control is activated.
71	\param resizingMode Defines the behavior of the control as the parent
72	       view resizes, see BView for more details.
73	\param flags Behavior \a flags for the control, see BView for details.
74*/
75
76
77/*!
78	\fn BControl::BControl(const char* name, const char* label,
79		BMessage* message, uint32 flags)
80	\brief Construct a control with a \a name, \a label, model \a message,
81	       and creation \a flags suitable for use with the Layout API.
82
83	The initial value of the control is set to 0 (\c B_CONTROL_OFF).
84	The \a label and the \a message parameters can be set to \c NULL.
85
86	\note This method was not available in BeOS R5.
87
88	\param name The \a name of the control.
89	\param label The \a label displayed along with the control.
90	\param message The \a message to send when the control is activated.
91	\param flags Behavior \a flags for the control, see BView for details.
92*/
93
94
95/*!
96	\fn BControl::~BControl()
97	\brief Frees all memory used by the BControl object including the memory
98	       used by the model message.
99*/
100
101
102/*!
103	\fn BControl::BControl(BMessage* archive)
104	\brief Creates a new BControl object from an \a archive message.
105
106	This method is usually not called directly. If you want to build a
107	control from a message you should call Instantiate() which can
108	handle errors properly.
109
110	If the \a archive deep, the BControl object will also unarchive each
111	of its child views recursively.
112
113	\param archive The \a archive message to restore from.
114*/
115
116
117/*!
118	\fn BArchivable* BControl::Instantiate(BMessage* archive)
119	\brief Creates a new object from an \a archive.
120
121	If the message is a valid object then the instance created from the
122	passed in \a archive will be returned. Otherwise this method will
123	return \c NULL.
124
125	\param archive The \a archive message.
126
127	\returns An instance of the object if \a archive is valid or \c NULL.
128
129	\sa BArchivable::Instantiate()
130*/
131
132
133/*!
134	\fn status_t BControl::Archive(BMessage* archive, bool deep) const
135	\brief Archives the control into \a archive.
136
137	\param archive The target \a archive that the data will go into.
138	\param deep Whether or not to recursively archive child views.
139
140	\retval B_OK The archive operation was successful.
141	\retval B_BAD_VALUE \c NULL \a archive message.
142	\retval B_ERROR The archive operation failed.
143
144	\sa BArchivable::Archive()
145*/
146
147
148/*!
149	\fn void BControl::WindowActivated(bool active)
150	\brief Hook method called when the attached window is activated or
151	       deactivated.
152
153	Redraws the focus ring around the menu field when the window is activated
154	or deactivated if it is the window's current focus view.
155
156	\param active \c true if the window becomes activated, \c false if the
157	       window becomes deactivated.
158
159	\sa BView::WindowActivated()
160*/
161
162
163/*!
164	\fn void BControl::AttachedToWindow()
165	\brief Hook method called when the control is attached to a window.
166
167	This method overrides BView::AttachedToWindow() setting the low color
168	and view color of the BControl so that it matches the view color of the
169	control's parent view. It also makes the attached window the default
170	target for Invoke() as long as another target has not already been set.
171
172	\sa BView::AttachedToWindow()
173	\sa Invoke()
174	\sa BInvoker::SetTarget()
175*/
176
177
178/*!
179	\fn void BControl::DetachedFromWindow()
180	\brief Hook method called when the object is detached from a window.
181
182	The default implementation does nothing.
183
184	\sa BView::DetachedFromWindow()
185*/
186
187
188/*!
189	\fn void BControl::AllAttached()
190	\brief Similar to AttachedToWindow() but this method is triggered after
191	       all child views have already been attached to a window.
192
193	The default implementation does nothing.
194
195	\sa BView::AllAttached()
196*/
197
198
199/*!
200	\fn void BControl::AllDetached()
201	\brief Similar to AttachedToWindow() but this method is triggered after
202	       all child views have already been detached from a window.
203
204	The default implementation does nothing.
205
206	\sa BView::AllDetached()
207*/
208
209
210/*!
211	\fn void BControl::MakeFocus(bool focused)
212	\brief Gives or removes focus from the control.
213
214	BControl::MakeFocus() overrides BView::MakeFocus() to call Draw() when
215	the focus changes. Derived classes generally don't have to re-implement
216	MakeFocus().
217
218	IsFocusChanging() returns \c true when Draw() is called from this method.
219
220	\param focused \a true to set focus, \a false to remove it.
221
222	\sa BView::MakeFocus()
223	\sa IsFocusChanging()
224*/
225
226
227/*!
228	\fn void BControl::KeyDown(const char *bytes, int32 numBytes)
229	\brief Hook method called when a keyboard key is pressed.
230
231	Overrides BView::KeyDown() to toggle the control value and then
232	calls Invoke() for \c B_SPACE or \c B_ENTER. If this is not desired
233	you should override this method in derived classes.
234
235	The KeyDown() method is only called if the BControl is the focus view
236	in the active window. If the window has a default button, \c B_ENTER
237	will be passed to that object instead of the focus view.
238
239	\param bytes The bytes of the key combination pressed.
240	\param numBytes The number of bytes in \a bytes.
241
242	\sa BView::KeyDown()
243	\sa MakeFocus()
244*/
245
246
247/*!
248	\fn void BControl::MessageReceived(BMessage* message)
249	\brief Handle \a message received by the associated looper.
250
251	\param message The \a message received by the associated looper.
252
253	\see BView::MessageReceived()
254*/
255
256
257/*!
258	\fn void BControl::MouseDown(BPoint where)
259	\brief Hook method called when a mouse button is pressed.
260
261	\param where The point on the screen where the mouse pointer is when
262	       the mouse button is pressed in the view's coordinate system.
263
264	\sa BView::MouseDown()
265*/
266
267
268/*!
269	\fn void BControl::MouseUp(BPoint where)
270	\brief Hook method called when a mouse button is released.
271
272	\param where The point on the screen where the mouse pointer is located
273	       when the mouse button is released in the view's coordinate system.
274
275	\sa BView::MouseUp()
276*/
277
278
279/*!
280	\fn void BControl::MouseMoved(BPoint where, uint32 code,
281		const BMessage* dragMessage)
282	\brief Hook method called when the mouse is moved.
283
284	\param where The new location of the mouse in the control's coordinate system.
285	\param code One of the following:
286	- \c B_ENTERED_VIEW The cursor has just entered the control.
287	- \c B_INSIDE_VIEW The cursor is inside the control.
288	- \c B_EXITED_VIEW The cursor has left the control's bounds. This only gets
289	     sent if the scope of the mouse events that the control can receive has
290	     been expanded by BView::SetEventMask() or BView::SetMouseEventMask().
291	- \c B_OUTSIDE_VIEW The cursor is outside the view. This only gets sent if the
292	     scope of the mouse events that the control can receive has been expanded
293	     by SetEventMask() or SetMouseEventMask().
294	\param dragMessage If a drag-and-drop operation is taking place this is a
295	       pointer to a BMessage that holds the drag information, otherwise the
296	       pointer is \c NULL.
297
298	\sa BView::MouseMoved()
299*/
300
301
302/*!
303	\fn void BControl::SetLabel(const char *label)
304	\brief Sets the \a label of the control.
305
306	If the \a label changes the control is redrawn.
307
308	\param label The \a label to set, can be \c NULL.
309*/
310
311
312/*!
313	\fn const char* BControl::Label() const
314	\brief Gets the label of the control.
315
316	\return The control's label.
317*/
318
319
320/*!
321	\fn void BControl::SetValue(int32 value)
322	\brief Sets the value of the control.
323
324	If the \a value changes the control is redrawn.
325
326	\param value The \a value to set.
327
328	\sa SetValueNoUpdate()
329*/
330
331
332/*!
333	\fn void BControl::SetValueNoUpdate(int32 value)
334	\brief Sets the value of the control without redrawing.
335
336	\note This method was not available in BeOS R5.
337
338	\param value The \a value to set.
339
340	\sa SetValue()
341*/
342
343
344/*!
345	\fn int32 BControl::Value() const
346	\brief Gets the value of the control.
347
348	\return The control's value.
349*/
350
351
352/*!
353	\fn void BControl::SetEnabled(bool enabled)
354	\brief Enables or disables the control.
355
356	BControl objects are enabled by default. If the control changes enabled
357	state then it is redrawn.
358
359	Disabled controls generally won't allow the user to focus on them
360	(The \c B_NAVIGABLE flag is turned off), and don't post any messages.
361
362	Disabled controls in derived classes should be drawn in subdued colors
363	to visually indicate that they are disabled and should not respond to
364	keyboard or mouse events.
365
366	\param enabled If \c true enables the control, if \c false, disables it.
367*/
368
369
370/*!
371	\fn bool BControl::IsEnabled() const
372	\brief Gets whether or not the control is currently enabled.
373
374	\return \c true if the control is enabled, \c false if it is disabled.
375*/
376
377
378/*!
379	\fn void BControl::GetPreferredSize(float *_width, float *_height)
380	\brief Fill out the preferred width and height of the control
381		into the \a _width and \a _height parameters.
382
383	Derived classes can override this method to set the preferred
384	width and height of the control.
385
386	\param[out] _width Pointer to a \c float to hold the width of the control.
387	\param[out] _height Pointer to a \c float to hold the height of the control.
388
389	\sa BView::GetPreferredSize()
390*/
391
392
393/*!
394	\fn void BControl::ResizeToPreferred()
395	\brief Resize the control to its preferred size.
396
397	\sa BView::ResizeToPreferred()
398*/
399
400
401/*!
402	\fn status_t BControl::Invoke(BMessage* message)
403	\brief Sends a copy of the model \a message to the designated target.
404
405	BControl::Invoke() overrides BInvoker::Invoke(). Derived classes
406	should use this method in their MouseDown() and KeyDown() methods
407	and should call IsEnabled() to check if the control is enabled
408	before calling Invoke().
409
410	The following fields added to the BMessage:
411		- "when"	\c B_INT64_TYPE	system_time()
412		- "source"	\c B_POINTER_TYPE	A pointer to the BControl object.
413
414	\param message The \a message to send.
415
416	\return \c B_OK if the control was invoked, otherwise an error
417	        code is returned.
418
419	\sa BInvoker::Invoke()
420	\sa IsEnabled()
421*/
422
423
424/*!
425	\fn BHandler* BControl::ResolveSpecifier(BMessage* message, int32 index,
426		BMessage* specifier, int32 what, const char* property)
427	\brief Determine the proper specifier for scripting messages.
428
429	\sa BHandler::ResolveSpecifier()
430*/
431
432
433/*!
434	\fn status_t BControl::GetSupportedSuites(BMessage* message)
435	\brief Report the suites of messages this control understands.
436
437	Adds the string "suite/vnd.Be-control" to the message.
438
439	\param message Allows you to add the names of the suites the control
440	       implements to the suites array.
441
442	\return \c B_OK if all went well or an error code otherwise.
443
444	\sa BHandler::GetSupportedSuites();
445*/
446
447
448/*!
449	\fn status_t BControl::Perform(perform_code code, void* _data)
450	\brief Perform some action. (Internal Method)
451
452	The following perform codes are recognized:
453		- \c PERFORM_CODE_MIN_SIZE
454		- \c PERFORM_CODE_MAX_SIZE
455		- \c PERFORM_CODE_PREFERRED_SIZE
456		- \c PERFORM_CODE_LAYOUT_ALIGNMENT
457		- \c PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH
458		- \c PERFORM_CODE_GET_HEIGHT_FOR_WIDTH
459		- \c PERFORM_CODE_SET_LAYOUT
460		- \c PERFORM_CODE_INVALIDATE_LAYOUT
461		- \c PERFORM_CODE_DO_LAYOUT
462
463	\param code The perform code.
464	\param _data A pointer to store some data.
465
466	\returns A status code.
467
468	\sa BHandler::Perform()
469*/
470
471
472/*!
473	\fn status_t BControl::SetIcon(const BBitmap* icon, uint32 flags)
474	\brief This convenience method is used to set the bitmaps
475	       for the standard states from a single bitmap.
476
477	It also supports cropping the icon to its non-transparent area.
478	The icon is meant as an addition to or replacement of the label.
479
480	\note This method was not available in BeOS R5.
481
482	\param icon The \a icon to set.
483	\param flags Modify how the icon is set.
484	- \c B_TRIM_ICON_BITMAP Crop the bitmap to the not fully transparent
485	     area, may change the icon size.
486	- \c B_TRIM_ICON_BITMAP_KEEP_ASPECT Like \c B_TRIM_BITMAP, but keeps
487	     the aspect ratio.
488	- \c B_CREATE_ACTIVE_ICON_BITMAP
489	- \c B_CREATE_PARTIALLY_ACTIVE_ICON_BITMAP
490	- \c B_CREATE_DISABLED_ICON_BITMAPS
491
492	\return \c B_OK if the icon was set or an error code otherwise.
493*/
494
495
496/*!
497	\fn status_t BControl::SetIconBitmap(const BBitmap* bitmap,
498		uint32 which, uint32 flags)
499	\brief Icon bitmaps for various states of the control (off, on,
500	       partially on, each enabled or disabled, plus up to 125
501	       custom states) can be set individually.
502
503	\note This method was not available in BeOS R5.
504
505	\param bitmap The \a bitmap icon to set.
506	\param which The state to set the icon for.
507	\param flags Modify how the icon is set.
508		- \c B_KEEP_ICON_BITMAP Transfer ownership of the bitmap to the control.
509
510	\return \c B_OK if the icon was set or an error code otherwise.
511*/