Change "union value" to dynamically allocate long strings.
[pspp-builds.git] / src / ui / gui / psppire-data-store.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2006, 2008, 2009  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <gettext.h>
21 #define _(msgid) gettext (msgid)
22 #define N_(msgid) msgid
23
24 #include <data/datasheet.h>
25 #include <data/data-out.h>
26 #include <data/variable.h>
27
28 #include <ui/gui/sheet/psppire-sheetmodel.h>
29 #include <ui/gui/psppire-marshal.h>
30
31 #include <pango/pango-context.h>
32
33 #include "psppire-data-store.h"
34 #include <libpspp/i18n.h>
35 #include "helper.h"
36
37 #include <data/dictionary.h>
38 #include <data/missing-values.h>
39 #include <data/value-labels.h>
40 #include <data/data-in.h>
41 #include <data/format.h>
42
43 #include <math/sort.h>
44
45 #include "xalloc.h"
46 #include "xmalloca.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 (PsppireSheetModelIface *iface);
53
54 static void psppire_data_store_finalize        (GObject           *object);
55 static void psppire_data_store_dispose        (GObject           *object);
56
57 static gboolean psppire_data_store_clear_datum (PsppireSheetModel *model,
58                                           glong row, glong column);
59
60
61 static gboolean psppire_data_store_insert_case (PsppireDataStore *ds,
62                                                 struct ccase *cc,
63                                                 casenumber posn);
64
65
66 static gboolean psppire_data_store_data_in (PsppireDataStore *ds,
67                                             casenumber casenum, gint idx,
68                                             struct substring input,
69                                             const struct fmt_spec *fmt);
70
71
72
73 static GObjectClass *parent_class = NULL;
74
75
76 enum
77   {
78     BACKEND_CHANGED,
79     CASES_DELETED,
80     CASE_INSERTED,
81     CASE_CHANGED,
82     n_SIGNALS
83   };
84
85 static guint signals [n_SIGNALS];
86
87
88 GType
89 psppire_data_store_get_type (void)
90 {
91   static GType data_store_type = 0;
92
93   if (!data_store_type)
94     {
95       static const GTypeInfo data_store_info =
96       {
97         sizeof (PsppireDataStoreClass),
98         NULL,           /* base_init */
99         NULL,           /* base_finalize */
100         (GClassInitFunc) psppire_data_store_class_init,
101         NULL,           /* class_finalize */
102         NULL,           /* class_data */
103         sizeof (PsppireDataStore),
104         0,
105         (GInstanceInitFunc) psppire_data_store_init,
106       };
107
108       static const GInterfaceInfo sheet_model_info =
109       {
110         (GInterfaceInitFunc) psppire_data_store_sheet_model_init,
111         NULL,
112         NULL
113       };
114
115
116       data_store_type = g_type_register_static (G_TYPE_OBJECT,
117                                                 "PsppireDataStore",
118                                                 &data_store_info, 0);
119
120       g_type_add_interface_static (data_store_type,
121                                    PSPPIRE_TYPE_SHEET_MODEL,
122                                    &sheet_model_info);
123
124     }
125
126   return data_store_type;
127 }
128
129
130 static void
131 psppire_data_store_class_init (PsppireDataStoreClass *class)
132 {
133   GObjectClass *object_class;
134
135   parent_class = g_type_class_peek_parent (class);
136   object_class = (GObjectClass*) class;
137
138   object_class->finalize = psppire_data_store_finalize;
139   object_class->dispose = psppire_data_store_dispose;
140
141   signals [BACKEND_CHANGED] =
142     g_signal_new ("backend-changed",
143                   G_TYPE_FROM_CLASS (class),
144                   G_SIGNAL_RUN_FIRST,
145                   0,
146                   NULL, NULL,
147                   g_cclosure_marshal_VOID__VOID,
148                   G_TYPE_NONE,
149                   0);
150
151   signals [CASE_INSERTED] =
152     g_signal_new ("case-inserted",
153                   G_TYPE_FROM_CLASS (class),
154                   G_SIGNAL_RUN_FIRST,
155                   0,
156                   NULL, NULL,
157                   g_cclosure_marshal_VOID__INT,
158                   G_TYPE_NONE,
159                   1,
160                   G_TYPE_INT);
161
162
163   signals [CASE_CHANGED] =
164     g_signal_new ("case-changed",
165                   G_TYPE_FROM_CLASS (class),
166                   G_SIGNAL_RUN_FIRST,
167                   0,
168                   NULL, NULL,
169                   g_cclosure_marshal_VOID__INT,
170                   G_TYPE_NONE,
171                   1,
172                   G_TYPE_INT);
173
174   signals [CASES_DELETED] =
175     g_signal_new ("cases-deleted",
176                   G_TYPE_FROM_CLASS (class),
177                   G_SIGNAL_RUN_FIRST,
178                   0,
179                   NULL, NULL,
180                   psppire_marshal_VOID__INT_INT,
181                   G_TYPE_NONE,
182                   2,
183                   G_TYPE_INT,
184                   G_TYPE_INT);
185 }
186
187
188
189 static gboolean
190 psppire_data_store_insert_value (PsppireDataStore *ds,
191                                   gint width, gint where);
192
193 static bool
194 psppire_data_store_get_value (const PsppireDataStore *ds,
195                               casenumber casenum, size_t idx,
196                               union value *value);
197
198
199 static gboolean
200 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
201                               gint idx, union value *v);
202
203
204
205
206 static glong
207 psppire_data_store_get_var_count (const PsppireSheetModel *model)
208 {
209   const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
210
211   return psppire_dict_get_var_cnt (store->dict);
212 }
213
214 casenumber
215 psppire_data_store_get_case_count (const PsppireDataStore *store)
216 {
217   return datasheet_get_n_rows (store->datasheet);
218 }
219
220 size_t
221 psppire_data_store_get_value_count (const PsppireDataStore *store)
222 {
223   return psppire_dict_get_value_cnt (store->dict);
224 }
225
226 const struct caseproto *
227 psppire_data_store_get_proto (const PsppireDataStore *store)
228 {
229   return psppire_dict_get_proto (store->dict);
230 }
231
232 static casenumber
233 psppire_data_store_get_case_count_wrapper (const PsppireSheetModel *model)
234 {
235   const PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
236   return psppire_data_store_get_case_count (store);
237 }
238
239 static void
240 psppire_data_store_init (PsppireDataStore *data_store)
241 {
242   data_store->dict = 0;
243   data_store->datasheet = NULL;
244   data_store->dispose_has_run = FALSE;
245 }
246
247 static inline gchar *
248 psppire_data_store_get_string_wrapper (const PsppireSheetModel *model, glong row,
249                                        glong column)
250 {
251   return psppire_data_store_get_string (PSPPIRE_DATA_STORE (model), row, column);
252 }
253
254
255 static inline gboolean
256 psppire_data_store_set_string_wrapper (PsppireSheetModel *model,
257                                        const gchar *text,
258                                        glong row, glong column)
259 {
260   return psppire_data_store_set_string (PSPPIRE_DATA_STORE (model), text,
261                                         row, column);
262 }
263
264
265
266 static gchar * get_column_subtitle (const PsppireSheetModel *model, gint col);
267 static gchar * get_column_button_label (const PsppireSheetModel *model, gint col);
268 static gboolean get_column_sensitivity (const PsppireSheetModel *model, gint col);
269 static GtkJustification get_column_justification (const PsppireSheetModel *model, gint col);
270
271 static gchar * get_row_button_label (const PsppireSheetModel *model, gint row);
272 static gboolean get_row_sensitivity (const PsppireSheetModel *model, gint row);
273 static gboolean get_row_overstrike (const PsppireSheetModel *model, gint row);
274
275
276 static void
277 psppire_data_store_sheet_model_init (PsppireSheetModelIface *iface)
278 {
279   iface->free_strings = TRUE;
280   iface->get_string = psppire_data_store_get_string_wrapper;
281   iface->set_string = psppire_data_store_set_string_wrapper;
282   iface->clear_datum = psppire_data_store_clear_datum;
283   iface->is_editable = NULL;
284   iface->get_foreground = NULL;
285   iface->get_background = NULL;
286   iface->get_column_count = psppire_data_store_get_var_count;
287   iface->get_row_count = psppire_data_store_get_case_count_wrapper;
288
289   iface->get_column_subtitle = get_column_subtitle;
290   iface->get_column_title = get_column_button_label;
291   iface->get_column_sensitivity = get_column_sensitivity;
292   iface->get_column_justification = get_column_justification;
293
294   iface->get_row_title = get_row_button_label;
295   iface->get_row_sensitivity = get_row_sensitivity;
296   iface->get_row_overstrike = get_row_overstrike;
297 }
298
299
300 /*
301    A callback which occurs after a variable has been deleted.
302  */
303 static void
304 delete_variable_callback (GObject *obj, gint dict_index,
305                           gint case_index, gint width,
306                           gpointer data)
307 {
308   PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
309
310
311   psppire_sheet_model_columns_deleted (PSPPIRE_SHEET_MODEL (store), dict_index, 1);
312   datasheet_delete_columns (store->datasheet, case_index, 1);
313   datasheet_insert_column (store->datasheet, NULL, -1, case_index);
314 #if AXIS_TRANSITION
315
316
317   psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store),
318                                    dict_index, -1);
319 #endif
320 }
321
322 static void
323 variable_changed_callback (GObject *obj, gint var_num, gpointer data)
324 {
325 #if AXIS_TRANSITION
326   PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
327
328   psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store),
329                                   var_num, 1);
330
331   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store),
332                                -1, var_num,
333                                -1, var_num);
334 #endif
335 }
336
337 static void
338 insert_variable_callback (GObject *obj, gint var_num, gpointer data)
339 {
340   struct variable *variable;
341   PsppireDataStore *store;
342   gint posn;
343
344   g_return_if_fail (data);
345
346   store  = PSPPIRE_DATA_STORE (data);
347
348   variable = psppire_dict_get_variable (store->dict, var_num);
349   posn = var_get_case_index (variable);
350   printf ("insert var_num=%d, posn=%d\n", var_num, posn);
351   psppire_data_store_insert_value (store, var_get_width (variable), posn);
352
353 #if AXIS_TRANSITION
354
355   psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store),
356                                   var_num, 1);
357 #endif
358
359   psppire_sheet_model_columns_inserted (PSPPIRE_SHEET_MODEL (store), var_num, 1);
360 }
361
362 struct resize_datum_aux
363   {
364     int old_width;
365     int new_width;
366   };
367
368
369 void
370 resize_datum (const union value *old, union value *new, void *aux_)
371 {
372   struct resize_datum_aux *aux = aux_;
373
374   if (aux->new_width == 0)
375     {
376       /* FIXME: try to parse string as number. */
377       new->f = SYSMIS;
378     }
379   else if (aux->old_width == 0)
380     {
381       /* FIXME: format number as string. */
382       value_set_missing (new, aux->new_width);
383     }
384   else
385     value_copy_rpad (new, aux->new_width, old, aux->old_width, ' ');
386 }
387
388 static void
389 dict_size_change_callback (GObject *obj,
390                           gint var_num, gint old_width, gpointer data)
391 {
392   PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
393   struct variable *variable;
394   int posn;
395
396   variable = psppire_dict_get_variable (store->dict, var_num);
397   posn = var_get_case_index (variable);
398
399   if (old_width != var_get_width (variable))
400     {
401       struct resize_datum_aux aux;
402       aux.old_width = old_width;
403       aux.new_width = var_get_width (variable);
404       datasheet_resize_column (store->datasheet, posn, aux.new_width,
405                                resize_datum, &aux);
406     }
407 }
408
409
410
411 /**
412  * psppire_data_store_new:
413  * @dict: The dictionary for this data_store.
414  *
415  *
416  * Return value: a new #PsppireDataStore
417  **/
418 PsppireDataStore *
419 psppire_data_store_new (PsppireDict *dict)
420 {
421   PsppireDataStore *retval;
422
423   retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
424
425   psppire_data_store_set_dictionary (retval, dict);
426
427   return retval;
428 }
429
430 void
431 psppire_data_store_set_reader (PsppireDataStore *ds,
432                                struct casereader *reader)
433 {
434   gint i;
435
436   if ( ds->datasheet)
437     datasheet_destroy (ds->datasheet);
438
439   ds->datasheet = datasheet_create (reader);
440
441   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (ds),
442                                -1, -1, -1, -1);
443
444   if ( ds->dict )
445     for (i = 0 ; i < n_dict_signals; ++i )
446       {
447         if ( ds->dict_handler_id [i] > 0)
448           {
449             g_signal_handler_unblock (ds->dict,
450                                       ds->dict_handler_id[i]);
451           }
452       }
453
454   g_signal_emit (ds, signals[BACKEND_CHANGED], 0);
455 }
456
457
458 /**
459  * psppire_data_store_replace_set_dictionary:
460  * @data_store: The variable store
461  * @dict: The dictionary to set
462  *
463  * If a dictionary is already associated with the data-store, then it will be
464  * destroyed.
465  **/
466 void
467 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
468 {
469   int i;
470
471   /* Disconnect any existing handlers */
472   if ( data_store->dict )
473     for (i = 0 ; i < n_dict_signals; ++i )
474       {
475         g_signal_handler_disconnect (data_store->dict,
476                                      data_store->dict_handler_id[i]);
477       }
478
479   data_store->dict = dict;
480
481   if ( dict != NULL)
482     {
483
484       data_store->dict_handler_id [VARIABLE_INSERTED] =
485         g_signal_connect (dict, "variable-inserted",
486                           G_CALLBACK (insert_variable_callback),
487                           data_store);
488
489       data_store->dict_handler_id [VARIABLE_DELETED] =
490         g_signal_connect (dict, "variable-deleted",
491                           G_CALLBACK (delete_variable_callback),
492                           data_store);
493
494       data_store->dict_handler_id [VARIABLE_CHANGED] =
495         g_signal_connect (dict, "variable-changed",
496                           G_CALLBACK (variable_changed_callback),
497                           data_store);
498
499       data_store->dict_handler_id [SIZE_CHANGED] =
500         g_signal_connect (dict, "dict-size-changed",
501                           G_CALLBACK (dict_size_change_callback),
502                           data_store);
503     }
504
505
506
507   /* The entire model has changed */
508   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (data_store), -1, -1, -1, -1);
509
510 #if AXIS_TRANSITION
511   psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (data_store), 0, -1);
512 #endif
513
514   if ( data_store->dict )
515     for (i = 0 ; i < n_dict_signals; ++i )
516       {
517         if ( data_store->dict_handler_id [i] > 0)
518           {
519             g_signal_handler_block (data_store->dict,
520                                     data_store->dict_handler_id[i]);
521           }
522       }
523 }
524
525 static void
526 psppire_data_store_finalize (GObject *object)
527 {
528
529   /* must chain up */
530   (* parent_class->finalize) (object);
531 }
532
533
534 static void
535 psppire_data_store_dispose (GObject *object)
536 {
537   PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
538
539   if (ds->dispose_has_run)
540     return;
541
542   if (ds->datasheet)
543     {
544       datasheet_destroy (ds->datasheet);
545       ds->datasheet = NULL;
546     }
547
548   /* must chain up */
549   (* parent_class->dispose) (object);
550
551   ds->dispose_has_run = TRUE;
552 }
553
554
555
556 /* Insert a blank case before POSN */
557 gboolean
558 psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn)
559 {
560   gboolean result;
561   const struct caseproto *proto;
562   struct ccase *cc;
563   g_return_val_if_fail (ds, FALSE);
564
565   proto = datasheet_get_proto (ds->datasheet);
566   g_return_val_if_fail (caseproto_get_n_widths (proto) > 0, FALSE);
567   g_return_val_if_fail (posn <= psppire_data_store_get_case_count (ds), FALSE);
568
569   cc = case_create (proto);
570   case_set_missing (cc);
571
572   result = psppire_data_store_insert_case (ds, cc, posn);
573
574   case_unref (cc);
575
576   return result;
577 }
578
579
580 gchar *
581 psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column)
582 {
583   gint idx;
584   char *text;
585   const struct fmt_spec *fp ;
586   const struct variable *pv ;
587   union value v;
588   int width;
589   GString *s;
590
591   g_return_val_if_fail (store->dict, NULL);
592   g_return_val_if_fail (store->datasheet, NULL);
593
594   if (column >= psppire_dict_get_var_cnt (store->dict))
595     return NULL;
596
597   if ( row >= psppire_data_store_get_case_count (store))
598     return NULL;
599
600   pv = psppire_dict_get_variable (store->dict, column);
601
602   g_assert (pv);
603
604   idx = var_get_case_index (pv);
605   width = var_get_width (pv);
606
607   g_assert (idx >= 0);
608
609   value_init (&v, width);
610   if (!psppire_data_store_get_value (store, row, idx, &v))
611     return NULL;
612
613   if ( store->show_labels)
614     {
615       const gchar *label = var_lookup_value_label (pv, &v);
616       if (label)
617         {
618           value_destroy (&v, width);
619           return recode_string (UTF8, psppire_dict_encoding (store->dict),
620                                 label, -1);
621         }
622     }
623
624   fp = var_get_write_format (pv);
625
626   s = g_string_sized_new (fp->w + 1);
627   g_string_set_size (s, fp->w);
628
629   memset (s->str, 0, fp->w);
630
631   g_assert (fp->w == s->len);
632
633   /* Converts binary value V into printable form in the exactly
634      FP->W character in buffer S according to format specification
635      FP.  No null terminator is appended to the buffer.  */
636   data_out (&v, fp, s->str);
637
638   text = recode_string (UTF8, psppire_dict_encoding (store->dict),
639                         s->str, fp->w);
640   g_string_free (s, TRUE);
641
642   g_strchomp (text);
643
644   value_destroy (&v, width);
645   return text;
646 }
647
648
649 static gboolean
650 psppire_data_store_clear_datum (PsppireSheetModel *model,
651                                           glong row, glong col)
652 {
653   PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
654
655   union value v;
656   int width;
657   const struct variable *pv = psppire_dict_get_variable (store->dict, col);
658
659   const gint index = var_get_case_index (pv) ;
660
661   width = var_is_numeric (pv) ? 0 : MAX_SHORT_STRING;
662   value_init (&v, width);
663   value_set_missing (&v, width);
664   psppire_data_store_set_value (store, row, index, &v);
665   value_destroy (&v, width);
666
667   psppire_sheet_model_range_changed (model, row, col, row, col);
668
669
670   return TRUE;
671 }
672
673
674 /* Attempts to update that part of the variable store which corresponds
675    to ROW, COL with  the value TEXT.
676    Returns true if anything was updated, false otherwise.
677 */
678 gboolean
679 psppire_data_store_set_string (PsppireDataStore *store,
680                                const gchar *text, glong row, glong col)
681 {
682   gchar *s;
683   glong n_cases;
684   const struct variable *pv = psppire_dict_get_variable (store->dict, col);
685   if ( NULL == pv)
686     return FALSE;
687
688   n_cases = psppire_data_store_get_case_count (store);
689
690   if ( row > n_cases)
691     return FALSE;
692
693   if (row == n_cases)
694     psppire_data_store_insert_new_case (store, row);
695
696   s = recode_string (psppire_dict_encoding (store->dict), UTF8, text, -1);
697
698   psppire_data_store_data_in (store, row,
699                               var_get_case_index (pv), ss_cstr (s),
700                               var_get_write_format (pv));
701   free (s);
702
703   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), row, col, row, col);
704
705   return TRUE;
706 }
707
708
709
710 void
711 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
712 {
713   g_return_if_fail (store);
714   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
715
716   store->show_labels = show_labels;
717
718   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store),
719                                  -1, -1, -1, -1);
720 }
721
722
723 void
724 psppire_data_store_clear (PsppireDataStore *ds)
725 {
726   datasheet_destroy (ds->datasheet);
727   ds->datasheet = NULL;
728
729   psppire_dict_clear (ds->dict);
730
731   g_signal_emit (ds, signals [CASES_DELETED], 0, 0, -1);
732 }
733
734
735
736 /* Return a casereader made from this datastore */
737 struct casereader *
738 psppire_data_store_get_reader (PsppireDataStore *ds)
739 {
740   int i;
741   struct casereader *reader ;
742
743   if ( ds->dict )
744     for (i = 0 ; i < n_dict_signals; ++i )
745       {
746         g_signal_handler_block (ds->dict,
747                                 ds->dict_handler_id[i]);
748       }
749
750   reader = datasheet_make_reader (ds->datasheet);
751
752   /* We must not reference this again */
753   ds->datasheet = NULL;
754
755   return reader;
756 }
757
758
759
760 /* Column related funcs */
761
762
763 static const gchar null_var_name[]=N_("var");
764
765 \f
766
767 /* Row related funcs */
768
769 static gchar *
770 get_row_button_label (const PsppireSheetModel *model, gint unit)
771 {
772   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
773   gchar *s = g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER);
774
775   gchar *text =  recode_string (UTF8, psppire_dict_encoding (ds->dict),
776                                 s, -1);
777
778   g_free (s);
779
780   return text;
781 }
782
783
784 static gboolean
785 get_row_sensitivity (const PsppireSheetModel *model, gint unit)
786 {
787   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
788
789   return (unit < psppire_data_store_get_case_count (ds));
790 }
791
792
793 \f
794
795 /* Column related stuff */
796
797 static gchar *
798 get_column_subtitle (const PsppireSheetModel *model, gint col)
799 {
800   gchar *text;
801   const struct variable *v ;
802   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
803
804   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
805     return NULL;
806
807   v = psppire_dict_get_variable (ds->dict, col);
808
809   if ( ! var_has_label (v))
810     return NULL;
811
812   text =  recode_string (UTF8, psppire_dict_encoding (ds->dict),
813                          var_get_label (v), -1);
814
815   return text;
816 }
817
818 static gchar *
819 get_column_button_label (const PsppireSheetModel *model, gint col)
820 {
821   gchar *text;
822   struct variable *pv ;
823   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
824
825   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
826     return g_locale_to_utf8 (null_var_name, -1, 0, 0, 0);
827
828   pv = psppire_dict_get_variable (ds->dict, col);
829
830   text = recode_string (UTF8, psppire_dict_encoding (ds->dict),
831                         var_get_name (pv), -1);
832
833   return text;
834 }
835
836 static gboolean
837 get_column_sensitivity (const PsppireSheetModel *model, gint col)
838 {
839   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
840
841   return (col < psppire_dict_get_var_cnt (ds->dict));
842 }
843
844
845
846 static GtkJustification
847 get_column_justification (const PsppireSheetModel *model, gint col)
848 {
849   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
850   const struct variable *pv ;
851
852   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
853     return GTK_JUSTIFY_LEFT;
854
855   pv = psppire_dict_get_variable (ds->dict, col);
856
857   return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
858           : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
859           : GTK_JUSTIFY_CENTER);
860 }
861
862
863
864 \f
865
866
867 /* Returns the CASENUMth case, or a null pointer on failure.
868  */
869 struct ccase *
870 psppire_data_store_get_case (const PsppireDataStore *ds,
871                              casenumber casenum)
872 {
873   g_return_val_if_fail (ds, FALSE);
874   g_return_val_if_fail (ds->datasheet, FALSE);
875
876   return datasheet_get_row (ds->datasheet, casenum);
877 }
878
879
880 gboolean
881 psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first,
882                                  casenumber n_cases)
883 {
884   g_return_val_if_fail (ds, FALSE);
885   g_return_val_if_fail (ds->datasheet, FALSE);
886
887   g_return_val_if_fail (first + n_cases <=
888                         psppire_data_store_get_case_count (ds), FALSE);
889
890
891   datasheet_delete_rows (ds->datasheet, first, n_cases);
892
893   g_signal_emit (ds, signals [CASES_DELETED], 0, first, n_cases);
894   psppire_sheet_model_rows_deleted (PSPPIRE_SHEET_MODEL (ds), first, n_cases);
895
896   return TRUE;
897 }
898
899
900
901 /* Insert case CC into the case file before POSN */
902 static gboolean
903 psppire_data_store_insert_case (PsppireDataStore *ds,
904                                 struct ccase *cc,
905                                 casenumber posn)
906 {
907   bool result ;
908
909   g_return_val_if_fail (ds, FALSE);
910   g_return_val_if_fail (ds->datasheet, FALSE);
911
912   case_ref (cc);
913   result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
914
915   if ( result )
916     {
917       g_signal_emit (ds, signals [CASE_INSERTED], 0, posn);
918       psppire_sheet_model_rows_inserted (PSPPIRE_SHEET_MODEL (ds), posn, 1);
919     }
920   else
921     g_warning ("Cannot insert case at position %ld\n", posn);
922
923   return result;
924 }
925
926
927 /* Copies the IDXth value from case CASENUM into VALUE, which
928    must be of the correct width for IDX.
929    Returns true if successful, false on failure. */
930 static bool
931 psppire_data_store_get_value (const PsppireDataStore *ds,
932                               casenumber casenum, size_t idx,
933                               union value *value)
934 {
935   g_return_val_if_fail (ds, false);
936   g_return_val_if_fail (ds->datasheet, false);
937   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), false);
938
939   return datasheet_get_value (ds->datasheet, casenum, idx, value);
940 }
941
942
943
944 /* Set the IDXth value of case C to V.
945    V must be the correct width for IDX.
946    Returns true if successful, false on I/O error. */
947 static gboolean
948 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
949                               gint idx, union value *v)
950 {
951   bool ok;
952
953   g_return_val_if_fail (ds, FALSE);
954   g_return_val_if_fail (ds->datasheet, FALSE);
955
956   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
957
958   ok = datasheet_put_value (ds->datasheet, casenum, idx, v);
959   if (ok)
960     g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
961
962   return ok;
963 }
964
965
966
967
968 /* Set the IDXth value of case C using D_IN */
969 static gboolean
970 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
971                             struct substring input, const struct fmt_spec *fmt)
972 {
973   union value value;
974   int width;
975   bool ok;
976
977   g_return_val_if_fail (ds, FALSE);
978   g_return_val_if_fail (ds->datasheet, FALSE);
979
980   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
981
982   width = fmt_var_width (fmt);
983   g_return_val_if_fail (caseproto_get_width (
984                           datasheet_get_proto (ds->datasheet), idx) == width,
985                         FALSE);
986   value_init (&value, width);
987   ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
988         && data_in (input, LEGACY_NATIVE, fmt->type, 0, 0, 0, &value, width)
989         && datasheet_put_value (ds->datasheet, casenum, idx, &value));
990   value_destroy (&value, width);
991
992   if (ok)
993     g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
994
995   return ok;
996 }
997
998 /* Resize the cases in the casefile, by inserting a value of the
999    given WIDTH into every one of them at the position immediately
1000    preceding WHERE.
1001 */
1002 static gboolean
1003 psppire_data_store_insert_value (PsppireDataStore *ds,
1004                                  gint width, gint where)
1005 {
1006   union value value;
1007
1008   g_return_val_if_fail (ds, FALSE);
1009
1010   g_assert (width >= 0);
1011
1012   if ( ! ds->datasheet )
1013     ds->datasheet = datasheet_create (NULL);
1014
1015   value_init (&value, width);
1016   if (width == 0)
1017     value.f = 0;
1018   else
1019     value_set_missing (&value, width);
1020
1021   printf("insert column width=%d\n", width);
1022   datasheet_insert_column (ds->datasheet, &value, width, where);
1023
1024   return TRUE;
1025 }
1026
1027 static gboolean
1028 get_row_overstrike (const PsppireSheetModel *model, gint row)
1029 {
1030   union value val;
1031   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
1032
1033   const struct dictionary *dict = ds->dict->dict;
1034
1035   const struct variable *filter = dict_get_filter (dict);
1036
1037   if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
1038     return FALSE;
1039
1040   if ( ! filter)
1041     return FALSE;
1042
1043   g_assert (var_is_numeric (filter));
1044
1045   value_init (&val, 0);
1046   if ( ! datasheet_get_value (ds->datasheet, row,
1047                               var_get_case_index (filter),
1048                               &val) )
1049     return FALSE;
1050
1051
1052   return (val.f == 0.0);
1053 }