aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcorvid <devnull@localhost>2015-02-02 22:23:45 +0000
committercorvid <devnull@localhost>2015-02-02 22:23:45 +0000
commit74a4875a72db4bbfd09432de7ed46445eccadb1a (patch)
tree965b2139bc7ba81c893ed5e666799349fd76d932
parent014a83d2f53cd49bd2540c53ee56f26d880fe2f5 (diff)
TEXTAREA placeholder attribute
-rw-r--r--dw/fltkplatform.cc5
-rw-r--r--dw/fltkplatform.hh3
-rw-r--r--dw/fltkui.cc180
-rw-r--r--dw/fltkui.hh7
-rw-r--r--dw/ui.hh3
-rw-r--r--src/form.cc4
-rw-r--r--test/dw_ui_test.cc3
7 files changed, 171 insertions, 34 deletions
diff --git a/dw/fltkplatform.cc b/dw/fltkplatform.cc
index 67b04688..948c1444 100644
--- a/dw/fltkplatform.cc
+++ b/dw/fltkplatform.cc
@@ -422,9 +422,10 @@ FltkPlatform::FltkResourceFactory::createEntryResource (int size,
core::ui::MultiLineTextResource *
FltkPlatform::FltkResourceFactory::createMultiLineTextResource (int cols,
- int rows)
+ int rows,
+ const char *placeholder)
{
- return new ui::FltkMultiLineTextResource (platform, cols, rows);
+ return new ui::FltkMultiLineTextResource (platform, cols, rows,placeholder);
}
core::ui::CheckButtonResource *
diff --git a/dw/fltkplatform.hh b/dw/fltkplatform.hh
index 5aa86e62..60dca7f2 100644
--- a/dw/fltkplatform.hh
+++ b/dw/fltkplatform.hh
@@ -113,7 +113,8 @@ private:
const char *label,
const char *placeholder);
core::ui::MultiLineTextResource *createMultiLineTextResource (int cols,
- int rows);
+ int rows,
+ const char *placeholder);
core::ui::CheckButtonResource *createCheckButtonResource (bool
activated);
core::ui::RadioButtonResource *
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 ()
diff --git a/dw/fltkui.hh b/dw/fltkui.hh
index 0ceef003..09cdc978 100644
--- a/dw/fltkui.hh
+++ b/dw/fltkui.hh
@@ -331,17 +331,16 @@ class FltkMultiLineTextResource:
public FltkSpecificResource <dw::core::ui::MultiLineTextResource>
{
private:
- Fl_Text_Buffer *buffer;
- char *text_copy;
bool editable;
int numCols, numRows;
-
+ char *placeholder;
protected:
Fl_Widget *createNewWidget (core::Allocation *allocation);
void setWidgetStyle (Fl_Widget *widget, core::style::Style *style);
public:
- FltkMultiLineTextResource (FltkPlatform *platform, int cols, int rows);
+ FltkMultiLineTextResource (FltkPlatform *platform, int cols, int rows,
+ const char *placeholder);
~FltkMultiLineTextResource ();
void sizeRequest (core::Requisition *requisition);
diff --git a/dw/ui.hh b/dw/ui.hh
index f31b44d7..c64880b4 100644
--- a/dw/ui.hh
+++ b/dw/ui.hh
@@ -583,7 +583,8 @@ public:
const char *label,
const char *placeholder) = 0;
virtual MultiLineTextResource *createMultiLineTextResource (int cols,
- int rows) = 0;
+ int rows,
+ const char *placeholder) = 0;
virtual CheckButtonResource *createCheckButtonResource (bool activated) = 0;
virtual RadioButtonResource *createRadioButtonResource (RadioButtonResource
*groupedWith,
diff --git a/src/form.cc b/src/form.cc
index 366b00b9..92ee3a42 100644
--- a/src/form.cc
+++ b/src/form.cc
@@ -669,9 +669,11 @@ void Html_tag_content_textarea(DilloHtml *html, const char *tag, int tagsize)
if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "name")))
name = dStrdup(attrbuf);
+ attrbuf = a_Html_get_attr(html, tag, tagsize, "placeholder");
+
ResourceFactory *factory = HT2LT(html)->getResourceFactory();
MultiLineTextResource *textres =
- factory->createMultiLineTextResource (cols, rows);
+ factory->createMultiLineTextResource (cols, rows, attrbuf);
Embed *embed = new Embed(textres);
/* Readonly or not? */
diff --git a/test/dw_ui_test.cc b/test/dw_ui_test.cc
index b70349ca..f24cc9b9 100644
--- a/test/dw_ui_test.cc
+++ b/test/dw_ui_test.cc
@@ -87,7 +87,8 @@ int main(int argc, char **argv)
layout->getResourceFactory()->createEntryResource (10, true, NULL,
"password field!");
MultiLineTextResource *textres =
- layout->getResourceFactory()->createMultiLineTextResource (15,3);
+ layout->getResourceFactory()->createMultiLineTextResource (15,3,
+ "textarea placeholder!");
RadioButtonResource *radiores1 =
layout->getResourceFactory()->createRadioButtonResource (NULL, false);
RadioButtonResource *radiores2 =