Another one.

This one is annoying.  Sometimes `const char *` means
`std::string`... but sometimes it's `std::optional< std::string >`.

A bit of a pretzel.
This commit is contained in:
2025-03-03 13:36:09 -05:00
parent fad3dbc39a
commit 4616cbac27

View File

@ -18,6 +18,9 @@
*/ */
#include <string>
#include <string_view>
#include <optional>
#include "fltkcore.hh" #include "fltkcore.hh"
#include "fltkflatview.hh" #include "fltkflatview.hh"
@ -56,8 +59,7 @@ static Fl_Color fltkui_dimmed(Fl_Color c, Fl_Color bg)
class CustInput2 : public Fl_Input { class CustInput2 : public Fl_Input {
public: public:
CustInput2 (int x, int y, int w, int h, const char* l=0); CustInput2 (int x, int y, int w, int h, const char* l=0);
~CustInput2 () { if (placeholder) free(placeholder); }; void set_placeholder(std::string_view str);
void set_placeholder(const char *str);
int show_placeholder(); int show_placeholder();
int show_normal(const char *str); int show_normal(const char *str);
void textcolor(Fl_Color c); void textcolor(Fl_Color c);
@ -66,7 +68,7 @@ public:
const char* value(); const char* value();
int handle(int e); int handle(int e);
private: private:
char *placeholder; std::optional< std::string > placeholder;
bool showing_placeholder; bool showing_placeholder;
Fl_Color usual_color; Fl_Color usual_color;
int usual_type; int usual_type;
@ -75,7 +77,6 @@ private:
CustInput2::CustInput2 (int x, int y, int w, int h, const char* l) : CustInput2::CustInput2 (int x, int y, int w, int h, const char* l) :
Fl_Input(x,y,w,h,l) Fl_Input(x,y,w,h,l)
{ {
placeholder = NULL;
showing_placeholder = false; showing_placeholder = false;
usual_color = FL_BLACK; /* just init until widget style is set */ usual_color = FL_BLACK; /* just init until widget style is set */
} }
@ -101,7 +102,7 @@ int CustInput2::show_placeholder()
showing_placeholder = true; showing_placeholder = true;
Fl_Input::textcolor(fltkui_dimmed(usual_color, color())); Fl_Input::textcolor(fltkui_dimmed(usual_color, color()));
Fl_Input::input_type(FL_NORMAL_INPUT); Fl_Input::input_type(FL_NORMAL_INPUT);
ret = Fl_Input::value(placeholder); ret = Fl_Input::value(placeholder.value().c_str());
position(0); position(0);
return ret; return ret;
} }
@ -109,11 +110,9 @@ int CustInput2::show_placeholder()
/* /*
* Set the placeholder text. * Set the placeholder text.
*/ */
void CustInput2::set_placeholder(const char *str) void CustInput2::set_placeholder(const std::string_view str)
{ {
if (placeholder) placeholder = str;
free(placeholder);
placeholder = dStrdup(str);
if ((Fl::focus() != this) && !*value()) { if ((Fl::focus() != this) && !*value()) {
show_placeholder(); show_placeholder();