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