39f6efe864dfa3d2acb9be2177142c863c025194
[pspp] / src / ui / gui / psppire-dialog-action-flip.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2013  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19
20 #include "psppire-dialog-action-flip.h"
21
22 #include "psppire-var-view.h"
23
24 #include "psppire-dialog.h"
25 #include "builder-wrapper.h"
26
27 static void psppire_dialog_action_flip_init            (PsppireDialogActionFlip      *act);
28 static void psppire_dialog_action_flip_class_init      (PsppireDialogActionFlipClass *class);
29
30 G_DEFINE_TYPE (PsppireDialogActionFlip, psppire_dialog_action_flip, PSPPIRE_TYPE_DIALOG_ACTION);
31
32
33 /*
34      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
35 */
36 static char *
37 generate_syntax (PsppireDialogAction *act)
38 {
39   const gchar *text;
40   PsppireDialogActionFlip *rd = PSPPIRE_DIALOG_ACTION_FLIP (act);
41
42   GString *string = g_string_new ("FLIP");
43   gchar *syntax ;
44
45   g_string_append (string, " /VARIABLES = ");
46
47   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dest), 0, string);
48
49   text = gtk_entry_get_text (GTK_ENTRY (rd->entry));
50
51   if ( text)
52     g_string_append_printf (string, " /NEWNAME = %s", text);
53
54   g_string_append (string, ".\n");
55
56   syntax = string->str;
57
58   g_string_free (string, FALSE);
59
60   return syntax;
61 }
62
63
64 static gboolean
65 dialog_state_valid (gpointer a)
66 {
67   PsppireDialogActionFlip *act = PSPPIRE_DIALOG_ACTION_FLIP (a);
68
69   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (act->dest));
70
71   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
72
73   if ( n_rows == 0 )
74     return FALSE;
75
76   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (act->entry))))
77     return FALSE;
78
79   return TRUE;
80 }
81
82 static void
83 refresh (PsppireDialogAction *rd_)
84 {
85   PsppireDialogActionFlip *rd = PSPPIRE_DIALOG_ACTION_FLIP (rd_);
86   GtkTreeModel *dmodel = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dest));
87
88   gtk_list_store_clear (GTK_LIST_STORE (dmodel));
89   gtk_entry_set_text (GTK_ENTRY (rd->entry), "");
90 }
91
92 static void
93 psppire_dialog_action_flip_activate (GtkAction *a)
94 {
95   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
96   PsppireDialogActionFlip *act = PSPPIRE_DIALOG_ACTION_FLIP (a);
97
98   GHashTable *thing = psppire_dialog_action_get_pointer (pda);
99   GtkBuilder *xml = g_hash_table_lookup (thing, a);
100   if (!xml)
101     {
102       xml = builder_new ("transpose.ui");
103       g_hash_table_insert (thing, a, xml);
104     }
105
106   pda->dialog = get_widget_assert   (xml, "transpose-dialog");
107   pda->source = get_widget_assert   (xml, "source-treeview");
108
109   act->dest = get_widget_assert (xml, "variables-treeview");
110   act->entry = get_widget_assert (xml, "new-name-entry");
111
112   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
113   psppire_dialog_action_set_refresh (pda, refresh);
114
115   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_flip_parent_class)->activate)
116     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_flip_parent_class)->activate (pda);
117 }
118
119 static void
120 psppire_dialog_action_flip_class_init (PsppireDialogActionFlipClass *class)
121 {
122   psppire_dialog_action_set_activation (class, psppire_dialog_action_flip_activate);
123   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
124 }
125
126
127 static void
128 psppire_dialog_action_flip_init (PsppireDialogActionFlip *act)
129 {
130 }
131