FLTK 1.3.2

- Main Page
- Related Pages
- Modules
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Protected Attributes
Fl_Menu_ Class Reference

Base class of all widgets that have a menu in FLTK. More...

`#include <Fl_Menu_.H>`

Inheritance diagram for Fl_Menu_:

List of all members.

## Public Member Functions
int | add (const char *, int shortcut, Fl_Callback *, void *=0, int=0)
| Adds a new menu item.
int | add (const char *a, const char *b, Fl_Callback *c, void *d=0, int e=0)
| See int Fl_Menu_::add(const char* label, int shortcut, Fl_Callback*, void *user_data=0, int flags=0)
int | add (const char *)
| This is a Forms (and SGI GL library) compatible add function, it adds many menu items, with '|' separating the menu items, and tab separating the menu item names from an optional shortcut string.
void | clear ()
| Same as menu(NULL), set the array pointer to null, indicating a zero-length menu.
int | clear_submenu (int index)
| Clears the specified submenu pointed to by `index` of all menu items.
void | copy (const Fl_Menu_Item *m, void *user_data=0)
| Sets the menu array pointer with a copy of m that will be automatically deleted.
Fl_Boxtype | down_box () const
| This box type is used to surround the currently-selected items in the menus.
void | down_box (Fl_Boxtype b)
| See Fl_Boxtype Fl_Menu_::down_box() const.
Fl_Color | down_color () const
| For back compatibility, same as selection_color()
void | down_color (unsigned c)
| For back compatibility, same as selection_color()
int | find_index (const char *name) const
| Find the menu item index for a given menu `pathname`, such as "Edit/Copy".
int | find_index (const Fl_Menu_Item *item) const
| Find the index the menu array for given `item`.
int | find_index (Fl_Callback *cb) const
| Find the index into the menu array for a given callback `cb`.
const Fl_Menu_Item * | find_item (const char *name)
| Find the menu item for a given menu `pathname`, such as "Edit/Copy".
const Fl_Menu_Item * | find_item (Fl_Callback *)
| Find the menu item for the given callback `cb`.
| Fl_Menu_ (int, int, int, int, const char *=0)
| Creates a new Fl_Menu_ widget using the given position, size, and label string.
void | global ()
| Make the shortcuts for this menu work no matter what window has the focus when you type it.
int | insert (int index, const char *, int shortcut, Fl_Callback *, void *=0, int=0)
| Inserts a new menu item at the specified `index` position.
int | insert (int index, const char *a, const char *b, Fl_Callback *c, void *d=0, int e=0)
| See int Fl_Menu_::insert(const char* label, int shortcut, Fl_Callback*, void *user_data=0, int flags=0)
int | item_pathname (char *name, int namelen, const Fl_Menu_Item *finditem=0) const
| Get the menu 'pathname' for the specified menuitem.
const Fl_Menu_Item * | menu () const
| Returns a pointer to the array of Fl_Menu_Items.
void | menu (const Fl_Menu_Item *m)
| Sets the menu array pointer directly.
void | mode (int i, int fl)
| Sets the flags of item i.
int | mode (int i) const
| Gets the flags of item i.
const Fl_Menu_Item * | mvalue () const
| Returns a pointer to the last menu item that was picked.
const Fl_Menu_Item * | picked (const Fl_Menu_Item *)
| When user picks a menu item, call this.
void | remove (int)
| Deletes item `i` from the menu.
void | replace (int, const char *)
| Changes the text of item `i`.
void | shortcut (int i, int s)
| Changes the shortcut of item i to n.
int | size () const
| This returns the number of Fl_Menu_Item structures that make up the menu, correctly counting submenus.
void | size (int W, int H)
| Changes the size of the widget.
const Fl_Menu_Item * | test_shortcut ()
| Returns the menu item with the entered shortcut (key value).
const char * | text () const
| Returns the title of the last item chosen.
const char * | text (int i) const
| Returns the title of item i.
Fl_Color | textcolor () const
| Get the current color of menu item labels.
void | textcolor (Fl_Color c)
| Sets the current color of menu item labels.
Fl_Font | textfont () const
| Gets the current font of menu item labels.
void | textfont (Fl_Font c)
| Sets the current font of menu item labels.
Fl_Fontsize | textsize () const
| Gets the font size of menu item labels.
void | textsize (Fl_Fontsize c)
| Sets the font size of menu item labels.
int | value () const
| Returns the index into menu() of the last item chosen by the user.
int | value (const Fl_Menu_Item *)
| The value is the index into menu() of the last item chosen by the user.
int | value (int i)
| The value is the index into menu() of the last item chosen by the user.
## Protected Attributes
uchar | alloc
uchar | down_box_
Fl_Color | textcolor_
Fl_Font | textfont_
Fl_Fontsize | textsize_

## Detailed Description

Base class of all widgets that have a menu in FLTK.

Currently FLTK provides you with Fl_Menu_Button, Fl_Menu_Bar, and Fl_Choice.

The class contains a pointer to an array of structures of type Fl_Menu_Item. The array may either be supplied directly by the user program, or it may be "private": a dynamically allocated array managed by the Fl_Menu_.

## Constructor & Destructor Documentation

Fl_Menu_::Fl_Menu_ | ( | int | X,
|  | int | Y,
|  | int | W,
|  | int | H,
|  | const char * | l = `0`
| ) |  |

Creates a new Fl_Menu_ widget using the given position, size, and label string.

menu() is initialized to null.

## Member Function Documentation

int Fl_Menu_::add | ( | const char * | label,
|  | int | shortcut,
|  | Fl_Callback * | callback,
|  | void * | userdata = `0`,
|  | int | flags = `0`
| ) |  |

Adds a new menu item.

Parameters:

  [in] | label | The text label for the menu item.
  [in] | shortcut | Optional keyboard shortcut that can be an int or string; (FL_CTRL+'a') or "^a". Default 0 if none.
  [in] | callback | Optional callback invoked when user clicks the item. Default 0 if none.
  [in] | userdata | Optional user data passed as an argument to the callback. Default 0 if none.
  [in] | flags | Optional flags that control the type of menu item; see below. Default is 0 for none.

Returns:
  The index into the menu() array, where the entry was added.

Description
  If the menu array was directly set with menu(x), then copy() is done to make a private array.

  Since this method can change the internal menu array, any menu item pointers or indecies the application may have cached can become stale, and should be recalculated/refreshed.

  A menu item's callback must not add() items to its parent menu during the callback.

Detailed Description of Parameters

label
  The menu item's label. This option is required.

  The characters "&", "/", "\", and "_" are treated as special characters in the label string. The "&" character specifies that the following character is an accelerator and will be underlined. The "\" character is used to escape the next character in the string. Labels starting with the "_" character cause a divider to be placed after that menu item.

  A label of the form "File/Quit" will create the submenu "File" with a menu item called "Quit". The "/" character is ignored if it appears as the first character of the label string, e.g. "/File/Quit".

  The label string is copied to new memory and can be freed. The other arguments (including the shortcut) are copied into the menu item unchanged.

  If an item exists already with that name then it is replaced with this new one. Otherwise this new one is added to the end of the correct menu or submenu. The return value is the offset into the array that the new entry was placed at.

shortcut
  The keyboard shortcut for this menu item.

  This parameter is optional, and defaults to 0 to indicate no shortcut.

  The shortcut can either be a raw integer value (eg. FL_CTRL+'A') or a string (eg. "^c" or "^97").

  Raw integer shortcuts can be a combination of keyboard chars (eg. 'A') and optional keyboard modifiers (see Fl::event_state(), e.g. FL_SHIFT, etc). In addition, FL_COMMAND can be used to denote FL_META under Mac OS X and FL_CTRL under other platforms.

  String shortcuts can be specified in one of two ways:

         [#+^]<ascii_value>    e.g. "97", "^97", "+97", "#97"
         [#+^]<ascii_char>     e.g. "a", "^a", "+a", "#a"

  ..where <ascii_value> is a decimal value representing an ascii character (eg. 97 is the ascii code for 'a'), and the optional prefixes enhance the value that follows. Multiple prefixes must appear in the order below.

         # - Alt
         + - Shift
         ^ - Control

  Internally, the text shortcuts are converted to integer values using fl_old_shortcut(const char*).

callback
  The callback to invoke when this menu item is selected.

  This parameter is optional, and defaults to 0 for no callback.

userdata
  The callback's 'user data' that is passed to the callback.

  This parameter is optional, and defaults to 0.

flags
  These are bit flags to define what kind of menu item this is.

  This parameter is optional, and defaults to 0 to define a 'regular' menu item.

  These flags can be 'OR'ed together:

            FL_MENU_INACTIVE     // Deactivate menu item (gray out)
            FL_MENU_TOGGLE       // Item is a checkbox toggle (shows checkbox for on/off state)
            FL_MENU_VALUE        // The on/off state for checkbox/radio buttons (if set, state is 'on')
            FL_MENU_RADIO        // Item is a radio button (one checkbox of many can be on)
            FL_MENU_INVISIBLE    // Item will not show up (shortcut will work)
            FL_SUBMENU_POINTER   // Indicates user_data() is a pointer to another menu array
            FL_SUBMENU           // This item is a submenu to other items
            FL_MENU_DIVIDER      // Creates divider line below this item. Also ends a group of radio buttons.

Todo:
  Raw integer shortcut needs examples. Dependent on responses to http://fltk.org/newsgroups.php?gfltk.development+v:10086 and results of STR#2344

Reimplemented in Fl_Sys_Menu_Bar.

int Fl_Menu_::add | ( | const char * | str | ) |

This is a Forms (and SGI GL library) compatible add function, it adds many menu items, with '|' separating the menu items, and tab separating the menu item names from an optional shortcut string.

The passed string is split at any '|' characters and then add(s,0,0,0,0) is done with each section. This is often useful if you are just using the value, and is compatible with Forms and other GL programs. The section strings use the same special characters as described for the long version of add().

No items must be added to a menu during a callback to the same menu.

Parameters:

  str | string containing multiple menu labels as described above

Returns:
  the index into the menu() array, where the entry was added

void Fl_Menu_::clear | ( |  | ) |

Same as menu(NULL), set the array pointer to null, indicating a zero-length menu.

Menus must not be cleared during a callback to the same menu.

Reimplemented in Fl_Sys_Menu_Bar.

int Fl_Menu_::clear_submenu | ( | int | index | ) |

Clears the specified submenu pointed to by `index` of all menu items.

This method is useful for clearing a submenu so that it can be re-populated with new items. Example: a "File/Recent Files/..." submenu that shows the last few files that have been opened.

The specified `index` must point to a submenu.

The submenu is cleared with remove(). If the menu array was directly set with menu(x), then copy() is done to make a private array.

Warning:
  Since this method can change the internal menu array, any menu item pointers or indecies the application may have cached can become stale, and should be recalculated/refreshed.

Example:

       int index = menubar->find_index("File/Recent");    // get index of "File/Recent" submenu
       if ( index != -1 ) menubar->clear_submenu(index);  // clear the submenu
       menubar->add("File/Recent/Aaa");
       menubar->add("File/Recent/Bbb");
       [..]

Parameters:

  index | The index of the submenu to be cleared

Returns:
  0 on success, -1 if the index is out of range or not a submenu

See also:
  remove(int)

Reimplemented in Fl_Sys_Menu_Bar.

void Fl_Menu_::copy | ( | const Fl_Menu_Item * | m,
|  | void * | ud = `0`
| ) |  |

Sets the menu array pointer with a copy of m that will be automatically deleted.

If userdata `ud` is not NULL, then all user data pointers are changed in the menus as well. See void Fl_Menu_::menu(const Fl_Menu_Item* m).

Fl_Boxtype Fl_Menu_::down_box | ( |  | ) | const`[inline]`

This box type is used to surround the currently-selected items in the menus.

If this is FL_NO_BOX then it acts like FL_THIN_UP_BOX and selection_color() acts like FL_WHITE, for back compatibility.

int Fl_Menu_::find_index | ( | const char * | pathname | ) | const

Find the menu item index for a given menu `pathname`, such as "Edit/Copy".

This method finds a menu item's index position for the given menu pathname, also traversing submenus, but not submenu pointers.

To get the menu item pointer for a pathname, use find_item()

Parameters:

  pathname | The path and name of the menu item index to find

Returns:
  The index of the matching item, or -1 if not found.

See also:
  item_pathname()

int Fl_Menu_::find_index | ( | const Fl_Menu_Item * | item | ) | const

Find the index the menu array for given `item`.

A way to convert a menu item pointer into an index.

Current implementation is fast and not expensive.

       // Convert an index-to-item
       int index = 12;
       const Fl_Menu_Item *item = mymenu->menu() + index;

       // Convert an item-to-index
       int index = mymenu->find_index(item);
       if ( index == -1 ) { ..error.. }

Parameters:

  item | The *item to be found

Returns:
  The index of the item, or -1 if not found.

See also:
  menu()

int Fl_Menu_::find_index | ( | Fl_Callback * | cb | ) | const

Find the index into the menu array for a given callback `cb`.

This method finds a menu item's index position, also traversing submenus, but not submenu pointers. This is useful if an application uses internationalisation and a menu item can not be found using its label. This search is also much faster.

Parameters:

  cb | Find the first item with this callback

Returns:
  The index of the item with the specific callback, or -1 if not found

See also:
  find_index(const char*)

const Fl_Menu_Item * Fl_Menu_::find_item | ( | const char * | pathname | ) |

Find the menu item for a given menu `pathname`, such as "Edit/Copy".

This method finds a menu item in the menu array, also traversing submenus, but not submenu pointers.

To get the menu item's index, use find_index(const char*)

Example:

        Fl_Menu_Bar *menubar = new Fl_Menu_Bar(..);
        menubar->add("File/&Open");
        menubar->add("File/&Save");
        menubar->add("Edit/&Copy");
        // [..]
        Fl_Menu_Item *item;
        if ( ( item = (Fl_Menu_Item*)menubar->find_item("File/&Open") ) != NULL ) {
            item->labelcolor(FL_RED);
        }
        if ( ( item = (Fl_Menu_Item*)menubar->find_item("Edit/&Copy") ) != NULL ) {
            item->labelcolor(FL_GREEN);
        }

Parameters:

  pathname | The path and name of the menu item

Returns:
  The item found, or NULL if not found

See also:
  find_index(const char*), find_item(Fl_Callback*), item_pathname()

const Fl_Menu_Item * Fl_Menu_::find_item | ( | Fl_Callback * | cb | ) |

Find the menu item for the given callback `cb`.

This method finds a menu item in a menu array, also traversing submenus, but not submenu pointers. This is useful if an application uses internationalisation and a menu item can not be found using its label. This search is also much faster.

Parameters:

  cb | find the first item with this callback

Returns:
  The item found, or NULL if not found

See also:
  find_item(const char*)

void Fl_Menu_::global | ( |  | ) |

Make the shortcuts for this menu work no matter what window has the focus when you type it.

This is done by using Fl::add_handler(). This Fl_Menu_ widget does not have to be visible (ie the window it is in can be hidden, or it does not have to be put in a window at all).

Currently there can be only one global()menu. Setting a new one will replace the old one. There is no way to remove the global() setting (so don't destroy the widget!)

int Fl_Menu_::insert | ( | int | index,
|  | const char * | label,
|  | int | shortcut,
|  | Fl_Callback * | callback,
|  | void * | userdata = `0`,
|  | int | flags = `0`
| ) |  |

Inserts a new menu item at the specified `index` position.

If `index` is -1, the menu item is appended; same behavior as add().

To properly insert a menu item, `label` must be the name of the item (eg. "Quit"), and not a 'menu pathname' (eg. "File/Quit"). If a menu pathname is specified, the value of `index` is ignored, the new item's position defined by the pathname.

For more details, see add(). Except for the `index` parameter, add() has more detailed information on parameters and behavior, and is functionally equivalent.

Parameters:

  [in] | index | The menu array's index position where the new item is inserted. If -1, behavior is the same as add().
  [in] | label | The text label for the menu item. If the label is a menu pathname, `index` is ignored, and the pathname indicates the position of the new item.
  [in] | shortcut | Optional keyboard shortcut. Can be an int (FL_CTRL+'a') or a string ("^a"). Default is 0.
  [in] | callback | Optional callback invoked when user clicks the item. Default 0 if none.
  [in] | userdata | Optional user data passed as an argument to the callback. Default 0 if none.
  [in] | flags | Optional flags that control the type of menu item; see add() for more info. Default is 0 for none.

Returns:
  The index into the menu() array, where the entry was added.

See also:
  add()

Reimplemented in Fl_Sys_Menu_Bar.

int Fl_Menu_::item_pathname | ( | char * | name,
|  | int | namelen,
|  | const Fl_Menu_Item * | finditem = `0`
| ) |  | const

Get the menu 'pathname' for the specified menuitem.

If finditem==NULL, mvalue() is used (the most recently picked menuitem).

Example:

          Fl_Menu_Bar *menubar = 0;
          void my_menu_callback(Fl_Widget*,void*) {
            char name[80];
            if ( menubar->item_pathname(name, sizeof(name)-1) == 0 ) {   // recently picked item
              if ( strcmp(name, "File/&Open") == 0 ) { .. }              // open invoked
              if ( strcmp(name, "File/&Save") == 0 ) { .. }              // save invoked
              if ( strcmp(name, "Edit/&Copy") == 0 ) { .. }              // copy invoked
            }
          }
          int main() {
            [..]
            menubar = new Fl_Menu_Bar(..);
            menubar->add("File/&Open",  0, my_menu_callback);
            menubar->add("File/&Save",  0, my_menu_callback);
            menubar->add("Edit/&Copy",  0, my_menu_callback);
            [..]
          }

Returns:
  - 0 : OK (name has menuitem's pathname)
  - -1 : item not found (name="")
  - -2 : 'name' not large enough (name="")

See also:
  find_item()

const Fl_Menu_Item* Fl_Menu_::menu | ( |  | ) | const`[inline]`

Returns a pointer to the array of Fl_Menu_Items.

This will either be the value passed to menu(value) or the private copy.

See also:
  size() -- returns the size of the Fl_Menu_Item array.

Example: How to walk the array:

        for ( int t=0; t<menubar->size(); t++ ) {                // walk array of items
            const Fl_Menu_Item &item = menubar->menu()[t];       // get each item
            fprintf(stderr, "item #%d -- label=%s, value=%s type=%s\n",
                t,
                item.label() ? item.label() : "(Null)",          // menu terminators have NULL labels
                (item.flags & FL_MENU_VALUE) ? "set" : "clear",  // value of toggle or radio items
                (item.flags & FL_SUBMENU) ? "Submenu" : "Item"); // see if item is a submenu or actual item
        }

Reimplemented in Fl_Sys_Menu_Bar.

void Fl_Menu_::menu | ( | const Fl_Menu_Item * | m | ) |

Sets the menu array pointer directly.

If the old menu is private it is deleted. NULL is allowed and acts the same as a zero-length menu. If you try to modify the array (with add(), replace(), or remove()) a private copy is automatically done.

Reimplemented in Fl_Sys_Menu_Bar.

void Fl_Menu_::mode | ( | int | i,
|  | int | fl
| ) |  | `[inline]`

Sets the flags of item i.

For a list of the flags, see Fl_Menu_Item.

int Fl_Menu_::mode | ( | int | i | ) | const`[inline]`

Gets the flags of item i.

For a list of the flags, see Fl_Menu_Item.

const Fl_Menu_Item* Fl_Menu_::mvalue | ( |  | ) | const`[inline]`

Returns a pointer to the last menu item that was picked.

const Fl_Menu_Item * Fl_Menu_::picked | ( | const Fl_Menu_Item * | v | ) |

When user picks a menu item, call this.

It will do the callback. Unfortunately this also casts away const for the checkboxes, but this was necessary so non-checkbox menus can really be declared const...

void Fl_Menu_::remove | ( | int | i | ) |

Deletes item `i` from the menu.

If the menu array was directly set with menu(x) then copy() is done to make a private array.

No items must be removed from a menu during a callback to the same menu.

Parameters:

  i | index into menu array

Reimplemented in Fl_Sys_Menu_Bar.

void Fl_Menu_::replace | ( | int | i,
|  | const char * | str
| ) |  |

Changes the text of item `i`.

This is the only way to get slash into an add()'ed menu item. If the menu array was directly set with menu(x) then copy() is done to make a private array.

Parameters:

  i | index into menu array
  str | new label for menu item at index i

Reimplemented in Fl_Sys_Menu_Bar.

void Fl_Menu_::shortcut | ( | int | i,
|  | int | s
| ) |  | `[inline]`

Changes the shortcut of item i to n.

int Fl_Menu_::size | ( |  | ) | const

This returns the number of Fl_Menu_Item structures that make up the menu, correctly counting submenus.

This includes the "terminator" item at the end. To copy a menu array you need to copy size()*sizeof(Fl_Menu_Item) bytes. If the menu is NULL this returns zero (an empty menu will return 1).

void Fl_Menu_::size | ( | int | W,
|  | int | H
| ) |  | `[inline]`

Changes the size of the widget.

size(W, H) is a shortcut for resize(x(), y(), W, H).

Parameters:

  [in] | W,H | new size

See also:
  position(int,int), resize(int,int,int,int)

Reimplemented from Fl_Widget.

const Fl_Menu_Item* Fl_Menu_::test_shortcut | ( |  | ) | `[inline]`

Returns the menu item with the entered shortcut (key value).

This searches the complete menu() for a shortcut that matches the entered key value. It must be called for a FL_KEYBOARD or FL_SHORTCUT event.

If a match is found, the menu's callback will be called.

Returns:
  matched Fl_Menu_Item or NULL.

Reimplemented from Fl_Widget.

const char* Fl_Menu_::text | ( |  | ) | const`[inline]`

Returns the title of the last item chosen.

const char* Fl_Menu_::text | ( | int | i | ) | const`[inline]`

Returns the title of item i.

Fl_Color Fl_Menu_::textcolor | ( |  | ) | const`[inline]`

Get the current color of menu item labels.

void Fl_Menu_::textcolor | ( | Fl_Color | c | ) | `[inline]`

Sets the current color of menu item labels.

Fl_Font Fl_Menu_::textfont | ( |  | ) | const`[inline]`

Gets the current font of menu item labels.

void Fl_Menu_::textfont | ( | Fl_Font | c | ) | `[inline]`

Sets the current font of menu item labels.

Fl_Fontsize Fl_Menu_::textsize | ( |  | ) | const`[inline]`

Gets the font size of menu item labels.

void Fl_Menu_::textsize | ( | Fl_Fontsize | c | ) | `[inline]`

Sets the font size of menu item labels.

int Fl_Menu_::value | ( |  | ) | const`[inline]`

Returns the index into menu() of the last item chosen by the user.

It is zero initially.

Reimplemented in Fl_Choice.

int Fl_Menu_::value | ( | const Fl_Menu_Item * | m | ) |

The value is the index into menu() of the last item chosen by the user.

It is zero initially. You can set it as an integer, or set it with a pointer to a menu item. The set routines return non-zero if the new value is different than the old one.

Reimplemented in Fl_Choice.

int Fl_Menu_::value | ( | int | i | ) | `[inline]`

The value is the index into menu() of the last item chosen by the user.

It is zero initially. You can set it as an integer, or set it with a pointer to a menu item. The set routines return non-zero if the new value is different than the old one.

Reimplemented in Fl_Choice.

The documentation for this class was generated from the following files:

- Fl_Menu_.H
- Fl_Menu_.cxx
- Fl_Menu_add.cxx
- Fl_Menu_global.cxx