Add an appropriate help page for the Variable Info dialog box
[pspp] / src / ui / gui / split-file-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011, 2012  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 #include <config.h>
18
19 #include "split-file-dialog.h"
20 #include "psppire-selector.h"
21 #include "psppire-dialog.h"
22 #include "executor.h"
23 #include "psppire-data-window.h"
24 #include "dict-display.h"
25 #include "builder-wrapper.h"
26 #include "helper.h"
27
28 #include <data/dictionary.h>
29
30 #include "psppire-var-view.h"
31
32 #include <gtk/gtk.h>
33
34
35 #include "dialog-common.h"
36
37
38 struct split_file_dialog
39 {
40   /* The XML that created the dialog */
41   GtkBuilder *xml;
42
43   /* The dictionary to which this dialog pertains */
44   PsppireDict *dict;
45
46   /* The treeview widget containing the list of variables
47      upon which the file should be split */
48   GtkTreeView *tv;
49
50   PsppireSelector *selector;
51 };
52
53
54 static gchar *
55 generate_syntax (const struct split_file_dialog *sfd)
56 {
57   gchar *text;
58   GtkWidget *off = get_widget_assert (sfd->xml, "split-radiobutton0");
59
60   GString *string = g_string_new ("SPLIT FILE OFF.");
61
62   if ( ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (off)))
63     {
64       GString * varlist = g_string_sized_new (80);
65       GtkWidget *sort = get_widget_assert (sfd->xml, "split-radiobutton3");
66       GtkWidget *layered = get_widget_assert (sfd->xml, "split-radiobutton1");
67       gint n_vars = psppire_var_view_append_names (PSPPIRE_VAR_VIEW (sfd->tv), 0, varlist);
68
69       if ( n_vars > 0 )
70         {
71           g_string_assign (string, "");
72
73           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sort)))
74             {
75               g_string_append (string, "SORT CASES BY");
76               g_string_append (string, varlist->str);
77               g_string_append (string, ".\n");
78             }
79
80           g_string_append (string, "SPLIT FILE ");
81
82           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (layered)))
83             g_string_append (string, "LAYERED ");
84           else
85             g_string_append (string, "SEPARATE ");
86
87           g_string_append (string, "BY ");
88           g_string_append (string, varlist->str);
89           g_string_append (string, ".");
90         }
91       g_string_free (varlist, TRUE);
92     }
93
94   text =  string->str;
95
96   g_string_free (string, FALSE);
97
98   return text;
99 };
100
101
102 static void
103 on_off_toggled (GtkToggleButton *togglebutton,
104                 gpointer         user_data)
105 {
106   GtkBuilder *xml = user_data;
107   GtkWidget *dest =   get_widget_assert (xml, "split-file-grouping-vars");
108   GtkWidget *selector = get_widget_assert (xml, "split-file-selector");
109   GtkWidget *source = get_widget_assert (xml, "split-file-dict-treeview");
110   GtkWidget *button3 = get_widget_assert (xml, "split-radiobutton3");
111   GtkWidget *button4 = get_widget_assert (xml, "split-radiobutton4");
112
113   gboolean state = !gtk_toggle_button_get_active (togglebutton);
114
115   gtk_widget_set_sensitive (dest, state);
116   gtk_widget_set_sensitive (selector, state);
117   gtk_widget_set_sensitive (source, state);
118   gtk_widget_set_sensitive (button3, state);
119   gtk_widget_set_sensitive (button4, state);
120 }
121
122 static void
123 refresh (PsppireDialog *dialog, struct split_file_dialog *d)
124 {
125   GtkWidget *off = get_widget_assert (d->xml, "split-radiobutton0");
126   GtkWidget *on = get_widget_assert (d->xml, "split-radiobutton1");
127
128   GtkTreeModel *liststore = gtk_tree_view_get_model (d->tv);
129
130   gint n_vars = dict_get_split_cnt (d->dict->dict);
131
132   gtk_list_store_clear (GTK_LIST_STORE (liststore));
133
134   if ( n_vars == 0 )
135     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(off), TRUE);
136   else
137     {
138       GtkTreeIter iter;
139       gint i;
140       const struct variable *const *vars = dict_get_split_vars (d->dict->dict);
141
142       for (i = 0 ; i < n_vars; ++i )
143         {
144           gtk_list_store_append (GTK_LIST_STORE (liststore), &iter);
145
146           gtk_list_store_set (GTK_LIST_STORE (liststore), &iter,
147                               0, vars[i],
148                               -1);
149         }
150
151       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(on), TRUE);
152     }
153
154   gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON(off));
155 }
156
157
158
159 /* Pops up the Split File dialog box */
160 void
161 split_file_dialog (PsppireDataWindow *de)
162 {
163   gint response;
164   struct split_file_dialog sfd;
165
166   GtkWidget *dialog   ;
167   GtkWidget *source   ;
168   GtkWidget *selector ;
169   GtkWidget *on_off   ;
170
171   sfd.xml = builder_new ("split-file.ui");
172
173   dialog = get_widget_assert   (sfd.xml, "split-file-dialog");
174   source = get_widget_assert   (sfd.xml, "split-file-dict-treeview");
175   selector = get_widget_assert (sfd.xml, "split-file-selector");
176   on_off = get_widget_assert   (sfd.xml, "split-radiobutton0");
177
178   sfd.tv = GTK_TREE_VIEW (get_widget_assert (sfd.xml, "split-file-grouping-vars"));
179
180   g_object_get (de->data_editor, "dictionary", &sfd.dict, NULL);
181
182   sfd.selector = PSPPIRE_SELECTOR (selector);
183
184   g_object_set (source, "model", sfd.dict, NULL);
185
186   g_signal_connect (on_off, "toggled", G_CALLBACK(on_off_toggled),  sfd.xml);
187
188   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &sfd);
189
190   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
191
192   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
193
194
195   switch (response)
196     {
197     case GTK_RESPONSE_OK:
198       g_free (execute_syntax_string (de, generate_syntax (&sfd)));
199       break;
200     case PSPPIRE_RESPONSE_PASTE:
201       g_free (paste_syntax_to_window (generate_syntax (&sfd)));
202       break;
203     default:
204       break;
205     }
206
207   g_object_unref (sfd.xml);
208 }
209