1 /* demo-Gtk-conf.c --- implements the dynamic configuration dialogs.
2 * xscreensaver, Copyright (c) 2001 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 static gboolean debug_p = FALSE;
92 char *id; /* widget name */
93 char *label; /* heading label, or null */
95 /* command, fake, description, fakepreview, string, file
97 char *string; /* file name, description, whatever. */
101 char *low_label; /* label for the left side */
102 char *high_label; /* label for the right side */
103 float low; /* minimum value */
104 float high; /* maximum value */
105 float value; /* default value */
106 gboolean integer_p; /* whether the range is integral, or real */
107 char *arg; /* command-line option to set (substitute "%") */
108 gboolean invert_p; /* whether to flip the value and pretend the
109 range goes from hi-low instead of low-hi. */
111 /* boolean, select-option
113 char *arg_set; /* command-line option to set for "yes", or null */
114 char *arg_unset; /* command-line option to set for "no", or null */
115 char *test; /* #### no idea - enablement? */
130 static parameter *make_select_option (const char *file, xmlNodePtr);
131 static void make_parameter_widget (const char *filename,
132 parameter *, GtkWidget *, int *);
133 static void browse_button_cb (GtkButton *button, gpointer user_data);
136 /* Frees the parameter object and all strings and sub-parameters.
137 Does not destroy the widget, if any.
140 free_parameter (parameter *p)
143 if (p->id) free (p->id);
144 if (p->label) free (p->label);
145 if (p->string) free (p->string);
146 if (p->low_label) free (p->low_label);
147 if (p->high_label) free (p->high_label);
148 if (p->arg) free (p->arg);
149 if (p->arg_set) free (p->arg_set);
150 if (p->arg_unset) free (p->arg_unset);
151 if (p->test) free (p->test);
153 for (rest = p->options; rest; rest = rest->next)
155 free_parameter ((parameter *) rest->data);
157 for (rest = p->enable; rest; rest = rest->next)
159 free ((char *) rest->data);
162 if (p->enable) g_list_free (p->enable);
164 memset (p, ~0, sizeof(*p));
169 /* Debugging: dumps out a `parameter' structure.
173 describe_parameter (FILE *out, parameter *p)
178 case COMMAND: fprintf (out, "command"); break;
179 case FAKE: fprintf (out, "fake"); break;
180 case DESCRIPTION: fprintf (out, "_description"); break;
181 case FAKEPREVIEW: fprintf (out, "fakepreview"); break;
182 case STRING: fprintf (out, "string"); break;
183 case FILENAME: fprintf (out, "filename"); break;
184 case SLIDER: fprintf (out, "number type=\"slider\""); break;
185 case SPINBUTTON: fprintf (out, "number type=\"spinbutton\""); break;
186 case BOOLEAN: fprintf (out, "boolean"); break;
187 case SELECT: fprintf (out, "select"); break;
188 default: abort(); break;
190 if (p->id) fprintf (out, " id=\"%s\"", p->id);
191 if (p->label) fprintf (out, " _label=\"%s\"", p->label);
192 if (p->string && p->type != DESCRIPTION)
193 fprintf (out, " string=\"%s\"", p->string);
194 if (p->low_label) fprintf (out, " _low-label=\"%s\"", p->low_label);
195 if (p->high_label) fprintf (out, " _high-label=\"%s\"", p->high_label);
196 if (p->low) fprintf (out, " low=\"%.2f\"", p->low);
197 if (p->high) fprintf (out, " high=\"%.2f\"", p->high);
198 if (p->value) fprintf (out, " default=\"%.2f\"", p->value);
199 if (p->arg) fprintf (out, " arg=\"%s\"", p->arg);
200 if (p->invert_p) fprintf (out, " convert=\"invert\"");
201 if (p->arg_set) fprintf (out, " arg-set=\"%s\"", p->arg_set);
202 if (p->arg_unset) fprintf (out, " arg-unset=\"%s\"", p->arg_unset);
203 if (p->test) fprintf (out, " test=\"%s\"", p->test);
204 fprintf (out, ">\n");
206 if (p->type == SELECT)
209 for (opt = p->options; opt; opt = opt->next)
211 parameter *o = (parameter *) opt->data;
212 if (o->type != SELECT_OPTION) abort();
213 fprintf (out, " <option");
214 if (o->id) fprintf (out, " id=\"%s\"", o->id);
215 if (o->label) fprintf (out, " _label=\"%s\"", o->label);
216 if (o->arg_set) fprintf (out, " arg-set=\"%s\"", o->arg_set);
217 if (o->arg_unset) fprintf (out, " arg-unset=\"%s\"", o->arg_unset);
218 if (o->test) fprintf (out, " test=\"%s\"", o->test);
222 fprintf (out, " enable=\"");
223 for (e = o->enable; e; e = e->next)
224 fprintf (out, "%s%s", (char *) e->data, (e->next ? "," : ""));
227 fprintf (out, ">\n");
229 fprintf (out, "</select>\n");
231 else if (p->type == DESCRIPTION)
234 fprintf (out, " %s\n", p->string);
235 fprintf (out, "</_description>\n");
241 /* Like xmlGetProp() but parses a float out of the string.
242 If the number was expressed as a float and not an integer
243 (that is, the string contained a decimal point) then
244 `floatp' is set to TRUE. Otherwise, it is unchanged.
247 xml_get_float (xmlNodePtr node, const char *name, gboolean *floatpP)
249 const char *s = xmlGetProp (node, name);
252 if (!s || 1 != sscanf (s, "%f %c", &f, &c))
256 if (strchr (s, '.')) *floatpP = TRUE;
262 static void sanity_check_parameter (const char *filename,
263 const char *node_name,
266 /* Allocates and returns a new `parameter' object based on the
267 properties in the given XML node. Returns 0 if there's nothing
268 to create (comment, or unknown tag.)
271 make_parameter (const char *filename, xmlNodePtr node)
274 const char *name = node->name;
276 gboolean floatp = FALSE;
278 if (node->type == XML_COMMENT_NODE)
281 p = calloc (1, sizeof(*p));
284 else if (!strcmp (name, "command")) p->type = COMMAND;
285 else if (!strcmp (name, "fullcommand")) p->type = COMMAND;
286 else if (!strcmp (name, "_description")) p->type = DESCRIPTION;
287 else if (!strcmp (name, "fakepreview")) p->type = FAKEPREVIEW;
288 else if (!strcmp (name, "fake")) p->type = FAKE;
289 else if (!strcmp (name, "boolean")) p->type = BOOLEAN;
290 else if (!strcmp (name, "string")) p->type = STRING;
291 else if (!strcmp (name, "file")) p->type = FILENAME;
292 else if (!strcmp (name, "number")) p->type = SPINBUTTON;
293 else if (!strcmp (name, "select")) p->type = SELECT;
297 fprintf (stderr, "%s: WARNING: %s: unknown tag: \"%s\"\n",
298 blurb(), filename, name);
303 if (p->type == SPINBUTTON)
305 const char *type = xmlGetProp (node, "type");
306 if (!type || !strcmp (type, "spinbutton")) p->type = SPINBUTTON;
307 else if (!strcmp (type, "slider")) p->type = SLIDER;
311 fprintf (stderr, "%s: WARNING: %s: unknown %s type: \"%s\"\n",
312 blurb(), filename, name, type);
317 else if (p->type == DESCRIPTION)
319 if (node->xmlChildrenNode &&
320 node->xmlChildrenNode->type == XML_TEXT_NODE &&
321 !node->xmlChildrenNode->next)
322 p->string = strdup (node->xmlChildrenNode->content);
325 p->id = xmlGetProp (node, "id");
326 p->label = xmlGetProp (node, "_label");
327 p->low_label = xmlGetProp (node, "_low-label");
328 p->high_label = xmlGetProp (node, "_high-label");
329 p->low = xml_get_float (node, "low", &floatp);
330 p->high = xml_get_float (node, "high", &floatp);
331 p->value = xml_get_float (node, "default", &floatp);
332 p->integer_p = !floatp;
333 convert = xmlGetProp (node, "convert");
334 p->invert_p = (convert && !strcmp (convert, "invert"));
335 p->arg = xmlGetProp (node, "arg");
336 p->arg_set = xmlGetProp (node, "arg-set");
337 p->arg_unset = xmlGetProp (node, "arg-unset");
338 p->test = xmlGetProp (node, "test");
340 /* Check for missing decimal point */
343 (p->high != p->low) &&
344 (p->high - p->low) <= 1)
346 "%s: WARNING: %s: %s: range [%.1f, %.1f] shouldn't be integral!\n",
347 blurb(), filename, p->id,
350 if (p->type == SELECT)
353 for (kids = node->xmlChildrenNode; kids; kids = kids->next)
355 parameter *s = make_select_option (filename, kids);
357 p->options = g_list_append (p->options, s);
361 sanity_check_parameter (filename, name, p);
367 /* Allocates and returns a new SELECT_OPTION `parameter' object based
368 on the properties in the given XML node. Returns 0 if there's nothing
369 to create (comment, or unknown tag.)
372 make_select_option (const char *filename, xmlNodePtr node)
374 if (node->type == XML_COMMENT_NODE)
376 else if (node->type != XML_ELEMENT_NODE)
380 "%s: WARNING: %s: %s: unexpected child tag type %d\n",
381 blurb(), filename, node->name, (int)node->type);
384 else if (strcmp (node->name, "option"))
388 "%s: WARNING: %s: %s: child not an option tag: \"%s\"\n",
389 blurb(), filename, node->name, node->name);
394 parameter *s = calloc (1, sizeof(*s));
397 s->type = SELECT_OPTION;
398 s->id = xmlGetProp (node, "id");
399 s->label = xmlGetProp (node, "_label");
400 s->arg_set = xmlGetProp (node, "arg-set");
401 s->arg_unset = xmlGetProp (node, "arg-unset");
402 s->test = xmlGetProp (node, "test");
403 enable = xmlGetProp (node, "enable");
407 enable = strdup (enable);
408 e = strtok (enable, ", ");
411 s->enable = g_list_append (s->enable, strdup (e));
412 e = strtok (0, ", ");
417 sanity_check_parameter (filename, node->name, s);
423 /* Rudimentary check to make sure someone hasn't typed "arg-set="
424 when they should have typed "arg=", etc.
427 sanity_check_parameter (const char *filename, const char *node_name,
445 memset (&allowed, 0, sizeof (allowed));
446 memset (&require, 0, sizeof (require));
457 allowed.string = TRUE;
464 allowed.label = TRUE;
465 require.label = TRUE;
472 allowed.label = TRUE;
479 allowed.label = TRUE;
480 allowed.low_label = TRUE;
481 allowed.high_label = TRUE;
485 /* require.low = TRUE; -- may be 0 */
487 /* require.high = TRUE; -- may be 0 */
488 allowed.value = TRUE;
489 /* require.value = TRUE; -- may be 0 */
490 allowed.invert_p = TRUE;
495 allowed.label = TRUE;
499 /* require.low = TRUE; -- may be 0 */
501 /* require.high = TRUE; -- may be 0 */
502 allowed.value = TRUE;
503 /* require.value = TRUE; -- may be 0 */
504 allowed.invert_p = TRUE;
509 allowed.label = TRUE;
510 allowed.arg_set = TRUE;
511 allowed.arg_unset = TRUE;
519 allowed.label = TRUE;
520 require.label = TRUE;
521 allowed.arg_set = TRUE;
529 fprintf (stderr, "%s: %s: " STR " in <%s%s id=\"%s\">\n", \
530 blurb(), filename, node_name, \
531 (!strcmp(node_name, "number") \
532 ? (p->type == SPINBUTTON ? " type=spinbutton" : " type=slider")\
534 (p->id ? p->id : ""))
535 # define CHECK(SLOT,NAME) \
536 if (p->SLOT && !allowed.SLOT) \
537 WARN ("\"" NAME "\" is not a valid option"); \
538 if (!p->SLOT && require.SLOT) \
539 WARN ("\"" NAME "\" is required")
542 CHECK (label, "_label");
543 CHECK (string, "(body text)");
544 CHECK (low_label, "_low-label");
545 CHECK (high_label, "_high-label");
547 CHECK (high, "high");
548 CHECK (value, "default");
550 CHECK (invert_p, "convert");
551 CHECK (arg_set, "arg-set");
552 CHECK (arg_unset, "arg-unset");
559 /* Helper for make_parameters()
562 make_parameters_1 (const char *filename, xmlNodePtr node,
563 GtkWidget *parent, int *row)
567 for (; node; node = node->next)
569 const char *name = node->name;
570 if (!strcmp (name, "hgroup") ||
571 !strcmp (name, "vgroup"))
573 GtkWidget *box = (*name == 'h'
574 ? gtk_hbox_new (FALSE, 0)
575 : gtk_vbox_new (FALSE, 0));
577 gtk_widget_show (box);
580 gtk_table_attach (GTK_TABLE (parent), box, 0, 3, *row, *row + 1,
583 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, FALSE, 0);
588 list2 = make_parameters_1 (filename, node->xmlChildrenNode, box, 0);
590 list = g_list_concat (list, list2);
594 parameter *p = make_parameter (filename, node);
597 list = g_list_append (list, p);
598 make_parameter_widget (filename, p, parent, row);
606 /* Calls make_parameter() and make_parameter_widget() on each relevant
607 tag in the XML tree. Also handles the "hgroup" and "vgroup" flags.
608 Returns a GList of `parameter' objects.
611 make_parameters (const char *filename, xmlNodePtr node, GtkWidget *parent)
614 for (; node; node = node->next)
616 if (node->type == XML_ELEMENT_NODE &&
617 !strcmp (node->name, "screensaver"))
618 return make_parameters_1 (filename, node->xmlChildrenNode, parent, &row);
625 invert_range (gfloat low, gfloat high, gfloat value)
627 gfloat range = high-low;
628 gfloat off = value-low;
629 return (low + (range - off));
633 static GtkAdjustment *
634 make_adjustment (const char *filename, parameter *p)
636 float range = (p->high - p->low);
637 float value = (p->invert_p
638 ? invert_range (p->low, p->high, p->value)
640 gfloat si = (p->high - p->low) / 100;
641 gfloat pi = (p->high - p->low) / 10;
643 if (p->value < p->low || p->value > p->high)
645 if (debug_p && p->integer_p)
646 fprintf (stderr, "%s: WARNING: %s: %d is not in range [%d, %d]\n",
648 (int) p->value, (int) p->low, (int) p->high);
651 "%s: WARNING: %s: %.2f is not in range [%.2f, %.2f]\n",
652 blurb(), filename, p->value, p->low, p->high);
653 value = (value < p->low ? p->low : p->high);
656 else if (debug_p && p->value < 1000 && p->high >= 10000)
660 "%s: WARNING: %s: %d is suspicious for range [%d, %d]\n",
662 (int) p->value, (int) p->low, (int) p->high);
665 "%s: WARNING: %s: %.2f is suspicious for range [%.2f, %.2f]\n",
666 blurb(), filename, p->value, p->low, p->high);
672 si = (int) (si + 0.5);
673 pi = (int) (pi + 0.5);
677 if (range <= 500) si = 1;
679 return GTK_ADJUSTMENT (gtk_adjustment_new (value, p->low, p->high,
685 /* Given a `parameter' struct, allocates an appropriate GtkWidget for it,
686 and stores it in `p->widget'.
687 `row' is used for keeping track of our position during table layout.
688 `parent' must be a GtkTable or a GtkBox.
691 make_parameter_widget (const char *filename,
692 parameter *p, GtkWidget *parent, int *row)
694 const char *label = p->label;
695 if (p->widget) return;
703 GtkWidget *w = gtk_label_new (_(label));
704 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
705 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
708 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
711 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
714 p->widget = gtk_entry_new ();
716 gtk_entry_set_text (GTK_ENTRY (p->widget), p->string);
718 gtk_table_attach (GTK_TABLE (parent), p->widget, 1, 3,
722 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
727 GtkWidget *L = gtk_label_new (label ? _(label) : "");
728 GtkWidget *entry = gtk_entry_new ();
729 GtkWidget *button = gtk_button_new_with_label (_("Browse..."));
730 gtk_widget_show (entry);
731 gtk_widget_show (button);
734 gtk_signal_connect (GTK_OBJECT (button),
735 "clicked", GTK_SIGNAL_FUNC (browse_button_cb),
738 gtk_label_set_justify (GTK_LABEL (L), GTK_JUSTIFY_RIGHT);
739 gtk_misc_set_alignment (GTK_MISC (L), 1.0, 0.5);
743 gtk_entry_set_text (GTK_ENTRY (entry), p->string);
747 gtk_table_attach (GTK_TABLE (parent), L, 0, 1,
750 gtk_table_attach (GTK_TABLE (parent), entry, 1, 2,
752 GTK_EXPAND | GTK_FILL, 0, 0, 0);
753 gtk_table_attach (GTK_TABLE (parent), button, 2, 3,
759 gtk_box_pack_start (GTK_BOX (parent), L, FALSE, FALSE, 4);
760 gtk_box_pack_start (GTK_BOX (parent), entry, TRUE, TRUE, 4);
761 gtk_box_pack_start (GTK_BOX (parent), button, FALSE, FALSE, 4);
767 GtkAdjustment *adj = make_adjustment (filename, p);
768 GtkWidget *scale = gtk_hscale_new (adj);
769 GtkWidget *labelw = 0;
773 labelw = gtk_label_new (_(label));
774 gtk_label_set_justify (GTK_LABEL (labelw), GTK_JUSTIFY_LEFT);
775 gtk_misc_set_alignment (GTK_MISC (labelw), 0.0, 0.5);
776 gtk_widget_show (labelw);
779 if (GTK_IS_VBOX (parent))
781 /* If we're inside a vbox, we need to put an hbox in it, to get
782 the low/high labels to be to the left/right of the slider.
784 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
786 /* But if we have a label, put that above the slider's hbox. */
789 gtk_box_pack_start (GTK_BOX (parent), labelw, FALSE, TRUE, 2);
793 gtk_box_pack_start (GTK_BOX (parent), hbox, TRUE, TRUE, 6);
794 gtk_widget_show (hbox);
802 gtk_table_attach (GTK_TABLE (parent), labelw,
803 0, 3, *row, *row + 1,
804 GTK_EXPAND | GTK_FILL, 0, 0, 0);
809 if (GTK_IS_HBOX (parent))
811 GtkWidget *box = gtk_vbox_new (FALSE, 0);
812 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, TRUE, 0);
813 gtk_widget_show (box);
814 gtk_box_pack_start (GTK_BOX (box), labelw, FALSE, TRUE, 4);
816 box = gtk_hbox_new (FALSE, 0);
817 gtk_widget_show (box);
818 gtk_box_pack_start (GTK_BOX (parent), box, TRUE, TRUE, 0);
822 gtk_box_pack_start (GTK_BOX (parent), labelw,
829 GtkWidget *w = gtk_label_new (_(p->low_label));
830 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
831 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
834 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
837 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
840 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM);
841 gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
842 gtk_scale_set_digits (GTK_SCALE (scale), (p->integer_p ? 0 : 2));
844 gtk_table_attach (GTK_TABLE (parent), scale, 1, 2,
846 GTK_EXPAND | GTK_FILL, 0, 0, 0);
848 gtk_box_pack_start (GTK_BOX (parent), scale, TRUE, TRUE, 4);
850 gtk_widget_show (scale);
854 GtkWidget *w = gtk_label_new (_(p->high_label));
855 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
856 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
859 gtk_table_attach (GTK_TABLE (parent), w, 2, 3, *row, *row + 1,
862 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
870 GtkAdjustment *adj = make_adjustment (filename, p);
871 GtkWidget *spin = gtk_spin_button_new (adj, 15, 0);
872 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
873 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (spin), TRUE);
874 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), adj->value);
878 GtkWidget *w = gtk_label_new (_(label));
879 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
880 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
883 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
886 gtk_box_pack_start (GTK_BOX (parent), w, TRUE, TRUE, 4);
889 gtk_widget_show (spin);
892 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
893 gtk_widget_show (hbox);
894 gtk_table_attach (GTK_TABLE (parent), hbox, 1, 3,
896 GTK_EXPAND | GTK_FILL, 0, 8, 0);
897 gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0);
900 gtk_box_pack_start (GTK_BOX (parent), spin, FALSE, FALSE, 4);
907 p->widget = gtk_check_button_new_with_label (label);
909 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
911 GTK_EXPAND | GTK_FILL, 0, 0, 0);
913 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
918 GtkWidget *opt = gtk_option_menu_new ();
919 GtkWidget *menu = gtk_menu_new ();
924 GtkWidget *w = gtk_label_new (_(label));
925 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
926 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
928 gtk_table_attach (GTK_TABLE (parent), w, 0, 3, *row, *row + 1,
929 GTK_EXPAND | GTK_FILL, 0, 0, 0);
933 for (opts = p->options; opts; opts = opts->next)
935 parameter *s = (parameter *) opts->data;
936 GtkWidget *i = gtk_menu_item_new_with_label (s->label);
938 gtk_menu_append (GTK_MENU (menu), i);
941 gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
944 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
946 GTK_EXPAND | GTK_FILL, 0, 0, 0);
948 gtk_box_pack_start (GTK_BOX (parent), p->widget, TRUE, TRUE, 4);
963 gtk_widget_set_name (p->widget, p->id);
964 gtk_widget_show (p->widget);
972 Absurdly, there is no GTK file entry widget, only a GNOME one,
973 so in order to avoid depending on GNOME in this code, we have
977 /* cancel button on GtkFileSelection: user_data unused */
979 file_sel_cancel (GtkWidget *button, gpointer user_data)
981 GtkWidget *dialog = button;
982 while (dialog->parent)
983 dialog = dialog->parent;
984 gtk_widget_destroy (dialog);
987 /* ok button on GtkFileSelection: user_data is the corresponding GtkEntry */
989 file_sel_ok (GtkWidget *button, gpointer user_data)
991 GtkWidget *entry = GTK_WIDGET (user_data);
992 GtkWidget *dialog = button;
994 while (dialog->parent)
995 dialog = dialog->parent;
996 gtk_widget_hide (dialog);
998 path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog));
999 /* apparently one doesn't free `path' */
1001 gtk_entry_set_text (GTK_ENTRY (entry), path);
1002 gtk_entry_set_position (GTK_ENTRY (entry), strlen (path));
1004 gtk_widget_destroy (dialog);
1007 /* WM close on GtkFileSelection: user_data unused */
1009 file_sel_close (GtkWidget *widget, GdkEvent *event, gpointer user_data)
1011 file_sel_cancel (widget, user_data);
1014 /* "Browse" button: user_data is the corresponding GtkEntry */
1016 browse_button_cb (GtkButton *button, gpointer user_data)
1018 GtkWidget *entry = GTK_WIDGET (user_data);
1019 char *text = gtk_entry_get_text (GTK_ENTRY (entry));
1020 GtkFileSelection *selector =
1021 GTK_FILE_SELECTION (gtk_file_selection_new (_("Select file.")));
1023 gtk_file_selection_set_filename (selector, text);
1024 gtk_signal_connect (GTK_OBJECT (selector->ok_button),
1025 "clicked", GTK_SIGNAL_FUNC (file_sel_ok),
1027 gtk_signal_connect (GTK_OBJECT (selector->cancel_button),
1028 "clicked", GTK_SIGNAL_FUNC (file_sel_cancel),
1030 gtk_signal_connect (GTK_OBJECT (selector), "delete_event",
1031 GTK_SIGNAL_FUNC (file_sel_close),
1034 gtk_window_set_modal (GTK_WINDOW (selector), TRUE);
1035 gtk_widget_show (GTK_WIDGET (selector));
1039 /* Converting to and from command-lines
1043 /* Returns a copy of string that has been quoted according to shell rules:
1044 it may have been wrapped in "" and had some characters backslashed; or
1045 it may be unchanged.
1048 shell_quotify (const char *string)
1050 char *string2 = (char *) malloc ((strlen (string) * 2) + 10);
1053 int need_quotes = 0;
1058 for (in = string; *in; in++)
1069 else if (*in <= ' ' ||
1095 return strdup (string);
1098 /* Modify the string in place to remove wrapping double-quotes
1099 and interior backslashes.
1102 de_stringify (char *s)
1105 if (q != '\'' && q != '\"' && q != '`')
1107 memmove (s, s+1, strlen (s)+1);
1108 while (*s && *s != q)
1111 memmove (s, s+1, strlen (s)+1);
1114 if (*s != q) abort();
1119 /* Substitutes a shell-quotified version of `value' into `p->arg' at
1120 the place where the `%' character appeared.
1123 format_switch (parameter *p, const char *value)
1128 if (!fmt || !value) return 0;
1129 v2 = shell_quotify (value);
1130 result = (char *) malloc (strlen (fmt) + strlen (v2) + 10);
1147 /* Maps a `parameter' to a command-line switch.
1148 Returns 0 if it can't, or if the parameter has the default value.
1151 parameter_to_switch (parameter *p)
1157 return strdup (p->arg);
1163 if (!p->widget) return 0;
1165 const char *s = gtk_entry_get_text (GTK_ENTRY (p->widget));
1167 if (!strcmp ((s ? s : ""),
1168 (p->string ? p->string : "")))
1169 v = 0; /* same as default */
1171 v = format_switch (p, s);
1173 /* don't free `s' */
1178 if (!p->widget) return 0;
1180 GtkAdjustment *adj =
1182 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1183 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1186 float value = (p->invert_p
1187 ? invert_range (adj->lower, adj->upper, adj->value)
1190 if (value == p->value) /* same as default */
1194 sprintf (buf, "%d", (int) (value + 0.5));
1196 sprintf (buf, "%.4f", value);
1198 s1 = strchr (buf, '.');
1201 char *s2 = s1 + strlen(s1) - 1;
1202 while (s2 > s1 && *s2 == '0') /* lose trailing zeroes */
1204 if (s2 >= s1 && *s2 == '.') /* lose trailing decimal */
1207 return format_switch (p, buf);
1210 if (!p->widget) return 0;
1212 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1213 const char *s = (gtk_toggle_button_get_active (b)
1222 if (!p->widget) return 0;
1224 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1225 GtkMenu *menu = GTK_MENU (gtk_option_menu_get_menu (opt));
1226 GtkWidget *selected = gtk_menu_get_active (menu);
1227 GList *kids = gtk_container_children (GTK_CONTAINER (menu));
1228 int menu_elt = g_list_index (kids, (gpointer) selected);
1229 GList *ol = g_list_nth (p->options, menu_elt);
1230 parameter *o = (ol ? (parameter *) ol->data : 0);
1233 if (o->type != SELECT_OPTION) abort();
1248 /* Maps a GList of `parameter' objects to a complete command-line string.
1249 All arguments will be properly quoted.
1252 parameters_to_cmd_line (GList *parms)
1254 int L = g_list_length (parms);
1256 char **strs = (char **) calloc (sizeof (*parms), L);
1261 for (i = 0; parms; parms = parms->next, i++)
1263 char *s = parameter_to_switch ((parameter *) parms->data);
1265 LL += (s ? strlen(s) : 0) + 1;
1268 result = (char *) malloc (LL + 10);
1270 for (i = 0; i < L; i++)
1273 strcpy (out, strs[i]);
1274 out += strlen (out);
1279 while (out > result && out[-1] == ' ') /* strip trailing spaces */
1287 /* Returns a GList of the tokens the string, using shell syntax;
1288 Quoted strings are handled as a single token.
1291 tokenize_command_line (const char *cmd)
1294 const char *s = cmd;
1299 for (; isspace(*s); s++); /* skip whitespace */
1302 if (*s == '\'' || *s == '\"' || *s == '`')
1306 while (*s && *s != q) /* skip to matching quote */
1308 if (*s == '\\' && s[1]) /* allowing backslash quoting */
1326 ss = (char *) malloc ((s - start) + 1);
1327 strncpy (ss, start, s-start);
1329 if (*ss == '\'' || *ss == '\"' || *ss == '`')
1331 result = g_list_append (result, ss);
1338 static void parameter_set_switch (parameter *, gpointer value);
1339 static gboolean parse_command_line_into_parameters_1 (const char *filename,
1346 /* Parses the command line, and flushes those options down into
1347 the `parameter' structs in the list.
1350 parse_command_line_into_parameters (const char *filename,
1351 const char *cmd, GList *parms)
1353 GList *tokens = tokenize_command_line (cmd);
1355 for (rest = tokens; rest; rest = rest->next)
1357 char *option = rest->data;
1360 if (option[0] != '-')
1363 fprintf (stderr, "%s: WARNING: %s: not a switch: \"%s\"\n",
1364 blurb(), filename, option);
1370 if (rest->next) /* pop off the arg to this option */
1372 char *s = (char *) rest->next->data;
1373 /* the next token is the next switch iff it matches "-[a-z]".
1374 (To avoid losing on "-x -3.1".)
1376 if (s && (s[0] != '-' || !isalpha(s[1])))
1379 rest->next->data = 0;
1384 parse_command_line_into_parameters_1 (filename, parms,
1386 if (value) free (value);
1390 g_list_free (tokens);
1395 compare_opts (const char *option, const char *value,
1396 const char *template)
1398 int ol = strlen (option);
1401 if (strncmp (option, template, ol))
1404 if (template[ol] != (value ? ' ' : 0))
1407 /* At this point, we have a match against "option".
1408 If template contains a %, we're done.
1409 Else, compare against "value" too.
1411 c = strchr (template, '%');
1416 return (template[ol] == 0);
1417 if (strcmp (template + ol + 1, value))
1425 parse_command_line_into_parameters_1 (const char *filename,
1432 parameter *match = 0;
1436 for (p = parms; p; p = p->next)
1438 parameter *pp = (parameter *) p->data;
1441 if (pp->type == SELECT)
1443 if (parse_command_line_into_parameters_1 (filename,
1454 if (compare_opts (option, value, pp->arg))
1460 else if (pp->arg_set)
1462 if (compare_opts (option, value, pp->arg_set))
1468 else if (pp->arg_unset)
1470 if (compare_opts (option, value, pp->arg_unset))
1485 if (debug_p && !parent)
1486 fprintf (stderr, "%s: WARNING: %s: no match for %s %s\n",
1487 blurb(), filename, option, (value ? value : ""));
1491 switch (match->type)
1497 if (which != -1) abort();
1498 parameter_set_switch (match, (gpointer) value);
1501 if (which != 0 && which != 1) abort();
1502 parameter_set_switch (match, (gpointer) which);
1505 if (which != 1) abort();
1506 parameter_set_switch (parent, (gpointer) index);
1515 /* Set the parameter's value.
1516 For STRING, FILENAME, SLIDER, and SPINBUTTON, `value' is a char*.
1517 For BOOLEAN and SELECT, `value' is an int.
1520 parameter_set_switch (parameter *p, gpointer value)
1522 if (p->type == SELECT_OPTION) abort();
1523 if (!p->widget) return;
1529 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) value);
1535 GtkAdjustment *adj =
1537 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1538 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1542 if (1 == sscanf ((char *) value, "%f %c", &f, &c))
1545 f = invert_range (adj->lower, adj->upper, f);
1546 gtk_adjustment_set_value (adj, f);
1552 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1553 gtk_toggle_button_set_active (b, (int) value);
1558 gtk_option_menu_set_history (GTK_OPTION_MENU (p->widget),
1569 restore_defaults (const char *progname, GList *parms)
1571 for (; parms; parms = parms->next)
1573 parameter *p = (parameter *) parms->data;
1574 if (!p->widget) continue;
1580 gtk_entry_set_text (GTK_ENTRY (p->widget),
1581 (p->string ? p->string : ""));
1587 GtkAdjustment *adj =
1589 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1590 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1591 float value = (p->invert_p
1592 ? invert_range (p->low, p->high, p->value)
1594 gtk_adjustment_set_value (adj, value);
1599 /* A toggle button should be on by default if it inserts
1600 nothing into the command line when on. E.g., it should
1601 be on if `arg_set' is null.
1603 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->widget),
1604 (!p->arg_set || !*p->arg_set));
1609 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1614 for (opts = p->options, index = 0; opts;
1615 opts = opts->next, index++)
1617 parameter *s = (parameter *) opts->data;
1618 /* The default menu item is the first one with
1619 no `arg_set' field. */
1627 gtk_option_menu_set_history (GTK_OPTION_MENU (opt), selected);
1638 /* Documentation strings
1642 get_description (GList *parms)
1645 for (; parms; parms = parms->next)
1647 parameter *p = (parameter *) parms->data;
1648 if (p->type == DESCRIPTION)
1655 if (!doc || !doc->string)
1659 char *d = strdup (doc->string);
1661 for (s = d; *s; s++)
1664 if (s[1] == '\n') /* blank line: leave it */
1666 else if (s[1] == ' ' || s[1] == '\t')
1667 s++; /* next line is indented: leave newline */
1669 s[0] = ' '; /* delete newline to un-fold this line */
1672 /* strip off leading whitespace on first line only */
1673 for (s = d; *s && (*s == ' ' || *s == '\t'); s++)
1675 while (*s == '\n') /* strip leading newlines */
1678 memmove (d, s, strlen(s)+1);
1680 /* strip off trailing whitespace and newlines */
1683 while (L && isspace(d[L-1]))
1692 /* External interface.
1696 load_configurator_1 (const char *program, const char *arguments,
1699 const char *dir = HACK_CONFIGURATION_PATH;
1700 int L = strlen (dir);
1706 if (L == 0) return 0;
1708 file = (char *) malloc (L + strlen (program) + 10);
1709 data = (conf_data *) calloc (1, sizeof(*data));
1712 if (file[L-1] != '/')
1714 strcpy (file+L, program);
1716 for (s = file+L; *s; s++)
1717 if (*s == '/' || *s == ' ')
1719 else if (isupper (*s))
1722 strcat (file+L, ".xml");
1724 f = fopen (file, "r");
1727 int res, size = 1024;
1729 xmlParserCtxtPtr ctxt;
1735 fprintf (stderr, "%s: reading %s...\n", blurb(), file);
1737 res = fread (chars, 1, 4, f);
1745 ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, file);
1746 while ((res = fread(chars, 1, size, f)) > 0)
1747 xmlParseChunk (ctxt, chars, res, 0);
1748 xmlParseChunk (ctxt, chars, 0, 1);
1750 xmlFreeParserCtxt (ctxt);
1753 /* Parsed the XML file. Now make some widgets. */
1755 table = gtk_table_new (1, 3, FALSE);
1756 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
1757 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
1758 gtk_container_set_border_width (GTK_CONTAINER (table), 8);
1759 gtk_widget_show (table);
1761 parms = make_parameters (file, doc->xmlRootNode, table);
1765 restore_defaults (program, parms);
1766 if (arguments && *arguments)
1767 parse_command_line_into_parameters (program, arguments, parms);
1769 data->widget = table;
1770 data->parameters = parms;
1771 data->description = get_description (parms);
1778 fprintf (stderr, "%s: %s does not exist.\n", blurb(), file);
1780 p = calloc (1, sizeof(*p));
1782 p->arg = strdup (arguments);
1784 data->parameters = g_list_append (0, (gpointer) p);
1787 data->progname = strdup (program);
1795 split_command_line (const char *full_command_line,
1796 char **prog_ret, char **args_ret)
1798 char *line = strdup (full_command_line);
1811 while (isspace (*s)) s++;
1814 else if (*s == '=') /* if the leading word contains an "=", skip it. */
1816 while (*s && !isspace (*s)) s++;
1817 while (isspace (*s)) s++;
1824 *prog_ret = strdup (prog);
1825 *args_ret = strdup (args);
1831 load_configurator (const char *full_command_line, gboolean verbose_p)
1836 split_command_line (full_command_line, &prog, &args);
1837 cd = load_configurator_1 (prog, args, verbose_p);
1846 get_configurator_command_line (conf_data *data)
1848 char *args = parameters_to_cmd_line (data->parameters);
1849 char *result = (char *) malloc (strlen (data->progname) +
1851 strcpy (result, data->progname);
1852 strcat (result, " ");
1853 strcat (result, args);
1860 set_configurator_command_line (conf_data *data, const char *full_command_line)
1864 split_command_line (full_command_line, &prog, &args);
1865 if (data->progname) free (data->progname);
1866 data->progname = prog;
1867 restore_defaults (prog, data->parameters);
1868 parse_command_line_into_parameters (prog, args, data->parameters);
1873 free_conf_data (conf_data *data)
1875 if (data->parameters)
1878 for (rest = data->parameters; rest; rest = rest->next)
1880 free_parameter ((parameter *) rest->data);
1883 g_list_free (data->parameters);
1884 data->parameters = 0;
1888 gtk_widget_destroy (data->widget);
1891 free (data->progname);
1892 if (data->description)
1893 free (data->description);
1895 memset (data, ~0, sizeof(*data));
1900 #endif /* HAVE_GTK && HAVE_XML -- whole file */