458a3c53532a1eafdddbd9552f14cc4b11286ffc
[pspp-builds.git] / src / ui / gui / split-file-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007  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 <gtk/gtk.h>
30
31
32 #include "dialog-common.h"
33
34 /* FIXME: These shouldn't be here */
35 #include "psppire-var-store.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
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   GtkWidget *vars =
62     get_widget_assert (sfd->xml, "split-file-grouping-vars");
63
64   GString *string = g_string_new ("SPLIT FILE OFF.");
65
66   if ( ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (off)))
67     {
68       GString * varlist = g_string_sized_new (80);
69       GtkWidget *sort = get_widget_assert (sfd->xml, "split-radiobutton3");
70       GtkWidget *layered = get_widget_assert (sfd->xml, "split-radiobutton1");
71       gint n_vars = append_variable_names (varlist,
72                                            sfd->dict, GTK_TREE_VIEW (vars), 0);
73
74       if ( n_vars > 0 )
75         {
76           g_string_assign (string, "");
77
78           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sort)))
79             {
80               g_string_append (string, "SORT CASES BY");
81               g_string_append (string, varlist->str);
82               g_string_append (string, ".\n");
83             }
84
85           g_string_append (string, "SPLIT FILE ");
86
87           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (layered)))
88             g_string_append (string, "LAYERED ");
89           else
90             g_string_append (string, "SEPARATE ");
91
92           g_string_append (string, "BY ");
93           g_string_append (string, varlist->str);
94           g_string_append (string, ".");
95         }
96       g_string_free (varlist, TRUE);
97     }
98
99   text =  string->str;
100
101   g_string_free (string, FALSE);
102
103   return text;
104 };
105
106
107 static void
108 on_off_toggled (GtkToggleButton *togglebutton,
109                 gpointer         user_data)
110 {
111   GtkBuilder *xml = user_data;
112   GtkWidget *dest =   get_widget_assert (xml, "split-file-grouping-vars");
113   GtkWidget *selector = get_widget_assert (xml, "split-file-selector");
114   GtkWidget *source = get_widget_assert (xml, "split-file-dict-treeview");
115   GtkWidget *button3 = get_widget_assert (xml, "split-radiobutton3");
116   GtkWidget *button4 = get_widget_assert (xml, "split-radiobutton4");
117
118   gboolean state = !gtk_toggle_button_get_active (togglebutton);
119
120   gtk_widget_set_sensitive (dest, state);
121   gtk_widget_set_sensitive (selector, state);
122   gtk_widget_set_sensitive (source, state);
123   gtk_widget_set_sensitive (button3, state);
124   gtk_widget_set_sensitive (button4, state);
125 }
126
127 static void
128 refresh (PsppireDialog *dialog, struct split_file_dialog *d)
129 {
130   GtkWidget *off = get_widget_assert (d->xml, "split-radiobutton0");
131   GtkWidget *on = get_widget_assert (d->xml, "split-radiobutton1");
132
133   GtkTreeModel *liststore = gtk_tree_view_get_model (d->tv);
134
135   gint n_vars = dict_get_split_cnt (d->dict->dict);
136
137
138   gtk_list_store_clear (GTK_LIST_STORE (liststore));
139
140   if ( n_vars == 0 )
141     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(off), TRUE);
142   else
143     {
144       GtkTreeIter iter;
145       gint i;
146       const struct variable *const *vars = dict_get_split_vars (d->dict->dict);
147
148       for (i = 0 ; i < n_vars; ++i )
149         {
150           gint idx = var_get_dict_index (vars[i]);
151
152           gtk_list_store_append (GTK_LIST_STORE (liststore), &iter);
153           gtk_list_store_set (GTK_LIST_STORE (liststore), &iter, 0, idx, -1);
154         }
155
156       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(on), TRUE);
157     }
158
159   gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON(off));
160 }
161
162
163
164 /* Pops up the Split File dialog box */
165 void
166 split_file_dialog (GObject *o, gpointer data)
167 {
168   gint response;
169   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
170   struct split_file_dialog sfd;
171   PsppireVarStore *vs ;
172
173   GtkWidget *dialog   ;
174   GtkWidget *source   ;
175   GtkWidget *dest     ;
176   GtkWidget *selector ;
177   GtkWidget *on_off   ;
178
179   sfd.xml = builder_new ("psppire.ui");
180
181   dialog = get_widget_assert   (sfd.xml, "split-file-dialog");
182   source = get_widget_assert   (sfd.xml, "split-file-dict-treeview");
183   dest =   get_widget_assert   (sfd.xml, "split-file-grouping-vars");
184   selector = get_widget_assert (sfd.xml, "split-file-selector");
185   on_off = get_widget_assert   (sfd.xml, "split-radiobutton0");
186
187   g_object_get (de->data_editor, "var-store", &vs, NULL);
188
189   sfd.dict = vs->dict;
190   sfd.tv = GTK_TREE_VIEW (dest);
191   sfd.selector  = PSPPIRE_SELECTOR (
192                                     get_widget_assert   (sfd.xml, "split-file-selector"));
193
194   g_object_set (source, "dictionary",
195                                  vs->dict, NULL);
196
197
198   g_signal_connect (on_off, "toggled", G_CALLBACK(on_off_toggled),  sfd.xml);
199
200
201   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
202
203   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
204                                  source,
205                                  dest,
206                                  insert_source_row_into_tree_view,
207                                  NULL,
208                                  NULL);
209
210   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &sfd);
211
212   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
213
214   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
215
216
217   switch (response)
218     {
219     case GTK_RESPONSE_OK:
220       {
221         gchar *syntax = generate_syntax (&sfd);
222
223         struct getl_interface *sss = create_syntax_string_source (syntax);
224         execute_syntax (sss);
225
226         g_free (syntax);
227       }
228       break;
229     case PSPPIRE_RESPONSE_PASTE:
230       {
231         gchar *syntax = generate_syntax (&sfd);
232         paste_syntax_in_new_window (syntax);
233
234         g_free (syntax);
235       }
236       break;
237     default:
238       break;
239     }
240
241   g_object_unref (sfd.xml);
242 }
243