r1283: Added a whole load of 'const' modifiers so we can do extra checking...
[rox-filer.git] / ROX-Filer / src / options.c
blobef8e118cab12344d3a9ba614d9432544f3918b36
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. Each line
29 * is a name/value pair, 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 Save is clicked:
65 * - All the options are written to the filesystem and the saver_callbacks are
66 * called.
69 #include "config.h"
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <errno.h>
75 #include <ctype.h>
76 #include <gtk/gtk.h>
77 #include <libxml/parser.h>
79 #include "global.h"
81 #include "choices.h"
82 #include "options.h"
83 #include "main.h"
84 #include "gui_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 /* List of functions to call after all option values are updated */
109 static GList *notify_callbacks = NULL;
111 /* List of functions to call after all options are saved */
112 static GList *saver_callbacks = NULL;
114 static int updating_widgets = 0; /* Ignore change signals when set */
116 /* Static prototypes */
117 static void save_options(gpointer unused);
118 static void revert_options(GtkWidget *widget, gpointer data);
119 static void build_options_window(void);
120 static GtkWidget *build_frame(void);
121 static void update_option_widgets(void);
122 static void button_patch_set_colour(GtkWidget *button, GdkColor *color);
123 static void option_add(Option *option, const gchar *key);
124 static void set_not_changed(gpointer key, gpointer value, gpointer data);
125 static void load_options(xmlDoc *doc);
127 static const char *process_option_line(gchar *line);
129 static GList *build_toggle(Option *option, xmlNode *node, guchar *label);
130 static GList *build_slider(Option *option, xmlNode *node, guchar *label);
131 static GList *build_entry(Option *option, xmlNode *node, guchar *label);
132 static GList *build_radio_group(Option *option, xmlNode *node, guchar *label);
133 static GList *build_colour(Option *option, xmlNode *node, guchar *label);
134 static GList *build_menu(Option *option, xmlNode *node, guchar *label);
135 static GList *build_font(Option *option, xmlNode *node, guchar *label);
137 static gboolean updating_file_format = FALSE;
139 /****************************************************************
140 * EXTERNAL INTERFACE *
141 ****************************************************************/
143 void options_init(void)
145 char *path;
146 xmlDoc *doc;
148 loading = g_hash_table_new(g_str_hash, g_str_equal);
149 option_hash = g_hash_table_new(g_str_hash, g_str_equal);
150 widget_builder = g_hash_table_new(g_str_hash, g_str_equal);
152 path = choices_find_path_load("Options", PROJECT);
153 if (path)
155 /* Load in all the options set in the filer, storing them
156 * temporarily in the loading hash table.
157 * They get moved to option_hash when they're registered.
159 doc = xmlParseFile(path);
160 if (doc)
162 load_options(doc);
163 xmlFreeDoc(doc);
165 else
167 parse_file(path, process_option_line);
168 updating_file_format = TRUE;
171 g_free(path);
174 option_register_widget("toggle", build_toggle);
175 option_register_widget("slider", build_slider);
176 option_register_widget("entry", build_entry);
177 option_register_widget("radio-group", build_radio_group);
178 option_register_widget("colour", build_colour);
179 option_register_widget("menu", build_menu);
180 option_register_widget("font", build_font);
183 /* When parsing the XML file, process an element named 'name' by
184 * calling 'builder(option, xml_node, label)'.
185 * builder returns the new widgets to add to the options box.
186 * 'name' should be a static string. Call 'option_check_widget' when
187 * the widget's value is modified.
189 * Functions to set or get the widget's state can be stored in 'option'.
190 * If the option doesn't have a name attribute in Options.xml then
191 * ui will be NULL on entry (this is used for buttons).
193 void option_register_widget(char *name, OptionBuildFn builder)
195 g_hash_table_insert(widget_builder, name, builder);
198 /* This is called when the widget's value is modified by the user.
199 * Reads the new value of the widget into the option and calls
200 * the notify callbacks.
202 void option_check_widget(Option *option)
204 guchar *new = NULL;
206 if (updating_widgets)
207 return; /* Not caused by the user... */
209 g_return_if_fail(option->read_widget != NULL);
211 new = option->read_widget(option);
213 g_return_if_fail(new != NULL);
215 g_hash_table_foreach(option_hash, set_not_changed, NULL);
217 option->has_changed = strcmp(option->value, new) != 0;
219 if (!option->has_changed)
221 g_free(new);
222 return;
225 g_free(option->value);
226 option->value = new;
227 option->int_value = atoi(new);
229 options_notify();
232 /* Call all the notify callbacks. This should happen after any options
233 * have their values changed. Set each has_changed before calling.
235 void options_notify(void)
237 GList *next;
239 for (next = notify_callbacks; next; next = next->next)
241 OptionNotify *cb = (OptionNotify *) next->data;
243 cb();
246 if (updating_file_format)
248 updating_file_format = FALSE;
249 save_options(NULL);
250 report_error(_("ROX-Filer has converted your Options file "
251 "to the new XML format"));
255 /* Store values used by Revert */
256 static void store_backup(gpointer key, gpointer value, gpointer data)
258 Option *option = (Option *) value;
260 g_free(option->backup);
261 option->backup = g_strdup(option->value);
264 /* Allow the user to edit the options. Returns the window widget (you don't
265 * normally need this). NULL if already open.
267 GtkWidget *options_show(void)
269 if (!option_tooltips)
270 option_tooltips = gtk_tooltips_new();
272 if (g_hash_table_size(loading) != 0)
274 g_printerr(PROJECT ": Some options loaded but not used:\n");
275 g_hash_table_foreach(loading, (GHFunc) puts, NULL);
278 if (window)
280 gtk_window_present(GTK_WINDOW(window));
281 return NULL;
284 g_hash_table_foreach(option_hash, store_backup, NULL);
286 build_options_window();
288 update_option_widgets();
290 gtk_widget_show_all(window);
292 return window;
295 /* Initialise and register a new integer option */
296 void option_add_int(Option *option, const gchar *key, int value)
298 option->value = g_strdup_printf("%d", value);
299 option->int_value = value;
300 option_add(option, key);
303 void option_add_string(Option *option, const gchar *key, const gchar *value)
305 option->value = g_strdup(value);
306 option->int_value = atoi(value);
307 option_add(option, key);
310 /* Add a callback which will be called after any options have changed their
311 * values. If serveral options change at once, this is called after all
312 * changes.
314 void option_add_notify(OptionNotify *callback)
316 g_return_if_fail(callback != NULL);
318 notify_callbacks = g_list_append(notify_callbacks, callback);
321 /* Call 'callback' after all the options have been saved */
322 void option_add_saver(OptionNotify *callback)
324 g_return_if_fail(callback != NULL);
326 saver_callbacks = g_list_append(saver_callbacks, callback);
329 /****************************************************************
330 * INTERNAL FUNCTIONS *
331 ****************************************************************/
333 /* Option should contain the default value.
334 * It must never be destroyed after being registered (Options are typically
335 * statically allocated).
336 * The key corresponds to the option's name in Options.xml, and to the key
337 * in the saved options file.
339 * On exit, the value will have been updated to the loaded value, if
340 * different to the default.
342 static void option_add(Option *option, const gchar *key)
344 gpointer okey, value;
346 g_return_if_fail(option_hash != NULL);
347 g_return_if_fail(g_hash_table_lookup(option_hash, key) == NULL);
348 g_return_if_fail(option->value != NULL);
350 option->has_changed = FALSE;
352 option->widget = NULL;
353 option->update_widget = NULL;
354 option->read_widget = NULL;
355 option->backup = NULL;
357 g_hash_table_insert(option_hash, (gchar *) key, option);
359 /* Use the value loaded from the file, if any */
360 if (g_hash_table_lookup_extended(loading, key, &okey, &value))
362 option->has_changed = strcmp(option->value, value) != 0;
364 g_free(option->value);
365 option->value = value;
366 option->int_value = atoi(value);
367 g_hash_table_remove(loading, key);
368 g_free(okey);
372 static GtkColorSelectionDialog *current_csel_box = NULL;
373 static GtkFontSelectionDialog *current_fontsel_box = NULL;
375 static void get_new_colour(GtkWidget *ok, Option *option)
377 GtkWidget *csel;
378 gdouble c[4];
379 GdkColor colour;
381 g_return_if_fail(current_csel_box != NULL);
383 csel = current_csel_box->colorsel;
385 gtk_color_selection_get_color(GTK_COLOR_SELECTION(csel), c);
386 colour.red = c[0] * 0xffff;
387 colour.green = c[1] * 0xffff;
388 colour.blue = c[2] * 0xffff;
390 button_patch_set_colour(option->widget, &colour);
392 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
394 option_check_widget(option);
397 static void set_to_null(gpointer *data)
399 *data = NULL;
402 static void open_coloursel(GtkWidget *button, Option *option)
404 GtkColorSelectionDialog *csel;
405 GtkWidget *dialog, *patch;
406 gdouble c[4];
408 if (current_csel_box)
409 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
411 dialog = gtk_color_selection_dialog_new(NULL);
412 csel = GTK_COLOR_SELECTION_DIALOG(dialog);
413 current_csel_box = csel;
414 gtk_window_set_position(GTK_WINDOW(csel), GTK_WIN_POS_MOUSE);
416 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
417 GTK_SIGNAL_FUNC(set_to_null),
418 (GtkObject *) &current_csel_box);
419 gtk_widget_hide(csel->help_button);
420 gtk_signal_connect_object(GTK_OBJECT(csel->cancel_button), "clicked",
421 GTK_SIGNAL_FUNC(gtk_widget_destroy),
422 GTK_OBJECT(dialog));
423 gtk_signal_connect(GTK_OBJECT(csel->ok_button), "clicked",
424 GTK_SIGNAL_FUNC(get_new_colour), option);
426 patch = GTK_BIN(button)->child;
428 c[0] = ((gdouble) patch->style->bg[GTK_STATE_NORMAL].red) / 0xffff;
429 c[1] = ((gdouble) patch->style->bg[GTK_STATE_NORMAL].green) / 0xffff;
430 c[2] = ((gdouble) patch->style->bg[GTK_STATE_NORMAL].blue) / 0xffff;
431 gtk_color_selection_set_color(GTK_COLOR_SELECTION(csel->colorsel), c);
433 gtk_widget_show(dialog);
436 static void font_chosen(GtkWidget *dialog, gint response, Option *option)
438 gchar *font;
440 if (response != GTK_RESPONSE_OK)
441 goto out;
443 font = gtk_font_selection_dialog_get_font_name(
444 GTK_FONT_SELECTION_DIALOG(dialog));
446 gtk_label_set_text(GTK_LABEL(option->widget), font);
448 g_free(font);
450 option_check_widget(option);
452 out:
453 gtk_widget_destroy(dialog);
457 static void open_fontsel(GtkWidget *button, Option *option)
459 if (current_fontsel_box)
460 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
462 current_fontsel_box = GTK_FONT_SELECTION_DIALOG(
463 gtk_font_selection_dialog_new(PROJECT));
465 gtk_window_set_position(GTK_WINDOW(current_fontsel_box),
466 GTK_WIN_POS_MOUSE);
468 gtk_signal_connect_object(GTK_OBJECT(current_fontsel_box), "destroy",
469 GTK_SIGNAL_FUNC(set_to_null),
470 (GtkObject *) &current_fontsel_box);
472 gtk_font_selection_dialog_set_font_name(current_fontsel_box,
473 option->value);
475 gtk_signal_connect(GTK_OBJECT(current_fontsel_box), "response",
476 GTK_SIGNAL_FUNC(font_chosen), option);
478 gtk_widget_show(GTK_WIDGET(current_fontsel_box));
481 /* These are used during parsing... */
482 static xmlDocPtr options_doc = NULL;
484 #define DATA(node) (xmlNodeListGetString(options_doc, node->xmlChildrenNode, 1))
486 static void may_add_tip(GtkWidget *widget, xmlNode *element)
488 guchar *data, *tip;
490 data = DATA(element);
491 if (!data)
492 return;
494 tip = g_strstrip(g_strdup(data));
495 g_free(data);
496 if (*tip)
497 OPTION_TIP(widget, _(tip));
498 g_free(tip);
501 static int get_int(xmlNode *node, guchar *attr)
503 guchar *txt;
504 int retval;
506 txt = xmlGetProp(node, attr);
507 if (!txt)
508 return 0;
510 retval = atoi(txt);
511 g_free(txt);
513 return retval;
516 static GtkWidget *build_radio(xmlNode *radio, GtkWidget *prev)
518 GtkWidget *button;
519 GtkRadioButton *prev_button = (GtkRadioButton *) prev;
520 guchar *label;
522 label = xmlGetProp(radio, "label");
524 button = gtk_radio_button_new_with_label(
525 prev_button ? gtk_radio_button_group(prev_button)
526 : NULL,
527 _(label));
528 g_free(label);
530 may_add_tip(button, radio);
532 gtk_object_set_data(GTK_OBJECT(button), "value",
533 xmlGetProp(radio, "value"));
535 return button;
538 static void build_menu_item(xmlNode *node, GtkWidget *option_menu)
540 GtkWidget *item, *menu;
541 guchar *label;
543 g_return_if_fail(strcmp(node->name, "item") == 0);
545 label = xmlGetProp(node, "label");
546 item = gtk_menu_item_new_with_label(_(label));
547 g_free(label);
549 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(option_menu));
550 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
551 gtk_widget_show_all(menu);
553 gtk_object_set_data(GTK_OBJECT(item),
554 "value", xmlGetProp(node, "value"));
557 static void build_widget(xmlNode *widget, GtkWidget *box)
559 const char *name = widget->name;
560 OptionBuildFn builder;
561 guchar *oname;
562 Option *option;
563 guchar *label;
565 if (strcmp(name, "label") == 0)
567 GtkWidget *label;
568 guchar *text;
570 text = DATA(widget);
571 label = gtk_label_new(_(text));
572 g_free(text);
574 gtk_misc_set_alignment(GTK_MISC(label), 0, 1);
575 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
576 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
577 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
578 return;
580 else if (strcmp(name, "spacer") == 0)
582 GtkWidget *eb;
584 eb = gtk_event_box_new();
585 gtk_widget_set_usize(eb, 12, 12);
586 gtk_box_pack_start(GTK_BOX(box), eb, FALSE, TRUE, 0);
587 return;
590 label = xmlGetProp(widget, "label");
592 if (strcmp(name, "hbox") == 0 || strcmp(name, "vbox") == 0)
594 GtkWidget *nbox;
595 xmlNode *hw;
597 if (name[0] == 'h')
598 nbox = gtk_hbox_new(FALSE, 4);
599 else
600 nbox = gtk_vbox_new(FALSE, 4);
602 if (label)
603 gtk_box_pack_start(GTK_BOX(nbox),
604 gtk_label_new(_(label)), FALSE, TRUE, 4);
605 gtk_box_pack_start(GTK_BOX(box), nbox, FALSE, TRUE, 0);
607 for (hw = widget->xmlChildrenNode; hw; hw = hw->next)
609 if (hw->type == XML_ELEMENT_NODE)
610 build_widget(hw, nbox);
613 g_free(label);
614 return;
617 oname = xmlGetProp(widget, "name");
619 if (oname)
621 option = g_hash_table_lookup(option_hash, oname);
623 if (!option)
625 g_warning("No Option for '%s'!\n", oname);
626 g_free(oname);
627 return;
630 g_free(oname);
632 else
633 option = NULL;
635 builder = g_hash_table_lookup(widget_builder, name);
636 if (builder)
638 GList *widgets, *next;
640 if (option && option->widget)
641 g_warning("Widget for option already exists!");
643 widgets = builder(option, widget, label);
645 for (next = widgets; next; next = next->next)
647 GtkWidget *w = (GtkWidget *) next->data;
648 gtk_box_pack_start(GTK_BOX(box), w, FALSE, TRUE, 0);
650 g_list_free(widgets);
652 else
653 g_warning("Unknown option type '%s'\n", name);
655 g_free(label);
658 static void build_sections(xmlNode *options, GtkWidget *sections_box)
660 xmlNode *section = options->xmlChildrenNode;
662 g_return_if_fail(strcmp(options->name, "options") == 0);
664 for (; section; section = section->next)
666 guchar *title;
667 GtkWidget *page, *scrolled_area;
668 xmlNode *widget;
670 if (section->type != XML_ELEMENT_NODE)
671 continue;
673 title = xmlGetProp(section, "title");
674 page = gtk_vbox_new(FALSE, 0);
675 gtk_container_set_border_width(GTK_CONTAINER(page), 4);
677 scrolled_area = gtk_scrolled_window_new(NULL, NULL);
678 gtk_scrolled_window_set_policy(
679 GTK_SCROLLED_WINDOW(scrolled_area),
680 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
682 gtk_scrolled_window_add_with_viewport(
683 GTK_SCROLLED_WINDOW(scrolled_area),
684 page);
686 gtk_notebook_append_page(GTK_NOTEBOOK(sections_box),
687 scrolled_area,
688 gtk_label_new(_(title)));
690 widget = section->xmlChildrenNode;
691 for (; widget; widget = widget->next)
693 if (widget->type == XML_ELEMENT_NODE)
694 build_widget(widget, page);
697 g_free(title);
701 /* Parse <app_dir>/Options.xml to create the options window.
702 * Sets the global 'window' variable.
704 static void build_options_window(void)
706 GtkWidget *sections_box;
707 xmlDocPtr options_doc;
708 gchar *path;
710 sections_box = build_frame();
712 path = g_strconcat(app_dir, "/Options.xml", NULL);
713 options_doc = xmlParseFile(path);
715 if (!options_doc)
717 report_error("Internal error: %s unreadable", path);
718 g_free(path);
719 return;
722 g_free(path);
724 build_sections(xmlDocGetRootElement(options_doc), sections_box);
726 xmlFreeDoc(options_doc);
727 options_doc = NULL;
730 static void null_widget(gpointer key, gpointer value, gpointer data)
732 Option *option = (Option *) value;
734 g_return_if_fail(option->widget != NULL);
736 option->widget = NULL;
739 static void options_destroyed(GtkWidget *widget, gpointer data)
741 if (current_csel_box)
742 gtk_widget_destroy(GTK_WIDGET(current_csel_box));
743 if (current_fontsel_box)
744 gtk_widget_destroy(GTK_WIDGET(current_fontsel_box));
746 if (widget == window)
748 window = NULL;
750 g_hash_table_foreach(option_hash, null_widget, NULL);
754 /* Creates the window and adds the various buttons to it.
755 * Returns the vbox to add sections to and sets the global
756 * 'window'.
758 static GtkWidget *build_frame(void)
760 GtkWidget *sections_box;
761 GtkWidget *tl_vbox;
762 GtkWidget *actions, *button;
763 char *string, *save_path;
765 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
767 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
768 gtk_window_set_title(GTK_WINDOW(window), _("Options"));
769 gtk_signal_connect(GTK_OBJECT(window), "destroy",
770 GTK_SIGNAL_FUNC(options_destroyed), NULL);
771 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
772 gtk_window_set_default_size(GTK_WINDOW(window), -1, 400);
774 tl_vbox = gtk_vbox_new(FALSE, 4);
775 gtk_container_add(GTK_CONTAINER(window), tl_vbox);
777 sections_box = gtk_notebook_new();
778 gtk_notebook_set_scrollable(GTK_NOTEBOOK(sections_box), TRUE);
779 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(sections_box), GTK_POS_LEFT);
780 gtk_box_pack_start(GTK_BOX(tl_vbox), sections_box, TRUE, TRUE, 0);
782 actions = gtk_hbutton_box_new();
783 gtk_button_box_set_layout(GTK_BUTTON_BOX(actions),
784 GTK_BUTTONBOX_END);
785 gtk_button_box_set_spacing(GTK_BUTTON_BOX(actions), 10);
787 save_path = choices_find_path_save("...", PROJECT, FALSE);
788 if (save_path)
789 gtk_box_pack_start(GTK_BOX(tl_vbox), actions, FALSE, TRUE, 0);
790 else
792 GtkWidget *hbox;
794 hbox = gtk_hbox_new(FALSE, 0);
795 gtk_box_pack_start(GTK_BOX(tl_vbox), hbox, FALSE, TRUE, 0);
796 gtk_box_pack_start(GTK_BOX(hbox),
797 gtk_label_new(_("(saving disabled by CHOICESPATH)")),
798 FALSE, FALSE, 0);
799 gtk_box_pack_start(GTK_BOX(hbox), actions, TRUE, TRUE, 0);
802 button = button_new_mixed(GTK_STOCK_UNDO, "_Revert");
803 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
804 gtk_box_pack_start(GTK_BOX(actions), button, FALSE, TRUE, 0);
805 gtk_signal_connect(GTK_OBJECT(button), "clicked",
806 GTK_SIGNAL_FUNC(revert_options), NULL);
807 gtk_tooltips_set_tip(option_tooltips, button,
808 _("Restore all choices to how they were when the "
809 "Options box was opened."), NULL);
811 button = button_new_mixed(GTK_STOCK_APPLY, "_OK");
812 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
813 gtk_box_pack_start(GTK_BOX(actions), button, FALSE, TRUE, 0);
814 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
815 GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(window));
817 if (save_path)
819 button = gtk_button_new_from_stock(GTK_STOCK_SAVE);
820 gtk_box_pack_start(GTK_BOX(actions), button, FALSE, TRUE, 0);
821 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
822 GTK_SIGNAL_FUNC(save_options), NULL);
824 string = g_strdup_printf(_("Choices will be saved as:\n%s"),
825 save_path);
826 gtk_tooltips_set_tip(option_tooltips, button, string, NULL);
827 g_free(save_path);
828 g_free(string);
829 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
830 gtk_widget_grab_default(button);
831 gtk_widget_grab_focus(button);
834 return sections_box;
837 /* Given the last radio button in the group, select whichever
838 * radio button matches the given value.
840 static void radio_group_set_value(GtkRadioButton *last, guchar *value)
842 GSList *next;
844 next = gtk_radio_button_group(last);
845 while (next)
847 GtkToggleButton *button = (GtkToggleButton *) next->data;
848 guchar *val;
850 val = gtk_object_get_data(GTK_OBJECT(button), "value");
851 g_return_if_fail(val != NULL);
853 if (strcmp(val, value) == 0)
855 gtk_toggle_button_set_active(button, TRUE);
856 return;
859 next = next->next;
862 g_warning("Can't find radio button with value %s\n", value);
865 /* Given the last radio button in the group, return a copy of the
866 * value for the selected radio item.
868 static guchar *radio_group_get_value(GtkRadioButton *last)
870 GSList *next;
872 next = gtk_radio_button_group(last);
873 while (next)
875 GtkToggleButton *button = (GtkToggleButton *) next->data;
877 if (gtk_toggle_button_get_active(button))
879 guchar *val;
881 val = gtk_object_get_data(GTK_OBJECT(button), "value");
882 g_return_val_if_fail(val != NULL, NULL);
884 return g_strdup(val);
887 next = next->next;
890 return NULL;
893 /* Select this item with this value */
894 static void option_menu_set(GtkOptionMenu *om, guchar *value)
896 GtkWidget *menu;
897 GList *list, *next;
898 int i = 0;
900 menu = gtk_option_menu_get_menu(om);
901 list = gtk_container_children(GTK_CONTAINER(menu));
903 for (next = list; next; next = next->next)
905 GtkObject *item = (GtkObject *) next->data;
906 guchar *data;
908 data = gtk_object_get_data(item, "value");
909 g_return_if_fail(data != NULL);
911 if (strcmp(data, value) == 0)
913 gtk_option_menu_set_history(om, i);
914 break;
917 i++;
920 g_list_free(list);
923 /* Get the value (static) of the selected item */
924 static guchar *option_menu_get(GtkOptionMenu *om)
926 GtkWidget *menu, *item;
928 menu = gtk_option_menu_get_menu(om);
929 item = gtk_menu_get_active(GTK_MENU(menu));
931 return gtk_object_get_data(GTK_OBJECT(item), "value");
934 static void restore_backup(gpointer key, gpointer value, gpointer data)
936 Option *option = (Option *) value;
938 g_return_if_fail(option->backup != NULL);
940 option->has_changed = strcmp(option->value, option->backup) != 0;
941 if (!option->has_changed)
942 return;
944 g_free(option->value);
945 option->value = g_strdup(option->backup);
946 option->int_value = atoi(option->value);
949 static void revert_options(GtkWidget *widget, gpointer data)
951 g_hash_table_foreach(option_hash, restore_backup, NULL);
952 options_notify();
953 update_option_widgets();
956 static void write_option(gpointer key, gpointer value, gpointer data)
958 xmlNodePtr doc = (xmlNodePtr) data;
959 Option *option = (Option *) value;
960 xmlNodePtr tree;
962 tree = xmlNewTextChild(doc, NULL, "Option", option->value);
963 xmlSetProp(tree, "name", (gchar *) key);
966 /* Save doc as XML as filename, 0 on success or -1 on failure */
967 static int save_xml_file(xmlDocPtr doc, gchar *filename)
969 #if LIBXML_VERSION > 20400
970 if (xmlSaveFormatFileEnc(filename, doc, NULL, 1) < 0)
971 return 1;
972 #else
973 FILE *out;
975 out = fopen(filename, "w");
976 if (!out)
977 return 1;
979 xmlDocDump(out, doc); /* Some versions return void */
981 if (fclose(out))
982 return 1;
983 #endif
985 return 0;
988 static void save_options(gpointer unused)
990 xmlDoc *doc;
991 GList *next;
992 guchar *save, *save_new;
994 save = choices_find_path_save("Options", PROJECT, TRUE);
995 if (!save)
997 report_error(_("Could not save options: %s"),
998 _("Choices saving is disabled by "
999 "CHOICESPATH variable"));
1000 return;
1003 save_new = g_strconcat(save, ".new", NULL);
1005 doc = xmlNewDoc("1.0");
1006 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "Options", NULL));
1008 g_hash_table_foreach(option_hash, write_option,
1009 xmlDocGetRootElement(doc));
1011 if (save_xml_file(doc, save_new) || rename(save_new, save))
1012 report_error(_("Error saving %s: %s"), save, g_strerror(errno));
1014 g_free(save_new);
1015 g_free(save);
1016 xmlFreeDoc(doc);
1018 for (next = saver_callbacks; next; next = next->next)
1020 OptionNotify *cb = (OptionNotify *) next->data;
1021 cb();
1024 if (window)
1025 gtk_widget_destroy(window);
1028 /* Make the widget reflect the current value of the option */
1029 static void update_cb(gpointer key, gpointer value, gpointer data)
1031 Option *option = (Option *) value;
1033 g_return_if_fail(option != NULL);
1034 g_return_if_fail(option->widget != NULL);
1036 updating_widgets++;
1038 if (option->update_widget)
1039 option->update_widget(option);
1041 updating_widgets--;
1044 /* Reflect the values in the Option structures by changing the widgets
1045 * in the Options window.
1047 static void update_option_widgets(void)
1049 g_hash_table_foreach(option_hash, update_cb, NULL);
1052 /* Each of the following update the widget to make it show the current
1053 * value of the option.
1056 static void update_toggle(Option *option)
1058 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(option->widget),
1059 option->int_value);
1062 static void update_entry(Option *option)
1064 gtk_entry_set_text(GTK_ENTRY(option->widget), option->value);
1067 static void update_radio_group(Option *option)
1069 radio_group_set_value(GTK_RADIO_BUTTON(option->widget), option->value);
1072 static void update_slider(Option *option)
1074 gtk_adjustment_set_value(
1075 gtk_range_get_adjustment(GTK_RANGE(option->widget)),
1076 option->int_value);
1079 static void update_menu(Option *option)
1081 option_menu_set(GTK_OPTION_MENU(option->widget), option->value);
1084 static void update_font(Option *option)
1086 gtk_label_set_text(GTK_LABEL(option->widget), option->value);
1089 static void update_colour(Option *option)
1091 GdkColor colour;
1093 gdk_color_parse(option->value, &colour);
1094 button_patch_set_colour(option->widget, &colour);
1097 /* Each of these read_* calls get the new (string) value of an option
1098 * from the widget.
1101 static guchar *read_toggle(Option *option)
1103 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(option->widget);
1105 return g_strdup_printf("%d", gtk_toggle_button_get_active(toggle));
1108 static guchar *read_entry(Option *option)
1110 return gtk_editable_get_chars(GTK_EDITABLE(option->widget), 0, -1);
1113 static guchar *read_slider(Option *option)
1115 return g_strdup_printf("%f",
1116 gtk_range_get_adjustment(GTK_RANGE(option->widget))->value);
1119 static guchar *read_radio_group(Option *option)
1121 return radio_group_get_value(GTK_RADIO_BUTTON(option->widget));
1124 static guchar *read_menu(Option *option)
1126 return g_strdup(option_menu_get(GTK_OPTION_MENU(option->widget)));
1129 static guchar *read_font(Option *option)
1131 return g_strdup(gtk_label_get_text(GTK_LABEL(option->widget)));
1134 static guchar *read_colour(Option *option)
1136 GtkStyle *style = GTK_BIN(option->widget)->child->style;
1138 return g_strdup_printf("#%04x%04x%04x",
1139 style->bg[GTK_STATE_NORMAL].red,
1140 style->bg[GTK_STATE_NORMAL].green,
1141 style->bg[GTK_STATE_NORMAL].blue);
1144 static void set_not_changed(gpointer key, gpointer value, gpointer data)
1146 Option *option = (Option *) value;
1148 option->has_changed = FALSE;
1151 /* These create new widgets in the options window and set the appropriate
1152 * callbacks.
1155 static GList *build_toggle(Option *option, xmlNode *node, guchar *label)
1157 GtkWidget *toggle;
1159 g_return_val_if_fail(option != NULL, NULL);
1161 toggle = gtk_check_button_new_with_label(_(label));
1163 may_add_tip(toggle, node);
1165 option->update_widget = update_toggle;
1166 option->read_widget = read_toggle;
1167 option->widget = toggle;
1169 gtk_signal_connect_object(GTK_OBJECT(toggle), "toggled",
1170 GTK_SIGNAL_FUNC(option_check_widget),
1171 (GtkObject *) option);
1173 return g_list_append(NULL, toggle);
1176 static GList *build_slider(Option *option, xmlNode *node, guchar *label)
1178 GtkAdjustment *adj;
1179 GtkWidget *hbox, *slide;
1180 int min, max;
1181 int fixed;
1182 int showvalue;
1183 guchar *end;
1185 g_return_val_if_fail(option != NULL, NULL);
1187 min = get_int(node, "min");
1188 max = get_int(node, "max");
1189 fixed = get_int(node, "fixed");
1190 showvalue = get_int(node, "showvalue");
1192 adj = GTK_ADJUSTMENT(gtk_adjustment_new(0,
1193 min, max, 1, 10, 0));
1195 hbox = gtk_hbox_new(FALSE, 4);
1196 gtk_box_pack_start(GTK_BOX(hbox),
1197 gtk_label_new(_(label)),
1198 FALSE, TRUE, 0);
1200 end = xmlGetProp(node, "end");
1201 if (end)
1203 gtk_box_pack_end(GTK_BOX(hbox), gtk_label_new(_(end)),
1204 FALSE, TRUE, 0);
1205 g_free(end);
1208 slide = gtk_hscale_new(adj);
1210 if (fixed)
1211 gtk_widget_set_usize(slide, adj->upper, 24);
1212 if (showvalue)
1214 gtk_scale_set_draw_value(GTK_SCALE(slide), TRUE);
1215 gtk_scale_set_value_pos(GTK_SCALE(slide),
1216 GTK_POS_LEFT);
1217 gtk_scale_set_digits(GTK_SCALE(slide), 0);
1219 else
1220 gtk_scale_set_draw_value(GTK_SCALE(slide), FALSE);
1221 GTK_WIDGET_UNSET_FLAGS(slide, GTK_CAN_FOCUS);
1223 may_add_tip(slide, node);
1225 gtk_box_pack_start(GTK_BOX(hbox), slide, !fixed, TRUE, 0);
1227 option->update_widget = update_slider;
1228 option->read_widget = read_slider;
1229 option->widget = slide;
1231 gtk_signal_connect_object(GTK_OBJECT(adj), "value-changed",
1232 GTK_SIGNAL_FUNC(option_check_widget),
1233 (GtkObject *) option);
1235 return g_list_append(NULL, hbox);
1238 static GList *build_entry(Option *option, xmlNode *node, guchar *label)
1240 GtkWidget *hbox;
1241 GtkWidget *entry;
1243 g_return_val_if_fail(option != NULL, NULL);
1245 hbox = gtk_hbox_new(FALSE, 4);
1247 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1248 FALSE, TRUE, 0);
1250 entry = gtk_entry_new();
1251 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1252 may_add_tip(entry, node);
1254 option->update_widget = update_entry;
1255 option->read_widget = read_entry;
1256 option->widget = entry;
1258 gtk_signal_connect_object_after(GTK_OBJECT(entry), "changed",
1259 GTK_SIGNAL_FUNC(option_check_widget),
1260 (GtkObject *) option);
1262 return g_list_append(NULL, hbox);
1265 static GList *build_radio_group(Option *option, xmlNode *node, guchar *label)
1267 GList *list = NULL;
1268 GtkWidget *button = NULL;
1269 xmlNode *rn;
1271 g_return_val_if_fail(option != NULL, NULL);
1273 for (rn = node->xmlChildrenNode; rn; rn = rn->next)
1275 if (rn->type == XML_ELEMENT_NODE)
1277 button = build_radio(rn, button);
1278 gtk_signal_connect_object(GTK_OBJECT(button), "toggled",
1279 GTK_SIGNAL_FUNC(option_check_widget),
1280 (GtkObject *) option);
1281 list = g_list_append(list, button);
1285 option->update_widget = update_radio_group;
1286 option->read_widget = read_radio_group;
1287 option->widget = button;
1289 return list;
1292 static GList *build_colour(Option *option, xmlNode *node, guchar *label)
1294 GtkWidget *hbox, *da, *button;
1295 int lpos;
1297 g_return_val_if_fail(option != NULL, NULL);
1299 /* lpos gives the position for the label
1300 * 0: label comes before the button
1301 * non-zero: label comes after the button
1303 lpos = get_int(node, "lpos");
1305 hbox = gtk_hbox_new(FALSE, 4);
1306 if (lpos == 0)
1307 gtk_box_pack_start(GTK_BOX(hbox),
1308 gtk_label_new(_(label)),
1309 FALSE, TRUE, 0);
1311 button = gtk_button_new();
1312 da = gtk_drawing_area_new();
1313 gtk_drawing_area_size(GTK_DRAWING_AREA(da), 64, 12);
1314 gtk_container_add(GTK_CONTAINER(button), da);
1315 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1316 GTK_SIGNAL_FUNC(open_coloursel), option);
1318 may_add_tip(button, node);
1320 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
1321 if (lpos)
1322 gtk_box_pack_start(GTK_BOX(hbox),
1323 gtk_label_new(_(label)),
1324 FALSE, TRUE, 0);
1326 option->update_widget = update_colour;
1327 option->read_widget = read_colour;
1328 option->widget = button;
1330 return g_list_append(NULL, hbox);
1333 static GList *build_menu(Option *option, xmlNode *node, guchar *label)
1335 GtkWidget *hbox, *om, *option_menu;
1336 xmlNode *item;
1337 GtkWidget *menu;
1338 GList *list, *kids;
1339 int min_w = 4, min_h = 4;
1341 g_return_val_if_fail(option != NULL, NULL);
1343 hbox = gtk_hbox_new(FALSE, 4);
1345 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1346 FALSE, TRUE, 0);
1348 option_menu = gtk_option_menu_new();
1349 gtk_box_pack_start(GTK_BOX(hbox), option_menu, FALSE, TRUE, 0);
1351 om = gtk_menu_new();
1352 gtk_option_menu_set_menu(GTK_OPTION_MENU(option_menu), om);
1354 for (item = node->xmlChildrenNode; item; item = item->next)
1356 if (item->type == XML_ELEMENT_NODE)
1357 build_menu_item(item, option_menu);
1360 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(option_menu));
1361 list = kids = gtk_container_children(GTK_CONTAINER(menu));
1363 while (kids)
1365 GtkWidget *item = (GtkWidget *) kids->data;
1366 GtkRequisition req;
1368 gtk_widget_size_request(item, &req);
1369 if (req.width > min_w)
1370 min_w = req.width;
1371 if (req.height > min_h)
1372 min_h = req.height;
1374 kids = kids->next;
1377 g_list_free(list);
1379 gtk_widget_set_usize(option_menu,
1380 min_w + 50, /* Else node doesn't work! */
1381 min_h + 4);
1383 option->update_widget = update_menu;
1384 option->read_widget = read_menu;
1385 option->widget = option_menu;
1387 gtk_signal_connect_object_after(GTK_OBJECT(option_menu), "changed",
1388 GTK_SIGNAL_FUNC(option_check_widget),
1389 (GtkObject *) option);
1391 return g_list_append(NULL, hbox);
1394 static GList *build_font(Option *option, xmlNode *node, guchar *label)
1396 GtkWidget *hbox, *button;
1398 g_return_val_if_fail(option != NULL, NULL);
1400 hbox = gtk_hbox_new(FALSE, 4);
1402 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1403 FALSE, TRUE, 0);
1405 button = gtk_button_new_with_label("");
1406 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
1408 option->update_widget = update_font;
1409 option->read_widget = read_font;
1410 option->widget = GTK_BIN(button)->child;
1411 may_add_tip(button, node);
1413 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1414 GTK_SIGNAL_FUNC(open_fontsel), (GtkObject *) option);
1416 return g_list_append(NULL, hbox);
1419 static void button_patch_set_colour(GtkWidget *button, GdkColor *colour)
1421 GtkStyle *style;
1422 GtkWidget *patch;
1424 patch = GTK_BIN(button)->child;
1426 style = gtk_style_copy(GTK_WIDGET(patch)->style);
1427 style->bg[GTK_STATE_NORMAL].red = colour->red;
1428 style->bg[GTK_STATE_NORMAL].green = colour->green;
1429 style->bg[GTK_STATE_NORMAL].blue = colour->blue;
1430 gtk_widget_set_style(patch, style);
1431 gtk_style_unref(style);
1433 if (GTK_WIDGET_REALIZED(patch))
1434 gdk_window_clear(patch->window);
1437 static void load_options(xmlDoc *doc)
1439 xmlNode *root, *node;
1441 root = xmlDocGetRootElement(doc);
1443 g_return_if_fail(strcmp(root->name, "Options") == 0);
1445 for (node = root->xmlChildrenNode; node; node = node->next)
1447 gchar *value, *name;
1449 if (node->type != XML_ELEMENT_NODE)
1450 continue;
1451 if (strcmp(node->name, "Option") != 0)
1452 continue;
1453 name = xmlGetProp(node, "name");
1454 if (!name)
1455 continue;
1457 value = xmlNodeGetContent(node);
1459 if (g_hash_table_lookup(loading, name))
1460 g_warning("Duplicate option found!");
1462 g_hash_table_insert(loading, name, value);
1464 /* (don't need to free name or value) */
1468 /* Process one line from the options file (\0 term'd).
1469 * Returns NULL on success, or a pointer to an error message.
1470 * The line is modified.
1472 static const char *process_option_line(gchar *line)
1474 gchar *eq, *c;
1475 gchar *name = line;
1477 g_return_val_if_fail(option_hash != NULL, "No registered options!");
1479 eq = strchr(line, '=');
1480 if (!eq)
1481 return _("Missing '='");
1483 c = eq - 1;
1484 while (c > line && (*c == ' ' || *c == '\t'))
1485 c--;
1486 c[1] = '\0';
1487 c = eq + 1;
1488 while (*c == ' ' || *c == '\t')
1489 c++;
1491 if (g_hash_table_lookup(loading, name))
1492 return "Duplicate option found!";
1494 g_hash_table_insert(loading, g_strdup(name), g_strdup(g_strstrip(c)));
1496 return NULL;