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 "data-editor.h"
24 #include "psppire-var-store.h"
25 #include "dialog-common.h"
26 #include "dict-display.h"
28 #include <language/expressions/public.h>
29 #include <language/syntax-string-source.h>
30 #include "syntax-editor.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 GladeXML *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 GladeXML *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 GladeXML *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 (GObject *o, gpointer data)
368 struct data_editor *de = data;
370 PsppireVarStore *vs = NULL;
371 struct compute_dialog scd;
373 GladeXML *xml = XML_NEW ("psppire.glade");
375 GtkWidget *dialog = get_widget_assert (xml, "compute-variable-dialog");
377 GtkWidget *dict_view = get_widget_assert (xml, "compute-treeview1");
378 GtkWidget *functions = get_widget_assert (xml, "compute-treeview2");
379 GtkWidget *keypad = get_widget_assert (xml, "psppire-keypad1");
380 GtkWidget *target = get_widget_assert (xml, "compute-entry1");
381 GtkWidget *syntax_area = get_widget_assert (xml, "compute-textview1");
382 GtkWidget *var_selector = get_widget_assert (xml, "compute-selector1");
383 GtkWidget *func_selector = get_widget_assert (xml, "compute-selector2");
384 GtkWidget *type_and_label = get_widget_assert (xml, "compute-button1");
386 GtkWidget *expression =
387 get_widget_assert (xml, "radio-button-expression-label");
390 g_object_get (de->data_editor, "var-store", &vs, NULL);
392 scd.use_type = FALSE;
394 g_signal_connect (expression, "toggled",
395 G_CALLBACK(on_expression_toggle), &scd);
397 gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
400 attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
402 GTK_SELECTION_SINGLE, NULL);
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);
450 struct getl_interface *sss = create_syntax_string_source (syntax);
451 execute_syntax (sss);
456 case PSPPIRE_RESPONSE_PASTE:
458 gchar *syntax = generate_syntax (&scd);
460 struct syntax_editor *se =
461 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
463 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
472 g_object_unref (xml);
484 function_list_populate (GtkTreeView *tv)
486 GtkListStore *liststore;
490 const gint n_funcs = expr_get_function_cnt ();
492 liststore = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
494 for (i = 0 ; i < n_funcs ; ++i)
496 const struct operation *op = expr_get_function (i);
498 gtk_list_store_append (liststore, &iter);
500 gtk_list_store_set (liststore, &iter,
501 COMPUTE_COL_NAME, expr_operation_get_name (op),
502 COMPUTE_COL_USAGE, expr_operation_get_prototype (op),
503 COMPUTE_COL_ARITY, expr_operation_get_arg_cnt (op),
509 /* Set the cell rendering */
512 GtkTreeViewColumn *col;
513 GtkCellRenderer *renderer;
516 col = gtk_tree_view_column_new ();
518 gtk_tree_view_append_column (tv, col);
520 renderer = gtk_cell_renderer_text_new ();
522 gtk_tree_view_column_pack_start (col, renderer, TRUE);
524 gtk_tree_view_column_add_attribute (col, renderer, "text", COMPUTE_COL_USAGE);
527 gtk_tree_view_set_model (tv, GTK_TREE_MODEL (liststore));
534 insert_function_into_syntax_area (GtkTreeIter iter,
535 GtkWidget *text_view,
541 GValue name_value = {0};
542 GValue arity_value = {0};
546 GtkTextBuffer *buffer ;
548 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
550 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
552 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_NAME, &name_value);
553 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_ARITY, &arity_value);
555 arity = g_value_get_int (&arity_value);
557 string = g_string_new (g_value_get_string (&name_value));
559 g_string_append (string, "(");
560 for ( i = 0 ; i < arity -1 ; ++i )
562 g_string_append (string, "?,");
564 g_string_append (string, "?)");
567 erase_selection (buffer);
569 gtk_text_buffer_insert_at_cursor (buffer, string->str, string->len);
571 g_value_unset (&name_value);
572 g_value_unset (&arity_value);
573 g_string_free (string, TRUE);
575 /* Now position the cursor over the first '?' */
578 GtkTextIter selectbound;
579 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
580 gtk_text_buffer_get_iter_at_mark (buffer, &insert, cursor);
581 for ( i = 0 ; i < arity ; ++i )
583 gtk_text_iter_backward_cursor_position (&insert);
584 gtk_text_iter_backward_cursor_position (&insert);
586 selectbound = insert;
587 gtk_text_iter_forward_cursor_position (&selectbound);
589 gtk_text_buffer_select_range (buffer, &insert, &selectbound);
594 /* Inserts the name of the selected variable into the destination widget.
595 The destination widget must be a GtkTextView
598 insert_source_row_into_text_view (GtkTreeIter iter,
607 struct variable *var;
608 GtkTreeIter dict_iter;
610 GtkTextBuffer *buffer;
612 g_return_if_fail (GTK_IS_TEXT_VIEW (dest));
614 if ( GTK_IS_TREE_MODEL_FILTER (model))
616 dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
617 (GTK_TREE_MODEL_FILTER(model)));
619 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
625 dict = PSPPIRE_DICT (model);
629 path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
631 idx = gtk_tree_path_get_indices (path);
633 var = psppire_dict_get_variable (dict, *idx);
635 gtk_tree_path_free (path);
637 name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
639 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dest));
641 erase_selection (buffer);
643 gtk_text_buffer_insert_at_cursor (buffer, name, -1);