Change gpointer variable to PsppireDataWindow * in dialog function signatures
[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 (PsppireDataWindow *de)
81 {
82   gint response ;
83   PsppireDict *dict = NULL;
84
85   GtkBuilder *xml = builder_new ("psppire.ui");
86
87   PsppireVarStore *vs = NULL;
88
89   GtkWidget *dialog = get_widget_assert (xml, "transpose-dialog");
90   GtkWidget *source = get_widget_assert (xml, "source-treeview");
91   GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector3");
92
93   g_object_get (de->data_editor, "var-store", &vs, NULL);
94
95   g_object_get (vs, "dictionary", &dict, NULL);
96   g_object_set (source, "model", dict, NULL);
97
98   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector2),
99                                  is_currently_in_entry);
100
101
102   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  xml);
103
104   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
105
106   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
107                                       dialog_state_valid, xml);
108
109   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
110
111   switch (response)
112     {
113     case GTK_RESPONSE_OK:
114       {
115         gchar *syntax = generate_syntax (dict, xml);
116
117         struct getl_interface *sss = create_syntax_string_source (syntax);
118         execute_syntax (sss);
119
120         g_free (syntax);
121       }
122       break;
123     case PSPPIRE_RESPONSE_PASTE:
124       {
125         gchar *syntax = generate_syntax (dict, xml);
126         paste_syntax_in_new_window (syntax);
127
128         g_free (syntax);
129       }
130       break;
131     default:
132       break;
133     }
134
135   g_object_unref (xml);
136 }
137
138
139   /*
140      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
141   */
142 static gchar *
143 generate_syntax (PsppireDict *dict, GtkBuilder *xml)
144 {
145   const gchar *text;
146   GString *string = g_string_new ("FLIP");
147   gchar *syntax ;
148
149   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
150   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
151
152   g_string_append (string, " /VARIABLES = ");
153
154   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (dest), 0, string);
155
156   text = gtk_entry_get_text (GTK_ENTRY (entry));
157
158   if ( text)
159     g_string_append_printf (string, " /NEWNAME = %s", text);
160
161   g_string_append (string, ".");
162
163   syntax = string->str;
164
165   g_string_free (string, FALSE);
166
167   return syntax;
168 }