fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Button.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Button widget for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2014 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 
19 #include <FL/Fl.H>
20 #include <FL/Fl_Button.H>
21 #include <FL/Fl_Group.H>
22 #include <FL/Fl_Window.H>
23 
24 #include <FL/Fl_Radio_Button.H>
25 #include <FL/Fl_Toggle_Button.H>
26 
27 
29 
30 
31 // There are a lot of subclasses, named Fl_*_Button. Some of
32 // them are implemented by setting the type() value and testing it
33 // here. This includes Fl_Radio_Button and Fl_Toggle_Button
34 
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 
59 void Fl_Button::setonly() { // set this radio button on, turn others off
60  value(1);
61  Fl_Group* g = parent();
62  Fl_Widget*const* a = g->array();
63  for (int i = g->children(); i--;) {
64  Fl_Widget* o = *a++;
65  if (o != this && o->type()==FL_RADIO_BUTTON) ((Fl_Button*)o)->value(0);
66  }
67 }
68 
70  if (type() == FL_HIDDEN_BUTTON) return;
71  Fl_Color col = value() ? selection_color() : color();
72  draw_box(value() ? (down_box()?down_box():fl_down(box())) : box(), col);
73  draw_backdrop();
74  if (labeltype() == FL_NORMAL_LABEL && value()) {
75  Fl_Color c = labelcolor();
76  labelcolor(fl_contrast(c, col));
77  draw_label();
78  labelcolor(c);
79  } else draw_label();
80  if (Fl::focus() == this) draw_focus();
81 }
82 
83 int Fl_Button::handle(int event) {
84  int newval;
85  switch (event) {
86  case FL_ENTER: /* FALLTHROUGH */
87  case FL_LEAVE:
88 // if ((value_?selection_color():color())==FL_GRAY) redraw();
89  return 1;
90  case FL_PUSH:
91  if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this);
92  /* FALLTHROUGH */
93  case FL_DRAG:
94  if (Fl::event_inside(this)) {
95  if (type() == FL_RADIO_BUTTON) newval = 1;
96  else newval = !oldval;
97  } else {
98  clear_changed();
99  newval = oldval;
100  }
101  if (newval != value_) {
102  value_ = newval;
103  set_changed();
104  redraw();
105  if (when() & FL_WHEN_CHANGED) do_callback();
106  }
107  return 1;
108  case FL_RELEASE:
109  if (value_ == oldval) {
111  return 1;
112  }
113  set_changed();
114  if (type() == FL_RADIO_BUTTON) setonly();
115  else if (type() == FL_TOGGLE_BUTTON) oldval = value_;
116  else {
117  value(oldval);
118  set_changed();
119  if (when() & FL_WHEN_CHANGED) {
120  Fl_Widget_Tracker wp(this);
121  do_callback();
122  if (wp.deleted()) return 1;
123  }
124  }
125  if (when() & FL_WHEN_RELEASE) do_callback();
126  return 1;
127  case FL_SHORTCUT:
128  if (!(shortcut() ?
129  Fl::test_shortcut(shortcut()) : test_shortcut())) return 0;
130  if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this);
131  goto triggered_by_keyboard;
132  case FL_FOCUS :
133  case FL_UNFOCUS :
134  if (Fl::visible_focus()) {
135  if (box() == FL_NO_BOX) {
136  // Widgets with the FL_NO_BOX boxtype need a parent to
137  // redraw, since it is responsible for redrawing the
138  // background...
139  int X = x() > 0 ? x() - 1 : 0;
140  int Y = y() > 0 ? y() - 1 : 0;
141  if (window()) window()->damage(FL_DAMAGE_ALL, X, Y, w() + 2, h() + 2);
142  } else redraw();
143  return 1;
144  } else return 0;
145  /* NOTREACHED */
146  case FL_KEYBOARD :
147  if (Fl::focus() == this && Fl::event_key() == ' ' &&
148  !(Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) {
149  set_changed();
150  triggered_by_keyboard:
151  Fl_Widget_Tracker wp(this);
152  if (type() == FL_RADIO_BUTTON) {
153  if (!value_) {
154  setonly();
155  if (when() & FL_WHEN_CHANGED) do_callback();
156  }
157  } else if (type() == FL_TOGGLE_BUTTON) {
158  value(!value());
159  if (when() & FL_WHEN_CHANGED) do_callback();
160  } else {
162  }
163  if (wp.deleted()) return 1;
164  if (when() & FL_WHEN_RELEASE) do_callback();
165  return 1;
166  }
167  default:
168  return 0;
169  }
170 }
171 
173 {
174  if (key_release_tracker) {
177  }
178  value(1);
179  redraw();
182 }
183 
185 {
187  if (!wt)
188  return;
189  if (wt==key_release_tracker)
190  key_release_tracker = 0L;
191  Fl_Button *btn = (Fl_Button*)wt->widget();
192  if (btn) {
193  btn->value(0);
194  btn->redraw();
195  }
196  delete wt;
197 }
198 
213 Fl_Button::Fl_Button(int X, int Y, int W, int H, const char *L)
214 : Fl_Widget(X,Y,W,H,L) {
215  box(FL_UP_BOX);
217  value_ = oldval = 0;
218  shortcut_ = 0;
220 }
221 
230 Fl_Radio_Button::Fl_Radio_Button(int X,int Y,int W,int H,const char *L)
231 : Fl_Button(X, Y, W, H, L) {
233 }
234 
243 Fl_Toggle_Button::Fl_Toggle_Button(int X,int Y,int W,int H,const char *L)
244 : Fl_Button(X,Y,W,H,L)
245 {
247 }
248 
249 
250 //
251 // End of "$Id$".
252 //
Fl_Widget::draw_label
void draw_label() const
Definition: fl_labeltype.cxx:99
Fl_Button::key_release_timeout
static void key_release_timeout(void *)
Definition: Fl_Button.cxx:184
Fl_Widget::y
int y() const
Definition: Fl_Widget.H:289
FL_DAMAGE_ALL
Definition: Enumerations.H:1112
Fl.H
Fl_Button::value
int value(int v)
Definition: Fl_Button.cxx:41
FL_META
#define FL_META
One of the meta/Windows keys is down.
Definition: Enumerations.H:563
Fl_Widget::draw_box
void draw_box() const
Definition: fl_boxtype.cxx:442
Fl_Widget::redraw_label
void redraw_label()
Definition: Fl.cxx:1786
Fl_Color
unsigned int Fl_Color
Definition: Enumerations.H:934
Fl_Button::setonly
void setonly()
Definition: Fl_Button.cxx:59
FL_TOGGLE_BUTTON
#define FL_TOGGLE_BUTTON
value() toggles between 0 and 1 at every click of the button
Definition: Fl_Button.H:33
FL_UNFOCUS
Definition: Enumerations.H:288
Fl_Button::down_box
Fl_Boxtype down_box() const
Definition: Fl_Button.H:152
Fl_Toggle_Button.H
Fl_Widget::labelcolor
Fl_Color labelcolor() const
Definition: Fl_Widget.H:472
Fl::event_state
static int event_state()
Definition: Fl.H:704
Fl_Button::shortcut
int shortcut() const
Definition: Fl_Button.H:125
FL_WHEN_NOT_CHANGED
Do the callback whenever the user interacts with the widget.
Definition: Enumerations.H:442
Fl_Button
Buttons generate callbacks when they are clicked by the user.
Definition: Fl_Button.H:79
Fl_Widget::window
Fl_Window * window() const
Definition: Fl_Window.cxx:118
Fl_Widget::clear_changed
void clear_changed()
Definition: Fl_Widget.H:791
FL_WHEN_RELEASE
Do the callback when the button or key is released and the value changes.
Definition: Enumerations.H:443
FL_DRAG
Definition: Enumerations.H:268
FL_SHIFT
#define FL_SHIFT
One of the shift keys is down.
Definition: Enumerations.H:557
Fl_Toggle_Button::Fl_Toggle_Button
Fl_Toggle_Button(int X, int Y, int W, int H, const char *l=0)
Definition: Fl_Button.cxx:243
Fl_Button::handle
virtual int handle(int)
Definition: Fl_Button.cxx:83
Fl_Button::value_
char value_
Definition: Fl_Button.H:82
H
static int H
Definition: Fl_Tooltip.cxx:76
Fl_Widget::set_flag
void set_flag(unsigned int c)
Definition: Fl_Widget.H:149
fl_down
Fl_Boxtype fl_down(Fl_Boxtype b)
Definition: Enumerations.H:731
Fl_Widget::x
int x() const
Definition: Fl_Widget.H:284
Fl_Widget::draw_backdrop
void draw_backdrop() const
Definition: fl_boxtype.cxx:447
Fl_Widget::when
Fl_When when() const
Definition: Fl_Widget.H:621
Fl_Widget::do_callback
void do_callback()
Definition: Fl_Widget.H:861
fl_contrast
Fl_Color fl_contrast(Fl_Color fg, Fl_Color bg)
Definition: fl_color.cxx:435
FL_LEAVE
Definition: Enumerations.H:259
Fl::event_key
static int event_key()
Definition: Fl.H:723
Fl_Widget::labeltype
Fl_Labeltype labeltype() const
Definition: Fl_Widget.H:456
Fl_Button::value
char value() const
Definition: Fl_Button.H:105
Fl_Widget_Tracker::deleted
int deleted()
Definition: Fl.H:1396
Fl_Button::key_release_tracker
static Fl_Widget_Tracker * key_release_tracker
Definition: Fl_Button.H:88
Fl_Widget::w
int w() const
Definition: Fl_Widget.H:294
Fl_Window.H
Fl_Widget::color
Fl_Color color() const
Definition: Fl_Widget.H:378
Fl::test_shortcut
static int test_shortcut(Fl_Shortcut)
Definition: fl_shortcut.cxx:54
FL_WHEN_CHANGED
Do the callback only when the widget value changes.
Definition: Enumerations.H:441
Fl_Widget::selection_color
Fl_Color selection_color() const
Definition: Fl_Widget.H:396
FL_ALT
#define FL_ALT
One of the alt keys is down.
Definition: Enumerations.H:560
Fl_Radio_Button::Fl_Radio_Button
Fl_Radio_Button(int X, int Y, int W, int H, const char *L=0)
Definition: Fl_Button.cxx:230
FL_UP_BOX
see figure 1
Definition: Enumerations.H:607
Fl_Button::simulate_key_action
void simulate_key_action()
Definition: Fl_Button.cxx:172
Fl_Group::array
Fl_Widget *const * array() const
Definition: Fl_Group.cxx:44
FL_FOCUS
Definition: Enumerations.H:283
Fl_Button::oldval
char oldval
Definition: Fl_Button.H:83
FL_PUSH
Definition: Enumerations.H:236
Fl_Widget::damage
uchar damage() const
Definition: Fl_Widget.H:917
Fl_Widget::test_shortcut
int test_shortcut()
Definition: fl_shortcut.cxx:494
Fl::remove_timeout
static void remove_timeout(Fl_Timeout_Handler, void *=0)
Definition: Fl.cxx:368
Fl_Widget::redraw
void redraw()
Definition: Fl.cxx:1782
Fl_Widget
Definition: Fl_Widget.H:101
Fl_Group
Definition: Fl_Group.H:41
FL_NO_BOX
nothing is drawn at all, this box is invisible
Definition: Enumerations.H:605
Fl::focus
static Fl_Widget * focus()
Definition: Fl.H:840
Fl_Widget::box
Fl_Boxtype box() const
Definition: Fl_Widget.H:363
Fl_Widget::h
int h() const
Definition: Fl_Widget.H:299
Fl_Widget_Tracker
Definition: Fl.H:1371
Fl_Group::children
int children() const
Definition: Fl_Group.H:75
FL_KEYBOARD
Definition: Enumerations.H:315
Fl_Button::Fl_Button
Fl_Button(int X, int Y, int W, int H, const char *L=0)
Definition: Fl_Button.cxx:213
Fl_Widget::set_changed
void set_changed()
Definition: Fl_Widget.H:786
Fl::add_timeout
static void add_timeout(double t, Fl_Timeout_Handler, void *=0)
Definition: Fl.cxx:329
Fl_Group.H
FL_SHORTCUT
Definition: Enumerations.H:349
Fl_Radio_Button.H
Fl::visible_focus
static int visible_focus()
Definition: Fl.H:1197
FL_NORMAL_LABEL
draws the text (0)
Definition: Enumerations.H:764
Fl_Widget::type
uchar type() const
Definition: Fl_Widget.H:274
Fl_Widget::parent
Fl_Group * parent() const
Definition: Fl_Widget.H:254
Fl_Button.H
Y
static int Y
Definition: Fl_Tooltip.cxx:76
Fl_Widget::SHORTCUT_LABEL
the label contains a shortcut we need to draw
Definition: Fl_Widget.H:162
Fl_Widget_Tracker::widget
Fl_Widget * widget()
Definition: Fl.H:1385
FL_RADIO_BUTTON
#define FL_RADIO_BUTTON
Definition: Fl_Button.H:34
FL_RELEASE
Definition: Enumerations.H:244
Fl_Button::draw
virtual void draw()
Definition: Fl_Button.cxx:69
FL_CTRL
#define FL_CTRL
One of the ctrl keys is down.
Definition: Enumerations.H:559
FL_HIDDEN_BUTTON
#define FL_HIDDEN_BUTTON
for Forms compatibility
Definition: Fl_Button.H:39
FL_ENTER
Definition: Enumerations.H:253
Fl_Button::shortcut_
int shortcut_
Definition: Fl_Button.H:81
Fl_Widget::draw_focus
void draw_focus()
Definition: Fl_Widget.H:186
Fl::event_inside
static int event_inside(int, int, int, int)
Definition: Fl.cxx:227