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