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