3 * Code to convert node tree to Vala...
5 * usage : x = (new JsRender.NodeToVala(node)).munge();
9 * pack - can we come up with a replacement?
10 - parent.child == child_widget -- actually uses getters and effectively does 'add'?
14 * args -- vala constructor args (should really only be used at top level - we did use it for clutter originally(
15 * ctor -- different ctor argument
24 public abstract class JsRender.NodeToVala : NodeWriter {
26 protected string this_el = "??";
28 int child_count = 1; // used to number the children.
29 public string cls; // node fqn()
33 Gee.ArrayList<string> ignoreWrappedList;
34 Gee.ArrayList<string> myvars;
38 int pane_number = 0;// ?? used when generating Gtk.Pane tabs
42 NodeWriter.globalIgnore("pack");
43 NodeWriter.globalIgnore("init");
44 NodeWriter.globalIgnore("xns");
45 NodeWriter.globalIgnore("xtype");
46 NodeWriter.globalIgnore("id");
50 * ctor - just initializes things
51 * - wraps a render node
53 protected NodeToVala( JsRender file, Node node, int depth, NodeToVala? parent)
56 base (file, node, depth, parent);
58 this.initPadding('\t', 1);
60 this.cls = node.xvala_cls;
61 this.xcls = node.xvala_xcls;
62 if (depth == 0 && this.xcls.contains(".")) {
63 var ar = this.xcls.split(".");
64 this.xcls = ar[ar.length-1];
68 this.ignoreWrappedList = new Gee.ArrayList<string>();
69 this.myvars = new Gee.ArrayList<string>();
76 this.cls = this.file.tree.xvala_cls;
77 this.xcls = this.file.tree.xvala_xcls;
79 public abstract string mungeChild( Node cnode);
82 public void namespaceHeader()
84 if (this.depth > 0 || this.file.file_namespace == "") {
87 this.addLine("namespace " + this.file.file_namespace);
91 public void namespaceFooter()
93 if (this.depth > 0 || this.file.file_namespace == "") {
101 protected abstract void classHeader();
106 * when ID is used... on an element, it registeres a property on the top level...
107 * so that _this.ID always works..
110 protected void addTopProperties()
112 if (this.depth > 0) {
115 // properties - global..??
116 foreach(var n in this.top_level_items) {
118 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
122 if (n.xvala_id[0] == '*' || n.xvala_id[0] == '+') {
126 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
132 * create properties that are not 'part of the wrapped element.
137 protected void addMyVars()
139 GLib.debug("calling addMyVars");
142 this.addLine(this.ipad + "// my vars (def)");
146 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
149 GLib.debug("Gir factory failed to find class %s", this.node.fqn());
156 foreach(var prop in this.node.props.values) {
158 if (this.shouldIgnore(prop.name)) {
162 // user defined method
163 if (prop.ptype == NodePropType.METHOD) {
166 if (prop.ptype == NodePropType.SPECIAL) {
170 if (prop.ptype == NodePropType.SIGNAL) {
171 this.node.setLine(this.cur_line, "p", prop.name);
172 this.addLine(this.pad + "public signal " + prop.rtype + " " + prop.name + " " + prop.val + ";");
174 this.ignore(prop.name);
178 GLib.debug("Got myvars: %s", prop.name.strip());
180 if (prop.rtype.strip().length < 1) {
184 var isUser = prop.ptype == NodePropType.USER;
185 if (this.node.fqn() == "Gtk.NotebookPage") {
188 // is it a class property...
189 if (cls != null && cls.props.has_key(prop.name) && !isUser) {
193 this.myvars.add(prop.name);
194 prop.start_line = this.cur_line;
196 this.node.setLine(this.cur_line, "p", prop.name);
198 this.addLine(this.pad + "public " + prop.rtype + " " + prop.name + ";"); // definer - does not include value.
201 prop.end_line = this.cur_line;
202 this.ignore(prop.name);
208 // if id of child is '+' then it's a property of this..
209 protected void addPlusProperties()
211 if (this.node.readItems().size < 1) {
214 var iter = this.node.readItems().list_iterator();
215 while (iter.next()) {
218 if (ci.xvala_id[0] != '+') {
219 continue; // skip generation of children?
223 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
229 * add the constructor definition..
231 protected abstract void addValaCtor();
233 * make sure _this is defined..
235 protected void addUnderThis()
239 this.addLine( this.ipad + "_this = this;");
242 // for non top level = _this point to owner, and _this.ID is set
244 this.addLine( this.ipad + "_this = _owner;");
246 if (this.node.props.has_key("id")
248 this.node.xvala_id != ""
250 this.node.xvala_id[0] != '*'
252 this.node.xvala_id[0] != '+'
254 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
262 protected void addInitMyVars()
264 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
265 //print(JSON.stringify(meths,null,4));Seed.quit();
269 // initialize.. my vars..
271 this.addLine( this.ipad + "// my vars (dec)");
273 var iter = this.myvars.list_iterator();
279 var prop = this.node.props.get(k);
281 var v = prop.val.strip();
286 // at this point start using
288 if (v == "FALSE" || v == "TRUE") {
291 //FIXME -- check for raw string.. "string XXXX"
292 var is_raw = prop.ptype == NodePropType.RAW;
294 // what's the type.. - if it's a string.. then we quote it..
295 if (prop.rtype == "string" && !is_raw) {
296 v = "\"" + v.escape("") + "\"";
298 // if it's a string...
300 prop.start_line = this.cur_line;
301 this.addLine(this.ipad + "this." + prop.name + " = " + v +";");
302 prop.end_line = this.cur_line;
310 protected void addWrappedProperties()
312 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
314 GLib.debug("Skipping wrapped properties - could not find class %s" , this.node.fqn());
318 if (this.node.fqn() == "Gtk.NotebookPage") {
321 // what are the properties of this class???
323 this.addLine(this.ipad + "// set gobject values");
325 foreach(var p in cls.props.keys) {
326 var val = cls.props.get(p);
327 //print("Check Write %s\n", p);
328 if (!this.node.has(p)) {
331 if (this.shouldIgnoreWrapped(p)) {
338 var prop = this.node.get_prop(p);
341 // user defined properties.
342 if (prop.ptype == NodePropType.USER) {
348 var is_raw = prop.ptype == NodePropType.RAW;
350 // what's the type.. - if it's a string.. then we quote it..
351 if (val.type == "string" && !is_raw) {
352 v = "\"" + v.escape("") + "\"";
354 if (v == "TRUE" || v == "FALSE") {
357 if (val.type == "float" && v[v.length-1] != 'f') {
361 prop.start_line = this.cur_line;
362 this.addLine("%s%s%s = %s;".printf(ipad,this.this_el,p,v)); // // %s, iter.get_value().type);
366 prop.end_line = this.cur_line;
373 * pack the children into the parent.
375 * if the child's id starts with '*' then it is not packed...
376 * - this allows you to define children and add them manually..
379 protected void addChildren()
382 GLib.debug("addChildren %s, %d", this.node.fqn(), (int)this.node.readItems().size);
383 if (this.node.readItems().size < 1) {
386 this.pane_number = 0;
387 var cols = this.node.has("* columns") ? int.max(1, int.parse(this.node.get_prop("* columns").val)) : 1;
395 foreach(var child in this.node.readItems()) {
400 if (child.xvala_id[0] == '*') {
401 continue; // skip generation of children?
404 // probably added in ctor..
405 if (child.has("* prop") && this.shouldIgnoreWrapped(child.get_prop("* prop").val)) {
408 // create the element..
411 // this is only needed if it does not have an ID???
412 var childname = this.addPropSet(child, child.has("id") ? child.get_prop("id").val : "") ;
413 if (!child.has("id") && this.this_el == "this.el.") {
414 this.addLine(this.ipad + childname +".ref();");
416 if (child.has("* prop")) {
417 if (this.node.fqn() == "Gtk.NotebookPage") {
418 switch (child.get_prop("* prop").val) {
420 nb_child = childname;
434 // fixme special packing!??!?!
435 if (child.get_prop("* prop").val.contains("[]")) {
436 // currently this is not used?
437 // and it will not add ref..
439 this.packChild(child, childname, 0, 0, child.get_prop("* prop").val); /// fixme - this is a bit speciall...
445 this.ignoreWrapped(child.get_prop("* prop").val);
446 var el_name = this.this_el == "this.el." ? ".el" : "";
449 this.addLine(ipad + this.this_el + child.get_prop("* prop").val + " = " + childname + el_name +";");
454 this.packChild(child, childname, cols, colpos);
456 if (child.has("colspan")) {
457 colpos += int.parse(child.get_prop("colspan").val);
463 // this.{id - without the '+'} = the element...
468 GLib.debug("got node %s with nb_child= %s", this.node.fqn() , nb_child);
469 if (this.node.fqn() == "Gtk.NotebookPage" && nb_child != "") {
470 if (nb_tab == "" && this.node.has("tab_label")) {
471 nb_tab = "new Gtk.Label(this.tab_label)";
474 if (nb_menu == "" && nb_tab == "") {
475 this.addLine(@"$(ipad)notebook.el.append_page( $(nb_child) );");
479 this.addLine(@"$(ipad)notebook.el.append_page( $(nb_child) , $(nb_tab) );");
482 this.addLine(@"$(ipad)notebook.el.append_page_menu( $(nb_child) , $(nb_tab), $(nb_menu) );");
488 var childname = new Xcls_.... (....)
492 protected string addPropSet(Node child, string child_name)
497 if (child.has("* args")) {
499 var ar = child.get_prop("* args").val.split(",");
500 for (var ari = 0 ; ari < ar.length; ari++ ) {
501 var arg = ar[ari].split(" ");
502 xargs += "," + arg[arg.length -1];
506 var childname = "child_" + "%d".printf(this.child_count++);
508 if (child_name == "") {
509 prefix = "var " + childname + " = ";
511 var cls = child.xvala_xcls;
513 if (this.this_el == "this.") {
514 var clsdata = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
515 //if (clsdata.is_sealed) {
516 cls = this.node.fqn(); // need ctor data...
517 this.addLine(this.ipad + @"$(prefix)new $cls( _this $xargs);" );
518 return child_name == "" ? childname : ("_this." + child_name);
522 if (child.fqn() == "Gtk.NotebookPage") {
526 this.addLine(this.ipad + @"$(prefix)new $cls( _this $xargs);" );
528 // add a ref... (if 'id' is not set... to a '+' ?? what does that mean? - fake ids?
529 // remove '+' support as I cant remember what it does!!!
530 //if (child.xvala_id.length < 1 ) {
531 // this.addLine(this.ipad + childname +".ref();"); // we need to reference increase unnamed children...
533 //if (child.xvala_id[0] == '+') {
534 // this.addLine(this.ipad + "this." + child.xvala_id.substring(1) + " = " + childname+ ";");
539 return child_name == "" ? childname : ("_this." + child_name);
545 protected void packChild(Node child, string childname, int cols, int colpos, string propname= "")
548 GLib.debug("packChild %s=>%s", this.node.fqn(), child.fqn());
549 // forcing no packing? - true or false? -should we just accept false?
550 if (child.has("* pack") && child.get("* pack").down() == "false") {
551 return; // force no packing
553 if (child.has("* pack") && child.get("* pack").down() == "true") {
554 return; // force no packing
556 var el_name = this.this_el == "this.el." ? ".el" : "";
557 var this_el = this.this_el;
558 // BC really - don't want to support this anymore.
559 if (child.has("* pack")) {
561 string[] packing = { "add" };
562 if (child.has("* pack")) {
563 packing = child.get("* pack").split(",");
566 var pack = packing[0];
567 this.addLine(this.ipad + this.this_el + pack.strip() + " ( " + childname + el_name + " " +
568 (packing.length > 1 ?
569 (", " + string.joinv(",", packing).substring(pack.length+1))
573 var childcls = this.file.project.palete.getClass(child.fqn()); // very trusting..
574 if (childcls == null) {
578 var is_event = childcls.inherits.contains("Gtk.EventController") || childcls.implements.contains("Gtk.EventController");
580 this.addLine(this.ipad + this.this_el + "add_controller( %s.el );".printf(childname) );
585 switch (this.node.fqn()) {
591 var x = child.has("x") ? child.get_prop("x").val : "0";
592 var y = child.has("y") ? child.get_prop("y").val : "0";
593 this.addLine(@"$(ipad)$(this_el)put( $(childname)$(el_name), $(x), $(y) );");
599 var named = child.has("stack_name") ? child.get_prop("stack_name").val.escape() : "";
600 var title = child.has("stack_title") ? child.get_prop("stack_title").val.escape() : "";
601 if (title.length > 0) {
602 this.addLine(@"$(ipad)$(this_el)add_titled( $(childname)$(el_name), \"$(named)\", \"$(title)\" );");
605 this.addLine(@"$(ipad)$(this_el)add_named( $(childname)$(el_name), \"$(named)\");");
608 case "Gtk.Notebook": // use label
609 if (child.fqn() == "Gtk.NotebookPage") {
612 var label = child.has("notebook_label") ? child.get_prop("notebook_label").val.escape() : "";
613 this.addLine(@"$(ipad)$(this_el)append_page( $(childname)$(el_name), new Gtk.Label(\"$(label)\");");
618 case "Gtk.TreeView": // adding TreeViewColumns
619 this.addLine(this.ipad + "this.el.append_column( " + childname + ".el );");
622 case "Gtk.TreeViewColumn": //adding Renderers - I think these are all proprerties of the renderer used...
623 if (child.has("markup_column") && int.parse(child.get_prop("markup_column").val) > -1) {
624 var val = child.get_prop("markup_column").val;
625 this.addLine(@"$(ipad)$(this_el)add_attribute( $(childname)$(el_name), \"markup\", $(val) );");
628 if (child.has("text_column") && int.parse(child.get_prop("text_column").val) > -1) {
629 var val = child.get_prop("text_column").val;
630 this.addLine(@"$(ipad)$(this_el)add_attribute( $(childname)$(el_name), \"text\", $(val) );");
632 if (child.has("pixbuf_column") && int.parse(child.get_prop("pixbuf_column").val) > -1) {
633 var val = child.get_prop("pixbuf_column").val;
634 this.addLine(@"$(ipad)$(this_el).add_attribute( $(childname)$(el_name), \"pixbuf\", $(val) );");
636 if (child.has("pixbuf_column") && int.parse(child.get_prop("active_column").val) > -1) {
637 var val = child.get_prop("active_column").val;
638 this.addLine(@"$(ipad)$(this_el).add_attribute( $(childname)$(el_name), \"active\", $(val) );");
640 if (child.has("background_column") && int.parse(child.get_prop("background_column").val) > -1) {
641 var val = child.get_prop("background_column").val;
642 this.addLine(@"$(ipad)$(this_el).add_attribute( $(childname)$(el_name), \"background-rgba\", $(val) );");
644 this.addLine(this.ipad + "this.el.add( " + childname + ".el );");
649 if (propname == "buttons[]") {
650 var resp_id = int.parse(childname.replace("child_", ""));
651 if (child.has("* response_id")) {
652 resp_id = int.parse(child.get_prop("* response_id").val);
654 this.addLine(@"$(ipad)$(this_el).add_action_widget( $(childname)$(el_name), $(resp_id) );");
659 this.addLine(@"$(ipad)$$(this_el)get_content_area().add( $(childname)$(el_name) );");
667 // known working with GTK4 !
668 case "Gtk.HeaderBar": // it could be end... - not sure how to hanle that other than overriding this.addLine(this.ipad + "this.el.add_action_widget( %s.el, %d);".printf(childname,resp_id) ); the pack method?
669 this.addLine(@"$(ipad)$(this_el)pack_start( $(childname)$(el_name) );");
673 this.addLine(@"$(ipad)$(this_el)append_item( $(childname)$(el_name) );");
678 switch(this.pane_number) {
680 this.addLine(@"$(ipad)$(this_el)pack_start( $(childname)$(el_name) );");
683 this.addLine(@"$(ipad)$(this_el)pack_end( $(childname)$(el_name) );");
691 case "Gtk.ColumnView":
692 this.addLine(@"$(ipad)$(this_el)append_column( $(childname)$(el_name) );");
696 var x = "%d".printf(colpos % cols);
697 var y = "%d".printf(( colpos - (colpos % cols) ) / cols);
698 var w = child.has("colspan") ? child.get_prop("colspan").val : "1";
700 this.addLine(@"$(ipad)$(this_el)attach( $(childname)$(el_name), $x, $y, $w, $h );");
704 this.addLine(@"$(ipad)$(this_el)append( $(childname)$(el_name) );");
705 // gtk4 uses append!!!! - gtk3 - uses add..
714 // fixme GtkDialog?!? buttons[]
716 // fixme ... add case "Gtk.RadioButton": // group_id ??
720 protected void addInit()
724 if (!this.node.has("* init")) {
728 this.addLine(ipad + "// init method");
730 this.node.setLine(this.cur_line, "p", "init");
732 var init = this.node.get_prop("* init");
733 init.start_line = this.cur_line;
734 this.addMultiLine(ipad + this.padMultiline(ipad, init.val) );
735 init.end_line = this.cur_line;
737 protected void addListeners()
739 if (this.node.listeners.size < 1) {
744 this.addLine(ipad + "//listeners");
748 var iter = this.node.listeners.map_iterator();
749 while (iter.next()) {
750 var k = iter.get_key();
751 var prop = iter.get_value();
754 prop.start_line = this.cur_line;
755 this.node.setLine(this.cur_line, "l", k);
756 this.addMultiLine(this.ipad + this.this_el + k + ".connect( " +
757 this.padMultiline(this.ipad,v) +");");
758 prop.end_line = this.cur_line;
761 protected void addEndCtor()
765 this.addLine(this.pad + "}");
770 * Standardize this crap...
772 * standard properties (use to set)
773 * If they are long values show the dialog..
776 * bool is_xxx :: can show a pulldown.. (true/false)
778 * $ string html = string with value interpolated eg. baseURL + ".."
779 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
780 * $ untypedvalue = javascript untyped value...
781 * _ string html ... = translatable..
784 * object properties (not part of the GOjbect being wrapped?
785 * # Gee.ArrayList<Xcls_fileitem> fileitems
790 * methods -- always text editor..
798 * * init -- big string?
800 * event handlers (listeners)
805 * +XXXX -- indicates it's a instance property / not glob...
806 * *XXXX -- skip writing glob property (used as classes that can be created...)
811 protected void addUserMethods()
814 this.addLine(this.pad + "// user defined functions");
816 // user defined functions...
817 var iter = this.node.props.map_iterator();
819 var prop = iter.get_value();
820 if (this.shouldIgnore(prop.name)) {
823 // HOW TO DETERIME if its a method?
824 if (prop.ptype != NodePropType.METHOD) {
825 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
829 // function in the format of {type} (args) { .... }
833 prop.start_line = this.cur_line;
834 this.node.setLine(this.cur_line, "p", prop.name);
835 this.addMultiLine(this.pad + "public " + prop.rtype + " " + prop.name + " " + this.padMultiline(this.pad, prop.val));;
836 prop.end_line = this.cur_line;
841 protected void iterChildren()
843 this.node.line_end = this.cur_line;
844 this.node.sortLines();
847 if (this.depth > 0) {
848 this.addLine(this.inpad + "}");
851 var iter = this.node.readItems().list_iterator();
853 while (iter.next()) {
854 this.addMultiLine(this.mungeChild(iter.get()));
857 if (this.depth < 1) {
858 this.addLine(this.inpad + "}");
865 protected void ignoreWrapped(string i) {
866 this.ignoreWrappedList.add(i);
870 protected bool shouldIgnoreWrapped(string i)
872 return ignoreWrappedList.contains(i);