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
121 /* translate the icons */
123 die("ro_gui_configure_translate failed");
124}
125
127{
128 int width, height;
129
133}
134
135bool ro_gui_configure_click(wimp_pointer *pointer)
136{
137 struct configure_tool *tool;
138
139 if (pointer->buttons == wimp_CLICK_MENU)
140 return true;
141
142 for (tool = configure_tools; tool; tool = tool->next) {
143 if (tool->i == pointer->i) {
144 if (!tool->open) {
145 tool->open = true;
146 if (!tool->initialise(tool->w))
147 return false;
150 tool->w, true);
152 tool->w,
154 } else {
155 ro_gui_dialog_open_top(tool->w, NULL, 0, 0);
156 }
157 break;
158 }
159 }
160 return true;
161}
162
164{
165 struct configure_tool *tool;
166
167 for (tool = configure_tools; tool; tool = tool->next) {
168 if (tool->w == w) {
169 tool->open = false;
170 if (tool->finalise)
171 tool->finalise(w);
172 break;
173 }
174 }
175}
176
178{
179 os_error *error;
180 int screen_width, screen_height;
181 int height, width;
182 int icons_per_line, icon_lines;
183 int max_height;
184 os_box extent = { 0, 0, 0, 0 };
185 struct configure_tool *tool;
186
188 ro_warn_user("ro_gui_configure_translate failed", 0);
189 return;
190 }
191
192 width = open->visible.x1 - open->visible.x0;
193 height = open->visible.y1 - open->visible.y0;
194 icons_per_line = width / configure_icon_width;
195 if (icons_per_line < 1)
196 icons_per_line = 1;
197
198 /* move our icons */
199 if (icons_per_line != configure_icons_per_line) {
200 int x, y, l;
201 configure_icons_per_line = icons_per_line;
204 l = 0;
205 for (tool = configure_tools; tool; tool = tool->next) {
206 error = xwimp_resize_icon(configure_window,
207 tool->i,
208 x,
209 y,
214 if (error) {
215 NSLOG(netsurf, INFO,
216 "xwimp_resize_icon: 0x%x: %s",
217 error->errnum,
218 error->errmess);
219 }
221 l++;
222 if (l >= icons_per_line) {
224 l = 0;
226 }
227 }
228 error = xwimp_force_redraw(configure_window,
229 0, -16384, 16384, 0);
230 if (error) {
231 NSLOG(netsurf, INFO, "xwimp_force_redraw: 0x%x: %s",
232 error->errnum, error->errmess);
233 ro_warn_user("WimpError", error->errmess);
234 }
235 }
236
237 /* restrict our height */
238 icon_lines = (configure_icons + icons_per_line - 1) /
239 icons_per_line;
240 max_height = (icon_lines * configure_icon_height);
241 if (height > max_height)
242 open->visible.y0 = open->visible.y1 - max_height;
243
244 /* set the extent */
245 if ((configure_height != height) || (configure_width != width)) {
246 int max_icons_per_line;
247 ro_gui_screen_size(&screen_width, &screen_height);
248 max_icons_per_line = screen_width / configure_icon_width;
249 if (max_icons_per_line > configure_icons)
250 max_icons_per_line = configure_icons;
251 extent.x1 = configure_icon_width * max_icons_per_line;
252 extent.y0 = -max_height;
253 error = xwimp_set_extent(open->w, &extent);
254 if (error) {
255 NSLOG(netsurf, INFO, "xwimp_set_extent: 0x%x: %s",
256 error->errnum, error->errmess);
257 ro_warn_user("WimpError", error->errmess);
258 return;
259 }
262 }
263
264 /* open the window */
265 error = xwimp_open_window(open);
266 if (error) {
267 NSLOG(netsurf, INFO, "xwimp_open_window: 0x%x: %s",
268 error->errnum, error->errmess);
269 ro_warn_user("WimpError", error->errmess);
270 return;
271 }
272}
273
274void ro_gui_configure_register(const char *window,
275 bool (*initialise)(wimp_w w), void (*finalise)(wimp_w w))
276{
277 wimp_icon_create new_icon;
278 struct configure_tool *tool;
279 struct configure_tool *link;
280 os_error *error;
281
282 /* create our tool */
283 tool = calloc(sizeof(struct configure_tool), 1);
284 if (!tool) {
285 NSLOG(netsurf, INFO, "Insufficient memory for calloc()");
286 die("Insufficient memory");
287 return; /* For the benefit of scan-build */
288 }
289 tool->name = window;
290 tool->translated[0] = '\0';
291 tool->validation = malloc(strlen(window) + 2);
292 if (!tool->validation) {
293 NSLOG(netsurf, INFO, "Insufficient memory for malloc()");
294 die("Insufficient memory");
295 }
296 sprintf(tool->validation, "S%s", window);
297 tool->initialise = initialise;
298 tool->finalise = finalise;
299 tool->w = ro_gui_dialog_create(tool->name);
300
301 /* create the icon */
302 new_icon.w = configure_window;
303 new_icon.icon.extent.x0 = 0;
304 new_icon.icon.extent.x1 = configure_icon_width;
305 new_icon.icon.extent.y1 = 0;
306 new_icon.icon.extent.y0 = -configure_icon_height;
307 new_icon.icon.flags = wimp_ICON_TEXT | wimp_ICON_SPRITE |
308 wimp_ICON_INDIRECTED | wimp_ICON_HCENTRED |
309 (wimp_COLOUR_VERY_LIGHT_GREY << wimp_ICON_BG_COLOUR_SHIFT) |
310 (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
311 (wimp_BUTTON_CLICK << wimp_ICON_BUTTON_TYPE_SHIFT);
312 new_icon.icon.data.indirected_text_and_sprite.text =
313 tool->translated;
314 new_icon.icon.data.indirected_text_and_sprite.validation =
315 tool->validation;
316 new_icon.icon.data.indirected_text_and_sprite.size =
318 error = xwimp_create_icon(&new_icon, &tool->i);
319 if (error) {
320 NSLOG(netsurf, INFO, "xwimp_create_icon: 0x%x: %s",
321 error->errnum, error->errmess);
322 die(error->errmess);
323 }
324
325 /* Set the icon's text in current local encoding */
327 messages_get(tool->name), true);
328
329 /* link into our list alphabetically */
330 if ((!configure_tools) ||
332 tool->translated) > 0)) {
333 tool->next = configure_tools;
334 configure_tools = tool;
335 } else {
336 for (link = configure_tools; link; link = link->next) {
337 if (link->next) {
338 if (strcmp(link->next->translated,
339 tool->translated) > 0) {
340 tool->next = link->next;
341 link->next = tool;
342 break;
343 }
344 } else {
345 link->next = tool;
346 break;
347 }
348 }
349 }
351}
352
353/**
354 * Translate tool icons into the system local encoding.
355 * This will also recalculate the minimum required icon width.
356 *
357 * \return true on success, false on memory exhaustion
358 */
360{
361 int alphabet;
362 struct configure_tool *tool;
363 int icon_width;
364 os_error *error;
365
366 /* read current alphabet */
367 error = xosbyte1(osbyte_ALPHABET_NUMBER, 127, 0,
368 &alphabet);
369 if (error) {
370 NSLOG(netsurf, INFO, "failed reading alphabet: 0x%x: %s",
371 error->errnum, error->errmess);
372 /* assume Latin1 */
373 alphabet = territory_ALPHABET_LATIN1;
374 }
375
376 if (alphabet == configure_current_encoding)
377 /* text is already in the correct encoding */
378 return true;
379
380 /* reset icon width */
382
383 for (tool = configure_tools; tool; tool = tool->next) {
384 /* re-translate the text */
386 messages_get(tool->name), true);
387
388 /* update the width */
389 error = xwimptextop_string_width(tool->translated,
390 strlen(tool->translated), &icon_width);
391 if (error) {
392 NSLOG(netsurf, INFO,
393 "xwimptextop_string_width: 0x%x: %s",
394 error->errnum,
395 error->errmess);
396 return false;
397 }
398 icon_width += CONFIGURE_ICON_PADDING_H;
399 if (icon_width > configure_icon_width)
400 configure_icon_width = icon_width;
401
402 error = xwimp_resize_icon(configure_window,
403 tool->i,
404 0,
407 0);
408 if (error) {
409 NSLOG(netsurf, INFO, "xwimp_resize_icon: 0x%x: %s",
410 error->errnum, error->errmess);
411 }
412 }
413
414 /* invalidate our global icons_per_line setting
415 * so the icons get reflowed */
417
418 /* finally, set the current encoding */
420
421 return true;
422}
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_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:359
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:135
#define CONFIGURE_DEFAULT_ICON_HEIGHT
Definition: configure.c:47
static void ro_gui_configure_close(wimp_w w)
Definition: configure.c:163
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:274
#define CONFIGURE_ICON_PADDING_H
Definition: configure.c:44
static void ro_gui_configure_open_window(wimp_open *open)
Definition: configure.c:177
void ro_gui_configure_show(void)
Definition: configure.c:126
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:241
Localised message support (interface).
void ro_gui_screen_size(int *width, int *height)
Find screen size in OS units.
Definition: gui.c:2343
int width
Definition: gui.c:160
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2077
int height
Definition: gui.c:161
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).