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