|
fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation) ![]() |
00001 // 00002 // "$Id: Fl_Valuator.cxx 7903 2010-11-28 21:06:39Z matt $" 00003 // 00004 // Valuator widget for the Fast Light Tool Kit (FLTK). 00005 // 00006 // Copyright 1998-2010 by Bill Spitzak and others. 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Library General Public 00010 // License as published by the Free Software Foundation; either 00011 // version 2 of the License, or (at your option) any later version. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // Library General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Library General Public 00019 // License along with this library; if not, write to the Free Software 00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00021 // USA. 00022 // 00023 // Please report all bugs and problems on the following page: 00024 // 00025 // http://www.fltk.org/str.php 00026 // 00027 00028 00029 // Base class for sliders and all other one-value "knobs" 00030 00031 #include <FL/Fl.H> 00032 #include <FL/Fl_Valuator.H> 00033 #include <FL/math.h> 00034 #include <stdio.h> 00035 #include "flstring.h" 00036 00037 Fl_Valuator::Fl_Valuator(int X, int Y, int W, int H, const char* L) 00042 : Fl_Widget(X,Y,W,H,L) { 00043 align(FL_ALIGN_BOTTOM); 00044 when(FL_WHEN_CHANGED); 00045 value_ = 0; 00046 previous_value_ = 1; 00047 min = 0; 00048 max = 1; 00049 A = 0.0; 00050 B = 1; 00051 } 00052 00053 const double epsilon = 4.66e-10; 00054 00056 void Fl_Valuator::step(double s) { 00057 if (s < 0) s = -s; 00058 A = rint(s); 00059 B = 1; 00060 while (fabs(s-A/B) > epsilon && B<=(0x7fffffff/10)) {B *= 10; A = rint(s*B);} 00061 } 00062 00064 void Fl_Valuator::precision(int p) { 00065 A = 1.0; 00066 for (B = 1; p--;) B *= 10; 00067 } 00069 void Fl_Valuator::value_damage() {damage(FL_DAMAGE_EXPOSE);} // by default do partial-redraw 00070 00081 int Fl_Valuator::value(double v) { 00082 clear_changed(); 00083 if (v == value_) return 0; 00084 value_ = v; 00085 value_damage(); 00086 return 1; 00087 } 00089 double Fl_Valuator::softclamp(double v) { 00090 int which = (min<=max); 00091 double p = previous_value_; 00092 if ((v<min)==which && p!=min && (p<min)!=which) return min; 00093 else if ((v>max)==which && p!=max && (p>max)!=which) return max; 00094 else return v; 00095 } 00096 00097 // inline void Fl_Valuator::handle_push() {previous_value_ = value_;} 00099 void Fl_Valuator::handle_drag(double v) { 00100 if (v != value_) { 00101 value_ = v; 00102 value_damage(); 00103 set_changed(); 00104 if (when() & FL_WHEN_CHANGED) do_callback(); 00105 } 00106 } 00108 void Fl_Valuator::handle_release() { 00109 if (when()&FL_WHEN_RELEASE) { 00110 // insure changed() is off even if no callback is done. It may have 00111 // been turned on by the drag, and then the slider returned to it's 00112 // initial position: 00113 clear_changed(); 00114 // now do the callback only if slider in new position or always is on: 00115 if (value_ != previous_value_ || when() & FL_WHEN_NOT_CHANGED) { 00116 do_callback(); 00117 } 00118 } 00119 } 00120 00125 double Fl_Valuator::round(double v) { 00126 if (A) return rint(v*B/A)*A/B; 00127 else return v; 00128 } 00129 00131 double Fl_Valuator::clamp(double v) { 00132 if ((v<min)==(min<=max)) return min; 00133 else if ((v>max)==(min<=max)) return max; 00134 else return v; 00135 } 00136 00142 double Fl_Valuator::increment(double v, int n) { 00143 if (!A) return v+n*(max-min)/100; 00144 if (min > max) n = -n; 00145 return (rint(v*B/A)+n)*A/B; 00146 } 00147 00168 int Fl_Valuator::format(char* buffer) { 00169 double v = value(); 00170 // MRS: THIS IS A HACK - RECOMMEND ADDING BUFFER SIZE ARGUMENT 00171 if (!A || !B) return snprintf(buffer, 128, "%g", v); 00172 00173 // Figure out how many digits are required to correctly format the 00174 // value. 00175 int i, c = 0; 00176 char temp[32]; 00177 // output a number with many digits after the decimal point. This 00178 // seems to be needed to get high precission 00179 snprintf(temp, sizeof(temp), "%.12f", A/B); 00180 // strip all trailing 0's 00181 for (i=strlen(temp)-1; i>0; i--) { 00182 if (temp[i]!='0') break; 00183 } 00184 // count digits until we find the decimal point (or comma or whatever 00185 // letter is set in the current locale) 00186 for (; i>0; i--, c++) { 00187 if (!isdigit(temp[i])) break; 00188 } 00189 00190 // MRS: THIS IS A HACK - RECOMMEND ADDING BUFFER SIZE ARGUMENT 00191 return snprintf(buffer, 128, "%.*f", c, v); 00192 } 00193 00194 // 00195 // End of "$Id: Fl_Valuator.cxx 7903 2010-11-28 21:06:39Z matt $". 00196 //