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