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