1 /* demo-Gtk-conf.c --- implements the dynamic configuration dialogs.
2 * xscreensaver, Copyright (c) 2001, 2003, 2004 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
17 #if defined(HAVE_GTK) && defined(HAVE_XML) /* whole file */
19 #include <xscreensaver-intl.h>
32 * Both of these workarounds can be removed when support for ancient
33 * libxml versions is dropped. versions 1.8.11 and 2.3.4 provide the
38 * Older libxml polluted the global headerspace, while libxml2 fixed
39 * this. To support both old and recent libxmls, we have this
42 #ifdef HAVE_OLD_XML_HEADERS
44 #else /* ! HAVE_OLD_XML_HEADERS */
45 # include <libxml/parser.h>
46 #endif /* HAVE_OLD_XML_HEADERS */
49 * handle non-native spelling mistakes in earlier versions and provide
50 * the source-compat fix for this that may not be in older versions.
52 #ifndef xmlChildrenNode
53 # if LIBXML_VERSION >= 20000
54 # define xmlChildrenNode children
55 # define xmlRootNode children
57 # define xmlChildrenNode childs
58 # define xmlRootNode root
59 # endif /* LIBXML_VERSION */
60 #endif /* xmlChildrenNode */
64 #include "demo-Gtk-conf.h"
67 extern const char *blurb (void);
70 const char *hack_configuration_path = HACK_CONFIGURATION_PATH;
72 static gboolean debug_p = FALSE;
94 xmlChar *id; /* widget name */
95 xmlChar *label; /* heading label, or null */
97 /* command, fake, description, fakepreview, string, file
99 xmlChar *string; /* file name, description, whatever. */
101 /* slider, spinbutton
103 xmlChar *low_label; /* label for the left side */
104 xmlChar *high_label; /* label for the right side */
105 float low; /* minimum value */
106 float high; /* maximum value */
107 float value; /* default value */
108 gboolean integer_p; /* whether the range is integral, or real */
109 xmlChar *arg; /* command-line option to set (substitute "%") */
110 gboolean invert_p; /* whether to flip the value and pretend the
111 range goes from hi-low instead of low-hi. */
113 /* boolean, select-option
115 xmlChar *arg_set; /* command-line option to set for "yes", or null */
116 xmlChar *arg_unset; /* command-line option to set for "no", or null */
117 xmlChar *test; /* #### no idea - enablement? */
132 static parameter *make_select_option (const char *file, xmlNodePtr);
133 static void make_parameter_widget (const char *filename,
134 parameter *, GtkWidget *, int *);
135 static void browse_button_cb (GtkButton *button, gpointer user_data);
138 /* Frees the parameter object and all strings and sub-parameters.
139 Does not destroy the widget, if any.
142 free_parameter (parameter *p)
145 if (p->id) free (p->id);
146 if (p->label) free (p->label);
147 if (p->string) free (p->string);
148 if (p->low_label) free (p->low_label);
149 if (p->high_label) free (p->high_label);
150 if (p->arg) free (p->arg);
151 if (p->arg_set) free (p->arg_set);
152 if (p->arg_unset) free (p->arg_unset);
153 if (p->test) free (p->test);
155 for (rest = p->options; rest; rest = rest->next)
157 free_parameter ((parameter *) rest->data);
159 for (rest = p->enable; rest; rest = rest->next)
161 free ((char *) rest->data);
164 if (p->enable) g_list_free (p->enable);
166 memset (p, ~0, sizeof(*p));
171 /* Debugging: dumps out a `parameter' structure.
175 describe_parameter (FILE *out, parameter *p)
180 case COMMAND: fprintf (out, "command"); break;
181 case FAKE: fprintf (out, "fake"); break;
182 case DESCRIPTION: fprintf (out, "_description"); break;
183 case FAKEPREVIEW: fprintf (out, "fakepreview"); break;
184 case STRING: fprintf (out, "string"); break;
185 case FILENAME: fprintf (out, "filename"); break;
186 case SLIDER: fprintf (out, "number type=\"slider\""); break;
187 case SPINBUTTON: fprintf (out, "number type=\"spinbutton\""); break;
188 case BOOLEAN: fprintf (out, "boolean"); break;
189 case SELECT: fprintf (out, "select"); break;
190 default: abort(); break;
192 if (p->id) fprintf (out, " id=\"%s\"", p->id);
193 if (p->label) fprintf (out, " _label=\"%s\"", p->label);
194 if (p->string && p->type != DESCRIPTION)
195 fprintf (out, " string=\"%s\"", p->string);
196 if (p->low_label) fprintf (out, " _low-label=\"%s\"", p->low_label);
197 if (p->high_label) fprintf (out, " _high-label=\"%s\"", p->high_label);
198 if (p->low) fprintf (out, " low=\"%.2f\"", p->low);
199 if (p->high) fprintf (out, " high=\"%.2f\"", p->high);
200 if (p->value) fprintf (out, " default=\"%.2f\"", p->value);
201 if (p->arg) fprintf (out, " arg=\"%s\"", p->arg);
202 if (p->invert_p) fprintf (out, " convert=\"invert\"");
203 if (p->arg_set) fprintf (out, " arg-set=\"%s\"", p->arg_set);
204 if (p->arg_unset) fprintf (out, " arg-unset=\"%s\"", p->arg_unset);
205 if (p->test) fprintf (out, " test=\"%s\"", p->test);
206 fprintf (out, ">\n");
208 if (p->type == SELECT)
211 for (opt = p->options; opt; opt = opt->next)
213 parameter *o = (parameter *) opt->data;
214 if (o->type != SELECT_OPTION) abort();
215 fprintf (out, " <option");
216 if (o->id) fprintf (out, " id=\"%s\"", o->id);
217 if (o->label) fprintf (out, " _label=\"%s\"", o->label);
218 if (o->arg_set) fprintf (out, " arg-set=\"%s\"", o->arg_set);
219 if (o->arg_unset) fprintf (out, " arg-unset=\"%s\"", o->arg_unset);
220 if (o->test) fprintf (out, " test=\"%s\"", o->test);
224 fprintf (out, " enable=\"");
225 for (e = o->enable; e; e = e->next)
226 fprintf (out, "%s%s", (char *) e->data, (e->next ? "," : ""));
229 fprintf (out, ">\n");
231 fprintf (out, "</select>\n");
233 else if (p->type == DESCRIPTION)
236 fprintf (out, " %s\n", p->string);
237 fprintf (out, "</_description>\n");
243 /* Like xmlGetProp() but parses a float out of the string.
244 If the number was expressed as a float and not an integer
245 (that is, the string contained a decimal point) then
246 `floatp' is set to TRUE. Otherwise, it is unchanged.
249 xml_get_float (xmlNodePtr node, const xmlChar *name, gboolean *floatpP)
251 const char *s = (char *) xmlGetProp (node, name);
254 if (!s || 1 != sscanf (s, "%f %c", &f, &c))
258 if (strchr (s, '.')) *floatpP = TRUE;
264 static void sanity_check_parameter (const char *filename,
265 const xmlChar *node_name,
268 /* Allocates and returns a new `parameter' object based on the
269 properties in the given XML node. Returns 0 if there's nothing
270 to create (comment, or unknown tag.)
273 make_parameter (const char *filename, xmlNodePtr node)
276 const char *name = (char *) node->name;
278 gboolean floatp = FALSE;
280 if (node->type == XML_COMMENT_NODE)
283 p = calloc (1, sizeof(*p));
286 else if (!strcmp (name, "command")) p->type = COMMAND;
287 else if (!strcmp (name, "fullcommand")) p->type = COMMAND;
288 else if (!strcmp (name, "_description")) p->type = DESCRIPTION;
289 else if (!strcmp (name, "fakepreview")) p->type = FAKEPREVIEW;
290 else if (!strcmp (name, "fake")) p->type = FAKE;
291 else if (!strcmp (name, "boolean")) p->type = BOOLEAN;
292 else if (!strcmp (name, "string")) p->type = STRING;
293 else if (!strcmp (name, "file")) p->type = FILENAME;
294 else if (!strcmp (name, "number")) p->type = SPINBUTTON;
295 else if (!strcmp (name, "select")) p->type = SELECT;
299 fprintf (stderr, "%s: WARNING: %s: unknown tag: \"%s\"\n",
300 blurb(), filename, name);
305 if (p->type == SPINBUTTON)
307 const char *type = (char *) xmlGetProp (node, (xmlChar *) "type");
308 if (!type || !strcmp (type, "spinbutton")) p->type = SPINBUTTON;
309 else if (!strcmp (type, "slider")) p->type = SLIDER;
313 fprintf (stderr, "%s: WARNING: %s: unknown %s type: \"%s\"\n",
314 blurb(), filename, name, type);
319 else if (p->type == DESCRIPTION)
321 if (node->xmlChildrenNode &&
322 node->xmlChildrenNode->type == XML_TEXT_NODE &&
323 !node->xmlChildrenNode->next)
324 p->string = (xmlChar *)
325 strdup ((char *) node->xmlChildrenNode->content);
328 p->id = xmlGetProp (node, (xmlChar *) "id");
329 p->label = xmlGetProp (node, (xmlChar *) "_label");
330 p->low_label = xmlGetProp (node, (xmlChar *) "_low-label");
331 p->high_label = xmlGetProp (node, (xmlChar *) "_high-label");
332 p->low = xml_get_float (node, (xmlChar *) "low", &floatp);
333 p->high = xml_get_float (node, (xmlChar *) "high", &floatp);
334 p->value = xml_get_float (node, (xmlChar *) "default", &floatp);
335 p->integer_p = !floatp;
336 convert = (char *) xmlGetProp (node, (xmlChar *) "convert");
337 p->invert_p = (convert && !strcmp (convert, "invert"));
338 p->arg = xmlGetProp (node, (xmlChar *) "arg");
339 p->arg_set = xmlGetProp (node, (xmlChar *) "arg-set");
340 p->arg_unset = xmlGetProp (node, (xmlChar *) "arg-unset");
341 p->test = xmlGetProp (node, (xmlChar *) "test");
343 /* Check for missing decimal point */
346 (p->high != p->low) &&
347 (p->high - p->low) <= 1)
349 "%s: WARNING: %s: %s: range [%.1f, %.1f] shouldn't be integral!\n",
350 blurb(), filename, p->id,
353 if (p->type == SELECT)
356 for (kids = node->xmlChildrenNode; kids; kids = kids->next)
358 parameter *s = make_select_option (filename, kids);
360 p->options = g_list_append (p->options, s);
364 sanity_check_parameter (filename, (const xmlChar *) name, p);
370 /* Allocates and returns a new SELECT_OPTION `parameter' object based
371 on the properties in the given XML node. Returns 0 if there's nothing
372 to create (comment, or unknown tag.)
375 make_select_option (const char *filename, xmlNodePtr node)
377 if (node->type == XML_COMMENT_NODE)
379 else if (node->type != XML_ELEMENT_NODE)
383 "%s: WARNING: %s: %s: unexpected child tag type %d\n",
384 blurb(), filename, node->name, (int)node->type);
387 else if (strcmp ((char *) node->name, "option"))
391 "%s: WARNING: %s: %s: child not an option tag: \"%s\"\n",
392 blurb(), filename, node->name, node->name);
397 parameter *s = calloc (1, sizeof(*s));
400 s->type = SELECT_OPTION;
401 s->id = xmlGetProp (node, (xmlChar *) "id");
402 s->label = xmlGetProp (node, (xmlChar *) "_label");
403 s->arg_set = xmlGetProp (node, (xmlChar *) "arg-set");
404 s->arg_unset = xmlGetProp (node, (xmlChar *) "arg-unset");
405 s->test = xmlGetProp (node, (xmlChar *) "test");
406 enable = (char*)xmlGetProp (node, (xmlChar *) "enable");
410 enable = strdup (enable);
411 e = strtok (enable, ", ");
414 s->enable = g_list_append (s->enable, strdup (e));
415 e = strtok (0, ", ");
420 sanity_check_parameter (filename, node->name, s);
426 /* Rudimentary check to make sure someone hasn't typed "arg-set="
427 when they should have typed "arg=", etc.
430 sanity_check_parameter (const char *filename, const xmlChar *node_name,
448 memset (&allowed, 0, sizeof (allowed));
449 memset (&require, 0, sizeof (require));
460 allowed.string = TRUE;
467 allowed.label = TRUE;
468 require.label = TRUE;
475 allowed.label = TRUE;
482 allowed.label = TRUE;
483 allowed.low_label = TRUE;
484 allowed.high_label = TRUE;
488 /* require.low = TRUE; -- may be 0 */
490 /* require.high = TRUE; -- may be 0 */
491 allowed.value = TRUE;
492 /* require.value = TRUE; -- may be 0 */
493 allowed.invert_p = TRUE;
498 allowed.label = TRUE;
502 /* require.low = TRUE; -- may be 0 */
504 /* require.high = TRUE; -- may be 0 */
505 allowed.value = TRUE;
506 /* require.value = TRUE; -- may be 0 */
507 allowed.invert_p = TRUE;
512 allowed.label = TRUE;
513 allowed.arg_set = TRUE;
514 allowed.arg_unset = TRUE;
522 allowed.label = TRUE;
523 require.label = TRUE;
524 allowed.arg_set = TRUE;
532 fprintf (stderr, "%s: %s: " STR " in <%s%s id=\"%s\">\n", \
533 blurb(), filename, node_name, \
534 (!strcmp((char *) node_name, "number") \
535 ? (p->type == SPINBUTTON ? " type=spinbutton" : " type=slider")\
537 (p->id ? (char *) p->id : ""))
538 # define CHECK(SLOT,NAME) \
539 if (p->SLOT && !allowed.SLOT) \
540 WARN ("\"" NAME "\" is not a valid option"); \
541 if (!p->SLOT && require.SLOT) \
542 WARN ("\"" NAME "\" is required")
545 CHECK (label, "_label");
546 CHECK (string, "(body text)");
547 CHECK (low_label, "_low-label");
548 CHECK (high_label, "_high-label");
550 CHECK (high, "high");
551 CHECK (value, "default");
553 CHECK (invert_p, "convert");
554 CHECK (arg_set, "arg-set");
555 CHECK (arg_unset, "arg-unset");
562 /* Helper for make_parameters()
565 make_parameters_1 (const char *filename, xmlNodePtr node,
566 GtkWidget *parent, int *row)
570 for (; node; node = node->next)
572 const char *name = (char *) node->name;
573 if (!strcmp (name, "hgroup") ||
574 !strcmp (name, "vgroup"))
576 GtkWidget *box = (*name == 'h'
577 ? gtk_hbox_new (FALSE, 0)
578 : gtk_vbox_new (FALSE, 0));
580 gtk_widget_show (box);
583 gtk_table_attach (GTK_TABLE (parent), box, 0, 3, *row, *row + 1,
586 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, FALSE, 0);
591 list2 = make_parameters_1 (filename, node->xmlChildrenNode, box, 0);
593 list = g_list_concat (list, list2);
597 parameter *p = make_parameter (filename, node);
600 list = g_list_append (list, p);
601 make_parameter_widget (filename, p, parent, row);
609 /* Calls make_parameter() and make_parameter_widget() on each relevant
610 tag in the XML tree. Also handles the "hgroup" and "vgroup" flags.
611 Returns a GList of `parameter' objects.
614 make_parameters (const char *filename, xmlNodePtr node, GtkWidget *parent)
617 for (; node; node = node->next)
619 if (node->type == XML_ELEMENT_NODE &&
620 !strcmp ((char *) node->name, "screensaver"))
621 return make_parameters_1 (filename, node->xmlChildrenNode, parent, &row);
628 invert_range (gfloat low, gfloat high, gfloat value)
630 gfloat range = high-low;
631 gfloat off = value-low;
632 return (low + (range - off));
636 static GtkAdjustment *
637 make_adjustment (const char *filename, parameter *p)
639 float range = (p->high - p->low);
640 float value = (p->invert_p
641 ? invert_range (p->low, p->high, p->value)
643 gfloat si = (p->high - p->low) / 100;
644 gfloat pi = (p->high - p->low) / 10;
646 if (p->value < p->low || p->value > p->high)
648 if (debug_p && p->integer_p)
649 fprintf (stderr, "%s: WARNING: %s: %d is not in range [%d, %d]\n",
651 (int) p->value, (int) p->low, (int) p->high);
654 "%s: WARNING: %s: %.2f is not in range [%.2f, %.2f]\n",
655 blurb(), filename, p->value, p->low, p->high);
656 value = (value < p->low ? p->low : p->high);
659 else if (debug_p && p->value < 1000 && p->high >= 10000)
663 "%s: WARNING: %s: %d is suspicious for range [%d, %d]\n",
665 (int) p->value, (int) p->low, (int) p->high);
668 "%s: WARNING: %s: %.2f is suspicious for range [%.2f, %.2f]\n",
669 blurb(), filename, p->value, p->low, p->high);
673 si = (int) (si + 0.5);
674 pi = (int) (pi + 0.5);
678 if (range <= 500) si = 1;
680 return GTK_ADJUSTMENT (gtk_adjustment_new (value,
688 /* Given a `parameter' struct, allocates an appropriate GtkWidget for it,
689 and stores it in `p->widget'.
690 `row' is used for keeping track of our position during table layout.
691 `parent' must be a GtkTable or a GtkBox.
694 make_parameter_widget (const char *filename,
695 parameter *p, GtkWidget *parent, int *row)
697 const char *label = (char *) p->label;
698 if (p->widget) return;
706 GtkWidget *w = gtk_label_new (_(label));
707 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
708 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
711 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
714 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
717 p->widget = gtk_entry_new ();
719 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) p->string);
721 gtk_table_attach (GTK_TABLE (parent), p->widget, 1, 3,
725 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
730 GtkWidget *L = gtk_label_new (label ? _(label) : "");
731 GtkWidget *entry = gtk_entry_new ();
732 GtkWidget *button = gtk_button_new_with_label (_("Browse..."));
733 gtk_widget_show (entry);
734 gtk_widget_show (button);
737 gtk_signal_connect (GTK_OBJECT (button),
738 "clicked", GTK_SIGNAL_FUNC (browse_button_cb),
741 gtk_label_set_justify (GTK_LABEL (L), GTK_JUSTIFY_RIGHT);
742 gtk_misc_set_alignment (GTK_MISC (L), 1.0, 0.5);
746 gtk_entry_set_text (GTK_ENTRY (entry), (char *) p->string);
750 gtk_table_attach (GTK_TABLE (parent), L, 0, 1,
753 gtk_table_attach (GTK_TABLE (parent), entry, 1, 2,
755 GTK_EXPAND | GTK_FILL, 0, 0, 0);
756 gtk_table_attach (GTK_TABLE (parent), button, 2, 3,
762 gtk_box_pack_start (GTK_BOX (parent), L, FALSE, FALSE, 4);
763 gtk_box_pack_start (GTK_BOX (parent), entry, TRUE, TRUE, 4);
764 gtk_box_pack_start (GTK_BOX (parent), button, FALSE, FALSE, 4);
770 GtkAdjustment *adj = make_adjustment (filename, p);
771 GtkWidget *scale = gtk_hscale_new (adj);
772 GtkWidget *labelw = 0;
776 labelw = gtk_label_new (_(label));
777 gtk_label_set_justify (GTK_LABEL (labelw), GTK_JUSTIFY_LEFT);
778 gtk_misc_set_alignment (GTK_MISC (labelw), 0.0, 0.5);
779 gtk_widget_show (labelw);
782 if (GTK_IS_VBOX (parent))
784 /* If we're inside a vbox, we need to put an hbox in it, to get
785 the low/high labels to be to the left/right of the slider.
787 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
789 /* But if we have a label, put that above the slider's hbox. */
792 gtk_box_pack_start (GTK_BOX (parent), labelw, FALSE, TRUE, 2);
796 gtk_box_pack_start (GTK_BOX (parent), hbox, TRUE, TRUE, 6);
797 gtk_widget_show (hbox);
805 gtk_table_attach (GTK_TABLE (parent), labelw,
806 0, 3, *row, *row + 1,
807 GTK_EXPAND | GTK_FILL, 0, 0, 0);
812 if (GTK_IS_HBOX (parent))
814 GtkWidget *box = gtk_vbox_new (FALSE, 0);
815 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, TRUE, 0);
816 gtk_widget_show (box);
817 gtk_box_pack_start (GTK_BOX (box), labelw, FALSE, TRUE, 4);
819 box = gtk_hbox_new (FALSE, 0);
820 gtk_widget_show (box);
821 gtk_box_pack_start (GTK_BOX (parent), box, TRUE, TRUE, 0);
825 gtk_box_pack_start (GTK_BOX (parent), labelw,
832 GtkWidget *w = gtk_label_new (_((char *) p->low_label));
833 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
834 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
837 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
840 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
843 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM);
844 gtk_scale_set_draw_value (GTK_SCALE (scale), debug_p);
845 gtk_scale_set_digits (GTK_SCALE (scale), (p->integer_p ? 0 : 2));
847 gtk_table_attach (GTK_TABLE (parent), scale, 1, 2,
849 GTK_EXPAND | GTK_FILL, 0, 0, 0);
851 gtk_box_pack_start (GTK_BOX (parent), scale, TRUE, TRUE, 4);
853 gtk_widget_show (scale);
857 GtkWidget *w = gtk_label_new (_((char *) p->high_label));
858 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
859 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
862 gtk_table_attach (GTK_TABLE (parent), w, 2, 3, *row, *row + 1,
865 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
873 GtkAdjustment *adj = make_adjustment (filename, p);
874 GtkWidget *spin = gtk_spin_button_new (adj, 15, 0);
875 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
876 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (spin), TRUE);
877 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), adj->value);
881 GtkWidget *w = gtk_label_new (_(label));
882 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
883 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
886 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
889 gtk_box_pack_start (GTK_BOX (parent), w, TRUE, TRUE, 4);
892 gtk_widget_show (spin);
895 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
896 gtk_widget_show (hbox);
897 gtk_table_attach (GTK_TABLE (parent), hbox, 1, 3,
899 GTK_EXPAND | GTK_FILL, 0, 8, 0);
900 gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0);
903 gtk_box_pack_start (GTK_BOX (parent), spin, FALSE, FALSE, 4);
910 p->widget = gtk_check_button_new_with_label (_(label));
912 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
914 GTK_EXPAND | GTK_FILL, 0, 0, 0);
916 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
921 GtkWidget *opt = gtk_option_menu_new ();
922 GtkWidget *menu = gtk_menu_new ();
927 GtkWidget *w = gtk_label_new (_(label));
928 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
929 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
931 gtk_table_attach (GTK_TABLE (parent), w, 0, 3, *row, *row + 1,
932 GTK_EXPAND | GTK_FILL, 0, 0, 0);
936 for (opts = p->options; opts; opts = opts->next)
938 parameter *s = (parameter *) opts->data;
939 GtkWidget *i = gtk_menu_item_new_with_label (_((char *) s->label));
941 gtk_menu_append (GTK_MENU (menu), i);
944 gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
947 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
949 GTK_EXPAND | GTK_FILL, 0, 0, 0);
951 gtk_box_pack_start (GTK_BOX (parent), p->widget, TRUE, TRUE, 4);
966 gtk_widget_set_name (p->widget, (char *) p->id);
967 gtk_widget_show (p->widget);
975 Absurdly, there is no GTK file entry widget, only a GNOME one,
976 so in order to avoid depending on GNOME in this code, we have
980 /* cancel button on GtkFileSelection: user_data unused */
982 file_sel_cancel (GtkWidget *button, gpointer user_data)
984 GtkWidget *dialog = button;
985 while (dialog->parent)
986 dialog = dialog->parent;
987 gtk_widget_destroy (dialog);
990 /* ok button on GtkFileSelection: user_data is the corresponding GtkEntry */
992 file_sel_ok (GtkWidget *button, gpointer user_data)
994 GtkWidget *entry = GTK_WIDGET (user_data);
995 GtkWidget *dialog = button;
997 while (dialog->parent)
998 dialog = dialog->parent;
999 gtk_widget_hide (dialog);
1001 path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog));
1002 /* apparently one doesn't free `path' */
1004 gtk_entry_set_text (GTK_ENTRY (entry), path);
1005 gtk_entry_set_position (GTK_ENTRY (entry), strlen (path));
1007 gtk_widget_destroy (dialog);
1010 /* WM close on GtkFileSelection: user_data unused */
1012 file_sel_close (GtkWidget *widget, GdkEvent *event, gpointer user_data)
1014 file_sel_cancel (widget, user_data);
1017 /* "Browse" button: user_data is the corresponding GtkEntry */
1019 browse_button_cb (GtkButton *button, gpointer user_data)
1021 GtkWidget *entry = GTK_WIDGET (user_data);
1022 const char *text = gtk_entry_get_text (GTK_ENTRY (entry));
1023 GtkFileSelection *selector =
1024 GTK_FILE_SELECTION (gtk_file_selection_new (_("Select file.")));
1026 gtk_file_selection_set_filename (selector, text);
1027 gtk_signal_connect (GTK_OBJECT (selector->ok_button),
1028 "clicked", GTK_SIGNAL_FUNC (file_sel_ok),
1030 gtk_signal_connect (GTK_OBJECT (selector->cancel_button),
1031 "clicked", GTK_SIGNAL_FUNC (file_sel_cancel),
1033 gtk_signal_connect (GTK_OBJECT (selector), "delete_event",
1034 GTK_SIGNAL_FUNC (file_sel_close),
1037 gtk_window_set_modal (GTK_WINDOW (selector), TRUE);
1038 gtk_widget_show (GTK_WIDGET (selector));
1042 /* Converting to and from command-lines
1046 /* Returns a copy of string that has been quoted according to shell rules:
1047 it may have been wrapped in "" and had some characters backslashed; or
1048 it may be unchanged.
1051 shell_quotify (const char *string)
1053 char *string2 = (char *) malloc ((strlen (string) * 2) + 10);
1056 int need_quotes = 0;
1061 for (in = string; *in; in++)
1072 else if (*in <= ' ' ||
1098 return strdup (string);
1101 /* Modify the string in place to remove wrapping double-quotes
1102 and interior backslashes.
1105 de_stringify (char *s)
1108 if (q != '\'' && q != '\"' && q != '`')
1110 memmove (s, s+1, strlen (s)+1);
1111 while (*s && *s != q)
1114 memmove (s, s+1, strlen (s)+1);
1117 if (*s != q) abort();
1122 /* Substitutes a shell-quotified version of `value' into `p->arg' at
1123 the place where the `%' character appeared.
1126 format_switch (parameter *p, const char *value)
1128 char *fmt = (char *) p->arg;
1131 if (!fmt || !value) return 0;
1132 v2 = shell_quotify (value);
1133 result = (char *) malloc (strlen (fmt) + strlen (v2) + 10);
1150 /* Maps a `parameter' to a command-line switch.
1151 Returns 0 if it can't, or if the parameter has the default value.
1154 parameter_to_switch (parameter *p)
1160 return strdup ((char *) p->arg);
1166 if (!p->widget) return 0;
1168 const char *s = gtk_entry_get_text (GTK_ENTRY (p->widget));
1170 if (!strcmp ((s ? s : ""),
1171 (p->string ? (char *) p->string : "")))
1172 v = 0; /* same as default */
1174 v = format_switch (p, s);
1176 /* don't free `s' */
1181 if (!p->widget) return 0;
1183 GtkAdjustment *adj =
1185 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1186 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1189 float value = (p->invert_p
1190 ? invert_range (adj->lower, adj->upper, adj->value) - 1
1193 if (value == p->value) /* same as default */
1197 sprintf (buf, "%d", (int) (value + 0.5));
1199 sprintf (buf, "%.4f", value);
1201 s1 = strchr (buf, '.');
1204 char *s2 = s1 + strlen(s1) - 1;
1205 while (s2 > s1 && *s2 == '0') /* lose trailing zeroes */
1207 if (s2 >= s1 && *s2 == '.') /* lose trailing decimal */
1210 return format_switch (p, buf);
1213 if (!p->widget) return 0;
1215 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1216 const char *s = (gtk_toggle_button_get_active (b)
1217 ? (char *) p->arg_set
1218 : (char *) p->arg_unset);
1225 if (!p->widget) return 0;
1227 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1228 GtkMenu *menu = GTK_MENU (gtk_option_menu_get_menu (opt));
1229 GtkWidget *selected = gtk_menu_get_active (menu);
1230 GList *kids = gtk_container_children (GTK_CONTAINER (menu));
1231 int menu_elt = g_list_index (kids, (gpointer) selected);
1232 GList *ol = g_list_nth (p->options, menu_elt);
1233 parameter *o = (ol ? (parameter *) ol->data : 0);
1236 if (o->type != SELECT_OPTION) abort();
1237 s = (char *) o->arg_set;
1251 /* Maps a GList of `parameter' objects to a complete command-line string.
1252 All arguments will be properly quoted.
1255 parameters_to_cmd_line (GList *parms)
1257 int L = g_list_length (parms);
1259 char **strs = (char **) calloc (sizeof (*parms), L);
1264 for (i = 0; parms; parms = parms->next, i++)
1266 char *s = parameter_to_switch ((parameter *) parms->data);
1268 LL += (s ? strlen(s) : 0) + 1;
1271 result = (char *) malloc (LL + 10);
1273 for (i = 0; i < L; i++)
1276 strcpy (out, strs[i]);
1277 out += strlen (out);
1282 while (out > result && out[-1] == ' ') /* strip trailing spaces */
1290 /* Returns a GList of the tokens the string, using shell syntax;
1291 Quoted strings are handled as a single token.
1294 tokenize_command_line (const char *cmd)
1297 const char *s = cmd;
1302 for (; isspace(*s); s++); /* skip whitespace */
1305 if (*s == '\'' || *s == '\"' || *s == '`')
1309 while (*s && *s != q) /* skip to matching quote */
1311 if (*s == '\\' && s[1]) /* allowing backslash quoting */
1329 ss = (char *) malloc ((s - start) + 1);
1330 strncpy (ss, start, s-start);
1332 if (*ss == '\'' || *ss == '\"' || *ss == '`')
1334 result = g_list_append (result, ss);
1341 static void parameter_set_switch (parameter *, gpointer value);
1342 static gboolean parse_command_line_into_parameters_1 (const char *filename,
1349 /* Parses the command line, and flushes those options down into
1350 the `parameter' structs in the list.
1353 parse_command_line_into_parameters (const char *filename,
1354 const char *cmd, GList *parms)
1356 GList *tokens = tokenize_command_line (cmd);
1358 for (rest = tokens; rest; rest = rest->next)
1360 char *option = rest->data;
1363 if (option[0] != '-')
1366 fprintf (stderr, "%s: WARNING: %s: not a switch: \"%s\"\n",
1367 blurb(), filename, option);
1373 if (rest->next) /* pop off the arg to this option */
1375 char *s = (char *) rest->next->data;
1376 /* the next token is the next switch iff it matches "-[a-z]".
1377 (To avoid losing on "-x -3.1".)
1379 if (s && (s[0] != '-' || !isalpha(s[1])))
1382 rest->next->data = 0;
1387 parse_command_line_into_parameters_1 (filename, parms,
1389 if (value) free (value);
1393 g_list_free (tokens);
1398 compare_opts (const char *option, const char *value,
1399 const char *template)
1401 int ol = strlen (option);
1404 if (strncmp (option, template, ol))
1407 if (template[ol] != (value ? ' ' : 0))
1410 /* At this point, we have a match against "option".
1411 If template contains a %, we're done.
1412 Else, compare against "value" too.
1414 c = strchr (template, '%');
1419 return (template[ol] == 0);
1420 if (strcmp (template + ol + 1, value))
1428 parse_command_line_into_parameters_1 (const char *filename,
1435 parameter *match = 0;
1439 for (p = parms; p; p = p->next)
1441 parameter *pp = (parameter *) p->data;
1444 if (pp->type == SELECT)
1446 if (parse_command_line_into_parameters_1 (filename,
1457 if (compare_opts (option, value, (char *) pp->arg))
1463 else if (pp->arg_set)
1465 if (compare_opts (option, value, (char *) pp->arg_set))
1471 else if (pp->arg_unset)
1473 if (compare_opts (option, value, (char *) pp->arg_unset))
1488 if (debug_p && !parent)
1489 fprintf (stderr, "%s: WARNING: %s: no match for %s %s\n",
1490 blurb(), filename, option, (value ? value : ""));
1494 switch (match->type)
1500 if (which != -1) abort();
1501 parameter_set_switch (match, (gpointer) value);
1504 if (which != 0 && which != 1) abort();
1505 parameter_set_switch (match, GINT_TO_POINTER(which));
1508 if (which != 1) abort();
1509 parameter_set_switch (parent, GINT_TO_POINTER(index));
1518 /* Set the parameter's value.
1519 For STRING, FILENAME, SLIDER, and SPINBUTTON, `value' is a char*.
1520 For BOOLEAN and SELECT, `value' is an int.
1523 parameter_set_switch (parameter *p, gpointer value)
1525 if (p->type == SELECT_OPTION) abort();
1526 if (!p->widget) return;
1532 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) value);
1538 GtkAdjustment *adj =
1540 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1541 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1545 if (1 == sscanf ((char *) value, "%f %c", &f, &c))
1548 f = invert_range (adj->lower, adj->upper, f) - 1;
1549 gtk_adjustment_set_value (adj, f);
1555 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1556 gtk_toggle_button_set_active (b, GPOINTER_TO_INT(value));
1561 gtk_option_menu_set_history (GTK_OPTION_MENU (p->widget),
1562 GPOINTER_TO_INT(value));
1572 restore_defaults (const char *progname, GList *parms)
1574 for (; parms; parms = parms->next)
1576 parameter *p = (parameter *) parms->data;
1577 if (!p->widget) continue;
1583 gtk_entry_set_text (GTK_ENTRY (p->widget),
1584 (p->string ? (char *) p->string : ""));
1590 GtkAdjustment *adj =
1592 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1593 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1594 float value = (p->invert_p
1595 ? invert_range (p->low, p->high, p->value)
1597 gtk_adjustment_set_value (adj, value);
1602 /* A toggle button should be on by default if it inserts
1603 nothing into the command line when on. E.g., it should
1604 be on if `arg_set' is null.
1606 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->widget),
1607 (!p->arg_set || !*p->arg_set));
1612 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1617 for (opts = p->options, index = 0; opts;
1618 opts = opts->next, index++)
1620 parameter *s = (parameter *) opts->data;
1621 /* The default menu item is the first one with
1622 no `arg_set' field. */
1630 gtk_option_menu_set_history (GTK_OPTION_MENU (opt), selected);
1641 /* Documentation strings
1645 get_description (GList *parms, gboolean verbose_p)
1648 for (; parms; parms = parms->next)
1650 parameter *p = (parameter *) parms->data;
1651 if (p->type == DESCRIPTION)
1658 if (!doc || !doc->string)
1662 char *d = strdup ((char *) doc->string);
1665 for (s = d; *s; s++)
1668 if (s[1] == '\n') /* blank line: leave it */
1670 else if (s[1] == ' ' || s[1] == '\t')
1671 s++; /* next line is indented: leave newline */
1673 s[0] = ' '; /* delete newline to un-fold this line */
1676 /* strip off leading whitespace on first line only */
1677 for (s = d; *s && (*s == ' ' || *s == '\t'); s++)
1679 while (*s == '\n') /* strip leading newlines */
1682 memmove (d, s, strlen(s)+1);
1684 /* strip off trailing whitespace and newlines */
1687 while (L && isspace(d[L-1]))
1691 /* strip off duplicated whitespaces */
1692 for (s = d; *s; s++)
1699 memmove (p, s, strlen(s)+1);
1705 fprintf (stderr, "%s: text read is \"%s\"\n", blurb(),doc->string);
1706 fprintf (stderr, "%s: description is \"%s\"\n", blurb(), d);
1707 fprintf (stderr, "%s: translation is \"%s\"\n", blurb(), _(d));
1716 /* External interface.
1720 load_configurator_1 (const char *program, const char *arguments,
1723 const char *dir = hack_configuration_path;
1724 int L = strlen (dir);
1730 if (L == 0) return 0;
1732 file = (char *) malloc (L + strlen (program) + 10);
1733 data = (conf_data *) calloc (1, sizeof(*data));
1736 if (file[L-1] != '/')
1738 strcpy (file+L, program);
1740 for (s = file+L; *s; s++)
1741 if (*s == '/' || *s == ' ')
1743 else if (isupper (*s))
1746 strcat (file+L, ".xml");
1748 f = fopen (file, "r");
1751 int res, size = 1024;
1753 xmlParserCtxtPtr ctxt;
1759 fprintf (stderr, "%s: reading %s...\n", blurb(), file);
1761 res = fread (chars, 1, 4, f);
1769 ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, file);
1770 while ((res = fread(chars, 1, size, f)) > 0)
1771 xmlParseChunk (ctxt, chars, res, 0);
1772 xmlParseChunk (ctxt, chars, 0, 1);
1774 xmlFreeParserCtxt (ctxt);
1777 /* Parsed the XML file. Now make some widgets. */
1779 table = gtk_table_new (1, 3, FALSE);
1780 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
1781 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
1782 gtk_container_set_border_width (GTK_CONTAINER (table), 8);
1783 gtk_widget_show (table);
1785 parms = make_parameters (file, doc->xmlRootNode, table);
1789 restore_defaults (program, parms);
1790 if (arguments && *arguments)
1791 parse_command_line_into_parameters (program, arguments, parms);
1793 data->widget = table;
1794 data->parameters = parms;
1795 data->description = get_description (parms, verbose_p);
1802 fprintf (stderr, "%s: %s does not exist.\n", blurb(), file);
1804 p = calloc (1, sizeof(*p));
1806 p->arg = (xmlChar *) strdup (arguments);
1808 data->parameters = g_list_append (0, (gpointer) p);
1811 data->progname = strdup (program);
1819 split_command_line (const char *full_command_line,
1820 char **prog_ret, char **args_ret)
1822 char *line = strdup (full_command_line);
1835 while (isspace (*s)) s++;
1838 else if (*s == '=') /* if the leading word contains an "=", skip it. */
1840 while (*s && !isspace (*s)) s++;
1841 while (isspace (*s)) s++;
1848 *prog_ret = strdup (prog);
1849 *args_ret = strdup (args);
1855 load_configurator (const char *full_command_line, gboolean verbose_p)
1860 debug_p = verbose_p;
1861 split_command_line (full_command_line, &prog, &args);
1862 cd = load_configurator_1 (prog, args, verbose_p);
1871 get_configurator_command_line (conf_data *data)
1873 char *args = parameters_to_cmd_line (data->parameters);
1874 char *result = (char *) malloc (strlen (data->progname) +
1876 strcpy (result, data->progname);
1877 strcat (result, " ");
1878 strcat (result, args);
1885 set_configurator_command_line (conf_data *data, const char *full_command_line)
1889 split_command_line (full_command_line, &prog, &args);
1890 if (data->progname) free (data->progname);
1891 data->progname = prog;
1892 restore_defaults (prog, data->parameters);
1893 parse_command_line_into_parameters (prog, args, data->parameters);
1898 free_conf_data (conf_data *data)
1900 if (data->parameters)
1903 for (rest = data->parameters; rest; rest = rest->next)
1905 free_parameter ((parameter *) rest->data);
1908 g_list_free (data->parameters);
1909 data->parameters = 0;
1913 gtk_widget_destroy (data->widget);
1916 free (data->progname);
1917 if (data->description)
1918 free (data->description);
1920 memset (data, ~0, sizeof(*data));
1925 #endif /* HAVE_GTK && HAVE_XML -- whole file */