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 const char *hack_configuration_path = HACK_CONFIGURATION_PATH;
72 static gboolean debug_p = FALSE;
94 char *id; /* widget name */
95 char *label; /* heading label, or null */
97 /* command, fake, description, fakepreview, string, file
99 char *string; /* file name, description, whatever. */
101 /* slider, spinbutton
103 char *low_label; /* label for the left side */
104 char *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 char *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 char *arg_set; /* command-line option to set for "yes", or null */
116 char *arg_unset; /* command-line option to set for "no", or null */
117 char *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 char *name, gboolean *floatpP)
251 const char *s = 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 char *node_name,
268 /* Allocates and returns a new `parameter' object based on the
269 properties in the given XML node. Returns 0 if there's nothing
270 to create (comment, or unknown tag.)
273 make_parameter (const char *filename, xmlNodePtr node)
276 const char *name = node->name;
278 gboolean floatp = FALSE;
280 if (node->type == XML_COMMENT_NODE)
283 p = calloc (1, sizeof(*p));
286 else if (!strcmp (name, "command")) p->type = COMMAND;
287 else if (!strcmp (name, "fullcommand")) p->type = COMMAND;
288 else if (!strcmp (name, "_description")) p->type = DESCRIPTION;
289 else if (!strcmp (name, "fakepreview")) p->type = FAKEPREVIEW;
290 else if (!strcmp (name, "fake")) p->type = FAKE;
291 else if (!strcmp (name, "boolean")) p->type = BOOLEAN;
292 else if (!strcmp (name, "string")) p->type = STRING;
293 else if (!strcmp (name, "file")) p->type = FILENAME;
294 else if (!strcmp (name, "number")) p->type = SPINBUTTON;
295 else if (!strcmp (name, "select")) p->type = SELECT;
299 fprintf (stderr, "%s: WARNING: %s: unknown tag: \"%s\"\n",
300 blurb(), filename, name);
305 if (p->type == SPINBUTTON)
307 const char *type = xmlGetProp (node, "type");
308 if (!type || !strcmp (type, "spinbutton")) p->type = SPINBUTTON;
309 else if (!strcmp (type, "slider")) p->type = SLIDER;
313 fprintf (stderr, "%s: WARNING: %s: unknown %s type: \"%s\"\n",
314 blurb(), filename, name, type);
319 else if (p->type == DESCRIPTION)
321 if (node->xmlChildrenNode &&
322 node->xmlChildrenNode->type == XML_TEXT_NODE &&
323 !node->xmlChildrenNode->next)
324 p->string = strdup (node->xmlChildrenNode->content);
327 p->id = xmlGetProp (node, "id");
328 p->label = xmlGetProp (node, "_label");
329 p->low_label = xmlGetProp (node, "_low-label");
330 p->high_label = xmlGetProp (node, "_high-label");
331 p->low = xml_get_float (node, "low", &floatp);
332 p->high = xml_get_float (node, "high", &floatp);
333 p->value = xml_get_float (node, "default", &floatp);
334 p->integer_p = !floatp;
335 convert = xmlGetProp (node, "convert");
336 p->invert_p = (convert && !strcmp (convert, "invert"));
337 p->arg = xmlGetProp (node, "arg");
338 p->arg_set = xmlGetProp (node, "arg-set");
339 p->arg_unset = xmlGetProp (node, "arg-unset");
340 p->test = xmlGetProp (node, "test");
342 /* Check for missing decimal point */
345 (p->high != p->low) &&
346 (p->high - p->low) <= 1)
348 "%s: WARNING: %s: %s: range [%.1f, %.1f] shouldn't be integral!\n",
349 blurb(), filename, p->id,
352 if (p->type == SELECT)
355 for (kids = node->xmlChildrenNode; kids; kids = kids->next)
357 parameter *s = make_select_option (filename, kids);
359 p->options = g_list_append (p->options, s);
363 sanity_check_parameter (filename, name, p);
369 /* Allocates and returns a new SELECT_OPTION `parameter' object based
370 on the properties in the given XML node. Returns 0 if there's nothing
371 to create (comment, or unknown tag.)
374 make_select_option (const char *filename, xmlNodePtr node)
376 if (node->type == XML_COMMENT_NODE)
378 else if (node->type != XML_ELEMENT_NODE)
382 "%s: WARNING: %s: %s: unexpected child tag type %d\n",
383 blurb(), filename, node->name, (int)node->type);
386 else if (strcmp (node->name, "option"))
390 "%s: WARNING: %s: %s: child not an option tag: \"%s\"\n",
391 blurb(), filename, node->name, node->name);
396 parameter *s = calloc (1, sizeof(*s));
399 s->type = SELECT_OPTION;
400 s->id = xmlGetProp (node, "id");
401 s->label = xmlGetProp (node, "_label");
402 s->arg_set = xmlGetProp (node, "arg-set");
403 s->arg_unset = xmlGetProp (node, "arg-unset");
404 s->test = xmlGetProp (node, "test");
405 enable = xmlGetProp (node, "enable");
409 enable = strdup (enable);
410 e = strtok (enable, ", ");
413 s->enable = g_list_append (s->enable, strdup (e));
414 e = strtok (0, ", ");
419 sanity_check_parameter (filename, node->name, s);
425 /* Rudimentary check to make sure someone hasn't typed "arg-set="
426 when they should have typed "arg=", etc.
429 sanity_check_parameter (const char *filename, const char *node_name,
447 memset (&allowed, 0, sizeof (allowed));
448 memset (&require, 0, sizeof (require));
459 allowed.string = TRUE;
466 allowed.label = TRUE;
467 require.label = TRUE;
474 allowed.label = TRUE;
481 allowed.label = TRUE;
482 allowed.low_label = TRUE;
483 allowed.high_label = TRUE;
487 /* require.low = TRUE; -- may be 0 */
489 /* require.high = TRUE; -- may be 0 */
490 allowed.value = TRUE;
491 /* require.value = TRUE; -- may be 0 */
492 allowed.invert_p = TRUE;
497 allowed.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;
512 allowed.arg_set = TRUE;
513 allowed.arg_unset = TRUE;
521 allowed.label = TRUE;
522 require.label = TRUE;
523 allowed.arg_set = TRUE;
531 fprintf (stderr, "%s: %s: " STR " in <%s%s id=\"%s\">\n", \
532 blurb(), filename, node_name, \
533 (!strcmp(node_name, "number") \
534 ? (p->type == SPINBUTTON ? " type=spinbutton" : " type=slider")\
536 (p->id ? p->id : ""))
537 # define CHECK(SLOT,NAME) \
538 if (p->SLOT && !allowed.SLOT) \
539 WARN ("\"" NAME "\" is not a valid option"); \
540 if (!p->SLOT && require.SLOT) \
541 WARN ("\"" NAME "\" is required")
544 CHECK (label, "_label");
545 CHECK (string, "(body text)");
546 CHECK (low_label, "_low-label");
547 CHECK (high_label, "_high-label");
549 CHECK (high, "high");
550 CHECK (value, "default");
552 CHECK (invert_p, "convert");
553 CHECK (arg_set, "arg-set");
554 CHECK (arg_unset, "arg-unset");
561 /* Helper for make_parameters()
564 make_parameters_1 (const char *filename, xmlNodePtr node,
565 GtkWidget *parent, int *row)
569 for (; node; node = node->next)
571 const char *name = node->name;
572 if (!strcmp (name, "hgroup") ||
573 !strcmp (name, "vgroup"))
575 GtkWidget *box = (*name == 'h'
576 ? gtk_hbox_new (FALSE, 0)
577 : gtk_vbox_new (FALSE, 0));
579 gtk_widget_show (box);
582 gtk_table_attach (GTK_TABLE (parent), box, 0, 3, *row, *row + 1,
585 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, FALSE, 0);
590 list2 = make_parameters_1 (filename, node->xmlChildrenNode, box, 0);
592 list = g_list_concat (list, list2);
596 parameter *p = make_parameter (filename, node);
599 list = g_list_append (list, p);
600 make_parameter_widget (filename, p, parent, row);
608 /* Calls make_parameter() and make_parameter_widget() on each relevant
609 tag in the XML tree. Also handles the "hgroup" and "vgroup" flags.
610 Returns a GList of `parameter' objects.
613 make_parameters (const char *filename, xmlNodePtr node, GtkWidget *parent)
616 for (; node; node = node->next)
618 if (node->type == XML_ELEMENT_NODE &&
619 !strcmp (node->name, "screensaver"))
620 return make_parameters_1 (filename, node->xmlChildrenNode, parent, &row);
627 invert_range (gfloat low, gfloat high, gfloat value)
629 gfloat range = high-low;
630 gfloat off = value-low;
631 return (low + (range - off));
635 static GtkAdjustment *
636 make_adjustment (const char *filename, parameter *p)
638 float range = (p->high - p->low);
639 float value = (p->invert_p
640 ? invert_range (p->low, p->high, p->value)
642 gfloat si = (p->high - p->low) / 100;
643 gfloat pi = (p->high - p->low) / 10;
645 if (p->value < p->low || p->value > p->high)
647 if (debug_p && p->integer_p)
648 fprintf (stderr, "%s: WARNING: %s: %d is not in range [%d, %d]\n",
650 (int) p->value, (int) p->low, (int) p->high);
653 "%s: WARNING: %s: %.2f is not in range [%.2f, %.2f]\n",
654 blurb(), filename, p->value, p->low, p->high);
655 value = (value < p->low ? p->low : p->high);
658 else if (debug_p && p->value < 1000 && p->high >= 10000)
662 "%s: WARNING: %s: %d is suspicious for range [%d, %d]\n",
664 (int) p->value, (int) p->low, (int) p->high);
667 "%s: WARNING: %s: %.2f is suspicious for range [%.2f, %.2f]\n",
668 blurb(), filename, p->value, p->low, p->high);
674 si = (int) (si + 0.5);
675 pi = (int) (pi + 0.5);
679 if (range <= 500) si = 1;
681 return GTK_ADJUSTMENT (gtk_adjustment_new (value, p->low, p->high,
687 /* Given a `parameter' struct, allocates an appropriate GtkWidget for it,
688 and stores it in `p->widget'.
689 `row' is used for keeping track of our position during table layout.
690 `parent' must be a GtkTable or a GtkBox.
693 make_parameter_widget (const char *filename,
694 parameter *p, GtkWidget *parent, int *row)
696 const char *label = p->label;
697 if (p->widget) return;
705 GtkWidget *w = gtk_label_new (_(label));
706 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
707 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
710 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
713 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
716 p->widget = gtk_entry_new ();
718 gtk_entry_set_text (GTK_ENTRY (p->widget), p->string);
720 gtk_table_attach (GTK_TABLE (parent), p->widget, 1, 3,
724 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
729 GtkWidget *L = gtk_label_new (label ? _(label) : "");
730 GtkWidget *entry = gtk_entry_new ();
731 GtkWidget *button = gtk_button_new_with_label (_("Browse..."));
732 gtk_widget_show (entry);
733 gtk_widget_show (button);
736 gtk_signal_connect (GTK_OBJECT (button),
737 "clicked", GTK_SIGNAL_FUNC (browse_button_cb),
740 gtk_label_set_justify (GTK_LABEL (L), GTK_JUSTIFY_RIGHT);
741 gtk_misc_set_alignment (GTK_MISC (L), 1.0, 0.5);
745 gtk_entry_set_text (GTK_ENTRY (entry), p->string);
749 gtk_table_attach (GTK_TABLE (parent), L, 0, 1,
752 gtk_table_attach (GTK_TABLE (parent), entry, 1, 2,
754 GTK_EXPAND | GTK_FILL, 0, 0, 0);
755 gtk_table_attach (GTK_TABLE (parent), button, 2, 3,
761 gtk_box_pack_start (GTK_BOX (parent), L, FALSE, FALSE, 4);
762 gtk_box_pack_start (GTK_BOX (parent), entry, TRUE, TRUE, 4);
763 gtk_box_pack_start (GTK_BOX (parent), button, FALSE, FALSE, 4);
769 GtkAdjustment *adj = make_adjustment (filename, p);
770 GtkWidget *scale = gtk_hscale_new (adj);
771 GtkWidget *labelw = 0;
775 labelw = gtk_label_new (_(label));
776 gtk_label_set_justify (GTK_LABEL (labelw), GTK_JUSTIFY_LEFT);
777 gtk_misc_set_alignment (GTK_MISC (labelw), 0.0, 0.5);
778 gtk_widget_show (labelw);
781 if (GTK_IS_VBOX (parent))
783 /* If we're inside a vbox, we need to put an hbox in it, to get
784 the low/high labels to be to the left/right of the slider.
786 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
788 /* But if we have a label, put that above the slider's hbox. */
791 gtk_box_pack_start (GTK_BOX (parent), labelw, FALSE, TRUE, 2);
795 gtk_box_pack_start (GTK_BOX (parent), hbox, TRUE, TRUE, 6);
796 gtk_widget_show (hbox);
804 gtk_table_attach (GTK_TABLE (parent), labelw,
805 0, 3, *row, *row + 1,
806 GTK_EXPAND | GTK_FILL, 0, 0, 0);
811 if (GTK_IS_HBOX (parent))
813 GtkWidget *box = gtk_vbox_new (FALSE, 0);
814 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, TRUE, 0);
815 gtk_widget_show (box);
816 gtk_box_pack_start (GTK_BOX (box), labelw, FALSE, TRUE, 4);
818 box = gtk_hbox_new (FALSE, 0);
819 gtk_widget_show (box);
820 gtk_box_pack_start (GTK_BOX (parent), box, TRUE, TRUE, 0);
824 gtk_box_pack_start (GTK_BOX (parent), labelw,
831 GtkWidget *w = gtk_label_new (_(p->low_label));
832 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
833 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
836 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
839 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
842 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM);
843 gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
844 gtk_scale_set_digits (GTK_SCALE (scale), (p->integer_p ? 0 : 2));
846 gtk_table_attach (GTK_TABLE (parent), scale, 1, 2,
848 GTK_EXPAND | GTK_FILL, 0, 0, 0);
850 gtk_box_pack_start (GTK_BOX (parent), scale, TRUE, TRUE, 4);
852 gtk_widget_show (scale);
856 GtkWidget *w = gtk_label_new (_(p->high_label));
857 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
858 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
861 gtk_table_attach (GTK_TABLE (parent), w, 2, 3, *row, *row + 1,
864 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
872 GtkAdjustment *adj = make_adjustment (filename, p);
873 GtkWidget *spin = gtk_spin_button_new (adj, 15, 0);
874 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
875 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (spin), TRUE);
876 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), adj->value);
880 GtkWidget *w = gtk_label_new (_(label));
881 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
882 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
885 gtk_table_attach (GTK_TABLE (parent), w, 0, 1, *row, *row + 1,
888 gtk_box_pack_start (GTK_BOX (parent), w, TRUE, TRUE, 4);
891 gtk_widget_show (spin);
894 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
895 gtk_widget_show (hbox);
896 gtk_table_attach (GTK_TABLE (parent), hbox, 1, 3,
898 GTK_EXPAND | GTK_FILL, 0, 8, 0);
899 gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0);
902 gtk_box_pack_start (GTK_BOX (parent), spin, FALSE, FALSE, 4);
909 p->widget = gtk_check_button_new_with_label (label);
911 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
913 GTK_EXPAND | GTK_FILL, 0, 0, 0);
915 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
920 GtkWidget *opt = gtk_option_menu_new ();
921 GtkWidget *menu = gtk_menu_new ();
926 GtkWidget *w = gtk_label_new (_(label));
927 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
928 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
930 gtk_table_attach (GTK_TABLE (parent), w, 0, 3, *row, *row + 1,
931 GTK_EXPAND | GTK_FILL, 0, 0, 0);
935 for (opts = p->options; opts; opts = opts->next)
937 parameter *s = (parameter *) opts->data;
938 GtkWidget *i = gtk_menu_item_new_with_label (s->label);
940 gtk_menu_append (GTK_MENU (menu), i);
943 gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
946 gtk_table_attach (GTK_TABLE (parent), p->widget, 0, 3,
948 GTK_EXPAND | GTK_FILL, 0, 0, 0);
950 gtk_box_pack_start (GTK_BOX (parent), p->widget, TRUE, TRUE, 4);
965 gtk_widget_set_name (p->widget, p->id);
966 gtk_widget_show (p->widget);
974 Absurdly, there is no GTK file entry widget, only a GNOME one,
975 so in order to avoid depending on GNOME in this code, we have
979 /* cancel button on GtkFileSelection: user_data unused */
981 file_sel_cancel (GtkWidget *button, gpointer user_data)
983 GtkWidget *dialog = button;
984 while (dialog->parent)
985 dialog = dialog->parent;
986 gtk_widget_destroy (dialog);
989 /* ok button on GtkFileSelection: user_data is the corresponding GtkEntry */
991 file_sel_ok (GtkWidget *button, gpointer user_data)
993 GtkWidget *entry = GTK_WIDGET (user_data);
994 GtkWidget *dialog = button;
996 while (dialog->parent)
997 dialog = dialog->parent;
998 gtk_widget_hide (dialog);
1000 path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog));
1001 /* apparently one doesn't free `path' */
1003 gtk_entry_set_text (GTK_ENTRY (entry), path);
1004 gtk_entry_set_position (GTK_ENTRY (entry), strlen (path));
1006 gtk_widget_destroy (dialog);
1009 /* WM close on GtkFileSelection: user_data unused */
1011 file_sel_close (GtkWidget *widget, GdkEvent *event, gpointer user_data)
1013 file_sel_cancel (widget, user_data);
1016 /* "Browse" button: user_data is the corresponding GtkEntry */
1018 browse_button_cb (GtkButton *button, gpointer user_data)
1020 GtkWidget *entry = GTK_WIDGET (user_data);
1021 const char *text = gtk_entry_get_text (GTK_ENTRY (entry));
1022 GtkFileSelection *selector =
1023 GTK_FILE_SELECTION (gtk_file_selection_new (_("Select file.")));
1025 gtk_file_selection_set_filename (selector, text);
1026 gtk_signal_connect (GTK_OBJECT (selector->ok_button),
1027 "clicked", GTK_SIGNAL_FUNC (file_sel_ok),
1029 gtk_signal_connect (GTK_OBJECT (selector->cancel_button),
1030 "clicked", GTK_SIGNAL_FUNC (file_sel_cancel),
1032 gtk_signal_connect (GTK_OBJECT (selector), "delete_event",
1033 GTK_SIGNAL_FUNC (file_sel_close),
1036 gtk_window_set_modal (GTK_WINDOW (selector), TRUE);
1037 gtk_widget_show (GTK_WIDGET (selector));
1041 /* Converting to and from command-lines
1045 /* Returns a copy of string that has been quoted according to shell rules:
1046 it may have been wrapped in "" and had some characters backslashed; or
1047 it may be unchanged.
1050 shell_quotify (const char *string)
1052 char *string2 = (char *) malloc ((strlen (string) * 2) + 10);
1055 int need_quotes = 0;
1060 for (in = string; *in; in++)
1071 else if (*in <= ' ' ||
1097 return strdup (string);
1100 /* Modify the string in place to remove wrapping double-quotes
1101 and interior backslashes.
1104 de_stringify (char *s)
1107 if (q != '\'' && q != '\"' && q != '`')
1109 memmove (s, s+1, strlen (s)+1);
1110 while (*s && *s != q)
1113 memmove (s, s+1, strlen (s)+1);
1116 if (*s != q) abort();
1121 /* Substitutes a shell-quotified version of `value' into `p->arg' at
1122 the place where the `%' character appeared.
1125 format_switch (parameter *p, const char *value)
1130 if (!fmt || !value) return 0;
1131 v2 = shell_quotify (value);
1132 result = (char *) malloc (strlen (fmt) + strlen (v2) + 10);
1149 /* Maps a `parameter' to a command-line switch.
1150 Returns 0 if it can't, or if the parameter has the default value.
1153 parameter_to_switch (parameter *p)
1159 return strdup (p->arg);
1165 if (!p->widget) return 0;
1167 const char *s = gtk_entry_get_text (GTK_ENTRY (p->widget));
1169 if (!strcmp ((s ? s : ""),
1170 (p->string ? p->string : "")))
1171 v = 0; /* same as default */
1173 v = format_switch (p, s);
1175 /* don't free `s' */
1180 if (!p->widget) return 0;
1182 GtkAdjustment *adj =
1184 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1185 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1188 float value = (p->invert_p
1189 ? invert_range (adj->lower, adj->upper, adj->value)
1192 if (value == p->value) /* same as default */
1196 sprintf (buf, "%d", (int) (value + 0.5));
1198 sprintf (buf, "%.4f", value);
1200 s1 = strchr (buf, '.');
1203 char *s2 = s1 + strlen(s1) - 1;
1204 while (s2 > s1 && *s2 == '0') /* lose trailing zeroes */
1206 if (s2 >= s1 && *s2 == '.') /* lose trailing decimal */
1209 return format_switch (p, buf);
1212 if (!p->widget) return 0;
1214 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1215 const char *s = (gtk_toggle_button_get_active (b)
1224 if (!p->widget) return 0;
1226 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1227 GtkMenu *menu = GTK_MENU (gtk_option_menu_get_menu (opt));
1228 GtkWidget *selected = gtk_menu_get_active (menu);
1229 GList *kids = gtk_container_children (GTK_CONTAINER (menu));
1230 int menu_elt = g_list_index (kids, (gpointer) selected);
1231 GList *ol = g_list_nth (p->options, menu_elt);
1232 parameter *o = (ol ? (parameter *) ol->data : 0);
1235 if (o->type != SELECT_OPTION) abort();
1250 /* Maps a GList of `parameter' objects to a complete command-line string.
1251 All arguments will be properly quoted.
1254 parameters_to_cmd_line (GList *parms)
1256 int L = g_list_length (parms);
1258 char **strs = (char **) calloc (sizeof (*parms), L);
1263 for (i = 0; parms; parms = parms->next, i++)
1265 char *s = parameter_to_switch ((parameter *) parms->data);
1267 LL += (s ? strlen(s) : 0) + 1;
1270 result = (char *) malloc (LL + 10);
1272 for (i = 0; i < L; i++)
1275 strcpy (out, strs[i]);
1276 out += strlen (out);
1281 while (out > result && out[-1] == ' ') /* strip trailing spaces */
1289 /* Returns a GList of the tokens the string, using shell syntax;
1290 Quoted strings are handled as a single token.
1293 tokenize_command_line (const char *cmd)
1296 const char *s = cmd;
1301 for (; isspace(*s); s++); /* skip whitespace */
1304 if (*s == '\'' || *s == '\"' || *s == '`')
1308 while (*s && *s != q) /* skip to matching quote */
1310 if (*s == '\\' && s[1]) /* allowing backslash quoting */
1328 ss = (char *) malloc ((s - start) + 1);
1329 strncpy (ss, start, s-start);
1331 if (*ss == '\'' || *ss == '\"' || *ss == '`')
1333 result = g_list_append (result, ss);
1340 static void parameter_set_switch (parameter *, gpointer value);
1341 static gboolean parse_command_line_into_parameters_1 (const char *filename,
1348 /* Parses the command line, and flushes those options down into
1349 the `parameter' structs in the list.
1352 parse_command_line_into_parameters (const char *filename,
1353 const char *cmd, GList *parms)
1355 GList *tokens = tokenize_command_line (cmd);
1357 for (rest = tokens; rest; rest = rest->next)
1359 char *option = rest->data;
1362 if (option[0] != '-')
1365 fprintf (stderr, "%s: WARNING: %s: not a switch: \"%s\"\n",
1366 blurb(), filename, option);
1372 if (rest->next) /* pop off the arg to this option */
1374 char *s = (char *) rest->next->data;
1375 /* the next token is the next switch iff it matches "-[a-z]".
1376 (To avoid losing on "-x -3.1".)
1378 if (s && (s[0] != '-' || !isalpha(s[1])))
1381 rest->next->data = 0;
1386 parse_command_line_into_parameters_1 (filename, parms,
1388 if (value) free (value);
1392 g_list_free (tokens);
1397 compare_opts (const char *option, const char *value,
1398 const char *template)
1400 int ol = strlen (option);
1403 if (strncmp (option, template, ol))
1406 if (template[ol] != (value ? ' ' : 0))
1409 /* At this point, we have a match against "option".
1410 If template contains a %, we're done.
1411 Else, compare against "value" too.
1413 c = strchr (template, '%');
1418 return (template[ol] == 0);
1419 if (strcmp (template + ol + 1, value))
1427 parse_command_line_into_parameters_1 (const char *filename,
1434 parameter *match = 0;
1438 for (p = parms; p; p = p->next)
1440 parameter *pp = (parameter *) p->data;
1443 if (pp->type == SELECT)
1445 if (parse_command_line_into_parameters_1 (filename,
1456 if (compare_opts (option, value, pp->arg))
1462 else if (pp->arg_set)
1464 if (compare_opts (option, value, pp->arg_set))
1470 else if (pp->arg_unset)
1472 if (compare_opts (option, value, pp->arg_unset))
1487 if (debug_p && !parent)
1488 fprintf (stderr, "%s: WARNING: %s: no match for %s %s\n",
1489 blurb(), filename, option, (value ? value : ""));
1493 switch (match->type)
1499 if (which != -1) abort();
1500 parameter_set_switch (match, (gpointer) value);
1503 if (which != 0 && which != 1) abort();
1504 parameter_set_switch (match, (gpointer) which);
1507 if (which != 1) abort();
1508 parameter_set_switch (parent, (gpointer) index);
1517 /* Set the parameter's value.
1518 For STRING, FILENAME, SLIDER, and SPINBUTTON, `value' is a char*.
1519 For BOOLEAN and SELECT, `value' is an int.
1522 parameter_set_switch (parameter *p, gpointer value)
1524 if (p->type == SELECT_OPTION) abort();
1525 if (!p->widget) return;
1531 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) value);
1537 GtkAdjustment *adj =
1539 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1540 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1544 if (1 == sscanf ((char *) value, "%f %c", &f, &c))
1547 f = invert_range (adj->lower, adj->upper, f);
1548 gtk_adjustment_set_value (adj, f);
1554 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1555 gtk_toggle_button_set_active (b, (int) value);
1560 gtk_option_menu_set_history (GTK_OPTION_MENU (p->widget),
1571 restore_defaults (const char *progname, GList *parms)
1573 for (; parms; parms = parms->next)
1575 parameter *p = (parameter *) parms->data;
1576 if (!p->widget) continue;
1582 gtk_entry_set_text (GTK_ENTRY (p->widget),
1583 (p->string ? p->string : ""));
1589 GtkAdjustment *adj =
1591 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1592 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1593 float value = (p->invert_p
1594 ? invert_range (p->low, p->high, p->value)
1596 gtk_adjustment_set_value (adj, value);
1601 /* A toggle button should be on by default if it inserts
1602 nothing into the command line when on. E.g., it should
1603 be on if `arg_set' is null.
1605 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->widget),
1606 (!p->arg_set || !*p->arg_set));
1611 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1616 for (opts = p->options, index = 0; opts;
1617 opts = opts->next, index++)
1619 parameter *s = (parameter *) opts->data;
1620 /* The default menu item is the first one with
1621 no `arg_set' field. */
1629 gtk_option_menu_set_history (GTK_OPTION_MENU (opt), selected);
1640 /* Documentation strings
1644 get_description (GList *parms)
1647 for (; parms; parms = parms->next)
1649 parameter *p = (parameter *) parms->data;
1650 if (p->type == DESCRIPTION)
1657 if (!doc || !doc->string)
1661 char *d = strdup (doc->string);
1663 for (s = d; *s; s++)
1666 if (s[1] == '\n') /* blank line: leave it */
1668 else if (s[1] == ' ' || s[1] == '\t')
1669 s++; /* next line is indented: leave newline */
1671 s[0] = ' '; /* delete newline to un-fold this line */
1674 /* strip off leading whitespace on first line only */
1675 for (s = d; *s && (*s == ' ' || *s == '\t'); s++)
1677 while (*s == '\n') /* strip leading newlines */
1680 memmove (d, s, strlen(s)+1);
1682 /* strip off trailing whitespace and newlines */
1685 while (L && isspace(d[L-1]))
1694 /* External interface.
1698 load_configurator_1 (const char *program, const char *arguments,
1701 const char *dir = hack_configuration_path;
1702 int L = strlen (dir);
1708 if (L == 0) return 0;
1710 file = (char *) malloc (L + strlen (program) + 10);
1711 data = (conf_data *) calloc (1, sizeof(*data));
1714 if (file[L-1] != '/')
1716 strcpy (file+L, program);
1718 for (s = file+L; *s; s++)
1719 if (*s == '/' || *s == ' ')
1721 else if (isupper (*s))
1724 strcat (file+L, ".xml");
1726 f = fopen (file, "r");
1729 int res, size = 1024;
1731 xmlParserCtxtPtr ctxt;
1737 fprintf (stderr, "%s: reading %s...\n", blurb(), file);
1739 res = fread (chars, 1, 4, f);
1747 ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, file);
1748 while ((res = fread(chars, 1, size, f)) > 0)
1749 xmlParseChunk (ctxt, chars, res, 0);
1750 xmlParseChunk (ctxt, chars, 0, 1);
1752 xmlFreeParserCtxt (ctxt);
1755 /* Parsed the XML file. Now make some widgets. */
1757 table = gtk_table_new (1, 3, FALSE);
1758 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
1759 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
1760 gtk_container_set_border_width (GTK_CONTAINER (table), 8);
1761 gtk_widget_show (table);
1763 parms = make_parameters (file, doc->xmlRootNode, table);
1767 restore_defaults (program, parms);
1768 if (arguments && *arguments)
1769 parse_command_line_into_parameters (program, arguments, parms);
1771 data->widget = table;
1772 data->parameters = parms;
1773 data->description = get_description (parms);
1780 fprintf (stderr, "%s: %s does not exist.\n", blurb(), file);
1782 p = calloc (1, sizeof(*p));
1784 p->arg = strdup (arguments);
1786 data->parameters = g_list_append (0, (gpointer) p);
1789 data->progname = strdup (program);
1797 split_command_line (const char *full_command_line,
1798 char **prog_ret, char **args_ret)
1800 char *line = strdup (full_command_line);
1813 while (isspace (*s)) s++;
1816 else if (*s == '=') /* if the leading word contains an "=", skip it. */
1818 while (*s && !isspace (*s)) s++;
1819 while (isspace (*s)) s++;
1826 *prog_ret = strdup (prog);
1827 *args_ret = strdup (args);
1833 load_configurator (const char *full_command_line, gboolean verbose_p)
1838 split_command_line (full_command_line, &prog, &args);
1839 cd = load_configurator_1 (prog, args, verbose_p);
1848 get_configurator_command_line (conf_data *data)
1850 char *args = parameters_to_cmd_line (data->parameters);
1851 char *result = (char *) malloc (strlen (data->progname) +
1853 strcpy (result, data->progname);
1854 strcat (result, " ");
1855 strcat (result, args);
1862 set_configurator_command_line (conf_data *data, const char *full_command_line)
1866 split_command_line (full_command_line, &prog, &args);
1867 if (data->progname) free (data->progname);
1868 data->progname = prog;
1869 restore_defaults (prog, data->parameters);
1870 parse_command_line_into_parameters (prog, args, data->parameters);
1875 free_conf_data (conf_data *data)
1877 if (data->parameters)
1880 for (rest = data->parameters; rest; rest = rest->next)
1882 free_parameter ((parameter *) rest->data);
1885 g_list_free (data->parameters);
1886 data->parameters = 0;
1890 gtk_widget_destroy (data->widget);
1893 free (data->progname);
1894 if (data->description)
1895 free (data->description);
1897 memset (data, ~0, sizeof(*data));
1902 #endif /* HAVE_GTK && HAVE_XML -- whole file */