From be7a45fefed2736f7ed7f1799e9f2b1cd52235b7 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Tue, 16 Jun 2015 00:20:37 +0100 Subject: [PATCH] Change gtk to use resource API for throbber --- gtk/gui.c | 41 +++------------ gtk/res/netsurf.gresource.xml | 9 ++++ gtk/resources.c | 9 ++++ gtk/throbber.c | 95 +++++++++++++++-------------------- gtk/throbber.h | 2 +- 5 files changed, 65 insertions(+), 91 deletions(-) diff --git a/gtk/gui.c b/gtk/gui.c index 90991dae9..1df7bfc56 100644 --- a/gtk/gui.c +++ b/gtk/gui.c @@ -84,8 +84,6 @@ GdkPixbuf *win_default_icon_pixbuf; /** default window icon pixbuf */ GtkBuilder *warning_builder; -#define THROBBER_FRAMES 9 - char **respaths; /** resource search path vector */ /** @@ -125,36 +123,6 @@ nsgtk_init_resource(const char *resource_path) return respath; } -/* This is an ugly hack to just get the new-style throbber going. - * It, along with the PNG throbber loader, need making more generic. - */ -static bool nsgtk_throbber_init(char **respath, int framec) -{ - char **filenames; - char targetname[PATH_MAX]; - int frame_num; - bool ret; - - filenames = calloc(framec, sizeof(char *)); - if (filenames == NULL) - return false; - - for (frame_num = 0; frame_num < framec; frame_num++) { - snprintf(targetname, PATH_MAX, "throbber/throbber%d.png", frame_num); - filenames[frame_num] = filepath_find(respath, targetname); - } - - ret = nsgtk_throbber_initialise_from_png(frame_num, filenames); - - for (frame_num = 0; frame_num < framec; frame_num++) { - free(filenames[frame_num]); - } - free(filenames); - - return ret; - -} - /** * Set option defaults for gtk frontend. @@ -307,9 +275,12 @@ static nserror nsgtk_init(int argc, char** argv, char **respath) toolbar_indices_file_location = filepath_find(respath, "toolbarIndices"); LOG("Using '%s' as custom toolbar settings file", toolbar_indices_file_location); - /* load throbber images */ - if (nsgtk_throbber_init(respath, THROBBER_FRAMES) == false) - die("Unable to load throbber image.\n"); + /* initialise throbber */ + error = nsgtk_throbber_init(); + if (error != NSERROR_OK) { + LOG("Unable to initialise throbber."); + return error; + } /* Initialise completions - cannot fail */ nsgtk_completion_init(); diff --git a/gtk/res/netsurf.gresource.xml b/gtk/res/netsurf.gresource.xml index 09683faa9..55f3a3dc8 100644 --- a/gtk/res/netsurf.gresource.xml +++ b/gtk/res/netsurf.gresource.xml @@ -30,5 +30,14 @@ favicon.png netsurf.xpm menu_cursor.png + throbber/throbber0.png + throbber/throbber1.png + throbber/throbber2.png + throbber/throbber3.png + throbber/throbber4.png + throbber/throbber5.png + throbber/throbber6.png + throbber/throbber7.png + throbber/throbber8.png diff --git a/gtk/resources.c b/gtk/resources.c index d28414d60..d1c3c86fc 100644 --- a/gtk/resources.c +++ b/gtk/resources.c @@ -78,6 +78,15 @@ static struct nsgtk_resource_s gen_resource[] = { { "favicon.png", 11, NSGTK_RESOURCE_FILE, NULL }, { "netsurf.xpm", 11, NSGTK_RESOURCE_FILE, NULL }, { "menu_cursor.png", 15, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber0.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber1.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber2.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber3.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber4.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber5.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber6.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber7.png", 22, NSGTK_RESOURCE_FILE, NULL }, + { "throbber/throbber8.png", 22, NSGTK_RESOURCE_FILE, NULL }, { NULL, 0, NSGTK_RESOURCE_FILE, NULL }, }; diff --git a/gtk/throbber.c b/gtk/throbber.c index 8726763ed..725df4626 100644 --- a/gtk/throbber.c +++ b/gtk/throbber.c @@ -20,76 +20,61 @@ #include #include #include -#ifdef WITH_GIF -#include -#endif +#include + #include "utils/log.h" + +#include "gtk/resources.h" #include "gtk/throbber.h" -#include "gtk/bitmap.h" struct nsgtk_throbber *nsgtk_throbber = NULL; -/** - * Creates the throbber using a PNG for each frame. - * - * The number of frames must be at least two. The first frame is the - * inactive frame, others are the active frames. - * - * \param frames The number of frames. Must be at least two. - * \param frame_files Filenames of PNGs containing frames. - * \return true on success. - */ -bool nsgtk_throbber_initialise_from_png(const int frames, char** frame_files) -{ - GError *err = NULL; - struct nsgtk_throbber *throb; /**< structure we generate */ - bool errors_when_loading = false; /**< true if a frame failed */ - int frame_loop; - - if (frames < 2) { - /* we need at least two frames - one for idle, one for active */ - LOG("Insufficent number of frames in throbber animation!"); - LOG("(called with %d frames, where 2 is a minimum.)", frames); - return false; - } - - throb = malloc(sizeof(*throb)); - if (throb == NULL) - return false; +#define THROBBER_FRAMES 9 +#define THROBBER_FMT "throbber/throbber%d.png" - throb->nframes = frames; - throb->framedata = malloc(sizeof(GdkPixbuf *) * throb->nframes); +/* exported interface documented in gtk/throbber.h */ +nserror nsgtk_throbber_init(void) +{ + struct nsgtk_throbber *throb; /**< structure we generate */ + int frame; + char resname[] = THROBBER_FMT; + nserror res = NSERROR_OK; + + throb = malloc(sizeof(*throb)); + if (throb == NULL) { + return NSERROR_NOMEM; + } + + throb->framedata = malloc(sizeof(GdkPixbuf *) * THROBBER_FRAMES); if (throb->framedata == NULL) { free(throb); return false; } - - for (frame_loop = 0; frame_loop < frames; frame_loop++) { - throb->framedata[frame_loop] = gdk_pixbuf_new_from_file(frame_files[frame_loop], &err); - if (err != NULL) { - LOG("Error when loading %s: %s (%d)", frame_files[frame_loop], err->message, err->code); - throb->framedata[frame_loop] = NULL; - errors_when_loading = true; - } - } - - if (errors_when_loading == true) { - for (frame_loop = 0; frame_loop < frames; frame_loop++) { - if (throb->framedata[frame_loop] != NULL) - g_object_unref(throb->framedata[frame_loop]); - } - free(throb->framedata); - free(throb); - - return false; + for (frame = 0; frame < THROBBER_FRAMES; frame++) { + snprintf(resname, sizeof(resname), THROBBER_FMT, frame); + res = nsgdk_pixbuf_new_from_resname(resname, + throb->framedata + frame); + if (res != NSERROR_OK) { + break; + } + LOG("%s",resname); } - + + if (frame < 1) { + /* we need at least two frames - one for idle, one for active */ + LOG("Insufficent number of frames (%d) in throbber animation.", frame); + res = NSERROR_INIT_FAILED; + } + + throb->nframes = frame; nsgtk_throbber = throb; - - return true; + return res; + + } + void nsgtk_throbber_finalise(void) { int i; diff --git a/gtk/throbber.h b/gtk/throbber.h index 1463c9b26..e0b47e15c 100644 --- a/gtk/throbber.h +++ b/gtk/throbber.h @@ -29,7 +29,7 @@ struct nsgtk_throbber extern struct nsgtk_throbber *nsgtk_throbber; -bool nsgtk_throbber_initialise_from_png(const int frames, char** frame_files); +nserror nsgtk_throbber_init(void); void nsgtk_throbber_finalise(void); #endif /* __GTK_THROBBER_H__ */