33 #ifndef EMP_WEB_WIDGET_H 34 #define EMP_WEB_WIDGET_H 38 #include "../base/vector.h" 39 #include "../tools/mem_track.h" 58 static size_t next_id = 0;
59 if (!inc_num)
return next_id;
85 template <
typename FWD_TYPE>
Widget & ForwardAppend(FWD_TYPE && arg);
109 Widget(
const std::string &
id);
117 bool IsNull()
const {
return info ==
nullptr; }
120 std::string InfoTypeName()
const;
122 bool IsInactive()
const;
123 bool IsWaiting()
const;
124 bool IsFrozen()
const;
125 bool IsActive()
const;
127 bool AppendOK()
const;
128 void PreventAppend();
130 bool IsButton()
const;
131 bool IsCanvas()
const;
132 bool IsImage()
const;
133 bool IsSelector()
const;
135 bool IsTable()
const;
138 const std::string & GetID()
const;
142 virtual std::string GetCSS(
const std::string & setting);
145 virtual bool HasCSS(
const std::string & setting);
148 virtual std::string GetAttr(
const std::string & setting);
151 virtual bool HasAttr(
const std::string & setting);
160 operator bool()
const {
return info !=
nullptr; }
166 double GetInnerWidth();
167 double GetInnerHeight();
168 double GetOuterWidth();
169 double GetOuterHeight();
179 virtual void Deactivate(
bool top_level=
true);
198 std::string GetInfoType()
const;
223 : ptr_count(1), id(in_id), parent(nullptr), state(
Widget::INACTIVE)
225 EMP_TRACK_CONSTRUCT(WebWidgetInfo);
234 EMP_TRACK_DESTRUCT(WebWidgetInfo);
238 virtual std::string
TypeName()
const {
return "WidgetInfo base"; }
266 template <
typename... T>
269 AddDependants(widgets...);
280 if (top_level) ReplaceHTML();
287 virtual Widget Append(
const std::string & text) {
return ForwardAppend(text); }
288 virtual Widget Append(
const std::function<std::string()> & fn) {
return ForwardAppend(fn); }
301 return ForwardAppend(cmd);
306 template <
typename FWD_TYPE>
308 emp_assert(parent &&
"Trying to forward append to parent, but no parent!",
id);
309 return parent->
Append(std::forward<FWD_TYPE>(arg));
313 virtual void GetHTML(std::stringstream & ss) = 0;
324 std::stringstream ss;
326 else ss <<
"<span id='" <<
id <<
"'></span>";
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());
343 virtual std::string
GetType() {
return "web::WidgetInfo"; }
355 EMP_TRACK_CONSTRUCT(WebWidget);
361 EMP_TRACK_CONSTRUCT(WebWidget);
368 if (info->ptr_count == 0)
delete info;
370 EMP_TRACK_DESTRUCT(WebWidget);
377 if (info == in_info)
return *
this;
382 if (info->ptr_count == 0)
delete info;
401 const std::string &
Widget::GetID()
const {
return info ? info->id : no_name; }
407 bool Widget::IsDiv()
const {
if (!info)
return false;
return info->IsDivInfo(); }
409 bool Widget::IsText()
const {
if (!info)
return false;
return info->IsTextInfo(); }
412 return info ? info->extras.GetStyle(setting) :
"";
415 return info ? info->extras.HasStyle(setting) :
false;
419 return info ? info->extras.GetAttr(setting) :
"";
422 return info ? info->extras.HasAttr(setting) :
false;
426 if (!info)
return -1.0;
428 return EM_ASM_DOUBLE({
429 var
id = Pointer_stringify($0);
430 var rect = $(
'#' + id).position();
436 if (!info)
return -1.0;
438 return EM_ASM_DOUBLE({
439 var
id = Pointer_stringify($0);
440 var rect = $(
'#' + id).position();
446 if (!info)
return -1.0;
448 return EM_ASM_DOUBLE({
449 var
id = Pointer_stringify($0);
450 return $(
'#' + id).width();
454 if (!info)
return -1.0;
456 return EM_ASM_DOUBLE({
457 var
id = Pointer_stringify($0);
458 return $(
'#' + id).height();
462 if (!info)
return -1.0;
464 return EM_ASM_DOUBLE({
465 var
id = Pointer_stringify($0);
466 return $(
'#' + id).innerWidth();
470 if (!info)
return -1.0;
472 return EM_ASM_DOUBLE({
473 var
id = Pointer_stringify($0);
474 return $(
'#' + id).innerHeight();
478 if (!info)
return -1.0;
480 return EM_ASM_DOUBLE({
481 var
id = Pointer_stringify($0);
482 return $(
'#' + id).outerWidth();
486 if (!info)
return -1.0;
488 return EM_ASM_DOUBLE({
489 var
id = Pointer_stringify($0);
490 return $(
'#' + id).outerHeight();
495 auto * cur_info = info;
496 info->state = WAITING;
497 OnDocumentReady( std::function<
void(
void)>([cur_info](){ cur_info->DoActivate(); }) );
501 info->state = FROZEN;
505 if (!info || info->state == INACTIVE)
return;
506 info->state = INACTIVE;
507 if (top_level) info->ReplaceHTML();
512 if (info->state != INACTIVE) Deactivate();
527 template <
typename IN_TYPE>
530 return info->Append(std::forward<IN_TYPE>(in_val));
534 if (!info)
return "UNINITIALIZED";
535 return info->GetType();
542 template <
typename RETURN_TYPE>
550 emp_assert(!in || dynamic_cast<typename RETURN_TYPE::INFO_TYPE *>(
Info(in) ) != NULL,
559 virtual void DoCSS(
const std::string & setting,
const std::string & value) {
560 info->extras.style.DoSet(setting, value);
565 virtual void DoAttr(
const std::string & setting,
const std::string & value) {
566 info->extras.attr.DoSet(setting, value);
571 virtual void DoListen(
const std::string & event_name,
size_t fun_id) {
572 info->extras.listen.Set(event_name, fun_id);
580 template <
typename SETTING_TYPE>
588 template <
typename SETTING_TYPE>
596 template <
typename T1,
typename T2,
typename... OTHER_SETTINGS>
598 const std::string & setting2, T2 && val2,
599 OTHER_SETTINGS... others) {
600 SetCSS(setting1, val1);
601 return SetCSS(setting2, val2, others...);
605 template <
typename T1,
typename T2,
typename... OTHER_SETTINGS>
607 const std::string & setting2, T2 && val2,
608 OTHER_SETTINGS... others) {
609 SetAttr(setting1, val1);
610 return SetAttr(setting2, val2, others...);
617 for (
const auto & s : in_style.
GetMap()) {
618 DoCSS(s.first, s.second);
627 for (
const auto & a : in_attr.
GetMap()) {
628 DoAttr(a.first, a.second);
635 return_t &
On(
const std::string & event_name,
const std::function<
void()> & fun) {
637 size_t fun_id = JSWrap(fun);
638 DoListen(event_name, fun_id);
645 const std::function<
void(
MouseEvent evt)> & fun) {
647 size_t fun_id = JSWrap(fun);
648 DoListen(event_name, fun_id);
655 const std::function<
void(
double,
double)> & fun) {
658 double x = evt.clientX - GetXPos();
659 double y = evt.clientY - GetYPos();
662 size_t fun_id = JSWrap(fun_cb);
663 DoListen(event_name, fun_id);
728 SetWidth(w, unit);
return SetHeight(h, unit);
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,
746 {
return SetPosition(x, y, unit,
"absolute",
"right",
"top"); }
750 {
return SetPosition(x, y, unit,
"absolute",
"right",
"bottom"); }
754 {
return SetPosition(x, y, unit,
"absolute",
"left",
"bottom"); }
758 {
return SetPosition(x, y, unit,
"fixed",
"left",
"top"); }
762 {
return SetPosition(x, y, unit,
"fixed",
"right",
"top"); }
766 {
return SetPosition(x, y, unit,
"fixed",
"right",
"bottom"); }
770 {
return SetPosition(x, y, unit,
"fixed",
"left",
"bottom"); }
826 return SetCSS(
"border", border_info);
void ConfigStyle(Style &style) const
Take a Style object an fill it out based on this font information.
Definition: Font.h:80
A Text widget handles putting text on a web page that can be controlled and modified.
Definition: Text.h:27
Maintains basic information about a font to be used in HTML.
Define Initialize() and other functions to set up Empirical to build Emscripten projects.
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
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.
void Apply(const std::string &widget_id)
Apply ALL of the style settings to a specified widget.
Definition: Style.h:85
void Apply(const std::string &widget_id)
Apply all of the listeners being tracked.
Definition: Listeners.h:83
void Apply(const std::string &widget_id)
Apply ALL of the Attribute's settings to dom element "widget_id".
Definition: Attributes.h:82
void emplace_back(ARGS &&...args)
Definition: vector.h:219
const std::map< std::string, std::string > & GetMap() const
Definition: Style.h:72
std::ostream & operator<<(std::ostream &out, const emp::Ptr< T > &ptr)
Definition: Ptr.h:800
bool has_whitespace(const std::string &test_str)
Determine if there is whitespace anywhere in a string.
Definition: string_utils.h:212
auto Find(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)
Definition: map_utils.h:29
Event handlers that use JQuery.
Mouse-specific information about web events.
Definition: events.h:62
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
#define emp_assert(...)
Definition: assert.h:199
void OnDocumentReady(FUN_TYPE &&fun)
Runs the specified function when the document is finished loading and being set up.
Definition: events.h:29
Maintain information about an HTML font.
Definition: Font.h:24
static std::string NextWidgetID()
Quick method for generating unique string IDs for Widgets.
Definition: Widget.h:64
static size_t NextWidgetNum(bool inc_num=true)
Quick method for generating unique Widget ID numbers when not otherwise specified.
Definition: Widget.h:57