Implemented the paired samples t test dialog. Closes patch #6378
[pspp-builds.git] / src / ui / gui / transpose-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 "transpose-dialog.h"
20 #include "psppire-selector.h"
21 #include "psppire-dialog.h"
22 #include "helper.h"
23 #include "data-editor.h"
24 #include "dict-display.h"
25 #include <language/syntax-string-source.h>
26 #include "syntax-editor.h"
27
28 #include "dialog-common.h"
29
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
32
33 #include <gettext.h>
34
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 /* FIXME: These shouldn't be here */
40 #include <gtksheet/gtksheet.h>
41 #include "psppire-var-store.h"
42
43
44 static gchar * generate_syntax (PsppireDict *dict, GladeXML *xml);
45
46 static void
47 refresh (PsppireDialog *dialog, gpointer data)
48 {
49   GladeXML *xml = data;
50   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
51   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
52   GtkTreeModel *dmodel = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
53
54   gtk_list_store_clear (GTK_LIST_STORE (dmodel));
55   gtk_entry_set_text (GTK_ENTRY (entry), "");
56 }
57
58 void
59 transpose_dialog (GObject *o, gpointer data)
60 {
61   gint response ;
62   struct data_editor *de = data;
63
64   GladeXML *xml = XML_NEW ("psppire.glade");
65
66   GtkSheet *var_sheet =
67     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
68
69   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
70
71   GtkWidget *dialog = get_widget_assert (xml, "transpose-dialog");
72   GtkWidget *source = get_widget_assert (xml, "source-treeview");
73   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
74   GtkWidget *selector1 = get_widget_assert (xml, "psppire-selector2");
75   GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector3");
76   GtkWidget *new_name_entry = get_widget_assert (xml, "new-name-entry");
77
78   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
79                                  vs->dict,
80                                  GTK_SELECTION_MULTIPLE, NULL);
81
82   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
83
84   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
85                                  source, dest,
86                                  insert_source_row_into_tree_view,
87                                  NULL,
88                                  NULL);
89
90
91   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
92                                  source, new_name_entry,
93                                  insert_source_row_into_entry,
94                                  is_currently_in_entry,
95                                  NULL);
96
97
98   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  xml);
99
100   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
101
102   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
103
104   switch (response)
105     {
106     case GTK_RESPONSE_OK:
107       {
108         gchar *syntax = generate_syntax (vs->dict, xml);
109         struct getl_interface *sss = create_syntax_string_source (syntax);
110         execute_syntax (sss);
111
112         g_free (syntax);
113       }
114       break;
115     case PSPPIRE_RESPONSE_PASTE:
116       {
117         gchar *syntax = generate_syntax (vs->dict, xml);
118
119         struct syntax_editor *se =
120           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
121
122         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
123
124         g_free (syntax);
125       }
126       break;
127     default:
128       break;
129     }
130
131   g_object_unref (xml);
132 }
133
134
135   /*
136      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
137   */
138 static gchar *
139 generate_syntax (PsppireDict *dict, GladeXML *xml)
140 {
141   const gchar *text;
142   GString *string = g_string_new ("FLIP");
143   gchar *syntax ;
144
145   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
146   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
147
148   g_string_append (string, " /VARIABLES = ");
149
150   append_variable_names (string, dict, GTK_TREE_VIEW (dest), 0);
151
152   text = gtk_entry_get_text (GTK_ENTRY (entry));
153
154   if ( text)
155     g_string_append_printf (string, " /NEWNAME = %s", text);
156
157   g_string_append (string, ".");
158
159   syntax = string->str;
160
161   g_string_free (string, FALSE);
162
163   return syntax;
164 }