0e378bbec1f989cea8b6c94e971e262ab69d9db5
[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   union value v;
587   int width;
588   GString *s;
589
590   g_return_val_if_fail (store->dict, NULL);
591   g_return_val_if_fail (store->datasheet, NULL);
592
593   if (column >= psppire_dict_get_var_cnt (store->dict))
594     return NULL;
595
596   if ( row >= psppire_data_store_get_case_count (store))
597     return NULL;
598
599   pv = psppire_dict_get_variable (store->dict, column);
600
601   g_assert (pv);
602
603   idx = var_get_case_index (pv);
604   width = var_get_width (pv);
605
606   g_assert (idx >= 0);
607
608   value_init (&v, width);
609   if (!psppire_data_store_get_value (store, row, idx, &v))
610     return NULL;
611
612   if ( store->show_labels)
613     {
614       const gchar *label = var_lookup_value_label (pv, &v);
615       if (label)
616         {
617           value_destroy (&v, width);
618           return recode_string (UTF8, psppire_dict_encoding (store->dict),
619                                 label, -1);
620         }
621     }
622
623   fp = var_get_write_format (pv);
624
625   s = g_string_sized_new (fp->w + 1);
626   g_string_set_size (s, fp->w);
627
628   memset (s->str, 0, fp->w);
629
630   g_assert (fp->w == s->len);
631
632   /* Converts binary value V into printable form in the exactly
633      FP->W character in buffer S according to format specification
634      FP.  No null terminator is appended to the buffer.  */
635   data_out (&v, fp, s->str);
636
637   text = recode_string (UTF8, psppire_dict_encoding (store->dict),
638                         s->str, fp->w);
639   g_string_free (s, TRUE);
640
641   g_strchomp (text);
642
643   value_destroy (&v, width);
644   return text;
645 }
646
647
648 static gboolean
649 psppire_data_store_clear_datum (PsppireSheetModel *model,
650                                           glong row, glong col)
651 {
652   PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
653
654   union value v;
655   const struct variable *pv = psppire_dict_get_variable (store->dict, col);
656   int width = var_get_width (pv);
657
658   const gint index = var_get_case_index (pv) ;
659
660   value_init (&v, width);
661   value_set_missing (&v, width);
662   psppire_data_store_set_value (store, row, index, &v);
663   value_destroy (&v, width);
664
665   psppire_sheet_model_range_changed (model, row, col, row, col);
666
667
668   return TRUE;
669 }
670
671
672 /* Attempts to update that part of the variable store which corresponds
673    to ROW, COL with  the value TEXT.
674    Returns true if anything was updated, false otherwise.
675 */
676 gboolean
677 psppire_data_store_set_string (PsppireDataStore *store,
678                                const gchar *text, glong row, glong col)
679 {
680   gchar *s;
681   glong n_cases;
682   const struct variable *pv = psppire_dict_get_variable (store->dict, col);
683   if ( NULL == pv)
684     return FALSE;
685
686   n_cases = psppire_data_store_get_case_count (store);
687
688   if ( row > n_cases)
689     return FALSE;
690
691   if (row == n_cases)
692     psppire_data_store_insert_new_case (store, row);
693
694   s = recode_string (psppire_dict_encoding (store->dict), UTF8, text, -1);
695
696   psppire_data_store_data_in (store, row,
697                               var_get_case_index (pv), ss_cstr (s),
698                               var_get_write_format (pv));
699   free (s);
700
701   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store), row, col, row, col);
702
703   return TRUE;
704 }
705
706
707
708 void
709 psppire_data_store_show_labels (PsppireDataStore *store, gboolean show_labels)
710 {
711   g_return_if_fail (store);
712   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
713
714   store->show_labels = show_labels;
715
716   psppire_sheet_model_range_changed (PSPPIRE_SHEET_MODEL (store),
717                                  -1, -1, -1, -1);
718 }
719
720
721 void
722 psppire_data_store_clear (PsppireDataStore *ds)
723 {
724   datasheet_destroy (ds->datasheet);
725   ds->datasheet = NULL;
726
727   psppire_dict_clear (ds->dict);
728
729   g_signal_emit (ds, signals [CASES_DELETED], 0, 0, -1);
730 }
731
732
733
734 /* Return a casereader made from this datastore */
735 struct casereader *
736 psppire_data_store_get_reader (PsppireDataStore *ds)
737 {
738   int i;
739   struct casereader *reader ;
740
741   if ( ds->dict )
742     for (i = 0 ; i < n_dict_signals; ++i )
743       {
744         g_signal_handler_block (ds->dict,
745                                 ds->dict_handler_id[i]);
746       }
747
748   reader = datasheet_make_reader (ds->datasheet);
749
750   /* We must not reference this again */
751   ds->datasheet = NULL;
752
753   return reader;
754 }
755
756
757
758 /* Column related funcs */
759
760
761 static const gchar null_var_name[]=N_("var");
762
763 \f
764
765 /* Row related funcs */
766
767 static gchar *
768 get_row_button_label (const PsppireSheetModel *model, gint unit)
769 {
770   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
771   gchar *s = g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER);
772
773   gchar *text =  recode_string (UTF8, psppire_dict_encoding (ds->dict),
774                                 s, -1);
775
776   g_free (s);
777
778   return text;
779 }
780
781
782 static gboolean
783 get_row_sensitivity (const PsppireSheetModel *model, gint unit)
784 {
785   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
786
787   return (unit < psppire_data_store_get_case_count (ds));
788 }
789
790
791 \f
792
793 /* Column related stuff */
794
795 static gchar *
796 get_column_subtitle (const PsppireSheetModel *model, gint col)
797 {
798   const struct variable *v ;
799   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
800
801   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
802     return NULL;
803
804   v = psppire_dict_get_variable (ds->dict, col);
805
806   if ( ! var_has_label (v))
807     return NULL;
808
809   return xstrdup (var_get_label (v));
810 }
811
812 static gchar *
813 get_column_button_label (const PsppireSheetModel *model, gint col)
814 {
815   struct variable *pv ;
816   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
817
818   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
819     return g_locale_to_utf8 (null_var_name, -1, 0, 0, 0);
820
821   pv = psppire_dict_get_variable (ds->dict, col);
822
823   return xstrdup (var_get_name (pv));
824 }
825
826 static gboolean
827 get_column_sensitivity (const PsppireSheetModel *model, gint col)
828 {
829   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
830
831   return (col < psppire_dict_get_var_cnt (ds->dict));
832 }
833
834
835
836 static GtkJustification
837 get_column_justification (const PsppireSheetModel *model, gint col)
838 {
839   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
840   const struct variable *pv ;
841
842   if ( col >= psppire_dict_get_var_cnt (ds->dict) )
843     return GTK_JUSTIFY_LEFT;
844
845   pv = psppire_dict_get_variable (ds->dict, col);
846
847   return (var_get_alignment (pv) == ALIGN_LEFT ? GTK_JUSTIFY_LEFT
848           : var_get_alignment (pv) == ALIGN_RIGHT ? GTK_JUSTIFY_RIGHT
849           : GTK_JUSTIFY_CENTER);
850 }
851
852
853
854 \f
855
856
857 /* Returns the CASENUMth case, or a null pointer on failure.
858  */
859 struct ccase *
860 psppire_data_store_get_case (const PsppireDataStore *ds,
861                              casenumber casenum)
862 {
863   g_return_val_if_fail (ds, FALSE);
864   g_return_val_if_fail (ds->datasheet, FALSE);
865
866   return datasheet_get_row (ds->datasheet, casenum);
867 }
868
869
870 gboolean
871 psppire_data_store_delete_cases (PsppireDataStore *ds, casenumber first,
872                                  casenumber n_cases)
873 {
874   g_return_val_if_fail (ds, FALSE);
875   g_return_val_if_fail (ds->datasheet, FALSE);
876
877   g_return_val_if_fail (first + n_cases <=
878                         psppire_data_store_get_case_count (ds), FALSE);
879
880
881   datasheet_delete_rows (ds->datasheet, first, n_cases);
882
883   g_signal_emit (ds, signals [CASES_DELETED], 0, first, n_cases);
884   psppire_sheet_model_rows_deleted (PSPPIRE_SHEET_MODEL (ds), first, n_cases);
885
886   return TRUE;
887 }
888
889
890
891 /* Insert case CC into the case file before POSN */
892 static gboolean
893 psppire_data_store_insert_case (PsppireDataStore *ds,
894                                 struct ccase *cc,
895                                 casenumber posn)
896 {
897   bool result ;
898
899   g_return_val_if_fail (ds, FALSE);
900   g_return_val_if_fail (ds->datasheet, FALSE);
901
902   case_ref (cc);
903   result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
904
905   if ( result )
906     {
907       g_signal_emit (ds, signals [CASE_INSERTED], 0, posn);
908       psppire_sheet_model_rows_inserted (PSPPIRE_SHEET_MODEL (ds), posn, 1);
909     }
910   else
911     g_warning ("Cannot insert case at position %ld\n", posn);
912
913   return result;
914 }
915
916
917 /* Copies the IDXth value from case CASENUM into VALUE, which
918    must be of the correct width for IDX.
919    Returns true if successful, false on failure. */
920 static bool
921 psppire_data_store_get_value (const PsppireDataStore *ds,
922                               casenumber casenum, size_t idx,
923                               union value *value)
924 {
925   g_return_val_if_fail (ds, false);
926   g_return_val_if_fail (ds->datasheet, false);
927   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), false);
928
929   return datasheet_get_value (ds->datasheet, casenum, idx, value);
930 }
931
932
933
934 /* Set the IDXth value of case C to V.
935    V must be the correct width for IDX.
936    Returns true if successful, false on I/O error. */
937 static gboolean
938 psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
939                               gint idx, union value *v)
940 {
941   bool ok;
942
943   g_return_val_if_fail (ds, FALSE);
944   g_return_val_if_fail (ds->datasheet, FALSE);
945
946   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
947
948   ok = datasheet_put_value (ds->datasheet, casenum, idx, v);
949   if (ok)
950     g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
951
952   return ok;
953 }
954
955
956
957
958 /* Set the IDXth value of case C using D_IN */
959 static gboolean
960 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
961                             struct substring input, const struct fmt_spec *fmt)
962 {
963   union value value;
964   int width;
965   bool ok;
966
967   g_return_val_if_fail (ds, FALSE);
968   g_return_val_if_fail (ds->datasheet, FALSE);
969
970   g_return_val_if_fail (idx < datasheet_get_n_columns (ds->datasheet), FALSE);
971
972   width = fmt_var_width (fmt);
973   g_return_val_if_fail (caseproto_get_width (
974                           datasheet_get_proto (ds->datasheet), idx) == width,
975                         FALSE);
976   value_init (&value, width);
977   ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
978         && data_in (input, LEGACY_NATIVE, fmt->type, 0, 0, 0, &value, width)
979         && datasheet_put_value (ds->datasheet, casenum, idx, &value));
980   value_destroy (&value, width);
981
982   if (ok)
983     g_signal_emit (ds, signals [CASE_CHANGED], 0, casenum);
984
985   return ok;
986 }
987
988 /* Resize the cases in the casefile, by inserting a value of the
989    given WIDTH into every one of them at the position immediately
990    preceding WHERE.
991 */
992 static gboolean
993 psppire_data_store_insert_value (PsppireDataStore *ds,
994                                  gint width, gint where)
995 {
996   union value value;
997
998   g_return_val_if_fail (ds, FALSE);
999
1000   g_assert (width >= 0);
1001
1002   if ( ! ds->datasheet )
1003     ds->datasheet = datasheet_create (NULL);
1004
1005   value_init (&value, width);
1006   if (width == 0)
1007     value.f = 0;
1008   else
1009     value_set_missing (&value, width);
1010
1011   datasheet_insert_column (ds->datasheet, &value, width, where);
1012
1013   return TRUE;
1014 }
1015
1016 static gboolean
1017 get_row_overstrike (const PsppireSheetModel *model, gint row)
1018 {
1019   union value val;
1020   PsppireDataStore *ds = PSPPIRE_DATA_STORE (model);
1021
1022   const struct dictionary *dict = ds->dict->dict;
1023
1024   const struct variable *filter = dict_get_filter (dict);
1025
1026   if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
1027     return FALSE;
1028
1029   if ( ! filter)
1030     return FALSE;
1031
1032   g_assert (var_is_numeric (filter));
1033
1034   value_init (&val, 0);
1035   if ( ! datasheet_get_value (ds->datasheet, row,
1036                               var_get_case_index (filter),
1037                               &val) )
1038     return FALSE;
1039
1040
1041   return (val.f == 0.0);
1042 }