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,
267 static void sanity_check_text_node (const char *filename,
268 const xmlNodePtr node);
270 /* Allocates and returns a new `parameter' object based on the
271 properties in the given XML node. Returns 0 if there's nothing
272 to create (comment, or unknown tag.)
275 make_parameter (const char *filename, xmlNodePtr node)
278 const char *name = (char *) node->name;
280 gboolean floatp = FALSE;
282 if (node->type == XML_COMMENT_NODE)
285 p = calloc (1, sizeof(*p));
288 else if (!strcmp (name, "command")) p->type = COMMAND;
289 else if (!strcmp (name, "fullcommand")) p->type = COMMAND;
290 else if (!strcmp (name, "_description")) p->type = DESCRIPTION;
291 else if (!strcmp (name, "fakepreview")) p->type = FAKEPREVIEW;
292 else if (!strcmp (name, "fake")) p->type = FAKE;
293 else if (!strcmp (name, "boolean")) p->type = BOOLEAN;
294 else if (!strcmp (name, "string")) p->type = STRING;
295 else if (!strcmp (name, "file")) p->type = FILENAME;
296 else if (!strcmp (name, "number")) p->type = SPINBUTTON;
297 else if (!strcmp (name, "select")) p->type = SELECT;
298 else if (node->type == XML_TEXT_NODE)
300 sanity_check_text_node (filename, node);
307 fprintf (stderr, "%s: WARNING: %s: unknown tag: \"%s\"\n",
308 blurb(), filename, name);
313 if (p->type == SPINBUTTON)
315 const char *type = (char *) xmlGetProp (node, (xmlChar *) "type");
316 if (!type || !strcmp (type, "spinbutton")) p->type = SPINBUTTON;
317 else if (!strcmp (type, "slider")) p->type = SLIDER;
321 fprintf (stderr, "%s: WARNING: %s: unknown %s type: \"%s\"\n",
322 blurb(), filename, name, type);
327 else if (p->type == DESCRIPTION)
329 if (node->xmlChildrenNode &&
330 node->xmlChildrenNode->type == XML_TEXT_NODE &&
331 !node->xmlChildrenNode->next)
332 p->string = (xmlChar *)
333 strdup ((char *) node->xmlChildrenNode->content);
336 p->id = xmlGetProp (node, (xmlChar *) "id");
337 p->label = xmlGetProp (node, (xmlChar *) "_label");
338 p->low_label = xmlGetProp (node, (xmlChar *) "_low-label");
339 p->high_label = xmlGetProp (node, (xmlChar *) "_high-label");
340 p->low = xml_get_float (node, (xmlChar *) "low", &floatp);
341 p->high = xml_get_float (node, (xmlChar *) "high", &floatp);
342 p->value = xml_get_float (node, (xmlChar *) "default", &floatp);
343 p->integer_p = !floatp;
344 convert = (char *) xmlGetProp (node, (xmlChar *) "convert");
345 p->invert_p = (convert && !strcmp (convert, "invert"));
346 p->arg = xmlGetProp (node, (xmlChar *) "arg");
347 p->arg_set = xmlGetProp (node, (xmlChar *) "arg-set");
348 p->arg_unset = xmlGetProp (node, (xmlChar *) "arg-unset");
349 p->test = xmlGetProp (node, (xmlChar *) "test");
351 /* Check for missing decimal point */
354 (p->high != p->low) &&
355 (p->high - p->low) <= 1)
357 "%s: WARNING: %s: %s: range [%.1f, %.1f] shouldn't be integral!\n",
358 blurb(), filename, p->id,
361 if (p->type == SELECT)
364 for (kids = node->xmlChildrenNode; kids; kids = kids->next)
366 parameter *s = make_select_option (filename, kids);
368 p->options = g_list_append (p->options, s);
372 sanity_check_parameter (filename, (const xmlChar *) name, p);
378 /* Allocates and returns a new SELECT_OPTION `parameter' object based
379 on the properties in the given XML node. Returns 0 if there's nothing
380 to create (comment, or unknown tag.)
383 make_select_option (const char *filename, xmlNodePtr node)
385 if (node->type == XML_COMMENT_NODE)
387 else if (node->type == XML_TEXT_NODE)
389 sanity_check_text_node (filename, node);
392 else if (node->type != XML_ELEMENT_NODE)
396 "%s: WARNING: %s: %s: unexpected child tag type %d\n",
397 blurb(), filename, node->name, (int)node->type);
400 else if (strcmp ((char *) node->name, "option"))
404 "%s: WARNING: %s: %s: child not an option tag: \"%s\"\n",
405 blurb(), filename, node->name, node->name);
410 parameter *s = calloc (1, sizeof(*s));
413 s->type = SELECT_OPTION;
414 s->id = xmlGetProp (node, (xmlChar *) "id");
415 s->label = xmlGetProp (node, (xmlChar *) "_label");
416 s->arg_set = xmlGetProp (node, (xmlChar *) "arg-set");
417 s->arg_unset = xmlGetProp (node, (xmlChar *) "arg-unset");
418 s->test = xmlGetProp (node, (xmlChar *) "test");
419 enable = (char*)xmlGetProp (node, (xmlChar *) "enable");
423 enable = strdup (enable);
424 e = strtok (enable, ", ");
427 s->enable = g_list_append (s->enable, strdup (e));
428 e = strtok (0, ", ");
433 sanity_check_parameter (filename, node->name, s);
439 /* Rudimentary check to make sure someone hasn't typed "arg-set="
440 when they should have typed "arg=", etc.
443 sanity_check_parameter (const char *filename, const xmlChar *node_name,
461 memset (&allowed, 0, sizeof (allowed));
462 memset (&require, 0, sizeof (require));
473 allowed.string = TRUE;
480 allowed.label = TRUE;
481 require.label = TRUE;
488 allowed.label = TRUE;
495 allowed.label = TRUE;
496 allowed.low_label = TRUE;
497 allowed.high_label = TRUE;
501 /* require.low = TRUE; -- may be 0 */
503 /* require.high = TRUE; -- may be 0 */
504 allowed.value = TRUE;
505 /* require.value = TRUE; -- may be 0 */
506 allowed.invert_p = TRUE;
511 allowed.label = TRUE;
515 /* require.low = TRUE; -- may be 0 */
517 /* require.high = TRUE; -- may be 0 */
518 allowed.value = TRUE;
519 /* require.value = TRUE; -- may be 0 */
520 allowed.invert_p = TRUE;
525 allowed.label = TRUE;
526 allowed.arg_set = TRUE;
527 allowed.arg_unset = TRUE;
535 allowed.label = TRUE;
536 require.label = TRUE;
537 allowed.arg_set = TRUE;
545 fprintf (stderr, "%s: %s: " STR " in <%s%s id=\"%s\">\n", \
546 blurb(), filename, node_name, \
547 (!strcmp((char *) node_name, "number") \
548 ? (p->type == SPINBUTTON ? " type=spinbutton" : " type=slider")\
550 (p->id ? (char *) p->id : ""))
551 # define CHECK(SLOT,NAME) \
552 if (p->SLOT && !allowed.SLOT) \
553 WARN ("\"" NAME "\" is not a valid option"); \
554 if (!p->SLOT && require.SLOT) \
555 WARN ("\"" NAME "\" is required")
558 CHECK (label, "_label");
559 CHECK (string, "(body text)");
560 CHECK (low_label, "_low-label");
561 CHECK (high_label, "_high-label");
563 CHECK (high, "high");
564 CHECK (value, "default");
566 CHECK (invert_p, "convert");
567 CHECK (arg_set, "arg-set");
568 CHECK (arg_unset, "arg-unset");
573 /* "text" nodes show up for all the non-tag text in the file, including
574 all the newlines between tags. Warn if there is text there that
578 sanity_check_text_node (const char *filename, const xmlNodePtr node)
580 const char *body = (const char *) node->content;
581 if (node->type != XML_TEXT_NODE) abort();
582 while (isspace (*body)) body++;
584 fprintf (stderr, "%s: WARNING: %s: random text present: \"%s\"\n",
585 blurb(), filename, body);
589 /* Returns a list of strings, every switch mentioned in the parameters.
590 The strings must be freed.
593 get_all_switches (const char *filename, GList *parms)
597 for (p = parms; p; p = p->next)
599 parameter *pp = (parameter *) p->data;
601 if (pp->type == SELECT)
603 GList *list2 = get_all_switches (filename, pp->options);
604 switches = g_list_concat (switches, list2);
606 if (pp->arg && *pp->arg)
607 switches = g_list_append (switches, strdup ((char *) pp->arg));
608 if (pp->arg_set && *pp->arg_set)
609 switches = g_list_append (switches, strdup ((char *) pp->arg_set));
610 if (pp->arg_unset && *pp->arg_unset)
611 switches = g_list_append (switches, strdup ((char *) pp->arg_unset));
617 /* Ensures that no switch is mentioned more than once.
620 sanity_check_parameters (const char *filename, GList *parms)
622 GList *list = get_all_switches (filename, parms);
624 for (p = list; p; p = p->next)
626 char *sw = (char *) p->data;
629 if (*sw != '-' && *sw != '+')
630 fprintf (stderr, "%s: %s: switch does not begin with hyphen \"%s\"\n",
631 blurb(), filename, sw);
633 for (p2 = p->next; p2; p2 = p2->next)
635 const char *sw2 = (const char *) p2->data;
636 if (!strcmp (sw, sw2))
637 fprintf (stderr, "%s: %s: duplicate switch \"%s\"\n",
638 blurb(), filename, sw);
647 /* Helper for make_parameters()
650 make_parameters_1 (const char *filename, xmlNodePtr node,
651 GtkWidget *parent, int *row)
655 for (; node; node = node->next)
657 const char *name = (char *) node->name;
658 if (!strcmp (name, "hgroup") ||
659 !strcmp (name, "vgroup"))
661 GtkWidget *box = (*name == 'h'
662 ? gtk_hbox_new (FALSE, 0)
663 : gtk_vbox_new (FALSE, 0));
665 gtk_widget_show (box);
668 gtk_table_attach (GTK_TABLE (parent), box, 0, 3, *row, *row + 1,
671 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, FALSE, 0);
676 list2 = make_parameters_1 (filename, node->xmlChildrenNode, box, 0);
678 list = g_list_concat (list, list2);
682 parameter *p = make_parameter (filename, node);
685 list = g_list_append (list, p);
686 make_parameter_widget (filename, p, parent, row);
694 /* Calls make_parameter() and make_parameter_widget() on each relevant
695 tag in the XML tree. Also handles the "hgroup" and "vgroup" flags.
696 Returns a GList of `parameter' objects.
699 make_parameters (const char *filename, xmlNodePtr node, GtkWidget *parent)
702 for (; node; node = node->next)
704 if (node->type == XML_ELEMENT_NODE &&
705 !strcmp ((char *) node->name, "screensaver"))
706 return make_parameters_1 (filename, node->xmlChildrenNode,
714 invert_range (gfloat low, gfloat high, gfloat value)
716 gfloat range = high-low;
717 gfloat off = value-low;
718 return (low + (range - off));
722 static GtkAdjustment *
723 make_adjustment (const char *filename, parameter *p)
725 float range = (p->high - p->low);
726 float value = (p->invert_p
727 ? invert_range (p->low, p->high, p->value)
729 gfloat si = (p->high - p->low) / 100;
730 gfloat pi = (p->high - p->low) / 10;
732 if (p->value < p->low || p->value > p->high)
734 if (debug_p && p->integer_p)
735 fprintf (stderr, "%s: WARNING: %s: %d is not in range [%d, %d]\n",
737 (int) p->value, (int) p->low, (int) p->high);
740 "%s: WARNING: %s: %.2f is not in range [%.2f, %.2f]\n",
741 blurb(), filename, p->value, p->low, p->high);
742 value = (value < p->low ? p->low : p->high);
745 else if (debug_p && p->value < 1000 && p->high >= 10000)
749 "%s: WARNING: %s: %d is suspicious for range [%d, %d]\n",
751 (int) p->value, (int) p->low, (int) p->high);
754 "%s: WARNING: %s: %.2f is suspicious for range [%.2f, %.2f]\n",
755 blurb(), filename, p->value, p->low, p->high);
759 si = (int) (si + 0.5);
760 pi = (int) (pi + 0.5);
764 if (range <= 500) si = 1;
766 return GTK_ADJUSTMENT (gtk_adjustment_new (value,
774 /* Given a `parameter' struct, allocates an appropriate GtkWidget for it,
775 and stores it in `p->widget'.
776 `row' is used for keeping track of our position during table layout.
777 `parent' must be a GtkTable or a GtkBox.
780 make_parameter_widget (const char *filename,
781 parameter *p, GtkWidget *parent, int *row)
783 const char *label = (char *) p->label;
784 if (p->widget) return;
792 GtkWidget *w = gtk_label_new (_(label));
793 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
794 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
797 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
800 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
803 p->widget = gtk_entry_new ();
805 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) p->string);
807 gtk_table_attach (GTK_TABLE (parent), p->widget, 1, 3,
811 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
816 GtkWidget *L = gtk_label_new (label ? _(label) : "");
817 GtkWidget *entry = gtk_entry_new ();
818 GtkWidget *button = gtk_button_new_with_label (_("Browse..."));
819 gtk_widget_show (entry);
820 gtk_widget_show (button);
823 gtk_signal_connect (GTK_OBJECT (button),
824 "clicked", GTK_SIGNAL_FUNC (browse_button_cb),
827 gtk_label_set_justify (GTK_LABEL (L), GTK_JUSTIFY_RIGHT);
828 gtk_misc_set_alignment (GTK_MISC (L), 1.0, 0.5);
832 gtk_entry_set_text (GTK_ENTRY (entry), (char *) p->string);
836 gtk_table_attach (GTK_TABLE (parent), L, 0, 1,
839 gtk_table_attach (GTK_TABLE (parent), entry, 1, 2,
841 GTK_EXPAND | GTK_FILL, 0, 0, 0);
842 gtk_table_attach (GTK_TABLE (parent), button, 2, 3,
848 gtk_box_pack_start (GTK_BOX (parent), L, FALSE, FALSE, 4);
849 gtk_box_pack_start (GTK_BOX (parent), entry, TRUE, TRUE, 4);
850 gtk_box_pack_start (GTK_BOX (parent), button, FALSE, FALSE, 4);
856 GtkAdjustment *adj = make_adjustment (filename, p);
857 GtkWidget *scale = gtk_hscale_new (adj);
858 GtkWidget *labelw = 0;
862 labelw = gtk_label_new (_(label));
863 gtk_label_set_justify (GTK_LABEL (labelw), GTK_JUSTIFY_LEFT);
864 gtk_misc_set_alignment (GTK_MISC (labelw), 0.0, 0.5);
865 gtk_widget_show (labelw);
868 if (GTK_IS_VBOX (parent))
870 /* If we're inside a vbox, we need to put an hbox in it, to get
871 the low/high labels to be to the left/right of the slider.
873 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
875 /* But if we have a label, put that above the slider's hbox. */
878 gtk_box_pack_start (GTK_BOX (parent), labelw, FALSE, TRUE, 2);
882 gtk_box_pack_start (GTK_BOX (parent), hbox, TRUE, TRUE, 6);
883 gtk_widget_show (hbox);
891 gtk_table_attach (GTK_TABLE (parent), labelw,
892 0, 3, *row, *row + 1,
893 GTK_EXPAND | GTK_FILL, 0, 0, 0);
898 if (GTK_IS_HBOX (parent))
900 GtkWidget *box = gtk_vbox_new (FALSE, 0);
901 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, TRUE, 0);
902 gtk_widget_show (box);
903 gtk_box_pack_start (GTK_BOX (box), labelw, FALSE, TRUE, 4);
905 box = gtk_hbox_new (FALSE, 0);
906 gtk_widget_show (box);
907 gtk_box_pack_start (GTK_BOX (parent), box, TRUE, TRUE, 0);
911 gtk_box_pack_start (GTK_BOX (parent), labelw,
918 GtkWidget *w = gtk_label_new (_((char *) p->low_label));
919 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
920 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
923 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
926 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
929 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM);
930 gtk_scale_set_draw_value (GTK_SCALE (scale), debug_p);
931 gtk_scale_set_digits (GTK_SCALE (scale), (p->integer_p ? 0 : 2));
933 gtk_table_attach (GTK_TABLE (parent), scale, 1, 2,
935 GTK_EXPAND | GTK_FILL, 0, 0, 0);
937 gtk_box_pack_start (GTK_BOX (parent), scale, TRUE, TRUE, 4);
939 gtk_widget_show (scale);
943 GtkWidget *w = gtk_label_new (_((char *) p->high_label));
944 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
945 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
948 gtk_table_attach (GTK_TABLE (parent), w, 2, 3, *row, *row + 1,
951 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
959 GtkAdjustment *adj = make_adjustment (filename, p);
960 GtkWidget *spin = gtk_spin_button_new (adj, 15, 0);
961 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
962 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (spin), TRUE);
963 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), adj->value);
967 GtkWidget *w = gtk_label_new (_(label));
968 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
969 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
972 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
975 gtk_box_pack_start (GTK_BOX (parent), w, TRUE, TRUE, 4);
978 gtk_widget_show (spin);
981 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
982 gtk_widget_show (hbox);
983 gtk_table_attach (GTK_TABLE (parent), hbox, 1, 3,
985 GTK_EXPAND | GTK_FILL, 0, 8, 0);
986 gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0);
989 gtk_box_pack_start (GTK_BOX (parent), spin, FALSE, FALSE, 4);
996 p->widget = gtk_check_button_new_with_label (_(label));
998 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
1000 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1002 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
1007 GtkWidget *opt = gtk_option_menu_new ();
1008 GtkWidget *menu = gtk_menu_new ();
1013 GtkWidget *w = gtk_label_new (_(label));
1014 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
1015 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
1016 gtk_widget_show (w);
1017 gtk_table_attach (GTK_TABLE (parent), w, 0, 3, *row, *row + 1,
1018 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1022 for (opts = p->options; opts; opts = opts->next)
1024 parameter *s = (parameter *) opts->data;
1025 GtkWidget *i = gtk_menu_item_new_with_label (_((char *) s->label));
1026 gtk_widget_show (i);
1027 gtk_menu_append (GTK_MENU (menu), i);
1030 gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
1033 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
1035 GTK_EXPAND | GTK_FILL, 0, 0, 0);
1037 gtk_box_pack_start (GTK_BOX (parent), p->widget, TRUE, TRUE, 4);
1052 gtk_widget_set_name (p->widget, (char *) p->id);
1053 gtk_widget_show (p->widget);
1061 Absurdly, there is no GTK file entry widget, only a GNOME one,
1062 so in order to avoid depending on GNOME in this code, we have
1066 /* cancel button on GtkFileSelection: user_data unused */
1068 file_sel_cancel (GtkWidget *button, gpointer user_data)
1070 GtkWidget *dialog = button;
1071 while (dialog->parent)
1072 dialog = dialog->parent;
1073 gtk_widget_destroy (dialog);
1076 /* ok button on GtkFileSelection: user_data is the corresponding GtkEntry */
1078 file_sel_ok (GtkWidget *button, gpointer user_data)
1080 GtkWidget *entry = GTK_WIDGET (user_data);
1081 GtkWidget *dialog = button;
1083 while (dialog->parent)
1084 dialog = dialog->parent;
1085 gtk_widget_hide (dialog);
1087 path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog));
1088 /* apparently one doesn't free `path' */
1090 gtk_entry_set_text (GTK_ENTRY (entry), path);
1091 gtk_entry_set_position (GTK_ENTRY (entry), strlen (path));
1093 gtk_widget_destroy (dialog);
1096 /* WM close on GtkFileSelection: user_data unused */
1098 file_sel_close (GtkWidget *widget, GdkEvent *event, gpointer user_data)
1100 file_sel_cancel (widget, user_data);
1103 /* "Browse" button: user_data is the corresponding GtkEntry */
1105 browse_button_cb (GtkButton *button, gpointer user_data)
1107 GtkWidget *entry = GTK_WIDGET (user_data);
1108 const char *text = gtk_entry_get_text (GTK_ENTRY (entry));
1109 GtkFileSelection *selector =
1110 GTK_FILE_SELECTION (gtk_file_selection_new (_("Select file.")));
1112 gtk_file_selection_set_filename (selector, text);
1113 gtk_signal_connect (GTK_OBJECT (selector->ok_button),
1114 "clicked", GTK_SIGNAL_FUNC (file_sel_ok),
1116 gtk_signal_connect (GTK_OBJECT (selector->cancel_button),
1117 "clicked", GTK_SIGNAL_FUNC (file_sel_cancel),
1119 gtk_signal_connect (GTK_OBJECT (selector), "delete_event",
1120 GTK_SIGNAL_FUNC (file_sel_close),
1123 gtk_window_set_modal (GTK_WINDOW (selector), TRUE);
1124 gtk_widget_show (GTK_WIDGET (selector));
1128 /* Converting to and from command-lines
1132 /* Returns a copy of string that has been quoted according to shell rules:
1133 it may have been wrapped in "" and had some characters backslashed; or
1134 it may be unchanged.
1137 shell_quotify (const char *string)
1139 char *string2 = (char *) malloc ((strlen (string) * 2) + 10);
1142 int need_quotes = 0;
1147 for (in = string; *in; in++)
1158 else if (*in <= ' ' ||
1184 return strdup (string);
1187 /* Modify the string in place to remove wrapping double-quotes
1188 and interior backslashes.
1191 de_stringify (char *s)
1194 if (q != '\'' && q != '\"' && q != '`')
1196 memmove (s, s+1, strlen (s)+1);
1197 while (*s && *s != q)
1200 memmove (s, s+1, strlen (s)+1);
1203 if (*s != q) abort();
1208 /* Substitutes a shell-quotified version of `value' into `p->arg' at
1209 the place where the `%' character appeared.
1212 format_switch (parameter *p, const char *value)
1214 char *fmt = (char *) p->arg;
1217 if (!fmt || !value) return 0;
1218 v2 = shell_quotify (value);
1219 result = (char *) malloc (strlen (fmt) + strlen (v2) + 10);
1236 /* Maps a `parameter' to a command-line switch.
1237 Returns 0 if it can't, or if the parameter has the default value.
1240 parameter_to_switch (parameter *p)
1246 return strdup ((char *) p->arg);
1252 if (!p->widget) return 0;
1254 const char *s = gtk_entry_get_text (GTK_ENTRY (p->widget));
1256 if (!strcmp ((s ? s : ""),
1257 (p->string ? (char *) p->string : "")))
1258 v = 0; /* same as default */
1260 v = format_switch (p, s);
1262 /* don't free `s' */
1267 if (!p->widget) return 0;
1269 GtkAdjustment *adj =
1271 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1272 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1275 float value = (p->invert_p
1276 ? invert_range (adj->lower, adj->upper, adj->value) - 1
1279 if (value == p->value) /* same as default */
1283 sprintf (buf, "%d", (int) (value + 0.5));
1285 sprintf (buf, "%.4f", value);
1287 s1 = strchr (buf, '.');
1290 char *s2 = s1 + strlen(s1) - 1;
1291 while (s2 > s1 && *s2 == '0') /* lose trailing zeroes */
1293 if (s2 >= s1 && *s2 == '.') /* lose trailing decimal */
1296 return format_switch (p, buf);
1299 if (!p->widget) return 0;
1301 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1302 const char *s = (gtk_toggle_button_get_active (b)
1303 ? (char *) p->arg_set
1304 : (char *) p->arg_unset);
1311 if (!p->widget) return 0;
1313 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1314 GtkMenu *menu = GTK_MENU (gtk_option_menu_get_menu (opt));
1315 GtkWidget *selected = gtk_menu_get_active (menu);
1316 GList *kids = gtk_container_children (GTK_CONTAINER (menu));
1317 int menu_elt = g_list_index (kids, (gpointer) selected);
1318 GList *ol = g_list_nth (p->options, menu_elt);
1319 parameter *o = (ol ? (parameter *) ol->data : 0);
1322 if (o->type != SELECT_OPTION) abort();
1323 s = (char *) o->arg_set;
1337 /* Maps a GList of `parameter' objects to a complete command-line string.
1338 All arguments will be properly quoted.
1341 parameters_to_cmd_line (GList *parms)
1343 int L = g_list_length (parms);
1345 char **strs = (char **) calloc (sizeof (*parms), L);
1350 for (i = 0; parms; parms = parms->next, i++)
1352 char *s = parameter_to_switch ((parameter *) parms->data);
1354 LL += (s ? strlen(s) : 0) + 1;
1357 result = (char *) malloc (LL + 10);
1359 for (i = 0; i < L; i++)
1362 strcpy (out, strs[i]);
1363 out += strlen (out);
1368 while (out > result && out[-1] == ' ') /* strip trailing spaces */
1376 /* Returns a GList of the tokens the string, using shell syntax;
1377 Quoted strings are handled as a single token.
1380 tokenize_command_line (const char *cmd)
1383 const char *s = cmd;
1388 for (; isspace(*s); s++); /* skip whitespace */
1391 if (*s == '\'' || *s == '\"' || *s == '`')
1395 while (*s && *s != q) /* skip to matching quote */
1397 if (*s == '\\' && s[1]) /* allowing backslash quoting */
1415 ss = (char *) malloc ((s - start) + 1);
1416 strncpy (ss, start, s-start);
1418 if (*ss == '\'' || *ss == '\"' || *ss == '`')
1420 result = g_list_append (result, ss);
1427 static void parameter_set_switch (parameter *, gpointer value);
1428 static gboolean parse_command_line_into_parameters_1 (const char *filename,
1435 /* Parses the command line, and flushes those options down into
1436 the `parameter' structs in the list.
1439 parse_command_line_into_parameters (const char *filename,
1440 const char *cmd, GList *parms)
1442 GList *tokens = tokenize_command_line (cmd);
1444 for (rest = tokens; rest; rest = rest->next)
1446 char *option = rest->data;
1449 if (option[0] != '-' && option[0] != '+')
1452 fprintf (stderr, "%s: WARNING: %s: not a switch: \"%s\"\n",
1453 blurb(), filename, option);
1459 if (rest->next) /* pop off the arg to this option */
1461 char *s = (char *) rest->next->data;
1462 /* the next token is the next switch iff it matches "-[a-z]".
1463 (To avoid losing on "-x -3.1".)
1465 if (s && (s[0] != '-' || !isalpha(s[1])))
1468 rest->next->data = 0;
1473 parse_command_line_into_parameters_1 (filename, parms,
1475 if (value) free (value);
1479 g_list_free (tokens);
1484 compare_opts (const char *option, const char *value,
1485 const char *template)
1487 int ol = strlen (option);
1490 if (strncmp (option, template, ol))
1493 if (template[ol] != (value ? ' ' : 0))
1496 /* At this point, we have a match against "option".
1497 If template contains a %, we're done.
1498 Else, compare against "value" too.
1500 c = strchr (template, '%');
1505 return (template[ol] == 0);
1506 if (strcmp (template + ol + 1, value))
1514 parse_command_line_into_parameters_1 (const char *filename,
1521 parameter *match = 0;
1525 for (p = parms; p; p = p->next)
1527 parameter *pp = (parameter *) p->data;
1530 if (pp->type == SELECT)
1532 if (parse_command_line_into_parameters_1 (filename,
1543 if (compare_opts (option, value, (char *) pp->arg))
1549 else if (pp->arg_set)
1551 if (compare_opts (option, value, (char *) pp->arg_set))
1557 else if (pp->arg_unset)
1559 if (compare_opts (option, value, (char *) pp->arg_unset))
1574 if (debug_p && !parent)
1575 fprintf (stderr, "%s: WARNING: %s: no match for %s %s\n",
1576 blurb(), filename, option, (value ? value : ""));
1580 switch (match->type)
1586 if (which != -1) abort();
1587 parameter_set_switch (match, (gpointer) value);
1590 if (which != 0 && which != 1) abort();
1591 parameter_set_switch (match, GINT_TO_POINTER(which));
1594 if (which != 1) abort();
1595 parameter_set_switch (parent, GINT_TO_POINTER(index));
1604 /* Set the parameter's value.
1605 For STRING, FILENAME, SLIDER, and SPINBUTTON, `value' is a char*.
1606 For BOOLEAN and SELECT, `value' is an int.
1609 parameter_set_switch (parameter *p, gpointer value)
1611 if (p->type == SELECT_OPTION) abort();
1612 if (!p->widget) return;
1618 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) value);
1624 GtkAdjustment *adj =
1626 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1627 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1631 if (1 == sscanf ((char *) value, "%f %c", &f, &c))
1634 f = invert_range (adj->lower, adj->upper, f) - 1;
1635 gtk_adjustment_set_value (adj, f);
1641 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1642 gtk_toggle_button_set_active (b, GPOINTER_TO_INT(value));
1647 gtk_option_menu_set_history (GTK_OPTION_MENU (p->widget),
1648 GPOINTER_TO_INT(value));
1658 restore_defaults (const char *progname, GList *parms)
1660 for (; parms; parms = parms->next)
1662 parameter *p = (parameter *) parms->data;
1663 if (!p->widget) continue;
1669 gtk_entry_set_text (GTK_ENTRY (p->widget),
1670 (p->string ? (char *) p->string : ""));
1676 GtkAdjustment *adj =
1678 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1679 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1680 float value = (p->invert_p
1681 ? invert_range (p->low, p->high, p->value)
1683 gtk_adjustment_set_value (adj, value);
1688 /* A toggle button should be on by default if it inserts
1689 nothing into the command line when on. E.g., it should
1690 be on if `arg_set' is null.
1692 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->widget),
1693 (!p->arg_set || !*p->arg_set));
1698 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1703 for (opts = p->options, index = 0; opts;
1704 opts = opts->next, index++)
1706 parameter *s = (parameter *) opts->data;
1707 /* The default menu item is the first one with
1708 no `arg_set' field. */
1716 gtk_option_menu_set_history (GTK_OPTION_MENU (opt), selected);
1727 /* Documentation strings
1731 get_description (GList *parms, gboolean verbose_p)
1734 for (; parms; parms = parms->next)
1736 parameter *p = (parameter *) parms->data;
1737 if (p->type == DESCRIPTION)
1744 if (!doc || !doc->string)
1748 char *d = strdup ((char *) doc->string);
1751 for (s = d; *s; s++)
1754 if (s[1] == '\n') /* blank line: leave it */
1756 else if (s[1] == ' ' || s[1] == '\t')
1757 s++; /* next line is indented: leave newline */
1759 s[0] = ' '; /* delete newline to un-fold this line */
1762 /* strip off leading whitespace on first line only */
1763 for (s = d; *s && (*s == ' ' || *s == '\t'); s++)
1765 while (*s == '\n') /* strip leading newlines */
1768 memmove (d, s, strlen(s)+1);
1770 /* strip off trailing whitespace and newlines */
1773 while (L && isspace(d[L-1]))
1777 /* strip off duplicated whitespaces */
1778 for (s = d; *s; s++)
1785 memmove (p, s, strlen(s)+1);
1791 fprintf (stderr, "%s: text read is \"%s\"\n", blurb(),doc->string);
1792 fprintf (stderr, "%s: description is \"%s\"\n", blurb(), d);
1793 fprintf (stderr, "%s: translation is \"%s\"\n", blurb(), _(d));
1802 /* External interface.
1806 load_configurator_1 (const char *program, const char *arguments,
1809 const char *dir = hack_configuration_path;
1810 int L = strlen (dir);
1816 if (L == 0) return 0;
1818 file = (char *) malloc (L + strlen (program) + 10);
1819 data = (conf_data *) calloc (1, sizeof(*data));
1822 if (file[L-1] != '/')
1824 strcpy (file+L, program);
1826 for (s = file+L; *s; s++)
1827 if (*s == '/' || *s == ' ')
1829 else if (isupper (*s))
1832 strcat (file+L, ".xml");
1834 f = fopen (file, "r");
1837 int res, size = 1024;
1839 xmlParserCtxtPtr ctxt;
1845 fprintf (stderr, "%s: reading %s...\n", blurb(), file);
1847 res = fread (chars, 1, 4, f);
1855 ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, file);
1856 while ((res = fread(chars, 1, size, f)) > 0)
1857 xmlParseChunk (ctxt, chars, res, 0);
1858 xmlParseChunk (ctxt, chars, 0, 1);
1860 xmlFreeParserCtxt (ctxt);
1863 /* Parsed the XML file. Now make some widgets. */
1865 table = gtk_table_new (1, 3, FALSE);
1866 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
1867 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
1868 gtk_container_set_border_width (GTK_CONTAINER (table), 8);
1869 gtk_widget_show (table);
1871 parms = make_parameters (file, doc->xmlRootNode, table);
1872 sanity_check_parameters (file, parms);
1876 restore_defaults (program, parms);
1877 if (arguments && *arguments)
1878 parse_command_line_into_parameters (program, arguments, parms);
1880 data->widget = table;
1881 data->parameters = parms;
1882 data->description = get_description (parms, verbose_p);
1889 fprintf (stderr, "%s: %s does not exist.\n", blurb(), file);
1891 p = calloc (1, sizeof(*p));
1893 p->arg = (xmlChar *) strdup (arguments);
1895 data->parameters = g_list_append (0, (gpointer) p);
1898 data->progname = strdup (program);
1906 split_command_line (const char *full_command_line,
1907 char **prog_ret, char **args_ret)
1909 char *line = strdup (full_command_line);
1922 while (isspace (*s)) s++;
1925 else if (*s == '=') /* if the leading word contains an "=", skip it. */
1927 while (*s && !isspace (*s)) s++;
1928 while (isspace (*s)) s++;
1935 *prog_ret = strdup (prog);
1936 *args_ret = strdup (args);
1942 load_configurator (const char *full_command_line, gboolean verbose_p)
1947 debug_p = verbose_p;
1948 split_command_line (full_command_line, &prog, &args);
1949 cd = load_configurator_1 (prog, args, verbose_p);
1958 get_configurator_command_line (conf_data *data)
1960 char *args = parameters_to_cmd_line (data->parameters);
1961 char *result = (char *) malloc (strlen (data->progname) +
1963 strcpy (result, data->progname);
1964 strcat (result, " ");
1965 strcat (result, args);
1972 set_configurator_command_line (conf_data *data, const char *full_command_line)
1976 split_command_line (full_command_line, &prog, &args);
1977 if (data->progname) free (data->progname);
1978 data->progname = prog;
1979 restore_defaults (prog, data->parameters);
1980 parse_command_line_into_parameters (prog, args, data->parameters);
1985 free_conf_data (conf_data *data)
1987 if (data->parameters)
1990 for (rest = data->parameters; rest; rest = rest->next)
1992 free_parameter ((parameter *) rest->data);
1995 g_list_free (data->parameters);
1996 data->parameters = 0;
2000 gtk_widget_destroy (data->widget);
2003 free (data->progname);
2004 if (data->description)
2005 free (data->description);
2007 memset (data, ~0, sizeof(*data));
2012 #endif /* HAVE_GTK && HAVE_XML -- whole file */