Split File Dialog: Convert to PsppireDialogAction
[pspp] / src / ui / gui / psppire-dialog-action-split.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011, 2012, 2015  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-split.h"
21 #include "psppire-selector.h"
22 #include "psppire-var-view.h"
23 #include "dict-display.h"
24
25 #include "psppire-dialog.h"
26 #include "builder-wrapper.h"
27
28 #include <gettext.h>
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
31
32 static void psppire_dialog_action_split_init            (PsppireDialogActionSplit      *act);
33 static void psppire_dialog_action_split_class_init      (PsppireDialogActionSplitClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionSplit, psppire_dialog_action_split, PSPPIRE_TYPE_DIALOG_ACTION);
36
37
38 static char *
39 generate_syntax (PsppireDialogAction *pda)
40 {
41   PsppireDialogActionSplit *act = PSPPIRE_DIALOG_ACTION_SPLIT (pda);
42   gchar *text;
43
44   GString *string = g_string_new ("SPLIT FILE OFF.");
45
46   if ( ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (act->off)))
47     {
48       GString * varlist = g_string_sized_new (80);
49       gint n_vars = psppire_var_view_append_names (PSPPIRE_VAR_VIEW (act->tv), 0, varlist);
50
51       if ( n_vars > 0 )
52         {
53           g_string_assign (string, "");
54
55           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(act->sort)))
56             {
57               g_string_append (string, "SORT CASES BY");
58               g_string_append (string, varlist->str);
59               g_string_append (string, ".\n");
60             }
61
62           g_string_append (string, "SPLIT FILE ");
63
64           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (act->layered)))
65             g_string_append (string, "LAYERED ");
66           else
67             g_string_append (string, "SEPARATE ");
68
69           g_string_append (string, "BY ");
70           g_string_append (string, varlist->str);
71           g_string_append (string, ".");
72         }
73       g_string_free (varlist, TRUE);
74     }
75
76   text =  string->str;
77
78   g_string_free (string, FALSE);
79
80   return text;
81 }
82
83
84 static gboolean
85 dialog_state_valid (gpointer data)
86 {
87   return TRUE;
88 }
89
90 static void
91 refresh (PsppireDialogAction *pda)
92 {
93   PsppireDialogActionSplit *act = PSPPIRE_DIALOG_ACTION_SPLIT (pda);
94
95   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (act->tv));
96
97   gint n_vars = dict_get_split_cnt (pda->dict->dict);
98
99   gtk_list_store_clear (GTK_LIST_STORE (liststore));
100
101   if ( n_vars == 0 )
102     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (act->off), TRUE);
103   else
104     {
105       GtkTreeIter iter;
106       gint i;
107       const struct variable *const *vars = dict_get_split_vars (pda->dict->dict);
108
109       for (i = 0 ; i < n_vars; ++i )
110         {
111           gtk_list_store_append (GTK_LIST_STORE (liststore), &iter);
112
113           gtk_list_store_set (GTK_LIST_STORE (liststore), &iter,
114                               0, vars[i],
115                               -1);
116         }
117
118       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (act->layered), TRUE);
119     }
120
121   gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (act->off));
122
123   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (act->sort), TRUE);
124 }
125
126 static void
127 on_off_toggled (GtkToggleButton *togglebutton,
128                 gpointer         user_data)
129 {
130   PsppireDialogActionSplit *act = PSPPIRE_DIALOG_ACTION_SPLIT (user_data);
131
132   gboolean state = !gtk_toggle_button_get_active (togglebutton);
133
134   gtk_widget_set_sensitive (act->dest, state);
135   gtk_widget_set_sensitive (act->selector, state);
136   gtk_widget_set_sensitive (act->source, state);
137 }
138
139 static void
140 psppire_dialog_action_split_activate (PsppireDialogAction *pda)
141 {
142   PsppireDialogActionSplit *act = PSPPIRE_DIALOG_ACTION_SPLIT (pda);
143
144   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
145   GtkBuilder *xml = g_hash_table_lookup (thing, pda);
146   if (!xml)
147     {
148       xml = builder_new ("split-file.ui");
149       g_hash_table_insert (thing, pda, xml);
150
151       pda->dialog = get_widget_assert   (xml, "split-file-dialog");
152       pda->source = get_widget_assert   (xml, "split-file-dict-treeview");
153       act->selector = get_widget_assert (xml, "split-file-selector");
154
155       act->dest =   get_widget_assert (xml, "split-file-grouping-vars");
156       act->source = get_widget_assert (xml, "split-file-dict-treeview");
157       act->sort = get_widget_assert (xml, "split-sort");
158
159       act->off = get_widget_assert   (xml, "split-off");
160       act->layered = get_widget_assert   (xml, "split-layered");
161
162       act->tv = get_widget_assert (xml, "split-file-grouping-vars");
163
164       g_signal_connect (act->off, "toggled", G_CALLBACK (on_off_toggled), pda);
165       g_signal_connect_swapped (pda->dialog, "show", G_CALLBACK (refresh), pda);
166     }
167   
168   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
169   psppire_dialog_action_set_refresh (pda, refresh);
170
171   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_split_parent_class)->activate)
172     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_split_parent_class)->activate (pda);
173 }
174
175 static void
176 psppire_dialog_action_split_class_init (PsppireDialogActionSplitClass *class)
177 {
178   psppire_dialog_action_set_activation (class, psppire_dialog_action_split_activate);
179   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
180 }
181
182
183 static void
184 psppire_dialog_action_split_init (PsppireDialogActionSplit *act)
185 {
186 }
187