Added the Variable Information dialog.
[pspp-builds.git] / src / ui / gui / psppire-var-store.c
1 /* psppire-var-store.c
2
3    PSPPIRE --- A Graphical User Interface for PSPP
4    Copyright (C) 2006  Free Software Foundation
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA. */
20
21 #include <config.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <gettext.h>
25 #define _(msgid) gettext (msgid)
26 #define N_(msgid) msgid
27
28
29
30 #include <gobject/gvaluecollector.h>
31
32 #include <gtksheet/gsheetmodel.h>
33
34 #include "psppire-var-store.h"
35 #include "var-sheet.h"
36 #include "helper.h"
37
38 #include <data/dictionary.h>
39 #include <data/variable.h>
40 #include <data/missing-values.h>
41
42 #include "val-labs-dialog.h"
43 #include "missing-val-dialog.h"
44 #include <data/value-labels.h>
45
46 #include "var-display.h"
47
48 #define TRAILING_ROWS 40
49
50 static void         psppire_var_store_init            (PsppireVarStore      *var_store);
51 static void         psppire_var_store_class_init      (PsppireVarStoreClass *class);
52 static void         psppire_var_store_sheet_model_init (GSheetModelIface *iface);
53 static void         psppire_var_store_finalize        (GObject           *object);
54
55
56 gchar * missing_values_to_string (const struct variable *pv, GError **err);
57
58
59 static gchar *psppire_var_store_get_string (const GSheetModel *sheet_model, gint row, gint column);
60
61 static gboolean  psppire_var_store_clear (GSheetModel *model,  gint row, gint col);
62
63
64 static gboolean psppire_var_store_set_string (GSheetModel *model,
65                                           const gchar *text, gint row, gint column);
66
67 static gint psppire_var_store_get_row_count (const GSheetModel * model);
68
69 static gchar *text_for_column (const struct variable *pv, gint c, GError **err);
70
71
72 static void psppire_var_store_sheet_row_init (GSheetRowIface *iface);
73
74
75
76 static GObjectClass *parent_class = NULL;
77
78 GType
79 psppire_var_store_get_type (void)
80 {
81   static GType var_store_type = 0;
82
83   if (!var_store_type)
84     {
85       static const GTypeInfo var_store_info =
86       {
87         sizeof (PsppireVarStoreClass),
88         NULL,           /* base_init */
89         NULL,           /* base_finalize */
90         (GClassInitFunc) psppire_var_store_class_init,
91         NULL,           /* class_finalize */
92         NULL,           /* class_data */
93         sizeof (PsppireVarStore),
94         0,
95         (GInstanceInitFunc) psppire_var_store_init,
96       };
97
98       static const GInterfaceInfo sheet_model_info =
99       {
100         (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
101         NULL,
102         NULL
103       };
104
105       static const GInterfaceInfo sheet_row_info =
106       {
107         (GInterfaceInitFunc) psppire_var_store_sheet_row_init,
108         NULL,
109         NULL
110       };
111
112       var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
113
114       g_type_add_interface_static (var_store_type,
115                                    G_TYPE_SHEET_MODEL,
116                                    &sheet_model_info);
117
118       g_type_add_interface_static (var_store_type,
119                                    G_TYPE_SHEET_ROW,
120                                    &sheet_row_info);
121
122
123     }
124
125   return var_store_type;
126 }
127
128 static void
129 psppire_var_store_class_init (PsppireVarStoreClass *class)
130 {
131   GObjectClass *object_class;
132
133   parent_class = g_type_class_peek_parent (class);
134   object_class = (GObjectClass*) class;
135
136   object_class->finalize = psppire_var_store_finalize;
137 }
138
139
140 static void
141 psppire_var_store_init (PsppireVarStore *var_store)
142 {
143   GdkColormap *colormap = gdk_colormap_get_system ();
144
145   g_assert (gdk_color_parse ("gray", &var_store->disabled));
146
147   gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
148
149   var_store->dict = 0;
150 }
151
152 static gboolean
153 psppire_var_store_item_editable (PsppireVarStore *var_store, gint row, gint column)
154 {
155   const struct fmt_spec *write_spec ;
156
157   struct variable *pv = psppire_var_store_get_var (var_store, row);
158
159   if ( !pv )
160     return TRUE;
161
162   if ( VAR_STRING == var_get_type (pv) && column == COL_DECIMALS )
163     return FALSE;
164
165   write_spec = var_get_print_format (pv);
166
167   switch ( write_spec->type )
168     {
169     case FMT_DATE:
170     case FMT_EDATE:
171     case FMT_SDATE:
172     case FMT_ADATE:
173     case FMT_JDATE:
174     case FMT_QYR:
175     case FMT_MOYR:
176     case FMT_WKYR:
177     case FMT_DATETIME:
178     case FMT_TIME:
179     case FMT_DTIME:
180     case FMT_WKDAY:
181     case FMT_MONTH:
182       if ( column == COL_DECIMALS || column == COL_WIDTH)
183         return FALSE;
184       break;
185     default:
186       break;
187     }
188
189   return TRUE;
190 }
191
192
193 struct variable *
194 psppire_var_store_get_var (PsppireVarStore *store, gint row)
195 {
196   return psppire_dict_get_variable (store->dict, row);
197 }
198
199 static gboolean
200 psppire_var_store_is_editable (const GSheetModel *model, gint row, gint column)
201 {
202   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
203   return psppire_var_store_item_editable (store, row, column);
204 }
205
206
207 static const GdkColor *
208 psppire_var_store_get_foreground (const GSheetModel *model, gint row, gint column)
209 {
210   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
211
212   if ( ! psppire_var_store_item_editable (store, row, column) )
213     return &store->disabled;
214
215   return NULL;
216 }
217
218
219 const PangoFontDescription *
220 psppire_var_store_get_font_desc (const GSheetModel *model,
221                               gint row, gint column)
222 {
223   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
224
225   return store->font_desc;
226 }
227
228
229
230 static void
231 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
232 {
233   iface->get_row_count = psppire_var_store_get_row_count;
234   iface->free_strings = TRUE;
235   iface->get_string = psppire_var_store_get_string;
236   iface->set_string = psppire_var_store_set_string;
237   iface->clear_datum = psppire_var_store_clear;
238   iface->is_editable = psppire_var_store_is_editable;
239   iface->is_visible = NULL;
240   iface->get_foreground = psppire_var_store_get_foreground;
241   iface->get_background = NULL;
242   iface->get_font_desc = psppire_var_store_get_font_desc;
243   iface->get_cell_border = NULL;
244 }
245
246
247
248 /**
249  * psppire_var_store_new:
250  * @dict: The dictionary for this var_store.
251  *
252  *
253  * Return value: a new #PsppireVarStore
254  **/
255 PsppireVarStore *
256 psppire_var_store_new (PsppireDict *dict)
257 {
258   PsppireVarStore *retval;
259
260   retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
261
262   psppire_var_store_set_dictionary (retval, dict);
263
264   return retval;
265 }
266
267 static void
268 var_change_callback (GtkWidget *w, gint n, gpointer data)
269 {
270   GSheetModel *model = G_SHEET_MODEL (data);
271
272   g_sheet_model_range_changed (model,
273                                  n, 0, n, n_COLS);
274 }
275
276
277 static void
278 var_delete_callback (GtkWidget *w, gint first, gint n, gpointer data)
279 {
280   GSheetModel *model = G_SHEET_MODEL (data);
281
282   g_sheet_model_rows_deleted (model, first, n);
283 }
284
285
286
287 static void
288 var_insert_callback (GtkWidget *w, gint row, gpointer data)
289 {
290   GSheetModel *model = G_SHEET_MODEL (data);
291
292   g_sheet_model_rows_inserted (model, row, 1);
293 }
294
295
296
297 /**
298  * psppire_var_store_replace_set_dictionary:
299  * @var_store: The variable store
300  * @dict: The dictionary to set
301  *
302  * If a dictionary is already associated with the var-store, then it will be
303  * destroyed.
304  **/
305 void
306 psppire_var_store_set_dictionary (PsppireVarStore *var_store, PsppireDict *dict)
307 {
308   if ( var_store->dict ) g_object_unref (var_store->dict);
309
310   var_store->dict = dict;
311
312   g_signal_connect (dict, "variable-changed", G_CALLBACK (var_change_callback),
313                    var_store);
314
315   g_signal_connect (dict, "variables-deleted", G_CALLBACK (var_delete_callback),
316                    var_store);
317
318   g_signal_connect (dict, "variable-inserted", G_CALLBACK (var_insert_callback),
319                    var_store);
320
321
322   /* The entire model has changed */
323   g_sheet_model_range_changed (G_SHEET_MODEL (var_store), -1, -1, -1, -1);
324 }
325
326 static void
327 psppire_var_store_finalize (GObject *object)
328 {
329   /* must chain up */
330   (* parent_class->finalize) (object);
331 }
332
333 static gchar *
334 psppire_var_store_get_string (const GSheetModel *model, gint row, gint column)
335 {
336   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
337
338   struct variable *pv;
339
340   if ( row >= psppire_dict_get_var_cnt (store->dict))
341     return 0;
342
343   pv = psppire_dict_get_variable (store->dict, row);
344
345   return text_for_column (pv, column, 0);
346 }
347
348
349 /* Clears that part of the variable store, if possible, which corresponds
350    to ROW, COL.
351    Returns true if anything was updated, false otherwise.
352 */
353 static gboolean
354 psppire_var_store_clear (GSheetModel *model,  gint row, gint col)
355 {
356   struct variable *pv ;
357
358   PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
359
360   if ( row >= psppire_dict_get_var_cnt (var_store->dict))
361       return FALSE;
362
363   pv = psppire_var_store_get_var (var_store, row);
364
365   if ( !pv )
366     return FALSE;
367
368   switch (col)
369     {
370     case COL_LABEL:
371       var_set_label (pv, 0);
372       return TRUE;
373       break;
374     }
375
376   return FALSE;
377 }
378
379 /* Attempts to update that part of the variable store which corresponds
380    to ROW, COL with  the value TEXT.
381    Returns true if anything was updated, false otherwise.
382 */
383 static gboolean
384 psppire_var_store_set_string (GSheetModel *model,
385                           const gchar *text, gint row, gint col)
386 {
387   struct variable *pv ;
388
389   PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
390
391   if ( row >= psppire_dict_get_var_cnt (var_store->dict))
392       return FALSE;
393
394   pv = psppire_var_store_get_var (var_store, row);
395
396   if ( !pv )
397     return FALSE;
398
399   switch (col)
400     {
401     case COL_NAME:
402       return psppire_dict_rename_var (var_store->dict, pv, text);
403       break;
404     case COL_COLUMNS:
405       if ( ! text) return FALSE;
406       var_set_display_width (pv, atoi (text));
407       return TRUE;
408       break;
409     case COL_WIDTH:
410       {
411         int width = atoi (text);
412         if ( ! text) return FALSE;
413         if ( var_is_alpha (pv))
414             var_set_width (pv, width);
415         else
416           {
417             struct fmt_spec fmt ;
418             fmt = *var_get_write_format (pv);
419             if ( width < fmt_min_output_width (fmt.type)
420                  ||
421                  width > fmt_max_output_width (fmt.type))
422               return FALSE;
423
424             fmt.w = width;
425             fmt.d = MIN (fmt_max_output_decimals (fmt.type, width), fmt.d);
426
427             var_set_both_formats (pv, &fmt);
428           }
429
430         return TRUE;
431       }
432       break;
433     case COL_DECIMALS:
434       {
435         int decimals;
436         struct fmt_spec fmt;
437         if ( ! text) return FALSE;
438         decimals = atoi (text);
439         fmt = *var_get_write_format (pv);
440         if ( decimals >
441              fmt_max_output_decimals (fmt.type,
442                                       fmt.w
443                                       ))
444           return FALSE;
445
446         fmt.d = decimals;
447         var_set_both_formats (pv, &fmt);
448         return TRUE;
449       }
450       break;
451     case COL_LABEL:
452       var_set_label (pv, text);
453       return TRUE;
454       break;
455     case COL_TYPE:
456     case COL_VALUES:
457     case COL_MISSING:
458     case COL_ALIGN:
459     case COL_MEASURE:
460       /* These can be modified only by their respective dialog boxes */
461       return FALSE;
462       break;
463     default:
464       g_assert_not_reached ();
465       return FALSE;
466     }
467
468   return TRUE;
469 }
470
471
472 const static gchar none[] = N_("None");
473
474 static  gchar *
475 text_for_column (const struct variable *pv, gint c, GError **err)
476 {
477   static const gchar *const type_label[] =
478     {
479       N_("Numeric"),
480       N_("Comma"),
481       N_("Dot"),
482       N_("Scientific"),
483       N_("Date"),
484       N_("Dollar"),
485       N_("Custom"),
486       N_("String")
487     };
488   enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
489         VT_CUSTOM, VT_STRING};
490
491   const struct fmt_spec *write_spec = var_get_write_format (pv);
492
493   switch (c)
494     {
495     case COL_NAME:
496       return pspp_locale_to_utf8 ( var_get_name (pv), -1, err);
497       break;
498     case COL_TYPE:
499       {
500         switch ( write_spec->type )
501           {
502           case FMT_F:
503             return g_locale_to_utf8 (gettext (type_label[VT_NUMERIC]), -1, 0, 0, err);
504             break;
505           case FMT_COMMA:
506             return g_locale_to_utf8 (gettext (type_label[VT_COMMA]), -1, 0, 0, err);
507             break;
508           case FMT_DOT:
509             return g_locale_to_utf8 (gettext (type_label[VT_DOT]), -1, 0, 0, err);
510             break;
511           case FMT_E:
512             return g_locale_to_utf8 (gettext (type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
513             break;
514           case FMT_DATE:
515           case FMT_EDATE:
516           case FMT_SDATE:
517           case FMT_ADATE:
518           case FMT_JDATE:
519           case FMT_QYR:
520           case FMT_MOYR:
521           case FMT_WKYR:
522           case FMT_DATETIME:
523           case FMT_TIME:
524           case FMT_DTIME:
525           case FMT_WKDAY:
526           case FMT_MONTH:
527             return g_locale_to_utf8 (type_label[VT_DATE], -1, 0, 0, err);
528             break;
529           case FMT_DOLLAR:
530             return g_locale_to_utf8 (type_label[VT_DOLLAR], -1, 0, 0, err);
531             break;
532           case FMT_CCA:
533           case FMT_CCB:
534           case FMT_CCC:
535           case FMT_CCD:
536           case FMT_CCE:
537             return g_locale_to_utf8 (gettext (type_label[VT_CUSTOM]), -1, 0, 0, err);
538             break;
539           case FMT_A:
540             return g_locale_to_utf8 (gettext (type_label[VT_STRING]), -1, 0, 0, err);
541             break;
542           default:
543             {
544               char str[FMT_STRING_LEN_MAX + 1];
545               g_warning ("Unknown format: \"%s\"\n",
546                         fmt_to_string (write_spec, str));
547             }
548             break;
549           }
550       }
551       break;
552     case COL_WIDTH:
553       {
554         gchar *s;
555         GString *gstr = g_string_sized_new (10);
556         g_string_printf (gstr, _("%d"), write_spec->w);
557         s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
558         g_string_free (gstr, TRUE);
559         return s;
560       }
561       break;
562     case COL_DECIMALS:
563       {
564         gchar *s;
565         GString *gstr = g_string_sized_new (10);
566         g_string_printf (gstr, _("%d"), write_spec->d);
567         s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
568         g_string_free (gstr, TRUE);
569         return s;
570       }
571       break;
572     case COL_COLUMNS:
573       {
574         gchar *s;
575         GString *gstr = g_string_sized_new (10);
576         g_string_printf (gstr, _("%d"), var_get_display_width (pv));
577         s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
578         g_string_free (gstr, TRUE);
579         return s;
580       }
581       break;
582     case COL_LABEL:
583       return pspp_locale_to_utf8 (var_get_label (pv), -1, err);
584       break;
585
586     case COL_MISSING:
587       {
588         return missing_values_to_string (pv, err);
589       }
590       break;
591     case COL_VALUES:
592       {
593         if ( ! var_has_value_labels (pv))
594           return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
595         else
596           {
597             gchar *ss;
598             GString *gstr = g_string_sized_new (10);
599             const struct val_labs *vls = var_get_value_labels (pv);
600             struct val_labs_iterator *ip = 0;
601             struct val_lab *vl = val_labs_first_sorted (vls, &ip);
602
603             g_assert (vl);
604
605             {
606               gchar *const vstr = value_to_text (vl->value, *write_spec);
607
608               g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
609               g_free (vstr);
610             }
611
612             val_labs_done (&ip);
613
614             ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
615             g_string_free (gstr, TRUE);
616             return ss;
617           }
618       }
619       break;
620     case COL_ALIGN:
621       {
622         const gint align = var_get_alignment (pv);
623
624         g_assert (align < n_ALIGNMENTS);
625         return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
626       }
627       break;
628     case COL_MEASURE:
629       {
630         return measure_to_string (pv, err);
631       }
632       break;
633     }
634   return 0;
635 }
636
637
638
639 /* Return the number of variables */
640 gint
641 psppire_var_store_get_var_cnt (PsppireVarStore  *store)
642 {
643   return psppire_dict_get_var_cnt (store->dict);
644 }
645
646
647 void
648 psppire_var_store_set_font (PsppireVarStore *store, const PangoFontDescription *fd)
649 {
650   g_return_if_fail (store);
651   g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
652
653   store->font_desc = fd;
654
655   g_sheet_model_range_changed (G_SHEET_MODEL (store), -1, -1, -1, -1);
656 }
657
658
659 static gint
660 psppire_var_store_get_row_count (const GSheetModel * model)
661 {
662   gint rows = 0;
663   PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
664
665   if (vs->dict)
666     rows =  psppire_dict_get_var_cnt (vs->dict);
667
668   return rows ;
669 }
670
671 /* Row related funcs */
672
673 static gint
674 geometry_get_row_count (const GSheetRow *geom, gpointer data)
675 {
676   gint rows = 0;
677   PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
678
679   if (vs->dict)
680     rows =  psppire_dict_get_var_cnt (vs->dict);
681
682   return rows + TRAILING_ROWS;
683 }
684
685
686 static gint
687 geometry_get_height (const GSheetRow *geom, gint row, gpointer data)
688 {
689   return 25;
690 }
691
692
693 static gboolean
694 geometry_is_sensitive (const GSheetRow *geom, gint row, gpointer data)
695 {
696   PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
697
698   if ( ! vs->dict)
699     return FALSE;
700
701   return  row < psppire_dict_get_var_cnt (vs->dict);
702 }
703
704 static
705 gboolean always_true ()
706 {
707   return TRUE;
708 }
709
710
711 static gchar *
712 geometry_get_button_label (const GSheetRow *geom, gint unit, gpointer data)
713 {
714   gchar *label = g_strdup_printf (_("%d"), unit);
715
716   return label;
717 }
718
719
720 static void
721 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
722 {
723   iface->get_row_count =     geometry_get_row_count;
724   iface->get_height =        geometry_get_height;
725   iface->set_height =        0;
726   iface->get_visibility =    always_true;
727   iface->get_sensitivity =   geometry_is_sensitive;
728
729   iface->get_button_label = geometry_get_button_label;
730 }
731
732
733