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.

223 lines
7.5KB

  1. //
  2. // "$Id: Fl_Input_Choice.H 8022 2010-12-12 23:21:03Z AlbrechtS $"
  3. //
  4. // An input/chooser widget.
  5. // ______________ ____
  6. // | || __ |
  7. // | input area || \/ |
  8. // |______________||____|
  9. //
  10. // Copyright 1998-2010 by Bill Spitzak and others.
  11. // Copyright 2004 by Greg Ercolano.
  12. //
  13. // This library is free software; you can redistribute it and/or
  14. // modify it under the terms of the GNU Library General Public
  15. // License as published by the Free Software Foundation; either
  16. // version 2 of the License, or (at your option) any later version.
  17. //
  18. // This library is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. // Library General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU Library General Public
  24. // License along with this library; if not, write to the Free Software
  25. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  26. // USA.
  27. //
  28. // Please report all bugs and problems on the following page:
  29. //
  30. // http://www.fltk.org/str.php
  31. //
  32. /* \file
  33. Fl_Input_Choice widget . */
  34. #ifndef Fl_Input_Choice_H
  35. #define Fl_Input_Choice_H
  36. #include <FL/Fl.H>
  37. #include <FL/Fl_Group.H>
  38. #include <FL/Fl_Input.H>
  39. #include <FL/Fl_Menu_Button.H>
  40. #include <FL/fl_draw.H>
  41. #include <string.h>
  42. /**
  43. A combination of the input widget and a menu button.
  44. The user can either type into the input area, or use the
  45. menu button chooser on the right, which loads the input area
  46. with predefined text. Normally it is drawn with an inset box
  47. and a white background.
  48. <P>
  49. The application can directly access both the input and menu
  50. widgets directly, using the menubutton()
  51. and input() accessor methods.
  52. */
  53. class FL_EXPORT Fl_Input_Choice : public Fl_Group {
  54. // Private class to handle slightly 'special' behavior of menu button
  55. class InputMenuButton : public Fl_Menu_Button {
  56. void draw() {
  57. draw_box(FL_UP_BOX, color());
  58. fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
  59. int xc = x()+w()/2, yc=y()+h()/2;
  60. fl_polygon(xc-5,yc-3,xc+5,yc-3,xc,yc+3);
  61. if (Fl::focus() == this) draw_focus();
  62. }
  63. public:
  64. InputMenuButton(int x,int y,int w,int h,const char*l=0) :
  65. Fl_Menu_Button(x,y,w,h,l) { box(FL_UP_BOX); }
  66. };
  67. Fl_Input *inp_;
  68. InputMenuButton *menu_;
  69. static void menu_cb(Fl_Widget*, void *data) {
  70. Fl_Input_Choice *o=(Fl_Input_Choice *)data;
  71. Fl_Widget_Tracker wp(o);
  72. const Fl_Menu_Item *item = o->menubutton()->mvalue();
  73. if (item && item->flags & (FL_SUBMENU|FL_SUBMENU_POINTER)) return; // ignore submenus
  74. if (!strcmp(o->inp_->value(), o->menu_->text()))
  75. {
  76. o->Fl_Widget::clear_changed();
  77. if (o->when() & FL_WHEN_NOT_CHANGED)
  78. o->do_callback();
  79. }
  80. else
  81. {
  82. o->inp_->value(o->menu_->text());
  83. o->inp_->set_changed();
  84. o->Fl_Widget::set_changed();
  85. if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
  86. o->do_callback();
  87. }
  88. if (wp.deleted()) return;
  89. if (o->callback() != default_callback)
  90. {
  91. o->Fl_Widget::clear_changed();
  92. o->inp_->clear_changed();
  93. }
  94. }
  95. static void inp_cb(Fl_Widget*, void *data) {
  96. Fl_Input_Choice *o=(Fl_Input_Choice *)data;
  97. Fl_Widget_Tracker wp(o);
  98. if (o->inp_->changed()) {
  99. o->Fl_Widget::set_changed();
  100. if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
  101. o->do_callback();
  102. } else {
  103. o->Fl_Widget::clear_changed();
  104. if (o->when() & FL_WHEN_NOT_CHANGED)
  105. o->do_callback();
  106. }
  107. if (wp.deleted()) return;
  108. if (o->callback() != default_callback)
  109. o->Fl_Widget::clear_changed();
  110. }
  111. // Custom resize behavior -- input stretches, menu button doesn't
  112. inline int inp_x() { return(x() + Fl::box_dx(box())); }
  113. inline int inp_y() { return(y() + Fl::box_dy(box())); }
  114. inline int inp_w() { return(w() - Fl::box_dw(box()) - 20); }
  115. inline int inp_h() { return(h() - Fl::box_dh(box())); }
  116. inline int menu_x() { return(x() + w() - 20 - Fl::box_dx(box())); }
  117. inline int menu_y() { return(y() + Fl::box_dy(box())); }
  118. inline int menu_w() { return(20); }
  119. inline int menu_h() { return(h() - Fl::box_dh(box())); }
  120. public:
  121. /**
  122. Creates a new Fl_Input_Choice widget using the given position, size,
  123. and label string.
  124. <P> Inherited destructor Destroys the widget and any value associated with it.
  125. */
  126. Fl_Input_Choice (int x,int y,int w,int h,const char*l=0) : Fl_Group(x,y,w,h,l) {
  127. Fl_Group::box(FL_DOWN_BOX);
  128. align(FL_ALIGN_LEFT); // default like Fl_Input
  129. inp_ = new Fl_Input(inp_x(), inp_y(),
  130. inp_w(), inp_h());
  131. inp_->callback(inp_cb, (void*)this);
  132. inp_->box(FL_FLAT_BOX); // cosmetic
  133. inp_->when(FL_WHEN_CHANGED|FL_WHEN_NOT_CHANGED);
  134. menu_ = new InputMenuButton(menu_x(), menu_y(),
  135. menu_w(), menu_h());
  136. menu_->callback(menu_cb, (void*)this);
  137. menu_->box(FL_FLAT_BOX); // cosmetic
  138. end();
  139. }
  140. /** Adds an item to the menu.*/
  141. void add(const char *s) { menu_->add(s); }
  142. int changed() const { return inp_->changed() | Fl_Widget::changed();}
  143. void clear_changed() {
  144. inp_->clear_changed();
  145. Fl_Widget::clear_changed();
  146. }
  147. void set_changed() {
  148. inp_->set_changed();
  149. // no need to call Fl_Widget::set_changed()
  150. }
  151. /** Removes all items from the menu. */
  152. void clear() { menu_->clear(); }
  153. /** Gets the box type of the menu button */
  154. Fl_Boxtype down_box() const { return (menu_->down_box()); }
  155. /** Sets the box type of the menu button */
  156. void down_box(Fl_Boxtype b) { menu_->down_box(b); }
  157. /** Gets the Fl_Menu_Item array used for the menu. */
  158. const Fl_Menu_Item *menu() { return (menu_->menu()); }
  159. /** Sets the Fl_Menu_Item array used for the menu. */
  160. void menu(const Fl_Menu_Item *m) { menu_->menu(m); }
  161. void resize(int X, int Y, int W, int H) {
  162. Fl_Group::resize(X,Y,W,H);
  163. inp_->resize(inp_x(), inp_y(), inp_w(), inp_h());
  164. menu_->resize(menu_x(), menu_y(), menu_w(), menu_h());
  165. }
  166. /** Gets the encapsulated input text color attributes */
  167. Fl_Color textcolor() const { return (inp_->textcolor());}
  168. /** Sets the encapsulated input text color attributes */
  169. void textcolor(Fl_Color c) { inp_->textcolor(c);}
  170. /** Gets the encapsulated input text font attributes */
  171. Fl_Font textfont() const { return (inp_->textfont());}
  172. /** Sets the encapsulated input text font attributes */
  173. void textfont(Fl_Font f) { inp_->textfont(f);}
  174. /** Gets the encapsulated input size attributes */
  175. Fl_Fontsize textsize() const { return (inp_->textsize()); }
  176. /** Sets the encapsulated input size attributes */
  177. void textsize(Fl_Fontsize s) { inp_->textsize(s); }
  178. /** See void Fl_Input_Choice::value(const char *s) */
  179. const char* value() const { return (inp_->value()); }
  180. /**
  181. Sets or returns the input widget's current contents. The
  182. second form sets the contents using the index into the menu
  183. which you can set as an integer. Setting the value effectively
  184. 'chooses' this menu item, and sets it as the new input text,
  185. deleting the previous text.
  186. */
  187. void value(const char *val) { inp_->value(val); }
  188. /** See void Fl_Input_Choice::value(const char *s) */
  189. void value(int val) {
  190. menu_->value(val);
  191. inp_->value(menu_->text(val));
  192. }
  193. /** Returns a reference to the internal Fl_Menu_Button widget. */
  194. Fl_Menu_Button *menubutton() { return menu_; }
  195. /**
  196. Returns a reference to the internal Fl_Input widget.</p>
  197. */
  198. Fl_Input *input() { return inp_; }
  199. };
  200. #endif // !Fl_Input_Choice_H
  201. //
  202. // End of "$Id: Fl_Input_Choice.H 8022 2010-12-12 23:21:03Z AlbrechtS $".
  203. //