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());
85 return new Gtk.Image.from_stock (Gtk.STOCK_HOME, 100)
92 XObject.prototype.init.call(this);
97 this.el, /* widget that will accept a drop */
98 Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT,
99 null, /* lists of target to support */
100 0, /* size of list */
101 Gdk.DragAction.COPY /* what to do with data after dropped */
104 // print("RB: TARGETS : " + LeftTree.atoms["STRING"]);
105 Gtk.drag_dest_set_target_list(this.el, LeftTree.targetList);
106 //Gtk.drag_dest_add_text_targets(this.el);
112 "drag-leave" : function () {
113 Seed.print("TARGET: drag-leave");
114 // stop monitoring of mouse montion in rendering..
117 'drag-motion' : function (w, ctx, x, y, time, ud)
121 // console.log('DRAG MOTION');
123 // if lastCurrentNode == this.currentNode.. -- don't change anything..
126 // A) find out from drag all the places that node could be dropped.
127 var src = Gtk.drag_get_source_widget(ctx);
129 Gdk.drag_status(ctx, 0, time);
132 // b) get what we are over.. (from activeNode)
133 // tree is empty.. - list should be correct..
134 if (!LeftTree.get('model').currentTree) {
135 Gdk.drag_status(ctx, Gdk.DragAction.COPY,time);
139 // c) ask tree where it should be dropped... - eg. parent.. (after node ontop)
140 var activeNode = this.getActiveNode(x, y);
143 var tg = LeftTree.get('model').findDropNode(activeNode, src.dropList);
146 Gdk.drag_status(ctx, 0,time);
147 LeftTree.get('view').highlight(false);
151 // if we have a target..
152 // -> highlight it! (in browser)
153 // -> highlight it! (in tree)
155 Gdk.drag_status(ctx, Gdk.DragAction.COPY,time);
156 LeftTree.get('view').highlight(tg);
157 this.targetData = tg;
158 // for tree we should handle this...
162 "drag-drop" : function (w, ctx,x,y,time, ud)
165 Seed.print("TARGET: drag-drop");
166 is_valid_drop_site = true;
171 w, /* will receive 'drag-data-received' signal */
172 ctx, /* represents the current state of the DnD */
173 LeftTree.atoms["STRING"], /* the target type we want */
174 time /* time stamp */
178 /* No target offered by source => error */
181 return is_valid_drop_site;
185 "drag-data-received" : function (w, ctx, x, y, sel_data, target_type, time, ud)
187 Seed.print("GtkView: drag-data-received");
188 var delete_selection_data = false;
189 var dnd_success = false;
190 /* Deal with what we are given from source */
191 if( sel_data && sel_data.length ) {
193 if (ctx.action == Gdk.DragAction.ASK) {
194 /* Ask the user to move or copy, then set the ctx action. */
197 if (ctx.action == Gdk.DragAction.MOVE) {
198 delete_selection_data = true;
200 var source = Gtk.drag_get_source_widget(ctx);
202 Seed.print("Browser: source.DRAGDATA? " + source.dragData);
203 if (this.targetData) {
204 Seed.print(this.targetData);
205 LeftTree.get('model').dropNode(this.targetData, source.dragData);
214 if (dnd_success == false)
216 Seed.print ("DnD data transfer failed!\n");
219 Gtk.drag_finish (ctx, dnd_success, delete_selection_data, time);
223 //'line-mark-activated' : line_mark_activated,
228 getActiveNode : function(x,y)
230 // workout what node is here..
241 showInWindow: function ()
245 var src= this.buildJS(this.get('/LeftTree.model').toJS()[0], true);
248 this.get('/Terminal').feed("Running\n");
250 //var x = new imports.sandbox.Context();
254 Seed.check_syntax('var e = ' + src);
257 this.get('/Terminal').feed(e.message || e.toString() + "\n");
258 this.get('/Terminal').feed(console._dump(e)+"\n");
260 var lines = src.split("\n");
261 var start = Math.max(0, e.line - 10);
262 var end = Math.min(lines.length, e.line + 10);
263 for (var i =start ; i < end; i++) {
265 this.get('/Terminal').feed(">>>>>" + lines[i] + "\n");
271 this.get('/Terminal').feed(lines[i] + "\n");
278 this.get('/BottomPane').el.set_current_page(1);
279 this.get('/Terminal').el.fork_command( null , [], [], "/tmp", false,false,false);
280 var cmd = "/usr/bin/seed /tmp/BuilderGtkView.js\n";
281 this.get('/Terminal').el.feed_child(cmd, cmd.length);
282 //'/usr/bin/seed', [ '/tmp/BuilderGtkView.js'], [], "/tmp", false,false,false);
284 var _top = x.get_global_object()._top;
286 _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
289 _top.el.popup(null, null, null, null, 3, null);
294 buildJS: function(data,withDebug)
296 var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject', 'GtkSource', 'WebKit', 'Vte' ];
298 i.forEach(function(e) {
299 src += e+" = imports.gi." + e +";\n";
303 src+= "imports.searchPath.push(" + JSON.stringify(GLib.path_get_dirname(__script_path__)) + ");\n";
306 src += "console = imports.console;\n"; // path?!!?
307 src += "XObject = imports.XObject.XObject;\n"; // path?!!?
308 src += "XObject.cache = {};\n"; // reset cache!
313 src += "Gtk.init(null,null);\n";
316 src += "XObject.debug=true;\n";
319 this.withDebug = withDebug;
320 src += '_top=new XObject('+ this.mungeToString(data) + ')\n;';
321 src += '_top.init();\n';
323 src += "_top.el.show_all();\n";
324 src += "Gtk.main();\n";
326 File.write('/tmp/BuilderGtkView.js', src);
327 print("Test code in /tmp/BuilderGtkView.js");
332 renderJS : function(data, withDebug)
334 // can we mess with data?!?!?
338 * sandbox it? - nope then will have dificulting passing. stuff aruond..
344 this.withDebug = false;
349 var src = this.buildJS(data,withDebug);
350 var x = new imports.sandbox.Context();
352 //x.get_global_object().a = "hello world";
355 Seed.check_syntax('var e = ' + src);
359 // return this.renderJS(data,true);
361 print(e.message || e.toString());
366 var r = new Gdk.Rectangle();
367 var _top = x.get_global_object()._top;
369 _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
374 _top.el.popup(null, null, null, null, 3, null);
379 var pb = _top.items[0].el.get_snapshot(r);
387 var Window = imports.Builder.Window.Window;
388 var gc = new Gdk.GC.c_new(Window.el.window);
390 // 10 points all round..
391 var full = new Gdk.Pixmap.c_new (Window.el.window, r.width+20, r.height+20, pb.get_depth());
392 // draw a white background..
393 // gc.set_rgb_fg_color({ red: 0, white: 0, black : 0 });
394 Gdk.draw_rectangle(full, gc, true, 0, 0, r.width+20, r.height+20);
396 Gdk.draw_drawable (full, gc, pb, 0, 0, 10, 10, r.width, r.height);
398 //gc.set_rgb_fg_color({ red: 255, white: 255, black : 255 });
399 Gdk.draw_rectangle(full, gc, true, 0, 0, 10, 10);
400 this.get('view').el.set_from_pixmap(full, null);
401 //this.get('view-vbox').el.set_size_request( r.width+20, r.height+20);
402 //var img = new Gtk.Image.from_file("/home/alan/solarpanels.jpeg");
407 mungeToString: function(obj, isListener, pad)
412 isListener = isListener || false;
414 // am I munging a object or array...
415 if (obj.constructor.toString() === Array.toString()) {
416 for (var i= 0; i < obj.length; i++) {
430 typeof(obj['|xns']) != 'undefined' &&
431 typeof(obj['xtype']) != 'undefined'
433 els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
434 skip.push('|xns','xtype');
441 keys.forEach(function(i) {
443 if (!isArray && skip.indexOf(i) > -1) {
447 if (!_this.withDebug) {
448 // do not write listeners unless we are debug mode.
451 //if (obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
454 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");
455 var lines = str.split("\n");
456 if (lines.length > 1) {
457 str = lines.join("\n" + pad);
459 els.push(JSON.stringify(i) + ":" + str);
463 // does not hapepnd with arrays..
464 if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
467 // this needs to go...
468 //if (typeof(el) == 'string' && 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);
478 els.push(JSON.stringify(i.substring(1)) + ":" + str);
481 var left = isArray ? '' : (JSON.stringify(i) + " : " )
482 if (typeof(el) == 'object') {
483 els.push(left + _this.mungeToString(el, i == 'listeners', pad + ' '));
486 els.push(JSON.stringify(i) + ":" + JSON.stringify(obj[i]));
488 var spad = pad.substring(0, pad.length-4);
489 return (isArray ? '[' : '{') + "\n" +
490 pad + els.join(",\n" + pad ) +
491 "\n" + spad + (isArray ? ']' : '}');
497 buildView : function()
499 var tree = this.get('/LeftTree.model').toJS()[0];
500 // in theory tree is actually window..
501 this.viewAdd(tree.items[0], this.get('view').el);
504 viewAdd : function(item, par)
506 // does something similar to xobject..
507 var pack = x.pack || 'add';
509 if (item.pack===false || item.pack === 'false') { // no ;
513 var ns = imports.gi[obj['|xns']];
514 var ctr = ns[obj['xtype']];
518 if (typeof(kv) == 'object' || typeof(kv) == 'function') {
537 var el = new ctr(ctr_args);
547 if (typeof(item.pack) == 'string') {
549 item.pack.split(',').forEach(function(e, i) {
551 if (e == 'false') { args.push( false); return; }
552 if (e == 'true') { args.push( true); return; }
553 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
556 //print(args.join(","));
558 pack_m = args.shift();
560 pack_m = item.pack.shift();
565 if (pack_m && typeof(par[pack_m]) == 'undefined') {
566 Seed.print('pack method not available : ' + item.xtype + '.' + pack_m);
572 //if (XObject.debug) print(pack_m + '[' + args.join(',') +']');
573 //Seed.print('args: ' + args.length);
575 par[pack_m].apply(par, args);
579 item.forEach(function(ch) {
580 _this.viewAdd(ch, el);