src/Builder4/WindowLeftTree.bjs
[app.Builder.js] / src / Builder4 / WindowLeftTree.vala
1 static Xcls_WindowLeftTree  _WindowLeftTree;
2
3 public class Xcls_WindowLeftTree : Object
4 {
5     public Gtk.ScrolledWindow el;
6     private Xcls_WindowLeftTree  _this;
7
8     public static Xcls_WindowLeftTree singleton()
9     {
10         if (_WindowLeftTree == null) {
11             _WindowLeftTree= new Xcls_WindowLeftTree();
12         }
13         return _WindowLeftTree;
14     }
15     public Xcls_view view;
16     public Xcls_model model;
17     public Xcls_renderer renderer;
18     public Xcls_LeftTreeMenu LeftTreeMenu;
19
20         // my vars (def)
21     public signal bool before_node_change (JsRender.Node? node);
22     public signal void changed ();
23     public signal void node_selected (JsRender.Node? node);
24     public Xcls_MainWindow main_window;
25
26     // ctor
27     public Xcls_WindowLeftTree()
28     {
29         _this = this;
30         this.el = new Gtk.ScrolledWindow( null, null );
31
32         // my vars (dec)
33         this.main_window = null;
34
35         // set gobject values
36         this.el.shadow_type = Gtk.ShadowType.IN;
37         var child_0 = new Xcls_view( _this );
38         child_0.ref();
39         this.el.add (  child_0.el  );
40         var child_1 = new Xcls_LeftTreeMenu( _this );
41         child_1.ref();
42
43         // init method
44
45         this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
46     }
47
48     // user defined functions
49     public           JsRender.Node? getActiveElement () { // return path to actie node.
50     
51          var path = this.getActivePath();
52          if (path.length < 1) {
53             return null;
54          }
55          return _this.model.pathToNode(path);
56          
57     }
58     public           JsRender.JsRender getActiveFile () {
59         return this.model.file;
60     }
61     public           string getActivePath () {
62         
63         var view = this.view.el;
64         if (view.get_selection().count_selected_rows() < 1) {
65             return "";
66         }
67         Gtk.TreeIter iter;
68         Gtk.TreeModel mod;
69         view.get_selection().get_selected(out mod, out iter);
70         return mod.get_path(iter).to_string();
71     }
72     public class Xcls_view : Object
73     {
74         public Gtk.TreeView el;
75         private Xcls_WindowLeftTree  _this;
76
77
78             // my vars (def)
79         public string dragData;
80         public int drag_x;
81         public string[] dropList;
82         public int drag_y;
83         public bool drag_in_motion;
84         public bool blockChanges;
85
86         // ctor
87         public Xcls_view(Xcls_WindowLeftTree _owner )
88         {
89             _this = _owner;
90             _this.view = this;
91             this.el = new Gtk.TreeView();
92
93             // my vars (dec)
94             this.blockChanges = false;
95
96             // set gobject values
97             this.el.tooltip_column = 1;
98             this.el.enable_tree_lines = true;
99             this.el.headers_visible = false;
100             var child_0 = new Xcls_model( _this );
101             child_0.ref();
102             this.el.set_model (  child_0.el  );
103             var child_1 = new Xcls_TreeViewColumn4( _this );
104             child_1.ref();
105             this.el.append_column (  child_1.el  );
106
107             // init method
108
109             {
110                 var description = new Pango.FontDescription();
111                 description.set_size(8000);
112                 this.el.override_font(description);
113             
114                 var selection = this.el.get_selection();
115                 selection.set_mode( Gtk.SelectionMode.SINGLE);
116             
117             
118                 // is this really needed??
119                 /*
120                 this.selection.signal['changed'].connect(function() {
121                     _this.get('/LeftTree.view').listeners.cursor_changed.apply(
122                         _this.get('/LeftTree.view'), [ _this.get('/LeftTree.view'), '']
123                     );
124                 });
125                 */
126                 Gtk.drag_source_set (
127                     this.el,            /* widget will be drag-able */
128                     Gdk.ModifierType.BUTTON1_MASK,       /* modifier that will start a drag */
129                     BuilderApplication.targetList,            /* lists of target to support */
130                     Gdk.DragAction.COPY   | Gdk.DragAction.MOVE    |  Gdk.DragAction.LINK           /* what to do with data after dropped */
131                 );
132             
133                 // ?? needed??
134                 //Gtk.drag_source_add_text_targets(this.el); 
135             
136                 Gtk.drag_dest_set
137                 (
138                     this.el,              /* widget that will accept a drop */
139                     Gtk.DestDefaults.MOTION  | Gtk.DestDefaults.HIGHLIGHT,
140                     BuilderApplication.targetList,            /* lists of target to support */
141                     Gdk.DragAction.COPY   | Gdk.DragAction.MOVE   | Gdk.DragAction.LINK     /* what to do with data after dropped */
142                 );
143             
144                 //Gtk.drag_dest_set_target_list(this.el, Builder.Application.targetList);
145                 //Gtk.drag_dest_add_text_targets(this.el);
146             }
147
148             //listeners
149             this.el.button_press_event.connect( ( ev) => {
150                 //console.log("button press?");
151                 if (! _this.before_node_change(null) ) {
152                    return true;
153                 }
154             
155                 
156                 if (ev.type != Gdk.EventType.BUTTON_PRESS  || ev.button != 3) {
157                     //print("click" + ev.type);
158                     return false;
159                 }
160                 Gtk.TreePath res;
161                 if (!_this.view.el.get_path_at_pos((int)ev.x,(int)ev.y, out res, null, null, null) ) {
162                     return true;
163                 }
164                  
165                 this.el.get_selection().select_path(res);
166                  
167                   //if (!this.get('/LeftTreeMenu').el)  { 
168                   //      this.get('/LeftTreeMenu').init(); 
169                   //  }
170                     
171                  _this.LeftTreeMenu.el.set_screen(Gdk.Screen.get_default());
172                  _this.LeftTreeMenu.el.show_all();
173                   _this.LeftTreeMenu.el.popup(null, null, null,  3, ev.time);
174                  //   print("click:" + res.path.to_string());
175                   return true;
176             });
177             this.el.drag_begin.connect( ( ctx)  => {
178                 //print('SOURCE: drag-begin');
179                     
180                     
181                     //this.targetData = "";
182                     
183                     // find what is selected in our tree...
184                     
185                     var s = _this.view.el.get_selection();
186                     if (s.count_selected_rows() < 1) {
187                         return;
188                     }
189                     Gtk.TreeIter iter;
190                     Gtk.TreeModel mod;
191                     s.get_selected(out mod, out iter);
192             
193                     
194             
195                     // set some properties of the tree for use by the dropped element.
196                     GLib.Value value;
197                     _this.model.el.get_value(iter, 2, out value);
198                     var tp = mod.get_path(iter).to_string();
199                     var data = (JsRender.Node)(value.dup_object());
200                     var xname = data.fqn();
201                     print ("XNAME  IS " + xname+ "\n");
202                     this.dragData = tp;
203                     this.dropList = _this.model.file.palete().getDropList(xname);
204                     
205                     print ("DROP LIST IS " + string.joinv(", ", this.dropList) + "\n");
206                     
207             
208                     // make the drag icon a picture of the node that was selected
209                 
210                     
211                 // by default returns the path..
212                    var path = _this.model.el.get_path(iter);
213             
214                      
215                     var pix = this.el.create_row_drag_icon ( path);
216                     
217                     Gtk.drag_set_icon_surface (ctx, pix) ;
218                     
219                     return;
220             });
221             this.el.cursor_changed.connect( ( ) => {
222             
223             
224                  if (this.blockChanges) { // probably not needed.. 
225                    return  ;
226                  }
227                   if (!_this.before_node_change(null) ) {
228                      this.blockChanges = true;
229                      this.el.get_selection().unselect_all();
230                      this.blockChanges = false;
231                      return;
232                  }
233                  if (_this.model.file == null) {
234                      return;
235                  } 
236                  
237                  //var render = this.get('/LeftTree').getRenderer();                
238                 print("LEFT TREE -> view -> selection changed called\n");
239                 
240                 
241                 // -- it appears that the selection is not updated.
242                 
243                 GLib.Timeout.add_full(GLib.Priority.DEFAULT,10 , () => {
244                      
245             
246                         if (this.el.get_selection().count_selected_rows() < 1) {
247             
248                             print("selected rows < 1\n");
249                             //??this.model.load( false);
250                             _this.node_selected(null);
251                             
252                             return false ;
253                         }
254                             
255                             //console.log('changed');
256                         var s = this.el.get_selection();
257                          Gtk.TreeIter iter;
258                          Gtk.TreeModel mod;
259                         s.get_selected(out mod, out iter);
260                         
261                         
262                         // var val = "";
263                         GLib.Value value;
264                         _this.model.el.get_value(iter, 2, out value);
265                         _this.model.activePath = mod.get_path(iter).to_string();
266                         
267                         var node = (JsRender.Node)value.dup_object();
268                         _this.node_selected(node);
269                         while (Gtk.events_pending()) {
270                             Gtk.main_iteration();
271                         }
272                         var cp = mod.get_path(iter);
273                         Gtk.TreePath sp, ep;
274                         this.el.get_visible_range(out sp, out ep);
275                         // if sp is before cp then retuns 1.
276                         // if cp is before ep then retuns 1.
277                         if (cp.compare(sp) >= 0 && ep.compare(cp) >=1) {
278                             return false;
279                         }
280                         
281                          
282                         
283                         this.el.scroll_to_cell(new Gtk.TreePath.from_string(_this.model.activePath), null, true, 0.1f,0.0f);
284                         
285                         return false;
286                   });  
287                 //_this.after_node_change(node);
288             
289             //        _this.model.file.changed(node, "tree");
290                
291                 //Seed.print( value.get_string());
292                 return  ;
293                             
294             });
295             this.el.drag_end.connect( (drag_context) => {
296                 //Seed.print('LEFT-TREE: drag-end');
297                     this.dragData = "";
298                     this.dropList = null;
299             //        this.targetData = "";
300                     this.highlightDropPath("",0);
301             //        return true;
302             });
303             this.el.drag_motion.connect( ( ctx, x, y, time)  => {
304                print("got drag motion\n");
305                 var src = Gtk.drag_get_source_widget(ctx);
306                this.drag_x = x;
307                this.drag_y = y;     
308             
309                if (src != this.el) {
310                
311              
312              
313                 // the point of this is to detect where an item could be dropped..
314                     print("requesting drag data\n");
315                    this.drag_in_motion = true;
316                    
317                         // request data that will be recieved by the recieve...              
318                     Gtk.drag_get_data
319                     (
320                             this.el,         // will receive 'drag-data-received' signal 
321                             ctx,        // represents the current state of the DnD 
322                             Gdk.Atom.intern("STRING",true),    // the target type we want 
323                             time            // time stamp 
324                     );
325                     return true;
326               }    
327             
328             
329               print("action: %d\n", ctx.get_actions());
330              //print("GETTING POS");
331                 var  targetData = "";
332             
333                 Gtk.TreePath path;
334                 Gtk.TreeViewDropPosition pos;
335                 var isOver = _this.view.el.get_dest_row_at_pos(this.drag_x,this.drag_y, out path, out pos);
336             
337                 // if there are not items in the tree.. the we have to set isOver to true for anything..
338                 var isEmpty = false;
339                 if (_this.model.el.iter_n_children(null) < 1) {
340                     print("got NO children?\n");
341                     isOver = true; //??? 
342                     isEmpty = true;
343                     pos = Gtk.TreeViewDropPosition.INTO_OR_AFTER;
344                 }
345             
346             
347                 // ------------- a drag from self..
348             
349             
350                 //var action = Gdk.DragAction.COPY;
351                     // unless we are copying!!! ctl button..
352                 
353                 var action = (ctx.get_actions() & Gdk.DragAction.MOVE) > 0 ?
354                              Gdk.DragAction.COPY  : Gdk.DragAction.MOVE ;
355                             // Gdk.DragAction.MOVE : Gdk.DragAction.COPY ;
356             
357             
358                 if (_this.model.el.iter_n_children(null) < 1) {
359                     // no children.. -- asume it's ok..
360                     
361                     targetData = "|%d|".printf((int)Gtk.TreeViewDropPosition.INTO_OR_AFTER);
362                        
363                     this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);        
364                     Gdk.drag_status(ctx, action ,time);
365                     return true;
366                     
367                     // continue through to allow drop...
368             
369                 } 
370                     
371                     
372             
373                 
374                 
375                 //print("ISOVER? " + isOver);
376                 if (!isOver) {
377               
378                     Gdk.drag_status(ctx, 0 ,time);
379                      this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);                    
380                      return false;
381             
382                 }
383                         
384                 // drag node is parent of child..
385                 //console.log("SRC TREEPATH: " + src.treepath);
386                 //console.log("TARGET TREEPATH: " + data.path.to_string());
387                 
388                 // nned to check a  few here..
389                 //Gtk.TreeViewDropPosition.INTO_OR_AFTER
390                 //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
391                 //Gtk.TreeViewDropPosition.AFTER
392                 //Gtk.TreeViewDropPosition.BEFORE
393                 
394                 // locally dragged items to not really use the 
395                 var selection_text = this.dragData;
396                 
397                         
398                         
399                 if (selection_text == null || selection_text.length < 1) {
400                             //print("Error  - drag selection text returned NULL");
401                          Gdk.drag_status(ctx, 0 ,time);
402                         this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
403                          return false;
404                  }
405                                    
406                         
407                         // see if we are dragging into ourself?
408                 var target_path = path.to_string();            
409                 print ("Drag  %s onto %s--%d\n ", selection_text, target_path, pos);
410                 
411                 // pos : 3 = ontop - 0 = after, 1 = before
412                 //print("target_path="+target_path);
413             
414                 // 
415                 if (selection_text  == target_path) {
416                     print("self drag ?? == we should perhaps allow copy onto self..\n");
417                             
418                      Gdk.drag_status(ctx, 0 ,time);
419                       this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
420                       return false;
421             //                 -- fixme -- this is not really correct..
422             
423                 }
424                         
425                 // check that 
426                 //print("DUMPING DATA");
427                 //console.dump(data);
428                 // path, pos
429                 
430                 //print(data.path.to_string() +' => '+  data.pos);
431                 
432                 // dropList is a list of xtypes that this node could be dropped on.
433                 // it is set up when we start to drag..
434                 
435                 
436                 targetData = _this.model.findDropNodeByPath( path.to_string(), this.dropList, pos);
437                     
438                 print("targetDAta: " + targetData +"\n");
439                 
440                 if (targetData.length < 1) {
441                     //print("Can not find drop node path");
442                    
443                     Gdk.drag_status(ctx, 0, time);
444                     this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
445                     return false;
446                 }
447                 
448                 var td_ar = targetData.split("|");
449                   
450                 
451             
452                 Gdk.drag_status(ctx, action ,time);
453                 this.highlightDropPath(td_ar[0], (Gtk.TreeViewDropPosition)int.parse(td_ar[1]));
454                 return true;
455                    
456                    
457             });
458             this.el.drag_data_get.connect( ( drag_context, data, info, time) => {
459                         
460                         
461                              //print("drag-data-get");
462                              var s = this.el.get_selection();
463                              if (s.count_selected_rows() < 1) {
464                                     data.set_text("",0);     
465                                      print("return empty string - no selection..");
466                                     return;
467                                 }
468                              
469                              Gtk.TreeIter iter;
470                              Gtk.TreeModel mod;
471                              
472                              s.get_selected(out mod, out iter);
473                              
474                             
475                             
476                              GLib.Value value;
477                              _this.model.el.get_value(iter, 2, out value);
478                              var ndata = (JsRender.Node)(value.dup_object());
479                              
480                             
481                             
482                             var tp = mod.get_path(iter).to_string();
483                             // by default returns the path..
484                             
485                            if ( info != Gdk.Atom.intern("STRING",true) ) {
486                                 tp = ndata.toJsonString();
487                            }   
488                            
489                            //data.set_text(tp,tp.length);   
490                             
491                             data.set (data.get_target (), 8, (uchar[]) tp.to_utf8 ());
492                         
493                             
494                            //  print("return " + tp);
495                         });
496             this.el.drag_data_received.connect( (ctx, x, y, sel, info, time)  => {
497               
498                     // THIS CODE ONLY RELATES TO drag  or drop of "NEW" elements or "FROM another tree.."
499               
500               
501                     //  print("Tree: drag-data-received\n");
502                     var selection_text = (string)sel.get_data();
503                     //print("selection_text= %s\n",selection_text);
504             
505                     var is_drag = this.drag_in_motion;
506                 
507                     
508             
509                     print("Is Drag %s\n", is_drag ? "Y": "N");
510                     var  targetData = "";
511                     
512                     Gtk.TreePath path;
513                     Gtk.TreeViewDropPosition pos;
514                     var isOver = _this.view.el.get_dest_row_at_pos(this.drag_x,this.drag_y, out path, out pos);
515                     
516                     // if there are not items in the tree.. the we have to set isOver to true for anything..
517                     var isEmpty = false;
518                     if (_this.model.el.iter_n_children(null) < 1) {
519                         print("got NO children?\n");
520                         isOver = true; //??? 
521                         isEmpty = true;
522                         pos = Gtk.TreeViewDropPosition.INTO_OR_AFTER;
523                     }
524                     
525                  
526                     //console.log("LEFT-TREE: drag-motion");
527                     var src = Gtk.drag_get_source_widget(ctx);
528                     
529                     // a drag from self - this should be handled by drop and motion.
530                     if (src == this.el) {
531                         print("Source == this element should not happen.. ? \n");
532                         return;
533                     }
534                     //print("drag_data_recieved from another element");
535                     
536                      
537                     
538                     
539                     if (selection_text == null || selection_text.length < 1 || !isOver) {
540                         // nothing valid foudn to drop...
541                            print("empty sel text or not over");
542                         if (is_drag) {
543                             Gdk.drag_status(ctx, 0, time);
544                             this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
545                             return;
546                         }
547                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
548                         // no drop action...
549                         return;            
550                     
551                     }
552                     var dropNode = new JsRender.Node(); 
553                     
554                     var dropNodeType  = selection_text;
555                     var show_templates = true;
556                     // for drop
557                     if (dropNodeType[0] == '{') {
558                         var pa = new Json.Parser();
559                         try {
560                             pa.load_from_data(dropNodeType);
561                         } catch (Error e) {
562                             Gtk.drag_finish (ctx, false, false, time);        // drop failed..
563                             // no drop action...
564                             return;   
565                         }
566                          
567                         dropNode.loadFromJson( pa.get_root().get_object(), 2);
568                         dropNodeType = dropNode.fqn();
569                         show_templates = false;
570                         
571                         
572                     } else {
573             
574                         dropNode.setFqn(selection_text);
575                     }
576             
577                      
578                     // dropList --- need to gather this ... 
579                     print("get dropList for : %s\n",dropNodeType);            
580                     var dropList = _this.model.file.palete().getDropList(dropNodeType);
581                     
582                     print("dropList: %s\n", string.joinv(" , ", dropList));
583                     
584                     // if drag action is link ... then we can drop it anywahere...
585                      if ((ctx.get_actions() & Gdk.DragAction.LINK) > 0) {
586                          // if path is null?? dragging into an empty tree?
587                          targetData = (path == null ? "" :  path.to_string()) + "|%d".printf((int)pos);
588                      } else {
589                     
590                     
591                         targetData = _this.model.findDropNodeByPath( isEmpty ? "" : path.to_string(), dropList, pos);
592                      }
593                     
594                     
595                         
596                     print("targetDAta: " + targetData +"\n");
597                     
598                     if (targetData.length < 1) {
599                      
600                         // invalid drop path..
601                         if (this.drag_in_motion) {
602                             Gdk.drag_status(ctx, 0, time);
603                             this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
604                             return;
605                         }
606                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
607                         // no drop action...
608                         return;
609                     }
610                     // valid drop path..
611                     
612                       var td_ar = targetData.split("|");
613                       
614                     
615                     if (this.drag_in_motion) { 
616                         Gdk.drag_status(ctx, Gdk.DragAction.COPY ,time);
617             
618                         this.highlightDropPath(  td_ar[0]  , (Gtk.TreeViewDropPosition)int.parse(td_ar[1]));
619                         return;
620                     }
621                     // continue on to allow drop..
622                 
623             
624                     // at this point, drag is not in motion... -- as checked above... - so it's a real drop event..
625                     
626             
627                     _this.model.dropNode(targetData, dropNode, show_templates);
628                     print("ADD new node!!!\n");
629                         
630                     ///Xcls_DialogTemplateSelect.singleton().show( _this.model.file.palete(), node);
631                     
632                     Gtk.drag_finish (ctx, false, false,time);
633                     
634                     
635                         
636                         
637                   
638             });
639             this.el.drag_drop.connect( (  ctx, x, y, time)  => {
640                   //Seed.print("TARGET: drag-drop");
641                
642                
643                 var src = Gtk.drag_get_source_widget(ctx);
644                  
645                if (src != this.el) {
646                
647                 
648                    
649                    this.drag_in_motion = false;   
650                         // request data that will be recieved by the recieve...              
651                     Gtk.drag_get_data
652                     (
653                             this.el,         // will receive 'drag-data-received' signal 
654                             ctx,        // represents the current state of the DnD 
655                             Gdk.Atom.intern("application/json",true),    // the target type we want 
656                             time            // time stamp 
657                     );
658             
659                      
660                     // No target offered by source => error
661                
662             
663                      return  false;
664                  }
665                  
666                  // handle drop around self..
667                  
668                               
669                         
670                 //print("GETTING POS");
671                 var  targetData = "";
672                 
673                 Gtk.TreePath path;
674                 Gtk.TreeViewDropPosition pos;
675                 var isOver = _this.view.el.get_dest_row_at_pos(this.drag_x,this.drag_y, out path, out pos);
676                 
677                 // if there are not items in the tree.. the we have to set isOver to true for anything..
678                 var isEmpty = false;
679                 if (_this.model.el.iter_n_children(null) < 1) {
680                     print("got NO children?\n");
681                     isOver = true; //??? 
682                     isEmpty = true;
683                     pos = Gtk.TreeViewDropPosition.INTO_OR_AFTER;
684                 }
685                 
686                  
687                  
688                 //var action = Gdk.DragAction.COPY;
689                     // unless we are copying!!! ctl button..
690                 
691                 var action = (ctx.get_actions() & Gdk.DragAction.MOVE) > 0 ?
692                              Gdk.DragAction.COPY  : Gdk.DragAction.MOVE ;
693                             // Gdk.DragAction.MOVE : Gdk.DragAction.COPY ;
694             
695                   
696                 if (_this.model.el.iter_n_children(null) < 1) {
697                     // no children.. -- asume it's ok..
698                     
699                     targetData = "|%d|".printf((int)Gtk.TreeViewDropPosition.INTO_OR_AFTER);
700                      
701                     // continue through to allow drop...
702             
703                 } else {
704                             
705                             
706                 
707                             
708                             
709                             //print("ISOVER? " + isOver);
710                     if (!isOver) {
711                         
712                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
713                         return true; // not over apoint!?! - no action on drop or motion..
714                     }
715                             
716                     // drag node is parent of child..
717                     //console.log("SRC TREEPATH: " + src.treepath);
718                     //console.log("TARGET TREEPATH: " + data.path.to_string());
719                     
720                     // nned to check a  few here..
721                     //Gtk.TreeViewDropPosition.INTO_OR_AFTER
722                     //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
723                     //Gtk.TreeViewDropPosition.AFTER
724                     //Gtk.TreeViewDropPosition.BEFORE
725                     
726                     // locally dragged items to not really use the 
727                     var selection_text = this.dragData;
728                     
729                     
730                     
731                     if (selection_text == null || selection_text.length < 1) {
732                         //print("Error  - drag selection text returned NULL");
733                       
734                          Gtk.drag_finish (ctx, false, false, time);        // drop failed..
735                          return true; /// -- fixme -- this is not really correct..
736                     }                
737                             
738                             // see if we are dragging into ourself?
739                             print ("got selection text of  " + selection_text);
740                     
741                     var target_path = path.to_string();
742                     //print("target_path="+target_path);
743             
744                     // 
745                     if (selection_text  == target_path) {
746                         print("self drag ?? == we should perhaps allow copy onto self..\n");
747                         
748                          Gtk.drag_finish (ctx, false, false, time);        // drop failed..
749             
750                          return true; /// -- fixme -- this is not really correct..
751             
752                     }
753                             
754                     // check that 
755                     //print("DUMPING DATA");
756                     //console.dump(data);
757                     // path, pos
758                     
759                     //print(data.path.to_string() +' => '+  data.pos);
760                     
761                     // dropList is a list of xtypes that this node could be dropped on.
762                     // it is set up when we start to drag..
763                     
764                     
765                     targetData = _this.model.findDropNodeByPath( path.to_string(), this.dropList, pos);
766                         
767                     print("targetDAta: " + targetData +"\n");
768                     
769                     if (targetData.length < 1) {
770                         //print("Can not find drop node path");
771                          
772                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
773                         return true;
774                     }
775                                 
776                             
777                             
778                             // continue on to allow drop..
779               }
780                     // at this point, drag is not in motion... -- as checked above... - so it's a real drop event..
781             
782             
783                  var delete_selection_data = false;
784                     
785                 if (action == Gdk.DragAction.ASK)  {
786                     /* Ask the user to move or copy, then set the ctx action. */
787                 }
788             
789                 if (action == Gdk.DragAction.MOVE) {
790                     delete_selection_data = true;
791                 }
792                   
793                             // drag around.. - reorder..
794                 _this.model.moveNode(targetData, action);
795                     
796                    
797                     
798                     
799                     
800                     // we can send stuff to souce here...
801             
802             
803             // do we always say failure, so we handle the reall drop?
804                 Gtk.drag_finish (ctx, false, false,time); //delete_selection_data, time);
805             
806                 return true;
807              
808              
809              
810              
811              
812              
813             });
814         }
815
816         // user defined functions
817         public           void highlightDropPath ( string treepath, Gtk.TreeViewDropPosition pos) {
818         
819                 // highlighting for drag/drop
820                 if (treepath.length > 0) {
821                     this.el.set_drag_dest_row(  new  Gtk.TreePath.from_string( treepath ), pos);
822                   } else {
823                     this.el.set_drag_dest_row(null, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
824                  }
825                      
826         }
827         public           void selectNode (string treepath_str) {
828             //this.selection.select_path(new  Gtk.TreePath.from_string( treepath_str));
829              var tp = new Gtk.TreePath.from_string(treepath_str);
830              
831              this.el.set_cursor(tp, null, false);  
832              this.el.scroll_to_cell(tp, null, false, 0,0);
833         }
834     }
835     public class Xcls_model : Object
836     {
837         public Gtk.TreeStore el;
838         private Xcls_WindowLeftTree  _this;
839
840
841             // my vars (def)
842         public DialogTemplateSelect template_select;
843         public string activePath;
844
845         // ctor
846         public Xcls_model(Xcls_WindowLeftTree _owner )
847         {
848             _this = _owner;
849             _this.model = this;
850             this.el = new Gtk.TreeStore( 3, typeof(string),typeof(string),typeof(Object) );
851
852             // my vars (dec)
853             this.template_select = null;
854             this.activePath = "";
855
856             // set gobject values
857
858             // init method
859
860             print("model initialized");
861         }
862
863         // user defined functions
864         public           string findDropNode (string treepath_str, string[] targets) {
865         
866             // this is used by the dragdrop code in the roo version AFAIR..
867         
868             //var path = treepath_str.replace(/^builder-/, '');
869             // treemap is depreciated... - should really check if model has any entries..
870         
871             if (this.el.iter_n_children(null) < 1) {
872                 //print("NO KEYS");
873                 return "|%d".printf((int)Gtk.TreeViewDropPosition.INTO_OR_AFTER);
874             }
875             //print("FIND treepath: " + path);
876             //console.dump(this.treemap);
877             
878             //if (!treepath_str.match(/^builder-/)) {
879             //    return []; // nothing!
880             //}
881             if (targets.length > 0 && targets[0] == "*") {
882                 return  treepath_str;
883             }
884             return this.findDropNodeByPath(treepath_str,targets, -1);
885         }
886         public           void loadFile (JsRender.JsRender f) {
887             //console.dump(f);
888             this.el.clear();
889             
890             // needed???
891             _this.main_window.windowstate.file = f;
892             
893            
894             if (f.tree == null) {
895                 f.loadItems( );
896             }
897             // if it's still null?
898             if (f.tree == null) {
899                 return;
900             }
901           
902             var o = new Gee.ArrayList<JsRender.Node>();
903             o.add(f.tree);
904             this.load(o,null);
905             
906             _this.view.el.expand_all();
907         
908             if (f.tree.items.size < 1) {
909                 // single item..
910                 
911                 //this.get('/Window.leftvpaned').el.set_position(80);
912                 // select first...
913                 _this.view.el.set_cursor( 
914                     new  Gtk.TreePath.from_string("0"), null, false);
915                 
916                 
917             } else {
918                   //this.get('/Window.leftvpaned').el.set_position(200);
919             }
920             
921             return;
922          
923                     
924         }
925         public    void updateSelected () {
926           
927            
928             var s = _this.view.el.get_selection();
929             
930              Gtk.TreeIter iter;
931             Gtk.TreeModel mod;
932             
933             
934             
935             if (!s.get_selected(out mod, out iter)) {
936                 return; // nothing seleted..
937             }
938           
939           GLib.Value value;
940             this.el.get_value(iter, 2, out value);
941             var node = (JsRender.Node)(value.get_object());
942             
943               this.el.set(iter, 0, node.nodeTitle(),
944                         1, node.nodeTip(), -1
945                 );
946         }
947         public           string findDropNodeByPath (string treepath_str, string[] targets, int in_pref = -1) {
948         
949             var path = treepath_str; // dupe it..
950             
951             
952             // pref : 3 = ontop - 0 = after, 1 = before
953             int pref = in_pref < 0  ?  Gtk.TreeViewDropPosition.INTO_OR_AFTER : in_pref;
954             
955             var last = "";
956             
957             //console.dump(this.treemap);
958             
959             print("findDropNodeByPath : got path length %d / %s\n", path.length, path);
960             
961             if (path.length == 0) {
962                 // top drop. // just return empty..
963                 return "|%d".printf((int)pref) ;
964                 
965             }
966             
967             
968             while (path.length > 0) {
969             
970                 if (path.length == treepath_str.length && pref != Gtk.TreeViewDropPosition.INTO_OR_AFTER) {
971                     if (path.last_index_of(":") < 0 ) {
972                         return "";
973                     }
974                     path = path.substring(0, path.last_index_of(":"));
975                     last = treepath_str;
976                     print("DROP  before or after : using %s\n",path);
977                     continue;
978                 }
979             
980                 //print("LOOKING FOR PATH: " + path);
981                 var node_data = this.pathToNode(path);
982                 
983                 if (node_data == null) {
984                     print("node not found");
985                     return "";
986                 }
987                 
988                 var xname = node_data.fqn();
989                 var match = "";
990                 var prop = "";
991                 
992                 for (var i =0; i < targets.length; i++)  {
993                     var tg = targets[i];
994                     if ((tg == xname)  ) {
995                         match = tg;
996                         break;
997                     }
998                     // if target is "xxxx:name"
999                     if (tg.contains(xname +":")) {
1000                         match = tg;
1001                         var ar = tg.split(":");
1002                         prop = ar[1];
1003                         break;
1004                     }
1005                 }
1006                 
1007                 if (match.length > 0) {
1008                     if (last.length > 0) { // pref is after/before..
1009                         // then it's after last
1010                         //if (pref > 1) {
1011                         //    return "";
1012                         //}
1013                         return last + "|%d".printf((int)pref) + "|" + prop;
1014         
1015                         
1016                     }
1017                     // we need to add prop - as :store -> needs to bee added when dropping onto.
1018                     return path + "|%d".printf( (int) Gtk.TreeViewDropPosition.INTO_OR_AFTER)  + "|" + prop;
1019                 }
1020                 /*
1021                 last = "" + path;
1022                 var par = path.split(":");
1023                 string [] ppar = {};
1024                 for (var i = 0; i < par.length-1; i++) {
1025                     ppar += par[i];
1026                 }
1027                 
1028                 path = string.joinv(":", ppar);
1029                 */
1030                 break;
1031         
1032             }
1033             
1034             return "";
1035                     
1036         }
1037         public           void moveNode (string target_data, Gdk.DragAction action) 
1038         {
1039            
1040            /// target_data = "path|pos");
1041            
1042            
1043             //print("MOVE NODE");
1044             // console.dump(target_data);
1045             Gtk.TreeIter old_iter;
1046             Gtk.TreeModel mod;
1047             
1048             var s = _this.view.el.get_selection();
1049             s.get_selected(out mod , out old_iter);
1050             mod.get_path(old_iter);
1051             
1052             var node = this.pathToNode(mod.get_path(old_iter).to_string());
1053             //console.dump(node);
1054             if (node == null) {
1055                 print("moveNode: ERROR - node is null?");
1056             }
1057             
1058             
1059         
1060             // needs to drop first, otherwise the target_data 
1061             // treepath will be invalid.
1062         
1063             
1064             if ((action & Gdk.DragAction.MOVE) > 0) {
1065                     print("REMOVING OLD NODE : " + target_data + "\n");
1066                     node.remove();
1067                     this.dropNode(target_data, node, false);
1068                     this.el.remove(ref old_iter);
1069                     
1070                     
1071                                  
1072             } else {
1073                 print("DROPPING NODE // copy: " + target_data + "\n");
1074                 node = node.deepClone();
1075                 this.dropNode(target_data, node, false);
1076             }
1077             _this.changed();
1078             this.activePath= "";
1079             //this.updateNode(false,true);
1080         }
1081         public           void load (Gee.ArrayList<JsRender.Node> tr, Gtk.TreeIter? iter) 
1082         {
1083             Gtk.TreeIter citer;
1084             //this.insert(citer,iter,0);
1085             for(var i =0 ; i < tr.size; i++) {
1086                 if (iter != null) {
1087                     this.el.insert(out citer,iter,-1); // why not append?
1088                 } else {
1089                     this.el.append(out citer,null);
1090                 }
1091                 
1092                 this.el.set(citer, 0, tr.get(i).nodeTitle(),
1093                         1, tr.get(i).nodeTip(), -1
1094                 );
1095                 var o =   GLib.Value(typeof(Object));
1096                 o.set_object((Object)tr.get(i));
1097                 
1098                 this.el.set_value(citer, 2, o);
1099                 
1100                 if (tr.get(i).items.size > 0) {
1101                     this.load(tr.get(i).items, citer);
1102                 }
1103              
1104             }
1105         
1106             
1107         }
1108         public           void deleteSelected () {
1109             
1110             print("DELETE SELECTED?");
1111             //_this.view.blockChanges = true;
1112             print("GET SELECTION?");
1113         
1114             var s = _this.view.el.get_selection();
1115             
1116             print("GET  SELECTED?");
1117            Gtk.TreeIter iter;
1118             Gtk.TreeModel mod;
1119         
1120             
1121             if (!s.get_selected(out mod, out iter)) {
1122                 return; // nothing seleted..
1123             }
1124               
1125         
1126         
1127             this.activePath= "";      
1128             print("GET  vnode value?");
1129         
1130             GLib.Value value;
1131             this.el.get_value(iter, 2, out value);
1132             var data = (JsRender.Node)(value.get_object());
1133             print("removing node from Render\n");
1134             if (data.parent == null) {
1135                 this.file.tree = null;
1136             } else {
1137                 data.remove();
1138             }
1139             print("removing node from Tree\n");    
1140             s.unselect_all();
1141             this.el.remove(ref iter);
1142         
1143             
1144             
1145             
1146             // 
1147             
1148             
1149         
1150         
1151             this.activePath= ""; // again!?!?      
1152             //this.changed(null,true);
1153             
1154             _this.changed();
1155             
1156             _this.view.blockChanges = false;
1157         }
1158         public           JsRender.Node pathToNode (string path) {
1159          
1160              
1161              Gtk.TreeIter   iter;
1162              _this.model.el.get_iter_from_string(out iter, path);
1163              
1164              GLib.Value value;
1165              _this.model.el.get_value(iter, 2, out value);
1166              
1167              return (JsRender.Node)value.dup_object();
1168         
1169         }
1170         public           void dropNode (string target_data_str, JsRender.Node node, bool show_templates) {
1171         //         print("drop Node");
1172              // console.dump(node);
1173           //    console.dump(target_data);
1174           
1175           
1176                 // 0 = before , 1=after 2/3 onto
1177           
1178           
1179                 var target_data= target_data_str.split("|");
1180           
1181                 var parent_str = target_data[0].length > 0 ? target_data[0] : "";
1182                 var pos = target_data.length > 1 ? int.parse(target_data[1]) : 2; // ontop..
1183           
1184           
1185                 Gtk.TreePath tree_path  =   parent_str.length > 0 ? new  Gtk.TreePath.from_string( parent_str ) : null;
1186                 
1187                 
1188                 
1189                 //print("add " + tp + "@" + target_data[1]  );
1190                 
1191                 JsRender.Node parentNode = null;
1192                 
1193                 Gtk.TreeIter iter_after;
1194                 Gtk.TreeIter iter_par ;
1195                 
1196                
1197                  if (target_data.length == 3 && target_data[2].length > 0) {
1198                     node.props.set("* prop", target_data[2]);
1199                 }
1200         
1201                 Gtk.TreePath expand_parent = null;
1202                 
1203                 // we only need to show the template if it's come from else where?
1204                  if (show_templates) {
1205                  
1206                      var ts = _this.main_window.windowstate.template_select;
1207                  
1208                      var new_node = ts.show(
1209                           _this.main_window, // (Gtk.Window) _this.el.get_toplevel (),
1210                               this.file.palete(),
1211                             node,
1212                             this.file.project);
1213                            
1214                      if (new_node == null) {
1215                          return; // do not add?
1216                      }
1217                      node = new_node;
1218                 }        
1219                 
1220                  //print("pos is %d  \n".printf(pos));
1221                 
1222                  Gtk.TreeIter n_iter; 
1223                  
1224                  if ( parent_str.length < 1) {
1225                       this.el.append(out n_iter, null); // drop at top level..
1226                       node.parent = null;
1227                       this.file.tree = node;
1228                       
1229                       
1230                 } else   if (pos  < 2) {
1231                     //print(target_data[1]  > 0 ? 'insert_after' : 'insert_before');
1232                     
1233                     this.el.get_iter(out iter_after, tree_path );            
1234                     this.el.iter_parent(out iter_par, iter_after);
1235                     expand_parent = this.el.get_path(iter_par);
1236                     
1237                     GLib.Value value;
1238                     this.el.get_value( iter_par, 2, out value);
1239                     parentNode =  (JsRender.Node)value.dup_object();
1240                     
1241                     
1242                     this.el.get_value( iter_after, 2, out value);
1243                     var relNode =  (JsRender.Node)value.dup_object();
1244                     
1245                     if ( pos  > 0 ) {
1246                      
1247                         this.el.insert_after(out n_iter,    iter_par  , iter_after);
1248                         var ix = parentNode.items.index_of(relNode);
1249                         parentNode.items.insert(ix+1, node);
1250                         
1251                     } else {
1252                         this.el.insert_before(out n_iter,  iter_par  , iter_after);
1253                         var ix = parentNode.items.index_of(relNode);
1254                         parentNode.items.insert(ix, node);
1255          
1256                     }
1257                     node.parent = parentNode;
1258                     
1259                     
1260                     
1261                 } else {
1262                    //  print("appending to  " + parent_str);
1263                     this.el.get_iter(out iter_par, tree_path);
1264                     this.el.append(out n_iter,   iter_par );
1265                     expand_parent = this.el.get_path(iter_par);
1266                     
1267                     GLib.Value value;
1268                     this.el.get_value( iter_par, 2, out value);
1269                     parentNode =  (JsRender.Node)value.dup_object();
1270                     node.parent = parentNode;
1271                     parentNode.items.add(node);
1272                 }
1273                 
1274                 // reparent node in tree...
1275                
1276                 
1277                 // why only on no parent???
1278                 
1279                 //if (node.parent = null) {
1280                      
1281                    
1282                     
1283                 //}
1284                 
1285                 
1286                 // work out what kind of packing to use.. -- should be in 
1287                 if (!node.has("pack")   && parent_str.length > 1) {
1288                     
1289                     this.file.palete().fillPack(node,parentNode);
1290                     
1291                     
1292                 }
1293                 
1294                 // add the node...
1295                 
1296                 this.el.set(n_iter, 0, node.nodeTitle(), 1, node.nodeTip(), -1  );
1297                 var o =   GLib.Value(typeof(Object));
1298                 o.set_object((Object)node);
1299                 
1300                 this.el.set_value(n_iter, 2, o);
1301                 
1302                 
1303                 
1304                 
1305         // load children - if it has any..
1306               
1307                 if (node.items.size > 0) {
1308                     this.load(node.items, n_iter);
1309                     _this.view.el.expand_row(this.el.get_path(n_iter), true);
1310                 } else if (expand_parent != null && !_this.view.el.is_row_expanded(expand_parent)) {
1311                    _this.view.el.expand_row(expand_parent,true);
1312                 }
1313         
1314                 //if (tp != null && (node.items.length() > 0 || pos > 1)) {
1315                 //    _this.view.el.expand_row(this.el.get_path(iter_par), true);
1316                // }
1317                 // wee need to get the empty proptypes from somewhere..
1318                 
1319                 //var olditer = this.activeIter;
1320                 this.activePath = this.el.get_path(n_iter).to_string();
1321         
1322         
1323                 
1324                 
1325                 _this.view.el.set_cursor(this.el.get_path(n_iter), null, false);
1326                 _this.changed();
1327              
1328                 
1329                     
1330         }
1331         public string treePathFromNode (JsRender.Node node) {
1332             // iterate through the tree and find the node
1333             var ret = "";
1334             
1335             this.el.foreach((mod, pth, iter) => {
1336                 // get the node..
1337               
1338              
1339                  GLib.Value value;
1340                  _this.model.el.get_value(iter, 2, out value);
1341                  
1342         
1343                  
1344                  var n = (JsRender.Node)value;
1345         
1346                  print("compare %s to %s\n", n.fqn(), node.fqn());
1347                 if (node == n) {
1348                     ret = pth.to_string();
1349                     return true;
1350                 }
1351                 return false;
1352             });
1353             return ret;
1354         
1355         }
1356     }
1357
1358     public class Xcls_TreeViewColumn4 : Object
1359     {
1360         public Gtk.TreeViewColumn el;
1361         private Xcls_WindowLeftTree  _this;
1362
1363
1364             // my vars (def)
1365
1366         // ctor
1367         public Xcls_TreeViewColumn4(Xcls_WindowLeftTree _owner )
1368         {
1369             _this = _owner;
1370             this.el = new Gtk.TreeViewColumn();
1371
1372             // my vars (dec)
1373
1374             // set gobject values
1375             this.el.title = "test";
1376             var child_0 = new Xcls_renderer( _this );
1377             child_0.ref();
1378             this.el.pack_start (  child_0.el , true );
1379
1380             // init method
1381
1382             this.el.add_attribute(_this.renderer.el , "markup", 0 );
1383         }
1384
1385         // user defined functions
1386     }
1387     public class Xcls_renderer : Object
1388     {
1389         public Gtk.CellRendererText el;
1390         private Xcls_WindowLeftTree  _this;
1391
1392
1393             // my vars (def)
1394
1395         // ctor
1396         public Xcls_renderer(Xcls_WindowLeftTree _owner )
1397         {
1398             _this = _owner;
1399             _this.renderer = this;
1400             this.el = new Gtk.CellRendererText();
1401
1402             // my vars (dec)
1403
1404             // set gobject values
1405         }
1406
1407         // user defined functions
1408     }
1409
1410
1411
1412     public class Xcls_LeftTreeMenu : Object
1413     {
1414         public Gtk.Menu el;
1415         private Xcls_WindowLeftTree  _this;
1416
1417
1418             // my vars (def)
1419
1420         // ctor
1421         public Xcls_LeftTreeMenu(Xcls_WindowLeftTree _owner )
1422         {
1423             _this = _owner;
1424             _this.LeftTreeMenu = this;
1425             this.el = new Gtk.Menu();
1426
1427             // my vars (dec)
1428
1429             // set gobject values
1430             var child_0 = new Xcls_MenuItem7( _this );
1431             child_0.ref();
1432             this.el.add (  child_0.el  );
1433             var child_1 = new Xcls_MenuItem8( _this );
1434             child_1.ref();
1435             this.el.add (  child_1.el  );
1436             var child_2 = new Xcls_MenuItem9( _this );
1437             child_2.ref();
1438             this.el.add (  child_2.el  );
1439         }
1440
1441         // user defined functions
1442     }
1443     public class Xcls_MenuItem7 : Object
1444     {
1445         public Gtk.MenuItem el;
1446         private Xcls_WindowLeftTree  _this;
1447
1448
1449             // my vars (def)
1450
1451         // ctor
1452         public Xcls_MenuItem7(Xcls_WindowLeftTree _owner )
1453         {
1454             _this = _owner;
1455             this.el = new Gtk.MenuItem();
1456
1457             // my vars (dec)
1458
1459             // set gobject values
1460             this.el.label = "Delete Element";
1461
1462             //listeners
1463             this.el.activate.connect( ( ) => {
1464                 
1465                 print("ACTIVATE?");
1466                 
1467               
1468                  _this.model.deleteSelected();
1469             });
1470         }
1471
1472         // user defined functions
1473     }
1474
1475     public class Xcls_MenuItem8 : Object
1476     {
1477         public Gtk.MenuItem el;
1478         private Xcls_WindowLeftTree  _this;
1479
1480
1481             // my vars (def)
1482
1483         // ctor
1484         public Xcls_MenuItem8(Xcls_WindowLeftTree _owner )
1485         {
1486             _this = _owner;
1487             this.el = new Gtk.MenuItem();
1488
1489             // my vars (dec)
1490
1491             // set gobject values
1492             this.el.label = "Save as Template";
1493
1494             //listeners
1495             this.el.activate.connect( () => {
1496             
1497                  DialogSaveTemplate.singleton().show(
1498                         (Gtk.Window) _this.el.get_toplevel (), 
1499                         _this.model.file.palete(), 
1500                         _this.getActiveElement()
1501                 );
1502                  
1503                 
1504             });
1505         }
1506
1507         // user defined functions
1508     }
1509
1510     public class Xcls_MenuItem9 : Object
1511     {
1512         public Gtk.MenuItem el;
1513         private Xcls_WindowLeftTree  _this;
1514
1515
1516             // my vars (def)
1517
1518         // ctor
1519         public Xcls_MenuItem9(Xcls_WindowLeftTree _owner )
1520         {
1521             _this = _owner;
1522             this.el = new Gtk.MenuItem();
1523
1524             // my vars (dec)
1525
1526             // set gobject values
1527             this.el.label = "Save as Module";
1528
1529             //listeners
1530             this.el.activate.connect( () => {
1531                 var node = _this.getActiveElement();
1532                  var name = DialogSaveModule.singleton().show(
1533                         (Gtk.Window) _this.el.get_toplevel (), 
1534                         _this.model.file.project, 
1535                         node
1536                  );
1537                  if (name.length < 1) {
1538                         return;
1539               
1540                  }
1541                  node.props.set("* xinclude", name);
1542                  node.items.clear();
1543             
1544             
1545                 var s = _this.view.el.get_selection();
1546                 
1547                 print("GET  SELECTED?");
1548                 Gtk.TreeIter iter;
1549                 Gtk.TreeModel mod;
1550             
1551                 
1552                 if (!s.get_selected(out mod, out iter)) {
1553                     return; // nothing seleted..
1554                 }
1555                 Gtk.TreeIter citer;
1556                 var n_cn = mod.iter_n_children(iter) -1;
1557                 for (var i = n_cn; i > -1; i--) {
1558                     mod.iter_nth_child(out citer, iter, i);
1559                     
1560             
1561                     print("removing node from Tree\n");    
1562                 
1563                     _this.model.el.remove(ref citer);
1564                 }
1565                 _this.changed();
1566                 _this.node_selected(node);
1567                  
1568                 
1569             });
1570         }
1571
1572         // user defined functions
1573     }
1574
1575
1576 }