1 //
2 // "$Id$"
3 //
4 // Menu base class header file for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2016 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file.  If this
10 // file is missing or damaged, see the license at:
11 //
12 //     http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 //     http://www.fltk.org/str.php
17 //
18 
19 /* \file
20    Fl_Menu_ widget . */
21 
22 #ifndef Fl_Menu__H
23 #define Fl_Menu__H
24 
25 #ifndef Fl_Widget_H
26 #include "Fl_Widget.H"
27 #endif
28 #include "Fl_Menu_Item.H"
29 
30 /**
31   Base class of all widgets that have a menu in FLTK.
32 
33   Currently FLTK provides you with Fl_Menu_Button, Fl_Menu_Bar, and Fl_Choice.
34 
35   The class contains a pointer to an array of structures of type Fl_Menu_Item.
36   The array may either be supplied directly by the user program, or it may
37   be "private": a dynamically allocated array managed by the Fl_Menu_.
38 
39   When the user clicks a menu item, value() is set to that item
40   and then:
41 
42     - If the Fl_Menu_Item has a callback set, that callback
43       is invoked with any userdata configured for it.
44       (The Fl_Menu_ widget's callback is NOT invoked.)
45 
46     - For any Fl_Menu_Items that \b don't have a callback set,
47       the Fl_Menu_ widget's callback is invoked with any userdata
48       configured for it. The callback can determine which item
49       was picked using value(), mvalue(), item_pathname(), etc.
50 */
51 class FL_EXPORT Fl_Menu_ : public Fl_Widget {
52 
53   Fl_Menu_Item *menu_;
54   const Fl_Menu_Item *value_;
55 
56 protected:
57 
58   uchar alloc;			// flag indicates if menu_ is a dynamic copy (=1) or not (=0)
59   uchar down_box_;
60   Fl_Font textfont_;
61   Fl_Fontsize textsize_;
62   Fl_Color textcolor_;
63 
64   int item_pathname_(char *name, int namelen, const Fl_Menu_Item *finditem,
65                      const Fl_Menu_Item *menu=0) const;
66 public:
67   Fl_Menu_(int,int,int,int,const char * =0);
68   ~Fl_Menu_();
69 
70   int item_pathname(char *name, int namelen, const Fl_Menu_Item *finditem=0) const;
71   const Fl_Menu_Item* picked(const Fl_Menu_Item*);
72   const Fl_Menu_Item* find_item(const char *name);
73   const Fl_Menu_Item* find_item(Fl_Callback*);
74   int find_index(const char *name) const;
75   int find_index(const Fl_Menu_Item *item) const;
76   int find_index(Fl_Callback *cb) const;
77 
78   /**
79     Returns the menu item with the entered shortcut (key value).
80 
81     This searches the complete menu() for a shortcut that matches the
82     entered key value.  It must be called for a FL_KEYBOARD or FL_SHORTCUT
83     event.
84 
85     If a match is found, the menu's callback will be called.
86 
87     \return matched Fl_Menu_Item or NULL.
88   */
test_shortcut()89   const Fl_Menu_Item* test_shortcut() {return picked(menu()->test_shortcut());}
90   void global();
91 
92   /**
93     Returns a pointer to the array of Fl_Menu_Items.  This will either be
94     the value passed to menu(value) or the private copy.
95     \sa size() -- returns the size of the Fl_Menu_Item array.
96 
97     \b Example: How to walk the array:
98     \code
99     for ( int t=0; t<menubar->size(); t++ ) {                // walk array of items
100         const Fl_Menu_Item &item = menubar->menu()[t];       // get each item
101         fprintf(stderr, "item #%d -- label=%s, value=%s type=%s\n",
102             t,
103             item.label() ? item.label() : "(Null)",          // menu terminators have NULL labels
104             (item.flags & FL_MENU_VALUE) ? "set" : "clear",  // value of toggle or radio items
105             (item.flags & FL_SUBMENU) ? "Submenu" : "Item"); // see if item is a submenu or actual item
106     }
107     \endcode
108 
109   */
menu()110   const Fl_Menu_Item *menu() const {return menu_;}
111   void menu(const Fl_Menu_Item *m);
112   void copy(const Fl_Menu_Item *m, void* user_data = 0);
113   int insert(int index, const char*, int shortcut, Fl_Callback*, void* = 0, int = 0);
114   int  add(const char*, int shortcut, Fl_Callback*, void* = 0, int = 0); // see src/Fl_Menu_add.cxx
115   /** See int Fl_Menu_::add(const char* label, int shortcut, Fl_Callback*, void *user_data=0, int flags=0) */
116   int  add(const char* a, const char* b, Fl_Callback* c, void* d = 0, int e = 0) {
117       return add(a,fl_old_shortcut(b),c,d,e);
118   }
119   /** See int Fl_Menu_::insert(const char* label, int shortcut, Fl_Callback*, void *user_data=0, int flags=0) */
120   int insert(int index, const char* a, const char* b, Fl_Callback* c, void* d = 0, int e = 0) {
121       return insert(index,a,fl_old_shortcut(b),c,d,e);
122   }
123   int  add(const char *);
124   int  size() const ;
size(int W,int H)125   void size(int W, int H) { Fl_Widget::size(W, H); }
126   void clear();
127   int clear_submenu(int index);
128   void replace(int,const char *);
129   void remove(int);
130   /** Changes the shortcut of item \p i to \p s. */
shortcut(int i,int s)131   void shortcut(int i, int s) {menu_[i].shortcut(s);}
132   /** Sets the flags of item i.  For a list of the flags, see Fl_Menu_Item.  */
mode(int i,int fl)133   void mode(int i,int fl) {menu_[i].flags = fl;}
134   /** Gets the flags of item i.  For a list of the flags, see Fl_Menu_Item.  */
mode(int i)135   int  mode(int i) const {return menu_[i].flags;}
136 
137   /** Returns a pointer to the last menu item that was picked.  */
mvalue()138   const Fl_Menu_Item *mvalue() const {return value_;}
139   /** Returns the index into menu() of the last item chosen by the user.  It is zero initially. */
value()140   int value() const {return value_ ? (int)(value_-menu_) : -1;}
141   int value(const Fl_Menu_Item*);
142   /**
143     The value is the index into menu() of the last item chosen by
144     the user.  It is zero initially.  You can set it as an integer, or set
145     it with a pointer to a menu item.  The set routines return non-zero if
146     the new value is different than the old one.
147   */
value(int i)148   int value(int i) {return value(menu_+i);}
149   /** Returns the title of the last item chosen.  */
text()150   const char *text() const {return value_ ? value_->text : 0;}
151   /** Returns the title of item i.  */
text(int i)152   const char *text(int i) const {return menu_[i].text;}
153 
154   /** Gets the current font of menu item labels.  */
textfont()155   Fl_Font textfont() const {return textfont_;}
156   /**  Sets the current font of menu item labels.  */
textfont(Fl_Font c)157   void textfont(Fl_Font c) {textfont_=c;}
158   /**  Gets the font size of menu item labels.  */
textsize()159   Fl_Fontsize textsize() const {return textsize_;}
160   /**  Sets the font size of menu item labels.  */
textsize(Fl_Fontsize c)161   void textsize(Fl_Fontsize c) {textsize_=c;}
162   /** Get the current color of menu item labels.  */
textcolor()163   Fl_Color textcolor() const {return textcolor_;}
164   /** Sets the current color of menu item labels. */
textcolor(Fl_Color c)165   void textcolor(Fl_Color c) {textcolor_=c;}
166 
167   /**
168     This box type is used to surround the currently-selected items in the
169     menus.  If this is FL_NO_BOX then it acts like
170     FL_THIN_UP_BOX and selection_color() acts like
171     FL_WHITE, for back compatibility.
172   */
down_box()173   Fl_Boxtype down_box() const {return (Fl_Boxtype)down_box_;}
174   /**    See Fl_Boxtype Fl_Menu_::down_box() const   */
down_box(Fl_Boxtype b)175   void down_box(Fl_Boxtype b) {down_box_ = b;}
176 
177   /** For back compatibility, same as selection_color() */
down_color()178   Fl_Color down_color() const {return selection_color();}
179   /** For back compatibility, same as selection_color() */
down_color(unsigned c)180   void down_color(unsigned c) {selection_color(c);}
181   void setonly(Fl_Menu_Item* item);
182 };
183 
184 #endif
185 
186 //
187 // End of "$Id$".
188 //
189