NTK 1.3.0
Fl_Spinner.H
1//
2// "$Id: Fl_Spinner.H 8339 2011-01-30 12:50:19Z ianmacarthur $"
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; 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/* \file
29 Fl_Spinner widget . */
30
31#ifndef Fl_Spinner_H
32# define Fl_Spinner_H
33
34//
35// Include necessary headers...
36//
37
38# include <FL/Enumerations.H>
39# include <FL/Fl_Group.H>
40# include <FL/Fl_Input.H>
41# include <FL/Fl_Repeat_Button.H>
42# include <stdio.h>
43# include <stdlib.h>
44
45
51class FL_EXPORT Fl_Spinner : public Fl_Group {
52
53 double value_; // Current value
54 double minimum_; // Minimum value
55 double maximum_; // Maximum value
56 double step_; // Amount to add/subtract for up/down
57 const char *format_; // Format string
58
59 Fl_Input input_; // Input field for the value
61 up_button_, // Up button
62 down_button_; // Down button
63
64
65 static void sb_cb(Fl_Widget *w, Fl_Spinner *sb) {
66 double v; // New value
67
68 if (w == &(sb->input_)) {
69 // Something changed in the input field...
70 v = atof(sb->input_.value());
71
72 if (v < sb->minimum_) {
73 sb->value_ = sb->minimum_;
74 sb->update();
75 } else if (v > sb->maximum_) {
76 sb->value_ = sb->maximum_;
77 sb->update();
78 } else sb->value_ = v;
79 } else if (w == &(sb->up_button_)) {
80 // Up button pressed...
81 v = sb->value_ + sb->step_;
82
83 if (v > sb->maximum_) sb->value_ = sb->minimum_;
84 else sb->value_ = v;
85
86 sb->update();
87 } else if (w == &(sb->down_button_)) {
88 // Down button pressed...
89 v = sb->value_ - sb->step_;
90
91 if (v < sb->minimum_) sb->value_ = sb->maximum_;
92 else sb->value_ = v;
93
94 sb->update();
95 }
96
97 sb->do_callback();
98 }
99 void update() {
100 char s[255]; // Value string
101
102 if (format_[0]=='%'&&format_[1]=='.'&&format_[2]=='*') { // precision argument
103 // this code block is a simplified version of
104 // Fl_Valuator::format() and works well (but looks ugly)
105 int c = 0;
106 char temp[64], *sp = temp;
107 sprintf(temp, "%.12f", step_);
108 while (*sp) sp++;
109 sp--;
110 while (sp>temp && *sp=='0') sp--;
111 while (sp>temp && (*sp>='0' && *sp<='9')) { sp--; c++; }
112 sprintf(s, format_, c, value_);
113 } else {
114 sprintf(s, format_, value_);
115 }
116 input_.value(s);
117 }
118
119 public:
120
126 Fl_Spinner(int X, int Y, int W, int H, const char *L = 0)
127 : Fl_Group(X, Y, W, H, L),
128 input_(X, Y, W - H / 2 - 2, H),
129 up_button_(X + W - H / 2 - 2, Y, H / 2 + 2, H / 2, "@-42<"),
130 down_button_(X + W - H / 2 - 2, Y + H - H / 2,
131 H / 2 + 2, H / 2, "@-42>") {
132 end();
133
134 value_ = 1.0;
135 minimum_ = 1.0;
136 maximum_ = 100.0;
137 step_ = 1.0;
138 format_ = "%g";
139
141
142 input_.value("1");
143 input_.type(FL_INT_INPUT);
145 input_.callback((Fl_Callback *)sb_cb, this);
146
147 up_button_.callback((Fl_Callback *)sb_cb, this);
148
149 down_button_.callback((Fl_Callback *)sb_cb, this);
150 }
151
153 const char *format() { return (format_); }
155 void format(const char *f) { format_ = f; update(); }
156
157 int handle(int event) {
158 switch (event) {
159 case FL_KEYDOWN :
160 case FL_SHORTCUT :
161 if (Fl::event_key() == FL_Up) {
162 up_button_.do_callback();
163 return 1;
164 } else if (Fl::event_key() == FL_Down) {
165 down_button_.do_callback();
166 return 1;
167 } else return 0;
168
169 case FL_FOCUS :
170 if (input_.take_focus()) return 1;
171 else return 0;
172 }
173
174 return Fl_Group::handle(event);
175 }
176
178 double maxinum() const { return (maximum_); }
180 double maximum() const { return (maximum_); }
182 void maximum(double m) { maximum_ = m; }
184 double mininum() const { return (minimum_); }
186 double minimum() const { return (minimum_); }
188 void minimum(double m) { minimum_ = m; }
190 void range(double a, double b) { minimum_ = a; maximum_ = b; }
191 void resize(int X, int Y, int W, int H) {
192 Fl_Group::resize(X,Y,W,H);
193
194 input_.resize(X, Y, W - H / 2 - 2, H);
195 up_button_.resize(X + W - H / 2 - 2, Y, H / 2 + 2, H / 2);
196 down_button_.resize(X + W - H / 2 - 2, Y + H - H / 2,
197 H / 2 + 2, H / 2);
198 }
204 double step() const { return (step_); }
206 void step(double s) {
207 step_ = s;
208 if (step_ != (int)step_) input_.type(FL_FLOAT_INPUT);
209 else input_.type(FL_INT_INPUT);
210 update();
211 }
214 return (input_.textcolor());
215 }
218 input_.textcolor(c);
219 }
222 return (input_.textfont());
223 }
226 input_.textfont(f);
227 }
230 return (input_.textsize());
231 }
234 input_.textsize(s);
235 }
239 uchar type() const { return (input_.type()); }
246 void type(uchar v) {
247 if (v==FL_FLOAT_INPUT) {
248 format("%.*f");
249 } else {
250 format("%.0f");
251 }
252 input_.type(v);
253 }
255 double value() const { return (value_); }
261 void value(double v) { value_ = v; update(); }
262};
263
264#endif // !Fl_Spinner_H
265
266//
267// End of "$Id: Fl_Spinner.H 8339 2011-01-30 12:50:19Z ianmacarthur $".
268//
This file contains type definitions and general enumerations.
#define FL_Up
The up arrow key.
Definition: Enumerations.H:350
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:707
#define FL_Down
The down arrow key.
Definition: Enumerations.H:352
unsigned int Fl_Color
an FLTK color value
Definition: Enumerations.H:764
@ FL_KEYDOWN
A key was pressed (FL_KEYDOWN) or released (FL_KEYUP).
Definition: Enumerations.H:196
@ FL_SHORTCUT
If the Fl::focus() widget is zero or ignores an FL_KEYBOARD event then FLTK tries sending this event ...
Definition: Enumerations.H:235
@ FL_FOCUS
This indicates an attempt to give a widget the keyboard focus.
Definition: Enumerations.H:169
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:736
@ FL_WHEN_ENTER_KEY
Do the callback when the user presses the ENTER key and the value changes.
Definition: Enumerations.H:319
@ FL_WHEN_RELEASE
Do the callback when the button or key is released and the value changes.
Definition: Enumerations.H:317
const Fl_Align FL_ALIGN_LEFT
Align the label at the left of the widget.
Definition: Enumerations.H:669
void() Fl_Callback(Fl_Widget *, void *)
Default callback type definition for all fltk widgets (by far the most used)
Definition: Fl_Widget.H:60
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:45
void end()
Exactly the same as current(this->parent()).
Definition: Fl_Group.cxx:80
int handle(int)
Handles the specified event.
Definition: Fl_Group.cxx:150
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Group.cxx:637
void resize(int, int, int, int)
Changes the size of the widget.
Definition: Fl_Input_.cxx:1224
Fl_Font textfont() const
Gets the font of the text in the input field.
Definition: Fl_Input_.H:387
Fl_Color textcolor() const
Gets the color of the text in the input field.
Definition: Fl_Input_.H:406
int value(const char *)
Changes the widget text.
Definition: Fl_Input_.cxx:1214
Fl_Fontsize textsize() const
Gets the size of the text in the input field.
Definition: Fl_Input_.H:396
This is the FLTK text input widget.
Definition: Fl_Input.H:230
The Fl_Repeat_Button is a subclass of Fl_Button that generates a callback when it is pressed and then...
Definition: Fl_Repeat_Button.H:42
This widget is a combination of the input widget and repeat buttons.
Definition: Fl_Spinner.H:51
Fl_Font textfont() const
Gets the font of the text in the input field.
Definition: Fl_Spinner.H:221
void textcolor(Fl_Color c)
Sets the color of the text in the input field.
Definition: Fl_Spinner.H:217
void step(double s)
See double Fl_Spinner::step() const.
Definition: Fl_Spinner.H:206
double value() const
Gets the current value of the widget.
Definition: Fl_Spinner.H:255
int handle(int event)
Handles the specified event.
Definition: Fl_Spinner.H:157
const char * format()
Sets or returns the format string for the value.
Definition: Fl_Spinner.H:153
void textsize(Fl_Fontsize s)
Sets the size of the text in the input field.
Definition: Fl_Spinner.H:233
double minimum() const
Gets the minimum value of the widget.
Definition: Fl_Spinner.H:186
void maximum(double m)
Sets the maximum value of the widget.
Definition: Fl_Spinner.H:182
void minimum(double m)
Sets the minimum value of the widget.
Definition: Fl_Spinner.H:188
double mininum() const
Speling mistakes retained for source compatibility.
Definition: Fl_Spinner.H:184
uchar type() const
Gets the numeric representation in the input field.
Definition: Fl_Spinner.H:239
Fl_Spinner(int X, int Y, int W, int H, const char *L=0)
Creates a new Fl_Spinner widget using the given position, size, and label string.
Definition: Fl_Spinner.H:126
void resize(int X, int Y, int W, int H)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Spinner.H:191
void textfont(Fl_Font f)
Sets the font of the text in the input field.
Definition: Fl_Spinner.H:225
Fl_Fontsize textsize() const
Gets the size of the text in the input field.
Definition: Fl_Spinner.H:229
void format(const char *f)
Sets or returns the format string for the value.
Definition: Fl_Spinner.H:155
void range(double a, double b)
Sets the minimum and maximum values for the widget.
Definition: Fl_Spinner.H:190
double maxinum() const
Speling mistakes retained for source compatibility.
Definition: Fl_Spinner.H:178
double step() const
Sets or returns the amount to change the value when the user clicks a button.
Definition: Fl_Spinner.H:204
double maximum() const
Gets the maximum value of the widget.
Definition: Fl_Spinner.H:180
void value(double v)
Sets the current value of the widget.
Definition: Fl_Spinner.H:261
Fl_Color textcolor() const
Gets the color of the text in the input field.
Definition: Fl_Spinner.H:213
void type(uchar v)
Sets the numeric representation in the input field.
Definition: Fl_Spinner.H:246
Fl_Widget is the base class for all widgets in FLTK.
Definition: Fl_Widget.H:111
void do_callback()
Calls the widget callback.
Definition: Fl_Widget.H:837
Fl_Align align() const
Gets the label alignment.
Definition: Fl_Widget.H:346
int take_focus()
Gives the widget the keyboard focus.
Definition: Fl_Widget.cxx:152
Fl_Callback_p callback() const
Gets the current callback function for the widget.
Definition: Fl_Widget.H:559
virtual void resize(int x, int y, int w, int h)
Changes the size or position of the widget.
Definition: Fl_Widget.cxx:140
Fl_When when() const
Returns the conditions under which the callback is called.
Definition: Fl_Widget.H:617
uchar type() const
Gets the widget type.
Definition: Fl_Widget.H:272
unsigned char uchar
unsigned char
Definition: fl_types.h:39
static int event_key()
Gets which key on the keyboard was last pushed.
Definition: Fl.H:613