2 * Copyright 2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * John Scipione, jscipione@gmail.com
9 * headers/os/interface/MenuItem.h hrev46969
10 * src/kits/interface/MenuItem.cpp hrev46969
18 \brief BMenuItem class definition.
26 \brief Display item for the BMenu class.
28 A BMenuItem either consists of a label or a submenu and message that
29 is sent to the attached menu's target when the item is selected. BMenu
30 and BMenuItem work in concert with each other in order to create a
31 menu tree hierarchy. BMenuItem's serve as nodes in the tree while
32 BMenu's serve as branches.
36 A menu item, unless it represents a submenu, can have a keyboard
37 shortcut which is a printable character used in combination with
38 the \key{Command} key and possibly other modifiers to invoke the item.
39 The shortcut is displayed right of the item's label.
43 A menu item may also have a trigger character assigned to it that
44 invokes the item without using the \key{Command} key. The trigger
45 characters is drawn underlined in the menu item's label. Unlike
46 shortcuts, triggers are automatically assigned to a menu item. You
47 can set the trigger character explicitly by calling SetTrigger().
51 \attention Triggers are currently disabled.
53 Both the shortcut character and trigger character are case-insensitive.
55 A menu item may be marked, which draws a checkmark on the left side
56 of the item. only one menu items may
57 be marked at a time if attached to a menu in radio mode.
60 \sa BMenu::SetRadioMode()
62 Menu items can also be enabled or disabled. A disabled item's label is drawn
63 in a lighter color to indicate that it may not be used. A disabled menu
64 item may not be selected or invoked. If the menu item controls a submenu the
65 submenu may still be opened but each of the items will be disabled.
74 \fn BMenuItem::BMenuItem(const char* label, BMessage* message,
75 char shortcut, uint32 modifiers)
76 \brief Creates a new BMenuItem object with the specified \a label
79 \param label The text \a label that is displayed.
80 \param message The BMessage that is sent when the item is selected.
81 \param shortcut The \a shortcut characters to activate the menu item.
82 \param modifiers The modifier keys to active the menu item,
83 \c B_COMMAND_KEY is assumed.
90 \fn BMenuItem::BMenuItem(BMenu* menu, BMessage* message)
91 \brief Creates a new BMenuItem object with the specified \a menu
94 The menu item's label is derived from the \a menu name. This method
95 makes the menu item a submenu.
97 \param menu The \a menu to assign to the item.
98 \param message The BMessage that is sent when the item is selected.
105 \fn BMenuItem::BMenuItem(BMessage* data)
106 \brief Archive constructor.
108 \param data The message \a data to construct the menu item from.
115 \fn BMenuItem::~BMenuItem()
118 Also frees the memory used by the label or submenu.
133 \fn BArchivable* BMenuItem::Instantiate(BMessage* data)
134 \brief Creates a new BMenuItem object from an \a data message.
136 \return A newly created BMenuItem object or \c NULL if the message
137 doesn't contain an archived BMenuItem.
144 \fn status_t BMenuItem::Archive(BMessage* data, bool deep) const
145 \brief Archives the the BMenuItem object into the \a data message.
147 Adds the label and current state of the BMenuItem to the archive.
149 \param data A pointer to the BMessage to archive the object into.
150 \param deep Whether or not to archive attached menus as well.
152 \return A status code, \c B_OK if everything went well or an error code
154 \retval B_OK The object was archived successfully.
155 \retval B_NO_MEMORY Ran out of memory while archiving the object.
165 \fn void BMenuItem::SetLabel(const char* string)
166 \brief Sets the menu item label to \a string.
168 The memory used by the label is copied so you may free the original.
169 Setting the label invalidates the attached menu.
171 \param string The \a string to set the label to.
178 \fn void BMenuItem::SetEnabled(bool enable)
179 \brief Enables or disables the menu item.
181 Enabling or disabling the menu item invalidates the attached menu.
183 \param enable \c true to enable the menu item, \c false to disable it.
190 \fn void BMenuItem::SetMarked(bool mark)
191 \brief Marks or unmarks the menu item.
193 Marking or unmarking the menu item invalidates the attached menu.
195 Marking a menu item attached to a menu in radio mode causes the currently
196 marked item to be unmarked.
198 \param mark \c true to mark the menu item, \c false to unmark it.
200 \sa BMenu::SetRadioMode()
207 \fn void BMenuItem::SetTrigger(char trigger)
208 \brief Set the character that activates this menu item. The triggered
209 character is drawn underlined in the menu.
211 \attention Triggers are currently disabled.
213 \param trigger The trigger character to set on this menu item.
220 \fn void BMenuItem::SetShortcut(char shortcut, uint32 modifiers)
221 \brief Set the keyboard shortcut of the menu item.
223 Setting a shortcut invalidates the attached menu.
225 This method will override the existing shortcut set to the window.
227 \param shortcut The ASCII shortcut character to set.
228 \param modifiers A bitmap mask of modifier keys used to activate
236 \fn const char* BMenuItem::Label() const
237 \brief Returns the item's label.
239 \return The item's label as a const char array.
246 \fn bool BMenuItem::IsEnabled() const
247 \brief Returns whether or not the item is enabled.
249 \return \c true if the item is enabled, \c false if disabled.
256 \fn bool BMenuItem::IsMarked() const
257 \brief Returns whether or not the item is marked.
259 \return \c true if the item is marked, \c false if unmarked.
266 \fn char BMenuItem::Trigger() const
267 \brief Returns the item's trigger character.
269 \return The current trigger character as a char or 0 if unset.
276 \fn char BMenuItem::Shortcut(uint32* modifiers) const
277 \brief Returns the currently set shortcut and fills out \a modifiers
278 with a bitmap of the modifier keys required to invoke the item.
280 \param modifiers A pointer to a uint32 to fill out.
282 \return The shortcut character assigned to the menu item as a char.
289 \fn BMenu* BMenuItem::Submenu() const
290 \brief Returns a pointer to the attached menu.
292 \return A pointer to the attached menu.
299 \fn BMenu* BMenuItem::Menu() const
300 \brief Returns a pointer to the menu that the item is attached to.
302 \return A pointer to the menu that the item is attached to.
309 \fn BRect BMenuItem::Frame() const
310 \brief Returns the bounds rectangle of the menu item.
312 \return The bounds rectangle of the menu item in the coordinate system
313 of the menu that the item is attached to.
320 \fn void BMenuItem::GetContentSize(float* _width, float* _height)
321 \brief Fills out \a _width and \a _height with the content rectangle
324 You only need to call this method if you're implementing your own
325 DrawContent() method to override how the contents of the menu item
328 The content rectangle excludes the item margins and the area that
329 contains the checkmark, shortcut, and submenu arrow.
331 The content rectangle can be calculated using this method as well as
332 ContentLocation() to get location of the top left corner.
334 \param _width Filled out with the width of the content rectangle.
335 \param _height Filled out with the height of the content rectangle.
337 \sa ContentLocation()
345 \fn void BMenuItem::TruncateLabel(float maxWidth, char* newLabel)
346 \brief Truncates the label and stashes it into \a newLabel.
348 You are responsible for allocating \a newLabel with enough space to fit
349 the label including the trailing \c NUL. The method will \c NUL terminate
352 \param maxWidth The maximum number of bytes to truncate the label to.
353 \param newLabel The buffer to store the truncated label in.
360 \fn void BMenuItem::DrawContent()
361 \brief Hook method used to draw the menu items contents.
363 This method is called automatically by BMenu::Draw(), you need not call it
364 yourself. You may want to override this method in derived classes to do
365 something different than drawing a text label.
372 \fn void BMenuItem::Draw()
373 \brief Hook method used to draw the menu items.
375 This method is called by automatically by BMenu::Draw(). You should not need to
376 call this method yourself but you may want to override it in a derived class
377 to do something other than the default. The default draws the mark, shortcut
378 and possibly a right arrow to indicate there is submenu and then calls
379 DrawContent() to fill in the label. Lastly Highlight() is called if the item
387 \fn void BMenuItem::Highlight(bool highlight)
388 \brief Highlights or unhighlights the menu item.
390 This method is called by Draw() when the item is selected or unselected.
392 You shouldn't need to call this method unless you override the Draw()
393 method in a derived class and you want to highlight differently.
395 \param highlight Highlights if \a highlight is \c true,
396 unhighlights if \c false.
403 \fn bool BMenuItem::IsSelected() const
404 \brief Returns whether or not the item is selected.
406 \return \c true if selected, \c false if not selected.
413 \fn BPoint BMenuItem::ContentLocation() const
414 \brief Returns the top-left point of the content rectangle.
416 You only need to call this method if you're implementing your own
417 DrawContent() method to override how the contents of the menu item
420 The content rectangle can be calculated using this method as well as
421 GetContentSize() to get the width and height.
423 \return The top-left point of the content rectangle as a BPoint in the
424 coordinate system of the attached BMenu.
434 \fn status_t BMenuItem::Invoke(BMessage* message)
435 \brief Sends a copy of the model \a message to the target.
437 This method extends BInvoker::Invoke() to guarantee that only enabled items
438 attached to the menu can be invoked and automatically marks the item.
440 The following fields added to the \a message:
441 - "when" \c B_INT64_TYPE system_time()
442 - "source" \c B_POINTER_TYPE A pointer to the menu item object.
443 - "index" \c B_INT32_TYPE The index of the menu item.
445 \param message The message to send or \c NULL to send the message set in
448 \return \c B_OK on success or an error code on error.
449 \retval B_OK The message was sent successfully.
450 \retval B_BAD_VALUE The message was \c NULL.
452 \sa BInvoker::Invoke()