allow toolbar customisation to open a tab

This commit is contained in:
Vincent Sanders 2019-09-06 20:42:23 +01:00 committed by Daniel Silverstone
parent 46e1116aaa
commit c7c89daff3
7 changed files with 151 additions and 86 deletions

View File

@ -1202,23 +1202,19 @@ struct nsgtk_scaffolding *nsgtk_current_scaffolding(void)
/* exported function documented in gtk/scaffolding.h */ /* exported function documented in gtk/scaffolding.h */
void nsgtk_window_set_title(struct gui_window *gw, const char *title) void nsgtk_scaffolding_set_title(struct gui_window *gw, const char *title)
{ {
struct nsgtk_scaffolding *gs = nsgtk_get_scaffold(gw); struct nsgtk_scaffolding *gs = nsgtk_get_scaffold(gw);
int title_len; int title_len;
char *newtitle; char *newtitle;
if ((title == NULL) || (title[0] == '\0')) { /* only set window title if top level window */
if (gs->top_level != gw) { if (gs->top_level != gw) {
gtk_window_set_title(gs->window, "NetSurf");
}
return; return;
} }
nsgtk_tab_set_title(gw, title); if (title == NULL || title[0] == '\0') {
gtk_window_set_title(gs->window, "NetSurf");
if (gs->top_level != gw) {
/* not top level window so do not set window title */
return; return;
} }
@ -1233,6 +1229,7 @@ void nsgtk_window_set_title(struct gui_window *gw, const char *title)
gtk_window_set_title(gs->window, newtitle); gtk_window_set_title(gs->window, newtitle);
free(newtitle); free(newtitle);
} }
@ -1366,7 +1363,7 @@ void nsgtk_scaffolding_set_top_level(struct gui_window *gw)
browser_window_search_clear(bw); browser_window_search_clear(bw);
/* Ensure the window's title bar is updated */ /* Ensure the window's title bar is updated */
nsgtk_window_set_title(gw, browser_window_get_title(bw)); nsgtk_scaffolding_set_title(gw, browser_window_get_title(bw));
} }

View File

@ -145,14 +145,13 @@ void nsgtk_scaffolding_set_sensitivity(struct nsgtk_scaffolding *g);
*/ */
void nsgtk_scaffolding_context_menu(struct nsgtk_scaffolding *g, gdouble x, gdouble y); void nsgtk_scaffolding_context_menu(struct nsgtk_scaffolding *g, gdouble x, gdouble y);
/* core acessors */
/** /**
* set the title in the window * set the title in the window
* *
* \param gw The gui window to set title on * \param gw The gui window to set title on
* \param title The title to set which may be NULL * \param title The title to set which may be NULL
*/ */
void nsgtk_window_set_title(struct gui_window *gw, const char *title); void nsgtk_scaffolding_set_title(struct gui_window *gw, const char *title);
#endif /* NETSURF_GTK_SCAFFOLDING_H */ #endif /* NETSURF_GTK_SCAFFOLDING_H */

View File

@ -73,9 +73,13 @@ nsgtk_tab_update_size(GtkWidget *hbox,
/** /**
* Create a notebook tab label * Create a notebook tab label
*
* \param page The page content widget
* \param title The title of the page
* \param icon_pixbuf The icon of the page
*/ */
static GtkWidget * static GtkWidget *
nsgtk_tab_label_setup(struct gui_window *window, nsgtk_tab_label_setup(GtkWidget *page,
const char *title, const char *title,
GdkPixbuf *icon_pixbuf) GdkPixbuf *icon_pixbuf)
{ {
@ -108,10 +112,13 @@ nsgtk_tab_label_setup(struct gui_window *window,
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
gtk_widget_set_tooltip_text(button, "Close this tab."); gtk_widget_set_tooltip_text(button, "Close this tab.");
g_signal_connect_swapped(button, "clicked", g_signal_connect_swapped(button,
G_CALLBACK(nsgtk_window_destroy_browser), window); "clicked",
g_signal_connect(hbox, "style-set", G_CALLBACK(gtk_widget_destroy), page);
G_CALLBACK(nsgtk_tab_update_size), button); g_signal_connect(hbox,
"style-set",
G_CALLBACK(nsgtk_tab_update_size),
button);
/* pack the widgets into the label box */ /* pack the widgets into the label box */
gtk_box_pack_start(GTK_BOX(hbox), favicon, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), favicon, FALSE, FALSE, 0);
@ -355,25 +362,19 @@ nserror nsgtk_notebook_create(GtkBuilder *builder, GtkNotebook **notebook_out)
} }
/* exported interface documented in gtk/tabs.h */ /* exported interface documented in gtk/tabs.h */
void nsgtk_tab_add(struct gui_window *gw, nserror
nsgtk_tab_add_page(GtkNotebook *notebook,
GtkWidget *tab_contents, GtkWidget *tab_contents,
bool background, bool background,
const char *title, const char *title,
GdkPixbuf *icon_pixbuf) GdkPixbuf *icon_pixbuf)
{ {
GtkNotebook *notebook;
GtkWidget *tabBox; GtkWidget *tabBox;
gint remember; gint remember;
gint pages; gint pages;
gint newpage; gint newpage;
g_object_set_data(G_OBJECT(tab_contents), "gui_window", gw); tabBox = nsgtk_tab_label_setup(tab_contents, title, icon_pixbuf);
notebook = nsgtk_scaffolding_notebook(nsgtk_get_scaffold(gw));
tabBox = nsgtk_tab_label_setup(gw, title, icon_pixbuf);
nsgtk_window_set_tab(gw, tabBox);
remember = gtk_notebook_get_current_page(notebook); remember = gtk_notebook_get_current_page(notebook);
@ -391,48 +392,85 @@ void nsgtk_tab_add(struct gui_window *gw,
gtk_notebook_set_current_page(notebook, newpage); gtk_notebook_set_current_page(notebook, newpage);
} }
return NSERROR_OK;
}
/* exported interface documented in gtk/tabs.h */
void nsgtk_tab_add(struct gui_window *gw,
GtkWidget *tab_contents,
bool background,
const char *title,
GdkPixbuf *icon_pixbuf)
{
GtkNotebook *notebook;
g_object_set_data(G_OBJECT(tab_contents), "gui_window", gw);
notebook = nsgtk_scaffolding_notebook(nsgtk_get_scaffold(gw));
nsgtk_tab_add_page(notebook, tab_contents, background, title, icon_pixbuf);
gtk_widget_grab_focus(GTK_WIDGET(nsgtk_scaffolding_urlbar( gtk_widget_grab_focus(GTK_WIDGET(nsgtk_scaffolding_urlbar(
nsgtk_get_scaffold(gw)))); nsgtk_get_scaffold(gw))));
} }
/* exported interface documented in gtk/tabs.h */ /* exported interface documented in gtk/tabs.h */
nserror nsgtk_tab_set_icon(struct gui_window *gw, GdkPixbuf *pixbuf) nserror nsgtk_tab_set_icon(GtkWidget *page, GdkPixbuf *pixbuf)
{ {
GtkWidget *favicon; GtkImage *favicon;
GtkWidget *tab; GtkWidget *tab_label;
GtkNotebook *notebook;
if (pixbuf == NULL) { if (pixbuf == NULL) {
return NSERROR_INVALID; return NSERROR_INVALID;
} }
notebook = GTK_NOTEBOOK(gtk_widget_get_ancestor(page, GTK_TYPE_NOTEBOOK));
if (notebook == NULL) {
return NSERROR_BAD_PARAMETER;
}
tab = nsgtk_window_get_tab(gw); tab_label = gtk_notebook_get_tab_label(notebook, page);
if (tab == NULL) { if (tab_label == NULL) {
return NSERROR_INVALID; return NSERROR_INVALID;
} }
favicon = g_object_get_data(G_OBJECT(tab), "favicon"); favicon = GTK_IMAGE(g_object_get_data(G_OBJECT(tab_label), "favicon"));
gtk_image_set_from_pixbuf(GTK_IMAGE(favicon), pixbuf); gtk_image_set_from_pixbuf(favicon, pixbuf);
return NSERROR_OK; return NSERROR_OK;
} }
/* exported interface documented in gtk/tabs.h */ /* exported interface documented in gtk/tabs.h */
void nsgtk_tab_set_title(struct gui_window *g, const char *title) nserror nsgtk_tab_set_title(GtkWidget *page, const char *title)
{ {
GtkWidget *label; GtkLabel *label;
GtkWidget *tab; GtkWidget *tab_label;
GtkNotebook *notebook;
tab = nsgtk_window_get_tab(g); if (title == NULL) {
if (tab == NULL) { return NSERROR_INVALID;
return;
} }
label = g_object_get_data(G_OBJECT(tab), "label"); notebook = GTK_NOTEBOOK(gtk_widget_get_ancestor(page, GTK_TYPE_NOTEBOOK));
gtk_label_set_text(GTK_LABEL(label), title); if (notebook == NULL) {
gtk_widget_set_tooltip_text(tab, title); return NSERROR_BAD_PARAMETER;
}
tab_label = gtk_notebook_get_tab_label(notebook, page);
if (tab_label == NULL) {
return NSERROR_INVALID;
}
label = GTK_LABEL(g_object_get_data(G_OBJECT(tab_label), "label"));
gtk_label_set_text(label, title);
gtk_widget_set_tooltip_text(tab_label, title);
return NSERROR_OK;
} }
/* exported interface documented in gtk/tabs.h */ /* exported interface documented in gtk/tabs.h */

View File

@ -34,10 +34,16 @@ struct gui_window;
nserror nsgtk_notebook_create(GtkBuilder *builder, GtkNotebook **notebook_out); nserror nsgtk_notebook_create(GtkBuilder *builder, GtkNotebook **notebook_out);
/** /**
* Add new tab to notebook. * Add new gui window page to notebook.
*/ */
void nsgtk_tab_add(struct gui_window *window, GtkWidget *tab_contents, bool background, const char *title, GdkPixbuf *icon_pixbuf); void nsgtk_tab_add(struct gui_window *window, GtkWidget *tab_contents, bool background, const char *title, GdkPixbuf *icon_pixbuf);
/**
* Add new page to a notebook
*/
nserror nsgtk_tab_add_page(GtkNotebook *notebook, GtkWidget *tab_contents, bool background, const char *title, GdkPixbuf *icon_pixbuf);
/** /**
* set the tab title * set the tab title
* *
@ -45,21 +51,22 @@ void nsgtk_tab_add(struct gui_window *window, GtkWidget *tab_contents, bool back
* *
* \note currently only called from nsgtk_window_set_title() * \note currently only called from nsgtk_window_set_title()
* *
* \param g the gui window to set tab title for. * \param page The page widget that was added to the notebook
* \param title The title text which may not be NULL. * \param title The title text which may not be NULL.
* \return NSERROR_OK on sucess else appropriate code.
*/ */
void nsgtk_tab_set_title(struct gui_window *g, const char *title); nserror nsgtk_tab_set_title(GtkWidget *page, const char *title);
/** /**
* set the tab icon * set the tab icon
* *
* The tab icon will be set to the \a pixbuf parameter * The tab icon will be set to the \a pixbuf parameter
* *
* \param gw The gui window to set teh tab icon for. * \param page The page widget that was added to the notebook
* \param pixbuf The pixbuf to set the icon to. * \param pixbuf The pixbuf to set the icon to.
* \return NSERROR_OK on sucess else appropriate code. * \return NSERROR_OK on sucess else appropriate code.
*/ */
nserror nsgtk_tab_set_icon(struct gui_window *gw, GdkPixbuf *pixbuf); nserror nsgtk_tab_set_icon(GtkWidget *page, GdkPixbuf *pixbuf);
void nsgtk_tab_options_changed(GtkNotebook *notebook); void nsgtk_tab_options_changed(GtkNotebook *notebook);
nserror nsgtk_tab_close_current(GtkNotebook *notebook); nserror nsgtk_tab_close_current(GtkNotebook *notebook);

View File

@ -148,9 +148,10 @@ struct nsgtk_toolbar_customization {
struct nsgtk_toolbar toolbar; struct nsgtk_toolbar toolbar;
/** /**
* toolbar gtk builder * The top level container (tabBox)
*/ */
GtkBuilder *builder; GtkWidget *container;
}; };
/** /**
@ -1530,16 +1531,34 @@ static gboolean cutomize_button_clicked_cb(GtkWidget *widget, gpointer data)
struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data; struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
struct nsgtk_toolbar_customization *tbc; struct nsgtk_toolbar_customization *tbc;
nserror res; nserror res;
GtkBuilder *builder;
GtkNotebook *notebook;
struct gui_window *gw;
/* create builder */
res = nsgtk_builder_new_from_resname("toolbar", &builder);
if (res != NSERROR_OK) {
NSLOG(netsurf, INFO, "Toolbar UI builder init failed");
return TRUE;
}
gtk_builder_connect_signals(builder, NULL);
/* create nsgtk_toolbar_customization which has nsgtk_toolbar /* create nsgtk_toolbar_customization which has nsgtk_toolbar
* at the front so we can reuse functions that take * at the front so we can reuse functions that take
* nsgtk_toolbar * nsgtk_toolbar
*/ */
tbc = calloc(1, sizeof(struct nsgtk_toolbar_customization)); tbc = calloc(1, sizeof(struct nsgtk_toolbar_customization));
if (tbc == NULL) {
g_object_unref(builder);
return TRUE;
}
/* create builder*/ /* get container box widget which forms a page of the tabs */
res = nsgtk_builder_new_from_resname("toolbar", &tbc->builder); tbc->container = GTK_WIDGET(gtk_builder_get_object(builder, "tabBox"));
if (res != NSERROR_OK) { if (tbc->container == NULL) {
NSLOG(netsurf, INFO, "Toolbar UI builder init failed"); free(tbc);
g_object_unref(builder);
NSLOG(netsurf, ERROR, "dammit");
return TRUE; return TRUE;
} }
@ -1550,6 +1569,22 @@ static gboolean cutomize_button_clicked_cb(GtkWidget *widget, gpointer data)
/* save and update on apply button then discard */ /* save and update on apply button then discard */
/* discard button causes destruction */ /* discard button causes destruction */
/* close and cleanup on destroy signal */ /* close and cleanup on destroy signal */
gw = tb->get_ctx; /** \todo stop assuming the context is a gui window */
notebook = nsgtk_scaffolding_notebook(nsgtk_get_scaffold(gw));
nsgtk_tab_add_page(notebook,
tbc->container,
false,
messages_get("gtkCustomizeToolbarTitle"),
favicon_pixbuf);
/* safe to drop the reference to the builder as the container is
* referenced by the notebook now.
*/
g_object_unref(builder);
return TRUE; return TRUE;
} }

View File

@ -31,11 +31,13 @@
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include <gdk-pixbuf/gdk-pixdata.h> #include <gdk-pixbuf/gdk-pixdata.h>
#include "netsurf/inttypes.h" #include "utils/utils.h"
#include "utils/log.h" #include "utils/log.h"
#include "utils/utf8.h" #include "utils/utf8.h"
#include "utils/nsoption.h" #include "utils/nsoption.h"
#include "utils/messages.h" #include "utils/messages.h"
#include "utils/nsurl.h"
#include "netsurf/inttypes.h"
#include "netsurf/content.h" #include "netsurf/content.h"
#include "netsurf/browser_window.h" #include "netsurf/browser_window.h"
#include "netsurf/mouse.h" #include "netsurf/mouse.h"
@ -45,7 +47,6 @@
#include "netsurf/keypress.h" #include "netsurf/keypress.h"
#include "desktop/searchweb.h" #include "desktop/searchweb.h"
#include "desktop/textinput.h" #include "desktop/textinput.h"
#include "utils/nsurl.h"
#include "gtk/selection.h" #include "gtk/selection.h"
#include "gtk/warn.h" #include "gtk/warn.h"
@ -940,7 +941,7 @@ gui_window_set_icon(struct gui_window *gw, struct hlcache_handle *icon)
gw->icon = favicon_pixbuf; gw->icon = favicon_pixbuf;
} }
nsgtk_tab_set_icon(gw, gw->icon); nsgtk_tab_set_icon(gw->container, gw->icon);
} }
@ -1377,6 +1378,22 @@ static nserror gui_window_set_url(struct gui_window *gw, nsurl *url)
} }
/**
* GTK window UI callback when core changes the current title
*
* \param gw The gui window on which the url has been set.
* \param url The new url.
*/
static void gui_window_set_title(struct gui_window *gw, const char *title)
{
if ((title != NULL) && (title[0] != '\0')) {
nsgtk_tab_set_title(gw->container, title);
}
nsgtk_scaffolding_set_title(gw, title);
}
/** /**
* GTK UI callback when search provider details are updated. * GTK UI callback when search provider details are updated.
* *
@ -1425,6 +1442,7 @@ static struct gui_window_table window_table = {
.event = gui_window_event, .event = gui_window_event,
.set_icon = gui_window_set_icon, .set_icon = gui_window_set_icon,
.set_title = gui_window_set_title,
.set_status = gui_window_set_status, .set_status = gui_window_set_status,
.set_pointer = gui_window_set_pointer, .set_pointer = gui_window_set_pointer,
.place_caret = gui_window_place_caret, .place_caret = gui_window_place_caret,
@ -1432,8 +1450,7 @@ static struct gui_window_table window_table = {
.file_gadget_open = gui_window_file_gadget_open, .file_gadget_open = gui_window_file_gadget_open,
.set_url = gui_window_set_url, .set_url = gui_window_set_url,
/* from scaffold */
.set_title = nsgtk_window_set_title,
}; };
struct gui_window_table *nsgtk_window_table = &window_table; struct gui_window_table *nsgtk_window_table = &window_table;
@ -1467,20 +1484,6 @@ GtkLayout *nsgtk_window_get_layout(struct gui_window *g)
} }
/* exported interface documented in window.h */
GtkWidget *nsgtk_window_get_tab(struct gui_window *g)
{
return g->tab;
}
/* exported interface documented in window.h */
void nsgtk_window_set_tab(struct gui_window *g, GtkWidget *w)
{
g->tab = w;
}
/* exported interface documented in window.h */ /* exported interface documented in window.h */
nserror nserror
nsgtk_window_item_activate(struct gui_window *gw, nsgtk_toolbar_button itemid) nsgtk_window_item_activate(struct gui_window *gw, nsgtk_toolbar_button itemid)

View File

@ -87,20 +87,6 @@ unsigned long nsgtk_window_get_signalhandler(struct gui_window *gw, int i);
*/ */
GtkLayout *nsgtk_window_get_layout(struct gui_window *gw); GtkLayout *nsgtk_window_get_layout(struct gui_window *gw);
/**
* get tab widget from gui window handle
*
* \param gw gui window handle
*/
GtkWidget *nsgtk_window_get_tab(struct gui_window *gw);
/**
* set tab widget associated with gui window handle
*
* \param gw gui window handle
* \param w gtk widget to associate
*/
void nsgtk_window_set_tab(struct gui_window *gw, GtkWidget *w);
/** /**
* activate the handler for a item in a toolbar of a gui window * activate the handler for a item in a toolbar of a gui window