summaryrefslogtreecommitdiff
path: root/riscos/theme.c
blob: f20705e691584bb1d9ede33e0243b6be049426c5 (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
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 * Copyright 2003 James Bursa <bursa@users.sourceforge.net>
 */

/** \file
 * Toolbar themes (implementation).
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "oslib/osfile.h"
#include "oslib/osspriteop.h"
#include "oslib/wimp.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/theme.h"
#include "netsurf/utils/utils.h"

static wimp_window *theme_toolbar_template = 0;
static osspriteop_area *theme_sprite_area = 0;
unsigned int theme_throbs;


/**
 * Load a theme from a directory.
 *
 * The directory must contain a Templates file containing the toolbar template,
 * and a Sprites file containing icons.
 */

void ro_theme_load(char *pathname)
{
	char name[] = "toolbar";
	int context, window_size, data_size, size, i;
	static char *data = 0;
	/**
	 * \note
	 * This is necessary as, when compiling with Norcroft 5.54,
	 * linking fails due to it trying to use
	 * __rt_allocauto and __rt_freeauto to allocate (and free)
	 * the stack space used by the filename buffer.
	 * These symbols are provided by the SCL but not by Unixlib
	 *
	 * \note
	 * There are three possible ways around this \#ifdef nastiness:
	 *  - Allocate filename on the heap instead
	 *  - Get NetSurf to build and link against the SCL
	 *  - Implement __rt_allocauto and __rt_freeauto for Unixlib
	 *
	 */
#ifdef __GNUC__
	char filename[strlen(pathname) + 12];
#else
        char *filename = xcalloc(strlen(pathname) + 12, sizeof(char));
#endif
	fileswitch_object_type obj_type;

	/* free old theme data */
	free(theme_toolbar_template);
	free(data);
	free(theme_sprite_area);

	/* load template */
	sprintf(filename, "%s.Templates", pathname);
	wimp_open_template(filename);

	/* find required buffer sizes */
	context = wimp_load_template(wimp_GET_SIZE, 0, 0, wimp_NO_FONTS,
			name, 0, &window_size, &data_size);
	assert(context != 0);

	theme_toolbar_template = xcalloc((unsigned int) window_size, 1);
	data = xcalloc((unsigned int) data_size, 1);

	/* load */
	wimp_load_template(theme_toolbar_template, data, data + data_size,
			wimp_NO_FONTS, name, 0, 0, 0);

	wimp_close_template();

	assert(ICON_TOOLBAR_RELOAD < theme_toolbar_template->icon_count);
	theme_toolbar_template->flags |= wimp_WINDOW_FURNITURE_WINDOW;
	theme_toolbar_template->icons[ICON_TOOLBAR_URL].data.indirected_text.size = 256;
	theme_toolbar_template->icons[ICON_TOOLBAR_STATUS].data.indirected_text.size = 256;

	/* load sprites */
	sprintf(filename, "%s.Sprites", pathname);
	obj_type = osfile_read_no_path(filename, 0, 0, &size, 0);
	assert(obj_type & fileswitch_IS_FILE);

	theme_sprite_area = xcalloc((unsigned int)(size + 16), 1);
	theme_sprite_area->size = size + 16;
	theme_sprite_area->sprite_count = 0;
	theme_sprite_area->first = 16;
	theme_sprite_area->used = 16;
	osspriteop_clear_sprites(osspriteop_USER_AREA, theme_sprite_area);
	osspriteop_load_sprite_file(osspriteop_USER_AREA, theme_sprite_area,
			filename);

	theme_toolbar_template->sprite_area = theme_sprite_area;
	theme_toolbar_template->icons[ICON_TOOLBAR_THROBBER].data.indirected_sprite.area =
			theme_sprite_area;

	/* find the highest sprite called throbber%i */
	theme_throbs = 0;
	for (i = 1; i <= theme_sprite_area->sprite_count; i++) {
		char name[32];
		osspriteop_return_name(osspriteop_USER_AREA,
				theme_sprite_area, name, 32, i);
		if (strncmp(name, "throbber", 8) == 0) {
			unsigned int n = atoi(name + 8);
			if (theme_throbs < n)
				theme_throbs = n;
		}
	}
#ifndef __GNUC__
        xfree(filename);
#endif
}


/**
 * Create a toolbar from the current theme.
 *
 * The buffers url_buffer and status_buffer must be at least 256 bytes each,
 * throbber_buffer at least 12 bytes;
 */

wimp_w ro_theme_create_toolbar(char *url_buffer, char *status_buffer,
		char *throbber_buffer)
{
	wimp_w w;

	theme_toolbar_template->icons[ICON_TOOLBAR_URL].data.indirected_text.text = url_buffer;
	theme_toolbar_template->icons[ICON_TOOLBAR_STATUS].data.indirected_text.text = status_buffer;
	theme_toolbar_template->icons[ICON_TOOLBAR_THROBBER].data.indirected_sprite.id =
			(osspriteop_id) throbber_buffer;

	w = wimp_create_window(theme_toolbar_template);
	return w;
}


/**
 * Return the height of the current toolbar.
 */

int ro_theme_toolbar_height(void)
{
	return abs(theme_toolbar_template->extent.y1 - theme_toolbar_template->extent.y0) + 2;
}


/**
 * Resize the URL icon in a toolbar.
 */

void ro_theme_resize_toolbar(wimp_w w, int width, int height)
{
	wimp_icon_state ic;
	ic.w = w;
	ic.i = ICON_TOOLBAR_URL;
	wimp_get_icon_state(&ic);

	wimp_resize_icon(w, ICON_TOOLBAR_URL, ic.icon.extent.x0, ic.icon.extent.y0,
			width - 8, ic.icon.extent.y1);
	wimp_force_redraw(w, ic.icon.extent.x0, ic.icon.extent.y0,
			width, ic.icon.extent.y1);
}