Fl_Menu_Item.H 17 KB

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