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(
33 xtype: Gtk.ScrolledWindow,
36 shadow_type : Gtk.ShadowType.IN,
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);
42 XObject.prototype.init.call(this);
43 this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
48 getPaleteProvider: function ()
50 var model = this.get('model');
51 var pm = imports.Builder.Provider.ProjectManager.ProjectManager;
52 return pm.getPalete(model.file.getType());
56 renderView: function() // renders into the preview pane..
58 var model = this.get('model');
59 print("RENDER VIEW?" + model.file.getType());
60 switch( model.file.getType()) {
62 var RightBrowser = imports.Builder.RightBrowser.RightBrowser;
63 RightBrowser.get('view').renderJS(model.toJS(false,true)[0]);
65 var RightGtkView = imports.Builder.RightGtkView.RightGtkView ;
66 RightGtkView.renderJS(model.toJS(false,true)[0]);
72 "STRING" : Gdk.atom_intern("STRING")
75 targetList : new Gtk.TargetList(),
81 headers_visible : false,
82 enable_tree_lines : true,
84 // selection -- set by init..
86 XObject.prototype.init.call(this);
88 var description = new Pango.FontDescription.c_new();
89 description.set_size(8000);
90 this.el.modify_font(description);
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'), '']
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 */
107 var targets = new Gtk.TargetList();
108 targets.add( LeftTree.atoms["STRING"], 0, 0);
109 Gtk.drag_source_set_target_list(this.el, targets);
111 Gtk.drag_source_add_text_targets(this.el);
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 */
121 Gtk.drag_dest_set_target_list(this.el, targets);
122 Gtk.drag_dest_add_text_targets(this.el);
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]);
132 this.el.set_drag_dest_row(null, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
137 selectNode : function(treepath_str)
140 this.selection.select_path(new Gtk.TreePath.from_string( treepath_str));
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);
156 LeftTree.get('view').el.get_path_at_pos(ev.button.x,ev.button.y, res);
158 if (!LeftTreeMenu.el) LeftTreeMenu.init();
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());
168 'drag-begin' : function (w, ctx, ud)
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);
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);
184 this.el.dragData = xname;
185 this.el.dropList = LeftTree.getPaleteProvider().getDropList(xname);
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();
192 var pix = this.el.create_row_drag_icon ( path);
194 Gtk.drag_set_icon_pixmap (ctx,
206 'drag-end' : function ( w, drag_context, x, y, time, user_data)
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);
218 'drag-motion' : function (w, ctx, x, y, time, ud)
221 console.log("LEFT-TREE: drag-motion - " + ctx.actions);
222 var src = Gtk.drag_get_source_widget(ctx);
224 // a drag from elsewhere...- prevent drop..
226 print("no drag data!");
227 Gdk.drag_status(ctx, 0, time);
228 this.targetData = false;
231 var action = Gdk.DragAction.COPY;
232 if (src == this.el) {
234 // unless we are copying!!! ctl button..
235 action = ctx.actions & Gdk.DragAction.MOVE ? Gdk.DragAction.MOVE : Gdk.DragAction.COPY ;
238 print("GETTING POS");
239 var isOver = LeftTree.get('view').el.get_dest_row_at_pos(x,y, data);
240 print("ISOVER? " + isOver);
242 Gdk.drag_status(ctx, 0 ,time);
243 return false; // not over apoint!?!
245 // drag node is parent of child..
246 console.log("SRC TREEPATH: " + src.treepath);
247 console.log("TARGET TREEPATH: " + data.path.to_string());
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
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);
263 //print("DUMPING DATA");
264 //console.dump(data);
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);
271 LeftTree.get('view').highlight(tg);
273 print("Can not find drop node path");
274 this.targetData = false;
275 Gdk.drag_status(ctx, 0, time);
279 this.targetData = tg;
282 Gdk.drag_status(ctx, action ,time);
287 'drag-drop' : function (w, ctx, x, y,time, ud)
290 Seed.print("TARGET: drag-drop");
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 */
301 /* No target offered by source => error */
308 'drag-data-received' : function (w, ctx, x, y, sel_data, target_type, time, ud)
310 Seed.print("Tree: drag-data-received");
311 delete_selection_data = false;
313 /* Deal with what we are given from source */
314 if( sel_data && sel_data.length ) {
316 if (ctx.action == Gdk.DragAction.ASK) {
317 /* Ask the user to move or copy, then set the ctx action. */
320 if (ctx.action == Gdk.DragAction.MOVE) {
321 //delete_selection_data = true;
324 var source = Gtk.drag_get_source_widget(ctx);
326 if (this.targetData) {
327 if (source != this.el) {
328 LeftTree.get('model').dropNode(this.targetData, source.dragData);
330 // drag around.. - reorder..
331 LeftTree.get('model').moveNode(this.targetData, ctx.action);
335 //Seed.print(this.targetData);
341 // we can send stuff to souce here...
347 if (dnd_success == false)
349 Seed.print ("DnD data transfer failed!\n");
352 Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
356 'cursor-changed' : function(tv, a) {
357 var iter = new Gtk.TreeIter();
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..
369 pm.load( LeftTree.getPaleteProvider().gatherList(
370 LeftTree.get('model').listAllTypes()));
375 //console.log('changed');
376 var s = this.selection;
377 s.get_selected(LeftTree.get('model').el, iter);
381 value = new GObject.Value('');
382 LeftTree.get('model').el.get_value(iter, 2, value);
383 LeftTree.get('model').activeIter = iter;
385 var data = JSON.parse(value.value);
386 MidPropTree.activeElement = data;
387 MidPropTree.hideWin();
388 LeftPanel.get('model').load( data);
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()));
400 //Seed.print( value.get_string());
411 pack : ['set_model'],
414 xtype: Gtk.TreeStore,
417 XObject.prototype.init.call(this);
420 this.el.set_column_types ( 3, [
421 GObject.TYPE_STRING, // title
422 GObject.TYPE_STRING, // tip
423 GObject.TYPE_STRING // source..
426 //if (LeftProjectTree.project).getProvider()
433 changed : function( n, refresh)
435 print("MODEL CHANGED CALLED" + this.activeIter);
436 if (this.activeIter) {
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)]);
441 this.el.set_value(this.activeIter, 2, [GObject.TYPE_STRING, this.nodeToJSON(n)]);
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);
448 this.currentTree = this.file.items[0];
449 //console.log(this.file.toSource());
452 print("REDNER BROWSER?!");
453 LeftTree.renderView();
455 var RightPalete = imports.Builder.RightPalete.RightPalete;
456 var pm = RightPalete.get('model');
457 if (!RightPalete.provider) {
463 pm.load( RightPalete.provider.gatherList(this.listAllTypes()));
464 //imports['Builder/RightBrowser.js'].renderJS(this.toJS());
470 loadFile : function(f)
477 console.log('missing file');
481 // load the file if not loaded..
482 if (f.items === false) {
484 f.loadItems(function() {
490 if (f.items.length && typeof(f.items[0]) == 'string') {
492 RightEditor.el.show();
493 RightEditor.get('view').load( f.items[0]);
497 //console.dump(f.items);
499 LeftTree.get('view').el.expand_all();
500 var Window = imports.Builder.Window.Window;
501 if (!f.items.length) {
504 Window.get('leftvpaned').el.set_position(80);
506 LeftTree.get('view').el.set_cursor(
507 new Gtk.TreePath.from_string('0'), null, false);
511 Window.get('leftvpaned').el.set_position(200);
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');
527 RightPalete.provider = LeftTree.getPaleteProvider();
529 if (!RightPalete.provider) {
530 print ("********* PALETE PROVIDER MISSING?!!");
532 LeftTree.renderView();
534 pm.load( LeftTree.getPaleteProvider().gatherList(this.listAllTypes()));
538 Window.get('view-notebook').el.set_current_page(
539 LeftTree.get('model').file.getType()== 'Roo' ? 0 : -1);
545 findDropNode : function (treepath_str, targets)
549 var path = treepath_str.replace(/^builder-/, '');
551 if (!XObject.keys(this.treemap).length) {
553 return [ '', Gtk.TreeViewDropPosition.INTO_OR_AFTER];
555 print("FIND treepath: " + path);
556 //console.dump(this.treemap);
558 if (!treepath_str.match(/^builder-/)) {
559 return []; // nothing!
561 if (targets === true) {
564 return this.findDropNodeByPath(path,targets)
566 findDropNodeByPath : function (treepath_str, targets, pref)
569 var path = treepath_str + ''; // dupe it..
570 pref = typeof(pref) == 'undefined' ? Gtk.TreeViewDropPosition.INTO_OR_AFTER : pref;
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");
581 var xname = LeftTree.get('model').file.guessName(node_data);
584 targets.forEach(function(tg) {
588 if ((tg == xname) ) {
591 if (tg.indexOf(xname +':') === 0) {
593 prop = tg.split(':').pop();
598 if (last) { // pref is after/before..
599 // then it's after last
601 return []; // do not allow..
603 return [ last, pref , prop];
606 return [ path , Gtk.TreeViewDropPosition.INTO_OR_AFTER , prop];
608 var par = path.split(':');
611 path = par.join(':');
619 * drop a node.. - tecncially add node..
621 * @param {Array} target_data - [ treepath_string, before/after/ , property (to add as)]
622 * @param {Object} node with data..
625 dropNode: function(target_data, node) {
627 console.dump(target_data);
628 var tp = target_data[0].length ? new Gtk.TreePath.from_string( target_data[0] ) : false;
630 print("add where: " + target_data[1] );
633 if (target_data[1] < 2) { // before or after..
634 var ar = target_data[0].split(':');
636 parent = new Gtk.TreePath.from_string( ar.join(':') );
639 var n_iter = new Gtk.TreeIter();
640 var iter_par = new Gtk.TreeIter();
641 var iter_after = after ? new Gtk.TreeIter() : false;
645 if (parent !== false) {
646 this.el.get_iter(iter_par, parent);
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);
659 this.el.append(n_iter, iter_par);
663 if (typeof(node) == 'string') {
664 var ar = node.split('.');
665 var xtype = ar.pop();
668 '|xns' : ar.join('.'),
671 if (target_data.length == 3 && target_data[2].length) {
672 node['*prop'] = target_data[2];
676 // work out what kind of packing to use..
677 if (typeof(node.pack) == 'undefined' && parent !== false) {
678 var pal = this.get('/LeftTree').getPaleteProvider();
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);
696 this.load(xitems, n_iter);
698 if (xitems || after) {
699 LeftTree.get('view').el.expand_row(this.el.get_path(iter_par), true);
701 // wee need to get the empty proptypes from somewhere..
703 //var olditer = this.activeIter;
704 this.activeIter = n_iter;
705 this.changed(node, true);
709 LeftTree.get('view').el.set_cursor(this.el.get_path(n_iter), null, false);
711 //Builder.MidPropTree._model.load(node);
712 //Builder.MidPropTree._win.hideWin();
713 //Builder.LeftPanel._model.load( node);
719 moveNode: function(target_data , action) {
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);
730 // needs to drop first, otherwise the target_data
731 // treepath will be invalid.
733 this.dropNode(target_data, node);
735 print("MOVENODE ACTION: " + action);
737 if (action & Gdk.DragAction.MOVE) {
738 print("REMOVING OLD NODE");
739 this.el.remove(old_iter);
743 this.activeIter = false;
744 this.changed(false,true);
745 return; // do not remove.
752 deleteSelected: function() {
756 var old_iter = new Gtk.TreeIter();
757 var s = LeftTree.get('view').selection;
758 s.get_selected(this.el, old_iter);
761 this.el.remove(old_iter);
766 //this.toJS(null, true) // does not do anything?
767 this.activeIter = false;
768 this.changed(false,true);
777 treemap: false, // map of treepath to nodes.
779 listAllTypes : function()
783 var s = LeftTree.get('view').selection;
784 print ("LIST ALL TYPES: " + s.count_selected_rows() );
786 if (s.count_selected_rows() > 0) {
787 var iter = new Gtk.TreeIter();
788 s.get_selected(LeftTree.get('model').el, iter);
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);
796 var xname = LeftTree.get('model').file.guessName(data);
797 console.log('selected:' + xname);
801 return []; // could not find it..
808 li.forEach(function(el) {
809 // this is specific to roo!!!?
811 var fullpath = LeftTree.get('model').file.guessName(el);
812 if (fullpath.length && ret.indexOf(fullpath) < 0) {
817 if (el.items && el.items.length) {
826 addall([this.currentTree]);
828 // only if we have nothing, should we add '*top'
832 console.log('all types in tree');
839 singleNodeToJS: function (treepath)
841 var iter = new Gtk.TreeIter();
842 if (!this.el.get_iter(iter, new Gtk.TreePath.from_string(treepath))) {
846 var iv = this.getValue(iter, 2);
848 return JSON.parse(iv);
853 * convert tree into a javascript array
856 nodeToJS: function (iter, with_id)
858 var par = new Gtk.TreeIter();
859 var iv = this.getValue(iter, 2);
861 var k = JSON.parse(iv);
862 if (k.json && !this.el.iter_parent( par, iter )) {
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 ;
874 this.treemap[ treepath_str ] = k;
875 k.xtreepath = treepath_str ;
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);
886 * iterates through child nodes (or top..)
889 toJS: function(iter, with_id)
891 //Seed.print("WITHID: "+ with_id);
898 iter = new Gtk.TreeIter();
899 if (!this.el.get_iter_first(iter)) {
909 var k = this.nodeToJS(iter, with_id);
913 if (!this.el.iter_next(iter)) {
919 // convert the list into a json string..
923 getValue: function (iter, col) {
924 var gval = new GObject.Value('');
925 this.el.get_value(iter, col ,gval);
931 nodeTitle: function(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 + ']'); }
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+ ']'); }
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>'); }
960 if (sr) txt.push('</s>');
961 return (txt.length == 0 ? "Element" : txt.join(" "));
963 //console.log(n.xtype);
967 nodeToJSON : function(c) {
975 return JSON.stringify(o);
978 * load javascript array onto an iter..
979 * @param tr = array of elements
980 * @param iter = iter of parent (or null if not..)
986 load : function(tr,iter)
988 var citer = new Gtk.TreeIter();
989 //this.insert(citer,iter,0);
990 for(var i =0 ; i < tr.length; i++) {
992 this.el.insert(citer,iter,-1);
994 this.el.append(citer);
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);
1012 // this.expand_all();
1017 xtype: Gtk.TreeViewColumn,
1018 pack : ['append_column'],
1020 XObject.prototype.init.call(this);
1021 this.el.add_attribute(this.items[0].el , 'markup', 0 );
1027 xtype: Gtk.CellRendererText,
1028 pack: [ 'pack_start']