NetSurf
configure.c
Go to the documentation of this file.
1/*
2 * Copyright 2005 Richard Wilson <info@tinct.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/**
20 * \file
21 * RISC OS option setting (implementation).
22 */
23
24#include <assert.h>
25#include <stdbool.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <oslib/os.h>
30#include <oslib/osbyte.h>
31#include <oslib/territory.h>
32#include <oslib/wimp.h>
33
34#include "utils/log.h"
35#include "utils/messages.h"
36
37#include "riscos/gui.h"
38#include "riscos/dialog.h"
39#include "riscos/configure.h"
40#include "riscos/wimp.h"
41#include "riscos/wimp_event.h"
43
44#define CONFIGURE_ICON_PADDING_H 32
45#define CONFIGURE_ICON_PADDING_V 32
46#define CONFIGURE_DEFAULT_ICON_WIDTH (68 + CONFIGURE_ICON_PADDING_H)
47#define CONFIGURE_DEFAULT_ICON_HEIGHT (128 + CONFIGURE_ICON_PADDING_V)
48
50 const char *name;
51#define CONFIGURE_TOOL_TRANSLATED_SIZE 64
54 bool (*initialise)(wimp_w w);
55 void (*finalise)(wimp_w w);
56 wimp_w w;
57 wimp_i i;
58 bool open;
60};
61
62static wimp_w configure_window;
64static int configure_icons = 0;
65static struct configure_tool *configure_tools = NULL;
69static int configure_width;
71
72static bool ro_gui_configure_click(wimp_pointer *pointer);
73static void ro_gui_configure_open_window(wimp_open *open);
74static void ro_gui_configure_close(wimp_w w);
75static bool ro_gui_configure_translate(void);
76static void ro_gui_configure_register(const char *window,
77 bool (*initialise)(wimp_w w), void (*finalise)(wimp_w w));
78
80{
81 /* create our window */
88
89 /* add in our option windows */
90 ro_gui_configure_register("con_cache",
93 ro_gui_configure_register("con_connect",
96 ro_gui_configure_register("con_content",
99 ro_gui_configure_register("con_fonts",
102 ro_gui_configure_register("con_home",
105 ro_gui_configure_register("con_image",
108 ro_gui_configure_register("con_inter",
111 ro_gui_configure_register("con_lang",
114 ro_gui_configure_register("con_theme",
117 ro_gui_configure_register("con_secure",
120 ro_gui_configure_register("con_search",
123
124 /* translate the icons */
126 die("ro_gui_configure_translate failed");
127}
128
130{
131 int width, height;
132
136}
137
138bool ro_gui_configure_click(wimp_pointer *pointer)
139{
140 struct configure_tool *tool;
141
142 if (pointer->buttons == wimp_CLICK_MENU)
143 return true;
144
145 for (tool = configure_tools; tool; tool = tool->next) {
146 if (tool->i == pointer->i) {
147 if (!tool->open) {
148 tool->open = true;
149 if (!tool->initialise(tool->w))
150 return false;
153 tool->w, true);
155 tool->w,
157 } else {
158 ro_gui_dialog_open_top(tool->w, NULL, 0, 0);
159 }
160 break;
161 }
162 }
163 return true;
164}
165
167{
168 struct configure_tool *tool;
169
170 for (tool = configure_tools; tool; tool = tool->next) {
171 if (tool->w == w) {
172 tool->open = false;
173 if (tool->finalise)
174 tool->finalise(w);
175 break;
176 }
177 }
178}
179
181{
182 os_error *error;
183 int screen_width, screen_height;
184 int height, width;
185 int icons_per_line, icon_lines;
186 int max_height;
187 os_box extent = { 0, 0, 0, 0 };
188 struct configure_tool *tool;
189
191 ro_warn_user("ro_gui_configure_translate failed", 0);
192 return;
193 }
194
195 width = open->visible.x1 - open->visible.x0;
196 height = open->visible.y1 - open->visible.y0;
197 icons_per_line = width / configure_icon_width;
198 if (icons_per_line < 1)
199 icons_per_line = 1;
200
201 /* move our icons */
202 if (icons_per_line != configure_icons_per_line) {
203 int x, y, l;
204 configure_icons_per_line = icons_per_line;
207 l = 0;
208 for (tool = configure_tools; tool; tool = tool->next) {
209 error = xwimp_resize_icon(configure_window,
210 tool->i,
211 x,
212 y,
217 if (error) {
218 NSLOG(netsurf, INFO,
219 "xwimp_resize_icon: 0x%x: %s",
220 error->errnum,
221 error->errmess);
222 }
224 l++;
225 if (l >= icons_per_line) {
227 l = 0;
229 }
230 }
231 error = xwimp_force_redraw(configure_window,
232 0, -16384, 16384, 0);
233 if (error) {
234 NSLOG(netsurf, INFO, "xwimp_force_redraw: 0x%x: %s",
235 error->errnum, error->errmess);
236 ro_warn_user("WimpError", error->errmess);
237 }
238 }
239
240 /* restrict our height */
241 icon_lines = (configure_icons + icons_per_line - 1) /
242 icons_per_line;
243 max_height = (icon_lines * configure_icon_height);
244 if (height > max_height)
245 open->visible.y0 = open->visible.y1 - max_height;
246
247 /* set the extent */
248 if ((configure_height != height) || (configure_width != width)) {
249 int max_icons_per_line;
250 ro_gui_screen_size(&screen_width, &screen_height);
251 max_icons_per_line = screen_width / configure_icon_width;
252 if (max_icons_per_line > configure_icons)
253 max_icons_per_line = configure_icons;
254 extent.x1 = configure_icon_width * max_icons_per_line;
255 extent.y0 = -max_height;
256 error = xwimp_set_extent(open->w, &extent);
257 if (error) {
258 NSLOG(netsurf, INFO, "xwimp_set_extent: 0x%x: %s",
259 error->errnum, error->errmess);
260 ro_warn_user("WimpError", error->errmess);
261 return;
262 }
265 }
266
267 /* open the window */
268 error = xwimp_open_window(open);
269 if (error) {
270 NSLOG(netsurf, INFO, "xwimp_open_window: 0x%x: %s",
271 error->errnum, error->errmess);
272 ro_warn_user("WimpError", error->errmess);
273 return;
274 }
275}
276
277void ro_gui_configure_register(const char *window,
278 bool (*initialise)(wimp_w w), void (*finalise)(wimp_w w))
279{
280 wimp_icon_create new_icon;
281 struct configure_tool *tool;
282 struct configure_tool *link;
283 os_error *error;
284
285 /* create our tool */
286 tool = calloc(sizeof(struct configure_tool), 1);
287 if (!tool) {
288 NSLOG(netsurf, INFO, "Insufficient memory for calloc()");
289 die("Insufficient memory");
290 return; /* For the benefit of scan-build */
291 }
292 tool->name = window;
293 tool->translated[0] = '\0';
294 tool->validation = malloc(strlen(window) + 2);
295 if (!tool->validation) {
296 NSLOG(netsurf, INFO, "Insufficient memory for malloc()");
297 die("Insufficient memory");
298 }
299 sprintf(tool->validation, "S%s", window);
300 tool->initialise = initialise;
301 tool->finalise = finalise;
302 tool->w = ro_gui_dialog_create(tool->name);
303
304 /* create the icon */
305 new_icon.w = configure_window;
306 new_icon.icon.extent.x0 = 0;
307 new_icon.icon.extent.x1 = configure_icon_width;
308 new_icon.icon.extent.y1 = 0;
309 new_icon.icon.extent.y0 = -configure_icon_height;
310 new_icon.icon.flags = wimp_ICON_TEXT | wimp_ICON_SPRITE |
311 wimp_ICON_INDIRECTED | wimp_ICON_HCENTRED |
312 (wimp_COLOUR_VERY_LIGHT_GREY << wimp_ICON_BG_COLOUR_SHIFT) |
313 (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
314 (wimp_BUTTON_CLICK << wimp_ICON_BUTTON_TYPE_SHIFT);
315 new_icon.icon.data.indirected_text_and_sprite.text =
316 tool->translated;
317 new_icon.icon.data.indirected_text_and_sprite.validation =
318 tool->validation;
319 new_icon.icon.data.indirected_text_and_sprite.size =
321 error = xwimp_create_icon(&new_icon, &tool->i);
322 if (error) {
323 NSLOG(netsurf, INFO, "xwimp_create_icon: 0x%x: %s",
324 error->errnum, error->errmess);
325 die(error->errmess);
326 }
327
328 /* Set the icon's text in current local encoding */
330 messages_get(tool->name), true);
331
332 /* link into our list alphabetically */
333 if ((!configure_tools) ||
335 tool->translated) > 0)) {
336 tool->next = configure_tools;
337 configure_tools = tool;
338 } else {
339 for (link = configure_tools; link; link = link->next) {
340 if (link->next) {
341 if (strcmp(link->next->translated,
342 tool->translated) > 0) {
343 tool->next = link->next;
344 link->next = tool;
345 break;
346 }
347 } else {
348 link->next = tool;
349 break;
350 }
351 }
352 }
354}
355
356/**
357 * Translate tool icons into the system local encoding.
358 * This will also recalculate the minimum required icon width.
359 *
360 * \return true on success, false on memory exhaustion
361 */
363{
364 int alphabet;
365 struct configure_tool *tool;
366 int icon_width;
367 os_error *error;
368
369 /* read current alphabet */
370 error = xosbyte1(osbyte_ALPHABET_NUMBER, 127, 0,
371 &alphabet);
372 if (error) {
373 NSLOG(netsurf, INFO, "failed reading alphabet: 0x%x: %s",
374 error->errnum, error->errmess);
375 /* assume Latin1 */
376 alphabet = territory_ALPHABET_LATIN1;
377 }
378
379 if (alphabet == configure_current_encoding)
380 /* text is already in the correct encoding */
381 return true;
382
383 /* reset icon width */
385
386 for (tool = configure_tools; tool; tool = tool->next) {
387 /* re-translate the text */
389 messages_get(tool->name), true);
390
391 /* update the width */
392 error = xwimptextop_string_width(tool->translated,
393 strlen(tool->translated), &icon_width);
394 if (error) {
395 NSLOG(netsurf, INFO,
396 "xwimptextop_string_width: 0x%x: %s",
397 error->errnum,
398 error->errmess);
399 return false;
400 }
401 icon_width += CONFIGURE_ICON_PADDING_H;
402 if (icon_width > configure_icon_width)
403 configure_icon_width = icon_width;
404
405 error = xwimp_resize_icon(configure_window,
406 tool->i,
407 0,
410 0);
411 if (error) {
412 NSLOG(netsurf, INFO, "xwimp_resize_icon: 0x%x: %s",
413 error->errnum, error->errmess);
414 }
415 }
416
417 /* invalidate our global icons_per_line setting
418 * so the icons get reflowed */
420
421 /* finally, set the current encoding */
423
424 return true;
425}
void die(const char *error)
Cause an abnormal program termination.
Definition: misc.c:69
bool ro_gui_options_cache_initialise(wimp_w w)
Definition: con_cache.c:50
bool ro_gui_options_connection_initialise(wimp_w w)
Definition: con_connect.c:66
bool ro_gui_options_content_initialise(wimp_w w)
Definition: con_content.c:43
bool ro_gui_options_fonts_initialise(wimp_w w)
Definition: con_fonts.c:75
bool ro_gui_options_home_initialise(wimp_w w)
Definition: con_home.c:46
void ro_gui_options_image_finalise(wimp_w w)
Definition: con_image.c:112
bool ro_gui_options_image_initialise(wimp_w w)
Definition: con_image.c:61
bool ro_gui_options_interface_initialise(wimp_w w)
Definition: con_inter.c:46
bool ro_gui_options_language_initialise(wimp_w w)
Definition: con_language.c:47
bool ro_gui_options_search_initialise(wimp_w w)
Definition: con_search.c:95
bool ro_gui_options_security_initialise(wimp_w w)
Definition: con_secure.c:42
bool ro_gui_options_theme_initialise(wimp_w w)
Definition: con_theme.c:94
void ro_gui_options_theme_finalise(wimp_w w)
Definition: con_theme.c:176
Automated RISC OS WIMP event handling (interface).
static int configure_width
Definition: configure.c:69
#define CONFIGURE_DEFAULT_ICON_WIDTH
Definition: configure.c:46
#define CONFIGURE_TOOL_TRANSLATED_SIZE
Definition: configure.c:51
static bool ro_gui_configure_translate(void)
Translate tool icons into the system local encoding.
Definition: configure.c:362
static int configure_current_encoding
Definition: configure.c:63
void ro_gui_configure_initialise(void)
Definition: configure.c:79
static bool ro_gui_configure_click(wimp_pointer *pointer)
Definition: configure.c:138
#define CONFIGURE_DEFAULT_ICON_HEIGHT
Definition: configure.c:47
static void ro_gui_configure_close(wimp_w w)
Definition: configure.c:166
static int configure_icon_height
Definition: configure.c:67
static int configure_icon_width
Definition: configure.c:66
static int configure_height
Definition: configure.c:70
static struct configure_tool * configure_tools
Definition: configure.c:65
static wimp_w configure_window
Definition: configure.c:62
#define CONFIGURE_ICON_PADDING_V
Definition: configure.c:45
static void ro_gui_configure_register(const char *window, bool(*initialise)(wimp_w w), void(*finalise)(wimp_w w))
Definition: configure.c:277
#define CONFIGURE_ICON_PADDING_H
Definition: configure.c:44
static void ro_gui_configure_open_window(wimp_open *open)
Definition: configure.c:180
void ro_gui_configure_show(void)
Definition: configure.c:129
static int configure_icons_per_line
Definition: configure.c:68
static int configure_icons
Definition: configure.c:64
RISC OS option setting (interface).
void ro_gui_dialog_open_persistent(wimp_w parent, wimp_w w, bool pointer)
Open a persistent dialog box relative to the pointer.
Definition: dialog.c:591
bool ro_gui_dialog_open_top(wimp_w w, struct toolbar *toolbar, int width, int height)
Moves a window to the top of the stack.
Definition: dialog.c:413
wimp_w ro_gui_dialog_create(const char *template_name)
Create a window from a template.
Definition: dialog.c:208
static nserror initialise(const struct llcache_store_parameters *parameters)
Initialise the backing store.
static nserror finalise(void)
Finalise the backing store.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:256
Localised message support (interface).
void ro_gui_screen_size(int *width, int *height)
Find screen size in OS units.
Definition: gui.c:2348
int width
Definition: gui.c:161
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2082
int height
Definition: gui.c:162
Interface to utility string handling.
struct configure_tool * next
Definition: configure.c:59
char * validation
Definition: configure.c:53
const char * name
Definition: configure.c:50
char translated[CONFIGURE_TOOL_TRANSLATED_SIZE]
Definition: configure.c:52
void(* finalise)(wimp_w w)
Definition: configure.c:55
bool(* initialise)(wimp_w w)
Definition: configure.c:54
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
General RISC OS WIMP/OS library functions (interface).
bool ro_gui_wimp_event_set_help_prefix(wimp_w w, const char *help_prefix)
Set the associated help prefix for a given window.
Definition: wimp_event.c:390
void ro_gui_wimp_event_finalise(wimp_w w)
Free any resources associated with a window.
Definition: wimp_event.c:296
bool ro_gui_wimp_event_register_mouse_click(wimp_w w, bool(*callback)(wimp_pointer *pointer))
Register a function to be called for all mouse-clicks to icons in a window that don't have registered...
Definition: wimp_event.c:1439
bool ro_gui_wimp_event_register_close_window(wimp_w w, void(*callback)(wimp_w w))
Register a function to be called after the window has been closed.
Definition: wimp_event.c:1492
bool ro_gui_wimp_event_register_open_window(wimp_w w, void(*callback)(wimp_open *open))
Register a function to be called for all window opening requests.
Definition: wimp_event.c:1477
Automated RISC OS WIMP event handling (interface).