Fl_Menu_.H 7.2 KB

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