1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Valuator widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2008-09-16 11:26:22 +04:00
|
|
|
// Copyright 1998-2008 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// Base class for sliders and all other one-value "knobs"
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Valuator.H>
|
|
|
|
#include <FL/math.h>
|
|
|
|
#include <stdio.h>
|
2004-02-26 06:06:41 +03:00
|
|
|
#include "flstring.h"
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
Fl_Valuator::Fl_Valuator(int X, int Y, int W, int H, const char* L)
|
2008-09-15 12:41:54 +04:00
|
|
|
/**
|
|
|
|
Creates a new Fl_Valuator widget using the given position,
|
|
|
|
size, and label string. The default boxtype is FL_NO_BOX.
|
|
|
|
*/
|
|
|
|
: Fl_Widget(X,Y,W,H,L) {
|
1998-10-06 22:21:25 +04:00
|
|
|
align(FL_ALIGN_BOTTOM);
|
|
|
|
when(FL_WHEN_CHANGED);
|
|
|
|
value_ = 0;
|
2005-12-30 13:13:17 +03:00
|
|
|
previous_value_ = 1;
|
1998-10-06 22:21:25 +04:00
|
|
|
min = 0;
|
|
|
|
max = 1;
|
|
|
|
A = 0.0;
|
|
|
|
B = 1;
|
|
|
|
}
|
|
|
|
|
2001-01-21 09:00:59 +03:00
|
|
|
const double epsilon = 4.66e-10;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
/** See double Fl_Valuator::step() const */
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Valuator::step(double s) {
|
|
|
|
if (s < 0) s = -s;
|
|
|
|
A = rint(s);
|
|
|
|
B = 1;
|
2001-01-21 09:00:59 +03:00
|
|
|
while (fabs(s-A/B) > epsilon && B<=(0x7fffffff/10)) {B *= 10; A = rint(s*B);}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2008-09-18 02:25:45 +04:00
|
|
|
/** Sets the step value to 1/10<SUP>digits</SUP>.*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Valuator::precision(int p) {
|
|
|
|
A = 1.0;
|
|
|
|
for (B = 1; p--;) B *= 10;
|
|
|
|
}
|
2008-09-15 12:41:54 +04:00
|
|
|
/** Asks for partial redraw */
|
1998-10-19 21:53:09 +04:00
|
|
|
void Fl_Valuator::value_damage() {damage(FL_DAMAGE_EXPOSE);} // by default do partial-redraw
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-18 02:25:45 +04:00
|
|
|
/**
|
|
|
|
Sets the current value. The new value is <I>not</I>
|
|
|
|
clamped or otherwise changed before storing it. Use
|
|
|
|
clamp() or round() to modify the value before
|
|
|
|
calling value(). The widget is redrawn if the new value
|
|
|
|
is different than the current one. The initial value is zero.
|
|
|
|
<P>changed() will return true if the user has moved the slider,
|
|
|
|
but it will be turned off by value(x) and just before doing a callback
|
|
|
|
(the callback can turn it back on if desired).
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Valuator::value(double v) {
|
|
|
|
clear_changed();
|
|
|
|
if (v == value_) return 0;
|
|
|
|
value_ = v;
|
|
|
|
value_damage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Fl_Valuator::softclamp(double v) {
|
|
|
|
int which = (min<=max);
|
|
|
|
double p = previous_value_;
|
|
|
|
if ((v<min)==which && p!=min && (p<min)!=which) return min;
|
|
|
|
else if ((v>max)==which && p!=max && (p>max)!=which) return max;
|
|
|
|
else return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inline void Fl_Valuator::handle_push() {previous_value_ = value_;}
|
|
|
|
|
|
|
|
void Fl_Valuator::handle_drag(double v) {
|
|
|
|
if (v != value_) {
|
|
|
|
value_ = v;
|
|
|
|
value_damage();
|
2004-07-27 20:02:21 +04:00
|
|
|
set_changed();
|
1998-10-06 22:21:25 +04:00
|
|
|
if (when() & FL_WHEN_CHANGED) do_callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Valuator::handle_release() {
|
|
|
|
if (when()&FL_WHEN_RELEASE) {
|
|
|
|
// insure changed() is off even if no callback is done. It may have
|
|
|
|
// been turned on by the drag, and then the slider returned to it's
|
|
|
|
// initial position:
|
|
|
|
clear_changed();
|
|
|
|
// now do the callback only if slider in new position or always is on:
|
2004-07-27 20:02:21 +04:00
|
|
|
if (value_ != previous_value_ || when() & FL_WHEN_NOT_CHANGED) {
|
1998-10-06 22:21:25 +04:00
|
|
|
do_callback();
|
2004-07-27 20:02:21 +04:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
/**
|
|
|
|
Round the passed value to the nearest step increment. Does
|
|
|
|
nothing if step is zero.
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
double Fl_Valuator::round(double v) {
|
|
|
|
if (A) return rint(v*B/A)*A/B;
|
|
|
|
else return v;
|
|
|
|
}
|
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
/** Clamps the passed value to the valuator range.*/
|
1998-10-06 22:21:25 +04:00
|
|
|
double Fl_Valuator::clamp(double v) {
|
|
|
|
if ((v<min)==(min<=max)) return min;
|
|
|
|
else if ((v>max)==(min<=max)) return max;
|
|
|
|
else return v;
|
|
|
|
}
|
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
/**
|
|
|
|
Adds n times the step value to the passed value. If
|
|
|
|
step was set to zero it uses fabs(maximum() - minimum()) /
|
|
|
|
100.
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
double Fl_Valuator::increment(double v, int n) {
|
|
|
|
if (!A) return v+n*(max-min)/100;
|
|
|
|
if (min > max) n = -n;
|
|
|
|
return (rint(v*B/A)+n)*A/B;
|
|
|
|
}
|
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
/**
|
|
|
|
Uses internal rules to format the fields numerical value into
|
|
|
|
the character array pointed to by the passed parameter.</P>
|
|
|
|
|
|
|
|
<P>The actual format used depends on the current step value. If
|
|
|
|
the step value has been set to zero then a %g format is used.
|
|
|
|
If the step value is non-zero, then a %.*f format is used,
|
|
|
|
where the precision is calculated to show sufficient digits
|
|
|
|
for the current step value. An integer step value, such as 1
|
|
|
|
or 1.0, gives a precision of 0, so the formatted value will
|
|
|
|
appear as an integer.</P>
|
|
|
|
|
|
|
|
<P>This method is used by the Fl_Value_... group of widgets to
|
|
|
|
format the current value into a text string.
|
|
|
|
The return value is the length of the formatted text.
|
|
|
|
The formatted value is written into in <i>buffer</i>.
|
|
|
|
<i>buffer</i> should have space for at least 128 bytes.</P>
|
|
|
|
|
|
|
|
<P>You may override this function to create your own text formatting.
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Valuator::format(char* buffer) {
|
|
|
|
double v = value();
|
2004-02-26 06:06:41 +03:00
|
|
|
// MRS: THIS IS A HACK - RECOMMEND ADDING BUFFER SIZE ARGUMENT
|
'Anonymous' reported a problem when using Fl_Value_Input. It seemed,
that contrary to the man pages, a fractional 'step' value above 1
would not create a floating point input.
In fact it does, but the output was formatted wrong so that the
digits after the decimal point were never rendered.
I changed the Fl_Valuator::format(double v) function how I beleive
it should format the output correctly, but as already stated by
the original author in the source code, this is a hack that should
be fixed by providing a 'precission' setting for valuators.
Anyway. My fix makes sure that all digits right of the decimal
point are always rendered, so that the step value will show full
precision. This gives a much better behavior in respect to steps values
like 2.5, 3.75, etc., but also leads to 8 digits after the decimal point
for step(1.0/3.0)... .
I suggest that we keep this change (hence the commit), risking that
rendering of valuator text will change in a few cases (odd step() values).
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4091 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2005-03-09 01:55:10 +03:00
|
|
|
if (!A || !B) return snprintf(buffer, 128, "%g", v);
|
2005-08-08 19:01:45 +04:00
|
|
|
|
|
|
|
// Figure out how many digits are required to correctly format the
|
|
|
|
// value.
|
2005-09-09 00:57:27 +04:00
|
|
|
int i, c = 0;
|
2005-09-09 15:16:21 +04:00
|
|
|
char temp[32];
|
2005-09-09 00:57:27 +04:00
|
|
|
// output a number with many digits after the decimal point. This
|
|
|
|
// seems to be needed to get high precission
|
|
|
|
snprintf(temp, sizeof(temp), "%.12f", A/B);
|
|
|
|
// strip all trailing 0's
|
|
|
|
for (i=strlen(temp)-1; i>0; i--) {
|
|
|
|
if (temp[i]!='0') break;
|
|
|
|
}
|
|
|
|
// count digits until we find the decimal point (or comma or whatever
|
|
|
|
// letter is set in the current locale)
|
|
|
|
for (; i>0; i--, c++) {
|
|
|
|
if (!isdigit(temp[i])) break;
|
|
|
|
}
|
2005-08-08 19:01:45 +04:00
|
|
|
|
|
|
|
// MRS: THIS IS A HACK - RECOMMEND ADDING BUFFER SIZE ARGUMENT
|
2005-09-09 00:57:27 +04:00
|
|
|
return snprintf(buffer, 128, "%.*f", c, v);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|