Fl_Menu_.H 6.7 KB

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