New module psppire-var-view
[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-var-view.h"
22 #include "psppire-dialog.h"
23 #include "executor.h"
24 #include "psppire-data-window.h"
25 #include "dict-display.h"
26 #include <language/syntax-string-source.h>
27 #include "helper.h"
28
29 #include "dialog-common.h"
30
31 #include <gtk/gtk.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 "psppire-var-store.h"
41
42
43 static gchar * generate_syntax (PsppireDict *dict, GtkBuilder *xml);
44
45 static void
46 refresh (PsppireDialog *dialog, gpointer data)
47 {
48   GtkBuilder *xml = data;
49   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
50   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
51   GtkTreeModel *dmodel = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
52
53   gtk_list_store_clear (GTK_LIST_STORE (dmodel));
54   gtk_entry_set_text (GTK_ENTRY (entry), "");
55 }
56
57 static gboolean
58 dialog_state_valid (gpointer data)
59 {
60   GtkBuilder *xml = data;
61
62   GtkWidget *tv = get_widget_assert (xml, "variables-treeview");
63   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
64
65   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (tv));
66
67   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
68
69   if ( n_rows == 0 )
70     return FALSE;
71
72   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))))
73     return FALSE;
74
75   return TRUE;
76 }
77
78
79 void
80 transpose_dialog (GObject *o, gpointer data)
81 {
82   gint response ;
83   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
84   PsppireDict *dict = NULL;
85
86   GtkBuilder *xml = builder_new ("psppire.ui");
87
88   PsppireVarStore *vs = NULL;
89
90   GtkWidget *dialog = get_widget_assert (xml, "transpose-dialog");
91   GtkWidget *source = get_widget_assert (xml, "source-treeview");
92   GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector3");
93
94   g_object_get (de->data_editor, "var-store", &vs, NULL);
95
96   g_object_get (vs, "dictionary", &dict, NULL);
97   g_object_set (source, "model", dict, NULL);
98
99   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector2),
100                                  is_currently_in_entry);
101
102
103   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  xml);
104
105   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
106
107   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
108                                       dialog_state_valid, xml);
109
110   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
111
112   switch (response)
113     {
114     case GTK_RESPONSE_OK:
115       {
116         gchar *syntax = generate_syntax (dict, xml);
117
118         struct getl_interface *sss = create_syntax_string_source (syntax);
119         execute_syntax (sss);
120
121         g_free (syntax);
122       }
123       break;
124     case PSPPIRE_RESPONSE_PASTE:
125       {
126         gchar *syntax = generate_syntax (dict, xml);
127         paste_syntax_in_new_window (syntax);
128
129         g_free (syntax);
130       }
131       break;
132     default:
133       break;
134     }
135
136   g_object_unref (xml);
137 }
138
139
140   /*
141      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
142   */
143 static gchar *
144 generate_syntax (PsppireDict *dict, GtkBuilder *xml)
145 {
146   const gchar *text;
147   GString *string = g_string_new ("FLIP");
148   gchar *syntax ;
149
150   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
151   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
152
153   g_string_append (string, " /VARIABLES = ");
154
155   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (dest), 0, string);
156
157   text = gtk_entry_get_text (GTK_ENTRY (entry));
158
159   if ( text)
160     g_string_append_printf (string, " /NEWNAME = %s", text);
161
162   g_string_append (string, ".");
163
164   syntax = string->str;
165
166   g_string_free (string, FALSE);
167
168   return syntax;
169 }