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