Change license from GPLv2+ to GPLv3+.
[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
89
90   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
91                                  source, new_name_entry,
92                                  insert_source_row_into_entry,
93                                  is_currently_in_entry);
94
95
96   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  xml);
97
98   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
99
100   switch (response)
101     {
102     case GTK_RESPONSE_OK:
103       {
104         gchar *syntax = generate_syntax (vs->dict, xml);
105         struct getl_interface *sss = create_syntax_string_source (syntax);
106         execute_syntax (sss);
107
108         g_free (syntax);
109       }
110       break;
111     case PSPPIRE_RESPONSE_PASTE:
112       {
113         gchar *syntax = generate_syntax (vs->dict, xml);
114
115         struct syntax_editor *se =
116           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
117
118         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
119
120         g_free (syntax);
121       }
122       break;
123     default:
124       break;
125     }
126
127   g_object_unref (xml);
128 }
129
130
131   /*
132      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
133   */
134 static gchar *
135 generate_syntax (PsppireDict *dict, GladeXML *xml)
136 {
137   const gchar *text;
138   GString *string = g_string_new ("FLIP");
139   gchar *syntax ;
140
141   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
142   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
143
144   g_string_append (string, " /VARIABLES = ");
145
146   append_variable_names (string, dict, GTK_TREE_VIEW (dest));
147
148   text = gtk_entry_get_text (GTK_ENTRY (entry));
149
150   if ( text)
151     g_string_append_printf (string, " /NEWNAME = %s", text);
152
153   g_string_append (string, ".");
154
155   syntax = string->str;
156
157   g_string_free (string, FALSE);
158
159   return syntax;
160 }