1 /* demo-Gtk-conf.c --- implements the dynamic configuration dialogs.
2 * xscreensaver, Copyright (c) 2001, 2003, 2004, 2006 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 */
129 static parameter *make_select_option (const char *file, xmlNodePtr);
130 static void make_parameter_widget (const char *filename,
131 parameter *, GtkWidget *, int *);
132 static void browse_button_cb (GtkButton *button, gpointer user_data);
135 /* Frees the parameter object and all strings and sub-parameters.
136 Does not destroy the widget, if any.
139 free_parameter (parameter *p)
142 if (p->id) free (p->id);
143 if (p->label) free (p->label);
144 if (p->string) free (p->string);
145 if (p->low_label) free (p->low_label);
146 if (p->high_label) free (p->high_label);
147 if (p->arg) free (p->arg);
148 if (p->arg_set) free (p->arg_set);
149 if (p->arg_unset) free (p->arg_unset);
151 for (rest = p->options; rest; rest = rest->next)
153 free_parameter ((parameter *) rest->data);
155 memset (p, ~0, sizeof(*p));
160 /* Debugging: dumps out a `parameter' structure.
164 describe_parameter (FILE *out, parameter *p)
169 case COMMAND: fprintf (out, "command"); break;
170 case FAKE: fprintf (out, "fake"); break;
171 case DESCRIPTION: fprintf (out, "_description"); break;
172 case FAKEPREVIEW: fprintf (out, "fakepreview"); break;
173 case STRING: fprintf (out, "string"); break;
174 case FILENAME: fprintf (out, "filename"); break;
175 case SLIDER: fprintf (out, "number type=\"slider\""); break;
176 case SPINBUTTON: fprintf (out, "number type=\"spinbutton\""); break;
177 case BOOLEAN: fprintf (out, "boolean"); break;
178 case SELECT: fprintf (out, "select"); break;
179 default: abort(); break;
181 if (p->id) fprintf (out, " id=\"%s\"", p->id);
182 if (p->label) fprintf (out, " _label=\"%s\"", p->label);
183 if (p->string && p->type != DESCRIPTION)
184 fprintf (out, " string=\"%s\"", p->string);
185 if (p->low_label) fprintf (out, " _low-label=\"%s\"", p->low_label);
186 if (p->high_label) fprintf (out, " _high-label=\"%s\"", p->high_label);
187 if (p->low) fprintf (out, " low=\"%.2f\"", p->low);
188 if (p->high) fprintf (out, " high=\"%.2f\"", p->high);
189 if (p->value) fprintf (out, " default=\"%.2f\"", p->value);
190 if (p->arg) fprintf (out, " arg=\"%s\"", p->arg);
191 if (p->invert_p) fprintf (out, " convert=\"invert\"");
192 if (p->arg_set) fprintf (out, " arg-set=\"%s\"", p->arg_set);
193 if (p->arg_unset) fprintf (out, " arg-unset=\"%s\"", p->arg_unset);
194 fprintf (out, ">\n");
196 if (p->type == SELECT)
199 for (opt = p->options; opt; opt = opt->next)
201 parameter *o = (parameter *) opt->data;
202 if (o->type != SELECT_OPTION) abort();
203 fprintf (out, " <option");
204 if (o->id) fprintf (out, " id=\"%s\"", o->id);
205 if (o->label) fprintf (out, " _label=\"%s\"", o->label);
206 if (o->arg_set) fprintf (out, " arg-set=\"%s\"", o->arg_set);
207 if (o->arg_unset) fprintf (out, " arg-unset=\"%s\"", o->arg_unset);
208 fprintf (out, ">\n");
210 fprintf (out, "</select>\n");
212 else if (p->type == DESCRIPTION)
215 fprintf (out, " %s\n", p->string);
216 fprintf (out, "</_description>\n");
222 /* Like xmlGetProp() but parses a float out of the string.
223 If the number was expressed as a float and not an integer
224 (that is, the string contained a decimal point) then
225 `floatp' is set to TRUE. Otherwise, it is unchanged.
228 xml_get_float (xmlNodePtr node, const xmlChar *name, gboolean *floatpP)
230 const char *s = (char *) xmlGetProp (node, name);
233 if (!s || 1 != sscanf (s, "%f %c", &f, &c))
237 if (strchr (s, '.')) *floatpP = TRUE;
243 static void sanity_check_parameter (const char *filename,
244 const xmlChar *node_name,
246 static void sanity_check_text_node (const char *filename,
247 const xmlNodePtr node);
248 static void sanity_check_menu_options (const char *filename,
249 const xmlChar *node_name,
252 /* Allocates and returns a new `parameter' object based on the
253 properties in the given XML node. Returns 0 if there's nothing
254 to create (comment, or unknown tag.)
257 make_parameter (const char *filename, xmlNodePtr node)
260 const char *name = (char *) node->name;
262 gboolean floatp = FALSE;
264 if (node->type == XML_COMMENT_NODE)
267 p = calloc (1, sizeof(*p));
270 else if (!strcmp (name, "command")) p->type = COMMAND;
271 else if (!strcmp (name, "fullcommand")) p->type = COMMAND;
272 else if (!strcmp (name, "_description")) p->type = DESCRIPTION;
273 else if (!strcmp (name, "fakepreview")) p->type = FAKEPREVIEW;
274 else if (!strcmp (name, "fake")) p->type = FAKE;
275 else if (!strcmp (name, "boolean")) p->type = BOOLEAN;
276 else if (!strcmp (name, "string")) p->type = STRING;
277 else if (!strcmp (name, "file")) p->type = FILENAME;
278 else if (!strcmp (name, "number")) p->type = SPINBUTTON;
279 else if (!strcmp (name, "select")) p->type = SELECT;
281 else if (!strcmp (name, "xscreensaver-text") || /* these are ignored in X11 */
282 !strcmp (name, "xscreensaver-image")) /* (they are used in Cocoa) */
287 else if (node->type == XML_TEXT_NODE)
289 sanity_check_text_node (filename, node);
296 fprintf (stderr, "%s: WARNING: %s: unknown tag: \"%s\"\n",
297 blurb(), filename, name);
302 if (p->type == SPINBUTTON)
304 const char *type = (char *) xmlGetProp (node, (xmlChar *) "type");
305 if (!type || !strcmp (type, "spinbutton")) p->type = SPINBUTTON;
306 else if (!strcmp (type, "slider")) p->type = SLIDER;
310 fprintf (stderr, "%s: WARNING: %s: unknown %s type: \"%s\"\n",
311 blurb(), filename, name, type);
316 else if (p->type == DESCRIPTION)
318 if (node->xmlChildrenNode &&
319 node->xmlChildrenNode->type == XML_TEXT_NODE &&
320 !node->xmlChildrenNode->next)
321 p->string = (xmlChar *)
322 strdup ((char *) node->xmlChildrenNode->content);
325 p->id = xmlGetProp (node, (xmlChar *) "id");
326 p->label = xmlGetProp (node, (xmlChar *) "_label");
327 p->low_label = xmlGetProp (node, (xmlChar *) "_low-label");
328 p->high_label = xmlGetProp (node, (xmlChar *) "_high-label");
329 p->low = xml_get_float (node, (xmlChar *) "low", &floatp);
330 p->high = xml_get_float (node, (xmlChar *) "high", &floatp);
331 p->value = xml_get_float (node, (xmlChar *) "default", &floatp);
332 p->integer_p = !floatp;
333 convert = (char *) xmlGetProp (node, (xmlChar *) "convert");
334 p->invert_p = (convert && !strcmp (convert, "invert"));
335 p->arg = xmlGetProp (node, (xmlChar *) "arg");
336 p->arg_set = xmlGetProp (node, (xmlChar *) "arg-set");
337 p->arg_unset = xmlGetProp (node, (xmlChar *) "arg-unset");
339 /* Check for missing decimal point */
342 (p->high != p->low) &&
343 (p->high - p->low) <= 1)
345 "%s: WARNING: %s: %s: range [%.1f, %.1f] shouldn't be integral!\n",
346 blurb(), filename, p->id,
349 if (p->type == SELECT)
352 for (kids = node->xmlChildrenNode; kids; kids = kids->next)
354 parameter *s = make_select_option (filename, kids);
356 p->options = g_list_append (p->options, s);
360 sanity_check_parameter (filename, (const xmlChar *) name, p);
366 /* Allocates and returns a new SELECT_OPTION `parameter' object based
367 on the properties in the given XML node. Returns 0 if there's nothing
368 to create (comment, or unknown tag.)
371 make_select_option (const char *filename, xmlNodePtr node)
373 if (node->type == XML_COMMENT_NODE)
375 else if (node->type == XML_TEXT_NODE)
377 sanity_check_text_node (filename, node);
380 else if (node->type != XML_ELEMENT_NODE)
384 "%s: WARNING: %s: %s: unexpected child tag type %d\n",
385 blurb(), filename, node->name, (int)node->type);
388 else if (strcmp ((char *) node->name, "option"))
392 "%s: WARNING: %s: %s: child not an option tag: \"%s\"\n",
393 blurb(), filename, node->name, node->name);
398 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");
406 sanity_check_parameter (filename, node->name, s);
412 /* Rudimentary check to make sure someone hasn't typed "arg-set="
413 when they should have typed "arg=", etc.
416 sanity_check_parameter (const char *filename, const xmlChar *node_name,
434 memset (&allowed, 0, sizeof (allowed));
435 memset (&require, 0, sizeof (require));
446 allowed.string = TRUE;
453 allowed.label = TRUE;
454 require.label = TRUE;
461 allowed.label = TRUE;
468 allowed.label = TRUE;
469 allowed.low_label = TRUE;
470 allowed.high_label = TRUE;
474 /* require.low = TRUE; -- may be 0 */
476 /* require.high = TRUE; -- may be 0 */
477 allowed.value = TRUE;
478 /* require.value = TRUE; -- may be 0 */
479 allowed.invert_p = TRUE;
484 allowed.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;
499 allowed.arg_set = TRUE;
500 allowed.arg_unset = TRUE;
508 allowed.label = TRUE;
509 require.label = TRUE;
510 allowed.arg_set = TRUE;
518 fprintf (stderr, "%s: %s: " STR " in <%s%s id=\"%s\">\n", \
519 blurb(), filename, node_name, \
520 (!strcmp((char *) node_name, "number") \
521 ? (p->type == SPINBUTTON ? " type=spinbutton" : " type=slider")\
523 (p->id ? (char *) p->id : ""))
524 # define CHECK(SLOT,NAME) \
525 if (p->SLOT && !allowed.SLOT) \
526 WARN ("\"" NAME "\" is not a valid option"); \
527 if (!p->SLOT && require.SLOT) \
528 WARN ("\"" NAME "\" is required")
531 CHECK (label, "_label");
532 CHECK (string, "(body text)");
533 CHECK (low_label, "_low-label");
534 CHECK (high_label, "_high-label");
536 CHECK (high, "high");
537 CHECK (value, "default");
539 CHECK (invert_p, "convert");
540 CHECK (arg_set, "arg-set");
541 CHECK (arg_unset, "arg-unset");
545 if (p->type == SELECT)
546 sanity_check_menu_options (filename, node_name, p);
551 sanity_check_menu_options (const char *filename, const xmlChar *node_name,
559 /* fprintf (stderr, "\n## %s\n", p->id);*/
560 for (opts = p->options; opts; opts = opts->next)
562 parameter *s = (parameter *) opts->data;
563 if (!s->arg_set) nulls++;
568 char *a = strdup ((char *) s->arg_set);
569 char *spc = strchr (a, ' ');
573 if (strcmp (a, prefix))
575 "%s: %s: both \"%s\" and \"%s\" used in <select id=\"%s\">\n",
576 blurb(), filename, prefix, a, p->id);
582 /* fprintf (stderr, "\n %s\n", s->arg_set);*/
585 if (prefix) free (prefix);
589 "%s: %s: more than one menu with no arg-set in <select id=\"%s\">\n",
590 blurb(), filename, p->id);
594 /* "text" nodes show up for all the non-tag text in the file, including
595 all the newlines between tags. Warn if there is text there that
599 sanity_check_text_node (const char *filename, const xmlNodePtr node)
601 const char *body = (const char *) node->content;
602 if (node->type != XML_TEXT_NODE) abort();
603 while (isspace (*body)) body++;
605 fprintf (stderr, "%s: WARNING: %s: random text present: \"%s\"\n",
606 blurb(), filename, body);
610 /* Returns a list of strings, every switch mentioned in the parameters.
611 The strings must be freed.
614 get_all_switches (const char *filename, GList *parms)
618 for (p = parms; p; p = p->next)
620 parameter *pp = (parameter *) p->data;
622 if (pp->type == SELECT)
624 GList *list2 = get_all_switches (filename, pp->options);
625 switches = g_list_concat (switches, list2);
627 if (pp->arg && *pp->arg)
628 switches = g_list_append (switches, strdup ((char *) pp->arg));
629 if (pp->arg_set && *pp->arg_set)
630 switches = g_list_append (switches, strdup ((char *) pp->arg_set));
631 if (pp->arg_unset && *pp->arg_unset)
632 switches = g_list_append (switches, strdup ((char *) pp->arg_unset));
638 /* Ensures that no switch is mentioned more than once.
641 sanity_check_parameters (const char *filename, GList *parms)
643 GList *list = get_all_switches (filename, parms);
645 for (p = list; p; p = p->next)
647 char *sw = (char *) p->data;
650 if (*sw != '-' && *sw != '+')
651 fprintf (stderr, "%s: %s: switch does not begin with hyphen \"%s\"\n",
652 blurb(), filename, sw);
654 for (p2 = p->next; p2; p2 = p2->next)
656 const char *sw2 = (const char *) p2->data;
657 if (!strcmp (sw, sw2))
658 fprintf (stderr, "%s: %s: duplicate switch \"%s\"\n",
659 blurb(), filename, sw);
668 /* Helper for make_parameters()
671 make_parameters_1 (const char *filename, xmlNodePtr node,
672 GtkWidget *parent, int *row)
676 for (; node; node = node->next)
678 const char *name = (char *) node->name;
679 if (!strcmp (name, "hgroup") ||
680 !strcmp (name, "vgroup"))
682 GtkWidget *box = (*name == 'h'
683 ? gtk_hbox_new (FALSE, 0)
684 : gtk_vbox_new (FALSE, 0));
686 gtk_widget_show (box);
689 gtk_table_attach (GTK_TABLE (parent), box, 0, 3, *row, *row + 1,
692 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, FALSE, 0);
697 list2 = make_parameters_1 (filename, node->xmlChildrenNode, box, 0);
699 list = g_list_concat (list, list2);
703 parameter *p = make_parameter (filename, node);
706 list = g_list_append (list, p);
707 make_parameter_widget (filename, p, parent, row);
715 /* Calls make_parameter() and make_parameter_widget() on each relevant
716 tag in the XML tree. Also handles the "hgroup" and "vgroup" flags.
717 Returns a GList of `parameter' objects.
720 make_parameters (const char *filename, xmlNodePtr node, GtkWidget *parent)
723 for (; node; node = node->next)
725 if (node->type == XML_ELEMENT_NODE &&
726 !strcmp ((char *) node->name, "screensaver"))
727 return make_parameters_1 (filename, node->xmlChildrenNode,
735 invert_range (gfloat low, gfloat high, gfloat value)
737 gfloat range = high-low;
738 gfloat off = value-low;
739 return (low + (range - off));
743 static GtkAdjustment *
744 make_adjustment (const char *filename, parameter *p)
746 float range = (p->high - p->low);
747 float value = (p->invert_p
748 ? invert_range (p->low, p->high, p->value)
750 gfloat si = (p->high - p->low) / 100;
751 gfloat pi = (p->high - p->low) / 10;
752 gfloat page_size = ((p->type == SLIDER) ? 1 : 0);
754 if (p->value < p->low || p->value > p->high)
756 if (debug_p && p->integer_p)
757 fprintf (stderr, "%s: WARNING: %s: %d is not in range [%d, %d]\n",
759 (int) p->value, (int) p->low, (int) p->high);
762 "%s: WARNING: %s: %.2f is not in range [%.2f, %.2f]\n",
763 blurb(), filename, p->value, p->low, p->high);
764 value = (value < p->low ? p->low : p->high);
767 else if (debug_p && p->value < 1000 && p->high >= 10000)
771 "%s: WARNING: %s: %d is suspicious for range [%d, %d]\n",
773 (int) p->value, (int) p->low, (int) p->high);
776 "%s: WARNING: %s: %.2f is suspicious for range [%.2f, %.2f]\n",
777 blurb(), filename, p->value, p->low, p->high);
781 si = (int) (si + 0.5);
782 pi = (int) (pi + 0.5);
786 if (range <= 500) si = 1;
788 return GTK_ADJUSTMENT (gtk_adjustment_new (value,
796 /* Given a `parameter' struct, allocates an appropriate GtkWidget for it,
797 and stores it in `p->widget'.
798 `row' is used for keeping track of our position during table layout.
799 `parent' must be a GtkTable or a GtkBox.
802 make_parameter_widget (const char *filename,
803 parameter *p, GtkWidget *parent, int *row)
805 const char *label = (char *) p->label;
806 if (p->widget) return;
814 GtkWidget *w = gtk_label_new (_(label));
815 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
816 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
819 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
822 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
825 p->widget = gtk_entry_new ();
827 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) p->string);
829 gtk_table_attach (GTK_TABLE (parent), p->widget, 1, 3,
833 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
838 GtkWidget *L = gtk_label_new (label ? _(label) : "");
839 GtkWidget *entry = gtk_entry_new ();
840 GtkWidget *button = gtk_button_new_with_label (_("Browse..."));
841 gtk_widget_show (entry);
842 gtk_widget_show (button);
845 gtk_signal_connect (GTK_OBJECT (button),
846 "clicked", GTK_SIGNAL_FUNC (browse_button_cb),
849 gtk_label_set_justify (GTK_LABEL (L), GTK_JUSTIFY_RIGHT);
850 gtk_misc_set_alignment (GTK_MISC (L), 1.0, 0.5);
854 gtk_entry_set_text (GTK_ENTRY (entry), (char *) p->string);
858 gtk_table_attach (GTK_TABLE (parent), L, 0, 1,
861 gtk_table_attach (GTK_TABLE (parent), entry, 1, 2,
863 GTK_EXPAND | GTK_FILL, 0, 0, 0);
864 gtk_table_attach (GTK_TABLE (parent), button, 2, 3,
870 gtk_box_pack_start (GTK_BOX (parent), L, FALSE, FALSE, 4);
871 gtk_box_pack_start (GTK_BOX (parent), entry, TRUE, TRUE, 4);
872 gtk_box_pack_start (GTK_BOX (parent), button, FALSE, FALSE, 4);
878 GtkAdjustment *adj = make_adjustment (filename, p);
879 GtkWidget *scale = gtk_hscale_new (adj);
880 GtkWidget *labelw = 0;
884 labelw = gtk_label_new (_(label));
885 gtk_label_set_justify (GTK_LABEL (labelw), GTK_JUSTIFY_LEFT);
886 gtk_misc_set_alignment (GTK_MISC (labelw), 0.0, 0.5);
887 gtk_widget_show (labelw);
890 if (GTK_IS_VBOX (parent))
892 /* If we're inside a vbox, we need to put an hbox in it, to get
893 the low/high labels to be to the left/right of the slider.
895 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
897 /* But if we have a label, put that above the slider's hbox. */
900 gtk_box_pack_start (GTK_BOX (parent), labelw, FALSE, TRUE, 2);
904 gtk_box_pack_start (GTK_BOX (parent), hbox, TRUE, TRUE, 6);
905 gtk_widget_show (hbox);
913 gtk_table_attach (GTK_TABLE (parent), labelw,
914 0, 3, *row, *row + 1,
915 GTK_EXPAND | GTK_FILL, 0, 0, 0);
920 if (GTK_IS_HBOX (parent))
922 GtkWidget *box = gtk_vbox_new (FALSE, 0);
923 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, TRUE, 0);
924 gtk_widget_show (box);
925 gtk_box_pack_start (GTK_BOX (box), labelw, FALSE, TRUE, 4);
927 box = gtk_hbox_new (FALSE, 0);
928 gtk_widget_show (box);
929 gtk_box_pack_start (GTK_BOX (parent), box, TRUE, TRUE, 0);
933 gtk_box_pack_start (GTK_BOX (parent), labelw,
940 GtkWidget *w = gtk_label_new (_((char *) p->low_label));
941 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
942 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
945 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
948 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
951 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM);
952 gtk_scale_set_draw_value (GTK_SCALE (scale), debug_p);
953 gtk_scale_set_digits (GTK_SCALE (scale), (p->integer_p ? 0 : 2));
955 gtk_table_attach (GTK_TABLE (parent), scale, 1, 2,
957 GTK_EXPAND | GTK_FILL, 0, 0, 0);
959 gtk_box_pack_start (GTK_BOX (parent), scale, TRUE, TRUE, 4);
961 gtk_widget_show (scale);
965 GtkWidget *w = gtk_label_new (_((char *) p->high_label));
966 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
967 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
970 gtk_table_attach (GTK_TABLE (parent), w, 2, 3, *row, *row + 1,
973 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
981 GtkAdjustment *adj = make_adjustment (filename, p);
982 GtkWidget *spin = gtk_spin_button_new (adj, 15, 0);
983 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
984 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (spin), TRUE);
985 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), adj->value);
989 GtkWidget *w = gtk_label_new (_(label));
990 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
991 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
994 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
997 gtk_box_pack_start (GTK_BOX (parent), w, TRUE, TRUE, 4);
1000 gtk_widget_show (spin);
1003 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
1004 gtk_widget_show (hbox);
1005 gtk_table_attach (GTK_TABLE (parent), hbox, 1, 3,
1007 GTK_EXPAND | GTK_FILL, 0, 8, 0);
1008 gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0);
1011 gtk_box_pack_start (GTK_BOX (parent), spin, FALSE, FALSE, 4);
1018 p->widget = gtk_check_button_new_with_label (_(label));
1020 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
1022 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1024 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
1029 GtkWidget *opt = gtk_option_menu_new ();
1030 GtkWidget *menu = gtk_menu_new ();
1035 GtkWidget *w = gtk_label_new (_(label));
1036 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
1037 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
1038 gtk_widget_show (w);
1039 gtk_table_attach (GTK_TABLE (parent), w, 0, 3, *row, *row + 1,
1040 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1044 for (opts = p->options; opts; opts = opts->next)
1046 parameter *s = (parameter *) opts->data;
1047 GtkWidget *i = gtk_menu_item_new_with_label (_((char *) s->label));
1048 gtk_widget_show (i);
1049 gtk_menu_append (GTK_MENU (menu), i);
1052 gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
1055 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
1057 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1059 gtk_box_pack_start (GTK_BOX (parent), p->widget, TRUE, TRUE, 4);
1074 gtk_widget_set_name (p->widget, (char *) p->id);
1075 gtk_widget_show (p->widget);
1083 Absurdly, there is no GTK file entry widget, only a GNOME one,
1084 so in order to avoid depending on GNOME in this code, we have
1088 /* cancel button on GtkFileSelection: user_data unused */
1090 file_sel_cancel (GtkWidget *button, gpointer user_data)
1092 GtkWidget *dialog = button;
1093 while (dialog->parent)
1094 dialog = dialog->parent;
1095 gtk_widget_destroy (dialog);
1098 /* ok button on GtkFileSelection: user_data is the corresponding GtkEntry */
1100 file_sel_ok (GtkWidget *button, gpointer user_data)
1102 GtkWidget *entry = GTK_WIDGET (user_data);
1103 GtkWidget *dialog = button;
1105 while (dialog->parent)
1106 dialog = dialog->parent;
1107 gtk_widget_hide (dialog);
1109 path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog));
1110 /* apparently one doesn't free `path' */
1112 gtk_entry_set_text (GTK_ENTRY (entry), path);
1113 gtk_entry_set_position (GTK_ENTRY (entry), strlen (path));
1115 gtk_widget_destroy (dialog);
1118 /* WM close on GtkFileSelection: user_data unused */
1120 file_sel_close (GtkWidget *widget, GdkEvent *event, gpointer user_data)
1122 file_sel_cancel (widget, user_data);
1125 /* "Browse" button: user_data is the corresponding GtkEntry */
1127 browse_button_cb (GtkButton *button, gpointer user_data)
1129 GtkWidget *entry = GTK_WIDGET (user_data);
1130 const char *text = gtk_entry_get_text (GTK_ENTRY (entry));
1131 GtkFileSelection *selector =
1132 GTK_FILE_SELECTION (gtk_file_selection_new (_("Select file.")));
1134 gtk_file_selection_set_filename (selector, text);
1135 gtk_signal_connect (GTK_OBJECT (selector->ok_button),
1136 "clicked", GTK_SIGNAL_FUNC (file_sel_ok),
1138 gtk_signal_connect (GTK_OBJECT (selector->cancel_button),
1139 "clicked", GTK_SIGNAL_FUNC (file_sel_cancel),
1141 gtk_signal_connect (GTK_OBJECT (selector), "delete_event",
1142 GTK_SIGNAL_FUNC (file_sel_close),
1145 gtk_window_set_modal (GTK_WINDOW (selector), TRUE);
1146 gtk_widget_show (GTK_WIDGET (selector));
1150 /* Converting to and from command-lines
1154 /* Returns a copy of string that has been quoted according to shell rules:
1155 it may have been wrapped in "" and had some characters backslashed; or
1156 it may be unchanged.
1159 shell_quotify (const char *string)
1161 char *string2 = (char *) malloc ((strlen (string) * 2) + 10);
1164 int need_quotes = 0;
1169 for (in = string; *in; in++)
1180 else if (*in <= ' ' ||
1206 return strdup (string);
1209 /* Modify the string in place to remove wrapping double-quotes
1210 and interior backslashes.
1213 de_stringify (char *s)
1216 if (q != '\'' && q != '\"' && q != '`')
1218 memmove (s, s+1, strlen (s)+1);
1219 while (*s && *s != q)
1222 memmove (s, s+1, strlen (s)+1);
1225 if (*s != q) abort();
1230 /* Substitutes a shell-quotified version of `value' into `p->arg' at
1231 the place where the `%' character appeared.
1234 format_switch (parameter *p, const char *value)
1236 char *fmt = (char *) p->arg;
1239 if (!fmt || !value) return 0;
1240 v2 = shell_quotify (value);
1241 result = (char *) malloc (strlen (fmt) + strlen (v2) + 10);
1258 /* Maps a `parameter' to a command-line switch.
1259 Returns 0 if it can't, or if the parameter has the default value.
1262 parameter_to_switch (parameter *p)
1268 return strdup ((char *) p->arg);
1274 if (!p->widget) return 0;
1276 const char *s = gtk_entry_get_text (GTK_ENTRY (p->widget));
1278 if (!strcmp ((s ? s : ""),
1279 (p->string ? (char *) p->string : "")))
1280 v = 0; /* same as default */
1282 v = format_switch (p, s);
1284 /* don't free `s' */
1289 if (!p->widget) return 0;
1291 GtkAdjustment *adj =
1293 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1294 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1297 float value = (p->invert_p
1298 ? invert_range (adj->lower, adj->upper, adj->value) - 1
1301 if (value == p->value) /* same as default */
1305 sprintf (buf, "%d", (int) (value + (value > 0 ? 0.5 : -0.5)));
1307 sprintf (buf, "%.4f", value);
1309 s1 = strchr (buf, '.');
1312 char *s2 = s1 + strlen(s1) - 1;
1313 while (s2 > s1 && *s2 == '0') /* lose trailing zeroes */
1315 if (s2 >= s1 && *s2 == '.') /* lose trailing decimal */
1318 return format_switch (p, buf);
1321 if (!p->widget) return 0;
1323 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1324 const char *s = (gtk_toggle_button_get_active (b)
1325 ? (char *) p->arg_set
1326 : (char *) p->arg_unset);
1333 if (!p->widget) return 0;
1335 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1336 GtkMenu *menu = GTK_MENU (gtk_option_menu_get_menu (opt));
1337 GtkWidget *selected = gtk_menu_get_active (menu);
1338 GList *kids = gtk_container_children (GTK_CONTAINER (menu));
1339 int menu_elt = g_list_index (kids, (gpointer) selected);
1340 GList *ol = g_list_nth (p->options, menu_elt);
1341 parameter *o = (ol ? (parameter *) ol->data : 0);
1344 if (o->type != SELECT_OPTION) abort();
1345 s = (char *) o->arg_set;
1359 /* Maps a GList of `parameter' objects to a complete command-line string.
1360 All arguments will be properly quoted.
1363 parameters_to_cmd_line (GList *parms)
1365 int L = g_list_length (parms);
1367 char **strs = (char **) calloc (sizeof (*parms), L);
1372 for (i = 0; parms; parms = parms->next, i++)
1374 char *s = parameter_to_switch ((parameter *) parms->data);
1376 LL += (s ? strlen(s) : 0) + 1;
1379 result = (char *) malloc (LL + 10);
1381 for (i = 0; i < L; i++)
1384 strcpy (out, strs[i]);
1385 out += strlen (out);
1390 while (out > result && out[-1] == ' ') /* strip trailing spaces */
1398 /* Returns a GList of the tokens the string, using shell syntax;
1399 Quoted strings are handled as a single token.
1402 tokenize_command_line (const char *cmd)
1405 const char *s = cmd;
1410 for (; isspace(*s); s++); /* skip whitespace */
1413 if (*s == '\'' || *s == '\"' || *s == '`')
1417 while (*s && *s != q) /* skip to matching quote */
1419 if (*s == '\\' && s[1]) /* allowing backslash quoting */
1437 ss = (char *) malloc ((s - start) + 1);
1438 strncpy (ss, start, s-start);
1440 if (*ss == '\'' || *ss == '\"' || *ss == '`')
1442 result = g_list_append (result, ss);
1449 static void parameter_set_switch (parameter *, gpointer value);
1450 static gboolean parse_command_line_into_parameters_1 (const char *filename,
1457 /* Parses the command line, and flushes those options down into
1458 the `parameter' structs in the list.
1461 parse_command_line_into_parameters (const char *filename,
1462 const char *cmd, GList *parms)
1464 GList *tokens = tokenize_command_line (cmd);
1466 for (rest = tokens; rest; rest = rest->next)
1468 char *option = rest->data;
1471 if (option[0] != '-' && option[0] != '+')
1474 fprintf (stderr, "%s: WARNING: %s: not a switch: \"%s\"\n",
1475 blurb(), filename, option);
1481 if (rest->next) /* pop off the arg to this option */
1483 char *s = (char *) rest->next->data;
1484 /* the next token is the next switch iff it matches "-[a-z]".
1485 (To avoid losing on "-x -3.1".)
1487 if (s && (s[0] != '-' || !isalpha(s[1])))
1490 rest->next->data = 0;
1495 parse_command_line_into_parameters_1 (filename, parms,
1497 if (value) free (value);
1501 g_list_free (tokens);
1506 compare_opts (const char *option, const char *value,
1507 const char *template)
1509 int ol = strlen (option);
1512 if (strncmp (option, template, ol))
1515 if (template[ol] != (value ? ' ' : 0))
1518 /* At this point, we have a match against "option".
1519 If template contains a %, we're done.
1520 Else, compare against "value" too.
1522 c = strchr (template, '%');
1527 return (template[ol] == 0);
1528 if (strcmp (template + ol + 1, value))
1536 parse_command_line_into_parameters_1 (const char *filename,
1543 parameter *match = 0;
1547 for (p = parms; p; p = p->next)
1549 parameter *pp = (parameter *) p->data;
1552 if (pp->type == SELECT)
1554 if (parse_command_line_into_parameters_1 (filename,
1565 if (compare_opts (option, value, (char *) pp->arg))
1571 else if (pp->arg_set)
1573 if (compare_opts (option, value, (char *) pp->arg_set))
1579 else if (pp->arg_unset)
1581 if (compare_opts (option, value, (char *) pp->arg_unset))
1596 if (debug_p && !parent)
1597 fprintf (stderr, "%s: WARNING: %s: no match for %s %s\n",
1598 blurb(), filename, option, (value ? value : ""));
1602 switch (match->type)
1608 if (which != -1) abort();
1609 parameter_set_switch (match, (gpointer) value);
1612 if (which != 0 && which != 1) abort();
1613 parameter_set_switch (match, GINT_TO_POINTER(which));
1616 if (which != 1) abort();
1617 parameter_set_switch (parent, GINT_TO_POINTER(index));
1626 /* Set the parameter's value.
1627 For STRING, FILENAME, SLIDER, and SPINBUTTON, `value' is a char*.
1628 For BOOLEAN and SELECT, `value' is an int.
1631 parameter_set_switch (parameter *p, gpointer value)
1633 if (p->type == SELECT_OPTION) abort();
1634 if (!p->widget) return;
1640 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) value);
1646 GtkAdjustment *adj =
1648 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1649 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1653 if (1 == sscanf ((char *) value, "%f %c", &f, &c))
1656 f = invert_range (adj->lower, adj->upper, f) - 1;
1657 gtk_adjustment_set_value (adj, f);
1663 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1664 gtk_toggle_button_set_active (b, GPOINTER_TO_INT(value));
1669 gtk_option_menu_set_history (GTK_OPTION_MENU (p->widget),
1670 GPOINTER_TO_INT(value));
1680 restore_defaults (const char *progname, GList *parms)
1682 for (; parms; parms = parms->next)
1684 parameter *p = (parameter *) parms->data;
1685 if (!p->widget) continue;
1691 gtk_entry_set_text (GTK_ENTRY (p->widget),
1692 (p->string ? (char *) p->string : ""));
1698 GtkAdjustment *adj =
1700 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1701 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1702 float value = (p->invert_p
1703 ? invert_range (p->low, p->high, p->value)
1705 gtk_adjustment_set_value (adj, value);
1710 /* A toggle button should be on by default if it inserts
1711 nothing into the command line when on. E.g., it should
1712 be on if `arg_set' is null.
1714 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->widget),
1715 (!p->arg_set || !*p->arg_set));
1720 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1725 for (opts = p->options, index = 0; opts;
1726 opts = opts->next, index++)
1728 parameter *s = (parameter *) opts->data;
1729 /* The default menu item is the first one with
1730 no `arg_set' field. */
1738 gtk_option_menu_set_history (GTK_OPTION_MENU (opt), selected);
1749 /* Documentation strings
1753 get_description (GList *parms, gboolean verbose_p)
1756 for (; parms; parms = parms->next)
1758 parameter *p = (parameter *) parms->data;
1759 if (p->type == DESCRIPTION)
1766 if (!doc || !doc->string)
1770 char *d = strdup ((char *) doc->string);
1773 for (s = d; *s; s++)
1776 if (s[1] == '\n') /* blank line: leave it */
1778 else if (s[1] == ' ' || s[1] == '\t')
1779 s++; /* next line is indented: leave newline */
1781 s[0] = ' '; /* delete newline to un-fold this line */
1784 /* strip off leading whitespace on first line only */
1785 for (s = d; *s && (*s == ' ' || *s == '\t'); s++)
1787 while (*s == '\n') /* strip leading newlines */
1790 memmove (d, s, strlen(s)+1);
1792 /* strip off trailing whitespace and newlines */
1795 while (L && isspace(d[L-1]))
1799 /* strip off duplicated whitespaces */
1800 for (s = d; *s; s++)
1807 memmove (p, s, strlen(s)+1);
1813 fprintf (stderr, "%s: text read is \"%s\"\n", blurb(),doc->string);
1814 fprintf (stderr, "%s: description is \"%s\"\n", blurb(), d);
1815 fprintf (stderr, "%s: translation is \"%s\"\n", blurb(), _(d));
1824 /* External interface.
1828 load_configurator_1 (const char *program, const char *arguments,
1831 const char *dir = hack_configuration_path;
1833 int L = strlen (dir);
1839 if (L == 0) return 0;
1841 base_program = strrchr(program, '/');
1842 if (base_program) base_program++;
1843 if (!base_program) base_program = (char *) program;
1845 file = (char *) malloc (L + strlen (base_program) + 10);
1846 data = (conf_data *) calloc (1, sizeof(*data));
1849 if (file[L-1] != '/')
1851 strcpy (file+L, base_program);
1853 for (s = file+L; *s; s++)
1854 if (*s == '/' || *s == ' ')
1856 else if (isupper (*s))
1859 strcat (file+L, ".xml");
1861 f = fopen (file, "r");
1864 int res, size = 1024;
1866 xmlParserCtxtPtr ctxt;
1872 fprintf (stderr, "%s: reading %s...\n", blurb(), file);
1874 res = fread (chars, 1, 4, f);
1882 ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, file);
1883 while ((res = fread(chars, 1, size, f)) > 0)
1884 xmlParseChunk (ctxt, chars, res, 0);
1885 xmlParseChunk (ctxt, chars, 0, 1);
1887 xmlFreeParserCtxt (ctxt);
1890 /* Parsed the XML file. Now make some widgets. */
1892 table = gtk_table_new (1, 3, FALSE);
1893 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
1894 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
1895 gtk_container_set_border_width (GTK_CONTAINER (table), 8);
1896 gtk_widget_show (table);
1898 parms = make_parameters (file, doc->xmlRootNode, table);
1899 sanity_check_parameters (file, parms);
1903 restore_defaults (program, parms);
1904 if (arguments && *arguments)
1905 parse_command_line_into_parameters (program, arguments, parms);
1907 data->widget = table;
1908 data->parameters = parms;
1909 data->description = get_description (parms, verbose_p);
1916 fprintf (stderr, "%s: %s does not exist.\n", blurb(), file);
1918 p = calloc (1, sizeof(*p));
1920 p->arg = (xmlChar *) strdup (arguments);
1922 data->parameters = g_list_append (0, (gpointer) p);
1925 data->progname = strdup (program);
1933 split_command_line (const char *full_command_line,
1934 char **prog_ret, char **args_ret)
1936 char *line = strdup (full_command_line);
1949 while (isspace (*s)) s++;
1952 else if (*s == '=') /* if the leading word contains an "=", skip it. */
1954 while (*s && !isspace (*s)) s++;
1955 while (isspace (*s)) s++;
1962 *prog_ret = strdup (prog);
1963 *args_ret = strdup (args);
1969 load_configurator (const char *full_command_line, gboolean verbose_p)
1974 debug_p = verbose_p;
1975 split_command_line (full_command_line, &prog, &args);
1976 cd = load_configurator_1 (prog, args, verbose_p);
1985 get_configurator_command_line (conf_data *data)
1987 char *args = parameters_to_cmd_line (data->parameters);
1988 char *result = (char *) malloc (strlen (data->progname) +
1990 strcpy (result, data->progname);
1991 strcat (result, " ");
1992 strcat (result, args);
1999 set_configurator_command_line (conf_data *data, const char *full_command_line)
2003 split_command_line (full_command_line, &prog, &args);
2004 if (data->progname) free (data->progname);
2005 data->progname = prog;
2006 restore_defaults (prog, data->parameters);
2007 parse_command_line_into_parameters (prog, args, data->parameters);
2012 free_conf_data (conf_data *data)
2014 if (data->parameters)
2017 for (rest = data->parameters; rest; rest = rest->next)
2019 free_parameter ((parameter *) rest->data);
2022 g_list_free (data->parameters);
2023 data->parameters = 0;
2027 gtk_widget_destroy (data->widget);
2030 free (data->progname);
2031 if (data->description)
2032 free (data->description);
2034 memset (data, ~0, sizeof(*data));
2039 #endif /* HAVE_GTK && HAVE_XML -- whole file */