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('/Terminal').el.fork_command('seed',
276 [ '/tmp/BuilderGtkView.js'], [], "/tmp", false,false,false);
278 var _top = x.get_global_object()._top;
280 _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
283 _top.el.popup(null, null, null, null, 3, null);
287 buildJS: function(data,withDebug) {
288 var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject', 'GtkSource', 'WebKit', 'Vte' ];
290 i.forEach(function(e) {
291 src += e+" = imports.gi." + e +";\n";
294 src += "console = imports.console;\n"; // path?!!?
295 src += "XObject = imports.XObject.XObject;\n"; // path?!!?
296 src += "XObject.cache = {};\n"; // reset cache!
298 src += "Gtk.init(null,null);\n";
301 src += "XObject.debug=true;\n";
305 src += '_top=new XObject('+ this.mungeToString(data) + ')\n;';
306 src += '_top.init();\n';
308 src += "_top.show_all();\n";
309 src += "Gtk.main();\n";
311 File.write('/tmp/BuilderGtkView.js', src);
312 print("Test code in /tmp/BuilderGtkView.js");
317 renderJS : function(data, withDebug)
319 // can we mess with data?!?!?
323 * sandbox it? - nope then will have dificulting passing. stuff aruond..
329 var src = this.buildJS(data,withDebug);
330 var x = new imports.sandbox.Context();
332 //x.get_global_object().a = "hello world";
335 Seed.check_syntax('var e = ' + src);
339 return this.renderJS(data,true);
341 print(e.message || e.toString());
346 var r = new Gdk.Rectangle();
347 var _top = x.get_global_object()._top;
349 _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
352 _top.el.popup(null, null, null, null, 3, null);
357 var pb = _top.el.get_snapshot(r);
364 var Window = imports.Builder.Window.Window;
365 var gc = new Gdk.GC.c_new(Window.el.window);
367 // 10 points all round..
368 var full = new Gdk.Pixmap.c_new (Window.el.window, r.width+20, r.height+20, pb.get_depth());
369 // draw a white background..
370 // gc.set_rgb_fg_color({ red: 0, white: 0, black : 0 });
371 Gdk.draw_rectangle(full, gc, true, 0, 0, r.width+20, r.height+20);
373 Gdk.draw_drawable (full, gc, pb, 0, 0, 10, 10, r.width, r.height);
375 //gc.set_rgb_fg_color({ red: 255, white: 255, black : 255 });
376 Gdk.draw_rectangle(full, gc, true, 0, 0, 10, 10);
377 this.get('view').el.set_from_pixmap(full, null);
378 //this.get('view-vbox').el.set_size_request( r.width+20, r.height+20);
379 //var img = new Gtk.Image.from_file("/home/alan/solarpanels.jpeg");
384 mungeToString: function(obj, isListener, pad)
389 isListener = isListener || false;
391 // am I munging a object or array...
392 if (obj.constructor.toString() === Array.toString()) {
393 for (var i= 0; i < obj.length; i++) {
407 typeof(obj['|xns']) != 'undefined' &&
408 typeof(obj['xtype']) != 'undefined'
410 els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
411 skip.push('|xns','xtype');
418 keys.forEach(function(i) {
420 if (!isArray && skip.indexOf(i) > -1) {
424 if (obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
427 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");
428 var lines = str.split("\n");
429 if (lines.length > 1) {
430 str = lines.join("\n" + pad);
432 els.push(JSON.stringify(i) + ":" + str);
436 // does not hapepnd with arrays..
437 if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
440 // this needs to go...
441 if (typeof(el) == 'string' && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
445 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
446 var lines = str.split("\n");
447 if (lines.length > 1) {
448 str = lines.join("\n" + pad);
451 els.push(JSON.stringify(i.substring(1)) + ":" + str);
454 var left = isArray ? '' : (JSON.stringify(i) + " : " )
455 if (typeof(el) == 'object') {
456 els.push(left + _this.mungeToString(el, i == 'listeners', pad + ' '));
459 els.push(JSON.stringify(i) + ":" + JSON.stringify(obj[i]));
461 var spad = pad.substring(0, pad.length-4);
462 return (isArray ? '[' : '{') + "\n" +
463 pad + els.join(",\n" + pad ) +
464 "\n" + spad + (isArray ? ']' : '}');