2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2016 - Scilab Enterprises - Antoine ELIAS
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
19 #include "webutils.hxx"
23 #include "setGraphicObjectProperty.h"
24 #include "getGraphicObjectProperty.h"
25 #include "graphicObjectProperties.h"
26 #include "LayoutType.h"
27 #include "BorderLayoutType.h"
30 WebUtils::SETTER WebUtils::setter;
31 WebUtils::WAITING_PROP WebUtils::waitprop;
33 int WebUtils::getIntProperty(int uid, int prop)
37 getGraphicObjectProperty(uid, prop, jni_int, (void **)&pVal);
41 bool WebUtils::getBoolProperty(int uid, int prop)
45 getGraphicObjectProperty(uid, prop, jni_bool, (void **)&pVal);
49 double WebUtils::getDoubleProperty(int uid, int prop)
53 getGraphicObjectProperty(uid, prop, jni_double, (void **)&pVal);
57 void WebUtils::getDoubleVectorProterty(int uid, int prop, std::vector<double>& vect)
59 double* values = NULL;
60 getGraphicObjectProperty(uid, prop, jni_double_vector, (void**)&values);
62 int size = (int)vect.size();
63 memcpy(vect.data(), values, size * sizeof(double));
64 releaseGraphicObjectProperty(prop, values, jni_double_vector, size);
67 void WebUtils::getIntVectorProterty(int uid, int prop, std::vector<int>& vect)
70 getGraphicObjectProperty(uid, prop, jni_int_vector, (void**)&values);
72 int size = (int)vect.size();
73 memcpy(vect.data(), values, size * sizeof(int));
74 releaseGraphicObjectProperty(prop, values, jni_int_vector, size);
77 std::string WebUtils::getStringProperty(int uid, int prop)
81 getGraphicObjectProperty(uid, prop, jni_string, (void**)&val);
83 releaseGraphicObjectProperty(prop, val, jni_string, 1);
87 void WebUtils::getStringVectorProperty(int uid, int prop, std::vector<std::string>& vect)
89 char **pstString = NULL;
90 getGraphicObjectProperty(uid, prop, jni_string_vector, (void **)&pstString);
92 int size = (int)vect.size();
93 for (int i = 0; i < size; ++i)
95 vect[i] = pstString[i];
98 releaseGraphicObjectProperty(prop, pstString, jni_string_vector, size);
101 bool WebUtils::setStringProperty(int uid, int prop, const std::string& value)
103 setGraphicObjectProperty(uid, prop, value.data(), jni_string, 1);
107 bool WebUtils::setStringVectorProperty(int uid, int prop, const std::vector<std::string>& values)
109 int size = (int)values.size();
110 std::vector<const char*> c(size, NULL);
111 for (int i = 0; i < size; ++i)
113 c[i] = values[i].data();
116 setGraphicObjectProperty(uid, prop, (void**)c.data(), jni_string_vector, size);
120 bool WebUtils::setDoubleVectorProperty(int uid, int prop, const std::vector<double>& values)
122 setGraphicObjectProperty(uid, prop, values.data(), jni_double_vector, (int)values.size());
126 std::string WebUtils::getIdString(int uid, const std::string& suffix)
128 return "'uid" + std::to_string(uid) + suffix + "'";
131 std::string WebUtils::getElementById(int uid, const std::string& suffix)
133 return "document.getElementById(" + getIdString(uid, suffix) + ");";
136 std::string WebUtils::createElement(const std::string& type)
138 return "document.createElement('" + type + "');";
141 std::string WebUtils::getColor(const std::vector<double>& c)
148 std::string str("'rgb(");
150 //use abs to convert [-1 -1 -1 ] to (255, 255, 255)
151 str += std::to_string((int)std::abs(c[0] * 255));
152 for (int i = 1; i < c.size(); ++i)
154 str += ", " + std::to_string((int)std::abs(c[i] * 255));
162 std::string WebUtils::getSubPadding(int val)
166 return std::to_string(val) + "px ";
172 std::string WebUtils::getPadding(int t, int r, int b, int l)
176 ret += getSubPadding(t);
177 ret += getSubPadding(r);
178 ret += getSubPadding(b);
179 ret += getSubPadding(l);
184 bool WebUtils::isManaged(int uid)
186 int type = getType(uid);
190 case __GO_UICONTROL__:
198 int WebUtils::getType(int uid)
200 return getIntProperty(uid, __GO_TYPE__);
203 int WebUtils::getStyle(int uid)
205 return getIntProperty(uid, __GO_STYLE__);
208 bool WebUtils::hasValidParent(int uid)
210 return getParent(uid) != 0;
213 int WebUtils::getParent(int uid)
215 return getIntProperty(uid, __GO_PARENT__);
218 void WebUtils::setParent(int uid, std::ostringstream& ostr, bool append)
220 int parent = getParent(uid);
224 ostr << "var __child__ = " << getElementById(uid) << std::endl;
227 ostr << "var __parent__ = " << getElementById(parent) << std::endl;
228 ostr << "__parent__.appendChild(__child__);" << std::endl;
230 if (hasStyle(parent, __GO_UI_TAB__))
232 //add button and properties for tab
233 addTabChild(parent, uid, ostr);
236 if (getUILayout(parent) == LAYOUT_BORDER && hasStyle(parent, __GO_UI_TAB__) == false)
238 //force update of border position, especially for center
239 addInWaitingQueue(uid, __GO_UI_BORDER_PREFERREDSIZE__);
242 setWaitingProperties(uid, ostr, true);
245 void WebUtils::getFigureSize(int uid, std::vector<int>& vect)
247 int fig = getFigureId(uid);
249 getIntVectorProterty(fig, __GO_AXES_SIZE__, vect);
252 void WebUtils::setFigureSize(int uid, std::ostringstream& ostr, bool append)
254 std::vector<int> size;
255 getFigureSize(uid, size);
259 ostr << "var __child__ = " << getElementById(uid) << std::endl;
261 ostr << "__child__.style.width = '" << size[0] << "px';" << std::endl;
262 ostr << "__child__.style.height = '" << size[1] << "px';" << std::endl;
265 int WebUtils::getFigureId(int uid)
268 while (isFigure(id) == false)
276 void WebUtils::getUIPosition(int uid, std::vector<double>& vect)
279 getDoubleVectorProterty(uid, __GO_POSITION__, vect);
282 void WebUtils::getUIString(int uid, std::vector<std::string> & vect)
284 int size = getIntProperty(uid, __GO_UI_STRING_SIZE__);
285 //int col = getIntProperty(uid, __GO_UI_STRING_COLNB__);
286 vect.resize(size, "");
287 getStringVectorProperty(uid, __GO_UI_STRING__, vect);
291 void WebUtils::setUIPosition(int uid, std::ostringstream& ostr, bool append)
293 std::vector<double> pos;
294 getUIPosition(uid, pos);
298 ostr << "var __child__ = " << getElementById(uid) << std::endl;
301 ostr << "__child__.style.left = '" << (int)pos[0] << "px';" << std::endl;
302 ostr << "__child__.style.bottom = '" << (int)pos[1] << "px';" << std::endl;
303 ostr << "__child__.style.width = '" << (int)pos[2] << "px';" << std::endl;
304 ostr << "__child__.style.height = '" << (int)pos[3] << "px';" << std::endl;
306 //to ensure vertical alignement, adapt style.line-height
307 if (hasStyle(uid, __GO_UI_TEXT__) || hasStyle(uid, __GO_UI_CHECKBOX__) || hasStyle(uid, __GO_UI_RADIOBUTTON__))
310 getUIVerticalAlignment(uid, v);
313 ostr << "__child__.style.lineHeight = '" << (int)pos[3] << "px';" << std::endl;
317 ostr << "__child__.style.lineHeight = 'initial';" << std::endl;
322 void WebUtils::setUIString(int uid, std::ostringstream& ostr, bool append)
324 std::vector<std::string> s;
331 switch (getStyle(uid))
333 case __GO_UI_CHECKBOX__:
334 case __GO_UI_RADIOBUTTON__:
338 ostr << "var __child__ = " << getElementById(uid) << std::endl;
341 ostr << "var __label__ = " << getElementById(uid, "_label") << std::endl;
342 ostr << "__label__.innerHTML = '" << s[0] << "';" << std::endl;
346 case __GO_UI_POPUPMENU__ :
347 case __GO_UI_LISTBOX__:
351 ostr << "var __child__ = " << getElementById(uid) << std::endl;
354 //remove previous values
355 ostr << "while (__child__.length) {__child__.remove(0);}" << std::endl;
357 ostr << "var option;" << std::endl;
358 int size = (int)s.size();
359 for (int i = 0; i < size; ++i)
361 ostr << "option = new Option('" << s[i] << "');" << std::endl;
362 ostr << "__child__.add(option);" << std::endl;
365 if (hasStyle(uid, __GO_UI_LISTBOX__))
367 //switch to listbox instead of combobox
368 ostr << "__child__.size = 2;" << std::endl;
379 case __GO_UI_FRAME__:
381 //if parent is a GO_UI_TAB, change value of button
382 if (hasStyle(getParent(uid), __GO_UI_TAB__))
386 ostr << "var __child__ = " << getElementById(uid) << std::endl;
389 ostr << "var __btn__ = " + getElementById(uid, "_btn") << std::endl;
390 ostr << "__btn__.value = '" << s[0] << "';" << std::endl;
399 ostr << "var __child__ = " << getElementById(uid) << std::endl;
402 if (isInputType(uid))
404 ostr << "__child__.value = '" << s[0] << "';" << std::endl;
408 ostr << "__child__.innerHTML = '" << s[0] << "';" << std::endl;
418 bool WebUtils::getVisible(int uid)
420 return getBoolProperty(uid, __GO_VISIBLE__);
423 void WebUtils::setVisible(int uid, std::ostringstream& ostr, bool append)
426 std::string v = getVisible(uid) ? "inherit" : "hidden";
429 ostr << "var __child__ = " << getElementById(uid) << std::endl;
432 ostr << "__child__.style.visibility = '" << v << "';" << std::endl;
435 bool WebUtils::getUIEnable(int uid)
437 return getBoolProperty(uid, __GO_UI_ENABLE__);
440 void WebUtils::setUIEnable(int uid, std::ostringstream& ostr, bool append)
443 std::string v = getUIEnable(uid) ? "false" : "true";
446 ostr << "var __child__ = " << getElementById(uid) << std::endl;
449 ostr << "__child__.disabled = " << v << ";" << std::endl;
452 bool WebUtils::getUIBackgroundColor(int uid, std::vector<double>& vect)
455 getDoubleVectorProterty(uid, __GO_UI_BACKGROUNDCOLOR__, vect);
459 void WebUtils::setUIBackgroundColor(int uid, std::ostringstream& ostr, bool append)
461 std::vector<double> c;
462 getUIBackgroundColor(uid, c);
465 ostr << "var __child__ = " << getElementById(uid) << std::endl;
468 ostr << "__child__.style.backgroundColor = " << getColor(c) << ";" << std::endl;
471 bool WebUtils::getUIFontAngle(int uid, std::string& val)
473 val = getStringProperty(uid, __GO_UI_FONTANGLE__);
477 void WebUtils::setUIFontAngle(int uid, std::ostringstream& ostr, bool append)
480 getUIFontAngle(uid, angle);
483 ostr << "var __child__ = " << getElementById(uid) << std::endl;
486 ostr << "__child__.style.fontStyle = '" << angle << "';" << std::endl;
489 bool WebUtils::getUIFontName(int uid, std::string& val)
491 val = getStringProperty(uid, __GO_UI_FONTNAME__);
495 void WebUtils::setUIFontName(int uid, std::ostringstream& ostr, bool append)
498 getUIFontName(uid, font);
501 ostr << "var __child__ = " << getElementById(uid) << std::endl;
504 ostr << "__child__.style.fontFamily = '" << font << "','serif' ;" << std::endl;
507 bool WebUtils::getUIFontUnits(int uid, std::string& val)
509 val = getStringProperty(uid, __GO_UI_FONTUNITS__);
513 double WebUtils::getUIFontSize(int uid)
515 return getDoubleProperty(uid, __GO_UI_FONTSIZE__);
518 void WebUtils::setUIFontSize(int uid, std::ostringstream& ostr, bool append)
520 int size = (int)getUIFontSize(uid);
523 getUIFontUnits(uid, units);
525 if (units == "pixels")
532 size = (int)(size * 0.75);
537 ostr << "var __child__ = " << getElementById(uid) << std::endl;
540 ostr << "__child__.style.fontSize = '" << size << units << "';" << std::endl;
543 bool WebUtils::getUIFontWeight(int uid, std::string& val)
545 val = getStringProperty(uid, __GO_UI_FONTWEIGHT__);
549 void WebUtils::setUIFontWeight(int uid, std::ostringstream& ostr, bool append)
552 getUIFontWeight(uid, weight);
556 ostr << "var __child__ = " << getElementById(uid) << std::endl;
559 ostr << "__child__.style.fontWeight = '" << weight << "';" << std::endl;
562 bool WebUtils::getUIForegroundColor(int uid, std::vector<double>& vect)
565 getDoubleVectorProterty(uid, __GO_UI_FOREGROUNDCOLOR__, vect);
569 void WebUtils::setUIForegroundColor(int uid, std::ostringstream& ostr, bool append)
571 std::vector<double> c;
572 getUIForegroundColor(uid, c);
576 ostr << "var __child__ = " << getElementById(uid) << std::endl;
579 ostr << "__child__.style.color = " << getColor(c) << ";" << std::endl;
582 bool WebUtils::getUIHorizontalAlignment(int uid, std::string& val)
584 val = getStringProperty(uid, __GO_UI_HORIZONTALALIGNMENT__);
588 void WebUtils::setUIHorizontalAlignment(int uid, std::ostringstream& ostr, bool append)
591 getUIHorizontalAlignment(uid, align);
595 ostr << "var __child__ = " << getElementById(uid) << std::endl;
598 ostr << "__child__.style.textAlign = '" << align << "';" << std::endl;
601 bool WebUtils::getUIRelief(int uid, std::string& val)
603 val = getStringProperty(uid, __GO_UI_RELIEF__);
607 void WebUtils::setUIRelief(int uid, std::ostringstream& ostr, bool append)
611 bool WebUtils::getUIVerticalAlignment(int uid, std::string& val)
613 val = getStringProperty(uid, __GO_UI_VERTICALALIGNMENT__);
617 void WebUtils::setUIVerticalAlignment(int uid, std::ostringstream& ostr, bool append)
621 int WebUtils::getUILayout(int uid)
623 return getIntProperty(uid, __GO_LAYOUT__);
627 void WebUtils::setUILayout(int uid, std::ostringstream& ostr, bool append)
629 if (isFigure(uid) || hasStyle(uid, __GO_UI_FRAME__))
631 //TODO clean previous layout before create new one
635 ostr << "var __child__ = " << getElementById(uid) << ";" << std::endl;
638 int layout = getUILayout(uid);
643 //create a table ( ascii art powa ! )
644 //+---------------------+
646 //+---+-------------+---+
652 //+---+-------------+---+
654 //+---------------------+
656 //add "frame" in child to be more ... flexible ;)
658 ostr << "var __layout__ = " << createElement("DIV") << std::endl;
659 ostr << "__layout__.id = " << getIdString(uid, "_layout") << ";" << std::endl;
660 ostr << "__layout__.style.display = 'flex';" << std::endl;
661 ostr << "__layout__.style.flexDirection = 'column';" << std::endl;
662 ostr << "__layout__.style.width = '100%';" << std::endl;
663 ostr << "__layout__.style.height = '100%';" << std::endl;
666 ostr << "var __top__ = " << createElement("HEADER") << std::endl;
667 ostr << "__top__.id = " << getIdString(uid, "_top") << ";" << std::endl;
668 ostr << "__top__.style.width = '100%';" << std::endl;
671 ostr << "var __middle__ = " << createElement("DIV") << std::endl;
672 ostr << "__middle__.id = " << getIdString(uid, "_middle") << ";" << std::endl;
673 ostr << "__middle__.style.flex = '1';" << std::endl;
674 ostr << "__middle__.style.display = 'flex';" << std::endl;
677 ostr << "var __left__ = " << createElement("ASIDE") << std::endl;
678 ostr << "__left__.id = " << getIdString(uid, "_left") << ";" << std::endl;
679 ostr << "__left__.style.display = 'flex';" << std::endl;
682 ostr << "var __center__ = " << createElement("ARTICLE") << std::endl;
683 ostr << "__center__.id = " << getIdString(uid, "_center") << ";" << std::endl;
684 ostr << "__center__.style.flex = '1';" << std::endl;
685 ostr << "__center__.style.display = 'flex';" << std::endl;
686 ostr << "__center__.style.width = '100%';" << std::endl;
689 ostr << "var __right__ = " << createElement("ASIDE") << std::endl;
690 ostr << "__right__.id = " << getIdString(uid, "_right") << ";" << std::endl;
691 ostr << "__right__.style.display = 'flex';" << std::endl;
694 ostr << "var __bottom__ = " << createElement("FOOTER") << std::endl;
695 ostr << "__bottom__.id = " << getIdString(uid, "_bottom") << ";" << std::endl;
696 ostr << "__bottom__.style.width = '100%';" << std::endl;
699 ostr << "__middle__.appendChild(__left__);" << std::endl;
700 ostr << "__middle__.appendChild(__center__);" << std::endl;
701 ostr << "__middle__.appendChild(__right__);" << std::endl;
703 ostr << "__layout__.appendChild(__top__);" << std::endl;
704 ostr << "__layout__.appendChild(__middle__);" << std::endl;
705 ostr << "__layout__.appendChild(__bottom__);" << std::endl;
707 ostr << "__child__.appendChild(__layout__);" << std::endl;
717 ostr << "var __table__ = " << createElement("TABLE") << std::endl;
718 ostr << "__table__.id = " << getIdString(uid, "_table") << ";" << std::endl;
719 ostr << "__table__.style.margin = '0';" << std::endl;
720 ostr << "__table__.style.padding = '0';" << std::endl;
721 ostr << "__table__.style.width = '100%';" << std::endl;
722 ostr << "__table__.style.height = '100%';" << std::endl;
723 ostr << "__table__.style.borderCollapse = 'collapse';" << std::endl;
726 ostr << "__child__.appendChild(__table__);" << std::endl;
739 double WebUtils::getUIMin(int uid)
741 return getDoubleProperty(uid, __GO_UI_MIN__);
744 void WebUtils::setUIMin(int uid, std::ostringstream& ostr, bool append)
746 if (hasStyle(uid, __GO_UI_SPINNER__) || hasStyle(uid, __GO_UI_SLIDER__))
748 double min = getUIMin(uid);
752 ostr << "var __child__ = " << getElementById(uid) << std::endl;
755 ostr << "__child__.min = '" << min << "';" << std::endl;
759 double WebUtils::getUIMax(int uid)
761 return getDoubleProperty(uid, __GO_UI_MAX__);
764 void WebUtils::setUIMax(int uid, std::ostringstream& ostr, bool append)
766 if (hasStyle(uid, __GO_UI_SPINNER__) || hasStyle(uid, __GO_UI_SLIDER__))
768 double max = getUIMax(uid);
772 ostr << "var __child__ = " << getElementById(uid) << std::endl;
775 ostr << "__child__.max = '" << max << "';" << std::endl;
779 double WebUtils::getUIStep(int uid)
781 std::vector<double> step(2, 0.);
782 getDoubleVectorProterty(uid, __GO_UI_SLIDERSTEP__, step);
786 void WebUtils::setUIStep(int uid, std::ostringstream& ostr, bool append)
788 if (hasStyle(uid, __GO_UI_SPINNER__) || hasStyle(uid, __GO_UI_SLIDER__))
790 double step = getUIStep(uid);
794 ostr << "var __child__ = " << getElementById(uid) << std::endl;
797 ostr << "__child__.step = '" << step << "';" << std::endl;
801 bool WebUtils::getUIValue(int uid, std::vector<double>& vect)
803 int size = getIntProperty(uid, __GO_UI_VALUE_SIZE__);
804 vect.resize(size, 0.);
805 getDoubleVectorProterty(uid, __GO_UI_VALUE__, vect);
809 void WebUtils::setUIValue(int uid, std::ostringstream& ostr, bool append)
811 switch (getStyle(uid))
813 case __GO_UI_SPINNER__:
814 case __GO_UI_SLIDER__:
816 std::vector<double> values;
817 getUIValue(uid, values);
818 if (values.size() == 0)
825 ostr << "var __child__ = " << getElementById(uid) << std::endl;
828 ostr << "__child__.value = '" << values[0] << "';" << std::endl;
834 std::vector<double> values;
835 getUIValue(uid, values);
836 if (values.size() == 0)
843 ostr << "var __child__ = " << getElementById(uid) << std::endl;
846 ostr << "tabSelectHelper(__child__, " << values[0] << ");" << std::endl;
852 void WebUtils::getUIBorderPreferredSize(int uid, std::vector<int>& vect)
855 getIntVectorProterty(uid, __GO_UI_BORDER_PREFERREDSIZE__, vect);
858 void WebUtils::getUIBorderPadding(int uid, std::vector<int>& vect)
861 getIntVectorProterty(uid, __GO_BORDER_OPT_PADDING__, vect);
864 int WebUtils::getUIBorderPosition(int uid)
866 return getIntProperty(uid, __GO_UI_BORDER_POSITION__);
869 void WebUtils::setUIBorder(int uid, std::ostringstream& ostr, bool append)
871 int parent = getParent(uid);
872 //std::cerr << "setUIBorder : " << uid << " -> " << parent << std::endl;
873 std::string position;
875 int border = getUIBorderPosition(uid);
877 std::vector<int> pad;
878 getUIBorderPadding(parent, pad);
880 std::vector<int> size;
881 getUIBorderPreferredSize(uid, size);
887 ostr << "var __child__ = " << getElementById(uid) << std::endl;
894 position = "_center";
895 ostr << "__child__.style.width = 'inherit';" << std::endl;
896 ostr << "__child__.style.height = 'inherit';" << std::endl;
897 padding = getPadding(0, 0, 0, 0);
900 position = "_bottom";
901 ostr << "__child__.style.width = '100%';" << std::endl;
905 ostr << "__child__.style.height = 'inherit';" << std::endl;
909 ostr << "__child__.style.height = '" << getSubPadding(size[1]) << "';" << std::endl;
912 padding = getPadding(pad[1], 0, 0, 0);
916 ostr << "__child__.style.width = '100%';" << std::endl;
920 ostr << "__child__.style.height = 'inherit';" << std::endl;
924 ostr << "__child__.style.height = '" << getSubPadding(size[1]) << "';" << std::endl;
927 padding = getPadding(0, 0, pad[1], 0);
934 ostr << "__child__.style.width = 'inherit';" << std::endl;
938 ostr << "__child__.style.width = '" << getSubPadding(size[0]) << "';" << std::endl;
941 ostr << "__child__.style.height = 'inherit';" << std::endl;
942 padding = getPadding(0, pad[0], 0, 0);
949 ostr << "__child__.style.width = 'inherit';" << std::endl;
953 ostr << "__child__.style.width = '" << getSubPadding(size[0]) << "';" << std::endl;
956 ostr << "__child__.style.height = 'inherit';" << std::endl;
957 padding = getPadding(0, 0, 0, pad[0]);
962 //move child in targeted cell
963 ostr << "__parent__ = " << getElementById(parent, position) << std::endl;
964 ostr << "__parent__.appendChild(__child__);" << std::endl;
965 ostr << "__parent__.style.padding = '" << padding << "';" << std::endl;
967 //to well perform positionning, we must clear some default properties
968 //position left, right, width, height,
970 ostr << "__child__.style.position = 'inherit';" << std::endl;
971 ostr << "__child__.style.left = 'inherit';" << std::endl;
972 ostr << "__child__.style.bottom = 'inherit';" << std::endl;
975 void WebUtils::getUIGridBagGrid(int uid, std::vector<int>& vect)
978 getIntVectorProterty(uid, __GO_UI_GRIDBAG_GRID__, vect);
982 void WebUtils::setUIGridBag(int uid, std::ostringstream& ostr, bool append)
984 int parent = getParent(uid);
985 std::vector<int> grid;
986 getUIGridBagGrid(uid, grid);
990 ostr << "var __child__ = " << getElementById(uid) << std::endl;
993 ostr << "__child__.style.position = 'inherit';" << std::endl;
994 ostr << "__child__.style.left = 'inherit';" << std::endl;
995 ostr << "__child__.style.bottom = 'inherit';" << std::endl;
996 ostr << "__child__.style.width = '100%';" << std::endl;
997 ostr << "__child__.style.height = '100%';" << std::endl;
998 //we have to create a td with grid information and add it to the good cell ( or create if not exist ! )
1002 td = "var __td__ = " + createElement("TD");
1004 std::string name("_" + std::to_string(grid[0]) + "_" + std::to_string(grid[1]));
1005 td += "__td__.id = " + getIdString(parent, name) + ";";
1009 td += "__td__.colSpan = '" + std::to_string(grid[2]) + "';";
1014 td += "__td__.rowSpan = '" + std::to_string(grid[3]) + "';";
1017 td += "__td__.appendChild(__child__);";
1020 name = "_" + std::to_string(grid[1]);
1022 tr = "var __tr__ = " + getElementById(parent, name);
1023 tr += "var __table__ = " + getElementById(parent, "_table");
1024 tr += "if(__tr__ == null){";
1025 tr += "__tr__ = " + createElement("TR");
1026 tr += "__tr__.id = " + getIdString(parent, name) + ";";
1027 tr += "gridbagHelperTR(__table__, __tr__, " + std::to_string(grid[1]) + ");";
1029 tr += "gridbagHelperTD(__tr__,__td__, " + std::to_string(grid[0]) + ");";
1031 //to force refresh of table, move it to another component and restore it
1032 std::string tricktoforcerefresh("var __scilab__ = document.getElementById('scilab');");
1033 tricktoforcerefresh += "__scilab__.appendChild(__table__);";
1034 tricktoforcerefresh += "var __parent__ = " + getElementById(parent);
1035 tricktoforcerefresh += "__parent__.appendChild(__table__);";
1038 ostr << td << std::endl;
1039 ostr << tr << std::endl;
1040 ostr << tricktoforcerefresh << std::endl;
1043 bool WebUtils::hasCallback(int uid)
1045 return getStringProperty(uid, __GO_CALLBACK__) != "";
1048 void WebUtils::setCallback(int uid, std::ostringstream& ostr, bool append)
1050 if (append == false)
1052 ostr << "var __child__ = " << getElementById(uid) << std::endl;
1057 switch (getStyle(uid))
1059 case __GO_UI_PUSHBUTTON__ :
1061 func = "onPushButton";
1063 case __GO_UI_CHECKBOX__:
1065 func = "onCheckBox";
1067 case __GO_UI_RADIOBUTTON__:
1069 func = "onRadioButton";
1071 case __GO_UI_LISTBOX__:
1075 case __GO_UI_POPUPMENU__:
1077 func = "onComboBox";
1082 func = "onPushButton";
1085 case __GO_UI_SLIDER__:
1089 case __GO_UI_EDIT__:
1093 case __GO_UI_SPINNER__:
1101 //add callback listener
1102 ostr << "__child__.addEventListener('" << event << "', " << func << ");" << std::endl;
1106 bool WebUtils::isInputType(int uid)
1108 switch (getStyle(uid))
1110 case __GO_UI_EDIT__ :
1111 case __GO_UI_SLIDER__:
1112 case __GO_UI_SPINNER__ :
1119 bool WebUtils::hasStyle(int uid, int style)
1121 if (isUIcontrol(uid))
1123 return getStyle(uid) == style;
1129 bool WebUtils::isFigure(int uid)
1131 return getType(uid) == __GO_FIGURE__;
1134 bool WebUtils::isUIcontrol(int uid)
1136 return getType(uid) == __GO_UICONTROL__;
1139 bool WebUtils::isButton(int uid)
1141 return hasStyle(uid, __GO_UI_PUSHBUTTON__);
1146 bool WebUtils::createFigure(int uid, std::ostringstream& ostr)
1148 //set figure uid to help children to find it
1149 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1150 ostr << "var __temp__ = " << createElement("DIV") << std::endl;
1151 ostr << "__temp__.id = " << getIdString(uid) << ";" << std::endl;
1152 ostr << "__temp__.className = 'GO_FIGURE';" << std::endl;
1153 ostr << "__parent__.innerHTML += '<br>';" << std::endl;
1154 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1155 ostr << "__parent__.innerHTML += '<br>';" << std::endl;
1159 bool WebUtils::createUIControl(int uid, std::ostringstream& ostr)
1161 int style = getStyle(uid);
1164 case __GO_UI_FRAME__:
1165 return createFrame(uid, ostr);
1166 case __GO_UI_PUSHBUTTON__:
1167 return createPushButton(uid, ostr);
1168 case __GO_UI_TEXT__:
1169 return createText(uid, ostr);
1170 case __GO_UI_EDIT__:
1171 return createEdit(uid, ostr);
1172 case __GO_UI_CHECKBOX__:
1173 return createCheckbox(uid, ostr);
1174 case __GO_UI_RADIOBUTTON__:
1175 return createRadio(uid, ostr);
1176 case __GO_UI_SLIDER__:
1177 return createSlider(uid, ostr);
1178 case __GO_UI_LISTBOX__:
1179 return createListbox(uid, ostr);
1180 case __GO_UI_POPUPMENU__:
1181 return createCombobox(uid, ostr);
1182 case __GO_UI_SPINNER__:
1183 return createSpinner(uid, ostr);
1185 return createTab(uid, ostr);
1191 bool WebUtils::createCommonIUControl(int uid, const std::string& htmlType, const std::string& cssClass, std::ostringstream& ostr)
1193 //create a button, with no parent, wait update with _GO_PARENT to update it
1194 ostr << "var __temp__ = " << createElement(htmlType) << std::endl;
1195 ostr << "__temp__.id = " << getIdString(uid) << ";" << std::endl;
1196 ostr << "__temp__.className = '" << cssClass << "';" << std::endl;
1201 bool WebUtils::createPushButton(int uid, std::ostringstream& ostr)
1204 createCommonIUControl(uid, "BUTTON", "GO_PUSHBUTTON", ostr);
1205 //add item temporary in main scilabview div waiting __GO_PARENT__ update
1206 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1207 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1212 bool WebUtils::createFrame(int uid, std::ostringstream& ostr)
1215 createCommonIUControl(uid, "DIV", "GO_FRAME", ostr);
1216 //add item temporary in main div waiting __GO_PARENT__ update
1217 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1218 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1223 bool WebUtils::createText(int uid, std::ostringstream& ostr)
1226 createCommonIUControl(uid, "LABEL", "GO_TEXT", ostr);
1227 //add item temporary in main div waiting __GO_PARENT__ update
1228 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1229 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1234 bool WebUtils::createEdit(int uid, std::ostringstream& ostr)
1237 createCommonIUControl(uid, "INPUT", "GO_EDIT", ostr);
1238 ostr << "__temp__.type = 'text';" << std::endl;
1239 //add item temporary in main div waiting __GO_PARENT__ update
1240 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1241 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1246 bool WebUtils::createCheckbox(int uid, std::ostringstream& ostr)
1250 //for checkbox we need to create 3 elements.
1252 //a div to enclose others
1253 ostr << "var __main__ = " << createElement("DIV") << std::endl;
1254 ostr << "__main__.id = " << getIdString(uid) << ";" << std::endl;
1255 ostr << "__main__.className = 'GO_CHECKBOX';" << std::endl;
1256 //add item temporary in main div waiting __GO_PARENT__ update
1257 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1258 ostr << "__parent__.appendChild(__main__);" << std::endl;
1260 //the checkbox itself
1261 ostr << "var __temp__ = " << createElement("INPUT") << std::endl;
1262 ostr << "__temp__.className = 'GO_CHECKBOX_CHECKBOX';" << std::endl;
1263 ostr << "__temp__.type = 'checkbox';" << std::endl;
1264 ostr << "__temp__.id = " << getIdString(uid, "_checkbox") << ";" << std::endl;
1265 //add item temporary in main div waiting __GO_PARENT__ update
1266 ostr << "__main__.appendChild(__temp__);" << std::endl;
1268 //the label of the checkbox
1269 ostr << "var __label__ = " << createElement("LABEL") << std::endl;
1270 ostr << "__label__.id = " << getIdString(uid, "_label") << ";" << std::endl;
1271 ostr << "__label__.className = 'GO_CHECKBOX_LABEL';" << std::endl;
1272 ostr << "__label__.htmlFor = " << getIdString(uid, "_checkbox") << ";" << std::endl;
1273 //add item temporary in main div waiting __GO_PARENT__ update
1274 ostr << "__main__.appendChild(__label__);" << std::endl;
1279 bool WebUtils::createRadio(int uid, std::ostringstream& ostr)
1283 //for radio we need to create 3 elements.
1285 //a div to enclose others
1286 ostr << "var __main__ = " << createElement("DIV") << std::endl;
1287 ostr << "__main__.id = " << getIdString(uid) << ";" << std::endl;
1288 ostr << "__main__.className = 'GO_RADIO';" << std::endl;
1289 //add item temporary in main div waiting __GO_PARENT__ update
1290 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1291 ostr << "__parent__.appendChild(__main__)" << std::endl;
1294 ostr << "var __temp__ = " << createElement("INPUT") << std::endl;
1295 ostr << "__temp__.className = 'GO_RADIO_RADIO';" << std::endl;
1296 ostr << "__temp__.type = 'radio';" << std::endl;
1297 ostr << "__temp__.id = " << getIdString(uid, "_radio") << ";" << std::endl;
1298 //add item temporary in main div waiting __GO_PARENT__ update
1299 ostr << "__main__.appendChild(__temp__);" << std::endl;
1301 //the label of the checkbox
1302 ostr << "var __label__ = " << createElement("LABEL") << std::endl;
1303 ostr << "__label__.id = " << getIdString(uid, "_label") << ";" << std::endl;
1304 ostr << "__label__.className = 'GO_RADIO_LABEL';" << std::endl;
1305 ostr << "__label__.htmlFor = " << getIdString(uid, "_radio") << ";" << std::endl;
1306 //add item temporary in main div waiting __GO_PARENT__ update
1307 ostr << "__main__.appendChild(__label__);" << std::endl;
1312 bool WebUtils::createSlider(int uid, std::ostringstream& ostr)
1315 createCommonIUControl(uid, "INPUT", "GO_SLIDER", ostr);
1316 ostr << "__temp__.type = 'range';" << std::endl;
1317 //add item temporary in main div waiting __GO_PARENT__ update
1318 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1319 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1324 bool WebUtils::createListbox(int uid, std::ostringstream& ostr)
1327 createCommonIUControl(uid, "SELECT", "GO_LISTBOX", ostr);
1328 //add item temporary in main div waiting __GO_PARENT__ update
1329 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1330 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1335 bool WebUtils::createCombobox(int uid, std::ostringstream& ostr)
1338 createCommonIUControl(uid, "SELECT", "GO_COMBOBOX", ostr);
1339 ostr << "__temp__.size = 1;" << std::endl;
1340 //add item temporary in main div waiting __GO_PARENT__ update
1341 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1342 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1347 bool WebUtils::createSpinner(int uid, std::ostringstream& ostr)
1350 createCommonIUControl(uid, "INPUT", "GO_SPINNER", ostr);
1351 ostr << "__temp__.type = 'number';" << std::endl;
1352 //add item temporary in main div waiting __GO_PARENT__ update
1353 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1354 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1359 bool WebUtils::createTab(int uid, std::ostringstream& ostr)
1362 createCommonIUControl(uid, "DIV", "GO_TAB", ostr);
1365 ostr << "var __ul__ = " << createElement("UL") << std::endl;
1366 ostr << "__ul__.id = " << getIdString(uid, "_ul") << ";" << std::endl;
1367 ostr << "__temp__.appendChild(__ul__);" << std::endl;
1370 //add item temporary in main div waiting __GO_PARENT__ update
1371 ostr << "var __parent__ = document.getElementById('scilab');" << std::endl;
1372 ostr << "__parent__.appendChild(__temp__);" << std::endl;
1377 bool WebUtils::addTabChild(int uid, int child, std::ostringstream& ostr)
1379 std::vector<std::string> vect;
1382 ostr << "var __li__ = " << createElement("LI") << std::endl;
1383 ostr << "__li__.id = " << getIdString(child, "_li") << ";" << std::endl;
1385 //create input button
1386 ostr << "var __btn__ = " << createElement("INPUT") << std::endl;
1387 ostr << "__btn__.id = " << getIdString(child, "_btn") << ";" << std::endl;
1388 ostr << "__btn__.type = 'button';" << std::endl;
1389 ostr << "__btn__.addEventListener('click', onTab);" << std::endl;
1392 ostr << "__li__.appendChild(__btn__);" << std::endl;
1394 //add il as first child in ul
1395 ostr << "var __ul__ = " << getElementById(uid, "_ul") << std::endl;
1396 ostr << "__ul__.insertBefore(__li__, __ul__.firstChild);" << std::endl;
1398 //update child properties
1399 ostr << "__child__.style.position = 'inherit';" << std::endl;
1400 ostr << "__child__.style.left = 'inherit';" << std::endl;
1401 ostr << "__child__.style.bottom = 'inherit';" << std::endl;
1402 ostr << "__child__.style.width = '100%';" << std::endl;
1403 ostr << "__child__.style.height = '100%';" << std::endl;
1405 //be carefull, we change __child__ to tab uid instead of frame uid
1406 setUIValue(uid, ostr);
1411 bool WebUtils::deleteObject(int uid, std::ostringstream& ostr)
1413 ostr << "var __child__ = " << getElementById(uid) << std::endl;
1414 ostr << "__child__.parentNode.removeChild(__child__);" << std::endl;
1419 bool WebUtils::updateDefaultProperties(int uid, std::ostringstream& ostr)
1422 setVisible(uid, ostr);
1425 setUIBackgroundColor(uid, ostr, true);
1428 setUIEnable(uid, ostr, true);
1431 setUIFontAngle(uid, ostr, true);
1434 setUIFontName(uid, ostr, true);
1436 //fontsize & fontunits
1437 setUIFontSize(uid, ostr, true);
1440 setUIFontWeight(uid, ostr, true);
1443 setUIForegroundColor(uid, ostr, true);
1445 //horizontalalignment
1446 setUIHorizontalAlignment(uid, ostr, true);
1449 setUIRelief(uid, ostr, true);
1452 setUIString(uid, ostr, true);
1455 setUIVerticalAlignment(uid, ostr, true);
1458 setUIPosition(uid, ostr, true);
1461 setUILayout(uid, ostr, true);
1464 setUIMin(uid, ostr, true);
1467 setUIMax(uid, ostr, true);
1470 setUIStep(uid, ostr, true);
1473 setUIValue(uid, ostr, true);
1475 //set callback uses to update values from web view
1476 setCallback(uid, ostr, true);
1481 bool WebUtils::set(int prop, int uid, std::ostringstream& ostr, bool append)
1483 SETTER::iterator it = setter.find(prop);
1484 if (it != setter.end())
1486 it->second(uid, ostr, append);
1494 void WebUtils::fillSetter()
1496 setter[__GO_PARENT__] = WebUtils::setParent;
1497 setter[__GO_POSITION__] = WebUtils::setUIPosition;
1498 setter[__GO_SIZE__] = WebUtils::setFigureSize;
1499 setter[__GO_UI_STRING__] = WebUtils::setUIString;
1500 setter[__GO_VISIBLE__] = WebUtils::setVisible;
1501 setter[__GO_UI_ENABLE__] = WebUtils::setUIEnable;
1502 setter[__GO_UI_BACKGROUNDCOLOR__] = WebUtils::setUIBackgroundColor;
1503 setter[__GO_UI_FONTANGLE__] = WebUtils::setUIFontAngle;
1504 setter[__GO_UI_FONTNAME__] = WebUtils::setUIFontName;
1505 setter[__GO_UI_FONTSIZE__] = WebUtils::setUIFontSize;
1506 setter[__GO_UI_FONTWEIGHT__] = WebUtils::setUIFontWeight;
1507 setter[__GO_UI_FOREGROUNDCOLOR__] = WebUtils::setUIForegroundColor;
1508 setter[__GO_UI_HORIZONTALALIGNMENT__] = WebUtils::setUIHorizontalAlignment;
1509 setter[__GO_UI_RELIEF__] = WebUtils::setUIRelief;
1510 setter[__GO_UI_VERTICALALIGNMENT__] = WebUtils::setUIVerticalAlignment;
1511 setter[__GO_LAYOUT__] = WebUtils::setUILayout;
1512 setter[__GO_UI_MIN__] = WebUtils::setUIMin;
1513 setter[__GO_UI_MAX__] = WebUtils::setUIMax;
1514 setter[__GO_UI_SLIDERSTEP__] = WebUtils::setUIStep;
1515 setter[__GO_UI_VALUE__] = WebUtils::setUIValue;
1516 //preferred size is the last property to be set for border constraints
1517 setter[__GO_UI_BORDER_PREFERREDSIZE__] = WebUtils::setUIBorder;
1518 //preferred size is the last property to be set for gridbag constraints
1519 setter[__GO_UI_GRIDBAG_PREFERREDSIZE__] = WebUtils::setUIGridBag;
1520 //setter[__GO_CALLBACK__] = WebUtils::setCallback;
1523 bool WebUtils::updateValue(int uid, const std::string& value)
1525 if (hasStyle(uid, __GO_UI_EDIT__))
1527 std::vector<std::string> v(1, value);
1528 setStringVectorProperty(uid, __GO_UI_STRING__, v);
1534 bool WebUtils::updateValue(int uid, const std::vector<double>& values)
1536 setDoubleVectorProperty(uid, __GO_UI_VALUE__, values);
1540 bool WebUtils::updateValue(int uid, double value)
1542 std::vector<double> v(1, value);
1543 setDoubleVectorProperty(uid, __GO_UI_VALUE__, v);
1547 bool WebUtils::updateValue(int uid, bool value)
1549 std::vector<double> v(1, value ? getUIMax(uid) : getUIMin(uid));
1550 setDoubleVectorProperty(uid, __GO_UI_VALUE__, v);
1554 void WebUtils::addInWaitingQueue(int uid, int prop)
1556 std::cerr << "add in queue : " << uid << "(" << prop << ")" << std::endl;
1557 waitprop[uid].push_back(prop);
1560 void WebUtils::setWaitingProperties(int uid, std::ostringstream& ostr, bool append)
1562 WAITING_PROP::iterator it = waitprop.find(uid);
1563 if (it != waitprop.end())
1565 for (int prop : it->second)
1567 set(prop, uid, ostr, append);