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