summaryrefslogtreecommitdiff
path: root/frontends/riscos/gui/throbber.c
blob: f3b79a68e56b5171910a238cf0fe1ff5ae3c592b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * Copyright 2004, 2005 Richard Wilson <info@tinct.net>
 * Copyright 2011 Stephen Fryatt <stevef@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Throbber (implementation).
 */

#include <alloca.h>
#include <assert.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "oslib/os.h"
#include "oslib/osspriteop.h"
#include "oslib/wimp.h"

#include "utils/log.h"
#include "riscos/gui.h"

#include "riscos/gui/throbber.h"
#include "riscos/theme.h"
#include "riscos/wimp.h"

#define THROBBER_SPRITE_NAME_LENGTH 12
#define THROBBER_ANIMATE_INTERVAL 10

struct throbber {
	/** The applied theme (or NULL to use the default) */
	struct theme_descriptor	*theme;

	/** The widget dimensions. */
	int			x_min, y_min;

	/** The window and icon details. */
	wimp_w			window;
	wimp_i			icon;
	os_box			extent;
	osspriteop_area		*sprites;
	bool			hidden;
	bool			shaded;

	/** The animation details. */
	int			max_frame;
	int			current_frame;
	os_t			last_update;
	char			sprite_name[THROBBER_SPRITE_NAME_LENGTH];
	bool			force_redraw;
};

/*
 * Private function prototypes.
 */

static bool ro_gui_throbber_icon_update(struct throbber *throbber);
static bool ro_gui_throbber_icon_resize(struct throbber *throbber);

/* This is an exported interface documented in throbber.h */

struct throbber *ro_gui_throbber_create(struct theme_descriptor *theme)
{
	struct throbber		*throbber;

	/* Allocate memory. */

	throbber = malloc(sizeof(struct throbber));
	if (throbber == NULL) {
		NSLOG(netsurf, INFO, "No memory for malloc()");
		return NULL;
	}

	/* Set up default parameters. If reading the throbber theme data
	 * fails, we give up and return a failure.
	 */

	if (!ro_gui_theme_get_throbber_data(theme, &throbber->max_frame,
			&throbber->x_min, &throbber->y_min, NULL,
			&throbber->force_redraw)) {
		free(throbber);
		return NULL;
	}

	throbber->sprites = ro_gui_theme_get_sprites(theme);

	throbber->theme = theme;

	throbber->extent.x0 = 0;
	throbber->extent.y0 = 0;
	throbber->extent.x1 = 0;
	throbber->extent.y1 = 0;

	throbber->current_frame = 0;
	throbber->last_update = 0;

	throbber->window = NULL;
	throbber->icon = -1;

	throbber->hidden = false;
	throbber->shaded = false;

	return throbber;
}


/* This is an exported interface documented in throbber.h */

bool ro_gui_throbber_rebuild(struct throbber *throbber,
		struct theme_descriptor *theme, theme_style style,
		wimp_w window, bool shaded)
{
	if (throbber == NULL)
		return false;

	throbber->theme = theme;
	throbber->window = window;
	throbber->sprites = ro_gui_theme_get_sprites(theme);

	throbber->icon = -1;

	throbber->shaded = shaded;

	strcpy(throbber->sprite_name, "throbber0");

	if (!ro_gui_theme_get_throbber_data(theme, &throbber->max_frame,
			&throbber->x_min, &throbber->y_min, NULL,
			&throbber->force_redraw)) {
		free(throbber);
		return false;
	}

	return ro_gui_throbber_icon_update(throbber);
}


/* This is an exported interface documented in throbber.h */

void ro_gui_throbber_destroy(struct throbber *throbber)
{
	if (throbber == NULL)
		return;

	free(throbber);
}


/* This is an exported interface documented in throbber.h */

bool ro_gui_throbber_get_dims(struct throbber *throbber,
		int *width, int *height)
{
	if (throbber == NULL)
		return false;

	if (throbber->x_min != -1 && throbber->y_min != -1) {
		if (width != NULL)
			*width = throbber->x_min;
		if (height != NULL)
			*height = throbber->y_min;

		return true;
	}

	return false;
}


/* This is an exported interface documented in throbber.h */

bool ro_gui_throbber_set_extent(struct throbber *throbber,
		int x0, int y0, int x1, int y1)
{
	if (throbber == NULL)
		return false;

	if ((x1 - x0) < throbber->x_min || (y1 - y0) < throbber->y_min)
		return false;

	if (throbber->extent.x0 == x0 && throbber->extent.y0 == y0 &&
			throbber->extent.x1 == x1 &&
			throbber->extent.y1 == y1)
		return true;

	/* Redraw the relevant bits of the toolbar. */

	if (throbber->window != NULL && throbber->icon != -1) {
		xwimp_force_redraw(throbber->window,
				throbber->extent.x0, throbber->extent.y0,
				throbber->extent.x1, throbber->extent.y1);
		xwimp_force_redraw(throbber->window, x0, y0, x1, y1);
	}

	/* Update the throbber position */

	throbber->extent.x0 = x0;
	throbber->extent.y0 = y0;
	throbber->extent.x1 = x1;
	throbber->extent.y1 = y1;

	return ro_gui_throbber_icon_resize(throbber);
}


/**
 * Create or delete a throbber's icon if required to bring it into sync with
 * the current hidden setting.
 *
 * \param *throbber		The throbber to update.
 * \return			true if successful; else false.
 */

bool ro_gui_throbber_icon_update(struct throbber *throbber)
{
	wimp_icon_create	icon;
	os_error		*error;

	if (throbber == NULL || throbber->window == NULL)
		return false;

	if (!throbber->hidden && throbber->icon == -1) {
		icon.w = throbber->window;
		icon.icon.extent.x0 = throbber->extent.x0;
		icon.icon.extent.y0 = throbber->extent.y0;
		icon.icon.extent.x1 = throbber->extent.x1;
		icon.icon.extent.y1 = throbber->extent.y1;
		icon.icon.flags = wimp_ICON_SPRITE | wimp_ICON_INDIRECTED |
				wimp_ICON_HCENTRED | wimp_ICON_VCENTRED;
		icon.icon.data.indirected_sprite.id =
				(osspriteop_id) throbber->sprite_name;
		icon.icon.data.indirected_sprite.area = throbber->sprites;
		icon.icon.data.indirected_sprite.size =
				THROBBER_SPRITE_NAME_LENGTH;

		error = xwimp_create_icon(&icon, &throbber->icon);
		if (error != NULL) {
			NSLOG(netsurf, INFO, "xwimp_create_icon: 0x%x: %s",
			      error->errnum, error->errmess);
			ro_warn_user("WimpError", error->errmess);
			throbber->icon = -1;
			return false;
		}

		if (!ro_gui_throbber_icon_resize(throbber))
			return false;
	} else if (throbber->hidden && throbber->icon != -1) {
		error = xwimp_delete_icon(throbber->window, throbber->icon);
		if (error != NULL) {
			NSLOG(netsurf, INFO, "xwimp_delete_icon: 0x%x: %s",
			      error->errnum, error->errmess);
			ro_warn_user("WimpError", error->errmess);
			return false;
		}

		throbber->icon = -1;
	}

	if (throbber->icon != -1)
		ro_gui_set_icon_shaded_state(throbber->window,
				throbber->icon, throbber->shaded);

	return true;
}


/**
 * Position the icons in the throbber to take account of the currently
 * configured extent.
 *
 * \param *throbber		The throbber to update.
 * \return			true if successful; else false.
 */

bool ro_gui_throbber_icon_resize(struct throbber *throbber)
{

	if (throbber->window == NULL)
		return false;

	if (throbber->icon != -1) {
		os_error *error;
		error = xwimp_resize_icon(throbber->window, throbber->icon,
				throbber->extent.x0, throbber->extent.y0,
				throbber->extent.x1, throbber->extent.y1);
		if (error != NULL) {
			NSLOG(netsurf, INFO, "xwimp_resize_icon: 0x%x: %s",
			      error->errnum, error->errmess);
			ro_warn_user("WimpError", error->errmess);
			throbber->icon = -1;
			return false;
		}
	}

	return true;
}


/* This is an exported interface documented in throbber.h */

bool ro_gui_throbber_hide(struct throbber *throbber, bool hide)
{
	if (throbber == NULL || throbber->hidden == hide)
		return (throbber == NULL) ? false : true;

	throbber->hidden = hide;

	return ro_gui_throbber_icon_update(throbber);
}


/* This is an exported interface documented in throbber.h */

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)
{
	os_coord			pos;

	if (throbber == NULL || throbber->hidden)
		return false;

	/* Check that the click was within our part of the window. */

	pos.x = mouse->x - state->visible.x0 + state->xscroll;
	pos.y = mouse->y - state->visible.y1 + state->yscroll;

	if (pos.x < throbber->extent.x0 || pos.x > throbber->extent.x1 ||
			pos.y < throbber->extent.y0 ||
			pos.y > throbber->extent.y1)
		return false;

	/* Return a hard-coded icon number that matches the one that was
	 * always allocated to the throbber in a previous implementation.
	 * If Messages can be updated, this could be changed.
	 */

	if (i == throbber->icon)
		*suffix = "16";
	else
		*suffix = "";

	return true;
}


/* This is an exported interface documented in throbber.h */

bool ro_gui_throbber_animate(struct throbber *throbber)
{
	os_t	t;
	char	sprite_name[THROBBER_SPRITE_NAME_LENGTH];

	if (throbber == NULL || throbber->hidden)
		return (throbber == NULL) ? false : true;

	xos_read_monotonic_time(&t);

	/* Drop out if we're not ready for the next frame, unless this
	 * call is to start animation from a stopped throbber (ie. if
	 * the current frame is 0).
	 */

	if ((t < (throbber->last_update + THROBBER_ANIMATE_INTERVAL)) &&
			(throbber->current_frame > 0))
		return true;

	throbber->last_update = t;
	throbber->current_frame++;

	if (throbber->current_frame > throbber->max_frame)
		throbber->current_frame = 1;

	snprintf(sprite_name, THROBBER_SPRITE_NAME_LENGTH,
			"throbber%i", throbber->current_frame);
	ro_gui_set_icon_string(throbber->window, throbber->icon,
			sprite_name, true);

	if (throbber->force_redraw)
		ro_gui_force_redraw_icon(throbber->window, throbber->icon);

	return true;
}


/* This is an exported interface documented in throbber.h */

bool ro_gui_throbber_stop(struct throbber *throbber)
{
	char	sprite_name[THROBBER_SPRITE_NAME_LENGTH];

	if (throbber == NULL || throbber->hidden ||
			throbber->current_frame == 0)
		return (throbber == FALSE) ? false : true;

	throbber->current_frame = 0;
	throbber->last_update = 0;

	strcpy(sprite_name, "throbber0");
	ro_gui_set_icon_string(throbber->window, throbber->icon,
			sprite_name, true);

	if (throbber->force_redraw)
		ro_gui_force_redraw_icon(throbber->window, throbber->icon);

	return true;
}