Fixed the refresh button on the dialogs.
[pspp-builds.git] / src / ui / gui / split-file-dialog.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2007  Free Software Foundation
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "split-file-dialog.h"
23 #include "psppire-selector.h"
24 #include "psppire-dialog.h"
25 #include "helper.h"
26 #include "data-editor.h"
27 #include "dict-display.h"
28 #include <language/syntax-string-source.h>
29 #include "syntax-editor.h"
30
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33
34
35 #include "dialog-common.h"
36
37 /* FIXME: These shouldn't be here */
38 #include <gtksheet/gtksheet.h>
39 #include "psppire-var-store.h"
40
41
42 struct split_file_dialog
43 {
44   /* The XML that created the dialog */
45   GladeXML *xml;
46
47   /* The dictionary to which this dialog pertains */
48   PsppireDict *dict;
49
50   /* The treeview widget containing the list of variables
51      upon which the file should be split */
52   GtkTreeView *tv;
53
54
55   PsppireSelector *selector;
56 };
57
58
59 static gchar *
60 generate_syntax (const struct split_file_dialog *sfd)
61 {
62   gchar *text;
63   GtkWidget *off = get_widget_assert (sfd->xml, "split-radiobutton0");
64
65   GtkWidget *vars =
66     get_widget_assert (sfd->xml, "split-file-grouping-vars");
67
68   GString *string = g_string_new ("SPLIT FILE OFF.");
69
70   if ( ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (off)))
71     {
72       GString * varlist = g_string_sized_new (80);
73       GtkWidget *sort = get_widget_assert (sfd->xml, "split-radiobutton3");
74       GtkWidget *layered = get_widget_assert (sfd->xml, "split-radiobutton1");
75       gint n_vars = append_variable_names (varlist,
76                                            sfd->dict, GTK_TREE_VIEW (vars));
77
78       if ( n_vars > 0 )
79         {
80           g_string_assign (string, "");
81
82           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sort)))
83             {
84               g_string_append (string, "SORT CASES BY");
85               g_string_append (string, varlist->str);
86               g_string_append (string, ".\n");
87             }
88
89           g_string_append (string, "SPLIT FILE ");
90
91           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (layered)))
92             g_string_append (string, "LAYERED ");
93           else
94             g_string_append (string, "SEPARATE ");
95
96           g_string_append (string, "BY ");
97           g_string_append (string, varlist->str);
98           g_string_append (string, ".");
99         }
100       g_string_free (varlist, TRUE);
101     }
102
103   text =  string->str;
104
105   g_string_free (string, FALSE);
106
107   return text;
108 };
109
110
111 static void
112 on_off_toggled (GtkToggleButton *togglebutton,
113                 gpointer         user_data)
114 {
115   GladeXML *xml = user_data;
116   GtkWidget *dest =   get_widget_assert (xml, "split-file-grouping-vars");
117   GtkWidget *selector = get_widget_assert (xml, "split-file-selector");
118   GtkWidget *source = get_widget_assert (xml, "split-file-dict-treeview");
119   GtkWidget *button3 = get_widget_assert (xml, "split-radiobutton3");
120   GtkWidget *button4 = get_widget_assert (xml, "split-radiobutton4");
121
122   gboolean state = !gtk_toggle_button_get_active (togglebutton);
123
124   gtk_widget_set_sensitive (dest, state);
125   gtk_widget_set_sensitive (selector, state);
126   gtk_widget_set_sensitive (source, state);
127   gtk_widget_set_sensitive (button3, state);
128   gtk_widget_set_sensitive (button4, state);
129 }
130
131 static void
132 refresh (PsppireDialog *dialog, struct split_file_dialog *d)
133 {
134   GtkWidget *off = get_widget_assert (d->xml, "split-radiobutton0");
135   GtkWidget *on = get_widget_assert (d->xml, "split-radiobutton1");
136
137   GtkTreeModel *liststore = gtk_tree_view_get_model (d->tv);
138
139   gint n_vars = dict_get_split_cnt (d->dict->dict);
140
141
142   gtk_list_store_clear (GTK_LIST_STORE (liststore));
143
144   if ( n_vars == 0 )
145     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(off), TRUE);
146   else
147     {
148       GtkTreeIter iter;
149       gint i;
150       struct variable *const *vars = dict_get_split_vars (d->dict->dict);
151
152       for (i = 0 ; i < n_vars; ++i )
153         {
154           gint idx = var_get_dict_index (vars[i]);
155
156           gtk_list_store_append (GTK_LIST_STORE (liststore), &iter);
157           gtk_list_store_set (GTK_LIST_STORE (liststore), &iter, 0, idx, -1);
158         }
159
160       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(on), TRUE);
161     }
162
163   gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON(off));
164 }
165
166
167
168 /* Pops up the Split File dialog box */
169 void
170 split_file_dialog (GObject *o, gpointer data)
171 {
172   gint response;
173   struct data_editor *de = data;
174   struct split_file_dialog sfd;
175
176   GtkWidget *dialog   ;
177   GtkWidget *source   ;
178   GtkWidget *dest     ;
179   GtkWidget *selector ;
180   GtkWidget *on_off   ;
181
182   GtkSheet *var_sheet ;
183
184   sfd.xml = XML_NEW ("psppire.glade");
185
186   dialog = get_widget_assert   (sfd.xml, "split-file-dialog");
187   source = get_widget_assert   (sfd.xml, "split-file-dict-treeview");
188   dest =   get_widget_assert   (sfd.xml, "split-file-grouping-vars");
189   selector = get_widget_assert (sfd.xml, "split-file-selector");
190   on_off = get_widget_assert   (sfd.xml, "split-radiobutton0");
191
192   var_sheet = GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
193
194   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
195
196   sfd.dict = vs->dict;
197   sfd.tv = GTK_TREE_VIEW (dest);
198   sfd.selector  = PSPPIRE_SELECTOR (
199                                     get_widget_assert   (sfd.xml, "split-file-selector"));
200
201   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
202                                  vs->dict,
203                                  GTK_SELECTION_MULTIPLE, NULL);
204
205
206   g_signal_connect (on_off, "toggled", G_CALLBACK(on_off_toggled),  sfd.xml);
207
208
209   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
210
211   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
212                                  source,
213                                  dest,
214                                  insert_source_row_into_tree_view,
215                                  NULL);
216
217   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &sfd);
218
219   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
220
221
222   switch (response)
223     {
224     case GTK_RESPONSE_OK:
225       {
226         gchar *syntax = generate_syntax (&sfd);
227         struct getl_interface *sss = create_syntax_string_source (syntax);
228         execute_syntax (sss);
229
230         g_free (syntax);
231       }
232       break;
233     case PSPPIRE_RESPONSE_PASTE:
234       {
235         gchar *syntax = generate_syntax (&sfd);
236
237         struct syntax_editor *se =
238           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
239
240         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
241
242         g_free (syntax);
243       }
244       break;
245     default:
246       break;
247     }
248
249   g_object_unref (sfd.xml);
250 }
251