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;
753 if (p->value < p->low || p->value > p->high)
755 if (debug_p && p->integer_p)
756 fprintf (stderr, "%s: WARNING: %s: %d is not in range [%d, %d]\n",
758 (int) p->value, (int) p->low, (int) p->high);
761 "%s: WARNING: %s: %.2f is not in range [%.2f, %.2f]\n",
762 blurb(), filename, p->value, p->low, p->high);
763 value = (value < p->low ? p->low : p->high);
766 else if (debug_p && p->value < 1000 && p->high >= 10000)
770 "%s: WARNING: %s: %d is suspicious for range [%d, %d]\n",
772 (int) p->value, (int) p->low, (int) p->high);
775 "%s: WARNING: %s: %.2f is suspicious for range [%.2f, %.2f]\n",
776 blurb(), filename, p->value, p->low, p->high);
780 si = (int) (si + 0.5);
781 pi = (int) (pi + 0.5);
785 if (range <= 500) si = 1;
787 return GTK_ADJUSTMENT (gtk_adjustment_new (value,
795 /* Given a `parameter' struct, allocates an appropriate GtkWidget for it,
796 and stores it in `p->widget'.
797 `row' is used for keeping track of our position during table layout.
798 `parent' must be a GtkTable or a GtkBox.
801 make_parameter_widget (const char *filename,
802 parameter *p, GtkWidget *parent, int *row)
804 const char *label = (char *) p->label;
805 if (p->widget) return;
813 GtkWidget *w = gtk_label_new (_(label));
814 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
815 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
818 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
821 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
824 p->widget = gtk_entry_new ();
826 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) p->string);
828 gtk_table_attach (GTK_TABLE (parent), p->widget, 1, 3,
832 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
837 GtkWidget *L = gtk_label_new (label ? _(label) : "");
838 GtkWidget *entry = gtk_entry_new ();
839 GtkWidget *button = gtk_button_new_with_label (_("Browse..."));
840 gtk_widget_show (entry);
841 gtk_widget_show (button);
844 gtk_signal_connect (GTK_OBJECT (button),
845 "clicked", GTK_SIGNAL_FUNC (browse_button_cb),
848 gtk_label_set_justify (GTK_LABEL (L), GTK_JUSTIFY_RIGHT);
849 gtk_misc_set_alignment (GTK_MISC (L), 1.0, 0.5);
853 gtk_entry_set_text (GTK_ENTRY (entry), (char *) p->string);
857 gtk_table_attach (GTK_TABLE (parent), L, 0, 1,
860 gtk_table_attach (GTK_TABLE (parent), entry, 1, 2,
862 GTK_EXPAND | GTK_FILL, 0, 0, 0);
863 gtk_table_attach (GTK_TABLE (parent), button, 2, 3,
869 gtk_box_pack_start (GTK_BOX (parent), L, FALSE, FALSE, 4);
870 gtk_box_pack_start (GTK_BOX (parent), entry, TRUE, TRUE, 4);
871 gtk_box_pack_start (GTK_BOX (parent), button, FALSE, FALSE, 4);
877 GtkAdjustment *adj = make_adjustment (filename, p);
878 GtkWidget *scale = gtk_hscale_new (adj);
879 GtkWidget *labelw = 0;
883 labelw = gtk_label_new (_(label));
884 gtk_label_set_justify (GTK_LABEL (labelw), GTK_JUSTIFY_LEFT);
885 gtk_misc_set_alignment (GTK_MISC (labelw), 0.0, 0.5);
886 gtk_widget_show (labelw);
889 if (GTK_IS_VBOX (parent))
891 /* If we're inside a vbox, we need to put an hbox in it, to get
892 the low/high labels to be to the left/right of the slider.
894 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
896 /* But if we have a label, put that above the slider's hbox. */
899 gtk_box_pack_start (GTK_BOX (parent), labelw, FALSE, TRUE, 2);
903 gtk_box_pack_start (GTK_BOX (parent), hbox, TRUE, TRUE, 6);
904 gtk_widget_show (hbox);
912 gtk_table_attach (GTK_TABLE (parent), labelw,
913 0, 3, *row, *row + 1,
914 GTK_EXPAND | GTK_FILL, 0, 0, 0);
919 if (GTK_IS_HBOX (parent))
921 GtkWidget *box = gtk_vbox_new (FALSE, 0);
922 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, TRUE, 0);
923 gtk_widget_show (box);
924 gtk_box_pack_start (GTK_BOX (box), labelw, FALSE, TRUE, 4);
926 box = gtk_hbox_new (FALSE, 0);
927 gtk_widget_show (box);
928 gtk_box_pack_start (GTK_BOX (parent), box, TRUE, TRUE, 0);
932 gtk_box_pack_start (GTK_BOX (parent), labelw,
939 GtkWidget *w = gtk_label_new (_((char *) p->low_label));
940 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
941 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
944 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
947 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
950 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM);
951 gtk_scale_set_draw_value (GTK_SCALE (scale), debug_p);
952 gtk_scale_set_digits (GTK_SCALE (scale), (p->integer_p ? 0 : 2));
954 gtk_table_attach (GTK_TABLE (parent), scale, 1, 2,
956 GTK_EXPAND | GTK_FILL, 0, 0, 0);
958 gtk_box_pack_start (GTK_BOX (parent), scale, TRUE, TRUE, 4);
960 gtk_widget_show (scale);
964 GtkWidget *w = gtk_label_new (_((char *) p->high_label));
965 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
966 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
969 gtk_table_attach (GTK_TABLE (parent), w, 2, 3, *row, *row + 1,
972 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
980 GtkAdjustment *adj = make_adjustment (filename, p);
981 GtkWidget *spin = gtk_spin_button_new (adj, 15, 0);
982 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
983 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (spin), TRUE);
984 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), adj->value);
988 GtkWidget *w = gtk_label_new (_(label));
989 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
990 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
993 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
996 gtk_box_pack_start (GTK_BOX (parent), w, TRUE, TRUE, 4);
999 gtk_widget_show (spin);
1002 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
1003 gtk_widget_show (hbox);
1004 gtk_table_attach (GTK_TABLE (parent), hbox, 1, 3,
1006 GTK_EXPAND | GTK_FILL, 0, 8, 0);
1007 gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0);
1010 gtk_box_pack_start (GTK_BOX (parent), spin, FALSE, FALSE, 4);
1017 p->widget = gtk_check_button_new_with_label (_(label));
1019 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
1021 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1023 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
1028 GtkWidget *opt = gtk_option_menu_new ();
1029 GtkWidget *menu = gtk_menu_new ();
1034 GtkWidget *w = gtk_label_new (_(label));
1035 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
1036 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
1037 gtk_widget_show (w);
1038 gtk_table_attach (GTK_TABLE (parent), w, 0, 3, *row, *row + 1,
1039 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1043 for (opts = p->options; opts; opts = opts->next)
1045 parameter *s = (parameter *) opts->data;
1046 GtkWidget *i = gtk_menu_item_new_with_label (_((char *) s->label));
1047 gtk_widget_show (i);
1048 gtk_menu_append (GTK_MENU (menu), i);
1051 gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
1054 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
1056 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1058 gtk_box_pack_start (GTK_BOX (parent), p->widget, TRUE, TRUE, 4);
1073 gtk_widget_set_name (p->widget, (char *) p->id);
1074 gtk_widget_show (p->widget);
1082 Absurdly, there is no GTK file entry widget, only a GNOME one,
1083 so in order to avoid depending on GNOME in this code, we have
1087 /* cancel button on GtkFileSelection: user_data unused */
1089 file_sel_cancel (GtkWidget *button, gpointer user_data)
1091 GtkWidget *dialog = button;
1092 while (dialog->parent)
1093 dialog = dialog->parent;
1094 gtk_widget_destroy (dialog);
1097 /* ok button on GtkFileSelection: user_data is the corresponding GtkEntry */
1099 file_sel_ok (GtkWidget *button, gpointer user_data)
1101 GtkWidget *entry = GTK_WIDGET (user_data);
1102 GtkWidget *dialog = button;
1104 while (dialog->parent)
1105 dialog = dialog->parent;
1106 gtk_widget_hide (dialog);
1108 path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog));
1109 /* apparently one doesn't free `path' */
1111 gtk_entry_set_text (GTK_ENTRY (entry), path);
1112 gtk_entry_set_position (GTK_ENTRY (entry), strlen (path));
1114 gtk_widget_destroy (dialog);
1117 /* WM close on GtkFileSelection: user_data unused */
1119 file_sel_close (GtkWidget *widget, GdkEvent *event, gpointer user_data)
1121 file_sel_cancel (widget, user_data);
1124 /* "Browse" button: user_data is the corresponding GtkEntry */
1126 browse_button_cb (GtkButton *button, gpointer user_data)
1128 GtkWidget *entry = GTK_WIDGET (user_data);
1129 const char *text = gtk_entry_get_text (GTK_ENTRY (entry));
1130 GtkFileSelection *selector =
1131 GTK_FILE_SELECTION (gtk_file_selection_new (_("Select file.")));
1133 gtk_file_selection_set_filename (selector, text);
1134 gtk_signal_connect (GTK_OBJECT (selector->ok_button),
1135 "clicked", GTK_SIGNAL_FUNC (file_sel_ok),
1137 gtk_signal_connect (GTK_OBJECT (selector->cancel_button),
1138 "clicked", GTK_SIGNAL_FUNC (file_sel_cancel),
1140 gtk_signal_connect (GTK_OBJECT (selector), "delete_event",
1141 GTK_SIGNAL_FUNC (file_sel_close),
1144 gtk_window_set_modal (GTK_WINDOW (selector), TRUE);
1145 gtk_widget_show (GTK_WIDGET (selector));
1149 /* Converting to and from command-lines
1153 /* Returns a copy of string that has been quoted according to shell rules:
1154 it may have been wrapped in "" and had some characters backslashed; or
1155 it may be unchanged.
1158 shell_quotify (const char *string)
1160 char *string2 = (char *) malloc ((strlen (string) * 2) + 10);
1163 int need_quotes = 0;
1168 for (in = string; *in; in++)
1179 else if (*in <= ' ' ||
1205 return strdup (string);
1208 /* Modify the string in place to remove wrapping double-quotes
1209 and interior backslashes.
1212 de_stringify (char *s)
1215 if (q != '\'' && q != '\"' && q != '`')
1217 memmove (s, s+1, strlen (s)+1);
1218 while (*s && *s != q)
1221 memmove (s, s+1, strlen (s)+1);
1224 if (*s != q) abort();
1229 /* Substitutes a shell-quotified version of `value' into `p->arg' at
1230 the place where the `%' character appeared.
1233 format_switch (parameter *p, const char *value)
1235 char *fmt = (char *) p->arg;
1238 if (!fmt || !value) return 0;
1239 v2 = shell_quotify (value);
1240 result = (char *) malloc (strlen (fmt) + strlen (v2) + 10);
1257 /* Maps a `parameter' to a command-line switch.
1258 Returns 0 if it can't, or if the parameter has the default value.
1261 parameter_to_switch (parameter *p)
1267 return strdup ((char *) p->arg);
1273 if (!p->widget) return 0;
1275 const char *s = gtk_entry_get_text (GTK_ENTRY (p->widget));
1277 if (!strcmp ((s ? s : ""),
1278 (p->string ? (char *) p->string : "")))
1279 v = 0; /* same as default */
1281 v = format_switch (p, s);
1283 /* don't free `s' */
1288 if (!p->widget) return 0;
1290 GtkAdjustment *adj =
1292 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1293 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1296 float value = (p->invert_p
1297 ? invert_range (adj->lower, adj->upper, adj->value) - 1
1300 if (value == p->value) /* same as default */
1304 sprintf (buf, "%d", (int) (value + (value > 0 ? 0.5 : -0.5)));
1306 sprintf (buf, "%.4f", value);
1308 s1 = strchr (buf, '.');
1311 char *s2 = s1 + strlen(s1) - 1;
1312 while (s2 > s1 && *s2 == '0') /* lose trailing zeroes */
1314 if (s2 >= s1 && *s2 == '.') /* lose trailing decimal */
1317 return format_switch (p, buf);
1320 if (!p->widget) return 0;
1322 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1323 const char *s = (gtk_toggle_button_get_active (b)
1324 ? (char *) p->arg_set
1325 : (char *) p->arg_unset);
1332 if (!p->widget) return 0;
1334 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1335 GtkMenu *menu = GTK_MENU (gtk_option_menu_get_menu (opt));
1336 GtkWidget *selected = gtk_menu_get_active (menu);
1337 GList *kids = gtk_container_children (GTK_CONTAINER (menu));
1338 int menu_elt = g_list_index (kids, (gpointer) selected);
1339 GList *ol = g_list_nth (p->options, menu_elt);
1340 parameter *o = (ol ? (parameter *) ol->data : 0);
1343 if (o->type != SELECT_OPTION) abort();
1344 s = (char *) o->arg_set;
1358 /* Maps a GList of `parameter' objects to a complete command-line string.
1359 All arguments will be properly quoted.
1362 parameters_to_cmd_line (GList *parms)
1364 int L = g_list_length (parms);
1366 char **strs = (char **) calloc (sizeof (*parms), L);
1371 for (i = 0; parms; parms = parms->next, i++)
1373 char *s = parameter_to_switch ((parameter *) parms->data);
1375 LL += (s ? strlen(s) : 0) + 1;
1378 result = (char *) malloc (LL + 10);
1380 for (i = 0; i < L; i++)
1383 strcpy (out, strs[i]);
1384 out += strlen (out);
1389 while (out > result && out[-1] == ' ') /* strip trailing spaces */
1397 /* Returns a GList of the tokens the string, using shell syntax;
1398 Quoted strings are handled as a single token.
1401 tokenize_command_line (const char *cmd)
1404 const char *s = cmd;
1409 for (; isspace(*s); s++); /* skip whitespace */
1412 if (*s == '\'' || *s == '\"' || *s == '`')
1416 while (*s && *s != q) /* skip to matching quote */
1418 if (*s == '\\' && s[1]) /* allowing backslash quoting */
1436 ss = (char *) malloc ((s - start) + 1);
1437 strncpy (ss, start, s-start);
1439 if (*ss == '\'' || *ss == '\"' || *ss == '`')
1441 result = g_list_append (result, ss);
1448 static void parameter_set_switch (parameter *, gpointer value);
1449 static gboolean parse_command_line_into_parameters_1 (const char *filename,
1456 /* Parses the command line, and flushes those options down into
1457 the `parameter' structs in the list.
1460 parse_command_line_into_parameters (const char *filename,
1461 const char *cmd, GList *parms)
1463 GList *tokens = tokenize_command_line (cmd);
1465 for (rest = tokens; rest; rest = rest->next)
1467 char *option = rest->data;
1470 if (option[0] != '-' && option[0] != '+')
1473 fprintf (stderr, "%s: WARNING: %s: not a switch: \"%s\"\n",
1474 blurb(), filename, option);
1480 if (rest->next) /* pop off the arg to this option */
1482 char *s = (char *) rest->next->data;
1483 /* the next token is the next switch iff it matches "-[a-z]".
1484 (To avoid losing on "-x -3.1".)
1486 if (s && (s[0] != '-' || !isalpha(s[1])))
1489 rest->next->data = 0;
1494 parse_command_line_into_parameters_1 (filename, parms,
1496 if (value) free (value);
1500 g_list_free (tokens);
1505 compare_opts (const char *option, const char *value,
1506 const char *template)
1508 int ol = strlen (option);
1511 if (strncmp (option, template, ol))
1514 if (template[ol] != (value ? ' ' : 0))
1517 /* At this point, we have a match against "option".
1518 If template contains a %, we're done.
1519 Else, compare against "value" too.
1521 c = strchr (template, '%');
1526 return (template[ol] == 0);
1527 if (strcmp (template + ol + 1, value))
1535 parse_command_line_into_parameters_1 (const char *filename,
1542 parameter *match = 0;
1546 for (p = parms; p; p = p->next)
1548 parameter *pp = (parameter *) p->data;
1551 if (pp->type == SELECT)
1553 if (parse_command_line_into_parameters_1 (filename,
1564 if (compare_opts (option, value, (char *) pp->arg))
1570 else if (pp->arg_set)
1572 if (compare_opts (option, value, (char *) pp->arg_set))
1578 else if (pp->arg_unset)
1580 if (compare_opts (option, value, (char *) pp->arg_unset))
1595 if (debug_p && !parent)
1596 fprintf (stderr, "%s: WARNING: %s: no match for %s %s\n",
1597 blurb(), filename, option, (value ? value : ""));
1601 switch (match->type)
1607 if (which != -1) abort();
1608 parameter_set_switch (match, (gpointer) value);
1611 if (which != 0 && which != 1) abort();
1612 parameter_set_switch (match, GINT_TO_POINTER(which));
1615 if (which != 1) abort();
1616 parameter_set_switch (parent, GINT_TO_POINTER(index));
1625 /* Set the parameter's value.
1626 For STRING, FILENAME, SLIDER, and SPINBUTTON, `value' is a char*.
1627 For BOOLEAN and SELECT, `value' is an int.
1630 parameter_set_switch (parameter *p, gpointer value)
1632 if (p->type == SELECT_OPTION) abort();
1633 if (!p->widget) return;
1639 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) value);
1645 GtkAdjustment *adj =
1647 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1648 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1652 if (1 == sscanf ((char *) value, "%f %c", &f, &c))
1655 f = invert_range (adj->lower, adj->upper, f) - 1;
1656 gtk_adjustment_set_value (adj, f);
1662 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1663 gtk_toggle_button_set_active (b, GPOINTER_TO_INT(value));
1668 gtk_option_menu_set_history (GTK_OPTION_MENU (p->widget),
1669 GPOINTER_TO_INT(value));
1679 restore_defaults (const char *progname, GList *parms)
1681 for (; parms; parms = parms->next)
1683 parameter *p = (parameter *) parms->data;
1684 if (!p->widget) continue;
1690 gtk_entry_set_text (GTK_ENTRY (p->widget),
1691 (p->string ? (char *) p->string : ""));
1697 GtkAdjustment *adj =
1699 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1700 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1701 float value = (p->invert_p
1702 ? invert_range (p->low, p->high, p->value)
1704 gtk_adjustment_set_value (adj, value);
1709 /* A toggle button should be on by default if it inserts
1710 nothing into the command line when on. E.g., it should
1711 be on if `arg_set' is null.
1713 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->widget),
1714 (!p->arg_set || !*p->arg_set));
1719 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1724 for (opts = p->options, index = 0; opts;
1725 opts = opts->next, index++)
1727 parameter *s = (parameter *) opts->data;
1728 /* The default menu item is the first one with
1729 no `arg_set' field. */
1737 gtk_option_menu_set_history (GTK_OPTION_MENU (opt), selected);
1748 /* Documentation strings
1752 get_description (GList *parms, gboolean verbose_p)
1755 for (; parms; parms = parms->next)
1757 parameter *p = (parameter *) parms->data;
1758 if (p->type == DESCRIPTION)
1765 if (!doc || !doc->string)
1769 char *d = strdup ((char *) doc->string);
1772 for (s = d; *s; s++)
1775 if (s[1] == '\n') /* blank line: leave it */
1777 else if (s[1] == ' ' || s[1] == '\t')
1778 s++; /* next line is indented: leave newline */
1780 s[0] = ' '; /* delete newline to un-fold this line */
1783 /* strip off leading whitespace on first line only */
1784 for (s = d; *s && (*s == ' ' || *s == '\t'); s++)
1786 while (*s == '\n') /* strip leading newlines */
1789 memmove (d, s, strlen(s)+1);
1791 /* strip off trailing whitespace and newlines */
1794 while (L && isspace(d[L-1]))
1798 /* strip off duplicated whitespaces */
1799 for (s = d; *s; s++)
1806 memmove (p, s, strlen(s)+1);
1812 fprintf (stderr, "%s: text read is \"%s\"\n", blurb(),doc->string);
1813 fprintf (stderr, "%s: description is \"%s\"\n", blurb(), d);
1814 fprintf (stderr, "%s: translation is \"%s\"\n", blurb(), _(d));
1823 /* External interface.
1827 load_configurator_1 (const char *program, const char *arguments,
1830 const char *dir = hack_configuration_path;
1832 int L = strlen (dir);
1838 if (L == 0) return 0;
1840 base_program = strrchr(program, '/');
1841 if (base_program) base_program++;
1842 if (!base_program) base_program = (char *) program;
1844 file = (char *) malloc (L + strlen (base_program) + 10);
1845 data = (conf_data *) calloc (1, sizeof(*data));
1848 if (file[L-1] != '/')
1850 strcpy (file+L, base_program);
1852 for (s = file+L; *s; s++)
1853 if (*s == '/' || *s == ' ')
1855 else if (isupper (*s))
1858 strcat (file+L, ".xml");
1860 f = fopen (file, "r");
1863 int res, size = 1024;
1865 xmlParserCtxtPtr ctxt;
1871 fprintf (stderr, "%s: reading %s...\n", blurb(), file);
1873 res = fread (chars, 1, 4, f);
1881 ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, file);
1882 while ((res = fread(chars, 1, size, f)) > 0)
1883 xmlParseChunk (ctxt, chars, res, 0);
1884 xmlParseChunk (ctxt, chars, 0, 1);
1886 xmlFreeParserCtxt (ctxt);
1889 /* Parsed the XML file. Now make some widgets. */
1891 table = gtk_table_new (1, 3, FALSE);
1892 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
1893 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
1894 gtk_container_set_border_width (GTK_CONTAINER (table), 8);
1895 gtk_widget_show (table);
1897 parms = make_parameters (file, doc->xmlRootNode, table);
1898 sanity_check_parameters (file, parms);
1902 restore_defaults (program, parms);
1903 if (arguments && *arguments)
1904 parse_command_line_into_parameters (program, arguments, parms);
1906 data->widget = table;
1907 data->parameters = parms;
1908 data->description = get_description (parms, verbose_p);
1915 fprintf (stderr, "%s: %s does not exist.\n", blurb(), file);
1917 p = calloc (1, sizeof(*p));
1919 p->arg = (xmlChar *) strdup (arguments);
1921 data->parameters = g_list_append (0, (gpointer) p);
1924 data->progname = strdup (program);
1932 split_command_line (const char *full_command_line,
1933 char **prog_ret, char **args_ret)
1935 char *line = strdup (full_command_line);
1948 while (isspace (*s)) s++;
1951 else if (*s == '=') /* if the leading word contains an "=", skip it. */
1953 while (*s && !isspace (*s)) s++;
1954 while (isspace (*s)) s++;
1961 *prog_ret = strdup (prog);
1962 *args_ret = strdup (args);
1968 load_configurator (const char *full_command_line, gboolean verbose_p)
1973 debug_p = verbose_p;
1974 split_command_line (full_command_line, &prog, &args);
1975 cd = load_configurator_1 (prog, args, verbose_p);
1984 get_configurator_command_line (conf_data *data)
1986 char *args = parameters_to_cmd_line (data->parameters);
1987 char *result = (char *) malloc (strlen (data->progname) +
1989 strcpy (result, data->progname);
1990 strcat (result, " ");
1991 strcat (result, args);
1998 set_configurator_command_line (conf_data *data, const char *full_command_line)
2002 split_command_line (full_command_line, &prog, &args);
2003 if (data->progname) free (data->progname);
2004 data->progname = prog;
2005 restore_defaults (prog, data->parameters);
2006 parse_command_line_into_parameters (prog, args, data->parameters);
2011 free_conf_data (conf_data *data)
2013 if (data->parameters)
2016 for (rest = data->parameters; rest; rest = rest->next)
2018 free_parameter ((parameter *) rest->data);
2021 g_list_free (data->parameters);
2022 data->parameters = 0;
2026 gtk_widget_destroy (data->widget);
2029 free (data->progname);
2030 if (data->description)
2031 free (data->description);
2033 memset (data, ~0, sizeof(*data));
2038 #endif /* HAVE_GTK && HAVE_XML -- whole file */