hide random home http://www.be.com/documentation/be_book/InterfaceKit/Menu.html (Amiga Plus Extra No. 5/97, 05/1997)



The Interface Kit: BMenu

Derived from: public BView

Declared in: <interface/Menu.h>


Overview

A BMenu object displays a pull-down or pop-up list of menu items. Menus organize the features of an application--the common ones as well as the more obscure--and provide users with points of entry for most everything the application can do.

Menus categorize the features of the application--all formatting possibilities might be grouped in one menu, a list of documents in another, graphics choices in a third, and so on. The arrangement of menus presents an outline of how the various parts of the application fit together.


Menu Hierarchy

Menus are hierarchically arranged; an item in one menu can control another menu. The controlled menu is a submenu; the menu that contains the item that controls it is its supermenu. A submenu remains hidden until the user operates the item that controls it; it becomes hidden again when the user is finished with it. A submenu can have its own submenus, and those submenus can have submenus of their own, and so on--although it becomes hard for users to find their way around in a menu hierarchy that becomes too deep.

The menu at the root of the hierarchy is displayed in a window as a list --perhaps a list of just one item. Since it, unlike other menus, doesn't have a controlling item, it must remain visible. A root menu is therefore a special kind of menu in that it behaves more like an ordinary view than do other menus, which stay hidden. Root menus should belong to the BMenuBar class, which is derived from BMenu. The typical root menu is a menu bar displayed across the top of a window (hence the name of the class).


Menu Items

Each item in a menu is a kind of BMenuItem object. An item can be marked (displayed with a check mark to its left), assigned a keyboard shortcut, enabled and disabled, and given a "trigger" character that the user can type to invoke the item when its menu is open on-screen.

Every item has a particular job to do. If an item controls a submenu, its job is to show the submenu on-screen and hide it again. All other items give instructions to the application. When invoked by the user, they post a BMessage object to a target BHandler. What the item does depends on the content of the BMessage and the BHandler's response to it.

The BMenu and BMenuItem classes share some functions that accomplish the same thing when called for a submenu or for the supermenu item that controls the submenu. For example, setting the target for a BMenu (SetTarget()) sets the target for each of its items. Disabling a submenu ( SetEnabled()) is the same as disabling the item that controls it; the user will be able to bring the submenu to the screen, but none of its items will work. This, in effect, disables all items and menus in the branch of the menu hierarchy under the superitem.


Hook Functions

ScreenLocation() Can be implemented to have the menu appear on-screen at some location other than the default.


Constructor and Destructor


BMenu()

public:

      BMenu(const char *name, menu_layout layout = B_ITEMS_IN_COLUMN)

      BMenu(const char *name, float width, float height)

protected:

      BMenu(BRect frame, const char *name, ulong resizingMode, ulong flags, menu_layout layout, bool resizeToFit)

Initializes the BMenu object. The name of the object becomes the initial label of the supermenu item that controls the menu and brings it to the screen. (It's also the name that can be passed to BView's FindView() function.)

A new BMenu object doesn't contain any items; you need to call AddItem() to set up its contents.

A menu can arrange its items in any of three ways:

B_ITEMS_IN_COLUMN The items are stacked vertically in a column, one on top of the other, as in a typical menu.
B_ITEMS_IN_ROW The items are laid out horizontally in a row, from end to end, as in a typical menu bar.
B_ITEMS_IN_MATRIX The items are arranged in a custom fashion, such as a matrix.

Either B_ITEMS_IN_ROW or the default B_ITEMS_IN_COLUMN can be passed as the layout argument to the public constructor. (A column is the default for ordinary menus; a row is the default for BMenuBars.) This version of the constructor isn't designed for B_ITEMS_IN_MATRIX layouts.

A BMenu object can arrange items that are laid out in a column or a row entirely on its own. The menu will be resized to exactly fit the items that are added to it.

However, when items are laid out in a custom matrix, the menu needs more help. First, the constructor must be informed of the exact width and height of the menu rectangle. The version of the constructor that takes these two parameters is designed just for matrix menus--it sets the layout to B_ITEMS_IN_MATRIX. Then, when items are added to the menu, the BMenu object expects to be informed of their precise positions within the specified area. The menu is not resized to fit the items that are added. Finally, when items in the matrix change, you must take care of any required adjustments in the layout yourself.

The protected version of the constructor is supplied for derived classes that don't simply devise different sorts of menu items or arrange them in a different way, but invent a different kind of menu. If the resizeToFit flag is TRUE, it's expected that the layout will be B_ITEMS_IN_COLUMN or B_ITEMS_IN_ROW. The menu will resize itself to fit the items that are added to it. If the layout is B_ITEMS_IN_MATRIX, the resizeToFit flag should be FALSE .


~BMenu()

      virtual ~BMenu(void)

Deletes all the items that were added to the menu and frees all memory allocated by the BMenu object. Deleting the items serves also to delete any submenus those items control and, thus, the whole branch of the menu hierarchy.


Member Functions


AddItem()

      bool AddItem(BMenuItem *item)
      bool AddItem(BMenuItem *item, long index)

      bool AddItem(BMenuItem *item, BRect frame)

      bool AddItem(BMenu *submenu)

      bool AddItem(BMenu *submenu, long index)

      bool AddItem(BMenu *submenu, BRect frame)

Adds an item to the menu list at index--or, if no index is mentioned, to the end of the list. If items are arranged in a matrix rather than a list, it's necessary to specify the item's frame rectangle--the exact position where it should be located in the menu view. Assume a coordinate system for the menu that has the origin, (0.0, 0.0), at the left top corner of the view rectangle. The rectangle will have the width and height that were specified when the menu was constructed.

The versions of this function that take an index (even an implicit one) can be used only if the menu arranges items in a column or row (B_ITEMS_IN_COLUMN or B_ITEMS_IN_ROW); it's an error to use them for items arranged in a matrix. Conversely, the versions of this function that take a frame rectangle can be used only if the menu arranges items in a matrix (B_ITEMS_IN_MATRIX ); it's an error to use them for items arranged in a list.

If a submenu is specified rather than an item, AddItem() constructs a controlling BMenuItem for the submenu and adds the item to the menu.

If it's unable to add the item to the menu--for example, if the index is out-of-range or the wrong version of the function has been called-- AddItem() returns FALSE. If successful, it returns TRUE.

See also: the BMenu constructor, the BMenuItem class, RemoveItem()


AddSeparatorItem()

      bool AddSeparatorItem(void)

Creates an instance of the BSeparatorItem class and adds it to the end of the menu list, returning TRUE if successful and FALSE if not (a very unlikely possibility). This function is a shorthand for:

   BSeparatorItem *separator = new BSeparatorItem;
   AddItem(separator);

A separator serves only to separate other items in the list. It counts as an item and has an indexed position in the list, but it doesn't do anything. It's drawn as a horizontal line across the menu. Therefore, it's appropriately added only to menus where the items are laid out in a column.

See also: AddItem(), the BSeparatorItem class


AreTriggersEnabled() see SetTriggersEnabled()


AttachedToWindow()

      virtual void AttachedToWindow(void)

Finishes initializing the BMenu object by setting graphics parameters and laying out items. This function is called for you each time the BMenu is assigned to a window. For a submenu, that means each time the menu is shown on-screen.

See also: BView::AttachedToWindow()


CountItems()

      long CountItems(void) const

Returns the total number of items in the menu, including separator items.


Draw()

      virtual void Draw(BRect updateRect)

Draws the menu. This function is called for you whenever the menu is placed on-screen or is updated while on-screen. It's not a function you need to call yourself.

See also: BView::Draw()


FindItem()

      BMenuItem *FindItem(const char *label) const
      BMenuItem *FindItem(ulong command) const

Returns the item with the specified label--or the one that posts a message with the specified command. If there's more than one item in the menu hierarchy with that particular label or associated with that particular command, this function returns the first one it finds. It recursively searches the menu by working down the list of items in order. If an item controls a submenu, it searches the submenu before returning to check any remaining items in the menu.

If none of the items in the menu hierarchy meet the stated criterion, FindItem() returns NULL.


FindMarked()

      BMenuItem *FindMarked(void)

Returns the first marked item in the menu list (the one with the lowest index), or NULL if no item is marked.

See also: SetRadioMode() , BMenuItem::SetMarked()


Hide(), Show()

protected:

      void Hide(void)

      void Show(bool selectFirst)
      virtual void Show(void)

These functions hide the menu (remove the BMenu view from the window it's in and remove the window from the screen) and show it (attach the BMenu to a window and place the window on-screen). If the selectFirst flag passed to Show() is TRUE, the first item in the menu will be selected when it's shown. If selectFirst is FALSE, the menu is shown without a selected item.

The version of Show() that doesn't take an argument simply calls the version that does and passes it a selectFirst value of FALSE.

These functions are not ones that you'd ordinarily call, even when implementing a derived class. You'd need them only if you're implementing a nonstandard menu of some kind and want to control when the menu appears on-screen.

See also: BView::Show() , Track()


IndexOf()

      long IndexOf(BMenuItem *item) const
      long IndexOf(BMenu *submenu) const

Returns the index of the specified menu item--or the item that controls the specified submenu. Indices record the position of the item in the menu list. They begin at 0 for the item at the top of a column or at the left of a row and include separator items.

If the menu doesn't contain the specified item, or the item that controls submenu, the return value will be B_ERROR.

See also: AddItem()


InvalidateLayout()

      void InvalidateLayout(void)

Forces the BMenu to recalculate the layout of all menu items and, consequently, its own size. It can do this only if the items are arranged in a row or a column. If the items are arranged in a matrix, it's up to you to keep their layout up-to-date.

All BMenu and BMenuItem functions that change an item in a way that might affect the overall menu automatically invalidate the menu's layout so it will be recalculated. For example, changing the label of an item might cause the menu to become wider (if it needs more room to accommodate the longer label) or narrower (if it no longer needs as much room as before).

Therefore, you don't need to call InvalidateLayout() after using a Kit function to change a menu or menu item; it's called for you. You'd call it only when making some other change to a menu.

See also: the BMenu constructor


IsEnabled() see SetEnabled()


IsLabelFromMarked() see SetLabelFromMarked()


IsRadioMode() see SetRadioMode()


ItemAt(), SubmenuAt()

      BMenuItem *ItemAt(long index) const
      BMenu *SubmenuAt(long index) const

These functions return the item at index--or the submenu controlled by the item at index. If there's no item at the index, they return NULL . SubmenuAt() is a shorthand for:

   ItemAt(index)->Submenu()

It returns NULL if the item at index doesn't control a submenu.

See also: AddItem()


KeyDown()

      virtual void KeyDown(ulong aChar)

Handles keyboard navigation through the menu. This function is called to respond to messages reporting key-down events. It should not be called from application code.

See also: BView::KeyDown()


Layout()

protected:

      menu_layout Layout(void) const

Returns B_ITEMS_IN_COLUMN if the items in the menu are stacked in a column from top to bottom, B_ITEMS_IN_ROW if they're stretched out in a row from left to right, or B_ITEMS_IN_MATRIX if they're arranged in some custom fashion. By default BMenu items are arranged in a column and BMenuBar items in a row.

The layout is established by the constructor.

See also: the BMenu and BMenuBar constructors


RemoveItem()

      BMenuItem *RemoveItem(long index)
      bool RemoveItem(BMenuItem *item)

      bool RemoveItem(BMenu *submenu)

Removes the item at index, or the specified item, or the item that controls the specified submenu. Removing the item doesn't free it.

When an item is removed from a menu, it loses its target; the cached value is set to NULL. If the item controls a submenu, it remains attached to the submenu even after being removed.

See also: AddItem()


ScreenLocation()

protected:

      virtual BPoint ScreenLocation(void)

Returns the point where the left top corner of the menu should appear when the menu is shown on-screen. The point is specified in the screen coordinate system.

This function is called each time a hidden menu (a submenu of another menu) is brought to the screen. It can be overridden in a derived class to change where the menu appears. For example, the BPopUpMenu class overrides it so that a pop-up menu pops up over the controlling item.

See also: the BPopUpMenu class


SetEnabled(), IsEnabled()

      virtual void SetEnabled(bool enabled)
      bool IsEnabled(void) const

SetEnabled() enables the BMenu if the enabled flag is TRUE, and disables it if enabled is FALSE. If the menu is a submenu, this enables or disables its controlling item, just as if SetEnabled() were called for that item. The controlling item is updated so that it displays its new state, if it happens to be visible on-screen.

Disabling a menu disables its entire branch of the menu hierarchy. All items in the menu, including those that control other menus, are disabled.

IsEnabled() returns TRUE if the BMenu, and every BMenu above it in the menu hierarchy, is enabled. It returns FALSE if the BMenu, or any BMenu above it in the menu hierarchy, is disabled.

See also: BMenuItem::SetEnabled()


SetLabelFromMarked(), IsLabelFromMarked()

protected:

      void SetLabelFromMarked(bool flag)

      bool IsLabelFromMarked(void)

SetLabelFromMarked() determines whether the label of the item that controls the menu (the label of the superitem) should be taken from the currently marked item within the menu. If flag is TRUE, the menu is placed in radio mode and the superitem's label is reset each time the user selects a different item. If flag is FALSE , the setting for radio mode doesn't change and the label of the superitem isn't automatically reset.

IsLabelFromMarked() returns whether the superitem's label is taken from the marked item (but not necessarily whether the BMenu is in radio mode).

See also: SetRadioMode()


SetRadioMode(), IsRadioMode()

      virtual void SetRadioMode(bool flag)
      bool IsRadioMode(void)

SetRadioMode() puts the BMenu in radio mode if flag is TRUE and takes it out of radio mode if flag is FALSE. In radio mode, only one item in the menu can be marked at a time. If the user selects an item, a check mark is placed in front of it automatically (you don't need to call BMenuItem's SetMarked() function; it's called for you). If another item was marked at the time, its mark is removed. Selecting a currently marked item retains the mark.

IsRadioMode() returns whether the BMenu is currently in radio mode. The default radio mode is FALSE for ordinary BMenus, but TRUE for BPopUpMenus.

SetRadioMode() doesn't change any of the items in the menu. If you want an initial item to be marked when the menu is put into radio mode, you must mark it yourself.

When SetRadioMode() turns radio mode off, it calls SetLabelFromMarked() and passes it an argument of FALSE--turning off the feature that changes the label of the menu's superitem each time the marked item changes. Similarly, when SetLabelFromMarked() turns on this feature, it calls SetRadioMode() and passes it an argument of TRUE--turning radio mode on.

See also: BMenuItem::SetMarked(), SetLabelFromMarked()


SetTargetForItems()

      virtual long SetTargetForItems(BHandler *target)

This function is a convenience for assigning the same target BHandler to all the items in the menu. It works through the list of items in order, calling BMenuItem's SetTarget() virtual function for each one. If it's unable to set the target of any item, it aborts and returns the error it encountered. If successful in setting the target of all items, it returns B_NO_ERROR. See BMenuItem's SetTarget() for information on acceptable target values.

This function doesn't work recursively; it acts only on items currently in the BMenu, not on items that might be added later nor on items in submenus.

See also: BMenuItem::SetTarget()


SetTriggersEnabled(), AreTriggersEnabled()

      virtual void SetTriggersEnabled(bool flag)
      bool AreTriggersEnabled(void) const

SetTriggersEnabled() enables the triggers for all items in the menu if flag is TRUE and disables them if flag is FALSE . AreTriggersEnabled() returns whether the triggers are currently enabled or disabled. They're enabled by default.

Triggers are displayed to the user only if they're enabled, and only when keyboard actions can operate the menu.

Triggers are appropriate for some menus, but not for others. SetTriggersEnabled() is typically called to initialize the BMenu when it's constructed, not to enable and disable triggers as the application is running. If triggers are ever enabled for a menu, they should always be enabled; if they're ever disabled, they should always be disabled.

See also: BMenuItem::SetTrigger()


Show() see Hide()


SubmenuAt() see ItemAt()


Superitem(), Supermenu()

      BMenuItem *Superitem(void) const
      BMenu *Supermenu(void) const

These functions return the supermenu item that controls the BMenu and the supermenu where that item is located. The supermenu could be a BMenuBar object. If the BMenu hasn't been made the submenu of another menu, both functions return NULL .

See also: AddItem()


Track()

protected:

      BMenuItem *Track(bool openAnyway = FALSE, BRect *clickToOpenRect = NULL) 

Initiates tracking of the cursor within the menu. This function passes tracking control to submenus (and submenus of submenus) depending on where the user moves the mouse. If the user ends tracking by invoking an item, Track() returns the item. If the user didn't invoke any item, it returns NULL. The item doesn't have to be located in the BMenu; it could, for example, belong to a submenu of the BMenu.

If the openAnyway flag is TRUE, Track() opens the menu and leaves it open even though a mouse button isn't held down. This enables menu navigation from the keyboard. If a clickToOpenRect is specified and the user has set the click-to-open preference, Track() will leave the menu open if the user releases the mouse button while the cursor is inside the rectangle. The rectangle should be stated in the screen coordinate system.

Track() is called by the BMenu to initiate tracking in the menu hierarchy. You would need to call it yourself only if you're implementing a different kind of menu that starts to track the cursor under nonstandard circumstances.






The Be Book, HTML Edition, for Developer Release 8 of the Be Operating System.

Copyright © 1996 Be, Inc. All rights reserved.

Be, the Be logo, BeBox, BeOS, BeWare, and GeekPort are trademarks of Be, Inc.

Last modified September 6, 1996.