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