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