Patch #5209
[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   gint posn;
272
273   g_return_if_fail (data);
274
275   store  = PSPPIRE_DATA_STORE(data);
276   
277   if ( var_num > 0 ) 
278     {
279       struct PsppireVariable *variable;
280       variable = psppire_dict_get_variable(store->dict, var_num);
281
282       posn = psppire_variable_get_fv(variable);
283     }
284   else
285     {
286       posn = 0;
287     }
288
289   psppire_case_file_insert_values(store->case_file, 1, posn);
290
291   g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
292                                   var_num, 1);
293
294   g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
295 }
296
297
298 static void
299 dict_size_change_callback(GObject *obj, 
300                           gint posn, gint adjustment, gpointer data)
301 {
302   PsppireDataStore *store ;
303
304   g_return_if_fail (data);
305
306   store  = PSPPIRE_DATA_STORE(data);
307
308   /* 
309   if ( adjustment > 0 )
310   */
311   psppire_case_file_insert_values (store->case_file, adjustment, posn);
312 }
313
314
315
316 /**
317  * psppire_data_store_new:
318  * @dict: The dictionary for this data_store.
319  *
320  *
321  * Return value: a new #PsppireDataStore
322  **/
323 PsppireDataStore *
324 psppire_data_store_new (PsppireDict *dict)
325 {
326   PsppireDataStore *retval;
327
328   retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
329
330   psppire_data_store_set_dictionary(retval, dict);
331
332
333   return retval;
334 }
335
336
337
338 /**
339  * psppire_data_store_replace_set_dictionary:
340  * @data_store: The variable store
341  * @dict: The dictionary to set
342  *
343  * If a dictionary is already associated with the data-store, then it will be
344  * destroyed.
345  **/
346 void
347 psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
348 {
349   gint var_cnt = psppire_dict_get_next_value_idx(dict);
350 #if 0
351   if ( data_store->dict ) g_object_unref(data_store->dict);
352 #endif
353
354   data_store->dict = dict;
355
356   if ( data_store->case_file)
357     {
358       g_object_unref(data_store->case_file);
359       data_store->case_file = 0;
360     }
361
362   data_store->case_file = psppire_case_file_new(var_cnt);
363
364   g_signal_connect(data_store->case_file, "cases-deleted", 
365                    G_CALLBACK(delete_cases_callback), 
366                    data_store);
367
368   g_signal_connect(data_store->case_file, "case-inserted", 
369                    G_CALLBACK(insert_case_callback), 
370                    data_store);
371
372
373   g_signal_connect(data_store->case_file, "case-changed", 
374                    G_CALLBACK(changed_case_callback), 
375                    data_store);
376
377   g_signal_connect(dict, "variable-inserted", 
378                    G_CALLBACK(insert_variable_callback), 
379                    data_store);
380
381   g_signal_connect(dict, "variables-deleted", 
382                    G_CALLBACK(delete_variables_callback), 
383                    data_store);
384
385   g_signal_connect (dict, "dict-size-changed", 
386                     G_CALLBACK(dict_size_change_callback),
387                     data_store);
388
389   /* The entire model has changed */
390   g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
391   
392   g_sheet_column_columns_changed(G_SHEET_COLUMN(data_store), 0, -1);
393 }
394
395 static void
396 psppire_data_store_finalize (GObject *object)
397 {
398
399   /* must chain up */
400   (* parent_class->finalize) (object);
401 }
402
403
404 static gchar *
405 psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
406 {
407   gint idx;
408   char *text;
409   const struct fmt_spec *fp ;
410   const struct PsppireVariable *pv ;
411   const union value *v ;
412   GString *s;
413   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
414
415   g_return_val_if_fail(store->dict, NULL);
416   g_return_val_if_fail(store->case_file, NULL);
417
418   if (column >= psppire_dict_get_var_cnt(store->dict))
419     return NULL;
420
421   if ( row >= psppire_case_file_get_case_count(store->case_file))
422     return NULL;
423
424   pv = psppire_dict_get_variable(store->dict, column);
425
426   idx = psppire_variable_get_fv(pv);
427
428   v = psppire_case_file_get_value(store->case_file, row, idx);
429
430   g_return_val_if_fail(v, NULL);
431
432   if ( store->show_labels) 
433     {
434       const struct val_labs * vl = psppire_variable_get_value_labels(pv);
435
436       const gchar *label;
437       if ( (label = val_labs_find(vl, *v)) )
438         {
439           return pspp_locale_to_utf8(label, -1, 0);
440         }
441     }
442
443   fp = psppire_variable_get_write_spec(pv);
444
445   s = g_string_sized_new (fp->w + 1);
446   g_string_set_size(s, fp->w);
447   
448   memset(s->str, 0, fp->w);
449
450   g_assert(fp->w == s->len);
451     
452   /* Converts binary value V into printable form in the exactly
453      FP->W character in buffer S according to format specification
454      FP.  No null terminator is appended to the buffer.  */
455   data_out (s->str, fp, v);
456
457   text = pspp_locale_to_utf8(s->str, fp->w, 0);
458   g_string_free(s, TRUE);
459
460   return text;
461 }
462
463
464 static gboolean 
465 psppire_data_store_clear_datum(GSheetModel *model, 
466                                           gint row, gint col)
467
468 {
469   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
470
471   union value v;
472   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
473
474   const gint index = psppire_variable_get_fv(pv) ;
475
476   if ( psppire_variable_get_type(pv) == NUMERIC) 
477     v.f = SYSMIS;
478   else
479     memcpy(v.s, "", MAX_SHORT_STRING);
480
481   psppire_case_file_set_value(store->case_file, row, index, &v, 
482                               psppire_variable_get_width(pv));
483   return TRUE;
484 }
485
486
487 /* Attempts to update that part of the variable store which corresponds 
488    to ROW, COL with  the value TEXT.
489    Returns true if anything was updated, false otherwise.
490 */
491 static gboolean 
492 psppire_data_store_set_string(GSheetModel *model, 
493                           const gchar *text, gint row, gint col)
494 {
495   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
496
497   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
498   g_return_val_if_fail(pv, FALSE);
499
500 #if 0
501   /* Allow the user to insert a lot of blank cases, simply by skipping rows */
502   for(r = psppire_case_file_get_case_count(store->case_file); r <= row ; ++r) 
503     {
504
505       gint c;
506
507       psppire_case_array_insert_case(store->cases, r, 0, 0);
508
509
510       for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c ) 
511         psppire_data_store_clear_datum(model, r, c);
512     }
513 #endif
514
515   {
516     const gint index = psppire_variable_get_fv(pv);
517
518     struct data_in d_in;
519     d_in.s = text;
520     d_in.e = text + strlen(text);
521     d_in.v = 0;
522     d_in.f1 = d_in.f2 = 0;
523     d_in.format = * psppire_variable_get_write_spec(pv);
524     d_in.flags = 0;
525
526     psppire_case_file_data_in(store->case_file, row, index, &d_in) ;
527   }
528
529   return TRUE;
530 }
531
532
533 void
534 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
535 {
536   g_return_if_fail (store);
537   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
538
539   store->font_desc = fd;
540   g_sheet_model_range_changed (G_SHEET_MODEL(store),
541                                  -1, -1, -1, -1);
542 }
543
544
545 void
546 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
547 {
548   g_return_if_fail (store);
549   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
550
551   store->show_labels = show_labels;
552
553   g_sheet_model_range_changed (G_SHEET_MODEL(store),
554                                  -1, -1, -1, -1);
555 }
556
557
558
559 void
560 psppire_data_store_create_system_file(PsppireDataStore *store,
561                               struct file_handle *handle)
562 {
563   const struct sfm_write_options wo = {
564     true, /* writeable */
565     false, /* dont compress */
566     3 /* version */
567   }; 
568
569   struct sfm_writer *writer ;
570
571   g_assert(handle);
572
573   writer = sfm_open_writer(handle, store->dict->dict, wo);
574
575   if ( ! writer) 
576     return;
577
578 #if 0
579   psppire_case_array_iterate_case(store->cases, write_case, writer);
580 #endif
581
582   sfm_close_writer(writer);
583 }
584
585
586
587 void 
588 psppire_data_store_clear(PsppireDataStore *data_store)
589 {
590   psppire_case_file_clear(data_store->case_file);
591
592   psppire_dict_clear(data_store->dict);
593 }
594
595
596
597
598 /* Column related funcs */
599
600 static gint
601 geometry_get_column_count(const GSheetColumn *geom, gpointer data)
602 {
603   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
604
605   return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
606 }
607
608 /* Return the width that an  'M' character would occupy when typeset at
609    row, col */
610 static guint 
611 M_width(const GtkSheet *sheet, gint row, gint col)
612 {
613   GtkSheetCellAttr attributes;
614   PangoRectangle rect;
615   /* FIXME: make this a member of the data store */
616   static PangoLayout *layout = 0;
617
618   gtk_sheet_get_attributes(sheet, row, col, &attributes);
619
620   if (! layout ) 
621     layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
622
623   g_assert(layout);
624   
625   pango_layout_set_font_description (layout, 
626                                      attributes.font_desc);
627
628   pango_layout_get_extents (layout, NULL, &rect);
629
630 #if 0
631   g_object_unref(G_OBJECT(layout));
632 #endif
633
634   return PANGO_PIXELS(rect.width);
635 }
636
637
638 /* Return the number of pixels corresponding to a column of 
639    WIDTH characters */
640 static inline guint 
641 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
642 {
643   return (M_width(sheet, 0, column) * width);
644 }
645
646
647 static gint
648 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
649 {
650   const struct PsppireVariable *pv ;
651   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
652
653   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
654     return 75;
655
656   /* FIXME: We can optimise this by caching the widths until they're resized */
657   pv = psppire_dict_get_variable(ds->dict, unit);
658
659   return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
660 }
661
662
663
664
665 static void
666 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
667 {
668   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
669
670   struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
671
672   psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
673 }
674
675
676
677 static GtkJustification
678 geometry_get_justification(const GSheetColumn *geom, gint unit, gpointer data)
679 {
680   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
681   const struct PsppireVariable *pv ;
682
683
684   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
685     return GTK_JUSTIFY_LEFT;
686
687   pv = psppire_dict_get_variable(ds->dict, unit);
688
689   /* Kludge: Happily GtkJustification is defined similarly
690      to enum alignment from pspp/variable.h */
691   return psppire_variable_get_alignment(pv);
692 }
693
694
695 static const gchar null_var_name[]=N_("var");
696  
697 static gchar *
698 geometry_get_column_button_label(const GSheetColumn *geom, gint unit, 
699                                  gpointer data)
700 {
701   gchar *text;
702   struct PsppireVariable *pv ;
703   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
704
705   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
706     return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
707
708   pv = psppire_dict_get_variable(ds->dict, unit);
709
710   text =  pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
711
712   return text;
713 }
714
715
716 static gboolean
717 geometry_get_sensitivity(const GSheetColumn *geom, gint unit, gpointer data)
718 {
719   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
720
721
722   return (unit < psppire_dict_get_var_cnt(ds->dict));
723 }
724
725
726 static void
727 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
728 {
729   iface->get_column_count = geometry_get_column_count;
730   iface->get_width = geometry_get_width;
731   iface->set_width = geometry_set_width;
732   iface->get_visibility = always_true;
733   iface->get_sensitivity = geometry_get_sensitivity;
734   iface->get_justification = geometry_get_justification;
735
736   iface->get_button_label = geometry_get_column_button_label;
737 }
738
739
740 /* Row related funcs */
741
742 static gint
743 geometry_get_row_count(const GSheetRow *geom, gpointer data)
744 {
745   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
746
747   return TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file);
748 }
749
750
751 static gint
752 geometry_get_height(const GSheetRow *geom, gint unit, gpointer data)
753 {
754   return 25;
755 }
756
757
758 static gboolean
759 geometry_get_row_sensitivity(const GSheetRow *geom, gint unit, gpointer data)
760 {
761   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
762
763   
764   return (unit < psppire_case_file_get_case_count(ds->case_file));
765 }
766
767
768 static gchar *
769 geometry_get_row_button_label(const GSheetRow *geom, gint unit, gpointer data)
770 {
771   gchar *text;
772   gchar *s;
773   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
774
775   if ( unit > 
776        TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file))
777     return 0;
778
779   s = g_strdup_printf(_("%d"), unit);
780
781   text =  pspp_locale_to_utf8(s, -1, 0);
782   
783   g_free(s);
784   
785   return text;
786 }
787
788
789 static void
790 psppire_data_store_sheet_row_init (GSheetRowIface *iface)
791 {
792   iface->get_row_count = geometry_get_row_count;
793
794   iface->get_height = geometry_get_height;
795   iface->set_height = 0;
796   iface->get_visibility = always_true;
797   iface->get_sensitivity = geometry_get_row_sensitivity;
798
799   iface->get_button_label = geometry_get_row_button_label;
800 }
801
802
803
804