2005-03-21 02:38:14 +03:00
|
|
|
//
|
|
|
|
// "$Id$"
|
|
|
|
//
|
|
|
|
// Spinner widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2016-03-26 05:55:14 +03:00
|
|
|
// Copyright 1998-2016 by Bill Spitzak and others.
|
2005-03-21 02:38:14 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// 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
|
2005-03-21 02:38:14 +03:00
|
|
|
//
|
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
|
|
|
//
|
|
|
|
|
2008-10-15 17:46:06 +04:00
|
|
|
/* \file
|
2008-09-16 11:26:22 +04:00
|
|
|
Fl_Spinner widget . */
|
|
|
|
|
2005-03-21 02:38:14 +03:00
|
|
|
#ifndef Fl_Spinner_H
|
|
|
|
# define Fl_Spinner_H
|
|
|
|
|
|
|
|
//
|
|
|
|
// Include necessary headers...
|
|
|
|
//
|
|
|
|
|
2006-10-29 15:34:14 +03:00
|
|
|
# include <FL/Enumerations.H>
|
2005-03-21 02:38:14 +03:00
|
|
|
# include <FL/Fl_Group.H>
|
|
|
|
# include <FL/Fl_Input.H>
|
|
|
|
# include <FL/Fl_Repeat_Button.H>
|
2005-03-21 07:51:16 +03:00
|
|
|
# include <stdio.h>
|
2005-03-21 02:38:14 +03:00
|
|
|
# include <stdlib.h>
|
|
|
|
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/**
|
|
|
|
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.
|
2012-05-11 16:12:00 +04:00
|
|
|
|
|
|
|
\image html Fl_Spinner.png "Fl_Spinner widget"
|
|
|
|
\image latex Fl_Spinner.png "Fl_Spinner widget" width=6cm
|
2008-09-15 20:39:05 +04:00
|
|
|
*/
|
2011-01-24 20:04:22 +03:00
|
|
|
class FL_EXPORT Fl_Spinner : public Fl_Group {
|
2014-09-12 13:08:41 +04:00
|
|
|
|
2005-03-25 05:39:25 +03:00
|
|
|
double value_; // Current value
|
|
|
|
double minimum_; // Minimum value
|
|
|
|
double maximum_; // Maximum value
|
|
|
|
double step_; // Amount to add/subtract for up/down
|
2005-03-21 02:38:14 +03:00
|
|
|
const char *format_; // Format string
|
|
|
|
|
2012-05-16 11:25:11 +04:00
|
|
|
protected:
|
2005-03-21 02:38:14 +03:00
|
|
|
Fl_Input input_; // Input field for the value
|
|
|
|
Fl_Repeat_Button
|
|
|
|
up_button_, // Up button
|
|
|
|
down_button_; // Down button
|
|
|
|
|
2012-05-16 11:25:11 +04:00
|
|
|
private:
|
2005-03-21 02:38:14 +03:00
|
|
|
static void sb_cb(Fl_Widget *w, Fl_Spinner *sb) {
|
2005-03-25 05:39:25 +03:00
|
|
|
double v; // New value
|
2005-03-21 02:38:14 +03:00
|
|
|
|
|
|
|
if (w == &(sb->input_)) {
|
|
|
|
// Something changed in the input field...
|
2005-03-25 05:39:25 +03:00
|
|
|
v = atof(sb->input_.value());
|
2005-03-21 02:38:14 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-09-12 13:08:41 +04:00
|
|
|
sb->set_changed();
|
2005-03-21 02:38:14 +03:00
|
|
|
sb->do_callback();
|
|
|
|
}
|
|
|
|
void update() {
|
|
|
|
char s[255]; // Value string
|
|
|
|
|
2014-09-12 13:08:41 +04:00
|
|
|
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++; }
|
2006-08-23 18:37:28 +04:00
|
|
|
sprintf(s, format_, c, value_);
|
2014-09-12 13:08:41 +04:00
|
|
|
} else {
|
2006-08-23 18:37:28 +04:00
|
|
|
sprintf(s, format_, value_);
|
2014-09-12 13:08:41 +04:00
|
|
|
}
|
2005-03-21 02:38:14 +03:00
|
|
|
input_.value(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
2012-07-24 08:37:22 +04:00
|
|
|
Fl_Spinner(int X, int Y, int W, int H, const char *L = 0);
|
2005-03-21 02:38:14 +03:00
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Sets or returns the format string for the value. */
|
2005-03-21 02:38:14 +03:00
|
|
|
const char *format() { return (format_); }
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Sets or returns the format string for the value. */
|
2005-03-21 02:38:14 +03:00
|
|
|
void format(const char *f) { format_ = f; update(); }
|
2006-10-29 15:34:14 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Speling mistakes retained for source compatibility \deprecated */
|
2005-03-25 05:39:25 +03:00
|
|
|
double maxinum() const { return (maximum_); }
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Gets the maximum value of the widget. */
|
2006-01-31 19:06:45 +03:00
|
|
|
double maximum() const { return (maximum_); }
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Sets the maximum value of the widget. */
|
2005-03-25 05:39:25 +03:00
|
|
|
void maximum(double m) { maximum_ = m; }
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Speling mistakes retained for source compatibility \deprecated */
|
2005-03-25 05:39:25 +03:00
|
|
|
double mininum() const { return (minimum_); }
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Gets the minimum value of the widget. */
|
2006-01-31 19:06:45 +03:00
|
|
|
double minimum() const { return (minimum_); }
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Sets the minimum value of the widget. */
|
2005-03-25 05:39:25 +03:00
|
|
|
void minimum(double m) { minimum_ = m; }
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Sets the minimum and maximum values for the widget. */
|
2005-03-25 05:39:25 +03:00
|
|
|
void range(double a, double b) { minimum_ = a; maximum_ = b; }
|
2005-03-21 02:38:14 +03:00
|
|
|
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);
|
|
|
|
}
|
2008-09-15 20:39:05 +04:00
|
|
|
/**
|
2014-09-12 13:08:41 +04:00
|
|
|
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.
|
2008-09-15 20:39:05 +04:00
|
|
|
*/
|
2005-03-25 05:39:25 +03:00
|
|
|
double step() const { return (step_); }
|
2008-09-15 20:39:05 +04:00
|
|
|
/** See double Fl_Spinner::step() const */
|
2006-12-08 07:41:52 +03:00
|
|
|
void step(double s) {
|
|
|
|
step_ = s;
|
|
|
|
if (step_ != (int)step_) input_.type(FL_FLOAT_INPUT);
|
|
|
|
else input_.type(FL_INT_INPUT);
|
|
|
|
update();
|
|
|
|
}
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Gets the color of the text in the input field. */
|
2005-03-21 02:38:14 +03:00
|
|
|
Fl_Color textcolor() const {
|
|
|
|
return (input_.textcolor());
|
|
|
|
}
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Sets the color of the text in the input field. */
|
2005-03-21 02:38:14 +03:00
|
|
|
void textcolor(Fl_Color c) {
|
|
|
|
input_.textcolor(c);
|
|
|
|
}
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Gets the font of the text in the input field. */
|
2008-04-23 23:09:28 +04:00
|
|
|
Fl_Font textfont() const {
|
2005-03-21 02:38:14 +03:00
|
|
|
return (input_.textfont());
|
|
|
|
}
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Sets the font of the text in the input field. */
|
2008-04-23 23:09:28 +04:00
|
|
|
void textfont(Fl_Font f) {
|
2005-03-21 02:38:14 +03:00
|
|
|
input_.textfont(f);
|
|
|
|
}
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Gets the size of the text in the input field. */
|
2008-08-16 01:11:21 +04:00
|
|
|
Fl_Fontsize textsize() const {
|
2005-03-21 02:38:14 +03:00
|
|
|
return (input_.textsize());
|
|
|
|
}
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Sets the size of the text in the input field. */
|
2008-08-16 01:11:21 +04:00
|
|
|
void textsize(Fl_Fontsize s) {
|
2005-03-21 02:38:14 +03:00
|
|
|
input_.textsize(s);
|
|
|
|
}
|
2011-01-30 11:45:34 +03:00
|
|
|
/** Gets the numeric representation in the input field.
|
2014-09-12 13:08:41 +04:00
|
|
|
\see Fl_Spinner::type(uchar)
|
2008-09-15 20:39:05 +04:00
|
|
|
*/
|
2006-06-22 11:35:39 +04:00
|
|
|
uchar type() const { return (input_.type()); }
|
2011-01-30 11:45:34 +03:00
|
|
|
/** 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.
|
2014-09-12 13:08:41 +04:00
|
|
|
\note type is not a virtual function.
|
2011-01-30 11:45:34 +03:00
|
|
|
*/
|
2014-09-12 13:08:41 +04:00
|
|
|
void type(uchar v) {
|
|
|
|
if (v==FL_FLOAT_INPUT) {
|
|
|
|
format("%.*f");
|
|
|
|
} else {
|
|
|
|
format("%.0f");
|
|
|
|
}
|
|
|
|
input_.type(v);
|
|
|
|
}
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Gets the current value of the widget. */
|
|
|
|
double value() const { return (value_); }
|
2008-09-15 20:39:05 +04:00
|
|
|
/**
|
2008-09-18 02:25:45 +04:00
|
|
|
Sets the current value of the widget.
|
2014-09-12 13:08:41 +04:00
|
|
|
Before setting value to a non-integer value, the spinner
|
|
|
|
type() should be changed to floating point.
|
2008-09-15 20:39:05 +04:00
|
|
|
*/
|
2005-03-25 05:39:25 +03:00
|
|
|
void value(double v) { value_ = v; update(); }
|
2012-05-16 11:25:11 +04:00
|
|
|
/**
|
|
|
|
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()); }
|
2013-05-16 09:02:18 +04:00
|
|
|
/**
|
|
|
|
Change the selection color of the spinner widget's input field.
|
|
|
|
*/
|
|
|
|
void selection_color(Fl_Color val) { input_.selection_color(val); }
|
|
|
|
/**
|
|
|
|
Return the selection color of the spinner widget's input field.
|
|
|
|
*/
|
|
|
|
Fl_Color selection_color() const { return input_.selection_color(); }
|
2005-03-21 02:38:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // !Fl_Spinner_H
|
|
|
|
|
|
|
|
//
|
|
|
|
// End of "$Id$".
|
|
|
|
//
|