logo

badwolf

Minimalist and privacy-oriented WebKitGTK+ browser
commit: da6a593ea5bc329b6d1a91c6b388acaccd5964b0
parent: df054a6cac79d19a27e677ae92f5879d1e675790
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 10 Mar 2019 20:18:43 +0100

WIP: Close tab button

Diffstat:

Mbadwolf.c68++++++++++++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 50 insertions(+), 18 deletions(-)

diff --git a/badwolf.c b/badwolf.c @@ -3,6 +3,7 @@ // SPDX-License-Identifier: BSD-3-Clause #include <gtk/gtk.h> +#include <glib/gprintf.h> /* g_fprintf() */ #include <webkit2/webkit2.h> #include "config.h" @@ -43,7 +44,9 @@ static gboolean SearchEntryCb_search__changed(GtkSearchEntry *search, gpointer u static gboolean SearchEntryCb_stop__search(GtkSearchEntry *search, gpointer user_data); struct Client *new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view); void new_tabCb_clicked(GtkButton *new_tab, gpointer user_data); -int badwolf_new_tab(GtkNotebook *notebook, GtkWidget *box); +void closeCb_clicked(GtkButton *close, gpointer user_data); +int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser); +GtkWidget *badwolf_new_tab_box(const gchar *title, struct Client *browser); int main(int argc, char *argv[]); static gboolean WebViewCb_close(WebKitWebView *webView, GtkWidget *window) @@ -90,6 +93,27 @@ static gboolean WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, return TRUE; } +GtkWidget *badwolf_new_tab_box(const gchar *title, struct Client *browser) { + GtkWidget *tab_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); + GtkWidget *close = gtk_button_new_from_icon_name("window-close", GTK_ICON_SIZE_SMALL_TOOLBAR); + GtkWidget *label = gtk_label_new(title); + + gtk_box_pack_start(GTK_BOX(tab_box), label, TRUE, TRUE, 0); + gtk_box_pack_start(GTK_BOX(tab_box), close, TRUE, TRUE, 0); + + gtk_button_set_relief(GTK_BUTTON(close), GTK_RELIEF_NONE); + + g_signal_connect(close, "clicked", G_CALLBACK(closeCb_clicked), browser); + + gtk_label_set_width_chars(GTK_LABEL(label), 25); + gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_MIDDLE); + gtk_label_set_single_line_mode(GTK_LABEL(label), TRUE); + + gtk_widget_show_all(tab_box); + + return tab_box; +} + static gboolean WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data) { @@ -98,20 +122,11 @@ WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user const gchar *title; struct Client *browser = (struct Client *)user_data; -#define BADWOLF_TAB_MAXCHARS 16 - title = webkit_web_view_get_title(browser->webView); - if(title == NULL) title = webkit_web_view_get_uri(browser->webView); - if(title == NULL) title = "BadWolf"; - if(strlen(title) > BADWOLF_TAB_MAXCHARS) - { - title = g_strndup(title, BADWOLF_TAB_MAXCHARS); - } - - gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(browser->window->notebook), browser->box, title); + gtk_notebook_set_tab_label(GTK_NOTEBOOK(browser->window->notebook), browser->box, badwolf_new_tab_box(title, browser)); return TRUE; } @@ -156,7 +171,7 @@ static WebKitWebView *WebViewCb_create(WebKitWebView *related_web_view, WebKitNa struct Window *window = (struct Window *)user_data; struct Client *browser = new_browser(window, NULL, related_web_view); - if(badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser->box) < 0) { + if(badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser) < 0) { return NULL; } else { return browser->webView; @@ -323,16 +338,19 @@ struct Client *new_browser(struct Window *window, gchar *target_url, WebKitWebVi return browser; } -int badwolf_new_tab(GtkNotebook *notebook, GtkWidget *box) +int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser) { gint current_page = gtk_notebook_get_current_page(notebook); - gtk_widget_show_all(box); + gchar *title = "New tab"; - if(gtk_notebook_insert_page(notebook, box, NULL, (current_page+2)) == -1) { + gtk_widget_show_all(browser->box); + + if(gtk_notebook_insert_page(notebook, browser->box, NULL, (current_page+2)) == -1) { return -1; } - gtk_notebook_set_tab_reorderable(notebook, box, TRUE); + gtk_notebook_set_tab_reorderable(notebook, browser->box, TRUE); + gtk_notebook_set_tab_label(notebook, browser->box, badwolf_new_tab_box(title, browser)); gtk_widget_queue_draw(GTK_WIDGET(notebook)); @@ -345,7 +363,20 @@ void new_tabCb_clicked(GtkButton *new_tab, gpointer user_data) struct Window *window = (struct Window *)user_data; struct Client *browser = new_browser(window, NULL, NULL); - badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser->box); + badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser); +} + +void closeCb_clicked(GtkButton *close, gpointer user_data) +{ + (void)close; + struct Client *browser = (struct Client *)user_data; + GtkNotebook *notebook = GTK_NOTEBOOK(browser->window->notebook); + gint current_page = gtk_notebook_get_current_page(notebook); + + if(current_page > 0){ + gtk_notebook_remove_page(notebook, current_page); + free(browser); + } } int main(int argc, char *argv[]) @@ -366,10 +397,11 @@ int main(int argc, char *argv[]) gtk_notebook_set_action_widget(GTK_NOTEBOOK(window->notebook), window->new_tab, GTK_PACK_END); gtk_notebook_set_scrollable(GTK_NOTEBOOK(window->notebook), TRUE); gtk_notebook_set_tab_pos(GTK_NOTEBOOK(window->notebook), BADWOLF_TAB_POSITION); + gtk_notebook_popup_enable(GTK_NOTEBOOK(window->notebook)); gtk_container_add(GTK_CONTAINER(window->main_window), window->notebook); - badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(window, target_url, NULL)->box); + badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(window, target_url, NULL)); g_signal_connect(window->main_window, "destroy", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(window->new_tab, "clicked", G_CALLBACK(new_tabCb_clicked), window);