1 //<Script type="text/javascript">
2 //<Script type="text/javascript">
6 GObject = imports.gi.GObject;
7 Pango = imports.gi.Pango ;
9 XObject = imports.XObject.XObject;
10 console = imports.console;
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;
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
22 function id(el, prefix){
23 prefix = prefix || "left-tree";
24 //el = Roo.getDom(el);
25 var ret = prefix + (++idSeed);
27 //return el ? (el.id ? el.id : (el.id = id)) : id;
30 LeftTree = new XObject(
32 xtype: Gtk.ScrolledWindow,
35 shadow_type : Gtk.ShadowType.IN,
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);
41 XObject.prototype.init.call(this);
42 this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
47 getPaleteProvider: function ()
49 var model = this.get('model');
50 var pm = imports.Builder.Provider.ProjectManager.ProjectManager;
51 return pm.getPalete(model.file.getType());
55 renderView: function() // renders into the preview pane..
57 var model = this.get('model');
58 print("RENDER VIEW?" + model.file.getType());
59 switch( model.file.getType()) {
61 var RightBrowser = imports.Builder.RightBrowser.RightBrowser;
62 RightBrowser.get('view').renderJS(model.toJS(false,true)[0]);
64 var RightGtkView = imports.Builder.RightGtkView.RightGtkView ;
65 RightGtkView.renderJS(model.toJS(false,true)[0]);
71 "STRING" : Gdk.atom_intern("STRING")
74 targetList : new Gtk.TargetList(),
80 headers_visible : false,
81 enable_tree_lines : true,
83 // selection -- set by init..
85 XObject.prototype.init.call(this);
87 var description = new Pango.FontDescription.c_new();
88 description.set_size(8000);
89 this.el.modify_font(description);
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'), '']
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 */
106 var targets = new Gtk.TargetList();
107 targets.add( LeftTree.atoms["STRING"], 0, 0);
108 Gtk.drag_source_set_target_list(this.el, targets);
110 Gtk.drag_source_add_text_targets(this.el);
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 */
120 Gtk.drag_dest_set_target_list(this.el, targets);
121 Gtk.drag_dest_add_text_targets(this.el);
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]);
131 this.el.set_drag_dest_row(null, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
136 selectNode : function(treepath_str)
139 this.selection.select_path(new Gtk.TreePath.from_string( treepath_str));
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);
155 LeftTree.get('view').el.get_path_at_pos(ev.button.x,ev.button.y, res);
157 if (!LeftTreeMenu.el) LeftTreeMenu.init();
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());
167 'drag-begin' : function (w, ctx, ud)
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);
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);
183 this.el.dragData = xname;
184 this.el.dropList = LeftTree.getPaleteProvider().getDropList(xname);
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();
191 var pix = this.el.create_row_drag_icon ( path);
193 Gtk.drag_set_icon_pixmap (ctx,
205 'drag-end' : function ( w, drag_context, x, y, time, user_data)
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);
217 'drag-motion' : function (w, ctx, x, y, time, ud)
220 console.log("LEFT-TREE: drag-motion");
221 var src = Gtk.drag_get_source_widget(ctx);
223 // a drag from elsewhere...- prevent drop..
225 print("no drag data!");
226 Gdk.drag_status(ctx, 0, time);
227 this.targetData = false;
230 var action = Gdk.DragAction.COPY;
231 if (src == this.el) {
232 // unless we are copying!!! ctl button..
233 action = Gdk.DragAction.MOVE;
236 print("GETTING POS");
237 var isOver = LeftTree.get('view').el.get_dest_row_at_pos(x,y, data);
238 print("ISOVER? " + isOver);
240 Gdk.drag_status(ctx, 0 ,time);
241 return false; // not over apoint!?!
243 // drag node is parent of child..
244 console.log("SRC TREEPATH: " + src.treepath);
245 console.log("TARGET TREEPATH: " + data.path.to_string());
247 // nned to check a few here..
248 //Gtk.TreeViewDropPosition.INTO_OR_AFTER
249 //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
250 //Gtk.TreeViewDropPosition.AFTER
251 //Gtk.TreeViewDropPosition.BEFORE
253 if (typeof(src.treepath) != 'undefined' &&
254 src.treepath == data.path.to_string().substring(0,src.treepath.length)) {
255 print("subpath drag");
256 Gdk.drag_status(ctx, 0 ,time);
261 //print("DUMPING DATA");
262 //console.dump(data);
265 Seed.print(data.path.to_string() +' => '+ data.pos);
266 var tg = LeftTree.get('model').findDropNodeByPath(data.path.to_string(), src.dropList, data.pos);
267 LeftTree.get('view').highlight(tg);
269 this.targetData = false;
270 Gdk.drag_status(ctx, 0, time);
274 this.targetData = tg;
277 Gdk.drag_status(ctx, action ,time);
282 'drag-drop' : function (w, ctx, x, y,time, ud)
285 Seed.print("TARGET: drag-drop");
289 w, /* will receive 'drag-data-received' signal */
290 ctx, /* represents the current state of the DnD */
291 LeftTree.atoms["STRING"], /* the target type we want */
292 time /* time stamp */
296 /* No target offered by source => error */
303 'drag-data-received' : function (w, ctx, x, y, sel_data, target_type, time, ud)
305 Seed.print("Tree: drag-data-received");
306 delete_selection_data = false;
308 /* Deal with what we are given from source */
309 if( sel_data && sel_data.length ) {
311 if (ctx.action == Gdk.DragAction.ASK) {
312 /* Ask the user to move or copy, then set the ctx action. */
315 if (ctx.action == Gdk.DragAction.MOVE) {
316 //delete_selection_data = true;
319 var source = Gtk.drag_get_source_widget(ctx);
321 if (this.targetData) {
322 if (source != this.el) {
323 LeftTree.get('model').dropNode(this.targetData, source.dragData);
325 // drag around.. - reorder..
326 LeftTree.get('model').moveNode(this.targetData);
330 //Seed.print(this.targetData);
336 // we can send stuff to souce here...
342 if (dnd_success == false)
344 Seed.print ("DnD data transfer failed!\n");
347 Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
351 'cursor-changed' : function(tv, a) {
352 var iter = new Gtk.TreeIter();
354 if (this.selection.count_selected_rows() < 1) {
355 LeftPanel.get('model').load( false);
356 MidPropTree.activeElement = false;
357 MidPropTree.hideWin();
358 var RightPalete = imports.Builder.RightPalete.RightPalete;
359 var pm = RightPalete.get('model');
360 if (!LeftTree.getPaleteProvider()) {
361 // it may not be loaded yet..
364 pm.load( LeftTree.getPaleteProvider().gatherList(
365 LeftTree.get('model').listAllTypes()));
370 //console.log('changed');
371 var s = this.selection;
372 s.get_selected(LeftTree.get('model').el, iter);
376 value = new GObject.Value('');
377 LeftTree.get('model').el.get_value(iter, 2, value);
378 LeftTree.get('model').activeIter = iter;
380 var data = JSON.parse(value.value);
381 MidPropTree.activeElement = data;
382 MidPropTree.hideWin();
383 LeftPanel.get('model').load( data);
385 console.log(value.value);
386 // _g.button.set_label(''+value.get_string());
387 var RightPalete = imports.Builder.RightPalete.RightPalete;
388 var pm = RightPalete.get('model');
389 pm.load( RightPalete.provider.gatherList(
390 LeftTree.get('model').listAllTypes()));
395 //Seed.print( value.get_string());
406 pack : ['set_model'],
409 xtype: Gtk.TreeStore,
412 XObject.prototype.init.call(this);
415 this.el.set_column_types ( 3, [
416 GObject.TYPE_STRING, // title
417 GObject.TYPE_STRING, // tip
418 GObject.TYPE_STRING // source..
421 //if (LeftProjectTree.project).getProvider()
428 changed : function( n, refresh)
430 print("MODEL CHANGED CALLED" + this.activeIter);
431 if (this.activeIter) {
433 this.el.set_value(this.activeIter, 0, [GObject.TYPE_STRING, this.nodeTitle(n)]);
434 this.el.set_value(this.activeIter, 1, [GObject.TYPE_STRING, this.nodeTitle(n)]);
436 this.el.set_value(this.activeIter, 2, [GObject.TYPE_STRING, this.nodeToJSON(n)]);
438 //this.currentTree = this.toJS(false, true)[0];
439 this.file.items = this.toJS(false, false);
440 print("AFTER CHANGED")
441 //console.dump(this.file.items);
443 this.currentTree = this.file.items[0];
444 //console.log(this.file.toSource());
447 print("REDNER BROWSER?!");
448 LeftTree.renderView();
450 var RightPalete = imports.Builder.RightPalete.RightPalete;
451 var pm = RightPalete.get('model');
452 if (!RightPalete.provider) {
458 pm.load( RightPalete.provider.gatherList(this.listAllTypes()));
459 //imports['Builder/RightBrowser.js'].renderJS(this.toJS());
465 loadFile : function(f)
472 console.log('missing file');
476 // load the file if not loaded..
477 if (f.items === false) {
479 f.loadItems(function() {
485 if (f.items.length && typeof(f.items[0]) == 'string') {
487 RightEditor.el.show();
488 RightEditor.get('view').load( f.items[0]);
492 //console.dump(f.items);
494 LeftTree.get('view').el.expand_all();
495 var Window = imports.Builder.Window.Window;
496 if (!f.items.length) {
499 Window.get('leftvpaned').el.set_position(80);
501 LeftTree.get('view').el.set_cursor(
502 new Gtk.TreePath.from_string('0'), null, false);
506 Window.get('leftvpaned').el.set_position(200);
510 print("hide right editior");
511 RightEditor.el.hide();
512 print("set current tree");
513 this.currentTree = this.toJS(false, false)[0];
514 //console.dump(this.currentTree);
515 this.currentTree = this.currentTree || { items: [] };
516 LeftTree.renderView();
517 //console.dump(this.map);
518 var RightPalete = imports.Builder.RightPalete.RightPalete;
519 var pm = RightPalete.get('model');
522 RightPalete.provider = LeftTree.getPaleteProvider();
524 if (!RightPalete.provider) {
525 print ("********* PALETE PROVIDER MISSING?!!");
527 LeftTree.renderView();
529 pm.load( LeftTree.getPaleteProvider().gatherList(this.listAllTypes()));
533 Window.get('view-notebook').el.set_current_page(
534 LeftTree.get('model').file.getType()== 'Roo' ? 0 : -1);
540 findDropNode : function (treepath_str, targets)
544 var path = treepath_str.replace(/^builder-/, '');
546 if (!XObject.keys(this.treemap).length) {
548 return [ '', Gtk.TreeViewDropPosition.INTO_OR_AFTER];
550 print("FIND treepath: " + path);
551 //console.dump(this.treemap);
553 if (!treepath_str.match(/^builder-/)) {
554 return []; // nothing!
556 if (targets === true) {
559 return this.findDropNodeByPath(path,targets)
561 findDropNodeByPath : function (treepath_str, targets, pref)
564 var path = treepath_str + ''; // dupe it..
565 pref = typeof(pref) == 'undefined' ? Gtk.TreeViewDropPosition.INTO_OR_AFTER : pref;
567 //console.dump(this.treemap);
568 while (path.length) {
569 if (typeof(this.treemap[path]) == 'undefined') {
573 var xname = LeftTree.get('model').file.guessName(this.treemap[path]);
576 targets.forEach(function(tg) {
580 if ((tg == xname) ) {
583 if (tg.indexOf(xname +':') === 0) {
585 prop = tg.split(':').pop();
590 if (last) { // pref is after/before..
591 // then it's after last
593 return []; // do not allow..
595 return [ last, pref , prop];
598 return [ path , Gtk.TreeViewDropPosition.INTO_OR_AFTER , prop];
600 var par = path.split(':');
603 path = par.join(':');
610 dropNode: function(target_data, node) {
612 console.dump(target_data);
613 var tp = target_data[0].length ? new Gtk.TreePath.from_string( target_data[0] ) : false;
615 print("add where: " + target_data[1] );
618 if (target_data[1] < 2) { // before or after..
619 var ar = target_data[0].split(':');
621 parent = new Gtk.TreePath.from_string( ar.join(':') );
624 var n_iter = new Gtk.TreeIter();
625 var iter_par = new Gtk.TreeIter();
626 var iter_after = after ? new Gtk.TreeIter() : false;
627 if (parent !== false) {
628 this.el.get_iter(iter_par, parent);
635 Seed.print(target_data[1] > 0 ? 'insert_after' : 'insert_before');
636 this.el.get_iter(iter_after, after);
637 this.el[ target_data[1] > 0 ? 'insert_after' : 'insert_before'](
638 n_iter, iter_par, iter_after);
641 this.el.append(n_iter, iter_par);
645 if (typeof(node) == 'string') {
646 var ar = node.split('.');
647 var xtype = ar.pop();
650 '|xns' : ar.join('.'),
653 if (target_data.length == 3 && target_data[2].length) {
654 node['*prop'] = target_data[2];
664 this.load(xitems, n_iter);
666 if (xitems || after) {
667 LeftTree.get('view').el.expand_row(this.el.get_path(iter_par), true);
669 // wee need to get the empty proptypes from somewhere..
671 //var olditer = this.activeIter;
672 this.activeIter = n_iter;
673 this.changed(node, true);
677 LeftTree.get('view').el.set_cursor(this.el.get_path(n_iter), null, false);
679 //Builder.MidPropTree._model.load(node);
680 //Builder.MidPropTree._win.hideWin();
681 //Builder.LeftPanel._model.load( node);
687 moveNode: function(target_data) {
689 //print("MOVE NODE");
690 // console.dump(target_data);
691 var old_iter = new Gtk.TreeIter();
692 var s = LeftTree.get('view').selection;
693 s.get_selected(this.el, old_iter);
694 var node = this.nodeToJS(old_iter,false);
695 //console.dump(node);
698 // needs to drop first, otherwise the target_data
699 // treepath will be invalid.
701 this.dropNode(target_data, node);
702 this.el.remove(old_iter);
707 deleteSelected: function() {
711 var old_iter = new Gtk.TreeIter();
712 var s = LeftTree.get('view').selection;
713 s.get_selected(this.el, old_iter);
716 this.el.remove(old_iter);
721 //this.toJS(null, true) // does not do anything?
722 this.activeIter = false;
723 this.changed(false,true);
732 treemap: false, // map of treepath to nodes.
734 listAllTypes : function()
738 var s = LeftTree.get('view').selection;
739 print ("LIST ALL TYPES: " + s.count_selected_rows() );
741 if (s.count_selected_rows() > 0) {
742 var iter = new Gtk.TreeIter();
743 s.get_selected(LeftTree.get('model').el, iter);
745 // set some properties of the tree for use by the dropped element.
746 var value = new GObject.Value('');
747 LeftTree.get('model').el.get_value(iter, 2, value);
748 var data = JSON.parse(value.value);
751 var xname = LeftTree.get('model').file.guessName(data);
752 console.log('selected:' + xname);
756 return []; // could not find it..
763 li.forEach(function(el) {
764 // this is specific to roo!!!?
766 var fullpath = LeftTree.get('model').file.guessName(el);
767 if (fullpath.length && ret.indexOf(fullpath) < 0) {
772 if (el.items && el.items.length) {
781 addall([this.currentTree]);
783 // only if we have nothing, should we add '*top'
787 console.log('all types in tree');
797 * convert tree into a javascript array
800 nodeToJS: function (iter, with_id)
802 var par = new Gtk.TreeIter();
803 var iv = this.getValue(iter, 2);
805 var k = JSON.parse(iv);
806 if (k.json && !this.el.iter_parent( par, iter )) {
811 var treepath_str = this.el.get_path(iter).to_string();
812 k.id = 'builder-'+ treepath_str ;
814 this.treemap[ treepath_str ] = k;
815 k.xtreepath = treepath_str ;
818 if (this.el.iter_has_child(iter)) {
819 citer = new Gtk.TreeIter();
820 this.el.iter_children(citer, iter);
821 k.items = this.toJS(citer,with_id);
826 * iterates through child nodes (or top..)
829 toJS: function(iter, with_id)
831 Seed.print("WITHID: "+ with_id);
838 iter = new Gtk.TreeIter();
839 if (!this.el.get_iter_first(iter)) {
849 var k = this.nodeToJS(iter, with_id);
853 if (!this.el.iter_next(iter)) {
859 // convert the list into a json string..
863 getValue: function (iter, col) {
864 var gval = new GObject.Value('');
865 this.el.get_value(iter, col ,gval);
871 nodeTitle: function(c)
876 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
877 if (sr) txt.push('<s>');
878 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
879 if (c.xtype) { txt.push(c.xtype); }
880 if (c.id) { txt.push('<b>[id=' + c.id + ']</b>'); }
881 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
882 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
885 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
886 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
887 if (c.label) { txt.push('<b>' + c.label+ '</b>'); }
888 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
889 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
890 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
891 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
892 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
893 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
895 // for flat classes...
896 if (typeof(c['*class']) != 'undefined') { txt.push('<b>' + c['*class']+ '</b>'); }
897 if (typeof(c['*extends']) != 'undefined') { txt.push(': <i>' + c['*extends']+ '</i>'); }
900 if (sr) txt.push('</s>');
901 return (txt.length == 0 ? "Element" : txt.join(" "));
903 //console.log(n.xtype);
907 nodeToJSON : function(c) {
915 return JSON.stringify(o);
918 * load javascript array onto an iter..
919 * @param tr = array of elements
920 * @param iter = iter of parent (or null if not..)
926 load : function(tr,iter)
928 var citer = new Gtk.TreeIter();
929 //this.insert(citer,iter,0);
930 for(var i =0 ; i < tr.length; i++) {
932 this.el.insert(citer,iter,-1);
934 this.el.append(citer);
937 this.el.set_value(citer, 0, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
938 this.el.set_value(citer, 1, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
939 this.el.set_value(citer, 2, [GObject.TYPE_STRING, this.nodeToJSON(tr[i])]);
940 if (tr[i].items && tr[i].items.length) {
941 this.load(tr[i].items, citer);
952 // this.expand_all();
957 xtype: Gtk.TreeViewColumn,
958 pack : ['append_column'],
960 XObject.prototype.init.call(this);
961 this.el.add_attribute(this.items[0].el , 'markup', 0 );
967 xtype: Gtk.CellRendererText,
968 pack: [ 'pack_start']