17bc0763d66f3c82b618d14af04caf73d2849925
[roobuilder] / src / JsRender / NodeProp.vala
1 /**
2
3 This is a replacement for our key/value 
4 events and properties
5
6  
7 */ 
8 public enum JsRender.NodePropType 
9 {
10         
11         NONE, // fake value - used in popoveraddprop.
12         CTOR, // not used exetp getProperties for?
13         
14         
15         // these are all stored as properties, and should not overlap.
16         PROP,
17         
18         RAW,
19         METHOD,
20         SIGNAL,
21                 
22         // in theory we could have user defined properties that overlap - but probably not a good idea.
23         USER,
24
25
26         
27         // specials - these should be in a seperate list?
28         SPECIAL,
29
30
31         
32         // listerens can definatly overlap as they are stored in a seperate list. << no need to use this for listeners?
33         LISTENER;
34         
35
36         
37         public static string to_abbr(NodePropType intype)
38         {
39                 switch(intype) {
40                         case PROP: return  "";
41                         case RAW: return "$";
42                         case METHOD : return  "|";      
43                         case SIGNAL : return  "@"; // vala signal
44                         case USER : return  "#"; // user defined.
45                         case SPECIAL : return  "*"; // * prop| args | ctor | init
46                         case LISTENER : return  "";  // always raw...
47                         // not used
48                         case NONE:
49                         case CTOR:
50                                  return "";
51                         
52                 }
53                 return "??";
54         }
55         
56         // only usefull for reall values.
57         public static NodePropType from_string(string str)
58         {
59                 switch(str) {
60                         //case "" : return PROP;
61                         case "$": return  RAW;
62                         case "|": return METHOD;
63                         case "@": return  SIGNAL;
64                         case "#": return USER;
65                         case "*": return SPECIAL;
66                         //case "": return case LISTENER : return  ""  // always raw...
67                 }
68                 return PROP;
69         
70         }
71         
72 }
73
74
75
76 public class JsRender.NodeProp : Object {
77
78
79
80         public string name  = "";
81         public NodePropType ptype;  
82         public string rtype = ""; // return or type
83         public string val = "";
84         public int start_line = 0;
85         public int end_line = 0;
86         
87         
88         
89         public NodeProp(string name, NodePropType ptype, string rtype, string val) {
90                 this.name = name;
91                 this.ptype = ptype;
92                 this.rtype = rtype;
93                 this.val = val;
94         }
95         
96         public NodeProp dupe()
97         {
98                 return new NodeProp(this.name, this.ptype, this.rtype,  this.val);
99         }
100         
101         
102         public NodeProp.from_json(string key, string inval)
103         {
104                 this.val = inval;
105                 var kkv = key.strip().split(" ");
106                 string[] kk = {};
107                 for (var i = 0; i < kkv.length; i++) {
108                         if (kkv[i].length > 0 ) {
109                                 kk += kkv[i];
110                         }
111                 }
112                 
113                 switch(kk.length) {
114                         case 1: 
115                                 this.name = kk[0];
116                                 this.ptype = NodePropType.PROP;
117                                 this.rtype = "";                
118                                 return;
119                         case 2: 
120                                 this.name = kk[1];
121                                 if (kk[0].length > 1) {
122                                         // void fred (no type)
123                                         this.rtype = kk[0];
124                                         this.ptype = NodePropType.PROP;
125                                 } else {
126                                         // has a ptype.
127                                         
128                                         this.rtype = ""; // no return type, only a ptype indicator.
129                                         this.ptype = NodePropType.from_string(kk[0]);
130                                 }
131                                 return;
132                         default: // 3 or more... (ignores spaces..)
133                         case 3:
134                                 this.name =  kk[2];
135                                 this.ptype = NodePropType.from_string(kk[0]);
136                                 this.rtype = kk[1];
137                                 return;
138                         
139                 }
140                 
141         }
142         public string  to_json_key()
143         {
144                 
145                 var ortype = this.rtype +  (this.rtype.length > 0 ? " " : "");
146                 var oabbr = NodePropType.to_abbr(this.ptype);
147                 if (oabbr.length > 0) {
148                         oabbr += " ";
149                 }
150                 switch(this.ptype) {
151                         
152
153                         case NodePropType.LISTENER : 
154                                 return this.name; 
155                                 
156                         case NodePropType.PROP:
157                                 return ortype + this.name;                      
158                         
159                         case NodePropType.RAW:
160                         case NodePropType.METHOD:
161                         case NodePropType.SIGNAL:                       
162                         case NodePropType.USER :                        
163                                 return oabbr + ortype + this.name;                      
164                                 
165
166
167                         case NodePropType.SPECIAL:                      
168                                 return oabbr +   this.name;
169                         case NodePropType.NONE: // not used
170                         case NodePropType.CTOR:
171                                  return "";
172                          
173                 }
174                 return this.name;
175         }
176          
177         
178         public string  to_index_key()
179         {
180                 switch(this.ptype) {
181                         case NodePropType.PROP:
182                         case NodePropType.RAW:
183                         case NodePropType.METHOD :
184                         case NodePropType.SIGNAL :
185                         case NodePropType.USER : 
186                                 return this.name;
187                         
188                         case NodePropType.SPECIAL : 
189                                 return "* " + this.name;
190                                 
191                         // in seperate list..
192                         case NodePropType.LISTENER : 
193                                 return  this.name;
194                                 
195                         case NodePropType.NONE: // not used
196                         case NodePropType.CTOR:
197                                  return "";
198
199                                 
200                 }
201                 return this.name;
202         
203         }
204         // how it appears on the property list. -- 
205         public string to_display_name()
206         {
207                 
208                 //return (this.rtype.length > 0 ? this.rtype + " " : "") +  this.name;
209                 // before we showed "@" for signals
210                 switch(this.ptype) {
211                         case NodePropType.PROP:
212                                 return  this.name;
213                                 
214                         case NodePropType.RAW:
215                                 return "<span style=\"italic\">" + GLib.Markup.escape_text(this.name) + "</span>";
216                                 
217                         case NodePropType.METHOD :
218                                 return "<i>" + GLib.Markup.escape_text(this.rtype)  + "</i> <span color=\"#008000\" font_weight=\"bold\">" + GLib.Markup.escape_text( this.name) + "</span>";
219                                 
220                         case NodePropType.SIGNAL : // purpley
221                                 return "<span   color=\"#ea00d6\" font_weight=\"bold\">" + GLib.Markup.escape_text(this.name)+ "</span>";
222                                 
223                         case NodePropType.USER : 
224                                 return  "<i>" + GLib.Markup.escape_text(this.rtype)  + "</i> <span  font_weight=\"bold\">" + GLib.Markup.escape_text(this.name) + "</span>";
225                         
226                         case NodePropType.SPECIAL : 
227                                 return "<span   color=\"#0000CC\" font_weight=\"bold\">" + GLib.Markup.escape_text(this.name) + "</span>";       
228                                 
229                         // in seperate list..
230                         case NodePropType.LISTENER : 
231                                 return  "<b>" + this.name + "</b>";
232                                 
233                         case NodePropType.NONE: // not used
234                         case NodePropType.CTOR:
235                                  return "";
236                 
237                                 
238                 }
239                 return this.name;
240         }
241         
242         // used ot sort the dispaly list of properties.
243         public string to_sort_key()
244         {
245                 var n = this.name;
246                  
247                 //return (this.rtype.length > 0 ? this.rtype + " " : "") +  this.name;
248                 // before we showed "@" for signals
249                 switch(this.ptype) {
250                         case NodePropType.PROP:
251                                 return "5" +  n;
252                                 
253                         case NodePropType.RAW:
254                                 return "5" +  n;
255                                 
256                         case NodePropType.METHOD :
257                                 return "2" +  n;
258                                 
259                         case NodePropType.SIGNAL :
260                                 return "3" +  n;
261                                 
262                         case NodePropType.USER : 
263                                 return "4" +  n;
264                         
265                         case NodePropType.SPECIAL : 
266                                 return "1" +  n;
267                                 
268                         // in seperate list..
269                         case NodePropType.LISTENER : 
270                                 return  "0" + this.name;
271                         
272                         case NodePropType.NONE: // not used
273                         case NodePropType.CTOR:
274                                  return "";
275                                 
276                 }
277                 return this.name;
278         }
279         // this is really only used for stuct ctors at present  
280         // which are only props (although RAW might be valid)
281         public string value_to_code()
282         {
283                 switch (this.ptype) {
284                         case NodePropType.PROP:
285                                 break;
286                                 
287                         case NodePropType.METHOD :                       
288                         case NodePropType.RAW:
289                         case NodePropType.SIGNAL :                      
290                         case NodePropType.USER : 
291                         case NodePropType.SPECIAL : 
292                         case NodePropType.LISTENER : 
293                         case NodePropType.NONE: // not used
294                         case NodePropType.CTOR:                 
295                                 return this.val;
296                 }
297                 if (this.rtype.contains(".")) {
298                         // probalby an enum
299                         return this.val;
300                 }
301                 
302                 
303                 switch (this.rtype) {
304                         case "string":
305                                 return "\"" + this.rtype.escape() + "\"";
306                         case "bool":
307                                 return this.val.down();
308                         case "float":
309                         case "double":
310                         default:
311                                 return this.val;
312                                 
313                         
314                 
315                 }
316                 return this.val;
317         }
318         
319         
320         
321         public string to_tooltip()
322         {
323                  
324                 switch(this.ptype) {
325                         case NodePropType.PROP:
326                                 return this.rtype + " " + this.name + " = \"" + this.val + "\"";
327                         case NodePropType.LISTENER : 
328                                 // thsi might look a bit odd on javascript?
329                                 return "on " + this.name + " " + this.val;
330                                 
331                         case NodePropType.RAW:
332                                 return  this.rtype + " " + this.name + " = " + this.val;
333                         case NodePropType.METHOD :
334                                 // functions - js    FRED  function () { }  <<< could probably be cleaner..
335                                 // functions - vala    FRED () { }
336                                 return  this.rtype + " " + this.name  + " "  + this.val;
337                         case NodePropType.SIGNAL :
338                                 return  "signal: "  + this.rtype + " " + this.name  +  " " + this.val;
339                         case NodePropType.USER : 
340                                 return  "user defined: "  + this.rtype + " " + this.name  + " = "  + this.val;
341                         
342                         case NodePropType.SPECIAL:                      
343                                 return  "special property: "  + this.rtype + " " + this.name  + " = " +   this.val;                     
344
345                         case NodePropType.NONE: // not used
346                         case NodePropType.CTOR:
347                                  return "";
348                 }
349                 return this.name;
350                  
351         }
352         
353          
354         public string to_property_option_markup(bool isbold)
355         {
356                 return isbold ?  "<b>" + this.name + "</b>" : this.name;
357         }
358         
359         public string to_property_option_tooltip()
360         {
361                 return this.to_property_option_markup( false ); // fixme will probaly want help info (possibly by havinga  reference to the GirObject that its created from
362         }
363         
364         
365         public bool is(NodeProp comp) {
366                 if (comp.ptype == NodePropType.LISTENER || this.ptype == NodePropType.LISTENER ) { 
367                         return comp.ptype == this.ptype && comp.name == this.name;
368                 }
369                 return comp.to_index_key() == this.to_index_key();
370         
371         }
372         
373         
374         /*
375         public NodeProp.listenerfromjson(string str, string inval)
376         {
377                 this.val = inval;
378                 this.name = str;
379                 this.ptype = NodePropType.LISTENER;
380                 this.rtype = "";
381                 
382         }
383         */
384         // regular addition - should work for properties  
385         public NodeProp.prop(string name, string rtype = "", string val = "")
386         {
387                 this(name, NodePropType.PROP, rtype, val);
388         }
389         public NodeProp.raw(string name, string rtype = "", string val = "")
390         {
391                 this(name, NodePropType.RAW, rtype, val);
392         }
393         
394         public NodeProp.valamethod(string name, string rtype = "void", string val = "() {\n\n}")
395         {
396                 this(name, NodePropType.METHOD, rtype, val);
397         }
398         public NodeProp.jsmethod(string name,  string val = "function() {\n\n}")
399         {
400                 this(name, NodePropType.METHOD, "", val);
401         }
402         
403         // vala (and js) specials.. props etc.. - they only have name/value (not type) - type is in xns/xtype
404         public NodeProp.special(string name, string val = "")
405         {
406                 this(name, NodePropType.SPECIAL, "", val);
407         }
408          
409         public NodeProp.listener(string name,   string val = "")
410         {
411                 this(name, NodePropType.LISTENER, "", val);
412         }
413          
414         public NodeProp.user(string name, string rtype = "", string val = "")
415         {
416                 this(name, NodePropType.USER, rtype, val);
417         }
418         public NodeProp.sig(string name, string rtype = "void", string val = "()")
419         {
420                 this(name, NodePropType.SIGNAL, rtype, val);
421         }
422         
423 }
424