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();
179 if (this.depth < 1) {
181 // top level - does not pass the top level element..
182 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
183 this.addLine(this.pad + "{");
185 if (cargs_str.length > 0) {
186 cargs_str = ", " + cargs_str;
188 var top = this.top as NodeToVala;
189 var tcls = top == null ? "???" : top.xcls;
190 // for sub classes = we passs the top level as _owner
191 this.addLine(this.pad + "public " + this.xcls + "(" + tcls + " _owner " + cargs_str + ")");
192 this.addLine(this.pad + "{");
198 * Initialize this.el to point to the wrapped element.
203 void addWrappedCtor()
206 // this may need to look up properties to fill in the arguments..
207 // introspection does not workk..... - as things like gtkmessagedialog
209 if (cls == 'Gtk.Table') {
211 var methods = this.palete.getPropertiesFor(cls, 'methods');
213 print(JSON.stringify(this.palete.proplist[cls], null,4));
218 // ctor can still override.
219 if (this.node.has("* ctor")) {
220 this.node.setLine(this.cur_line, "p", "* ctor");
221 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
225 this.node.setLine(this.cur_line, "p", "* xtype");;
227 // is the wrapped element a struct?
229 var ncls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
230 if (ncls != null && ncls.nodetype == "Struct") {
231 // we can use regular setters to apply the values.
232 this.addLine(this.ipad + "this.el = " + this.node.fqn() + "();");
240 switch(this.node.fqn()) {
242 // FIXME -- these are all GTK3 - can be removed when I get rid of them..
244 var is_entry = this.node.has("has_entry") && this.node.get_prop("has_entry").val.down() == "true";
246 break; // regular ctor.
248 this.ignoreWrapped("has_entry");
249 ctor = ".with_entry";
253 case "Gtk.ListStore":
254 case "Gtk.TreeStore":
256 // not sure if this works.. otherwise we have to go with varargs and count + vals...
257 if (this.node.has("* types")) {
258 args_str = this.node.get_prop("* types").val;
260 if (this.node.has("n_columns") && this.node.has("columns")) { // old value?
261 args_str = " { " + this.node.get_prop("columns").val + " } ";
262 this.ignoreWrapped("columns");
263 this.ignoreWrapped("n_columns");
266 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ".newv( " + args_str + " );");
270 case "Gtk.LinkButton": // args filled with values.
271 if (this.node.has("label")) {
272 ctor = ".with_label";
279 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ctor);
282 // use the default ctor - with arguments (from properties)
284 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
286 foreach(var param in default_ctor.paramset.params) {
289 GLib.debug("building CTOR ARGS: %s, %s", n, param.is_varargs ? "VARARGS": "");
290 if (n == "___") { // for some reason our varargs are converted to '___' ...
294 if (this.node.has(n)) { // node does not have a value
296 this.ignoreWrapped(n);
299 var v = this.node.get(n);
301 if (param.type == "string") {
302 v = "\"" + v.escape("") + "\"";
304 if (v == "TRUE" || v == "FALSE") {
312 var propnode = this.node.findProp(n);
313 if (propnode != null) {
316 var pname = this.addPropSet(propnode, propnode.has("id") ? propnode.get_prop("id").val : "");
317 args += (pname + ".el") ;
318 if (!propnode.has("id")) {
319 this.addLine(this.ipad + pname +".ref();");
324 this.ignoreWrapped(n);
333 if (param.type.contains("int")) {
337 if (param.type.contains("float")) {
341 if (param.type.contains("bool")) {
342 args += "true"; // always default to true?
345 // any other types???
355 this.node.setLine(this.cur_line, "p", "* xtype");
356 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "( "+ string.joinv(", ",args) + " );") ;
360 // default ctor with no params..
361 if (default_ctor != null && ctor != ".new" ) {
362 this.node.setLine(this.cur_line, "p", "* xtype");
364 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ctor + "( );") ;
369 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "(" + args_str + ");");