1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
//
// "$Id$"
//
// Spinner widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
/* \file
Fl_Spinner widget . */
#ifndef Fl_Spinner_H
# define Fl_Spinner_H
//
// Include necessary headers...
//
# include <FL/Enumerations.H>
# include <FL/Fl_Group.H>
# include <FL/Fl_Input.H>
# include <FL/Fl_Repeat_Button.H>
# include <stdio.h>
# include <stdlib.h>
/**
This widget is a combination of the input
widget and repeat buttons. The user can either type into the
input area or use the buttons to change the value.
\image html Fl_Spinner.png "Fl_Spinner widget"
\image latex Fl_Spinner.png "Fl_Spinner widget" width=6cm
*/
class FL_EXPORT Fl_Spinner : public Fl_Group {
double value_; // Current value
double minimum_; // Minimum value
double maximum_; // Maximum value
double step_; // Amount to add/subtract for up/down
const char *format_; // Format string
#if FLTK_ABI_VERSION >= 10302
// NEW
protected:
#endif
Fl_Input input_; // Input field for the value
Fl_Repeat_Button
up_button_, // Up button
down_button_; // Down button
private:
static void sb_cb(Fl_Widget *w, Fl_Spinner *sb) {
double v; // New value
if (w == &(sb->input_)) {
// Something changed in the input field...
v = atof(sb->input_.value());
if (v < sb->minimum_) {
sb->value_ = sb->minimum_;
sb->update();
} else if (v > sb->maximum_) {
sb->value_ = sb->maximum_;
sb->update();
} else sb->value_ = v;
} else if (w == &(sb->up_button_)) {
// Up button pressed...
v = sb->value_ + sb->step_;
if (v > sb->maximum_) sb->value_ = sb->minimum_;
else sb->value_ = v;
sb->update();
} else if (w == &(sb->down_button_)) {
// Down button pressed...
v = sb->value_ - sb->step_;
if (v < sb->minimum_) sb->value_ = sb->maximum_;
else sb->value_ = v;
sb->update();
}
sb->do_callback();
}
void update() {
char s[255]; // Value string
if (format_[0]=='%'&&format_[1]=='.'&&format_[2]=='*') { // precision argument
// this code block is a simplified version of
// Fl_Valuator::format() and works well (but looks ugly)
int c = 0;
char temp[64], *sp = temp;
sprintf(temp, "%.12f", step_);
while (*sp) sp++;
sp--;
while (sp>temp && *sp=='0') sp--;
while (sp>temp && (*sp>='0' && *sp<='9')) { sp--; c++; }
sprintf(s, format_, c, value_);
} else {
sprintf(s, format_, value_);
}
input_.value(s);
}
public:
/**
Creates a new Fl_Spinner widget using the given position, size,
and label string.
<P>Inherited destructor Destroys the widget and any value associated with it.
*/
Fl_Spinner(int X, int Y, int W, int H, const char *L = 0)
: Fl_Group(X, Y, W, H, L),
input_(X, Y, W - H / 2 - 2, H),
up_button_(X + W - H / 2 - 2, Y, H / 2 + 2, H / 2, "@-42<"),
down_button_(X + W - H / 2 - 2, Y + H - H / 2,
H / 2 + 2, H / 2, "@-42>") {
end();
value_ = 1.0;
minimum_ = 1.0;
maximum_ = 100.0;
step_ = 1.0;
format_ = "%g";
align(FL_ALIGN_LEFT);
input_.value("1");
input_.type(FL_INT_INPUT);
input_.when(FL_WHEN_ENTER_KEY | FL_WHEN_RELEASE);
input_.callback((Fl_Callback *)sb_cb, this);
up_button_.callback((Fl_Callback *)sb_cb, this);
down_button_.callback((Fl_Callback *)sb_cb, this);
}
/** Sets or returns the format string for the value. */
const char *format() { return (format_); }
/** Sets or returns the format string for the value. */
void format(const char *f) { format_ = f; update(); }
int handle(int event) {
switch (event) {
case FL_KEYDOWN :
case FL_SHORTCUT :
if (Fl::event_key() == FL_Up) {
up_button_.do_callback();
return 1;
} else if (Fl::event_key() == FL_Down) {
down_button_.do_callback();
return 1;
} else return 0;
case FL_FOCUS :
if (input_.take_focus()) return 1;
else return 0;
}
return Fl_Group::handle(event);
}
/** Speling mistakes retained for source compatibility \deprecated */
double maxinum() const { return (maximum_); }
/** Gets the maximum value of the widget. */
double maximum() const { return (maximum_); }
/** Sets the maximum value of the widget. */
void maximum(double m) { maximum_ = m; }
/** Speling mistakes retained for source compatibility \deprecated */
double mininum() const { return (minimum_); }
/** Gets the minimum value of the widget. */
double minimum() const { return (minimum_); }
/** Sets the minimum value of the widget. */
void minimum(double m) { minimum_ = m; }
/** Sets the minimum and maximum values for the widget. */
void range(double a, double b) { minimum_ = a; maximum_ = b; }
void resize(int X, int Y, int W, int H) {
Fl_Group::resize(X,Y,W,H);
input_.resize(X, Y, W - H / 2 - 2, H);
up_button_.resize(X + W - H / 2 - 2, Y, H / 2 + 2, H / 2);
down_button_.resize(X + W - H / 2 - 2, Y + H - H / 2,
H / 2 + 2, H / 2);
}
/**
Sets or returns the amount to change the value when the user clicks a button.
Before setting step to a non-integer value, the spinner
type() should be changed to floating point.
*/
double step() const { return (step_); }
/** See double Fl_Spinner::step() const */
void step(double s) {
step_ = s;
if (step_ != (int)step_) input_.type(FL_FLOAT_INPUT);
else input_.type(FL_INT_INPUT);
update();
}
/** Gets the color of the text in the input field. */
Fl_Color textcolor() const {
return (input_.textcolor());
}
/** Sets the color of the text in the input field. */
void textcolor(Fl_Color c) {
input_.textcolor(c);
}
/** Gets the font of the text in the input field. */
Fl_Font textfont() const {
return (input_.textfont());
}
/** Sets the font of the text in the input field. */
void textfont(Fl_Font f) {
input_.textfont(f);
}
/** Gets the size of the text in the input field. */
Fl_Fontsize textsize() const {
return (input_.textsize());
}
/** Sets the size of the text in the input field. */
void textsize(Fl_Fontsize s) {
input_.textsize(s);
}
/** Gets the numeric representation in the input field.
\see Fl_Spinner::type(uchar)
*/
uchar type() const { return (input_.type()); }
/** Sets the numeric representation in the input field.
Valid values are FL_INT_INPUT and FL_FLOAT_INPUT.
Also changes the format() template.
Setting a new spinner type via a superclass pointer will not work.
\note type is not a virtual function.
*/
void type(uchar v) {
if (v==FL_FLOAT_INPUT) {
format("%.*f");
} else {
format("%.0f");
}
input_.type(v);
}
/** Gets the current value of the widget. */
double value() const { return (value_); }
/**
Sets the current value of the widget.
Before setting value to a non-integer value, the spinner
type() should be changed to floating point.
*/
void value(double v) { value_ = v; update(); }
/**
Change the background color of the spinner widget's input field.
*/
void color(Fl_Color v) { input_.color(v); }
/**
Return the background color of the spinner widget's input field.
*/
Fl_Color color() const { return(input_.color()); }
};
#endif // !Fl_Spinner_H
//
// End of "$Id$".
//
|