Fix crash when opening empty dataset
[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   psppire_data_store_insert_value (store, var_get_width (variable), posn);
351
352 #if AXIS_TRANSITION
353
354   psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (store),
355                                   var_num, 1);
356 #endif
357
358   psppire_sheet_model_columns_inserted (PSPPIRE_SHEET_MODEL (store), var_num, 1);
359 }
360
361 struct resize_datum_aux
362   {
363     int old_width;
364     int new_width;
365   };
366
367
368 void
369 resize_datum (const union value *old, union value *new, void *aux_)
370 {
371   struct resize_datum_aux *aux = aux_;
372
373   if (aux->new_width == 0)
374     {
375       /* FIXME: try to parse string as number. */
376       new->f = SYSMIS;
377     }
378   else if (aux->old_width == 0)
379     {
380       /* FIXME: format number as string. */
381       value_set_missing (new, aux->new_width);
382     }
383   else
384     value_copy_rpad (new, aux->new_width, old, aux->old_width, ' ');
385 }
386
387 static void
388 dict_size_change_callback (GObject *obj,
389                           gint var_num, gint old_width, gpointer data)
390 {
391   PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
392   struct variable *variable;
393   int posn;
394
395   variable = psppire_dict_get_variable (store->dict, var_num);
396   posn = var_get_case_index (variable);
397
398   if (old_width != var_get_width (variable))
399     {
400       struct resize_datum_aux aux;
401       aux.old_width = old_width;
402       aux.new_width = var_get_width (variable);
403       datasheet_resize_column (store->datasheet, posn, aux.new_width,
404                                resize_datum, &aux);
405     }
406 }
407
408
409
410 /**
411  * psppire_data_store_new:
412  * @dict: The dictionary for this data_store.
413  *
414  *
415  * Return value: a new #PsppireDataStore
416  **/
417 PsppireDataStore *
418 psppire_data_store_new (PsppireDict *dict)
419 {
420   PsppireDataStore *retval;
421
422   retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
423
424   psppire_data_store_set_dictionary (retval, dict);
425
426   return retval;
427 }
428
429 void
430 psppire_data_store_set_reader (PsppireDataStore *ds,
431                                struct casereader *reader)
432 {
433   gint i;
434
435   if ( ds->datasheet)
436     datasheet_destroy (ds->datasheet);
437
438   ds->datasheet = datasheet_create (reader);
439
440   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (ds),
441                                -1, -1, -1, -1);
442
443   if ( ds->dict )
444     for (i = 0 ; i < n_dict_signals; ++i )
445       {
446         if ( ds->dict_handler_id [i] > 0)
447           {
448             g_signal_handler_unblock (ds->dict,
449                                       ds->dict_handler_id[i]);
450           }
451       }
452
453   g_signal_emit (ds, signals[BACKEND_CHANGED], 0);
454 }
455
456
457 /**
458  * psppire_data_store_replace_set_dictionary:
459  * @data_store: The variable store
460  * @dict: The dictionary to set
461  *
462  * If a dictionary is already associated with the data-store, then it will be
463  * destroyed.
464  **/
465 void
466 psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *dict)
467 {
468   int i;
469
470   /* Disconnect any existing handlers */
471   if ( data_store->dict )
472     for (i = 0 ; i < n_dict_signals; ++i )
473       {
474         g_signal_handler_disconnect (data_store->dict,
475                                      data_store->dict_handler_id[i]);
476       }
477
478   data_store->dict = dict;
479
480   if ( dict != NULL)
481     {
482
483       data_store->dict_handler_id [VARIABLE_INSERTED] =
484         g_signal_connect (dict, "variable-inserted",
485                           G_CALLBACK (insert_variable_callback),
486                           data_store);
487
488       data_store->dict_handler_id [VARIABLE_DELETED] =
489         g_signal_connect (dict, "variable-deleted",
490                           G_CALLBACK (delete_variable_callback),
491                           data_store);
492
493       data_store->dict_handler_id [VARIABLE_CHANGED] =
494         g_signal_connect (dict, "variable-changed",
495                           G_CALLBACK (variable_changed_callback),
496                           data_store);
497
498       data_store->dict_handler_id [SIZE_CHANGED] =
499         g_signal_connect (dict, "dict-size-changed",
500                           G_CALLBACK (dict_size_change_callback),
501                           data_store);
502     }
503
504
505
506   /* The entire model has changed */
507   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (data_store), -1, -1, -1, -1);
508
509 #if AXIS_TRANSITION
510   psppire_sheet_column_columns_changed (PSPPIRE_SHEET_COLUMN (data_store), 0, -1);
511 #endif
512
513   if ( data_store->dict )
514     for (i = 0 ; i < n_dict_signals; ++i )
515       {
516         if ( data_store->dict_handler_id [i] > 0)
517           {
518             g_signal_handler_block (data_store->dict,
519                                     data_store->dict_handler_id[i]);
520           }
521       }
522 }
523
524 static void
525 psppire_data_store_finalize (GObject *object)
526 {
527
528   /* must chain up */
529   (* parent_class->finalize) (object);
530 }
531
532
533 static void
534 psppire_data_store_dispose (GObject *object)
535 {
536   PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
537
538   if (ds->dispose_has_run)
539     return;
540
541   if (ds->datasheet)
542     {
543       datasheet_destroy (ds->datasheet);
544       ds->datasheet = NULL;
545     }
546
547   /* must chain up */
548   (* parent_class->dispose) (object);
549
550   ds->dispose_has_run = TRUE;
551 }
552
553
554
555 /* Insert a blank case before POSN */
556 gboolean
557 psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn)
558 {
559   gboolean result;
560   const struct caseproto *proto;
561   struct ccase *cc;
562   g_return_val_if_fail (ds, FALSE);
563
564   proto = datasheet_get_proto (ds->datasheet);
565   g_return_val_if_fail (caseproto_get_n_widths (proto) > 0, FALSE);
566   g_return_val_if_fail (posn <= psppire_data_store_get_case_count (ds), FALSE);
567
568   cc = case_create (proto);
569   case_set_missing (cc);
570
571   result = psppire_data_store_insert_case (ds, cc, posn);
572
573   case_unref (cc);
574
575   return result;
576 }
577
578
579 gchar *
580 psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column)
581 {
582   gint idx;
583   char *text;
584   const struct fmt_spec *fp ;
585   const struct variable *pv ;
586   const struct dictionary *dict;
587   union value v;
588   int width;
589
590   g_return_val_if_fail (store->dict, NULL);
591   g_return_val_if_fail (store->datasheet, NULL);
592
593   dict = store->dict->dict;
594
595   if (column >= psppire_dict_get_var_cnt (store->dict))
596     return NULL;
597
598   if ( row >= psppire_data_store_get_case_count (store))
599     return NULL;
600
601   pv = psppire_dict_get_variable (store->dict, column);
602
603   g_assert (pv);
604
605   idx = var_get_case_index (pv);
606   width = var_get_width (pv);
607
608   g_assert (idx >= 0);
609
610   value_init (&v, width);
611   if (!psppire_data_store_get_value (store, row, idx, &v))
612     return NULL;
613
614   if ( store->show_labels)
615     {
616       const gchar *label = var_lookup_value_label (pv, &v);
617       if (label)
618         {
619           value_destroy (&v, width);
620           return g_strdup (label);
621         }
622     }
623
624   fp = var_get_write_format (pv);
625
626   text = data_out (&v, dict_get_encoding (dict), fp);
627
628   g_strchomp (text);
629
630   value_destroy (&v, width);
631   return text;
632 }
633
634
635 static gboolean
636 psppire_data_store_clear_datum (PsppireSheetModel *model,
637                                           glong row, glong col)
638 {
639   PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
640
641   union value v;
642   const struct variable *pv = psppire_dict_get_variable (store->dict, col);
643   int width = var_get_width (pv);
644
645   const gint index = var_get_case_index (pv) ;
646
647   value_init (&v, width);
648   value_set_missing (&v, width);
649   psppire_data_store_set_value (store, row, index, &v);
650   value_destroy (&v, width);
651
652   psppire_sheet_model_range_changed (model, row, col, row, col);
653
654
655   return TRUE;
656 }
657
658
659 /* Attempts to update that part of the variable store which corresponds
660    to ROW, COL with  the value TEXT.
661    Returns true if anything was updated, false otherwise.
662 */
663 gboolean
664 psppire_data_store_set_string (PsppireDataStore *store,
665                                const gchar *text, glong row, glong col)
666 {
667   gchar *s;
668   glong n_cases;
669   const struct variable *pv = psppire_dict_get_variable (store->dict, col);
670   if ( NULL == pv)
671     return FALSE;
672
673   n_cases = psppire_data_store_get_case_count (store);
674
675   if ( row > n_cases)
676     return FALSE;
677
678   if (row == n_cases)
679     psppire_data_store_insert_new_case (store, row);
680
681   s = recode_string (psppire_dict_encoding (store->dict), UTF8, text, -1);
682
683   psppire_data_store_data_in (store, row,
684                               var_get_case_index (pv), ss_cstr (s),
685                               var_get_write_format (pv));
686   free (s);
687
688   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), row, col, row, col);
689
690   return TRUE;
691 }
692
693
694
695 void
696 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
697 {
698   g_return_if_fail (store);
699   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
700
701   store->show_labels = show_labels;
702
703   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store),
704                                  -1, -1, -1, -1);
705 }
706
707
708 void
709 psppire_data_store_clear (PsppireDataStore *ds)
710 {
711   datasheet_destroy (ds->datasheet);
712   ds->datasheet = NULL;
713
714   psppire_dict_clear (ds->dict);
715
716   g_signal_emit (ds, signals [CASES_DELETED], 0, 0, -1);
717 }
718
719
720
721 /* Return a casereader made from this datastore */
722 struct casereader *
723 psppire_data_store_get_reader (PsppireDataStore *ds)
724 {
725   int i;
726   struct casereader *reader ;
727
728   if ( ds->dict )
729     for (i = 0 ; i < n_dict_signals; ++i )
730       {
731         g_signal_handler_block (ds->dict,
732                                 ds->dict_handler_id[i]);
733       }
734
735   reader = datasheet_make_reader (ds->datasheet);
736
737   /* We must not reference this again */
738   ds->datasheet = NULL;
739
740   return reader;
741 }
742
743
744
745 /* Column related funcs */
746
747
748 static const gchar null_var_name[]=N_("var");
749
750 \f
751
752 /* Row related funcs */
753
754 static gchar *
755 get_row_button_label (const PsppireSheetModel *model, gint unit)
756 {
757   // PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
758
759   return g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER);
760 }
761
762
763 static gboolean
764 get_row_sensitivity (const PsppireSheetModel *model, gint unit)
765 {
766   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
767
768   return (unit < psppire_data_store_get_case_count (ds));
769 }
770
771
772 \f
773
774 /* Column related stuff */
775
776 static gchar *
777 get_column_subtitle (const PsppireSheetModel *model, gint col)
778 {
779   const struct variable *v ;
780   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
781
782   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
783     return NULL;
784
785   v = psppire_dict_get_variable (ds->dict, col);
786
787   if ( ! var_has_label (v))
788     return NULL;
789
790   return xstrdup (var_get_label (v));
791 }
792
793 static gchar *
794 get_column_button_label (const PsppireSheetModel *model, gint col)
795 {
796   struct variable *pv ;
797   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
798
799   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
800     return g_locale_to_utf8 (null_var_name, -1, 0, 0, 0);
801
802   pv = psppire_dict_get_variable (ds->dict, col);
803
804   if (NULL == pv)
805     return NULL;
806
807   return xstrdup (var_get_name (pv));
808 }
809
810 static gboolean
811 get_column_sensitivity (const PsppireSheetModel *model, gint col)
812 {
813   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
814
815   return (col < psppire_dict_get_var_cnt (ds->dict));
816 }
817
818
819
820 static GtkJustification
821 get_column_justification (const PsppireSheetModel *model, gint col)
822 {
823   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
824   const struct variable *pv ;
825
826   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
827     return GTK_JUSTIFY_LEFT;
828
829   pv = psppire_dict_get_variable (ds->dict, col);
830
831   return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
832           : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
833           : GTK_JUSTIFY_CENTER);
834 }
835
836
837
838 \f
839
840
841 /* Returns the CASENUMth case, or a null pointer on failure.
842  */
843 struct ccase *
844 psppire_data_store_get_case (const PsppireDataStore *ds,
845                              casenumber casenum)
846 {
847   g_return_val_if_fail (ds, FALSE);
848   g_return_val_if_fail (ds->datasheet, FALSE);
849
850   return datasheet_get_row (ds->datasheet, casenum);
851 }
852
853
854 gboolean
855 psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first,
856                                  casenumber n_cases)
857 {
858   g_return_val_if_fail (ds, FALSE);
859   g_return_val_if_fail (ds->datasheet, FALSE);
860
861   g_return_val_if_fail (first + n_cases <=
862                         psppire_data_store_get_case_count (ds), FALSE);
863
864
865   datasheet_delete_rows (ds->datasheet, first, n_cases);
866
867   g_signal_emit (ds, signals [CASES_DELETED], 0, first, n_cases);
868   psppire_sheet_model_rows_deleted (PSPPIRE_SHEET_MODEL (ds), first, n_cases);
869
870   return TRUE;
871 }
872
873
874
875 /* Insert case CC into the case file before POSN */
876 static gboolean
877 psppire_data_store_insert_case (PsppireDataStore *ds,
878                                 struct ccase *cc,
879                                 casenumber posn)
880 {
881   bool result ;
882
883   g_return_val_if_fail (ds, FALSE);
884   g_return_val_if_fail (ds->datasheet, FALSE);
885
886   case_ref (cc);
887   result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
888
889   if ( result )
890     {
891       g_signal_emit (ds, signals [CASE_INSERTED], 0, posn);
892       psppire_sheet_model_rows_inserted (PSPPIRE_SHEET_MODEL (ds), posn, 1);
893     }
894   else
895     g_warning ("Cannot insert case at position %ld\n", posn);
896
897   return result;
898 }
899
900
901 /* Copies the IDXth value from case CASENUM into VALUE, which
902    must be of the correct width for IDX.
903    Returns true if successful, false on failure. */
904 static bool
905 psppire_data_store_get_value (const PsppireDataStore *ds,
906                               casenumber casenum, size_t idx,
907                               union value *value)
908 {
909   g_return_val_if_fail (ds, false);
910   g_return_val_if_fail (ds->datasheet, false);
911   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), false);
912
913   return datasheet_get_value (ds->datasheet, casenum, idx, value);
914 }
915
916
917
918 /* Set the IDXth value of case C to V.
919    V must be the correct width for IDX.
920    Returns true if successful, false on I/O error. */
921 static gboolean
922 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
923                               gint idx, union value *v)
924 {
925   bool ok;
926
927   g_return_val_if_fail (ds, FALSE);
928   g_return_val_if_fail (ds->datasheet, FALSE);
929
930   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
931
932   ok = datasheet_put_value (ds->datasheet, casenum, idx, v);
933   if (ok)
934     g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
935
936   return ok;
937 }
938
939
940
941
942 /* Set the IDXth value of case C using D_IN */
943 static gboolean
944 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
945                             struct substring input, const struct fmt_spec *fmt)
946 {
947   union value value;
948   int width;
949   bool ok;
950
951   g_return_val_if_fail (ds, FALSE);
952   g_return_val_if_fail (ds->datasheet, FALSE);
953
954   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
955
956   width = fmt_var_width (fmt);
957   g_return_val_if_fail (caseproto_get_width (
958                           datasheet_get_proto (ds->datasheet), idx) == width,
959                         FALSE);
960   value_init (&value, width);
961   ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
962         && data_in (input, LEGACY_NATIVE, fmt->type, 0, 0, 0, &value, width)
963         && datasheet_put_value (ds->datasheet, casenum, idx, &value));
964   value_destroy (&value, width);
965
966   if (ok)
967     g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
968
969   return ok;
970 }
971
972 /* Resize the cases in the casefile, by inserting a value of the
973    given WIDTH into every one of them at the position immediately
974    preceding WHERE.
975 */
976 static gboolean
977 psppire_data_store_insert_value (PsppireDataStore *ds,
978                                  gint width, gint where)
979 {
980   union value value;
981
982   g_return_val_if_fail (ds, FALSE);
983
984   g_assert (width >= 0);
985
986   if ( ! ds->datasheet )
987     ds->datasheet = datasheet_create (NULL);
988
989   value_init (&value, width);
990   if (width == 0)
991     value.f = 0;
992   else
993     value_set_missing (&value, width);
994
995   datasheet_insert_column (ds->datasheet, &value, width, where);
996
997   return TRUE;
998 }
999
1000 static gboolean
1001 get_row_overstrike (const PsppireSheetModel *model, gint row)
1002 {
1003   union value val;
1004   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
1005
1006   const struct dictionary *dict = ds->dict->dict;
1007
1008   const struct variable *filter = dict_get_filter (dict);
1009
1010   if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
1011     return FALSE;
1012
1013   if ( ! filter)
1014     return FALSE;
1015
1016   g_assert (var_is_numeric (filter));
1017
1018   value_init (&val, 0);
1019   if ( ! datasheet_get_value (ds->datasheet, row,
1020                               var_get_case_index (filter),
1021                               &val) )
1022     return FALSE;
1023
1024
1025   return (val.f == 0.0);
1026 }