r1996: Cope slightly better with invalid filenames in various places (reported by
[rox-filer.git] / ROX-Filer / src / options.c
blobc2134de34f9359f7158c1efbacabaeffa321c881
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, Thomas Leonard, <tal197@users.sourceforge.net>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* options.c - code for handling user choices */
24 /* How it works:
26 * On startup:
28 * - The <Choices>/PROJECT/Options file is read in, which contains a list of
29 * name/value pairs, and these are stored in the 'loading' hash table.
31 * - Each part of the filer then calls option_add_int(), or a related function,
32 * supplying the name for each option and a default value. Once an option is
33 * registered, it is removed from the loading table.
35 * - If things need to happen when values change, modules register with
36 * option_add_notify().
38 * - option_register_widget() can be used during initialisation (any time
39 * before the Options box is displayed) to tell the system how to render a
40 * particular type of option.
42 * - Finally, all notify callbacks are called. Use the Option->has_changed
43 * field to work out what has changed from the defaults.
45 * When the user opens the Options box:
47 * - The Options.xml file is read and used to create the Options dialog box.
48 * Each element in the file has a key corresponding to an option named
49 * above.
51 * - For each widget in the box, the current value of the option is used to
52 * set the widget's state.
54 * - All current values are saved for a possible Revert later.
56 * When the user changes an option or clicks on Revert:
58 * - The option values are updated.
60 * - All notify callbacks are called. Use the Option->has_changed field
61 * to see what changed.
63 * When OK is clicked:
65 * - If anything changed then:
66 * - All the options are written to the filesystem
67 * - The saver_callbacks are called.
70 #include "config.h"
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <errno.h>
76 #include <ctype.h>
77 #include <gtk/gtk.h>
78 #include <libxml/parser.h>
80 #include "global.h"
82 #include "choices.h"
83 #include "options.h"
84 #include "main.h"
85 #include "gui_support.h"
87 /* Add all option tooltips to this group */
88 static GtkTooltips *option_tooltips = NULL;
89 #define OPTION_TIP(widget, tip) \
90 gtk_tooltips_set_tip(option_tooltips, widget, tip, NULL)
92 /* The Options window. NULL if not yet created. */
93 static GtkWidget *window = NULL;
95 /* "filer_unique" -> (Option *) */
96 static GHashTable *option_hash = NULL;
98 /* A mapping (name -> value) for options which have been loaded by not
99 * yet registered. The options in this table cannot be used until
100 * option_add_*() is called to move them into option_hash.
102 static GHashTable *loading = NULL;
104 /* A mapping (XML name -> OptionBuildFn). When reading the Options.xml
105 * file, this table gives the function used to create the widgets.
107 static GHashTable *widget_builder = NULL;
109 /* A mapping (name -> GtkSizeGroup) of size groups used by the widgets
110 * in the options box. This hash table is created/destroyed every time
111 * the box is opened/destroyed.
113 static GHashTable *size_groups = NULL;
115 /* List of functions to call after all option values are updated */
116 static GList *notify_callbacks = NULL;
118 /* List of functions to call after all options are saved */
119 static GList *saver_callbacks = NULL;
121 static int updating_widgets = 0; /* Ignore change signals when set */
123 static GtkWidget *revert_widget = NULL;
125 /* Static prototypes */
126 static void save_options(void);
127 static void revert_options(GtkWidget *widget, gpointer data);
128 static void build_options_window(void);
129 static GtkWidget *build_window_frame(GtkTreeView **tree_view);
130 static void update_option_widgets(void);
131 static void button_patch_set_colour(GtkWidget *button, GdkColor *color);
132 static void option_add(Option *option, const gchar *key);
133 static void set_not_changed(gpointer key, gpointer value, gpointer data);
134 static void load_options(xmlDoc *doc);
135 static gboolean check_anything_changed(void);
137 static const char *process_option_line(gchar *line);
139 static GList *build_label(Option *option, xmlNode *node, guchar *label);
140 static GList *build_spacer(Option *option, xmlNode *node, guchar *label);
141 static GList *build_frame(Option *option, xmlNode *node, guchar *label);
143 static GList *build_toggle(Option *option, xmlNode *node, guchar *label);
144 static GList *build_slider(Option *option, xmlNode *node, guchar *label);
145 static GList *build_entry(Option *option, xmlNode *node, guchar *label);
146 static GList *build_numentry(Option *option, xmlNode *node, guchar *label);
147 static GList *build_radio_group(Option *option, xmlNode *node, guchar *label);
148 static GList *build_colour(Option *option, xmlNode *node, guchar *label);
149 static GList *build_menu(Option *option, xmlNode *node, guchar *label);
150 static GList *build_font(Option *option, xmlNode *node, guchar *label);
152 static gboolean updating_file_format = FALSE;
154 /****************************************************************
155 * EXTERNAL INTERFACE *
156 ****************************************************************/
158 void options_init(void)
160 char *path;
161 xmlDoc *doc;
163 loading = g_hash_table_new(g_str_hash, g_str_equal);
164 option_hash = g_hash_table_new(g_str_hash, g_str_equal);
165 widget_builder = g_hash_table_new(g_str_hash, g_str_equal);
167 path = choices_find_path_load("Options", PROJECT);
168 if (path)
170 /* Load in all the options set in the filer, storing them
171 * temporarily in the loading hash table.
172 * They get moved to option_hash when they're registered.
174 doc = xmlParseFile(path);
175 if (doc)
177 load_options(doc);
178 xmlFreeDoc(doc);
180 else
182 parse_file(path, process_option_line);
183 updating_file_format = TRUE;
186 g_free(path);
189 option_register_widget("label", build_label);
190 option_register_widget("spacer", build_spacer);
191 option_register_widget("frame", build_frame);
193 option_register_widget("toggle", build_toggle);
194 option_register_widget("slider", build_slider);
195 option_register_widget("entry", build_entry);
196 option_register_widget("numentry", build_numentry);
197 option_register_widget("radio-group", build_radio_group);
198 option_register_widget("colour", build_colour);
199 option_register_widget("menu", build_menu);
200 option_register_widget("font", build_font);
203 /* When parsing the XML file, process an element named 'name' by
204 * calling 'builder(option, xml_node, label)'.
205 * builder returns the new widgets to add to the options box.
206 * 'name' should be a static string. Call 'option_check_widget' when
207 * the widget's value is modified.
209 * Functions to set or get the widget's state can be stored in 'option'.
210 * If the option doesn't have a name attribute in Options.xml then
211 * ui will be NULL on entry (this is used for buttons).
213 void option_register_widget(char *name, OptionBuildFn builder)
215 g_hash_table_insert(widget_builder, name, builder);
218 /* This is called when the widget's value is modified by the user.
219 * Reads the new value of the widget into the option and calls
220 * the notify callbacks.
222 void option_check_widget(Option *option)
224 guchar *new = NULL;
226 if (updating_widgets)
227 return; /* Not caused by the user... */
229 g_return_if_fail(option->read_widget != NULL);
231 new = option->read_widget(option);
233 g_return_if_fail(new != NULL);
235 g_hash_table_foreach(option_hash, set_not_changed, NULL);
237 option->has_changed = strcmp(option->value, new) != 0;
239 if (!option->has_changed)
241 g_free(new);
242 return;
245 g_free(option->value);
246 option->value = new;
247 option->int_value = atoi(new);
249 options_notify();
252 /* Call all the notify callbacks. This should happen after any options
253 * have their values changed.
254 * Set each option->has_changed flag before calling this function.
256 void options_notify(void)
258 GList *next;
260 for (next = notify_callbacks; next; next = next->next)
262 OptionNotify *cb = (OptionNotify *) next->data;
264 cb();
267 if (updating_file_format)
269 updating_file_format = FALSE;
270 save_options();
271 info_message(_("ROX-Filer has converted your Options file "
272 "to the new XML format"));
275 if (revert_widget)
276 gtk_widget_set_sensitive(revert_widget,
277 check_anything_changed());
280 /* Store values used by Revert */
281 static void store_backup(gpointer key, gpointer value, gpointer data)
283 Option *option = (Option *) value;
285 g_free(option->backup);
286 option->backup = g_strdup(option->value);
289 /* Allow the user to edit the options. Returns the window widget (you don't
290 * normally need this). NULL if already open.
292 GtkWidget *options_show(void)
294 if (!option_tooltips)
295 option_tooltips = gtk_tooltips_new();
297 if (g_hash_table_size(loading) != 0)
299 g_printerr(PROJECT ": Some options loaded but not used:\n");
300 g_hash_table_foreach(loading, (GHFunc) puts, NULL);
303 if (window)
305 gtk_window_present(GTK_WINDOW(window));
306 return NULL;
309 g_hash_table_foreach(option_hash, store_backup, NULL);
311 build_options_window();
313 update_option_widgets();
315 gtk_widget_show_all(window);
317 return window;
320 /* Initialise and register a new integer option */
321 void option_add_int(Option *option, const gchar *key, int value)
323 option->value = g_strdup_printf("%d", value);
324 option->int_value = value;
325 option_add(option, key);
328 void option_add_string(Option *option, const gchar *key, const gchar *value)
330 option->value = g_strdup(value);
331 option->int_value = atoi(value);
332 option_add(option, key);
335 /* Add a callback which will be called after any options have changed their
336 * values. If several options change at once, this is called after all
337 * changes.
339 void option_add_notify(OptionNotify *callback)
341 g_return_if_fail(callback != NULL);
343 notify_callbacks = g_list_append(notify_callbacks, callback);
346 /* Call 'callback' after all the options have been saved */
347 void option_add_saver(OptionNotify *callback)
349 g_return_if_fail(callback != NULL);
351 saver_callbacks = g_list_append(saver_callbacks, callback);
354 /****************************************************************
355 * INTERNAL FUNCTIONS *
356 ****************************************************************/
358 /* Option should contain the default value.
359 * It must never be destroyed after being registered (Options are typically
360 * statically allocated).
361 * The key corresponds to the option's name in Options.xml, and to the key
362 * in the saved options file.
364 * On exit, the value will have been updated to the loaded value, if
365 * different to the default.
367 static void option_add(Option *option, const gchar *key)
369 gpointer okey, value;
371 g_return_if_fail(option_hash != NULL);
372 g_return_if_fail(g_hash_table_lookup(option_hash, key) == NULL);
373 g_return_if_fail(option->value != NULL);
375 option->has_changed = FALSE;
377 option->widget = NULL;
378 option->update_widget = NULL;
379 option->read_widget = NULL;
380 option->backup = NULL;
382 g_hash_table_insert(option_hash, (gchar *) key, option);
384 /* Use the value loaded from the file, if any */
385 if (g_hash_table_lookup_extended(loading, key, &okey, &value))
387 option->has_changed = strcmp(option->value, value) != 0;
389 g_free(option->value);
390 option->value = value;
391 option->int_value = atoi(value);
392 g_hash_table_remove(loading, key);
393 g_free(okey);
397 static GtkColorSelectionDialog *current_csel_box = NULL;
398 static GtkFontSelectionDialog *current_fontsel_box = NULL;
400 static void get_new_colour(GtkWidget *ok, Option *option)
402 GtkWidget *csel;
403 GdkColor c;
405 g_return_if_fail(current_csel_box != NULL);
407 csel = current_csel_box->colorsel;
409 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(csel), &c);
411 button_patch_set_colour(option->widget, &c);
413 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
415 option_check_widget(option);
418 static void open_coloursel(GtkWidget *button, Option *option)
420 GtkColorSelectionDialog *csel;
421 GtkWidget *dialog, *patch;
423 if (current_csel_box)
424 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
426 dialog = gtk_color_selection_dialog_new(NULL);
427 csel = GTK_COLOR_SELECTION_DIALOG(dialog);
428 current_csel_box = csel;
429 gtk_window_set_position(GTK_WINDOW(csel), GTK_WIN_POS_MOUSE);
431 g_signal_connect(dialog, "destroy",
432 G_CALLBACK(gtk_widget_destroyed), &current_csel_box);
433 gtk_widget_hide(csel->help_button);
434 g_signal_connect_swapped(csel->cancel_button, "clicked",
435 G_CALLBACK(gtk_widget_destroy), dialog);
436 g_signal_connect(csel->ok_button, "clicked",
437 G_CALLBACK(get_new_colour), option);
439 patch = GTK_BIN(button)->child;
441 gtk_color_selection_set_current_color(
442 GTK_COLOR_SELECTION(csel->colorsel),
443 &patch->style->bg[GTK_STATE_NORMAL]);
445 gtk_widget_show(dialog);
448 static void font_chosen(GtkWidget *dialog, gint response, Option *option)
450 gchar *font;
452 if (response != GTK_RESPONSE_OK)
453 goto out;
455 font = gtk_font_selection_dialog_get_font_name(
456 GTK_FONT_SELECTION_DIALOG(dialog));
458 gtk_label_set_text(GTK_LABEL(option->widget), font);
460 g_free(font);
462 option_check_widget(option);
464 out:
465 gtk_widget_destroy(dialog);
469 static void toggle_active_font(GtkToggleButton *toggle, Option *option)
471 if (current_fontsel_box)
472 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
474 if (gtk_toggle_button_get_active(toggle))
476 gtk_widget_set_sensitive(option->widget->parent, TRUE);
477 gtk_label_set_text(GTK_LABEL(option->widget), "Sans 12");
479 else
481 gtk_widget_set_sensitive(option->widget->parent, FALSE);
482 gtk_label_set_text(GTK_LABEL(option->widget),
483 _("(use default)"));
486 option_check_widget(option);
489 static void open_fontsel(GtkWidget *button, Option *option)
491 if (current_fontsel_box)
492 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
494 current_fontsel_box = GTK_FONT_SELECTION_DIALOG(
495 gtk_font_selection_dialog_new(PROJECT));
497 gtk_window_set_position(GTK_WINDOW(current_fontsel_box),
498 GTK_WIN_POS_MOUSE);
500 g_signal_connect(current_fontsel_box, "destroy",
501 G_CALLBACK(gtk_widget_destroyed), &current_fontsel_box);
503 gtk_font_selection_dialog_set_font_name(current_fontsel_box,
504 option->value);
506 g_signal_connect(current_fontsel_box, "response",
507 G_CALLBACK(font_chosen), option);
509 gtk_widget_show(GTK_WIDGET(current_fontsel_box));
512 /* These are used during parsing... */
513 static xmlDocPtr options_doc = NULL;
515 #define DATA(node) (xmlNodeListGetString(options_doc, node->xmlChildrenNode, 1))
517 static void may_add_tip(GtkWidget *widget, xmlNode *element)
519 guchar *data, *tip;
521 data = DATA(element);
522 if (!data)
523 return;
525 tip = g_strstrip(g_strdup(data));
526 g_free(data);
527 if (*tip)
528 OPTION_TIP(widget, _(tip));
529 g_free(tip);
532 /* Returns zero if attribute is not present */
533 static int get_int(xmlNode *node, guchar *attr)
535 guchar *txt;
536 int retval;
538 txt = xmlGetProp(node, attr);
539 if (!txt)
540 return 0;
542 retval = atoi(txt);
543 g_free(txt);
545 return retval;
548 /* Adds 'widget' to the GtkSizeGroup selected by 'index'. This function
549 * does nothing if 'node' has no "sizegroup" attribute.
550 * The value of "sizegroup" is either a key. All widgets with the same
551 * key request the same size.
552 * Size groups are created on the fly and get destroyed when the options
553 * box is closed.
555 static void add_to_size_group(xmlNode *node, GtkWidget *widget)
557 GtkSizeGroup *sg;
558 guchar *name;
560 g_return_if_fail(node != NULL);
561 g_return_if_fail(widget != NULL);
563 name = xmlGetProp(node, "sizegroup");
564 if (!name)
565 return;
567 if (size_groups == NULL)
568 size_groups = g_hash_table_new_full(g_str_hash, g_str_equal,
569 g_free, NULL);
571 sg = (GtkSizeGroup *) g_hash_table_lookup(size_groups, name);
572 if (sg == NULL)
575 sg = (GtkSizeGroup *) gtk_size_group_new(
576 GTK_SIZE_GROUP_HORIZONTAL);
577 g_hash_table_insert(size_groups, name, sg);
578 gtk_size_group_add_widget(sg, widget);
579 g_object_unref(G_OBJECT(sg));
581 else
583 gtk_size_group_add_widget(sg, widget);
584 g_free(name);
588 static GtkWidget *build_radio(xmlNode *radio, GtkWidget *prev)
590 GtkWidget *button;
591 GtkRadioButton *prev_button = (GtkRadioButton *) prev;
592 guchar *label;
594 label = xmlGetProp(radio, "label");
596 button = gtk_radio_button_new_with_label(
597 prev_button ? gtk_radio_button_get_group(prev_button)
598 : NULL,
599 _(label));
600 g_free(label);
602 may_add_tip(button, radio);
604 g_object_set_data(G_OBJECT(button), "value",
605 xmlGetProp(radio, "value"));
607 return button;
610 static void build_menu_item(xmlNode *node, GtkWidget *option_menu)
612 GtkWidget *item, *menu;
613 guchar *label;
615 g_return_if_fail(strcmp(node->name, "item") == 0);
617 label = xmlGetProp(node, "label");
618 item = gtk_menu_item_new_with_label(_(label));
619 g_free(label);
621 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(option_menu));
622 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
623 gtk_widget_show_all(menu);
625 g_object_set_data(G_OBJECT(item), "value", xmlGetProp(node, "value"));
628 static void build_widget(xmlNode *widget, GtkWidget *box)
630 const char *name = widget->name;
631 OptionBuildFn builder;
632 guchar *oname;
633 Option *option;
634 guchar *label;
636 label = xmlGetProp(widget, "label");
638 if (strcmp(name, "hbox") == 0 || strcmp(name, "vbox") == 0)
640 GtkWidget *nbox;
641 xmlNode *hw;
643 if (name[0] == 'h')
644 nbox = gtk_hbox_new(FALSE, 4);
645 else
646 nbox = gtk_vbox_new(FALSE, 0);
648 if (label)
649 gtk_box_pack_start(GTK_BOX(nbox),
650 gtk_label_new(_(label)), FALSE, TRUE, 4);
651 gtk_box_pack_start(GTK_BOX(box), nbox, FALSE, TRUE, 0);
653 for (hw = widget->xmlChildrenNode; hw; hw = hw->next)
655 if (hw->type == XML_ELEMENT_NODE)
656 build_widget(hw, nbox);
659 g_free(label);
660 return;
663 oname = xmlGetProp(widget, "name");
665 if (oname)
667 option = g_hash_table_lookup(option_hash, oname);
669 if (!option)
671 g_warning("No Option for '%s'!\n", oname);
672 g_free(oname);
673 return;
676 g_free(oname);
678 else
679 option = NULL;
681 builder = g_hash_table_lookup(widget_builder, name);
682 if (builder)
684 GList *widgets, *next;
686 if (option && option->widget)
687 g_warning("Widget for option already exists!");
689 widgets = builder(option, widget, label);
691 for (next = widgets; next; next = next->next)
693 GtkWidget *w = (GtkWidget *) next->data;
694 gtk_box_pack_start(GTK_BOX(box), w, FALSE, TRUE, 0);
696 g_list_free(widgets);
698 else
699 g_warning("Unknown option type '%s'\n", name);
701 g_free(label);
704 static void build_section(xmlNode *section, GtkWidget *notebook,
705 GtkTreeStore *tree_store, GtkTreeIter *parent)
707 guchar *title = NULL;
708 GtkWidget *page;
709 GtkTreeIter iter;
710 xmlNode *widget;
712 title = xmlGetProp(section, "title");
713 page = gtk_vbox_new(FALSE, 4);
714 gtk_container_set_border_width(GTK_CONTAINER(page), 4);
715 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page, NULL);
717 gtk_tree_store_append(tree_store, &iter, parent);
718 gtk_tree_store_set(tree_store, &iter, 0, _(title), 1, page, -1);
719 g_free(title);
721 widget = section->xmlChildrenNode;
722 for (; widget; widget = widget->next)
724 if (widget->type == XML_ELEMENT_NODE)
726 if (strcmp(widget->name, "section") == 0)
727 build_section(widget, notebook,
728 tree_store, &iter);
729 else
730 build_widget(widget, page);
735 /* Parse <app_dir>/Options.xml to create the options window.
736 * Sets the global 'window' variable.
738 static void build_options_window(void)
740 GtkTreeView *tree;
741 GtkTreeStore *store;
742 GtkWidget *notebook;
743 xmlDocPtr options_doc;
744 xmlNode *options, *section;
745 gchar *path;
747 notebook = build_window_frame(&tree);
749 path = g_strconcat(app_dir, "/Options.xml", NULL);
750 options_doc = xmlParseFile(path);
752 if (!options_doc)
754 report_error(_("Internal error: %s unreadable"), path);
755 g_free(path);
756 return;
759 g_free(path);
761 options = xmlDocGetRootElement(options_doc);
762 if (strcmp(options->name, "options") == 0)
764 GtkTreePath *treepath;
766 store = (GtkTreeStore *) gtk_tree_view_get_model(tree);
767 section = options->xmlChildrenNode;
768 for (; section; section = section->next)
769 if (section->type == XML_ELEMENT_NODE)
770 build_section(section, notebook, store, NULL);
772 gtk_tree_view_expand_all(tree);
773 treepath = gtk_tree_path_new_first();
774 if (treepath)
776 gtk_tree_view_set_cursor(tree, treepath, NULL, FALSE);
777 gtk_tree_path_free(treepath);
781 xmlFreeDoc(options_doc);
782 options_doc = NULL;
785 static void null_widget(gpointer key, gpointer value, gpointer data)
787 Option *option = (Option *) value;
789 g_return_if_fail(option->widget != NULL);
791 option->widget = NULL;
794 static void options_destroyed(GtkWidget *widget, gpointer data)
796 if (current_csel_box)
797 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
798 if (current_fontsel_box)
799 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
801 revert_widget = NULL;
803 if (check_anything_changed())
804 save_options();
806 if (widget == window)
808 window = NULL;
810 g_hash_table_foreach(option_hash, null_widget, NULL);
812 if (size_groups)
814 g_hash_table_destroy(size_groups);
815 size_groups = NULL;
821 /* The cursor has been changed in the tree view, so switch to the new
822 * page in the notebook.
824 static void tree_cursor_changed(GtkTreeView *tv, gpointer data)
826 GtkTreePath *path = NULL;
827 GtkNotebook *nbook = GTK_NOTEBOOK(data);
828 GtkTreeModel *model;
829 GtkWidget *page = NULL;
830 GtkTreeIter iter;
832 gtk_tree_view_get_cursor(tv, &path, NULL);
833 if (!path)
834 return;
836 model = gtk_tree_view_get_model(tv);
837 gtk_tree_model_get_iter(model, &iter, path);
838 gtk_tree_path_free(path);
839 gtk_tree_model_get(model, &iter, 1, &page, -1);
841 if (page)
842 gtk_notebook_set_current_page(nbook,
843 gtk_notebook_page_num(nbook, page));
846 /* Creates the window and adds the various buttons to it.
847 * Returns the notebook to add sections to and sets the global
848 * 'window'. If 'tree_view' is non-NULL, it stores the address
849 * of the tree view widget there.
851 static GtkWidget *build_window_frame(GtkTreeView **tree_view)
853 GtkWidget *notebook;
854 GtkWidget *tl_vbox, *hbox, *sw, *tv;
855 GtkWidget *actions, *button, *frame;
856 GtkTreeStore *model;
857 char *string, *save_path;
859 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
861 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
862 gtk_window_set_title(GTK_WINDOW(window), _("Options"));
863 g_signal_connect(window, "destroy",
864 G_CALLBACK(options_destroyed), NULL);
865 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
867 tl_vbox = gtk_vbox_new(FALSE, 4);
868 gtk_container_add(GTK_CONTAINER(window), tl_vbox);
870 hbox = gtk_hbox_new(FALSE, 4);
871 gtk_box_pack_start(GTK_BOX(tl_vbox), hbox, TRUE, TRUE, 0);
873 frame = gtk_frame_new(NULL);
874 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
875 gtk_box_pack_end(GTK_BOX(hbox), frame, TRUE, TRUE, 0);
877 notebook = gtk_notebook_new();
878 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
879 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
880 gtk_container_add(GTK_CONTAINER(frame), notebook);
882 /* scrolled window for the tree view */
883 sw = gtk_scrolled_window_new(NULL, NULL);
884 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
885 GTK_SHADOW_IN);
886 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
887 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
888 gtk_box_pack_start(GTK_BOX(hbox), sw, FALSE, TRUE, 0);
890 /* tree view */
891 model = gtk_tree_store_new(2, G_TYPE_STRING, GTK_TYPE_WIDGET);
892 tv = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
893 g_object_unref(model);
894 gtk_tree_selection_set_mode(
895 gtk_tree_view_get_selection(GTK_TREE_VIEW(tv)),
896 GTK_SELECTION_BROWSE);
897 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tv), FALSE);
898 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tv), -1,
899 NULL, gtk_cell_renderer_text_new(), "text", 0, NULL);
900 gtk_container_add(GTK_CONTAINER(sw), tv);
901 g_signal_connect(tv, "cursor_changed",
902 G_CALLBACK(tree_cursor_changed), notebook);
904 actions = gtk_hbutton_box_new();
905 gtk_button_box_set_layout(GTK_BUTTON_BOX(actions),
906 GTK_BUTTONBOX_END);
907 gtk_box_set_spacing(GTK_BOX(actions), 10);
909 gtk_box_pack_start(GTK_BOX(tl_vbox), actions, FALSE, TRUE, 0);
911 revert_widget = button_new_mixed(GTK_STOCK_UNDO, _("_Revert"));
912 GTK_WIDGET_SET_FLAGS(revert_widget, GTK_CAN_DEFAULT);
913 gtk_box_pack_start(GTK_BOX(actions), revert_widget, FALSE, TRUE, 0);
914 g_signal_connect(revert_widget, "clicked",
915 G_CALLBACK(revert_options), NULL);
916 gtk_tooltips_set_tip(option_tooltips, revert_widget,
917 _("Restore all choices to how they were when the "
918 "Options box was opened."), NULL);
919 gtk_widget_set_sensitive(revert_widget, check_anything_changed());
921 button = gtk_button_new_from_stock(GTK_STOCK_OK);
922 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
923 gtk_box_pack_start(GTK_BOX(actions), button, FALSE, TRUE, 0);
924 g_signal_connect_swapped(button, "clicked",
925 G_CALLBACK(gtk_widget_destroy), window);
926 gtk_widget_grab_default(button);
927 gtk_widget_grab_focus(button);
929 save_path = choices_find_path_save("...", PROJECT, FALSE);
930 if (save_path)
932 string = g_strdup_printf(_("Choices will be saved as:\n%s"),
933 save_path);
934 gtk_tooltips_set_tip(option_tooltips, button, string, NULL);
935 g_free(string);
936 g_free(save_path);
938 else
939 gtk_tooltips_set_tip(option_tooltips, button,
940 _("(saving disabled by CHOICESPATH)"), NULL);
942 if (tree_view)
943 *tree_view = GTK_TREE_VIEW(tv);
945 return notebook;
948 /* Given the last radio button in the group, select whichever
949 * radio button matches the given value.
951 static void radio_group_set_value(GtkRadioButton *last, guchar *value)
953 GSList *next;
955 for (next = gtk_radio_button_get_group(last); next; next = next->next)
957 GtkToggleButton *button = (GtkToggleButton *) next->data;
958 guchar *val;
960 val = g_object_get_data(G_OBJECT(button), "value");
961 g_return_if_fail(val != NULL);
963 if (strcmp(val, value) == 0)
965 gtk_toggle_button_set_active(button, TRUE);
966 return;
970 g_warning("Can't find radio button with value %s\n", value);
973 /* Given the last radio button in the group, return a copy of the
974 * value for the selected radio item.
976 static guchar *radio_group_get_value(GtkRadioButton *last)
978 GSList *next;
980 for (next = gtk_radio_button_get_group(last); next; next = next->next)
982 GtkToggleButton *button = (GtkToggleButton *) next->data;
984 if (gtk_toggle_button_get_active(button))
986 guchar *val;
988 val = g_object_get_data(G_OBJECT(button), "value");
989 g_return_val_if_fail(val != NULL, NULL);
991 return g_strdup(val);
995 return NULL;
998 /* Select this item with this value */
999 static void option_menu_set(GtkOptionMenu *om, guchar *value)
1001 GtkWidget *menu;
1002 GList *list, *next;
1003 int i = 0;
1005 menu = gtk_option_menu_get_menu(om);
1006 list = gtk_container_get_children(GTK_CONTAINER(menu));
1008 for (next = list; next; next = next->next)
1010 GObject *item = (GObject *) next->data;
1011 guchar *data;
1013 data = g_object_get_data(item, "value");
1014 g_return_if_fail(data != NULL);
1016 if (strcmp(data, value) == 0)
1018 gtk_option_menu_set_history(om, i);
1019 break;
1022 i++;
1025 g_list_free(list);
1028 /* Get the value (static) of the selected item */
1029 static guchar *option_menu_get(GtkOptionMenu *om)
1031 GtkWidget *menu, *item;
1033 menu = gtk_option_menu_get_menu(om);
1034 item = gtk_menu_get_active(GTK_MENU(menu));
1036 return g_object_get_data(G_OBJECT(item), "value");
1039 static void restore_backup(gpointer key, gpointer value, gpointer data)
1041 Option *option = (Option *) value;
1043 g_return_if_fail(option->backup != NULL);
1045 option->has_changed = strcmp(option->value, option->backup) != 0;
1046 if (!option->has_changed)
1047 return;
1049 g_free(option->value);
1050 option->value = g_strdup(option->backup);
1051 option->int_value = atoi(option->value);
1054 static void revert_options(GtkWidget *widget, gpointer data)
1056 g_hash_table_foreach(option_hash, restore_backup, NULL);
1057 options_notify();
1058 update_option_widgets();
1061 static void check_changed_cb(gpointer key, gpointer value, gpointer data)
1063 Option *option = (Option *) value;
1064 gboolean *changed = (gboolean *) data;
1066 g_return_if_fail(option->backup != NULL);
1068 if (*changed)
1069 return;
1071 if (strcmp(option->value, option->backup) != 0)
1072 *changed = TRUE;
1075 static gboolean check_anything_changed(void)
1077 gboolean retval = FALSE;
1079 g_hash_table_foreach(option_hash, check_changed_cb, &retval);
1081 return retval;
1084 static void write_option(gpointer key, gpointer value, gpointer data)
1086 xmlNodePtr doc = (xmlNodePtr) data;
1087 Option *option = (Option *) value;
1088 xmlNodePtr tree;
1090 tree = xmlNewTextChild(doc, NULL, "Option", option->value);
1091 xmlSetProp(tree, "name", (gchar *) key);
1094 /* Save doc as XML as filename, 0 on success or -1 on failure */
1095 static int save_xml_file(xmlDocPtr doc, gchar *filename)
1097 #if LIBXML_VERSION > 20400
1098 if (xmlSaveFormatFileEnc(filename, doc, NULL, 1) < 0)
1099 return 1;
1100 #else
1101 FILE *out;
1103 out = fopen(filename, "w");
1104 if (!out)
1105 return 1;
1107 xmlDocDump(out, doc); /* Some versions return void */
1109 if (fclose(out))
1110 return 1;
1111 #endif
1113 return 0;
1116 static void save_options(void)
1118 xmlDoc *doc;
1119 GList *next;
1120 guchar *save, *save_new;
1122 save = choices_find_path_save("Options", PROJECT, TRUE);
1123 if (!save)
1124 goto out;
1126 save_new = g_strconcat(save, ".new", NULL);
1128 doc = xmlNewDoc("1.0");
1129 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "Options", NULL));
1131 g_hash_table_foreach(option_hash, write_option,
1132 xmlDocGetRootElement(doc));
1134 if (save_xml_file(doc, save_new) || rename(save_new, save))
1135 report_error(_("Error saving %s: %s"), save, g_strerror(errno));
1137 g_free(save_new);
1138 g_free(save);
1139 xmlFreeDoc(doc);
1141 for (next = saver_callbacks; next; next = next->next)
1143 OptionNotify *cb = (OptionNotify *) next->data;
1144 cb();
1147 out:
1148 if (window)
1149 gtk_widget_destroy(window);
1152 /* Make the widget reflect the current value of the option */
1153 static void update_cb(gpointer key, gpointer value, gpointer data)
1155 Option *option = (Option *) value;
1157 g_return_if_fail(option != NULL);
1158 g_return_if_fail(option->widget != NULL);
1160 updating_widgets++;
1162 if (option->update_widget)
1163 option->update_widget(option);
1165 updating_widgets--;
1168 /* Reflect the values in the Option structures by changing the widgets
1169 * in the Options window.
1171 static void update_option_widgets(void)
1173 g_hash_table_foreach(option_hash, update_cb, NULL);
1176 /* Each of the following update the widget to make it show the current
1177 * value of the option.
1180 static void update_toggle(Option *option)
1182 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(option->widget),
1183 option->int_value);
1186 static void update_entry(Option *option)
1188 gtk_entry_set_text(GTK_ENTRY(option->widget), option->value);
1191 static void update_numentry(Option *option)
1193 gtk_spin_button_set_value(GTK_SPIN_BUTTON(option->widget),
1194 option->int_value);
1197 static void update_radio_group(Option *option)
1199 radio_group_set_value(GTK_RADIO_BUTTON(option->widget), option->value);
1202 static void update_slider(Option *option)
1204 gtk_adjustment_set_value(
1205 gtk_range_get_adjustment(GTK_RANGE(option->widget)),
1206 option->int_value);
1209 static void update_menu(Option *option)
1211 option_menu_set(GTK_OPTION_MENU(option->widget), option->value);
1214 static void update_font(Option *option)
1216 GtkToggleButton *active;
1217 gboolean have_font = option->value[0] != '\0';
1219 active = g_object_get_data(G_OBJECT(option->widget), "rox_override");
1221 if (active)
1223 gtk_toggle_button_set_active(active, have_font);
1224 gtk_widget_set_sensitive(option->widget->parent, have_font);
1227 gtk_label_set_text(GTK_LABEL(option->widget),
1228 have_font ? option->value
1229 : (guchar *) _("(use default)"));
1232 static void update_colour(Option *option)
1234 GdkColor colour;
1236 gdk_color_parse(option->value, &colour);
1237 button_patch_set_colour(option->widget, &colour);
1240 /* Each of these read_* calls get the new (string) value of an option
1241 * from the widget.
1244 static guchar *read_toggle(Option *option)
1246 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(option->widget);
1248 return g_strdup_printf("%d", gtk_toggle_button_get_active(toggle));
1251 static guchar *read_entry(Option *option)
1253 return gtk_editable_get_chars(GTK_EDITABLE(option->widget), 0, -1);
1256 static guchar *read_numentry(Option *option)
1258 return g_strdup_printf("%d", (int)
1259 gtk_spin_button_get_value(GTK_SPIN_BUTTON(option->widget)));
1262 static guchar *read_slider(Option *option)
1264 return g_strdup_printf("%d", (int)
1265 gtk_range_get_adjustment(GTK_RANGE(option->widget))->value);
1268 static guchar *read_radio_group(Option *option)
1270 return radio_group_get_value(GTK_RADIO_BUTTON(option->widget));
1273 static guchar *read_menu(Option *option)
1275 return g_strdup(option_menu_get(GTK_OPTION_MENU(option->widget)));
1278 static guchar *read_font(Option *option)
1280 GtkToggleButton *active;
1282 active = g_object_get_data(G_OBJECT(option->widget), "rox_override");
1283 if (active && !gtk_toggle_button_get_active(active))
1284 return g_strdup("");
1286 return g_strdup(gtk_label_get_text(GTK_LABEL(option->widget)));
1289 static guchar *read_colour(Option *option)
1291 GtkStyle *style = GTK_BIN(option->widget)->child->style;
1293 return g_strdup_printf("#%04x%04x%04x",
1294 style->bg[GTK_STATE_NORMAL].red,
1295 style->bg[GTK_STATE_NORMAL].green,
1296 style->bg[GTK_STATE_NORMAL].blue);
1299 static void set_not_changed(gpointer key, gpointer value, gpointer data)
1301 Option *option = (Option *) value;
1303 option->has_changed = FALSE;
1306 /* Builders for decorations (no corresponding option) */
1308 static GList *build_label(Option *option, xmlNode *node, guchar *label)
1310 GtkWidget *widget;
1311 guchar *text;
1312 int help;
1314 g_return_val_if_fail(option == NULL, NULL);
1315 g_return_val_if_fail(label == NULL, NULL);
1317 text = DATA(node);
1318 widget = gtk_label_new(_(text));
1319 g_free(text);
1321 help = get_int(node, "help");
1323 gtk_misc_set_alignment(GTK_MISC(widget), 0, help ? 0.5 : 1);
1324 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_LEFT);
1325 gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
1327 if (help)
1329 GtkWidget *hbox, *image, *align;
1331 hbox = gtk_hbox_new(FALSE, 4);
1332 image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_INFO,
1333 GTK_ICON_SIZE_BUTTON);
1334 align = gtk_alignment_new(0, 0, 0, 0);
1336 gtk_container_add(GTK_CONTAINER(align), image);
1337 gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, TRUE, 0);
1338 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
1340 return g_list_append(NULL, hbox);
1343 return g_list_append(NULL, widget);
1346 static GList *build_spacer(Option *option, xmlNode *node, guchar *label)
1348 GtkWidget *eb;
1350 g_return_val_if_fail(option == NULL, NULL);
1351 g_return_val_if_fail(label == NULL, NULL);
1353 eb = gtk_event_box_new();
1354 gtk_widget_set_size_request(eb, 12, 12);
1356 return g_list_append(NULL, eb);
1359 static GList *build_frame(Option *option, xmlNode *node, guchar *label)
1361 GtkWidget *nbox, *frame;
1362 xmlNode *hw;
1364 g_return_val_if_fail(option == NULL, NULL);
1365 g_return_val_if_fail(label != NULL, NULL);
1367 frame = gtk_frame_new(_(label));
1369 nbox = gtk_vbox_new(FALSE, 0);
1370 gtk_container_set_border_width(GTK_CONTAINER(nbox), 4);
1371 gtk_container_add(GTK_CONTAINER(frame), nbox);
1373 for (hw = node->xmlChildrenNode; hw; hw = hw->next)
1374 if (hw->type == XML_ELEMENT_NODE)
1375 build_widget(hw, nbox);
1377 return g_list_append(NULL, frame);
1380 /* These create new widgets in the options window and set the appropriate
1381 * callbacks.
1384 static GList *build_toggle(Option *option, xmlNode *node, guchar *label)
1386 GtkWidget *toggle;
1388 g_return_val_if_fail(option != NULL, NULL);
1390 toggle = gtk_check_button_new_with_label(_(label));
1392 may_add_tip(toggle, node);
1394 option->update_widget = update_toggle;
1395 option->read_widget = read_toggle;
1396 option->widget = toggle;
1398 g_signal_connect_swapped(toggle, "toggled",
1399 G_CALLBACK(option_check_widget), option);
1401 return g_list_append(NULL, toggle);
1404 static GList *build_slider(Option *option, xmlNode *node, guchar *label)
1406 GtkAdjustment *adj;
1407 GtkWidget *hbox, *slide, *label_wid;
1408 int min, max;
1409 int fixed;
1410 int showvalue;
1411 guchar *end;
1413 g_return_val_if_fail(option != NULL, NULL);
1415 min = get_int(node, "min");
1416 max = get_int(node, "max");
1417 fixed = get_int(node, "fixed");
1418 showvalue = get_int(node, "showvalue");
1420 adj = GTK_ADJUSTMENT(gtk_adjustment_new(0,
1421 min, max, 1, 10, 0));
1423 hbox = gtk_hbox_new(FALSE, 4);
1425 if (label)
1427 label_wid = gtk_label_new(_(label));
1428 gtk_misc_set_alignment(GTK_MISC(label_wid), 0, 0.5);
1429 gtk_box_pack_start(GTK_BOX(hbox), label_wid, FALSE, TRUE, 0);
1430 add_to_size_group(node, label_wid);
1433 end = xmlGetProp(node, "end");
1434 if (end)
1436 gtk_box_pack_end(GTK_BOX(hbox), gtk_label_new(_(end)),
1437 FALSE, TRUE, 0);
1438 g_free(end);
1441 slide = gtk_hscale_new(adj);
1443 if (fixed)
1444 gtk_widget_set_size_request(slide, adj->upper, 24);
1445 if (showvalue)
1447 gtk_scale_set_draw_value(GTK_SCALE(slide), TRUE);
1448 gtk_scale_set_value_pos(GTK_SCALE(slide),
1449 GTK_POS_LEFT);
1450 gtk_scale_set_digits(GTK_SCALE(slide), 0);
1452 else
1453 gtk_scale_set_draw_value(GTK_SCALE(slide), FALSE);
1454 GTK_WIDGET_UNSET_FLAGS(slide, GTK_CAN_FOCUS);
1456 may_add_tip(slide, node);
1458 gtk_box_pack_start(GTK_BOX(hbox), slide, !fixed, TRUE, 0);
1460 option->update_widget = update_slider;
1461 option->read_widget = read_slider;
1462 option->widget = slide;
1464 g_signal_connect_swapped(adj, "value-changed",
1465 G_CALLBACK(option_check_widget), option);
1467 return g_list_append(NULL, hbox);
1470 static GList *build_entry(Option *option, xmlNode *node, guchar *label)
1472 GtkWidget *hbox;
1473 GtkWidget *entry;
1474 GtkWidget *label_wid;
1476 g_return_val_if_fail(option != NULL, NULL);
1478 hbox = gtk_hbox_new(FALSE, 4);
1480 if (label)
1482 label_wid = gtk_label_new(_(label));
1483 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
1484 gtk_box_pack_start(GTK_BOX(hbox), label_wid, FALSE, TRUE, 0);
1487 entry = gtk_entry_new();
1488 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1489 add_to_size_group(node, entry);
1490 may_add_tip(entry, node);
1492 option->update_widget = update_entry;
1493 option->read_widget = read_entry;
1494 option->widget = entry;
1496 g_signal_connect_data(entry, "changed",
1497 G_CALLBACK(option_check_widget), option,
1498 NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED);
1500 return g_list_append(NULL, hbox);
1503 static GList *build_numentry(Option *option, xmlNode *node, guchar *label)
1505 GtkWidget *hbox;
1506 GtkWidget *spin;
1507 GtkWidget *label_wid;
1508 guchar *unit;
1509 int min, max, step, width;
1511 g_return_val_if_fail(option != NULL, NULL);
1513 min = get_int(node, "min");
1514 max = get_int(node, "max");
1515 step = get_int(node, "step");
1516 width = get_int(node, "width");
1517 unit = xmlGetProp(node, "unit");
1519 hbox = gtk_hbox_new(FALSE, 4);
1521 if (label)
1523 label_wid = gtk_label_new(_(label));
1524 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
1525 gtk_box_pack_start(GTK_BOX(hbox), label_wid, FALSE, TRUE, 0);
1526 add_to_size_group(node, label_wid);
1529 spin = gtk_spin_button_new_with_range(min, max, step > 0 ? step : 1);
1530 gtk_entry_set_width_chars(GTK_ENTRY(spin), width > 1 ? width + 1 : -1);
1531 gtk_box_pack_start(GTK_BOX(hbox), spin, FALSE, TRUE, 0);
1532 may_add_tip(spin, node);
1534 if (unit)
1536 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(unit)),
1537 FALSE, TRUE, 0);
1538 g_free(unit);
1541 option->update_widget = update_numentry;
1542 option->read_widget = read_numentry;
1543 option->widget = spin;
1545 g_signal_connect_swapped(spin, "value-changed",
1546 G_CALLBACK(option_check_widget), option);
1548 return g_list_append(NULL, hbox);
1551 static GList *build_radio_group(Option *option, xmlNode *node, guchar *label)
1553 GList *list = NULL;
1554 GtkWidget *button = NULL;
1555 xmlNode *rn;
1556 int cols;
1558 g_return_val_if_fail(option != NULL, NULL);
1560 for (rn = node->xmlChildrenNode; rn; rn = rn->next)
1562 if (rn->type == XML_ELEMENT_NODE)
1564 button = build_radio(rn, button);
1565 g_signal_connect_swapped(button, "toggled",
1566 G_CALLBACK(option_check_widget), option);
1567 list = g_list_append(list, button);
1571 option->update_widget = update_radio_group;
1572 option->read_widget = read_radio_group;
1573 option->widget = button;
1575 cols = get_int(node, "columns");
1576 if (cols > 1)
1578 GtkWidget *table;
1579 GList *next;
1580 int i, n;
1581 int rows;
1583 n = g_list_length(list);
1584 rows = (n + cols - 1) / cols;
1586 table = gtk_table_new(rows, cols, FALSE);
1588 i = 0;
1589 for (next = list; next; next = next->next)
1591 GtkWidget *button = GTK_WIDGET(next->data);
1592 int left = i / rows;
1593 int top = i % rows;
1595 gtk_table_attach_defaults(GTK_TABLE(table), button,
1596 left, left + 1, top, top + 1);
1598 i++;
1601 g_list_free(list);
1602 list = g_list_prepend(NULL, table);
1605 return list;
1608 static GList *build_colour(Option *option, xmlNode *node, guchar *label)
1610 GtkWidget *hbox, *da, *button, *label_wid;
1612 g_return_val_if_fail(option != NULL, NULL);
1614 hbox = gtk_hbox_new(FALSE, 4);
1616 if (label)
1618 label_wid = gtk_label_new(_(label));
1619 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
1620 gtk_box_pack_start(GTK_BOX(hbox), label_wid, TRUE, TRUE, 0);
1623 button = gtk_button_new();
1624 da = gtk_drawing_area_new();
1625 gtk_widget_set_size_request(da, 64, 12);
1626 gtk_container_add(GTK_CONTAINER(button), da);
1627 g_signal_connect(button, "clicked", G_CALLBACK(open_coloursel), option);
1629 may_add_tip(button, node);
1631 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
1633 option->update_widget = update_colour;
1634 option->read_widget = read_colour;
1635 option->widget = button;
1637 return g_list_append(NULL, hbox);
1640 static GList *build_menu(Option *option, xmlNode *node, guchar *label)
1642 GtkWidget *hbox, *om, *option_menu, *label_wid;
1643 xmlNode *item;
1645 g_return_val_if_fail(option != NULL, NULL);
1647 hbox = gtk_hbox_new(FALSE, 4);
1649 label_wid = gtk_label_new(_(label));
1650 gtk_misc_set_alignment(GTK_MISC(label_wid), 1.0, 0.5);
1651 gtk_box_pack_start(GTK_BOX(hbox), label_wid, TRUE, TRUE, 0);
1653 option_menu = gtk_option_menu_new();
1654 gtk_box_pack_start(GTK_BOX(hbox), option_menu, FALSE, TRUE, 0);
1656 om = gtk_menu_new();
1657 gtk_option_menu_set_menu(GTK_OPTION_MENU(option_menu), om);
1659 add_to_size_group(node, option_menu);
1661 for (item = node->xmlChildrenNode; item; item = item->next)
1663 if (item->type == XML_ELEMENT_NODE)
1664 build_menu_item(item, option_menu);
1667 option->update_widget = update_menu;
1668 option->read_widget = read_menu;
1669 option->widget = option_menu;
1671 g_signal_connect_data(option_menu, "changed",
1672 G_CALLBACK(option_check_widget), option,
1673 NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED);
1675 return g_list_append(NULL, hbox);
1678 static GList *build_font(Option *option, xmlNode *node, guchar *label)
1680 GtkWidget *hbox, *button;
1681 GtkWidget *active = NULL;
1682 int override;
1684 g_return_val_if_fail(option != NULL, NULL);
1686 override = get_int(node, "override");
1688 hbox = gtk_hbox_new(FALSE, 4);
1690 if (override)
1692 /* Add a check button to enable the font chooser. If off,
1693 * the option's value is "".
1695 active = gtk_check_button_new_with_label(_(label));
1696 gtk_box_pack_start(GTK_BOX(hbox), active, FALSE, TRUE, 0);
1697 g_signal_connect(active, "toggled",
1698 G_CALLBACK(toggle_active_font), option);
1700 else
1701 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1702 FALSE, TRUE, 0);
1704 button = gtk_button_new_with_label("");
1705 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
1707 option->update_widget = update_font;
1708 option->read_widget = read_font;
1709 option->widget = GTK_BIN(button)->child;
1710 may_add_tip(button, node);
1712 g_object_set_data(G_OBJECT(option->widget), "rox_override", active);
1714 g_signal_connect(button, "clicked", G_CALLBACK(open_fontsel), option);
1716 return g_list_append(NULL, hbox);
1719 static void button_patch_set_colour(GtkWidget *button, GdkColor *colour)
1721 GtkStyle *style;
1722 GtkWidget *patch;
1724 patch = GTK_BIN(button)->child;
1726 style = gtk_style_copy(GTK_WIDGET(patch)->style);
1727 style->bg[GTK_STATE_NORMAL].red = colour->red;
1728 style->bg[GTK_STATE_NORMAL].green = colour->green;
1729 style->bg[GTK_STATE_NORMAL].blue = colour->blue;
1730 gtk_widget_set_style(patch, style);
1731 g_object_unref(G_OBJECT(style));
1733 if (GTK_WIDGET_REALIZED(patch))
1734 gdk_window_clear(patch->window);
1737 static void load_options(xmlDoc *doc)
1739 xmlNode *root, *node;
1741 root = xmlDocGetRootElement(doc);
1743 g_return_if_fail(strcmp(root->name, "Options") == 0);
1745 for (node = root->xmlChildrenNode; node; node = node->next)
1747 gchar *value, *name;
1749 if (node->type != XML_ELEMENT_NODE)
1750 continue;
1751 if (strcmp(node->name, "Option") != 0)
1752 continue;
1753 name = xmlGetProp(node, "name");
1754 if (!name)
1755 continue;
1757 value = xmlNodeGetContent(node);
1759 if (g_hash_table_lookup(loading, name))
1760 g_warning("Duplicate option found!");
1762 g_hash_table_insert(loading, name, value);
1764 /* (don't need to free name or value) */
1768 /* Process one line from the options file (\0 term'd).
1769 * Returns NULL on success, or a pointer to an error message.
1770 * The line is modified.
1772 static const char *process_option_line(gchar *line)
1774 gchar *eq, *c;
1775 gchar *name = line;
1777 g_return_val_if_fail(option_hash != NULL, "No registered options!");
1779 eq = strchr(line, '=');
1780 if (!eq)
1781 return _("Missing '='");
1783 c = eq - 1;
1784 while (c > line && (*c == ' ' || *c == '\t'))
1785 c--;
1786 c[1] = '\0';
1787 c = eq + 1;
1788 while (*c == ' ' || *c == '\t')
1789 c++;
1791 if (g_hash_table_lookup(loading, name))
1792 return "Duplicate option found!";
1794 g_hash_table_insert(loading, g_strdup(name), g_strdup(g_strstrip(c)));
1796 return NULL;