Example #1
1
int main(int argc, char *argv[])
{
  //GtkBuilder *builder;
  //GtkWidget *scrolledWindow;

  //gtk_init (&argc, &argv);

  ///* Construct a GtkBuilder instance and load our UI description */
  //builder = gtk_builder_new ();
  //gtk_builder_add_from_file (builder, "builder.ui", NULL);

  // Create a browser instance

  /* Connect signal handlers to the constructed widgets. */
  //main_window = gtk_builder_get_object (builder, "main_window");
  //g_signal_connect (main_window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  //scrolledWindow = GTK_WIDGET(gtk_builder_get_object (builder, "scrolledWindow"));
  //gtk_container_add(GTK_CONTAINER(scrolledWindow), GTK_WIDGET(webView));

  //// Set up callbacks so that if either the main window or the browser instance is
  //// closed, the program will exit
  //g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL);
  //g_signal_connect(webView, "close-web-view", G_CALLBACK(closeWebViewCb), main_window);

  // Load a web page into the browser instance
  gtk_init(0, NULL);
  GtkWidget *main_window = gtk_offscreen_window_new();
  WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());

  gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(webView));
  webkit_web_view_load_uri(webView, "http://www.baidu.com/");



    gtk_widget_grab_focus(GTK_WIDGET(webView));
  // webView in not shown yet
  gtk_widget_show_all(GTK_WIDGET(main_window));

  webkit_web_view_get_snapshot(webView,
          WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT,
          WEBKIT_SNAPSHOT_OPTIONS_NONE,
          NULL,
          get_snapshot_finish,
          webView
          );
  gtk_main();
  return 0;
}
/* We should probably experiment some more with this.
 * Right now the rendered icon is pretty good for most
 * themes. However, the icon is slightly large for themes
 * with large toolbar icons.
 */
static GdkPixbuf *
new_pixbuf_from_widget (GtkWidget *widget)
{
  GtkWidget *window;
  GdkPixbuf *pixbuf;
  gint icon_height;
  GdkScreen *screen;

  screen = gtk_widget_get_screen (widget);

  if (!gtk_icon_size_lookup_for_settings (gtk_settings_get_for_screen (screen),
					  GTK_ICON_SIZE_LARGE_TOOLBAR,
					  NULL,
					  &icon_height))
    {
      icon_height = DEFAULT_ICON_HEIGHT;
    }

  window = gtk_offscreen_window_new ();
  /* Set the width to -1 as we want the separator to be as thin as possible. */
  gtk_widget_set_size_request (widget, -1, icon_height);
  gtk_container_add (GTK_CONTAINER (window), widget);
  gtk_widget_show_all (window);

  /* Process the waiting events to have the widget actually drawn */
  gdk_window_process_updates (gtk_widget_get_window (window), TRUE);
  pixbuf = gtk_offscreen_window_get_pixbuf (GTK_OFFSCREEN_WINDOW (window));
  gtk_widget_destroy (window);

  return pixbuf;
}
Example #3
0
int
main (int argc, gchar* argv[]) {
    gtk_init(&argc, &argv);

    if (argc < 2) {
        printf("Usage: URI [filename]\n");
        return 1;
    }
    const gchar *uri = argv[1];
    const gchar *filename = argc > 2 ? argv[2] : "a.pdf";

    if (!g_thread_supported()) {g_thread_init(NULL);}

    WebKitWebView *web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_signal_connect(web_view, "notify::load-status", G_CALLBACK(load_status_cb), (gpointer) filename);

    GtkWidget *offscren = gtk_offscreen_window_new();
    gtk_container_add(GTK_CONTAINER(offscren), GTK_WIDGET(web_view));
    gtk_widget_show_all(offscren);

    webkit_web_view_load_uri(web_view, uri);
    gtk_main();

    return 0;
}
Example #4
0
int main(int argc, gchar* argv[]) {
    gtk_init(&argc, &argv);

    GError *error = NULL;
    GOptionContext *context;
    context = g_option_context_new("URL");
    g_option_context_add_main_entries(context, entries, NULL);
    g_option_context_add_group(context, gtk_get_option_group(TRUE));
    if (!g_option_context_parse(context, &argc, &argv, &error)) {
        errx(1, "option parsing failed: %s", error->message);
    }

    if (argc < 2) {
        printf("Usage: %s [OPTION...] URL\n", argv[0]);
        printf("Try '%s --help' for more information.\n", argv[0]);
        return 1;
    }

    todo = (mhtml_file != NULL) + (html_file != NULL) + (png_file != NULL) + (title_file != NULL);
    if (!todo) {
        printf("Specify at least one of: --mhtml, --html, --png, --title\n");
        printf("Try '%s --help' for more information.\n", argv[0]);
        return 1;
    }

    const gchar *url = argv[1];
    if (g_uri_parse_scheme(url) == NULL) {
        errx(1, "invalid URI: %s", url);
    }

    WebKitWebView *web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
    g_signal_connect(web_view, "load-changed", G_CALLBACK(load_changed), NULL);
    g_signal_connect(web_view, "load-failed", G_CALLBACK(load_failed), NULL);

    WebKitSettings *settings = webkit_settings_new_with_settings(
        "user-agent", USER_AGENT,
        "enable-javascript", enable_js,
        "enable-java", FALSE,
        "enable-plugins", FALSE,
        "enable-private-browsing", TRUE,
        "enable-offline-web-application-cache", FALSE,
        "enable-page-cache", FALSE,
        NULL
    );
    webkit_web_view_set_settings(web_view, settings);

    GtkWidget *window = gtk_offscreen_window_new();
    gtk_window_set_default_size(GTK_WINDOW(window), 1280, 1024);
    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(web_view));
    gtk_widget_show_all(window);

    webkit_web_view_load_uri(web_view, url);
    gtk_main();

    return 0;
}
Example #5
0
static void test_webkit_web_view_in_offscreen_window_does_not_crash()
{
    loop = g_main_loop_new(NULL, TRUE);

    GtkWidget *window = gtk_offscreen_window_new();
    GtkWidget *web_view = webkit_web_view_new();

    gtk_container_add(GTK_CONTAINER(window), web_view);
    gtk_widget_show_all(window);
    g_signal_connect(web_view, "notify::load-status", G_CALLBACK(idle_quit_loop_cb), NULL);
    webkit_web_view_load_uri(WEBKIT_WEB_VIEW(web_view), base_uri);

    g_main_loop_run(loop);

    gtk_widget_destroy(window);
    g_main_loop_unref(loop);
}
Example #6
0
static void test_webkit_web_view_does_not_steal_focus()
{
    loop = g_main_loop_new(NULL, TRUE);

    GtkWidget *window = gtk_offscreen_window_new();
    GtkWidget *webView = webkit_web_view_new();
    GtkWidget *entry = gtk_entry_new();

#ifdef GTK_API_VERSION_2
    GtkWidget *box = gtk_hbox_new(FALSE, 0);
#else
    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
#endif

    gtk_container_add(GTK_CONTAINER(box), webView);
    gtk_container_add(GTK_CONTAINER(box), entry);
    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);

    gtk_widget_grab_focus(entry);
    g_assert(gtk_widget_is_focus(entry));

    g_signal_connect(webView, "notify::load-status", G_CALLBACK(idle_quit_loop_cb), NULL);
    webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(webView),
                                     "<html><body>"
                                     "    <input id=\"entry\" type=\"text\"/>"
                                     "    <script>"
                                     "        document.getElementById(\"entry\").focus();"
                                     "    </script>"
                                     "</body></html>", "file://");

    g_main_loop_run(loop);

    g_assert(gtk_widget_is_focus(entry));

    gtk_widget_destroy(window);
    g_main_loop_unref(loop);
}
JNIEXPORT jlong JNICALL
Java_org_gnome_gtk_GtkOffscreenWindow_gtk_1offscreen_1window_1new
(
	JNIEnv* env,
	jclass cls
)
{
	GtkWidget* result;
	jlong _result;

	// call function
	result = gtk_offscreen_window_new();

	// translate return value to JNI type
	_result = (jlong) result;

	// cleanup return value
	if (result != NULL) {
		bindings_java_memory_cleanup((GObject*)result, TRUE);
	}

	// and finally
	return _result;
}
Example #8
0
static gint32
webpage_capture (void)
{
  gchar *scheme;
  GtkWidget *window;
  GtkWidget *view;
  WebKitSettings *settings;
  char *ua;

  if ((!webpagevals.url) ||
      (strlen (webpagevals.url) == 0))
    {
      g_set_error (&webpagevals.error, 0, 0, _("No URL was specified"));
      return -1;
    }

  scheme = g_uri_parse_scheme (webpagevals.url);
  if (!scheme)
    {
      char *url;

      /* If we were not given a well-formed URL, make one. */

      url = g_strconcat ("http://", webpagevals.url, NULL);
      g_free (webpagevals.url);
      webpagevals.url = url;

      g_free (scheme);
    }

  if (webpagevals.width < 32)
    {
      g_warning ("Width '%d' is too small. Clamped to 32.",
                 webpagevals.width);
      webpagevals.width = 32;
    }
  else if (webpagevals.width > 8192)
    {
      g_warning ("Width '%d' is too large. Clamped to 8192.",
                 webpagevals.width);
      webpagevals.width = 8192;
    }

  window = gtk_offscreen_window_new ();
  gtk_widget_show (window);

  view = webkit_web_view_new ();
  gtk_widget_show (view);

  gtk_widget_set_vexpand (view, TRUE);
  gtk_widget_set_size_request (view, webpagevals.width, -1);
  gtk_container_add (GTK_CONTAINER (window), view);

  /* Append "GIMP/<GIMP_VERSION>" to the user agent string */
  settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (view));
  ua = g_strdup_printf ("%s GIMP/%s",
                        webkit_settings_get_user_agent (settings),
                        GIMP_VERSION);
  webkit_settings_set_user_agent (settings, ua);
  g_free (ua);

  /* Set font size */
  webkit_settings_set_default_font_size (settings, webpagevals.font_size);

  g_signal_connect (view, "notify::estimated-load-progress",
                    G_CALLBACK (notify_progress_cb),
                    window);
  g_signal_connect (view, "load-failed",
                    G_CALLBACK (load_failed_cb),
                    window);
  g_signal_connect (view, "load-changed",
                    G_CALLBACK (load_changed_cb),
                    window);

  gimp_progress_init_printf (_("Downloading webpage '%s'"), webpagevals.url);

  webkit_web_view_load_uri (WEBKIT_WEB_VIEW (view),
                            webpagevals.url);

  gtk_main ();

  gtk_widget_destroy (window);

  gimp_progress_update (1.0);

  return webpagevals.image;
}
Example #9
0
static gint32
webpage_capture (void)
{
  gint32 image = -1;
  gchar *scheme;
  GtkWidget *window;
  GtkWidget *view;
  WebKitWebSettings *settings;
  char *ua_old;
  char *ua;

  if (webpagevals.pixbuf)
    {
      g_object_unref (webpagevals.pixbuf);
      webpagevals.pixbuf = NULL;
    }
  if (webpagevals.error)
    {
      g_error_free (webpagevals.error);
      webpagevals.error = NULL;
    }

  if ((!webpagevals.url) ||
      (strlen (webpagevals.url) == 0))
    {
      g_set_error (&webpagevals.error, 0, 0, _("No URL was specified"));
      return -1;
    }

  scheme = g_uri_parse_scheme (webpagevals.url);
  if (!scheme)
    {
      char *url;

      /* If we were not given a well-formed URL, make one. */

      url = g_strconcat ("http://", webpagevals.url, NULL);
      g_free (webpagevals.url);
      webpagevals.url = url;

      g_free (scheme);
    }

  if (webpagevals.width < 32)
    {
      g_warning ("Width '%d' is too small. Clamped to 32.",
                 webpagevals.width);
      webpagevals.width = 32;
    }
  else if (webpagevals.width > 8192)
    {
      g_warning ("Width '%d' is too large. Clamped to 8192.",
                 webpagevals.width);
      webpagevals.width = 8192;
    }

  window = gtk_offscreen_window_new ();
  gtk_widget_show (window);

  view = webkit_web_view_new ();
  gtk_widget_show (view);

  gtk_widget_set_size_request (view, webpagevals.width, -1);
  gtk_container_add (GTK_CONTAINER (window), view);

  /* Append "GIMP/<GIMP_VERSION>" to the user agent string */
  settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (view));
  g_object_get (settings,
                "user-agent", &ua_old,
                NULL);
  ua = g_strdup_printf ("%s GIMP/%s", ua_old, GIMP_VERSION);
  g_object_set (settings,
                "user-agent", ua,
                NULL);
  g_free (ua_old);
  g_free (ua);

  /* Set font size */
  g_object_set (settings,
                "default-font-size", webpagevals.font_size,
                NULL);

  g_signal_connect (view, "notify::progress",
                    G_CALLBACK (notify_progress_cb),
                    window);
  g_signal_connect (view, "load-error",
                    G_CALLBACK (load_error_cb),
                    window);
  g_signal_connect (view, "notify::load-status",
                    G_CALLBACK (notify_load_status_cb),
                    window);

  gimp_progress_init_printf (_("Downloading webpage '%s'"), webpagevals.url);

  webkit_web_view_open (WEBKIT_WEB_VIEW (view),
                        webpagevals.url);

  gtk_main ();

  gtk_widget_destroy (window);

  gimp_progress_update (1.0);

  if (webpagevals.pixbuf)
    {
      gint width;
      gint height;
      gint32 layer;

      gimp_progress_init_printf (_("Transferring webpage image for '%s'"),
                                 webpagevals.url);

      width  = gdk_pixbuf_get_width (webpagevals.pixbuf);
      height = gdk_pixbuf_get_height (webpagevals.pixbuf);

      image = gimp_image_new (width, height, GIMP_RGB);

      gimp_image_undo_disable (image);
      layer = gimp_layer_new_from_pixbuf (image, _("Webpage"), webpagevals.pixbuf,
                                          100, GIMP_NORMAL_MODE, 0.0, 1.0);
      gimp_image_insert_layer (image, layer, -1, 0);
      gimp_image_undo_enable (image);

      g_object_unref (webpagevals.pixbuf);
      webpagevals.pixbuf = NULL;

      gimp_progress_update (1.0);
    }

  return image;
}