You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
16KB

  1. //
  2. // "$Id: Fl_Menu_Item.H 7983 2010-12-09 00:04:06Z AlbrechtS $"
  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.png
  97. \image latex menu.png "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 the menu's callback
  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. Returns the menu item's labeltype.
  145. A labeltype identifies a routine that draws the label of the
  146. widget. This can be used for special effects such as emboss, or to use
  147. the label() pointer as another form of data such as a bitmap.
  148. The value FL_NORMAL_LABEL prints the label as text.
  149. */
  150. Fl_Labeltype labeltype() const {return (Fl_Labeltype)labeltype_;}
  151. /**
  152. Sets the menu item's labeltype.
  153. A labeltype identifies a routine that draws the label of the
  154. widget. This can be used for special effects such as emboss, or to use
  155. the label() pointer as another form of data such as a bitmap.
  156. The value FL_NORMAL_LABEL prints the label as text.
  157. */
  158. void labeltype(Fl_Labeltype a) {labeltype_ = a;}
  159. /**
  160. Gets the menu item's label color.
  161. This color is passed to the labeltype routine, and is typically the
  162. color of the label text. This defaults to FL_BLACK. If this
  163. color is not black fltk will \b not use overlay bitplanes to draw
  164. the menu - this is so that images put in the menu draw correctly.
  165. */
  166. Fl_Color labelcolor() const {return labelcolor_;}
  167. /**
  168. Sets the menu item's label color.
  169. \see Fl_Color Fl_Menu_Item::labelcolor() const
  170. */
  171. void labelcolor(Fl_Color a) {labelcolor_ = a;}
  172. /**
  173. Gets the menu item's label font.
  174. Fonts are identified by small 8-bit indexes into a table. See the
  175. enumeration list for predefined fonts. The default value is a
  176. Helvetica font. The function Fl::set_font() can define new fonts.
  177. */
  178. Fl_Font labelfont() const {return labelfont_;}
  179. /**
  180. Sets the menu item's label font.
  181. Fonts are identified by small 8-bit indexes into a table. See the
  182. enumeration list for predefined fonts. The default value is a
  183. Helvetica font. The function Fl::set_font() can define new fonts.
  184. */
  185. void labelfont(Fl_Font a) {labelfont_ = a;}
  186. /** Gets the label font pixel size/height. */
  187. Fl_Fontsize labelsize() const {return labelsize_;}
  188. /** Sets the label font pixel size/height.*/
  189. void labelsize(Fl_Fontsize a) {labelsize_ = a;}
  190. /**
  191. Returns the callback function that is set for the menu item.
  192. Each item has space for a callback function and an argument for that
  193. function. Due to back compatibility, the Fl_Menu_Item itself
  194. is not passed to the callback, instead you have to get it by calling
  195. ((Fl_Menu_*)w)->mvalue() where w is the widget argument.
  196. */
  197. Fl_Callback_p callback() const {return callback_;}
  198. /**
  199. Sets the menu item's callback function and userdata() argument.
  200. \see Fl_Callback_p Fl_MenuItem::callback() const
  201. */
  202. void callback(Fl_Callback* c, void* p) {callback_=c; user_data_=p;}
  203. /**
  204. Sets the menu item's callback function.
  205. This method does not set the userdata() argument.
  206. \see Fl_Callback_p Fl_MenuItem::callback() const
  207. */
  208. void callback(Fl_Callback* c) {callback_=c;}
  209. /**
  210. Sets the menu item's callback function.
  211. This method does not set the userdata() argument.
  212. \see Fl_Callback_p Fl_MenuItem::callback() const
  213. */
  214. void callback(Fl_Callback0*c) {callback_=(Fl_Callback*)c;}
  215. /**
  216. Sets the menu item's callback function and userdata() argument.
  217. This method does not set the userdata() argument.
  218. The argument \p is cast to void* and stored as the userdata()
  219. for the menu item's callback function.
  220. \see Fl_Callback_p Fl_MenuItem::callback() const
  221. */
  222. void callback(Fl_Callback1*c, long p=0) {callback_=(Fl_Callback*)c; user_data_=(void*)p;}
  223. /**
  224. Gets the user_data() argument that is sent to the callback function.
  225. */
  226. void* user_data() const {return user_data_;}
  227. /**
  228. Sets the user_data() argument that is sent to the callback function.
  229. */
  230. void user_data(void* v) {user_data_ = v;}
  231. /**
  232. Gets the user_data() argument that is sent to the callback function.
  233. For convenience you can also define the callback as taking a long
  234. argument. This method casts the stored userdata() argument to long
  235. and returns it as a \e long value.
  236. */
  237. long argument() const {return (long)(fl_intptr_t)user_data_;}
  238. /**
  239. Sets the user_data() argument that is sent to the callback function.
  240. For convenience you can also define the callback as taking a long
  241. argument. This method casts the given argument \p v to void*
  242. and stores it in the menu item's userdata() member.
  243. This may not be portable to some machines.
  244. */
  245. void argument(long v) {user_data_ = (void*)v;}
  246. /** Gets what key combination shortcut will trigger the menu item. */
  247. int shortcut() const {return shortcut_;}
  248. /**
  249. Sets exactly what key combination will trigger the menu item. The
  250. value is a logical 'or' of a key and a set of shift flags, for instance
  251. FL_ALT+'a' or FL_ALT+FL_F+10 or just 'a'. A value of
  252. zero disables the shortcut.
  253. The key can be any value returned by Fl::event_key(), but will usually
  254. be an ASCII letter. Use a lower-case letter unless you require the shift
  255. key to be held down.
  256. The shift flags can be any set of values accepted by Fl::event_state().
  257. If the bit is on that shift key must be pushed. Meta, Alt, Ctrl,
  258. and Shift must be off if they are not in the shift flags (zero for the
  259. other bits indicates a "don't care" setting).
  260. */
  261. void shortcut(int s) {shortcut_ = s;}
  262. /**
  263. Returns true if either FL_SUBMENU or FL_SUBMENU_POINTER
  264. is on in the flags. FL_SUBMENU indicates an embedded submenu
  265. that goes from the next item through the next one with a NULL
  266. label(). FL_SUBMENU_POINTER indicates that user_data()
  267. is a pointer to another menu array.
  268. */
  269. int submenu() const {return flags&(FL_SUBMENU|FL_SUBMENU_POINTER);}
  270. /**
  271. Returns true if a checkbox will be drawn next to this item.
  272. This is true if FL_MENU_TOGGLE or FL_MENU_RADIO is set in the flags.
  273. */
  274. int checkbox() const {return flags&FL_MENU_TOGGLE;}
  275. /**
  276. Returns true if this item is a radio item.
  277. When a radio button is selected all "adjacent" radio buttons are
  278. turned off. A set of radio items is delimited by an item that has
  279. radio() false, or by an item with FL_MENU_DIVIDER turned on.
  280. */
  281. int radio() const {return flags&FL_MENU_RADIO;}
  282. /** Returns the current value of the check or radio item. */
  283. int value() const {return flags&FL_MENU_VALUE;}
  284. /**
  285. Turns the check or radio item "on" for the menu item. Note that this
  286. does not turn off any adjacent radio items like set_only() does.
  287. */
  288. void set() {flags |= FL_MENU_VALUE;}
  289. /** Turns the check or radio item "off" for the menu item. */
  290. void clear() {flags &= ~FL_MENU_VALUE;}
  291. void setonly();
  292. /** Gets the visibility of an item. */
  293. int visible() const {return !(flags&FL_MENU_INVISIBLE);}
  294. /** Makes an item visible in the menu. */
  295. void show() {flags &= ~FL_MENU_INVISIBLE;}
  296. /** Hides an item in the menu. */
  297. void hide() {flags |= FL_MENU_INVISIBLE;}
  298. /** Gets whether or not the item can be picked. */
  299. int active() const {return !(flags&FL_MENU_INACTIVE);}
  300. /** Allows a menu item to be picked. */
  301. void activate() {flags &= ~FL_MENU_INACTIVE;}
  302. /**
  303. Prevents a menu item from being picked. Note that this will also cause
  304. the menu item to appear grayed-out.
  305. */
  306. void deactivate() {flags |= FL_MENU_INACTIVE;}
  307. /** Returns non 0 if FL_INACTIVE and FL_INVISIBLE are cleared, 0 otherwise. */
  308. int activevisible() const {return !(flags & (FL_MENU_INACTIVE|FL_MENU_INVISIBLE));}
  309. // compatibility for FLUID so it can set the image of a menu item...
  310. /** compatibility api for FLUID, same as a->label(this) */
  311. void image(Fl_Image* a) {a->label(this);}
  312. /** compatibility api for FLUID, same as a.label(this) */
  313. void image(Fl_Image& a) {a.label(this);}
  314. // used by menubar:
  315. int measure(int* h, const Fl_Menu_*) const;
  316. void draw(int x, int y, int w, int h, const Fl_Menu_*, int t=0) const;
  317. // popup menus without using an Fl_Menu_ widget:
  318. const Fl_Menu_Item* popup(
  319. int X, int Y,
  320. const char *title = 0,
  321. const Fl_Menu_Item* picked=0,
  322. const Fl_Menu_* = 0) const;
  323. const Fl_Menu_Item* pulldown(
  324. int X, int Y, int W, int H,
  325. const Fl_Menu_Item* picked = 0,
  326. const Fl_Menu_* = 0,
  327. const Fl_Menu_Item* title = 0,
  328. int menubar=0) const;
  329. const Fl_Menu_Item* test_shortcut() const;
  330. const Fl_Menu_Item* find_shortcut(int *ip=0, const bool require_alt = false) const;
  331. /**
  332. Calls the Fl_Menu_Item item's callback, and provides the Fl_Widget argument.
  333. The callback is called with the stored user_data() as its second argument.
  334. You must first check that callback() is non-zero before calling this.
  335. */
  336. void do_callback(Fl_Widget* o) const {callback_(o, user_data_);}
  337. /**
  338. Calls the Fl_Menu_Item item's callback, and provides the Fl_Widget argument.
  339. This call overrides the callback's second argument with the given value \p arg.
  340. You must first check that callback() is non-zero before calling this.
  341. */
  342. void do_callback(Fl_Widget* o,void* arg) const {callback_(o, arg);}
  343. /**
  344. Calls the Fl_Menu_Item item's callback, and provides the Fl_Widget argument.
  345. This call overrides the callback's second argument with the
  346. given value \p arg. long \p arg is cast to void* when calling
  347. the callback.
  348. You must first check that callback() is non-zero before calling this.
  349. */
  350. void do_callback(Fl_Widget* o,long arg) const {callback_(o, (void*)arg);}
  351. // back-compatibility, do not use:
  352. /** back compatibility only \deprecated. */
  353. int checked() const {return flags&FL_MENU_VALUE;}
  354. /** back compatibility only \deprecated. */
  355. void check() {flags |= FL_MENU_VALUE;}
  356. /** back compatibility only \deprecated. */
  357. void uncheck() {flags &= ~FL_MENU_VALUE;}
  358. int insert(int,const char*,int,Fl_Callback*,void* =0, int =0);
  359. int add(const char*, int shortcut, Fl_Callback*, void* =0, int = 0);
  360. /** See int add(const char*, int shortcut, Fl_Callback*, void*, int) */
  361. int add(const char*a, const char* b, Fl_Callback* c,
  362. void* d = 0, int e = 0) {
  363. return add(a,fl_old_shortcut(b),c,d,e);}
  364. int size() const ;
  365. };
  366. typedef Fl_Menu_Item Fl_Menu; // back compatibility
  367. enum { // back-compatibility enum:
  368. FL_PUP_NONE = 0,
  369. FL_PUP_GREY = FL_MENU_INACTIVE,
  370. FL_PUP_GRAY = FL_MENU_INACTIVE,
  371. FL_MENU_BOX = FL_MENU_TOGGLE,
  372. FL_PUP_BOX = FL_MENU_TOGGLE,
  373. FL_MENU_CHECK = FL_MENU_VALUE,
  374. FL_PUP_CHECK = FL_MENU_VALUE,
  375. FL_PUP_RADIO = FL_MENU_RADIO,
  376. FL_PUP_INVISIBLE = FL_MENU_INVISIBLE,
  377. FL_PUP_SUBMENU = FL_SUBMENU_POINTER
  378. };
  379. #endif
  380. //
  381. // End of "$Id: Fl_Menu_Item.H 7983 2010-12-09 00:04:06Z AlbrechtS $".
  382. //