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