diff options
| author | Matthias Melcher <github@matthiasm.com> | 2026-01-04 15:50:49 +0100 |
|---|---|---|
| committer | Matthias Melcher <github@matthiasm.com> | 2026-01-04 15:50:56 +0100 |
| commit | 0e570fb6729ff6b97249256c2c9d5f3aa58c7bb4 (patch) | |
| tree | 7140e3b2d97c6f7852e915759918d14bf6b35b00 /src | |
| parent | 357336bd40d868fd65e95c61d1adaf9de0fb7811 (diff) | |
Add C++11 Fl_Valuator::format API.
This is helpful for writing language wrapper, in
this particular case for PyFLTK.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Fl_Color_Chooser.cxx | 13 | ||||
| -rw-r--r-- | src/Fl_Counter.cxx | 6 | ||||
| -rw-r--r-- | src/Fl_Valuator.cxx | 17 | ||||
| -rw-r--r-- | src/Fl_Value_Input.cxx | 7 | ||||
| -rw-r--r-- | src/Fl_Value_Output.cxx | 7 | ||||
| -rw-r--r-- | src/Fl_Value_Slider.cxx | 7 |
6 files changed, 39 insertions, 18 deletions
diff --git a/src/Fl_Color_Chooser.cxx b/src/Fl_Color_Chooser.cxx index 209f4b45b..341a51347 100644 --- a/src/Fl_Color_Chooser.cxx +++ b/src/Fl_Color_Chooser.cxx @@ -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() { diff --git a/src/Fl_Counter.cxx b/src/Fl_Counter.cxx index 17a1efa19..3536a2213 100644 --- a/src/Fl_Counter.cxx +++ b/src/Fl_Counter.cxx @@ -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 diff --git a/src/Fl_Valuator.cxx b/src/Fl_Valuator.cxx index 38383551e..90fa23325 100644 --- a/src/Fl_Valuator.cxx +++ b/src/Fl_Valuator.cxx @@ -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); +} diff --git a/src/Fl_Value_Input.cxx b/src/Fl_Value_Input.cxx index 37be93e80..c44386662 100644 --- a/src/Fl_Value_Input.cxx +++ b/src/Fl_Value_Input.cxx @@ -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 } diff --git a/src/Fl_Value_Output.cxx b/src/Fl_Value_Output.cxx index 3274cd114..924829937 100644 --- a/src/Fl_Value_Output.cxx +++ b/src/Fl_Value_Output.cxx @@ -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) { diff --git a/src/Fl_Value_Slider.cxx b/src/Fl_Value_Slider.cxx index 918d07742..776632751 100644 --- a/src/Fl_Value_Slider.cxx +++ b/src/Fl_Value_Slider.cxx @@ -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) { |
