Empirical
Widget.h
Go to the documentation of this file.
1 
33 #ifndef EMP_WEB_WIDGET_H
34 #define EMP_WEB_WIDGET_H
35 
36 #include <string>
37 
38 #include "../base/vector.h"
39 #include "../tools/mem_track.h"
40 
41 #include "events.h"
42 #include "Font.h"
43 #include "init.h"
44 #include "WidgetExtras.h"
45 
46 namespace emp {
47 namespace web {
48 
49  // Setup some types we will need later
50  namespace internal {
51  // Pre-declare WidgetInfo so classes can inter-operate.
52  class WidgetInfo;
53  class DivInfo;
54  class TableInfo;
55 
57  static size_t NextWidgetNum(bool inc_num=true) {
58  static size_t next_id = 0;
59  if (!inc_num) return next_id;
60  return next_id++;
61  }
62 
64  static std::string NextWidgetID() {
65  return emp::to_string("emp__", NextWidgetNum());
66  }
67 
69  class WidgetCommand {
70  public:
71  virtual ~WidgetCommand() { ; }
72  virtual bool Trigger(WidgetInfo &) const = 0;
73  };
74  }
75 
76 
78  class Widget {
80  protected:
83 
85  template <typename FWD_TYPE> Widget & ForwardAppend(FWD_TYPE && arg);
86 
88  Widget & SetInfo(WidgetInfo * in_info);
89 
91  WidgetInfo * operator->() { return info; }
92 
94  static WidgetInfo * Info(const Widget & w) { return w.info; }
95 
101 
102  enum ActivityState { INACTIVE, WAITING, FROZEN, ACTIVE };
103 
105  static const std::string no_name;
106 
107  public:
109  Widget(const std::string & id);
110  Widget(WidgetInfo * in_info=nullptr);
111  Widget(const Widget & in) : Widget(in.info) { ; }
112  Widget & operator=(const Widget & in) { return SetInfo(in.info); }
113 
114  virtual ~Widget();
115 
117  bool IsNull() const { return info == nullptr; }
118 
120  std::string InfoTypeName() const;
121 
122  bool IsInactive() const;
123  bool IsWaiting() const;
124  bool IsFrozen() const;
125  bool IsActive() const;
126 
127  bool AppendOK() const;
128  void PreventAppend();
129 
130  bool IsButton() const;
131  bool IsCanvas() const;
132  bool IsImage() const;
133  bool IsSelector() const;
134  bool IsDiv() const;
135  bool IsTable() const;
136  bool IsText() const;
137 
138  const std::string & GetID() const;
139 
142  virtual std::string GetCSS(const std::string & setting);
143 
145  virtual bool HasCSS(const std::string & setting);
146 
148  virtual std::string GetAttr(const std::string & setting);
149 
151  virtual bool HasAttr(const std::string & setting);
152 
154  bool operator==(const Widget & in) const { return info == in.info; }
155 
157  bool operator!=(const Widget & in) const { return info != in.info; }
158 
160  operator bool() const { return info != nullptr; }
161 
162  double GetXPos();
163  double GetYPos();
164  double GetWidth();
165  double GetHeight();
166  double GetInnerWidth();
167  double GetInnerHeight();
168  double GetOuterWidth();
169  double GetOuterHeight();
170 
172  void Activate();
173 
175  void Freeze();
176 
179  virtual void Deactivate(bool top_level=true);
180 
182  bool ToggleActive();
183 
185  void Redraw();
186 
188  Widget & Find(const std::string & test_name);
189 
191  Widget & AddDependant(const Widget & w);
192 
194  virtual void PrepareAppend() { ; }
195  template <typename IN_TYPE> Widget operator<<(IN_TYPE && in_val);
196 
198  std::string GetInfoType() const;
199  };
200 
201  namespace internal {
202 
203  // WidgetInfo is a base class containing information needed by all GUI widget classes
204  // (Buttons, Images, etc...). It take in a return type to be cast to for accessors.
205 
206  class WidgetInfo {
207  public:
208  // Smart-pointer info
209  int ptr_count;
210 
211  // Basic info about a widget
212  std::string id;
214 
215  // Track hiearchy
219 
220 
222  WidgetInfo(const std::string & in_id="")
223  : ptr_count(1), id(in_id), parent(nullptr), state(Widget::INACTIVE)
224  {
225  EMP_TRACK_CONSTRUCT(WebWidgetInfo);
226  if (id == "") id = NextWidgetID();
227  }
228 
230  WidgetInfo(const WidgetInfo &) = delete;
231  WidgetInfo & operator=(const WidgetInfo &) = delete;
232 
233  virtual ~WidgetInfo() {
234  EMP_TRACK_DESTRUCT(WebWidgetInfo);
235  }
236 
238  virtual std::string TypeName() const { return "WidgetInfo base"; }
239 
240  virtual bool IsButtonInfo() const { return false; }
241  virtual bool IsCanvasInfo() const { return false; }
242  virtual bool IsImageInfo() const { return false; }
243  virtual bool IsSelectorInfo() const { return false; }
244  virtual bool IsDivInfo() const { return false; }
245  virtual bool IsTableInfo() const { return false; }
246  virtual bool IsTextInfo() const { return false; }
247  virtual bool IsTextAreaInfo() const { return false; }
248  virtual bool IsD3VisualiationInfo() const { return false; }
249 
250  // If not overloaded, pass along widget registration to parent.
251  virtual void Register_recurse(Widget & w) { if (parent) parent->Register_recurse(w); }
252  virtual void Register(Widget & w) { if (parent) parent->Register(w); }
253  virtual void Unregister_recurse(Widget & w) { if (parent) parent->Unregister_recurse(w); }
254  virtual void Unregister(Widget & w) { if (parent) parent->Unregister(w); }
255 
256  // Some nodes can have children and need to be able to recursively register them.
257  virtual void RegisterChildren(DivInfo * registrar) { ; } // No children by default.
258  virtual void UnregisterChildren(DivInfo * regestrar) { ; } // No children by default.
259 
260  // Record dependants. Dependants are only acted upon when this widget's action is
261  // triggered (e.g. a button is pressed)
262  void AddDependant(Widget in) {
263  dependants.emplace_back(in);
264  }
265 
266  template <typename... T>
267  void AddDependants(Widget first, T... widgets) {
268  AddDependant(first);
269  AddDependants(widgets...);
270  }
271 
272  void AddDependants() { ; }
273 
274  void UpdateDependants() { for (auto & d : dependants) d->ReplaceHTML(); }
275 
276 
277  // Activate is delayed until the document is ready, when DoActivate will be called.
278  virtual void DoActivate(bool top_level=true) {
279  state = Widget::ACTIVE; // Activate this widget and its children.
280  if (top_level) ReplaceHTML(); // Print full contents to document.
281  }
282 
283  virtual bool AppendOK() const { return false; } // Most widgets can't be appended to.
284  virtual void PreventAppend() { emp_assert(false, TypeName()); } // Only for appendable widgets.
285 
286  // By default, elements should forward unknown appends to their parents.
287  virtual Widget Append(const std::string & text) { return ForwardAppend(text); }
288  virtual Widget Append(const std::function<std::string()> & fn) { return ForwardAppend(fn); }
289  virtual Widget Append(Widget info) { return ForwardAppend(info); }
290  virtual Widget Append(const Font & font) { return ForwardAppend(font); }
291 
292  // Convert arbitrary inputs to a string and try again!
293  virtual Widget Append(char in_char) { return Append(emp::to_string(in_char)); }
294  virtual Widget Append(double in_num) { return Append(emp::to_string(in_num)); }
295  virtual Widget Append(int in_num) { return Append(emp::to_string(in_num)); }
296  virtual Widget Append(uint32_t in_num) { return Append(emp::to_string(in_num)); }
297 
298  // Handle special commands
300  if (cmd.Trigger(*this)) return Widget(this);
301  return ForwardAppend(cmd); // Otherwise pass the Close to parent!
302  }
303 
304 
305  // If an Append doesn't work with current class, forward it to the parent.
306  template <typename FWD_TYPE>
307  Widget ForwardAppend(FWD_TYPE && arg) {
308  emp_assert(parent && "Trying to forward append to parent, but no parent!", id);
309  return parent->Append(std::forward<FWD_TYPE>(arg));
310  }
311 
312  // All derived widgets must suply a mechanism for providing associated HTML code.
313  virtual void GetHTML(std::stringstream & ss) = 0;
314 
315  // Derived widgets may also provide JavaScript code to be run on redraw.
316  virtual void TriggerJS() { ; }
317 
318  // Assume that the associated ID exists and replace it with the current HTML code.
319  virtual void ReplaceHTML() {
320  // If this node is frozen, don't change it!
321  if (state == Widget::FROZEN) return;
322 
323  // If this node is active, put its contents in ss; otherwise make ss an empty span.
324  std::stringstream ss;
325  if (state == Widget::ACTIVE) GetHTML(ss);
326  else ss << "<span id='" << id << "'></span>";
327 
328  // Now do the replacement.
329  EM_ASM_ARGS({
330  var widget_id = Pointer_stringify($0);
331  var out_html = Pointer_stringify($1);
332  $('#' + widget_id).replaceWith(out_html);
333  }, id.c_str(), ss.str().c_str());
334 
335  // If active update style, trigger JS, and recurse to children!
336  if (state == Widget::ACTIVE) {
337  extras.Apply(id); // Update the attributes, style, and listeners.
338  TriggerJS(); // Run associated Javascript code, if any (e.g., to fill out a canvas)
339  }
340  }
341 
342  public:
343  virtual std::string GetType() { return "web::WidgetInfo"; }
344  };
345 
346  } // end namespaceinternal
347 
348  // Implementation of Widget methods...
349 
350  Widget::Widget(const std::string & id) {
351  emp_assert(has_whitespace(id) == false);
352  // We are creating a new widget; in derived class, make sure:
353  // ... to assign info pointer to new object of proper *Info type
354  // ... NOT to increment info->ptr_count since it's initialized to 1.
355  EMP_TRACK_CONSTRUCT(WebWidget);
356  }
357 
359  info = in_info;
360  if (info) info->ptr_count++;
361  EMP_TRACK_CONSTRUCT(WebWidget);
362  }
363 
365  // We are deleting a widget.
366  if (info) {
367  info->ptr_count--;
368  if (info->ptr_count == 0) delete info;
369  }
370  EMP_TRACK_DESTRUCT(WebWidget);
371  }
372 
373  std::string Widget::InfoTypeName() const { if (IsNull()) return "NULL"; return info->TypeName(); }
374 
376  // If the widget is already set correctly, stop here.
377  if (info == in_info) return *this;
378 
379  // Clean up the old info that was previously pointed to.
380  if (info) {
381  info->ptr_count--;
382  if (info->ptr_count == 0) delete info;
383  }
384 
385  // Setup new info.
386  info = in_info;
387  if (info) info->ptr_count++;
388 
389  return *this;
390  }
391 
392  bool Widget::IsInactive() const { if (!info) return false; return info->state == INACTIVE; }
393  bool Widget::IsWaiting() const { if (!info) return false; return info->state == WAITING; }
394  bool Widget::IsFrozen() const { if (!info) return false; return info->state == FROZEN; }
395  bool Widget::IsActive() const { if (!info) return false; return info->state == ACTIVE; }
396 
397  bool Widget::AppendOK() const { if (!info) return false; return info->AppendOK(); }
398  void Widget::PreventAppend() { emp_assert(info); info->PreventAppend(); }
399 
400  const std::string Widget::no_name = "(none)";
401  const std::string & Widget::GetID() const { return info ? info->id : no_name; }
402 
403  bool Widget::IsButton() const { if (!info) return false; return info->IsButtonInfo(); }
404  bool Widget::IsCanvas() const { if (!info) return false; return info->IsCanvasInfo(); }
405  bool Widget::IsImage() const { if (!info) return false; return info->IsImageInfo(); }
406  bool Widget::IsSelector() const { if (!info) return false; return info->IsSelectorInfo(); }
407  bool Widget::IsDiv() const { if (!info) return false; return info->IsDivInfo(); }
408  bool Widget::IsTable() const { if (!info) return false; return info->IsTableInfo(); }
409  bool Widget::IsText() const { if (!info) return false; return info->IsTextInfo(); }
410 
411  std::string Widget::GetCSS(const std::string & setting) {
412  return info ? info->extras.GetStyle(setting) : "";
413  }
414  bool Widget::HasCSS(const std::string & setting) {
415  return info ? info->extras.HasStyle(setting) : false;
416  }
417 
418  std::string Widget::GetAttr(const std::string & setting) {
419  return info ? info->extras.GetAttr(setting) : "";
420  }
421  bool Widget::HasAttr(const std::string & setting) {
422  return info ? info->extras.HasAttr(setting) : false;
423  }
424 
425  double Widget::GetXPos() {
426  if (!info) return -1.0;
427  emp_assert(GetID() != ""); // Must have a name!
428  return EM_ASM_DOUBLE({
429  var id = Pointer_stringify($0);
430  var rect = $('#' + id).position();
431  return rect.left;
432  }, GetID().c_str());
433  }
434 
435  double Widget::GetYPos() {
436  if (!info) return -1.0;
437  emp_assert(GetID() != ""); // Must have a name!
438  return EM_ASM_DOUBLE({
439  var id = Pointer_stringify($0);
440  var rect = $('#' + id).position();
441  return rect.top;
442  }, GetID().c_str());
443  }
444 
446  if (!info) return -1.0;
447  emp_assert(GetID() != ""); // Must have a name!
448  return EM_ASM_DOUBLE({
449  var id = Pointer_stringify($0);
450  return $('#' + id).width();
451  }, GetID().c_str());
452  }
454  if (!info) return -1.0;
455  emp_assert(GetID() != ""); // Must have a name!
456  return EM_ASM_DOUBLE({
457  var id = Pointer_stringify($0);
458  return $('#' + id).height();
459  }, GetID().c_str());
460  }
462  if (!info) return -1.0;
463  emp_assert(GetID() != ""); // Must have a name!
464  return EM_ASM_DOUBLE({
465  var id = Pointer_stringify($0);
466  return $('#' + id).innerWidth();
467  }, GetID().c_str());
468  }
470  if (!info) return -1.0;
471  emp_assert(GetID() != ""); // Must have a name!
472  return EM_ASM_DOUBLE({
473  var id = Pointer_stringify($0);
474  return $('#' + id).innerHeight();
475  }, GetID().c_str());
476  }
478  if (!info) return -1.0;
479  emp_assert(GetID() != ""); // Must have a name!
480  return EM_ASM_DOUBLE({
481  var id = Pointer_stringify($0);
482  return $('#' + id).outerWidth();
483  }, GetID().c_str());
484  }
486  if (!info) return -1.0;
487  emp_assert(GetID() != ""); // Must have a name!
488  return EM_ASM_DOUBLE({
489  var id = Pointer_stringify($0);
490  return $('#' + id).outerHeight();
491  }, GetID().c_str());
492  }
493 
495  auto * cur_info = info;
496  info->state = WAITING;
497  OnDocumentReady( std::function<void(void)>([cur_info](){ cur_info->DoActivate(); }) );
498  }
499 
500  void Widget::Freeze() {
501  info->state = FROZEN;
502  }
503 
504  void Widget::Deactivate(bool top_level) {
505  if (!info || info->state == INACTIVE) return; // Skip if we are not active.
506  info->state = INACTIVE;
507  if (top_level) info->ReplaceHTML(); // If at top level, clear the contents
508  }
509 
511  emp_assert(info);
512  if (info->state != INACTIVE) Deactivate();
513  else Activate();
514  return info->state;
515  }
516 
517  void Widget::Redraw() {
518  emp_assert(info);
519  info->ReplaceHTML();
520  }
521 
523  info->AddDependant(w);
524  return *this;
525  }
526 
527  template <typename IN_TYPE>
528  Widget Widget::operator<<(IN_TYPE && in_val) {
529  PrepareAppend();
530  return info->Append(std::forward<IN_TYPE>(in_val));
531  }
532 
533  std::string Widget::GetInfoType() const {
534  if (!info) return "UNINITIALIZED";
535  return info->GetType();
536  }
537 
538 
539  namespace internal {
540 
542  template <typename RETURN_TYPE>
543  class WidgetFacet : public Widget {
544  protected:
546  WidgetFacet(const std::string & in_id="") : Widget(in_id) { ; }
547  WidgetFacet(const WidgetFacet & in) : Widget(in) { ; }
548  WidgetFacet(const Widget & in) : Widget(in) {
549  // Converting from a generic widget; make sure type is correct or non-existant!
550  emp_assert(!in || dynamic_cast<typename RETURN_TYPE::INFO_TYPE *>( Info(in) ) != NULL,
551  in.GetID());
552  }
553  WidgetFacet(WidgetInfo * in_info) : Widget(in_info) { ; }
554  WidgetFacet & operator=(const WidgetFacet & in) { Widget::operator=(in); return *this; }
555  virtual ~WidgetFacet() { ; }
556 
559  virtual void DoCSS(const std::string & setting, const std::string & value) {
560  info->extras.style.DoSet(setting, value);
561  if (IsActive()) Style::Apply(info->id, setting, value);
562  }
565  virtual void DoAttr(const std::string & setting, const std::string & value) {
566  info->extras.attr.DoSet(setting, value);
567  if (IsActive()) Attributes::Apply(info->id, setting, value);
568  }
571  virtual void DoListen(const std::string & event_name, size_t fun_id) {
572  info->extras.listen.Set(event_name, fun_id);
573  if (IsActive()) Listeners::Apply(info->id, event_name, fun_id);
574  }
575 
576  public:
577  using return_t = RETURN_TYPE;
578 
580  template <typename SETTING_TYPE>
581  return_t & SetCSS(const std::string & setting, SETTING_TYPE && value) {
582  emp_assert(info != nullptr);
583  DoCSS(setting, emp::to_string(value));
584  return (return_t &) *this;
585  }
586 
588  template <typename SETTING_TYPE>
589  return_t & SetAttr(const std::string & setting, SETTING_TYPE && value) {
590  emp_assert(info != nullptr);
591  DoAttr(setting, emp::to_string(value));
592  return (return_t &) *this;
593  }
594 
596  template <typename T1, typename T2, typename... OTHER_SETTINGS>
597  return_t & SetCSS(const std::string & setting1, T1 && val1,
598  const std::string & setting2, T2 && val2,
599  OTHER_SETTINGS... others) {
600  SetCSS(setting1, val1); // Set the first CSS value.
601  return SetCSS(setting2, val2, others...); // Recurse to the others.
602  }
603 
605  template <typename T1, typename T2, typename... OTHER_SETTINGS>
606  return_t & SetAttr(const std::string & setting1, T1 && val1,
607  const std::string & setting2, T2 && val2,
608  OTHER_SETTINGS... others) {
609  SetAttr(setting1, val1); // Set the first CSS value.
610  return SetAttr(setting2, val2, others...); // Recurse to the others.
611  }
612 
615  return_t & SetCSS(const Style & in_style) {
616  emp_assert(info != nullptr);
617  for (const auto & s : in_style.GetMap()) {
618  DoCSS(s.first, s.second);
619  }
620  return (return_t &) *this;
621  }
622 
625  return_t & SetAttr(const Attributes & in_attr) {
626  emp_assert(info != nullptr);
627  for (const auto & a : in_attr.GetMap()) {
628  DoAttr(a.first, a.second);
629  }
630  return (return_t &) *this;
631  }
632 
635  return_t & On(const std::string & event_name, const std::function<void()> & fun) {
636  emp_assert(info != nullptr);
637  size_t fun_id = JSWrap(fun);
638  DoListen(event_name, fun_id);
639  return (return_t &) *this;
640  }
641 
644  return_t & On(const std::string & event_name,
645  const std::function<void(MouseEvent evt)> & fun) {
646  emp_assert(info != nullptr);
647  size_t fun_id = JSWrap(fun);
648  DoListen(event_name, fun_id);
649  return (return_t &) *this;
650  }
651 
654  return_t & On(const std::string & event_name,
655  const std::function<void(double,double)> & fun) {
656  emp_assert(info != nullptr);
657  auto fun_cb = [this, fun](MouseEvent evt){
658  double x = evt.clientX - GetXPos();
659  double y = evt.clientY - GetYPos();
660  fun(x,y);
661  };
662  size_t fun_id = JSWrap(fun_cb);
663  DoListen(event_name, fun_id);
664  return (return_t &) *this;
665  }
666 
668  template <typename T> return_t & OnResize(T && arg) { return On("resize", arg); }
669 
671  template <typename T> return_t & OnClick(T && arg) { return On("click", arg); }
672 
674  template <typename T> return_t & OnDoubleClick(T && arg) { return On("dblclick", arg); }
675 
677  template <typename T> return_t & OnMouseDown(T && arg) { return On("mousedown", arg); }
678 
680  template <typename T> return_t & OnMouseUp(T && arg) { return On("mouseup", arg); }
681 
683  template <typename T> return_t & OnMouseMove(T && arg) { return On("mousemove", arg); }
684 
686  template <typename T> return_t & OnMouseOut(T && arg) { return On("mouseout", arg); }
687 
689  template <typename T> return_t & OnMouseOver(T && arg) { return On("mouseover", arg); }
690 
692  template <typename T> return_t & OnMouseWheel(T && arg) { return On("mousewheel", arg); }
693 
695  template <typename T> return_t & OnKeydown(T && arg) { return On("keydown", arg); }
696 
698  template <typename T> return_t & OnKeypress(T && arg) { return On("keypress", arg); }
699 
701  template <typename T> return_t & OnKeyup(T && arg) { return On("keyup", arg); }
702 
704  template <typename T> return_t & OnCopy(T && arg) { return On("copy", arg); }
705 
707  template <typename T> return_t & OnCut(T && arg) { return On("cut", arg); }
708 
710  template <typename T> return_t & OnPaste(T && arg) { return On("paste", arg); }
711 
712 
715  return_t & SetWidth(double w, const std::string & unit="px") {
716  return SetCSS("width", emp::to_string(w, unit) );
717  }
718 
721  return_t & SetHeight(double h, const std::string & unit="px") {
722  return SetCSS("height", emp::to_string(h, unit) );
723  }
724 
727  return_t & SetSize(double w, double h, const std::string & unit="px") {
728  SetWidth(w, unit); return SetHeight(h, unit);
729  }
730 
732  return_t & Center() { return SetCSS("margin", "auto"); }
733 
735  return_t & SetPosition(int x, int y, const std::string & unit="px",
736  const std::string & pos_type="absolute",
737  const std::string & x_anchor="left",
738  const std::string & y_anchor="top") {
739  return SetCSS("position", pos_type,
740  x_anchor, emp::to_string(x, unit),
741  y_anchor, emp::to_string(y, unit));
742  }
743 
745  return_t & SetPositionRT(int x, int y, const std::string & unit="px")
746  { return SetPosition(x, y, unit, "absolute", "right", "top"); }
747 
749  return_t & SetPositionRB(int x, int y, const std::string & unit="px")
750  { return SetPosition(x, y, unit, "absolute", "right", "bottom"); }
751 
753  return_t & SetPositionLB(int x, int y, const std::string & unit="px")
754  { return SetPosition(x, y, unit, "absolute", "left", "bottom"); }
755 
757  return_t & SetPositionFixed(int x, int y, const std::string & unit="px")
758  { return SetPosition(x, y, unit, "fixed", "left", "top"); }
759 
761  return_t & SetPositionFixedRT(int x, int y, const std::string & unit="px")
762  { return SetPosition(x, y, unit, "fixed", "right", "top"); }
763 
765  return_t & SetPositionFixedRB(int x, int y, const std::string & unit="px")
766  { return SetPosition(x, y, unit, "fixed", "right", "bottom"); }
767 
769  return_t & SetPositionFixedLB(int x, int y, const std::string & unit="px")
770  { return SetPosition(x, y, unit, "fixed", "left", "bottom"); }
771 
772 
774  return_t & SetFloat(const std::string & f="left") { return SetCSS("float", f); }
775 
777  return_t & SetOverflow(const std::string & o="auto") { return SetCSS("overflow", o); }
778 
780  return_t & SetScroll() { return SetCSS("overflow", "scroll"); }
781 
783  return_t & SetScrollAuto() { return SetCSS("overflow", "auto"); }
784 
786  return_t & SetResizable() { return SetCSS("resize", "both"); }
787 
789  return_t & SetResizableX() { return SetCSS("resize", "horizontal"); }
790 
792  return_t & SetResizableY() { return SetCSS("resize", "vertical"); }
793 
795  return_t & SetResizableOff() { return SetCSS("resize", "none"); }
796 
798  return_t & SetFont(const Font & font) {
799  font.ConfigStyle(info->extras.style);
800  return (return_t &) *this;
801  }
802 
804  return_t & SetFont(const std::string & font) { return SetCSS("font-family", font); }
805 
807  return_t & SetFontSize(int s) { return SetCSS("font-size", emp::to_string(s, "px")); }
808 
810  return_t & SetFontSizeVW(double s) { return SetCSS("font-size", emp::to_string(s, "vw")); }
811 
813  return_t & SetCenterText() { return SetCSS("text-align", "center"); }
814 
816  return_t & SetBackground(const std::string & v) { return SetCSS("background-color", v); }
817 
819  return_t & SetColor(const std::string & v) { return SetCSS("color", v); }
820 
822  return_t & SetOpacity(double v) { return SetCSS("opacity", v); }
823 
825  return_t & SetBorder(const std::string & border_info) {
826  return SetCSS("border", border_info);
827  }
828 
830  return_t & SetPadding(double p, const std::string & unit="px") {
831  return SetCSS("padding", emp::to_string(p, unit));
832  }
833  };
834 
835  }
836 
837 }
838 }
839 
840 
841 #endif
void ConfigStyle(Style &style) const
Take a Style object an fill it out based on this font information.
Definition: Font.h:80
virtual Widget Append(double in_num)
Definition: Widget.h:294
virtual Widget Append(const Font &font)
Definition: Widget.h:290
return_t & On(const std::string &event_name, const std::function< void()> &fun)
Definition: Widget.h:635
int ptr_count
How many widgets are pointing to this info?
Definition: Widget.h:209
virtual bool IsTableInfo() const
Definition: Widget.h:245
A Text widget handles putting text on a web page that can be controlled and modified.
Definition: Text.h:27
Definition: Widget.h:206
return_t & OnKeydown(T &&arg)
Provide a function to be called whenever a key is pressed down in this Widget.
Definition: Widget.h:695
Maintains basic information about a font to be used in HTML.
bool IsActive() const
Test if the activity state of this widget is currently ACTIVE.
Definition: Widget.h:395
Widget(const Widget &in)
Definition: Widget.h:111
return_t & OnPaste(T &&arg)
Provide a function to be called whenever text is pasted in this Widget.
Definition: Widget.h:710
virtual void ReplaceHTML()
Definition: Widget.h:319
return_t & SetPositionFixedRT(int x, int y, const std::string &unit="px")
Set the x-y position of the top-right corner this Widget, fixed within the browser window...
Definition: Widget.h:761
WidgetInfo * operator->()
Internally, we can treat a Widget as a pointer to its WidgetInfo.
Definition: Widget.h:91
virtual ~WidgetFacet()
Definition: Widget.h:555
virtual bool AppendOK() const
Definition: Widget.h:283
return_t & OnKeyup(T &&arg)
Provide a function to be called whenever a key is pressed released in this Widget.
Definition: Widget.h:701
Define Initialize() and other functions to set up Empirical to build Emscripten projects.
double GetOuterWidth()
Get the width of this Widget including all padding.
Definition: Widget.h:477
std::string id
ID used for associated DOM element.
Definition: Widget.h:212
return_t & SetPadding(double p, const std::string &unit="px")
The the number of pixels (or alternate unit) for the padding around cells (used with Tables) ...
Definition: Widget.h:830
return_t & SetResizableOff()
Setup how this Widget to NOT be resizable.
Definition: Widget.h:795
virtual std::string GetCSS(const std::string &setting)
Definition: Widget.h:411
const std::map< std::string, std::string > & GetMap() const
Definition: Attributes.h:70
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
return_t & SetWidth(double w, const std::string &unit="px")
Definition: Widget.h:715
Widget & operator=(const Widget &in)
Definition: Widget.h:112
bool operator!=(const Widget &in) const
Are two Widgets refering to differnt HTML objects?
Definition: Widget.h:157
return_t & SetPositionFixedRB(int x, int y, const std::string &unit="px")
Set the x-y position of the bottom-right corner this Widget, fixed within the browser window...
Definition: Widget.h:765
return_t & SetScroll()
Setup how this Widget to always have scrollbars.
Definition: Widget.h:780
bool IsTable() const
Is this Widget a Table?
Definition: Widget.h:408
void Freeze()
Record changes internally, but keep static screen until Activate() is called.
Definition: Widget.h:500
virtual bool IsTextAreaInfo() const
Definition: Widget.h:247
bool operator==(const Widget &in) const
Are two Widgets refering to the same HTML object?
Definition: Widget.h:154
Widget operator<<(IN_TYPE &&in_val)
Definition: Widget.h:528
return_t & SetResizable()
Setup how this Widget to be user-resizable.
Definition: Widget.h:786
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
virtual bool IsD3VisualiationInfo() const
Definition: Widget.h:248
return_t & SetCSS(const Style &in_style)
Definition: Widget.h:615
WidgetFacet(const Widget &in)
Definition: Widget.h:548
virtual Widget Append(char in_char)
Definition: Widget.h:293
virtual bool HasAttr(const std::string &setting)
Determine is an attribute has been set on this Widget.
Definition: Widget.h:421
bool IsNull() const
Test if this widget is valid.
Definition: Widget.h:117
virtual Widget Append(Widget info)
Definition: Widget.h:289
return_t & SetBorder(const std::string &border_info)
Set information about the Widget board.
Definition: Widget.h:825
Definition: Style.h:29
Maintains a map of attribute names to values for use in JavaScript Closely related to Style...
Definition: Attributes.h:29
Include information (name, keyword, description) for each instance.
bool ToggleActive()
Doggle between Active and Deactivated.
Definition: Widget.h:510
void Redraw()
Clear and redraw the current widget on the screen.
Definition: Widget.h:517
return_t & OnMouseWheel(T &&arg)
Provide a function to be called whenever the mouse wheel moves in this Widget.
Definition: Widget.h:692
virtual bool IsButtonInfo() const
Definition: Widget.h:240
void Activate()
Make this widget live, so changes occur immediately (once document is ready)
Definition: Widget.h:494
return_t & OnMouseOut(T &&arg)
Provide a function to be called whenever the mouse leaves the Widget.
Definition: Widget.h:686
void Apply(const std::string &widget_id)
Apply ALL of the style settings to a specified widget.
Definition: Style.h:85
double GetWidth()
Get the width of this Widget on screen.
Definition: Widget.h:445
double GetInnerWidth()
Get the width of this Widget not including padding.
Definition: Widget.h:461
virtual std::string GetAttr(const std::string &setting)
Retrieve a specific attribute associated with this Widget.
Definition: Widget.h:418
virtual Widget Append(uint32_t in_num)
Definition: Widget.h:296
return_t & On(const std::string &event_name, const std::function< void(double, double)> &fun)
Definition: Widget.h:654
return_t & OnKeypress(T &&arg)
Provide a function to be called whenever a key is pressed down and released in this Widget...
Definition: Widget.h:698
return_t & OnMouseMove(T &&arg)
Provide a function to be called whenever the mouse moves in this Widget.
Definition: Widget.h:683
virtual void Unregister_recurse(Widget &w)
Definition: Widget.h:253
return_t & OnCut(T &&arg)
Provide a function to be called whenever text is cut in this Widget.
Definition: Widget.h:707
return_t & SetColor(const std::string &v)
Set the foreground color of this Widget.
Definition: Widget.h:819
Definition: Widget.h:102
return_t & SetPosition(int x, int y, const std::string &unit="px", const std::string &pos_type="absolute", const std::string &x_anchor="left", const std::string &y_anchor="top")
Set the x-y position of this widget within its container.
Definition: Widget.h:735
virtual void DoCSS(const std::string &setting, const std::string &value)
Definition: Widget.h:559
virtual Widget Append(const std::function< std::string()> &fn)
Definition: Widget.h:288
return_t & SetAttr(const std::string &setting1, T1 &&val1, const std::string &setting2, T2 &&val2, OTHER_SETTINGS...others)
Multiple Attributes can be provided simultaneously.
Definition: Widget.h:606
void Apply(const std::string &widget_id)
Apply all of the listeners being tracked.
Definition: Listeners.h:83
std::string GetInfoType() const
Debug...
Definition: Widget.h:533
void Apply(const std::string &widget_id)
Apply ALL of the Attribute&#39;s settings to dom element "widget_id".
Definition: Attributes.h:82
return_t & OnMouseDown(T &&arg)
Provide a function to be called when the mouse button is pushed down in this Widget.
Definition: Widget.h:677
return_t & SetCSS(const std::string &setting, SETTING_TYPE &&value)
Set a specific CSS value for this widget.
Definition: Widget.h:581
void AddDependants()
Definition: Widget.h:272
virtual ~WidgetInfo()
Definition: Widget.h:233
double GetYPos()
Get the Y-position of this Widget within its parent.
Definition: Widget.h:435
WidgetExtras extras
HTML attributes, CSS style, and listeners for web events.
Definition: Widget.h:213
bool AppendOK() const
Is it okay to add more internal Widgets into this one?
Definition: Widget.h:397
void emplace_back(ARGS &&...args)
Definition: vector.h:219
bool IsCanvas() const
Is this Widget a Canvas?
Definition: Widget.h:404
virtual void PreventAppend()
Definition: Widget.h:284
Widget & AddDependant(const Widget &w)
Add a dependant to this Widget that should be redrawn when it is.
Definition: Widget.h:522
return_t & SetCenterText()
Align text to be centered.
Definition: Widget.h:813
return_t & SetResizableY()
Setup how this Widget for the y only to be user-resizable.
Definition: Widget.h:792
const std::map< std::string, std::string > & GetMap() const
Definition: Style.h:72
virtual void RegisterChildren(DivInfo *registrar)
Definition: Widget.h:257
Widget::ActivityState state
Is this element active in DOM?
Definition: Widget.h:218
virtual bool Trigger(WidgetInfo &) const =0
virtual ~WidgetCommand()
Definition: Widget.h:71
const std::string & GetID() const
What is the HTML string ID for this Widget?
Definition: Widget.h:401
emp::vector< Widget > dependants
Widgets to be refreshed if this one is triggered.
Definition: Widget.h:217
Widget & SetInfo(WidgetInfo *in_info)
Set the information associated with this widget.
Definition: Widget.h:375
static WidgetInfo * Info(const Widget &w)
Give derived classes the ability to access widget info.
Definition: Widget.h:94
std::ostream & operator<<(std::ostream &out, const emp::Ptr< T > &ptr)
Definition: Ptr.h:800
Definition: Widget.h:102
return_t & OnMouseOver(T &&arg)
Provide a function to be called whenever the mouse moves over the Widget.
Definition: Widget.h:689
virtual void Register(Widget &w)
Definition: Widget.h:252
return_t & SetPositionRT(int x, int y, const std::string &unit="px")
Set the x-y position of this Widget within its container, using the TOP-RIGHT as an anchor...
Definition: Widget.h:745
bool has_whitespace(const std::string &test_str)
Determine if there is whitespace anywhere in a string.
Definition: string_utils.h:212
bool IsInactive() const
Test if the activity state of this widget is currently INACTIVE.
Definition: Widget.h:392
auto Find(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)
Definition: map_utils.h:29
return_t & SetScrollAuto()
Setup how this Widget to have scrollbars if needed for overflow.
Definition: Widget.h:783
return_t & SetFontSize(int s)
Setup the size of the Font to be used in this Widget.
Definition: Widget.h:807
Event handlers that use JQuery.
virtual std::string TypeName() const
Debugging helpers...
Definition: Widget.h:238
WidgetInfo * parent
Which WidgetInfo is this one contained within?
Definition: Widget.h:216
virtual bool IsCanvasInfo() const
Definition: Widget.h:241
static const std::string no_name
Default name for un-initialized widgets.
Definition: Widget.h:105
return_t & SetHeight(double h, const std::string &unit="px")
Definition: Widget.h:721
return_t & SetPositionFixed(int x, int y, const std::string &unit="px")
Set the x-y position of this Widget, fixed within the browser window.
Definition: Widget.h:757
virtual void PrepareAppend()
Setup << operator to redirect to Append; option preparation can be overridden.
Definition: Widget.h:194
Definition: Div.h:52
return_t & SetPositionFixedLB(int x, int y, const std::string &unit="px")
Set the x-y position of the bottom-left corner this Widget, fixed within the browser window...
Definition: Widget.h:769
return_t & OnMouseUp(T &&arg)
Provide a function to be called when the mouse button is released in this Widget. ...
Definition: Widget.h:680
virtual bool IsDivInfo() const
Definition: Widget.h:244
bool IsSelector() const
Is this Widget a Selector?
Definition: Widget.h:406
Widget(const std::string &id)
When Widgets are first created, they should be provided with an ID.
Definition: Widget.h:350
return_t & SetResizableX()
Setup how this Widget for the x only to be user-resizable.
Definition: Widget.h:789
virtual bool HasCSS(const std::string &setting)
Determine is a CSS trait has been set on this Widget.
Definition: Widget.h:414
return_t & OnClick(T &&arg)
Provide a function to be called when the mouse button is clicked in this Widget.
Definition: Widget.h:671
Mouse-specific information about web events.
Definition: events.h:62
return_t & SetFont(const Font &font)
Provide a Font object to setup the font for this widget.
Definition: Widget.h:798
bool IsDiv() const
Is this Widget a Div?
Definition: Widget.h:407
return_t & OnDoubleClick(T &&arg)
Provide a function to be called when the mouse button is double clicked in this Widget.
Definition: Widget.h:674
void PreventAppend()
Disallow further appending to this Widget.
Definition: Widget.h:398
return_t & On(const std::string &event_name, const std::function< void(MouseEvent evt)> &fun)
Definition: Widget.h:644
virtual bool IsTextInfo() const
Definition: Widget.h:246
virtual bool IsSelectorInfo() const
Definition: Widget.h:243
virtual void TriggerJS()
Definition: Widget.h:316
bool IsImage() const
Is this Widget an Image?
Definition: Widget.h:405
return_t & SetAttr(const Attributes &in_attr)
Definition: Widget.h:625
bool IsFrozen() const
Test if the activity state of this widget is currently FROZEN.
Definition: Widget.h:394
A collection of extra details about HTML Widgets (attributes, style, listerns)
If we are in emscripten, make sure to include the header.
Definition: array.h:37
virtual std::string GetType()
Definition: Widget.h:343
WidgetFacet(WidgetInfo *in_info)
Definition: Widget.h:553
return_t & Center()
Move this widget to the center of its container.
Definition: Widget.h:732
Widget ForwardAppend(FWD_TYPE &&arg)
Definition: Widget.h:307
Definition: WidgetExtras.h:21
virtual void DoActivate(bool top_level=true)
Definition: Widget.h:278
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
virtual Widget Append(const emp::web::internal::WidgetCommand &cmd)
Definition: Widget.h:299
virtual void Register_recurse(Widget &w)
Definition: Widget.h:251
WidgetFacet(const WidgetFacet &in)
Definition: Widget.h:547
Widget is effectively a smart pointer to a WidgetInfo object, plus some basic accessors.
Definition: Widget.h:78
virtual Widget Append(const std::string &text)
Definition: Widget.h:287
Base class for command-objects that can be fed into widgets.
Definition: Widget.h:69
#define emp_assert(...)
Definition: assert.h:199
virtual void Unregister(Widget &w)
Definition: Widget.h:254
virtual bool IsImageInfo() const
Definition: Widget.h:242
bool IsText() const
Is this Widget a Text?
Definition: Widget.h:409
return_t & SetFloat(const std::string &f="left")
Set this Widget to float appropriately within its containter.
Definition: Widget.h:774
Definition: Widget.h:102
void Apply(const std::string &name)
Apply all HTML details associated with this widget.
Definition: WidgetExtras.h:39
return_t & OnResize(T &&arg)
Provide a function to be called when the window is resized.
Definition: Widget.h:668
virtual void UnregisterChildren(DivInfo *regestrar)
Definition: Widget.h:258
WidgetFacet & operator=(const WidgetFacet &in)
Definition: Widget.h:554
double GetInnerHeight()
Get the height of this Widget not including padding.
Definition: Widget.h:469
Definition: Table.h:107
return_t & SetSize(double w, double h, const std::string &unit="px")
Definition: Widget.h:727
WidgetInfo(const std::string &in_id="")
WidgetInfo cannot be built unless within derived class, so constructor is protected.
Definition: Widget.h:222
return_t & SetFontSizeVW(double s)
Setup the size of the Font to be used in this Widget in units of % of viewport width.
Definition: Widget.h:810
void AddDependants(Widget first, T...widgets)
Definition: Widget.h:267
void OnDocumentReady(FUN_TYPE &&fun)
Runs the specified function when the document is finished loading and being set up.
Definition: events.h:29
return_t & SetBackground(const std::string &v)
Set the background color of this Widget.
Definition: Widget.h:816
return_t & SetOverflow(const std::string &o="auto")
Setup how this Widget should handle overflow.
Definition: Widget.h:777
Maintain information about an HTML font.
Definition: Font.h:24
void AddDependant(Widget in)
Definition: Widget.h:262
bool IsButton() const
Is this Widget a Button?
Definition: Widget.h:403
return_t & SetPositionRB(int x, int y, const std::string &unit="px")
Set the x-y position of this Widget within its container, using the BOTTOM-RIGHT as an anchor...
Definition: Widget.h:749
double GetOuterHeight()
Get the height of this Widget including all padding.
Definition: Widget.h:485
return_t & SetFont(const std::string &font)
Setup the Font to be used in this Widget.
Definition: Widget.h:804
virtual void DoAttr(const std::string &setting, const std::string &value)
Definition: Widget.h:565
double GetXPos()
Get the X-position of this Widget within its parent.
Definition: Widget.h:425
return_t & OnCopy(T &&arg)
Provide a function to be called whenever text is copied in this Widget.
Definition: Widget.h:704
static std::string NextWidgetID()
Quick method for generating unique string IDs for Widgets.
Definition: Widget.h:64
bool IsWaiting() const
Test if the activity state of this widget is currently WAITING.
Definition: Widget.h:393
static size_t NextWidgetNum(bool inc_num=true)
Quick method for generating unique Widget ID numbers when not otherwise specified.
Definition: Widget.h:57
std::string InfoTypeName() const
Some debugging helpers...
Definition: Widget.h:373
return_t & SetAttr(const std::string &setting, SETTING_TYPE &&value)
Set a specific Attribute value for this widget.
Definition: Widget.h:589
virtual void DoListen(const std::string &event_name, size_t fun_id)
Definition: Widget.h:571
virtual void Deactivate(bool top_level=true)
Definition: Widget.h:504
void UpdateDependants()
Definition: Widget.h:274
virtual Widget Append(int in_num)
Definition: Widget.h:295
return_t & SetOpacity(double v)
Set the opacity level of this Widget.
Definition: Widget.h:822
double GetHeight()
Get the height of this Widget on screen.
Definition: Widget.h:453
return_t & SetCSS(const std::string &setting1, T1 &&val1, const std::string &setting2, T2 &&val2, OTHER_SETTINGS...others)
Multiple CSS settings can be provided simultaneously.
Definition: Widget.h:597
WidgetFacet(const std::string &in_id="")
WidgetFacet cannot be built unless within derived class, so constructors are protected.
Definition: Widget.h:546
virtual ~Widget()
Definition: Widget.h:364
ActivityState
Definition: Widget.h:102
return_t & SetPositionLB(int x, int y, const std::string &unit="px")
Set the x-y position of this Widget within its container, using the BOTTOM-LEFT as an anchor...
Definition: Widget.h:753