Implemented data-store using a casefile instead of an array of cases.
[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 #include <data/casefile.h>
30 #include <data/case.h>
31
32 #include <gtksheet/gtksheet.h>
33 #include <gtksheet/gsheetmodel.h>
34 #include <gtksheet/gsheet-column-iface.h>
35
36 #include "psppire-variable.h"
37 #include "psppire-data-store.h"
38 #include "helper.h"
39
40 #include <data/dictionary.h>
41 #include <data/missing-values.h>
42 #include <data/value-labels.h>
43 #include <data/data-in.h>
44
45 #include <data/file-handle-def.h>
46 #include <data/sys-file-writer.h>
47
48
49
50 static void psppire_data_store_init            (PsppireDataStore      *data_store);
51 static void psppire_data_store_class_init      (PsppireDataStoreClass *class);
52 static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
53 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
54 static void psppire_data_store_sheet_row_init (GSheetRowIface *iface);
55
56 static void psppire_data_store_finalize        (GObject           *object);
57
58 static gchar *psppire_data_store_get_string(const GSheetModel *sheet_model, gint row, gint column);
59
60 static gboolean psppire_data_store_set_string(GSheetModel *model, 
61                                           const gchar *text, gint row, gint column);
62
63 static gboolean psppire_data_store_clear_datum(GSheetModel *model, 
64                                           gint row, gint column);
65
66
67 #define MIN_COLUMNS 10
68
69 #define TRAILING_ROWS 10
70
71 static GObjectClass *parent_class = NULL;
72
73 inline GType
74 psppire_data_store_get_type (void)
75 {
76   static GType data_store_type = 0;
77
78   if (!data_store_type)
79     {
80       static const GTypeInfo data_store_info =
81       {
82         sizeof (PsppireDataStoreClass),
83         NULL,           /* base_init */
84         NULL,           /* base_finalize */
85         (GClassInitFunc) psppire_data_store_class_init,
86         NULL,           /* class_finalize */
87         NULL,           /* class_data */
88         sizeof (PsppireDataStore),
89         0,
90         (GInstanceInitFunc) psppire_data_store_init,
91       };
92
93       static const GInterfaceInfo sheet_model_info =
94       {
95         (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
96         NULL,
97         NULL
98       };
99
100       static const GInterfaceInfo sheet_column_info =
101       {
102         (GInterfaceInitFunc) psppire_data_store_sheet_column_init,
103         NULL,
104         NULL
105       };
106
107       static const GInterfaceInfo sheet_row_info =
108       {
109         (GInterfaceInitFunc) psppire_data_store_sheet_row_init,
110         NULL,
111         NULL
112       };
113
114
115       data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
116                                                 &data_store_info, 0);
117
118       g_type_add_interface_static (data_store_type,
119                                    G_TYPE_SHEET_MODEL,
120                                    &sheet_model_info);
121
122       g_type_add_interface_static (data_store_type,
123                                    G_TYPE_SHEET_COLUMN,
124                                    &sheet_column_info);
125
126       g_type_add_interface_static (data_store_type,
127                                    G_TYPE_SHEET_ROW,
128                                    &sheet_row_info);
129     }
130
131   return data_store_type;
132 }
133
134 static void
135 psppire_data_store_class_init (PsppireDataStoreClass *class)
136 {
137   GObjectClass *object_class;
138
139   parent_class = g_type_class_peek_parent (class);
140   object_class = (GObjectClass*) class;
141
142   object_class->finalize = psppire_data_store_finalize;
143 }
144
145
146
147 static gint
148 psppire_data_store_get_var_count (const GSheetModel *model)
149 {
150   const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
151   
152   return psppire_dict_get_var_cnt(store->dict);
153 }
154
155 static gint
156 psppire_data_store_get_case_count (const GSheetModel *model)
157 {
158   const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
159
160   return psppire_case_file_get_case_count(store->case_file);
161 }
162
163
164 static void
165 psppire_data_store_init (PsppireDataStore *data_store)
166 {
167   data_store->dict = 0;
168   data_store->case_file = 0;
169 }
170
171 const PangoFontDescription *
172 psppire_data_store_get_font_desc(const GSheetModel *model,
173                               gint row, gint column)
174 {
175   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
176   
177   return store->font_desc;
178 }
179
180
181 static void
182 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
183 {
184   iface->free_strings = TRUE;
185   iface->get_string = psppire_data_store_get_string;
186   iface->set_string = psppire_data_store_set_string;
187   iface->clear_datum = psppire_data_store_clear_datum;
188   iface->is_editable = NULL;
189   iface->is_visible = NULL;
190   iface->get_foreground = NULL;
191   iface->get_background = NULL;
192   iface->get_font_desc = psppire_data_store_get_font_desc;
193   iface->get_cell_border = NULL;
194   iface->get_column_count = psppire_data_store_get_var_count;
195   iface->get_row_count = psppire_data_store_get_case_count;
196 }
197
198 static
199 gboolean always_true()
200 {
201   return TRUE;
202 }
203
204
205 static void
206 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
207 {
208   PsppireDataStore *store  ;
209
210   g_return_if_fail (data);
211
212   store  = PSPPIRE_DATA_STORE(data);
213
214   g_assert(first >= 0);
215
216   g_sheet_model_rows_deleted (G_SHEET_MODEL(store), first, n_cases);
217 }
218
219
220 static void
221 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
222 {
223   PsppireDataStore *store  ;
224
225   g_return_if_fail (data);
226
227   store  = PSPPIRE_DATA_STORE(data);
228   
229   g_sheet_model_range_changed (G_SHEET_MODEL(store),
230                                casenum, -1,
231                                psppire_case_file_get_case_count(store->case_file),
232                                -1);
233
234   g_sheet_model_rows_inserted (G_SHEET_MODEL(store), casenum, 1);
235 }
236
237
238 static void
239 changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
240 {
241   PsppireDataStore *store  ;
242   g_return_if_fail (data);
243
244   store  = PSPPIRE_DATA_STORE(data);
245   
246   g_sheet_model_range_changed (G_SHEET_MODEL(store),
247                                  casenum, -1,
248                                  casenum, -1);
249 }
250
251
252 static void
253 delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
254 {
255   PsppireDataStore *store ;
256
257   g_return_if_fail (data);
258
259   store  = PSPPIRE_DATA_STORE(data);
260
261   g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
262
263   g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
264                                    var_num, -1);
265 }
266
267 static void
268 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
269 {
270   PsppireDataStore *store;
271
272   g_return_if_fail (data);
273
274   store  = PSPPIRE_DATA_STORE(data);
275   
276   /* 
277   g_sheet_model_range_changed (G_SHEET_MODEL(store),
278                                  casenum, -1,
279                                  psppire_case_array_get_n_cases(store->cases),
280                                  -1);
281   */
282
283 #if 0
284   psppire_case_array_resize(store->cases, 
285                          dict_get_next_value_idx (store->dict->dict));
286 #endif
287
288   g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
289                                   var_num, 1);
290
291
292   g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
293 }
294
295
296
297
298 /**
299  * psppire_data_store_new:
300  * @dict: The dictionary for this data_store.
301  *
302  *
303  * Return value: a new #PsppireDataStore
304  **/
305 PsppireDataStore *
306 psppire_data_store_new (PsppireDict *dict)
307 {
308   PsppireDataStore *retval;
309
310   retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
311
312   psppire_data_store_set_dictionary(retval, dict);
313
314
315   return retval;
316 }
317
318
319
320 /**
321  * psppire_data_store_replace_set_dictionary:
322  * @data_store: The variable store
323  * @dict: The dictionary to set
324  *
325  * If a dictionary is already associated with the data-store, then it will be
326  * destroyed.
327  **/
328 void
329 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
330 {
331   gint var_cnt = psppire_dict_get_next_value_idx(dict);
332 #if 0
333   if ( data_store->dict ) g_object_unref(data_store->dict);
334 #endif
335
336   data_store->dict = dict;
337
338   if ( data_store->case_file)
339     {
340       g_object_unref(data_store->case_file);
341       data_store->case_file = 0;
342     }
343
344   data_store->case_file = psppire_case_file_new(var_cnt);
345
346   g_signal_connect(data_store->case_file, "cases-deleted", 
347                    G_CALLBACK(delete_cases_callback), 
348                    data_store);
349
350   g_signal_connect(data_store->case_file, "case-inserted", 
351                    G_CALLBACK(insert_case_callback), 
352                    data_store);
353
354
355   g_signal_connect(data_store->case_file, "case-changed", 
356                    G_CALLBACK(changed_case_callback), 
357                    data_store);
358
359
360   g_signal_connect(dict, "variable-inserted", 
361                    G_CALLBACK(insert_variable_callback), 
362                    data_store);
363
364   g_signal_connect(dict, "variables-deleted", 
365                    G_CALLBACK(delete_variables_callback), 
366                    data_store);
367
368   /* The entire model has changed */
369   g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
370   
371   g_sheet_column_columns_changed(G_SHEET_COLUMN(data_store), 0, -1);
372 }
373
374 static void
375 psppire_data_store_finalize (GObject *object)
376 {
377
378   /* must chain up */
379   (* parent_class->finalize) (object);
380 }
381
382
383 static gchar *
384 psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
385 {
386   gint idx;
387   char *text;
388   const struct fmt_spec *fp ;
389   const struct PsppireVariable *pv ;
390   const union value *v ;
391   GString *s;
392   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
393
394   g_return_val_if_fail(store->dict, NULL);
395   g_return_val_if_fail(store->case_file, NULL);
396
397   if (column >= psppire_dict_get_var_cnt(store->dict))
398     return NULL;
399
400   if ( row >= psppire_case_file_get_case_count(store->case_file))
401     return NULL;
402
403   pv = psppire_dict_get_variable(store->dict, column);
404
405   idx = psppire_variable_get_index(pv);
406
407   v = psppire_case_file_get_value(store->case_file, row, idx);
408
409   if ( store->show_labels) 
410     {
411       const struct val_labs * vl = psppire_variable_get_value_labels(pv);
412
413       const gchar *label;
414       if ( (label = val_labs_find(vl, *v)) )
415         {
416           return pspp_locale_to_utf8(label, -1, 0);
417         }
418     }
419
420   fp = psppire_variable_get_write_spec(pv);
421
422   s = g_string_sized_new (fp->w + 1);
423   g_string_set_size(s, fp->w);
424   
425   memset(s->str, 0, fp->w);
426
427   g_assert(fp->w == s->len);
428     
429   /* Converts binary value V into printable form in the exactly
430      FP->W character in buffer S according to format specification
431      FP.  No null terminator is appended to the buffer.  */
432   data_out (s->str, fp, v);
433
434   text = pspp_locale_to_utf8(s->str, fp->w, 0);
435   g_string_free(s, TRUE);
436
437   return text;
438 }
439
440
441 static gboolean 
442 psppire_data_store_clear_datum(GSheetModel *model, 
443                                           gint row, gint col)
444
445 {
446   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
447
448   union value v;
449   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
450
451   const gint index = psppire_variable_get_index(pv) ;
452
453   if ( psppire_variable_get_type(pv) == NUMERIC) 
454     v.f = SYSMIS;
455   else
456     memcpy(v.s, "", MAX_SHORT_STRING);
457
458   psppire_case_file_set_value(store->case_file, row, index, &v);
459
460   return TRUE;
461 }
462
463
464 /* Attempts to update that part of the variable store which corresponds 
465    to ROW, COL with  the value TEXT.
466    Returns true if anything was updated, false otherwise.
467 */
468 static gboolean 
469 psppire_data_store_set_string(GSheetModel *model, 
470                           const gchar *text, gint row, gint col)
471 {
472   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
473
474   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
475   g_return_val_if_fail(pv, FALSE);
476
477 #if 0
478   /* Allow the user to insert a lot of blank cases, simply by skipping rows */
479   for(r = psppire_case_file_get_case_count(store->case_file); r <= row ; ++r) 
480     {
481
482       gint c;
483
484       psppire_case_array_insert_case(store->cases, r, 0, 0);
485
486
487       for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c ) 
488         psppire_data_store_clear_datum(model, r, c);
489     }
490 #endif
491
492   {
493     const gint index = psppire_variable_get_index(pv);
494
495     struct data_in d_in;
496     d_in.s = text;
497     d_in.e = text + strlen(text);
498     d_in.v = 0;
499     d_in.f1 = d_in.f2 = 0;
500     d_in.format = * psppire_variable_get_write_spec(pv);
501     d_in.flags = 0;
502
503     /* 
504     if ( ! data_in(&d_in) ) 
505       {
506         g_warning("Cannot encode string");
507         return FALSE;
508       }
509     */
510
511     psppire_case_file_set_value(store->case_file, row, index, &d_in) ;
512   }
513
514   return TRUE;
515 }
516
517
518 void
519 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
520 {
521   g_return_if_fail (store);
522   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
523
524   store->font_desc = fd;
525   g_sheet_model_range_changed (G_SHEET_MODEL(store),
526                                  -1, -1, -1, -1);
527 }
528
529
530 void
531 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
532 {
533   g_return_if_fail (store);
534   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
535
536   store->show_labels = show_labels;
537
538   g_sheet_model_range_changed (G_SHEET_MODEL(store),
539                                  -1, -1, -1, -1);
540 }
541
542
543
544 static gboolean 
545 write_case(const struct ccase *cc, 
546            gpointer aux)
547 {
548   struct sfm_writer *writer = aux;
549
550   if ( ! sfm_write_case(writer, cc) )
551     return FALSE;
552
553
554   return TRUE;
555 }
556
557 void
558 psppire_data_store_create_system_file(PsppireDataStore *store,
559                               struct file_handle *handle)
560 {
561   const struct sfm_write_options wo = {
562     true, /* writeable */
563     false, /* dont compress */
564     3 /* version */
565   }; 
566
567   struct sfm_writer *writer ;
568
569   g_assert(handle);
570
571   writer = sfm_open_writer(handle, store->dict->dict, wo);
572
573   if ( ! writer) 
574     return;
575
576 #if 0
577   psppire_case_array_iterate_case(store->cases, write_case, writer);
578 #endif
579
580   sfm_close_writer(writer);
581 }
582
583
584
585 void 
586 psppire_data_store_clear(PsppireDataStore *data_store)
587 {
588   psppire_case_file_clear(data_store->case_file);
589
590   psppire_dict_clear(data_store->dict);
591 }
592
593
594
595
596 /* Column related funcs */
597
598 static gint
599 geometry_get_column_count(const GSheetColumn *geom, gpointer data)
600 {
601   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
602
603   return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
604 }
605
606 /* Return the width that an  'M' character would occupy when typeset at
607    row, col */
608 static guint 
609 M_width(const GtkSheet *sheet, gint row, gint col)
610 {
611   GtkSheetCellAttr attributes;
612   PangoRectangle rect;
613   /* FIXME: make this a member of the data store */
614   static PangoLayout *layout = 0;
615
616   gtk_sheet_get_attributes(sheet, row, col, &attributes);
617
618   if (! layout ) 
619     layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
620
621   g_assert(layout);
622   
623   pango_layout_set_font_description (layout, 
624                                      attributes.font_desc);
625
626   pango_layout_get_extents (layout, NULL, &rect);
627
628 #if 0
629   g_object_unref(G_OBJECT(layout));
630 #endif
631
632   return PANGO_PIXELS(rect.width);
633 }
634
635
636 /* Return the number of pixels corresponding to a column of 
637    WIDTH characters */
638 static inline guint 
639 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
640 {
641   return (M_width(sheet, 0, column) * width);
642 }
643
644
645 static gint
646 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
647 {
648   const struct PsppireVariable *pv ;
649   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
650
651   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
652     return 75;
653
654   /* FIXME: We can optimise this by caching the widths until they're resized */
655   pv = psppire_dict_get_variable(ds->dict, unit);
656
657   return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
658 }
659
660
661
662
663 static void
664 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
665 {
666   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
667
668   struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
669
670   psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
671 }
672
673
674
675 static GtkJustification
676 geometry_get_justification(const GSheetColumn *geom, gint unit, gpointer data)
677 {
678   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
679   const struct PsppireVariable *pv ;
680
681
682   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
683     return GTK_JUSTIFY_LEFT;
684
685   pv = psppire_dict_get_variable(ds->dict, unit);
686
687   /* Kludge: Happily GtkJustification is defined similarly
688      to enum alignment from pspp/variable.h */
689   return psppire_variable_get_alignment(pv);
690 }
691
692
693 static const gchar null_var_name[]=N_("var");
694  
695 static gchar *
696 geometry_get_column_button_label(const GSheetColumn *geom, gint unit, 
697                                  gpointer data)
698 {
699   gchar *text;
700   struct PsppireVariable *pv ;
701   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
702
703   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
704     return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
705
706   pv = psppire_dict_get_variable(ds->dict, unit);
707
708   text =  pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
709
710   return text;
711 }
712
713
714 static gboolean
715 geometry_get_sensitivity(const GSheetColumn *geom, gint unit, gpointer data)
716 {
717   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
718
719
720   return (unit < psppire_dict_get_var_cnt(ds->dict));
721 }
722
723
724 static void
725 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
726 {
727   iface->get_column_count = geometry_get_column_count;
728   iface->get_width = geometry_get_width;
729   iface->set_width = geometry_set_width;
730   iface->get_visibility = always_true;
731   iface->get_sensitivity = geometry_get_sensitivity;
732   iface->get_justification = geometry_get_justification;
733
734   iface->get_button_label = geometry_get_column_button_label;
735 }
736
737
738 /* Row related funcs */
739
740 static gint
741 geometry_get_row_count(const GSheetRow *geom, gpointer data)
742 {
743   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
744
745   return TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file);
746 }
747
748
749 static gint
750 geometry_get_height(const GSheetRow *geom, gint unit, gpointer data)
751 {
752   return 25;
753 }
754
755
756 static gboolean
757 geometry_get_row_sensitivity(const GSheetRow *geom, gint unit, gpointer data)
758 {
759   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
760
761   
762   return (unit < psppire_case_file_get_case_count(ds->case_file));
763 }
764
765
766 static gchar *
767 geometry_get_row_button_label(const GSheetRow *geom, gint unit, gpointer data)
768 {
769   gchar *text;
770   gchar *s;
771   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
772
773   if ( unit > 
774        TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file))
775     return 0;
776
777   s = g_strdup_printf(_("%d"), unit);
778
779   text =  pspp_locale_to_utf8(s, -1, 0);
780   
781   g_free(s);
782   
783   return text;
784 }
785
786
787 static void
788 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
789 {
790   iface->get_row_count = geometry_get_row_count;
791
792   iface->get_height = geometry_get_height;
793   iface->set_height = 0;
794   iface->get_visibility = always_true;
795   iface->get_sensitivity = geometry_get_row_sensitivity;
796
797   iface->get_button_label = geometry_get_row_button_label;
798 }
799
800
801
802