Re-added Data->Insert_Variable menu item
[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
267   g_sheet_model_range_changed (model,
268                                  n, 0, n, n_COLS);
269 }
270
271
272 static void
273 var_delete_callback (GtkWidget *w, gint first, gint n, gpointer data)
274 {
275   GSheetModel *model = G_SHEET_MODEL (data);
276
277   g_sheet_model_rows_deleted (model, first, n);
278 }
279
280
281
282 static void
283 var_insert_callback (GtkWidget *w, gint row, gpointer data)
284 {
285   GSheetModel *model = G_SHEET_MODEL (data);
286
287   g_sheet_model_rows_inserted (model, row, 1);
288 }
289
290
291
292 /**
293  * psppire_var_store_replace_set_dictionary:
294  * @var_store: The variable store
295  * @dict: The dictionary to set
296  *
297  * If a dictionary is already associated with the var-store, then it will be
298  * destroyed.
299  **/
300 void
301 psppire_var_store_set_dictionary (PsppireVarStore *var_store, PsppireDict *dict)
302 {
303   if ( var_store->dict ) g_object_unref (var_store->dict);
304
305   var_store->dict = dict;
306
307   g_signal_connect (dict, "variable-changed", G_CALLBACK (var_change_callback),
308                    var_store);
309
310   g_signal_connect (dict, "variables-deleted", G_CALLBACK (var_delete_callback),
311                    var_store);
312
313   g_signal_connect (dict, "variable-inserted", G_CALLBACK (var_insert_callback),
314                    var_store);
315
316
317   /* The entire model has changed */
318   g_sheet_model_range_changed (G_SHEET_MODEL (var_store), -1, -1, -1, -1);
319 }
320
321 static void
322 psppire_var_store_finalize (GObject *object)
323 {
324   /* must chain up */
325   (* parent_class->finalize) (object);
326 }
327
328 static gchar *
329 psppire_var_store_get_string (const GSheetModel *model, gint row, gint column)
330 {
331   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
332
333   struct variable *pv;
334
335   if ( row >= psppire_dict_get_var_cnt (store->dict))
336     return 0;
337
338   pv = psppire_dict_get_variable (store->dict, row);
339
340   return text_for_column (pv, column, 0);
341 }
342
343
344 /* Clears that part of the variable store, if possible, which corresponds
345    to ROW, COL.
346    Returns true if anything was updated, false otherwise.
347 */
348 static gboolean
349 psppire_var_store_clear (GSheetModel *model,  gint row, gint col)
350 {
351   struct variable *pv ;
352
353   PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
354
355   if ( row >= psppire_dict_get_var_cnt (var_store->dict))
356       return FALSE;
357
358   pv = psppire_var_store_get_var (var_store, row);
359
360   if ( !pv )
361     return FALSE;
362
363   switch (col)
364     {
365     case COL_LABEL:
366       var_set_label (pv, 0);
367       return TRUE;
368       break;
369     }
370
371   return FALSE;
372 }
373
374 /* Attempts to update that part of the variable store which corresponds
375    to ROW, COL with  the value TEXT.
376    Returns true if anything was updated, false otherwise.
377 */
378 static gboolean
379 psppire_var_store_set_string (GSheetModel *model,
380                           const gchar *text, gint row, gint col)
381 {
382   struct variable *pv ;
383
384   PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
385
386   if ( row >= psppire_dict_get_var_cnt (var_store->dict))
387       return FALSE;
388
389   pv = psppire_var_store_get_var (var_store, row);
390
391   if ( !pv )
392     return FALSE;
393
394   switch (col)
395     {
396     case COL_NAME:
397       psppire_dict_rename_var (var_store->dict, pv, text);
398       return TRUE;
399       break;
400     case COL_COLUMNS:
401       if ( ! text) return FALSE;
402       var_set_display_width (pv, atoi (text));
403       return TRUE;
404       break;
405     case COL_WIDTH:
406       {
407         int width = atoi (text);
408         if ( ! text) return FALSE;
409         if ( var_is_alpha (pv))
410             var_set_width (pv, width);
411         else
412           {
413             struct fmt_spec fmt ;
414             fmt = *var_get_write_format (pv);
415             if ( width < fmt_min_output_width (fmt.type)
416                  ||
417                  width > fmt_max_output_width (fmt.type))
418               return FALSE;
419
420             fmt.w = width;
421             fmt.d = MIN (fmt_max_output_decimals (fmt.type, width), fmt.d);
422
423             var_set_both_formats (pv, &fmt);
424           }
425
426         return TRUE;
427       }
428       break;
429     case COL_DECIMALS:
430       {
431         int decimals;
432         struct fmt_spec fmt;
433         if ( ! text) return FALSE;
434         decimals = atoi (text);
435         fmt = *var_get_write_format (pv);
436         if ( decimals >
437              fmt_max_output_decimals (fmt.type,
438                                       fmt.w
439                                       ))
440           return FALSE;
441
442         fmt.d = decimals;
443         var_set_both_formats (pv, &fmt);
444         return TRUE;
445       }
446       break;
447     case COL_LABEL:
448       var_set_label (pv, text);
449       return TRUE;
450       break;
451     case COL_TYPE:
452     case COL_VALUES:
453     case COL_MISSING:
454     case COL_ALIGN:
455     case COL_MEASURE:
456       /* These can be modified only by their respective dialog boxes */
457       return FALSE;
458       break;
459     default:
460       g_assert_not_reached ();
461       return FALSE;
462     }
463
464   return TRUE;
465 }
466
467
468 static  gchar *
469 text_for_column (const struct variable *pv, gint c, GError **err)
470 {
471   static gchar none[] = N_("None");
472
473   static const gchar *const type_label[] =
474     {
475       N_("Numeric"),
476       N_("Comma"),
477       N_("Dot"),
478       N_("Scientific"),
479       N_("Date"),
480       N_("Dollar"),
481       N_("Custom"),
482       N_("String")
483     };
484   enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR,
485         VT_CUSTOM, VT_STRING};
486
487   const struct fmt_spec *write_spec = var_get_write_format (pv);
488
489   switch (c)
490     {
491     case COL_NAME:
492       return pspp_locale_to_utf8 ( var_get_name (pv), -1, err);
493       break;
494     case COL_TYPE:
495       {
496         switch ( write_spec->type )
497           {
498           case FMT_F:
499             return g_locale_to_utf8 (gettext (type_label[VT_NUMERIC]), -1, 0, 0, err);
500             break;
501           case FMT_COMMA:
502             return g_locale_to_utf8 (gettext (type_label[VT_COMMA]), -1, 0, 0, err);
503             break;
504           case FMT_DOT:
505             return g_locale_to_utf8 (gettext (type_label[VT_DOT]), -1, 0, 0, err);
506             break;
507           case FMT_E:
508             return g_locale_to_utf8 (gettext (type_label[VT_SCIENTIFIC]), -1, 0, 0, err);
509             break;
510           case FMT_DATE:
511           case FMT_EDATE:
512           case FMT_SDATE:
513           case FMT_ADATE:
514           case FMT_JDATE:
515           case FMT_QYR:
516           case FMT_MOYR:
517           case FMT_WKYR:
518           case FMT_DATETIME:
519           case FMT_TIME:
520           case FMT_DTIME:
521           case FMT_WKDAY:
522           case FMT_MONTH:
523             return g_locale_to_utf8 (type_label[VT_DATE], -1, 0, 0, err);
524             break;
525           case FMT_DOLLAR:
526             return g_locale_to_utf8 (type_label[VT_DOLLAR], -1, 0, 0, err);
527             break;
528           case FMT_CCA:
529           case FMT_CCB:
530           case FMT_CCC:
531           case FMT_CCD:
532           case FMT_CCE:
533             return g_locale_to_utf8 (gettext (type_label[VT_CUSTOM]), -1, 0, 0, err);
534             break;
535           case FMT_A:
536             return g_locale_to_utf8 (gettext (type_label[VT_STRING]), -1, 0, 0, err);
537             break;
538           default:
539             {
540               char str[FMT_STRING_LEN_MAX + 1];
541               g_warning ("Unknown format: \"%s\"\n",
542                         fmt_to_string (write_spec, str));
543             }
544             break;
545           }
546       }
547       break;
548     case COL_WIDTH:
549       {
550         gchar *s;
551         GString *gstr = g_string_sized_new (10);
552         g_string_printf (gstr, _("%d"), write_spec->w);
553         s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
554         g_string_free (gstr, TRUE);
555         return s;
556       }
557       break;
558     case COL_DECIMALS:
559       {
560         gchar *s;
561         GString *gstr = g_string_sized_new (10);
562         g_string_printf (gstr, _("%d"), write_spec->d);
563         s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
564         g_string_free (gstr, TRUE);
565         return s;
566       }
567       break;
568     case COL_COLUMNS:
569       {
570         gchar *s;
571         GString *gstr = g_string_sized_new (10);
572         g_string_printf (gstr, _("%d"), var_get_display_width (pv));
573         s = g_locale_to_utf8 (gstr->str, gstr->len, 0, 0, err);
574         g_string_free (gstr, TRUE);
575         return s;
576       }
577       break;
578     case COL_LABEL:
579       return pspp_locale_to_utf8 (var_get_label (pv), -1, err);
580       break;
581
582     case COL_MISSING:
583       {
584         gchar *s;
585         const struct missing_values *miss = var_get_missing_values (pv);
586         if ( mv_is_empty (miss))
587           return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
588         else
589           {
590             if ( ! mv_has_range (miss))
591               {
592                 GString *gstr = g_string_sized_new (10);
593                 const int n = mv_n_values (miss);
594                 gchar *mv[4] = {0,0,0,0};
595                 gint i;
596                 for (i = 0 ; i < n; ++i )
597                   {
598                     union value v;
599                     mv_peek_value (miss, &v, i);
600                     mv[i] = value_to_text (v, *write_spec);
601                     if ( i > 0 )
602                       g_string_append (gstr, ", ");
603                     g_string_append (gstr, mv[i]);
604                     g_free (mv[i]);
605                   }
606                 s = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
607                 g_string_free (gstr, TRUE);
608               }
609             else
610               {
611                 GString *gstr = g_string_sized_new (10);
612                 gchar *l, *h;
613                 union value low, high;
614                 mv_peek_range (miss, &low.f, &high.f);
615
616                 l = value_to_text (low, *write_spec);
617                 h = value_to_text (high, *write_spec);
618
619                 g_string_printf (gstr, "%s - %s", l, h);
620                 g_free (l);
621                 g_free (h);
622
623                 if ( mv_has_value (miss))
624                   {
625                     gchar *ss = 0;
626                     union value v;
627                     mv_peek_value (miss, &v, 0);
628
629                     ss = value_to_text (v, *write_spec);
630
631                     g_string_append (gstr, ", ");
632                     g_string_append (gstr, ss);
633                     free (ss);
634                   }
635                 s = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
636                 g_string_free (gstr, TRUE);
637               }
638
639             return s;
640           }
641       }
642       break;
643     case COL_VALUES:
644       {
645         if ( ! var_has_value_labels (pv))
646           return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
647         else
648           {
649             gchar *ss;
650             GString *gstr = g_string_sized_new (10);
651             const struct val_labs *vls = var_get_value_labels (pv);
652             struct val_labs_iterator *ip = 0;
653             struct val_lab *vl = val_labs_first_sorted (vls, &ip);
654
655             g_assert (vl);
656
657             {
658               gchar *const vstr = value_to_text (vl->value, *write_spec);
659
660               g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
661               g_free (vstr);
662             }
663
664             val_labs_done (&ip);
665
666             ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
667             g_string_free (gstr, TRUE);
668             return ss;
669           }
670       }
671       break;
672     case COL_ALIGN:
673       {
674         const gint align = var_get_alignment (pv);
675
676         g_assert (align < n_ALIGNMENTS);
677         return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
678       }
679       break;
680     case COL_MEASURE:
681       {
682         const gint measure = var_get_measure (pv);
683
684         g_assert (measure < n_MEASURES);
685         return g_locale_to_utf8 (gettext (measures[measure]),
686                                  -1, 0, 0, err);
687       }
688       break;
689     }
690   return 0;
691 }
692
693
694
695 /* Return the number of variables */
696 gint
697 psppire_var_store_get_var_cnt (PsppireVarStore  *store)
698 {
699   return psppire_dict_get_var_cnt (store->dict);
700 }
701
702
703 void
704 psppire_var_store_set_font (PsppireVarStore *store, const PangoFontDescription *fd)
705 {
706   g_return_if_fail (store);
707   g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
708
709   store->font_desc = fd;
710
711   g_sheet_model_range_changed (G_SHEET_MODEL (store), -1, -1, -1, -1);
712 }
713
714
715 static gint
716 psppire_var_store_get_row_count (const GSheetModel * model)
717 {
718   gint rows = 0;
719   PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
720
721   if (vs->dict)
722     rows =  psppire_dict_get_var_cnt (vs->dict);
723
724   return rows ;
725 }
726
727 /* Row related funcs */
728
729 static gint
730 geometry_get_row_count (const GSheetRow *geom, gpointer data)
731 {
732   gint rows = 0;
733   PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
734
735   if (vs->dict)
736     rows =  psppire_dict_get_var_cnt (vs->dict);
737
738   return rows + TRAILING_ROWS;
739 }
740
741
742 static gint
743 geometry_get_height (const GSheetRow *geom, gint row, gpointer data)
744 {
745   return 25;
746 }
747
748
749 static gboolean
750 geometry_is_sensitive (const GSheetRow *geom, gint row, gpointer data)
751 {
752   PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
753
754   if ( ! vs->dict)
755     return FALSE;
756
757   return  row < psppire_dict_get_var_cnt (vs->dict);
758 }
759
760 static
761 gboolean always_true ()
762 {
763   return TRUE;
764 }
765
766
767 static gchar *
768 geometry_get_button_label (const GSheetRow *geom, gint unit, gpointer data)
769 {
770   gchar *label = g_strdup_printf (_("%d"), unit);
771
772   return label;
773 }
774
775
776 static void
777 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
778 {
779   iface->get_row_count =     geometry_get_row_count;
780   iface->get_height =        geometry_get_height;
781   iface->set_height =        0;
782   iface->get_visibility =    always_true;
783   iface->get_sensitivity =   geometry_is_sensitive;
784
785   iface->get_button_label = geometry_get_button_label;
786 }