Added the ability to resize string variables from the GUI. Thanks to
[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
65 static gchar *text_for_column (const struct variable *pv, gint c, GError **err);
66
67
68 static void psppire_var_store_sheet_row_init (GSheetRowIface *iface);
69
70
71
72 static GObjectClass *parent_class = NULL;
73
74 GType
75 psppire_var_store_get_type (void)
76 {
77   static GType var_store_type = 0;
78
79   if (!var_store_type)
80     {
81       static const GTypeInfo var_store_info =
82       {
83         sizeof (PsppireVarStoreClass),
84         NULL,           /* base_init */
85         NULL,           /* base_finalize */
86         (GClassInitFunc) psppire_var_store_class_init,
87         NULL,           /* class_finalize */
88         NULL,           /* class_data */
89         sizeof (PsppireVarStore),
90         0,
91         (GInstanceInitFunc) psppire_var_store_init,
92       };
93
94       static const GInterfaceInfo sheet_model_info =
95       {
96         (GInterfaceInitFunc) psppire_var_store_sheet_model_init,
97         NULL,
98         NULL
99       };
100
101       static const GInterfaceInfo sheet_row_info =
102       {
103         (GInterfaceInitFunc) psppire_var_store_sheet_row_init,
104         NULL,
105         NULL
106       };
107
108       var_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireVarStore", &var_store_info, 0);
109
110       g_type_add_interface_static (var_store_type,
111                                    G_TYPE_SHEET_MODEL,
112                                    &sheet_model_info);
113
114       g_type_add_interface_static (var_store_type,
115                                    G_TYPE_SHEET_ROW,
116                                    &sheet_row_info);
117
118
119     }
120
121   return var_store_type;
122 }
123
124 static void
125 psppire_var_store_class_init (PsppireVarStoreClass *class)
126 {
127   GObjectClass *object_class;
128
129   parent_class = g_type_class_peek_parent (class);
130   object_class = (GObjectClass*) class;
131
132   object_class->finalize = psppire_var_store_finalize;
133 }
134
135
136 static void
137 psppire_var_store_init (PsppireVarStore *var_store)
138 {
139   GdkColormap *colormap = gdk_colormap_get_system ();
140
141   g_assert (gdk_color_parse ("gray", &var_store->disabled));
142
143   gdk_colormap_alloc_color (colormap, &var_store->disabled, FALSE, TRUE);
144
145   var_store->dict = 0;
146 }
147
148 static gboolean
149 psppire_var_store_item_editable (PsppireVarStore *var_store, glong row, glong column)
150 {
151   const struct fmt_spec *write_spec ;
152
153   struct variable *pv = psppire_var_store_get_var (var_store, row);
154
155   if ( !pv )
156     return TRUE;
157
158   if ( var_is_alpha (pv) && column == COL_DECIMALS )
159     return FALSE;
160
161   write_spec = var_get_print_format (pv);
162
163   switch ( write_spec->type )
164     {
165     case FMT_DATE:
166     case FMT_EDATE:
167     case FMT_SDATE:
168     case FMT_ADATE:
169     case FMT_JDATE:
170     case FMT_QYR:
171     case FMT_MOYR:
172     case FMT_WKYR:
173     case FMT_DATETIME:
174     case FMT_TIME:
175     case FMT_DTIME:
176     case FMT_WKDAY:
177     case FMT_MONTH:
178       if ( column == COL_DECIMALS || column == COL_WIDTH)
179         return FALSE;
180       break;
181     default:
182       break;
183     }
184
185   return TRUE;
186 }
187
188
189 struct variable *
190 psppire_var_store_get_var (PsppireVarStore *store, glong row)
191 {
192   return psppire_dict_get_variable (store->dict, row);
193 }
194
195 static gboolean
196 psppire_var_store_is_editable (const GSheetModel *model, glong row, glong column)
197 {
198   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
199   return psppire_var_store_item_editable (store, row, column);
200 }
201
202
203 static const GdkColor *
204 psppire_var_store_get_foreground (const GSheetModel *model, glong row, glong column)
205 {
206   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
207
208   if ( ! psppire_var_store_item_editable (store, row, column) )
209     return &store->disabled;
210
211   return NULL;
212 }
213
214
215 const PangoFontDescription *
216 psppire_var_store_get_font_desc (const GSheetModel *model,
217                               glong row, glong column)
218 {
219   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
220
221   return store->font_desc;
222 }
223
224
225
226 static void
227 psppire_var_store_sheet_model_init (GSheetModelIface *iface)
228 {
229   iface->get_row_count = psppire_var_store_get_row_count;
230   iface->free_strings = TRUE;
231   iface->get_string = psppire_var_store_get_string;
232   iface->set_string = psppire_var_store_set_string;
233   iface->clear_datum = psppire_var_store_clear;
234   iface->is_editable = psppire_var_store_is_editable;
235   iface->is_visible = NULL;
236   iface->get_foreground = psppire_var_store_get_foreground;
237   iface->get_background = NULL;
238   iface->get_font_desc = psppire_var_store_get_font_desc;
239   iface->get_cell_border = NULL;
240 }
241
242
243
244 /**
245  * psppire_var_store_new:
246  * @dict: The dictionary for this var_store.
247  *
248  *
249  * Return value: a new #PsppireVarStore
250  **/
251 PsppireVarStore *
252 psppire_var_store_new (PsppireDict *dict)
253 {
254   PsppireVarStore *retval;
255
256   retval = g_object_new (GTK_TYPE_VAR_STORE, NULL);
257
258   psppire_var_store_set_dictionary (retval, dict);
259
260   return retval;
261 }
262
263 static void
264 var_change_callback (GtkWidget *w, gint n, gpointer data)
265 {
266   GSheetModel *model = G_SHEET_MODEL (data);
267
268   g_sheet_model_range_changed (model,
269                                  n, 0, n, n_COLS);
270 }
271
272
273 static void
274 var_delete_callback (GtkWidget *w, gint dict_idx, gint case_idx, gint val_cnt, gpointer data)
275 {
276   GSheetModel *model = G_SHEET_MODEL (data);
277
278   g_sheet_model_rows_deleted (model, dict_idx, 1);
279 }
280
281
282
283 static void
284 var_insert_callback (GtkWidget *w, glong row, gpointer data)
285 {
286   GSheetModel *model = G_SHEET_MODEL (data);
287
288   g_sheet_model_rows_inserted (model, row, 1);
289 }
290
291
292
293 /**
294  * psppire_var_store_replace_set_dictionary:
295  * @var_store: The variable store
296  * @dict: The dictionary to set
297  *
298  * If a dictionary is already associated with the var-store, then it will be
299  * destroyed.
300  **/
301 void
302 psppire_var_store_set_dictionary (PsppireVarStore *var_store, PsppireDict *dict)
303 {
304   if ( var_store->dict ) g_object_unref (var_store->dict);
305
306   var_store->dict = dict;
307
308   g_signal_connect (dict, "variable-changed", G_CALLBACK (var_change_callback),
309                    var_store);
310
311   g_signal_connect (dict, "variable-deleted", G_CALLBACK (var_delete_callback),
312                    var_store);
313
314   g_signal_connect (dict, "variable-inserted", G_CALLBACK (var_insert_callback),
315                    var_store);
316
317
318   /* The entire model has changed */
319   g_sheet_model_range_changed (G_SHEET_MODEL (var_store), -1, -1, -1, -1);
320 }
321
322 static void
323 psppire_var_store_finalize (GObject *object)
324 {
325   /* must chain up */
326   (* parent_class->finalize) (object);
327 }
328
329 static gchar *
330 psppire_var_store_get_string (const GSheetModel *model, glong row, glong column)
331 {
332   PsppireVarStore *store = PSPPIRE_VAR_STORE (model);
333
334   struct variable *pv;
335
336   if ( row >= psppire_dict_get_var_cnt (store->dict))
337     return 0;
338
339   pv = psppire_dict_get_variable (store->dict, row);
340
341   return text_for_column (pv, column, 0);
342 }
343
344
345 /* Clears that part of the variable store, if possible, which corresponds
346    to ROW, COL.
347    Returns true if anything was updated, false otherwise.
348 */
349 static gboolean
350 psppire_var_store_clear (GSheetModel *model,  glong row, glong col)
351 {
352   struct variable *pv ;
353
354   PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
355
356   if ( row >= psppire_dict_get_var_cnt (var_store->dict))
357       return FALSE;
358
359   pv = psppire_var_store_get_var (var_store, row);
360
361   if ( !pv )
362     return FALSE;
363
364   switch (col)
365     {
366     case COL_LABEL:
367       var_set_label (pv, 0);
368       return TRUE;
369       break;
370     }
371
372   return FALSE;
373 }
374
375 /* Attempts to update that part of the variable store which corresponds
376    to ROW, COL with  the value TEXT.
377    Returns true if anything was updated, false otherwise.
378 */
379 static gboolean
380 psppire_var_store_set_string (GSheetModel *model,
381                           const gchar *text, glong row, glong col)
382 {
383   struct variable *pv ;
384
385   PsppireVarStore *var_store = PSPPIRE_VAR_STORE (model);
386
387   if ( row >= psppire_dict_get_var_cnt (var_store->dict))
388       return FALSE;
389
390   pv = psppire_var_store_get_var (var_store, row);
391
392   if ( !pv )
393     return FALSE;
394
395   switch (col)
396     {
397     case COL_NAME:
398       return psppire_dict_rename_var (var_store->dict, pv, text);
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 const static gchar none[] = N_("None");
469
470 static  gchar *
471 text_for_column (const struct variable *pv, gint c, GError **err)
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         return missing_values_to_string (pv, err);
585       }
586       break;
587     case COL_VALUES:
588       {
589         if ( ! var_has_value_labels (pv))
590           return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
591         else
592           {
593             gchar *ss;
594             GString *gstr = g_string_sized_new (10);
595             const struct val_labs *vls = var_get_value_labels (pv);
596             struct val_labs_iterator *ip = 0;
597             struct val_lab *vl = val_labs_first_sorted (vls, &ip);
598
599             g_assert (vl);
600
601             {
602               gchar *const vstr = value_to_text (vl->value, *write_spec);
603
604               g_string_printf (gstr, "{%s,\"%s\"}_", vstr, vl->label);
605               g_free (vstr);
606             }
607
608             val_labs_done (&ip);
609
610             ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
611             g_string_free (gstr, TRUE);
612             return ss;
613           }
614       }
615       break;
616     case COL_ALIGN:
617       {
618         const gint align = var_get_alignment (pv);
619
620         g_assert (align < n_ALIGNMENTS);
621         return g_locale_to_utf8 (gettext (alignments[align]), -1, 0, 0, err);
622       }
623       break;
624     case COL_MEASURE:
625       {
626         return measure_to_string (pv, err);
627       }
628       break;
629     }
630   return 0;
631 }
632
633
634
635 /* Return the number of variables */
636 gint
637 psppire_var_store_get_var_cnt (PsppireVarStore  *store)
638 {
639   return psppire_dict_get_var_cnt (store->dict);
640 }
641
642
643 void
644 psppire_var_store_set_font (PsppireVarStore *store, const PangoFontDescription *fd)
645 {
646   g_return_if_fail (store);
647   g_return_if_fail (PSPPIRE_IS_VAR_STORE (store));
648
649   store->font_desc = fd;
650
651   g_sheet_model_range_changed (G_SHEET_MODEL (store), -1, -1, -1, -1);
652 }
653
654
655 static glong
656 psppire_var_store_get_row_count (const GSheetModel * model)
657 {
658   gint rows = 0;
659   PsppireVarStore *vs = PSPPIRE_VAR_STORE (model);
660
661   if (vs->dict)
662     rows =  psppire_dict_get_var_cnt (vs->dict);
663
664   return rows ;
665 }
666
667 /* Row related funcs */
668
669 static glong
670 geometry_get_row_count (const GSheetRow *geom, gpointer data)
671 {
672   gint rows = 0;
673   PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
674
675   if (vs->dict)
676     rows =  psppire_dict_get_var_cnt (vs->dict);
677
678   return rows + TRAILING_ROWS;
679 }
680
681
682 static gint
683 geometry_get_height (const GSheetRow *geom, glong row, gpointer data)
684 {
685   return 25;
686 }
687
688
689 static gboolean
690 geometry_is_sensitive (const GSheetRow *geom, glong row, gpointer data)
691 {
692   PsppireVarStore *vs = PSPPIRE_VAR_STORE (geom);
693
694   if ( ! vs->dict)
695     return FALSE;
696
697   return  row < psppire_dict_get_var_cnt (vs->dict);
698 }
699
700 static
701 gboolean always_true ()
702 {
703   return TRUE;
704 }
705
706
707 static gchar *
708 geometry_get_button_label (const GSheetRow *geom, glong unit, gpointer data)
709 {
710   gchar *label = g_strdup_printf (_("%ld"), unit);
711
712   return label;
713 }
714
715 static void
716 psppire_var_store_sheet_row_init (GSheetRowIface *iface)
717 {
718   iface->get_row_count =     geometry_get_row_count;
719   iface->get_height =        geometry_get_height;
720   iface->set_height =        0;
721   iface->get_visibility =    always_true;
722   iface->get_sensitivity =   geometry_is_sensitive;
723
724   iface->get_button_label = geometry_get_button_label;
725 }
726
727
728