Whitespace changes only.
[pspp-builds.git] / src / ui / gui / psppire-dict.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2004, 2006  Free Software Foundation
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20 #include <config.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include <gtk/gtk.h>
25 #include <gtksheet/gtkextra-marshal.h>
26
27 #include "psppire-object.h"
28 #include "psppire-dict.h"
29 #include <data/format.h>
30 #include <data/dictionary.h>
31 #include <data/missing-values.h>
32 #include <data/value-labels.h>
33 #include <data/variable.h>
34
35 #include "message-dialog.h"
36
37 /* --- prototypes --- */
38 static void psppire_dict_class_init     (PsppireDictClass       *class);
39 static void psppire_dict_init   (PsppireDict            *dict);
40 static void psppire_dict_finalize       (GObject                *object);
41
42 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
43
44
45 /* --- variables --- */
46 static GObjectClass     *parent_class = NULL;
47
48 enum  {VARIABLE_CHANGED,
49        VARIABLE_RESIZED,
50        VARIABLE_INSERTED,
51        VARIABLES_DELETED,
52        n_SIGNALS};
53
54 static guint signal[n_SIGNALS];
55
56 #define CACHE_CHUNK 5
57
58 /* --- functions --- */
59 /**
60  * psppire_dict_get_type:
61  * @returns: the type ID for accelerator groups.
62  */
63 GType
64 psppire_dict_get_type (void)
65 {
66   static GType object_type = 0;
67
68   if (!object_type)
69     {
70       static const GTypeInfo object_info = {
71         sizeof (PsppireDictClass),
72         (GBaseInitFunc) NULL,
73         (GBaseFinalizeFunc) NULL,
74         (GClassInitFunc) psppire_dict_class_init,
75         NULL,   /* class_finalize */
76         NULL,   /* class_data */
77         sizeof (PsppireDict),
78         0,      /* n_preallocs */
79         (GInstanceInitFunc) psppire_dict_init,
80       };
81
82       static const GInterfaceInfo tree_model_info = {
83         (GInterfaceInitFunc) dictionary_tree_model_init,
84         NULL,
85         NULL
86       };
87
88       object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT,
89                                             "PsppireDict",
90                                             &object_info, 0);
91
92       g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
93                                   &tree_model_info);
94
95
96     }
97
98   return object_type;
99 }
100
101
102 static void
103 psppire_dict_class_init (PsppireDictClass *class)
104 {
105   GObjectClass *object_class = G_OBJECT_CLASS (class);
106
107   parent_class = g_type_class_peek_parent (class);
108
109   object_class->finalize = psppire_dict_finalize;
110
111   signal[VARIABLE_CHANGED] =
112     g_signal_new ("variable_changed",
113                   G_TYPE_FROM_CLASS (class),
114                   G_SIGNAL_RUN_FIRST,
115                   0,
116                   NULL, NULL,
117                   g_cclosure_marshal_VOID__INT,
118                   G_TYPE_NONE,
119                   1,
120                   G_TYPE_INT);
121
122
123
124   signal[VARIABLE_INSERTED] =
125     g_signal_new ("variable_inserted",
126                   G_TYPE_FROM_CLASS (class),
127                   G_SIGNAL_RUN_FIRST,
128                   0,
129                   NULL, NULL,
130                   g_cclosure_marshal_VOID__INT,
131                   G_TYPE_NONE,
132                   1,
133                   G_TYPE_INT);
134
135
136   signal[VARIABLES_DELETED] =
137     g_signal_new ("variables_deleted",
138                   G_TYPE_FROM_CLASS (class),
139                   G_SIGNAL_RUN_FIRST,
140                   0,
141                   NULL, NULL,
142                   gtkextra_VOID__INT_INT,
143                   G_TYPE_NONE,
144                   2,
145                   G_TYPE_INT,
146                   G_TYPE_INT);
147
148
149   signal[VARIABLE_RESIZED] =
150     g_signal_new ("dict-size-changed",
151                   G_TYPE_FROM_CLASS (class),
152                   G_SIGNAL_RUN_FIRST,
153                   0,
154                   NULL, NULL,
155                   gtkextra_VOID__INT_INT,
156                   G_TYPE_NONE,
157                   2,
158                   G_TYPE_INT,
159                   G_TYPE_INT);
160
161 }
162
163 static void
164 psppire_dict_finalize (GObject *object)
165 {
166   PsppireDict *d = PSPPIRE_DICT (object);
167
168   dict_destroy (d->dict);
169
170   G_OBJECT_CLASS (parent_class)->finalize (object);
171 }
172
173 /* Pass on callbacks from src/data/dictionary, as
174    signals in the Gtk library */
175 static void
176 addcb (struct dictionary *d, int idx, void *pd)
177 {
178   g_signal_emit (pd, signal[VARIABLE_INSERTED], 0, idx);
179 }
180
181 static void
182 delcb (struct dictionary *d, int idx, void *pd)
183 {
184   g_signal_emit (pd, signal[VARIABLES_DELETED], 0, idx, 1);
185 }
186
187 static void
188 mutcb (struct dictionary *d, int idx, void *pd)
189 {
190   g_signal_emit (pd, signal[VARIABLE_CHANGED], 0, idx);
191 }
192
193 static const struct dict_callbacks gui_callbacks =
194   {
195     addcb,
196     delcb,
197     mutcb
198   };
199
200 static void
201 psppire_dict_init (PsppireDict *psppire_dict)
202 {
203   psppire_dict->stamp = g_random_int ();
204 }
205
206 /**
207  * psppire_dict_new_from_dict:
208  * @returns: a new #PsppireDict object
209  *
210  * Creates a new #PsppireDict.
211  */
212 PsppireDict*
213 psppire_dict_new_from_dict (struct dictionary *d)
214 {
215   PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
216   new_dict->dict = d;
217
218   dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
219
220   return new_dict;
221 }
222
223
224 /* Returns a valid name for a new variable in DICT.
225    The return value is statically allocated */
226 static gchar *
227 auto_generate_var_name (PsppireDict *dict)
228 {
229   gint d = 0;
230   static gchar name[10];
231
232   while (g_snprintf (name, 10, "VAR%05d",d++),
233          psppire_dict_lookup_var (dict, name))
234     ;
235
236   return name;
237 }
238
239 /* Insert a new variable at posn IDX, with the name NAME.
240    If NAME is null, then a name will be automatically assigned.
241  */
242 void
243 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
244 {
245   struct variable *var ;
246   g_return_if_fail (d);
247   g_return_if_fail (G_IS_PSPPIRE_DICT (d));
248
249
250   if ( ! name )
251     name = auto_generate_var_name (d);
252
253   var = dict_create_var (d->dict, name, 0);
254
255   dict_reorder_var (d->dict, var, idx);
256
257   g_signal_emit (d, signal[VARIABLE_INSERTED], 0, idx );
258 }
259
260 /* Delete N variables beginning at FIRST */
261 void
262 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
263 {
264   gint idx;
265   g_return_if_fail (d);
266   g_return_if_fail (d->dict);
267   g_return_if_fail (G_IS_PSPPIRE_DICT (d));
268
269   for (idx = 0 ; idx < n ; ++idx )
270     {
271       struct variable *var;
272
273       /* Do nothing if it's out of bounds */
274       if ( first >= dict_get_var_cnt (d->dict))
275         break;
276
277       var = dict_get_var (d->dict, first);
278       dict_delete_var (d->dict, var);
279     }
280   dict_compact_values (d->dict);
281
282   g_signal_emit (d, signal[VARIABLES_DELETED], 0, first, idx );
283 }
284
285
286 void
287 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
288 {
289   struct variable *var;
290   g_assert (d);
291   g_assert (G_IS_PSPPIRE_DICT (d));
292
293
294   if ( idx < dict_get_var_cnt (d->dict))
295     {
296       /* This is an existing variable? */
297       var = dict_get_var (d->dict, idx);
298       dict_rename_var (d->dict, var, name);
299       g_signal_emit (d, signal[VARIABLE_CHANGED], 0, idx);
300     }
301   else
302     {
303       /* new variable */
304       dict_create_var (d->dict, name, 0);
305       g_signal_emit (d, signal[VARIABLE_INSERTED], 0, idx);
306     }
307 }
308
309
310
311 /* Return the IDXth variable */
312 struct variable *
313 psppire_dict_get_variable (PsppireDict *d, gint idx)
314 {
315   g_return_val_if_fail (d, NULL);
316   g_return_val_if_fail (d->dict, NULL);
317
318   if ( dict_get_var_cnt (d->dict) <= idx )
319     return NULL;
320
321   return dict_get_var (d->dict, idx);
322 }
323
324
325 /* Return the number of variables in the dictionary */
326 gint
327 psppire_dict_get_var_cnt (const PsppireDict *d)
328 {
329   g_return_val_if_fail (d, -1);
330   g_return_val_if_fail (d->dict, -1);
331
332   return dict_get_var_cnt (d->dict);
333 }
334
335
336 /* Return a variable by name.
337    Return NULL if it doesn't exist
338 */
339 struct variable *
340 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
341 {
342   g_return_val_if_fail (d, NULL);
343   g_return_val_if_fail (d->dict, NULL);
344
345   return dict_lookup_var (d->dict, name);
346 }
347
348
349 void
350 psppire_dict_var_changed (PsppireDict *d, gint idx)
351 {
352   g_return_if_fail (d);
353
354   g_signal_emit (d, signal[VARIABLE_CHANGED], 0, idx);
355 }
356
357
358 /* Clears the contents of D */
359 void
360 psppire_dict_clear (PsppireDict *d)
361 {
362   g_return_if_fail (d);
363   g_return_if_fail (d->dict);
364
365   {
366     const gint n_vars = dict_get_var_cnt (d->dict);
367
368     dict_clear (d->dict);
369
370     g_signal_emit (d, signal[VARIABLES_DELETED], 0, 0, n_vars );
371   }
372 }
373
374
375
376 /* Return true is NAME would be a valid name of a variable to add to the
377    dictionary.  False otherwise.
378    If REPORT is true, then invalid names will be reported as such as errors
379 */
380 gboolean
381 psppire_dict_check_name (const PsppireDict *dict,
382                      const gchar *name, gboolean report)
383 {
384   if ( ! var_is_valid_name (name, report ) )
385       return FALSE;
386
387   if (psppire_dict_lookup_var (dict, name))
388     {
389       if ( report )
390         msg (ME,"Duplicate variable name.");
391       return FALSE;
392     }
393
394   return TRUE;
395 }
396
397
398 inline gint
399 psppire_dict_get_next_value_idx (const PsppireDict *dict)
400 {
401   return dict_get_next_value_idx (dict->dict);
402 }
403
404
405 void
406 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
407                               gint old_size, gint new_size)
408 {
409   gint fv;
410   g_return_if_fail (d);
411   g_return_if_fail (d->dict);
412
413   if ( old_size == new_size )
414     return ;
415
416   dict_compact_values (d->dict);
417
418   fv = var_get_case_index (pv);
419
420   g_signal_emit (d, signal[VARIABLE_RESIZED], 0,
421                 fv + old_size,
422                 new_size - old_size );
423 }
424
425
426 /* Tree Model Stuff */
427
428 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
429
430 static gint tree_model_n_columns (GtkTreeModel *model);
431
432 static GType tree_model_column_type (GtkTreeModel *model, gint index);
433
434 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
435                                      GtkTreePath *path);
436
437 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
438
439 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
440                                           GtkTreeIter *iter);
441
442 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
443                                   gint column, GValue *value);
444
445 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
446                                       GtkTreeIter *parent, gint n);
447
448
449 static void
450 dictionary_tree_model_init (GtkTreeModelIface *iface)
451 {
452   iface->get_flags = tree_model_get_flags;
453   iface->get_n_columns = tree_model_n_columns;
454   iface->get_column_type = tree_model_column_type;
455   iface->get_iter = tree_model_get_iter;
456   iface->iter_next = tree_model_iter_next;
457   iface->get_path = tree_model_get_path;
458   iface->get_value = tree_model_get_value;
459
460   iface->iter_children = 0;
461   iface->iter_has_child =0;
462   iface->iter_n_children =0;
463   iface->iter_nth_child = tree_model_nth_child ;
464   iface->iter_parent =0;
465 }
466
467 static GtkTreeModelFlags
468 tree_model_get_flags (GtkTreeModel *model)
469 {
470   g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GtkTreeModelFlags) 0);
471
472   return GTK_TREE_MODEL_LIST_ONLY;
473 }
474
475
476 static gint
477 tree_model_n_columns (GtkTreeModel *model)
478 {
479   return n_DICT_COLS;
480 }
481
482 static GType
483 tree_model_column_type (GtkTreeModel *model, gint index)
484 {
485   g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), (GType) 0);
486
487   switch (index)
488     {
489     case DICT_TVM_COL_NAME:
490       return G_TYPE_STRING;
491       break;
492     case DICT_TVM_COL_VAR:
493       return G_TYPE_POINTER;
494       break;
495     default:
496       g_return_val_if_reached ((GType)0);
497       break;
498     }
499
500   g_assert_not_reached ();
501   return ((GType)0);
502 }
503
504 static gboolean
505 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
506 {
507   gint *indices, depth;
508   gint n;
509   struct variable *variable;
510
511   PsppireDict *dict = PSPPIRE_DICT (model);
512
513   g_return_val_if_fail (path, FALSE);
514
515   indices = gtk_tree_path_get_indices (path);
516   depth = gtk_tree_path_get_depth (path);
517
518   g_return_val_if_fail (depth == 1, FALSE);
519
520   n = indices[0];
521
522   if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
523     return FALSE;
524
525   variable = dict_get_var (dict->dict, n);
526
527   g_assert (var_get_dict_index (variable) == n);
528
529   iter->stamp = dict->stamp;
530   iter->user_data = variable;
531
532   return TRUE;
533 }
534
535
536 static gboolean
537 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
538 {
539   PsppireDict *dict = PSPPIRE_DICT (model);
540   struct variable *variable;
541   gint idx;
542
543   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
544
545   if ( iter == NULL || iter->user_data == NULL)
546     return FALSE;
547
548   variable = (struct variable *) iter->user_data;
549
550   idx = var_get_dict_index (variable);
551
552   if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
553     return FALSE;
554
555   variable = psppire_dict_get_variable (dict, idx + 1);
556
557   g_assert (var_get_dict_index (variable) == idx + 1);
558
559   iter->user_data = variable;
560
561   return TRUE;
562 }
563
564 static GtkTreePath *
565 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
566 {
567   GtkTreePath *path;
568   struct variable *variable;
569   PsppireDict *dict = PSPPIRE_DICT (model);
570
571   g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
572
573   variable = (struct variable *) iter->user_data;
574
575   path = gtk_tree_path_new ();
576   gtk_tree_path_append_index (path, var_get_dict_index (variable));
577
578   return path;
579 }
580
581
582 static void
583 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
584                      gint column, GValue *value)
585 {
586   struct variable *variable;
587   PsppireDict *dict = PSPPIRE_DICT (model);
588
589   g_return_if_fail (iter->stamp == dict->stamp);
590
591   variable = (struct variable *) iter->user_data;
592
593   switch (column)
594     {
595     case DICT_TVM_COL_NAME:
596       g_value_init (value, G_TYPE_STRING);
597       g_value_set_string (value, var_get_name (variable));
598       break;
599     case DICT_TVM_COL_VAR:
600       g_value_init (value, G_TYPE_POINTER);
601       g_value_set_pointer (value, variable);
602       break;
603     default:
604       g_return_if_reached ();
605       break;
606     }
607 }
608
609
610 static gboolean
611 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
612                      GtkTreeIter *parent, gint n)
613 {
614   PsppireDict *dict;
615   g_return_val_if_fail (G_IS_PSPPIRE_DICT (model), FALSE);
616
617   dict = PSPPIRE_DICT (model);
618
619   if ( parent )
620     return FALSE;
621
622   if ( n >= psppire_dict_get_var_cnt (dict) )
623     return FALSE;
624
625   iter->stamp = dict->stamp;
626   iter->user_data = psppire_dict_get_variable (dict, n);
627
628   if ( !iter->user_data)
629     return FALSE;
630
631
632   return TRUE;
633 }
634
635
636 void
637 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
638                          const gchar *text)
639 {
640   dict_rename_var (dict->dict, v, text);
641 }