From cfa5856eea7c0d840a19590baf1e66f6fee06b83 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 22 Apr 2018 09:31:48 +0100 Subject: Initial conversion of GTK accelerators to separate config file --- frontends/gtk/Makefile | 2 +- frontends/gtk/accelerator.c | 60 ++++++++++++++++++++++++++ frontends/gtk/accelerator.h | 2 + frontends/gtk/gui.c | 74 ++++++++++++++++++++------------- frontends/gtk/menu.c | 44 ++++++++++++-------- frontends/gtk/res/accelerators | 40 ++++++++++++++++++ frontends/gtk/res/netsurf.gresource.xml | 1 + frontends/gtk/resources.c | 30 +++++++------ 8 files changed, 193 insertions(+), 60 deletions(-) create mode 100644 frontends/gtk/accelerator.c create mode 100644 frontends/gtk/accelerator.h create mode 100644 frontends/gtk/res/accelerators (limited to 'frontends') diff --git a/frontends/gtk/Makefile b/frontends/gtk/Makefile index 6c2f06f06..ec60ce70c 100644 --- a/frontends/gtk/Makefile +++ b/frontends/gtk/Makefile @@ -165,7 +165,7 @@ endif # S_FRONTEND are sources purely for the GTK frontend S_FRONTEND := gui.c schedule.c layout_pango.c bitmap.c plotters.c \ - scaffolding.c gdk.c completion.c login.c throbber.c \ + scaffolding.c gdk.c completion.c login.c throbber.c accelerator.c \ selection.c window.c fetch.c download.c menu.c print.c \ search.c tabs.c toolbar.c gettext.c compat.c viewdata.c \ viewsource.c preferences.c about.c resources.c corewindow.c \ diff --git a/frontends/gtk/accelerator.c b/frontends/gtk/accelerator.c new file mode 100644 index 000000000..9339de8a8 --- /dev/null +++ b/frontends/gtk/accelerator.c @@ -0,0 +1,60 @@ +/* + * Copyright 2018 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file + * GTK accelerator support + * + */ + +#include +#include + +#include "utils/errors.h" +#include "utils/hashtable.h" + +#include "gtk/resources.h" +#include "gtk/accelerator.h" + +/** The hash table used to store the accelerators */ +static struct hash_table *accelerators_hash = NULL; + +nserror nsgtk_accelerator_init(char **respaths) +{ + nserror ret; + const uint8_t *data; + size_t data_size; + + ret = nsgtk_data_from_resname("accelerators", &data, &data_size); + if (ret == NSERROR_OK) { + //ret = hashtable_add_from_inline(data, data_size); + } else { + const char *accelerators; + /* Obtain path to accelerators */ + ret = nsgtk_path_from_resname("accelerators", &accelerators); + if (ret == NSERROR_OK) { + //ret = hashtable_add_from_file(messages); + } + } + return ret; +} + +const char *nsgtk_accelerator_get_desc(const char *key) +{ + return NULL; +} diff --git a/frontends/gtk/accelerator.h b/frontends/gtk/accelerator.h new file mode 100644 index 000000000..09c253eb9 --- /dev/null +++ b/frontends/gtk/accelerator.h @@ -0,0 +1,2 @@ +nserror nsgtk_accelerator_init(char **respaths); +const char *nsgtk_accelerator_get_desc(const char *key); diff --git a/frontends/gtk/gui.c b/frontends/gtk/gui.c index 0f79a1b8e..3163be16d 100644 --- a/frontends/gtk/gui.c +++ b/frontends/gtk/gui.c @@ -72,6 +72,7 @@ #include "gtk/resources.h" #include "gtk/login.h" #include "gtk/layout_pango.h" +#include "gtk/accelerator.h" bool nsgtk_complete = false; @@ -227,7 +228,11 @@ static nserror set_defaults(struct nsoption_s *defaults) /** - * Initialize GTK interface. + * Initialize GTK specific parts of the browser. + * + * \param argc The number of arguments on the command line + * \param argv A string vector of command line arguments. + * \respath A string vector of the path elements of resources */ static nserror nsgtk_init(int argc, char** argv, char **respath) { @@ -235,20 +240,29 @@ static nserror nsgtk_init(int argc, char** argv, char **respath) char *resource_filename; char *addr = NULL; nsurl *url; - nserror error; + nserror res; + + /* Initialise gtk accelerator table */ + res = nsgtk_accelerator_init(respaths); + if (res != NSERROR_OK) { + NSLOG(netsurf, INFO, + "Unable to load gtk accelerator configuration"); + /* not fatal if this does not load */ + } - error = nsgtk_builder_new_from_resname("warning", &warning_builder); - if (error != NSERROR_OK) { + /* initialise warning dialog */ + res = nsgtk_builder_new_from_resname("warning", &warning_builder); + if (res != NSERROR_OK) { NSLOG(netsurf, INFO, "Unable to initialise warning dialog"); - return error; + return res; } gtk_builder_connect_signals(warning_builder, NULL); /* set default icon if its available */ - error = nsgdk_pixbuf_new_from_resname("netsurf.xpm", - &win_default_icon_pixbuf); - if (error == NSERROR_OK) { + res = nsgdk_pixbuf_new_from_resname("netsurf.xpm", + &win_default_icon_pixbuf); + if (res == NSERROR_OK) { NSLOG(netsurf, INFO, "Seting default window icon"); gtk_window_set_default_icon(win_default_icon_pixbuf); } @@ -263,25 +277,25 @@ static nserror nsgtk_init(int argc, char** argv, char **respath) } /* Default favicon */ - error = nsgdk_pixbuf_new_from_resname("favicon.png", &favicon_pixbuf); - if (error != NSERROR_OK) { + res = nsgdk_pixbuf_new_from_resname("favicon.png", &favicon_pixbuf); + if (res != NSERROR_OK) { favicon_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8, 16, 16); } /* arrow down icon */ - error = nsgdk_pixbuf_new_from_resname("arrow_down_8x32.png", - &arrow_down_pixbuf); - if (error != NSERROR_OK) { + res = nsgdk_pixbuf_new_from_resname("arrow_down_8x32.png", + &arrow_down_pixbuf); + if (res != NSERROR_OK) { arrow_down_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8, 8, 32); } /* initialise throbber */ - error = nsgtk_throbber_init(); - if (error != NSERROR_OK) { + res = nsgtk_throbber_init(); + if (res != NSERROR_OK) { NSLOG(netsurf, INFO, "Unable to initialise throbber."); - return error; + return res; } /* Initialise completions - cannot fail */ @@ -302,13 +316,13 @@ static nserror nsgtk_init(int argc, char** argv, char **respath) urldb_load(nsoption_charp(url_file)); urldb_load_cookies(nsoption_charp(cookie_file)); hotlist_init(nsoption_charp(hotlist_path), - nsoption_charp(hotlist_path)); + nsoption_charp(hotlist_path)); /* Initialise top level UI elements */ - error = nsgtk_download_init(); - if (error != NSERROR_OK) { + res = nsgtk_download_init(); + if (res != NSERROR_OK) { NSLOG(netsurf, INFO, "Unable to initialise download window."); - return error; + return res; } /* If there is a url specified on the command line use it */ @@ -338,19 +352,19 @@ static nserror nsgtk_init(int argc, char** argv, char **respath) } /* create an initial browser window */ - error = nsurl_create(addr, &url); - if (error == NSERROR_OK) { - error = browser_window_create(BW_CREATE_HISTORY, - url, - NULL, - NULL, - NULL); + res = nsurl_create(addr, &url); + if (res == NSERROR_OK) { + res = browser_window_create(BW_CREATE_HISTORY, + url, + NULL, + NULL, + NULL); nsurl_unref(url); } free(addr); - return error; + return res; } @@ -1163,7 +1177,7 @@ int main(int argc, char** argv) NSLOG(netsurf, INFO, "Unable to load translated messages"); /** \todo decide if message load faliure should be fatal */ } - + /* Locate the correct user cache directory path */ ret = get_cache_home(&cache_home); if (ret == NSERROR_NOT_FOUND) { @@ -1183,7 +1197,7 @@ int main(int argc, char** argv) return 1; } - /* run the browser */ + /* gtk specific initalisation and main run loop */ ret = nsgtk_init(argc, argv, respaths); if (ret != NSERROR_OK) { fprintf(stderr, "NetSurf gtk initialise failed (%s)\n", diff --git a/frontends/gtk/menu.c b/frontends/gtk/menu.c index a93ef9385..6a6033231 100644 --- a/frontends/gtk/menu.c +++ b/frontends/gtk/menu.c @@ -27,6 +27,7 @@ #include "gtk/compat.h" #include "gtk/menu.h" #include "gtk/warn.h" +#include "gtk/accelerator.h" /** * Adds image menu item to a menu. @@ -34,28 +35,37 @@ * \param menu the menu to add the item to * \param item_out a pointer to the item's location in the menu struct * \param message the menu item I18n lookup value - * \param messageAccel the menu item accelerator I18n lookup value * \param group the 'global' in a gtk sense accelerator group * \return true if sucessful and \a item_out updated else false. */ -static bool nsgtk_menu_add_image_item(GtkMenu *menu, - GtkWidget **item_out, const char *message, - const char *messageAccel, GtkAccelGroup *group) +static bool +nsgtk_menu_add_image_item(GtkMenu *menu, + GtkWidget **item_out, + const char *message, + GtkAccelGroup *group) { unsigned int key; GdkModifierType mod; GtkWidget *item; - + const char *accelerator_desc; /* accelerator key description */ + item = nsgtk_image_menu_item_new_with_mnemonic(messages_get(message)); if (item == NULL) { return false; } - - gtk_accelerator_parse(messages_get(messageAccel), &key, &mod); - if (key > 0) { - gtk_widget_add_accelerator(item, "activate", group, key, mod, - GTK_ACCEL_VISIBLE); + + accelerator_desc = nsgtk_accelerator_get_desc(message); + if (accelerator_desc != NULL) { + gtk_accelerator_parse(accelerator_desc, &key, &mod); + if (key > 0) { + gtk_widget_add_accelerator(item, + "activate", + group, + key, + mod, + GTK_ACCEL_VISIBLE); + } } gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); gtk_widget_show(item); @@ -73,8 +83,7 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu, n->m##_menu = GTK_MENU(gtk_menu_new()) #define IMAGE_ITEM(p, q, r, s, t)\ - nsgtk_menu_add_image_item(s->p##_menu, &(s->q##_menuitem), #r,\ - #r "Accel", t) + nsgtk_menu_add_image_item(s->p##_menu, &(s->q##_menuitem), #r, t) #define CHECK_ITEM(p, q, r, s)\ s->q##_menuitem = GTK_CHECK_MENU_ITEM(\ @@ -130,8 +139,8 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu, * creates an export submenu * \param group the 'global' in a gtk sense accelerator reference */ - -static struct nsgtk_export_submenu *nsgtk_menu_export_submenu(GtkAccelGroup *group) +static struct nsgtk_export_submenu * +nsgtk_menu_export_submenu(GtkAccelGroup *group) { struct nsgtk_export_submenu *ret = malloc(sizeof(struct nsgtk_export_submenu)); @@ -157,8 +166,8 @@ static struct nsgtk_export_submenu *nsgtk_menu_export_submenu(GtkAccelGroup *gro * \param group the 'global' in a gtk sense accelerator reference */ -static struct nsgtk_scaleview_submenu *nsgtk_menu_scaleview_submenu( - GtkAccelGroup *group) +static struct nsgtk_scaleview_submenu * +nsgtk_menu_scaleview_submenu(GtkAccelGroup *group) { struct nsgtk_scaleview_submenu *ret = malloc(sizeof(struct nsgtk_scaleview_submenu)); @@ -185,7 +194,8 @@ static struct nsgtk_scaleview_submenu *nsgtk_menu_scaleview_submenu( static struct nsgtk_tabs_submenu *nsgtk_menu_tabs_submenu(GtkAccelGroup *group) { - struct nsgtk_tabs_submenu *ret = malloc(sizeof(struct nsgtk_tabs_submenu)); + struct nsgtk_tabs_submenu *ret; + ret = malloc(sizeof(struct nsgtk_tabs_submenu)); if (ret == NULL) { nsgtk_warning(messages_get("NoMemory"), 0); return NULL; diff --git a/frontends/gtk/res/accelerators b/frontends/gtk/res/accelerators new file mode 100644 index 000000000..e4140fce5 --- /dev/null +++ b/frontends/gtk/res/accelerators @@ -0,0 +1,40 @@ +# GTK accelerator keys for menu entries +# The keys must match those in the menus to be applied +# +# These are passed to gtk_accelerator_parse and must not be translated +# The key names are the same as those in the gdk/gdkkeysyms.h header file +# but without the leading “GDK_KEY_”. + +gtkNextTab:Right +gtkPrevTab:Left +gtkCloseTab:w +gtkNewTab:t +gtkNewWindow:n +gtkOpenFile:o +gtkCloseWindow:w +gtkSavePage:s +gtkPrintPreview:p +gtkPrint:p +gtkQuitMenu:q +gtkCut:x +gtkCopy:c +gtkPaste:v +gtkSelectAll:a +gtkFind:f +gtkStop:Escape +gtkReload:F5 +gtkZoomPlus:plus +gtkZoomMinus:minus +gtkZoomNormal:0 +gtkFullScreen:F11 +gtkPageSource:U +gtkDownloads:j +gtkBack:Left +gtkForward:Right +gtkHome:Down +gtkLocalHistory:h +gtkGlobalHistory:h +gtkAddBookMarks:d +gtkShowBookMarks:F6 +gtkShowCookies:F9 +gtkOpenLocation:l diff --git a/frontends/gtk/res/netsurf.gresource.xml b/frontends/gtk/res/netsurf.gresource.xml index 5bae777f3..e8243254a 100644 --- a/frontends/gtk/res/netsurf.gresource.xml +++ b/frontends/gtk/res/netsurf.gresource.xml @@ -68,5 +68,6 @@ icons/hotlist-rmv.png icons/search.png languages + accelerators diff --git a/frontends/gtk/resources.c b/frontends/gtk/resources.c index ef92fefc8..fc3ac6ff3 100644 --- a/frontends/gtk/resources.c +++ b/frontends/gtk/resources.c @@ -129,6 +129,7 @@ static struct nsgtk_resource_s direct_resource[] = { RES_ENTRY("icons/hotlist-rmv.png"), RES_ENTRY("icons/search.png"), RES_ENTRY("languages"), + RES_ENTRY("accelerators"), RES_ENTRY("Messages"), { NULL, 0, NSGTK_RESOURCE_FILE, NULL }, }; @@ -178,11 +179,12 @@ init_resource(char **respath, struct nsgtk_resource_s *resource) langv = g_get_language_names(); + /* look for resource under per language paths */ while (langv[langc] != NULL) { + /* allocate and fill a full resource name path buffer */ resnamelen = snprintf(NULL, 0, "/org/netsurf/%s/%s", langv[langc], resource->name); - resname = malloc(resnamelen + 1); if (resname == NULL) { return NSERROR_NOMEM; @@ -191,6 +193,7 @@ init_resource(char **respath, struct nsgtk_resource_s *resource) "/org/netsurf/%s/%s", langv[langc], resource->name); + /* check if resource is present */ present = g_resources_get_info(resname, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL); @@ -208,8 +211,9 @@ init_resource(char **respath, struct nsgtk_resource_s *resource) langc++; } - resnamelen = snprintf(NULL, 0, "/org/netsurf/%s", resource->name); + /* allocate and fill a full resource name path buffer with no language*/ + resnamelen = snprintf(NULL, 0, "/org/netsurf/%s", resource->name); resname = malloc(resnamelen + 1); if (resname == NULL) { return NSERROR_NOMEM; @@ -232,20 +236,22 @@ init_resource(char **respath, struct nsgtk_resource_s *resource) #endif + /* look for file on disc */ resname = filepath_find(respath, resource->name); - if (resname == NULL) { + if (resname != NULL) { + /* found an entry on the path */ + resource->path = resname; + resource->type = NSGTK_RESOURCE_FILE; + NSLOG(netsurf, INFO, - "Unable to find resource %s on resource path", - resource->name); - return NSERROR_NOT_FOUND; + "Found file resource path %s", resource->path); + return NSERROR_OK; } - /* found an entry on the path */ - resource->path = resname; - resource->type = NSGTK_RESOURCE_FILE; + NSLOG(netsurf, INFO, "Unable to find resource %s on resource path", + resource->name); - NSLOG(netsurf, INFO, "Found file resource path %s", resource->path); - return NSERROR_OK; + return NSERROR_NOT_FOUND; } /** @@ -381,7 +387,7 @@ find_resource_from_name(const char *resname, struct nsgtk_resource_s *resource) #ifdef SHOW_GRESOURCE /** - * Debug dump of all resources compile din via GResource. + * Debug dump of all resources compiled in via GResource. */ static void list_gresource(void) { -- cgit v1.2.3