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