1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011 Free Software Foundation, Inc.
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/>.
20 #include "psppire-scanf.h"
22 #include <gl/printf-parse.h>
29 static void psppire_scanf_class_init (PsppireScanfClass *class);
30 static void psppire_scanf_init (PsppireScanf *w);
32 G_DEFINE_TYPE (PsppireScanf, psppire_scanf, GTK_TYPE_BOX)
44 /* Create a GtkLabel and pack it into BOX.
45 The label is created using part of the string at S, and the directives
46 at DIRS[DIR_IDX] and subsequent.
48 After this function returns, *S points to the first unused character.
51 ship_label (PsppireScanf *box, const char **s,
52 const char_directives *dirs, size_t dir_idx)
55 GString *str = g_string_new (*s);
59 char_directive dir = dirs->dir[dir_idx];
62 while (dir_idx < dirs->count && dir.conversion == '%')
64 g_string_erase (str, dir.dir_start - *s, 1);
65 dir = dirs->dir[++dir_idx];
69 g_string_truncate (str, dir.dir_start - *s - n);
71 if (dir_idx >= dirs->count)
77 label = gtk_label_new (str->str);
79 g_string_free (str, TRUE);
81 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
82 gtk_widget_show (label);
86 guts (PsppireScanf *scanf)
90 const char *s = scanf->format;
92 /* Get the number of args into D */
93 g_return_if_fail (0 == printf_parse (scanf->format, &scanf->d, &a));
95 if (scanf->d.count > 0)
96 scanf->widgets = xcalloc (scanf->d.count, sizeof (*scanf->widgets));
98 /* A is not used, so get rid of it */
99 if (a.arg != a.direct_alloc_arg)
102 for (i = 0 ; i < scanf->d.count ; ++i)
105 char_directive dir = scanf->d.dir[i];
109 if (dir.precision_start && dir.precision_end)
110 precision = strtol (dir.precision_start + 1,
111 (char **) &dir.precision_end, 10);
113 if (dir.width_start && dir.width_end)
114 width = strtol (dir.width_start, (char **) &dir.width_end, 10);
116 if (dir.dir_start > s)
117 ship_label (scanf, &s, &scanf->d, i);
119 if (dir.conversion == '%')
125 w = &scanf->widgets [dir.arg_index];
126 switch (dir.conversion)
132 *w = gtk_spin_button_new_with_range (0, 100.0, 1.0);
133 g_object_set (*w, "digits", precision, NULL);
137 *w = gtk_entry_new ();
140 g_object_set (*w, "width-chars", width, NULL);
141 gtk_box_pack_start (GTK_BOX (scanf), *w, FALSE, FALSE, 0);
142 gtk_widget_show (*w);
146 ship_label (scanf, &s, NULL, 0);
152 set_mnemonic (PsppireScanf *scanf)
154 if (scanf->use_underline || scanf->mnemonic_widget)
156 GList *l = gtk_container_get_children (GTK_CONTAINER (scanf));
159 if (GTK_IS_LABEL (l->data))
161 const gchar *t = gtk_label_get_label (l->data);
162 if (g_strstr_len (t, -1, "_"))
164 g_object_set (l->data,
165 "use-underline", TRUE,
166 "mnemonic-widget", scanf->mnemonic_widget,
179 psppire_scanf_set_property (GObject *object,
184 PsppireScanf *scanf = PSPPIRE_SCANF (object);
189 scanf->format = g_value_get_string (value);
192 case PROP_MNEMONIC_WIDGET:
193 scanf->mnemonic_widget = g_value_get_object (value);
194 set_mnemonic (scanf);
196 case PROP_USE_UNDERLINE:
197 scanf->use_underline = g_value_get_boolean (value);
198 set_mnemonic (scanf);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 psppire_scanf_get_property (GObject *object,
213 PsppireScanf *scanf = PSPPIRE_SCANF (object);
218 g_value_set_string (value, scanf->format);
221 g_value_set_int (value, scanf->d.count);
223 case PROP_USE_UNDERLINE:
224 g_value_set_boolean (value, scanf->use_underline);
226 case PROP_MNEMONIC_WIDGET:
227 g_value_set_object (value, scanf->mnemonic_widget);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 static GObjectClass *parent_class = NULL;
239 psppire_scanf_dispose (GObject *obj)
241 PsppireScanf *w = (PsppireScanf *)obj;
243 if (w->dispose_has_run)
246 /* Make sure dispose does not run twice. */
247 w->dispose_has_run = TRUE;
250 /* Chain up to the parent class */
251 G_OBJECT_CLASS (parent_class)->dispose (obj);
255 psppire_scanf_finalize (GObject *obj)
257 PsppireScanf *w = PSPPIRE_SCANF (obj);
261 if (w->d.dir != w->d.direct_alloc_dir)
264 /* Chain up to the parent class */
265 G_OBJECT_CLASS (parent_class)->finalize (obj);
269 psppire_scanf_class_init (PsppireScanfClass *class)
271 GObjectClass *object_class = G_OBJECT_CLASS (class);
273 GParamSpec *format_spec =
274 g_param_spec_string ("format",
276 "A Scanf style format string",
278 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
280 GParamSpec *nconv_spec =
281 g_param_spec_int ("n-conv",
283 "The number of conversions in the format string",
288 GParamSpec *use_underline_spec =
289 g_param_spec_boolean ("use-underline",
291 "If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key",
296 GParamSpec *mnemonic_widget_spec =
297 g_param_spec_object ("mnemonic-widget",
299 "The widget which is to be activated when the Scanf's mnemonic key is pressed. Has no effect if use-underline is false.",
304 parent_class = g_type_class_peek_parent (class);
306 object_class->dispose = psppire_scanf_dispose;
307 object_class->finalize = psppire_scanf_finalize;
309 object_class->set_property = psppire_scanf_set_property;
310 object_class->get_property = psppire_scanf_get_property;
312 g_object_class_install_property (object_class,
316 g_object_class_install_property (object_class,
320 g_object_class_install_property (object_class,
324 g_object_class_install_property (object_class,
325 PROP_MNEMONIC_WIDGET,
326 mnemonic_widget_spec);
332 psppire_scanf_init (PsppireScanf *w)
334 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), GTK_ORIENTATION_HORIZONTAL);
338 psppire_get_conversion_char (PsppireScanf *w, gint n)
340 g_return_val_if_fail (n < w->d.count, '\0');
341 return w->d.dir[n].conversion;
345 psppire_scanf_get_child (PsppireScanf *w, gint n)
347 g_return_val_if_fail (n < w->d.count, NULL);
348 return w->widgets[n];
353 This widget is a horizontal GtkBox populated with GtkLabel and GtkEntry widgets.
354 Each conversion in FMT will cause a GtkEntry (possibly a GtkSpinButton) to
355 be created. Any text between conversions produces a GtkLabel.
356 There should be N arguments following FMT should be of type GtkEntry **,
357 where N is the number of conversions.
358 These arguments will be filled with a pointer to the corresponding widgets.
359 Their properties may be changed, but they should not be unrefed.
362 psppire_scanf_new (const gchar *fmt, ...)
367 GtkWidget *w = GTK_WIDGET (g_object_new (psppire_scanf_get_type (),
368 "format", fmt, NULL));
370 g_object_get (w, "n-conv", &n, NULL);
374 for (i = 0 ; i < n ; ++i)
378 if (psppire_get_conversion_char (PSPPIRE_SCANF (w), i) == '%')
381 field = va_arg (ap, GtkWidget **);
383 *field = psppire_scanf_get_child (PSPPIRE_SCANF (w), i);