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("callinged addMhyVars");
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 // is it a class property...
185 if (cls != null && cls.props.has_key(prop.name) && prop.ptype != NodePropType.USER) {
189 this.myvars.add(prop.name);
190 prop.start_line = this.cur_line;
192 this.node.setLine(this.cur_line, "p", prop.name);
194 this.addLine(this.pad + "public " + prop.rtype + " " + prop.name + ";"); // definer - does not include value.
197 prop.end_line = this.cur_line;
198 this.ignore(prop.name);
204 // if id of child is '+' then it's a property of this..
205 protected void addPlusProperties()
207 if (this.node.readItems().size < 1) {
210 var iter = this.node.readItems().list_iterator();
211 while (iter.next()) {
214 if (ci.xvala_id[0] != '+') {
215 continue; // skip generation of children?
219 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
225 * add the constructor definition..
227 protected abstract void addValaCtor();
229 * make sure _this is defined..
231 protected void addUnderThis()
235 this.addLine( this.ipad + "_this = this;");
238 // for non top level = _this point to owner, and _this.ID is set
240 this.addLine( this.ipad + "_this = _owner;");
242 if (this.node.props.has_key("id")
244 this.node.xvala_id != ""
246 this.node.xvala_id[0] != '*'
248 this.node.xvala_id[0] != '+'
250 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
258 protected void addInitMyVars()
260 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
261 //print(JSON.stringify(meths,null,4));Seed.quit();
265 // initialize.. my vars..
267 this.addLine( this.ipad + "// my vars (dec)");
269 var iter = this.myvars.list_iterator();
275 var prop = this.node.props.get(k);
277 var v = prop.val.strip();
282 // at this point start using
284 if (v == "FALSE" || v == "TRUE") {
287 //FIXME -- check for raw string.. "string XXXX"
289 // if it's a string...
291 prop.start_line = this.cur_line;
292 this.addLine(this.ipad + "this." + prop.name + " = " + v +";");
293 prop.end_line = this.cur_line;
301 protected void addWrappedProperties()
303 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
305 GLib.debug("Skipping wrapped properties - could not find class %s" , this.node.fqn());
308 // what are the properties of this class???
310 this.addLine(this.ipad + "// set gobject values");
312 foreach(var p in cls.props.keys) {
313 var val = cls.props.get(p);
314 //print("Check Write %s\n", p);
315 if (!this.node.has(p)) {
318 if (this.shouldIgnoreWrapped(p)) {
325 var prop = this.node.get_prop(p);
328 // user defined properties.
329 if (prop.ptype == NodePropType.USER) {
335 var is_raw = prop.ptype == NodePropType.RAW;
337 // what's the type.. - if it's a string.. then we quote it..
338 if (val.type == "string" && !is_raw) {
339 v = "\"" + v.escape("") + "\"";
341 if (v == "TRUE" || v == "FALSE") {
344 if (val.type == "float" && v[v.length-1] != 'f') {
348 prop.start_line = this.cur_line;
349 this.addLine("%s%s%s = %s;".printf(ipad,this.this_el,p,v)); // // %s, iter.get_value().type);
350 prop.end_line = this.cur_line;
357 * pack the children into the parent.
359 * if the child's id starts with '*' then it is not packed...
360 * - this allows you to define children and add them manually..
363 protected void addChildren()
366 if (this.node.readItems().size < 1) {
369 this.pane_number = 0;
370 var cols = this.node.has("* columns") ? int.max(1, int.parse(this.node.get_prop("* columns").val)) : 1;
375 foreach(var child in this.node.readItems()) {
380 if (child.xvala_id[0] == '*') {
381 continue; // skip generation of children?
384 // probably added in ctor..
385 if (child.has("* prop") && this.shouldIgnoreWrapped(child.get_prop("* prop").val)) {
388 // create the element..
390 // this is only needed if it does not have an ID???
391 var childname = this.addPropSet(child, child.has("id") ? child.get_prop("id").val : "") ;
393 if (child.has("* prop")) {
396 // fixme special packing!??!?!
397 if (child.get_prop("* prop").val.contains("[]")) {
398 // currently these 'child props
399 // used for label[] on Notebook
400 // used for button[] on Dialog?
403 this.packChild(child, childname, 0, 0, child.get_prop("* prop").val); /// fixme - this is a bit speciall...
409 this.ignoreWrapped(child.get_prop("* prop").val);
410 var el_name = this.this_el == "this.el." ? ".el" : "";
411 this.addLine(ipad + this.this_el + child.get_prop("* prop").val + " = " + childname + el_name +";");
415 if (!child.has("id") && this.this_el == "this.el.") {
416 this.addLine(this.ipad + childname +".ref();");
418 this.packChild(child, childname, cols, colpos);
420 if (child.has("colspan")) {
421 colpos += int.parse(child.get_prop("colspan").val);
427 // this.{id - without the '+'} = the element...
433 protected string addPropSet(Node child, string child_name)
438 if (child.has("* args")) {
440 var ar = child.get_prop("* args").val.split(",");
441 for (var ari = 0 ; ari < ar.length; ari++ ) {
442 var arg = ar[ari].split(" ");
443 xargs += "," + arg[arg.length -1];
447 var childname = "child_" + "%d".printf(this.child_count++);
449 if (child_name == "") {
450 prefix = "var " + childname + " = ";
452 var cls = child.xvala_xcls;
454 if (this.this_el == "this.") {
455 var clsdata = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
456 //if (clsdata.is_sealed) {
457 cls = this.node.fqn(); // need ctor data...
458 this.addLine(this.ipad + @"$(prefix)new $cls( _this $xargs);" );
459 return child_name == "" ? childname : ("_this." + child_name);
464 this.addLine(this.ipad + @"$(prefix)new $cls( _this $xargs);" );
466 // add a ref... (if 'id' is not set... to a '+' ?? what does that mean? - fake ids?
467 // remove '+' support as I cant remember what it does!!!
468 //if (child.xvala_id.length < 1 ) {
469 // this.addLine(this.ipad + childname +".ref();"); // we need to reference increase unnamed children...
471 //if (child.xvala_id[0] == '+') {
472 // this.addLine(this.ipad + "this." + child.xvala_id.substring(1) + " = " + childname+ ";");
477 return child_name == "" ? childname : ("_this." + child_name);
483 protected void packChild(Node child, string childname, int cols, int colpos, string propname= "")
486 GLib.debug("packChild %s=>%s", this.node.fqn(), child.fqn());
487 // forcing no packing? - true or false? -should we just accept false?
488 if (child.has("* pack") && child.get("* pack").down() == "false") {
489 return; // force no packing
491 if (child.has("* pack") && child.get("* pack").down() == "true") {
492 return; // force no packing
494 var el_name = this.this_el == "this.el." ? ".el" : "";
495 var this_el = this.this_el;
496 // BC really - don't want to support this anymore.
497 if (child.has("* pack")) {
499 string[] packing = { "add" };
500 if (child.has("* pack")) {
501 packing = child.get("* pack").split(",");
504 var pack = packing[0];
505 this.addLine(this.ipad + this.this_el + pack.strip() + " ( " + childname + el_name + " " +
506 (packing.length > 1 ?
507 (", " + string.joinv(",", packing).substring(pack.length+1))
511 var childcls = this.file.project.palete.getClass(child.fqn()); // very trusting..
512 if (childcls == null) {
516 var is_event = childcls.inherits.contains("Gtk.EventController") || childcls.implements.contains("Gtk.EventController");
518 this.addLine(this.ipad + this.this_el + "add_controller( %s.el );".printf(childname) );
523 switch (this.node.fqn()) {
529 var x = child.has("x") ? child.get_prop("x").val : "0";
530 var y = child.has("y") ? child.get_prop("y").val : "0";
531 this.addLine(@"$(ipad)$(this_el)put( $(childname)$(el_name), $(x), $(y) );");
537 var named = child.has("stack_name") ? child.get_prop("stack_name").val.escape() : "";
538 var title = child.has("stack_title") ? child.get_prop("stack_title").val.escape() : "";
539 if (title.length > 0) {
540 this.addLine(@"$(ipad)$(this_el)add_titled( $(childname)$(el_name), \"$(named)\", \"$(title)\" );");
543 this.addLine(@"$(ipad)$(this_el)add_named( $(childname)$(el_name), \"$(named)\");");
546 case "Gtk.Notebook": // use label
547 var label = child.has("notebook_label") ? child.get_prop("notebook_label").val.escape() : "";
548 this.addLine(@"$(ipad)$(this_el)append_page( $(childname)$(el_name), new Gtk.Label(\"$(label)\");");
553 case "Gtk.TreeView": // adding TreeViewColumns
554 this.addLine(this.ipad + "this.el.append_column( " + childname + ".el );");
557 case "Gtk.TreeViewColumn": //adding Renderers - I think these are all proprerties of the renderer used...
558 if (child.has("markup_column") && int.parse(child.get_prop("markup_column").val) > -1) {
559 var val = child.get_prop("markup_column").val;
560 this.addLine(@"$(ipad)$(this_el)add_attribute( $(childname)$(el_name), \"markup\", $(val) );");
563 if (child.has("text_column") && int.parse(child.get_prop("text_column").val) > -1) {
564 var val = child.get_prop("text_column").val;
565 this.addLine(@"$(ipad)$(this_el)add_attribute( $(childname)$(el_name), \"text\", $(val) );");
567 if (child.has("pixbuf_column") && int.parse(child.get_prop("pixbuf_column").val) > -1) {
568 var val = child.get_prop("pixbuf_column").val;
569 this.addLine(@"$(ipad)$(this_el).add_attribute( $(childname)$(el_name), \"pixbuf\", $(val) );");
571 if (child.has("pixbuf_column") && int.parse(child.get_prop("active_column").val) > -1) {
572 var val = child.get_prop("active_column").val;
573 this.addLine(@"$(ipad)$(this_el).add_attribute( $(childname)$(el_name), \"active\", $(val) );");
575 if (child.has("background_column") && int.parse(child.get_prop("background_column").val) > -1) {
576 var val = child.get_prop("background_column").val;
577 this.addLine(@"$(ipad)$(this_el).add_attribute( $(childname)$(el_name), \"background-rgba\", $(val) );");
579 this.addLine(this.ipad + "this.el.add( " + childname + ".el );");
584 if (propname == "buttons[]") {
585 var resp_id = int.parse(childname.replace("child_", ""));
586 if (child.has("* response_id")) {
587 resp_id = int.parse(child.get_prop("* response_id").val);
589 this.addLine(@"$(ipad)$(this_el).add_action_widget( $(childname)$(el_name), $(resp_id) );");
594 this.addLine(@"$(ipad)$$(this_el)get_content_area().add( $(childname)$(el_name) );");
602 // known working with GTK4 !
603 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?
604 this.addLine(@"$(ipad)$(this_el)pack_start( $(childname)$(el_name) );");
608 this.addLine(@"$(ipad)$(this_el)append_item( $(childname)$(el_name) );");
613 switch(this.pane_number) {
615 this.addLine(@"$(ipad)$(this_el)pack_start( $(childname)$(el_name) );");
618 this.addLine(@"$(ipad)$(this_el)pack_end( $(childname)$(el_name) );");
626 case "Gtk.ColumnView":
627 this.addLine(@"$(ipad)$(this_el)append_column( $(childname)$(el_name) );");
631 var x = "%d".printf(colpos % cols);
632 var y = "%d".printf(( colpos - (colpos % cols) ) / cols);
633 var w = child.has("colspan") ? child.get_prop("colspan").val : "1";
635 this.addLine(@"$(ipad)$(this_el)attach( $(childname)$(el_name), $x, $y, $w, $h );");
639 this.addLine(@"$(ipad)$(this_el)append( $(childname)$(el_name) );");
640 // gtk4 uses append!!!! - gtk3 - uses add..
649 // fixme GtkDialog?!? buttons[]
651 // fixme ... add case "Gtk.RadioButton": // group_id ??
655 protected void addInit()
659 if (!this.node.has("* init")) {
663 this.addLine(ipad + "// init method");
665 this.node.setLine(this.cur_line, "p", "init");
667 var init = this.node.get_prop("* init");
668 init.start_line = this.cur_line;
669 this.addMultiLine(ipad + this.padMultiline(ipad, init.val) );
670 init.end_line = this.cur_line;
672 protected void addListeners()
674 if (this.node.listeners.size < 1) {
679 this.addLine(ipad + "//listeners");
683 var iter = this.node.listeners.map_iterator();
684 while (iter.next()) {
685 var k = iter.get_key();
686 var prop = iter.get_value();
689 prop.start_line = this.cur_line;
690 this.node.setLine(this.cur_line, "l", k);
691 this.addMultiLine(this.ipad + this.this_el + k + ".connect( " +
692 this.padMultiline(this.ipad,v) +");");
693 prop.end_line = this.cur_line;
696 protected void addEndCtor()
700 this.addLine(this.pad + "}");
705 * Standardize this crap...
707 * standard properties (use to set)
708 * If they are long values show the dialog..
711 * bool is_xxx :: can show a pulldown.. (true/false)
713 * $ string html = string with value interpolated eg. baseURL + ".."
714 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
715 * $ untypedvalue = javascript untyped value...
716 * _ string html ... = translatable..
719 * object properties (not part of the GOjbect being wrapped?
720 * # Gee.ArrayList<Xcls_fileitem> fileitems
725 * methods -- always text editor..
733 * * init -- big string?
735 * event handlers (listeners)
740 * +XXXX -- indicates it's a instance property / not glob...
741 * *XXXX -- skip writing glob property (used as classes that can be created...)
746 protected void addUserMethods()
749 this.addLine(this.pad + "// user defined functions");
751 // user defined functions...
752 var iter = this.node.props.map_iterator();
754 var prop = iter.get_value();
755 if (this.shouldIgnore(prop.name)) {
758 // HOW TO DETERIME if its a method?
759 if (prop.ptype != NodePropType.METHOD) {
760 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
764 // function in the format of {type} (args) { .... }
768 prop.start_line = this.cur_line;
769 this.node.setLine(this.cur_line, "p", prop.name);
770 this.addMultiLine(this.pad + "public " + prop.rtype + " " + prop.name + " " + this.padMultiline(this.pad, prop.val));;
771 prop.end_line = this.cur_line;
776 protected void iterChildren()
778 this.node.line_end = this.cur_line;
779 this.node.sortLines();
782 if (this.depth > 0) {
783 this.addLine(this.inpad + "}");
786 var iter = this.node.readItems().list_iterator();
788 while (iter.next()) {
789 this.addMultiLine(this.mungeChild(iter.get()));
792 if (this.depth < 1) {
793 this.addLine(this.inpad + "}");
800 protected void ignoreWrapped(string i) {
801 this.ignoreWrappedList.add(i);
805 protected bool shouldIgnoreWrapped(string i)
807 return ignoreWrappedList.contains(i);