Numerous GUI enhancements.
[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 void
50 transpose_dialog (GObject *o, gpointer data)
51 {
52   gint response ;
53   struct data_editor *de = data;
54
55   GladeXML *xml = XML_NEW ("psppire.glade");
56
57   GtkSheet *var_sheet =
58     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
59
60   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
61
62   GtkWidget *dialog = get_widget_assert (xml, "transpose-dialog");
63   GtkWidget *source = get_widget_assert (xml, "source-treeview");
64   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
65   GtkWidget *selector1 = get_widget_assert (xml, "psppire-selector2");
66   GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector3");
67   GtkWidget *new_name_entry = get_widget_assert (xml, "new-name-entry");
68
69   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
70                                  vs->dict,
71                                  GTK_SELECTION_MULTIPLE, NULL);
72
73   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
74
75   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
76                                  source, dest,
77                                  insert_source_row_into_tree_view,
78                                  NULL);
79
80
81   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
82                                  source, new_name_entry,
83                                  insert_source_row_into_entry,
84                                  is_currently_in_entry);
85
86
87   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
88
89   switch (response)
90     {
91     case GTK_RESPONSE_OK:
92       {
93         gchar *syntax = generate_syntax (vs->dict, xml);
94         struct getl_interface *sss = create_syntax_string_source (syntax);
95         execute_syntax (sss);
96
97         g_free (syntax);
98       }
99       break;
100     case PSPPIRE_RESPONSE_PASTE:
101       {
102         gchar *syntax = generate_syntax (vs->dict, xml);
103
104         struct syntax_editor *se =
105           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
106
107         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
108
109         g_free (syntax);
110       }
111       break;
112     default:
113       break;
114     }
115
116   g_object_unref (xml);
117 }
118
119
120   /*
121      FLIP /VARIABLES=var_list /NEWNAMES=var_name.
122   */
123 static gchar *
124 generate_syntax (PsppireDict *dict, GladeXML *xml)
125 {
126   const gchar *text;
127   GString *string = g_string_new ("FLIP");
128   gchar *syntax ;
129
130   GtkWidget *dest = get_widget_assert (xml, "variables-treeview");
131   GtkWidget *entry = get_widget_assert (xml, "new-name-entry");
132
133   g_string_append (string, " /VARIABLES = ");
134
135   append_variable_names (string, dict, GTK_TREE_VIEW (dest));
136
137   text = gtk_entry_get_text (GTK_ENTRY (entry));
138
139   if ( text)
140     g_string_append_printf (string, " /NEWNAME = %s", text);
141
142   g_string_append (string, ".");
143
144   syntax = string->str;
145
146   g_string_free (string, FALSE);
147
148   return syntax;
149 }