FLTK 1.3.7: Fl_Spinner.H Source File

FLTK 1.3.7

Fl_Spinner.H
1 //
2 // "$Id$"
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
58 Fl_Repeat_Button
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->set_changed();
96 sb->do_callback();
97 }
98 void update() {
99 char s[255]; // Value string
100
101 if (format_[0]=='%'&&format_[1]=='.'&&format_[2]=='*') { // precision argument
102 // this code block is a simplified version of
103 // Fl_Valuator::format() and works well (but looks ugly)
104 int c = 0;
105 char temp[64], *sp = temp;
106 sprintf(temp, "%.12f", step_);
107 while (*sp) sp++;
108 sp--;
109 while (sp>temp && *sp=='0') sp--;
110 while (sp>temp && (*sp>='0' && *sp<='9')) { sp--; c++; }
111 sprintf(s, format_, c, value_);
112 } else {
113 sprintf(s, format_, value_);
114 }
115 input_.value(s);
116 }
117
118 public:
119
125 Fl_Spinner(int X, int Y, int W, int H, const char *L = 0);
126
128 const char *format() { return (format_); }
130 void format(const char *f) { format_ = f; update(); }
131
132 int handle(int event) {
133 switch (event) {
134 case FL_KEYDOWN :
135 case FL_SHORTCUT :
136 if (Fl::event_key() == FL_Up) {
137 up_button_.do_callback();
138 return 1;
139 } else if (Fl::event_key() == FL_Down) {
140 down_button_.do_callback();
141 return 1;
142 } else return 0;
143
144 case FL_FOCUS :
145 if (input_.take_focus()) return 1;
146 else return 0;
147 }
148
149 return Fl_Group::handle(event);
150 }
151
153 double maxinum() const { return (maximum_); }
155 double maximum() const { return (maximum_); }
157 void maximum(double m) { maximum_ = m; }
159 double mininum() const { return (minimum_); }
161 double minimum() const { return (minimum_); }
163 void minimum(double m) { minimum_ = m; }
165 void range(double a, double b) { minimum_ = a; maximum_ = b; }
166 void resize(int X, int Y, int W, int H) {
167 Fl_Group::resize(X,Y,W,H);
168
169 input_.resize(X, Y, W - H / 2 - 2, H);
170 up_button_.resize(X + W - H / 2 - 2, Y, H / 2 + 2, H / 2);
171 down_button_.resize(X + W - H / 2 - 2, Y + H - H / 2,
172 H / 2 + 2, H / 2);
173 }
179 double step() const { return (step_); }
181 void step(double s) {
182 step_ = s;
183 if (step_ != (int)step_) input_.type(FL_FLOAT_INPUT);
184 else input_.type(FL_INT_INPUT);
185 update();
186 }
188 Fl_Color textcolor() const {
189 return (input_.textcolor());
190 }
192 void textcolor(Fl_Color c) {
193 input_.textcolor(c);
194 }
196 Fl_Font textfont() const {
197 return (input_.textfont());
198 }
200 void textfont(Fl_Font f) {
201 input_.textfont(f);
202 }
204 Fl_Fontsize textsize() const {
205 return (input_.textsize());
206 }
208 void textsize(Fl_Fontsize s) {
209 input_.textsize(s);
210 }
214 uchar type() const { return (input_.type()); }
221 void type(uchar v) {
222 if (v==FL_FLOAT_INPUT) {
223 format("%.*f");
224 } else {
225 format("%.0f");
226 }
227 input_.type(v);
228 }
230 double value() const { return (value_); }
236 void value(double v) { value_ = v; update(); }
240 void color(Fl_Color v) { input_.color(v); }
244 Fl_Color color() const { return(input_.color()); }
248 void selection_color(Fl_Color val) { input_.selection_color(val); }
252 Fl_Color selection_color() const { return input_.selection_color(); }
253 };
254
255 #endif // !Fl_Spinner_H
256
257 //
258 // End of "$Id$".
259 //
Enumerations.H
This file contains type definitions and general enumerations.
FL_Up
#define FL_Up
The up arrow key.
Definition: Enumerations.H:479
Fl_Font
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:875
FL_Down
#define FL_Down
The down arrow key.
Definition: Enumerations.H:481
Fl_Color
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition: Enumerations.H:932
FL_KEYDOWN
@ FL_KEYDOWN
A key was pressed (FL_KEYDOWN) or released (FL_KEYUP).
Definition: Enumerations.H:308
FL_SHORTCUT
@ FL_SHORTCUT
If the Fl::focus() widget is zero or ignores an FL_KEYBOARD event then FLTK tries sending this event ...
Definition: Enumerations.H:347
FL_FOCUS
@ FL_FOCUS
This indicates an attempt to give a widget the keyboard focus.
Definition: Enumerations.H:281
Fl_Fontsize
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:904
Fl_Group
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:41
Fl_Group::handle
int handle(int)
Handles the specified event.
Definition: Fl_Group.cxx:147
Fl_Group::resize
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Group.cxx:634
Fl_Input_::resize
void resize(int, int, int, int)
Changes the size of the widget.
Definition: Fl_Input_.cxx:1294
Fl_Input_::textfont
Fl_Font textfont() const
Gets the font of the text in the input field.
Definition: Fl_Input_.H:386
Fl_Input_::textcolor
Fl_Color textcolor() const
Gets the color of the text in the input field.
Definition: Fl_Input_.H:405
Fl_Input_::value
int value(const char *)
Changes the widget text.
Definition: Fl_Input_.cxx:1284
Fl_Input_::textsize
Fl_Fontsize textsize() const
Gets the size of the text in the input field.
Definition: Fl_Input_.H:395
Fl_Input
This is the FLTK text input widget.
Definition: Fl_Input.H:222
Fl_Repeat_Button
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:33
Fl_Spinner
This widget is a combination of the input widget and repeat buttons.
Definition: Fl_Spinner.H:45
Fl_Spinner::textfont
Fl_Font textfont() const
Gets the font of the text in the input field.
Definition: Fl_Spinner.H:196
Fl_Spinner::textcolor
void textcolor(Fl_Color c)
Sets the color of the text in the input field.
Definition: Fl_Spinner.H:192
Fl_Spinner::step
void step(double s)
See double Fl_Spinner::step() const.
Definition: Fl_Spinner.H:181
Fl_Spinner::value
double value() const
Gets the current value of the widget.
Definition: Fl_Spinner.H:230
Fl_Spinner::selection_color
void selection_color(Fl_Color val)
Change the selection color of the spinner widget's input field.
Definition: Fl_Spinner.H:248
Fl_Spinner::format
const char * format()
Sets or returns the format string for the value.
Definition: Fl_Spinner.H:128
Fl_Spinner::handle
int handle(int event)
Handles the specified event.
Definition: Fl_Spinner.H:132
Fl_Spinner::textsize
void textsize(Fl_Fontsize s)
Sets the size of the text in the input field.
Definition: Fl_Spinner.H:208
Fl_Spinner::minimum
double minimum() const
Gets the minimum value of the widget.
Definition: Fl_Spinner.H:161
Fl_Spinner::maximum
void maximum(double m)
Sets the maximum value of the widget.
Definition: Fl_Spinner.H:157
Fl_Spinner::color
Fl_Color color() const
Return the background color of the spinner widget's input field.
Definition: Fl_Spinner.H:244
Fl_Spinner::minimum
void minimum(double m)
Sets the minimum value of the widget.
Definition: Fl_Spinner.H:163
Fl_Spinner::mininum
double mininum() const
Speling mistakes retained for source compatibility.
Definition: Fl_Spinner.H:159
Fl_Spinner::type
uchar type() const
Gets the numeric representation in the input field.
Definition: Fl_Spinner.H:214
Fl_Spinner::resize
void resize(int X, int Y, int W, int H)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Spinner.H:166
Fl_Spinner::textfont
void textfont(Fl_Font f)
Sets the font of the text in the input field.
Definition: Fl_Spinner.H:200
Fl_Spinner::selection_color
Fl_Color selection_color() const
Return the selection color of the spinner widget's input field.
Definition: Fl_Spinner.H:252
Fl_Spinner::textsize
Fl_Fontsize textsize() const
Gets the size of the text in the input field.
Definition: Fl_Spinner.H:204
Fl_Spinner::format
void format(const char *f)
Sets or returns the format string for the value.
Definition: Fl_Spinner.H:130
Fl_Spinner::range
void range(double a, double b)
Sets the minimum and maximum values for the widget.
Definition: Fl_Spinner.H:165
Fl_Spinner::maxinum
double maxinum() const
Speling mistakes retained for source compatibility.
Definition: Fl_Spinner.H:153
Fl_Spinner::step
double step() const
Sets or returns the amount to change the value when the user clicks a button.
Definition: Fl_Spinner.H:179
Fl_Spinner::maximum
double maximum() const
Gets the maximum value of the widget.
Definition: Fl_Spinner.H:155
Fl_Spinner::value
void value(double v)
Sets the current value of the widget.
Definition: Fl_Spinner.H:236
Fl_Spinner::color
void color(Fl_Color v)
Change the background color of the spinner widget's input field.
Definition: Fl_Spinner.H:240
Fl_Spinner::textcolor
Fl_Color textcolor() const
Gets the color of the text in the input field.
Definition: Fl_Spinner.H:188
Fl_Spinner::type
void type(uchar v)
Sets the numeric representation in the input field.
Definition: Fl_Spinner.H:221
Fl_Widget
Fl_Widget is the base class for all widgets in FLTK.
Definition: Fl_Widget.H:101
Fl_Widget::color
Fl_Color color() const
Gets the background color of the widget.
Definition: Fl_Widget.H:378
Fl_Widget::do_callback
void do_callback()
Calls the widget callback.
Definition: Fl_Widget.H:861
Fl_Widget::set_changed
void set_changed()
Marks the value of the widget as changed.
Definition: Fl_Widget.H:786
Fl_Widget::selection_color
Fl_Color selection_color() const
Gets the selection color.
Definition: Fl_Widget.H:396
Fl_Widget::take_focus
int take_focus()
Gives the widget the keyboard focus.
Definition: Fl_Widget.cxx:162
Fl_Widget::resize
virtual void resize(int x, int y, int w, int h)
Changes the size or position of the widget.
Definition: Fl_Widget.cxx:150
Fl_Widget::type
uchar type() const
Gets the widget type.
Definition: Fl_Widget.H:274
uchar
unsigned char uchar
unsigned char
Definition: fl_types.h:30
Fl::event_key
static int event_key()
Gets which key on the keyboard was last pushed.
Definition: Fl.H:723
- Copyright © 1998-2021 by Bill Spitzak and others.