Seperate the GtkBuilder definition for Split into its own file.
[pspp] / 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 "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
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 = psppire_var_view_append_names (PSPPIRE_VAR_VIEW (vars), 0, varlist);
74
75       if ( n_vars > 0 )
76         {
77           g_string_assign (string, "");
78
79           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sort)))
80             {
81               g_string_append (string, "SORT CASES BY");
82               g_string_append (string, varlist->str);
83               g_string_append (string, ".\n");
84             }
85
86           g_string_append (string, "SPLIT FILE ");
87
88           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (layered)))
89             g_string_append (string, "LAYERED ");
90           else
91             g_string_append (string, "SEPARATE ");
92
93           g_string_append (string, "BY ");
94           g_string_append (string, varlist->str);
95           g_string_append (string, ".");
96         }
97       g_string_free (varlist, TRUE);
98     }
99
100   text =  string->str;
101
102   g_string_free (string, FALSE);
103
104   return text;
105 };
106
107
108 static void
109 on_off_toggled (GtkToggleButton *togglebutton,
110                 gpointer         user_data)
111 {
112   GtkBuilder *xml = user_data;
113   GtkWidget *dest =   get_widget_assert (xml, "split-file-grouping-vars");
114   GtkWidget *selector = get_widget_assert (xml, "split-file-selector");
115   GtkWidget *source = get_widget_assert (xml, "split-file-dict-treeview");
116   GtkWidget *button3 = get_widget_assert (xml, "split-radiobutton3");
117   GtkWidget *button4 = get_widget_assert (xml, "split-radiobutton4");
118
119   gboolean state = !gtk_toggle_button_get_active (togglebutton);
120
121   gtk_widget_set_sensitive (dest, state);
122   gtk_widget_set_sensitive (selector, state);
123   gtk_widget_set_sensitive (source, state);
124   gtk_widget_set_sensitive (button3, state);
125   gtk_widget_set_sensitive (button4, state);
126 }
127
128 static void
129 refresh (PsppireDialog *dialog, struct split_file_dialog *d)
130 {
131   GtkWidget *off = get_widget_assert (d->xml, "split-radiobutton0");
132   GtkWidget *on = get_widget_assert (d->xml, "split-radiobutton1");
133
134   GtkTreeModel *liststore = gtk_tree_view_get_model (d->tv);
135
136   gint n_vars = dict_get_split_cnt (d->dict->dict);
137
138
139   gtk_list_store_clear (GTK_LIST_STORE (liststore));
140
141   if ( n_vars == 0 )
142     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(off), TRUE);
143   else
144     {
145       GtkTreeIter iter;
146       gint i;
147       const struct variable *const *vars = dict_get_split_vars (d->dict->dict);
148
149       for (i = 0 ; i < n_vars; ++i )
150         {
151           gint idx = var_get_dict_index (vars[i]);
152
153           gtk_list_store_append (GTK_LIST_STORE (liststore), &iter);
154           gtk_list_store_set (GTK_LIST_STORE (liststore), &iter, 0, idx, -1);
155         }
156
157       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(on), TRUE);
158     }
159
160   gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON(off));
161 }
162
163
164
165 /* Pops up the Split File dialog box */
166 void
167 split_file_dialog (PsppireDataWindow *de)
168 {
169   gint response;
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 ("split-file.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   g_object_get (vs, "dictionary", &sfd.dict, NULL);
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, "model", sfd.dict, NULL);
195
196   g_signal_connect (on_off, "toggled", G_CALLBACK(on_off_toggled),  sfd.xml);
197
198   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &sfd);
199
200   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
201
202   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
203
204
205   switch (response)
206     {
207     case GTK_RESPONSE_OK:
208       {
209         gchar *syntax = generate_syntax (&sfd);
210
211         struct getl_interface *sss = create_syntax_string_source (syntax);
212         execute_syntax (sss);
213
214         g_free (syntax);
215       }
216       break;
217     case PSPPIRE_RESPONSE_PASTE:
218       {
219         gchar *syntax = generate_syntax (&sfd);
220         paste_syntax_to_window (syntax);
221
222         g_free (syntax);
223       }
224       break;
225     default:
226       break;
227     }
228
229   g_object_unref (sfd.xml);
230 }
231