FLTK 1.3.2
Fl_Spinner.H
1 //
2 // "$Id: Fl_Spinner.H 9706 2012-11-06 20:46:14Z matt $"
3 //
4 // Spinner 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. 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 /* \file
20  Fl_Spinner widget . */
21 
22 #ifndef Fl_Spinner_H
23 # define Fl_Spinner_H
24 
25 //
26 // Include necessary headers...
27 //
28 
29 # include <FL/Enumerations.H>
30 # include <FL/Fl_Group.H>
31 # include <FL/Fl_Input.H>
32 # include <FL/Fl_Repeat_Button.H>
33 # include <stdio.h>
34 # include <stdlib.h>
35 
36 
45 class FL_EXPORT Fl_Spinner : public Fl_Group {
46 
47  double value_; // Current value
48  double minimum_; // Minimum value
49  double maximum_; // Maximum value
50  double step_; // Amount to add/subtract for up/down
51  const char *format_; // Format string
52 
53 #if FLTK_ABI_VERSION >= 10301
54 // NEW
55 protected:
56 #endif
57  Fl_Input input_; // Input field for the value
59  up_button_, // Up button
60  down_button_; // Down button
61 
62 private:
63  static void sb_cb(Fl_Widget *w, Fl_Spinner *sb) {
64  double v; // New value
65 
66  if (w == &(sb->input_)) {
67  // Something changed in the input field...
68  v = atof(sb->input_.value());
69 
70  if (v < sb->minimum_) {
71  sb->value_ = sb->minimum_;
72  sb->update();
73  } else if (v > sb->maximum_) {
74  sb->value_ = sb->maximum_;
75  sb->update();
76  } else sb->value_ = v;
77  } else if (w == &(sb->up_button_)) {
78  // Up button pressed...
79  v = sb->value_ + sb->step_;
80 
81  if (v > sb->maximum_) sb->value_ = sb->minimum_;
82  else sb->value_ = v;
83 
84  sb->update();
85  } else if (w == &(sb->down_button_)) {
86  // Down button pressed...
87  v = sb->value_ - sb->step_;
88 
89  if (v < sb->minimum_) sb->value_ = sb->maximum_;
90  else sb->value_ = v;
91 
92  sb->update();
93  }
94 
95  sb->do_callback();
96  }
97  void update() {
98  char s[255]; // Value string
99 
100  if (format_[0]=='%'&&format_[1]=='.'&&format_[2]=='*') { // precision argument
101  // this code block is a simplified version of
102  // Fl_Valuator::format() and works well (but looks ugly)
103  int c = 0;
104  char temp[64], *sp = temp;
105  sprintf(temp, "%.12f", step_);
106  while (*sp) sp++;
107  sp--;
108  while (sp>temp && *sp=='0') sp--;
109  while (sp>temp && (*sp>='0' && *sp<='9')) { sp--; c++; }
110  sprintf(s, format_, c, value_);
111  } else {
112  sprintf(s, format_, value_);
113  }
114  input_.value(s);
115  }
116 
117  public:
118 
124  Fl_Spinner(int X, int Y, int W, int H, const char *L = 0);
125 
127  const char *format() { return (format_); }
129  void format(const char *f) { format_ = f; update(); }
130 
131  int handle(int event) {
132  switch (event) {
133  case FL_KEYDOWN :
134  case FL_SHORTCUT :
135  if (Fl::event_key() == FL_Up) {
136  up_button_.do_callback();
137  return 1;
138  } else if (Fl::event_key() == FL_Down) {
139  down_button_.do_callback();
140  return 1;
141  } else return 0;
142 
143  case FL_FOCUS :
144  if (input_.take_focus()) return 1;
145  else return 0;
146  }
147 
148  return Fl_Group::handle(event);
149  }
150 
152  double maxinum() const { return (maximum_); }
154  double maximum() const { return (maximum_); }
156  void maximum(double m) { maximum_ = m; }
158  double mininum() const { return (minimum_); }
160  double minimum() const { return (minimum_); }
162  void minimum(double m) { minimum_ = m; }
164  void range(double a, double b) { minimum_ = a; maximum_ = b; }
165  void resize(int X, int Y, int W, int H) {
166  Fl_Group::resize(X,Y,W,H);
167 
168  input_.resize(X, Y, W - H / 2 - 2, H);
169  up_button_.resize(X + W - H / 2 - 2, Y, H / 2 + 2, H / 2);
170  down_button_.resize(X + W - H / 2 - 2, Y + H - H / 2,
171  H / 2 + 2, H / 2);
172  }
178  double step() const { return (step_); }
180  void step(double s) {
181  step_ = s;
182  if (step_ != (int)step_) input_.type(FL_FLOAT_INPUT);
183  else input_.type(FL_INT_INPUT);
184  update();
185  }
187  Fl_Color textcolor() const {
188  return (input_.textcolor());
189  }
191  void textcolor(Fl_Color c) {
192  input_.textcolor(c);
193  }
195  Fl_Font textfont() const {
196  return (input_.textfont());
197  }
199  void textfont(Fl_Font f) {
200  input_.textfont(f);
201  }
204  return (input_.textsize());
205  }
208  input_.textsize(s);
209  }
213  uchar type() const { return (input_.type()); }
220  void type(uchar v) {
221  if (v==FL_FLOAT_INPUT) {
222  format("%.*f");
223  } else {
224  format("%.0f");
225  }
226  input_.type(v);
227  }
229  double value() const { return (value_); }
235  void value(double v) { value_ = v; update(); }
239  void color(Fl_Color v) { input_.color(v); }
243  Fl_Color color() const { return(input_.color()); }
244 };
245 
246 #endif // !Fl_Spinner_H
247 
248 //
249 // End of "$Id: Fl_Spinner.H 9706 2012-11-06 20:46:14Z matt $".
250 //