2 this is the code to write the 'classic' node to vala output
6 * Code to convert node tree to Vala...
8 * usage : x = (new JsRender.NodeToVala(node)).munge();
12 * pack - can we come up with a replacement?
13 - parent.child == child_widget -- actually uses getters and effectively does 'add'?
17 * args -- vala constructor args (should really only be used at top level - we did use it for clutter originally(
18 * ctor -- different ctor argument
27 public class JsRender.NodeToValaWrapped : NodeToVala {
31 public NodeToValaWrapped( JsRender file, Node node, int depth, NodeToVala? parent)
33 base (file, node, depth, parent);
34 this.this_el = "this.el.";
38 * Main entry point to convert a file into a string..
40 public static string mungeFile(JsRender file)
42 if (file.tree == null) {
46 var n = new NodeToValaWrapped(file, file.tree, 0, null);
47 n.toValaName(file.tree);
49 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
55 public override string munge ( )
57 //return this.mungeToString(this.node);
58 if (this.node.as_source_version > 0 &&
59 this.node.as_source_version == this.node.updated_count &&
60 this.node.as_source_start_line == cur_line &&
61 this.node.as_source != ""
63 return this.node.as_source;
65 this.node.as_source_start_line = cur_line;
67 this.namespaceHeader();
71 this.addTopProperties();
73 this.addPlusProperties(); // (this is child properties whos 'id' starts with '+' ??? not sure..
76 this.addWrappedCtor(); // var this.el = new XXXXX()
79 this.addWrappedProperties();
81 //this.addAutoShow(); // not needed gtk4 autoshow menuitems
86 this.addUserMethods();
88 this.namespaceFooter();
91 this.node.as_source_version = this.node.updated_count;
92 this.node.as_source == this.ret;
97 public override string mungeChild( Node cnode)
99 var x = new NodeToValaWrapped(this.file, cnode, this.depth+1, this);
103 protected override void classHeader()
105 var top = this.top as NodeToVala;
111 // class xxx { WrappedGtk el; }
112 this.node.line_start = this.cur_line;
114 this.top.node.setNodeLine(this.cur_line, this.node);
116 this.addLine(this.inpad + "public class " + this.xcls + " : Object");
117 this.addLine(this.inpad + "{");
120 this.addLine(this.pad + "public " + this.cls + " el;");
122 this.addLine(this.pad + "private " + top.xcls + " _this;");
129 public void globalVars()
131 if (this.depth > 0) {
134 // Global Vars..??? when did this get removed..?
135 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
137 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
141 protected void addSingleton()
146 this.addLine(pad + "public static " + xcls + " singleton()");
147 this.addLine(this.pad + "{");
148 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
149 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
150 this.addLine(this.ipad + "}");
151 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
152 this.addLine(this.pad + "}");
155 * add the constructor definition..
157 protected override void addValaCtor()
167 this.addLine(this.pad + "// ctor");
169 if (this.node.has("* args")) {
170 // not sure what this is supposed to be ding..
172 cargs_str = this.node.get("* args");
173 //var ar = this.node.get("* args");.split(",");
174 //for (var ari =0; ari < ar.length; ari++) {
175 // cargs += (ar[ari].trim().split(" ").pop();
180 if (this.depth < 1) {
182 // top level - does not pass the top level element..
183 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
184 this.addLine(this.pad + "{");
186 if (cargs_str.length > 0) {
187 cargs_str = ", " + cargs_str;
189 var top = this.top as NodeToVala;
190 var tcls = top == null ? "???" : top.xcls;
191 // for sub classes = we passs the top level as _owner
192 if (this.node.fqn() == "Gtk.NotebookPage") {
193 cargs_str += ", " + this.node.parent.xvala_xcls + " notebook";
196 this.addLine(this.pad + "public " + this.xcls + "(" + tcls + " _owner " + cargs_str + ")");
197 this.addLine(this.pad + "{");
203 * Initialize this.el to point to the wrapped element.
208 void addWrappedCtor()
211 // this may need to look up properties to fill in the arguments..
212 // introspection does not workk..... - as things like gtkmessagedialog
214 if (cls == 'Gtk.Table') {
216 var methods = this.palete.getPropertiesFor(cls, 'methods');
218 print(JSON.stringify(this.palete.proplist[cls], null,4));
223 // ctor can still override.
224 if (this.node.has("* ctor")) {
225 this.node.setLine(this.cur_line, "p", "* ctor");
226 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
230 this.node.setLine(this.cur_line, "p", "* xtype");;
233 // Notebookpage is a fake element
234 // used to hold label and child...
236 // is the wrapped element a struct?
237 var ncls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
238 if (ncls != null && ncls.nodetype == "Struct") {
239 // we can use regular setters to apply the values.
240 this.addLine(this.ipad + "this.el = " + this.node.fqn() + "();");
248 switch(this.node.fqn()) {
252 case "Gtk.NotebookPage":
258 // FIXME -- these are all GTK3 - can be removed when I get rid of them..
260 var is_entry = this.node.has("has_entry") && this.node.get_prop("has_entry").val.down() == "true";
262 break; // regular ctor.
264 this.ignoreWrapped("has_entry");
265 ctor = ".with_entry";
269 case "Gtk.ListStore":
270 case "Gtk.TreeStore":
272 // not sure if this works.. otherwise we have to go with varargs and count + vals...
273 if (this.node.has("* types")) {
274 args_str = this.node.get_prop("* types").val;
276 if (this.node.has("n_columns") && this.node.has("columns")) { // old value?
277 args_str = " { " + this.node.get_prop("columns").val + " } ";
278 this.ignoreWrapped("columns");
279 this.ignoreWrapped("n_columns");
282 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ".newv( " + args_str + " );");
286 case "Gtk.LinkButton": // args filled with values.
287 if (this.node.has("label")) {
288 ctor = ".with_label";
295 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ctor);
298 // use the default ctor - with arguments (from properties)
300 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
302 foreach(var param in default_ctor.paramset.params) {
305 GLib.debug("building CTOR ARGS: %s, %s", n, param.is_varargs ? "VARARGS": "");
306 if (n == "___") { // for some reason our varargs are converted to '___' ...
310 if (this.node.has(n)) { // node does not have a value
312 this.ignoreWrapped(n);
315 var v = this.node.get(n);
317 if (param.type == "string") {
318 v = "\"" + v.escape("") + "\"";
320 if (v == "TRUE" || v == "FALSE") {
328 var propnode = this.node.findProp(n);
329 if (propnode != null) {
332 var pname = this.addPropSet(propnode, propnode.has("id") ? propnode.get_prop("id").val : "");
333 args += (pname + ".el") ;
334 if (!propnode.has("id")) {
335 this.addLine(this.ipad + pname +".ref();");
340 this.ignoreWrapped(n);
349 if (param.type.contains("int")) {
353 if (param.type.contains("float")) {
357 if (param.type.contains("bool")) {
358 args += "true"; // always default to true?
361 // any other types???
371 this.node.setLine(this.cur_line, "p", "* xtype");
372 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "( "+ string.joinv(", ",args) + " );") ;
376 // default ctor with no params..
377 if (default_ctor != null && ctor != ".new" ) {
378 this.node.setLine(this.cur_line, "p", "* xtype");
380 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ctor + "( );") ;
385 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "(" + args_str + ");");