Internationalisation.
[pspp-builds.git] / src / ui / gui / psppire-data-store.c
1 /* psppire-data-store.c
2  
3    PSPPIRE --- A Graphical User Interface for PSPP
4    Copyright (C) 2006  Free Software Foundation
5    Written by John Darrington
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA. */
21
22 #include <config.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <gettext.h>
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) msgid
28
29
30
31 #include <gtksheet/gtksheet.h>
32 #include <gtksheet/gsheetmodel.h>
33 #include <gtksheet/gsheet-column-iface.h>
34
35 #include "psppire-variable.h"
36 #include "psppire-data-store.h"
37 #include "helper.h"
38
39 #include <data/dictionary.h>
40 #include <data/missing-values.h>
41 #include <data/value-labels.h>
42 #include <data/data-in.h>
43
44 #include <data/file-handle-def.h>
45 #include <data/sys-file-writer.h>
46
47
48
49 static void psppire_data_store_init            (PsppireDataStore      *data_store);
50 static void psppire_data_store_class_init      (PsppireDataStoreClass *class);
51 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
52 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
53 static void psppire_data_store_finalize        (GObject           *object);
54
55 static const gchar *psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column);
56
57 static gboolean psppire_data_store_set_string(GSheetModel *model, 
58                                           const gchar *text, gint row, gint column);
59
60 static gboolean psppire_data_store_clear_datum(GSheetModel *model, 
61                                           gint row, gint column);
62
63
64 #define MIN_COLUMNS 10
65
66
67 static GObjectClass *parent_class = NULL;
68
69 inline GType
70 psppire_data_store_get_type (void)
71 {
72   static GType data_store_type = 0;
73
74   if (!data_store_type)
75     {
76       static const GTypeInfo data_store_info =
77       {
78         sizeof (PsppireDataStoreClass),
79         NULL,           /* base_init */
80         NULL,           /* base_finalize */
81         (GClassInitFunc) psppire_data_store_class_init,
82         NULL,           /* class_finalize */
83         NULL,           /* class_data */
84         sizeof (PsppireDataStore),
85         0,
86         (GInstanceInitFunc) psppire_data_store_init,
87       };
88
89       static const GInterfaceInfo sheet_model_info =
90       {
91         (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
92         NULL,
93         NULL
94       };
95
96       static const GInterfaceInfo sheet_column_info =
97       {
98         (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
99         NULL,
100         NULL
101       };
102
103
104
105       data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
106                                                 &data_store_info, 0);
107
108       g_type_add_interface_static (data_store_type,
109                                    G_TYPE_SHEET_MODEL,
110                                    &sheet_model_info);
111
112       g_type_add_interface_static (data_store_type,
113                                    G_TYPE_SHEET_COLUMN,
114                                    &sheet_column_info);
115
116     }
117
118   return data_store_type;
119 }
120
121 static void
122 psppire_data_store_class_init (PsppireDataStoreClass *class)
123 {
124   GObjectClass *object_class;
125
126   parent_class = g_type_class_peek_parent (class);
127   object_class = (GObjectClass*) class;
128
129   object_class->finalize = psppire_data_store_finalize;
130 }
131
132
133
134 static gint
135 psppire_data_store_get_var_count (const GSheetModel *model)
136 {
137   const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
138   
139   return psppire_dict_get_var_cnt(store->dict);
140 }
141
142 static gint
143 psppire_data_store_get_case_count (const GSheetModel *model)
144 {
145   const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
146
147   return psppire_case_array_get_n_cases(store->cases);
148 }
149
150
151 static void
152 psppire_data_store_init (PsppireDataStore *data_store)
153 {
154   data_store->dict = 0;
155   data_store->cases = 0;
156 }
157
158 const PangoFontDescription *
159 psppire_data_store_get_font_desc(GSheetModel *model,
160                               gint row, gint column)
161 {
162   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
163   
164   return store->font_desc;
165 }
166
167
168 static void
169 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
170 {
171   iface->free_strings = TRUE;
172   iface->get_string = psppire_data_store_get_string;
173   iface->set_string = psppire_data_store_set_string;
174   iface->clear_datum = psppire_data_store_clear_datum;
175   iface->is_editable = NULL;
176   iface->is_visible = NULL;
177   iface->get_foreground = NULL;
178   iface->get_background = NULL;
179   iface->get_font_desc = psppire_data_store_get_font_desc;
180   iface->get_cell_border = NULL;
181   iface->get_column_count = psppire_data_store_get_var_count;
182   iface->get_row_count = psppire_data_store_get_case_count;
183 }
184
185 static
186 gboolean always_true()
187 {
188   return TRUE;
189 }
190
191
192 static void
193 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
194 {
195   PsppireDataStore *store  ;
196
197   g_return_if_fail (data);
198
199   store  = PSPPIRE_DATA_STORE(data);
200
201   g_assert(first >= 0);
202
203   g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
204 }
205
206
207 static void
208 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
209 {
210   PsppireDataStore *store  ;
211
212   g_return_if_fail (data);
213
214   store  = PSPPIRE_DATA_STORE(data);
215   
216   g_sheet_model_range_changed (G_SHEET_MODEL(store),
217                                casenum, -1,
218                                psppire_case_array_get_n_cases(store->cases),
219                                -1);
220 }
221
222
223 static void
224 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
225 {
226   PsppireDataStore *store  ;
227   g_return_if_fail (data);
228
229   store  = PSPPIRE_DATA_STORE(data);
230   
231   g_sheet_model_range_changed (G_SHEET_MODEL(store),
232                                  casenum, -1,
233                                  casenum, -1);
234
235 }
236
237
238 static void
239 delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
240 {
241   PsppireDataStore *store ;
242
243   g_return_if_fail (data);
244
245   store  = PSPPIRE_DATA_STORE(data);
246
247   g_sheet_column_columns_deleted(G_SHEET_COLUMN(store),
248                                    var_num, n_vars);
249
250   g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
251 }
252
253
254 static void
255 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
256 {
257   PsppireDataStore *store;
258
259   g_return_if_fail (data);
260
261   store  = PSPPIRE_DATA_STORE(data);
262   
263   /* 
264   g_sheet_model_range_changed (G_SHEET_MODEL(store),
265                                  casenum, -1,
266                                  psppire_case_array_get_n_cases(store->cases),
267                                  -1);
268   */
269
270   psppire_case_array_resize(store->cases, 
271                          dict_get_next_value_idx (store->dict->dict));
272
273   g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
274 }
275
276
277
278
279 /**
280  * psppire_data_store_new:
281  * @dict: The dictionary for this data_store.
282  *
283  *
284  * Return value: a new #PsppireDataStore
285  **/
286 PsppireDataStore *
287 psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
288 {
289   PsppireDataStore *retval;
290
291   retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
292
293   retval->cases = cases;
294   g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback), 
295                    retval);
296
297   g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback), 
298                    retval);
299
300
301   g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback), 
302                    retval);
303
304   psppire_data_store_set_dictionary(retval, dict);
305
306
307   return retval;
308 }
309
310
311
312 /**
313  * psppire_data_store_replace_set_dictionary:
314  * @data_store: The variable store
315  * @dict: The dictionary to set
316  *
317  * If a dictionary is already associated with the data-store, then it will be
318  * destroyed.
319  **/
320 void
321 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
322 {
323 #if 0
324   if ( data_store->dict ) g_object_unref(data_store->dict);
325 #endif
326
327   data_store->dict = dict;
328
329   psppire_case_array_resize(data_store->cases, 
330                          dict_get_next_value_idx (data_store->dict->dict));
331
332
333   g_signal_connect(dict, "variable-inserted", 
334                    G_CALLBACK(insert_variable_callback), 
335                    data_store);
336
337   g_signal_connect(dict, "variables-deleted", 
338                    G_CALLBACK(delete_variables_callback), 
339                    data_store);
340
341   /* The entire model has changed */
342   g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
343 }
344
345 static void
346 psppire_data_store_finalize (GObject *object)
347 {
348
349   /* must chain up */
350   (* parent_class->finalize) (object);
351 }
352
353
354 static const gchar *
355 psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
356 {
357   const char *text;
358   const struct fmt_spec *fp ;
359   const struct PsppireVariable *pv ;
360   const union value *v ;
361   GString *s;
362   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
363
364   g_return_val_if_fail(store->dict, NULL);
365   g_return_val_if_fail(store->cases, NULL);
366
367   if (column >= psppire_dict_get_var_cnt(store->dict))
368     return NULL;
369
370   if ( row >= psppire_case_array_get_n_cases(store->cases))
371     return NULL;
372
373
374   pv = psppire_dict_get_variable(store->dict, column);
375
376   v =  psppire_case_array_get_value(store->cases, row, 
377                               psppire_variable_get_index(pv));
378
379   if ( store->show_labels) 
380     {
381       const struct val_labs * vl = psppire_variable_get_value_labels(pv);
382
383       const gchar *label;
384       if ( (label = val_labs_find(vl, *v)) )
385         {
386           return pspp_locale_to_utf8(label, -1, 0);
387         }
388     }
389
390   fp = psppire_variable_get_write_spec(pv);
391
392   s = g_string_sized_new (fp->w + 1);
393   g_string_set_size(s, fp->w);
394   
395   memset(s->str, 0, fp->w);
396
397   g_assert(fp->w == s->len);
398     
399   /* Converts binary value V into printable form in the exactly
400      FP->W character in buffer S according to format specification
401      FP.  No null terminator is appended to the buffer.  */
402   data_out (s->str, fp, v);
403
404   text = pspp_locale_to_utf8(s->str, fp->w, 0);
405   g_string_free(s, TRUE);
406
407   return text;
408 }
409
410
411 static gboolean
412 set_null_string_value(union value *val, gpointer data)
413 {
414   strcpy(val->s, "");
415   return TRUE;
416 }
417
418 static gboolean
419 set_sysmis_value(union value *val, gpointer data)
420 {
421   val->f = SYSMIS;
422   return TRUE;
423 }
424
425
426 static gboolean 
427 psppire_data_store_clear_datum(GSheetModel *model, 
428                                           gint row, gint col)
429
430 {
431   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
432
433   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
434
435   const gint index = psppire_variable_get_index(pv) ;
436
437   if ( psppire_variable_get_type(pv) == NUMERIC) 
438     psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
439   else
440     psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
441   return TRUE;
442 }
443
444
445 static gboolean
446 fillit(union value *val, gpointer data)
447 {
448   struct data_in *d_in = data;
449
450   d_in->v = val;
451
452   if ( ! data_in(d_in) ) 
453     {
454       g_warning("Cant encode string\n");
455       return FALSE;
456     }
457
458   return TRUE;
459 }
460
461
462 /* Attempts to update that part of the variable store which corresponds 
463    to ROW, COL with  the value TEXT.
464    Returns true if anything was updated, false otherwise.
465 */
466 static gboolean 
467 psppire_data_store_set_string(GSheetModel *model, 
468                           const gchar *text, gint row, gint col)
469 {
470   gint r;
471   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
472
473   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
474   g_return_val_if_fail(pv, FALSE);
475
476   for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r ) 
477     {
478       gint c;
479       psppire_case_array_insert_case(store->cases, r, 0, 0);
480
481       for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c ) 
482         psppire_data_store_clear_datum(model, r, c);
483     }
484
485   {
486     const gint index = psppire_variable_get_index(pv);
487
488     struct data_in d_in;
489     d_in.s = text;
490     d_in.e = text + strlen(text);
491     d_in.v = 0;
492     d_in.f1 = d_in.f2 = 0;
493     d_in.format = * psppire_variable_get_write_spec(pv);
494     d_in.flags = 0;
495
496     psppire_case_array_set_value(store->cases, row, index, fillit, &d_in);
497   }
498
499   return TRUE;
500 }
501
502
503 void
504 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
505 {
506   g_return_if_fail (store);
507   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
508
509   store->font_desc = fd;
510   g_sheet_model_range_changed (G_SHEET_MODEL(store),
511                                  -1, -1, -1, -1);
512 }
513
514
515 void
516 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
517 {
518   g_return_if_fail (store);
519   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
520
521   store->show_labels = show_labels;
522
523   g_sheet_model_range_changed (G_SHEET_MODEL(store),
524                                  -1, -1, -1, -1);
525 }
526
527
528
529 static gboolean 
530 write_case(const struct ccase *cc, 
531            gpointer aux)
532 {
533   struct sfm_writer *writer = aux;
534
535   if ( ! sfm_write_case(writer, cc) )
536     return FALSE;
537
538
539   return TRUE;
540 }
541
542 void
543 psppire_data_store_create_system_file(PsppireDataStore *store,
544                               struct file_handle *handle)
545 {
546   const struct sfm_write_options wo = {
547     true, /* writeable */
548     false, /* dont compress */
549     3 /* version */
550   }; 
551
552   struct sfm_writer *writer ;
553
554   g_assert(handle);
555
556   writer = sfm_open_writer(handle, store->dict->dict, wo);
557
558   if ( ! writer) 
559     return;
560
561   psppire_case_array_iterate_case(store->cases, write_case, writer);
562
563   sfm_close_writer(writer);
564 }
565
566
567
568 /* Column related funcs */
569
570 static gint
571 geometry_get_column_count(const GSheetColumn *geom)
572 {
573   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
574
575   return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
576 }
577
578 /* Return the width that an  'M' character would occupy when typeset at
579    row, col */
580 static guint 
581 M_width(const GtkSheet *sheet, gint row, gint col)
582 {
583   GtkSheetCellAttr attributes;
584   PangoRectangle rect;
585   /* FIXME: make this a member of the data store */
586   static PangoLayout *layout = 0;
587
588   gtk_sheet_get_attributes(sheet, row, col, &attributes);
589
590   if (! layout ) 
591     layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
592
593   g_assert(layout);
594   
595   pango_layout_set_font_description (layout, 
596                                      attributes.font_desc);
597
598   pango_layout_get_extents (layout, NULL, &rect);
599
600 #if 0
601   g_object_unref(G_OBJECT(layout));
602 #endif
603
604   return PANGO_PIXELS(rect.width);
605 }
606
607
608 /* Return the number of pixels corresponding to a column of 
609    WIDTH characters */
610 static inline guint 
611 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
612 {
613   return (M_width(sheet, 0, column) * width);
614 }
615
616
617 static gint
618 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
619 {
620   const struct PsppireVariable *pv ;
621   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
622
623   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
624     return 75;
625
626   /* FIXME: We can optimise this by caching the widths until they're resized */
627   pv = psppire_dict_get_variable(ds->dict, unit);
628
629   return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
630 }
631
632
633
634
635 static void
636 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
637 {
638   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
639
640   struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
641
642   psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
643 }
644
645
646
647 static GtkJustification
648 geometry_get_justification(const GSheetColumn *geom, gint unit)
649 {
650   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
651   const struct PsppireVariable *pv ;
652
653
654   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
655     return GTK_JUSTIFY_LEFT;
656
657   pv = psppire_dict_get_variable(ds->dict, unit);
658
659   /* Kludge: Happily GtkJustification is defined similarly
660      to enum alignment from pspp/variable.h */
661   return psppire_variable_get_alignment(pv);
662 }
663
664
665 static const gchar null_var_name[]=N_("var");
666  
667 static const gchar *
668 geometry_get_button_label(const GSheetColumn *geom, gint unit)
669 {
670   const gchar *text;
671   struct PsppireVariable *pv ;
672   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
673
674   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
675     return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
676
677   pv = psppire_dict_get_variable(ds->dict, unit);
678
679   text =  pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
680
681   return text;
682 }
683
684
685 static gboolean
686 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
687 {
688   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
689
690
691   return (unit < psppire_dict_get_var_cnt(ds->dict));
692 }
693
694
695 static void
696 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
697 {
698   iface->get_column_count = geometry_get_column_count;
699   iface->get_width = geometry_get_width;
700   iface->set_width = geometry_set_width;
701   iface->get_visibility = always_true;
702   iface->get_sensitivity = geometry_get_sensitivity;
703   iface->get_justification = geometry_get_justification;
704
705   iface->get_button_label = geometry_get_button_label;
706 }