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