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