NetSurf
throbber.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 François Revol <mmu_man@users.sourceforge.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define __STDBOOL_H__ 1
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23extern "C" {
24#include "utils/log.h"
25}
26#include "beos/throbber.h"
27#include "beos/bitmap.h"
28#include "beos/fetch_rsrc.h"
29
30#include <File.h>
31#include <Resources.h>
32#include <TranslationUtils.h>
33
35
36/**
37 * Creates the throbber using a PNG for each frame. The number of frames must
38 * be at least two. The first frame is the inactive frame, others are the
39 * active frames.
40 *
41 * \param frames The number of frames. Must be at least two.
42 * \param ... Filenames of PNGs containing frames.
43 * \return true on success.
44 */
45bool nsbeos_throbber_initialise_from_png(const int frames, ...)
46{
47 va_list filenames;
48 struct nsbeos_throbber *throb; /**< structure we generate */
49 bool errors_when_loading = false; /**< true if a frame failed */
50
51 if (frames < 2) {
52 /* we need at least two frames - one for idle, one for active */
53 NSLOG(netsurf, INFO,
54 "Insufficent number of frames in throbber animation!");
55 NSLOG(netsurf, INFO,
56 "(called with %d frames, where 2 is a minimum.)",
57 frames);
58 return false;
59 }
60
61 BResources *res = get_app_resources();
62 if (res == NULL) {
63 NSLOG(netsurf, INFO, "Can't find resources for throbber!");
64 return false;
65 }
66
67 throb = (struct nsbeos_throbber *)malloc(sizeof(*throb));
68 throb->nframes = frames;
69 throb->framedata = (BBitmap **)malloc(sizeof(BBitmap *) * throb->nframes);
70
71 va_start(filenames, frames);
72
73 for (int i = 0; i < frames; i++) {
74 const char *fn = va_arg(filenames, const char *);
75 const void *data;
76 size_t size;
77 data = res->LoadResource('data', fn, &size);
78 throb->framedata[i] = NULL;
79 if (!data) {
80 NSLOG(netsurf, INFO, "Error when loading resource %s",
81 fn);
82 errors_when_loading = true;
83 continue;
84 }
85 BMemoryIO mem(data, size);
86 throb->framedata[i] = BTranslationUtils::GetBitmap(&mem);
87 if (throb->framedata[i] == NULL) {
88 NSLOG(netsurf, INFO,
89 "Error when loading %s: GetBitmap() returned NULL",
90 fn);
91 errors_when_loading = true;
92 }
93 }
94
95 va_end(filenames);
96
97 if (errors_when_loading == true) {
98 for (int i = 0; i < frames; i++) {
99 delete throb->framedata[i];
100 }
101
102 free(throb->framedata);
103 free(throb);
104
105 return false;
106 }
107
108 nsbeos_throbber = throb;
109
110 return true;
111}
112
114{
115 int i;
116
117 for (i = 0; i < nsbeos_throbber->nframes; i++)
118 delete nsbeos_throbber->framedata[i];
119
121 free(nsbeos_throbber);
122
123 nsbeos_throbber = NULL;
124}
BResources * get_app_resources()
Definition: fetch_rsrc.cpp:360
rsrc: URL method handler
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Interface to utility string handling.
int nframes
Number of frames in the throbber.
Definition: throbber.h:26
BBitmap ** framedata
Definition: throbber.h:27
bool nsbeos_throbber_initialise_from_png(const int frames,...)
Creates the throbber using a PNG for each frame.
Definition: throbber.cpp:45
void nsbeos_throbber_finalise(void)
Definition: throbber.cpp:113