BrowserWindow.bjs
[app.webkitpdf] / BrowserWindow.bjs
1 {
2  "name" : "BrowserWindow",
3  "parent" : "",
4  "title" : "",
5  "path" : "/home/alan/gitlive/app.webkitpdf/BrowserWindow.bjs",
6  "permname" : "",
7  "modOrder" : "",
8  "build_module" : "NEW GROUP",
9  "items" : [
10   {
11    "listeners" : {
12     "destroy" : "() => {\n    Gtk.main_quit();\n}",
13     "show" : "() => {\n\t\n\tprint(\"resizing window\\n\");\n\tthis.el.resize(1200,500);\n\tprint(\"loading url\\n\");\n\tthis.view.el.load_uri(webkitpdf.opt_url);\n\t\n\t// if we have not finished in 25 seconds\n\t// call printit...\n\t  GLib.Timeout.add_seconds(25, () => { \n\t\t\t_this.view.printit();\n\t\t\treturn true;\n\t});\n\t\n\n}"
14    },
15    "xtype" : "Window",
16    "$ xns" : "Gtk",
17    "Gtk.WindowType type" : "Gtk.WindowType.TOPLEVEL",
18    "items" : [
19     {
20      "id" : "scrolled_window",
21      "* init" : "{\n\tthis.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);\n}\n",
22      "* pack" : "add",
23      "xtype" : "ScrolledWindow",
24      "$ xns" : "Gtk",
25      "items" : [
26       {
27        "listeners" : {
28         "mime_type_policy_decision_requested" : "(p0, netreq, mimetype, pol ) => {\n //  print(\"resource response  %s, %s\\n\", \t webres.get_mime_type(), netres.uri);\n\tprint(\"mimetype:  %s\", mimetype);\n // netres.message.got_headers.connect(() => { \n //   print(\"GOT HEADERS?  [%s] : %s\\n\", \n\t//  netres.message.request_headers.get_content_type(null),netres.uri);\n\t//  });\n\t\n\treturn true; // default behaviour..\n  \n   \n}",
29         "resource_request_starting" : "(p0, webres, netreq ) => {\n  print(\"resource req. started %s\\n\", netreq.uri);\n   \n}",
30         "load_finished" : "( ) => {\n\t\t \n       this.printit();\n \n}"
31        },
32        "id" : "view",
33        "| void printit" : "() {\n   print(\"load_finsihed\\n\");\n   \n   if (this.print_started) {\n      print(\"load_finsihed - already printing..\\n\");\n      return;\n  }\n   \n    this.print_started = true; // flag to stop double call.\n    \n    \n\t// inject any javascript if needed..\n\tif (webkitpdf.opt_inject_js != null) {\n\t\n\t\tprint(\"injecting javascript files \\n\");\n\t\tforeach(var inject_js in  webkitpdf.opt_inject_js) {\n\t\tprint(\"inject? %s\\n\", inject_js);\n\t\t\tstring str;\n\t\t\ttry {\n\t\t\t\tFileUtils.get_contents( inject_js, out str);\n\t\t\t} catch(FileError e) {\n\t\t\t\tprint(\"Failed to inject %s\\n\",  inject_js);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.el.execute_script(str);\n\t\t}\n\t}\n\t\n\t\n\t\n\tprint(\"calling print in %d seconds\\n\", (int)webkitpdf.opt_delay);\n\tif (webkitpdf.opt_target_pdf != null) {\n\t\tGLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n\n\t\t\tthis.printpdf();\n\t\t\t return false;\n\t\t}, GLib.Priority.DEFAULT);\n\t}\n\t\t\n\t\t\n    GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n   \n\t\t\n\t\t// ----------------- JPEG \n\t\t// resize the window...\n\t\t// window.document.documentElement.scrollHeight\n\t\tvar scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;\n\t\tprint(\"Scroll height %d\\n\", scroll_height);\n\t\tif (scroll_height> 1024) {\n\t\t\t//_this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));\n\t\t\t_this.scrolled_window.el.set_size_request( \n\t\t\t\t\twebkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200, \n\t\t\t\t\twebkitpdf.opt_height > 0 ?  webkitpdf.opt_height  : int.min(scroll_height, 6000)\n\t\t\t);\n\t\t\tprint(\"Resize to %d\\n\", scroll_height);\n\t\t    GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n\t\t\t\tthis.printpng();\n\t\t\t\treturn false;\n\t\t\t  }, GLib.Priority.DEFAULT);\n  \t\t    return false;\n\t\t}\n\t\tthis.printpng();\t\t\n\t\treturn false;\n\t  }, GLib.Priority.DEFAULT);\n\n\n    \n    \n}\n",
34        "* pack" : "add",
35        "xtype" : "WebView",
36        "| bool printpdf" : "() {\n\t \n\t\tvar  pe = new  Gtk.PrintOperation();\n\t\tpe.export_filename = webkitpdf.opt_target_pdf;\n\t\tprint(\"got callback for print\");\n\t\tpe.ref();\n\t\t/*pe.failed.connect(() => {\n\t\t\tprint(\"print failed\\n\");\n\t\t\t Gtk.main_quit();\n    \t\t\n\t\t})\n\t\t*/;\n\t\t pe.done.connect(() => {\n\t\t\tprint(\"print completed\\n\");\n\t\t\t Gtk.main_quit();\n    \t\t\n\t\t});\n\t\t\n\t\t//pe.run_dialog(_this.el);\n\t \n\t\t\n\t    var list = Gtk.PaperSize.get_paper_sizes(false);\n\t    var psetup = new Gtk.PageSetup();\n\t    for(var i = 0; i < list.length(); i++ ) {\n\t        var entry = list.nth_data(i).copy();\n\t        if (entry.get_name() == \"iso_a2\") {\n\t            psetup.set_paper_size(entry);\n\t        }\n\t    }\n\t        \n\n\n\t    pe.set_default_page_setup(psetup);\n\t    print(\"Calling Print?\\n\");\n\t    //pe.run_dialog(_this.el);\n\t    try {\n\t\t    this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);\n\t    } catch (Error e) {\n\t\t\t// print failed...\n\t\t}\n\t    \n\t    \n\t     \n\t\treturn false;\n}\n",
37        "| void printpng" : "() {\n    //var filename = \"/tmp/test.pdf\";\n    //print(\"load_changed %d ?= %d\\n\", le, LoadEvent.FINISHED);\n\n    //if (le != LoadEvent.FINISHED) {\n    //    return;\n   // }\n   \n// what size is the documet.\n\n     print(\"making screenshot\\n\");\n     \n    // fix vapi - get_snapshot add '?' to all null.\n    var pixmap = _this.view.el.get_snapshot( null );\n\n    \n    \n    int w,h;\n    \n    // add out to gdkpixmap.get_size\n\tpixmap.get_size( out  w , out   h);\n\tvar pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);\n\tprint(\"pixbuf size: %d x %d\\n \", w,h);\n\t \n    \n    \n     pixbuf.save(webkitpdf.opt_target_png, \"png\");\n     print(\"Saved to %s\\n\", webkitpdf.opt_target_png);\n    //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {\n    //    var sf = view.get_snapshot.end(res);\n\n    //    sf.write_to_png(Browser.opt_target_png);\n    //});\n    Gtk.main_quit();\n     \n   \n    \n    \n\n}\n",
38        "bool print_started" : false,
39        "$ xns" : "WebKit"
40       }
41      ]
42     }
43    ]
44   }
45  ]
46 }