1 /* demo-Gtk-conf.c --- implements the dynamic configuration dialogs.
2 * xscreensaver, Copyright (c) 2001-2008 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;
75 #define MIN_SLIDER_WIDTH 150
76 #define MIN_SPINBUTTON_WIDTH 48
77 #define MIN_LABEL_WIDTH 70
99 xmlChar *id; /* widget name */
100 xmlChar *label; /* heading label, or null */
102 /* command, fake, description, fakepreview, string, file
104 xmlChar *string; /* file name, description, whatever. */
106 /* slider, spinbutton
108 xmlChar *low_label; /* label for the left side */
109 xmlChar *high_label; /* label for the right side */
110 float low; /* minimum value */
111 float high; /* maximum value */
112 float value; /* default value */
113 gboolean integer_p; /* whether the range is integral, or real */
114 xmlChar *arg; /* command-line option to set (substitute "%") */
115 gboolean invert_p; /* whether to flip the value and pretend the
116 range goes from hi-low instead of low-hi. */
118 /* boolean, select-option
120 xmlChar *arg_set; /* command-line option to set for "yes", or null */
121 xmlChar *arg_unset; /* command-line option to set for "no", or null */
134 static parameter *make_select_option (const char *file, xmlNodePtr);
135 static void make_parameter_widget (const char *filename,
136 parameter *, GtkWidget *);
137 static void browse_button_cb (GtkButton *button, gpointer user_data);
140 /* Frees the parameter object and all strings and sub-parameters.
141 Does not destroy the widget, if any.
144 free_parameter (parameter *p)
147 if (p->id) free (p->id);
148 if (p->label) free (p->label);
149 if (p->string) free (p->string);
150 if (p->low_label) free (p->low_label);
151 if (p->high_label) free (p->high_label);
152 if (p->arg) free (p->arg);
153 if (p->arg_set) free (p->arg_set);
154 if (p->arg_unset) free (p->arg_unset);
156 for (rest = p->options; rest; rest = rest->next)
158 free_parameter ((parameter *) rest->data);
160 memset (p, ~0, sizeof(*p));
165 /* Debugging: dumps out a `parameter' structure.
169 describe_parameter (FILE *out, parameter *p)
174 case COMMAND: fprintf (out, "command"); break;
175 case FAKE: fprintf (out, "fake"); break;
176 case DESCRIPTION: fprintf (out, "_description"); break;
177 case FAKEPREVIEW: fprintf (out, "fakepreview"); break;
178 case STRING: fprintf (out, "string"); break;
179 case FILENAME: fprintf (out, "filename"); break;
180 case SLIDER: fprintf (out, "number type=\"slider\""); break;
181 case SPINBUTTON: fprintf (out, "number type=\"spinbutton\""); break;
182 case BOOLEAN: fprintf (out, "boolean"); break;
183 case SELECT: fprintf (out, "select"); break;
184 default: abort(); break;
186 if (p->id) fprintf (out, " id=\"%s\"", p->id);
187 if (p->label) fprintf (out, " _label=\"%s\"", p->label);
188 if (p->string && p->type != DESCRIPTION)
189 fprintf (out, " string=\"%s\"", p->string);
190 if (p->low_label) fprintf (out, " _low-label=\"%s\"", p->low_label);
191 if (p->high_label) fprintf (out, " _high-label=\"%s\"", p->high_label);
192 if (p->low) fprintf (out, " low=\"%.2f\"", p->low);
193 if (p->high) fprintf (out, " high=\"%.2f\"", p->high);
194 if (p->value) fprintf (out, " default=\"%.2f\"", p->value);
195 if (p->arg) fprintf (out, " arg=\"%s\"", p->arg);
196 if (p->invert_p) fprintf (out, " convert=\"invert\"");
197 if (p->arg_set) fprintf (out, " arg-set=\"%s\"", p->arg_set);
198 if (p->arg_unset) fprintf (out, " arg-unset=\"%s\"", p->arg_unset);
199 fprintf (out, ">\n");
201 if (p->type == SELECT)
204 for (opt = p->options; opt; opt = opt->next)
206 parameter *o = (parameter *) opt->data;
207 if (o->type != SELECT_OPTION) abort();
208 fprintf (out, " <option");
209 if (o->id) fprintf (out, " id=\"%s\"", o->id);
210 if (o->label) fprintf (out, " _label=\"%s\"", o->label);
211 if (o->arg_set) fprintf (out, " arg-set=\"%s\"", o->arg_set);
212 if (o->arg_unset) fprintf (out, " arg-unset=\"%s\"", o->arg_unset);
213 fprintf (out, ">\n");
215 fprintf (out, "</select>\n");
217 else if (p->type == DESCRIPTION)
220 fprintf (out, " %s\n", p->string);
221 fprintf (out, "</_description>\n");
227 /* Like xmlGetProp() but parses a float out of the string.
228 If the number was expressed as a float and not an integer
229 (that is, the string contained a decimal point) then
230 `floatp' is set to TRUE. Otherwise, it is unchanged.
233 xml_get_float (xmlNodePtr node, const xmlChar *name, gboolean *floatpP)
235 const char *s = (char *) xmlGetProp (node, name);
238 if (!s || 1 != sscanf (s, "%f %c", &f, &c))
242 if (strchr (s, '.')) *floatpP = TRUE;
248 static void sanity_check_parameter (const char *filename,
249 const xmlChar *node_name,
251 static void sanity_check_text_node (const char *filename,
252 const xmlNodePtr node);
253 static void sanity_check_menu_options (const char *filename,
254 const xmlChar *node_name,
257 /* Allocates and returns a new `parameter' object based on the
258 properties in the given XML node. Returns 0 if there's nothing
259 to create (comment, or unknown tag.)
262 make_parameter (const char *filename, xmlNodePtr node)
265 const char *name = (char *) node->name;
267 gboolean floatp = FALSE;
269 if (node->type == XML_COMMENT_NODE)
272 p = calloc (1, sizeof(*p));
275 else if (!strcmp (name, "command")) p->type = COMMAND;
276 else if (!strcmp (name, "fullcommand")) p->type = COMMAND;
277 else if (!strcmp (name, "_description")) p->type = DESCRIPTION;
278 else if (!strcmp (name, "fakepreview")) p->type = FAKEPREVIEW;
279 else if (!strcmp (name, "fake")) p->type = FAKE;
280 else if (!strcmp (name, "boolean")) p->type = BOOLEAN;
281 else if (!strcmp (name, "string")) p->type = STRING;
282 else if (!strcmp (name, "file")) p->type = FILENAME;
283 else if (!strcmp (name, "number")) p->type = SPINBUTTON;
284 else if (!strcmp (name, "select")) p->type = SELECT;
286 else if (!strcmp (name, "xscreensaver-text") || /* these are ignored in X11 */
287 !strcmp (name, "xscreensaver-image")) /* (they are used in Cocoa) */
292 else if (node->type == XML_TEXT_NODE)
294 sanity_check_text_node (filename, node);
301 fprintf (stderr, "%s: WARNING: %s: unknown tag: \"%s\"\n",
302 blurb(), filename, name);
307 if (p->type == SPINBUTTON)
309 const char *type = (char *) xmlGetProp (node, (xmlChar *) "type");
310 if (!type || !strcmp (type, "spinbutton")) p->type = SPINBUTTON;
311 else if (!strcmp (type, "slider")) p->type = SLIDER;
315 fprintf (stderr, "%s: WARNING: %s: unknown %s type: \"%s\"\n",
316 blurb(), filename, name, type);
321 else if (p->type == DESCRIPTION)
323 if (node->xmlChildrenNode &&
324 node->xmlChildrenNode->type == XML_TEXT_NODE &&
325 !node->xmlChildrenNode->next)
326 p->string = (xmlChar *)
327 strdup ((char *) node->xmlChildrenNode->content);
330 p->id = xmlGetProp (node, (xmlChar *) "id");
331 p->label = xmlGetProp (node, (xmlChar *) "_label");
332 p->low_label = xmlGetProp (node, (xmlChar *) "_low-label");
333 p->high_label = xmlGetProp (node, (xmlChar *) "_high-label");
334 p->low = xml_get_float (node, (xmlChar *) "low", &floatp);
335 p->high = xml_get_float (node, (xmlChar *) "high", &floatp);
336 p->value = xml_get_float (node, (xmlChar *) "default", &floatp);
337 p->integer_p = !floatp;
338 convert = (char *) xmlGetProp (node, (xmlChar *) "convert");
339 p->invert_p = (convert && !strcmp (convert, "invert"));
340 p->arg = xmlGetProp (node, (xmlChar *) "arg");
341 p->arg_set = xmlGetProp (node, (xmlChar *) "arg-set");
342 p->arg_unset = xmlGetProp (node, (xmlChar *) "arg-unset");
344 /* Check for missing decimal point */
347 (p->high != p->low) &&
348 (p->high - p->low) <= 1)
350 "%s: WARNING: %s: %s: range [%.1f, %.1f] shouldn't be integral!\n",
351 blurb(), filename, p->id,
354 if (p->type == SELECT)
357 for (kids = node->xmlChildrenNode; kids; kids = kids->next)
359 parameter *s = make_select_option (filename, kids);
361 p->options = g_list_append (p->options, s);
365 sanity_check_parameter (filename, (const xmlChar *) name, p);
371 /* Allocates and returns a new SELECT_OPTION `parameter' object based
372 on the properties in the given XML node. Returns 0 if there's nothing
373 to create (comment, or unknown tag.)
376 make_select_option (const char *filename, xmlNodePtr node)
378 if (node->type == XML_COMMENT_NODE)
380 else if (node->type == XML_TEXT_NODE)
382 sanity_check_text_node (filename, node);
385 else if (node->type != XML_ELEMENT_NODE)
389 "%s: WARNING: %s: %s: unexpected child tag type %d\n",
390 blurb(), filename, node->name, (int)node->type);
393 else if (strcmp ((char *) node->name, "option"))
397 "%s: WARNING: %s: %s: child not an option tag: \"%s\"\n",
398 blurb(), filename, node->name, node->name);
403 parameter *s = calloc (1, sizeof(*s));
405 s->type = SELECT_OPTION;
406 s->id = xmlGetProp (node, (xmlChar *) "id");
407 s->label = xmlGetProp (node, (xmlChar *) "_label");
408 s->arg_set = xmlGetProp (node, (xmlChar *) "arg-set");
409 s->arg_unset = xmlGetProp (node, (xmlChar *) "arg-unset");
411 sanity_check_parameter (filename, node->name, s);
417 /* Rudimentary check to make sure someone hasn't typed "arg-set="
418 when they should have typed "arg=", etc.
421 sanity_check_parameter (const char *filename, const xmlChar *node_name,
439 memset (&allowed, 0, sizeof (allowed));
440 memset (&require, 0, sizeof (require));
451 allowed.string = TRUE;
458 allowed.label = TRUE;
459 require.label = TRUE;
466 allowed.label = TRUE;
473 allowed.label = TRUE;
474 allowed.low_label = TRUE;
475 allowed.high_label = TRUE;
479 /* require.low = TRUE; -- may be 0 */
481 /* require.high = TRUE; -- may be 0 */
482 allowed.value = TRUE;
483 /* require.value = TRUE; -- may be 0 */
484 allowed.invert_p = TRUE;
489 allowed.label = TRUE;
493 /* require.low = TRUE; -- may be 0 */
495 /* require.high = TRUE; -- may be 0 */
496 allowed.value = TRUE;
497 /* require.value = TRUE; -- may be 0 */
498 allowed.invert_p = TRUE;
503 allowed.label = TRUE;
504 allowed.arg_set = TRUE;
505 allowed.arg_unset = TRUE;
513 allowed.label = TRUE;
514 require.label = TRUE;
515 allowed.arg_set = TRUE;
523 fprintf (stderr, "%s: %s: " STR " in <%s%s id=\"%s\">\n", \
524 blurb(), filename, node_name, \
525 (!strcmp((char *) node_name, "number") \
526 ? (p->type == SPINBUTTON ? " type=spinbutton" : " type=slider")\
528 (p->id ? (char *) p->id : ""))
529 # define CHECK(SLOT,NAME) \
530 if (p->SLOT && !allowed.SLOT) \
531 WARN ("\"" NAME "\" is not a valid option"); \
532 if (!p->SLOT && require.SLOT) \
533 WARN ("\"" NAME "\" is required")
536 CHECK (label, "_label");
537 CHECK (string, "(body text)");
538 CHECK (low_label, "_low-label");
539 CHECK (high_label, "_high-label");
541 CHECK (high, "high");
542 CHECK (value, "default");
544 CHECK (invert_p, "convert");
545 CHECK (arg_set, "arg-set");
546 CHECK (arg_unset, "arg-unset");
550 if (p->type == SELECT)
551 sanity_check_menu_options (filename, node_name, p);
556 sanity_check_menu_options (const char *filename, const xmlChar *node_name,
564 /* fprintf (stderr, "\n## %s\n", p->id);*/
565 for (opts = p->options; opts; opts = opts->next)
567 parameter *s = (parameter *) opts->data;
568 if (!s->arg_set) nulls++;
573 char *a = strdup ((char *) s->arg_set);
574 char *spc = strchr (a, ' ');
578 if (strcmp (a, prefix))
580 "%s: %s: both \"%s\" and \"%s\" used in <select id=\"%s\">\n",
581 blurb(), filename, prefix, a, p->id);
587 /* fprintf (stderr, "\n %s\n", s->arg_set);*/
590 if (prefix) free (prefix);
594 "%s: %s: more than one menu with no arg-set in <select id=\"%s\">\n",
595 blurb(), filename, p->id);
599 /* "text" nodes show up for all the non-tag text in the file, including
600 all the newlines between tags. Warn if there is text there that
604 sanity_check_text_node (const char *filename, const xmlNodePtr node)
606 const char *body = (const char *) node->content;
607 if (node->type != XML_TEXT_NODE) abort();
608 while (isspace (*body)) body++;
610 fprintf (stderr, "%s: WARNING: %s: random text present: \"%s\"\n",
611 blurb(), filename, body);
615 /* Returns a list of strings, every switch mentioned in the parameters.
616 The strings must be freed.
619 get_all_switches (const char *filename, GList *parms)
623 for (p = parms; p; p = p->next)
625 parameter *pp = (parameter *) p->data;
627 if (pp->type == SELECT)
629 GList *list2 = get_all_switches (filename, pp->options);
630 switches = g_list_concat (switches, list2);
632 if (pp->arg && *pp->arg)
633 switches = g_list_append (switches, strdup ((char *) pp->arg));
634 if (pp->arg_set && *pp->arg_set)
635 switches = g_list_append (switches, strdup ((char *) pp->arg_set));
636 if (pp->arg_unset && *pp->arg_unset)
637 switches = g_list_append (switches, strdup ((char *) pp->arg_unset));
643 /* Ensures that no switch is mentioned more than once.
646 sanity_check_parameters (const char *filename, GList *parms)
648 GList *list = get_all_switches (filename, parms);
650 for (p = list; p; p = p->next)
652 char *sw = (char *) p->data;
655 if (*sw != '-' && *sw != '+')
656 fprintf (stderr, "%s: %s: switch does not begin with hyphen \"%s\"\n",
657 blurb(), filename, sw);
659 for (p2 = p->next; p2; p2 = p2->next)
661 const char *sw2 = (const char *) p2->data;
662 if (!strcmp (sw, sw2))
663 fprintf (stderr, "%s: %s: duplicate switch \"%s\"\n",
664 blurb(), filename, sw);
673 /* Helper for make_parameters()
676 make_parameters_1 (const char *filename, xmlNodePtr node, GtkWidget *parent)
680 for (; node; node = node->next)
682 const char *name = (char *) node->name;
683 if (!strcmp (name, "hgroup") ||
684 !strcmp (name, "vgroup"))
686 GtkWidget *box = (*name == 'h'
687 ? gtk_hbox_new (FALSE, 0)
688 : gtk_vbox_new (FALSE, 0));
690 gtk_widget_show (box);
691 gtk_box_pack_start (GTK_BOX (parent), box, FALSE, FALSE, 0);
693 list2 = make_parameters_1 (filename, node->xmlChildrenNode, box);
695 list = g_list_concat (list, list2);
699 parameter *p = make_parameter (filename, node);
702 list = g_list_append (list, p);
703 make_parameter_widget (filename, p, parent);
711 /* Calls make_parameter() and make_parameter_widget() on each relevant
712 tag in the XML tree. Also handles the "hgroup" and "vgroup" flags.
713 Returns a GList of `parameter' objects.
716 make_parameters (const char *filename, xmlNodePtr node, GtkWidget *parent)
718 for (; node; node = node->next)
720 if (node->type == XML_ELEMENT_NODE &&
721 !strcmp ((char *) node->name, "screensaver"))
722 return make_parameters_1 (filename, node->xmlChildrenNode, parent);
729 invert_range (gfloat low, gfloat high, gfloat value)
731 gfloat range = high-low;
732 gfloat off = value-low;
733 return (low + (range - off));
737 static GtkAdjustment *
738 make_adjustment (const char *filename, parameter *p)
740 float range = (p->high - p->low);
741 float value = (p->invert_p
742 ? invert_range (p->low, p->high, p->value)
744 gfloat si = (p->high - p->low) / 100;
745 gfloat pi = (p->high - p->low) / 10;
746 gfloat page_size = ((p->type == SLIDER) ? 1 : 0);
748 if (p->value < p->low || p->value > p->high)
750 if (debug_p && p->integer_p)
751 fprintf (stderr, "%s: WARNING: %s: %d is not in range [%d, %d]\n",
753 (int) p->value, (int) p->low, (int) p->high);
756 "%s: WARNING: %s: %.2f is not in range [%.2f, %.2f]\n",
757 blurb(), filename, p->value, p->low, p->high);
758 value = (value < p->low ? p->low : p->high);
761 else if (debug_p && p->value < 1000 && p->high >= 10000)
765 "%s: WARNING: %s: %d is suspicious for range [%d, %d]\n",
767 (int) p->value, (int) p->low, (int) p->high);
770 "%s: WARNING: %s: %.2f is suspicious for range [%.2f, %.2f]\n",
771 blurb(), filename, p->value, p->low, p->high);
775 si = (int) (si + 0.5);
776 pi = (int) (pi + 0.5);
780 if (range <= 500) si = 1;
782 return GTK_ADJUSTMENT (gtk_adjustment_new (value,
791 set_widget_min_width (GtkWidget *w, int width)
794 gtk_widget_size_request (GTK_WIDGET (w), &req);
795 if (req.width < width)
796 gtk_widget_set_size_request (GTK_WIDGET (w), width, -1);
800 /* If we're inside a vbox, we need to put an hbox in it, or labels appear
801 on top instead of to the left, and things stretch to the full width of
805 insert_fake_hbox (GtkWidget *parent)
807 if (GTK_IS_VBOX (parent))
809 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
810 gtk_box_pack_start (GTK_BOX (parent), hbox, FALSE, FALSE, 4);
811 gtk_widget_show (hbox);
818 /* Given a `parameter' struct, allocates an appropriate GtkWidget for it,
819 and stores it in `p->widget'.
820 `parent' must be a GtkBox.
823 make_parameter_widget (const char *filename, parameter *p, GtkWidget *parent)
825 const char *label = (char *) p->label;
826 if (p->widget) return;
832 parent = insert_fake_hbox (parent);
835 GtkWidget *w = gtk_label_new (_(label));
836 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
837 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
838 set_widget_min_width (GTK_WIDGET (w), MIN_LABEL_WIDTH);
840 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
843 p->widget = gtk_entry_new ();
845 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) p->string);
846 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
851 GtkWidget *L = gtk_label_new (label ? _(label) : "");
852 GtkWidget *entry = gtk_entry_new ();
853 GtkWidget *button = gtk_button_new_with_label (_("Browse..."));
854 gtk_widget_show (entry);
855 gtk_widget_show (button);
858 gtk_signal_connect (GTK_OBJECT (button),
859 "clicked", GTK_SIGNAL_FUNC (browse_button_cb),
862 gtk_label_set_justify (GTK_LABEL (L), GTK_JUSTIFY_RIGHT);
863 gtk_misc_set_alignment (GTK_MISC (L), 1.0, 0.5);
864 set_widget_min_width (GTK_WIDGET (L), MIN_LABEL_WIDTH);
868 gtk_entry_set_text (GTK_ENTRY (entry), (char *) p->string);
870 parent = insert_fake_hbox (parent);
871 gtk_box_pack_start (GTK_BOX (parent), L, FALSE, FALSE, 4);
872 gtk_box_pack_start (GTK_BOX (parent), entry, TRUE, TRUE, 4);
873 gtk_box_pack_start (GTK_BOX (parent), button, FALSE, FALSE, 4);
878 GtkAdjustment *adj = make_adjustment (filename, p);
879 GtkWidget *scale = gtk_hscale_new (adj);
880 GtkWidget *labelw = 0;
884 labelw = gtk_label_new (_(label));
885 gtk_label_set_justify (GTK_LABEL (labelw), GTK_JUSTIFY_LEFT);
886 gtk_misc_set_alignment (GTK_MISC (labelw), 0.0, 0.5);
887 set_widget_min_width (GTK_WIDGET (labelw), MIN_LABEL_WIDTH);
888 gtk_widget_show (labelw);
889 gtk_box_pack_start (GTK_BOX (parent), labelw, FALSE, FALSE, 2);
892 /* Do this after 'labelw' so that it appears above, not to left. */
893 parent = insert_fake_hbox (parent);
897 GtkWidget *w = gtk_label_new (_((char *) p->low_label));
898 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
899 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
900 set_widget_min_width (GTK_WIDGET (w), MIN_LABEL_WIDTH);
902 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
905 gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM);
906 gtk_scale_set_draw_value (GTK_SCALE (scale), debug_p);
907 gtk_scale_set_digits (GTK_SCALE (scale), (p->integer_p ? 0 : 2));
908 set_widget_min_width (GTK_WIDGET (scale), MIN_SLIDER_WIDTH);
910 gtk_box_pack_start (GTK_BOX (parent), scale, FALSE, FALSE, 4);
912 gtk_widget_show (scale);
916 GtkWidget *w = gtk_label_new (_((char *) p->high_label));
917 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
918 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
919 set_widget_min_width (GTK_WIDGET (w), MIN_LABEL_WIDTH);
921 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
929 GtkAdjustment *adj = make_adjustment (filename, p);
930 GtkWidget *spin = gtk_spin_button_new (adj, 15, 0);
931 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin), TRUE);
932 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (spin), TRUE);
933 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), adj->value);
934 set_widget_min_width (GTK_WIDGET (spin), MIN_SPINBUTTON_WIDTH);
938 GtkWidget *w = gtk_label_new (_(label));
939 gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_RIGHT);
940 gtk_misc_set_alignment (GTK_MISC (w), 1.0, 0.5);
941 set_widget_min_width (GTK_WIDGET (w), MIN_LABEL_WIDTH);
943 parent = insert_fake_hbox (parent);
944 gtk_box_pack_start (GTK_BOX (parent), w, FALSE, FALSE, 4);
947 gtk_widget_show (spin);
948 gtk_box_pack_start (GTK_BOX (parent), spin, FALSE, FALSE, 4);
955 p->widget = gtk_check_button_new_with_label (_(label));
956 /* Let these stretch -- doesn't hurt.
957 parent = insert_fake_hbox (parent);
959 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
964 GtkWidget *opt = gtk_option_menu_new ();
965 GtkWidget *menu = gtk_menu_new ();
968 for (opts = p->options; opts; opts = opts->next)
970 parameter *s = (parameter *) opts->data;
971 GtkWidget *i = gtk_menu_item_new_with_label (_((char *) s->label));
973 gtk_menu_append (GTK_MENU (menu), i);
976 gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
978 parent = insert_fake_hbox (parent);
979 gtk_box_pack_start (GTK_BOX (parent), p->widget, FALSE, FALSE, 4);
994 gtk_widget_set_name (p->widget, (char *) p->id);
995 gtk_widget_show (p->widget);
1001 Absurdly, there is no GTK file entry widget, only a GNOME one,
1002 so in order to avoid depending on GNOME in this code, we have
1006 /* cancel button on GtkFileSelection: user_data unused */
1008 file_sel_cancel (GtkWidget *button, gpointer user_data)
1010 GtkWidget *dialog = button;
1011 while (dialog->parent)
1012 dialog = dialog->parent;
1013 gtk_widget_destroy (dialog);
1016 /* ok button on GtkFileSelection: user_data is the corresponding GtkEntry */
1018 file_sel_ok (GtkWidget *button, gpointer user_data)
1020 GtkWidget *entry = GTK_WIDGET (user_data);
1021 GtkWidget *dialog = button;
1023 while (dialog->parent)
1024 dialog = dialog->parent;
1025 gtk_widget_hide (dialog);
1027 path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog));
1028 /* apparently one doesn't free `path' */
1030 gtk_entry_set_text (GTK_ENTRY (entry), path);
1031 gtk_entry_set_position (GTK_ENTRY (entry), strlen (path));
1033 gtk_widget_destroy (dialog);
1036 /* WM close on GtkFileSelection: user_data unused */
1038 file_sel_close (GtkWidget *widget, GdkEvent *event, gpointer user_data)
1040 file_sel_cancel (widget, user_data);
1043 /* "Browse" button: user_data is the corresponding GtkEntry */
1045 browse_button_cb (GtkButton *button, gpointer user_data)
1047 GtkWidget *entry = GTK_WIDGET (user_data);
1048 const char *text = gtk_entry_get_text (GTK_ENTRY (entry));
1049 GtkFileSelection *selector =
1050 GTK_FILE_SELECTION (gtk_file_selection_new (_("Select file.")));
1052 gtk_file_selection_set_filename (selector, text);
1053 gtk_signal_connect (GTK_OBJECT (selector->ok_button),
1054 "clicked", GTK_SIGNAL_FUNC (file_sel_ok),
1056 gtk_signal_connect (GTK_OBJECT (selector->cancel_button),
1057 "clicked", GTK_SIGNAL_FUNC (file_sel_cancel),
1059 gtk_signal_connect (GTK_OBJECT (selector), "delete_event",
1060 GTK_SIGNAL_FUNC (file_sel_close),
1063 gtk_window_set_modal (GTK_WINDOW (selector), TRUE);
1064 gtk_widget_show (GTK_WIDGET (selector));
1068 /* Converting to and from command-lines
1072 /* Returns a copy of string that has been quoted according to shell rules:
1073 it may have been wrapped in "" and had some characters backslashed; or
1074 it may be unchanged.
1077 shell_quotify (const char *string)
1079 char *string2 = (char *) malloc ((strlen (string) * 2) + 10);
1082 int need_quotes = 0;
1087 for (in = string; *in; in++)
1098 else if (*in <= ' ' ||
1124 return strdup (string);
1127 /* Modify the string in place to remove wrapping double-quotes
1128 and interior backslashes.
1131 de_stringify (char *s)
1134 if (q != '\'' && q != '\"' && q != '`')
1136 memmove (s, s+1, strlen (s)+1);
1137 while (*s && *s != q)
1140 memmove (s, s+1, strlen (s)+1);
1143 if (*s != q) abort();
1148 /* Substitutes a shell-quotified version of `value' into `p->arg' at
1149 the place where the `%' character appeared.
1152 format_switch (parameter *p, const char *value)
1154 char *fmt = (char *) p->arg;
1157 if (!fmt || !value) return 0;
1158 v2 = shell_quotify (value);
1159 result = (char *) malloc (strlen (fmt) + strlen (v2) + 10);
1176 /* Maps a `parameter' to a command-line switch.
1177 Returns 0 if it can't, or if the parameter has the default value.
1180 parameter_to_switch (parameter *p)
1186 return strdup ((char *) p->arg);
1192 if (!p->widget) return 0;
1194 const char *s = gtk_entry_get_text (GTK_ENTRY (p->widget));
1196 if (!strcmp ((s ? s : ""),
1197 (p->string ? (char *) p->string : "")))
1198 v = 0; /* same as default */
1200 v = format_switch (p, s);
1202 /* don't free `s' */
1207 if (!p->widget) return 0;
1209 GtkAdjustment *adj =
1211 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1212 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1215 float value = (p->invert_p
1216 ? invert_range (adj->lower, adj->upper, adj->value) - 1
1219 if (value == p->value) /* same as default */
1223 sprintf (buf, "%d", (int) (value + (value > 0 ? 0.5 : -0.5)));
1225 sprintf (buf, "%.4f", value);
1227 s1 = strchr (buf, '.');
1230 char *s2 = s1 + strlen(s1) - 1;
1231 while (s2 > s1 && *s2 == '0') /* lose trailing zeroes */
1233 if (s2 >= s1 && *s2 == '.') /* lose trailing decimal */
1236 return format_switch (p, buf);
1239 if (!p->widget) return 0;
1241 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1242 const char *s = (gtk_toggle_button_get_active (b)
1243 ? (char *) p->arg_set
1244 : (char *) p->arg_unset);
1251 if (!p->widget) return 0;
1253 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1254 GtkMenu *menu = GTK_MENU (gtk_option_menu_get_menu (opt));
1255 GtkWidget *selected = gtk_menu_get_active (menu);
1256 GList *kids = gtk_container_children (GTK_CONTAINER (menu));
1257 int menu_elt = g_list_index (kids, (gpointer) selected);
1258 GList *ol = g_list_nth (p->options, menu_elt);
1259 parameter *o = (ol ? (parameter *) ol->data : 0);
1262 if (o->type != SELECT_OPTION) abort();
1263 s = (char *) o->arg_set;
1277 /* Maps a GList of `parameter' objects to a complete command-line string.
1278 All arguments will be properly quoted.
1281 parameters_to_cmd_line (GList *parms, gboolean default_p)
1283 int L = g_list_length (parms);
1285 char **strs = (char **) calloc (sizeof (*parms), L);
1290 for (i = 0, j = 0; parms; parms = parms->next, i++)
1292 parameter *p = (parameter *) parms->data;
1293 if (!default_p || p->type == COMMAND)
1295 char *s = parameter_to_switch (p);
1297 LL += (s ? strlen(s) : 0) + 1;
1301 result = (char *) malloc (LL + 10);
1303 for (i = 0; i < j; i++)
1306 strcpy (out, strs[i]);
1307 out += strlen (out);
1312 while (out > result && out[-1] == ' ') /* strip trailing spaces */
1320 /* Returns a GList of the tokens the string, using shell syntax;
1321 Quoted strings are handled as a single token.
1324 tokenize_command_line (const char *cmd)
1327 const char *s = cmd;
1332 for (; isspace(*s); s++); /* skip whitespace */
1335 if (*s == '\'' || *s == '\"' || *s == '`')
1339 while (*s && *s != q) /* skip to matching quote */
1341 if (*s == '\\' && s[1]) /* allowing backslash quoting */
1359 ss = (char *) malloc ((s - start) + 1);
1360 strncpy (ss, start, s-start);
1362 if (*ss == '\'' || *ss == '\"' || *ss == '`')
1364 result = g_list_append (result, ss);
1371 static void parameter_set_switch (parameter *, gpointer value);
1372 static gboolean parse_command_line_into_parameters_1 (const char *filename,
1379 /* Parses the command line, and flushes those options down into
1380 the `parameter' structs in the list.
1383 parse_command_line_into_parameters (const char *filename,
1384 const char *cmd, GList *parms)
1386 GList *tokens = tokenize_command_line (cmd);
1388 for (rest = tokens; rest; rest = rest->next)
1390 char *option = rest->data;
1393 if (option[0] != '-' && option[0] != '+')
1396 fprintf (stderr, "%s: WARNING: %s: not a switch: \"%s\"\n",
1397 blurb(), filename, option);
1403 if (rest->next) /* pop off the arg to this option */
1405 char *s = (char *) rest->next->data;
1406 /* the next token is the next switch iff it matches "-[a-z]".
1407 (To avoid losing on "-x -3.1".)
1409 if (s && (s[0] != '-' || !isalpha(s[1])))
1412 rest->next->data = 0;
1417 parse_command_line_into_parameters_1 (filename, parms,
1419 if (value) free (value);
1423 g_list_free (tokens);
1428 compare_opts (const char *option, const char *value,
1429 const char *template)
1431 int ol = strlen (option);
1434 if (strncmp (option, template, ol))
1437 if (template[ol] != (value ? ' ' : 0))
1440 /* At this point, we have a match against "option".
1441 If template contains a %, we're done.
1442 Else, compare against "value" too.
1444 c = strchr (template, '%');
1449 return (template[ol] == 0);
1450 if (strcmp (template + ol + 1, value))
1458 parse_command_line_into_parameters_1 (const char *filename,
1465 parameter *match = 0;
1469 for (p = parms; p; p = p->next)
1471 parameter *pp = (parameter *) p->data;
1474 if (pp->type == SELECT)
1476 if (parse_command_line_into_parameters_1 (filename,
1487 if (compare_opts (option, value, (char *) pp->arg))
1493 else if (pp->arg_set)
1495 if (compare_opts (option, value, (char *) pp->arg_set))
1501 else if (pp->arg_unset)
1503 if (compare_opts (option, value, (char *) pp->arg_unset))
1518 if (debug_p && !parent)
1519 fprintf (stderr, "%s: WARNING: %s: no match for %s %s\n",
1520 blurb(), filename, option, (value ? value : ""));
1524 switch (match->type)
1530 if (which != -1) abort();
1531 parameter_set_switch (match, (gpointer) value);
1534 if (which != 0 && which != 1) abort();
1535 parameter_set_switch (match, GINT_TO_POINTER(which));
1538 if (which != 1) abort();
1539 parameter_set_switch (parent, GINT_TO_POINTER(index));
1548 /* Set the parameter's value.
1549 For STRING, FILENAME, SLIDER, and SPINBUTTON, `value' is a char*.
1550 For BOOLEAN and SELECT, `value' is an int.
1553 parameter_set_switch (parameter *p, gpointer value)
1555 if (p->type == SELECT_OPTION) abort();
1556 if (!p->widget) return;
1562 gtk_entry_set_text (GTK_ENTRY (p->widget), (char *) value);
1568 GtkAdjustment *adj =
1570 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1571 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1575 if (1 == sscanf ((char *) value, "%f %c", &f, &c))
1578 f = invert_range (adj->lower, adj->upper, f) - 1;
1579 gtk_adjustment_set_value (adj, f);
1585 GtkToggleButton *b = GTK_TOGGLE_BUTTON (p->widget);
1586 gtk_toggle_button_set_active (b, GPOINTER_TO_INT(value));
1591 gtk_option_menu_set_history (GTK_OPTION_MENU (p->widget),
1592 GPOINTER_TO_INT(value));
1602 restore_defaults (const char *progname, GList *parms)
1604 for (; parms; parms = parms->next)
1606 parameter *p = (parameter *) parms->data;
1607 if (!p->widget) continue;
1613 gtk_entry_set_text (GTK_ENTRY (p->widget),
1614 (p->string ? (char *) p->string : ""));
1620 GtkAdjustment *adj =
1622 ? gtk_range_get_adjustment (GTK_RANGE (p->widget))
1623 : gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (p->widget)));
1624 float value = (p->invert_p
1625 ? invert_range (p->low, p->high, p->value)
1627 gtk_adjustment_set_value (adj, value);
1632 /* A toggle button should be on by default if it inserts
1633 nothing into the command line when on. E.g., it should
1634 be on if `arg_set' is null.
1636 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->widget),
1637 (!p->arg_set || !*p->arg_set));
1642 GtkOptionMenu *opt = GTK_OPTION_MENU (p->widget);
1647 for (opts = p->options, index = 0; opts;
1648 opts = opts->next, index++)
1650 parameter *s = (parameter *) opts->data;
1651 /* The default menu item is the first one with
1652 no `arg_set' field. */
1660 gtk_option_menu_set_history (GTK_OPTION_MENU (opt), selected);
1671 /* Documentation strings
1675 get_description (GList *parms, gboolean verbose_p)
1678 for (; parms; parms = parms->next)
1680 parameter *p = (parameter *) parms->data;
1681 if (p->type == DESCRIPTION)
1688 if (!doc || !doc->string)
1692 char *d = strdup ((char *) doc->string);
1695 for (s = d; *s; s++)
1698 if (s[1] == '\n') /* blank line: leave it */
1700 else if (s[1] == ' ' || s[1] == '\t')
1701 s++; /* next line is indented: leave newline */
1702 else if (!strncmp(s+1, "http:", 5))
1703 s++; /* next line begins a URL: leave newline */
1705 s[0] = ' '; /* delete newline to un-fold this line */
1708 /* strip off leading whitespace on first line only */
1709 for (s = d; *s && (*s == ' ' || *s == '\t'); s++)
1711 while (*s == '\n') /* strip leading newlines */
1714 memmove (d, s, strlen(s)+1);
1716 /* strip off trailing whitespace and newlines */
1719 while (L && isspace(d[L-1]))
1723 /* strip off duplicated whitespaces */
1724 for (s = d; *s; s++)
1731 memmove (p, s, strlen(s)+1);
1737 fprintf (stderr, "%s: text read is \"%s\"\n", blurb(),doc->string);
1738 fprintf (stderr, "%s: description is \"%s\"\n", blurb(), d);
1739 fprintf (stderr, "%s: translation is \"%s\"\n", blurb(), _(d));
1748 /* External interface.
1752 load_configurator_1 (const char *program, const char *arguments,
1755 const char *dir = hack_configuration_path;
1757 int L = strlen (dir);
1763 if (L == 0) return 0;
1765 base_program = strrchr(program, '/');
1766 if (base_program) base_program++;
1767 if (!base_program) base_program = (char *) program;
1769 file = (char *) malloc (L + strlen (base_program) + 10);
1770 data = (conf_data *) calloc (1, sizeof(*data));
1773 if (file[L-1] != '/')
1775 strcpy (file+L, base_program);
1777 for (s = file+L; *s; s++)
1778 if (*s == '/' || *s == ' ')
1780 else if (isupper (*s))
1783 strcat (file+L, ".xml");
1785 f = fopen (file, "r");
1788 int res, size = 1024;
1790 xmlParserCtxtPtr ctxt;
1796 fprintf (stderr, "%s: reading %s...\n", blurb(), file);
1798 res = fread (chars, 1, 4, f);
1806 ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, file);
1807 while ((res = fread(chars, 1, size, f)) > 0)
1808 xmlParseChunk (ctxt, chars, res, 0);
1809 xmlParseChunk (ctxt, chars, 0, 1);
1811 xmlFreeParserCtxt (ctxt);
1814 /* Parsed the XML file. Now make some widgets. */
1816 vbox0 = gtk_vbox_new (FALSE, 0);
1817 gtk_widget_show (vbox0);
1819 parms = make_parameters (file, doc->xmlRootNode, vbox0);
1820 sanity_check_parameters (file, parms);
1824 restore_defaults (program, parms);
1825 if (arguments && *arguments)
1826 parse_command_line_into_parameters (program, arguments, parms);
1828 data->widget = vbox0;
1829 data->parameters = parms;
1830 data->description = get_description (parms, verbose_p);
1837 fprintf (stderr, "%s: %s does not exist.\n", blurb(), file);
1839 p = calloc (1, sizeof(*p));
1841 p->arg = (xmlChar *) strdup (arguments);
1843 data->parameters = g_list_append (0, (gpointer) p);
1846 data->progname = strdup (program);
1854 split_command_line (const char *full_command_line,
1855 char **prog_ret, char **args_ret)
1857 char *line = strdup (full_command_line);
1870 while (isspace (*s)) s++;
1873 else if (*s == '=') /* if the leading word contains an "=", skip it. */
1875 while (*s && !isspace (*s)) s++;
1876 while (isspace (*s)) s++;
1883 *prog_ret = strdup (prog);
1884 *args_ret = strdup (args);
1890 load_configurator (const char *full_command_line, gboolean verbose_p)
1895 debug_p = verbose_p;
1896 split_command_line (full_command_line, &prog, &args);
1897 cd = load_configurator_1 (prog, args, verbose_p);
1906 get_configurator_command_line (conf_data *data, gboolean default_p)
1908 char *args = parameters_to_cmd_line (data->parameters, default_p);
1909 char *result = (char *) malloc (strlen (data->progname) +
1911 strcpy (result, data->progname);
1912 strcat (result, " ");
1913 strcat (result, args);
1920 set_configurator_command_line (conf_data *data, const char *full_command_line)
1924 split_command_line (full_command_line, &prog, &args);
1925 if (data->progname) free (data->progname);
1926 data->progname = prog;
1927 restore_defaults (prog, data->parameters);
1928 parse_command_line_into_parameters (prog, args, data->parameters);
1933 free_conf_data (conf_data *data)
1935 if (data->parameters)
1938 for (rest = data->parameters; rest; rest = rest->next)
1940 free_parameter ((parameter *) rest->data);
1943 g_list_free (data->parameters);
1944 data->parameters = 0;
1948 gtk_widget_destroy (data->widget);
1951 free (data->progname);
1952 if (data->description)
1953 free (data->description);
1955 memset (data, ~0, sizeof(*data));
1960 #endif /* HAVE_GTK && HAVE_XML -- whole file */