summaryrefslogtreecommitdiff
path: root/dw/fltkui.cc
diff options
context:
space:
mode:
Diffstat (limited to 'dw/fltkui.cc')
-rw-r--r--dw/fltkui.cc180
1 files changed, 156 insertions, 24 deletions
diff --git a/dw/fltkui.cc b/dw/fltkui.cc
index b2d18ba0..846ef87a 100644
--- a/dw/fltkui.cc
+++ b/dw/fltkui.cc
@@ -37,6 +37,13 @@
#include <stdio.h>
//----------------------------------------------------------------------------
+
+static Fl_Color fltkui_dimmed(Fl_Color c, Fl_Color bg)
+{
+ return fl_color_average(c, bg, .33f);
+};
+
+//----------------------------------------------------------------------------
/*
* Local sub classes
*/
@@ -58,7 +65,6 @@ public:
const char* value();
int handle(int e);
private:
- Fl_Color dimmed(Fl_Color c) {return fl_color_average(c, color(), .33f); };
char *placeholder;
bool showing_placeholder;
Fl_Color usual_color;
@@ -90,7 +96,7 @@ int CustInput2::show_normal(const char *str)
int CustInput2::show_placeholder()
{
showing_placeholder = true;
- Fl_Input::textcolor(dimmed(usual_color));
+ Fl_Input::textcolor(fltkui_dimmed(usual_color, color()));
Fl_Input::input_type(FL_NORMAL_INPUT);
return Fl_Input::value(placeholder);
}
@@ -116,7 +122,7 @@ void CustInput2::textcolor(Fl_Color c)
{
usual_color = c;
if (showing_placeholder)
- c = dimmed(c);
+ c = fltkui_dimmed(c, color());
Fl_Input::textcolor(c);
}
@@ -195,6 +201,140 @@ int CustInput2::handle(int e)
return rc;
}
+/*
+ * Used to show optional placeholder text.
+ */
+class CustTextEditor : public Fl_Text_Editor {
+public:
+ CustTextEditor (int x, int y, int w, int h, const char* l=0);
+ ~CustTextEditor ();
+ void set_placeholder(const char *str);
+ void show_placeholder();
+ void show_normal(const char *str);
+ void textcolor(Fl_Color c);
+ void value(const char* str);
+ char* value();
+ int handle(int e);
+private:
+ char *placeholder;
+ bool showing_placeholder;
+ Fl_Color usual_color;
+ char *text_copy;
+};
+
+CustTextEditor::CustTextEditor (int x, int y, int w, int h, const char* l) :
+ Fl_Text_Editor(x,y,w,h,l)
+{
+ placeholder = NULL;
+ showing_placeholder = false;
+ buffer(new Fl_Text_Buffer());
+ usual_color = FL_BLACK; /* just init until widget style is set */
+ text_copy = NULL;
+};
+
+CustTextEditor::~CustTextEditor ()
+{
+ Fl_Text_Buffer *buf = buffer();
+
+ buffer(NULL);
+ delete buf;
+
+ if (placeholder)
+ free(placeholder);
+ if (text_copy)
+ free(text_copy);
+}
+
+/*
+ * Show normal text.
+ */
+void CustTextEditor::show_normal(const char *str)
+{
+ showing_placeholder = false;
+ Fl_Text_Editor::textcolor(usual_color);
+ buffer()->text(str);
+}
+
+/*
+ * Show the placeholder text.
+ */
+void CustTextEditor::show_placeholder()
+{
+ showing_placeholder = true;
+ Fl_Text_Editor::textcolor(fltkui_dimmed(usual_color, color()));
+ buffer()->text(placeholder);
+}
+
+/*
+ * Set the placeholder text.
+ */
+void CustTextEditor::set_placeholder(const char *str)
+{
+ if (placeholder)
+ free(placeholder);
+ placeholder = strdup(str);
+
+ if ((Fl::focus() != this) && buffer()->length() == 0) {
+ show_placeholder();
+ }
+}
+
+/*
+ * Set the text color.
+ */
+void CustTextEditor::textcolor(Fl_Color c)
+{
+ usual_color = c;
+ if (showing_placeholder)
+ c = fltkui_dimmed(c, color());
+ Fl_Text_Editor::textcolor(c);
+}
+
+/*
+ * Set the value of the input.
+ */
+void CustTextEditor::value(const char *str)
+{
+ if (placeholder && (!str || !*str) && Fl::focus() != this)
+ show_placeholder();
+ else
+ show_normal(str);
+}
+
+/*
+ * Return the value (text) of the input.
+ */
+char* CustTextEditor::value()
+{
+ /* FLTK-1.3 insists upon returning a new copy of the buffer text, so
+ * we have to keep track of it.
+ */
+ if (text_copy)
+ free(text_copy);
+ text_copy = showing_placeholder ? strdup("") : buffer()->text();
+ return text_copy;
+}
+
+int CustTextEditor::handle(int e)
+{
+ int rc;
+
+ if (e == FL_UNFOCUS) {
+ if (placeholder && buffer()->length() == 0) {
+ show_placeholder();
+ }
+ }
+
+ rc = Fl_Text_Editor::handle(e);
+
+ if (rc && e == FL_FOCUS) {
+ // Nonzero return from handle() should mean that focus was accepted.
+ if (showing_placeholder)
+ show_normal("");
+ }
+ return rc;
+}
+
/*
* Used to handle some keystrokes as shortcuts to option menuitems
@@ -884,11 +1024,10 @@ static int kf_backspace_word (int c, Fl_Text_Editor *e)
}
FltkMultiLineTextResource::FltkMultiLineTextResource (FltkPlatform *platform,
- int cols, int rows):
+ int cols, int rows,
+ const char *placeholder):
FltkSpecificResource <dw::core::ui::MultiLineTextResource> (platform)
{
- buffer = new Fl_Text_Buffer;
- text_copy = NULL;
editable = false;
numCols = cols;
@@ -903,42 +1042,41 @@ FltkMultiLineTextResource::FltkMultiLineTextResource (FltkPlatform *platform,
MSG_WARN("numRows = %d is set to 1.\n", numRows);
numRows = 1;
}
+ this->placeholder = placeholder ? strdup(placeholder) : NULL;
init (platform);
}
FltkMultiLineTextResource::~FltkMultiLineTextResource ()
{
- /* Free memory avoiding a double-free of text buffers */
- ((Fl_Text_Editor *) widget)->buffer (0);
- delete buffer;
- if (text_copy)
- free(text_copy);
+ if (placeholder)
+ free(placeholder);
}
Fl_Widget *FltkMultiLineTextResource::createNewWidget (core::Allocation
*allocation)
{
- Fl_Text_Editor *text =
- new Fl_Text_Editor (allocation->x, allocation->y, allocation->width,
+ CustTextEditor *text =
+ new CustTextEditor (allocation->x, allocation->y, allocation->width,
allocation->ascent + allocation->descent);
text->wrap_mode(Fl_Text_Display::WRAP_AT_BOUNDS, 0);
- text->buffer (buffer);
text->remove_key_binding(FL_BackSpace, FL_TEXT_EDITOR_ANY_STATE);
text->add_key_binding(FL_BackSpace, 0, Fl_Text_Editor::kf_backspace);
text->add_key_binding(FL_BackSpace, FL_CTRL, kf_backspace_word);
+ if (placeholder)
+ text->set_placeholder(placeholder);
return text;
}
void FltkMultiLineTextResource::setWidgetStyle (Fl_Widget *widget,
core::style::Style *style)
{
- Fl_Text_Editor *ed = (Fl_Text_Editor *)widget;
+ CustTextEditor *ed = (CustTextEditor *)widget;
FltkResource::setWidgetStyle(widget, style);
ed->textcolor(widget->labelcolor());
- ed->cursor_color(ed->textcolor());
+ ed->cursor_color(widget->labelcolor());
ed->textsize(ed->labelsize());
ed->textfont(ed->labelfont());
}
@@ -973,18 +1111,12 @@ void FltkMultiLineTextResource::sizeRequest (core::Requisition *requisition)
const char *FltkMultiLineTextResource::getText ()
{
- /* FLTK-1.3 insists upon returning a new copy of the buffer text, so
- * we have to keep track of it.
- */
- if (text_copy)
- free(text_copy);
- text_copy = buffer->text();
- return text_copy;
+ return ((CustTextEditor*)widget)->value ();
}
void FltkMultiLineTextResource::setText (const char *text)
{
- buffer->text (text);
+ ((CustTextEditor*)widget)->value (text);
}
bool FltkMultiLineTextResource::isEditable ()