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