Empirical
Text.h
Go to the documentation of this file.
1 
15 #ifndef EMP_WEB_TEXT_H
16 #define EMP_WEB_TEXT_H
17 
18 #include "../tools/DynamicString.h"
19 
20 #include "Widget.h"
21 
22 namespace emp {
23 namespace web {
24 
26 
27  class Text : public internal::WidgetFacet<Text> {
28  friend class TextInfo;
29  protected:
30 
31  class TextInfo : public internal::WidgetInfo {
32  friend Text;
33  protected:
35  bool append_ok;
36 
37  TextInfo(const std::string & in_id="") : internal::WidgetInfo(in_id), append_ok(true) { ; }
38  TextInfo(const TextInfo &) = delete; // No copies of INFO allowed
39  TextInfo & operator=(const TextInfo &) = delete; // No copies of INFO allowed
40  virtual ~TextInfo() { ; }
41 
42  std::string TypeName() const override { return "TextInfo"; }
43  virtual bool IsTextInfo() const override { return true; }
44 
45  bool AppendOK() const override { return append_ok; }
46  void PreventAppend() override { append_ok = false; }
47 
48  Widget Append(const std::string & in_text) override;
49  Widget Append(const std::function<std::string()> & in_fun) override;
50 
51  // All derived widgets must suply a mechanism for providing associated HTML code.
52  virtual void GetHTML(std::stringstream & HTML) override {
53  HTML.str(""); // Clear the current text.
54  HTML << "<span id=\'" << id << "'>" // Initial span tag to keep id.
55  << strings // Save the current value of all of the strings.
56  << "</span>"; // Close span tag.
57  }
58 
59  public:
60  virtual std::string GetType() override { return "web::TextInfo"; }
61  }; // End of TextInfo
62 
63 
64  // Get a properly cast version of indo.
65  TextInfo * Info() { return (TextInfo *) info; }
66  const TextInfo * Info() const { return (TextInfo *) info; }
67 
68  Text(TextInfo * in_info) : WidgetFacet(in_info) { ; }
69  public:
70  Text(const std::string & in_id="") : WidgetFacet(in_id) {
71  // When a name is provided, create an associated Widget info.
72  info = new TextInfo(in_id);
73  }
74  Text(const Text & in) : WidgetFacet(in) { ; }
75  Text(const Widget & in) : WidgetFacet(in) { emp_assert(info->IsTextInfo()); }
76  ~Text() { ; }
77 
79 
81  Text & Clear() { Info()->strings.Clear(); return *this; }
82  };
83 
85  Widget Text::TextInfo::Append(const std::string & text) {
86  if (!append_ok) return ForwardAppend(text); // If text widget cannot append, forward to parent.
87  strings.Append(text); // Record the new string being added.
88  if (state == Widget::ACTIVE) ReplaceHTML(); // If node is active, immediately redraw!
89  return web::Text(this);
90  }
91 
96  Widget Text::TextInfo::Append(const std::function<std::string()> & fun) {
97  if (!append_ok) return ForwardAppend(fun); // If text widget cannot append, forward to parent.
98  strings.Append(fun); // Record the new function being added.
99  if (state == Widget::ACTIVE) ReplaceHTML(); // If node is active, immediately redraw!
100  return web::Text(this);
101  }
102 
103 }
104 }
105 
106 #endif
A Text widget handles putting text on a web page that can be controlled and modified.
Definition: Text.h:27
Definition: Widget.h:206
DynamicString & Clear()
Remove all contents on this DynamicString.
Definition: DynamicString.h:46
virtual void ReplaceHTML()
Definition: Widget.h:319
DynamicString & Append(const value_t &in_fun)
Add a new function to the end of the DynamicString.
Definition: DynamicString.h:67
TextInfo(const std::string &in_id="")
Definition: Text.h:37
bool AppendOK() const override
Definition: Text.h:45
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
std::string TypeName() const override
Debugging helpers...
Definition: Text.h:42
virtual bool IsTextInfo() const override
Definition: Text.h:43
Text & Clear()
Erase current text.
Definition: Text.h:81
~Text()
Definition: Text.h:76
Text(const Text &in)
Definition: Text.h:74
Definition: Widget.h:102
Text(const std::string &in_id="")
Definition: Text.h:70
void PreventAppend() override
Definition: Text.h:46
Widget::ActivityState state
Is this element active in DOM?
Definition: Widget.h:218
TextInfo & operator=(const TextInfo &)=delete
Widget Append(const std::string &in_text) override
Add new text to this string.
Definition: Text.h:85
virtual ~TextInfo()
Definition: Text.h:40
const TextInfo * Info() const
Definition: Text.h:66
DynamicString strings
All string (and functions returning strings) in Text widget.
Definition: Text.h:34
virtual void GetHTML(std::stringstream &HTML) override
Definition: Text.h:52
TextInfo * Info()
Definition: Text.h:65
virtual bool IsTextInfo() const
Definition: Widget.h:246
Widgets maintain individual components on a web page and link to Elements.
If we are in emscripten, make sure to include the header.
Definition: array.h:37
virtual std::string GetType() override
Definition: Text.h:60
Widget ForwardAppend(FWD_TYPE &&arg)
Definition: Widget.h:307
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
Definition: DynamicString.h:24
bool append_ok
Can this Text widget be extended?
Definition: Text.h:35
Text(TextInfo *in_info)
Definition: Text.h:68
Definition: Text.h:31
Text(const Widget &in)
Definition: Text.h:75
WidgetFacet(const std::string &in_id="")
WidgetFacet cannot be built unless within derived class, so constructors are protected.
Definition: Widget.h:546