commit: edf42bcc6f542fbaff599f0a9b6c1b764970c5e7
parent: a364a1caaa3441ecf125872644a4b1c2c1d4abc6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 7 Jan 2019 17:47:20 +0100
badwolf.c: put the browser in a struct to handle notify::uri
Diffstat:
M | badwolf.c | 53 | ++++++++++++++++++++++++++++++----------------------- |
1 file changed, 30 insertions(+), 23 deletions(-)
diff --git a/badwolf.c b/badwolf.c
@@ -5,13 +5,20 @@
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>
+struct Client {
+ GtkWidget *main_window;
+ GtkWidget *box;
+ GtkWidget *location;
+ WebKitWebView *webView;
+};
+
static gboolean WebViewCb_close(WebKitWebView *_webView, GtkWidget *window)
{
gtk_widget_destroy(window);
return TRUE;
}
-static gboolean WebViewCb_web_process_terminated(WebKitWebView *web_view, WebKitWebProcessTerminationReason reason, gpointer user_data)
+static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView, WebKitWebProcessTerminationReason reason, gpointer user_data)
{
char *str_reason;
@@ -33,55 +40,55 @@ static gboolean WebViewCb_web_process_terminated(WebKitWebView *web_view, WebKit
return TRUE;
}
-static gboolean WebViewCb_notify__uri(WebKitWebView *webView, GtkWidget *location)
+static gboolean WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data)
{
const gchar *location_uri;
+ struct Client *browser = (struct Client *)user_data;
- location_uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(webView));
+ location_uri = webkit_web_view_get_uri(browser->webView);
- gtk_entry_set_text(GTK_ENTRY(location), location_uri);
+ gtk_entry_set_text(GTK_ENTRY(browser->location), location_uri);
return TRUE;
}
int main(int argc, char *argv[])
{
+ struct Client *browser = calloc(1, sizeof(struct Client));
char *target_url = "about:blank";
- GtkWidget *main_window, *box, *location;
- WebKitWebView *webView;
if(argv[1]) target_url = argv[1];
gtk_init(0, 0);
- main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- location = gtk_entry_new();
- box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
- webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
+ browser->main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ browser->location = gtk_entry_new();
+ browser->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+ browser->webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
- gtk_container_add(GTK_CONTAINER(main_window), box);
- gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(location), FALSE, FALSE, 0);
- gtk_box_pack_end(GTK_BOX(box), GTK_WIDGET(webView), TRUE, TRUE, 0);
+ gtk_container_add(GTK_CONTAINER(browser->main_window), browser->box);
+ gtk_box_pack_start(GTK_BOX(browser->box), GTK_WIDGET(browser->location), FALSE, FALSE, 0);
+ gtk_box_pack_end(GTK_BOX(browser->box), GTK_WIDGET(browser->webView), TRUE, TRUE, 0);
- gtk_entry_set_text(GTK_ENTRY(location), target_url);
+ gtk_entry_set_text(GTK_ENTRY(browser->location), target_url);
- g_signal_connect(main_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
- g_signal_connect(webView, "close", G_CALLBACK(WebViewCb_close), main_window);
- g_signal_connect(webView, "web-process-terminated", G_CALLBACK(WebViewCb_web_process_terminated), NULL);
- g_signal_connect(webView, "notify::uri", G_CALLBACK(WebViewCb_notify__uri), location);
+ g_signal_connect(browser->main_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+ g_signal_connect(browser->webView, "close", G_CALLBACK(WebViewCb_close), browser->main_window);
+ g_signal_connect(browser->webView, "web-process-terminated", G_CALLBACK(WebViewCb_web_process_terminated), NULL);
+ g_signal_connect(browser->webView, "notify::uri", G_CALLBACK(WebViewCb_notify__uri), browser);
- WebKitSettings *settings = webkit_web_view_get_settings(webView);
+ WebKitSettings *settings = webkit_web_view_get_settings(browser->webView);
webkit_settings_set_javascript_can_access_clipboard(settings, FALSE);
webkit_settings_set_enable_developer_extras(settings, TRUE);
webkit_settings_set_default_charset(settings, "utf-8");
- webkit_web_view_set_settings(webView, settings);
+ webkit_web_view_set_settings(browser->webView, settings);
- webkit_web_view_load_uri(webView, target_url);
+ webkit_web_view_load_uri(browser->webView, target_url);
- gtk_widget_grab_focus(GTK_WIDGET(webView));
- gtk_widget_show_all(main_window);
+ gtk_widget_grab_focus(GTK_WIDGET(browser->webView));
+ gtk_widget_show_all(browser->main_window);
gtk_main();
return 0;
}