fix line numbering issues with vala generator - hopefully fixes completion in node...
[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                 
103                 // other problems!!!
104                 
105                 if (gdata.fqn() == ("Gtk.ListStore")) {
106                         return doc;
107                 }
108                 
109                 // should really use GXml... 
110                 var obj = this.create_element("object");
111                 //var id = this.node.uid();
112                 var skip_props = false;
113                 if (gdata.inherits.contains("Gtk.Native")) {
114                          
115                         obj->set_prop("class", "GtkFrame");
116                         skip_props = true;
117                 } else {
118                 
119                         obj->set_prop("class", cls);
120                 }
121                 obj->set_prop("id", "w" + this.node.oid.to_string());
122                 this.parent->add_child(obj);
123                 // properties..
124                 var props = Palete.Gir.factoryFqn(this.project, this.node.fqn()).props;
125  
126               
127                 var pviter = props.map_iterator();
128                 while (!skip_props && pviter.next()) {
129                         
130                         GLib.debug ("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
131                         
132                 // skip items we have already handled..
133                 if  (!this.node.has(pviter.get_key())) {
134                                 continue;
135                         }
136                         var k = pviter.get_key();       
137                         var prop = props.get(k);
138                         var val = this.node.get(pviter.get_key()).strip();      
139                         // for Enums - we change it to lowercase, and remove all the previous bits.. hopefully might work.
140                         if (prop.type.contains(".") && val.contains(".")) {
141                                 var typ =  Palete.Gir.factoryFqn(this.project, prop.type);
142                                 if (typ.nodetype == "Enum") {
143                                          var bits = val.split(".");
144                                          val = bits[bits.length-1].down();
145                                 }
146                         }
147                                 
148                         
149
150
151                         var domprop = this.create_element("property");
152                         domprop->set_prop("name", k);
153                          
154                         
155                         
156                         domprop->add_child(new Xml.Node.text(val));
157                         obj->add_child(domprop); 
158         }
159                 // packing???
160 /*
161                 var pack = "";
162                 
163                 if (with_packing   ) {
164  
165                         pack = this.packString();
166                         
167
168                 
169                 }       */
170                 // children..
171
172                 var items = this.node.readItems();
173                 for (var i = 0; i < items.size; i++ ) {
174                         var cn = items.get(i);
175                         var child  = this.create_element("child");
176                         if (cls == "GtkWindow" && cn.fqn() == "Gtk.HeaderBar") {
177                                 child->set_prop("type", "titlebar");
178                         }
179                         
180                         
181                         this.mungeChild(cn, child);
182                         if (child->child_element_count()  < 1) {
183                                 continue;
184                         }
185                         obj->add_child(child);
186                          
187                 }
188                 return doc;
189
190                  
191
192         }
193          
194          
195
196
197                 
198 }