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.

214 lines
6.4KB

  1. //
  2. // "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Choice widget 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. #include <FL/Fl.H>
  28. #include <FL/Fl_Choice.H>
  29. #include <FL/fl_draw.H>
  30. #include "flstring.h"
  31. // Emulates the Forms choice widget. This is almost exactly the same
  32. // as an Fl_Menu_Button. The only difference is the appearance of the
  33. // button: it draws the text of the current pick and a down-arrow.
  34. void Fl_Choice::draw() {
  35. int dx = Fl::box_dx(FL_DOWN_BOX);
  36. int dy = Fl::box_dy(FL_DOWN_BOX);
  37. int H = h() - 2 * dy;
  38. int W = (H > 20) ? 20 : H;
  39. int X = x() + w() - W - dx;
  40. int Y = y() + dy;
  41. int w1 = (W - 4) / 3; if (w1 < 1) w1 = 1;
  42. int x1 = X + (W - 2 * w1 - 1) / 2;
  43. int y1 = Y + (H - w1 - 1) / 2;
  44. if (Fl::scheme()) {
  45. draw_box(FL_UP_BOX, color());
  46. fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
  47. if (!strcmp(Fl::scheme(), "plastic")) {
  48. // Show larger up/down arrows...
  49. fl_polygon(x1, y1 + 3, x1 + w1, y1 + w1 + 3, x1 + 2 * w1, y1 + 3);
  50. fl_polygon(x1, y1 + 1, x1 + w1, y1 - w1 + 1, x1 + 2 * w1, y1 + 1);
  51. } else {
  52. // Show smaller up/down arrows with a divider...
  53. x1 = x() + w() - 13 - dx;
  54. y1 = y() + h() / 2;
  55. fl_polygon(x1, y1 - 2, x1 + 3, y1 - 5, x1 + 6, y1 - 2);
  56. fl_polygon(x1, y1 + 2, x1 + 3, y1 + 5, x1 + 6, y1 + 2);
  57. fl_color(fl_darker(color()));
  58. fl_yxline(x1 - 7, y1 - 8, y1 + 8);
  59. fl_color(fl_lighter(color()));
  60. fl_yxline(x1 - 6, y1 - 8, y1 + 8);
  61. }
  62. } else {
  63. if (fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) == textcolor()) {
  64. draw_box(FL_DOWN_BOX, FL_BACKGROUND2_COLOR);
  65. } else {
  66. draw_box(FL_DOWN_BOX, fl_lighter(color()));
  67. }
  68. draw_box(FL_UP_BOX,X,Y,W,H,color());
  69. fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
  70. fl_polygon(x1, y1, x1 + w1, y1 + w1, x1 + 2 * w1, y1);
  71. }
  72. W += 2 * dx;
  73. if (mvalue()) {
  74. Fl_Menu_Item m = *mvalue();
  75. if (active_r()) m.activate(); else m.deactivate();
  76. // ERCO
  77. int xx = x() + dx, yy = y() + dy + 1, ww = w() - W, hh = H - 2;
  78. fl_push_clip(xx, yy, ww, hh);
  79. if ( Fl::scheme()) {
  80. Fl_Label l;
  81. l.value = m.text;
  82. l.image = 0;
  83. l.deimage = 0;
  84. l.type = m.labeltype_;
  85. l.font = m.labelsize_ || m.labelfont_ ? m.labelfont_ : textfont();
  86. l.size = m.labelsize_ ? m.labelsize_ : textsize();
  87. l.color= m.labelcolor_ ? m.labelcolor_ : textcolor();
  88. if (!m.active()) l.color = fl_inactive((Fl_Color)l.color);
  89. fl_draw_shortcut = 2; // hack value to make '&' disappear
  90. l.draw(xx+3, yy, ww>6 ? ww-6 : 0, hh, FL_ALIGN_LEFT);
  91. fl_draw_shortcut = 0;
  92. if ( Fl::focus() == this ) draw_focus(box(), xx, yy, ww, hh);
  93. }
  94. else {
  95. fl_draw_shortcut = 2; // hack value to make '&' disappear
  96. m.draw(xx, yy, ww, hh, this, Fl::focus() == this);
  97. fl_draw_shortcut = 0;
  98. }
  99. fl_pop_clip();
  100. }
  101. draw_label();
  102. }
  103. /**
  104. Create a new Fl_Choice widget using the given position, size and label string.
  105. The default boxtype is \c FL_UP_BOX.
  106. The constructor sets menu() to NULL.
  107. See Fl_Menu_ for the methods to set or change the menu.
  108. \param[in] X, Y, W, H position and size of the widget
  109. \param[in] L widget label, default is no label
  110. */
  111. Fl_Choice::Fl_Choice(int X, int Y, int W, int H, const char *L)
  112. : Fl_Menu_(X,Y,W,H,L) {
  113. align(FL_ALIGN_LEFT);
  114. when(FL_WHEN_RELEASE);
  115. textfont(FL_HELVETICA);
  116. box(FL_FLAT_BOX);
  117. down_box(FL_BORDER_BOX);
  118. }
  119. /**
  120. Sets the currently selected value using a pointer to menu item.
  121. Changing the selected value causes a redraw().
  122. \param[in] v pointer to menu item in the menu item array.
  123. \returns non-zero if the new value is different to the old one.
  124. */
  125. int Fl_Choice::value(const Fl_Menu_Item *v) {
  126. if (!Fl_Menu_::value(v)) return 0;
  127. redraw();
  128. return 1;
  129. }
  130. /**
  131. Sets the currently selected value using the index into the menu item array.
  132. Changing the selected value causes a redraw().
  133. \param[in] v index of value in the menu item array.
  134. \returns non-zero if the new value is different to the old one.
  135. */
  136. int Fl_Choice::value(int v) {
  137. if (v == -1) return value((const Fl_Menu_Item *)0);
  138. if (v < 0 || v >= (size() - 1)) return 0;
  139. if (!Fl_Menu_::value(v)) return 0;
  140. redraw();
  141. return 1;
  142. }
  143. int Fl_Choice::handle(int e) {
  144. if (!menu() || !menu()->text) return 0;
  145. const Fl_Menu_Item* v;
  146. switch (e) {
  147. case FL_ENTER:
  148. case FL_LEAVE:
  149. return 1;
  150. case FL_KEYBOARD:
  151. if (Fl::event_key() != ' ' ||
  152. (Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) return 0;
  153. case FL_PUSH:
  154. if (Fl::visible_focus()) Fl::focus(this);
  155. J1:
  156. if (Fl::scheme()
  157. || fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) != textcolor()) {
  158. v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
  159. } else {
  160. // In order to preserve the old look-n-feel of "white" menus,
  161. // temporarily override the color() of this widget...
  162. Fl_Color c = color();
  163. color(FL_BACKGROUND2_COLOR);
  164. v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
  165. color(c);
  166. }
  167. if (!v || v->submenu()) return 1;
  168. if (v != mvalue()) redraw();
  169. picked(v);
  170. return 1;
  171. case FL_SHORTCUT:
  172. if (Fl_Widget::test_shortcut()) goto J1;
  173. v = menu()->test_shortcut();
  174. if (!v) return 0;
  175. if (v != mvalue()) redraw();
  176. picked(v);
  177. return 1;
  178. case FL_FOCUS:
  179. case FL_UNFOCUS:
  180. if (Fl::visible_focus()) {
  181. redraw();
  182. return 1;
  183. } else return 0;
  184. default:
  185. return 0;
  186. }
  187. }
  188. //
  189. // End of "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $".
  190. //