1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 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>
30 #include <language/syntax-string-source.h>
33 static void function_list_populate (GtkTreeView *tv);
35 static void insert_function_into_syntax_area (GtkTreeIter iter,
41 static void insert_source_row_into_text_view (GtkTreeIter iter,
51 GtkBuilder *xml; /* The xml that generated the widgets */
58 on_target_change (GObject *obj, struct compute_dialog *cd)
60 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
61 GtkWidget *type_and_label = get_widget_assert (cd->xml, "compute-button1");
63 const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (target));
64 gboolean valid = var_name && strcmp ("", var_name);
66 gtk_widget_set_sensitive (type_and_label, valid);
70 refresh (GObject *obj, const struct compute_dialog *cd)
72 GtkTextIter start, end;
73 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
74 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
75 GtkWidget *varlist = get_widget_assert (cd->xml, "compute-treeview1");
76 GtkWidget *funclist = get_widget_assert (cd->xml, "compute-treeview2");
78 GtkTextBuffer *buffer =
79 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
81 GtkTreeSelection *selection;
83 /* Clear the target variable entry box */
84 gtk_entry_set_text (GTK_ENTRY (target), "");
85 g_signal_emit_by_name (target, "changed");
87 /* Clear the syntax area textbuffer */
88 gtk_text_buffer_get_start_iter (buffer, &start);
89 gtk_text_buffer_get_end_iter (buffer, &end);
90 gtk_text_buffer_delete (buffer, &start, &end);
92 /* Unselect all items in the treeview */
93 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (varlist));
94 gtk_tree_selection_unselect_all (selection);
96 /* And the other one */
97 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (funclist));
98 gtk_tree_selection_unselect_all (selection);
103 erase_selection (GtkTextBuffer *buffer)
105 GtkTextIter start, end;
107 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
109 gtk_text_buffer_delete (buffer, &start, &end);
115 on_keypad_button (PsppireKeypad *kp, const gchar *syntax, gpointer data)
117 GtkBuilder *xml = data;
119 GtkWidget *rhs = get_widget_assert (xml, "compute-textview1");
121 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (rhs));
123 erase_selection (buffer);
125 gtk_text_buffer_insert_at_cursor (buffer, syntax, strlen (syntax));
127 if (0 == strcmp (syntax, "()"))
130 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
131 gtk_text_buffer_get_iter_at_mark (buffer, &iter, cursor);
132 gtk_text_iter_backward_cursor_position (&iter);
133 gtk_text_buffer_move_mark (buffer, cursor, &iter);
139 erase (PsppireKeypad *kp, gpointer data)
141 GtkBuilder *xml = data;
143 GtkWidget *rhs = get_widget_assert (xml, "compute-textview1");
145 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (rhs));
147 erase_selection (buffer);
151 generate_syntax (const struct compute_dialog *cd)
155 const gchar *target_name ;
158 GtkTextIter start, end;
159 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
160 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
161 GtkWidget *string_toggle = get_widget_assert (cd->xml, "radio-button-string");
162 GtkWidget *user_label_toggle =
163 get_widget_assert (cd->xml, "radio-button-user-label");
164 GtkWidget *width_entry = get_widget_assert (cd->xml, "type-and-label-width");
165 GtkWidget *label_entry = get_widget_assert (cd->xml,
166 "type-and-label-label-entry");
169 GtkTextBuffer *buffer =
170 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
172 gtk_text_buffer_get_start_iter (buffer, &start);
173 gtk_text_buffer_get_end_iter (buffer, &end);
175 target_name = gtk_entry_get_text (GTK_ENTRY (target));
177 expression = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
179 string = g_string_sized_new (64);
181 if ( cd-> use_type &&
182 NULL == psppire_dict_lookup_var (cd->dict, target_name ))
184 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (string_toggle)))
186 const char *w = gtk_entry_get_text (GTK_ENTRY(width_entry));
187 g_string_append_printf (string,
188 "STRING %s (a%s).\n", target_name, w);
191 g_string_append_printf (string, "NUMERIC %s.\n", target_name);
194 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (user_label_toggle)))
195 label = gtk_entry_get_text (GTK_ENTRY (label_entry));
199 if ( strlen (label) > 0 )
200 g_string_append_printf (string, "VARIABLE LABEL %s '%s'.\n",
204 g_string_append_printf (string, "COMPUTE %s = %s.\n",
209 g_string_append (string, "EXECUTE.\n");
216 g_string_free (string, FALSE);
222 reset_type_label_dialog (struct compute_dialog *cd)
224 const gchar *target_name;
225 struct variable *target_var;
227 GtkWidget *width_entry =
228 get_widget_assert (cd->xml, "type-and-label-width");
230 GtkWidget *label_entry =
231 get_widget_assert (cd->xml, "type-and-label-label-entry");
233 GtkWidget *numeric_target =
234 get_widget_assert (cd->xml, "radio-button-numeric");
236 GtkWidget *string_target =
237 get_widget_assert (cd->xml, "radio-button-string");
240 target_name = gtk_entry_get_text
241 (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
244 if ( (target_var = psppire_dict_lookup_var (cd->dict, target_name)) )
246 /* Existing Variable */
248 GtkWidget *user_label =
249 get_widget_assert (cd->xml, "radio-button-user-label");
251 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (user_label), TRUE);
253 label = var_get_label (target_var);
257 gtk_entry_set_text (GTK_ENTRY (label_entry), label);
260 gtk_widget_set_sensitive (width_entry, FALSE);
262 if ( var_is_numeric (target_var))
263 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
266 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (string_target),
269 gtk_widget_set_sensitive (numeric_target, FALSE);
270 gtk_widget_set_sensitive (string_target, FALSE);
274 GtkWidget *expression =
275 get_widget_assert (cd->xml, "radio-button-expression-label");
277 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (expression), TRUE);
279 gtk_widget_set_sensitive (width_entry, TRUE);
280 gtk_widget_set_sensitive (numeric_target, TRUE);
281 gtk_widget_set_sensitive (string_target, TRUE);
283 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
290 run_type_label_dialog (GtkButton *b, gpointer data)
292 struct compute_dialog *cd = data;
295 GtkWidget *subdialog = get_widget_assert (cd->xml, "type-and-label-dialog");
296 GtkWidget *dialog = get_widget_assert (cd->xml, "compute-variable-dialog");
298 gtk_window_set_transient_for (GTK_WINDOW (subdialog), GTK_WINDOW (dialog));
300 reset_type_label_dialog (cd);
301 response = psppire_dialog_run (PSPPIRE_DIALOG (subdialog));
302 if ( response == PSPPIRE_RESPONSE_CONTINUE)
308 on_expression_toggle (GtkToggleButton *button, gpointer data)
310 struct compute_dialog *cd = data;
313 get_widget_assert (cd->xml, "type-and-label-label-entry");
315 if ( gtk_toggle_button_get_active (button))
317 gtk_entry_set_text (GTK_ENTRY (entry), "");
318 gtk_widget_set_sensitive (entry, FALSE);
323 struct variable *target_var;
324 const gchar *target_name = gtk_entry_get_text
325 (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
327 target_var = psppire_dict_lookup_var (cd->dict, target_name);
330 label = var_get_label (target_var);
333 gtk_entry_set_text (GTK_ENTRY (entry), label);
336 gtk_entry_set_text (GTK_ENTRY (entry), "");
338 gtk_widget_set_sensitive (entry, TRUE);
343 /* Return TRUE if the dialog box's widgets' state are such that clicking OK
344 might not result in erroneous syntax being generated */
346 contents_plausible (gpointer data)
348 struct compute_dialog *cd = data;
350 GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
351 GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
352 GtkTextBuffer *buffer =
353 gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
355 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (target))))
358 if ( gtk_text_buffer_get_char_count (buffer) == 0 )
364 /* Pops up the Compute dialog box */
366 compute_dialog (GObject *o, gpointer data)
369 PsppireDataWindow *de = data;
371 PsppireVarStore *vs = NULL;
372 struct compute_dialog scd;
374 GtkBuilder *xml = builder_new ("psppire.ui");
376 GtkWidget *dialog = get_widget_assert (xml, "compute-variable-dialog");
378 GtkWidget *dict_view = get_widget_assert (xml, "compute-treeview1");
379 GtkWidget *functions = get_widget_assert (xml, "compute-treeview2");
380 GtkWidget *keypad = get_widget_assert (xml, "psppire-keypad1");
381 GtkWidget *target = get_widget_assert (xml, "compute-entry1");
382 GtkWidget *syntax_area = get_widget_assert (xml, "compute-textview1");
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);
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));
400 g_object_set (dict_view,
401 "dictionary", vs->dict,
402 "selection-mode", GTK_SELECTION_SINGLE,
405 psppire_selector_set_subjects (PSPPIRE_SELECTOR (var_selector),
406 dict_view, syntax_area,
407 insert_source_row_into_text_view,
412 function_list_populate (GTK_TREE_VIEW (functions));
414 psppire_selector_set_subjects (PSPPIRE_SELECTOR (func_selector),
415 functions, syntax_area,
416 insert_function_into_syntax_area,
423 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
424 contents_plausible, &scd);
426 g_signal_connect (target, "changed", G_CALLBACK (on_target_change), &scd);
428 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &scd);
430 g_signal_connect (keypad, "insert-syntax",
431 G_CALLBACK (on_keypad_button), xml);
433 g_signal_connect (keypad, "erase",
434 G_CALLBACK (erase), xml);
437 g_signal_connect (type_and_label, "clicked",
438 G_CALLBACK (run_type_label_dialog), &scd);
442 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
447 case GTK_RESPONSE_OK:
449 gchar *syntax = generate_syntax (&scd);
451 struct getl_interface *sss = create_syntax_string_source (syntax);
452 execute_syntax (sss);
457 case PSPPIRE_RESPONSE_PASTE:
459 gchar *syntax = generate_syntax (&scd);
461 paste_syntax_in_new_window (syntax);
470 g_object_unref (xml);
482 function_list_populate (GtkTreeView *tv)
484 GtkListStore *liststore;
488 const gint n_funcs = expr_get_function_cnt ();
490 liststore = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
492 for (i = 0 ; i < n_funcs ; ++i)
494 const struct operation *op = expr_get_function (i);
496 gtk_list_store_append (liststore, &iter);
498 gtk_list_store_set (liststore, &iter,
499 COMPUTE_COL_NAME, expr_operation_get_name (op),
500 COMPUTE_COL_USAGE, expr_operation_get_prototype (op),
501 COMPUTE_COL_ARITY, expr_operation_get_arg_cnt (op),
507 /* Set the cell rendering */
510 GtkTreeViewColumn *col;
511 GtkCellRenderer *renderer;
514 col = gtk_tree_view_column_new ();
516 gtk_tree_view_append_column (tv, col);
518 renderer = gtk_cell_renderer_text_new ();
520 gtk_tree_view_column_pack_start (col, renderer, TRUE);
522 gtk_tree_view_column_add_attribute (col, renderer, "text", COMPUTE_COL_USAGE);
525 gtk_tree_view_set_model (tv, GTK_TREE_MODEL (liststore));
532 insert_function_into_syntax_area (GtkTreeIter iter,
533 GtkWidget *text_view,
539 GValue name_value = {0};
540 GValue arity_value = {0};
544 GtkTextBuffer *buffer ;
546 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
548 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
550 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_NAME, &name_value);
551 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_ARITY, &arity_value);
553 arity = g_value_get_int (&arity_value);
555 string = g_string_new (g_value_get_string (&name_value));
557 g_string_append (string, "(");
558 for ( i = 0 ; i < arity -1 ; ++i )
560 g_string_append (string, "?,");
562 g_string_append (string, "?)");
565 erase_selection (buffer);
567 gtk_text_buffer_insert_at_cursor (buffer, string->str, string->len);
569 g_value_unset (&name_value);
570 g_value_unset (&arity_value);
571 g_string_free (string, TRUE);
573 /* Now position the cursor over the first '?' */
576 GtkTextIter selectbound;
577 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
578 gtk_text_buffer_get_iter_at_mark (buffer, &insert, cursor);
579 for ( i = 0 ; i < arity ; ++i )
581 gtk_text_iter_backward_cursor_position (&insert);
582 gtk_text_iter_backward_cursor_position (&insert);
584 selectbound = insert;
585 gtk_text_iter_forward_cursor_position (&selectbound);
587 gtk_text_buffer_select_range (buffer, &insert, &selectbound);
592 /* Inserts the name of the selected variable into the destination widget.
593 The destination widget must be a GtkTextView
596 insert_source_row_into_text_view (GtkTreeIter iter,
605 struct variable *var;
606 GtkTreeIter dict_iter;
607 GtkTextBuffer *buffer;
609 g_return_if_fail (GTK_IS_TEXT_VIEW (dest));
611 if ( GTK_IS_TREE_MODEL_FILTER (model))
613 dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
614 (GTK_TREE_MODEL_FILTER(model)));
616 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
622 dict = PSPPIRE_DICT (model);
626 path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
628 idx = gtk_tree_path_get_indices (path);
630 var = psppire_dict_get_variable (dict, *idx);
632 gtk_tree_path_free (path);
634 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dest));
636 erase_selection (buffer);
638 gtk_text_buffer_insert_at_cursor (buffer, var_get_name (var), -1);