r4567: Get ATK headers through Zero Install.
[rox-filer/translations.git] / ROX-Filer / src / options.c
blob883010e7f46f07c4f38a1b8380f4ee644bb93ada
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* options.c - code for handling user choices */
22 /* How it works:
24 * On startup:
26 * - The <Choices>/PROJECT/Options file is read in, which contains a list of
27 * name/value pairs, and these are stored in the 'loading' hash table.
29 * - Each part of the filer then calls option_add_int(), or a related function,
30 * supplying the name for each option and a default value. Once an option is
31 * registered, it is removed from the loading table.
33 * - If things need to happen when values change, modules register with
34 * option_add_notify().
36 * - option_register_widget() can be used during initialisation (any time
37 * before the Options box is displayed) to tell the system how to render a
38 * particular type of option.
40 * - Finally, all notify callbacks are called. Use the Option->has_changed
41 * field to work out what has changed from the defaults.
43 * When the user opens the Options box:
45 * - The Options.xml file is read and used to create the Options dialog box.
46 * Each element in the file has a key corresponding to an option named
47 * above.
49 * - For each widget in the box, the current value of the option is used to
50 * set the widget's state.
52 * - All current values are saved for a possible Revert later.
54 * When the user changes an option or clicks on Revert:
56 * - The option values are updated.
58 * - All notify callbacks are called. Use the Option->has_changed field
59 * to see what changed.
61 * When OK is clicked:
63 * - If anything changed then:
64 * - All the options are written to the filesystem
65 * - The saver_callbacks are called.
68 #include "config.h"
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <errno.h>
74 #include <ctype.h>
75 #include <gtk/gtk.h>
76 #include <libxml/parser.h>
78 #include "global.h"
80 #include "choices.h"
81 #include "options.h"
82 #include "main.h"
83 #include "gui_support.h"
84 #include "support.h"
86 /* Add all option tooltips to this group */
87 static GtkTooltips *option_tooltips = NULL;
88 #define OPTION_TIP(widget, tip) \
89 gtk_tooltips_set_tip(option_tooltips, widget, tip, NULL)
91 /* The Options window. NULL if not yet created. */
92 static GtkWidget *window = NULL;
94 /* "filer_unique" -> (Option *) */
95 static GHashTable *option_hash = NULL;
97 /* A mapping (name -> value) for options which have been loaded by not
98 * yet registered. The options in this table cannot be used until
99 * option_add_*() is called to move them into option_hash.
101 static GHashTable *loading = NULL;
103 /* A mapping (XML name -> OptionBuildFn). When reading the Options.xml
104 * file, this table gives the function used to create the widgets.
106 static GHashTable *widget_builder = NULL;
108 /* A mapping (name -> GtkSizeGroup) of size groups used by the widgets
109 * in the options box. This hash table is created/destroyed every time
110 * the box is opened/destroyed.
112 static GHashTable *size_groups = NULL;
114 /* List of functions to call after all option values are updated */
115 static GList *notify_callbacks = NULL;
117 /* List of functions to call after all options are saved */
118 static GList *saver_callbacks = NULL;
120 static int updating_widgets = 0; /* Ignore change signals when set */
122 static GtkWidget *revert_widget = NULL;
124 /* Static prototypes */
125 static void save_options(void);
126 static void revert_options(GtkWidget *widget, gpointer data);
127 static void build_options_window(void);
128 static GtkWidget *build_window_frame(GtkTreeView **tree_view);
129 static void update_option_widgets(void);
130 static void button_patch_set_colour(GtkWidget *button, GdkColor *color);
131 static void option_add(Option *option, const gchar *key);
132 static void set_not_changed(gpointer key, gpointer value, gpointer data);
133 static void load_options(xmlDoc *doc);
134 static gboolean check_anything_changed(void);
135 static int get_int(xmlNode *node, guchar *attr);
136 static void may_add_tip(GtkWidget *widget, xmlNode *element);
137 static void add_to_size_group(xmlNode *node, GtkWidget *widget);
139 static const char *process_option_line(gchar *line);
141 static GList *build_label(Option *option, xmlNode *node, guchar *label);
142 static GList *build_spacer(Option *option, xmlNode *node, guchar *label);
143 static GList *build_frame(Option *option, xmlNode *node, guchar *label);
145 static GList *build_toggle(Option *option, xmlNode *node, guchar *label);
146 static GList *build_slider(Option *option, xmlNode *node, guchar *label);
147 static GList *build_entry(Option *option, xmlNode *node, guchar *label);
148 static GList *build_radio_group(Option *option, xmlNode *node, guchar *label);
149 static GList *build_colour(Option *option, xmlNode *node, guchar *label);
150 static GList *build_menu(Option *option, xmlNode *node, guchar *label);
151 static GList *build_font(Option *option, xmlNode *node, guchar *label);
152 static GList *build_numentry(Option *option, xmlNode *node, guchar *label);
153 static void update_numentry(Option *option);
154 static guchar *read_numentry(Option *option);
156 static gboolean updating_file_format = FALSE;
158 /****************************************************************
159 * EXTERNAL INTERFACE *
160 ****************************************************************/
162 void options_init(void)
164 char *path;
165 xmlDoc *doc;
167 loading = g_hash_table_new(g_str_hash, g_str_equal);
168 option_hash = g_hash_table_new(g_str_hash, g_str_equal);
169 widget_builder = g_hash_table_new(g_str_hash, g_str_equal);
171 path = choices_find_xdg_path_load("Options", PROJECT, SITE);
172 if (path)
174 /* Load in all the options set in the filer, storing them
175 * temporarily in the loading hash table.
176 * They get moved to option_hash when they're registered.
178 doc = xmlParseFile(path);
179 if (doc)
181 load_options(doc);
182 xmlFreeDoc(doc);
184 else
186 parse_file(path, process_option_line);
187 updating_file_format = TRUE;
190 g_free(path);
193 option_register_widget("label", build_label);
194 option_register_widget("spacer", build_spacer);
195 option_register_widget("frame", build_frame);
197 option_register_widget("toggle", build_toggle);
198 option_register_widget("slider", build_slider);
199 option_register_widget("entry", build_entry);
200 option_register_widget("numentry", build_numentry);
201 option_register_widget("radio-group", build_radio_group);
202 option_register_widget("colour", build_colour);
203 option_register_widget("menu", build_menu);
204 option_register_widget("font", build_font);
207 /* When parsing the XML file, process an element named 'name' by
208 * calling 'builder(option, xml_node, label)'.
209 * builder returns the new widgets to add to the options box.
210 * 'name' should be a static string. Call 'option_check_widget' when
211 * the widget's value is modified.
213 * Functions to set or get the widget's state can be stored in 'option'.
214 * If the option doesn't have a name attribute in Options.xml then
215 * ui will be NULL on entry (this is used for buttons).
217 void option_register_widget(char *name, OptionBuildFn builder)
219 g_hash_table_insert(widget_builder, name, builder);
222 /* This is called when the widget's value is modified by the user.
223 * Reads the new value of the widget into the option and calls
224 * the notify callbacks.
226 void option_check_widget(Option *option)
228 guchar *new = NULL;
230 if (updating_widgets)
231 return; /* Not caused by the user... */
233 g_return_if_fail(option->read_widget != NULL);
235 new = option->read_widget(option);
237 g_return_if_fail(new != NULL);
239 g_hash_table_foreach(option_hash, set_not_changed, NULL);
241 option->has_changed = strcmp(option->value, new) != 0;
243 if (!option->has_changed)
245 g_free(new);
246 return;
249 g_free(option->value);
250 option->value = new;
251 option->int_value = atoi(new);
253 options_notify();
256 /* Call all the notify callbacks. This should happen after any options
257 * have their values changed.
258 * Set each option->has_changed flag before calling this function.
260 void options_notify(void)
262 GList *next;
264 for (next = notify_callbacks; next; next = next->next)
266 OptionNotify *cb = (OptionNotify *) next->data;
268 cb();
271 if (updating_file_format)
273 updating_file_format = FALSE;
274 save_options();
275 info_message(_("ROX-Filer has converted your Options file "
276 "to the new XML format"));
279 if (revert_widget)
280 gtk_widget_set_sensitive(revert_widget,
281 check_anything_changed());
284 /* Store values used by Revert */
285 static void store_backup(gpointer key, gpointer value, gpointer data)
287 Option *option = (Option *) value;
289 g_free(option->backup);
290 option->backup = g_strdup(option->value);
293 /* Allow the user to edit the options. Returns the window widget (you don't
294 * normally need this). NULL if already open.
296 GtkWidget *options_show(void)
298 if (!option_tooltips)
299 option_tooltips = gtk_tooltips_new();
301 if (g_hash_table_size(loading) != 0)
303 g_print(PROJECT ": Some options loaded but not used:\n");
304 g_hash_table_foreach(loading, (GHFunc) puts, NULL);
307 if (window)
309 gtk_window_present(GTK_WINDOW(window));
310 return NULL;
313 g_hash_table_foreach(option_hash, store_backup, NULL);
315 build_options_window();
317 update_option_widgets();
319 gtk_widget_show_all(window);
321 return window;
324 /* Initialise and register a new integer option */
325 void option_add_int(Option *option, const gchar *key, int value)
327 option->value = g_strdup_printf("%d", value);
328 option->int_value = value;
329 option_add(option, key);
332 void option_add_string(Option *option, const gchar *key, const gchar *value)
334 option->value = g_strdup(value);
335 option->int_value = atoi(value);
336 option_add(option, key);
339 /* Add a callback which will be called after any options have changed their
340 * values. If several options change at once, this is called after all
341 * changes.
343 void option_add_notify(OptionNotify *callback)
345 g_return_if_fail(callback != NULL);
347 notify_callbacks = g_list_append(notify_callbacks, callback);
350 /* Call 'callback' after all the options have been saved */
351 void option_add_saver(OptionNotify *callback)
353 g_return_if_fail(callback != NULL);
355 saver_callbacks = g_list_append(saver_callbacks, callback);
358 /* Base class for building numentry widgets with particular ranges */
359 GList *build_numentry_base(Option *option, xmlNode *node,
360 guchar *label, GtkAdjustment *adj)
362 GtkWidget *hbox;
363 GtkWidget *spin;
364 GtkWidget *label_wid;
365 guchar *unit;
366 int width;
368 width = get_int(node, "width");
369 unit = xmlGetProp(node, "unit");
371 hbox = gtk_hbox_new(FALSE, 4);
373 if (label)
375 label_wid = gtk_label_new(_(label));
376 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
377 gtk_box_pack_start(GTK_BOX(hbox), label_wid, FALSE, TRUE, 0);
378 add_to_size_group(node, label_wid);
381 spin = gtk_spin_button_new(adj, adj->step_increment, 0);
382 gtk_entry_set_width_chars(GTK_ENTRY(spin),
383 width > 1 ? width + 1 : 2);
384 gtk_box_pack_start(GTK_BOX(hbox), spin, FALSE, TRUE, 0);
385 may_add_tip(spin, node);
387 if (unit)
389 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(unit)),
390 FALSE, TRUE, 0);
391 g_free(unit);
394 option->update_widget = update_numentry;
395 option->read_widget = read_numentry;
396 option->widget = spin;
398 g_signal_connect_swapped(spin, "value-changed",
399 G_CALLBACK(option_check_widget), option);
401 return g_list_append(NULL, hbox);
404 /****************************************************************
405 * INTERNAL FUNCTIONS *
406 ****************************************************************/
408 /* Option should contain the default value.
409 * It must never be destroyed after being registered (Options are typically
410 * statically allocated).
411 * The key corresponds to the option's name in Options.xml, and to the key
412 * in the saved options file.
414 * On exit, the value will have been updated to the loaded value, if
415 * different to the default.
417 static void option_add(Option *option, const gchar *key)
419 gpointer okey, value;
421 g_return_if_fail(option_hash != NULL);
422 g_return_if_fail(g_hash_table_lookup(option_hash, key) == NULL);
423 g_return_if_fail(option->value != NULL);
425 option->has_changed = FALSE;
427 option->widget = NULL;
428 option->update_widget = NULL;
429 option->read_widget = NULL;
430 option->backup = NULL;
432 g_hash_table_insert(option_hash, (gchar *) key, option);
434 /* Use the value loaded from the file, if any */
435 if (g_hash_table_lookup_extended(loading, key, &okey, &value))
437 option->has_changed = strcmp(option->value, value) != 0;
439 g_free(option->value);
440 option->value = value;
441 option->int_value = atoi(value);
442 g_hash_table_remove(loading, key);
443 g_free(okey);
447 static GtkColorSelectionDialog *current_csel_box = NULL;
448 static GtkFontSelectionDialog *current_fontsel_box = NULL;
450 static void get_new_colour(GtkWidget *ok, Option *option)
452 GtkWidget *csel;
453 GdkColor c;
455 g_return_if_fail(current_csel_box != NULL);
457 csel = current_csel_box->colorsel;
459 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(csel), &c);
461 button_patch_set_colour(option->widget, &c);
463 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
465 option_check_widget(option);
468 static void open_coloursel(GtkWidget *button, Option *option)
470 GtkColorSelectionDialog *csel;
471 GtkWidget *dialog, *patch;
473 if (current_csel_box)
474 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
476 dialog = gtk_color_selection_dialog_new(NULL);
477 csel = GTK_COLOR_SELECTION_DIALOG(dialog);
478 current_csel_box = csel;
479 gtk_window_set_position(GTK_WINDOW(csel), GTK_WIN_POS_MOUSE);
481 g_signal_connect(dialog, "destroy",
482 G_CALLBACK(gtk_widget_destroyed), &current_csel_box);
483 gtk_widget_hide(csel->help_button);
484 g_signal_connect_swapped(csel->cancel_button, "clicked",
485 G_CALLBACK(gtk_widget_destroy), dialog);
486 g_signal_connect(csel->ok_button, "clicked",
487 G_CALLBACK(get_new_colour), option);
489 patch = GTK_BIN(button)->child;
491 gtk_color_selection_set_current_color(
492 GTK_COLOR_SELECTION(csel->colorsel),
493 &patch->style->bg[GTK_STATE_NORMAL]);
495 gtk_widget_show(dialog);
498 static void font_chosen(GtkWidget *dialog, gint response, Option *option)
500 gchar *font;
502 if (response != GTK_RESPONSE_OK)
503 goto out;
505 font = gtk_font_selection_dialog_get_font_name(
506 GTK_FONT_SELECTION_DIALOG(dialog));
508 gtk_label_set_text(GTK_LABEL(option->widget), font);
510 g_free(font);
512 option_check_widget(option);
514 out:
515 gtk_widget_destroy(dialog);
519 static void toggle_active_font(GtkToggleButton *toggle, Option *option)
521 if (current_fontsel_box)
522 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
524 if (gtk_toggle_button_get_active(toggle))
526 gtk_widget_set_sensitive(option->widget->parent, TRUE);
527 gtk_label_set_text(GTK_LABEL(option->widget), "Sans 12");
529 else
531 gtk_widget_set_sensitive(option->widget->parent, FALSE);
532 gtk_label_set_text(GTK_LABEL(option->widget),
533 _("(use default)"));
536 option_check_widget(option);
539 static void open_fontsel(GtkWidget *button, Option *option)
541 if (current_fontsel_box)
542 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
544 current_fontsel_box = GTK_FONT_SELECTION_DIALOG(
545 gtk_font_selection_dialog_new(PROJECT));
547 gtk_window_set_position(GTK_WINDOW(current_fontsel_box),
548 GTK_WIN_POS_MOUSE);
550 g_signal_connect(current_fontsel_box, "destroy",
551 G_CALLBACK(gtk_widget_destroyed), &current_fontsel_box);
553 gtk_font_selection_dialog_set_font_name(current_fontsel_box,
554 option->value);
556 g_signal_connect(current_fontsel_box, "response",
557 G_CALLBACK(font_chosen), option);
559 gtk_widget_show(GTK_WIDGET(current_fontsel_box));
562 /* These are used during parsing... */
563 static xmlDocPtr options_doc = NULL;
565 #define DATA(node) (xmlNodeListGetString(options_doc, node->xmlChildrenNode, 1))
567 static void may_add_tip(GtkWidget *widget, xmlNode *element)
569 guchar *data, *tip;
571 data = DATA(element);
572 if (!data)
573 return;
575 tip = g_strstrip(g_strdup(data));
576 g_free(data);
577 if (*tip)
578 OPTION_TIP(widget, _(tip));
579 g_free(tip);
582 /* Returns zero if attribute is not present */
583 static int get_int(xmlNode *node, guchar *attr)
585 guchar *txt;
586 int retval;
588 txt = xmlGetProp(node, attr);
589 if (!txt)
590 return 0;
592 retval = atoi(txt);
593 g_free(txt);
595 return retval;
598 /* Adds 'widget' to the GtkSizeGroup selected by 'index'. This function
599 * does nothing if 'node' has no "sizegroup" attribute.
600 * The value of "sizegroup" is either a key. All widgets with the same
601 * key request the same size.
602 * Size groups are created on the fly and get destroyed when the options
603 * box is closed.
605 static void add_to_size_group(xmlNode *node, GtkWidget *widget)
607 GtkSizeGroup *sg;
608 guchar *name;
610 g_return_if_fail(node != NULL);
611 g_return_if_fail(widget != NULL);
613 name = xmlGetProp(node, "sizegroup");
614 if (!name)
615 return;
617 if (size_groups == NULL)
618 size_groups = g_hash_table_new_full(g_str_hash, g_str_equal,
619 g_free, NULL);
621 sg = (GtkSizeGroup *) g_hash_table_lookup(size_groups, name);
622 if (sg == NULL)
625 sg = (GtkSizeGroup *) gtk_size_group_new(
626 GTK_SIZE_GROUP_HORIZONTAL);
627 g_hash_table_insert(size_groups, name, sg);
628 gtk_size_group_add_widget(sg, widget);
629 g_object_unref(G_OBJECT(sg));
631 else
633 gtk_size_group_add_widget(sg, widget);
634 g_free(name);
638 static GtkWidget *build_radio(xmlNode *radio, GtkWidget *prev)
640 GtkWidget *button;
641 GtkRadioButton *prev_button = (GtkRadioButton *) prev;
642 guchar *label;
644 label = xmlGetProp(radio, "label");
646 button = gtk_radio_button_new_with_label(
647 prev_button ? gtk_radio_button_get_group(prev_button)
648 : NULL,
649 _(label));
650 g_free(label);
652 may_add_tip(button, radio);
654 g_object_set_data(G_OBJECT(button), "value",
655 xmlGetProp(radio, "value"));
657 return button;
660 static void build_menu_item(xmlNode *node, GtkWidget *menu)
662 GtkWidget *item;
663 guchar *label;
665 g_return_if_fail(strcmp(node->name, "item") == 0);
667 label = xmlGetProp(node, "label");
668 item = gtk_menu_item_new_with_label(_(label));
669 g_free(label);
671 gtk_widget_show(item);
672 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
674 g_object_set_data(G_OBJECT(item), "value", xmlGetProp(node, "value"));
677 static void build_widget(xmlNode *widget, GtkWidget *box)
679 const char *name = widget->name;
680 OptionBuildFn builder;
681 guchar *oname;
682 Option *option;
683 guchar *label;
685 label = xmlGetProp(widget, "label");
687 if (strcmp(name, "hbox") == 0 || strcmp(name, "vbox") == 0)
689 GtkWidget *nbox;
690 xmlNode *hw;
692 if (name[0] == 'h')
693 nbox = gtk_hbox_new(FALSE, 4);
694 else
695 nbox = gtk_vbox_new(FALSE, 0);
697 if (label)
698 gtk_box_pack_start(GTK_BOX(nbox),
699 gtk_label_new(_(label)), FALSE, TRUE, 4);
700 gtk_box_pack_start(GTK_BOX(box), nbox, FALSE, TRUE, 0);
702 for (hw = widget->xmlChildrenNode; hw; hw = hw->next)
704 if (hw->type == XML_ELEMENT_NODE)
705 build_widget(hw, nbox);
708 g_free(label);
709 return;
712 oname = xmlGetProp(widget, "name");
714 if (oname)
716 option = g_hash_table_lookup(option_hash, oname);
718 if (!option)
720 g_warning("No Option for '%s'!\n", oname);
721 g_free(oname);
722 return;
725 g_free(oname);
727 else
728 option = NULL;
730 builder = g_hash_table_lookup(widget_builder, name);
731 if (builder)
733 GList *widgets, *next;
735 if (option && option->widget)
736 g_warning("Widget for option already exists!");
738 widgets = builder(option, widget, label);
740 for (next = widgets; next; next = next->next)
742 GtkWidget *w = (GtkWidget *) next->data;
743 gtk_box_pack_start(GTK_BOX(box), w, FALSE, TRUE, 0);
745 g_list_free(widgets);
747 else
748 g_warning("Unknown option type '%s'\n", name);
750 g_free(label);
753 static void build_section(xmlNode *section, GtkWidget *notebook,
754 GtkTreeStore *tree_store, GtkTreeIter *parent)
756 guchar *title = NULL;
757 GtkWidget *page;
758 GtkTreeIter iter;
759 xmlNode *widget;
761 title = xmlGetProp(section, "title");
762 page = gtk_vbox_new(FALSE, 4);
763 gtk_container_set_border_width(GTK_CONTAINER(page), 4);
764 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page, NULL);
766 gtk_tree_store_append(tree_store, &iter, parent);
767 gtk_tree_store_set(tree_store, &iter, 0, _(title), 1, page, -1);
768 g_free(title);
770 widget = section->xmlChildrenNode;
771 for (; widget; widget = widget->next)
773 if (widget->type == XML_ELEMENT_NODE)
775 if (strcmp(widget->name, "section") == 0)
776 build_section(widget, notebook,
777 tree_store, &iter);
778 else
779 build_widget(widget, page);
784 /* Parse <app_dir>/Options.xml to create the options window.
785 * Sets the global 'window' variable.
787 static void build_options_window(void)
789 GtkTreeView *tree;
790 GtkTreeStore *store;
791 GtkWidget *notebook;
792 xmlDocPtr options_doc;
793 xmlNode *options, *section;
794 gchar *path;
796 notebook = build_window_frame(&tree);
798 path = g_strconcat(app_dir, "/Options.xml", NULL);
799 options_doc = xmlParseFile(path);
801 if (!options_doc)
803 report_error(_("Internal error: %s unreadable"), path);
804 g_free(path);
805 return;
808 g_free(path);
810 options = xmlDocGetRootElement(options_doc);
811 if (strcmp(options->name, "options") == 0)
813 GtkTreePath *treepath;
815 store = (GtkTreeStore *) gtk_tree_view_get_model(tree);
816 section = options->xmlChildrenNode;
817 for (; section; section = section->next)
818 if (section->type == XML_ELEMENT_NODE)
819 build_section(section, notebook, store, NULL);
821 gtk_tree_view_expand_all(tree);
822 treepath = gtk_tree_path_new_first();
823 if (treepath)
825 gtk_tree_view_set_cursor(tree, treepath, NULL, FALSE);
826 gtk_tree_path_free(treepath);
830 xmlFreeDoc(options_doc);
831 options_doc = NULL;
834 static void null_widget(gpointer key, gpointer value, gpointer data)
836 Option *option = (Option *) value;
838 g_return_if_fail(option->widget != NULL);
840 option->widget = NULL;
843 static void options_destroyed(GtkWidget *widget, gpointer data)
845 if (current_csel_box)
846 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
847 if (current_fontsel_box)
848 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
850 revert_widget = NULL;
852 if (check_anything_changed())
853 save_options();
855 if (widget == window)
857 window = NULL;
859 g_hash_table_foreach(option_hash, null_widget, NULL);
861 if (size_groups)
863 g_hash_table_destroy(size_groups);
864 size_groups = NULL;
870 /* The cursor has been changed in the tree view, so switch to the new
871 * page in the notebook.
873 static void tree_cursor_changed(GtkTreeView *tv, gpointer data)
875 GtkTreePath *path = NULL;
876 GtkNotebook *nbook = GTK_NOTEBOOK(data);
877 GtkTreeModel *model;
878 GtkWidget *page = NULL;
879 GtkTreeIter iter;
881 gtk_tree_view_get_cursor(tv, &path, NULL);
882 if (!path)
883 return;
885 model = gtk_tree_view_get_model(tv);
886 gtk_tree_model_get_iter(model, &iter, path);
887 gtk_tree_path_free(path);
888 gtk_tree_model_get(model, &iter, 1, &page, -1);
890 if (page)
892 gtk_notebook_set_current_page(nbook,
893 gtk_notebook_page_num(nbook, page));
894 g_object_unref(page);
898 /* Creates the window and adds the various buttons to it.
899 * Returns the notebook to add sections to and sets the global
900 * 'window'. If 'tree_view' is non-NULL, it stores the address
901 * of the tree view widget there.
903 static GtkWidget *build_window_frame(GtkTreeView **tree_view)
905 GtkWidget *notebook;
906 GtkWidget *tl_vbox, *hbox, *frame, *tv;
907 GtkWidget *actions, *button;
908 GtkTreeStore *model;
909 char *string, *save_path;
911 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
913 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
914 gtk_window_set_title(GTK_WINDOW(window), _("Options"));
915 g_signal_connect(window, "destroy",
916 G_CALLBACK(options_destroyed), NULL);
917 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
919 tl_vbox = gtk_vbox_new(FALSE, 4);
920 gtk_container_add(GTK_CONTAINER(window), tl_vbox);
922 hbox = gtk_hbox_new(FALSE, 4);
923 gtk_box_pack_start(GTK_BOX(tl_vbox), hbox, TRUE, TRUE, 0);
925 frame = gtk_frame_new(NULL);
926 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
927 gtk_box_pack_end(GTK_BOX(hbox), frame, TRUE, TRUE, 0);
929 notebook = gtk_notebook_new();
930 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
931 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
932 gtk_container_add(GTK_CONTAINER(frame), notebook);
934 frame = gtk_frame_new(NULL);
935 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
936 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
938 /* tree view */
939 model = gtk_tree_store_new(2, G_TYPE_STRING, GTK_TYPE_WIDGET);
940 tv = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
941 g_object_unref(model);
942 gtk_tree_selection_set_mode(
943 gtk_tree_view_get_selection(GTK_TREE_VIEW(tv)),
944 GTK_SELECTION_BROWSE);
945 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tv), FALSE);
946 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tv), -1,
947 NULL, gtk_cell_renderer_text_new(), "text", 0, NULL);
948 gtk_container_add(GTK_CONTAINER(frame), tv);
949 g_signal_connect(tv, "cursor_changed",
950 G_CALLBACK(tree_cursor_changed), notebook);
952 actions = gtk_hbutton_box_new();
953 gtk_button_box_set_layout(GTK_BUTTON_BOX(actions),
954 GTK_BUTTONBOX_END);
955 gtk_box_set_spacing(GTK_BOX(actions), 10);
957 gtk_box_pack_start(GTK_BOX(tl_vbox), actions, FALSE, TRUE, 0);
959 revert_widget = button_new_mixed(GTK_STOCK_UNDO, _("_Revert"));
960 GTK_WIDGET_SET_FLAGS(revert_widget, GTK_CAN_DEFAULT);
961 gtk_box_pack_start(GTK_BOX(actions), revert_widget, FALSE, TRUE, 0);
962 g_signal_connect(revert_widget, "clicked",
963 G_CALLBACK(revert_options), NULL);
964 gtk_tooltips_set_tip(option_tooltips, revert_widget,
965 _("Restore all choices to how they were when the "
966 "Options box was opened."), NULL);
967 gtk_widget_set_sensitive(revert_widget, check_anything_changed());
969 button = gtk_button_new_from_stock(GTK_STOCK_OK);
970 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
971 gtk_box_pack_start(GTK_BOX(actions), button, FALSE, TRUE, 0);
972 g_signal_connect_swapped(button, "clicked",
973 G_CALLBACK(gtk_widget_destroy), window);
974 gtk_widget_grab_default(button);
975 gtk_widget_grab_focus(button);
977 save_path = choices_find_xdg_path_save("...", PROJECT, SITE, FALSE);
978 if (save_path)
980 string = g_strdup_printf(_("Choices will be saved as:\n%s"),
981 save_path);
982 gtk_tooltips_set_tip(option_tooltips, button, string, NULL);
983 g_free(string);
984 g_free(save_path);
986 else
987 gtk_tooltips_set_tip(option_tooltips, button,
988 _("(saving disabled by CHOICESPATH)"), NULL);
990 if (tree_view)
991 *tree_view = GTK_TREE_VIEW(tv);
993 return notebook;
996 /* Given the last radio button in the group, select whichever
997 * radio button matches the given value.
999 static void radio_group_set_value(GtkRadioButton *last, guchar *value)
1001 GSList *next;
1003 for (next = gtk_radio_button_get_group(last); next; next = next->next)
1005 GtkToggleButton *button = (GtkToggleButton *) next->data;
1006 guchar *val;
1008 val = g_object_get_data(G_OBJECT(button), "value");
1009 g_return_if_fail(val != NULL);
1011 if (strcmp(val, value) == 0)
1013 gtk_toggle_button_set_active(button, TRUE);
1014 return;
1018 g_warning("Can't find radio button with value %s\n", value);
1021 /* Given the last radio button in the group, return a copy of the
1022 * value for the selected radio item.
1024 static guchar *radio_group_get_value(GtkRadioButton *last)
1026 GSList *next;
1028 for (next = gtk_radio_button_get_group(last); next; next = next->next)
1030 GtkToggleButton *button = (GtkToggleButton *) next->data;
1032 if (gtk_toggle_button_get_active(button))
1034 guchar *val;
1036 val = g_object_get_data(G_OBJECT(button), "value");
1037 g_return_val_if_fail(val != NULL, NULL);
1039 return g_strdup(val);
1043 return NULL;
1046 /* Select this item with this value */
1047 static void option_menu_set(GtkOptionMenu *om, guchar *value)
1049 GtkWidget *menu;
1050 GList *list, *next;
1051 int i = 0;
1053 menu = gtk_option_menu_get_menu(om);
1054 list = gtk_container_get_children(GTK_CONTAINER(menu));
1056 for (next = list; next; next = next->next)
1058 GObject *item = (GObject *) next->data;
1059 guchar *data;
1061 data = g_object_get_data(item, "value");
1062 g_return_if_fail(data != NULL);
1064 if (strcmp(data, value) == 0)
1066 gtk_option_menu_set_history(om, i);
1067 break;
1070 i++;
1073 g_list_free(list);
1076 /* Get the value (static) of the selected item */
1077 static guchar *option_menu_get(GtkOptionMenu *om)
1079 GtkWidget *menu, *item;
1081 menu = gtk_option_menu_get_menu(om);
1082 item = gtk_menu_get_active(GTK_MENU(menu));
1084 return g_object_get_data(G_OBJECT(item), "value");
1087 static void restore_backup(gpointer key, gpointer value, gpointer data)
1089 Option *option = (Option *) value;
1091 g_return_if_fail(option->backup != NULL);
1093 option->has_changed = strcmp(option->value, option->backup) != 0;
1094 if (!option->has_changed)
1095 return;
1097 g_free(option->value);
1098 option->value = g_strdup(option->backup);
1099 option->int_value = atoi(option->value);
1102 static void revert_options(GtkWidget *widget, gpointer data)
1104 g_hash_table_foreach(option_hash, restore_backup, NULL);
1105 options_notify();
1106 update_option_widgets();
1109 static void check_changed_cb(gpointer key, gpointer value, gpointer data)
1111 Option *option = (Option *) value;
1112 gboolean *changed = (gboolean *) data;
1114 g_return_if_fail(option->backup != NULL);
1116 if (*changed)
1117 return;
1119 if (strcmp(option->value, option->backup) != 0)
1120 *changed = TRUE;
1123 static gboolean check_anything_changed(void)
1125 gboolean retval = FALSE;
1127 g_hash_table_foreach(option_hash, check_changed_cb, &retval);
1129 return retval;
1132 static void write_option(gpointer key, gpointer value, gpointer data)
1134 xmlNodePtr doc = (xmlNodePtr) data;
1135 Option *option = (Option *) value;
1136 xmlNodePtr tree;
1138 tree = xmlNewTextChild(doc, NULL, "Option", option->value);
1139 xmlSetProp(tree, "name", (gchar *) key);
1142 static void save_options(void)
1144 xmlDoc *doc;
1145 GList *next;
1146 guchar *save, *save_new;
1148 save = choices_find_xdg_path_save("Options", PROJECT, SITE, TRUE);
1149 if (!save)
1150 goto out;
1152 save_new = g_strconcat(save, ".new", NULL);
1154 doc = xmlNewDoc("1.0");
1155 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "Options", NULL));
1157 g_hash_table_foreach(option_hash, write_option,
1158 xmlDocGetRootElement(doc));
1160 if (save_xml_file(doc, save_new) || rename(save_new, save))
1161 report_error(_("Error saving %s: %s"), save, g_strerror(errno));
1163 g_free(save_new);
1164 g_free(save);
1165 xmlFreeDoc(doc);
1167 for (next = saver_callbacks; next; next = next->next)
1169 OptionNotify *cb = (OptionNotify *) next->data;
1170 cb();
1173 out:
1174 if (window)
1175 gtk_widget_destroy(window);
1178 /* Make the widget reflect the current value of the option */
1179 static void update_cb(gpointer key, gpointer value, gpointer data)
1181 Option *option = (Option *) value;
1183 g_return_if_fail(option != NULL);
1184 if (option->widget == NULL) {
1185 g_warning("No widget for option %s", (char *) key);
1186 return;
1189 updating_widgets++;
1191 if (option->update_widget)
1192 option->update_widget(option);
1194 updating_widgets--;
1197 /* Reflect the values in the Option structures by changing the widgets
1198 * in the Options window.
1200 static void update_option_widgets(void)
1202 g_hash_table_foreach(option_hash, update_cb, NULL);
1205 /* Each of the following update the widget to make it show the current
1206 * value of the option.
1209 static void update_toggle(Option *option)
1211 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(option->widget),
1212 option->int_value);
1215 static void update_entry(Option *option)
1217 gtk_entry_set_text(GTK_ENTRY(option->widget), option->value);
1220 static void update_numentry(Option *option)
1222 gtk_spin_button_set_value(GTK_SPIN_BUTTON(option->widget),
1223 option->int_value);
1226 static void update_radio_group(Option *option)
1228 radio_group_set_value(GTK_RADIO_BUTTON(option->widget), option->value);
1231 static void update_slider(Option *option)
1233 gtk_adjustment_set_value(
1234 gtk_range_get_adjustment(GTK_RANGE(option->widget)),
1235 option->int_value);
1238 static void update_menu(Option *option)
1240 option_menu_set(GTK_OPTION_MENU(option->widget), option->value);
1243 static void update_font(Option *option)
1245 GtkToggleButton *active;
1246 gboolean have_font = option->value[0] != '\0';
1248 active = g_object_get_data(G_OBJECT(option->widget), "rox_override");
1250 if (active)
1252 gtk_toggle_button_set_active(active, have_font);
1253 gtk_widget_set_sensitive(option->widget->parent, have_font);
1256 gtk_label_set_text(GTK_LABEL(option->widget),
1257 have_font ? option->value
1258 : (guchar *) _("(use default)"));
1261 static void update_colour(Option *option)
1263 GdkColor colour;
1265 gdk_color_parse(option->value, &colour);
1266 button_patch_set_colour(option->widget, &colour);
1269 /* Each of these read_* calls get the new (string) value of an option
1270 * from the widget.
1273 static guchar *read_toggle(Option *option)
1275 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(option->widget);
1277 return g_strdup_printf("%d", gtk_toggle_button_get_active(toggle));
1280 static guchar *read_entry(Option *option)
1282 return gtk_editable_get_chars(GTK_EDITABLE(option->widget), 0, -1);
1285 static guchar *read_numentry(Option *option)
1287 return g_strdup_printf("%d", (int)
1288 gtk_spin_button_get_value(GTK_SPIN_BUTTON(option->widget)));
1291 static guchar *read_slider(Option *option)
1293 return g_strdup_printf("%d", (int)
1294 gtk_range_get_adjustment(GTK_RANGE(option->widget))->value);
1297 static guchar *read_radio_group(Option *option)
1299 return radio_group_get_value(GTK_RADIO_BUTTON(option->widget));
1302 static guchar *read_menu(Option *option)
1304 return g_strdup(option_menu_get(GTK_OPTION_MENU(option->widget)));
1307 static guchar *read_font(Option *option)
1309 GtkToggleButton *active;
1311 active = g_object_get_data(G_OBJECT(option->widget), "rox_override");
1312 if (active && !gtk_toggle_button_get_active(active))
1313 return g_strdup("");
1315 return g_strdup(gtk_label_get_text(GTK_LABEL(option->widget)));
1318 static guchar *read_colour(Option *option)
1320 GtkStyle *style = GTK_BIN(option->widget)->child->style;
1322 return g_strdup_printf("#%04x%04x%04x",
1323 style->bg[GTK_STATE_NORMAL].red,
1324 style->bg[GTK_STATE_NORMAL].green,
1325 style->bg[GTK_STATE_NORMAL].blue);
1328 static void set_not_changed(gpointer key, gpointer value, gpointer data)
1330 Option *option = (Option *) value;
1332 option->has_changed = FALSE;
1335 /* Builders for decorations (no corresponding option) */
1337 static GList *build_label(Option *option, xmlNode *node, guchar *label)
1339 GtkWidget *widget;
1340 guchar *text;
1341 int help;
1343 g_return_val_if_fail(option == NULL, NULL);
1344 g_return_val_if_fail(label == NULL, NULL);
1346 text = DATA(node);
1347 widget = gtk_label_new(_(text));
1348 g_free(text);
1350 help = get_int(node, "help");
1352 gtk_misc_set_alignment(GTK_MISC(widget), 0, help ? 0.5 : 1);
1353 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_LEFT);
1354 gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
1356 if (help)
1358 GtkWidget *hbox, *image, *align, *spacer;
1360 hbox = gtk_hbox_new(FALSE, 4);
1361 image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_INFO,
1362 GTK_ICON_SIZE_BUTTON);
1363 align = gtk_alignment_new(0, 0, 0, 0);
1365 gtk_container_add(GTK_CONTAINER(align), image);
1366 gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, TRUE, 0);
1367 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
1369 spacer = gtk_event_box_new();
1370 gtk_widget_set_size_request(spacer, 6, 6);
1372 return g_list_append(g_list_append(NULL, hbox), spacer);
1375 return g_list_append(NULL, widget);
1378 static GList *build_spacer(Option *option, xmlNode *node, guchar *label)
1380 GtkWidget *eb;
1382 g_return_val_if_fail(option == NULL, NULL);
1383 g_return_val_if_fail(label == NULL, NULL);
1385 eb = gtk_event_box_new();
1386 gtk_widget_set_size_request(eb, 12, 12);
1388 return g_list_append(NULL, eb);
1391 static GList *build_frame(Option *option, xmlNode *node, guchar *label)
1393 GtkWidget *nbox, *frame, *label_widget;
1394 xmlNode *hw;
1395 PangoAttrList *list;
1396 PangoAttribute *attr;
1398 g_return_val_if_fail(option == NULL, NULL);
1399 g_return_val_if_fail(label != NULL, NULL);
1401 frame = gtk_frame_new(_(label));
1402 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1404 /* Make the title bold */
1405 label_widget = gtk_frame_get_label_widget(GTK_FRAME(frame));
1406 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
1408 attr->start_index = 0;
1409 attr->end_index = -1;
1410 list = pango_attr_list_new();
1411 pango_attr_list_insert(list, attr);
1412 gtk_label_set_attributes(GTK_LABEL(label_widget), list);
1414 nbox = gtk_vbox_new(FALSE, 0);
1415 gtk_container_set_border_width(GTK_CONTAINER(nbox), 12);
1416 gtk_container_add(GTK_CONTAINER(frame), nbox);
1418 for (hw = node->xmlChildrenNode; hw; hw = hw->next)
1419 if (hw->type == XML_ELEMENT_NODE)
1420 build_widget(hw, nbox);
1422 return g_list_append(NULL, frame);
1425 /* These create new widgets in the options window and set the appropriate
1426 * callbacks.
1429 static GList *build_toggle(Option *option, xmlNode *node, guchar *label)
1431 GtkWidget *toggle;
1433 g_return_val_if_fail(option != NULL, NULL);
1435 toggle = gtk_check_button_new_with_label(_(label));
1437 may_add_tip(toggle, node);
1439 option->update_widget = update_toggle;
1440 option->read_widget = read_toggle;
1441 option->widget = toggle;
1443 g_signal_connect_swapped(toggle, "toggled",
1444 G_CALLBACK(option_check_widget), option);
1446 return g_list_append(NULL, toggle);
1449 static GList *build_slider(Option *option, xmlNode *node, guchar *label)
1451 GtkAdjustment *adj;
1452 GtkWidget *hbox, *slide, *label_wid;
1453 int min, max;
1454 int fixed;
1455 int showvalue;
1456 guchar *end;
1458 g_return_val_if_fail(option != NULL, NULL);
1460 min = get_int(node, "min");
1461 max = get_int(node, "max");
1462 fixed = get_int(node, "fixed");
1463 showvalue = get_int(node, "showvalue");
1465 adj = GTK_ADJUSTMENT(gtk_adjustment_new(0,
1466 min, max, 1, 10, 0));
1468 hbox = gtk_hbox_new(FALSE, 4);
1470 if (label)
1472 label_wid = gtk_label_new(_(label));
1473 gtk_misc_set_alignment(GTK_MISC(label_wid), 0, 0.5);
1474 gtk_box_pack_start(GTK_BOX(hbox), label_wid, FALSE, TRUE, 0);
1475 add_to_size_group(node, label_wid);
1478 end = xmlGetProp(node, "end");
1479 if (end)
1481 gtk_box_pack_end(GTK_BOX(hbox), gtk_label_new(_(end)),
1482 FALSE, TRUE, 0);
1483 g_free(end);
1486 slide = gtk_hscale_new(adj);
1488 if (fixed)
1489 gtk_widget_set_size_request(slide, adj->upper, 24);
1490 if (showvalue)
1492 gtk_scale_set_draw_value(GTK_SCALE(slide), TRUE);
1493 gtk_scale_set_value_pos(GTK_SCALE(slide),
1494 GTK_POS_LEFT);
1495 gtk_scale_set_digits(GTK_SCALE(slide), 0);
1497 else
1498 gtk_scale_set_draw_value(GTK_SCALE(slide), FALSE);
1499 GTK_WIDGET_UNSET_FLAGS(slide, GTK_CAN_FOCUS);
1501 may_add_tip(slide, node);
1503 gtk_box_pack_start(GTK_BOX(hbox), slide, !fixed, TRUE, 0);
1505 option->update_widget = update_slider;
1506 option->read_widget = read_slider;
1507 option->widget = slide;
1509 g_signal_connect_swapped(adj, "value-changed",
1510 G_CALLBACK(option_check_widget), option);
1512 return g_list_append(NULL, hbox);
1515 static GList *build_entry(Option *option, xmlNode *node, guchar *label)
1517 GtkWidget *hbox;
1518 GtkWidget *entry;
1519 GtkWidget *label_wid;
1521 g_return_val_if_fail(option != NULL, NULL);
1523 hbox = gtk_hbox_new(FALSE, 4);
1525 if (label)
1527 label_wid = gtk_label_new(_(label));
1528 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
1529 gtk_box_pack_start(GTK_BOX(hbox), label_wid, FALSE, TRUE, 0);
1532 entry = gtk_entry_new();
1533 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1534 add_to_size_group(node, entry);
1535 may_add_tip(entry, node);
1537 option->update_widget = update_entry;
1538 option->read_widget = read_entry;
1539 option->widget = entry;
1541 g_signal_connect_data(entry, "changed",
1542 G_CALLBACK(option_check_widget), option,
1543 NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED);
1545 return g_list_append(NULL, hbox);
1548 static GList *build_numentry(Option *option, xmlNode *node, guchar *label)
1550 GtkObject *adj;
1551 int min, max, step;
1553 g_return_val_if_fail(option != NULL, NULL);
1555 min = get_int(node, "min");
1556 max = get_int(node, "max");
1557 step = MAX(1, get_int(node, "step"));
1559 adj = gtk_adjustment_new(min, min, max, step, step * 10, 1);
1561 return build_numentry_base(option, node, label, GTK_ADJUSTMENT(adj));
1564 static GList *build_radio_group(Option *option, xmlNode *node, guchar *label)
1566 GList *list = NULL;
1567 GtkWidget *button = NULL;
1568 xmlNode *rn;
1569 int cols;
1571 g_return_val_if_fail(option != NULL, NULL);
1573 for (rn = node->xmlChildrenNode; rn; rn = rn->next)
1575 if (rn->type == XML_ELEMENT_NODE)
1577 button = build_radio(rn, button);
1578 g_signal_connect_swapped(button, "toggled",
1579 G_CALLBACK(option_check_widget), option);
1580 list = g_list_append(list, button);
1584 option->update_widget = update_radio_group;
1585 option->read_widget = read_radio_group;
1586 option->widget = button;
1588 cols = get_int(node, "columns");
1589 if (cols > 1)
1591 GtkWidget *table;
1592 GList *next;
1593 int i, n;
1594 int rows;
1596 n = g_list_length(list);
1597 rows = (n + cols - 1) / cols;
1599 table = gtk_table_new(rows, cols, FALSE);
1601 i = 0;
1602 for (next = list; next; next = next->next)
1604 GtkWidget *button = GTK_WIDGET(next->data);
1605 int left = i / rows;
1606 int top = i % rows;
1608 gtk_table_attach_defaults(GTK_TABLE(table), button,
1609 left, left + 1, top, top + 1);
1611 i++;
1614 g_list_free(list);
1615 list = g_list_prepend(NULL, table);
1618 return list;
1621 static GList *build_colour(Option *option, xmlNode *node, guchar *label)
1623 GtkWidget *hbox, *da, *button, *label_wid;
1625 g_return_val_if_fail(option != NULL, NULL);
1627 hbox = gtk_hbox_new(FALSE, 4);
1629 if (label)
1631 label_wid = gtk_label_new(_(label));
1632 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
1633 gtk_box_pack_start(GTK_BOX(hbox), label_wid, TRUE, TRUE, 0);
1636 button = gtk_button_new();
1637 da = gtk_drawing_area_new();
1638 gtk_widget_set_size_request(da, 64, 12);
1639 gtk_container_add(GTK_CONTAINER(button), da);
1640 g_signal_connect(button, "clicked", G_CALLBACK(open_coloursel), option);
1642 may_add_tip(button, node);
1644 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
1646 option->update_widget = update_colour;
1647 option->read_widget = read_colour;
1648 option->widget = button;
1650 return g_list_append(NULL, hbox);
1653 static GList *build_menu(Option *option, xmlNode *node, guchar *label)
1655 GtkWidget *hbox, *om, *option_menu, *label_wid;
1656 xmlNode *item;
1658 g_return_val_if_fail(option != NULL, NULL);
1660 hbox = gtk_hbox_new(FALSE, 4);
1662 label_wid = gtk_label_new(_(label));
1663 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
1664 gtk_box_pack_start(GTK_BOX(hbox), label_wid, TRUE, TRUE, 0);
1666 option_menu = gtk_option_menu_new();
1667 gtk_box_pack_start(GTK_BOX(hbox), option_menu, FALSE, TRUE, 0);
1669 om = gtk_menu_new();
1671 for (item = node->xmlChildrenNode; item; item = item->next)
1673 if (item->type == XML_ELEMENT_NODE)
1674 build_menu_item(item, om);
1677 gtk_widget_show(om);
1678 gtk_option_menu_set_menu(GTK_OPTION_MENU(option_menu), om);
1679 add_to_size_group(node, option_menu);
1681 option->update_widget = update_menu;
1682 option->read_widget = read_menu;
1683 option->widget = option_menu;
1685 g_signal_connect_data(option_menu, "changed",
1686 G_CALLBACK(option_check_widget), option,
1687 NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED);
1689 return g_list_append(NULL, hbox);
1692 static GList *build_font(Option *option, xmlNode *node, guchar *label)
1694 GtkWidget *hbox, *button;
1695 GtkWidget *active = NULL;
1696 int override;
1698 g_return_val_if_fail(option != NULL, NULL);
1700 override = get_int(node, "override");
1702 hbox = gtk_hbox_new(FALSE, 4);
1704 if (override)
1706 /* Add a check button to enable the font chooser. If off,
1707 * the option's value is "".
1709 active = gtk_check_button_new_with_label(_(label));
1710 gtk_box_pack_start(GTK_BOX(hbox), active, FALSE, TRUE, 0);
1711 g_signal_connect(active, "toggled",
1712 G_CALLBACK(toggle_active_font), option);
1714 else
1715 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1716 FALSE, TRUE, 0);
1718 button = gtk_button_new_with_label("");
1719 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
1721 option->update_widget = update_font;
1722 option->read_widget = read_font;
1723 option->widget = GTK_BIN(button)->child;
1724 may_add_tip(button, node);
1726 g_object_set_data(G_OBJECT(option->widget), "rox_override", active);
1728 g_signal_connect(button, "clicked", G_CALLBACK(open_fontsel), option);
1730 return g_list_append(NULL, hbox);
1733 static void button_patch_set_colour(GtkWidget *button, GdkColor *colour)
1735 GtkStyle *style;
1736 GtkWidget *patch;
1738 patch = GTK_BIN(button)->child;
1740 style = gtk_style_copy(GTK_WIDGET(patch)->style);
1741 style->bg[GTK_STATE_NORMAL].red = colour->red;
1742 style->bg[GTK_STATE_NORMAL].green = colour->green;
1743 style->bg[GTK_STATE_NORMAL].blue = colour->blue;
1744 gtk_widget_set_style(patch, style);
1745 g_object_unref(G_OBJECT(style));
1747 if (GTK_WIDGET_REALIZED(patch))
1748 gdk_window_clear(patch->window);
1751 static void load_options(xmlDoc *doc)
1753 xmlNode *root, *node;
1755 root = xmlDocGetRootElement(doc);
1757 g_return_if_fail(strcmp(root->name, "Options") == 0);
1759 for (node = root->xmlChildrenNode; node; node = node->next)
1761 gchar *value, *name;
1763 if (node->type != XML_ELEMENT_NODE)
1764 continue;
1765 if (strcmp(node->name, "Option") != 0)
1766 continue;
1767 name = xmlGetProp(node, "name");
1768 if (!name)
1769 continue;
1771 value = xmlNodeGetContent(node);
1773 if (g_hash_table_lookup(loading, name))
1774 g_warning("Duplicate option found!");
1776 g_hash_table_insert(loading, name, value);
1778 /* (don't need to free name or value) */
1782 /* Process one line from the options file (\0 term'd).
1783 * Returns NULL on success, or a pointer to an error message.
1784 * The line is modified.
1786 static const char *process_option_line(gchar *line)
1788 gchar *eq, *c;
1789 gchar *name = line;
1791 g_return_val_if_fail(option_hash != NULL, "No registered options!");
1793 eq = strchr(line, '=');
1794 if (!eq)
1795 return _("Missing '='");
1797 c = eq - 1;
1798 while (c > line && (*c == ' ' || *c == '\t'))
1799 c--;
1800 c[1] = '\0';
1801 c = eq + 1;
1802 while (*c == ' ' || *c == '\t')
1803 c++;
1805 if (g_hash_table_lookup(loading, name))
1806 return "Duplicate option found!";
1808 g_hash_table_insert(loading, g_strdup(name), g_strdup(g_strstrip(c)));
1810 return NULL;