NetSurf
throbber.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Rob Kendrick <rjek@netsurf-browser.org>
3 * Copyright 2008 Sean Fox <dyntryx@gmail.com>
4 *
5 * This file is part of NetSurf, http://www.netsurf-browser.org/
6 *
7 * NetSurf is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * NetSurf is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <stdarg.h>
23#include <gtk/gtk.h>
24#include <stdint.h>
25
26#include "utils/log.h"
27
28#include "gtk/resources.h"
29#include "gtk/throbber.h"
30
31/**
32 * Throbber images context
33 */
35{
36 int nframes; /**< Number of frames in the throbber */
37 GdkPixbuf **framedata; /* pixbuf data for the frames */
38};
39
40static struct nsgtk_throbber *nsgtk_throbber = NULL;
41
42#define THROBBER_FRAMES 9
43#define THROBBER_FMT "throbber/throbber%d.png"
44
45/* exported interface documented in gtk/throbber.h */
47{
48 nserror res = NSERROR_OK;
49 struct nsgtk_throbber *throb;
50 int frame;
51 char resname[] = THROBBER_FMT;
52
53 throb = malloc(sizeof(*throb));
54 if (throb == NULL) {
55 return NSERROR_NOMEM;
56 }
57
58 throb->framedata = malloc(sizeof(GdkPixbuf *) * THROBBER_FRAMES);
59 if (throb->framedata == NULL) {
60 free(throb);
61 return NSERROR_NOMEM;
62 }
63
64 for (frame = 0; frame < THROBBER_FRAMES; frame++) {
65 snprintf(resname, sizeof(resname), THROBBER_FMT, frame);
67 throb->framedata + frame);
68 if (res != NSERROR_OK) {
69 break;
70 }
71 NSLOG(netsurf, INFO, "%s", resname);
72 }
73
74 if (frame < 1) {
75 /* we need at least two frames - one for idle, one for active */
76 NSLOG(netsurf, INFO,
77 "Insufficent number of frames (%d) in throbber animation.",
78 frame);
80 }
81
82 throb->nframes = frame;
83 nsgtk_throbber = throb;
84 return res;
85}
86
87/* exported interface documented in gtk/throbber.h */
89{
90 int i;
91
92 for (i = 0; i < nsgtk_throbber->nframes; i++)
93 g_object_unref(nsgtk_throbber->framedata[i]);
94
96 free(nsgtk_throbber);
97
98 nsgtk_throbber = NULL;
99}
100
101
102/* exported interface documented in gtk/throbber.h */
103nserror nsgtk_throbber_get_frame(int frame, GdkPixbuf **pixbuf)
104{
105 nserror res = NSERROR_OK;
106
107 /* ensure initialisation */
108 if (nsgtk_throbber == NULL) {
109 res = nsgtk_throbber_init();
110 }
111 if (res != NSERROR_OK) {
112 return res;
113 }
114
115 /* ensure frame in range */
116 if ((frame < 0) || (frame >= nsgtk_throbber->nframes)) {
117 return NSERROR_BAD_SIZE;
118 }
119
120 /* ensure there is frame data */
121 if (nsgtk_throbber->framedata[frame] == NULL) {
122 return NSERROR_INVALID;
123 }
124
125 *pixbuf = nsgtk_throbber->framedata[frame];
126 return NSERROR_OK;
127}
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_INIT_FAILED
Initialisation failed.
Definition: errors.h:38
@ NSERROR_BAD_SIZE
Bad size.
Definition: errors.h:60
@ NSERROR_INVALID
Invalid data.
Definition: errors.h:49
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
nserror nsgtk_throbber_init(void)
Initialise global throbber context.
Definition: throbber.c:46
nserror nsgtk_throbber_get_frame(int frame, GdkPixbuf **pixbuf)
get the pixbuf of a given frame of the throbber
Definition: throbber.c:103
#define THROBBER_FMT
Definition: throbber.c:43
void nsgtk_throbber_finalise(void)
release global throbber context
Definition: throbber.c:88
#define THROBBER_FRAMES
Definition: throbber.c:42
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
nserror nsgdk_pixbuf_new_from_resname(const char *resname, GdkPixbuf **pixbuf_out)
Create gdk pixbuf for the named ui resource.
Definition: resources.c:470
Interface to gtk builtin resource handling.
Throbber images context.
Definition: throbber.c:35
GdkPixbuf ** framedata
Definition: throbber.c:37
int nframes
Number of frames in the throbber.
Definition: throbber.c:36