1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "widget-io.h"
23 #include <gl/printf-parse.h>
27 #include <gl/gettext.h>
32 ship_label (GtkBox *box, const char **s, const char_directive *dir)
35 gchar *text = g_strdup (*s);
39 text [ dir->dir_start - *s ] = '\0';
43 label = gtk_label_new (text);
46 gtk_box_pack_start (box, label, FALSE, FALSE, 0);
47 gtk_widget_show (label);
50 /* Returns a string generated from FMT and a list of GtkEntry widgets.
51 Each conversion in FMT will be replaced with the text from the
52 corresponding GtkEntry. The normal printf semantics will be ignored.
53 Note that the GtkEntry widgets may be GtkSpinbuttons or any other widget
54 derived from GtkEntry.
55 The returned string should be freed when no longer required.
58 widget_printf (const gchar *fmt, ...)
69 if ( 0 != printf_parse (fmt, &d, &a) )
72 widgets = calloc (sizeof (*widgets), d.count);
74 for (i = 0 ; i < d.count ; ++i )
76 if ( d.dir[i].conversion != '%')
77 widgets[i] = va_arg (ap, GtkWidget *);
83 output = g_string_sized_new (strlen (fmt));
85 for (i = 0 ; i < d.count ; ++i )
87 char_directive dir = d.dir[i];
89 const gchar *entry_text;
91 if ( dir.conversion == '%')
97 w = widgets [dir.arg_index];
98 entry_text = gtk_entry_get_text (GTK_ENTRY (w));
100 if ( dir.dir_start > s )
101 g_string_append_len (output, s, dir.dir_start - s);
105 g_string_append (output, entry_text);
112 g_string_append_len (output, s, -1);
115 g_string_free (output, FALSE);
120 Returns a GtkHBox populated with an GtkLabel and GtkEntry widgets.
121 Each conversion in FMT will cause a GtkEntry (possibly a GtkSpinButton) to
122 be created. Any text between conversions produces a GtkLabel.
123 There should be N arguments following FMT should be of type GtkEntry **,
124 where N is the number of conversions.
125 These arguments will be filled with a pointer to the corresponding widgets.
126 Their properties may be changed, but they should not be unrefed.
129 widget_scanf (const gchar *fmt, ...)
135 GtkWidget ***widgets = NULL;
136 GtkWidget *hbox = NULL;
140 if ( 0 != printf_parse (fmt, &d, &a) )
149 hbox = gtk_hbox_new (FALSE, 0);
150 widgets = calloc (sizeof (*widgets), d.count);
153 for (i = 0 ; i < d.count ; ++i )
155 if ( d.dir[i].conversion != '%')
156 widgets[i] = va_arg (ap, GtkWidget **);
161 for (i = 0 ; i < d.count ; ++i )
163 char_directive dir = d.dir[i];
168 if ( dir.precision_start && dir.precision_end)
169 precision = strtol (dir.precision_start + 1,
170 (char **) &dir.precision_end, 10);
172 if ( dir.width_start && dir.width_end )
173 width = strtol (dir.width_start, (char **) &dir.width_end, 10);
175 if ( dir.dir_start > s )
176 ship_label (GTK_BOX (hbox), &s, &dir);
178 if ( dir.conversion == '%')
184 w = widgets [dir.arg_index];
185 switch (dir.conversion)
191 *w = gtk_spin_button_new_with_range (0, 100.0, 1.0);
192 g_object_set (*w, "digits", precision, NULL);
196 *w = gtk_entry_new ();
200 g_object_set (*w, "width-chars", width, NULL);
201 gtk_box_pack_start (GTK_BOX (hbox), *w, FALSE, FALSE, 0);
202 gtk_widget_show (*w);
206 ship_label (GTK_BOX (hbox), &s, NULL);