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