NetSurf
throbber.c
Go to the documentation of this file.
1/*
2 * Copyright 2004, 2005 Richard Wilson <info@tinct.net>
3 * Copyright 2011 Stephen Fryatt <stevef@netsurf-browser.org>
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/** \file
21 * Throbber (implementation).
22 */
23
24#include <alloca.h>
25#include <assert.h>
26#include <stdio.h>
27#include <stdbool.h>
28#include <stdlib.h>
29#include <string.h>
30#include "oslib/os.h"
31#include "oslib/osspriteop.h"
32#include "oslib/wimp.h"
33
34#include "utils/log.h"
35#include "utils/utils.h"
36#include "riscos/gui.h"
37
38#include "riscos/gui/throbber.h"
39#include "riscos/theme.h"
40#include "riscos/wimp.h"
41
42#define THROBBER_SPRITE_NAME_LENGTH 12
43#define THROBBER_ANIMATE_INTERVAL 10
44
45struct throbber {
46 /** The applied theme (or NULL to use the default) */
48
49 /** The widget dimensions. */
51
52 /** The window and icon details. */
53 wimp_w window;
54 wimp_i icon;
55 os_box extent;
56 osspriteop_area *sprites;
57 bool hidden;
58 bool shaded;
59
60 /** The animation details. */
66};
67
68/*
69 * Private function prototypes.
70 */
71
74
75/* This is an exported interface documented in throbber.h */
76
78{
79 struct throbber *throbber;
80
81 /* Allocate memory. */
82
83 throbber = malloc(sizeof(struct throbber));
84 if (throbber == NULL) {
85 NSLOG(netsurf, INFO, "No memory for malloc()");
86 return NULL;
87 }
88
89 /* Set up default parameters. If reading the throbber theme data
90 * fails, we give up and return a failure.
91 */
92
94 &throbber->x_min, &throbber->y_min, NULL,
96 free(throbber);
97 return NULL;
98 }
99
101
103
104 throbber->extent.x0 = 0;
105 throbber->extent.y0 = 0;
106 throbber->extent.x1 = 0;
107 throbber->extent.y1 = 0;
108
111
112 throbber->window = NULL;
113 throbber->icon = -1;
114
115 throbber->hidden = false;
116 throbber->shaded = false;
117
118 return throbber;
119}
120
121
122/* This is an exported interface documented in throbber.h */
123
125 struct theme_descriptor *theme, theme_style style,
126 wimp_w window, bool shaded)
127{
128 if (throbber == NULL)
129 return false;
130
134
135 throbber->icon = -1;
136
138
139 strcpy(throbber->sprite_name, "throbber0");
140
142 &throbber->x_min, &throbber->y_min, NULL,
144 free(throbber);
145 return false;
146 }
147
149}
150
151
152/* This is an exported interface documented in throbber.h */
153
155{
156 if (throbber == NULL)
157 return;
158
159 free(throbber);
160}
161
162
163/* This is an exported interface documented in throbber.h */
164
166 int *width, int *height)
167{
168 if (throbber == NULL)
169 return false;
170
171 if (throbber->x_min != -1 && throbber->y_min != -1) {
172 if (width != NULL)
173 *width = throbber->x_min;
174 if (height != NULL)
176
177 return true;
178 }
179
180 return false;
181}
182
183
184/* This is an exported interface documented in throbber.h */
185
187 int x0, int y0, int x1, int y1)
188{
189 if (throbber == NULL)
190 return false;
191
192 if ((x1 - x0) < throbber->x_min || (y1 - y0) < throbber->y_min)
193 return false;
194
195 if (throbber->extent.x0 == x0 && throbber->extent.y0 == y0 &&
196 throbber->extent.x1 == x1 &&
197 throbber->extent.y1 == y1)
198 return true;
199
200 /* Redraw the relevant bits of the toolbar. */
201
202 if (throbber->window != NULL && throbber->icon != -1) {
203 xwimp_force_redraw(throbber->window,
205 throbber->extent.x1, throbber->extent.y1);
206 xwimp_force_redraw(throbber->window, x0, y0, x1, y1);
207 }
208
209 /* Update the throbber position */
210
211 throbber->extent.x0 = x0;
212 throbber->extent.y0 = y0;
213 throbber->extent.x1 = x1;
214 throbber->extent.y1 = y1;
215
217}
218
219
220/**
221 * Create or delete a throbber's icon if required to bring it into sync with
222 * the current hidden setting.
223 *
224 * \param *throbber The throbber to update.
225 * \return true if successful; else false.
226 */
227
229{
230 wimp_icon_create icon;
231 os_error *error;
232
233 if (throbber == NULL || throbber->window == NULL)
234 return false;
235
236 if (!throbber->hidden && throbber->icon == -1) {
237 icon.w = throbber->window;
238 icon.icon.extent.x0 = throbber->extent.x0;
239 icon.icon.extent.y0 = throbber->extent.y0;
240 icon.icon.extent.x1 = throbber->extent.x1;
241 icon.icon.extent.y1 = throbber->extent.y1;
242 icon.icon.flags = wimp_ICON_SPRITE | wimp_ICON_INDIRECTED |
243 wimp_ICON_HCENTRED | wimp_ICON_VCENTRED;
244 icon.icon.data.indirected_sprite.id =
245 (osspriteop_id) throbber->sprite_name;
246 icon.icon.data.indirected_sprite.area = throbber->sprites;
247 icon.icon.data.indirected_sprite.size =
249
250 error = xwimp_create_icon(&icon, &throbber->icon);
251 if (error != NULL) {
252 NSLOG(netsurf, INFO, "xwimp_create_icon: 0x%x: %s",
253 error->errnum, error->errmess);
254 ro_warn_user("WimpError", error->errmess);
255 throbber->icon = -1;
256 return false;
257 }
258
260 return false;
261 } else if (throbber->hidden && throbber->icon != -1) {
262 error = xwimp_delete_icon(throbber->window, throbber->icon);
263 if (error != NULL) {
264 NSLOG(netsurf, INFO, "xwimp_delete_icon: 0x%x: %s",
265 error->errnum, error->errmess);
266 ro_warn_user("WimpError", error->errmess);
267 return false;
268 }
269
270 throbber->icon = -1;
271 }
272
273 if (throbber->icon != -1)
276
277 return true;
278}
279
280
281/**
282 * Position the icons in the throbber to take account of the currently
283 * configured extent.
284 *
285 * \param *throbber The throbber to update.
286 * \return true if successful; else false.
287 */
288
290{
291
292 if (throbber->window == NULL)
293 return false;
294
295 if (throbber->icon != -1) {
296 os_error *error;
297 error = xwimp_resize_icon(throbber->window, throbber->icon,
299 throbber->extent.x1, throbber->extent.y1);
300 if (error != NULL) {
301 NSLOG(netsurf, INFO, "xwimp_resize_icon: 0x%x: %s",
302 error->errnum, error->errmess);
303 ro_warn_user("WimpError", error->errmess);
304 throbber->icon = -1;
305 return false;
306 }
307 }
308
309 return true;
310}
311
312
313/* This is an exported interface documented in throbber.h */
314
315bool ro_gui_throbber_hide(struct throbber *throbber, bool hide)
316{
317 if (throbber == NULL || throbber->hidden == hide)
318 return (throbber == NULL) ? false : true;
319
320 throbber->hidden = hide;
321
323}
324
325
326/* This is an exported interface documented in throbber.h */
327
329 os_coord *mouse, wimp_window_state *state,
330 wimp_mouse_state buttons, const char **suffix)
331{
332 os_coord pos;
333
334 if (throbber == NULL || throbber->hidden)
335 return false;
336
337 /* Check that the click was within our part of the window. */
338
339 pos.x = mouse->x - state->visible.x0 + state->xscroll;
340 pos.y = mouse->y - state->visible.y1 + state->yscroll;
341
342 if (pos.x < throbber->extent.x0 || pos.x > throbber->extent.x1 ||
343 pos.y < throbber->extent.y0 ||
344 pos.y > throbber->extent.y1)
345 return false;
346
347 /* Return a hard-coded icon number that matches the one that was
348 * always allocated to the throbber in a previous implementation.
349 * If Messages can be updated, this could be changed.
350 */
351
352 if (i == throbber->icon)
353 *suffix = "16";
354 else
355 *suffix = "";
356
357 return true;
358}
359
360
361/* This is an exported interface documented in throbber.h */
362
364{
365 os_t t;
367
368 if (throbber == NULL || throbber->hidden)
369 return (throbber == NULL) ? false : true;
370
371 xos_read_monotonic_time(&t);
372
373 /* Drop out if we're not ready for the next frame, unless this
374 * call is to start animation from a stopped throbber (ie. if
375 * the current frame is 0).
376 */
377
379 (throbber->current_frame > 0))
380 return true;
381
384
387
389 "throbber%i",
390 min(max(throbber->current_frame, 0), 999));
392 sprite_name, true);
393
396
397 return true;
398}
399
400
401/* This is an exported interface documented in throbber.h */
402
404{
406
407 if (throbber == NULL || throbber->hidden ||
409 return (throbber == FALSE) ? false : true;
410
413
414 strcpy(sprite_name, "throbber0");
416 sprite_name, true);
417
420
421 return true;
422}
423
static struct BitMap * throbber
Definition: theme.c:55
Window themes(interface).
theme_style
Theme styles, collecting groups of attributes for different locations.
Definition: theme.h:31
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
bool ro_gui_throbber_stop(struct throbber *throbber)
Stop the amimation of a throbber.
Definition: throbber.c:403
static bool ro_gui_throbber_icon_update(struct throbber *throbber)
Create or delete a throbber's icon if required to bring it into sync with the current hidden setting.
Definition: throbber.c:228
bool ro_gui_throbber_set_extent(struct throbber *throbber, int x0, int y0, int x1, int y1)
Set or update the dimensions to be used by the throbber in RO units.
Definition: throbber.c:186
#define THROBBER_SPRITE_NAME_LENGTH
Definition: throbber.c:42
void ro_gui_throbber_destroy(struct throbber *throbber)
Destroy a throbber widget.
Definition: throbber.c:154
bool ro_gui_throbber_hide(struct throbber *throbber, bool hide)
Show or hide a throbber.
Definition: throbber.c:315
#define THROBBER_ANIMATE_INTERVAL
Definition: throbber.c:43
bool ro_gui_throbber_animate(struct throbber *throbber)
Start or update the amimation of a throbber.
Definition: throbber.c:363
bool ro_gui_throbber_get_dims(struct throbber *throbber, int *width, int *height)
Return the MINIMUM dimensions required by the throbber, in RO units, allowing for the current theme.
Definition: throbber.c:165
static bool ro_gui_throbber_icon_resize(struct throbber *throbber)
Position the icons in the throbber to take account of the currently configured extent.
Definition: throbber.c:289
bool ro_gui_throbber_rebuild(struct throbber *throbber, struct theme_descriptor *theme, theme_style style, wimp_w window, bool shaded)
Place a throbber into a toolbar window and initialise any theme-specific settings.
Definition: throbber.c:124
struct throbber * ro_gui_throbber_create(struct theme_descriptor *theme)
Create a new throbber widget.
Definition: throbber.c:77
bool ro_gui_throbber_help_suffix(struct throbber *throbber, wimp_i i, os_coord *mouse, wimp_window_state *state, wimp_mouse_state buttons, const char **suffix)
Translate mouse data into an interactive help message for the throbber.
Definition: throbber.c:328
Throbber (interface).
int width
Definition: gui.c:166
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2217
int height
Definition: gui.c:167
osspriteop_area * ro_gui_theme_get_sprites(struct theme_descriptor *descriptor)
Returns a sprite area for use with the given theme.
Definition: theme.c:221
bool ro_gui_theme_get_throbber_data(struct theme_descriptor *descriptor, int *frames, int *width, int *height, bool *right, bool *redraw)
Returns details of the throbber as defined in a theme.
Definition: theme.c:325
Interface to utility string handling.
Definition: theme.h:64
char sprite_name[THROBBER_SPRITE_NAME_LENGTH]
Definition: throbber.c:64
int y_min
Definition: throbber.c:50
bool shaded
Definition: throbber.c:58
osspriteop_area * sprites
Definition: throbber.c:56
wimp_w window
The window and icon details.
Definition: throbber.c:53
struct theme_descriptor * theme
The applied theme (or NULL to use the default)
Definition: throbber.c:47
os_t last_update
Definition: throbber.c:63
int current_frame
Definition: throbber.c:62
bool hidden
Definition: throbber.c:57
wimp_i icon
Definition: throbber.c:54
int max_frame
The animation details.
Definition: throbber.c:61
os_box extent
Definition: throbber.c:55
int x_min
The widget dimensions.
Definition: throbber.c:50
bool force_redraw
Definition: throbber.c:65
Interface to a number of general purpose functionality.
#define min(x, y)
Definition: utils.h:46
#define max(x, y)
Definition: utils.h:50
void ro_gui_set_icon_string(wimp_w w, wimp_i i, const char *text, bool is_utf8)
Set the contents of a text or sprite icon to a string.
Definition: wimp.c:269
void ro_gui_force_redraw_icon(wimp_w w, wimp_i i)
Forces an icon to be redrawn entirely (ie not just updated).
Definition: wimp.c:193
void ro_gui_set_icon_shaded_state(wimp_w w, wimp_i i, bool state)
Set the shaded state of an icon.
Definition: wimp.c:487
General RISC OS WIMP/OS library functions (interface).