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);
75 pack : 'add_with_viewport' ,
82 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)
293 var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject', 'GtkSource', 'WebKit', 'Vte' ];
295 i.forEach(function(e) {
296 src += e+" = imports.gi." + e +";\n";
300 src+= "imports.searchPath.push(" + JSON.stringify(GLib.path_get_dirname(__script_path__)) + ");\n";
303 src += "console = imports.console;\n"; // path?!!?
304 src += "XObject = imports.XObject.XObject;\n"; // path?!!?
305 src += "XObject.cache = {};\n"; // reset cache!
310 src += "Gtk.init(null,null);\n";
313 src += "XObject.debug=true;\n";
316 this.withDebug = withDebug;
317 src += '_top=new XObject('+ this.mungeToString(data) + ')\n;';
318 src += '_top.init();\n';
320 src += "_top.el.show_all();\n";
321 src += "Gtk.main();\n";
323 File.write('/tmp/BuilderGtkView.js', src);
324 print("Test code in /tmp/BuilderGtkView.js");
329 renderJS : function(data, withDebug)
331 // can we mess with data?!?!?
335 * sandbox it? - nope then will have dificulting passing. stuff aruond..
341 this.withDebug = false;
343 if (this.renderedEl) {
344 this.get('view').el.remove(this.renderedEl);
345 this.renderedEl.destroy();
346 this.renderedEl = false;
349 var tree = this.get('/LeftTree.model').toJS(false,true)[0];
350 // in theory tree is actually window..
351 this.renderedEl = this.viewAdd(tree.items[0], this.get('view').el);
352 this.get('view').el.set_size_request(
353 tree.default_width * 1 || 400, tree.default_height * 1 || 400
356 this.renderedEl.set_size_request(
357 tree.default_width || 600,
358 tree.default_height || 400
360 this.get('view').el.show_all();
365 var src = this.buildJS(data,withDebug);
366 var x = new imports.sandbox.Context();
368 //x.get_global_object().a = "hello world";
371 Seed.check_syntax('var e = ' + src);
375 // return this.renderJS(data,true);
377 print(e.message || e.toString());
382 var r = new Gdk.Rectangle();
383 var _top = x.get_global_object()._top;
385 _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
390 _top.el.popup(null, null, null, null, 3, null);
395 var pb = _top.items[0].el.get_snapshot(r);
403 var Window = imports.Builder.Window.Window;
404 var gc = new Gdk.GC.c_new(Window.el.window);
406 // 10 points all round..
407 var full = new Gdk.Pixmap.c_new (Window.el.window, r.width+20, r.height+20, pb.get_depth());
408 // draw a white background..
409 // gc.set_rgb_fg_color({ red: 0, white: 0, black : 0 });
410 Gdk.draw_rectangle(full, gc, true, 0, 0, r.width+20, r.height+20);
412 Gdk.draw_drawable (full, gc, pb, 0, 0, 10, 10, r.width, r.height);
414 //gc.set_rgb_fg_color({ red: 255, white: 255, black : 255 });
415 Gdk.draw_rectangle(full, gc, true, 0, 0, 10, 10);
416 this.get('view').el.set_from_pixmap(full, null);
417 //this.get('view-vbox').el.set_size_request( r.width+20, r.height+20);
418 //var img = new Gtk.Image.from_file("/home/alan/solarpanels.jpeg");
423 mungeToString: function(obj, isListener, pad)
428 isListener = isListener || false;
430 // am I munging a object or array...
431 if (obj.constructor.toString() === Array.toString()) {
432 for (var i= 0; i < obj.length; i++) {
446 typeof(obj['|xns']) != 'undefined' &&
447 typeof(obj['xtype']) != 'undefined'
449 els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
450 skip.push('|xns','xtype');
457 keys.forEach(function(i) {
459 if (!isArray && skip.indexOf(i) > -1) {
463 if (!_this.withDebug) {
464 // do not write listeners unless we are debug mode.
467 //if (obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
470 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");
471 var lines = str.split("\n");
472 if (lines.length > 1) {
473 str = lines.join("\n" + pad);
475 els.push(JSON.stringify(i) + ":" + str);
479 // does not hapepnd with arrays..
480 if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
483 // this needs to go...
484 //if (typeof(el) == 'string' && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
488 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
489 var lines = str.split("\n");
490 if (lines.length > 1) {
491 str = lines.join("\n" + pad);
494 els.push(JSON.stringify(i.substring(1)) + ":" + str);
497 var left = isArray ? '' : (JSON.stringify(i) + " : " )
498 if (typeof(el) == 'object') {
499 els.push(left + _this.mungeToString(el, i == 'listeners', pad + ' '));
502 els.push(JSON.stringify(i) + ":" + JSON.stringify(obj[i]));
504 var spad = pad.substring(0, pad.length-4);
505 return (isArray ? '[' : '{') + "\n" +
506 pad + els.join(",\n" + pad ) +
507 "\n" + spad + (isArray ? ']' : '}');
513 buildView : function()
518 viewAdd : function(item, par)
520 // does something similar to xobject..
521 item.pack = (typeof(item.pack) == 'undefined') ? 'add' : item.pack;
523 if (item.pack===false || item.pack === 'false') { // no ;
526 print("CREATE: " + item['|xns'] + '.' + item['xtype']);
527 var ns = imports.gi[item['|xns']];
528 var ctr = ns[item['xtype']];
532 if (typeof(kv) == 'object' || typeof(kv) == 'function') {
551 var el = new ctr(ctr_args);
554 console.dump(item.pack);
561 if (typeof(item.pack) == 'string') {
563 item.pack.split(',').forEach(function(e, i) {
565 if (e == 'false') { args.push( false); return; }
566 if (e == 'true') { args.push( true); return; }
567 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
570 //print(args.join(","));
572 pack_m = args.shift();
574 pack_m = item.pack.shift();
579 if (pack_m && typeof(par[pack_m]) == 'undefined') {
580 Seed.print('pack method not available : ' + item.xtype + '.' + pack_m);
586 //if (XObject.debug) print(pack_m + '[' + args.join(',') +']');
587 //Seed.print('args: ' + args.length);
589 par[pack_m].apply(par, args);
593 item.items = item.items || [];
594 item.items.forEach(function(ch) {
595 _this.viewAdd(ch, el);
600 // add the signal handlers.
601 // is it a widget!?!!?
607 el.signal.expose_event.connect(XObject.createDelegate(this.widgetExposeEvent, this, [ item ], true));
608 el.signal.drag_motion.connect(XObject.createDelegate(this.widgetDragMotionEvent, this,[ item ], true));
609 el.signal.drag_drop.connect(XObject.createDelegate(this.widgetDragDropEvent, this, [ item ], true));
610 el.signal.button_press_event.connect(XObject.createDelegate(this.widgetPressEvent, this, [ item ], true ));
620 widgetExposeEvent : function()
622 print("WIDGET EXPOSE"); // draw highlight??
625 widgetDragMotionEvent : function()
627 print("WIDGET DRAGMOTION");
630 widgetDragDropEvent : function()
632 print("WIDGET DRAGDROP");
635 widgetPressEvent : function(w,e,u,d)
637 print("WIDGET PRESs" + d.xtreepath );