Change gtk to use resource API for throbber

This commit is contained in:
Vincent Sanders 2015-06-16 00:20:37 +01:00
parent 3e2270482f
commit be7a45fefe
5 changed files with 65 additions and 91 deletions

View File

@ -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();

View File

@ -30,5 +30,14 @@
<file preprocess="to-pixdata">favicon.png</file>
<file preprocess="to-pixdata">netsurf.xpm</file>
<file preprocess="to-pixdata">menu_cursor.png</file>
<file preprocess="to-pixdata">throbber/throbber0.png</file>
<file preprocess="to-pixdata">throbber/throbber1.png</file>
<file preprocess="to-pixdata">throbber/throbber2.png</file>
<file preprocess="to-pixdata">throbber/throbber3.png</file>
<file preprocess="to-pixdata">throbber/throbber4.png</file>
<file preprocess="to-pixdata">throbber/throbber5.png</file>
<file preprocess="to-pixdata">throbber/throbber6.png</file>
<file preprocess="to-pixdata">throbber/throbber7.png</file>
<file preprocess="to-pixdata">throbber/throbber8.png</file>
</gresource>
</gresources>

View File

@ -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 },
};

View File

@ -20,76 +20,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef WITH_GIF
#include <libnsgif.h>
#endif
#include <gtk/gtk.h>
#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;
#define THROBBER_FRAMES 9
#define THROBBER_FMT "throbber/throbber%d.png"
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;
}
/* 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 false;
if (throb == NULL) {
return NSERROR_NOMEM;
}
throb->nframes = frames;
throb->framedata = malloc(sizeof(GdkPixbuf *) * throb->nframes);
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;
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 (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;
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 res;
return true;
}
void nsgtk_throbber_finalise(void)
{
int i;

View File

@ -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__ */