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