1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011 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"
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>
29 #include <language/expressions/public.h>
32 static void function_list_populate (GtkTreeView *tv);
34 static void insert_function_into_syntax_area (GtkTreeIter iter,
40 static void insert_source_row_into_text_view (GtkTreeIter iter,
50 GtkBuilder *xml; /* The xml that generated the widgets */
57 on_target_change (GObject *obj, struct compute_dialog *cd)
59 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
60 GtkWidget *type_and_label = get_widget_assert (cd->xml, "compute-button1");
62 const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (target));
63 gboolean valid = var_name && strcmp ("", var_name);
65 gtk_widget_set_sensitive (type_and_label, valid);
69 refresh (GObject *obj, const struct compute_dialog *cd)
71 GtkTextIter start, end;
72 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
73 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
74 GtkWidget *varlist = get_widget_assert (cd->xml, "compute-treeview1");
75 GtkWidget *funclist = get_widget_assert (cd->xml, "compute-treeview2");
77 GtkTextBuffer *buffer =
78 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
80 GtkTreeSelection *selection;
82 /* Clear the target variable entry box */
83 gtk_entry_set_text (GTK_ENTRY (target), "");
84 g_signal_emit_by_name (target, "changed");
86 /* Clear the syntax area textbuffer */
87 gtk_text_buffer_get_start_iter (buffer, &start);
88 gtk_text_buffer_get_end_iter (buffer, &end);
89 gtk_text_buffer_delete (buffer, &start, &end);
91 /* Unselect all items in the treeview */
92 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (varlist));
93 gtk_tree_selection_unselect_all (selection);
95 /* And the other one */
96 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (funclist));
97 gtk_tree_selection_unselect_all (selection);
102 erase_selection (GtkTextBuffer *buffer)
104 GtkTextIter start, end;
106 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
108 gtk_text_buffer_delete (buffer, &start, &end);
114 on_keypad_button (PsppireKeypad *kp, const gchar *syntax, gpointer data)
116 GtkBuilder *xml = data;
118 GtkWidget *rhs = get_widget_assert (xml, "compute-textview1");
120 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (rhs));
122 erase_selection (buffer);
124 gtk_text_buffer_insert_at_cursor (buffer, syntax, strlen (syntax));
126 if (0 == strcmp (syntax, "()"))
129 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
130 gtk_text_buffer_get_iter_at_mark (buffer, &iter, cursor);
131 gtk_text_iter_backward_cursor_position (&iter);
132 gtk_text_buffer_move_mark (buffer, cursor, &iter);
138 erase (PsppireKeypad *kp, gpointer data)
140 GtkBuilder *xml = data;
142 GtkWidget *rhs = get_widget_assert (xml, "compute-textview1");
144 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (rhs));
146 erase_selection (buffer);
150 generate_syntax (const struct compute_dialog *cd)
154 const gchar *target_name ;
157 GtkTextIter start, end;
158 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
159 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
160 GtkWidget *string_toggle = get_widget_assert (cd->xml, "radio-button-string");
161 GtkWidget *user_label_toggle =
162 get_widget_assert (cd->xml, "radio-button-user-label");
163 GtkWidget *width_entry = get_widget_assert (cd->xml, "type-and-label-width");
164 GtkWidget *label_entry = get_widget_assert (cd->xml,
165 "type-and-label-label-entry");
168 GtkTextBuffer *buffer =
169 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
171 gtk_text_buffer_get_start_iter (buffer, &start);
172 gtk_text_buffer_get_end_iter (buffer, &end);
174 target_name = gtk_entry_get_text (GTK_ENTRY (target));
176 expression = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
178 string = g_string_sized_new (64);
180 if ( cd-> use_type &&
181 NULL == psppire_dict_lookup_var (cd->dict, target_name ))
183 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (string_toggle)))
185 const char *w = gtk_entry_get_text (GTK_ENTRY(width_entry));
186 g_string_append_printf (string,
187 "STRING %s (a%s).\n", target_name, w);
190 g_string_append_printf (string, "NUMERIC %s.\n", target_name);
193 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (user_label_toggle)))
194 label = gtk_entry_get_text (GTK_ENTRY (label_entry));
198 if ( strlen (label) > 0 )
199 g_string_append_printf (string, "VARIABLE LABEL %s '%s'.\n",
203 g_string_append_printf (string, "COMPUTE %s = %s.\n",
208 g_string_append (string, "EXECUTE.\n");
215 g_string_free (string, FALSE);
221 reset_type_label_dialog (struct compute_dialog *cd)
223 const gchar *target_name;
224 struct variable *target_var;
226 GtkWidget *width_entry =
227 get_widget_assert (cd->xml, "type-and-label-width");
229 GtkWidget *label_entry =
230 get_widget_assert (cd->xml, "type-and-label-label-entry");
232 GtkWidget *numeric_target =
233 get_widget_assert (cd->xml, "radio-button-numeric");
235 GtkWidget *string_target =
236 get_widget_assert (cd->xml, "radio-button-string");
239 target_name = gtk_entry_get_text
240 (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
243 if ( (target_var = psppire_dict_lookup_var (cd->dict, target_name)) )
245 /* Existing Variable */
247 GtkWidget *user_label =
248 get_widget_assert (cd->xml, "radio-button-user-label");
250 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (user_label), TRUE);
252 label = var_get_label (target_var);
256 gtk_entry_set_text (GTK_ENTRY (label_entry), label);
259 gtk_widget_set_sensitive (width_entry, FALSE);
261 if ( var_is_numeric (target_var))
262 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
265 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (string_target),
268 gtk_widget_set_sensitive (numeric_target, FALSE);
269 gtk_widget_set_sensitive (string_target, FALSE);
273 GtkWidget *expression =
274 get_widget_assert (cd->xml, "radio-button-expression-label");
276 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (expression), TRUE);
278 gtk_widget_set_sensitive (width_entry, TRUE);
279 gtk_widget_set_sensitive (numeric_target, TRUE);
280 gtk_widget_set_sensitive (string_target, TRUE);
282 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
289 run_type_label_dialog (GtkButton *b, gpointer data)
291 struct compute_dialog *cd = data;
294 GtkWidget *subdialog = get_widget_assert (cd->xml, "type-and-label-dialog");
295 GtkWidget *dialog = get_widget_assert (cd->xml, "compute-variable-dialog");
297 gtk_window_set_transient_for (GTK_WINDOW (subdialog), GTK_WINDOW (dialog));
299 reset_type_label_dialog (cd);
300 response = psppire_dialog_run (PSPPIRE_DIALOG (subdialog));
301 if ( response == PSPPIRE_RESPONSE_CONTINUE)
307 on_expression_toggle (GtkToggleButton *button, gpointer data)
309 struct compute_dialog *cd = data;
312 get_widget_assert (cd->xml, "type-and-label-label-entry");
314 if ( gtk_toggle_button_get_active (button))
316 gtk_entry_set_text (GTK_ENTRY (entry), "");
317 gtk_widget_set_sensitive (entry, FALSE);
322 struct variable *target_var;
323 const gchar *target_name = gtk_entry_get_text
324 (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
326 target_var = psppire_dict_lookup_var (cd->dict, target_name);
329 label = var_get_label (target_var);
332 gtk_entry_set_text (GTK_ENTRY (entry), label);
335 gtk_entry_set_text (GTK_ENTRY (entry), "");
337 gtk_widget_set_sensitive (entry, TRUE);
342 /* Return TRUE if the dialog box's widgets' state are such that clicking OK
343 might not result in erroneous syntax being generated */
345 contents_plausible (gpointer data)
347 struct compute_dialog *cd = data;
349 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
350 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
351 GtkTextBuffer *buffer =
352 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
354 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (target))))
357 if ( gtk_text_buffer_get_char_count (buffer) == 0 )
363 /* Pops up the Compute dialog box */
365 compute_dialog (PsppireDataWindow *de)
369 PsppireVarStore *vs = NULL;
370 struct compute_dialog scd;
372 GtkBuilder *xml = builder_new ("compute.ui");
374 GtkWidget *dialog = get_widget_assert (xml, "compute-variable-dialog");
376 GtkWidget *dict_view = get_widget_assert (xml, "compute-treeview1");
377 GtkWidget *functions = get_widget_assert (xml, "compute-treeview2");
378 GtkWidget *keypad = get_widget_assert (xml, "psppire-keypad1");
379 GtkWidget *target = get_widget_assert (xml, "compute-entry1");
380 GtkWidget *var_selector = get_widget_assert (xml, "compute-selector1");
381 GtkWidget *func_selector = get_widget_assert (xml, "compute-selector2");
382 GtkWidget *type_and_label = get_widget_assert (xml, "compute-button1");
384 GtkWidget *expression =
385 get_widget_assert (xml, "radio-button-expression-label");
388 g_object_get (de->data_editor, "var-store", &vs, NULL);
389 g_object_get (vs, "dictionary", &scd.dict, NULL);
390 scd.use_type = FALSE;
392 g_signal_connect (expression, "toggled",
393 G_CALLBACK(on_expression_toggle), &scd);
395 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
398 g_object_set (dict_view, "model", scd.dict,
399 "selection-mode", GTK_SELECTION_SINGLE,
402 psppire_selector_set_select_func (PSPPIRE_SELECTOR (var_selector),
403 insert_source_row_into_text_view, NULL);
405 function_list_populate (GTK_TREE_VIEW (functions));
407 psppire_selector_set_select_func (PSPPIRE_SELECTOR (func_selector),
408 insert_function_into_syntax_area, NULL);
412 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
413 contents_plausible, &scd);
415 g_signal_connect (target, "changed", G_CALLBACK (on_target_change), &scd);
417 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &scd);
419 g_signal_connect (keypad, "insert-syntax",
420 G_CALLBACK (on_keypad_button), xml);
422 g_signal_connect (keypad, "erase",
423 G_CALLBACK (erase), xml);
426 g_signal_connect (type_and_label, "clicked",
427 G_CALLBACK (run_type_label_dialog), &scd);
431 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
436 case GTK_RESPONSE_OK:
437 g_free (execute_syntax_string (de, generate_syntax (&scd)));
439 case PSPPIRE_RESPONSE_PASTE:
440 g_free (paste_syntax_to_window (generate_syntax (&scd)));
446 g_object_unref (xml);
458 function_list_populate (GtkTreeView *tv)
460 GtkListStore *liststore;
464 const gint n_funcs = expr_get_function_cnt ();
466 liststore = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
468 for (i = 0 ; i < n_funcs ; ++i)
470 const struct operation *op = expr_get_function (i);
472 gtk_list_store_append (liststore, &iter);
474 gtk_list_store_set (liststore, &iter,
475 COMPUTE_COL_NAME, expr_operation_get_name (op),
476 COMPUTE_COL_USAGE, expr_operation_get_prototype (op),
477 COMPUTE_COL_ARITY, expr_operation_get_arg_cnt (op),
483 /* Set the cell rendering */
486 GtkTreeViewColumn *col;
487 GtkCellRenderer *renderer;
490 col = gtk_tree_view_column_new ();
492 gtk_tree_view_append_column (tv, col);
494 renderer = gtk_cell_renderer_text_new ();
496 gtk_tree_view_column_pack_start (col, renderer, TRUE);
498 gtk_tree_view_column_add_attribute (col, renderer, "text", COMPUTE_COL_USAGE);
501 gtk_tree_view_set_model (tv, GTK_TREE_MODEL (liststore));
508 insert_function_into_syntax_area (GtkTreeIter iter,
509 GtkWidget *text_view,
515 GValue name_value = {0};
516 GValue arity_value = {0};
520 GtkTextBuffer *buffer ;
522 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
524 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
526 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_NAME, &name_value);
527 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_ARITY, &arity_value);
529 arity = g_value_get_int (&arity_value);
531 string = g_string_new (g_value_get_string (&name_value));
533 g_string_append (string, "(");
534 for ( i = 0 ; i < arity -1 ; ++i )
536 g_string_append (string, "?,");
538 g_string_append (string, "?)");
541 erase_selection (buffer);
543 gtk_text_buffer_insert_at_cursor (buffer, string->str, string->len);
545 g_value_unset (&name_value);
546 g_value_unset (&arity_value);
547 g_string_free (string, TRUE);
549 /* Now position the cursor over the first '?' */
552 GtkTextIter selectbound;
553 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
554 gtk_text_buffer_get_iter_at_mark (buffer, &insert, cursor);
555 for ( i = 0 ; i < arity ; ++i )
557 gtk_text_iter_backward_cursor_position (&insert);
558 gtk_text_iter_backward_cursor_position (&insert);
560 selectbound = insert;
561 gtk_text_iter_forward_cursor_position (&selectbound);
563 gtk_text_buffer_select_range (buffer, &insert, &selectbound);
568 /* Inserts the name of the selected variable into the destination widget.
569 The destination widget must be a GtkTextView
572 insert_source_row_into_text_view (GtkTreeIter iter,
581 struct variable *var;
582 GtkTreeIter dict_iter;
583 GtkTextBuffer *buffer;
585 g_return_if_fail (GTK_IS_TEXT_VIEW (dest));
587 if ( GTK_IS_TREE_MODEL_FILTER (model))
589 dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
590 (GTK_TREE_MODEL_FILTER(model)));
592 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
598 dict = PSPPIRE_DICT (model);
602 path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
604 idx = gtk_tree_path_get_indices (path);
606 var = psppire_dict_get_variable (dict, *idx);
608 gtk_tree_path_free (path);
610 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dest));
612 erase_selection (buffer);
614 gtk_text_buffer_insert_at_cursor (buffer, var_get_name (var), -1);