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 (PsppireDataWindow *de)
370 PsppireVarStore *vs = NULL;
371 struct compute_dialog scd;
373 GtkBuilder *xml = builder_new ("compute.ui");
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 *var_selector = get_widget_assert (xml, "compute-selector1");
382 GtkWidget *func_selector = get_widget_assert (xml, "compute-selector2");
383 GtkWidget *type_and_label = get_widget_assert (xml, "compute-button1");
385 GtkWidget *expression =
386 get_widget_assert (xml, "radio-button-expression-label");
389 g_object_get (de->data_editor, "var-store", &vs, NULL);
390 g_object_get (vs, "dictionary", &scd.dict, NULL);
391 scd.use_type = FALSE;
393 g_signal_connect (expression, "toggled",
394 G_CALLBACK(on_expression_toggle), &scd);
396 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
399 g_object_set (dict_view, "model", scd.dict,
400 "selection-mode", GTK_SELECTION_SINGLE,
403 psppire_selector_set_select_func (PSPPIRE_SELECTOR (var_selector),
404 insert_source_row_into_text_view, NULL);
406 function_list_populate (GTK_TREE_VIEW (functions));
408 psppire_selector_set_select_func (PSPPIRE_SELECTOR (func_selector),
409 insert_function_into_syntax_area, NULL);
413 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
414 contents_plausible, &scd);
416 g_signal_connect (target, "changed", G_CALLBACK (on_target_change), &scd);
418 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &scd);
420 g_signal_connect (keypad, "insert-syntax",
421 G_CALLBACK (on_keypad_button), xml);
423 g_signal_connect (keypad, "erase",
424 G_CALLBACK (erase), xml);
427 g_signal_connect (type_and_label, "clicked",
428 G_CALLBACK (run_type_label_dialog), &scd);
432 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
437 case GTK_RESPONSE_OK:
439 gchar *syntax = generate_syntax (&scd);
441 struct getl_interface *sss = create_syntax_string_source (syntax);
442 execute_syntax (sss);
447 case PSPPIRE_RESPONSE_PASTE:
449 gchar *syntax = generate_syntax (&scd);
451 paste_syntax_to_window (syntax);
460 g_object_unref (xml);
472 function_list_populate (GtkTreeView *tv)
474 GtkListStore *liststore;
478 const gint n_funcs = expr_get_function_cnt ();
480 liststore = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
482 for (i = 0 ; i < n_funcs ; ++i)
484 const struct operation *op = expr_get_function (i);
486 gtk_list_store_append (liststore, &iter);
488 gtk_list_store_set (liststore, &iter,
489 COMPUTE_COL_NAME, expr_operation_get_name (op),
490 COMPUTE_COL_USAGE, expr_operation_get_prototype (op),
491 COMPUTE_COL_ARITY, expr_operation_get_arg_cnt (op),
497 /* Set the cell rendering */
500 GtkTreeViewColumn *col;
501 GtkCellRenderer *renderer;
504 col = gtk_tree_view_column_new ();
506 gtk_tree_view_append_column (tv, col);
508 renderer = gtk_cell_renderer_text_new ();
510 gtk_tree_view_column_pack_start (col, renderer, TRUE);
512 gtk_tree_view_column_add_attribute (col, renderer, "text", COMPUTE_COL_USAGE);
515 gtk_tree_view_set_model (tv, GTK_TREE_MODEL (liststore));
522 insert_function_into_syntax_area (GtkTreeIter iter,
523 GtkWidget *text_view,
529 GValue name_value = {0};
530 GValue arity_value = {0};
534 GtkTextBuffer *buffer ;
536 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
538 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
540 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_NAME, &name_value);
541 gtk_tree_model_get_value (model, &iter, COMPUTE_COL_ARITY, &arity_value);
543 arity = g_value_get_int (&arity_value);
545 string = g_string_new (g_value_get_string (&name_value));
547 g_string_append (string, "(");
548 for ( i = 0 ; i < arity -1 ; ++i )
550 g_string_append (string, "?,");
552 g_string_append (string, "?)");
555 erase_selection (buffer);
557 gtk_text_buffer_insert_at_cursor (buffer, string->str, string->len);
559 g_value_unset (&name_value);
560 g_value_unset (&arity_value);
561 g_string_free (string, TRUE);
563 /* Now position the cursor over the first '?' */
566 GtkTextIter selectbound;
567 GtkTextMark *cursor = gtk_text_buffer_get_insert (buffer);
568 gtk_text_buffer_get_iter_at_mark (buffer, &insert, cursor);
569 for ( i = 0 ; i < arity ; ++i )
571 gtk_text_iter_backward_cursor_position (&insert);
572 gtk_text_iter_backward_cursor_position (&insert);
574 selectbound = insert;
575 gtk_text_iter_forward_cursor_position (&selectbound);
577 gtk_text_buffer_select_range (buffer, &insert, &selectbound);
582 /* Inserts the name of the selected variable into the destination widget.
583 The destination widget must be a GtkTextView
586 insert_source_row_into_text_view (GtkTreeIter iter,
595 struct variable *var;
596 GtkTreeIter dict_iter;
597 GtkTextBuffer *buffer;
599 g_return_if_fail (GTK_IS_TEXT_VIEW (dest));
601 if ( GTK_IS_TREE_MODEL_FILTER (model))
603 dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
604 (GTK_TREE_MODEL_FILTER(model)));
606 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
612 dict = PSPPIRE_DICT (model);
616 path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
618 idx = gtk_tree_path_get_indices (path);
620 var = psppire_dict_get_variable (dict, *idx);
622 gtk_tree_path_free (path);
624 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dest));
626 erase_selection (buffer);
628 gtk_text_buffer_insert_at_cursor (buffer, var_get_name (var), -1);