62bbfef9c879fb4f6aff76ee649494390cd87787
[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   
405   text = pspp_locale_to_utf8(s->str, fp->w, 0);
406   g_string_free(s, TRUE);
407
408   return text;
409 }
410
411
412 static gboolean
413 set_null_string_value(union value *val, gpointer data)
414 {
415   strcpy(val->s, "");
416   return TRUE;
417 }
418
419 static gboolean
420 set_sysmis_value(union value *val, gpointer data)
421 {
422   val->f = SYSMIS;
423   return TRUE;
424 }
425
426
427 static gboolean 
428 psppire_data_store_clear_datum(GSheetModel *model, 
429                                           gint row, gint col)
430
431 {
432   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
433
434   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
435
436   const gint index = psppire_variable_get_index(pv) ;
437
438   if ( psppire_variable_get_type(pv) == NUMERIC) 
439     psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
440   else
441     psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
442   return TRUE;
443 }
444
445
446 static gboolean
447 fillit(union value *val, gpointer data)
448 {
449   struct data_in *d_in = data;
450
451   d_in->v = val;
452
453   if ( ! data_in(d_in) ) 
454     {
455       g_warning("Cant encode string\n");
456       return FALSE;
457     }
458
459   return TRUE;
460 }
461
462
463 /* Attempts to update that part of the variable store which corresponds 
464    to ROW, COL with  the value TEXT.
465    Returns true if anything was updated, false otherwise.
466 */
467 static gboolean 
468 psppire_data_store_set_string(GSheetModel *model, 
469                           const gchar *text, gint row, gint col)
470 {
471   gint r;
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   for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r ) 
478     {
479       gint c;
480       psppire_case_array_insert_case(store->cases, r, 0, 0);
481
482       for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c ) 
483         psppire_data_store_clear_datum(model, r, c);
484     }
485
486   {
487     const gint index = psppire_variable_get_index(pv);
488
489     struct data_in d_in;
490     d_in.s = text;
491     d_in.e = text + strlen(text);
492     d_in.v = 0;
493     d_in.f1 = d_in.f2 = 0;
494     d_in.format = * psppire_variable_get_write_spec(pv);
495     d_in.flags = 0;
496
497     psppire_case_array_set_value(store->cases, row, index, fillit, &d_in);
498   }
499
500   return TRUE;
501 }
502
503
504 void
505 psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
506 {
507   g_return_if_fail (store);
508   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
509
510   store->font_desc = fd;
511   g_sheet_model_range_changed (G_SHEET_MODEL(store),
512                                  -1, -1, -1, -1);
513 }
514
515
516 void
517 psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
518 {
519   g_return_if_fail (store);
520   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
521
522   store->show_labels = show_labels;
523
524   g_sheet_model_range_changed (G_SHEET_MODEL(store),
525                                  -1, -1, -1, -1);
526 }
527
528
529
530 static gboolean 
531 write_case(const struct ccase *cc, 
532            gpointer aux)
533 {
534   struct sfm_writer *writer = aux;
535
536   if ( ! sfm_write_case(writer, cc) )
537     return FALSE;
538
539
540   return TRUE;
541 }
542
543 void
544 psppire_data_store_create_system_file(PsppireDataStore *store,
545                               struct file_handle *handle)
546 {
547   const struct sfm_write_options wo = {
548     true, /* writeable */
549     false, /* dont compress */
550     3 /* version */
551   }; 
552
553   struct sfm_writer *writer ;
554
555   g_assert(handle);
556
557   writer = sfm_open_writer(handle, store->dict->dict, wo);
558
559   if ( ! writer) 
560     return;
561
562   psppire_case_array_iterate_case(store->cases, write_case, writer);
563
564   sfm_close_writer(writer);
565 }
566
567
568
569 /* Column related funcs */
570
571 static gint
572 geometry_get_column_count(const GSheetColumn *geom)
573 {
574   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
575
576   return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
577 }
578
579 /* Return the width that an  'M' character would occupy when typeset at
580    row, col */
581 static guint 
582 M_width(GtkSheet *sheet, gint row, gint col)
583 {
584   GtkSheetCellAttr attributes;
585   PangoRectangle rect;
586   /* FIXME: make this a member of the data store */
587   static PangoLayout *layout = 0;
588
589   gtk_sheet_get_attributes(sheet, row, col, &attributes);
590
591   if (! layout ) 
592     layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
593
594   g_assert(layout);
595   
596   pango_layout_set_font_description (layout, 
597                                      attributes.font_desc);
598
599   pango_layout_get_extents (layout, NULL, &rect);
600
601 #if 0
602   g_object_unref(G_OBJECT(layout));
603 #endif
604
605   return PANGO_PIXELS(rect.width);
606 }
607
608
609 /* Return the number of pixels corresponding to a column of 
610    WIDTH characters */
611 static inline guint 
612 columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
613 {
614   return (M_width(sheet, 0, column) * width);
615 }
616
617
618 static gint
619 geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
620 {
621   const struct PsppireVariable *pv ;
622   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
623
624   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
625     return 75;
626
627   /* FIXME: We can optimise this by caching the widths until they're resized */
628   pv = psppire_dict_get_variable(ds->dict, unit);
629
630   return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
631 }
632
633
634
635
636 static void
637 geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
638 {
639   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
640
641   struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
642
643   psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
644 }
645
646
647
648 static GtkJustification
649 geometry_get_justification(const GSheetColumn *geom, gint unit)
650 {
651   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
652   const struct PsppireVariable *pv ;
653
654
655   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
656     return GTK_JUSTIFY_LEFT;
657
658   pv = psppire_dict_get_variable(ds->dict, unit);
659
660   /* Kludge: Happily GtkJustification is defined similarly
661      to enum alignment from pspp/variable.h */
662   return psppire_variable_get_alignment(pv);
663 }
664
665
666 static const gchar null_var_name[]=N_("var");
667  
668 static const gchar *
669 geometry_get_button_label(const GSheetColumn *geom, gint unit)
670 {
671   const gchar *text;
672   struct PsppireVariable *pv ;
673   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
674
675   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
676     return pspp_locale_to_utf8(null_var_name, -1, 0);
677
678   pv = psppire_dict_get_variable(ds->dict, unit);
679
680   text =  pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
681
682   return text;
683 }
684
685
686 static gboolean
687 geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
688 {
689   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
690
691
692   return (unit < psppire_dict_get_var_cnt(ds->dict));
693 }
694
695
696 static void
697 psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
698 {
699   iface->get_column_count = geometry_get_column_count;
700   iface->get_width = geometry_get_width;
701   iface->set_width = geometry_set_width;
702   iface->get_visibility = always_true;
703   iface->get_sensitivity = geometry_get_sensitivity;
704   iface->get_justification = geometry_get_justification;
705
706   iface->get_button_label = geometry_get_button_label;
707 }