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