Runs dialog: convert from old style to PsppireDialogAction
[pspp] / src / ui / gui / transpose-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011, 2012  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 "builder-wrapper.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 static gchar * generate_syntax (PsppireDict *dict, GtkBuilder *xml);
40
41 static void
42 refresh (PsppireDialog *dialog, gpointer data)
43 {
44   GtkBuilder *xml = data;
45   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
46   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
47   GtkTreeModel *dmodel = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
48
49   gtk_list_store_clear (GTK_LIST_STORE (dmodel));
50   gtk_entry_set_text (GTK_ENTRY (entry), "");
51 }
52
53 static gboolean
54 dialog_state_valid (gpointer data)
55 {
56   GtkBuilder *xml = data;
57
58   GtkWidget *tv = get_widget_assert (xml, "variables-treeview");
59   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
60
61   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (tv));
62
63   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
64
65   if ( n_rows == 0 )
66     return FALSE;
67
68   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))))
69     return FALSE;
70
71   return TRUE;
72 }
73
74
75 void
76 transpose_dialog (PsppireDataWindow *de)
77 {
78   gint response ;
79   PsppireDict *dict = NULL;
80
81   GtkBuilder *xml = builder_new ("psppire.ui");
82
83   GtkWidget *dialog = get_widget_assert (xml, "transpose-dialog");
84   GtkWidget *source = get_widget_assert (xml, "source-treeview");
85   GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector3");
86
87   g_object_get (de->data_editor, "dictionary", &dict, NULL);
88   g_object_set (source, "model", dict, NULL);
89
90   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector2),
91                                  is_currently_in_entry);
92
93
94   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  xml);
95
96   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
97
98   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
99                                       dialog_state_valid, xml);
100
101   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
102
103   switch (response)
104     {
105     case GTK_RESPONSE_OK:
106       g_free (execute_syntax_string (de, generate_syntax (dict, xml)));
107       break;
108     case PSPPIRE_RESPONSE_PASTE:
109       {
110         gchar *syntax = generate_syntax (dict, xml);
111         paste_syntax_to_window (syntax);
112
113         g_free (syntax);
114       }
115       break;
116     default:
117       break;
118     }
119
120   g_object_unref (xml);
121 }
122
123
124   /*
125      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
126   */
127 static gchar *
128 generate_syntax (PsppireDict *dict, GtkBuilder *xml)
129 {
130   const gchar *text;
131   GString *string = g_string_new ("FLIP");
132   gchar *syntax ;
133
134   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
135   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
136
137   g_string_append (string, " /VARIABLES = ");
138
139   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (dest), 0, string);
140
141   text = gtk_entry_get_text (GTK_ENTRY (entry));
142
143   if ( text)
144     g_string_append_printf (string, " /NEWNAME = %s", text);
145
146   g_string_append (string, ".");
147
148   syntax = string->str;
149
150   g_string_free (string, FALSE);
151
152   return syntax;
153 }