1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012,
3 2016, 2017 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 3 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, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "ui/gui/psppire-dict.h"
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <gtk/gtk.h>
25
26 #include "data/dictionary.h"
27 #include "data/identifier.h"
28 #include "data/missing-values.h"
29 #include "data/value-labels.h"
30 #include "data/variable.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/message.h"
33 #include "ui/gui/helper.h"
34 #include "ui/gui/psppire-marshal.h"
35 #include "ui/gui/psppire-var-ptr.h"
36
37 #include <gobject/genums.h>
38
39 #include <gettext.h>
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
42
43
44
45 GType align_enum_type;
46 GType measure_enum_type;
47 GType role_enum_type;
48
49
50 enum {
51 VARIABLE_CHANGED,
52 VARIABLE_INSERTED,
53 VARIABLE_DELETED,
54
55 WEIGHT_CHANGED,
56 FILTER_CHANGED,
57 SPLIT_CHANGED,
58
59 RESIZE_ITEM,
60
61 n_SIGNALS
62 };
63
64
65 /* --- prototypes --- */
66 static void psppire_dict_dispose (GObject *object);
67
68 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
69
70
71
72 static guint
gni(GListModel * list)73 gni (GListModel *list)
74 {
75 PsppireDict *dict = PSPPIRE_DICT (list);
76
77 return psppire_dict_get_var_cnt (dict);
78 }
79
80 static GType
git(GListModel * list)81 git (GListModel *list)
82 {
83 return GTK_TYPE_BUTTON;
84 }
85
86 static gpointer
gi(GListModel * list,guint id)87 gi (GListModel *list, guint id)
88 {
89 GtkWidget *button = gtk_button_new ();
90
91 PsppireDict *dict = PSPPIRE_DICT (list);
92
93 if (id >= psppire_dict_get_var_cnt (dict))
94 {
95 gtk_button_set_label (GTK_BUTTON (button), _("Var"));
96 }
97 else
98 {
99 const struct variable *v = psppire_dict_get_variable (dict, id);
100
101 gtk_button_set_label (GTK_BUTTON (button), var_get_name (v));
102 gtk_widget_set_tooltip_text (button, var_get_label (v));
103
104 {
105 PangoContext *context = gtk_widget_create_pango_context (button);
106 PangoLayout *layout = pango_layout_new (context);
107 PangoRectangle rect;
108
109 pango_layout_set_text (layout, "M", 1);
110
111 pango_layout_get_extents (layout, NULL, &rect);
112
113 g_object_unref (G_OBJECT (layout));
114 g_object_unref (G_OBJECT (context));
115
116 gtk_widget_set_size_request (button,
117 (0.25 + var_get_display_width (v))
118 * rect.width / PANGO_SCALE,
119 -1);
120 }
121 }
122
123 return button;
124 }
125
126
127 static void
ssw_init_iface(GListModelInterface * iface)128 ssw_init_iface (GListModelInterface *iface)
129 {
130 iface->get_n_items = gni;
131 iface->get_item = gi;
132 iface->get_item_type = git;
133 }
134
135
136 /* --- variables --- */
137 static GObjectClass *parent_class = NULL;
138
139 static guint signals [n_SIGNALS];
140
141 /* --- functions --- */
142
G_DEFINE_TYPE_WITH_CODE(PsppireDict,psppire_dict,G_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,dictionary_tree_model_init)G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,ssw_init_iface))143 G_DEFINE_TYPE_WITH_CODE (PsppireDict, psppire_dict, G_TYPE_OBJECT,
144 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
145 dictionary_tree_model_init)
146 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
147 ssw_init_iface))
148
149 static void
150 psppire_dict_class_init (PsppireDictClass *class)
151 {
152 GObjectClass *object_class = G_OBJECT_CLASS (class);
153
154 parent_class = g_type_class_peek_parent (class);
155
156 object_class->dispose = psppire_dict_dispose;
157
158 signals [RESIZE_ITEM] =
159 g_signal_new ("resize-item",
160 G_TYPE_FROM_CLASS (class),
161 G_SIGNAL_RUN_LAST,
162 0,
163 NULL, NULL,
164 psppire_marshal_BOOLEAN__INT_INT,
165 G_TYPE_BOOLEAN,
166 2,
167 G_TYPE_INT,
168 G_TYPE_INT);
169
170 signals [VARIABLE_CHANGED] =
171 g_signal_new ("variable-changed",
172 G_TYPE_FROM_CLASS (class),
173 G_SIGNAL_RUN_FIRST,
174 0,
175 NULL, NULL,
176 psppire_marshal_VOID__INT_UINT_POINTER,
177 G_TYPE_NONE,
178 3,
179 G_TYPE_INT,
180 G_TYPE_UINT,
181 G_TYPE_POINTER);
182
183 signals [VARIABLE_INSERTED] =
184 g_signal_new ("variable-inserted",
185 G_TYPE_FROM_CLASS (class),
186 G_SIGNAL_RUN_FIRST,
187 0,
188 NULL, NULL,
189 g_cclosure_marshal_VOID__INT,
190 G_TYPE_NONE,
191 1,
192 G_TYPE_INT);
193
194 signals [VARIABLE_DELETED] =
195 g_signal_new ("variable-deleted",
196 G_TYPE_FROM_CLASS (class),
197 G_SIGNAL_RUN_FIRST,
198 0,
199 NULL, NULL,
200 psppire_marshal_VOID__POINTER_INT_INT,
201 G_TYPE_NONE,
202 3,
203 G_TYPE_POINTER,
204 G_TYPE_INT,
205 G_TYPE_INT);
206
207 signals [WEIGHT_CHANGED] =
208 g_signal_new ("weight-changed",
209 G_TYPE_FROM_CLASS (class),
210 G_SIGNAL_RUN_FIRST,
211 0,
212 NULL, NULL,
213 g_cclosure_marshal_VOID__INT,
214 G_TYPE_NONE,
215 1,
216 G_TYPE_INT);
217
218 signals [FILTER_CHANGED] =
219 g_signal_new ("filter-changed",
220 G_TYPE_FROM_CLASS (class),
221 G_SIGNAL_RUN_FIRST,
222 0,
223 NULL, NULL,
224 g_cclosure_marshal_VOID__INT,
225 G_TYPE_NONE,
226 1,
227 G_TYPE_INT);
228
229 signals [SPLIT_CHANGED] =
230 g_signal_new ("split-changed",
231 G_TYPE_FROM_CLASS (class),
232 G_SIGNAL_RUN_FIRST,
233 0,
234 NULL, NULL,
235 g_cclosure_marshal_VOID__VOID,
236 G_TYPE_NONE,
237 0);
238 }
239
240 static void
psppire_dict_dispose(GObject * object)241 psppire_dict_dispose (GObject *object)
242 {
243 PsppireDict *d = PSPPIRE_DICT (object);
244
245 if (!d->dispose_has_run)
246 return;
247
248 d->dispose_has_run = TRUE;
249
250 dict_set_callbacks (d->dict, NULL, NULL);
251 dict_unref (d->dict);
252
253 G_OBJECT_CLASS (parent_class)->dispose (object);
254 }
255
256 /* Pass on callbacks from src/data/dictionary, as
257 signals in the Gtk library */
258 static void
addcb(struct dictionary * d,int idx,void * pd)259 addcb (struct dictionary *d, int idx, void *pd)
260 {
261 PsppireDict *dict = PSPPIRE_DICT (pd);
262
263 if (! dict->disable_insert_signal)
264 {
265 g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
266 g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
267 }
268 }
269
270 static void
delcb(struct dictionary * d,const struct variable * var,int dict_idx,int case_idx,void * pd)271 delcb (struct dictionary *d, const struct variable *var,
272 int dict_idx, int case_idx, void *pd)
273 {
274 g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
275 var, dict_idx, case_idx);
276 g_signal_emit_by_name (pd, "items-changed", dict_idx, 1, 0);
277 }
278
279 static void
mutcb(struct dictionary * d,int idx,unsigned int what,const struct variable * oldvar,void * pd)280 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
281 {
282 g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
283 g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
284 }
285
286 static void
weight_changed_callback(struct dictionary * d,int idx,void * pd)287 weight_changed_callback (struct dictionary *d, int idx, void *pd)
288 {
289 g_signal_emit (pd, signals [WEIGHT_CHANGED], 0, idx);
290 }
291
292 static void
filter_changed_callback(struct dictionary * d,int idx,void * pd)293 filter_changed_callback (struct dictionary *d, int idx, void *pd)
294 {
295 g_signal_emit (pd, signals [FILTER_CHANGED], 0, idx);
296 }
297
298 static void
split_changed_callback(struct dictionary * d,void * pd)299 split_changed_callback (struct dictionary *d, void *pd)
300 {
301 g_signal_emit (pd, signals [SPLIT_CHANGED], 0);
302 }
303
304 static const struct dict_callbacks gui_callbacks =
305 {
306 addcb,
307 delcb,
308 mutcb,
309 weight_changed_callback,
310 filter_changed_callback,
311 split_changed_callback
312 };
313
314 static void
psppire_dict_init(PsppireDict * d)315 psppire_dict_init (PsppireDict *d)
316 {
317 d->dispose_has_run = FALSE;
318
319 d->stamp = g_random_int ();
320 d->disable_insert_signal = FALSE;
321 }
322
323 /**
324 * psppire_dict_new_from_dict:
325 * @returns: a new #PsppireDict object
326 *
327 * Creates a new #PsppireDict.
328 */
329 PsppireDict*
psppire_dict_new_from_dict(struct dictionary * d)330 psppire_dict_new_from_dict (struct dictionary *d)
331 {
332 PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
333 new_dict->dict = dict_ref (d);
334
335 dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
336
337 return new_dict;
338 }
339
340
341 void
psppire_dict_replace_dictionary(PsppireDict * dict,struct dictionary * d)342 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
343 {
344 const struct variable *var = dict_get_weight (d);
345
346 struct dictionary *old_dict = dict->dict;
347
348 guint old_n = dict_get_var_cnt (dict->dict);
349 guint new_n = dict_get_var_cnt (d);
350
351 dict->dict = dict_ref (d);
352 dict_unref (old_dict);
353
354 weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
355
356 var = dict_get_filter (d);
357 filter_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
358
359 split_changed_callback (d, dict);
360
361 dict_set_callbacks (dict->dict, &gui_callbacks, dict);
362
363 g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
364 }
365
366
367 /* Stores a valid name for a new variable in DICT into the SIZE bytes in NAME.
368 Returns true if successful, false if SIZE is insufficient. */
369 bool
psppire_dict_generate_name(const PsppireDict * dict,char * name,size_t size)370 psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
371 {
372 gint d;
373
374 for (d = 1; ; d++)
375 {
376 int len;
377
378 /* TRANSLATORS: This string must be a valid variable name. That means:
379 - The string must be at most 64 bytes (not characters) long.
380 - The string may not contain whitespace.
381 - The first character may not be '$'
382 - The first character may not be a digit
383 - The final character may not be '.' or '_'
384 */
385 len = snprintf (name, size, _("Var%04d"), d);
386 if (len + 1 >= size)
387 return false;
388
389 if (psppire_dict_lookup_var (dict, name) == NULL)
390 return true;
391 }
392
393 return name;
394 }
395
396 /* Insert a new variable at posn IDX, with the name NAME, and return the
397 new variable.
398 IDX may take the special value -1, which will be treated the same as
399 zero. If NAME is null, then a name will be automatically assigned.
400 */
401 struct variable *
psppire_dict_insert_variable(PsppireDict * d,gint idx,const gchar * name)402 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
403 {
404 struct variable *var;
405 char tmpname[64];
406
407 if (idx == -1) /* Note bug #56392. */
408 idx = 0;
409 g_return_val_if_fail (d, NULL);
410 g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
411
412 if (name == NULL)
413 {
414 if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
415 g_return_val_if_reached (NULL);
416
417 name = tmpname;
418 }
419
420 d->disable_insert_signal = TRUE;
421
422 var = dict_create_var (d->dict, name, 0);
423
424 dict_reorder_var (d->dict, var, idx);
425
426 d->disable_insert_signal = FALSE;
427
428 g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
429 g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
430
431 return var;
432 }
433
434 /* Delete N variables beginning at FIRST */
435 void
psppire_dict_delete_variables(PsppireDict * d,gint first,gint n)436 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
437 {
438 g_return_if_fail (d);
439 g_return_if_fail (d->dict);
440 g_return_if_fail (PSPPIRE_IS_DICT (d));
441 size_t varcnt = dict_get_var_cnt (d->dict);
442 g_return_if_fail (first < varcnt);
443 g_return_if_fail (first >= 0);
444 g_return_if_fail (n > 0);
445 g_return_if_fail (first + n <= varcnt);
446
447 dict_delete_consecutive_vars (d->dict, first, n);
448 }
449
450
451 gboolean
psppire_dict_set_name(PsppireDict * d,gint idx,const gchar * name)452 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
453 {
454 struct variable *var;
455 g_assert (d);
456 g_assert (PSPPIRE_IS_DICT (d));
457
458 if (! dict_id_is_valid (d->dict, name, false))
459 return FALSE;
460
461 if (idx < dict_get_var_cnt (d->dict))
462 {
463 /* This is an existing variable? */
464 var = dict_get_var (d->dict, idx);
465 dict_rename_var (d->dict, var, name);
466 }
467 else
468 {
469 /* new variable */
470 dict_create_var (d->dict, name, 0);
471 }
472
473 return TRUE;
474 }
475
476
477
478 /* Return the IDXth variable.
479 Will return NULL if IDX exceeds the number of variables in the dictionary.
480 */
481 struct variable *
psppire_dict_get_variable(const PsppireDict * d,gint idx)482 psppire_dict_get_variable (const PsppireDict *d, gint idx)
483 {
484 g_return_val_if_fail (d, NULL);
485 g_return_val_if_fail (d->dict, NULL);
486
487 if (dict_get_var_cnt (d->dict) <= idx)
488 return NULL;
489
490 return dict_get_var (d->dict, idx);
491 }
492
493
494 /* Return the number of variables in the dictionary */
495 gint
psppire_dict_get_var_cnt(const PsppireDict * d)496 psppire_dict_get_var_cnt (const PsppireDict *d)
497 {
498 g_return_val_if_fail (d, -1);
499 g_return_val_if_fail (d->dict, -1);
500
501 return dict_get_var_cnt (d->dict);
502 }
503
504
505 /* Return the number of `union value's in the dictionary */
506 size_t
psppire_dict_get_value_cnt(const PsppireDict * d)507 psppire_dict_get_value_cnt (const PsppireDict *d)
508 {
509 g_return_val_if_fail (d, -1);
510 g_return_val_if_fail (d->dict, -1);
511
512 return dict_get_next_value_idx (d->dict);
513 }
514
515
516 /* Returns the prototype for the cases that match the dictionary */
517 const struct caseproto *
psppire_dict_get_proto(const PsppireDict * d)518 psppire_dict_get_proto (const PsppireDict *d)
519 {
520 g_return_val_if_fail (d, NULL);
521 g_return_val_if_fail (d->dict, NULL);
522
523 return dict_get_proto (d->dict);
524 }
525
526
527 /* Return a variable by name.
528 Return NULL if it doesn't exist
529 */
530 struct variable *
psppire_dict_lookup_var(const PsppireDict * d,const gchar * name)531 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
532 {
533 g_return_val_if_fail (d, NULL);
534 g_return_val_if_fail (d->dict, NULL);
535
536 return dict_lookup_var (d->dict, name);
537 }
538
539 /* Clears the contents of D */
540 void
psppire_dict_clear(PsppireDict * d)541 psppire_dict_clear (PsppireDict *d)
542 {
543 g_return_if_fail (d);
544 g_return_if_fail (d->dict);
545
546 {
547 dict_clear (d->dict);
548 }
549 }
550
551
552 /* Return true if NAME would be a valid name of a variable to add to the
553 dictionary. False otherwise.
554 If REPORT is true, then invalid names will be reported as such as errors
555 */
556 gboolean
psppire_dict_check_name(const PsppireDict * dict,const gchar * name,gboolean report)557 psppire_dict_check_name (const PsppireDict *dict,
558 const gchar *name, gboolean report)
559 {
560 if (! dict_id_is_valid (dict->dict, name, report))
561 return FALSE;
562
563 if (psppire_dict_lookup_var (dict, name))
564 {
565 if (report)
566 msg (ME, _("Duplicate variable name."));
567 return FALSE;
568 }
569
570 return TRUE;
571 }
572
573
574 gint
psppire_dict_get_next_value_idx(const PsppireDict * dict)575 psppire_dict_get_next_value_idx (const PsppireDict *dict)
576 {
577 return dict_get_next_value_idx (dict->dict);
578 }
579
580
581 /* Tree Model Stuff */
582
583 static GtkTreeModelFlags tree_model_get_flags (GtkTreeModel *model);
584
585 static gint tree_model_n_columns (GtkTreeModel *model);
586
587 static GType tree_model_column_type (GtkTreeModel *model, gint index);
588
589 static gboolean tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter,
590 GtkTreePath *path);
591
592 static gboolean tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter);
593
594 static GtkTreePath * tree_model_get_path (GtkTreeModel *model,
595 GtkTreeIter *iter);
596
597 static void tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
598 gint column, GValue *value);
599
600 static gboolean tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
601 GtkTreeIter *parent, gint n);
602
603 static gint tree_model_n_children (GtkTreeModel *tree_model,
604 GtkTreeIter *iter);
605
606 static gboolean tree_model_iter_children (GtkTreeModel *,
607 GtkTreeIter *,
608 GtkTreeIter *);
609
610 static gboolean tree_model_iter_parent (GtkTreeModel *tree_model,
611 GtkTreeIter *iter,
612 GtkTreeIter *child);
613
614 static gboolean tree_model_iter_has_child (GtkTreeModel *tree_model,
615 GtkTreeIter *iter);
616
617 static void
dictionary_tree_model_init(GtkTreeModelIface * iface)618 dictionary_tree_model_init (GtkTreeModelIface *iface)
619 {
620 iface->get_flags = tree_model_get_flags;
621 iface->get_n_columns = tree_model_n_columns;
622 iface->get_column_type = tree_model_column_type;
623 iface->get_iter = tree_model_get_iter;
624 iface->iter_next = tree_model_iter_next;
625 iface->get_path = tree_model_get_path;
626 iface->get_value = tree_model_get_value;
627
628 iface->iter_children = tree_model_iter_children ;
629 iface->iter_has_child = tree_model_iter_has_child ;
630 iface->iter_n_children = tree_model_n_children ;
631 iface->iter_nth_child = tree_model_nth_child ;
632 iface->iter_parent = tree_model_iter_parent ;
633 }
634
635 static gboolean
tree_model_iter_has_child(GtkTreeModel * tree_model,GtkTreeIter * iter)636 tree_model_iter_has_child (GtkTreeModel *tree_model,
637 GtkTreeIter *iter)
638 {
639 return FALSE;
640 }
641
642 static gboolean
tree_model_iter_parent(GtkTreeModel * tree_model,GtkTreeIter * iter,GtkTreeIter * child)643 tree_model_iter_parent (GtkTreeModel *tree_model,
644 GtkTreeIter *iter,
645 GtkTreeIter *child)
646 {
647 return TRUE;
648 }
649
650 static GtkTreeModelFlags
tree_model_get_flags(GtkTreeModel * model)651 tree_model_get_flags (GtkTreeModel *model)
652 {
653 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GtkTreeModelFlags) 0);
654
655 return GTK_TREE_MODEL_LIST_ONLY;
656 }
657
658
659 static gint
tree_model_n_columns(GtkTreeModel * model)660 tree_model_n_columns (GtkTreeModel *model)
661 {
662 return n_DICT_COLS;
663 }
664
665 static GType
tree_model_column_type(GtkTreeModel * model,gint index)666 tree_model_column_type (GtkTreeModel *model, gint index)
667 {
668 g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
669
670 GType t = 0;
671
672 switch (index)
673 {
674 case DICT_TVM_COL_NAME:
675 case DICT_TVM_COL_LABEL:
676 t = G_TYPE_STRING;
677 break;
678 case DICT_TVM_COL_DECIMAL:
679 case DICT_TVM_COL_WIDTH:
680 case DICT_TVM_COL_COLUMNS:
681 t = G_TYPE_INT;
682 break;
683 case DICT_TVM_COL_VAR:
684 t = PSPPIRE_VAR_PTR_TYPE;
685 break;
686 case DICT_TVM_COL_ALIGNMENT:
687 t = align_enum_type;
688 break;
689 case DICT_TVM_COL_MEASURE:
690 t = measure_enum_type;
691 break;
692 case DICT_TVM_COL_ROLE:
693 t = role_enum_type;
694 break;
695 }
696
697 return t;
698 }
699
700 static gboolean
tree_model_get_iter(GtkTreeModel * model,GtkTreeIter * iter,GtkTreePath * path)701 tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
702 {
703 gint *indices, depth;
704 gint n;
705 struct variable *var;
706
707 PsppireDict *dict = PSPPIRE_DICT (model);
708
709 g_return_val_if_fail (path, FALSE);
710
711 indices = gtk_tree_path_get_indices (path);
712 depth = gtk_tree_path_get_depth (path);
713
714 g_return_val_if_fail (depth == 1, FALSE);
715
716 n = indices [0];
717
718 if (n < 0 || n >= psppire_dict_get_var_cnt (dict))
719 {
720 iter->stamp = 0;
721 iter->user_data = NULL;
722 return FALSE;
723 }
724
725 var = psppire_dict_get_variable (dict, n);
726
727 g_assert (var_get_dict_index (var) == n);
728
729 iter->stamp = dict->stamp;
730 iter->user_data = var;
731
732 return TRUE;
733 }
734
735
736 static gboolean
tree_model_iter_next(GtkTreeModel * model,GtkTreeIter * iter)737 tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
738 {
739 PsppireDict *dict = PSPPIRE_DICT (model);
740 struct variable *var;
741 gint idx;
742
743 if (iter == NULL || iter->user_data == NULL)
744 return FALSE;
745
746 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
747
748 var = iter->user_data;
749
750 idx = var_get_dict_index (var);
751
752 if (idx + 1 >= psppire_dict_get_var_cnt (dict))
753 {
754 iter->user_data = NULL;
755 iter->stamp = 0;
756 return FALSE;
757 }
758
759 var = psppire_dict_get_variable (dict, idx + 1);
760
761 g_assert (var_get_dict_index (var) == idx + 1);
762
763 iter->user_data = var;
764
765 return TRUE;
766 }
767
768 static GtkTreePath *
tree_model_get_path(GtkTreeModel * model,GtkTreeIter * iter)769 tree_model_get_path (GtkTreeModel *model, GtkTreeIter *iter)
770 {
771 GtkTreePath *path;
772 struct variable *var;
773 PsppireDict *dict = PSPPIRE_DICT (model);
774
775 g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
776
777 var = iter->user_data;
778
779 path = gtk_tree_path_new ();
780 gtk_tree_path_append_index (path, var_get_dict_index (var));
781
782 return path;
783 }
784
785 const struct fmt_spec *var_get_write_format (const struct variable *);
786
787 static void
tree_model_get_value(GtkTreeModel * model,GtkTreeIter * iter,gint column,GValue * value)788 tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
789 gint column, GValue *value)
790 {
791 struct variable *var;
792 PsppireDict *dict = PSPPIRE_DICT (model);
793
794 g_return_if_fail (iter->stamp == dict->stamp);
795
796 var = iter->user_data;
797
798 const struct fmt_spec *fs = var_get_write_format (var);
799
800 switch (column)
801 {
802 case DICT_TVM_COL_NAME:
803 g_value_init (value, G_TYPE_STRING);
804 g_value_set_string (value, var_get_name (var));
805 break;
806 case DICT_TVM_COL_WIDTH:
807 g_value_init (value, G_TYPE_INT);
808 g_value_set_int (value, fs->w);
809 break;
810 case DICT_TVM_COL_DECIMAL:
811 g_value_init (value, G_TYPE_INT);
812 g_value_set_int (value, fs->d);
813 break;
814 case DICT_TVM_COL_LABEL:
815 g_value_init (value, G_TYPE_STRING);
816 g_value_set_string (value, var_get_label (var));
817 break;
818 case DICT_TVM_COL_COLUMNS:
819 g_value_init (value, G_TYPE_INT);
820 g_value_set_int (value, var_get_display_width (var));
821 break;
822 case DICT_TVM_COL_ALIGNMENT:
823 g_value_init (value, align_enum_type);
824 g_value_set_enum (value, var_get_alignment (var));
825 break;
826 case DICT_TVM_COL_MEASURE:
827 g_value_init (value, measure_enum_type);
828 g_value_set_enum (value, var_get_measure (var));
829 break;
830 case DICT_TVM_COL_ROLE:
831 g_value_init (value, role_enum_type);
832 g_value_set_enum (value, var_get_role (var));
833 break;
834 case DICT_TVM_COL_VAR:
835 g_value_init (value, PSPPIRE_VAR_PTR_TYPE);
836 g_value_set_boxed (value, var);
837 break;
838 default:
839 g_value_init (value, G_TYPE_STRING);
840 g_value_set_string (value, "????");
841 break;
842 }
843 }
844
845 static gboolean
tree_model_iter_children(GtkTreeModel * tree_model,GtkTreeIter * iter,GtkTreeIter * parent)846 tree_model_iter_children (GtkTreeModel *tree_model,
847 GtkTreeIter *iter,
848 GtkTreeIter *parent)
849 {
850 return FALSE;
851 }
852
853 static gint
tree_model_n_children(GtkTreeModel * model,GtkTreeIter * iter)854 tree_model_n_children (GtkTreeModel *model,
855 GtkTreeIter *iter)
856 {
857 PsppireDict *dict = PSPPIRE_DICT (model);
858
859 if (iter == NULL)
860 return psppire_dict_get_var_cnt (dict);
861
862 return 0;
863 }
864
865 static gboolean
tree_model_nth_child(GtkTreeModel * model,GtkTreeIter * iter,GtkTreeIter * parent,gint n)866 tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
867 GtkTreeIter *parent, gint n)
868 {
869 PsppireDict *dict;
870
871 g_return_val_if_fail (PSPPIRE_IS_DICT (model), FALSE);
872
873 dict = PSPPIRE_DICT (model);
874
875 if (parent)
876 return FALSE;
877
878 if (n >= psppire_dict_get_var_cnt (dict))
879 return FALSE;
880
881 iter->stamp = dict->stamp;
882 iter->user_data = psppire_dict_get_variable (dict, n);
883
884 if (!iter->user_data)
885 return FALSE;
886
887 return TRUE;
888 }
889
890
891 gboolean
psppire_dict_rename_var(PsppireDict * dict,struct variable * v,const gchar * name)892 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
893 const gchar *name)
894 {
895 if (! dict_id_is_valid (dict->dict, name, false))
896 return FALSE;
897
898 /* Make sure no other variable has this name */
899 if (NULL != psppire_dict_lookup_var (dict, name))
900 return FALSE;
901
902 dict_rename_var (dict->dict, v, name);
903
904 return TRUE;
905 }
906
907
908 struct variable *
psppire_dict_get_weight_variable(const PsppireDict * dict)909 psppire_dict_get_weight_variable (const PsppireDict *dict)
910 {
911 return dict_get_weight (dict->dict);
912 }
913
914 const gchar *
psppire_dict_encoding(const PsppireDict * dict)915 psppire_dict_encoding (const PsppireDict *dict)
916 {
917 return dict_get_encoding (dict->dict);
918 }
919