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 = psppire_var_view_get_current_model (PSPPIRE_VAR_VIEW (rd->var_view));
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))
190 gtk_widget_set_sensitive (rd->change_button, valid);
193 /* Callback which gets called when a new row is selected
194 in the variable treeview.
195 It sets the name and label entry widgets to reflect the
196 currently selected row.
199 on_selection_change (GtkTreeSelection *selection, gpointer data)
201 struct autorecode *rd = data;
202 GtkTreeModel *model = psppire_var_view_get_current_model (PSPPIRE_VAR_VIEW (rd->var_view));
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);
219 g_return_if_fail (ok);
221 gtk_tree_model_get (model, &iter, 0, &var, -1);
223 nlp = g_hash_table_lookup (rd->varmap, var);
226 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), nlp->name ? nlp->name : "");
228 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
232 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
233 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
234 gtk_widget_set_sensitive (rd->change_button, FALSE);
237 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
242 render_new_var_name (GtkTreeViewColumn *tree_column,
243 GtkCellRenderer *cell,
244 GtkTreeModel *tree_model,
248 struct nlp *nlp = NULL;
249 struct autorecode *rd = data;
251 struct variable *var = NULL;
253 gtk_tree_model_get (tree_model, iter,
257 nlp = g_hash_table_lookup (rd->varmap, var);
260 g_object_set (cell, "text", nlp->name, NULL);
262 g_object_set (cell, "text", "", NULL);
267 /* Pops up the Autorecode dialog box */
269 autorecode_dialog (PsppireDataWindow *de)
271 struct autorecode rd;
274 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);
328 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
330 g_object_get (de->data_editor, "dictionary", &rd.dict, NULL);
331 g_object_set (source, "model", rd.dict, NULL);
334 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &rd);
336 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
337 dialog_state_valid, &rd);
339 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
344 case GTK_RESPONSE_OK:
345 g_free (execute_syntax_string (de, generate_syntax (&rd)));
347 case PSPPIRE_RESPONSE_PASTE:
348 g_free (paste_syntax_to_window (generate_syntax (&rd)));
354 g_object_unref (xml);
361 generate_syntax (const struct autorecode *rd)
366 GString *string = g_string_new ("AUTORECODE");
368 g_string_append (string, "\n\tVARIABLES =");
370 g_hash_table_iter_init (&iter, rd->varmap);
371 while (g_hash_table_iter_next (&iter, &key, &value))
373 struct variable *var = key;
374 g_string_append (string, " ");
375 g_string_append (string, var_get_name (var));
378 g_string_append (string, " INTO");
380 g_hash_table_iter_init (&iter, rd->varmap);
381 while (g_hash_table_iter_next (&iter, &key, &value))
383 struct nlp *nlp = value;
384 g_string_append (string, " ");
385 g_string_append (string, nlp->name);
388 if ( ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->ascending)))
389 g_string_append (string, "\n\t/DESCENDING");
391 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->group)))
392 g_string_append (string, "\n\t/GROUP");
394 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->blank)))
395 g_string_append (string, "\n\t/BLANK");
397 g_string_append (string, ".\n");
401 g_string_free (string, FALSE);