fix init output
[roobuilder] / src / Builder4 / PopoverProperty.vala
1 static Xcls_PopoverProperty  _PopoverProperty;
2
3 public class Xcls_PopoverProperty : Object
4 {
5     public Gtk.Popover el;
6     private Xcls_PopoverProperty  _this;
7
8     public static Xcls_PopoverProperty singleton()
9     {
10         if (_PopoverProperty == null) {
11             _PopoverProperty= new Xcls_PopoverProperty();
12         }
13         return _PopoverProperty;
14     }
15     public Xcls_header header;
16     public Xcls_kflag kflag;
17     public Xcls_dbcellrenderer dbcellrenderer;
18     public Xcls_dbmodel dbmodel;
19     public Xcls_ktype ktype;
20     public Xcls_kname kname;
21     public Xcls_error error;
22     public Xcls_buttonbar buttonbar;
23
24         // my vars (def)
25     public string old_keyname;
26     public bool is_new;
27     public signal void success (Project.Project pr, JsRender.JsRender file);
28     public bool done;
29     public JsRender.NodeProp? prop;
30     public Xcls_MainWindow mainwindow;
31     public string key_type;
32     public JsRender.Node node;
33
34     // ctor
35     public Xcls_PopoverProperty()
36     {
37         _this = this;
38         this.el = new Gtk.Popover( null );
39
40         // my vars (dec)
41         this.is_new = false;
42         this.done = false;
43         this.mainwindow = null;
44
45         // set gobject values
46         this.el.border_width = 0;
47         this.el.modal = true;
48         this.el.position = Gtk.PositionType.LEFT;
49         var child_0 = new Xcls_Box2( _this );
50         child_0.ref();
51         this.el.add (  child_0.el  );
52
53         //listeners
54         this.el.closed.connect( () => {
55         
56                 GLib.debug("popover closed");
57                 if (_this.is_new) {
58                         // dont allow hiding if we are creating a new one.
59                         // on.hide will reshow it.
60                         return;
61                 }
62                 if (_this.prop == null) {
63                         // hide and dont update.
64                         return;
65                 }
66                 if (this.kname.el.get_text().strip().length < 1) {
67                         return;
68                 }
69                 
70                 var oldkey = this.prop.to_index_key();  
71                         
72                  
73                 this.updateProp();
74                         
75                 var newkey = this.prop.to_index_key();  
76                 if (oldkey != newkey) {
77                 
78                         if (_this.prop.ptype == JsRender.NodePropType.LISTENER) {
79                                 this.node.listeners.unset(oldkey);
80                                 this.node.listeners.set(newkey, _this.prop);
81                         } else {
82                                 this.node.props.unset(oldkey);
83                                 this.node.props.set(newkey, _this.prop);
84                         }
85                 
86                 }
87                 _this.mainwindow.windowstate.left_props.reload();
88         
89         
90           
91         });
92         this.el.hide.connect( () => {
93                 GLib.debug("popover hidden");
94                 if (_this.is_new || this.kname.el.get_text().strip().length < 1) {
95                         // dont allow hiding if we are creating a new one.
96                         GLib.debug("prevent hiding as its new or text is empty"); 
97                         this.el.show_all();
98                         return;
99         
100                 }
101                 
102         });
103     }
104
105     // user defined functions
106     public void show (
107         Gtk.Widget btn, 
108         JsRender.Node node, 
109         JsRender.NodeProp prop, 
110         int y,
111         bool is_new = false
112          ) 
113     {
114         
115        
116         this.is_new = is_new; 
117         var pref = is_new ? "Add " : "Modify ";
118         if (prop.ptype == JsRender.NodePropType.LISTENER) {
119                 this.header.el.title = pref + "Event Listener"; // cant really happen yet?
120         } else {
121                 this.header.el.title = pref + "Property";
122         }
123         this.prop = prop;
124         this.node = node;
125         
126         _this.kname.el.set_text(prop.name);
127         _this.ktype.el.set_text(prop.rtype);
128         
129         _this.dbmodel.loadData(prop );
130         // does node have this property...
131     
132     
133         _this.node = node;
134         //console.log('show all');
135         this.el.set_modal(true);
136         this.el.set_relative_to(btn);
137         if (y > -1) {
138                 var  r = Gdk.Rectangle() {
139                         x = 0, // align left...
140                         y = y,
141                         width = 1,
142                         height = 1
143                 };
144                 this.el.set_pointing_to( r);
145         }
146         
147         
148     
149         //this.el.set_position(Gtk.PositionType.TOP);
150     
151         // window + header?
152          print("SHOWALL - POPIP\n");
153         this.el.show_all();
154         this.kname.el.grab_focus();
155         this.buttonbar.el.hide();
156         if (this.is_new) {
157                 this.buttonbar.el.show();
158         }
159          this.error.setError("");
160     
161         //this.success = c.success;
162      
163     }
164     public void updateProp () {
165      
166         Gtk.TreeIter citer;
167         GLib.Value gval;
168         this.kflag.el.get_active_iter(out citer);
169         this.dbmodel.el.get_value(citer, 0, out  gval);
170     
171     
172         _this.prop.name = this.kname.el.get_text().strip(); 
173         _this.prop.rtype = this.ktype.el.get_text().strip(); 
174         _this.prop.ptype =  (JsRender.NodePropType) gval;
175     
176     }
177     public class Xcls_Box2 : Object
178     {
179         public Gtk.Box el;
180         private Xcls_PopoverProperty  _this;
181
182
183             // my vars (def)
184
185         // ctor
186         public Xcls_Box2(Xcls_PopoverProperty _owner )
187         {
188             _this = _owner;
189             this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
190
191             // my vars (dec)
192
193             // set gobject values
194             this.el.homogeneous = false;
195             var child_0 = new Xcls_header( _this );
196             child_0.ref();
197             this.el.pack_start (  child_0.el , false,true,0 );
198             var child_1 = new Xcls_Label4( _this );
199             child_1.ref();
200             this.el.add (  child_1.el  );
201             var child_2 = new Xcls_kflag( _this );
202             child_2.ref();
203             this.el.add (  child_2.el  );
204             var child_3 = new Xcls_Label8( _this );
205             child_3.ref();
206             this.el.add (  child_3.el  );
207             var child_4 = new Xcls_ktype( _this );
208             child_4.ref();
209             this.el.add (  child_4.el  );
210             var child_5 = new Xcls_Label10( _this );
211             child_5.ref();
212             this.el.add (  child_5.el  );
213             var child_6 = new Xcls_kname( _this );
214             child_6.ref();
215             this.el.add (  child_6.el  );
216             var child_7 = new Xcls_error( _this );
217             child_7.ref();
218             this.el.add (  child_7.el  );
219             var child_8 = new Xcls_buttonbar( _this );
220             child_8.ref();
221             this.el.add (  child_8.el  );
222         }
223
224         // user defined functions
225     }
226     public class Xcls_header : Object
227     {
228         public Gtk.HeaderBar el;
229         private Xcls_PopoverProperty  _this;
230
231
232             // my vars (def)
233
234         // ctor
235         public Xcls_header(Xcls_PopoverProperty _owner )
236         {
237             _this = _owner;
238             _this.header = this;
239             this.el = new Gtk.HeaderBar();
240
241             // my vars (dec)
242
243             // set gobject values
244             this.el.title = "Modify / Create Property";
245         }
246
247         // user defined functions
248     }
249
250     public class Xcls_Label4 : Object
251     {
252         public Gtk.Label el;
253         private Xcls_PopoverProperty  _this;
254
255
256             // my vars (def)
257
258         // ctor
259         public Xcls_Label4(Xcls_PopoverProperty _owner )
260         {
261             _this = _owner;
262             this.el = new Gtk.Label( "Special Flags" );
263
264             // my vars (dec)
265
266             // set gobject values
267             this.el.halign = Gtk.Align.START;
268             this.el.justify = Gtk.Justification.LEFT;
269             this.el.margin_top = 12;
270         }
271
272         // user defined functions
273     }
274
275     public class Xcls_kflag : Object
276     {
277         public Gtk.ComboBox el;
278         private Xcls_PopoverProperty  _this;
279
280
281             // my vars (def)
282
283         // ctor
284         public Xcls_kflag(Xcls_PopoverProperty _owner )
285         {
286             _this = _owner;
287             _this.kflag = this;
288             this.el = new Gtk.ComboBox();
289
290             // my vars (dec)
291
292             // set gobject values
293             var child_0 = new Xcls_dbcellrenderer( _this );
294             child_0.ref();
295             this.el.pack_start (  child_0.el , true );
296             var child_1 = new Xcls_dbmodel( _this );
297             child_1.ref();
298             this.el.set_model (  child_1.el  );
299
300             // init method
301
302             this.el.add_attribute(_this.dbcellrenderer.el , "markup", 1 );
303         }
304
305         // user defined functions
306     }
307     public class Xcls_dbcellrenderer : Object
308     {
309         public Gtk.CellRendererText el;
310         private Xcls_PopoverProperty  _this;
311
312
313             // my vars (def)
314
315         // ctor
316         public Xcls_dbcellrenderer(Xcls_PopoverProperty _owner )
317         {
318             _this = _owner;
319             _this.dbcellrenderer = this;
320             this.el = new Gtk.CellRendererText();
321
322             // my vars (dec)
323
324             // set gobject values
325         }
326
327         // user defined functions
328     }
329
330     public class Xcls_dbmodel : Object
331     {
332         public Gtk.ListStore el;
333         private Xcls_PopoverProperty  _this;
334
335
336             // my vars (def)
337
338         // ctor
339         public Xcls_dbmodel(Xcls_PopoverProperty _owner )
340         {
341             _this = _owner;
342             _this.dbmodel = this;
343             this.el = new Gtk.ListStore( 2, typeof(JsRender.NodePropType),typeof(string) );
344
345             // my vars (dec)
346
347             // set gobject values
348         }
349
350         // user defined functions
351         public void loadData (JsRender.NodeProp prop) {
352             this.el.clear();                                    
353             Gtk.TreeIter iter;
354             var el = this.el;
355             
356             
357             // vala signal.. '@'
358             // raw value '$'
359             // user defined property '#'
360             // user defined method '|'
361             // special property '*' => prop  |args|ctor|init
362             
363             
364             
365            /// el.append(out iter);
366             
367              
368            // el.set_value(iter, 0, "");
369            // el.set_value(iter, 1, "aaa  - Just add Element - aaa");
370         
371             
372                 if (prop.ptype == JsRender.NodePropType.LISTENER) { 
373                         el.append(out iter);
374                         el.set(iter, 0, JsRender.NodePropType.LISTENER, 1,   "Event Handler / Listener", -1);
375                 }        
376                 else if (_this.mainwindow.windowstate.file.xtype == "Gtk") {
377                          el.append(out iter);
378                     el.set(iter, 0, JsRender.NodePropType.PROP, 1,   "Normal Property", -1);
379                 
380                         
381                         el.append(out iter);
382                         el.set(iter, 0, JsRender.NodePropType.RAW, 1,   "Raw Property (not escaped)", -1);
383                          
384                         
385                         el.append(out iter);
386                         el.set(iter, 0, JsRender.NodePropType.USER, 1,   "User defined property", -1);
387                          
388                         el.append(out iter);
389                         el.set(iter, 0, JsRender.NodePropType.METHOD, 1,   "User defined method", -1);
390                          
391                         el.append(out iter);
392                         el.set(iter, 0, JsRender.NodePropType.SPECIAL, 1,   "Special property (eg. prop | args | ctor | init )", -1);
393                          
394                         
395                         el.append(out iter);
396                     el.set(iter, 0, JsRender.NodePropType.SIGNAL, 1,   "Vala Signal", -1);
397                          
398                         
399                 } else { 
400                         // javascript
401                     el.append(out iter);
402                     el.set(iter, 0, JsRender.NodePropType.PROP, 1,   "Normal Property", -1);
403                 
404                         el.append(out iter);
405                         el.set(iter, 0, JsRender.NodePropType.RAW, 1,   "Raw Property (not escaped)", -1);
406                          
407                         el.append(out iter);
408                         el.set(iter, 0, JsRender.NodePropType.METHOD, 1,   "User defined method", -1);
409                  
410                         el.append(out iter);
411                         el.set(iter, 0,  JsRender.NodePropType.SPECIAL, 1,   "(*) Special property (eg. prop )", -1);
412                          
413                 
414                 }
415                 // set selected, based on arg
416                 el.foreach((tm, tp, titer) => {
417                         GLib.Value val;
418                         el.get_value(titer, 0, out val);
419                          
420                         //print("check %s against %s\n", (string)val, _this.prop.ptype);
421                         if (((JsRender.NodePropType)val) == prop.ptype) {
422                                 _this.kflag.el.set_active_iter(titer);
423                                 return true;
424                         }
425                         return false;
426                 });
427                 
428         
429                                              
430         }
431     }
432
433
434     public class Xcls_Label8 : Object
435     {
436         public Gtk.Label el;
437         private Xcls_PopoverProperty  _this;
438
439
440             // my vars (def)
441
442         // ctor
443         public Xcls_Label8(Xcls_PopoverProperty _owner )
444         {
445             _this = _owner;
446             this.el = new Gtk.Label( "Type or Return Type" );
447
448             // my vars (dec)
449
450             // set gobject values
451             this.el.halign = Gtk.Align.START;
452             this.el.justify = Gtk.Justification.LEFT;
453             this.el.margin_top = 12;
454             this.el.visible = true;
455         }
456
457         // user defined functions
458     }
459
460     public class Xcls_ktype : Object
461     {
462         public Gtk.Entry el;
463         private Xcls_PopoverProperty  _this;
464
465
466             // my vars (def)
467
468         // ctor
469         public Xcls_ktype(Xcls_PopoverProperty _owner )
470         {
471             _this = _owner;
472             _this.ktype = this;
473             this.el = new Gtk.Entry();
474
475             // my vars (dec)
476
477             // set gobject values
478             this.el.visible = true;
479         }
480
481         // user defined functions
482     }
483
484     public class Xcls_Label10 : Object
485     {
486         public Gtk.Label el;
487         private Xcls_PopoverProperty  _this;
488
489
490             // my vars (def)
491
492         // ctor
493         public Xcls_Label10(Xcls_PopoverProperty _owner )
494         {
495             _this = _owner;
496             this.el = new Gtk.Label( "Name" );
497
498             // my vars (dec)
499
500             // set gobject values
501             this.el.halign = Gtk.Align.START;
502             this.el.justify = Gtk.Justification.LEFT;
503             this.el.tooltip_text = "center, north, south, east, west";
504             this.el.margin_top = 12;
505             this.el.visible = true;
506         }
507
508         // user defined functions
509     }
510
511     public class Xcls_kname : Object
512     {
513         public Gtk.Entry el;
514         private Xcls_PopoverProperty  _this;
515
516
517             // my vars (def)
518
519         // ctor
520         public Xcls_kname(Xcls_PopoverProperty _owner )
521         {
522             _this = _owner;
523             _this.kname = this;
524             this.el = new Gtk.Entry();
525
526             // my vars (dec)
527
528             // set gobject values
529             this.el.visible = true;
530
531             //listeners
532             this.el.focus_out_event.connect( ()=>{
533                 _this.error.setError("");
534                 var val = this.el.get_text().strip(); 
535                 if (val.length < 1) {
536                         _this.error.setError("Name can not be empty");
537                 }
538                 return true;
539             });
540             this.el.key_release_event.connect( ()=>{
541                 _this.error.setError("");
542                 var val = this.el.get_text().strip(); 
543                 if (val.length < 1) {
544                         _this.error.setError("Name can not be empty");
545                 }
546                 return true;
547             });
548         }
549
550         // user defined functions
551     }
552
553     public class Xcls_error : Object
554     {
555         public Gtk.Label el;
556         private Xcls_PopoverProperty  _this;
557
558
559             // my vars (def)
560
561         // ctor
562         public Xcls_error(Xcls_PopoverProperty _owner )
563         {
564             _this = _owner;
565             _this.error = this;
566             this.el = new Gtk.Label( "<span color=\"red\">Error Message</span>" );
567
568             // my vars (dec)
569
570             // set gobject values
571             this.el.halign = Gtk.Align.START;
572             this.el.justify = Gtk.Justification.LEFT;
573             this.el.tooltip_text = "center, north, south, east, west";
574             this.el.margin_top = 0;
575             this.el.visible = true;
576             this.el.use_markup = true;
577         }
578
579         // user defined functions
580         public void setError (string err)   {
581                 if (err == "") {
582                         this.el.hide();
583                 } else {
584                         this.el.show();
585                         
586                         this.el.label = "<span color=\"red\">" + err + "</span>";
587                 }
588         }
589     }
590
591     public class Xcls_buttonbar : Object
592     {
593         public Gtk.Box el;
594         private Xcls_PopoverProperty  _this;
595
596
597             // my vars (def)
598
599         // ctor
600         public Xcls_buttonbar(Xcls_PopoverProperty _owner )
601         {
602             _this = _owner;
603             _this.buttonbar = this;
604             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
605
606             // my vars (dec)
607
608             // set gobject values
609             this.el.margin_top = 20;
610             var child_0 = new Xcls_Button14( _this );
611             child_0.ref();
612             this.el.add (  child_0.el  );
613             var child_1 = new Xcls_Button16( _this );
614             child_1.ref();
615             this.el.add (  child_1.el  );
616         }
617
618         // user defined functions
619     }
620     public class Xcls_Button14 : Object
621     {
622         public Gtk.Button el;
623         private Xcls_PopoverProperty  _this;
624
625
626             // my vars (def)
627
628         // ctor
629         public Xcls_Button14(Xcls_PopoverProperty _owner )
630         {
631             _this = _owner;
632             this.el = new Gtk.Button();
633
634             // my vars (dec)
635
636             // set gobject values
637             this.el.hexpand = true;
638             this.el.always_show_image = true;
639             this.el.label = "Cancel";
640             var child_0 = new Xcls_Image15( _this );
641             child_0.ref();
642             this.el.image = child_0.el;
643
644             //listeners
645             this.el.pressed.connect( () => { 
646             
647                 _this.prop = null;
648                 _this.is_new = false;
649                 _this.kname.el.set_text("Cancel");
650                 _this.el.hide();
651             
652             });
653         }
654
655         // user defined functions
656     }
657     public class Xcls_Image15 : Object
658     {
659         public Gtk.Image el;
660         private Xcls_PopoverProperty  _this;
661
662
663             // my vars (def)
664
665         // ctor
666         public Xcls_Image15(Xcls_PopoverProperty _owner )
667         {
668             _this = _owner;
669             this.el = new Gtk.Image();
670
671             // my vars (dec)
672
673             // set gobject values
674             this.el.icon_name = "window-close";
675         }
676
677         // user defined functions
678     }
679
680
681     public class Xcls_Button16 : Object
682     {
683         public Gtk.Button el;
684         private Xcls_PopoverProperty  _this;
685
686
687             // my vars (def)
688
689         // ctor
690         public Xcls_Button16(Xcls_PopoverProperty _owner )
691         {
692             _this = _owner;
693             this.el = new Gtk.Button();
694
695             // my vars (dec)
696
697             // set gobject values
698             this.el.hexpand = true;
699             this.el.always_show_image = true;
700             this.el.label = "Add Property";
701             var child_0 = new Xcls_Image17( _this );
702             child_0.ref();
703             this.el.image = child_0.el;
704
705             //listeners
706             this.el.pressed.connect( () => {
707                 // check if text is not empty..
708                 if ( _this.kname.el.get_text().strip().length < 1) {
709                         // error should already be showing?
710                         return;
711                 }
712                 _this.updateProp();
713                 
714                 // since we can't add listeners?!?!?
715                 // only check props.
716                 // check if property already exists in node.    
717                 var prop = _this.prop;
718                 if (_this.node.props.has_key(prop.to_index_key())) {
719                         _this.error.setError("Property already exists");
720                         return; 
721                 }
722                 
723                 
724                  
725                 _this.is_new = false;   
726                   
727                 // hide self
728                 _this.prop = null; // skip checks..
729                 _this.el.hide();
730             
731             // add it, 
732                 // trigger editing of property.
733                 // allow hide to work?
734                 while (Gtk.events_pending()) {
735                         Gtk.main_iteration();
736                 }
737                 
738                 _this.mainwindow.windowstate.left_props.addProp(prop);          
739                 
740             
741             });
742         }
743
744         // user defined functions
745     }
746     public class Xcls_Image17 : Object
747     {
748         public Gtk.Image el;
749         private Xcls_PopoverProperty  _this;
750
751
752             // my vars (def)
753
754         // ctor
755         public Xcls_Image17(Xcls_PopoverProperty _owner )
756         {
757             _this = _owner;
758             this.el = new Gtk.Image();
759
760             // my vars (dec)
761
762             // set gobject values
763             this.el.icon_name = "list-add";
764         }
765
766         // user defined functions
767     }
768
769
770
771
772 }