1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2012 Free Software Foundation
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.
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.
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/>. */
19 #include "dialog-common.h"
20 #include "autorecode-dialog.h"
21 #include "psppire-selector.h"
22 #include "psppire-dictview.h"
23 #include "psppire-var-ptr.h"
24 #include "psppire-dialog.h"
26 #include "psppire-data-window.h"
27 #include "psppire-var-view.h"
30 #include "builder-wrapper.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
49 GtkWidget *change_button;
50 GtkWidget *new_name_entry;
53 /* A hash table of struct nlp's indexed by variable */
62 nlp_create (const char *name, const char *label)
64 struct nlp *nlp = xmalloc (sizeof *nlp);
66 nlp->name = g_strdup (name);
70 if ( label != NULL && 0 != strcmp ("", label))
71 nlp->label = g_strdup (label);
77 nlp_destroy (gpointer data)
79 struct nlp *nlp = data ;
89 on_change_clicked (GObject *obj, gpointer data)
91 struct autorecode *rd = data;
92 struct variable *var = NULL;
94 GtkTreeModel *model = GTK_TREE_MODEL (PSPPIRE_VAR_VIEW (rd->var_view)->list);
96 GtkTreeSelection *selection =
97 gtk_tree_view_get_selection (GTK_TREE_VIEW (rd->var_view));
99 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
101 const gchar *dest_var_name =
102 gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
104 if ( NULL == rows || rows->next != NULL)
107 gtk_tree_model_get_iter (model, &iter, rows->data);
109 gtk_tree_model_get (model, &iter, 0, &var, -1);
111 g_hash_table_remove (rd->varmap, var);
113 nlp = nlp_create (dest_var_name, NULL);
115 g_hash_table_insert (rd->varmap, var, nlp);
117 gtk_tree_model_row_changed (model, rows->data, &iter);
120 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
125 static char * generate_syntax (const struct autorecode *rd);
129 refresh (PsppireDialog *dialog, struct autorecode *rd)
131 GtkTreeModel *target_list = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->var_view));
133 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
134 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
135 gtk_widget_set_sensitive (rd->change_button, FALSE);
137 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->ascending), TRUE);
138 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->group), FALSE);
139 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->blank), FALSE);
142 g_hash_table_remove_all (rd->varmap);
144 gtk_list_store_clear (GTK_LIST_STORE (target_list));
149 dialog_state_valid (gpointer data)
151 struct autorecode *rd = data;
153 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->var_view));
155 if (g_hash_table_size (rd->varmap) != gtk_tree_model_iter_n_children (model, NULL) )
163 on_entry_change (struct autorecode *rd)
165 gboolean valid = TRUE;
166 const char *text = gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
168 if ( 0 == strcmp ("", text))
170 else if (psppire_dict_lookup_var (rd->dict, text))
177 g_hash_table_iter_init (&iter, rd->varmap);
178 while (g_hash_table_iter_next (&iter, &key, &value))
180 struct nlp *nlp = value;
182 if ( 0 == strcmp (nlp->name, text))
191 gtk_widget_set_sensitive (rd->change_button, valid);
194 /* Callback which gets called when a new row is selected
195 in the variable treeview.
196 It sets the name and label entry widgets to reflect the
197 currently selected row.
200 on_selection_change (GtkTreeSelection *selection, gpointer data)
202 struct autorecode *rd = data;
203 GtkTreeModel *model = GTK_TREE_MODEL (PSPPIRE_VAR_VIEW (rd->var_view)->list);
204 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
206 if ( rows && !rows->next)
208 /* Exactly one row is selected */
210 struct variable *var;
214 gtk_widget_set_sensitive (rd->new_name_entry, TRUE);
215 gtk_widget_set_sensitive (rd->change_button, TRUE);
218 ok = gtk_tree_model_get_iter (model, &iter, (GtkTreePath*) rows->data);
220 gtk_tree_model_get (model, &iter, 0, &var, -1);
222 nlp = g_hash_table_lookup (rd->varmap, var);
225 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), nlp->name ? nlp->name : "");
227 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
231 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
232 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
233 gtk_widget_set_sensitive (rd->change_button, FALSE);
236 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
241 render_new_var_name (GtkTreeViewColumn *tree_column,
242 GtkCellRenderer *cell,
243 GtkTreeModel *tree_model,
247 struct nlp *nlp = NULL;
248 struct autorecode *rd = data;
250 struct variable *var = NULL;
252 gtk_tree_model_get (tree_model, iter,
256 nlp = g_hash_table_lookup (rd->varmap, var);
259 g_object_set (cell, "text", nlp->name, NULL);
261 g_object_set (cell, "text", "", NULL);
266 /* Pops up the Autorecode dialog box */
268 autorecode_dialog (PsppireDataWindow *de)
270 struct autorecode rd;
273 GtkBuilder *xml = builder_new ("autorecode.ui");
276 GtkWidget *dialog = get_widget_assert (xml, "autorecode-dialog");
277 GtkWidget *source = get_widget_assert (xml, "dict-view");
279 rd.var_view = get_widget_assert (xml, "var-view");
281 rd.new_name_entry = get_widget_assert (xml, "entry1");
282 rd.change_button = get_widget_assert (xml, "button1");
283 rd.ascending = get_widget_assert (xml, "radiobutton1");
284 rd.group = get_widget_assert (xml, "checkbutton1");
285 rd.blank = get_widget_assert (xml, "checkbutton2");
288 GtkTreeSelection *sel;
290 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
292 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes (_("New"),
297 gtk_tree_view_column_set_cell_data_func (col, renderer,
301 gtk_tree_view_append_column (GTK_TREE_VIEW (rd.var_view), col);
304 col = gtk_tree_view_get_column (GTK_TREE_VIEW (rd.var_view), 0);
306 g_object_set (col, "title", _("Old"), NULL);
308 g_object_set (rd.var_view, "headers-visible", TRUE, NULL);
310 rd.varmap = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, nlp_destroy);
313 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd.var_view));
316 g_signal_connect (sel, "changed",
317 G_CALLBACK (on_selection_change), &rd);
319 g_signal_connect (rd.change_button, "clicked",
320 G_CALLBACK (on_change_clicked), &rd);
322 g_signal_connect_swapped (rd.new_name_entry, "changed",
323 G_CALLBACK (on_entry_change), &rd);
327 g_object_get (de->data_editor, "var-store", &vs, NULL);
329 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
331 g_object_get (vs, "dictionary", &rd.dict, NULL);
332 g_object_set (source, "model", rd.dict, NULL);
335 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &rd);
337 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
338 dialog_state_valid, &rd);
340 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
345 case GTK_RESPONSE_OK:
346 g_free (execute_syntax_string (de, generate_syntax (&rd)));
348 case PSPPIRE_RESPONSE_PASTE:
349 g_free (paste_syntax_to_window (generate_syntax (&rd)));
355 g_object_unref (xml);
362 generate_syntax (const struct autorecode *rd)
367 GString *string = g_string_new ("AUTORECODE");
369 g_string_append (string, "\n\tVARIABLES =");
371 g_hash_table_iter_init (&iter, rd->varmap);
372 while (g_hash_table_iter_next (&iter, &key, &value))
374 struct variable *var = key;
375 g_string_append (string, " ");
376 g_string_append (string, var_get_name (var));
379 g_string_append (string, " INTO");
381 g_hash_table_iter_init (&iter, rd->varmap);
382 while (g_hash_table_iter_next (&iter, &key, &value))
384 struct nlp *nlp = value;
385 g_string_append (string, " ");
386 g_string_append (string, nlp->name);
389 if ( ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->ascending)))
390 g_string_append (string, "\n\t/DESCENDING");
392 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->group)))
393 g_string_append (string, "\n\t/GROUP");
395 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->blank)))
396 g_string_append (string, "\n\t/BLANK");
398 g_string_append (string, ".\n");
402 g_string_free (string, FALSE);