1 //<script type="text/javascript">
5 GObject = imports.gi.GObject;
9 * we use a hidden window to render the created dialog...
10 * then use snapshot to render it to an image...
15 XObject = imports.XObject.XObject;
16 File = imports.File.File;
17 console = imports.console;
19 LeftTree = imports.Builder.LeftTree.LeftTree ;
20 LeftPanel = imports.Builder.LeftPanel.LeftPanel;
21 //console.dump(imports.Builder.LeftTree);
24 RightGtkView = new XObject({
27 pack : [ 'append_page', new Gtk.Label({ label : "Gtk View" }) ],
32 pack : [ 'pack_start', false, true, 0 ],
38 label : 'Show in New Window',
39 pack : [ 'pack_start', false, false, 0 ],
42 'button-press-event' : function(w, ev ){
44 RightGtkView.showInWindow();
46 // show the MidPropTree..
57 xtype: Gtk.ScrolledWindow,
60 shadow_type : Gtk.ShadowType.IN ,
62 XObject.prototype.init.call(this);
64 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
73 XObject.prototype.init.call(this);
74 this.el.set_hadjustment(this.parent.el.get_hadjustment());
75 this.el.set_vadjustment(this.parent.el.get_vadjustment());
83 return new Gtk.Image.from_stock (Gtk.STOCK_HOME, 100)
89 XObject.prototype.init.call(this);
94 this.el, /* widget that will accept a drop */
95 Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT,
96 null, /* lists of target to support */
98 Gdk.DragAction.COPY /* what to do with data after dropped */
101 // print("RB: TARGETS : " + LeftTree.atoms["STRING"]);
102 Gtk.drag_dest_set_target_list(this.el, LeftTree.targetList);
103 //Gtk.drag_dest_add_text_targets(this.el);
109 "drag-leave" : function () {
110 Seed.print("TARGET: drag-leave");
111 // stop monitoring of mouse montion in rendering..
114 'drag-motion' : function (w, ctx, x, y, time, ud)
118 // console.log('DRAG MOTION');
120 // if lastCurrentNode == this.currentNode.. -- don't change anything..
123 // A) find out from drag all the places that node could be dropped.
124 var src = Gtk.drag_get_source_widget(ctx);
126 Gdk.drag_status(ctx, 0, time);
129 // b) get what we are over.. (from activeNode)
130 // tree is empty.. - list should be correct..
131 if (!LeftTree.get('model').currentTree) {
132 Gdk.drag_status(ctx, Gdk.DragAction.COPY,time);
136 // c) ask tree where it should be dropped... - eg. parent.. (after node ontop)
137 var activeNode = this.getActiveNode(x, y);
140 var tg = LeftTree.get('model').findDropNode(activeNode, src.dropList);
143 Gdk.drag_status(ctx, 0,time);
144 LeftTree.get('view').highlight(false);
148 // if we have a target..
149 // -> highlight it! (in browser)
150 // -> highlight it! (in tree)
152 Gdk.drag_status(ctx, Gdk.DragAction.COPY,time);
153 LeftTree.get('view').highlight(tg);
154 this.targetData = tg;
155 // for tree we should handle this...
159 "drag-drop" : function (w, ctx,x,y,time, ud)
162 Seed.print("TARGET: drag-drop");
163 is_valid_drop_site = true;
168 w, /* will receive 'drag-data-received' signal */
169 ctx, /* represents the current state of the DnD */
170 LeftTree.atoms["STRING"], /* the target type we want */
171 time /* time stamp */
175 /* No target offered by source => error */
178 return is_valid_drop_site;
182 "drag-data-received" : function (w, ctx, x, y, sel_data, target_type, time, ud)
184 Seed.print("GtkView: drag-data-received");
185 var delete_selection_data = false;
186 var dnd_success = false;
187 /* Deal with what we are given from source */
188 if( sel_data && sel_data.length ) {
190 if (ctx.action == Gdk.DragAction.ASK) {
191 /* Ask the user to move or copy, then set the ctx action. */
194 if (ctx.action == Gdk.DragAction.MOVE) {
195 delete_selection_data = true;
197 var source = Gtk.drag_get_source_widget(ctx);
199 Seed.print("Browser: source.DRAGDATA? " + source.dragData);
200 if (this.targetData) {
201 Seed.print(this.targetData);
202 LeftTree.get('model').dropNode(this.targetData, source.dragData);
211 if (dnd_success == false)
213 Seed.print ("DnD data transfer failed!\n");
216 Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
220 //'line-mark-activated' : line_mark_activated,
225 getActiveNode : function(x,y)
227 // workout what node is here..
238 showInWindow: function ()
242 var src= this.buildJS(this.get('/LeftTree.model').toJS()[0], true);
245 this.get('/Terminal').feed("Running\n");
247 //var x = new imports.sandbox.Context();
251 Seed.check_syntax('var e = ' + src);
254 this.get('/Terminal').feed(e.message || e.toString() + "\n");
255 this.get('/Terminal').feed(console._dump(e)+"\n");
257 var lines = src.split("\n");
258 var start = Math.max(0, e.line - 10);
259 var end = Math.min(lines.length, e.line + 10);
260 for (var i =start ; i < end; i++) {
262 this.get('/Terminal').feed(">>>>>" + lines[i] + "\n");
268 this.get('/Terminal').feed(lines[i] + "\n");
275 this.get('/BottomPane').el.set_current_page(1);
276 this.get('/Terminal').el.fork_command( null , [], [], "/tmp", false,false,false);
277 var cmd = "/usr/bin/seed /tmp/BuilderGtkView.js\n";
278 this.get('/Terminal').el.feed_child(cmd, cmd.length);
279 //'/usr/bin/seed', [ '/tmp/BuilderGtkView.js'], [], "/tmp", false,false,false);
281 var _top = x.get_global_object()._top;
283 _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
286 _top.el.popup(null, null, null, null, 3, null);
291 buildJS: function(data,withDebug) {
292 var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject', 'GtkSource', 'WebKit', 'Vte' ];
294 i.forEach(function(e) {
295 src += e+" = imports.gi." + e +";\n";
299 src+= "imports.searchPath.push(" + JSON.stringify(GLib.path_get_dirname(__script_path__)) + ");\n";
302 src += "console = imports.console;\n"; // path?!!?
303 src += "XObject = imports.XObject.XObject;\n"; // path?!!?
304 src += "XObject.cache = {};\n"; // reset cache!
309 src += "Gtk.init(null,null);\n";
312 src += "XObject.debug=true;\n";
315 this.withDebug = withDebug;
316 src += '_top=new XObject('+ this.mungeToString(data) + ')\n;';
317 src += '_top.init();\n';
319 src += "_top.el.show_all();\n";
320 src += "Gtk.main();\n";
322 File.write('/tmp/BuilderGtkView.js', src);
323 print("Test code in /tmp/BuilderGtkView.js");
328 renderJS : function(data, withDebug)
330 // can we mess with data?!?!?
334 * sandbox it? - nope then will have dificulting passing. stuff aruond..
340 this.withDebug = false;
343 var src = this.buildJS(data,withDebug);
344 var x = new imports.sandbox.Context();
346 //x.get_global_object().a = "hello world";
349 Seed.check_syntax('var e = ' + src);
353 // return this.renderJS(data,true);
355 print(e.message || e.toString());
360 var r = new Gdk.Rectangle();
361 var _top = x.get_global_object()._top;
363 //_top.el.set_screen(Gdk.Screen.get_default()); // just in case..
364 //_top.el.show_all();
365 var tp = new Gtk.OffscreenWindow();
368 tp.signal.show.connect(function() {
370 var pb = tp.get_pixmap();
371 print("GOT PIXMAP" + pb);
372 console.dump(pb.get_size());
373 _this.get('view').el.set_from_pixmap(pb, null);
379 _top.el.remove(_top.items[0].el);
380 tp.add(_top.items[0].el);
389 //var pb = tp.get_pixmap();
392 _top.el.popup(null, null, null, null, 3, null);
397 var pb = _top.items[0].el.get_snapshot(r);
405 var Window = imports.Builder.Window.Window;
406 var gc = new Gdk.GC.c_new(Window.el.window);
408 // 10 points all round..
409 var full = new Gdk.Pixmap.c_new (Window.el.window, r.width+20, r.height+20, pb.get_depth());
410 // draw a white background..
411 // gc.set_rgb_fg_color({ red: 0, white: 0, black : 0 });
412 Gdk.draw_rectangle(full, gc, true, 0, 0, r.width+20, r.height+20);
414 Gdk.draw_drawable (full, gc, pb, 0, 0, 10, 10, r.width, r.height);
416 //gc.set_rgb_fg_color({ red: 255, white: 255, black : 255 });
417 Gdk.draw_rectangle(full, gc, true, 0, 0, 10, 10);
418 this.get('view').el.set_from_pixmap(full, null);
419 //this.get('view-vbox').el.set_size_request( r.width+20, r.height+20);
420 //var img = new Gtk.Image.from_file("/home/alan/solarpanels.jpeg");
425 mungeToString: function(obj, isListener, pad)
430 isListener = isListener || false;
432 // am I munging a object or array...
433 if (obj.constructor.toString() === Array.toString()) {
434 for (var i= 0; i < obj.length; i++) {
448 typeof(obj['|xns']) != 'undefined' &&
449 typeof(obj['xtype']) != 'undefined'
451 els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
452 skip.push('|xns','xtype');
459 keys.forEach(function(i) {
461 if (!isArray && skip.indexOf(i) > -1) {
465 if (!_this.withDebug) {
466 // do not write listeners unless we are debug mode.
469 //if (obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
472 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");
473 var lines = str.split("\n");
474 if (lines.length > 1) {
475 str = lines.join("\n" + pad);
477 els.push(JSON.stringify(i) + ":" + str);
481 // does not hapepnd with arrays..
482 if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
485 // this needs to go...
486 //if (typeof(el) == 'string' && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
490 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
491 var lines = str.split("\n");
492 if (lines.length > 1) {
493 str = lines.join("\n" + pad);
496 els.push(JSON.stringify(i.substring(1)) + ":" + str);
499 var left = isArray ? '' : (JSON.stringify(i) + " : " )
500 if (typeof(el) == 'object') {
501 els.push(left + _this.mungeToString(el, i == 'listeners', pad + ' '));
504 els.push(JSON.stringify(i) + ":" + JSON.stringify(obj[i]));
506 var spad = pad.substring(0, pad.length-4);
507 return (isArray ? '[' : '{') + "\n" +
508 pad + els.join(",\n" + pad ) +
509 "\n" + spad + (isArray ? ']' : '}');