Removed my authorship lines.
[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
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->dict = dict_create ();
204
205   dict_set_callbacks (psppire_dict->dict, &gui_callbacks, psppire_dict);
206
207   psppire_dict->stamp = g_random_int();
208 }
209
210 /**
211  * psppire_dict_new:
212  * @returns: a new #PsppireDict object
213  *
214  * Creates a new #PsppireDict.
215  */
216 PsppireDict*
217 psppire_dict_new (void)
218 {
219   return g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
220 }
221
222
223 /**
224  * psppire_dict_new_from_dict:
225  * @returns: a new #PsppireDict object
226  *
227  * Creates a new #PsppireDict.
228  */
229 PsppireDict*
230 psppire_dict_new_from_dict (struct dictionary *d)
231 {
232   PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
233   new_dict->dict = d;
234
235   return new_dict;
236 }
237
238
239 /* Returns a valid name for a new variable in DICT.
240    The return value is statically allocated */
241 static gchar * 
242 auto_generate_var_name (PsppireDict *dict)
243 {
244   gint d = 0;
245   static gchar name[10];
246
247   while (g_snprintf(name, 10, "VAR%05d",d++),
248          psppire_dict_lookup_var(dict, name))
249     ;
250
251   return name;
252 }
253
254 /* Insert a new variable at posn IDX, with the name NAME.
255    If NAME is null, then a name will be automatically assigned.
256  */
257 void
258 psppire_dict_insert_variable(PsppireDict *d, gint idx, const gchar *name)
259 {
260   struct variable *var ;
261   g_return_if_fail(d);
262   g_return_if_fail(G_IS_PSPPIRE_DICT(d));
263
264
265   if ( ! name ) 
266     name = auto_generate_var_name(d);
267   
268   var = dict_create_var(d->dict, name, 0);
269
270   dict_reorder_var(d->dict, var, idx);
271
272   g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx );  
273 }
274
275 /* Delete N variables beginning at FIRST */
276 void
277 psppire_dict_delete_variables(PsppireDict *d, gint first, gint n)
278 {
279   gint idx;
280   g_return_if_fail(d);
281   g_return_if_fail(d->dict);
282   g_return_if_fail(G_IS_PSPPIRE_DICT(d));
283
284   for (idx = 0 ; idx < n ; ++idx ) 
285     {
286       struct variable *var;
287
288       /* Do nothing if it's out of bounds */
289       if ( first >= dict_get_var_cnt (d->dict))
290         break; 
291
292       var = dict_get_var(d->dict, first);
293       dict_delete_var (d->dict, var);
294     }
295   dict_compact_values(d->dict);
296
297   g_signal_emit(d, signal[VARIABLES_DELETED], 0, first, idx );  
298 }
299
300
301 void
302 psppire_dict_set_name(PsppireDict* d, gint idx, const gchar *name)
303 {
304   struct variable *var;
305   g_assert(d);
306   g_assert(G_IS_PSPPIRE_DICT(d));
307
308
309   if ( idx < dict_get_var_cnt(d->dict))
310     {
311       /* This is an existing variable? */
312       var = dict_get_var(d->dict, idx);
313       dict_rename_var(d->dict, var, name);
314       g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
315     }
316   else
317     {
318       /* new variable */
319       dict_create_var(d->dict, name, 0);
320       g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx);
321     }
322 }
323
324
325
326 /* Return the IDXth variable */
327 struct variable *
328 psppire_dict_get_variable(PsppireDict *d, gint idx)
329 {
330   g_return_val_if_fail(d, NULL);
331   g_return_val_if_fail(d->dict, NULL);
332
333   if ( dict_get_var_cnt (d->dict) <= idx )
334     return NULL;
335
336   return dict_get_var (d->dict, idx);
337 }
338
339
340 /* Return the number of variables in the dictionary */
341 gint
342 psppire_dict_get_var_cnt (const PsppireDict *d)
343 {
344   g_return_val_if_fail (d, -1);
345   g_return_val_if_fail (d->dict, -1);
346
347   return dict_get_var_cnt (d->dict);
348 }
349
350
351 /* Return a variable by name.
352    Return NULL if it doesn't exist
353 */
354 struct variable *
355 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
356 {
357   g_return_val_if_fail (d, NULL);
358   g_return_val_if_fail (d->dict, NULL);
359
360   return dict_lookup_var (d->dict, name);
361 }
362
363
364 void
365 psppire_dict_var_changed (PsppireDict *d, gint idx)
366 {
367   g_return_if_fail(d);
368
369   g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
370 }
371
372
373 /* Clears the contents of D */
374 void
375 psppire_dict_clear(PsppireDict *d)
376 {
377   g_return_if_fail(d);
378   g_return_if_fail(d->dict);
379
380   {
381     const gint n_vars = dict_get_var_cnt(d->dict);
382
383     dict_clear(d->dict);
384
385     g_signal_emit(d, signal[VARIABLES_DELETED], 0, 0, n_vars );
386   }
387 }
388
389
390
391 /* Return true is NAME would be a valid name of a variable to add to the
392    dictionary.  False otherwise.
393    If REPORT is true, then invalid names will be reported as such as errors
394 */
395 gboolean
396 psppire_dict_check_name(const PsppireDict *dict,
397                      const gchar *name, gboolean report)
398 {
399   if ( ! var_is_valid_name(name, report ) )
400       return FALSE;
401
402   if (psppire_dict_lookup_var(dict, name))
403     {
404       if ( report )
405         msg(ME,"Duplicate variable name.");
406       return FALSE;
407     }
408
409   return TRUE;
410 }
411
412
413 inline gint
414 psppire_dict_get_next_value_idx (const PsppireDict *dict)
415 {
416   return dict_get_next_value_idx(dict->dict);
417 }
418
419
420 void
421 psppire_dict_resize_variable (PsppireDict *d, const struct variable *pv,
422                               gint old_size, gint new_size)
423 {
424   gint fv;
425   g_return_if_fail (d);
426   g_return_if_fail (d->dict);
427
428   if ( old_size == new_size )
429     return ;
430
431   dict_compact_values (d->dict);
432
433   fv = var_get_case_index (pv);
434
435   g_signal_emit(d, signal[VARIABLE_RESIZED], 0,
436                 fv + old_size,
437                 new_size - old_size );
438 }
439
440
441 /* Tree Model Stuff */
442
443 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
444
445 static gint tree_model_n_columns (GtkTreeModel *model);
446
447 static GType tree_model_column_type (GtkTreeModel *model, gint index);
448
449 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, 
450                                      GtkTreePath *path);
451
452 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
453
454 static GtkTreePath * tree_model_get_path (GtkTreeModel *model, 
455                                           GtkTreeIter *iter);
456
457 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
458                                   gint column, GValue *value);
459
460 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter, 
461                                       GtkTreeIter *parent, gint n);
462
463
464 static void
465 dictionary_tree_model_init (GtkTreeModelIface *iface)
466 {
467   iface->get_flags = tree_model_get_flags;
468   iface->get_n_columns = tree_model_n_columns;
469   iface->get_column_type = tree_model_column_type;
470   iface->get_iter = tree_model_get_iter;
471   iface->iter_next = tree_model_iter_next;
472   iface->get_path = tree_model_get_path;
473   iface->get_value = tree_model_get_value;
474
475   iface->iter_children = 0;
476   iface->iter_has_child =0;
477   iface->iter_n_children =0;
478   iface->iter_nth_child = tree_model_nth_child ;
479   iface->iter_parent =0;
480 }
481
482 static GtkTreeModelFlags
483 tree_model_get_flags (GtkTreeModel *model)
484 {
485   g_return_val_if_fail (G_IS_PSPPIRE_DICT(model), (GtkTreeModelFlags) 0);
486
487   return GTK_TREE_MODEL_LIST_ONLY;
488 }
489
490
491 static gint
492 tree_model_n_columns (GtkTreeModel *model)
493 {
494   return n_DICT_COLS;
495 }
496
497 static GType
498 tree_model_column_type (GtkTreeModel *model, gint index)
499 {
500   g_return_val_if_fail (G_IS_PSPPIRE_DICT(model), (GType) 0);
501
502   switch(index) 
503     {
504     case DICT_TVM_COL_NAME:
505       return G_TYPE_STRING;
506       break;
507     case DICT_TVM_COL_VAR:
508       return G_TYPE_POINTER;
509       break;
510     default:
511       g_return_val_if_reached((GType)0);
512       break;
513     }
514
515   g_assert_not_reached();
516   return ((GType)0);
517 }
518
519 static gboolean
520 tree_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
521 {
522   gint *indices, depth;
523   gint n;
524   struct variable *variable;
525
526   PsppireDict *dict = PSPPIRE_DICT (model);
527
528   g_return_val_if_fail (path, FALSE);
529
530   indices = gtk_tree_path_get_indices (path);
531   depth = gtk_tree_path_get_depth (path);
532
533   g_return_val_if_fail(depth == 1, FALSE);
534
535   n = indices[0];
536
537   if ( n < 0 || n >= psppire_dict_get_var_cnt (dict)) 
538     return FALSE;
539
540   variable = dict_get_var (dict->dict, n);
541
542   g_assert (var_get_dict_index (variable) == n);
543
544   iter->stamp = dict->stamp;
545   iter->user_data = variable;
546
547   return TRUE;
548 }
549
550
551 static gboolean
552 tree_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter)
553 {
554   PsppireDict *dict = PSPPIRE_DICT (model);
555   struct variable *variable;
556   gint idx;
557
558   g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
559
560   if ( iter == NULL || iter->user_data == NULL)
561     return FALSE;
562
563   variable = (struct variable *) iter->user_data;
564
565   idx = var_get_dict_index (variable);
566
567   if ( idx + 1 >= psppire_dict_get_var_cnt(dict))
568     return FALSE;
569
570   variable = psppire_dict_get_variable (dict, idx + 1);
571
572   g_assert (var_get_dict_index (variable) == idx + 1);
573
574   iter->user_data = variable;
575
576   return TRUE;
577 }
578
579 static GtkTreePath *
580 tree_model_get_path(GtkTreeModel *model, GtkTreeIter *iter)
581 {
582   GtkTreePath *path;
583   struct variable *variable;
584   PsppireDict *dict = PSPPIRE_DICT (model);
585
586   g_return_val_if_fail(iter->stamp == dict->stamp, FALSE);
587
588   variable = (struct variable *) iter->user_data;
589
590   path = gtk_tree_path_new();
591   gtk_tree_path_append_index(path, var_get_dict_index (variable));
592
593   return path;
594 }
595
596
597 static void
598 tree_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
599                      gint column, GValue *value)
600 {
601   struct variable *variable;
602   PsppireDict *dict = PSPPIRE_DICT (model);
603
604   g_return_if_fail(iter->stamp == dict->stamp);
605
606   variable = (struct variable *) iter->user_data;
607
608   switch(column)
609     {
610     case DICT_TVM_COL_NAME:
611       g_value_init(value, G_TYPE_STRING);
612       g_value_set_string(value, var_get_name(variable));
613       break;
614     case DICT_TVM_COL_VAR:
615       g_value_init(value, G_TYPE_POINTER);
616       g_value_set_pointer(value, variable);
617       break;
618     default:
619       g_return_if_reached();
620       break;
621     }
622 }
623
624
625 static gboolean
626 tree_model_nth_child(GtkTreeModel *model, GtkTreeIter *iter,
627                      GtkTreeIter *parent, gint n)
628 {
629   PsppireDict *dict;
630   g_return_val_if_fail(G_IS_PSPPIRE_DICT(model), FALSE);
631
632   dict = PSPPIRE_DICT(model);
633
634   if ( parent )
635     return FALSE;
636
637   if ( n >= psppire_dict_get_var_cnt(dict) )
638     return FALSE;
639
640   iter->stamp = dict->stamp;
641   iter->user_data = psppire_dict_get_variable(dict, n);
642
643   if ( !iter->user_data)
644     return FALSE;
645
646
647   return TRUE;
648 }
649
650
651 void
652 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
653                          const gchar *text)
654 {
655   dict_rename_var (dict->dict, v, text);
656 }