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)
48 "STRING" : Gdk.atom_intern("STRING")
51 targetList : new Gtk.TargetList(),
57 headers_visible : false,
58 enable_tree_lines : true,
60 // selection -- set by init..
62 XObject.prototype.init.call(this);
64 var description = new Pango.FontDescription.c_new();
65 description.set_size(8000);
66 this.el.modify_font(description);
68 this.selection = this.el.get_selection();
69 this.selection.set_mode( Gtk.SelectionMode.SINGLE);
70 this.selection.signal['changed'].connect(function() {
71 LeftTree.get('view').listeners['cursor-changed'].apply(
72 LeftTree.get('view'), [ LeftTree.get('view'), '']
77 this.el, /* widget will be drag-able */
78 Gdk.ModifierType.BUTTON1_MASK, /* modifier that will start a drag */
79 null, /* lists of target to support */
81 Gdk.DragAction.COPY | Gdk.DragAction.MOVE /* what to do with data after dropped */
83 var targets = new Gtk.TargetList();
84 targets.add( LeftTree.atoms["STRING"], 0, 0);
85 Gtk.drag_source_set_target_list(this.el, targets);
87 Gtk.drag_source_add_text_targets(this.el);
90 this.el, /* widget that will accept a drop */
91 Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT,
92 null, /* lists of target to support */
94 Gdk.DragAction.COPY | Gdk.DragAction.MOVE /* what to do with data after dropped */
97 Gtk.drag_dest_set_target_list(this.el, targets);
98 Gtk.drag_dest_add_text_targets(this.el);
103 highlight : function(treepath_ar) {
104 if (treepath_ar.length && treepath_ar[0].length ) {
105 this.el.set_drag_dest_row(
106 new Gtk.TreePath.from_string( treepath_ar[0] ), treepath_ar[1]);
108 this.el.set_drag_dest_row(null, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
113 selectNode : function(treepath_str)
116 this.selection.select_path(new Gtk.TreePath.from_string( treepath_str));
123 'button-press-event' : function(tv, ev) {
124 console.log("button press?");
125 if (ev.type != Gdk.EventType.BUTTON_PRESS || ev.button.button != 3) {
126 Seed.print("click" + ev.type);
132 LeftTree.get('view').el.get_path_at_pos(ev.button.x,ev.button.y, res);
134 if (!LeftTreeMenu.el) LeftTreeMenu.init();
136 LeftTreeMenu.el.set_screen(Gdk.Screen.get_default());
137 LeftTreeMenu.el.show_all();
138 LeftTreeMenu.el.popup(null, null, null, null, 3, ev.button.time);
139 Seed.print("click:" + res.path.to_string());
144 'drag-begin' : function (w, ctx, ud)
146 // we could fill this in now...
147 Seed.print('SOURCE: drag-begin');
148 this.targetData = false;
149 // find what is selected in our tree...
150 var iter = new Gtk.TreeIter();
151 var s = this.selection;
152 s.get_selected(LeftTree.get('model').el, iter);
154 // set some properties of the tree for use by the dropped element.
155 var value = new GObject.Value('');
156 LeftTree.get('model').el.get_value(iter, 2, value);
157 var data = JSON.parse(value.value);
158 var xname = Roo.guessName(data);
160 this.el.dragData = xname;
161 this.el.dropList = LeftTree.get('model').provider.getDropList(xname);
164 // make the drag icon a picture of the node that was selected
165 var path = LeftTree.get('model').el.get_path(iter);
166 var pix = this.el.create_row_drag_icon ( path);
167 Gtk.drag_set_icon_pixmap (ctx,
179 'drag-end' : function ( w, drag_context, x, y, time, user_data)
181 // i'm not sure if this would work, without implementing the whole kaboodle.
182 Seed.print('LEFT-TREE: drag-end');
183 this.el.dragData = false;
184 this.el.dropList = false;
185 this.targetData = false;
186 LeftTree.get('view').highlight(false);
191 'drag-motion' : function (w, ctx, x, y, time, ud)
194 console.log("LEFT-TREE: drag-motion");
195 var src = Gtk.drag_get_source_widget(ctx);
197 // a drag from elsewhere...- prevent drop..
199 Gdk.drag_status(ctx, 0, time);
200 this.targetData = false;
203 var action = Gdk.DragAction.COPY;
204 if (src == this.el) {
205 // unless we are copying!!! ctl button..
206 action = Gdk.DragAction.MOVE;
209 if (!LeftTree.get('view').el.get_dest_row_at_pos(x,y, data)) {
210 return false; // not over apoint!?!
214 Seed.print(data.path.to_string() +' => '+ data.pos);
215 var tg = LeftTree.get('model').findDropNodeByPath(data.path.to_string(), src.dropList, data.pos);
216 LeftTree.get('view').highlight(tg);
218 this.targetData = false;
219 Gdk.drag_status(ctx, 0, time);
223 this.targetData = tg;
226 Gdk.drag_status(ctx, action ,time);
231 'drag-drop' : function (w, ctx, x, y,time, ud)
234 Seed.print("TARGET: drag-drop");
238 w, /* will receive 'drag-data-received' signal */
239 ctx, /* represents the current state of the DnD */
240 LeftTree.atoms["STRING"], /* the target type we want */
241 time /* time stamp */
245 /* No target offered by source => error */
252 'drag-data-received' : function (w, ctx, x, y, sel_data, target_type, time, ud)
254 Seed.print("Tree: drag-data-received");
255 delete_selection_data = false;
257 /* Deal with what we are given from source */
258 if( sel_data && sel_data.length ) {
260 if (ctx.action == Gdk.DragAction.ASK) {
261 /* Ask the user to move or copy, then set the ctx action. */
264 if (ctx.action == Gdk.DragAction.MOVE) {
265 delete_selection_data = true;
268 var source = Gtk.drag_get_source_widget(ctx);
270 if (this.targetData) {
271 if (source != this.el) {
272 LeftTree.get('model').dropNode(this.targetData, source.dragData);
274 // drag around.. - reorder..
275 LeftTree.get('model').moveNode(this.targetData);
279 Seed.print(this.targetData);
285 // we can send stuff to souce here...
291 if (dnd_success == false)
293 Seed.print ("DnD data transfer failed!\n");
296 Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
300 'cursor-changed' : function(tv, a) {
301 var iter = new Gtk.TreeIter();
303 if (this.selection.count_selected_rows() < 1) {
304 LeftPanel.get('model').load( false);
305 MidPropTree.get('model').load(data);
306 MidPropTree.hideWin();
307 var RightPalete = imports.Builder.RightPalete.RightPalete;
308 var pm = RightPalete.get('model');
309 pm.load( RightPalete.provider.gatherList(LeftTree.get('model').listAllTypes()));
314 //console.log('changed');
315 var s = this.selection;
316 s.get_selected(LeftTree.get('model').el, iter);
320 value = new GObject.Value('');
321 LeftTree.get('model').el.get_value(iter, 2, value);
322 LeftTree.get('model').activeIter = iter;
324 var data = JSON.parse(value.value);
325 MidPropTree.get('model').load(data);
326 MidPropTree.hideWin();
327 LeftPanel.get('model').load( data);
329 console.log(value.value);
330 // _g.button.set_label(''+value.get_string());
331 var RightPalete = imports.Builder.RightPalete.RightPalete;
332 var pm = RightPalete.get('model');
333 pm.load( RightPalete.provider.gatherList(LeftTree.get('model').listAllTypes()));
338 //Seed.print( value.get_string());
349 pack : ['set_model'],
352 xtype: Gtk.TreeStore,
355 XObject.prototype.init.call(this);
358 this.el.set_column_types ( 3, [
359 GObject.TYPE_STRING, // title
360 GObject.TYPE_STRING, // tip
361 GObject.TYPE_STRING // source..
364 //if (LeftProjectTree.project).getProvider()
371 changed : function( n, refresh)
373 print("MODEL CHANGED CALLED" + this.activeIter);
374 if (this.activeIter) {
376 this.el.set_value(this.activeIter, 0, [GObject.TYPE_STRING, this.nodeTitle(n)]);
377 this.el.set_value(this.activeIter, 1, [GObject.TYPE_STRING, this.nodeTitle(n)]);
379 this.el.set_value(this.activeIter, 2, [GObject.TYPE_STRING, this.nodeToJSON(n)]);
381 //this.currentTree = this.toJS(false, true)[0];
382 this.file.items = this.toJS(false, false);
383 print("AFTER CHANGED")
384 console.dump(this.file.items);
386 this.currentTree = this.file.items[0];
387 //console.log(this.file.toSource());
390 print("REDNER BROWSER?!");
392 var RightBrowser = imports.Builder.RightBrowser.RightBrowser;
393 RightBrowser.get('view').renderJS(this.toJS(false,true)[0]);
394 var RightPalete = imports.Builder.RightPalete.RightPalete;
395 var pm = RightPalete.get('model');
396 if (!RightPalete.provider) {
402 pm.load( RightPalete.provider.gatherList(this.listAllTypes()));
403 //imports['Builder/RightBrowser.js'].renderJS(this.toJS());
409 loadFile : function(f)
416 console.log('missing file');
420 // load the file if not loaded..
421 if (f.items === false) {
423 f.loadItems(function() {
429 if (f.items.length && typeof(f.items[0]) == 'string') {
431 RightEditor.el.show();
432 RightEditor.get('view').load( f.items[0]);
437 LeftTree.get('view').el.expand_all();
438 var Window = imports.Builder.Window.Window;
439 if (!f.items.length) {
442 Window.get('leftvpaned').el.set_position(80);
444 LeftTree.get('view').el.set_cursor(
445 new Gtk.TreePath.from_string('0'), null, false);
449 Window.get('leftvpaned').el.set_position(200);
453 print("hide right editior");
454 RightEditor.el.hide();
455 print("set current tree");
456 this.currentTree = this.toJS(false, false)[0];
457 console.dump(this.currentTree);
458 this.currentTree = this.currentTree || { items: [] };
459 var RightBrowser = imports.Builder.RightBrowser.RightBrowser;
460 RightBrowser.get('view').renderJS(this.toJS(false,true)[0]);
461 //console.dump(this.map);
462 var RightPalete = imports.Builder.RightPalete.RightPalete;
463 var pm = RightPalete.get('model');
465 var LeftProjectTree = imports.Builder.LeftProjectTree.LeftProjectTree;
466 //if (!LeftProjectTree.project) {
467 // pm.provider = false;
469 RightPalete.provider = LeftProjectTree.project.getPalete();
471 var pr = LeftProjectTree.project;
472 pm.load( RightPalete.provider.gatherList(this.listAllTypes()));
476 Window.get('view-notebook').el.set_current_page(pr.xtype == 'Roo' ? 0 : -1);
482 findDropNode : function (treepath_str, targets)
486 var path = treepath_str.replace(/^builder-/, '');
488 if (!XObject.keys(this.treemap).length) {
490 return [ '', Gtk.TreeViewDropPosition.INTO_OR_AFTER];
492 print("FIND treepath: " + path);
493 //console.dump(this.treemap);
495 if (!treepath_str.match(/^builder-/)) {
496 return []; // nothing!
498 if (targets === true) {
501 return this.findDropNodeByPath(path,targets)
503 findDropNodeByPath : function (treepath_str, targets, pref)
506 var path = treepath_str + ''; // dupe it..
507 pref = typeof(pref) == 'undefined' ? Gtk.TreeViewDropPosition.INTO_OR_AFTER : pref;
509 console.dump(this.treemap);
510 while (path.length) {
511 if (typeof(this.treemap[path]) == 'undefined') {
515 var xname = Roo.guessName(this.treemap[path]);
518 targets.forEach(function(tg) {
522 if ((tg == xname) ) {
525 if (tg.indexOf(xname +':') === 0) {
527 prop = tg.split(':').pop();
532 if (last) { // pref is after/before..
533 // then it's after last
535 return []; // do not allow..
537 return [ last, pref , prop];
540 return [ path , Gtk.TreeViewDropPosition.INTO_OR_AFTER , prop];
542 var par = path.split(':');
545 path = par.join(':');
552 dropNode: function(target_data, node) {
554 console.dump(target_data);
555 var tp = target_data[0].length ? new Gtk.TreePath.from_string( target_data[0] ) : false;
557 print("add where: " + target_data[1] );
560 if (target_data[1] < 2) { // before or after..
561 var ar = target_data[0].split(':');
563 parent = new Gtk.TreePath.from_string( ar.join(':') );
566 var n_iter = new Gtk.TreeIter();
567 var iter_par = new Gtk.TreeIter();
568 var iter_after = after ? new Gtk.TreeIter() : false;
569 if (parent !== false) {
570 this.el.get_iter(iter_par, parent);
577 Seed.print(target_data[1] > 0 ? 'insert_after' : 'insert_before');
578 this.el.get_iter(iter_after, after);
579 this.el[ target_data[1] > 0 ? 'insert_after' : 'insert_before'](
580 n_iter, iter_par, iter_after);
583 this.el.append(n_iter, iter_par);
587 if (typeof(node) == 'string') {
588 var ar = node.split('.');
589 var xtype = ar.pop();
592 '|xns' : ar.join('.'),
595 if (target_data.length == 3 && target_data[2].length) {
596 node['*prop'] = target_data[2];
606 this.load(xitems, n_iter);
608 if (xitems || after) {
609 LeftTree.get('view').el.expand_row(this.el.get_path(iter_par), true);
611 // wee need to get the empty proptypes from somewhere..
613 //var olditer = this.activeIter;
614 this.activeIter = n_iter;
615 this.changed(node, true);
619 LeftTree.get('view').el.set_cursor(this.el.get_path(n_iter), null, false);
621 //Builder.MidPropTree._model.load(node);
622 //Builder.MidPropTree._win.hideWin();
623 //Builder.LeftPanel._model.load( node);
629 moveNode: function(target_data, type) {
633 var old_iter = new Gtk.TreeIter();
634 var s = LeftTree.get('view').selection;
635 s.get_selected(this.el, old_iter);
636 var node = this.nodeToJS(old_iter,false);
637 this.el.remove(old_iter);
638 this.dropNode(target_data, node);
644 deleteSelected: function() {
648 var old_iter = new Gtk.TreeIter();
649 var s = LeftTree.get('view').selection;
650 s.get_selected(this.el, old_iter);
653 this.el.remove(old_iter);
658 //this.toJS(null, true) // does not do anything?
659 this.activeIter = false;
660 this.changed(false,true);
669 treemap: false, // map of treepath to nodes.
671 listAllTypes : function()
675 var s = LeftTree.get('view').selection;
676 print ("LIST ALL TYPES: " + s.count_selected_rows() );
678 if (s.count_selected_rows() > 0) {
679 var iter = new Gtk.TreeIter();
680 s.get_selected(LeftTree.get('model').el, iter);
682 // set some properties of the tree for use by the dropped element.
683 var value = new GObject.Value('');
684 LeftTree.get('model').el.get_value(iter, 2, value);
685 var data = JSON.parse(value.value);
686 var xname = Roo.guessName(data);
687 console.log('selected:' + xname);
691 return []; // could not find it..
698 li.forEach(function(el) {
699 // this is specific to roo!!!?
700 var fullpath = Roo.guessName(el);
701 if (fullpath.length && ret.indexOf(fullpath) < 0) {
706 if (el.items && el.items.length) {
715 addall([this.currentTree]);
717 // only if we have nothing, should we add '*top'
721 console.log('all types in tree');
731 * convert tree into a javascript array
734 nodeToJS: function (iter, with_id)
736 var par = new Gtk.TreeIter();
737 var iv = this.getValue(iter, 2);
739 var k = JSON.parse(iv);
740 if (k.json && !this.el.iter_parent( par, iter )) {
745 var treepath_str = this.el.get_path(iter).to_string();
746 k.id = 'builder-'+ treepath_str ;
748 this.treemap[ treepath_str ] = k;
749 k.xtreepath = treepath_str ;
752 if (this.el.iter_has_child(iter)) {
753 citer = new Gtk.TreeIter();
754 this.el.iter_children(citer, iter);
755 k.items = this.toJS(citer,with_id);
760 * iterates through child nodes (or top..)
763 toJS: function(iter, with_id)
765 Seed.print("WITHID: "+ with_id);
772 iter = new Gtk.TreeIter();
773 if (!this.el.get_iter_first(iter)) {
783 var k = this.nodeToJS(iter, with_id);
787 if (!this.el.iter_next(iter)) {
793 // convert the list into a json string..
797 getValue: function (iter, col) {
798 var gval = new GObject.Value('');
799 this.el.get_value(iter, col ,gval);
805 nodeTitle: function(c)
810 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
811 if (sr) txt.push('<s>');
812 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
813 if (c.xtype) { txt.push(c.xtype); }
814 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
815 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
818 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
819 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
820 if (c.label) { txt.push('<b>' + c.label+ '</b>'); }
821 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
822 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
823 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
824 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
825 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
826 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
828 // for flat classes...
829 if (typeof(c['*class']) != 'undefined') { txt.push('<b>' + c['*class']+ '</b>'); }
830 if (typeof(c['*extends']) != 'undefined') { txt.push(': <i>' + c['*extends']+ '</i>'); }
833 if (sr) txt.push('</s>');
834 return (txt.length == 0 ? "Element" : txt.join(" "));
836 //console.log(n.xtype);
840 nodeToJSON : function(c) {
848 return JSON.stringify(o);
851 * load javascript array onto an iter..
852 * @param tr = array of elements
853 * @param iter = iter of parent (or null if not..)
859 load : function(tr,iter)
861 var citer = new Gtk.TreeIter();
862 //this.insert(citer,iter,0);
863 for(var i =0 ; i < tr.length; i++) {
865 this.el.insert(citer,iter,-1);
867 this.el.append(citer);
870 this.el.set_value(citer, 0, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
871 this.el.set_value(citer, 1, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
872 this.el.set_value(citer, 2, [GObject.TYPE_STRING, this.nodeToJSON(tr[i])]);
873 if (tr[i].items && tr[i].items.length) {
874 this.load(tr[i].items, citer);
885 // this.expand_all();
890 xtype: Gtk.TreeViewColumn,
891 pack : ['append_column'],
893 XObject.prototype.init.call(this);
894 this.el.add_attribute(this.items[0].el , 'markup', 0 );
900 xtype: Gtk.CellRendererText,
901 pack: [ 'pack_start']