Fix #8076 - gtknotebookpage fake wrapper
[roobuilder] / src / JsRender / NodeToGlade.vala
index d70fdae..e3b833d 100644 (file)
@@ -10,6 +10,7 @@ public class JsRender.NodeToGlade : Object {
        Node node;
        Project.Gtk project;
        Xml.Node* parent;
+       Xml.Doc* doc;
        
        public NodeToGlade( Project.Gtk project, Node node, Xml.Node* parent) 
        {
@@ -31,7 +32,7 @@ public class JsRender.NodeToGlade : Object {
                ///n.toValaName(file.tree);
                
                
-               GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls); 
+               //GLib.debug("top cls %s / xlcs %s ",file.tree.xvala_cls,file.tree.xvala_cls); 
                //n.cls = file.tree.xvala_cls;
                //n.xcls = file.tree.xvala_xcls;
                return n.munge();
@@ -43,17 +44,17 @@ public class JsRender.NodeToGlade : Object {
        {
 
 
-               var doc = this.mungeNode ();
+               this.mungeNode ();
                string ret;
                int len;
-        doc->dump_memory_format (out ret, out len, true);
+        this.doc->dump_memory_format (out ret, out len, true);
 
                return ret;
        
           
                     
        }
-       public Xml.Doc* mungeChild( Node cnode , Xml.Node* cdom)
+       public Xml.Node* mungeChild( Node cnode , Xml.Node* cdom)
        {
                var x = new  NodeToGlade(this.project, cnode,  cdom);
                return x.mungeNode();
@@ -72,61 +73,115 @@ public class JsRender.NodeToGlade : Object {
        
        }
        
-       public Xml.Doc* mungeNode()
+       public Xml.Node* mungeNode()
        {
-               Xml.Doc* doc;
+                
+               var is_top = false;
                if (this.parent == null) {
-                       doc = new Xml.Doc("1.0");
+                       is_top = true;
+                       this.doc = new Xml.Doc("1.0");
 
                        var inf = this.create_element("interface");
-                       doc->set_root_element(inf);
+                       this.doc->set_root_element(inf);
                        var req = this.create_element("requires");
                        req->set_prop("lib", "gtk+");
-                       req->set_prop("version", "3.12");
+                       req->set_prop("version", "4.1");
                        inf->add_child(req);
                        this.parent = inf;
-               } else {
-                       doc = this.parent->doc;
-               }
+               } 
+               
                var cls = this.node.fqn().replace(".", "");
                
-               Palete.Gir.factoryFqn(this.project, this.node.fqn());
+               var gdata = Palete.Gir.factoryFqn(this.project, this.node.fqn());
+               if (gdata == null || !gdata.inherits.contains("Gtk.Buildable")) {
+                       switch(cls) {
+                       //exception to the rule.. (must be buildable to work with glade?
+                               case "GtkColumnViewColumn": 
+                               case "GtkNotebookPage": 
+                               break;
+                               default:
+                                       return null;
+                       }
+               }
+               if (gdata.inherits.contains("Gtk.Native")&& !is_top) {
+                       return null;
+               }
+               // what namespaces are supported
+               switch(this.node.NS) {
+                       case "Gtk":
+                       case "Webkit": //??
+                       case "Adw": // works if you call adw.init() in main!
+                               break;
+                       default:
+                               return null;
+               }
                
-        
+               // other problems!!!
                
+               if (gdata.fqn() == ("Gtk.ListStore")) {
+                       return null;
+               }
+                
+                // <object class="GtkNotebookPage">
+               //         <property name="tab-expand">1</property>
+         //       <property name="child">
+                //      <property name="label">
                // should really use GXml... 
                var obj = this.create_element("object");
-               var id = this.node.uid();
-               obj->set_prop("class", cls);
-               obj->set_prop("id", id);
+               //var id = this.node.uid();
+               var skip_props = false;
+               if (gdata.inherits.contains("Gtk.Native")) {
+                        
+                       obj->set_prop("class", "GtkFrame");
+                       skip_props = true;
+               } else {
+                       switch(cls) {
+                               case  "GtkHeaderBar":
+                                       obj->set_prop("class", "GtkBox");
+                                       this.addProperty(obj, "orientation", "horizontal");
+                                       skip_props = true;
+                                       break;
+                       
+                               default:
+                                       obj->set_prop("class", cls);
+                                       break;
+                       }       
+               }
+               
+               obj->set_prop("id", "w" + this.node.oid.to_string());
                this.parent->add_child(obj);
                // properties..
                var props = Palete.Gir.factoryFqn(this.project, this.node.fqn()).props;
  
               
                var pviter = props.map_iterator();
-               while (pviter.next()) {
+               while (!skip_props && pviter.next()) {
                        
-                       GLib.debug ("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
+                       //GLib.debug ("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
                        
                // skip items we have already handled..
                if  (!this.node.has(pviter.get_key())) {
                                continue;
                        }
-                       var k = pviter.get_key();
-                       var val = this.node.get(pviter.get_key()).strip();
-                       var prop = this.create_element("property");
-                       prop->set_prop("name", k);
-                       switch (k) { 
-                               case "orientation":
-                                       var bits = val.split(".");
-                                       val = bits.length > 2 ? bits[2].down() : "vertical"; // ??
-                                       break;
+                       var k = pviter.get_key();       
+                       var prop = props.get(k);
+                       var val = this.node.get(pviter.get_key()).strip();      
+                       // for Enums - we change it to lowercase, and remove all the previous bits.. hopefully might work.
+                       if (prop.type.contains(".") && val.contains(".")) {
+                               var typ =  Palete.Gir.factoryFqn(this.project, prop.type);
+                               if (typ.nodetype == "Enum") {
+                                        var bits = val.split(".");
+                                        val = bits[bits.length-1].down();
+                               }
                        }
                        
-                       
-                       prop->add_child(new Xml.Node.text(val));
-                       obj->add_child(prop); 
+                       //  value for model seems to cause problems...(it's ok as a property?)
+                       if (k == "model") {
+                               continue;
+                       }
+                       this.addProperty(obj, k, val);
+
+                        
         }
                // packing???
 /*
@@ -140,148 +195,79 @@ public class JsRender.NodeToGlade : Object {
                
                }       */
                // children..
-
-               
-               for (var i = 0; i < this.node.items.size; i++ ) {
-                       var cn = this.node.items.get(i);
-                       var child  = this.create_element("child");
-                       if (cls == "GtkWindow" && cn.fqn() == "Gtk.HeaderBar") {
-                               child->set_prop("type", "titlebar");
+               var left = 0, top = 0, cols = 1;
+               if (cls == "GtkGrid") { 
+               var colval = this.node.get_prop("* columns");
+                       GLib.debug("Columns %s", colval == null ? "no columns" : colval.val);
+                       if (colval != null) {
+                               cols = int.parse(colval.val);
+                       }
+               }
+               var items = this.node.readItems();
+               for (var i = 0; i < items.size; i++ ) {
+                       var cn = items.get(i);
+                       
+                       var childname = "child";
+                       var pname = "";
+                       if (cn.has("* prop")) { // && cn.get_prop("* prop").val == "child") {
+                               childname = "property";
+                               pname = cn.get_prop("* prop").val;
+                       }
+                       
+                       var child  = this.create_element(childname);
+                       if (pname != "") {
+                               child->set_prop("name", pname);
+                       }
+                       
+                       if ((cls == "GtkWindow" || cls == "GtkApplicationWindow") && cn.fqn() == "Gtk.HeaderBar") {
+                               child->set_prop("type", "label");
+                       }
+                       
+                       
+                       var sub_obj = this.mungeChild(cn, child);
+                       if (sub_obj == null) {
+                               continue;
+                       }
+                       if (cls == "GtkGrid") {
+                               this.addGridAttach(sub_obj, left, top);
+                               left++;
+                               if (left == cols) {
+                                       left = 0;
+                                       top++;
+                               }
+                       
+                       
                        }
                        
                        
-                       this.mungeChild(cn, child);
+                       
+                       
                        if (child->child_element_count()  < 1) {
                                continue;
                        }
                        obj->add_child(child);
                         
                }
-               return doc;
+               return obj;
 
                 
 
        }
-        
-        /*
-       public string packString()
+       void addProperty(Xml.Node* obj, string k, string val) 
        {
+               var domprop = this.create_element("property");
+               domprop->set_prop("name", k);
+               domprop->add_child(new Xml.Node.text(val));
+               obj->add_child(domprop); 
+       }
+        void addGridAttach(Xml.Node* obj, int left, int top) 
+       {
+               var layout = this.create_element("layout");
+               this.addProperty(layout, "column", left.to_string());
+               this.addProperty(layout, "row", top.to_string());
+               obj->add_child(layout); 
                
-               
-               
-               
-               // pack is part of the parent element..
-               var p = node.parent;
-               string[]  pk= { "add" };
-               var pfqn = "Gtk.Box";
-               if (p != null) {
-                       pfqn  = p.fqn();
-                       if (this.node.props.get("* pack") == null) {
-                               return "";
-                       }
-                       pk = this.node.get("* pack").split(",");
-               } else {
-                       if (this.node.props.get("* pack") != null) {
-                               pk = this.node.get("* pack").split(",");
-                       }
-                       
-               }
-               
-               if (pfqn == null) {
-                       return "";
-               }
-               if (pfqn == "Gtk.ScrolledWindow") {
-                       return "";
-               }
-               var p_parts =pfqn.split(".");
-
-               var ns = p_parts[0];
-               var gir =  Palete.Gir.factory(this.project, ns);
-               var cls = gir.classes.get(p_parts[1]);
-               var mdef = cls.methods.get(pk[0]);
-               if (mdef == null) {
-                       GLib.debug ("could not find method : %s\n", pk[0]);
-                       return "";
-               }
-               /*
-               var generator = new Json.Generator ();
-               var n = new Json.Node(Json.NodeType.OBJECT);
-               n.set_object(mdef.toJSON());
-               generator.set_root(n);
-               generator.indent = 4;
-               generator.pretty = true;
-                   
-               GLib.debug print(generator.to_data(null));
-               */
-               /*
-               string[]  pbody  = {};
-               switch(pk[0]) {
-
-                       case "pack_start":
-                               pbody += @"$pad    <property name=\"pack_type\">start</property>\n";
-                               break;
-                       
-                       case "pack_end":
-                               pbody += @"$pad    <property name=\"pack_type\">start</property>\n";
-                               break;
-                               
-                       case "add":
-                               //pbody += @"$pad    <property name=\"pack_type\">start</property>\n";
-                                pbody += @"$pad    <property name=\"expand\">True</property>\n";
-                               pbody += @"$pad    <property name=\"fill\">True</property>\n";
-                               //pbody += @"$pad    <property name=\"position\">1</property>\n";
-                               var pack = @"$pad<packing>\n" +
-                                       string.joinv("", pbody) + 
-                                               @"$pad</packing>\n";
-                               return pack;
-                
-                       case "set_model":
-                               GLib.debug ("set_model not handled yet..");
-                               return "";
-                       
-                       default:
-                               GLib.debug  ("unknown pack type: %s", pk[0]);
-                               return "";
-                               
-               }
-                       
-
-                
-               for (var i = 2; i < mdef.paramset.params.size; i++) {
-                       var poff = i - 1;
-                       if (poff > (pk.length-1)) {
-                               break;
-                       }
-                       
-                       var key = mdef.paramset.params.get(i).name;
-                       var val = pk[poff];
-                       pbody += @"$pad    <property name=\"$key\">$val</property>\n";
-               
-               }
-            
-               if (pbody.length < 1) {
-                       /*var generator = new Json.Generator ();
-                       var n = new Json.Node(Json.NodeType.OBJECT);
-                       n.set_object(mdef.toJSON());
-                       generator.set_root(n);
-                       generator.indent = 4;
-                       generator.pretty = true;
-                           
-                       print(generator.to_data(null));
-                       */ 
-                       /*
-                       GLib.debug ("skip - packing - no arguments (" + pk[0] + ")\n");
-                       return "";
-               }
-               
-               var pack = @"$pad<packing>\n" +
-                               string.joinv("", pbody) + 
-                               @"$pad</packing>\n";
-               return pack;
-
        }
-       */