Fl_Menu_Item.H 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // "$Id: Fl_Menu_Item.H 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Menu item header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #ifndef Fl_Menu_Item_H
  28. #define Fl_Menu_Item_H
  29. # include "Fl_Widget.H"
  30. # include "Fl_Image.H"
  31. # if defined(__APPLE__) && defined(check)
  32. # undef check
  33. # endif
  34. enum { // values for flags:
  35. FL_MENU_INACTIVE = 1, ///< Deactivate menu item (gray out)
  36. FL_MENU_TOGGLE= 2, ///< Item is a checkbox toggle (shows checkbox for on/off state)
  37. FL_MENU_VALUE = 4, ///< The on/off state for checkbox/radio buttons (if set, state is 'on')
  38. FL_MENU_RADIO = 8, ///< Item is a radio button (one checkbox of many can be on)
  39. FL_MENU_INVISIBLE = 0x10, ///< Item will not show up (shortcut will work)
  40. FL_SUBMENU_POINTER = 0x20, ///< Indicates user_data() is a pointer to another menu array
  41. FL_SUBMENU = 0x40, ///< This item is a submenu to other items
  42. FL_MENU_DIVIDER = 0x80, ///< Creates divider line below this item. Also ends a group of radio buttons.
  43. FL_MENU_HORIZONTAL = 0x100 ///< ??? -- reserved
  44. };
  45. extern FL_EXPORT Fl_Shortcut fl_old_shortcut(const char*);
  46. class Fl_Menu_;
  47. /**
  48. The Fl_Menu_Item structure defines a single menu item that
  49. is used by the Fl_Menu_ class.
  50. \code
  51. struct Fl_Menu_Item {
  52. const char* text; // label()
  53. ulong shortcut_;
  54. Fl_Callback* callback_;
  55. void* user_data_;
  56. int flags;
  57. uchar labeltype_;
  58. uchar labelfont_;
  59. uchar labelsize_;
  60. uchar labelcolor_;
  61. };
  62. enum { // values for flags:
  63. FL_MENU_INACTIVE = 1, // Deactivate menu item (gray out)
  64. FL_MENU_TOGGLE = 2, // Item is a checkbox toggle (shows checkbox for on/off state)
  65. FL_MENU_VALUE = 4, // The on/off state for checkbox/radio buttons (if set, state is 'on')
  66. FL_MENU_RADIO = 8, // Item is a radio button (one checkbox of many can be on)
  67. FL_MENU_INVISIBLE = 0x10, // Item will not show up (shortcut will work)
  68. FL_SUBMENU_POINTER = 0x20, // Indicates user_data() is a pointer to another menu array
  69. FL_SUBMENU = 0x40, // This item is a submenu to other items
  70. FL_MENU_DIVIDER = 0x80, // Creates divider line below this item. Also ends a group of radio buttons.
  71. FL_MENU_HORIZONTAL = 0x100 // ??? -- reserved
  72. };
  73. \endcode
  74. Typically menu items are statically defined; for example:
  75. \code
  76. Fl_Menu_Item popup[] = {
  77. {"&alpha", FL_ALT+'a', the_cb, (void*)1},
  78. {"&beta", FL_ALT+'b', the_cb, (void*)2},
  79. {"gamma", FL_ALT+'c', the_cb, (void*)3, FL_MENU_DIVIDER},
  80. {"&strange", 0, strange_cb},
  81. {"&charm", 0, charm_cb},
  82. {"&truth", 0, truth_cb},
  83. {"b&eauty", 0, beauty_cb},
  84. {"sub&menu", 0, 0, 0, FL_SUBMENU},
  85. {"one"},
  86. {"two"},
  87. {"three"},
  88. {0},
  89. {"inactive", FL_ALT+'i', 0, 0, FL_MENU_INACTIVE|FL_MENU_DIVIDER},
  90. {"invisible",FL_ALT+'i', 0, 0, FL_MENU_INVISIBLE},
  91. {"check", FL_ALT+'i', 0, 0, FL_MENU_TOGGLE|FL_MENU_VALUE},
  92. {"box", FL_ALT+'i', 0, 0, FL_MENU_TOGGLE},
  93. {0}};
  94. \endcode
  95. produces:
  96. \image html menu.gif
  97. \image latex menu.eps "menu" width=10cm
  98. A submenu title is identified by the bit FL_SUBMENU in the
  99. flags field, and ends with a label() that is NULL.
  100. You can nest menus to any depth. A pointer to the first item in the
  101. submenu can be treated as an Fl_Menu array itself. It is also
  102. possible to make separate submenu arrays with FL_SUBMENU_POINTER flags.
  103. You should use the method functions to access structure members and
  104. not access them directly to avoid compatibility problems with future
  105. releases of FLTK.
  106. */
  107. struct FL_EXPORT Fl_Menu_Item {
  108. const char *text; ///< menu item text, returned by label()
  109. int shortcut_; ///< menu item shortcut
  110. Fl_Callback *callback_; ///< menu item callback
  111. void *user_data_; ///< menu item user_data for 3rd party apps
  112. int flags; ///< menu item flags like FL_MENU_TOGGLE, FL_MENU_RADIO
  113. uchar labeltype_; ///< how the menu item text looks like
  114. Fl_Font labelfont_; ///< which font for this menu item text
  115. Fl_Fontsize labelsize_; ///< size of menu item text
  116. Fl_Color labelcolor_; ///< menu item text color
  117. // advance N items, skipping submenus:
  118. const Fl_Menu_Item *next(int=1) const;
  119. /**
  120. Advances a pointer by n items through a menu array, skipping
  121. the contents of submenus and invisible items. There are two calls so
  122. that you can advance through const and non-const data.
  123. */
  124. Fl_Menu_Item *next(int i=1) {
  125. return (Fl_Menu_Item*)(((const Fl_Menu_Item*)this)->next(i));}
  126. /** Returns the first menu item, same as next(0). */
  127. const Fl_Menu_Item *first() const { return next(0); }
  128. /** Returns the first menu item, same as next(0). */
  129. Fl_Menu_Item *first() { return next(0); }
  130. // methods on menu items:
  131. /**
  132. Returns the title of the item.
  133. A NULL here indicates the end of the menu (or of a submenu).
  134. A '&' in the item will print an underscore under the next letter,
  135. and if the menu is popped up that letter will be a "shortcut" to pick
  136. that item. To get a real '&' put two in a row.
  137. */
  138. const char* label() const {return text;}
  139. /** See const char* Fl_Menu_Item::label() const */
  140. void label(const char* a) {text=a;}
  141. /** See const char* Fl_Menu_Item::label() const */
  142. void label(Fl_Labeltype a,const char* b) {labeltype_ = a; text = b;}
  143. /**
  144. A labeltype identifies a routine that draws the label of the
  145. widget. This can be used for special effects such as emboss, or to use
  146. the label() pointer as another form of data such as a bitmap.
  147. The value FL_NORMAL_LABEL prints the label as text.
  148. */
  149. Fl_Labeltype labeltype() const {return (Fl_Labeltype)labeltype_;}
  150. /**
  151. A labeltype identifies a routine that draws the label of the
  152. widget. This can be used for special effects such as emboss, or to use
  153. the label() pointer as another form of data such as a bitmap.
  154. The value FL_NORMAL_LABEL prints the label as text.
  155. */
  156. void labeltype(Fl_Labeltype a) {labeltype_ = a;}
  157. /**
  158. This color is passed to the labeltype routine, and is typically the
  159. color of the label text. This defaults to FL_BLACK. If this
  160. color is not black fltk will <I>not</I> use overlay bitplanes to draw
  161. the menu - this is so that images put in the menu draw correctly.
  162. */
  163. Fl_Color labelcolor() const {return labelcolor_;}
  164. /** See Fl_Color Fl_Menu_Item::labelcolor() const */
  165. void labelcolor(Fl_Color a) {labelcolor_ = a;}
  166. /**
  167. Fonts are identified by small 8-bit indexes into a table. See the
  168. enumeration list for predefined fonts. The default value is a
  169. Helvetica font. The function Fl::set_font() can define new fonts.
  170. */
  171. Fl_Font labelfont() const {return labelfont_;}
  172. /**
  173. Fonts are identified by small 8-bit indexes into a table. See the
  174. enumeration list for predefined fonts. The default value is a
  175. Helvetica font. The function Fl::set_font() can define new fonts.
  176. */
  177. void labelfont(Fl_Font a) {labelfont_ = a;}
  178. /** Gets the label font pixel size/height.*/
  179. Fl_Fontsize labelsize() const {return labelsize_;}
  180. /** Sets the label font pixel size/height.*/
  181. void labelsize(Fl_Fontsize a) {labelsize_ = a;}
  182. /**
  183. Each item has space for a callback function and an argument for that
  184. function. Due to back compatibility, the Fl_Menu_Item itself
  185. is not passed to the callback, instead you have to get it by calling
  186. ((Fl_Menu_*)w)->mvalue() where w is the widget argument.
  187. */
  188. Fl_Callback_p callback() const {return callback_;}
  189. /** See Fl_Callback_p Fl_MenuItem::callback() const */
  190. void callback(Fl_Callback* c, void* p) {callback_=c; user_data_=p;}
  191. /** See Fl_Callback_p Fl_MenuItem::callback() const */
  192. void callback(Fl_Callback* c) {callback_=c;}
  193. /** See Fl_Callback_p Fl_MenuItem::callback() const */
  194. void callback(Fl_Callback0*c) {callback_=(Fl_Callback*)c;}
  195. /** See Fl_Callback_p Fl_MenuItem::callback() const */
  196. void callback(Fl_Callback1*c, long p=0) {callback_=(Fl_Callback*)c; user_data_=(void*)p;}
  197. /**
  198. Get or set the user_data argument that is sent to the
  199. callback function.
  200. */
  201. void* user_data() const {return user_data_;}
  202. /**
  203. Get or set the user_data argument that is sent to the
  204. callback function.
  205. */
  206. void user_data(void* v) {user_data_ = v;}
  207. /**
  208. For convenience you can also define the callback as taking a long
  209. argument. This is implemented by casting this to a Fl_Callback
  210. and casting the long to a void* and may not be
  211. portable to some machines.
  212. */
  213. long argument() const {return (long)user_data_;}
  214. /**
  215. For convenience you can also define the callback as taking a long
  216. argument. This is implemented by casting this to a Fl_Callback
  217. and casting the long to a void* and may not be
  218. portable to some machines.
  219. */
  220. void argument(long v) {user_data_ = (void*)v;}
  221. /** Gets what key combination shortcut will trigger the menu item. */
  222. int shortcut() const {return shortcut_;}
  223. /**
  224. Sets exactly what key combination will trigger the menu item. The
  225. value is a logical 'or' of a key and a set of shift flags, for instance
  226. FL_ALT+'a' or FL_ALT+FL_F+10 or just 'a'. A value of
  227. zero disables the shortcut.
  228. The key can be any value returned by Fl::event_key(), but will usually
  229. be an ASCII letter. Use a lower-case letter unless you require the shift
  230. key to be held down.
  231. The shift flags can be any set of values accepted by Fl::event_state().
  232. If the bit is on that shift key must be pushed. Meta, Alt, Ctrl,
  233. and Shift must be off if they are not in the shift flags (zero for the
  234. other bits indicates a "don't care" setting).
  235. */
  236. void shortcut(int s) {shortcut_ = s;}
  237. /**
  238. Returns true if either FL_SUBMENU or FL_SUBMENU_POINTER
  239. is on in the flags. FL_SUBMENU indicates an embedded submenu
  240. that goes from the next item through the next one with a NULL
  241. label(). FL_SUBMENU_POINTER indicates that user_data()
  242. is a pointer to another menu array.
  243. */
  244. int submenu() const {return flags&(FL_SUBMENU|FL_SUBMENU_POINTER);}
  245. /**
  246. Returns true if a checkbox will be drawn next to this item. This is
  247. true if FL_MENU_TOGGLE or FL_MENU_RADIO is set in the flags.
  248. */
  249. int checkbox() const {return flags&FL_MENU_TOGGLE;}
  250. /**
  251. Returns true if this item is a radio item. When a radio button is
  252. selected all "adjacent" radio buttons are turned off. A set of radio
  253. items is delimited by an item that has radio() false, or by an
  254. item with FL_MENU_DIVIDER turned on.
  255. */
  256. int radio() const {return flags&FL_MENU_RADIO;}
  257. /** Returns the current value of the check or radio item. */
  258. int value() const {return flags&FL_MENU_VALUE;}
  259. /**
  260. Turns the check or radio item "on" for the menu item. Note that this
  261. does not turn off any adjacent radio items like set_only() does.
  262. */
  263. void set() {flags |= FL_MENU_VALUE;}
  264. /** Turns the check or radio item "off" for the menu item. */
  265. void clear() {flags &= ~FL_MENU_VALUE;}
  266. void setonly();
  267. /** Gets the visibility of an item. */
  268. int visible() const {return !(flags&FL_MENU_INVISIBLE);}
  269. /** Makes an item visible in the menu. */
  270. void show() {flags &= ~FL_MENU_INVISIBLE;}
  271. /** Hides an item in the menu. */
  272. void hide() {flags |= FL_MENU_INVISIBLE;}
  273. /** Gets whether or not the item can be picked. */
  274. int active() const {return !(flags&FL_MENU_INACTIVE);}
  275. /** Allows a menu item to be picked. */
  276. void activate() {flags &= ~FL_MENU_INACTIVE;}
  277. /**
  278. Prevents a menu item from being picked. Note that this will also cause
  279. the menu item to appear grayed-out.
  280. */
  281. void deactivate() {flags |= FL_MENU_INACTIVE;}
  282. /** Returns non 0 if FL_INACTIVE and FL_INVISIBLE are cleared, 0 otherwise. */
  283. int activevisible() const {return !(flags & (FL_MENU_INACTIVE|FL_MENU_INVISIBLE));}
  284. // compatibility for FLUID so it can set the image of a menu item...
  285. /** compatibility api for FLUID, same as a->label(this) */
  286. void image(Fl_Image* a) {a->label(this);}
  287. /** compatibility api for FLUID, same as a.label(this) */
  288. void image(Fl_Image& a) {a.label(this);}
  289. // used by menubar:
  290. int measure(int* h, const Fl_Menu_*) const;
  291. void draw(int x, int y, int w, int h, const Fl_Menu_*, int t=0) const;
  292. // popup menus without using an Fl_Menu_ widget:
  293. const Fl_Menu_Item* popup(
  294. int X, int Y,
  295. const char *title = 0,
  296. const Fl_Menu_Item* picked=0,
  297. const Fl_Menu_* = 0) const;
  298. const Fl_Menu_Item* pulldown(
  299. int X, int Y, int W, int H,
  300. const Fl_Menu_Item* picked = 0,
  301. const Fl_Menu_* = 0,
  302. const Fl_Menu_Item* title = 0,
  303. int menubar=0) const;
  304. const Fl_Menu_Item* test_shortcut() const;
  305. const Fl_Menu_Item* find_shortcut(int *ip=0, const bool require_alt = false) const;
  306. /**
  307. Calls the Fl_Menu_Item item's callback, and provides the
  308. Fl_Widget argument (and optionally overrides the user_data()
  309. argument). You must first check that callback() is non-zero
  310. before calling this.
  311. */
  312. void do_callback(Fl_Widget* o) const {callback_(o, user_data_);}
  313. /**
  314. Calls the Fl_Menu_Item item's callback, and provides the
  315. Fl_Widget argument (and optionally overrides the user_data()
  316. argument). You must first check that callback() is non-zero
  317. before calling this.
  318. */
  319. void do_callback(Fl_Widget* o,void* arg) const {callback_(o, arg);}
  320. /**
  321. Calls the Fl_Menu_Item item's callback, and provides the
  322. Fl_Widget argument (and optionally overrides the user_data()
  323. argument). You must first check that callback() is non-zero
  324. before calling this.
  325. */
  326. void do_callback(Fl_Widget* o,long arg) const {callback_(o, (void*)arg);}
  327. // back-compatibility, do not use:
  328. /** back compatibility only \deprecated. */
  329. int checked() const {return flags&FL_MENU_VALUE;}
  330. /** back compatibility only \deprecated. */
  331. void check() {flags |= FL_MENU_VALUE;}
  332. /** back compatibility only \deprecated. */
  333. void uncheck() {flags &= ~FL_MENU_VALUE;}
  334. int insert(int,const char*,int,Fl_Callback*,void* =0, int =0);
  335. int add(const char*, int shortcut, Fl_Callback*, void* =0, int = 0);
  336. /** See int add(const char*, int shortcut, Fl_Callback*, void*, int) */
  337. int add(const char*a, const char* b, Fl_Callback* c,
  338. void* d = 0, int e = 0) {
  339. return add(a,fl_old_shortcut(b),c,d,e);}
  340. int size() const ;
  341. };
  342. typedef Fl_Menu_Item Fl_Menu; // back compatibility
  343. enum { // back-compatibility enum:
  344. FL_PUP_NONE = 0,
  345. FL_PUP_GREY = FL_MENU_INACTIVE,
  346. FL_PUP_GRAY = FL_MENU_INACTIVE,
  347. FL_MENU_BOX = FL_MENU_TOGGLE,
  348. FL_PUP_BOX = FL_MENU_TOGGLE,
  349. FL_MENU_CHECK = FL_MENU_VALUE,
  350. FL_PUP_CHECK = FL_MENU_VALUE,
  351. FL_PUP_RADIO = FL_MENU_RADIO,
  352. FL_PUP_INVISIBLE = FL_MENU_INVISIBLE,
  353. FL_PUP_SUBMENU = FL_SUBMENU_POINTER
  354. };
  355. #endif
  356. //
  357. // End of "$Id: Fl_Menu_Item.H 7903 2010-11-28 21:06:39Z matt $".
  358. //