Fix #7996 - handle namespaces in glade, adw overlay for window selection and open
[roobuilder] / src / JsRender / NodeToGlade.vala
1 /*
2  This kind of works - however there are issues with embedding gladeui that do not seem fixable.
3  - rendering is borked for windows - they detach for some reason.
4  - selecting stuff and drag etc. would probably be complicated...
5  
6  
7 */
8 public class JsRender.NodeToGlade : Object {
9
10         Node node;
11         Project.Gtk project;
12         Xml.Node* parent;
13         
14         public NodeToGlade( Project.Gtk project, Node node, Xml.Node* parent) 
15         {
16                 
17                 this.parent = parent;
18                 this.project = project;
19                 this.node = node;
20                 
21         }
22         
23         public static string mungeFile(JsRender file) 
24         {
25                 if (file.tree == null) {
26                         return "";
27                 }
28
29                 var n = new NodeToGlade(  (Project.Gtk) file.project, file.tree,  null);
30         
31                 ///n.toValaName(file.tree);
32                 
33                 
34                 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls); 
35                 //n.cls = file.tree.xvala_cls;
36                 //n.xcls = file.tree.xvala_xcls;
37                 return n.munge();
38                 
39
40         }
41         
42         public string munge ( )
43         {
44
45
46                 var doc = this.mungeNode ();
47                 string ret;
48                 int len;
49         doc->dump_memory_format (out ret, out len, true);
50
51                 return ret;
52         
53           
54                      
55         }
56         public Xml.Doc* mungeChild( Node cnode , Xml.Node* cdom)
57         {
58                 var x = new  NodeToGlade(this.project, cnode,  cdom);
59                 return x.mungeNode();
60         }
61         public static Xml.Ns* ns = null;
62         
63         
64         public  Xml.Node* create_element(string n)
65         {
66                 if (NodeToGlade.ns == null) {
67                         Xml.Ns* ns = new Xml.Ns (null, "", "");
68                 ns->type = Xml.ElementType.ELEMENT_NODE;
69                 }
70                 Xml.Node* nn =  new Xml.Node (ns, n);
71        return nn;
72         
73         }
74         
75         public Xml.Doc* mungeNode()
76         {
77                 Xml.Doc* doc;
78                 var is_top = false;
79                 if (this.parent == null) {
80                         is_top = true;
81                         doc = new Xml.Doc("1.0");
82
83                         var inf = this.create_element("interface");
84                         doc->set_root_element(inf);
85                         var req = this.create_element("requires");
86                         req->set_prop("lib", "gtk+");
87                         req->set_prop("version", "4.1");
88                         inf->add_child(req);
89                         this.parent = inf;
90                 } else {
91                         doc = this.parent->doc;
92                 }
93                 var cls = this.node.fqn().replace(".", "");
94                 
95                 var gdata = Palete.Gir.factoryFqn(this.project, this.node.fqn());
96                 if (gdata == null || !gdata.inherits.contains("Gtk.Buildable")) {
97                         return doc;
98                 }
99                 if (gdata.inherits.contains("Gtk.Native")&& !is_top) {
100                         return doc;
101                 }
102                 // what namespaces are supported
103                 switch(this.node.NS) {
104                         case "Gtk":
105                         case "Webkit": //??
106                                 break;
107                         default:
108                                 return doc;
109                 }
110                 
111                 // other problems!!!
112                 
113                 if (gdata.fqn() == ("Gtk.ListStore")) {
114                         return doc;
115                 }
116                 
117                 // should really use GXml... 
118                 var obj = this.create_element("object");
119                 //var id = this.node.uid();
120                 var skip_props = false;
121                 if (gdata.inherits.contains("Gtk.Native")) {
122                          
123                         obj->set_prop("class", "GtkFrame");
124                         skip_props = true;
125                 } else {
126                 
127                         obj->set_prop("class", cls);
128                 }
129                 obj->set_prop("id", "w" + this.node.oid.to_string());
130                 this.parent->add_child(obj);
131                 // properties..
132                 var props = Palete.Gir.factoryFqn(this.project, this.node.fqn()).props;
133  
134               
135                 var pviter = props.map_iterator();
136                 while (!skip_props && pviter.next()) {
137                         
138                         //GLib.debug ("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
139                         
140                 // skip items we have already handled..
141                 if  (!this.node.has(pviter.get_key())) {
142                                 continue;
143                         }
144                         var k = pviter.get_key();       
145                         var prop = props.get(k);
146                         var val = this.node.get(pviter.get_key()).strip();      
147                         // for Enums - we change it to lowercase, and remove all the previous bits.. hopefully might work.
148                         if (prop.type.contains(".") && val.contains(".")) {
149                                 var typ =  Palete.Gir.factoryFqn(this.project, prop.type);
150                                 if (typ.nodetype == "Enum") {
151                                          var bits = val.split(".");
152                                          val = bits[bits.length-1].down();
153                                 }
154                         }
155                                 
156                         
157
158
159                         var domprop = this.create_element("property");
160                         domprop->set_prop("name", k);
161                          
162                         
163                         
164                         domprop->add_child(new Xml.Node.text(val));
165                         obj->add_child(domprop); 
166         }
167                 // packing???
168 /*
169                 var pack = "";
170                 
171                 if (with_packing   ) {
172  
173                         pack = this.packString();
174                         
175
176                 
177                 }       */
178                 // children..
179
180                 var items = this.node.readItems();
181                 for (var i = 0; i < items.size; i++ ) {
182                         var cn = items.get(i);
183                         var child  = this.create_element("child");
184                         if (cls == "GtkWindow" && cn.fqn() == "Gtk.HeaderBar") {
185                                 child->set_prop("type", "titlebar");
186                         }
187                         
188                         
189                         this.mungeChild(cn, child);
190                         if (child->child_element_count()  < 1) {
191                                 continue;
192                         }
193                         obj->add_child(child);
194                          
195                 }
196                 return doc;
197
198                  
199
200         }
201          
202          
203
204
205                 
206 }