1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 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 "compute-dialog.h"
20 #include "builder-wrapper.h"
21 #include "psppire-dialog.h"
22 #include "psppire-keypad.h"
23 #include "psppire-data-window.h"
24 #include "psppire-var-store.h"
25 #include "psppire-selector.h"
26 #include "dialog-common.h"
27 #include <libpspp/i18n.h>
30 #include <language/expressions/public.h>
35 static void function_list_populate (GtkTreeView *tv);
37 static void insert_function_into_syntax_area (GtkTreeIter iter,
43 static void insert_source_row_into_text_view (GtkTreeIter iter,
53 GtkBuilder *xml; /* The xml that generated the widgets */
60 on_target_change (GObject *obj, struct compute_dialog *cd)
62 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
63 GtkWidget *type_and_label = get_widget_assert (cd->xml, "compute-button1");
65 const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (target));
66 gboolean valid = var_name && strcmp ("", var_name);
68 gtk_widget_set_sensitive (type_and_label, valid);
72 refresh (GObject *obj, const struct compute_dialog *cd)
74 GtkTextIter start, end;
75 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
76 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
77 GtkWidget *varlist = get_widget_assert (cd->xml, "compute-treeview1");
78 GtkWidget *funclist = get_widget_assert (cd->xml, "compute-treeview2");
80 GtkTextBuffer *buffer =
81 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
83 GtkTreeSelection *selection;
85 /* Clear the target variable entry box */
86 gtk_entry_set_text (GTK_ENTRY (target), "");
87 g_signal_emit_by_name (target, "changed");
89 /* Clear the syntax area textbuffer */
90 gtk_text_buffer_get_start_iter (buffer, &start);
91 gtk_text_buffer_get_end_iter (buffer, &end);
92 gtk_text_buffer_delete (buffer, &start, &end);
94 /* Unselect all items in the treeview */
95 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (varlist));
96 gtk_tree_selection_unselect_all (selection);
98 /* And the other one */
99 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (funclist));
100 gtk_tree_selection_unselect_all (selection);
105 erase_selection (GtkTextBuffer *buffer)
107 GtkTextIter start, end;
109 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
111 gtk_text_buffer_delete (buffer, &start, &end);
117 on_keypad_button (PsppireKeypad *kp, const gchar *syntax, gpointer data)
119 GtkBuilder *xml = data;
121 GtkWidget *rhs = get_widget_assert (xml, "compute-textview1");
123 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (rhs));
125 erase_selection (buffer);
127 gtk_text_buffer_insert_at_cursor (buffer, syntax, strlen (syntax));
129 if (0 == strcmp (syntax, "()"))
132 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
133 gtk_text_buffer_get_iter_at_mark (buffer, &iter, cursor);
134 gtk_text_iter_backward_cursor_position (&iter);
135 gtk_text_buffer_move_mark (buffer, cursor, &iter);
141 erase (PsppireKeypad *kp, gpointer data)
143 GtkBuilder *xml = data;
145 GtkWidget *rhs = get_widget_assert (xml, "compute-textview1");
147 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (rhs));
149 erase_selection (buffer);
153 generate_syntax (const struct compute_dialog *cd)
157 const gchar *target_name ;
160 GtkTextIter start, end;
161 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
162 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
163 GtkWidget *string_toggle = get_widget_assert (cd->xml, "radio-button-string");
164 GtkWidget *user_label_toggle =
165 get_widget_assert (cd->xml, "radio-button-user-label");
166 GtkWidget *width_entry = get_widget_assert (cd->xml, "type-and-label-width");
167 GtkWidget *label_entry = get_widget_assert (cd->xml,
168 "type-and-label-label-entry");
171 GtkTextBuffer *buffer =
172 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
174 gtk_text_buffer_get_start_iter (buffer, &start);
175 gtk_text_buffer_get_end_iter (buffer, &end);
177 target_name = gtk_entry_get_text (GTK_ENTRY (target));
179 expression = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
181 string = g_string_sized_new (64);
183 if ( cd-> use_type &&
184 NULL == psppire_dict_lookup_var (cd->dict, target_name ))
186 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (string_toggle)))
188 const char *w = gtk_entry_get_text (GTK_ENTRY(width_entry));
189 g_string_append_printf (string,
190 "STRING %s (a%s).\n", target_name, w);
193 g_string_append_printf (string, "NUMERIC %s.\n", target_name);
196 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (user_label_toggle)))
197 label = gtk_entry_get_text (GTK_ENTRY (label_entry));
201 if ( strlen (label) > 0 )
202 g_string_append_printf (string, "VARIABLE LABEL %s '%s'.\n",
206 g_string_append_printf (string, "COMPUTE %s = %s.\n",
211 g_string_append (string, "EXECUTE.\n");
218 g_string_free (string, FALSE);
224 reset_type_label_dialog (struct compute_dialog *cd)
226 const gchar *target_name;
227 struct variable *target_var;
229 GtkWidget *width_entry =
230 get_widget_assert (cd->xml, "type-and-label-width");
232 GtkWidget *label_entry =
233 get_widget_assert (cd->xml, "type-and-label-label-entry");
235 GtkWidget *numeric_target =
236 get_widget_assert (cd->xml, "radio-button-numeric");
238 GtkWidget *string_target =
239 get_widget_assert (cd->xml, "radio-button-string");
242 target_name = gtk_entry_get_text
243 (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
246 if ( (target_var = psppire_dict_lookup_var (cd->dict, target_name)) )
248 /* Existing Variable */
250 GtkWidget *user_label =
251 get_widget_assert (cd->xml, "radio-button-user-label");
253 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (user_label), TRUE);
255 label = var_get_label (target_var);
259 gtk_entry_set_text (GTK_ENTRY (label_entry), label);
262 gtk_widget_set_sensitive (width_entry, FALSE);
264 if ( var_is_numeric (target_var))
265 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
268 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (string_target),
271 gtk_widget_set_sensitive (numeric_target, FALSE);
272 gtk_widget_set_sensitive (string_target, FALSE);
276 GtkWidget *expression =
277 get_widget_assert (cd->xml, "radio-button-expression-label");
279 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (expression), TRUE);
281 gtk_widget_set_sensitive (width_entry, TRUE);
282 gtk_widget_set_sensitive (numeric_target, TRUE);
283 gtk_widget_set_sensitive (string_target, TRUE);
285 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
292 run_type_label_dialog (GtkButton *b, gpointer data)
294 struct compute_dialog *cd = data;
297 GtkWidget *subdialog = get_widget_assert (cd->xml, "type-and-label-dialog");
298 GtkWidget *dialog = get_widget_assert (cd->xml, "compute-variable-dialog");
300 gtk_window_set_transient_for (GTK_WINDOW (subdialog), GTK_WINDOW (dialog));
302 reset_type_label_dialog (cd);
303 response = psppire_dialog_run (PSPPIRE_DIALOG (subdialog));
304 if ( response == PSPPIRE_RESPONSE_CONTINUE)
310 on_expression_toggle (GtkToggleButton *button, gpointer data)
312 struct compute_dialog *cd = data;
315 get_widget_assert (cd->xml, "type-and-label-label-entry");
317 if ( gtk_toggle_button_get_active (button))
319 gtk_entry_set_text (GTK_ENTRY (entry), "");
320 gtk_widget_set_sensitive (entry, FALSE);
325 struct variable *target_var;
326 const gchar *target_name = gtk_entry_get_text
327 (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
329 target_var = psppire_dict_lookup_var (cd->dict, target_name);
332 label = var_get_label (target_var);
335 gtk_entry_set_text (GTK_ENTRY (entry), label);
338 gtk_entry_set_text (GTK_ENTRY (entry), "");
340 gtk_widget_set_sensitive (entry, TRUE);
345 /* Return TRUE if the dialog box's widgets' state are such that clicking OK
346 might not result in erroneous syntax being generated */
348 contents_plausible (gpointer data)
350 struct compute_dialog *cd = data;
352 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
353 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
354 GtkTextBuffer *buffer =
355 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
357 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (target))))
360 if ( gtk_text_buffer_get_char_count (buffer) == 0 )
366 /* Pops up the Compute dialog box */
368 compute_dialog (PsppireDataWindow *de)
372 PsppireVarStore *vs = NULL;
373 struct compute_dialog scd;
375 GtkBuilder *xml = builder_new ("compute.ui");
377 GtkWidget *dialog = get_widget_assert (xml, "compute-variable-dialog");
379 GtkWidget *dict_view = get_widget_assert (xml, "compute-treeview1");
380 GtkWidget *functions = get_widget_assert (xml, "compute-treeview2");
381 GtkWidget *keypad = get_widget_assert (xml, "psppire-keypad1");
382 GtkWidget *target = get_widget_assert (xml, "compute-entry1");
383 GtkWidget *var_selector = get_widget_assert (xml, "compute-selector1");
384 GtkWidget *func_selector = get_widget_assert (xml, "compute-selector2");
385 GtkWidget *type_and_label = get_widget_assert (xml, "compute-button1");
387 GtkWidget *expression =
388 get_widget_assert (xml, "radio-button-expression-label");
391 g_object_get (de->data_editor, "var-store", &vs, NULL);
392 g_object_get (vs, "dictionary", &scd.dict, NULL);
393 scd.use_type = FALSE;
395 g_signal_connect (expression, "toggled",
396 G_CALLBACK(on_expression_toggle), &scd);
398 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
401 g_object_set (dict_view, "model", scd.dict,
402 "selection-mode", GTK_SELECTION_SINGLE,
405 psppire_selector_set_select_func (PSPPIRE_SELECTOR (var_selector),
406 insert_source_row_into_text_view, NULL);
408 function_list_populate (GTK_TREE_VIEW (functions));
410 psppire_selector_set_select_func (PSPPIRE_SELECTOR (func_selector),
411 insert_function_into_syntax_area, NULL);
415 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
416 contents_plausible, &scd);
418 g_signal_connect (target, "changed", G_CALLBACK (on_target_change), &scd);
420 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &scd);
422 g_signal_connect (keypad, "insert-syntax",
423 G_CALLBACK (on_keypad_button), xml);
425 g_signal_connect (keypad, "erase",
426 G_CALLBACK (erase), xml);
429 g_signal_connect (type_and_label, "clicked",
430 G_CALLBACK (run_type_label_dialog), &scd);
434 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
439 case GTK_RESPONSE_OK:
440 g_free (execute_syntax_string (de, generate_syntax (&scd)));
442 case PSPPIRE_RESPONSE_PASTE:
443 g_free (paste_syntax_to_window (generate_syntax (&scd)));
449 g_object_unref (xml);
461 function_list_populate (GtkTreeView *tv)
463 GtkListStore *liststore;
467 const gint n_funcs = expr_get_function_cnt ();
469 liststore = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
471 for (i = 0 ; i < n_funcs ; ++i)
473 const struct operation *op = expr_get_function (i);
475 gtk_list_store_append (liststore, &iter);
477 gtk_list_store_set (liststore, &iter,
478 COMPUTE_COL_NAME, expr_operation_get_name (op),
479 COMPUTE_COL_USAGE, expr_operation_get_prototype (op),
480 COMPUTE_COL_ARITY, expr_operation_get_arg_cnt (op),
486 /* Set the cell rendering */
489 GtkTreeViewColumn *col;
490 GtkCellRenderer *renderer;
493 col = gtk_tree_view_column_new ();
495 gtk_tree_view_append_column (tv, col);
497 renderer = gtk_cell_renderer_text_new ();
499 gtk_tree_view_column_pack_start (col, renderer, TRUE);
501 gtk_tree_view_column_add_attribute (col, renderer, "text", COMPUTE_COL_USAGE);
504 gtk_tree_view_set_model (tv, GTK_TREE_MODEL (liststore));
505 g_object_unref (liststore);
512 insert_function_into_syntax_area (GtkTreeIter iter,
513 GtkWidget *text_view,
519 GValue name_value = {0};
520 GValue arity_value = {0};
524 GtkTextBuffer *buffer ;
526 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
528 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
530 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_NAME, &name_value);
531 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_ARITY, &arity_value);
533 arity = g_value_get_int (&arity_value);
535 string = g_string_new (g_value_get_string (&name_value));
537 g_string_append (string, "(");
538 for ( i = 0 ; i < arity -1 ; ++i )
540 g_string_append (string, "?,");
542 g_string_append (string, "?)");
545 erase_selection (buffer);
547 gtk_text_buffer_insert_at_cursor (buffer, string->str, string->len);
549 g_value_unset (&name_value);
550 g_value_unset (&arity_value);
551 g_string_free (string, TRUE);
553 /* Now position the cursor over the first '?' */
556 GtkTextIter selectbound;
557 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
558 gtk_text_buffer_get_iter_at_mark (buffer, &insert, cursor);
559 for ( i = 0 ; i < arity ; ++i )
561 gtk_text_iter_backward_cursor_position (&insert);
562 gtk_text_iter_backward_cursor_position (&insert);
564 selectbound = insert;
565 gtk_text_iter_forward_cursor_position (&selectbound);
567 gtk_text_buffer_select_range (buffer, &insert, &selectbound);
572 /* Inserts the name of the selected variable into the destination widget.
573 The destination widget must be a GtkTextView
576 insert_source_row_into_text_view (GtkTreeIter iter,
585 struct variable *var;
586 GtkTreeIter dict_iter;
587 GtkTextBuffer *buffer;
589 g_return_if_fail (GTK_IS_TEXT_VIEW (dest));
591 if ( GTK_IS_TREE_MODEL_FILTER (model))
593 dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
594 (GTK_TREE_MODEL_FILTER(model)));
596 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
602 dict = PSPPIRE_DICT (model);
606 path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
608 idx = gtk_tree_path_get_indices (path);
610 var = psppire_dict_get_variable (dict, *idx);
612 gtk_tree_path_free (path);
614 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dest));
616 erase_selection (buffer);
618 gtk_text_buffer_insert_at_cursor (buffer, var_get_name (var), -1);