63cbe7b76ffc63d75330f72d10028fdd1a29d0aa
[app.Builder.js] / Builder / LeftTree.js
1 //<Script type="text/javascript">
2 //<Script type="text/javascript">
3 Gio = imports.gi.Gio;
4 Gtk = imports.gi.Gtk;
5 Gdk = imports.gi.Gdk;
6 GObject = imports.gi.GObject;
7 Pango = imports.gi.Pango ;
8
9 XObject = imports.XObject.XObject;
10 console = imports.console;
11
12 // recursive imports here break!!?
13 Roo             = imports.Builder.Provider.Palete.Roo.Roo;
14 LeftTreeMenu    = imports.Builder.LeftTreeMenu.LeftTreeMenu;
15 LeftPanel       = imports.Builder.LeftPanel.LeftPanel;
16 MidPropTree     = imports.Builder.MidPropTree.MidPropTree;
17
18 RightEditor     = imports.Builder.RightEditor.RightEditor;
19 // http://www.google.com/codesearch/p?hl=en#EKZaOgYQHwo/unstable/sources/sylpheed-2.2.9.tar.bz2%7C1erxr_ilM1o/sylpheed-2.2.9/src/folderview.c&q=gtk_tree_view_get_drag_dest_row
20
21 var idSeed = 0;
22 function id(el, prefix){
23     prefix = prefix || "left-tree";
24     //el = Roo.getDom(el);
25     var ret = prefix + (++idSeed);
26     return ret;
27     //return el ? (el.id ? el.id : (el.id = id)) : id;
28 }
29
30 LeftTree = new XObject(
31 {
32         xtype: Gtk.ScrolledWindow,
33         smooth_scroll : true,
34         
35         shadow_type :  Gtk.ShadowType.IN,
36         init : function() {
37             this.targetList.add( this.atoms["STRING"], 0 , 1);
38             // will not work without changes to gir..
39            // var ta_ar = Gtk.target_table_new_from_list(this.targetList,r);
40             
41             XObject.prototype.init.call(this); 
42             this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
43             
44              
45         },
46         
47         getPaleteProvider: function ()
48         {
49             var model = this.get('model');
50             var pm = imports.Builder.Provider.ProjectManager.ProjectManager;
51             return pm.getPalete(model.file.getType());
52             
53         },
54         
55         renderView: function() // renders into the preview pane..
56         {
57             var model = this.get('model');
58             print("RENDER VIEW?" + model.file.getType());
59             switch( model.file.getType()) {
60                 case 'Roo':
61                     var RightBrowser    = imports.Builder.RightBrowser.RightBrowser;
62                     RightBrowser.get('view').renderJS(model.toJS(false,true)[0]);
63                 case 'Gtk':
64                     var RightGtkView = imports.Builder.RightGtkView.RightGtkView ;
65                     RightGtkView.renderJS(model.toJS(false,true)[0]);
66             }
67             
68         },
69         
70         atoms : {
71            "STRING" : Gdk.atom_intern("STRING")
72         },
73                         
74         targetList :  new Gtk.TargetList(),
75         
76         items : [        
77             {
78                 id : 'view',
79                 xtype: Gtk.TreeView,
80                 headers_visible :  false,
81                 enable_tree_lines :  true,
82                 tooltip_column : 0,
83                 // selection  -- set by init..
84                 init : function() {
85                     XObject.prototype.init.call(this); 
86                     
87                     var description = new Pango.FontDescription.c_new();
88                     description.set_size(8000);
89                     this.el.modify_font(description);
90                     
91                     this.selection = this.el.get_selection();
92                     this.selection.set_mode( Gtk.SelectionMode.SINGLE);
93                     this.selection.signal['changed'].connect(function() {
94                         LeftTree.get('view').listeners['cursor-changed'].apply(
95                             LeftTree.get('view'), [ LeftTree.get('view'), '']
96                         );
97                     });
98                     
99                     Gtk.drag_source_set (
100                         this.el,            /* widget will be drag-able */
101                         Gdk.ModifierType.BUTTON1_MASK,       /* modifier that will start a drag */
102                         null,            /* lists of target to support */
103                         0,              /* size of list */
104                         Gdk.DragAction.COPY   | Gdk.DragAction.MOVE           /* what to do with data after dropped */
105                     );
106                     var targets = new Gtk.TargetList();
107                     targets.add( LeftTree.atoms["STRING"], 0, 0);
108                     Gtk.drag_source_set_target_list(this.el, targets);
109
110                     Gtk.drag_source_add_text_targets(this.el); 
111                     Gtk.drag_dest_set
112                     (
113                             this.el,              /* widget that will accept a drop */
114                             Gtk.DestDefaults.MOTION  | Gtk.DestDefaults.HIGHLIGHT,
115                             null,            /* lists of target to support */
116                             0,              /* size of list */
117                             Gdk.DragAction.COPY   | Gdk.DragAction.MOVE       /* what to do with data after dropped */
118                     );
119                      
120                     Gtk.drag_dest_set_target_list(this.el, targets);
121                     Gtk.drag_dest_add_text_targets(this.el);
122                     
123                     
124                 },
125                 
126                 highlight : function(treepath_ar) {
127                     if (treepath_ar.length && treepath_ar[0].length ) {
128                         this.el.set_drag_dest_row( 
129                             new  Gtk.TreePath.from_string( treepath_ar[0] ),  treepath_ar[1]);
130                     } else {
131                         this.el.set_drag_dest_row(null, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
132                     }
133                      
134                 },
135                 
136                 selectNode : function(treepath_str) 
137                 {
138                     
139                    this.selection.select_path(new  Gtk.TreePath.from_string( treepath_str));
140                 },
141                 
142                 listeners : {
143                     
144                     
145                     
146                     'button-press-event' : function(tv, ev) {
147                         console.log("button press?");
148                         if (ev.type != Gdk.EventType.BUTTON_PRESS  || ev.button.button != 3) {
149                             Seed.print("click" + ev.type);
150                             return false;
151                         }
152                       
153                     
154                         var res = {}; 
155                         LeftTree.get('view').el.get_path_at_pos(ev.button.x,ev.button.y, res);
156                         
157                         if (!LeftTreeMenu.el)  LeftTreeMenu.init();
158                         
159                         LeftTreeMenu.el.set_screen(Gdk.Screen.get_default());
160                         LeftTreeMenu.el.show_all();
161                         LeftTreeMenu.el.popup(null, null, null, null, 3, ev.button.time);
162                         Seed.print("click:" + res.path.to_string());
163                         return false;
164                         
165                     },
166                     
167                      'drag-begin' : function (w, ctx, ud) 
168                     {
169                            // we could fill this in now...
170                         Seed.print('SOURCE: drag-begin');
171                          this.targetData = false;
172                         // find what is selected in our tree...
173                         var iter = new Gtk.TreeIter();
174                         var s = this.selection;
175                         s.get_selected(LeftTree.get('model').el, iter);
176
177                         // set some properties of the tree for use by the dropped element.
178                         var value = new GObject.Value('');
179                         LeftTree.get('model').el.get_value(iter, 2, value);
180                         var data = JSON.parse(value.value);
181                         var xname = LeftTree.get('model').file.guessName(data);
182                         
183                         this.el.dragData = xname;
184                         this.el.dropList = LeftTree.getPaleteProvider().getDropList(xname);
185                         
186
187                         // make the drag icon a picture of the node that was selected
188                         var path = LeftTree.get('model').el.get_path(iter);
189                         this.el.treepath = path.to_string();
190                         
191                         var pix = this.el.create_row_drag_icon ( path);
192                         
193                         Gtk.drag_set_icon_pixmap (ctx,
194                             pix.get_colormap(),
195                             pix,
196                             null,
197                             -10,
198                             -10);
199                         
200                         return true;
201                       
202                         
203                     },
204                     
205                     'drag-end' : function ( w,  drag_context, x, y, time, user_data)   
206                     {
207                         // i'm not sure if this would work, without implementing the whole kaboodle.
208                         Seed.print('LEFT-TREE: drag-end');
209                         this.el.dragData = false;
210                         this.el.dropList = false;
211                         this.targetData = false;
212                         LeftTree.get('view').highlight(false);
213                         return true;
214                       
215                       
216                     },
217                     'drag-motion' : function (w, ctx,  x,   y,   time, ud) 
218                     {
219                         
220                         console.log("LEFT-TREE: drag-motion");
221                         var src = Gtk.drag_get_source_widget(ctx);
222
223                         // a drag from  elsewhere...- prevent drop..
224                         if (!src.dragData) {
225                             Gdk.drag_status(ctx, 0, time);
226                             this.targetData = false;
227                             return true;
228                         }
229                         var action = Gdk.DragAction.COPY;
230                         if (src == this.el) {
231                             // unless we are copying!!! ctl button..
232                             action = Gdk.DragAction.MOVE;
233                         }
234                         var data = {};
235                         print("GETTING POS");
236                         var isOver = LeftTree.get('view').el.get_dest_row_at_pos(x,y, data);
237                         print("ISOVER? " + isOver);
238                         if (!isOver) {
239                             Gdk.drag_status(ctx, 0 ,time);
240                             return false; // not over apoint!?!
241                         }
242                         // drag node is parent of child..
243                         console.log("SRC TREEPATH: " + src.treepath);
244                         console.log("TARGET TREEPATH: " + data.path.to_string());
245                         
246                         // nned to check a  few here..
247                         //Gtk.TreeViewDropPosition.INTO_OR_AFTER
248                         //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
249                         //Gtk.TreeViewDropPosition.AFTER
250                         //Gtk.TreeViewDropPosition.BEFORE
251                         
252                         if (typeof(src.treepath) != 'undefined'  && 
253                             src.treepath == data.path.to_string().substring(0,src.treepath.length)) {
254                              Gdk.drag_status(ctx, 0 ,time);
255                             return false;
256                         }
257                         
258                         // check that 
259                         //print("DUMPING DATA");
260                         //console.dump(data);
261                         // path, pos
262                         
263                         Seed.print(data.path.to_string() +' => '+  data.pos);
264                         var tg = LeftTree.get('model').findDropNodeByPath(data.path.to_string(), src.dropList, data.pos);
265                         LeftTree.get('view').highlight(tg);
266                         if (!tg.length) {
267                             this.targetData = false;
268                             Gdk.drag_status(ctx, 0, time);
269                             return true;
270                         }
271                         //console.dump(tg);
272                         this.targetData = tg;    
273                         
274                         
275                         Gdk.drag_status(ctx, action ,time);
276                          
277                         return true;
278                     },
279                     
280                     'drag-drop'  : function (w, ctx, x, y,time, ud) 
281                     {
282                                 
283                         Seed.print("TARGET: drag-drop");
284                        
285                         Gtk.drag_get_data
286                         (
287                                 w,         /* will receive 'drag-data-received' signal */
288                                 ctx,        /* represents the current state of the DnD */
289                                 LeftTree.atoms["STRING"],    /* the target type we want */
290                                 time            /* time stamp */
291                         );
292                         
293                          
294                         /* No target offered by source => error */
295                        
296
297                         return  true;
298                         
299
300                     },
301                    'drag-data-received' : function (w, ctx,  x,  y, sel_data,  target_type,  time, ud) 
302                     {
303                         Seed.print("Tree: drag-data-received");
304                         delete_selection_data = false;
305                         dnd_success = false;
306                         /* Deal with what we are given from source */
307                         if( sel_data && sel_data.length ) {
308                             
309                             if (ctx.action == Gdk.DragAction.ASK)  {
310                                 /* Ask the user to move or copy, then set the ctx action. */
311                             }
312
313                             if (ctx.action == Gdk.DragAction.MOVE) {
314                                 //delete_selection_data = true;
315                             }
316                             
317                             var source = Gtk.drag_get_source_widget(ctx);
318  
319                             if (this.targetData) {
320                                 if (source != this.el) {
321                                     LeftTree.get('model').dropNode(this.targetData,  source.dragData);
322                                 } else {
323                                     // drag around.. - reorder..
324                                     LeftTree.get('model').moveNode(this.targetData);
325                                     
326                                     
327                                 }
328                                 //Seed.print(this.targetData);
329                               
330                             }
331                             
332                             
333                             
334                             // we can send stuff to souce here...
335
336                             dnd_success = true;
337
338                         }
339
340                         if (dnd_success == false)
341                         {
342                                 Seed.print ("DnD data transfer failed!\n");
343                         }
344
345                         Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
346                         return true;
347                     },
348                     
349                     'cursor-changed'  : function(tv, a) {
350                         var iter = new Gtk.TreeIter();
351                         
352                         if (this.selection.count_selected_rows() < 1) {
353                             LeftPanel.get('model').load( false);
354                             MidPropTree.activeElement =  false;
355                             MidPropTree.hideWin();
356                             var RightPalete     = imports.Builder.RightPalete.RightPalete;
357                             var pm = RightPalete.get('model');
358                             if (!LeftTree.getPaleteProvider()) {
359                                 // it may not be loaded yet..
360                                 return  true;
361                             }
362                             pm.load( LeftTree.getPaleteProvider().gatherList(
363                                 LeftTree.get('model').listAllTypes()));
364                            
365                             return true;
366                         }
367                         
368                         //console.log('changed');
369                         var s = this.selection;
370                         s.get_selected(LeftTree.get('model').el, iter);
371                         
372                         
373                         // var val = "";
374                         value = new GObject.Value('');
375                         LeftTree.get('model').el.get_value(iter, 2, value);
376                         LeftTree.get('model').activeIter = iter;
377                         
378                         var data = JSON.parse(value.value);
379                         MidPropTree.activeElement =  data;
380                         MidPropTree.hideWin();
381                         LeftPanel.get('model').load( data);
382                         
383                         console.log(value.value);
384                        // _g.button.set_label(''+value.get_string());
385                         var RightPalete     = imports.Builder.RightPalete.RightPalete;
386                         var pm = RightPalete.get('model');
387                         pm.load( RightPalete.provider.gatherList(
388                             LeftTree.get('model').listAllTypes()));
389                        
390                         
391                        
392                        
393                         //Seed.print( value.get_string());
394                         return true;
395                         
396                         
397                     
398                     }
399                 },
400                 
401                 items  : [
402                     {
403                         id : 'model',
404                         pack : ['set_model'],
405                         
406                         
407                         xtype: Gtk.TreeStore,
408                          
409                         init : function() {
410                             XObject.prototype.init.call(this); 
411                  
412                             
413                             this.el.set_column_types ( 3, [
414                                                     GObject.TYPE_STRING, // title 
415                                                     GObject.TYPE_STRING, // tip
416                                                     GObject.TYPE_STRING // source..
417                                                     ] );
418                             
419                             //if (LeftProjectTree.project).getProvider()
420                             
421                            
422                         },
423                         activeIter : false,
424                         
425                         
426                         changed : function( n, refresh) 
427                         {
428                             print("MODEL CHANGED CALLED" + this.activeIter);
429                             if (this.activeIter) {
430                                     
431                                 this.el.set_value(this.activeIter, 0, [GObject.TYPE_STRING, this.nodeTitle(n)]);
432                                 this.el.set_value(this.activeIter, 1, [GObject.TYPE_STRING, this.nodeTitle(n)]);
433                                 
434                                 this.el.set_value(this.activeIter, 2, [GObject.TYPE_STRING, this.nodeToJSON(n)]);
435                             }
436                                 //this.currentTree = this.toJS(false, true)[0];
437                             this.file.items = this.toJS(false, false);
438                             print("AFTER CHANGED")
439                             //console.dump(this.file.items);
440                             this.file.save();
441                             this.currentTree = this.file.items[0];
442                             //console.log(this.file.toSource());
443                             
444                             if (refresh) {
445                                 print("REDNER BROWSER?!");
446                                 LeftTree.renderView();
447                                 
448                                 var RightPalete     = imports.Builder.RightPalete.RightPalete;
449                                 var pm = RightPalete.get('model');
450                                 if (!RightPalete.provider) {
451                                     pm.load([]);
452                                     return;
453                                 }
454                                 
455                                 
456                                 pm.load( RightPalete.provider.gatherList(this.listAllTypes()));
457                                 //imports['Builder/RightBrowser.js'].renderJS(this.toJS());
458                             }
459                              
460                         },
461                         
462                         
463                         loadFile : function(f)
464                         {
465                             //console.dump(f);
466                             this.el.clear();
467                             this.file = f;
468                             
469                             if (!f) {
470                                 console.log('missing file');
471                                 return;
472                             }
473                             
474                             // load the file if not loaded..
475                             if (f.items === false) {
476                                 var _this = this;
477                                 f.loadItems(function() {
478                                     _this.loadFile(f);
479                                 });
480                                 return;
481                                 
482                             }
483                             if (f.items.length && typeof(f.items[0]) == 'string') {
484                             
485                                 RightEditor.el.show();
486                                 RightEditor.get('view').load( f.items[0]);
487                                 return;
488                             }
489                             print("LOAD");
490                             //console.dump(f.items);
491                             this.load(f.items);
492                             LeftTree.get('view').el.expand_all();
493                             var Window = imports.Builder.Window.Window;
494                             if (!f.items.length) {
495                                 // single item..
496                                 
497                                 Window.get('leftvpaned').el.set_position(80);
498                                 // select first...
499                                 LeftTree.get('view').el.set_cursor( 
500                                     new  Gtk.TreePath.from_string('0'), null, false);
501                                 
502                                 
503                             } else {
504                                   Window.get('leftvpaned').el.set_position(200);
505                             }
506                             
507                             
508                             print("hide right editior");
509                             RightEditor.el.hide();
510                             print("set current tree");
511                             this.currentTree = this.toJS(false, false)[0];
512                             //console.dump(this.currentTree);
513                             this.currentTree = this.currentTree || { items: [] };
514                             LeftTree.renderView();
515                             //console.dump(this.map);
516                             var RightPalete     = imports.Builder.RightPalete.RightPalete;
517                             var pm = RightPalete.get('model');
518                             // set up provider..
519                             
520                             RightPalete.provider = LeftTree.getPaleteProvider();
521                             
522                             if (!RightPalete.provider) {
523                                 print ("********* PALETE PROVIDER MISSING?!!");
524                             }
525                             LeftTree.renderView();
526                             
527                             pm.load( LeftTree.getPaleteProvider().gatherList(this.listAllTypes()));
528                             
529                             
530                                     
531                             Window.get('view-notebook').el.set_current_page(
532                                 LeftTree.get('model').file.getType()== 'Roo' ? 0 : -1);
533                                     
534                             
535                             
536                         },
537                         
538                         findDropNode : function (treepath_str, targets)
539                         {
540                             
541                             
542                             var path = treepath_str.replace(/^builder-/, '');
543                             
544                             if (!XObject.keys(this.treemap).length) {
545                                 print("NO KEYS");
546                                 return [ '',  Gtk.TreeViewDropPosition.INTO_OR_AFTER];
547                             }
548                             print("FIND treepath: " + path);
549                             //console.dump(this.treemap);
550                             
551                             if (!treepath_str.match(/^builder-/)) {
552                                 return []; // nothing!
553                             }
554                             if (targets === true) {
555                                 return [ path ];
556                             }
557                             return this.findDropNodeByPath(path,targets) 
558                         },
559                         findDropNodeByPath : function (treepath_str, targets, pref)
560                         {
561                             
562                             var path = treepath_str + ''; // dupe it..
563                             pref = typeof(pref) == 'undefined' ?  Gtk.TreeViewDropPosition.INTO_OR_AFTER : pref;
564                             var last = false;
565                             //console.dump(this.treemap);
566                             while (path.length) {
567                                 if (typeof(this.treemap[path]) == 'undefined') {
568                                     return [];
569                                 }
570                                 
571                                 var xname = LeftTree.get('model').file.guessName(this.treemap[path]);
572                                 var match = false;
573                                 var prop = '';
574                                 targets.forEach(function(tg) {
575                                     if (match) {
576                                         return;;
577                                     }
578                                     if ((tg == xname)  ) {
579                                         match = tg;
580                                     }
581                                     if (tg.indexOf(xname +':') === 0) {
582                                         match = tg;
583                                         prop = tg.split(':').pop();
584                                     }
585                                 });
586                                 
587                                 if (match) {
588                                     if (last) { // pref is after/before..
589                                         // then it's after last
590                                         if (pref > 1) {
591                                             return []; // do not allow..
592                                         }
593                                         return [ last, pref , prop];
594                                         
595                                     }
596                                     return [ path , Gtk.TreeViewDropPosition.INTO_OR_AFTER , prop];
597                                 }
598                                 var par = path.split(':');
599                                 last = path;
600                                 par.pop();
601                                 path = par.join(':');
602                             }
603                             return [];
604                             
605                             
606                         },
607                         
608                         dropNode: function(target_data, node) {
609                             
610                             console.dump(target_data);
611                             var tp = target_data[0].length ? new  Gtk.TreePath.from_string( target_data[0] ) : false;
612                             
613                             print("add where: " + target_data[1]  );
614                             var parent = tp;
615                             var after = false;
616                             if (target_data[1]  < 2) { // before or after..
617                                 var ar = target_data[0].split(':');
618                                 ar.pop();
619                                 parent  = new  Gtk.TreePath.from_string( ar.join(':') );
620                                 after = tp;
621                             }
622                             var n_iter = new Gtk.TreeIter();
623                             var iter_par = new Gtk.TreeIter();
624                             var iter_after = after ? new Gtk.TreeIter() : false;
625                             if (parent !== false) {
626                                 this.el.get_iter(iter_par, parent);
627                             } else {
628                                 iter_par = null;
629                             }
630                             
631                             
632                             if (after) {
633                                 Seed.print(target_data[1]  > 0 ? 'insert_after' : 'insert_before');
634                                 this.el.get_iter(iter_after, after);
635                                 this.el[ target_data[1]  > 0 ? 'insert_after' : 'insert_before'](
636                                         n_iter, iter_par, iter_after);
637                                 
638                             } else {
639                                 this.el.append(n_iter, iter_par);
640                                 
641                             }
642                             
643                             if (typeof(node) == 'string') {
644                                 var ar = node.split('.');
645                                 var xtype = ar.pop();
646                                 
647                                 node = {
648                                     '|xns' : ar.join('.'),
649                                     'xtype' : xtype
650                                 };
651                                 if (target_data.length == 3 && target_data[2].length) {
652                                     node['*prop'] = target_data[2];
653                                 }
654                                 
655                             }
656                             var xitems = [];
657                             if (node.items) {
658                                 xitems = node.items;
659                                 delete node.items;
660                             }
661                             if (xitems) {
662                                 this.load(xitems, n_iter);
663                             }
664                             if (xitems || after) {
665                                 LeftTree.get('view').el.expand_row(this.el.get_path(iter_par), true);
666                             }
667                             // wee need to get the empty proptypes from somewhere..
668                             
669                             //var olditer = this.activeIter;
670                             this.activeIter = n_iter;
671                             this.changed(node, true);
672                             
673                             
674                             
675                             LeftTree.get('view').el.set_cursor(this.el.get_path(n_iter), null, false);
676                             
677                             //Builder.MidPropTree._model.load(node);
678                             //Builder.MidPropTree._win.hideWin();
679                             //Builder.LeftPanel._model.load( node);
680                             
681                             
682                             
683                             
684                         },
685                         moveNode: function(target_data) {
686                             
687                             //print("MOVE NODE");
688                            // console.dump(target_data);
689                             var old_iter = new Gtk.TreeIter();
690                             var s = LeftTree.get('view').selection;
691                             s.get_selected(this.el, old_iter);
692                             var node = this.nodeToJS(old_iter,false);
693                             //console.dump(node);
694                             
695                             
696                             // needs to drop first, otherwise the target_data 
697                             // treepath will be invalid.
698                             
699                             this.dropNode(target_data, node);
700                             this.el.remove(old_iter);
701                             
702                             
703                         },
704                         
705                         deleteSelected: function() {
706                             
707                             
708                             
709                             var old_iter = new Gtk.TreeIter();
710                             var s = LeftTree.get('view').selection;
711                             s.get_selected(this.el, old_iter);
712                             s.unselect_all();
713                             
714                             this.el.remove(old_iter);
715                             
716                             // rebuild treemap.
717                             this.map = {};
718                             this.treemap = { };
719                             //this.toJS(null, true) // does not do anything?
720                             this.activeIter = false;
721                             this.changed(false,true);
722                             
723                             
724                             
725                         },    
726                         
727                         
728                         currentTree  : false,
729                          
730                         treemap: false, // map of treepath to nodes.
731                         
732                         listAllTypes : function()
733                         {
734                             
735                             
736                             var s = LeftTree.get('view').selection;
737                             print ("LIST ALL TYPES: " + s.count_selected_rows() );
738                             
739                             if (s.count_selected_rows() > 0) {
740                                 var iter = new Gtk.TreeIter();    
741                                 s.get_selected(LeftTree.get('model').el, iter);
742
743                                 // set some properties of the tree for use by the dropped element.
744                                 var value = new GObject.Value('');
745                                 LeftTree.get('model').el.get_value(iter, 2, value);
746                                 var data = JSON.parse(value.value);
747                                 
748                                 
749                                 var xname = LeftTree.get('model').file.guessName(data);
750                                 console.log('selected:' + xname);
751                                 if (xname.length) {
752                                     return [ xname ];
753                                 }
754                                 return []; // could not find it..
755                             }
756                             
757                             var ret = [ ];
758                             
759                             function addall(li)
760                             {
761                                 li.forEach(function(el) {
762                                     // this is specific to roo!!!?
763                                     
764                                     var fullpath =  LeftTree.get('model').file.guessName(el);
765                                     if (fullpath.length && ret.indexOf(fullpath) < 0) {
766                                         ret.push(fullpath);
767                                     }
768                                     
769                                     
770                                     if (el.items && el.items.length) {
771                                         addall(el.items);
772                                     }
773                                     
774                                 })
775                                 
776                                 
777                             }
778                             
779                             addall([this.currentTree]);
780                             
781                             // only if we have nothing, should we add '*top'
782                             if (!ret.length) {
783                                 ret = [ '*top' ];
784                             }
785                             console.log('all types in tree');
786                             console.dump(ret);
787                             
788                             return ret;
789                             
790                             
791                         },
792                         
793                         
794                         /**
795                          * convert tree into a javascript array
796                          * 
797                          */
798                         nodeToJS: function (iter, with_id) 
799                         {
800                             var par = new Gtk.TreeIter(); 
801                             var iv = this.getValue(iter, 2);
802                            // print("IV" + iv);
803                             var k = JSON.parse(iv);
804                             if (k.json && !this.el.iter_parent( par, iter  )) {
805                                 delete k.json;
806                             }
807                             
808                             if (with_id) {
809                                 var treepath_str = this.el.get_path(iter).to_string();
810                                 k.id =  'builder-'+ treepath_str ;
811                                
812                                 this.treemap[  treepath_str ] = k;
813                                 k.xtreepath = treepath_str ;
814                                 
815                             }
816                             if (this.el.iter_has_child(iter)) {
817                                 citer = new Gtk.TreeIter();
818                                 this.el.iter_children(citer, iter);
819                                 k.items = this.toJS(citer,with_id);
820                             }
821                             return k;
822                         },
823                          /**
824                           * iterates through child nodes (or top..)
825                           * 
826                           */
827                         toJS: function(iter, with_id)
828                         {
829                             Seed.print("WITHID: "+ with_id);
830                             
831                             var first = false;
832                             if (!iter) {
833                                 
834                                 this.treemap = { }; 
835                                 
836                                 iter = new Gtk.TreeIter();
837                                 if (!this.el.get_iter_first(iter)) {
838                                     return [];
839                                 }
840                                 first = true;
841                             } 
842                             
843                             var ar = [];
844                                
845                             while (true) {
846                                 
847                                 var k = this.nodeToJS(iter, with_id); 
848                                 ar.push(k);
849                                 
850                                 
851                                 if (!this.el.iter_next(iter)) {
852                                     break;
853                                 }
854                             }
855                             
856                             return ar;
857                             // convert the list into a json string..
858                         
859                             
860                         },
861                         getValue: function (iter, col) {
862                             var gval = new GObject.Value('');
863                             this.el.get_value(iter, col ,gval);
864                             return  gval.value;
865                             
866                             
867                         },
868                         
869                         nodeTitle: function(c)
870                         {
871                               
872                             var txt = [];
873                             c = c || {};
874                             var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
875                             if (sr) txt.push('<s>');
876                             if (typeof(c['*prop']) != 'undefined')   { txt.push(c['*prop']+ ':'); }
877                             if (c.xtype)      { txt.push(c.xtype); }
878                             if (c.id)      { txt.push('<b>[id=' + c.id + ']</b>'); }
879                             if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
880                             if (c.boxLabel)   { txt.push('[' + c.boxLabel + ']'); }
881                             
882                             
883                             if (c.layout)     { txt.push('<i>' + c.layout + '</i>'); }
884                             if (c.title)      { txt.push('<b>' + c.title + '</b>'); }
885                             if (c.label)      { txt.push('<b>' + c.label+ '</b>'); }
886                             if (c.header)    { txt.push('<b>' + c.header + '</b>'); }
887                             if (c.legend)      { txt.push('<b>' + c.legend + '</b>'); }
888                             if (c.text)       { txt.push('<b>' + c.text + '</b>'); }
889                             if (c.name)       { txt.push('<b>' + c.name+ '</b>'); }
890                             if (c.region)     { txt.push('<i>(' + c.region + ')</i>'); }
891                             if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
892                             
893                             // for flat classes...
894                             if (typeof(c['*class']) != 'undefined')  { txt.push('<b>' +  c['*class']+  '</b>'); }
895                             if (typeof(c['*extends']) != 'undefined')  { txt.push(': <i>' +  c['*extends']+  '</i>'); }
896                             
897                             
898                             if (sr) txt.push('</s>');
899                             return (txt.length == 0 ? "Element" : txt.join(" "));
900                         
901                       //console.log(n.xtype);
902                            // return n.xtype;
903                         },
904                         
905                         nodeToJSON : function(c) {
906                             var o  = {}
907                             for (var i in c) {
908                                 if (i == 'items') {
909                                      continue;
910                                 }
911                                 o[i] = c[i];
912                             }
913                             return JSON.stringify(o);
914                         },
915                         /**
916                          * load javascript array onto an iter..
917                          * @param tr = array of elements
918                          * @param iter = iter of parent (or null if not..)
919                          */
920                         
921                         
922                          
923                         
924                         load : function(tr,iter)
925                         {
926                             var citer = new Gtk.TreeIter();
927                             //this.insert(citer,iter,0);
928                             for(var i =0 ; i < tr.length; i++) {
929                                 if (iter) {
930                                     this.el.insert(citer,iter,-1);
931                                 } else {
932                                     this.el.append(citer);
933                                 }
934                                 
935                                 this.el.set_value(citer, 0, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
936                                 this.el.set_value(citer, 1, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
937                                 this.el.set_value(citer, 2, [GObject.TYPE_STRING, this.nodeToJSON(tr[i])]);
938                                 if (tr[i].items && tr[i].items.length) {
939                                     this.load(tr[i].items, citer);
940                                 }
941                             }
942                             
943                             
944                             
945                             
946                         },
947                         
948                         
949                         
950                       //  this.expand_all();
951                     },
952                     
953                       
954                     {
955                         xtype: Gtk.TreeViewColumn,
956                         pack : ['append_column'],
957                         init : function(){
958                             XObject.prototype.init.call(this); 
959                             this.el.add_attribute(this.items[0].el , 'markup', 0 );
960                            
961                         },
962                         items : [
963                             {
964                                 
965                                 xtype: Gtk.CellRendererText,
966                                 pack: [ 'pack_start']
967                                   
968                             } 
969                         ],
970                      
971                       
972                     }
973                     
974                ]
975             }
976         ]
977                 
978          
979     }
980 );
981