From 4ac063c2da0b1e3bf3ca4ace29f0d5eb24b7c0a1 Mon Sep 17 00:00:00 2001
From: "Carlos J. Torres" <vlaadbrain@gmail.com>
Date: Thu, 21 Feb 2013 09:59:07 -0500
Subject: [PATCH] disable/toggle scrollbars

	* add flag to main
	* add flag to manual
	* add signal handler to block default scrollbar policy
	* add toggle of scrollbars (hacky - but no reload) with a twitch
	* add key map to manual
	* add commandline flag to children surfers
	* update TODO

---
 TODO.md      |  2 +-
 config.def.h |  2 ++
 surf.1       |  6 +++++
 surf.c       | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/TODO.md b/TODO.md
index d236d11..6ffdb3b 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,7 +2,7 @@
 
 * suckless adblocking
 * integrate the WebKitWebInspector API
-* make scrollbars a switch and allow them to be disabled
+* replace twitch() with proper gtk calls to make scrollbars reappear
 * replace webkit with something sane
 * add video player options
 	* play in plugin
diff --git a/config.def.h b/config.def.h
index bbfb1ed..d9a2be9 100644
--- a/config.def.h
+++ b/config.def.h
@@ -10,6 +10,7 @@ static char *cafile         = "/etc/ssl/certs/ca-certificates.crt";
 static char *strictssl      = FALSE; /* Refuse untrusted SSL connections */
 
 /* Webkit default features */
+static Bool enablescrollbars = TRUE;
 static Bool enablespatialbrowsing = TRUE;
 static Bool enableplugins = TRUE;
 static Bool enablescripts = TRUE;
@@ -84,5 +85,6 @@ static Key keys[] = {
     { MODKEY|GDK_SHIFT_MASK,GDK_s,      toggle,     { .v = "enable-scripts" } },
     { MODKEY|GDK_SHIFT_MASK,GDK_v,      toggle,     { .v = "enable-plugins" } },
     { MODKEY|GDK_SHIFT_MASK,GDK_m,      togglestyle,{ 0 } },
+    { MODKEY|GDK_SHIFT_MASK,GDK_b,      togglescrollbars,{ 0 } },
 };
 
diff --git a/surf.1 b/surf.1
index 1bad494..d66afd8 100644
--- a/surf.1
+++ b/surf.1
@@ -35,6 +35,9 @@ Disable the Web Inspector (Developer Tools).
 .B \-p
 Disable Plugins
 .TP
+.B \-b
+Disable Scrollbars
+.TP
 .B \-r scriptfile 
 Specify the user
 .I scriptfile.
@@ -147,6 +150,9 @@ Toggle script execution.
 .B Ctrl\-Shift\-v
 Toggle the enabling of plugins on that surf instance.
 .TP
+.B Ctrl\-Shift\-b
+Toggle scrollbars
+.TP
 .B F11
 Toggle fullscreen mode.
 .SH ENVIRONMENT
diff --git a/surf.c b/surf.c
index ba00e5b..5de3ff3 100644
--- a/surf.c
+++ b/surf.c
@@ -153,6 +153,7 @@ static void stop(Client *c, const Arg *arg);
 static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
 		const char *title, Client *c);
 static void toggle(Client *c, const Arg *arg);
+static void togglescrollbars(Client *c, const Arg *arg);
 static void togglestyle(Client *c, const Arg *arg);
 static void update(Client *c);
 static void updatewinid(Client *c);
@@ -654,13 +655,9 @@ newclient(void) {
 	c->vbox = gtk_vbox_new(FALSE, 0);
 	gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
 
-	/* Scrolled Window */
-	c->scroll = gtk_scrolled_window_new(NULL, NULL);
-	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
-			GTK_POLICY_NEVER, GTK_POLICY_NEVER);
-
 	/* Webview */
 	c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
+
 	g_signal_connect(G_OBJECT(c->view),
 			"title-changed",
 			G_CALLBACK(titlechange), c);
@@ -698,6 +695,21 @@ newclient(void) {
 			"resource-request-starting",
 			G_CALLBACK(beforerequest), c);
 
+	/* Scrolled Window */
+	c->scroll = gtk_scrolled_window_new(NULL, NULL);
+
+	frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
+	g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
+			G_CALLBACK(gtk_true), NULL);
+
+	if(!enablescrollbars) {
+		gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
+				GTK_POLICY_NEVER, GTK_POLICY_NEVER);
+	} else {
+		gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
+				GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+	}
+
 	/* Arranging */
 	gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
 	gtk_container_add(GTK_CONTAINER(c->win), c->pane);
@@ -718,8 +730,8 @@ newclient(void) {
 	gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
 	webkit_web_view_set_full_content_zoom(c->view, TRUE);
 
-	frame = webkit_web_view_get_main_frame(c->view);
 	runscript(frame);
+
 	settings = webkit_web_view_get_settings(c->view);
 	if(!(ua = getenv("SURF_USERAGENT")))
 		ua = useragent;
@@ -778,7 +790,7 @@ newclient(void) {
 static void
 newwindow(Client *c, const Arg *arg, gboolean noembed) {
 	guint i = 0;
-	const char *cmd[10], *uri;
+	const char *cmd[11], *uri;
 	const Arg a = { .v = (void *)cmd };
 	char tmp[64];
 
@@ -788,6 +800,8 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) {
 		snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
 		cmd[i++] = tmp;
 	}
+	if(!enablescrollbars)
+		cmd[i++] = "-b";
 	if(!enablescripts)
 		cmd[i++] = "-s";
 	if(!enableplugins)
@@ -1048,6 +1062,44 @@ toggle(Client *c, const Arg *arg) {
 }
 
 static void
+twitch(Client *c, const Arg *arg) {
+	GtkAdjustment *a;
+	gdouble v;
+
+	a = gtk_scrolled_window_get_vadjustment(
+			GTK_SCROLLED_WINDOW(c->scroll));
+
+	v = gtk_adjustment_get_value(a);
+
+	v += arg->i;
+
+	v = MAX(v, 0.0);
+	v = MIN(v, gtk_adjustment_get_upper(a) -
+			gtk_adjustment_get_page_size(a));
+	gtk_adjustment_set_value(a, v);
+}
+
+static void
+togglescrollbars(Client *c, const Arg *arg) {
+	GtkPolicyType vspolicy;
+	Arg a;
+
+	gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
+
+	if(vspolicy == GTK_POLICY_AUTOMATIC) {
+		gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
+				GTK_POLICY_NEVER, GTK_POLICY_NEVER);
+	} else {
+		gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
+				GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+		a.i = +1;
+		twitch(c, &a);
+		a.i = -1;
+		twitch(c, &a);
+	}
+}
+
+static void
 togglestyle(Client *c, const Arg *arg) {
 	WebKitWebSettings *settings;
 	char *uri;
@@ -1128,7 +1180,7 @@ updatewinid(Client *c) {
 
 static void
 usage(void) {
-	die("usage: %s [-inpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
+	die("usage: %s [-ibnpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
 		" [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
 }
 
@@ -1171,6 +1223,9 @@ main(int argc, char *argv[]) {
 	case 'i':
 		loadimages = 0;
 		break;
+	case 'b':
+		enablescrollbars = 0;
+		break;
 	case 'n':
 		enableinspector = 0;
 		break;
-- 
1.7.12.1

