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 Gdk.drag_status(ctx, 0, time);
226 this.targetData = false;
229 var action = Gdk.DragAction.COPY;
230 if (src == this.el) {
231 // unless we are copying!!! ctl button..
232 action = Gdk.DragAction.MOVE;
235 print("GETTING POS");
236 var isOver = LeftTree.get('view').el.get_dest_row_at_pos(x,y, data);
237 print("ISOVER? " + isOver);
239 Gdk.drag_status(ctx, 0 ,time);
240 return false; // not over apoint!?!
242 // drag node is parent of child..
243 console.log("SRC TREEPATH: " + src.treepath);
244 console.log("TARGET TREEPATH: " + data.path.to_string());
246 // nned to check a few here..
247 //Gtk.TreeViewDropPosition.INTO_OR_AFTER
248 //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
249 //Gtk.TreeViewDropPosition.AFTER
250 //Gtk.TreeViewDropPosition.BEFORE
252 if (typeof(src.treepath) != 'undefined' &&
253 src.treepath == data.path.to_string().substring(0,src.treepath.length)) {
254 Gdk.drag_status(ctx, 0 ,time);
259 //print("DUMPING DATA");
260 //console.dump(data);
263 Seed.print(data.path.to_string() +' => '+ data.pos);
264 var tg = LeftTree.get('model').findDropNodeByPath(data.path.to_string(), src.dropList, data.pos);
265 LeftTree.get('view').highlight(tg);
267 this.targetData = false;
268 Gdk.drag_status(ctx, 0, time);
272 this.targetData = tg;
275 Gdk.drag_status(ctx, action ,time);
280 'drag-drop' : function (w, ctx, x, y,time, ud)
283 Seed.print("TARGET: drag-drop");
287 w, /* will receive 'drag-data-received' signal */
288 ctx, /* represents the current state of the DnD */
289 LeftTree.atoms["STRING"], /* the target type we want */
290 time /* time stamp */
294 /* No target offered by source => error */
301 'drag-data-received' : function (w, ctx, x, y, sel_data, target_type, time, ud)
303 Seed.print("Tree: drag-data-received");
304 delete_selection_data = false;
306 /* Deal with what we are given from source */
307 if( sel_data && sel_data.length ) {
309 if (ctx.action == Gdk.DragAction.ASK) {
310 /* Ask the user to move or copy, then set the ctx action. */
313 if (ctx.action == Gdk.DragAction.MOVE) {
314 //delete_selection_data = true;
317 var source = Gtk.drag_get_source_widget(ctx);
319 if (this.targetData) {
320 if (source != this.el) {
321 LeftTree.get('model').dropNode(this.targetData, source.dragData);
323 // drag around.. - reorder..
324 LeftTree.get('model').moveNode(this.targetData);
328 //Seed.print(this.targetData);
334 // we can send stuff to souce here...
340 if (dnd_success == false)
342 Seed.print ("DnD data transfer failed!\n");
345 Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
349 'cursor-changed' : function(tv, a) {
350 var iter = new Gtk.TreeIter();
352 if (this.selection.count_selected_rows() < 1) {
353 LeftPanel.get('model').load( false);
354 MidPropTree.activeElement = false;
355 MidPropTree.hideWin();
356 var RightPalete = imports.Builder.RightPalete.RightPalete;
357 var pm = RightPalete.get('model');
358 if (!LeftTree.getPaleteProvider()) {
359 // it may not be loaded yet..
362 pm.load( LeftTree.getPaleteProvider().gatherList(
363 LeftTree.get('model').listAllTypes()));
368 //console.log('changed');
369 var s = this.selection;
370 s.get_selected(LeftTree.get('model').el, iter);
374 value = new GObject.Value('');
375 LeftTree.get('model').el.get_value(iter, 2, value);
376 LeftTree.get('model').activeIter = iter;
378 var data = JSON.parse(value.value);
379 MidPropTree.activeElement = data;
380 MidPropTree.hideWin();
381 LeftPanel.get('model').load( data);
383 console.log(value.value);
384 // _g.button.set_label(''+value.get_string());
385 var RightPalete = imports.Builder.RightPalete.RightPalete;
386 var pm = RightPalete.get('model');
387 pm.load( RightPalete.provider.gatherList(
388 LeftTree.get('model').listAllTypes()));
393 //Seed.print( value.get_string());
404 pack : ['set_model'],
407 xtype: Gtk.TreeStore,
410 XObject.prototype.init.call(this);
413 this.el.set_column_types ( 3, [
414 GObject.TYPE_STRING, // title
415 GObject.TYPE_STRING, // tip
416 GObject.TYPE_STRING // source..
419 //if (LeftProjectTree.project).getProvider()
426 changed : function( n, refresh)
428 print("MODEL CHANGED CALLED" + this.activeIter);
429 if (this.activeIter) {
431 this.el.set_value(this.activeIter, 0, [GObject.TYPE_STRING, this.nodeTitle(n)]);
432 this.el.set_value(this.activeIter, 1, [GObject.TYPE_STRING, this.nodeTitle(n)]);
434 this.el.set_value(this.activeIter, 2, [GObject.TYPE_STRING, this.nodeToJSON(n)]);
436 //this.currentTree = this.toJS(false, true)[0];
437 this.file.items = this.toJS(false, false);
438 print("AFTER CHANGED")
439 //console.dump(this.file.items);
441 this.currentTree = this.file.items[0];
442 //console.log(this.file.toSource());
445 print("REDNER BROWSER?!");
446 LeftTree.renderView();
448 var RightPalete = imports.Builder.RightPalete.RightPalete;
449 var pm = RightPalete.get('model');
450 if (!RightPalete.provider) {
456 pm.load( RightPalete.provider.gatherList(this.listAllTypes()));
457 //imports['Builder/RightBrowser.js'].renderJS(this.toJS());
463 loadFile : function(f)
470 console.log('missing file');
474 // load the file if not loaded..
475 if (f.items === false) {
477 f.loadItems(function() {
483 if (f.items.length && typeof(f.items[0]) == 'string') {
485 RightEditor.el.show();
486 RightEditor.get('view').load( f.items[0]);
490 //console.dump(f.items);
492 LeftTree.get('view').el.expand_all();
493 var Window = imports.Builder.Window.Window;
494 if (!f.items.length) {
497 Window.get('leftvpaned').el.set_position(80);
499 LeftTree.get('view').el.set_cursor(
500 new Gtk.TreePath.from_string('0'), null, false);
504 Window.get('leftvpaned').el.set_position(200);
508 print("hide right editior");
509 RightEditor.el.hide();
510 print("set current tree");
511 this.currentTree = this.toJS(false, false)[0];
512 //console.dump(this.currentTree);
513 this.currentTree = this.currentTree || { items: [] };
514 LeftTree.renderView();
515 //console.dump(this.map);
516 var RightPalete = imports.Builder.RightPalete.RightPalete;
517 var pm = RightPalete.get('model');
520 RightPalete.provider = LeftTree.getPaleteProvider();
522 if (!RightPalete.provider) {
523 print ("********* PALETE PROVIDER MISSING?!!");
525 LeftTree.renderView();
527 pm.load( LeftTree.getPaleteProvider().gatherList(this.listAllTypes()));
531 Window.get('view-notebook').el.set_current_page(
532 LeftTree.get('model').file.getType()== 'Roo' ? 0 : -1);
538 findDropNode : function (treepath_str, targets)
542 var path = treepath_str.replace(/^builder-/, '');
544 if (!XObject.keys(this.treemap).length) {
546 return [ '', Gtk.TreeViewDropPosition.INTO_OR_AFTER];
548 print("FIND treepath: " + path);
549 //console.dump(this.treemap);
551 if (!treepath_str.match(/^builder-/)) {
552 return []; // nothing!
554 if (targets === true) {
557 return this.findDropNodeByPath(path,targets)
559 findDropNodeByPath : function (treepath_str, targets, pref)
562 var path = treepath_str + ''; // dupe it..
563 pref = typeof(pref) == 'undefined' ? Gtk.TreeViewDropPosition.INTO_OR_AFTER : pref;
565 //console.dump(this.treemap);
566 while (path.length) {
567 if (typeof(this.treemap[path]) == 'undefined') {
571 var xname = LeftTree.get('model').file.guessName(this.treemap[path]);
574 targets.forEach(function(tg) {
578 if ((tg == xname) ) {
581 if (tg.indexOf(xname +':') === 0) {
583 prop = tg.split(':').pop();
588 if (last) { // pref is after/before..
589 // then it's after last
591 return []; // do not allow..
593 return [ last, pref , prop];
596 return [ path , Gtk.TreeViewDropPosition.INTO_OR_AFTER , prop];
598 var par = path.split(':');
601 path = par.join(':');
608 dropNode: function(target_data, node) {
610 console.dump(target_data);
611 var tp = target_data[0].length ? new Gtk.TreePath.from_string( target_data[0] ) : false;
613 print("add where: " + target_data[1] );
616 if (target_data[1] < 2) { // before or after..
617 var ar = target_data[0].split(':');
619 parent = new Gtk.TreePath.from_string( ar.join(':') );
622 var n_iter = new Gtk.TreeIter();
623 var iter_par = new Gtk.TreeIter();
624 var iter_after = after ? new Gtk.TreeIter() : false;
625 if (parent !== false) {
626 this.el.get_iter(iter_par, parent);
633 Seed.print(target_data[1] > 0 ? 'insert_after' : 'insert_before');
634 this.el.get_iter(iter_after, after);
635 this.el[ target_data[1] > 0 ? 'insert_after' : 'insert_before'](
636 n_iter, iter_par, iter_after);
639 this.el.append(n_iter, iter_par);
643 if (typeof(node) == 'string') {
644 var ar = node.split('.');
645 var xtype = ar.pop();
648 '|xns' : ar.join('.'),
651 if (target_data.length == 3 && target_data[2].length) {
652 node['*prop'] = target_data[2];
662 this.load(xitems, n_iter);
664 if (xitems || after) {
665 LeftTree.get('view').el.expand_row(this.el.get_path(iter_par), true);
667 // wee need to get the empty proptypes from somewhere..
669 //var olditer = this.activeIter;
670 this.activeIter = n_iter;
671 this.changed(node, true);
675 LeftTree.get('view').el.set_cursor(this.el.get_path(n_iter), null, false);
677 //Builder.MidPropTree._model.load(node);
678 //Builder.MidPropTree._win.hideWin();
679 //Builder.LeftPanel._model.load( node);
685 moveNode: function(target_data) {
687 //print("MOVE NODE");
688 // console.dump(target_data);
689 var old_iter = new Gtk.TreeIter();
690 var s = LeftTree.get('view').selection;
691 s.get_selected(this.el, old_iter);
692 var node = this.nodeToJS(old_iter,false);
693 //console.dump(node);
696 // needs to drop first, otherwise the target_data
697 // treepath will be invalid.
699 this.dropNode(target_data, node);
700 this.el.remove(old_iter);
705 deleteSelected: function() {
709 var old_iter = new Gtk.TreeIter();
710 var s = LeftTree.get('view').selection;
711 s.get_selected(this.el, old_iter);
714 this.el.remove(old_iter);
719 //this.toJS(null, true) // does not do anything?
720 this.activeIter = false;
721 this.changed(false,true);
730 treemap: false, // map of treepath to nodes.
732 listAllTypes : function()
736 var s = LeftTree.get('view').selection;
737 print ("LIST ALL TYPES: " + s.count_selected_rows() );
739 if (s.count_selected_rows() > 0) {
740 var iter = new Gtk.TreeIter();
741 s.get_selected(LeftTree.get('model').el, iter);
743 // set some properties of the tree for use by the dropped element.
744 var value = new GObject.Value('');
745 LeftTree.get('model').el.get_value(iter, 2, value);
746 var data = JSON.parse(value.value);
749 var xname = LeftTree.get('model').file.guessName(data);
750 console.log('selected:' + xname);
754 return []; // could not find it..
761 li.forEach(function(el) {
762 // this is specific to roo!!!?
764 var fullpath = LeftTree.get('model').file.guessName(el);
765 if (fullpath.length && ret.indexOf(fullpath) < 0) {
770 if (el.items && el.items.length) {
779 addall([this.currentTree]);
781 // only if we have nothing, should we add '*top'
785 console.log('all types in tree');
795 * convert tree into a javascript array
798 nodeToJS: function (iter, with_id)
800 var par = new Gtk.TreeIter();
801 var iv = this.getValue(iter, 2);
803 var k = JSON.parse(iv);
804 if (k.json && !this.el.iter_parent( par, iter )) {
809 var treepath_str = this.el.get_path(iter).to_string();
810 k.id = 'builder-'+ treepath_str ;
812 this.treemap[ treepath_str ] = k;
813 k.xtreepath = treepath_str ;
816 if (this.el.iter_has_child(iter)) {
817 citer = new Gtk.TreeIter();
818 this.el.iter_children(citer, iter);
819 k.items = this.toJS(citer,with_id);
824 * iterates through child nodes (or top..)
827 toJS: function(iter, with_id)
829 Seed.print("WITHID: "+ with_id);
836 iter = new Gtk.TreeIter();
837 if (!this.el.get_iter_first(iter)) {
847 var k = this.nodeToJS(iter, with_id);
851 if (!this.el.iter_next(iter)) {
857 // convert the list into a json string..
861 getValue: function (iter, col) {
862 var gval = new GObject.Value('');
863 this.el.get_value(iter, col ,gval);
869 nodeTitle: function(c)
874 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
875 if (sr) txt.push('<s>');
876 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
877 if (c.xtype) { txt.push(c.xtype); }
878 if (c.id) { txt.push('<b>[id=' + c.id + ']</b>'); }
879 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
880 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
883 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
884 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
885 if (c.label) { txt.push('<b>' + c.label+ '</b>'); }
886 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
887 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
888 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
889 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
890 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
891 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
893 // for flat classes...
894 if (typeof(c['*class']) != 'undefined') { txt.push('<b>' + c['*class']+ '</b>'); }
895 if (typeof(c['*extends']) != 'undefined') { txt.push(': <i>' + c['*extends']+ '</i>'); }
898 if (sr) txt.push('</s>');
899 return (txt.length == 0 ? "Element" : txt.join(" "));
901 //console.log(n.xtype);
905 nodeToJSON : function(c) {
913 return JSON.stringify(o);
916 * load javascript array onto an iter..
917 * @param tr = array of elements
918 * @param iter = iter of parent (or null if not..)
924 load : function(tr,iter)
926 var citer = new Gtk.TreeIter();
927 //this.insert(citer,iter,0);
928 for(var i =0 ; i < tr.length; i++) {
930 this.el.insert(citer,iter,-1);
932 this.el.append(citer);
935 this.el.set_value(citer, 0, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
936 this.el.set_value(citer, 1, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
937 this.el.set_value(citer, 2, [GObject.TYPE_STRING, this.nodeToJSON(tr[i])]);
938 if (tr[i].items && tr[i].items.length) {
939 this.load(tr[i].items, citer);
950 // this.expand_all();
955 xtype: Gtk.TreeViewColumn,
956 pack : ['append_column'],
958 XObject.prototype.init.call(this);
959 this.el.add_attribute(this.items[0].el , 'markup', 0 );
965 xtype: Gtk.CellRendererText,
966 pack: [ 'pack_start']