Fix #8103 - updating of tree
[roobuilder] / src / Builder4 / PopoverAddProp.vala
1     static Xcls_PopoverAddProp  _PopoverAddProp;
2
3     public class Xcls_PopoverAddProp : Object
4     {
5         public Gtk.Popover el;
6         private Xcls_PopoverAddProp  _this;
7
8         public static Xcls_PopoverAddProp singleton()
9         {
10             if (_PopoverAddProp == null) {
11                 _PopoverAddProp= new Xcls_PopoverAddProp();
12             }
13             return _PopoverAddProp;
14         }
15         public Xcls_viewwin viewwin;
16         public Xcls_view view;
17         public Xcls_selmodel selmodel;
18         public Xcls_sortmodel sortmodel;
19         public Xcls_model model;
20         public Xcls_name name;
21
22             // my vars (def)
23         public bool modal;
24         public JsRender.NodePropType ptype;
25         public bool active;
26         public Xcls_MainWindow mainwindow;
27         public JsRender.Node? node;
28
29         // ctor
30         public Xcls_PopoverAddProp()
31         {
32             _this = this;
33             this.el = new Gtk.Popover();
34
35             // my vars (dec)
36             this.modal = true;
37             this.active = false;
38             this.node = null;
39
40             // set gobject values
41             this.el.width_request = 900;
42             this.el.height_request = 800;
43             this.el.hexpand = false;
44             this.el.position = Gtk.PositionType.RIGHT;
45             new Xcls_viewwin( _this );
46             this.el.set_child ( _this.viewwin.el  );
47         }
48
49         // user defined functions
50         public void show (Palete.Palete pal, JsRender.NodePropType ptype, JsRender.Node node ,  Gtk.Widget onbtn) {
51         
52             /// what does this do?
53             //if (this.prop_or_listener  != "" && this.prop_or_listener == prop_or_listener) {
54             //  this.prop_or_listener = "";
55             //  this.el.hide();
56             //  return;
57                 //}
58                 this.node = node;
59                 
60                 var xtype = node.fqn();
61                 
62                 // 
63                 
64             this.ptype = ptype;
65             
66                  var m = (GLib.ListStore) _this.model.el.model;
67                  m.remove_all();
68          
69             
70             ///Gee.HashMap<string,GirObject>
71             var elementList = pal.getPropertiesFor( xtype, ptype);
72              
73             //print ("GOT " + elementList.length + " items for " + fullpath + "|" + type);
74                    // console.dump(elementList);
75                    
76             var miter = elementList.map_iterator();
77             while (miter.next()) {
78                 var p = miter.get_value(); // nodeprop.
79                   
80                         var prop = p.toNodeProp(pal, xtype);
81                         if (prop == null) {
82                                 continue;
83                         }
84                         if (node.has_prop_key(prop)) {
85                                 GLib.debug("Skip - has key already %s",  prop.name);
86                                 continue;                       
87                         }
88                         //JsRender.NodeProp
89                         GLib.debug("Prop add %s",  prop.name);
90                          m.append(prop);
91             }
92             
93             // set size up...
94             
95          
96              var win = this.mainwindow.el;
97            // var  w = win.get_width();
98             var h = win.get_height() - 50;
99         
100         
101             // left tree = 250, editor area = 500?
102             
103             // min 450?
104                 // max hieght ...
105             this.el.set_size_request( 550, h);
106             //this.el.set_parent(onbtn);
107                 _this.view.el.sort_by_column(null, Gtk.SortType.ASCENDING);
108                 _this.view.el.sort_by_column(_this.name.el, Gtk.SortType.ASCENDING);
109         
110                 //Gtk.Allocation rect;
111                 //onbtn.get_allocation(out rect);
112                 //this.el.set_pointing_to(rect);
113             this.el.show();
114            
115             //while(Gtk.events_pending()) { 
116             //        Gtk.main_iteration();   // why?
117             //}       
118          //   this.hpane.el.set_position( 0);
119         }
120         public void clear () {
121           var m = (GLib.ListStore) _this.model.el.model;
122                 m.remove_all();
123         
124         }
125         public void hide () {
126                 this.ptype = JsRender.NodePropType.NONE;
127                 this.el.hide();
128                 this.node = null;
129         }
130         public class Xcls_viewwin : Object
131         {
132             public Gtk.ScrolledWindow el;
133             private Xcls_PopoverAddProp  _this;
134
135
136                 // my vars (def)
137
138             // ctor
139             public Xcls_viewwin(Xcls_PopoverAddProp _owner )
140             {
141                 _this = _owner;
142                 _this.viewwin = this;
143                 this.el = new Gtk.ScrolledWindow();
144
145                 // my vars (dec)
146
147                 // set gobject values
148                 this.el.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
149                 this.el.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
150                 new Xcls_view( _this );
151                 this.el.set_child ( _this.view.el  );
152             }
153
154             // user defined functions
155         }
156         public class Xcls_view : Object
157         {
158             public Gtk.ColumnView el;
159             private Xcls_PopoverAddProp  _this;
160
161
162                 // my vars (def)
163
164             // ctor
165             public Xcls_view(Xcls_PopoverAddProp _owner )
166             {
167                 _this = _owner;
168                 _this.view = this;
169                 new Xcls_selmodel( _this );
170                 this.el = new Gtk.ColumnView( _this.selmodel.el );
171
172                 // my vars (dec)
173
174                 // set gobject values
175                 this.el.single_click_activate = false;
176                 this.el.hexpand = true;
177                 this.el.vexpand = true;
178                 this.el.show_row_separators = true;
179                 this.el.show_column_separators = true;
180                 this.el.reorderable = true;
181                 var child_2 = new Xcls_GestureClick4( _this );
182                 child_2.ref();
183                 this.el.add_controller(  child_2.el );
184                 new Xcls_name( _this );
185                 this.el.append_column ( _this.name.el  );
186                 var child_4 = new Xcls_ColumnViewColumn13( _this );
187                 child_4.ref();
188                 this.el.append_column ( child_4.el  );
189                 var child_5 = new Xcls_ColumnViewColumn15( _this );
190                 child_5.ref();
191                 this.el.append_column ( child_5.el  );
192             }
193
194             // user defined functions
195             public int getRowAt (double x,  double in_y, out string pos) {
196             
197             
198                  
199             
200             /*
201                         
202             from        https://discourse.gnome.org/t/gtk4-finding-a-row-data-on-gtkcolumnview/8465
203                         var colview = gesture.widget;
204                         var line_no = check_list_widget(colview, x,y);
205                      if (line_no > -1) {
206                                 var item = colview.model.get_item(line_no);
207                                  
208                         }
209                         */
210                          
211                         
212                         //GLib.debug("offset = %d  y = %d", (int) voff, (int) in_y);
213                         var y = in_y + _this.viewwin.el.vadjustment.value; 
214                     var  child = this.el.get_first_child(); 
215                         //Gtk.Allocation alloc = { 0, 0, 0, 0 };
216                         var line_no = -1; 
217                         var reading_header = true;
218                         var real_y = 0;
219                         var header_height  = 0;
220                         pos = "none";
221                         var h = 0;
222                         while (child != null) {
223                                 //GLib.debug("Got %s", child.get_type().name());
224                             if (reading_header) {
225                                         
226             
227                                         if (child.get_type().name() != "GtkColumnListView") {
228                                         h += child.get_height();
229                                                 child = child.get_next_sibling();
230                                                 continue;
231                                         }
232                                         // should be columnlistview
233                                         child = child.get_first_child(); 
234                                     GLib.debug("header height=%d", h);
235                                         header_height =  h;
236                                         
237                                         reading_header = false;
238                                         
239                         }
240                         
241                             if (child.get_type().name() != "GtkColumnViewRowWidget") {
242                                     child = child.get_next_sibling();
243                                     continue;
244                             }
245                             
246                                 if (y < header_height) {
247                                 return -1;
248                         }
249                             
250                             line_no++;
251                                 var hh = child.get_height();
252                                 //child.get_allocation(out alloc);
253                                 //GLib.debug("got cell xy = %d,%d  w,h= %d,%d", alloc.x, alloc.y, alloc.width, alloc.height);
254                                 //GLib.debug("row %d y= %d %s", line_no, (int) (header_height + alloc.y),
255                                 
256                                 //      child.visible ? "VIS" : "hidden");
257             
258                             if (y >  (header_height + real_y) && y <= (header_height +  real_y + hh) ) {
259                                 if (y > ( header_height + real_y + (hh * 0.8))) {
260                                         pos = "below";
261                                 } else if (y > ( header_height + real_y + (hh * 0.2))) {
262                                         pos = "over";
263                                         } else {
264                                                 pos = "above";
265                                         }
266                                  GLib.debug("getRowAt return : %d, %s", line_no, pos);
267                                     return line_no;
268                             }
269              
270             
271                             if (real_y + hh > y) {
272                                 return -1;
273                         }
274                         real_y += hh;
275                         child = child.get_next_sibling(); 
276                         }
277                     return -1;
278             
279              }
280         }
281         public class Xcls_GestureClick4 : Object
282         {
283             public Gtk.GestureClick el;
284             private Xcls_PopoverAddProp  _this;
285
286
287                 // my vars (def)
288
289             // ctor
290             public Xcls_GestureClick4(Xcls_PopoverAddProp _owner )
291             {
292                 _this = _owner;
293                 this.el = new Gtk.GestureClick();
294
295                 // my vars (dec)
296
297                 // set gobject values
298
299                 //listeners
300                 this.el.pressed.connect( (n_press, x, y) => {
301                  
302                         if (n_press < 2) { /// doubleclick?
303                                 return;
304                         }
305                         //string pos;
306                         
307                         
308                         // use selection?!
309                         var tr = (Gtk.TreeListRow)_this.selmodel.el.selected_item;
310                         GLib.debug("SELECTED = %s", tr.item.get_type().name());
311                         var prop = (JsRender.NodeProp) tr.item;
312                 
313                         
314                         
315                         // double press ? 
316                 //      var row = _this.view.getRowAt(x,y, out pos );
317                 //      var prop  = _this.sortmodel.getNodeAt(row);
318                  
319                 //      _this.select(np);
320                         
321                         if (!prop.name.contains("[]") && _this.node.has_prop_key(prop)) {
322                                 GLib.debug("node already has this key.");
323                                 return; // cant add it twice? --  
324                         }
325                         // you can not click on ones with children.
326                         
327                         if (prop.childstore.n_items > 0 ) {
328                                 GLib.debug("no clicking on expandables");
329                                 return;
330                         }
331                         // if it's a node...
332                         if (prop.add_node != null) {
333                 
334                                 if (!prop.name.contains("[]") && null != _this.node.findProp(prop.name)) {
335                                         GLib.debug("Add Child already contains child with %s", prop.name);      
336                                         return;                                 
337                                 }
338                                  GLib.debug("Add Child Node %s", prop.name);                    
339                                  _this.el.hide();
340                                  var add = prop.add_node.deepClone();
341                                 _this.node.appendChild(add);
342                                  _this.mainwindow.windowstate.left_props.changed();
343                                  _this.mainwindow.windowstate.left_tree.model.selectNode(add);
344                                  
345                                 return;
346                         }
347                         
348                         _this.el.hide();
349                         GLib.debug("Add %s", prop.name);
350                         var add = prop.dupe();
351                         _this.node.add_prop(add);
352                          _this.mainwindow.windowstate.left_props.changed();
353                         _this.mainwindow.windowstate.left_props.view.editProp(add);
354                         
355                 
356                          //_this.mainwindow.windowstate.left_props.changed();
357                 
358                 });
359             }
360
361             // user defined functions
362         }
363
364         public class Xcls_selmodel : Object
365         {
366             public Gtk.SingleSelection el;
367             private Xcls_PopoverAddProp  _this;
368
369
370                 // my vars (def)
371
372             // ctor
373             public Xcls_selmodel(Xcls_PopoverAddProp _owner )
374             {
375                 _this = _owner;
376                 _this.selmodel = this;
377                 new Xcls_sortmodel( _this );
378                 this.el = new Gtk.SingleSelection( _this.sortmodel.el );
379
380                 // my vars (dec)
381
382                 // set gobject values
383                 this.el.can_unselect = true;
384             }
385
386             // user defined functions
387             public JsRender.NodeProp? getNodeAt (uint row) {
388             
389                var tr = (Gtk.TreeListRow)this.el.get_item(row);
390                
391                var a = tr.get_item();;   
392                GLib.debug("get_item (2) = %s", a.get_type().name());
393                 
394                return (JsRender.NodeProp)tr.get_item();
395                  
396             }
397         }
398         public class Xcls_sortmodel : Object
399         {
400             public Gtk.SortListModel el;
401             private Xcls_PopoverAddProp  _this;
402
403
404                 // my vars (def)
405
406             // ctor
407             public Xcls_sortmodel(Xcls_PopoverAddProp _owner )
408             {
409                 _this = _owner;
410                 _this.sortmodel = this;
411                 new Xcls_model( _this );
412                 var child_2 = new Xcls_TreeListRowSorter8( _this );
413                 child_2.ref();
414                 this.el = new Gtk.SortListModel( _this.model.el, child_2.el );
415
416                 // my vars (dec)
417
418                 // set gobject values
419
420                 // init method
421
422                 {
423                         //this.el.set_sorter(new Gtk.TreeListRowSorter(_this.view.el.sorter));
424                 }
425             }
426
427             // user defined functions
428             public JsRender.NodeProp? getNodeAt (uint row) {
429             
430                var tr = (Gtk.TreeListRow)this.el.get_item(row);
431                
432                 // GLib.debug("get_item (2) = %s", a.get_type().name());
433                 
434                
435                return (JsRender.NodeProp)tr.get_item();
436                  
437             }
438         }
439         public class Xcls_model : Object
440         {
441             public Gtk.TreeListModel el;
442             private Xcls_PopoverAddProp  _this;
443
444
445                 // my vars (def)
446
447             // ctor
448             public Xcls_model(Xcls_PopoverAddProp _owner )
449             {
450                 _this = _owner;
451                 _this.model = this;
452                 this.el = new Gtk.TreeListModel(
453     new GLib.ListStore(typeof(JsRender.NodeProp)), //..... << that's our store..
454     false, // passthru
455     false, // autexpand
456     (item) => {
457         return ((JsRender.NodeProp)item).childstore;
458     
459     }
460     
461     
462 );
463
464                 // my vars (dec)
465
466                 // set gobject values
467             }
468
469             // user defined functions
470             public JsRender.NodeProp getNodeAt (uint row) {
471             
472                var tr = (Gtk.TreeListRow)this.el.get_item(row);
473                 
474                return (JsRender.NodeProp)tr.get_item();
475                  
476             }
477         }
478
479         public class Xcls_TreeListRowSorter8 : Object
480         {
481             public Gtk.TreeListRowSorter el;
482             private Xcls_PopoverAddProp  _this;
483
484
485                 // my vars (def)
486
487             // ctor
488             public Xcls_TreeListRowSorter8(Xcls_PopoverAddProp _owner )
489             {
490                 _this = _owner;
491                 var child_1 = new Xcls_StringSorter9( _this );
492                 child_1.ref();
493                 this.el = new Gtk.TreeListRowSorter( child_1.el );
494
495                 // my vars (dec)
496
497                 // set gobject values
498             }
499
500             // user defined functions
501         }
502         public class Xcls_StringSorter9 : Object
503         {
504             public Gtk.StringSorter el;
505             private Xcls_PopoverAddProp  _this;
506
507
508                 // my vars (def)
509
510             // ctor
511             public Xcls_StringSorter9(Xcls_PopoverAddProp _owner )
512             {
513                 _this = _owner;
514                 var child_1 = new Xcls_PropertyExpression10( _this );
515                 child_1.ref();
516                 this.el = new Gtk.StringSorter( child_1.el );
517
518                 // my vars (dec)
519
520                 // set gobject values
521             }
522
523             // user defined functions
524         }
525         public class Xcls_PropertyExpression10 : Object
526         {
527             public Gtk.PropertyExpression el;
528             private Xcls_PopoverAddProp  _this;
529
530
531                 // my vars (def)
532
533             // ctor
534             public Xcls_PropertyExpression10(Xcls_PopoverAddProp _owner )
535             {
536                 _this = _owner;
537                 this.el = new Gtk.PropertyExpression( typeof(JsRender.NodeProp), null, "sort_name" );
538
539                 // my vars (dec)
540
541                 // set gobject values
542             }
543
544             // user defined functions
545         }
546
547
548
549
550
551         public class Xcls_name : Object
552         {
553             public Gtk.ColumnViewColumn el;
554             private Xcls_PopoverAddProp  _this;
555
556
557                 // my vars (def)
558
559             // ctor
560             public Xcls_name(Xcls_PopoverAddProp _owner )
561             {
562                 _this = _owner;
563                 _this.name = this;
564                 var child_1 = new Xcls_SignalListItemFactory12( _this );
565                 child_1.ref();
566                 this.el = new Gtk.ColumnViewColumn( "Double click to add", child_1.el );
567
568                 // my vars (dec)
569
570                 // set gobject values
571                 this.el.id = "name";
572                 this.el.expand = true;
573                 this.el.resizable = true;
574
575                 // init method
576
577                 {
578                          this.el.set_sorter(  new Gtk.StringSorter(
579                                 new Gtk.PropertyExpression(typeof(JsRender.NodeProp), null, "name")
580                         ));
581                                 
582                 }
583             }
584
585             // user defined functions
586         }
587         public class Xcls_SignalListItemFactory12 : Object
588         {
589             public Gtk.SignalListItemFactory el;
590             private Xcls_PopoverAddProp  _this;
591
592
593                 // my vars (def)
594
595             // ctor
596             public Xcls_SignalListItemFactory12(Xcls_PopoverAddProp _owner )
597             {
598                 _this = _owner;
599                 this.el = new Gtk.SignalListItemFactory();
600
601                 // my vars (dec)
602
603                 // set gobject values
604
605                 //listeners
606                 this.el.setup.connect( (listitem) => {
607                         
608                         var expand = new Gtk.TreeExpander();
609                          
610                         expand.set_indent_for_depth(true);
611                         expand.set_indent_for_icon(true);
612                          
613                         var lbl = new Gtk.Label("");
614                         lbl.use_markup = true;
615                         
616                         
617                         lbl.halign = Gtk.Align.START;
618                         lbl.xalign = 0;
619                 
620                  
621                         expand.set_child(lbl);
622                         ((Gtk.ListItem)listitem).set_child(expand);
623                         ((Gtk.ListItem)listitem).activatable = false;
624                 });
625                 this.el.bind.connect( (listitem) => {
626                          //GLib.debug("listitme is is %s", ((Gtk.ListItem)listitem).get_type().name());
627                         
628                         
629                         
630                         //var expand = (Gtk.TreeExpander) ((Gtk.ListItem)listitem).get_child();
631                         var expand = (Gtk.TreeExpander)  ((Gtk.ListItem)listitem).get_child();
632                           
633                  
634                         var lbl = (Gtk.Label) expand.child;
635                         
636                          if (lbl.label != "") { // do not update
637                                 return;
638                         }
639                         
640                 
641                         var lr = (Gtk.TreeListRow)((Gtk.ListItem)listitem).get_item();
642                         var np = (JsRender.NodeProp) lr.get_item();
643                         GLib.debug("change  %s to %s", lbl.label, np.name);
644                         lbl.label = np.to_property_option_markup(np.propertyof == _this.node.fqn());
645                         lbl.tooltip_markup = np.to_property_option_tooltip();
646                          
647                     expand.set_hide_expander(  np.childstore.n_items < 1);
648                         expand.set_list_row(lr);
649                  
650                          
651                         // bind image...
652                         
653                 });
654             }
655
656             // user defined functions
657         }
658
659
660         public class Xcls_ColumnViewColumn13 : Object
661         {
662             public Gtk.ColumnViewColumn el;
663             private Xcls_PopoverAddProp  _this;
664
665
666                 // my vars (def)
667
668             // ctor
669             public Xcls_ColumnViewColumn13(Xcls_PopoverAddProp _owner )
670             {
671                 _this = _owner;
672                 var child_1 = new Xcls_SignalListItemFactory14( _this );
673                 child_1.ref();
674                 this.el = new Gtk.ColumnViewColumn( "Type", child_1.el );
675
676                 // my vars (dec)
677
678                 // set gobject values
679                 this.el.expand = true;
680                 this.el.resizable = true;
681
682                 // init method
683
684                 {
685                          this.el.set_sorter(  new Gtk.StringSorter(
686                                 new Gtk.PropertyExpression(typeof(JsRender.NodeProp), null, "rtype")
687                         ));
688                                 
689                 }
690             }
691
692             // user defined functions
693         }
694         public class Xcls_SignalListItemFactory14 : Object
695         {
696             public Gtk.SignalListItemFactory el;
697             private Xcls_PopoverAddProp  _this;
698
699
700                 // my vars (def)
701
702             // ctor
703             public Xcls_SignalListItemFactory14(Xcls_PopoverAddProp _owner )
704             {
705                 _this = _owner;
706                 this.el = new Gtk.SignalListItemFactory();
707
708                 // my vars (dec)
709
710                 // set gobject values
711
712                 //listeners
713                 this.el.setup.connect( (listitem) => {
714                 
715                          
716                         var label = new Gtk.Label("");
717                         label.halign = Gtk.Align.START;
718                         label.xalign = 0;
719                         ((Gtk.ListItem)listitem).set_child(label);
720                         ((Gtk.ListItem)listitem).activatable = false;
721                 });
722                 this.el.bind.connect( (listitem) => {
723                         
724                         var lbl = (Gtk.Label) ((Gtk.ListItem)listitem).get_child(); 
725                         var lr = (Gtk.TreeListRow)((Gtk.ListItem)listitem).get_item();
726                         var np = (JsRender.NodeProp) lr.get_item();
727                         
728                   if (lbl.label != "") { // do not update
729                                 return;
730                         }
731                         lbl.label = np.rtype;
732                          
733                 });
734             }
735
736             // user defined functions
737         }
738
739
740         public class Xcls_ColumnViewColumn15 : Object
741         {
742             public Gtk.ColumnViewColumn el;
743             private Xcls_PopoverAddProp  _this;
744
745
746                 // my vars (def)
747
748             // ctor
749             public Xcls_ColumnViewColumn15(Xcls_PopoverAddProp _owner )
750             {
751                 _this = _owner;
752                 var child_1 = new Xcls_SignalListItemFactory16( _this );
753                 child_1.ref();
754                 this.el = new Gtk.ColumnViewColumn( "Property of", child_1.el );
755
756                 // my vars (dec)
757
758                 // set gobject values
759                 this.el.expand = true;
760                 this.el.resizable = true;
761
762                 // init method
763
764                 {
765                          this.el.set_sorter(  new Gtk.StringSorter(
766                                 new Gtk.PropertyExpression(typeof(JsRender.NodeProp), null, "propertyof")
767                         ));
768                                 
769                 }
770             }
771
772             // user defined functions
773         }
774         public class Xcls_SignalListItemFactory16 : Object
775         {
776             public Gtk.SignalListItemFactory el;
777             private Xcls_PopoverAddProp  _this;
778
779
780                 // my vars (def)
781
782             // ctor
783             public Xcls_SignalListItemFactory16(Xcls_PopoverAddProp _owner )
784             {
785                 _this = _owner;
786                 this.el = new Gtk.SignalListItemFactory();
787
788                 // my vars (dec)
789
790                 // set gobject values
791
792                 //listeners
793                 this.el.setup.connect( (listitem) => {
794                 
795                          
796                         var label = new Gtk.Label("");
797                         label.halign = Gtk.Align.START;
798                         label.xalign = 0;
799                         ((Gtk.ListItem)listitem).set_child(label);
800                         ((Gtk.ListItem)listitem).activatable = false;
801                 });
802                 this.el.bind.connect( (listitem) => {
803                 
804                         var lbl = (Gtk.Label) ((Gtk.ListItem)listitem).get_child(); 
805                         var lr = (Gtk.TreeListRow)((Gtk.ListItem)listitem).get_item();
806                         var np = (JsRender.NodeProp) lr.get_item();
807                         
808                   if (lbl.label != "") { // do not update
809                                 return;
810                         }
811                         lbl.label = np.propertyof;
812                          
813                 });
814             }
815
816             // user defined functions
817         }
818
819
820
821
822     }