]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
Scroll tabs using mouse wheel
[chorizo.git] / browser.c
index ca8eb37485825635b4d7440f3cdb6f54aae1e8fc..3c68dd3776e96f4aba7fb55761487bc94cb28262 100644 (file)
--- a/browser.c
+++ b/browser.c
 #include <JavaScriptCore/JavaScript.h>
 
 
-static gboolean button_tablabel(GtkWidget *, GdkEvent *, gpointer);
-static void client_destroy(GtkWidget *, gpointer);
-static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
-static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
-                                         gpointer);
-static void cooperation_setup(void);
-static void changed_download_progress(GObject *, GParamSpec *, gpointer);
-static void changed_load_progress(GObject *, GParamSpec *, gpointer);
-static void changed_title(GObject *, GParamSpec *, gpointer);
-static void changed_uri(GObject *, GParamSpec *, gpointer);
-static gboolean crashed_web_view(WebKitWebView *, gpointer);
-static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
-                              WebKitPolicyDecisionType, gpointer);
-static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
-static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
-static void downloadmanager_cancel(GtkToolButton *, gpointer);
-static gboolean downloadmanager_delete(GtkWidget *, gpointer);
-static void downloadmanager_setup(void);
-static gchar *ensure_uri_scheme(const gchar *);
-static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
-static void grab_environment_configuration(void);
-static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
-static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
-static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
-static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
-static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
-static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
-static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
-static void keywords_load(void);
-static gboolean keywords_try_search(WebKitWebView *, const gchar *);
-static void mainwindow_setup(void);
-static void mainwindow_title(gint);
-static void mainwindow_title_before(GtkNotebook *, GtkWidget *, guint, gpointer);
-static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
-                              WebKitHitTestResult *, gpointer);
-static gboolean quit_if_nothing_active(void);
-static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
-static void run_user_scripts(WebKitWebView *);
-static void search(gpointer, gint);
-static void show_web_view(WebKitWebView *, gpointer);
-static void trust_user_certs(WebKitWebContext *);
+void client_destroy(GtkWidget *, gpointer);
+WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean,
+                          gboolean);
+WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
+                                  gpointer);
+void cooperation_setup(void);
+void changed_download_progress(GObject *, GParamSpec *, gpointer);
+void changed_load_progress(GObject *, GParamSpec *, gpointer);
+void changed_favicon(GObject *, GParamSpec *, gpointer);
+void changed_title(GObject *, GParamSpec *, gpointer);
+void changed_uri(GObject *, GParamSpec *, gpointer);
+gboolean crashed_web_view(WebKitWebView *, gpointer);
+gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
+                       WebKitPolicyDecisionType, gpointer);
+gboolean download_handle(WebKitDownload *, gchar *, gpointer);
+void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
+void downloadmanager_cancel(GtkToolButton *, gpointer);
+gboolean downloadmanager_delete(GtkWidget *, gpointer);
+void downloadmanager_setup(void);
+gchar *ensure_uri_scheme(const gchar *);
+void external_handler_run(GSimpleAction *, GVariant *, gpointer);
+void grab_environment_configuration(void);
+void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
+void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
+void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
+gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
+gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
+gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
+gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
+gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
+void mainwindow_setup(void);
+gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
+                       WebKitHitTestResult *, gpointer);
+gboolean quit_if_nothing_active(void);
+gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
+void run_user_scripts(WebKitWebView *);
+void search(gpointer, gint);
+void show_web_view(WebKitWebView *, gpointer);
+void trust_user_certs(WebKitWebContext *);
 
 
 struct Client
@@ -63,9 +61,11 @@ struct Client
     gchar *hover_uri;
     gchar *feed_html;
     GtkWidget *location;
+    GtkWidget *tabicon;
     GtkWidget *tablabel;
     GtkWidget *vbox;
     GtkWidget *web_view;
+    gboolean focus_new_tab;
 };
 
 struct MainWindow
@@ -82,40 +82,24 @@ struct DownloadManager
 } dm;
 
 
-static const gchar *accepted_language[2] = { NULL, NULL };
-static gint clients = 0, downloads = 0;
-static gboolean cooperative_alone = TRUE;
-static gboolean cooperative_instances = TRUE;
-static int cooperative_pipe_fp = 0;
-static gchar *download_dir = "/var/tmp";
-static gboolean enable_console_to_stdout = FALSE;
-static gchar *fifo_suffix = "main";
-static gdouble global_zoom = 1.0;
-static gchar *history_file = NULL;
-static gchar *home_uri = "about:blank";
-static gboolean initial_wc_setup_done = FALSE;
-static GHashTable *keywords = NULL;
-static gchar *search_text = NULL;
-static GtkPositionType tab_pos = GTK_POS_TOP;
-static gint tab_width_chars = 20;
-static gchar *user_agent = NULL;
+const gchar *accepted_language[2] = { NULL, NULL };
+gint clients = 0, downloads = 0;
+gboolean cooperative_alone = TRUE;
+gboolean cooperative_instances = TRUE;
+int cooperative_pipe_fp = 0;
+gchar *download_dir = "/var/tmp";
+gboolean enable_console_to_stdout = FALSE;
+gchar *fifo_suffix = "main";
+gdouble global_zoom = 1.0;
+gchar *history_file = NULL;
+gchar *home_uri = "about:blank";
+gboolean initial_wc_setup_done = FALSE;
+gchar *search_text = NULL;
+GtkPositionType tab_pos = GTK_POS_TOP;
+gint tab_width_chars = 20;
+gchar *user_agent = NULL;
 
 
-gboolean
-button_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
-{
-    if (event->type == GDK_BUTTON_RELEASE)
-    {
-        switch (((GdkEventButton *)event)->button)
-        {
-            case 2:
-                client_destroy(NULL, data);
-                return TRUE;
-        }
-    }
-    return FALSE;
-}
-
 void
 client_destroy(GtkWidget *widget, gpointer data)
 {
@@ -138,12 +122,13 @@ client_destroy(GtkWidget *widget, gpointer data)
 }
 
 WebKitWebView *
-client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
+client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
+           gboolean focus_tab)
 {
     struct Client *c;
     WebKitWebContext *wc;
     gchar *f;
-    GtkWidget *evbox;
+    GtkWidget *evbox, *tabbox;
 
     if (uri != NULL && cooperative_instances && !cooperative_alone)
     {
@@ -161,6 +146,8 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
         exit(EXIT_FAILURE);
     }
 
+    c->focus_new_tab = focus_tab;
+
     if (related_wv == NULL)
         c->web_view = webkit_web_view_new();
     else
@@ -168,6 +155,8 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
     wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
 
     webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
+    g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
+                     G_CALLBACK(changed_favicon), c);
     g_signal_connect(G_OBJECT(c->web_view), "notify::title",
                      G_CALLBACK(changed_title), c);
     g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
@@ -203,6 +192,8 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
 
         trust_user_certs(wc);
 
+        webkit_web_context_set_favicon_database_directory(wc, NULL);
+
         initial_wc_setup_done = TRUE;
     }
 
@@ -233,21 +224,36 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
     gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
     gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
 
+    c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
+
     c->tablabel = gtk_label_new(__NAME__);
     gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
     gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
+    gtk_widget_set_has_tooltip(c->tablabel, TRUE);
+
+    /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
+     * does the right thing. */
+    tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
+                         5 * gtk_widget_get_scale_factor(mw.win));
+    gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
+    gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
 
     evbox = gtk_event_box_new();
-    gtk_container_add(GTK_CONTAINER(evbox), c->tablabel);
+    gtk_container_add(GTK_CONTAINER(evbox), tabbox);
     g_signal_connect(G_OBJECT(evbox), "button-release-event",
-                     G_CALLBACK(button_tablabel), c);
+                     G_CALLBACK(key_tablabel), c);
+
+    gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
+    g_signal_connect(G_OBJECT(evbox), "scroll-event",
+                     G_CALLBACK(key_tablabel), c);
 
     /* This only shows the event box and the label inside, nothing else.
      * Needed because the evbox/label is "internal" to the notebook and
      * not part of the normal "widget tree" (IIUC). */
     gtk_widget_show_all(evbox);
 
-    gtk_notebook_append_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox);
+    gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
+                             gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
     gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
 
     if (show)
@@ -272,7 +278,7 @@ WebKitWebView *
 client_new_request(WebKitWebView *web_view,
                    WebKitNavigationAction *navigation_action, gpointer data)
 {
-    return client_new(NULL, web_view, FALSE);
+    return client_new(NULL, web_view, FALSE, FALSE);
 }
 
 void
@@ -393,6 +399,39 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
 }
 
+void
+changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
+{
+    struct Client *c = (struct Client *)data;
+    cairo_surface_t *f;
+    int w, h, w_should, h_should;
+    GdkPixbuf *pb, *pb_scaled;
+
+    f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
+    if (f == NULL)
+    {
+        gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
+                                     GTK_ICON_SIZE_SMALL_TOOLBAR);
+    }
+    else
+    {
+        w = cairo_image_surface_get_width(f);
+        h = cairo_image_surface_get_height(f);
+        pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
+        if (pb != NULL)
+        {
+            w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
+            h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
+            pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
+                                                GDK_INTERP_BILINEAR);
+            gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
+
+            g_object_unref(pb_scaled);
+            g_object_unref(pb);
+        }
+    }
+}
+
 void
 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
 {
@@ -409,7 +448,7 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
     t = t[0] == 0 ? u : t;
 
     gtk_label_set_text(GTK_LABEL(c->tablabel), t);
-    mainwindow_title(-1);
+    gtk_widget_set_tooltip_text(c->tablabel, t);
 }
 
 void
@@ -856,7 +895,7 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
                     return TRUE;
                 case GDK_KEY_e:  /* new tab (left hand) */
                     f = ensure_uri_scheme(home_uri);
-                    client_new(f, NULL, TRUE);
+                    client_new(f, NULL, TRUE, TRUE);
                     g_free(f);
                     return TRUE;
                 case GDK_KEY_r:  /* reload (left hand) */
@@ -961,7 +1000,7 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
                     search_text = g_strdup(t + 2);
                     search(c, 0);
                 }
-                else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
+                else
                 {
                     f = ensure_uri_scheme(t);
                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
@@ -979,6 +1018,39 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
     return FALSE;
 }
 
+gboolean
+key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+    GdkScrollDirection direction;
+
+    if (event->type == GDK_BUTTON_RELEASE)
+    {
+        switch (((GdkEventButton *)event)->button)
+        {
+            case 2:
+                client_destroy(NULL, data);
+                return TRUE;
+        }
+    }
+    else if (event->type == GDK_SCROLL)
+    {
+        gdk_event_get_scroll_direction(event, &direction);
+        switch (direction)
+        {
+            case GDK_SCROLL_UP:
+                gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
+                break;
+            case GDK_SCROLL_DOWN:
+                gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
+                break;
+            default:
+                break;
+        }
+        return TRUE;
+    }
+    return FALSE;
+}
+
 gboolean
 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
@@ -1004,7 +1076,7 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
             case 2:
                 if (c->hover_uri != NULL)
                 {
-                    client_new(c->hover_uri, NULL, TRUE);
+                    client_new(c->hover_uri, NULL, TRUE, FALSE);
                     return TRUE;
                 }
                 break;
@@ -1033,66 +1105,6 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
     return FALSE;
 }
 
-void
-keywords_load(void)
-{
-    GError *err = NULL;
-    GIOChannel *channel = NULL;
-    gchar *path = NULL, *buf = NULL;
-    gchar **tokens = NULL;
-
-    keywords = g_hash_table_new(g_str_hash, g_str_equal);
-
-    path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
-                            NULL);
-    channel = g_io_channel_new_file(path, "r", &err);
-    if (channel != NULL)
-    {
-        while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
-               == G_IO_STATUS_NORMAL)
-        {
-            g_strstrip(buf);
-            if (buf[0] != '#')
-            {
-                tokens = g_strsplit(buf, " ", 2);
-                if (tokens[0] != NULL && tokens[1] != NULL)
-                    g_hash_table_insert(keywords, g_strdup(tokens[0]),
-                                        g_strdup(tokens[1]));
-                g_strfreev(tokens);
-            }
-            g_free(buf);
-        }
-        g_io_channel_shutdown(channel, FALSE, NULL);
-    }
-    g_free(path);
-}
-
-gboolean
-keywords_try_search(WebKitWebView *web_view, const gchar *t)
-{
-    gboolean ret = FALSE;
-    gchar **tokens = NULL;
-    gchar *val = NULL, *escaped = NULL, *uri = NULL;
-
-    tokens = g_strsplit(t, " ", 2);
-    if (tokens[0] != NULL && tokens[1] != NULL)
-    {
-        val = g_hash_table_lookup(keywords, tokens[0]);
-        if (val != NULL)
-        {
-            escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
-            uri = g_strdup_printf((gchar *)val, escaped);
-            webkit_web_view_load_uri(web_view, uri);
-            g_free(uri);
-            g_free(escaped);
-            ret = TRUE;
-        }
-    }
-    g_strfreev(tokens);
-
-    return ret;
-}
-
 void
 mainwindow_setup(void)
 {
@@ -1105,37 +1117,6 @@ mainwindow_setup(void)
     gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
     gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
     gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
-    g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
-                     G_CALLBACK(mainwindow_title_before), NULL);
-}
-
-void
-mainwindow_title_before(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
-{
-    mainwindow_title(idx);
-}
-
-void
-mainwindow_title(gint idx)
-{
-    GtkWidget *child, *evbox, *label;
-    const gchar *text;
-
-    if (idx == -1)
-    {
-        idx = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook));
-        if (idx == -1)
-            return;
-    }
-
-    child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
-    if (child == NULL)
-        return;
-
-    evbox = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
-    label = gtk_bin_get_child(GTK_BIN(evbox));
-    text = gtk_label_get_text(GTK_LABEL(label));
-    gtk_window_set_title(GTK_WINDOW(mw.win), text);
 }
 
 gboolean
@@ -1203,7 +1184,7 @@ remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
     if (uri)
     {
         g_strstrip(uri);
-        client_new(uri, NULL, TRUE);
+        client_new(uri, NULL, TRUE, TRUE);
         g_free(uri);
     }
     return TRUE;
@@ -1276,11 +1257,14 @@ show_web_view(WebKitWebView *web_view, gpointer data)
 
     gtk_widget_show_all(mw.win);
 
-    idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
-    if (idx != -1)
-        gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
+    if (c->focus_new_tab)
+    {
+        idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
+        if (idx != -1)
+            gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
 
-    gtk_widget_grab_focus(c->web_view);
+        gtk_widget_grab_focus(c->web_view);
+    }
 }
 
 void
@@ -1336,7 +1320,6 @@ main(int argc, char **argv)
         }
     }
 
-    keywords_load();
     if (cooperative_instances)
         cooperation_setup();
     downloadmanager_setup();
@@ -1353,11 +1336,11 @@ main(int argc, char **argv)
     }
 
     if (optind >= argc)
-        client_new(home_uri, NULL, TRUE);
+        client_new(home_uri, NULL, TRUE, TRUE);
     else
     {
         for (i = optind; i < argc; i++)
-            client_new(argv[i], NULL, TRUE);
+            client_new(argv[i], NULL, TRUE, TRUE);
     }
 
     if (!cooperative_instances || cooperative_alone)