Numerous GUI enhancements.
[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
43 static gchar *
44 generate_syntax (GladeXML *xml, PsppireDict *dict)
45 {
46   gchar *text;
47   GtkWidget *off = get_widget_assert (xml, "split-radiobutton0");
48
49   GtkWidget *vars =
50     get_widget_assert (xml, "split-file-grouping-vars");
51
52   GString *string = g_string_new ("SPLIT FILE OFF.");
53
54   if ( ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (off)))
55     {
56       GString * varlist = g_string_sized_new (80);
57       GtkWidget *sort = get_widget_assert (xml, "split-radiobutton3");
58       GtkWidget *layered = get_widget_assert (xml, "split-radiobutton1");
59       gint n_vars = append_variable_names (varlist,
60                                            dict, GTK_TREE_VIEW (vars));
61
62       if ( n_vars > 0 )
63         {
64           g_string_assign (string, "");
65
66           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sort)))
67             {
68               g_string_append (string, "SORT CASES BY");
69               g_string_append (string, varlist->str);
70               g_string_append (string, ".\n");
71             }
72
73           g_string_append (string, "SPLIT FILE ");
74
75           if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (layered)))
76             g_string_append (string, "LAYERED ");
77           else
78             g_string_append (string, "SEPARATE ");
79
80           g_string_append (string, "BY ");
81           g_string_append (string, varlist->str);
82           g_string_append (string, ".");
83         }
84       g_string_free (varlist, TRUE);
85     }
86
87   text =  string->str;
88
89   g_string_free (string, FALSE);
90
91   return text;
92 };
93
94
95 static void
96 on_off_toggled (GtkToggleButton *togglebutton,
97                 gpointer         user_data)
98 {
99   GladeXML *xml = user_data;
100   GtkWidget *dest =   get_widget_assert (xml, "split-file-grouping-vars");
101   GtkWidget *selector = get_widget_assert (xml, "split-file-selector");
102   GtkWidget *source = get_widget_assert (xml, "split-file-dict-treeview");
103   GtkWidget *button3 = get_widget_assert (xml, "split-radiobutton3");
104   GtkWidget *button4 = get_widget_assert (xml, "split-radiobutton4");
105
106   gboolean state = !gtk_toggle_button_get_active (togglebutton);
107
108   gtk_widget_set_sensitive (dest, state);
109   gtk_widget_set_sensitive (selector, state);
110   gtk_widget_set_sensitive (source, state);
111   gtk_widget_set_sensitive (button3, state);
112   gtk_widget_set_sensitive (button4, state);
113 }
114
115 static void
116 refresh (GladeXML *xml)
117 {
118   GtkWidget *off = get_widget_assert (xml, "split-radiobutton0");
119
120   g_print ("%s\n", __FUNCTION__);
121
122   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(off), TRUE);
123   gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON(off));
124 }
125
126
127
128 /* Pops up the Weight Cases dialog box */
129 void
130 split_file_dialog (GObject *o, gpointer data)
131 {
132   gint response;
133   struct data_editor *de = data;
134   PsppireDict *dict;
135
136   GladeXML *xml = XML_NEW ("psppire.glade");
137
138   GtkWidget *dialog = get_widget_assert   (xml, "split-file-dialog");
139   GtkWidget *source = get_widget_assert   (xml, "split-file-dict-treeview");
140   GtkWidget *dest =   get_widget_assert   (xml, "split-file-grouping-vars");
141   GtkWidget *selector = get_widget_assert (xml, "split-file-selector");
142   GtkWidget *on_off = get_widget_assert   (xml, "split-radiobutton0");
143
144   GtkSheet *var_sheet =
145     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
146
147   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
148
149   dict = vs->dict;
150
151   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
152                                  vs->dict,
153                                  GTK_SELECTION_MULTIPLE, NULL);
154
155
156   g_signal_connect (on_off, "toggled", G_CALLBACK(on_off_toggled),  xml);
157
158
159   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
160
161   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
162                                  source,
163                                  dest,
164                                  insert_source_row_into_tree_view,
165                                  NULL);
166
167   refresh (xml);
168
169   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
170
171
172   switch (response)
173     {
174     case GTK_RESPONSE_OK:
175       {
176         gchar *syntax = generate_syntax (xml, dict);
177         struct getl_interface *sss = create_syntax_string_source (syntax);
178         execute_syntax (sss);
179
180         g_free (syntax);
181       }
182       break;
183     case PSPPIRE_RESPONSE_PASTE:
184       {
185         gchar *syntax = generate_syntax (xml, dict);
186
187         struct syntax_editor *se =
188           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
189
190         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
191
192         g_free (syntax);
193       }
194       break;
195     default:
196       break;
197     }
198
199   g_object_unref (xml);
200 }
201