Fixed the refresh button on the dialogs.
[pspp-builds.git] / src / ui / gui / transpose-dialog.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2007  Free Software Foundation
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "transpose-dialog.h"
23 #include "psppire-selector.h"
24 #include "psppire-dialog.h"
25 #include "helper.h"
26 #include "data-editor.h"
27 #include "dict-display.h"
28 #include <language/syntax-string-source.h>
29 #include "syntax-editor.h"
30
31 #include "dialog-common.h"
32
33 #include <gtk/gtk.h>
34 #include <glade/glade.h>
35
36 #include <gettext.h>
37
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41
42 /* FIXME: These shouldn't be here */
43 #include <gtksheet/gtksheet.h>
44 #include "psppire-var-store.h"
45
46
47 static gchar * generate_syntax (PsppireDict *dict, GladeXML *xml);
48
49 static void
50 refresh (PsppireDialog *dialog, gpointer data)
51 {
52   GladeXML *xml = data;
53   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
54   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
55   GtkTreeModel *dmodel = gtk_tree_view_get_model (dest);
56
57   gtk_list_store_clear (GTK_LIST_STORE (dmodel));
58   gtk_entry_set_text (GTK_ENTRY (entry), "");
59 }
60
61 void
62 transpose_dialog (GObject *o, gpointer data)
63 {
64   gint response ;
65   struct data_editor *de = data;
66
67   GladeXML *xml = XML_NEW ("psppire.glade");
68
69   GtkSheet *var_sheet =
70     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
71
72   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
73
74   GtkWidget *dialog = get_widget_assert (xml, "transpose-dialog");
75   GtkWidget *source = get_widget_assert (xml, "source-treeview");
76   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
77   GtkWidget *selector1 = get_widget_assert (xml, "psppire-selector2");
78   GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector3");
79   GtkWidget *new_name_entry = get_widget_assert (xml, "new-name-entry");
80
81   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
82                                  vs->dict,
83                                  GTK_SELECTION_MULTIPLE, NULL);
84
85   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
86
87   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
88                                  source, dest,
89                                  insert_source_row_into_tree_view,
90                                  NULL);
91
92
93   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
94                                  source, new_name_entry,
95                                  insert_source_row_into_entry,
96                                  is_currently_in_entry);
97
98
99   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  xml);
100
101   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
102
103   switch (response)
104     {
105     case GTK_RESPONSE_OK:
106       {
107         gchar *syntax = generate_syntax (vs->dict, xml);
108         struct getl_interface *sss = create_syntax_string_source (syntax);
109         execute_syntax (sss);
110
111         g_free (syntax);
112       }
113       break;
114     case PSPPIRE_RESPONSE_PASTE:
115       {
116         gchar *syntax = generate_syntax (vs->dict, xml);
117
118         struct syntax_editor *se =
119           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
120
121         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
122
123         g_free (syntax);
124       }
125       break;
126     default:
127       break;
128     }
129
130   g_object_unref (xml);
131 }
132
133
134   /*
135      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
136   */
137 static gchar *
138 generate_syntax (PsppireDict *dict, GladeXML *xml)
139 {
140   const gchar *text;
141   GString *string = g_string_new ("FLIP");
142   gchar *syntax ;
143
144   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
145   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
146
147   g_string_append (string, " /VARIABLES = ");
148
149   append_variable_names (string, dict, GTK_TREE_VIEW (dest));
150
151   text = gtk_entry_get_text (GTK_ENTRY (entry));
152
153   if ( text)
154     g_string_append_printf (string, " /NEWNAME = %s", text);
155
156   g_string_append (string, ".");
157
158   syntax = string->str;
159
160   g_string_free (string, FALSE);
161
162   return syntax;
163 }