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 LeftProjectTree = imports.Builder.LeftProjectTree.LeftProjectTree;
50 return LeftProjectTree.project.getPalete();
53 renderView: function() // renders into the preview pane..
55 var model = this.get('model');
56 print("RENDER VIEW?" + model.file.getType());
57 switch( model.file.getType()) {
59 var RightBrowser = imports.Builder.RightBrowser.RightBrowser;
60 RightBrowser.get('view').renderJS(model.toJS(false,true)[0]);
62 var RightGtkView = imports.Builder.RightGtkView.RightGtkView ;
63 RightGtkView.renderJS(model.toJS(false,true)[0]);
69 "STRING" : Gdk.atom_intern("STRING")
72 targetList : new Gtk.TargetList(),
78 headers_visible : false,
79 enable_tree_lines : true,
81 // selection -- set by init..
83 XObject.prototype.init.call(this);
85 var description = new Pango.FontDescription.c_new();
86 description.set_size(8000);
87 this.el.modify_font(description);
89 this.selection = this.el.get_selection();
90 this.selection.set_mode( Gtk.SelectionMode.SINGLE);
91 this.selection.signal['changed'].connect(function() {
92 LeftTree.get('view').listeners['cursor-changed'].apply(
93 LeftTree.get('view'), [ LeftTree.get('view'), '']
98 this.el, /* widget will be drag-able */
99 Gdk.ModifierType.BUTTON1_MASK, /* modifier that will start a drag */
100 null, /* lists of target to support */
101 0, /* size of list */
102 Gdk.DragAction.COPY | Gdk.DragAction.MOVE /* what to do with data after dropped */
104 var targets = new Gtk.TargetList();
105 targets.add( LeftTree.atoms["STRING"], 0, 0);
106 Gtk.drag_source_set_target_list(this.el, targets);
108 Gtk.drag_source_add_text_targets(this.el);
111 this.el, /* widget that will accept a drop */
112 Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT,
113 null, /* lists of target to support */
114 0, /* size of list */
115 Gdk.DragAction.COPY | Gdk.DragAction.MOVE /* what to do with data after dropped */
118 Gtk.drag_dest_set_target_list(this.el, targets);
119 Gtk.drag_dest_add_text_targets(this.el);
124 highlight : function(treepath_ar) {
125 if (treepath_ar.length && treepath_ar[0].length ) {
126 this.el.set_drag_dest_row(
127 new Gtk.TreePath.from_string( treepath_ar[0] ), treepath_ar[1]);
129 this.el.set_drag_dest_row(null, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
134 selectNode : function(treepath_str)
137 this.selection.select_path(new Gtk.TreePath.from_string( treepath_str));
144 'button-press-event' : function(tv, ev) {
145 console.log("button press?");
146 if (ev.type != Gdk.EventType.BUTTON_PRESS || ev.button.button != 3) {
147 Seed.print("click" + ev.type);
153 LeftTree.get('view').el.get_path_at_pos(ev.button.x,ev.button.y, res);
155 if (!LeftTreeMenu.el) LeftTreeMenu.init();
157 LeftTreeMenu.el.set_screen(Gdk.Screen.get_default());
158 LeftTreeMenu.el.show_all();
159 LeftTreeMenu.el.popup(null, null, null, null, 3, ev.button.time);
160 Seed.print("click:" + res.path.to_string());
165 'drag-begin' : function (w, ctx, ud)
167 // we could fill this in now...
168 Seed.print('SOURCE: drag-begin');
169 this.targetData = false;
170 // find what is selected in our tree...
171 var iter = new Gtk.TreeIter();
172 var s = this.selection;
173 s.get_selected(LeftTree.get('model').el, iter);
175 // set some properties of the tree for use by the dropped element.
176 var value = new GObject.Value('');
177 LeftTree.get('model').el.get_value(iter, 2, value);
178 var data = JSON.parse(value.value);
179 var xname = LeftTree.get('model').file.guessName(data);
181 this.el.dragData = xname;
182 this.el.dropList = LeftTree.getPaleteProvider().getDropList(xname);
185 // make the drag icon a picture of the node that was selected
186 var path = LeftTree.get('model').el.get_path(iter);
187 this.el.treepath = path.to_string();
189 var pix = this.el.create_row_drag_icon ( path);
191 Gtk.drag_set_icon_pixmap (ctx,
203 'drag-end' : function ( w, drag_context, x, y, time, user_data)
205 // i'm not sure if this would work, without implementing the whole kaboodle.
206 Seed.print('LEFT-TREE: drag-end');
207 this.el.dragData = false;
208 this.el.dropList = false;
209 this.targetData = false;
210 LeftTree.get('view').highlight(false);
215 'drag-motion' : function (w, ctx, x, y, time, ud)
218 console.log("LEFT-TREE: drag-motion");
219 var src = Gtk.drag_get_source_widget(ctx);
221 // a drag from elsewhere...- prevent drop..
223 Gdk.drag_status(ctx, 0, time);
224 this.targetData = false;
227 var action = Gdk.DragAction.COPY;
228 if (src == this.el) {
229 // unless we are copying!!! ctl button..
230 action = Gdk.DragAction.MOVE;
233 print("GETTING POS");
234 var isOver = LeftTree.get('view').el.get_dest_row_at_pos(x,y, data);
235 print("ISOVER? " + isOver);
237 Gdk.drag_status(ctx, 0 ,time);
238 return false; // not over apoint!?!
240 // drag node is parent of child..
241 console.log("SRC TREEPATH: " + src.treepath);
242 console.log("TARGET TREEPATH: " + data.path.to_string());
244 // nned to check a few here..
245 //Gtk.TreeViewDropPosition.INTO_OR_AFTER
246 //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
247 //Gtk.TreeViewDropPosition.AFTER
248 //Gtk.TreeViewDropPosition.BEFORE
250 if (typeof(src.treepath) != 'undefined' &&
251 src.treepath == data.path.to_string().substring(0,src.treepath.length)) {
252 Gdk.drag_status(ctx, 0 ,time);
257 //print("DUMPING DATA");
258 //console.dump(data);
261 Seed.print(data.path.to_string() +' => '+ data.pos);
262 var tg = LeftTree.get('model').findDropNodeByPath(data.path.to_string(), src.dropList, data.pos);
263 LeftTree.get('view').highlight(tg);
265 this.targetData = false;
266 Gdk.drag_status(ctx, 0, time);
270 this.targetData = tg;
273 Gdk.drag_status(ctx, action ,time);
278 'drag-drop' : function (w, ctx, x, y,time, ud)
281 Seed.print("TARGET: drag-drop");
285 w, /* will receive 'drag-data-received' signal */
286 ctx, /* represents the current state of the DnD */
287 LeftTree.atoms["STRING"], /* the target type we want */
288 time /* time stamp */
292 /* No target offered by source => error */
299 'drag-data-received' : function (w, ctx, x, y, sel_data, target_type, time, ud)
301 Seed.print("Tree: drag-data-received");
302 delete_selection_data = false;
304 /* Deal with what we are given from source */
305 if( sel_data && sel_data.length ) {
307 if (ctx.action == Gdk.DragAction.ASK) {
308 /* Ask the user to move or copy, then set the ctx action. */
311 if (ctx.action == Gdk.DragAction.MOVE) {
312 //delete_selection_data = true;
315 var source = Gtk.drag_get_source_widget(ctx);
317 if (this.targetData) {
318 if (source != this.el) {
319 LeftTree.get('model').dropNode(this.targetData, source.dragData);
321 // drag around.. - reorder..
322 LeftTree.get('model').moveNode(this.targetData);
326 //Seed.print(this.targetData);
332 // we can send stuff to souce here...
338 if (dnd_success == false)
340 Seed.print ("DnD data transfer failed!\n");
343 Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
347 'cursor-changed' : function(tv, a) {
348 var iter = new Gtk.TreeIter();
350 if (this.selection.count_selected_rows() < 1) {
351 LeftPanel.get('model').load( false);
352 MidPropTree.activeElement = false;
353 MidPropTree.hideWin();
354 var RightPalete = imports.Builder.RightPalete.RightPalete;
355 var pm = RightPalete.get('model');
356 if (!LeftTree.getPaleteProvider()) {
357 // it may not be loaded yet..
360 pm.load( LeftTree.getPaleteProvider().gatherList(
361 LeftTree.get('model').listAllTypes()));
366 //console.log('changed');
367 var s = this.selection;
368 s.get_selected(LeftTree.get('model').el, iter);
372 value = new GObject.Value('');
373 LeftTree.get('model').el.get_value(iter, 2, value);
374 LeftTree.get('model').activeIter = iter;
376 var data = JSON.parse(value.value);
377 MidPropTree.activeElement = data;
378 MidPropTree.hideWin();
379 LeftPanel.get('model').load( data);
381 console.log(value.value);
382 // _g.button.set_label(''+value.get_string());
383 var RightPalete = imports.Builder.RightPalete.RightPalete;
384 var pm = RightPalete.get('model');
385 pm.load( RightPalete.provider.gatherList(
386 LeftTree.get('model').listAllTypes()));
391 //Seed.print( value.get_string());
402 pack : ['set_model'],
405 xtype: Gtk.TreeStore,
408 XObject.prototype.init.call(this);
411 this.el.set_column_types ( 3, [
412 GObject.TYPE_STRING, // title
413 GObject.TYPE_STRING, // tip
414 GObject.TYPE_STRING // source..
417 //if (LeftProjectTree.project).getProvider()
424 changed : function( n, refresh)
426 print("MODEL CHANGED CALLED" + this.activeIter);
427 if (this.activeIter) {
429 this.el.set_value(this.activeIter, 0, [GObject.TYPE_STRING, this.nodeTitle(n)]);
430 this.el.set_value(this.activeIter, 1, [GObject.TYPE_STRING, this.nodeTitle(n)]);
432 this.el.set_value(this.activeIter, 2, [GObject.TYPE_STRING, this.nodeToJSON(n)]);
434 //this.currentTree = this.toJS(false, true)[0];
435 this.file.items = this.toJS(false, false);
436 print("AFTER CHANGED")
437 console.dump(this.file.items);
439 this.currentTree = this.file.items[0];
440 //console.log(this.file.toSource());
443 print("REDNER BROWSER?!");
444 LeftTree.renderView();
446 var RightPalete = imports.Builder.RightPalete.RightPalete;
447 var pm = RightPalete.get('model');
448 if (!RightPalete.provider) {
454 pm.load( RightPalete.provider.gatherList(this.listAllTypes()));
455 //imports['Builder/RightBrowser.js'].renderJS(this.toJS());
461 loadFile : function(f)
468 console.log('missing file');
472 // load the file if not loaded..
473 if (f.items === false) {
475 f.loadItems(function() {
481 if (f.items.length && typeof(f.items[0]) == 'string') {
483 RightEditor.el.show();
484 RightEditor.get('view').load( f.items[0]);
488 //console.dump(f.items);
490 LeftTree.get('view').el.expand_all();
491 var Window = imports.Builder.Window.Window;
492 if (!f.items.length) {
495 Window.get('leftvpaned').el.set_position(80);
497 LeftTree.get('view').el.set_cursor(
498 new Gtk.TreePath.from_string('0'), null, false);
502 Window.get('leftvpaned').el.set_position(200);
506 print("hide right editior");
507 RightEditor.el.hide();
508 print("set current tree");
509 this.currentTree = this.toJS(false, false)[0];
510 console.dump(this.currentTree);
511 this.currentTree = this.currentTree || { items: [] };
512 LeftTree.renderView();
513 //console.dump(this.map);
514 var RightPalete = imports.Builder.RightPalete.RightPalete;
515 var pm = RightPalete.get('model');
518 RightPalete.provider = LeftTree.getPaleteProvider();
520 if (!RightPalete.provider) {
521 print ("********* PALETE PROVIDER MISSING?!!");
523 LeftTree.renderView();
525 pm.load( LeftTree.getPaleteProvider().gatherList(this.listAllTypes()));
529 Window.get('view-notebook').el.set_current_page(
530 LeftTree.get('model').file.getType()== 'Roo' ? 0 : -1);
536 findDropNode : function (treepath_str, targets)
540 var path = treepath_str.replace(/^builder-/, '');
542 if (!XObject.keys(this.treemap).length) {
544 return [ '', Gtk.TreeViewDropPosition.INTO_OR_AFTER];
546 print("FIND treepath: " + path);
547 //console.dump(this.treemap);
549 if (!treepath_str.match(/^builder-/)) {
550 return []; // nothing!
552 if (targets === true) {
555 return this.findDropNodeByPath(path,targets)
557 findDropNodeByPath : function (treepath_str, targets, pref)
560 var path = treepath_str + ''; // dupe it..
561 pref = typeof(pref) == 'undefined' ? Gtk.TreeViewDropPosition.INTO_OR_AFTER : pref;
563 //console.dump(this.treemap);
564 while (path.length) {
565 if (typeof(this.treemap[path]) == 'undefined') {
569 var xname = LeftTree.get('model').file.guessName(this.treemap[path]);
572 targets.forEach(function(tg) {
576 if ((tg == xname) ) {
579 if (tg.indexOf(xname +':') === 0) {
581 prop = tg.split(':').pop();
586 if (last) { // pref is after/before..
587 // then it's after last
589 return []; // do not allow..
591 return [ last, pref , prop];
594 return [ path , Gtk.TreeViewDropPosition.INTO_OR_AFTER , prop];
596 var par = path.split(':');
599 path = par.join(':');
606 dropNode: function(target_data, node) {
608 console.dump(target_data);
609 var tp = target_data[0].length ? new Gtk.TreePath.from_string( target_data[0] ) : false;
611 print("add where: " + target_data[1] );
614 if (target_data[1] < 2) { // before or after..
615 var ar = target_data[0].split(':');
617 parent = new Gtk.TreePath.from_string( ar.join(':') );
620 var n_iter = new Gtk.TreeIter();
621 var iter_par = new Gtk.TreeIter();
622 var iter_after = after ? new Gtk.TreeIter() : false;
623 if (parent !== false) {
624 this.el.get_iter(iter_par, parent);
631 Seed.print(target_data[1] > 0 ? 'insert_after' : 'insert_before');
632 this.el.get_iter(iter_after, after);
633 this.el[ target_data[1] > 0 ? 'insert_after' : 'insert_before'](
634 n_iter, iter_par, iter_after);
637 this.el.append(n_iter, iter_par);
641 if (typeof(node) == 'string') {
642 var ar = node.split('.');
643 var xtype = ar.pop();
646 '|xns' : ar.join('.'),
649 if (target_data.length == 3 && target_data[2].length) {
650 node['*prop'] = target_data[2];
660 this.load(xitems, n_iter);
662 if (xitems || after) {
663 LeftTree.get('view').el.expand_row(this.el.get_path(iter_par), true);
665 // wee need to get the empty proptypes from somewhere..
667 //var olditer = this.activeIter;
668 this.activeIter = n_iter;
669 this.changed(node, true);
673 LeftTree.get('view').el.set_cursor(this.el.get_path(n_iter), null, false);
675 //Builder.MidPropTree._model.load(node);
676 //Builder.MidPropTree._win.hideWin();
677 //Builder.LeftPanel._model.load( node);
683 moveNode: function(target_data) {
685 //print("MOVE NODE");
686 // console.dump(target_data);
687 var old_iter = new Gtk.TreeIter();
688 var s = LeftTree.get('view').selection;
689 s.get_selected(this.el, old_iter);
690 var node = this.nodeToJS(old_iter,false);
691 //console.dump(node);
694 // needs to drop first, otherwise the target_data
695 // treepath will be invalid.
697 this.dropNode(target_data, node);
698 this.el.remove(old_iter);
703 deleteSelected: function() {
707 var old_iter = new Gtk.TreeIter();
708 var s = LeftTree.get('view').selection;
709 s.get_selected(this.el, old_iter);
712 this.el.remove(old_iter);
717 //this.toJS(null, true) // does not do anything?
718 this.activeIter = false;
719 this.changed(false,true);
728 treemap: false, // map of treepath to nodes.
730 listAllTypes : function()
734 var s = LeftTree.get('view').selection;
735 print ("LIST ALL TYPES: " + s.count_selected_rows() );
737 if (s.count_selected_rows() > 0) {
738 var iter = new Gtk.TreeIter();
739 s.get_selected(LeftTree.get('model').el, iter);
741 // set some properties of the tree for use by the dropped element.
742 var value = new GObject.Value('');
743 LeftTree.get('model').el.get_value(iter, 2, value);
744 var data = JSON.parse(value.value);
747 var xname = LeftTree.get('model').file.guessName(data);
748 console.log('selected:' + xname);
752 return []; // could not find it..
759 li.forEach(function(el) {
760 // this is specific to roo!!!?
762 var fullpath = LeftTree.get('model').file.guessName(el);
763 if (fullpath.length && ret.indexOf(fullpath) < 0) {
768 if (el.items && el.items.length) {
777 addall([this.currentTree]);
779 // only if we have nothing, should we add '*top'
783 console.log('all types in tree');
793 * convert tree into a javascript array
796 nodeToJS: function (iter, with_id)
798 var par = new Gtk.TreeIter();
799 var iv = this.getValue(iter, 2);
801 var k = JSON.parse(iv);
802 if (k.json && !this.el.iter_parent( par, iter )) {
807 var treepath_str = this.el.get_path(iter).to_string();
808 k.id = 'builder-'+ treepath_str ;
810 this.treemap[ treepath_str ] = k;
811 k.xtreepath = treepath_str ;
814 if (this.el.iter_has_child(iter)) {
815 citer = new Gtk.TreeIter();
816 this.el.iter_children(citer, iter);
817 k.items = this.toJS(citer,with_id);
822 * iterates through child nodes (or top..)
825 toJS: function(iter, with_id)
827 Seed.print("WITHID: "+ with_id);
834 iter = new Gtk.TreeIter();
835 if (!this.el.get_iter_first(iter)) {
845 var k = this.nodeToJS(iter, with_id);
849 if (!this.el.iter_next(iter)) {
855 // convert the list into a json string..
859 getValue: function (iter, col) {
860 var gval = new GObject.Value('');
861 this.el.get_value(iter, col ,gval);
867 nodeTitle: function(c)
872 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
873 if (sr) txt.push('<s>');
874 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
875 if (c.xtype) { txt.push(c.xtype); }
876 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
877 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
880 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
881 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
882 if (c.label) { txt.push('<b>' + c.label+ '</b>'); }
883 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
884 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
885 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
886 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
887 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
888 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
890 // for flat classes...
891 if (typeof(c['*class']) != 'undefined') { txt.push('<b>' + c['*class']+ '</b>'); }
892 if (typeof(c['*extends']) != 'undefined') { txt.push(': <i>' + c['*extends']+ '</i>'); }
895 if (sr) txt.push('</s>');
896 return (txt.length == 0 ? "Element" : txt.join(" "));
898 //console.log(n.xtype);
902 nodeToJSON : function(c) {
910 return JSON.stringify(o);
913 * load javascript array onto an iter..
914 * @param tr = array of elements
915 * @param iter = iter of parent (or null if not..)
921 load : function(tr,iter)
923 var citer = new Gtk.TreeIter();
924 //this.insert(citer,iter,0);
925 for(var i =0 ; i < tr.length; i++) {
927 this.el.insert(citer,iter,-1);
929 this.el.append(citer);
932 this.el.set_value(citer, 0, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
933 this.el.set_value(citer, 1, [GObject.TYPE_STRING, this.nodeTitle(tr[i]) ]);
934 this.el.set_value(citer, 2, [GObject.TYPE_STRING, this.nodeToJSON(tr[i])]);
935 if (tr[i].items && tr[i].items.length) {
936 this.load(tr[i].items, citer);
947 // this.expand_all();
952 xtype: Gtk.TreeViewColumn,
953 pack : ['append_column'],
955 XObject.prototype.init.call(this);
956 this.el.add_attribute(this.items[0].el , 'markup', 0 );
962 xtype: Gtk.CellRendererText,
963 pack: [ 'pack_start']