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