Empirical
TextArea.h
Go to the documentation of this file.
1 
13 #ifndef EMP_WEB_TEXT_AREA_H
14 #define EMP_WEB_TEXT_AREA_H
15 
16 #include "Widget.h"
17 
18 namespace emp {
19 namespace web {
20 
24 
25  class TextArea : public internal::WidgetFacet<TextArea> {
26  friend class TextAreaInfo;
27  protected:
28 
29  // TextAreas associated with the same DOM element share a single TextAreaInfo object.
31  friend TextArea;
32  protected:
33  int cols;
34  int rows;
35  int max_length;
36 
37  std::string cur_text;
38 
39  bool autofocus;
40  bool disabled;
41 
42  std::function<void(const std::string &)> callback;
43  uint32_t callback_id;
44 
45  TextAreaInfo(const std::string & in_id="") : internal::WidgetInfo(in_id) { ; }
46  TextAreaInfo(const TextAreaInfo &) = delete; // No copies of INFO allowed
47  TextAreaInfo & operator=(const TextAreaInfo &) = delete; // No copies of INFO allowed
48  virtual ~TextAreaInfo() {
49  if (callback_id) emp::JSDelete(callback_id); // Delete callback wrapper.
50  }
51 
52  std::string TypeName() const override { return "TextAreaInfo"; }
53  virtual bool IsTextAreaInfo() const override { return true; }
54 
55  void DoCallback(std::string in_text) {
56  cur_text = in_text;
57  if (callback) callback(cur_text);
59  }
60 
61  virtual void GetHTML(std::stringstream & HTML) override {
62  HTML.str(""); // Clear the current text.
63  HTML << "<textarea "; // Start the textarea tag.
64  if (disabled) { HTML << " disabled=true"; } // Check if should be disabled
65  HTML << " id=\"" << id << "\""; // Indicate ID.
66  HTML << " onkeyup=\"emp.Callback(" << callback_id << ", $(this).val())\"";
67  HTML << " rows=\"" << rows << "\""
68  << " cols=\"" << cols << "\"";
69  if (max_length >= 0) { HTML << " maxlength=\"" << max_length << "\""; }
70  HTML << ">" << cur_text << "</textarea>"; // Close and label the textarea
71  }
72 
73  void UpdateAutofocus(bool in_af) {
74  autofocus = in_af;
75  if (state == Widget::ACTIVE) ReplaceHTML(); // If node is active, immediately redraw!
76  }
77 
78  void UpdateCallback(const std::function<void(const std::string &)> & in_cb) {
79  callback = in_cb;
80  }
81 
82  void UpdateDisabled(bool in_dis) {
83  disabled = in_dis;
84  if (state == Widget::ACTIVE) ReplaceHTML(); // If node is active, immediately redraw!
85  }
86 
87  void UpdateText(const std::string & in_string) {
88  EM_ASM_ARGS({
89  var id = Pointer_stringify($0);
90  var text = Pointer_stringify($1);
91  $('#' + id).val(text);
92  }, id.c_str(), in_string.c_str());
93  }
94 
95  public:
96  virtual std::string GetType() override { return "web::TextAreaInfo"; }
97  }; // End of TextAreaInfo definition
98 
99 
100  // Get a properly cast version of indo.
101  TextAreaInfo * Info() { return (TextAreaInfo *) info; }
102  const TextAreaInfo * Info() const { return (TextAreaInfo *) info; }
103 
104  TextArea(TextAreaInfo * in_info) : WidgetFacet(in_info) { ; }
105 
106  public:
108  TextArea(const std::string & in_id="")
109  : WidgetFacet(in_id)
110  {
111  info = new TextAreaInfo(in_id);
112 
113  Info()->cols = 20;
114  Info()->rows = 1;
115  Info()->max_length = -1;
116  Info()->cur_text = "";
117  Info()->autofocus = false;
118  Info()->disabled = false;
119 
120  TextAreaInfo * ta_info = Info();
121  Info()->callback_id = JSWrap( std::function<void(std::string)>(
122  [ta_info](std::string in_str){ ta_info->DoCallback(in_str); }
123  ));
124  }
125 
127  TextArea(std::function<void(const std::string &)> in_cb, const std::string & in_id="")
128  : TextArea(in_id)
129  {
130  Info()->callback = in_cb;
131  }
132 
134  TextArea(const TextArea & in) : WidgetFacet(in) { ; }
136  virtual ~TextArea() { ; }
137 
139 
141  const std::string & GetText() const { return Info()->cur_text; }
142 
144  TextArea & SetAutofocus(bool in_af) { Info()->UpdateAutofocus(in_af); return *this; }
145 
147  TextArea & SetCallback(const std::function<void(const std::string &)> & in_cb) {
148  Info()->UpdateCallback(in_cb);
149  return *this;
150  }
151 
153  TextArea & SetDisabled(bool in_dis) { Info()->UpdateDisabled(in_dis); return *this; }
154 
156  TextArea & SetText(const std::string & in_text) {
157  Info()->cur_text = in_text;
158  Info()->UpdateText(in_text);
159  return *this;
160  }
161 
163  bool HasAutofocus() const { return Info()->autofocus; }
164 
166  bool IsDisabled() const { return Info()->disabled; }
167  };
168 
169 
170 }
171 }
172 
173 #endif
virtual ~TextAreaInfo()
Definition: TextArea.h:48
TextAreaInfo & operator=(const TextAreaInfo &)=delete
TextArea & SetText(const std::string &in_text)
Set the text contained in the text area.
Definition: TextArea.h:156
Definition: Widget.h:206
TextArea(const Widget &in)
Definition: TextArea.h:135
virtual void ReplaceHTML()
Definition: Widget.h:319
int cols
How many columns of text in the area?
Definition: TextArea.h:33
std::string id
ID used for associated DOM element.
Definition: Widget.h:212
Definition: TextArea.h:30
void UpdateAutofocus(bool in_af)
Definition: TextArea.h:73
virtual bool IsTextAreaInfo() const
Definition: Widget.h:247
WidgetInfo * info
Information associated with this widget.
Definition: Widget.h:82
WidgetFacet is a template that provides accessors into Widget with a derived return type...
Definition: Widget.h:543
TextArea(const std::string &in_id="")
Build a text area with a specified HTML identifier.
Definition: TextArea.h:108
bool disabled
Should this TextArea be disabled?
Definition: TextArea.h:40
bool HasAutofocus() const
Does this widget have auto focus set?
Definition: TextArea.h:163
int max_length
Maximum number of total characters allowed.
Definition: TextArea.h:35
int rows
How many rows of text in the area?
Definition: TextArea.h:34
virtual void GetHTML(std::stringstream &HTML) override
Definition: TextArea.h:61
void DoCallback(std::string in_text)
Definition: TextArea.h:55
Definition: Widget.h:102
TextArea(const TextArea &in)
Connect to an existing TextArea.
Definition: TextArea.h:134
TextArea(std::function< void(const std::string &)> in_cb, const std::string &in_id="")
Build a text area with a specified function to call with every change.
Definition: TextArea.h:127
TextAreaInfo * Info()
Definition: TextArea.h:101
const std::string & GetText() const
Get the current text in this TextArea.
Definition: TextArea.h:141
TextArea & SetAutofocus(bool in_af)
Make this text area have focus by default.
Definition: TextArea.h:144
Widget::ActivityState state
Is this element active in DOM?
Definition: Widget.h:218
const TextAreaInfo * Info() const
Definition: TextArea.h:102
std::string TypeName() const override
Debugging helpers...
Definition: TextArea.h:52
Definition: TextArea.h:25
void UpdateText(const std::string &in_string)
Definition: TextArea.h:87
Widgets maintain individual components on a web page and link to Elements.
void UpdateDisabled(bool in_dis)
Definition: TextArea.h:82
If we are in emscripten, make sure to include the header.
Definition: array.h:37
virtual bool IsTextAreaInfo() const override
Definition: TextArea.h:53
TextArea & SetDisabled(bool in_dis)
Gray out this text area.
Definition: TextArea.h:153
Widget is effectively a smart pointer to a WidgetInfo object, plus some basic accessors.
Definition: Widget.h:78
#define emp_assert(...)
Definition: assert.h:199
virtual ~TextArea()
Definition: TextArea.h:136
virtual std::string GetType() override
Definition: TextArea.h:96
TextArea(TextAreaInfo *in_info)
Definition: TextArea.h:104
TextAreaInfo(const std::string &in_id="")
Definition: TextArea.h:45
std::string cur_text
Text that should currently be in the box.
Definition: TextArea.h:37
uint32_t callback_id
Callback ID the built-in function for this text area.
Definition: TextArea.h:43
void UpdateCallback(const std::function< void(const std::string &)> &in_cb)
Definition: TextArea.h:78
bool autofocus
Should this TextArea be set as Autofocus?
Definition: TextArea.h:39
std::function< void(const std::string &)> callback
Function to call with each keypress.
Definition: TextArea.h:42
bool IsDisabled() const
Is this widget currently disabled?
Definition: TextArea.h:166
void UpdateDependants()
Definition: Widget.h:274
WidgetFacet(const std::string &in_id="")
WidgetFacet cannot be built unless within derived class, so constructors are protected.
Definition: Widget.h:546
TextArea & SetCallback(const std::function< void(const std::string &)> &in_cb)
Change the callback function for this TextArea.
Definition: TextArea.h:147