From 0860ac499d47ea6049642ea84785dea77c2c8406 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 13 Aug 2011 22:02:36 +0000 Subject: refactor to fix warning about out of range enums split default image finding for toolbar and findbar reorder to avoid forward declarations refactor macros to be *slightly* less awful svn path=/trunk/netsurf/; revision=12640 --- gtk/theme.c | 831 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 438 insertions(+), 393 deletions(-) (limited to 'gtk') diff --git a/gtk/theme.c b/gtk/theme.c index dc5eb5ee4..6284e92d1 100644 --- a/gtk/theme.c +++ b/gtk/theme.c @@ -54,20 +54,99 @@ static char *current_theme_name = NULL; static struct nsgtk_theme_cache *theme_cache_menu = NULL; static struct nsgtk_theme_cache *theme_cache_toolbar = NULL; -static struct nsgtk_theme *nsgtk_theme_default(GtkIconSize s); -static GtkImage *nsgtk_theme_image_default(nsgtk_toolbar_button i, - GtkIconSize s); -static bool nsgtk_theme_verify(const char *themename); -static void nsgtk_theme_cache_image(nsgtk_toolbar_button i, - const char *filename, const char *path); -static void nsgtk_theme_cache_searchimage(nsgtk_search_buttons i, - const char *filename, const char *path); - -#ifdef WITH_THEME_INSTALL -static nserror theme_install_callback(hlcache_handle *c, - const hlcache_event *event, void *pw); -static bool theme_install_read(const char *data, unsigned long len); -#endif +/** + * \param themename contains a name of theme to check whether it may + * properly be added to the list; alternatively NULL to check the integrity + * of the list + * \return true for themename may be added / every item in the list is + * a valid directory + */ + +static bool nsgtk_theme_verify(const char *themename) +{ + long filelength; + FILE *fp; + size_t val = SLEN("themelist") + strlen(res_dir_location) + 1; + char buf[50]; + char themefile[val]; + snprintf(themefile, val, "%s%s", res_dir_location, "themelist"); + if (themename == NULL) { + char *filecontent, *testfile; + struct stat sta; + fp = fopen(themefile, "r+"); + if (fp == NULL) { + warn_user(messages_get("gtkFileError"), themefile); + return true; + } + fseek(fp, 0L, SEEK_END); + filelength = ftell(fp); + filecontent = malloc(filelength + + SLEN("gtk default theme\n") + SLEN("\n") + + 1); + if (filecontent == NULL) { + warn_user(messages_get("NoMemory"), 0); + fclose(fp); + return true; + } + strcpy(filecontent, "gtk default theme\n"); + fseek(fp, 0L, SEEK_SET); + while (fgets(buf, sizeof(buf), fp) != NULL) { + /* iterate list */ + buf[strlen(buf) - 1] = '\0'; + /* "\n\0" -> "\0\0" */ + testfile = malloc(strlen(res_dir_location) + + SLEN("themes/") + strlen(buf) + 1); + if (testfile == NULL) { + warn_user(messages_get("NoMemory"), 0); + free(filecontent); + fclose(fp); + return false; + } + sprintf(testfile, "%sthemes/%s", res_dir_location, + buf); + /* check every directory */ + if (access(testfile, R_OK) == 0) { + stat(testfile, &sta); + if (S_ISDIR(sta.st_mode)) { + buf[strlen(buf)] = '\n'; + /* "\0\0" -> "\n\0" */ + strcat(filecontent, buf); + } + } + free(testfile); + } + fclose(fp); + fp = fopen(themefile, "w"); + if (fp == NULL) { + warn_user(messages_get("gtkFileError"), themefile); + free(filecontent); + return true; + } + val = fwrite(filecontent, strlen(filecontent), 1, fp); + if (val == 0) + LOG(("empty write themelist")); + fclose(fp); + free(filecontent); + return true; + } else { + fp = fopen(themefile, "r"); + if (fp == NULL) { + warn_user(messages_get("gtkFileError"), themefile); + return false; + } + while (fgets(buf, sizeof(buf), fp) != NULL) { + buf[strlen(buf) - 1] = '\0'; + /* "\n\0" -> "\0\0" */ + if (strcmp(buf, themename) == 0) { + fclose(fp); + return false; + } + } + fclose(fp); + return true; + } + +} /** * called during gui init phase to retrieve theme name from file then @@ -77,8 +156,10 @@ static bool theme_install_read(const char *data, unsigned long len); void nsgtk_theme_init(void) { size_t len; - if (option_current_theme == 0) + if (option_current_theme == 0) { return; + } + len = SLEN("themelist") + strlen(res_dir_location) + 1; char themefile[len]; snprintf(themefile, len, "%s%s", res_dir_location, "themelist"); @@ -103,7 +184,7 @@ void nsgtk_theme_init(void) } } fclose(fp); - + while (list != NULL) { nsgtk_theme_implement(list); list = nsgtk_scaffolding_iterate(list); @@ -153,7 +234,7 @@ void nsgtk_theme_add(const char *themename) } fprintf(fp, "%s\n", themename); fclose(fp); - + /* notification that theme was added successfully */ notification = gtk_dialog_new_with_buttons(messages_get("gtkThemeAdd"), NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, @@ -165,118 +246,24 @@ void nsgtk_theme_add(const char *themename) len = SLEN("\t\t\t\t\t\t") + strlen(messages_get("gtkThemeAdd")) + 1; char labelcontent[len]; snprintf(labelcontent, len, "\t\t\t%s\t\t\t", - messages_get("gtkThemeAdd")); + messages_get("gtkThemeAdd")); label = gtk_label_new(labelcontent); if (label == NULL) { warn_user(messages_get("NoMemory"), 0); return; } g_signal_connect_swapped(notification, "response", - G_CALLBACK(gtk_widget_destroy), notification); + G_CALLBACK(gtk_widget_destroy), notification); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(notification)->vbox), label); gtk_widget_show_all(notification); - - /* update combo */ - if (wndPreferences == NULL) - return; - nsgtk_options_combo_theme_add(themename); - -} -/** - * \param themename contains a name of theme to check whether it may - * properly be added to the list; alternatively NULL to check the integrity - * of the list - * \return true for themename may be added / every item in the list is - * a valid directory - */ - -bool nsgtk_theme_verify(const char *themename) -{ - long filelength; - FILE *fp; - size_t val = SLEN("themelist") + strlen(res_dir_location) + 1; - char buf[50]; - char themefile[val]; - snprintf(themefile, val, "%s%s", res_dir_location, "themelist"); - if (themename == NULL) { - char *filecontent, *testfile; - struct stat sta; - fp = fopen(themefile, "r+"); - if (fp == NULL) { - warn_user(messages_get("gtkFileError"), themefile); - return true; - } - fseek(fp, 0L, SEEK_END); - filelength = ftell(fp); - filecontent = malloc(filelength + - SLEN("gtk default theme\n") + SLEN("\n") - + 1); - if (filecontent == NULL) { - warn_user(messages_get("NoMemory"), 0); - fclose(fp); - return true; - } - strcpy(filecontent, "gtk default theme\n"); - fseek(fp, 0L, SEEK_SET); - while (fgets(buf, sizeof(buf), fp) != NULL) { - /* iterate list */ - buf[strlen(buf) - 1] = '\0'; - /* "\n\0" -> "\0\0" */ - testfile = malloc(strlen(res_dir_location) + - SLEN("themes/") + strlen(buf) + 1); - if (testfile == NULL) { - warn_user(messages_get("NoMemory"), 0); - free(filecontent); - fclose(fp); - return false; - } - sprintf(testfile, "%sthemes/%s", res_dir_location, - buf); - /* check every directory */ - if (access(testfile, R_OK) == 0) { - stat(testfile, &sta); - if (S_ISDIR(sta.st_mode)) { - buf[strlen(buf)] = '\n'; - /* "\0\0" -> "\n\0" */ - strcat(filecontent, buf); - } - } - free(testfile); - } - fclose(fp); - fp = fopen(themefile, "w"); - if (fp == NULL) { - warn_user(messages_get("gtkFileError"), themefile); - free(filecontent); - return true; - } - val = fwrite(filecontent, strlen(filecontent), 1, fp); - if (val == 0) - LOG(("empty write themelist")); - fclose(fp); - free(filecontent); - return true; - } else { - fp = fopen(themefile, "r"); - if (fp == NULL) { - warn_user(messages_get("gtkFileError"), themefile); - return false; - } - while (fgets(buf, sizeof(buf), fp) != NULL) { - buf[strlen(buf) - 1] = '\0'; - /* "\n\0" -> "\0\0" */ - if (strcmp(buf, themename) == 0) { - fclose(fp); - return false; - } - } - fclose(fp); - return true; + /* update combo */ + if (wndPreferences != NULL) { + nsgtk_options_combo_theme_add(themename); } - } + /** * sets the images for a particular scaffolding according to the current theme */ @@ -290,86 +277,229 @@ void nsgtk_theme_implement(struct gtk_scaffolding *g) for (i = 0; i <= IMAGE_SET_POPUP_MENU; i++) theme[i] = nsgtk_theme_load(GTK_ICON_SIZE_MENU); - + theme[IMAGE_SET_BUTTONS] = - nsgtk_theme_load(GTK_ICON_SIZE_LARGE_TOOLBAR); + nsgtk_theme_load(GTK_ICON_SIZE_LARGE_TOOLBAR); for (i = BACK_BUTTON; i < PLACEHOLDER_BUTTON; i++) { - if ((i == URL_BAR_ITEM) || (i == THROBBER_ITEM) || - (i == WEBSEARCH_ITEM)) + if ((i == URL_BAR_ITEM) || (i == THROBBER_ITEM) || + (i == WEBSEARCH_ITEM)) continue; button = nsgtk_scaffolding_button(g, i); if (button == NULL) continue; /* gtk_image_menu_item_set_image accepts NULL image */ - if ((button->main != NULL) && - (theme[IMAGE_SET_MAIN_MENU] != NULL)) { + if ((button->main != NULL) && + (theme[IMAGE_SET_MAIN_MENU] != NULL)) { gtk_image_menu_item_set_image(button->main, - GTK_WIDGET( - theme[IMAGE_SET_MAIN_MENU]-> - image[i])); + GTK_WIDGET( + theme[IMAGE_SET_MAIN_MENU]-> + image[i])); gtk_widget_show_all(GTK_WIDGET(button->main)); } - if ((button->rclick != NULL) && - (theme[IMAGE_SET_RCLICK_MENU] != NULL)) { + if ((button->rclick != NULL) && + (theme[IMAGE_SET_RCLICK_MENU] != NULL)) { gtk_image_menu_item_set_image(button->rclick, - GTK_WIDGET( - theme[IMAGE_SET_RCLICK_MENU]-> - image[i])); + GTK_WIDGET( + theme[IMAGE_SET_RCLICK_MENU]-> + image[i])); gtk_widget_show_all(GTK_WIDGET(button->rclick)); } - if ((button->popup != NULL) && - (theme[IMAGE_SET_POPUP_MENU] != NULL)) { + if ((button->popup != NULL) && + (theme[IMAGE_SET_POPUP_MENU] != NULL)) { gtk_image_menu_item_set_image(button->popup, - GTK_WIDGET( - theme[IMAGE_SET_POPUP_MENU]-> - image[i])); + GTK_WIDGET( + theme[IMAGE_SET_POPUP_MENU]-> + image[i])); gtk_widget_show_all(GTK_WIDGET(button->popup)); } - if ((button->location != -1) && (button->button != NULL) && - (theme[IMAGE_SET_BUTTONS] != NULL)) { + if ((button->location != -1) && (button->button != NULL) && + (theme[IMAGE_SET_BUTTONS] != NULL)) { gtk_tool_button_set_icon_widget( - GTK_TOOL_BUTTON(button->button), - GTK_WIDGET( + GTK_TOOL_BUTTON(button->button), + GTK_WIDGET( theme[IMAGE_SET_BUTTONS]-> image[i])); gtk_widget_show_all(GTK_WIDGET(button->button)); } } - + /* set search bar images */ search = nsgtk_scaffolding_search(g); if ((search != NULL) && (theme[IMAGE_SET_MAIN_MENU] != NULL)) { /* gtk_tool_button_set_icon_widget accepts NULL image */ if (search->buttons[SEARCH_BACK_BUTTON] != NULL) { gtk_tool_button_set_icon_widget( - search->buttons[SEARCH_BACK_BUTTON], - GTK_WIDGET(theme[IMAGE_SET_MAIN_MENU]-> - searchimage[SEARCH_BACK_BUTTON])); + search->buttons[SEARCH_BACK_BUTTON], + GTK_WIDGET(theme[IMAGE_SET_MAIN_MENU]-> + searchimage[SEARCH_BACK_BUTTON])); gtk_widget_show_all(GTK_WIDGET( - search->buttons[SEARCH_BACK_BUTTON])); + search->buttons[SEARCH_BACK_BUTTON])); } if (search->buttons[SEARCH_FORWARD_BUTTON] != NULL) { gtk_tool_button_set_icon_widget( - search->buttons[SEARCH_FORWARD_BUTTON], - GTK_WIDGET(theme[IMAGE_SET_MAIN_MENU]-> - searchimage[SEARCH_FORWARD_BUTTON])); + search->buttons[SEARCH_FORWARD_BUTTON], + GTK_WIDGET(theme[IMAGE_SET_MAIN_MENU]-> + searchimage[SEARCH_FORWARD_BUTTON])); gtk_widget_show_all(GTK_WIDGET( - search->buttons[ - SEARCH_FORWARD_BUTTON])); + search->buttons[ + SEARCH_FORWARD_BUTTON])); } if (search->buttons[SEARCH_CLOSE_BUTTON] != NULL) { gtk_tool_button_set_icon_widget( - search->buttons[SEARCH_CLOSE_BUTTON], - GTK_WIDGET(theme[IMAGE_SET_MAIN_MENU]-> - searchimage[SEARCH_CLOSE_BUTTON])); + search->buttons[SEARCH_CLOSE_BUTTON], + GTK_WIDGET(theme[IMAGE_SET_MAIN_MENU]-> + searchimage[SEARCH_CLOSE_BUTTON])); gtk_widget_show_all(GTK_WIDGET( - search->buttons[SEARCH_CLOSE_BUTTON])); + search->buttons[SEARCH_CLOSE_BUTTON])); } } - for (i = 0; i < IMAGE_SET_COUNT; i++) - if (theme[i] != NULL) + for (i = 0; i < IMAGE_SET_COUNT; i++) { + if (theme[i] != NULL) { free(theme[i]); + } + } +} + +/** + * returns default image for buttons / menu items from gtk stock items + * \param tbbutton button reference + */ + +static GtkImage * +nsgtk_theme_image_default(nsgtk_toolbar_button tbbutton, GtkIconSize iconsize) +{ + GtkImage *image; /* The GTK image to return */ + char *imagefile; + size_t len; + + switch(tbbutton) { + +#define BUTTON_IMAGE(p, q) \ + case p##_BUTTON: \ + image = GTK_IMAGE(gtk_image_new_from_stock(#q, iconsize)); \ + break + + BUTTON_IMAGE(BACK, gtk-go-back); + BUTTON_IMAGE(FORWARD, gtk-go-forward); + BUTTON_IMAGE(STOP, gtk-stop); + BUTTON_IMAGE(RELOAD, gtk-refresh); + BUTTON_IMAGE(HOME, gtk-home); + BUTTON_IMAGE(NEWWINDOW, gtk-new); + BUTTON_IMAGE(NEWTAB, gtk-new); + BUTTON_IMAGE(OPENFILE, gtk-open); + BUTTON_IMAGE(CLOSETAB, gtk-close); + BUTTON_IMAGE(CLOSEWINDOW, gtk-close); + BUTTON_IMAGE(SAVEPAGE, gtk-save-as); + BUTTON_IMAGE(PRINTPREVIEW, gtk-print-preview); + BUTTON_IMAGE(PRINT, gtk-print); + BUTTON_IMAGE(QUIT, gtk-quit); + BUTTON_IMAGE(CUT, gtk-cut); + BUTTON_IMAGE(COPY, gtk-copy); + BUTTON_IMAGE(PASTE, gtk-paste); + BUTTON_IMAGE(DELETE, gtk-delete); + BUTTON_IMAGE(SELECTALL, gtk-select-all); + BUTTON_IMAGE(FIND, gtk-find); + BUTTON_IMAGE(PREFERENCES, gtk-preferences); + BUTTON_IMAGE(ZOOMPLUS, gtk-zoom-in); + BUTTON_IMAGE(ZOOMMINUS, gtk-zoom-out); + BUTTON_IMAGE(ZOOMNORMAL, gtk-zoom-100); + BUTTON_IMAGE(FULLSCREEN, gtk-fullscreen); + BUTTON_IMAGE(VIEWSOURCE, gtk-index); + BUTTON_IMAGE(CONTENTS, gtk-help); + BUTTON_IMAGE(ABOUT, gtk-about); +#undef BUTTON_IMAGE + + case HISTORY_BUTTON: + len = SLEN("arrow_down_8x32.png") +strlen(res_dir_location) + 1; + imagefile = malloc(len); + if (imagefile == NULL) { + warn_user(messages_get("NoMemory"), 0); + return NULL; + } + snprintf(imagefile, len, "%sarrow_down_8x32.png", + res_dir_location); + image = GTK_IMAGE(gtk_image_new_from_file(imagefile)); + free(imagefile); + break; + + default: + len = SLEN("themes/Alpha.png") + strlen(res_dir_location) + 1; + imagefile = malloc(len); + if (imagefile == NULL) { + warn_user(messages_get("NoMemory"), 0); + return NULL; + } + snprintf(imagefile, len, "%sthemes/Alpha.png",res_dir_location); + image = GTK_IMAGE(gtk_image_new_from_file(imagefile)); + free(imagefile); + break; + + } + return image; + +} + +/** + * returns default image for search buttons / menu items from gtk stock items + * \param tbbutton button reference + */ + +static GtkImage * +nsgtk_theme_searchimage_default(nsgtk_search_buttons tbbutton, + GtkIconSize iconsize) +{ + char *imagefile; + GtkImage *image; + switch(tbbutton) { + + case (SEARCH_BACK_BUTTON): + return GTK_IMAGE(gtk_image_new_from_stock("gtk-go-back", iconsize)); + case (SEARCH_FORWARD_BUTTON): + return GTK_IMAGE(gtk_image_new_from_stock("gtk-go-forward", + iconsize)); + case (SEARCH_CLOSE_BUTTON): + return GTK_IMAGE(gtk_image_new_from_stock("gtk-close", iconsize)); + default: { + size_t len = SLEN("themes/Alpha.png") + + strlen(res_dir_location) + 1; + imagefile = malloc(len); + if (imagefile == NULL) { + warn_user(messages_get("NoMemory"), 0); + return NULL; + } + snprintf(imagefile, len, "%sthemes/Alpha.png", + res_dir_location); + image = GTK_IMAGE( + gtk_image_new_from_file(imagefile)); + free(imagefile); + return image; + } + } +} + +/** + * loads the set of default images for the toolbar / menus + */ + +static struct nsgtk_theme *nsgtk_theme_default(GtkIconSize iconsize) +{ + struct nsgtk_theme *theme = malloc(sizeof(struct nsgtk_theme)); + int btnloop; + + if (theme == NULL) { + warn_user("NoMemory", 0); + return NULL; + } + + for (btnloop = BACK_BUTTON; btnloop < PLACEHOLDER_BUTTON ; btnloop++) { + theme->image[btnloop] = nsgtk_theme_image_default(btnloop, iconsize); + } + + for (btnloop = SEARCH_BACK_BUTTON; btnloop < SEARCH_BUTTONS_COUNT; btnloop++) { + theme->searchimage[btnloop] = nsgtk_theme_searchimage_default(btnloop, iconsize); + } + return theme; } /** @@ -380,101 +510,104 @@ void nsgtk_theme_implement(struct gtk_scaffolding *g) struct nsgtk_theme *nsgtk_theme_load(GtkIconSize s) { - if (current_theme_name == NULL) + if (current_theme_name == NULL) { return nsgtk_theme_default(s); + } struct nsgtk_theme *theme = malloc(sizeof(struct nsgtk_theme)); - if (theme == NULL) + if (theme == NULL) { return theme; - - if ((theme_cache_menu == NULL) || (theme_cache_toolbar == NULL)) + } + + if ((theme_cache_menu == NULL) || (theme_cache_toolbar == NULL)) { nsgtk_theme_prepare(); - + } + /* load theme from cache */ struct nsgtk_theme_cache *cachetheme = (s == GTK_ICON_SIZE_MENU) ? - theme_cache_menu : theme_cache_toolbar; + theme_cache_menu : theme_cache_toolbar; if (cachetheme == NULL) { free(theme); return NULL; } -#define SET_BUTTON_IMAGE(p, q, r)\ - if (p->image[q##_BUTTON] != NULL)\ - r->image[q##_BUTTON] = GTK_IMAGE(gtk_image_new_from_pixbuf(\ - p->image[q##_BUTTON]));\ - else\ - r->image[q##_BUTTON] = nsgtk_theme_image_default(\ - q##_BUTTON, s); - - SET_BUTTON_IMAGE(cachetheme, BACK, theme) - SET_BUTTON_IMAGE(cachetheme, HISTORY, theme) - SET_BUTTON_IMAGE(cachetheme, FORWARD, theme) - SET_BUTTON_IMAGE(cachetheme, STOP, theme) - SET_BUTTON_IMAGE(cachetheme, RELOAD, theme) - SET_BUTTON_IMAGE(cachetheme, HOME, theme) - SET_BUTTON_IMAGE(cachetheme, NEWWINDOW, theme) - SET_BUTTON_IMAGE(cachetheme, NEWTAB, theme) - SET_BUTTON_IMAGE(cachetheme, OPENFILE, theme) - SET_BUTTON_IMAGE(cachetheme, CLOSETAB, theme) - SET_BUTTON_IMAGE(cachetheme, CLOSEWINDOW, theme) - SET_BUTTON_IMAGE(cachetheme, SAVEPAGE, theme) - SET_BUTTON_IMAGE(cachetheme, PRINTPREVIEW, theme) - SET_BUTTON_IMAGE(cachetheme, PRINT, theme) - SET_BUTTON_IMAGE(cachetheme, QUIT, theme) - SET_BUTTON_IMAGE(cachetheme, CUT, theme) - SET_BUTTON_IMAGE(cachetheme, COPY, theme) - SET_BUTTON_IMAGE(cachetheme, PASTE, theme) - SET_BUTTON_IMAGE(cachetheme, DELETE, theme) - SET_BUTTON_IMAGE(cachetheme, SELECTALL, theme) - SET_BUTTON_IMAGE(cachetheme, PREFERENCES, theme) - SET_BUTTON_IMAGE(cachetheme, ZOOMPLUS, theme) - SET_BUTTON_IMAGE(cachetheme, ZOOMMINUS, theme) - SET_BUTTON_IMAGE(cachetheme, ZOOMNORMAL, theme) - SET_BUTTON_IMAGE(cachetheme, FULLSCREEN, theme) - SET_BUTTON_IMAGE(cachetheme, VIEWSOURCE, theme) - SET_BUTTON_IMAGE(cachetheme, CONTENTS, theme) - SET_BUTTON_IMAGE(cachetheme, ABOUT, theme) - SET_BUTTON_IMAGE(cachetheme, PDF, theme) - SET_BUTTON_IMAGE(cachetheme, PLAINTEXT, theme) - SET_BUTTON_IMAGE(cachetheme, DRAWFILE, theme) - SET_BUTTON_IMAGE(cachetheme, POSTSCRIPT, theme) - SET_BUTTON_IMAGE(cachetheme, FIND, theme) - SET_BUTTON_IMAGE(cachetheme, DOWNLOADS, theme) - SET_BUTTON_IMAGE(cachetheme, SAVEWINDOWSIZE, theme) - SET_BUTTON_IMAGE(cachetheme, TOGGLEDEBUGGING, theme) - SET_BUTTON_IMAGE(cachetheme, SAVEBOXTREE, theme) - SET_BUTTON_IMAGE(cachetheme, SAVEDOMTREE, theme) - SET_BUTTON_IMAGE(cachetheme, LOCALHISTORY, theme) - SET_BUTTON_IMAGE(cachetheme, GLOBALHISTORY, theme) - SET_BUTTON_IMAGE(cachetheme, ADDBOOKMARKS, theme) - SET_BUTTON_IMAGE(cachetheme, SHOWBOOKMARKS, theme) - SET_BUTTON_IMAGE(cachetheme, SHOWCOOKIES, theme) - SET_BUTTON_IMAGE(cachetheme, OPENLOCATION, theme) - SET_BUTTON_IMAGE(cachetheme, NEXTTAB, theme) - SET_BUTTON_IMAGE(cachetheme, PREVTAB, theme) - SET_BUTTON_IMAGE(cachetheme, GUIDE, theme) - SET_BUTTON_IMAGE(cachetheme, INFO, theme) -#undef SET_BUTTON_IMAGE -#define SET_BUTTON_IMAGE(p, q, qq, r)\ - if (qq->searchimage[SEARCH_##p##_BUTTON] != NULL)\ - r->searchimage[SEARCH_##p##_BUTTON] =\ - GTK_IMAGE(gtk_image_new_from_pixbuf(\ - qq->searchimage[\ - SEARCH_##p##_BUTTON]));\ - else if (qq->image[q##_BUTTON] != NULL)\ - r->searchimage[SEARCH_##p##_BUTTON] =\ - GTK_IMAGE(gtk_image_new_from_pixbuf(\ - qq->image[q##_BUTTON]));\ - else\ - r->searchimage[SEARCH_##p##_BUTTON] =\ - nsgtk_theme_image_default(\ - PLACEHOLDER_BUTTON + SEARCH_##p##_BUTTON, s); - - SET_BUTTON_IMAGE(BACK, BACK, cachetheme, theme) - SET_BUTTON_IMAGE(FORWARD, FORWARD, cachetheme, theme) - SET_BUTTON_IMAGE(CLOSE, CLOSEWINDOW, cachetheme, theme) +#define SET_BUTTON_IMAGE(p, q, r) \ + if (p->image[q##_BUTTON] != NULL) \ + r->image[q##_BUTTON] = GTK_IMAGE(gtk_image_new_from_pixbuf( \ + p->image[q##_BUTTON])); \ + else \ + r->image[q##_BUTTON] = nsgtk_theme_image_default(q##_BUTTON, s) + + SET_BUTTON_IMAGE(cachetheme, BACK, theme); + SET_BUTTON_IMAGE(cachetheme, HISTORY, theme); + SET_BUTTON_IMAGE(cachetheme, FORWARD, theme); + SET_BUTTON_IMAGE(cachetheme, STOP, theme); + SET_BUTTON_IMAGE(cachetheme, RELOAD, theme); + SET_BUTTON_IMAGE(cachetheme, HOME, theme); + SET_BUTTON_IMAGE(cachetheme, NEWWINDOW, theme); + SET_BUTTON_IMAGE(cachetheme, NEWTAB, theme); + SET_BUTTON_IMAGE(cachetheme, OPENFILE, theme); + SET_BUTTON_IMAGE(cachetheme, CLOSETAB, theme); + SET_BUTTON_IMAGE(cachetheme, CLOSEWINDOW, theme); + SET_BUTTON_IMAGE(cachetheme, SAVEPAGE, theme); + SET_BUTTON_IMAGE(cachetheme, PRINTPREVIEW, theme); + SET_BUTTON_IMAGE(cachetheme, PRINT, theme); + SET_BUTTON_IMAGE(cachetheme, QUIT, theme); + SET_BUTTON_IMAGE(cachetheme, CUT, theme); + SET_BUTTON_IMAGE(cachetheme, COPY, theme); + SET_BUTTON_IMAGE(cachetheme, PASTE, theme); + SET_BUTTON_IMAGE(cachetheme, DELETE, theme); + SET_BUTTON_IMAGE(cachetheme, SELECTALL, theme); + SET_BUTTON_IMAGE(cachetheme, PREFERENCES, theme); + SET_BUTTON_IMAGE(cachetheme, ZOOMPLUS, theme); + SET_BUTTON_IMAGE(cachetheme, ZOOMMINUS, theme); + SET_BUTTON_IMAGE(cachetheme, ZOOMNORMAL, theme); + SET_BUTTON_IMAGE(cachetheme, FULLSCREEN, theme); + SET_BUTTON_IMAGE(cachetheme, VIEWSOURCE, theme); + SET_BUTTON_IMAGE(cachetheme, CONTENTS, theme); + SET_BUTTON_IMAGE(cachetheme, ABOUT, theme); + SET_BUTTON_IMAGE(cachetheme, PDF, theme); + SET_BUTTON_IMAGE(cachetheme, PLAINTEXT, theme); + SET_BUTTON_IMAGE(cachetheme, DRAWFILE, theme); + SET_BUTTON_IMAGE(cachetheme, POSTSCRIPT, theme); + SET_BUTTON_IMAGE(cachetheme, FIND, theme); + SET_BUTTON_IMAGE(cachetheme, DOWNLOADS, theme); + SET_BUTTON_IMAGE(cachetheme, SAVEWINDOWSIZE, theme); + SET_BUTTON_IMAGE(cachetheme, TOGGLEDEBUGGING, theme); + SET_BUTTON_IMAGE(cachetheme, SAVEBOXTREE, theme); + SET_BUTTON_IMAGE(cachetheme, SAVEDOMTREE, theme); + SET_BUTTON_IMAGE(cachetheme, LOCALHISTORY, theme); + SET_BUTTON_IMAGE(cachetheme, GLOBALHISTORY, theme); + SET_BUTTON_IMAGE(cachetheme, ADDBOOKMARKS, theme); + SET_BUTTON_IMAGE(cachetheme, SHOWBOOKMARKS, theme); + SET_BUTTON_IMAGE(cachetheme, SHOWCOOKIES, theme); + SET_BUTTON_IMAGE(cachetheme, OPENLOCATION, theme); + SET_BUTTON_IMAGE(cachetheme, NEXTTAB, theme); + SET_BUTTON_IMAGE(cachetheme, PREVTAB, theme); + SET_BUTTON_IMAGE(cachetheme, GUIDE, theme); + SET_BUTTON_IMAGE(cachetheme, INFO, theme); #undef SET_BUTTON_IMAGE - return theme; + +#define SET_BUTTON_IMAGE(p, q, qq, r) \ + if (qq->searchimage[SEARCH_##p##_BUTTON] != NULL) \ + r->searchimage[SEARCH_##p##_BUTTON] = \ + GTK_IMAGE(gtk_image_new_from_pixbuf( \ + qq->searchimage[ \ + SEARCH_##p##_BUTTON])); \ + else if (qq->image[q##_BUTTON] != NULL) \ + r->searchimage[SEARCH_##p##_BUTTON] = \ + GTK_IMAGE(gtk_image_new_from_pixbuf( \ + qq->image[q##_BUTTON])); \ + else \ + r->searchimage[SEARCH_##p##_BUTTON] = \ + nsgtk_theme_searchimage_default( \ + SEARCH_##p##_BUTTON, s) + + SET_BUTTON_IMAGE(BACK, BACK, cachetheme, theme); + SET_BUTTON_IMAGE(FORWARD, FORWARD, cachetheme, theme); + SET_BUTTON_IMAGE(CLOSE, CLOSEWINDOW, cachetheme, theme); +#undef SET_BUTTON_IMAGE + return theme; } /** @@ -483,33 +616,40 @@ struct nsgtk_theme *nsgtk_theme_load(GtkIconSize s) * \param filename the image file name * \param path the path to the theme folder */ -void nsgtk_theme_cache_image(nsgtk_toolbar_button i, const char *filename, +static void +nsgtk_theme_cache_image(nsgtk_toolbar_button i, const char *filename, const char *path) { char fullpath[strlen(filename) + strlen(path) + 1]; sprintf(fullpath, "%s%s", path, filename); if (theme_cache_toolbar != NULL) theme_cache_toolbar->image[i] = - gdk_pixbuf_new_from_file_at_size(fullpath, - 24, 24, NULL); - if (theme_cache_menu != NULL) + gdk_pixbuf_new_from_file_at_size(fullpath, + 24, 24, NULL); + if (theme_cache_menu != NULL) { theme_cache_menu->image[i] = gdk_pixbuf_new_from_file_at_size( - fullpath, 16, 16, NULL); + fullpath, 16, 16, NULL); + } } -void nsgtk_theme_cache_searchimage(nsgtk_search_buttons i, +static void +nsgtk_theme_cache_searchimage(nsgtk_search_buttons i, const char *filename, const char *path) { char fullpath[strlen(filename) + strlen(path) + 1]; + sprintf(fullpath, "%s%s", path, filename); - if (theme_cache_toolbar != NULL) + if (theme_cache_toolbar != NULL) { theme_cache_toolbar->searchimage[i] = - gdk_pixbuf_new_from_file_at_size(fullpath, - 24, 24, NULL); - if (theme_cache_menu != NULL) + gdk_pixbuf_new_from_file_at_size(fullpath, + 24, 24, NULL); + } + + if (theme_cache_menu != NULL) { theme_cache_menu->searchimage[i] = - gdk_pixbuf_new_from_file_at_size(fullpath, - 16, 16, NULL); + gdk_pixbuf_new_from_file_at_size(fullpath, + 16, 16, NULL); + } } /** @@ -524,11 +664,11 @@ void nsgtk_theme_prepare(void) if (theme_cache_toolbar == NULL) theme_cache_toolbar = malloc(sizeof(struct nsgtk_theme_cache)); size_t len = strlen(res_dir_location) + SLEN("/themes/") + - strlen(current_theme_name) + 1; + strlen(current_theme_name) + 1; char path[len]; - snprintf(path, len, "%sthemes/%s/", res_dir_location, - current_theme_name); -#define CACHE_IMAGE(p, q, r)\ + snprintf(path, len, "%sthemes/%s/", res_dir_location, + current_theme_name); +#define CACHE_IMAGE(p, q, r) \ nsgtk_theme_cache_image(p##_BUTTON, #q ".png", r) CACHE_IMAGE(BACK, back, path); @@ -580,119 +720,60 @@ void nsgtk_theme_prepare(void) CACHE_IMAGE(GUIDE, helpguide, path); CACHE_IMAGE(INFO, helpinfo, path); #undef CACHE_IMAGE -#define CACHE_IMAGE(p, q, r)\ +#define CACHE_IMAGE(p, q, r) \ nsgtk_theme_cache_searchimage(p, #q ".png", r); - + CACHE_IMAGE(SEARCH_BACK_BUTTON, searchback, path); CACHE_IMAGE(SEARCH_FORWARD_BUTTON, searchforward, path); CACHE_IMAGE(SEARCH_CLOSE_BUTTON, searchclose, path); #undef CACHE_IMAGE } -/** - * returns default image for buttons / menu items from gtk stock items - * \param i button reference - */ - -GtkImage *nsgtk_theme_image_default(nsgtk_toolbar_button i, GtkIconSize s) -{ - char *imagefile; - GtkImage *image; - switch(i) { -#define BUTTON_IMAGE(p, q)\ - case p##_BUTTON:\ - return GTK_IMAGE(gtk_image_new_from_stock(#q, s)) - - BUTTON_IMAGE(BACK, gtk-go-back); - case HISTORY_BUTTON: { - size_t len = SLEN("arrow_down_8x32.png") + - strlen(res_dir_location) + 1; - imagefile = malloc(len); - if (imagefile == NULL) { - warn_user(messages_get("NoMemory"), 0); - return NULL; - } - snprintf(imagefile, len, "%sarrow_down_8x32.png", - res_dir_location); - image = GTK_IMAGE(gtk_image_new_from_file(imagefile)); - free(imagefile); - return image; - } - BUTTON_IMAGE(FORWARD, gtk-go-forward); - BUTTON_IMAGE(STOP, gtk-stop); - BUTTON_IMAGE(RELOAD, gtk-refresh); - BUTTON_IMAGE(HOME, gtk-home); - BUTTON_IMAGE(NEWWINDOW, gtk-new); - BUTTON_IMAGE(NEWTAB, gtk-new); - BUTTON_IMAGE(OPENFILE, gtk-open); - BUTTON_IMAGE(CLOSETAB, gtk-close); - BUTTON_IMAGE(CLOSEWINDOW, gtk-close); - BUTTON_IMAGE(SAVEPAGE, gtk-save-as); - BUTTON_IMAGE(PRINTPREVIEW, gtk-print-preview); - BUTTON_IMAGE(PRINT, gtk-print); - BUTTON_IMAGE(QUIT, gtk-quit); - BUTTON_IMAGE(CUT, gtk-cut); - BUTTON_IMAGE(COPY, gtk-copy); - BUTTON_IMAGE(PASTE, gtk-paste); - BUTTON_IMAGE(DELETE, gtk-delete); - BUTTON_IMAGE(SELECTALL, gtk-select-all); - BUTTON_IMAGE(FIND, gtk-find); - BUTTON_IMAGE(PREFERENCES, gtk-preferences); - BUTTON_IMAGE(ZOOMPLUS, gtk-zoom-in); - BUTTON_IMAGE(ZOOMMINUS, gtk-zoom-out); - BUTTON_IMAGE(ZOOMNORMAL, gtk-zoom-100); - BUTTON_IMAGE(FULLSCREEN, gtk-fullscreen); - BUTTON_IMAGE(VIEWSOURCE, gtk-index); - BUTTON_IMAGE(CONTENTS, gtk-help); - BUTTON_IMAGE(ABOUT, gtk-about); -#undef BUTTON_IMAGE - case (PLACEHOLDER_BUTTON + SEARCH_BACK_BUTTON): - return GTK_IMAGE(gtk_image_new_from_stock("gtk-go-back", s)); - case (PLACEHOLDER_BUTTON + SEARCH_FORWARD_BUTTON): - return GTK_IMAGE(gtk_image_new_from_stock("gtk-go-forward", - s)); - case (PLACEHOLDER_BUTTON + SEARCH_CLOSE_BUTTON): - return GTK_IMAGE(gtk_image_new_from_stock("gtk-close", s)); - default: { - size_t len = SLEN("themes/Alpha.png") + - strlen(res_dir_location) + 1; - imagefile = malloc(len); - if (imagefile == NULL) { - warn_user(messages_get("NoMemory"), 0); - return NULL; - } - snprintf(imagefile, len, "%sthemes/Alpha.png", - res_dir_location); - image = GTK_IMAGE( - gtk_image_new_from_file(imagefile)); - free(imagefile); - return image; - } - } -} #ifdef WITH_THEME_INSTALL + /** - * when CONTENT_THEME needs handling call this function + * handler saves theme data content as a local theme */ -void theme_install_start(hlcache_handle *c) + +static bool theme_install_read(const char *data, unsigned long len) { - assert(c); - assert(content_get_type(c) == CONTENT_THEME); + char *filename, *newfilename; + size_t namelen; + int handle = g_file_open_tmp("nsgtkthemeXXXXXX", &filename, NULL); + if (handle == -1) { + warn_user(messages_get("gtkFileError"), + "temporary theme file"); + return false; + } + ssize_t written = write(handle, data, len); + close(handle); + if ((unsigned)written != len) + return false; - /* stop theme sitting in memory cache */ - content_invalidate_reuse_data(c); + /* get name of theme; set as dirname */ + namelen = SLEN("themes/") + strlen(res_dir_location) + 1; + char dirname[namelen]; + snprintf(dirname, namelen, "%sthemes/", res_dir_location); - hlcache_handle_replace_callback(c, theme_install_callback, NULL); -} + /* save individual files in theme */ + newfilename = container_extract_theme(filename, dirname); + g_free(filename); + if (newfilename == NULL) + return false; + nsgtk_theme_add(newfilename); + free(newfilename); + return true; +} /** * Callback for fetchcache() for theme install fetches. */ -nserror theme_install_callback(hlcache_handle *c, +static nserror +theme_install_callback(hlcache_handle *c, const hlcache_event *event, void *pw) { switch (event->type) { @@ -705,13 +786,13 @@ nserror theme_install_callback(hlcache_handle *c, unsigned long source_size; source_data = content_get_source_data(c, &source_size); - + if (!theme_install_read(source_data, source_size)) warn_user("ThemeInvalid", 0); hlcache_handle_release(c); } - break; + break; case CONTENT_MSG_ERROR: warn_user(event->data.error, 0); @@ -732,55 +813,19 @@ nserror theme_install_callback(hlcache_handle *c, } /** - * handler saves theme data content as a local theme + * when CONTENT_THEME needs handling call this function */ - -bool theme_install_read(const char *data, unsigned long len) +void theme_install_start(hlcache_handle *c) { - char *filename, *newfilename; - size_t namelen; - int handle = g_file_open_tmp("nsgtkthemeXXXXXX", &filename, NULL); - if (handle == -1) { - warn_user(messages_get("gtkFileError"), - "temporary theme file"); - return false; - } - ssize_t written = write(handle, data, len); - close(handle); - if ((unsigned)written != len) - return false; - - /* get name of theme; set as dirname */ - namelen = SLEN("themes/") + strlen(res_dir_location) + 1; - char dirname[namelen]; - snprintf(dirname, namelen, "%sthemes/", res_dir_location); + assert(c); + assert(content_get_type(c) == CONTENT_THEME); - /* save individual files in theme */ - newfilename = container_extract_theme(filename, dirname); - g_free(filename); - if (newfilename == NULL) - return false; - nsgtk_theme_add(newfilename); - free(newfilename); - - return true; + /* stop theme sitting in memory cache */ + content_invalidate_reuse_data(c); + + hlcache_handle_replace_callback(c, theme_install_callback, NULL); } -#endif -/** - * loads the set of default images for the toolbar / menus - */ -struct nsgtk_theme *nsgtk_theme_default(GtkIconSize s) -{ - struct nsgtk_theme *theme = malloc(sizeof(struct nsgtk_theme)); - if (theme == NULL) { - warn_user("NoMemory", 0); - return NULL; - } - for (int i = BACK_BUTTON; i < PLACEHOLDER_BUTTON + - SEARCH_BUTTONS_COUNT; i++) - theme->image[i] = nsgtk_theme_image_default(i, s); - return theme; -} +#endif -- cgit v1.2.3