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