summaryrefslogtreecommitdiff
path: root/riscos/theme.c
blob: a78c9679037403685010c74138af518b8ac13f82 (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
/*
 * 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 2004, 2005 Richard Wilson <info@tinct.net>
 */

/** \file
 * Window themes and toolbars (implementation).
 */

#include <alloca.h>
#include <assert.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "oslib/os.h"
#include "oslib/osgbpb.h"
#include "oslib/osgbpb.h"
#include "oslib/osfile.h"
#include "oslib/osfind.h"
#include "oslib/osspriteop.h"
#include "oslib/wimpspriteop.h"
#include "oslib/squash.h"
#include "oslib/wimp.h"
#include "oslib/wimpextend.h"
#include "oslib/wimpspriteop.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/theme.h"
#include "netsurf/riscos/wimp.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"

#define THEME_URL_MEMORY 256
#define THEME_THROBBER_MEMORY 12
#define THEME_STATUS_MEMORY 256

static struct theme_descriptor *theme_current = NULL;
static struct theme_descriptor *theme_descriptors = NULL;

static void ro_gui_theme_get_available_in_dir(const char *directory);
static void ro_gui_theme_free(struct theme_descriptor *descriptor, bool list);
static struct toolbar_icon *ro_gui_theme_add_toolbar_icon(struct toolbar *toolbar, const char *name,
		int icon_number);
static void ro_gui_theme_update_toolbar_icon(struct toolbar *toolbar, struct toolbar_icon *icon);
static void ro_gui_theme_destroy_toolbar_icon(struct toolbar_icon *icon);


/*	A basic window for the toolbar and status
*/
static wimp_window theme_toolbar_window = {
	{0, 0, 16384, 16384},
	0,
	0,
	wimp_TOP,
	wimp_WINDOW_NEW_FORMAT | wimp_WINDOW_MOVEABLE | wimp_WINDOW_FURNITURE_WINDOW |
			wimp_WINDOW_IGNORE_XEXTENT | wimp_WINDOW_IGNORE_YEXTENT,
	wimp_COLOUR_BLACK,
	wimp_COLOUR_LIGHT_GREY,
	wimp_COLOUR_LIGHT_GREY,
	wimp_COLOUR_VERY_LIGHT_GREY,
	wimp_COLOUR_DARK_GREY,
	wimp_COLOUR_MID_LIGHT_GREY,
	wimp_COLOUR_CREAM,
	wimp_WINDOW_NEVER3D | 0x16u /* RISC OS 5.03+ - greyed icons detected for interactive help */,
	{0, 0, 16384, 16384},
	0,
	0,
	wimpspriteop_AREA,
	12,
	1,
	{""},
	0,
	{ }
};


/*	Shared icon validation
*/
static char theme_url_validation[] = "Pptr_write;KN\0";
static char theme_resize_validation[] = "R1;Pptr_lr,8,6\0";
static char theme_null_text_string[] = "\0";
static char theme_separator_name[] = "separator\0";


/**
 * Initialise the theme handler
 */
void ro_gui_theme_initialise(void) {
	/*	Get an initial theme list
	*/
	theme_descriptors = ro_gui_theme_get_available();
}


/**
 * Finalise the theme handler
 */
void ro_gui_theme_finalise(void) {
	/*	Free all closed descriptors
	*/
	ro_gui_theme_close(theme_current, false);
	ro_gui_theme_free(theme_descriptors, true);
}


/**
 * Finds a theme from the cached values.
 *
 * The returned theme is only guaranteed to be valid until the next call
 * to ro_gui_theme_get_available() unless it has been opened using
 * ro_gui_theme_open().
 *
 * \param filename  the filename of the theme_descriptor to return
 * \return the requested theme_descriptor, or NULL if not found
 */
struct theme_descriptor *ro_gui_theme_find(const char *filename) {
	struct theme_descriptor *descriptor;

	/*	Check for bad filename
	*/
	if (!filename) return NULL;

	/*	Work through until we find our required filename
	*/
	descriptor = theme_descriptors;
	while (descriptor) {
		if (!strcmp(filename, descriptor->filename)) return descriptor;
		descriptor = descriptor->next;
	}
	return NULL;
}


/**
 * Reads and caches the currently available themes.
 *
 * \return the requested theme_descriptor, or NULL if not found
 */
struct theme_descriptor *ro_gui_theme_get_available(void) {
	struct theme_descriptor *current;
	struct theme_descriptor *test;
	char pathname[256];

	/*	Close any descriptors we've got so far
	*/
	ro_gui_theme_free(theme_descriptors, true);

	/*	Open a variety of directories
	*/
	snprintf(pathname, 256, "%s.Resources", NETSURF_DIR);
	pathname[255] = '\0';
	ro_gui_theme_get_available_in_dir(pathname);
#ifndef NCOS
	ro_gui_theme_get_available_in_dir("Choices:WWW.NetSurf.Themes");
#else
	ro_gui_theme_get_available_in_dir("<User$Path>.Choices.NetSurf.Choices.Themes");
#endif

	/*	Sort alphabetically in a very rubbish way
	*/
	if ((theme_descriptors) && (theme_descriptors->next)) {
		current = theme_descriptors;
		while ((test = current->next)) {
			if (strcmp(current->name, test->name) > 0) {
				current->next->previous = current->previous;
				if (current->previous)
					current->previous->next = current->next;
				current->next = test->next;
				test->next = current;
				current->previous = test;
				if (current->next)
					current->next->previous = current;

				current = test->previous;
				if (!current) current = test;
			} else {
				current = current->next;
			}
		}
		while (theme_descriptors->previous)
			theme_descriptors = theme_descriptors->previous;
	}
	return theme_descriptors;
}


/**
 * Adds the themes in a directory to the global cache.
 *
 * \param directory  the directory to scan
 */
static void ro_gui_theme_get_available_in_dir(const char *directory) {
	struct theme_file_header file_header;
	struct theme_descriptor *current;
	char pathname[256];
	int context = 0;
	int read_count;
	osgbpb_INFO(100) info;
	int output_left;
	os_fw file_handle;
	os_error *error;

	/*	Create a new set
	*/
	while (context != -1) {
		/*	Get the next entry
		*/
		error = xosgbpb_dir_entries_info(directory,
				(osgbpb_info_list *) &info, 1, context,
				sizeof(info), 0, &read_count, &context);
		if (error) {
			LOG(("xosgbpb_dir_entries_info: 0x%x: %s",
				error->errnum, error->errmess));
			if (error->errnum == 0xd6)	/* no such dir */
				return;
			warn_user("MiscError", error->errmess);
			break;
		}

		/*	Check if we've read anything
		*/
		if (read_count == 0)
			continue;

		/*	Get our full filename
		*/
		snprintf(pathname, sizeof pathname, "%s.%s",
				directory, info.name);
		pathname[sizeof pathname - 1] = 0;

		/*	Only process files
		*/
		if ((info.obj_type == fileswitch_IS_FILE) && (!ro_gui_theme_find(pathname))) {

			/*	Get the header
			*/
			error = xosfind_openinw(osfind_NO_PATH, pathname, 0, &file_handle);
			if (error) {
				LOG(("xosfind_openinw: 0x%x: %s",
					error->errnum, error->errmess));
				warn_user("FileError", error->errmess);
				continue;
			}
			if (file_handle == 0)
				continue;
			error = xosgbpb_read_atw(file_handle, (char *)&file_header,
					sizeof (struct theme_file_header),
					0, &output_left);
			xosfind_closew(file_handle);
			if (error) {
				LOG(("xosbgpb_read_atw: 0x%x: %s",
					error->errnum, error->errmess));
				warn_user("FileError", error->errmess);
				continue;
			}
			if (output_left > 0)
				continue;	/* should try to read more? */

			/*	Create a new theme descriptor
			*/
			current = (struct theme_descriptor *)calloc(1,
					sizeof(struct theme_descriptor));
			if (!current) {
				LOG(("calloc failed"));
				warn_user("NoMemory", 0);
				return;
			}

			if (!ro_gui_theme_read_file_header(current, &file_header)) {
				free(current);
				continue;
			}

			current->filename = malloc(strlen(pathname) + 1);
			if (!current->filename) {
				LOG(("malloc failed"));
				warn_user("NoMemory", 0);
				free(current);
				return;
			}
			strcpy(current->filename, pathname);

			/*	Link in our new descriptor
			*/
			if (theme_descriptors) {
				current->next = theme_descriptors;
				theme_descriptors->previous = current;
			}
			theme_descriptors = current;
		}
	}
}


/**
 * Fills in the basic details for a descriptor from a file header.
 * The filename string is not set.
 *
 * \param descriptor   the descriptor to set up
 * \param file_header  the header to read from
 * \return false for a badly formed theme, true otherwise
 */
bool ro_gui_theme_read_file_header(struct theme_descriptor *descriptor,
		struct theme_file_header *file_header) {

	if ((file_header->magic_value != 0x4d54534e) ||
			(file_header->parser_version > 2))
		return false;

	strcpy(descriptor->name, file_header->name);
	strcpy(descriptor->author, file_header->author);
	descriptor->browser_background = file_header->browser_bg;
	descriptor->hotlist_background = file_header->hotlist_bg;
	descriptor->status_background = file_header->status_bg;
	descriptor->status_foreground = file_header->status_fg;
	descriptor->decompressed_size = file_header->decompressed_sprite_size;
	descriptor->compressed_size = file_header->compressed_sprite_size;
	if (file_header->parser_version >= 2) {
		descriptor->throbber_right = !(file_header->theme_flags & (1 << 0));
		descriptor->throbber_redraw = file_header->theme_flags & (1 << 1);
	} else {
		descriptor->throbber_right = (file_header->theme_flags == 0x00);
		descriptor->throbber_redraw = true;	  
	}
	return true;
}


/**
 * Opens a theme ready for use.
 *
 * \param descriptor  the theme_descriptor to open
 * \param list	      whether to open all themes in the list
 * \return whether the operation was successful
 */
bool ro_gui_theme_open(struct theme_descriptor *descriptor, bool list) {
	fileswitch_object_type obj_type;
	squash_output_status status;
	os_coord dimensions;
	os_mode mode;
	os_error *error;
	char sprite_name[16];
	bool result = true;
	int i, n;
	int workspace_size, file_size;
	char *raw_data, *workspace;
	osspriteop_area *decompressed;
	
	/*	If we are freeing the whole of the list then we need to
		start at the first descriptor.
	*/
	if (list && descriptor) {
		while (descriptor->previous) descriptor = descriptor->previous;
	}

	/*	Open the themes
	*/
	while (descriptor) {
		/*	If we are already loaded, increase the usage count
		*/
		if (descriptor->theme) {
			descriptor->theme->users = descriptor->theme->users + 1;
		} else if (descriptor->decompressed_size > 0) {
			/*	Create a new theme
			*/
			descriptor->theme = (struct theme *)calloc(1, sizeof(struct theme));
			if (!descriptor->theme) {
				LOG(("calloc failed"));
				warn_user("NoMemory", 0);
				return false;
			}
			descriptor->theme->users = 1;

			/*	Load the file. We use a goto to exit from here on in as using
				a continue leaves us in an infinite loop - it's nasty, and really
				should be rewritten properly.
			*/
			error = xosfile_read_stamped_no_path(descriptor->filename,
					&obj_type, 0, 0, &file_size, 0, 0);
			if (error) {
				LOG(("xosfile_read_stamped_no_path: 0x%x: %s",
						error->errnum, error->errmess));
				warn_user("FileError", error->errmess);
				goto ro_gui_theme_open_continue;
			}
			if (obj_type != fileswitch_IS_FILE)
				goto ro_gui_theme_open_continue;
			raw_data = malloc(file_size);
			if (!raw_data) {
				LOG(("No memory for malloc()"));
				warn_user("NoMemory", 0);
				return false;
			}
			error = xosfile_load_stamped_no_path(descriptor->filename, (byte *)raw_data,
					0, 0, 0, 0, 0);
			if (error) {
				free(raw_data);
				LOG(("xosfile_load_stamped_no_path: 0x%x: %s",
						error->errnum, error->errmess));
				warn_user("FileError", error->errmess);
				goto ro_gui_theme_open_continue;
			}

			/*	Decompress the sprites
			*/
			error = xsquash_decompress_return_sizes(-1, &workspace_size, 0);
			if (error) {
				free(raw_data);
				LOG(("xsquash_decompress_return_sizes: 0x%x: %s",
						error->errnum, error->errmess));
				warn_user("MiscError", error->errmess);
				goto ro_gui_theme_open_continue;
			}
			decompressed = (osspriteop_area *)malloc(descriptor->decompressed_size);
			workspace = malloc(workspace_size);
			if ((!decompressed) || (!workspace)) {
				if (decompressed) free(decompressed);
				if (workspace) free(raw_data);
				LOG(("No memory for malloc()"));
				warn_user("NoMemory", 0);
				return false;
			}
			error = xsquash_decompress(squash_INPUT_ALL_PRESENT,
					workspace,
					(byte *)(raw_data + sizeof(struct theme_file_header)),
					descriptor->compressed_size,
					(byte *)decompressed,
					descriptor->decompressed_size,
					&status, 0, 0, 0, 0);
			free(workspace);
			free(raw_data);
			if (error) {
				free(decompressed);
				LOG(("xsquash_decompress: 0x%x: %s",
						error->errnum, error->errmess));
				warn_user("MiscError", error->errmess);
				goto ro_gui_theme_open_continue;
			}
			if (status != 0) {
				free(decompressed);
				goto ro_gui_theme_open_continue;
			}
			descriptor->theme->sprite_area = decompressed;

			/*	Find the highest sprite called 'throbber%i', and get the
				maximum dimensions for all 'thobber%i' icons.
			*/
			for (i = 1; i <= descriptor->theme->sprite_area->sprite_count; i++) {
				xosspriteop_return_name(osspriteop_USER_AREA,
						descriptor->theme->sprite_area, sprite_name, 16, i, 0);
				if (strncmp(sprite_name, "throbber", 8) == 0) {
					/*	Get the max sprite width/height
					*/
					xosspriteop_read_sprite_info(osspriteop_USER_AREA,
						descriptor->theme->sprite_area,
						(osspriteop_id)sprite_name,
						&dimensions.x, &dimensions.y,
						(osbool *)0, &mode);
					ro_convert_pixels_to_os_units(&dimensions, mode);
					if (dimensions.x > descriptor->theme->throbber_width)
						descriptor->theme->throbber_width = dimensions.x;
					if (dimensions.y > descriptor->theme->throbber_height)
						descriptor->theme->throbber_height = dimensions.y;

					/*	Get the throbber number
					*/
					n = atoi(sprite_name + 8);
					if (descriptor->theme->throbber_frames < n)
						descriptor->theme->throbber_frames = n;
				}
			}
		}
ro_gui_theme_open_continue:
		
		/*	Loop or return depending on whether the entire list
			is to be processed.
		*/
		if (list && descriptor)
			descriptor = descriptor->next;
		else
			return result;
	}
	return result;
}


/**
 * Applies the theme to all current windows and subsequent ones.
 *
 * \param descriptor  the theme_descriptor to open
 * \return whether the operation was successful
 */
bool ro_gui_theme_apply(struct theme_descriptor *descriptor) {
	struct theme_descriptor *theme_previous;

	/*	Check if the theme is already applied
	*/
	if (descriptor == theme_current) return true;

	/*	Re-open the new-theme and release the current theme
	*/
	if (!ro_gui_theme_open(descriptor, false)) {
		/*	The error has already been reported
		*/
		return false;
	}
	theme_previous = theme_current;
	theme_current = descriptor;

	/*	Apply the theme to all the current windows
	*/
	ro_gui_window_update_theme();

	/*	Release the previous theme
	*/
	ro_gui_theme_close(theme_previous, false);
	return true;
}


/**
 * Closes a theme after use.
 *
 * \param descriptor  the theme_descriptor to close
 * \param list	      whether to open all themes in the list
 * \return whether the operation was successful
 */
void ro_gui_theme_close(struct theme_descriptor *descriptor, bool list) {

	/*	We might not have created any descriptors yet to close.
	*/
	if (!descriptor) return;

	/*	If we are freeing the whole of the list then we need to
		start at the first descriptor.
	*/
	if (list) {
		while (descriptor->previous) descriptor = descriptor->previous;
	}

	/*	Close the themes
	*/
	while (descriptor) {
		/*	Lower the theme usage count
		*/
		if (descriptor->theme) {
			descriptor->theme->users = descriptor->theme->users - 1;
			if (descriptor->theme->users <= 0) {
				free(descriptor->theme->sprite_area);
				free(descriptor->theme);
				descriptor->theme = NULL;
			}
		}

		/*	Loop or return depending on whether the entire list
			is to be processed.
		*/
		if (list) {
			descriptor = descriptor->next;
		} else {
			return;
		}
	}
}


/**
 * Performs the redraw for a toolbar
 *
 * \param redraw   the redraw area
 * \param toolbar  the toolbar to redraw
 */
void ro_gui_theme_redraw(struct toolbar *toolbar, wimp_draw *redraw) {
	assert(toolbar);

	struct toolbar_icon *icon;
	osbool more = wimp_redraw_window(redraw);
	wimp_icon separator_icon;
	bool perform_redraw = false;

	/*	Set up the icon
	*/
	if ((toolbar->descriptor) && (toolbar->descriptor->theme) &&
			(toolbar->descriptor->theme->sprite_area)) {
		separator_icon.flags = wimp_ICON_SPRITE | wimp_ICON_INDIRECTED | wimp_ICON_HCENTRED |
				wimp_ICON_VCENTRED;
		separator_icon.data.indirected_sprite.id = (osspriteop_id)theme_separator_name;
		separator_icon.data.indirected_sprite.area =
				toolbar->descriptor->theme->sprite_area;
		separator_icon.data.indirected_sprite.size = 12;
		separator_icon.extent.y0 = 0;
		separator_icon.extent.y1 = toolbar->height;
		perform_redraw = true;
	}
	perform_redraw &= toolbar->display_buttons;

	while (more) {
		if (perform_redraw) {
			for (icon = toolbar->icon; icon; icon = icon->next) {
				if ((icon->icon_number == -1) && (icon->display))  {
					separator_icon.extent.x0 = icon->x;
					separator_icon.extent.x1 = icon->x + icon->width;
					wimp_plot_icon(&separator_icon);
				}
			}
	  	}
		more = wimp_get_rectangle(redraw);
	}
}


/**
 * Frees any unused theme descriptors.
 *
 * \param descriptor  the theme_descriptor to free
 * \param list	      whether to open all themes in the list
 * \return whether the operation was successful
 */
void ro_gui_theme_free(struct theme_descriptor *descriptor, bool list) {
	struct theme_descriptor *next_descriptor;

	/*	We might not have created any descriptors yet to close.
	*/
	if (!descriptor) return;

	/*	If we are freeing the whole of the list then we need to
		start at the first descriptor.
	*/
	while ((list) && (descriptor->previous))
		descriptor = descriptor->previous;

	/*	Close the themes
	*/
	while (descriptor) {
		next_descriptor = descriptor->next;

		/*	If we have no loaded theme then we can kill the descriptor
		*/
		if (!descriptor->theme) {
			if (descriptor->previous)
					descriptor->previous->next = descriptor->next;
			if (descriptor->next)
					descriptor->next->previous = descriptor->previous;

			/*	Keep the cached list in sync
			*/
			if (theme_descriptors == descriptor)
					theme_descriptors = next_descriptor;

			/*	Release any memory
			*/
			free(descriptor->filename);
			free(descriptor);
		}

		if (list)
			descriptor = next_descriptor;
		else
			return;
	}
}


/**
 * Creates a toolbar.
 *
 * \param descriptor  the theme to use, or NULL for current
 * \param type	      the toolbar type
 * \return a new toolbar, or NULL for failure
 */
struct toolbar *ro_gui_theme_create_toolbar(struct theme_descriptor *descriptor, toolbar_type type) {
	struct toolbar *toolbar;

	/*	Create a new toolbar
	*/
	toolbar = calloc(sizeof(struct toolbar), 1);
	if (!toolbar) {
		LOG(("No memory for malloc()"));
		warn_user("NoMemory", 0);
		return NULL;
	}
	toolbar->type = type;

	/*	Store the theme
	*/
	if (!descriptor) descriptor = theme_current;
	toolbar->descriptor = descriptor;

	/*	Apply the default settings
	*/
	toolbar->display_buttons = true;
	toolbar->toolbar_current = 16384;
	switch (type) {
		case THEME_BROWSER_TOOLBAR:
			toolbar->display_url = true;
			toolbar->display_throbber = true;
			toolbar->display_status = true;
			ro_gui_theme_add_toolbar_icon(toolbar, "back", ICON_TOOLBAR_BACK);
			ro_gui_theme_add_toolbar_icon(toolbar, "forward", ICON_TOOLBAR_FORWARD);
			ro_gui_theme_add_toolbar_icon(toolbar, "stop", ICON_TOOLBAR_STOP);
			ro_gui_theme_add_toolbar_icon(toolbar, "reload", ICON_TOOLBAR_RELOAD);
			ro_gui_theme_add_toolbar_icon(toolbar, "home", ICON_TOOLBAR_HOME);
			ro_gui_theme_add_toolbar_icon(toolbar, NULL, -1);
			ro_gui_theme_add_toolbar_icon(toolbar, "search", ICON_TOOLBAR_SEARCH);
			ro_gui_theme_add_toolbar_icon(toolbar, "history", ICON_TOOLBAR_HISTORY);
			ro_gui_theme_add_toolbar_icon(toolbar, "scale", ICON_TOOLBAR_SCALE);
			ro_gui_theme_add_toolbar_icon(toolbar, NULL, -1);
			ro_gui_theme_add_toolbar_icon(toolbar, "hotlist", ICON_TOOLBAR_BOOKMARK);
			ro_gui_theme_add_toolbar_icon(toolbar, "save", ICON_TOOLBAR_SAVE);
			ro_gui_theme_add_toolbar_icon(toolbar, "print", ICON_TOOLBAR_PRINT);
			toolbar->suggest = ro_gui_theme_add_toolbar_icon(NULL, "gright",
					ICON_TOOLBAR_SUGGEST);
			break;
		case THEME_HOTLIST_TOOLBAR:
			ro_gui_theme_add_toolbar_icon(toolbar, "create", ICON_TOOLBAR_CREATE);
		case THEME_HISTORY_TOOLBAR:
			ro_gui_theme_add_toolbar_icon(toolbar, "delete", ICON_TOOLBAR_DELETE);
			ro_gui_theme_add_toolbar_icon(toolbar, "launch", ICON_TOOLBAR_LAUNCH);
			ro_gui_theme_add_toolbar_icon(toolbar, NULL, -1);
			ro_gui_theme_add_toolbar_icon(toolbar, "open", ICON_TOOLBAR_OPEN);
			ro_gui_theme_add_toolbar_icon(toolbar, "expand", ICON_TOOLBAR_EXPAND);
			break;
	}

	/*	Claim the memory for our Wimp indirection
	*/
	if (type == THEME_BROWSER_TOOLBAR) {
		toolbar->url_buffer = calloc(1, THEME_URL_MEMORY + THEME_THROBBER_MEMORY +
				THEME_STATUS_MEMORY);
		if (!toolbar->url_buffer) {
			LOG(("No memory for calloc()"));
			ro_gui_theme_destroy_toolbar(toolbar);
			return NULL;
		}
		toolbar->throbber_buffer = toolbar->url_buffer + THEME_URL_MEMORY;
		toolbar->status_buffer = toolbar->throbber_buffer + THEME_THROBBER_MEMORY;
		sprintf(toolbar->throbber_buffer, "throbber0");
	}

	/*	Apply the desired theme to the toolbar
	*/
	if (!ro_gui_theme_update_toolbar(descriptor, toolbar)) {
		ro_gui_theme_destroy_toolbar(toolbar);
		return NULL;
	}
	return toolbar;
}


/**
 * Updates a toolbar to use a particular theme.
 * The toolbar may be unstable on failure and should be destroyed.
 *
 * \param descriptor  the theme to use, or NULL for current
 * \param toolbar     the toolbar to update
 * \return whether the operation was successful
 */
bool ro_gui_theme_update_toolbar(struct theme_descriptor *descriptor, struct toolbar *toolbar) {
	wimp_icon_create new_icon;
	os_error *error;
	osspriteop_area *sprite_area;
	struct toolbar_icon *toolbar_icon;
	int width, max_icon;
	wimp_icon_flags icon_flags;
	if (!toolbar) return false;

	/*	Set the theme and window sprite area
	*/
	if (!descriptor) descriptor = theme_current;
	toolbar->descriptor = descriptor;
	if ((toolbar->descriptor) && (toolbar->descriptor->theme))
		sprite_area = toolbar->descriptor->theme->sprite_area;
	else
		sprite_area = (osspriteop_area *)1;
	theme_toolbar_window.sprite_area = sprite_area;

	/*	Update the icon sizes
	*/
	for (toolbar_icon = toolbar->icon; toolbar_icon; toolbar_icon = toolbar_icon->next)
		ro_gui_theme_update_toolbar_icon(toolbar, toolbar_icon);
	if (toolbar->suggest)
		ro_gui_theme_update_toolbar_icon(toolbar, toolbar->suggest);

	/*	Recreate the toolbar window
	*/
	if (toolbar->descriptor) {
		if (toolbar->type == THEME_BROWSER_TOOLBAR)
			theme_toolbar_window.work_bg = toolbar->descriptor->browser_background;
		else
			theme_toolbar_window.work_bg = toolbar->descriptor->hotlist_background;
	} else {
		theme_toolbar_window.work_bg = wimp_COLOUR_VERY_LIGHT_GREY;
	}
	theme_toolbar_window.flags &= ~wimp_WINDOW_AUTO_REDRAW;
	theme_toolbar_window.flags |= wimp_WINDOW_NO_BOUNDS;
	theme_toolbar_window.xmin = 1;
	theme_toolbar_window.ymin = 1;
	theme_toolbar_window.extent.x1 = 16384;
	theme_toolbar_window.extent.y1 = 16384;
	theme_toolbar_window.sprite_area = sprite_area;
	if (toolbar->toolbar_handle) {
		error = xwimp_delete_window(toolbar->toolbar_handle);
		if (error)
			LOG(("xwimp_delete_window: 0x%x: %s",
					error->errnum, error->errmess));
		toolbar->toolbar_handle = NULL;
	}
	error = xwimp_create_window(&theme_toolbar_window, &toolbar->toolbar_handle);
	if (error) {
		LOG(("xwimp_create_window: 0x%x: %s",
				error->errnum, error->errmess));
		warn_user("WimpError", error->errmess);
		return false;
	}

	/*	Create the basic icons
	*/	
	if (toolbar->type == THEME_HOTLIST_TOOLBAR)
		max_icon = ICON_TOOLBAR_HOTLIST_LAST;
	else if (toolbar->type == THEME_HISTORY_TOOLBAR)
		max_icon = ICON_TOOLBAR_HISTORY_LAST;
	else
		max_icon = ICON_TOOLBAR_URL;
	new_icon.w = toolbar->toolbar_handle;
	new_icon.icon.data.indirected_text.size = 1;
	new_icon.icon.flags = wimp_ICON_TEXT | wimp_ICON_SPRITE |
			wimp_ICON_INDIRECTED | wimp_ICON_HCENTRED |
			wimp_ICON_VCENTRED |
			(wimp_BUTTON_CLICK << wimp_ICON_BUTTON_TYPE_SHIFT);
	if (toolbar->descriptor)
		new_icon.icon.flags |= (toolbar->descriptor->browser_background
				 << wimp_ICON_BG_COLOUR_SHIFT);
	else
		new_icon.icon.flags |= (wimp_COLOUR_VERY_LIGHT_GREY
				 << wimp_ICON_BG_COLOUR_SHIFT);
	icon_flags = new_icon.icon.flags;
				 
	for (int i = 0; i < max_icon; i++) {
		new_icon.icon.data.indirected_text.text = theme_null_text_string;
		new_icon.icon.data.indirected_text.validation = theme_null_text_string;
		toolbar_icon = toolbar->icon;
		while (toolbar_icon) {
			if (toolbar_icon->icon_number == i) {
				new_icon.icon.data.indirected_text.validation =
					toolbar_icon->validation;
				break;
			} else {
				toolbar_icon = toolbar_icon->next;
			}
		}
		error = xwimp_create_icon(&new_icon, 0);
		if (error) {
			LOG(("xwimp_create_icon: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}
	}

	/*	Create the URL/throbber icons
	*/
	if (toolbar->type == THEME_BROWSER_TOOLBAR) {
		new_icon.icon.flags = wimp_ICON_TEXT | wimp_ICON_INDIRECTED | wimp_ICON_VCENTRED |
				wimp_ICON_BORDER | wimp_ICON_FILLED |
				(wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
				(wimp_BUTTON_WRITE_CLICK_DRAG << wimp_ICON_BUTTON_TYPE_SHIFT);
		new_icon.icon.data.indirected_text.text = toolbar->url_buffer;
		new_icon.icon.data.indirected_text.validation = theme_url_validation;
		new_icon.icon.data.indirected_text.size = THEME_URL_MEMORY;
		error = xwimp_create_icon(&new_icon, 0);
		if (error) {
			LOG(("xwimp_create_icon: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}

		/*	Now the throbber
		*/
		new_icon.icon.flags = wimp_ICON_SPRITE | wimp_ICON_INDIRECTED | wimp_ICON_HCENTRED |
				wimp_ICON_VCENTRED;
		new_icon.icon.data.indirected_sprite.id = (osspriteop_id)toolbar->throbber_buffer;
		new_icon.icon.data.indirected_sprite.area = sprite_area;
		new_icon.icon.data.indirected_sprite.size = THEME_THROBBER_MEMORY;
		error = xwimp_create_icon(&new_icon, 0);
		if (error) {
			LOG(("xwimp_create_icon: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}
		
		/* 	Now the URL suggestion icon
		*/
		new_icon.icon.data.indirected_text.text = theme_null_text_string;
		new_icon.icon.data.indirected_text.size = 1;
		new_icon.icon.flags = icon_flags;
		if (toolbar->suggest)
			new_icon.icon.data.indirected_text.validation = toolbar->suggest->validation;
		else
			new_icon.icon.data.indirected_text.validation = theme_null_text_string;
		error = xwimp_create_icon(&new_icon, 0);
		if (error) {
			LOG(("xwimp_create_icon: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}
	}
	if (toolbar->parent_handle) {
		ro_gui_theme_attach_toolbar(toolbar, toolbar->parent_handle);
	}

	/*	Recreate the status window
	*/
	if (toolbar->type == THEME_BROWSER_TOOLBAR) {
		/*	Delete the old window and create a new one
		*/
		if (toolbar->status_handle) {
			xwimp_delete_window(toolbar->status_handle);
			toolbar->status_handle = NULL;
		}
		if (toolbar->descriptor)
			theme_toolbar_window.work_bg = toolbar->descriptor->status_background;
		else
			theme_toolbar_window.work_bg = wimp_COLOUR_VERY_LIGHT_GREY;
		theme_toolbar_window.flags &= ~wimp_WINDOW_NO_BOUNDS;
		theme_toolbar_window.flags |= wimp_WINDOW_AUTO_REDRAW;
		theme_toolbar_window.xmin = 12;
		theme_toolbar_window.ymin = ro_get_hscroll_height((wimp_w)0) - 4;
		theme_toolbar_window.extent.y1 = theme_toolbar_window.ymin;
		error = xwimp_create_window(&theme_toolbar_window, &toolbar->status_handle);
		if (error) {
			LOG(("xwimp_create_window: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}

		/*	Create the status resize icon
		*/
		new_icon.w = toolbar->status_handle;
		new_icon.icon.flags = wimp_ICON_TEXT | wimp_ICON_INDIRECTED |
				wimp_ICON_BORDER | wimp_ICON_FILLED |
				(wimp_COLOUR_LIGHT_GREY << wimp_ICON_BG_COLOUR_SHIFT) |
				(wimp_BUTTON_CLICK_DRAG << wimp_ICON_BUTTON_TYPE_SHIFT);
		new_icon.icon.data.indirected_text.text = theme_null_text_string;
		new_icon.icon.data.indirected_text.validation = theme_resize_validation;
		new_icon.icon.data.indirected_text.size = 1;
		error = xwimp_create_icon(&new_icon, 0);
		if (error) {
			LOG(("xwimp_create_icon: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}

		/*	And finally our status display icon
		*/
		new_icon.icon.flags = wimp_ICON_TEXT | wimp_ICON_INDIRECTED | wimp_ICON_VCENTRED;
		if (toolbar->descriptor)
			new_icon.icon.flags |=
				(toolbar->descriptor->status_foreground << wimp_ICON_FG_COLOUR_SHIFT) |
				(toolbar->descriptor->status_background << wimp_ICON_BG_COLOUR_SHIFT);
		else
			new_icon.icon.flags |=
				(wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
				(wimp_COLOUR_VERY_LIGHT_GREY << wimp_ICON_BG_COLOUR_SHIFT);
		new_icon.icon.data.indirected_text.text = toolbar->status_buffer;
		new_icon.icon.data.indirected_text.validation = theme_null_text_string;
		new_icon.icon.data.indirected_text.size = THEME_STATUS_MEMORY;
		error = xwimp_create_icon(&new_icon, 0);
		if (error) {
			LOG(("xwimp_create_icon: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}

	}

	/*	Force a re-processing of the toolbar
	*/
	width = toolbar->toolbar_current;
	toolbar->reformat_buttons = true;
	toolbar->toolbar_current = -1;
	toolbar->status_current = -1;
	ro_gui_theme_process_toolbar(toolbar, width);
	return true;
}


/**
 * Attaches a toolbar to a window.
 *
 * \param toolbar     the toolbar to update
 * \param parent      the window to contain the toolbar
 * \return whether the operation was successful
 */
bool ro_gui_theme_attach_toolbar(struct toolbar *toolbar, wimp_w parent) {
	wimp_outline outline;
	wimp_window_state state;
	if (!toolbar) return false;

	/*	Attach/close the window
	*/
	toolbar->parent_handle = parent;
	if (toolbar->height > 0) {
		outline.w = parent;
		xwimp_get_window_outline(&outline);
		state.w = parent;
		xwimp_get_window_state(&state);
		state.w = toolbar->toolbar_handle;
		state.visible.x1 = outline.outline.x1 - 2;
		state.visible.y0 = state.visible.y1 - toolbar->height + 2;
		state.xscroll = 0;
		state.yscroll = 0;
		xwimp_open_window_nested((wimp_open *)&state, parent,
				wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
						<< wimp_CHILD_XORIGIN_SHIFT |
				wimp_CHILD_LINKS_PARENT_VISIBLE_TOP_OR_RIGHT
						<< wimp_CHILD_YORIGIN_SHIFT |
				wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
						<< wimp_CHILD_LS_EDGE_SHIFT |
				wimp_CHILD_LINKS_PARENT_VISIBLE_TOP_OR_RIGHT
						<< wimp_CHILD_BS_EDGE_SHIFT |
				wimp_CHILD_LINKS_PARENT_VISIBLE_TOP_OR_RIGHT
						<< wimp_CHILD_RS_EDGE_SHIFT |
				wimp_CHILD_LINKS_PARENT_VISIBLE_TOP_OR_RIGHT
						<< wimp_CHILD_TS_EDGE_SHIFT);
	} else {
		xwimp_close_window(toolbar->toolbar_handle);
	}
	return true;
}


/**
 * Updates the toolbars status bar settings to reflect the current size
 *
 * \param toolbar     the toolbar to update
 */
void ro_gui_theme_resize_toolbar_status(struct toolbar *toolbar) {
	os_error *error;
	wimp_outline outline;
	wimp_window_state state;
	wimp_w parent = NULL;
	int parent_size, status_size;
	if ((!toolbar) || (!toolbar->parent_handle)) return;

	/*	Get the width to scale to
	*/
	parent = toolbar->parent_handle;
	outline.w = toolbar->parent_handle;
	error = xwimp_get_window_outline(&outline);
	if (error) {
		LOG(("xwimp_get_window_outline: 0x%x: %s",
			error->errnum, error->errmess));
		warn_user("WimpError", error->errmess);
		return;
	}
	parent_size = outline.outline.x1 - outline.outline.x0 - ro_get_vscroll_width(parent) - 2;

	/*	Get the current size
	*/
	state.w = toolbar->status_handle;
	error = xwimp_get_window_state(&state);
	if (error) {
		LOG(("xwimp_get_window_state: 0x%x: %s",
			error->errnum, error->errmess));
		warn_user("WimpError", error->errmess);
		return;
	}
	status_size = state.visible.x1 - state.visible.x0;

	/*	Store the new size
	*/
	toolbar->status_width = (10000 * status_size) / parent_size;
	if (toolbar->status_width > 10000) toolbar->status_width = 10000;
	ro_gui_theme_process_toolbar(toolbar, -1);
}


/**
 * Updates the toolbar to reflect changes to the icon flags and any reformatting
 * required due to the change in parent window size.
 *
 * \param toolbar     the toolbar to update
 * \param width	      a specific width to resize to, or -1 to use parent width
 * \return whether the operation was successful
 */
bool ro_gui_theme_process_toolbar(struct toolbar *toolbar, int width) {
	wimp_caret caret;
	os_box extent = { 0, 0, 0, 0 };
	os_error *error;
	wimp_w parent = NULL;
	wimp_outline outline;
	wimp_window_state state;
	int throbber_x = -1;
	int status_max;
	int left_edge, right_edge, bottom_edge;
	int status_size = 0;
	int status_height = 0;
	if (!toolbar) return false;
	int old_height = toolbar->height;
	int old_width = toolbar->toolbar_current;
	struct toolbar_icon *toolbar_icon;
	struct toolbar_icon *last_icon = NULL;
	bool visible_icon = false;

  	/*	Disable lone separators
  	*/
  	if (toolbar->reformat_buttons) {
	  	visible_icon = false;
  		toolbar_icon = toolbar->icon;
  		while (toolbar_icon) {
  		  	if (toolbar_icon->icon_number < 0) {
  		  		toolbar_icon->display = visible_icon;
  		  		visible_icon = false;
  		  	} else if (toolbar_icon->width > 0) {
  		  	  	visible_icon |= toolbar_icon->display;
  		  	}
  		  	if (toolbar_icon->display) last_icon = toolbar_icon;
  		  	toolbar_icon = toolbar_icon->next;
  		}
  		if ((last_icon) && (last_icon->icon_number < 0)) {
  			last_icon->display = false;
  		}
  		visible_icon = false;
  	}

	/*	Find the parent window handle if we need to process the status window,
		or the caller has requested we calculate the width ourself.
	*/
	if ((width == -1) || ((toolbar->status_handle) && (toolbar->display_status))) {
		parent = toolbar->parent_handle;

		/*	Get the window outline width
		*/
		if (width == -1) {
			if (!parent) return false;
			outline.w = toolbar->parent_handle;
			error = xwimp_get_window_outline(&outline);
			if (error) {
				LOG(("xwimp_get_window_outline: 0x%x: %s",
					error->errnum, error->errmess));
				warn_user("WimpError", error->errmess);
				return false;
			}
			width = outline.outline.x1 - outline.outline.x0 - 2;
		}
	}

	/*	Reformat the buttons starting with the throbber
	*/
	if ((width != old_width) || (toolbar->reformat_buttons)) {
		left_edge = 6;
		right_edge = width - 8;
		toolbar->height = 0;
		if ((toolbar->descriptor) && (toolbar->descriptor->theme) &&
				(toolbar->type == THEME_BROWSER_TOOLBAR) &&
				(toolbar->display_throbber)) {
			if (!toolbar->descriptor->throbber_right) {
				throbber_x = left_edge;
				left_edge += toolbar->descriptor->theme->throbber_width + 8;
			}
			toolbar->height = toolbar->descriptor->theme->throbber_height + 8;
		}
		if ((toolbar->type == THEME_BROWSER_TOOLBAR) &&	(toolbar->display_url)) {
			if (toolbar->height < 52 + 8)
				toolbar->height = 52 + 8;
			if ((toolbar->suggest) && (toolbar->height < (toolbar->suggest->height + 8)))
				toolbar->height = toolbar->suggest->height + 8;
		}

		/*	Get the minimum height of the icons
		*/
		bottom_edge = left_edge;
		if ((toolbar->display_buttons) && (toolbar->descriptor) &&
				(toolbar->descriptor->theme)) {
			toolbar_icon = toolbar->icon;
			while (toolbar_icon) {
				if (toolbar_icon->display) {
					bottom_edge += toolbar_icon->width;
					visible_icon = true;
					if ((toolbar_icon->height != 0) &&
						(toolbar->height < toolbar_icon->height + 8)) {
						toolbar->height = toolbar_icon->height + 8;
					}
				}
				toolbar_icon = toolbar_icon->next;
			}
			if (visible_icon) bottom_edge += 8;
		}

		/*	Check for minimum widths
		*/
		if (toolbar->type == THEME_BROWSER_TOOLBAR) {
			if (!toolbar->reformat_buttons) left_edge = bottom_edge;
			if (toolbar->display_url) {
				bottom_edge += 64;
				if (toolbar->suggest)
					bottom_edge += toolbar->suggest->width + 8;
			}
			if (bottom_edge > right_edge)
				right_edge = bottom_edge;
			if ((toolbar->descriptor) && (toolbar->descriptor->theme) &&
					(toolbar->display_throbber) &&
					(toolbar->descriptor->throbber_right)) {
				bottom_edge += toolbar->descriptor->theme->throbber_width;
				if (bottom_edge > right_edge) right_edge = bottom_edge;
				throbber_x = right_edge - toolbar->descriptor->theme->throbber_width;
				right_edge -= toolbar->descriptor->theme->throbber_width + 8;
			}
		}

		if (toolbar->reformat_buttons) {
			/*	Hide the URL bar if we should
			*/
			if (!toolbar->display_url) {
				if (!xwimp_get_caret_position(&caret)) {
					if ((caret.w == toolbar->toolbar_handle) &&
							(caret.i == ICON_TOOLBAR_URL))
						xwimp_set_caret_position((wimp_w)-1, 0, 0, 0, 0, 0);
				}
				xwimp_resize_icon(toolbar->toolbar_handle, ICON_TOOLBAR_URL,
					0, -16384, 0, -16384);
				xwimp_resize_icon(toolbar->toolbar_handle, ICON_TOOLBAR_SUGGEST,
					0, -16384, 0, -16384);
			}
			ro_gui_set_icon_shaded_state(toolbar->toolbar_handle, ICON_TOOLBAR_URL,
				!toolbar->display_url);
			xwimp_force_redraw(toolbar->toolbar_handle,
				0, 0, 16384, 16384);

			/*	Move the buttons
			*/
			toolbar_icon = toolbar->icon;
			while (toolbar_icon) {
				if ((toolbar->display_buttons) && (toolbar_icon->display)
					&& (toolbar_icon->width > 0)) {
					visible_icon = true;
					bottom_edge = (toolbar->height -
							toolbar_icon->height) / 2;
					toolbar_icon->x = left_edge;
					toolbar_icon->y = bottom_edge;
					xwimp_resize_icon(toolbar->toolbar_handle,
							toolbar_icon->icon_number,
							left_edge, bottom_edge,
							left_edge + toolbar_icon->width,
							bottom_edge + toolbar_icon->height);
					left_edge += toolbar_icon->width;
				} else {
					xwimp_resize_icon(toolbar->toolbar_handle,
							toolbar_icon->icon_number,
							0, -16384, 0, -16384);
				}
				toolbar_icon = toolbar_icon->next;
			}
			if (visible_icon) left_edge += 8;
		}


		if (toolbar->height != 0) toolbar->height += 2;
		if (toolbar->type == THEME_BROWSER_TOOLBAR) {
			/*	Move the URL bar
			*/
			if (toolbar->display_url) {
			  	if (toolbar->suggest) {
					xwimp_resize_icon(toolbar->toolbar_handle, ICON_TOOLBAR_URL,
						left_edge, (toolbar->height / 2) - 26,
						right_edge - toolbar->suggest->width - 8,
						(toolbar->height / 2) + 26);
					xwimp_resize_icon(toolbar->toolbar_handle, ICON_TOOLBAR_SUGGEST,
						right_edge - toolbar->suggest->width,
						(toolbar->height - toolbar->suggest->height) / 2,
						right_edge,
						(toolbar->height + toolbar->suggest->height) / 2);
					xwimp_force_redraw(toolbar->toolbar_handle,
						right_edge - toolbar->suggest->width - 8, 0,
						16384, 16384);
			  	} else {
					xwimp_resize_icon(toolbar->toolbar_handle, ICON_TOOLBAR_URL,
						left_edge, (toolbar->height / 2) - 26,
						right_edge, (toolbar->height / 2) + 26);
					xwimp_force_redraw(toolbar->toolbar_handle,
						right_edge, 0, 16384, 16384);
				}
				if (!xwimp_get_caret_position(&caret)) {
					if ((caret.w == toolbar->toolbar_handle) &&
							(caret.i == ICON_TOOLBAR_URL)) {
						xwimp_set_caret_position(toolbar->toolbar_handle,
								ICON_TOOLBAR_URL,
								caret.pos.x, caret.pos.y,
								-1, caret.index);
					}
				}
				ro_gui_redraw_icon(toolbar->toolbar_handle, ICON_TOOLBAR_URL);
			}

			/*	Move the throbber
			*/
			if ((toolbar->descriptor) && (toolbar->descriptor->theme) &&
					(throbber_x >= 0) && (toolbar->display_throbber)) {
				xwimp_resize_icon(toolbar->toolbar_handle, ICON_TOOLBAR_THROBBER,
					throbber_x, 0,
					throbber_x + toolbar->descriptor->theme->throbber_width,
					toolbar->height);
				if (toolbar->descriptor->throbber_right) {
					xwimp_force_redraw(toolbar->toolbar_handle,
						old_width - width + throbber_x, 0, 16384, 16384);
					xwimp_force_redraw(toolbar->toolbar_handle,
						throbber_x, 0, 16384, 16384);
				}

			} else {
				xwimp_resize_icon(toolbar->toolbar_handle, ICON_TOOLBAR_THROBBER,
					0, -16384, 0, -16384);
			}
		}

		/*	Re-attach to the parent
		*/
		toolbar->toolbar_current = width;
		if (toolbar->reformat_buttons) {
			extent.x1 = 16384;
			extent.y0 = 0;
			extent.y1 = toolbar->height;
			xwimp_set_extent(toolbar->toolbar_handle, &extent);
			if ((parent) && (old_height != toolbar->height)) {
				ro_gui_theme_attach_toolbar(toolbar, parent);
			}
		}
		toolbar->reformat_buttons = false;
	}

	/*	Reformat the status bar
	*/
	if ((toolbar->status_handle) && (parent)) {
		/*	Get the current state
		*/
		state.w = toolbar->status_handle;
		error = xwimp_get_window_state(&state);
		if (error) {
			LOG(("xwimp_get_window_state: 0x%x: %s",
				error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return false;
		}

		/*	Open or close the window
		*/
		if (!toolbar->display_status) {
			if (state.flags & wimp_WINDOW_OPEN)
				xwimp_close_window(toolbar->status_handle);
		} else {
			/*	Get the status bar height/width
			*/
			status_max = width - ro_get_vscroll_width(parent);
			status_size = (status_max * toolbar->status_width) / 10000;
			if (status_size < 12) status_size = 12;
			status_height = ro_get_hscroll_height(parent) - 2;

			/*	Update the extent
			*/
			extent.x1 = status_max;
			extent.y1 = status_height - 2;
			xwimp_set_extent(toolbar->status_handle, &extent);

			/*	Re-open the window
			*/
			state.w = toolbar->status_handle;
			state.xscroll = 0;
			state.yscroll = 0;
			state.next = wimp_TOP;
			state.visible.x0 = outline.outline.x0;
			state.visible.x1 = outline.outline.x0 + status_size;
			state.visible.y0 = outline.outline.y0 - status_height;
			state.visible.y1 = outline.outline.y0 - 2;
			xwimp_open_window_nested((wimp_open *)&state, parent,
					wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
							<< wimp_CHILD_XORIGIN_SHIFT |
					wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
							<< wimp_CHILD_YORIGIN_SHIFT |
					wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
							<< wimp_CHILD_LS_EDGE_SHIFT |
					wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
							<< wimp_CHILD_BS_EDGE_SHIFT |
					wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
							<< wimp_CHILD_RS_EDGE_SHIFT |
					wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT
							<< wimp_CHILD_TS_EDGE_SHIFT);

			/*	Resize and redraw the icons
			*/
			status_size = state.visible.x1 - state.visible.x0;
			if (status_size != toolbar->status_current) {
				xwimp_resize_icon(toolbar->status_handle, ICON_STATUS_TEXT,
						0, 0,
						status_size - 12, status_height - 2);
				xwimp_resize_icon(toolbar->status_handle, ICON_STATUS_RESIZE,
						status_size - 12, 0,
						status_size, status_height - 2);
				xwimp_force_redraw(toolbar->status_handle,
						toolbar->status_current - 12, 0,
						status_size - 12, status_height - 2);
				xwimp_force_redraw(toolbar->status_handle,
						status_size - 12, 0,
						status_size, status_height - 2);
				toolbar->status_current = status_size;
			}
		}
	}
	return true;
}


/**
 * Destroys a toolbar and frees any associated memory.
 *
 * \param toolbar     the toolbar to destroy
 */
void ro_gui_theme_destroy_toolbar(struct toolbar *toolbar) {
	struct toolbar_icon *icon;
	struct toolbar_icon *next_icon;
	if (!toolbar) return;

	/*	Delete our windows
	*/
	if (toolbar->toolbar_handle)
		xwimp_delete_window(toolbar->toolbar_handle);
	if (toolbar->status_handle)
		xwimp_delete_window(toolbar->status_handle);

	/*	Free the Wimp buffer (we only created one for them all)
	*/
	free(toolbar->url_buffer);

	/*	Free all the icons
	*/
	next_icon = toolbar->icon;
	while ((icon = next_icon) != NULL) {
		next_icon = icon->next;
		ro_gui_theme_destroy_toolbar_icon(icon);
	}
	ro_gui_theme_destroy_toolbar_icon(toolbar->suggest);
	free(toolbar);
}


/**
 * Adds a toolbar icon to the end of a toolbar
 *
 * \param toolbar      the toolbar to add the icon to the end of
 * \param name	       the icon name, or NULL for a separator
 * \param icon_number  the RISC OS Wimp icon number for the icon (not used for separators)
 */
struct toolbar_icon *ro_gui_theme_add_toolbar_icon(struct toolbar *toolbar, const char *name,
		int icon_number) {
	struct toolbar_icon *toolbar_icon;
	struct toolbar_icon *link_icon;

	/*	Separators are really a sprite called "separator"
	*/
	if (name == NULL) {
		name = "separator";
		icon_number = -1;
	}

	/*	Create a new toolbar
	*/
	toolbar_icon = calloc(sizeof(struct toolbar_icon), 1);
	if (!toolbar_icon) {
		LOG(("No memory for malloc()"));
		warn_user("NoMemory", 0);
		return NULL;
	}

	/*	Set up and link in the icon
	*/
	sprintf(toolbar_icon->name, name);
	sprintf(toolbar_icon->validation, "R5;S%s,p%s", name, name);
	toolbar_icon->icon_number = icon_number;
	toolbar_icon->display = true;
	if (toolbar) {
		if (!toolbar->icon) {
			toolbar->icon = toolbar_icon;
		} else {
			link_icon = toolbar->icon;
			while (link_icon->next) link_icon = link_icon->next;
			link_icon->next = toolbar_icon;
		}
	}
	return toolbar_icon;
}


/**
 * Updates a toolbar icon with respect to the associated sprite.
 *
 * \param icon	   the toolbar icon to update
 */
void ro_gui_theme_update_toolbar_icon(struct toolbar *toolbar, struct toolbar_icon *icon) {
	os_coord dimensions;
	os_mode mode;
	os_error *error;
	int default_width = 0;

	/*	Separators default to a width of 16
	*/
	if (icon->icon_number == -1) default_width = 16;

	/*	Handle no theme/no sprite area
	*/
	if (!toolbar) return;
	if ((!toolbar->descriptor) || (!toolbar->descriptor->theme) ||
			(!toolbar->descriptor->theme->sprite_area)) {
		icon->width = 0;
		icon->height = 0;
		return;
	}

	/*	Get the sprite details
	*/
	error = xosspriteop_read_sprite_info(osspriteop_USER_AREA,
			toolbar->descriptor->theme->sprite_area, (osspriteop_id)icon->name,
			&dimensions.x, &dimensions.y, 0, &mode);

  	/* fallback to user area just for 'gright' */
	if ((error) && (!strcmp(icon->name, "gright")))
		error = xwimpspriteop_read_sprite_info(icon->name,
				&dimensions.x, &dimensions.y, 0, &mode);
	if (error) {
		icon->width = default_width;
		icon->height = 0;
		if (error->errnum != error_SPRITE_OP_DOESNT_EXIST) {
			LOG(("xosspriteop_read_sprite_info: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("MiscError", error->errmess);
		}
		return;
	}

	/*	Store the details
	*/
	ro_convert_pixels_to_os_units(&dimensions, mode);
	icon->width = dimensions.x;
	icon->height = dimensions.y;
}


/**
 * Destroys a toolbar icon and frees any associated memory.
 * The icon is not removed from any linked list.
 *
 * \param icon	   the toolbar icon to destroy
 */
void ro_gui_theme_destroy_toolbar_icon(struct toolbar_icon *icon) {
	free(icon);
}


/**
 * Returns the toolbar icon at a specified position
 *
 * \param toolbar  the toolbar to examine
 * \param x	   the x co-ordinate to check
 * \param y	   the y co-ordinate to check
 * \return the toolbar icon at the specified position, or NULL for no icon
 */
struct toolbar_icon *ro_gui_theme_toolbar_get_icon(struct toolbar *toolbar, int x, int y) {
	struct toolbar_icon *icon;
	icon = toolbar->icon;
/* FINISH ME */
	return NULL;
}

/**
 * Returns whether a separator can follow the specified icon
 *
 * \param icon  the icon to check
 * \return whether a separator can follow
 */
bool ro_gui_theme_toolbar_separator_following(struct toolbar_icon *icon) {
	while (icon) {
		if (icon->display) return (icon->width > 0);
                icon = icon->next;
	}
	return false;
}