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