Add C++11 Fl_Valuator::format API.

This is helpful for writing language wrapper, in
this particular case for PyFLTK.
This commit is contained in:
Matthias Melcher 2026-01-04 15:50:49 +01:00
parent 357336bd40
commit 0e570fb672
9 changed files with 67 additions and 39 deletions

View File

@ -1,7 +1,7 @@
//
// Color chooser header file for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2019 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -30,6 +30,8 @@
#include <FL/Fl_Choice.H>
#include <FL/Fl_Value_Input.H>
#include <string>
#ifndef FL_DOXYGEN
/** For internal use only */
@ -60,6 +62,7 @@ public:
class FL_EXPORT Flcc_Value_Input : public Fl_Value_Input {
public:
int format(char*) override;
std::string format_str() override;
Flcc_Value_Input(int X, int Y, int W, int H) : Fl_Value_Input(X,Y,W,H) {}
};

View File

@ -1,7 +1,7 @@
//
// Valuator header file for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2022 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -24,6 +24,8 @@
#include "Fl_Widget.H"
#endif
#include <string>
// shared type() values for classes that work in both directions:
#define FL_VERTICAL 0 ///< The valuator can work vertically
#define FL_HORIZONTAL 1 ///< The valuator can work horizontally
@ -126,6 +128,7 @@ public:
int value(double);
virtual int format(char*);
virtual std::string format_str();
double round(double); // round to nearest multiple of step
double clamp(double); // keep in range
double increment(double, int); // add n*step to value

View File

@ -1,7 +1,7 @@
//
// Color chooser for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2023 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -32,6 +32,7 @@
// The "hue box" can be a circle or rectilinear.
// You get a circle by defining this:
#define CIRCLE 1
// And the "hue box" can auto-update when the value changes
// you get this by defining this:
#define UPDATE_HUE_BOX 1
@ -98,11 +99,21 @@ static const Fl_Menu_Item mode_menu[] = {
};
#ifndef FL_DOXYGEN
int Flcc_Value_Input::format(char* buf) {
Fl_Color_Chooser* c = (Fl_Color_Chooser*)parent();
if (c->mode() == M_HEX) return snprintf(buf, 5,"0x%02X", int(value()));
else return Fl_Valuator::format(buf);
}
// Note: although Flcc_Value_Input is marked private in the header files,
// it nevertheless is publicly accessible, so implement this here just in case.
std::string Flcc_Value_Input::format_str() {
char buffer[129];
int size = format(buffer);
return std::string(buffer, size);
}
#endif // !FL_DOXYGEN
void Fl_Color_Chooser::set_valuators() {

View File

@ -1,7 +1,7 @@
//
// Counter widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2022 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -102,8 +102,8 @@ void Fl_Counter::draw() {
draw_box(tbt, tx, y(), tw, h(), FL_BACKGROUND2_COLOR);
fl_font(textfont(), textsize());
fl_color(active_r() ? textcolor() : fl_inactive(textcolor()));
char str[128]; format(str);
fl_draw(str, tx, y(), tw, h(), FL_ALIGN_CENTER);
std::string str = format_str();
fl_draw(str.c_str(), tx, y(), tw, h(), FL_ALIGN_CENTER);
if (Fl::focus() == this) draw_focus(tbt, tx, y(), tw, h());
if (!(damage()&FL_DAMAGE_ALL)) return; // only need to redraw text

View File

@ -1,7 +1,7 @@
//
// Valuator widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2016 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -175,7 +175,7 @@ int Fl_Valuator::format(char* buffer) {
int i, c = 0;
char temp[32];
// output a number with many digits after the decimal point. This
// seems to be needed to get high precission
// seems to be needed to get high precision
snprintf(temp, sizeof(temp), "%.12f", A/B);
// strip all trailing 0's
for (i=(int) strlen(temp)-1; i>0; i--) {
@ -190,3 +190,16 @@ int Fl_Valuator::format(char* buffer) {
// MRS: THIS IS A HACK - RECOMMEND ADDING BUFFER SIZE ARGUMENT
return snprintf(buffer, 128, "%.*f", c, v);
}
/**
\brief C++11 API for Fl_Valuator::format(char* buffer).
Users can override either version to change the format of the text output
in the valuator.
\return the formatted text of the current value
\see Fl_Valuator::format(char* buffer)
*/
std::string Fl_Valuator::format_str() {
char buffer[129];
int size = format(buffer);
return std::string(buffer, size);
}

View File

@ -1,7 +1,7 @@
//
// Value input widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -51,9 +51,8 @@ void Fl_Value_Input::resize(int X, int Y, int W, int H) {
}
void Fl_Value_Input::value_damage() {
char buf[128];
format(buf);
input.value(buf);
std::string buf = format_str();
input.value(buf.c_str());
input.mark(input.insert_position()); // turn off selection highlight
}

View File

@ -1,7 +1,7 @@
//
// Value output widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -34,11 +34,10 @@ void Fl_Value_Output::draw() {
fl_color(color());
fl_rectf(X, Y, W, H);
}
char buf[128];
format(buf);
std::string buf = format_str();
fl_color(active_r() ? textcolor() : fl_inactive(textcolor()));
fl_font(textfont(), textsize());
fl_draw(buf,X,Y,W,H,FL_ALIGN_LEFT);
fl_draw(buf.c_str(),X,Y,W,H,FL_ALIGN_LEFT);
}
int Fl_Value_Output::handle(int event) {

View File

@ -1,7 +1,7 @@
//
// Value Slider widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2022 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -53,11 +53,10 @@ void Fl_Value_Slider::draw() {
sww-Fl::box_dw(box()),
shh-Fl::box_dh(box()));
draw_box(box(),bxx,byy,bww,bhh,color());
char buf[128];
format(buf);
std::string buf = format_str();
fl_font(textfont(), textsize());
fl_color(active_r() ? textcolor() : fl_inactive(textcolor()));
fl_draw(buf, bxx, byy, bww, bhh, FL_ALIGN_CLIP);
fl_draw(buf.c_str(), bxx, byy, bww, bhh, FL_ALIGN_CLIP);
}
int Fl_Value_Slider::handle(int event) {

View File

@ -1,7 +1,7 @@
//
// Adjuster test program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
// Copyright 1998-2026 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
@ -23,29 +23,30 @@
void adjcb(Fl_Widget *o, void *v) {
Fl_Adjuster *a = (Fl_Adjuster*)o;
Fl_Box *b = (Fl_Box *)v;
a->format((char *)(b->label()));
std::string new_label = a->format_str();
b->copy_label(new_label.c_str());
b->redraw();
}
int main(int argc, char ** argv) {
Fl_Double_Window window(320,100,argv[0]);
Fl_Double_Window window(320, 100, argv[0]);
char buf1[100];
Fl_Box b1(FL_DOWN_BOX,20,30,80,25,buf1);
b1.color(FL_WHITE);
Fl_Adjuster a1(20+80,30,3*25,25);
a1.callback(adjcb,&b1);
adjcb(&a1,&b1);
Fl_Box b1(20, 30, 80, 25);
b1.box(FL_DOWN_BOX);
b1.color(FL_WHITE);
Fl_Adjuster a1(20+80, 30, 3*25, 25);
a1.callback(adjcb, &b1);
adjcb(&a1, &b1);
char buf2[100];
Fl_Box b2(FL_DOWN_BOX,20+80+4*25,30,80,25,buf2);
b2.color(FL_WHITE);
Fl_Adjuster a2(b2.x()+b2.w(),10,25,3*25);
a2.callback(adjcb,&b2);
adjcb(&a2,&b2);
Fl_Box b2(20+80+4*25, 30, 80, 25);
b2.box(FL_DOWN_BOX);
b2.color(FL_WHITE);
Fl_Adjuster a2(b2.x()+b2.w(), 10, 25, 3*25);
a2.callback(adjcb, &b2);
adjcb(&a2, &b2);
window.resizable(window);
window.end();
window.show(argc, argv);
return Fl::run();
window.resizable(window);
window.end();
window.show(argc, argv);
return Fl::run();
}