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.

219 lines
6.0KB

  1. //
  2. // "$Id: Fl_Button.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Button 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_Button.H>
  29. #include <FL/Fl_Group.H>
  30. #include <FL/Fl_Window.H>
  31. Fl_Widget_Tracker *Fl_Button::key_release_tracker = 0;
  32. // There are a lot of subclasses, named Fl_*_Button. Some of
  33. // them are implemented by setting the type() value and testing it
  34. // here. This includes Fl_Radio_Button and Fl_Toggle_Button
  35. /**
  36. Sets the current value of the button.
  37. A non-zero value sets the button to 1 (ON), and zero sets it to 0 (OFF).
  38. \param[in] v button value.
  39. \see set(), clear()
  40. */
  41. int Fl_Button::value(int v) {
  42. v = v ? 1 : 0;
  43. oldval = v;
  44. clear_changed();
  45. if (value_ != v) {
  46. value_ = v;
  47. if (box()) redraw();
  48. else redraw_label();
  49. return 1;
  50. } else {
  51. return 0;
  52. }
  53. }
  54. /**
  55. Turns on this button and turns off all other radio buttons in the group
  56. (calling \c value(1) or \c set() does not do this).
  57. */
  58. void Fl_Button::setonly() { // set this radio button on, turn others off
  59. value(1);
  60. Fl_Group* g = parent();
  61. Fl_Widget*const* a = g->array();
  62. for (int i = g->children(); i--;) {
  63. Fl_Widget* o = *a++;
  64. if (o != this && o->type()==FL_RADIO_BUTTON) ((Fl_Button*)o)->value(0);
  65. }
  66. }
  67. void Fl_Button::draw() {
  68. if (type() == FL_HIDDEN_BUTTON) return;
  69. Fl_Color col = value() ? selection_color() : color();
  70. draw_box(value() ? (down_box()?down_box():fl_down(box())) : box(), col);
  71. draw_backdrop();
  72. if (labeltype() == FL_NORMAL_LABEL && value()) {
  73. Fl_Color c = labelcolor();
  74. labelcolor(fl_contrast(c, col));
  75. draw_label();
  76. labelcolor(c);
  77. } else draw_label();
  78. if (Fl::focus() == this) draw_focus();
  79. }
  80. int Fl_Button::handle(int event) {
  81. int newval;
  82. switch (event) {
  83. case FL_ENTER: /* FALLTHROUGH */
  84. case FL_LEAVE:
  85. // if ((value_?selection_color():color())==FL_GRAY) redraw();
  86. return 1;
  87. case FL_PUSH:
  88. if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this);
  89. case FL_DRAG:
  90. if (Fl::event_inside(this)) {
  91. if (type() == FL_RADIO_BUTTON) newval = 1;
  92. else newval = !oldval;
  93. } else
  94. {
  95. clear_changed();
  96. newval = oldval;
  97. }
  98. if (newval != value_) {
  99. value_ = newval;
  100. set_changed();
  101. redraw();
  102. if (when() & FL_WHEN_CHANGED) do_callback();
  103. }
  104. return 1;
  105. case FL_RELEASE:
  106. if (value_ == oldval) {
  107. if (when() & FL_WHEN_NOT_CHANGED) do_callback();
  108. return 1;
  109. }
  110. set_changed();
  111. if (type() == FL_RADIO_BUTTON) setonly();
  112. else if (type() == FL_TOGGLE_BUTTON) oldval = value_;
  113. else {
  114. value(oldval);
  115. set_changed();
  116. if (when() & FL_WHEN_CHANGED) {
  117. Fl_Widget_Tracker wp(this);
  118. do_callback();
  119. if (wp.deleted()) return 1;
  120. }
  121. }
  122. if (when() & FL_WHEN_RELEASE) do_callback();
  123. return 1;
  124. case FL_SHORTCUT:
  125. if (!(shortcut() ?
  126. Fl::test_shortcut(shortcut()) : test_shortcut())) return 0;
  127. if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this);
  128. goto triggered_by_keyboard;
  129. case FL_FOCUS : /* FALLTHROUGH */
  130. case FL_UNFOCUS :
  131. if (Fl::visible_focus()) {
  132. if (box() == FL_NO_BOX) {
  133. // Widgets with the FL_NO_BOX boxtype need a parent to
  134. // redraw, since it is responsible for redrawing the
  135. // background...
  136. int X = x() > 0 ? x() - 1 : 0;
  137. int Y = y() > 0 ? y() - 1 : 0;
  138. if (window()) window()->damage(FL_DAMAGE_ALL, X, Y, w() + 2, h() + 2);
  139. } else redraw();
  140. return 1;
  141. } else return 0;
  142. case FL_KEYBOARD :
  143. if (Fl::focus() == this && Fl::event_key() == ' ' &&
  144. !(Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) {
  145. set_changed();
  146. triggered_by_keyboard:
  147. Fl_Widget_Tracker wp(this);
  148. if (type() == FL_RADIO_BUTTON && !value_) {
  149. setonly();
  150. if (when() & FL_WHEN_CHANGED) do_callback();
  151. } else if (type() == FL_TOGGLE_BUTTON) {
  152. value(!value());
  153. if (when() & FL_WHEN_CHANGED) do_callback();
  154. } else {
  155. simulate_key_action();
  156. }
  157. if (wp.deleted()) return 1;
  158. if (when() & FL_WHEN_RELEASE) do_callback();
  159. return 1;
  160. }
  161. default:
  162. return 0;
  163. }
  164. }
  165. void Fl_Button::simulate_key_action()
  166. {
  167. if (key_release_tracker) {
  168. Fl::remove_timeout(key_release_timeout, key_release_tracker);
  169. key_release_timeout(key_release_tracker);
  170. }
  171. value(1);
  172. redraw();
  173. key_release_tracker = new Fl_Widget_Tracker(this);
  174. Fl::add_timeout(0.15, key_release_timeout, key_release_tracker);
  175. }
  176. void Fl_Button::key_release_timeout(void *d)
  177. {
  178. Fl_Widget_Tracker *wt = (Fl_Widget_Tracker*)d;
  179. if (!wt)
  180. return;
  181. if (wt==key_release_tracker)
  182. key_release_tracker = 0L;
  183. Fl_Button *btn = (Fl_Button*)wt->widget();
  184. if (btn) {
  185. btn->value(0);
  186. btn->redraw();
  187. }
  188. delete wt;
  189. }
  190. /**
  191. The constructor creates the button using the given position, size and label.
  192. \param[in] X, Y, W, H position and size of the widget
  193. \param[in] L widget label, default is no label
  194. */
  195. Fl_Button::Fl_Button(int X, int Y, int W, int H, const char *L)
  196. : Fl_Widget(X,Y,W,H,L) {
  197. box(FL_UP_BOX);
  198. down_box(FL_NO_BOX);
  199. value_ = oldval = 0;
  200. shortcut_ = 0;
  201. set_flag(SHORTCUT_LABEL);
  202. }
  203. //
  204. // End of "$Id: Fl_Button.cxx 7903 2010-11-28 21:06:39Z matt $".
  205. //