2e715039a9979783ea44426dcfe62b0dddc59d7e
[pspp-builds.git] / src / ui / gui / correlation-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009, 2010, 2011  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "dialog-common.h"
20 #include <ui/syntax-gen.h>
21 #include <libpspp/str.h>
22
23 #include "correlation-dialog.h"
24 #include "psppire-selector.h"
25 #include "psppire-dictview.h"
26 #include "psppire-dialog.h"
27
28 #include "psppire-data-window.h"
29 #include "psppire-var-view.h"
30
31 #include "executor.h"
32 #include "helper.h"
33
34 #include <gtk/gtk.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
39
40
41 struct correlation
42 {
43   PsppireDict *dict;
44
45   GtkWidget *variables ;
46
47   GtkWidget *significant;
48   GtkWidget *two_tailed;
49 };
50
51
52 static char * generate_syntax (const struct correlation *rd);
53
54
55 static void
56 refresh (struct correlation *rd)
57 {
58   GtkTreeModel *liststore =
59     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
60   gtk_list_store_clear (GTK_LIST_STORE (liststore));
61
62   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->significant), FALSE);
63
64   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->two_tailed), TRUE);
65 }
66
67
68 static gboolean
69 dialog_state_valid (gpointer data)
70 {
71   struct correlation *corr = data;
72
73   GtkTreeModel *liststore =
74     gtk_tree_view_get_model (GTK_TREE_VIEW (corr->variables));
75
76   if  (gtk_tree_model_iter_n_children (liststore, NULL) > 1)
77     return TRUE;
78
79   return FALSE;
80 }
81
82
83 /* Pops up the Correlation dialog box */
84 void
85 correlation_dialog (PsppireDataWindow *de)
86 {
87   struct correlation rd;
88   gint response;
89
90   GtkBuilder *xml = builder_new ("correlation.ui");
91   PsppireVarStore *vs;
92
93   GtkWidget *dialog = get_widget_assert   (xml, "correlation-dialog");
94   GtkWidget *source = get_widget_assert   (xml, "dict-view");
95
96   g_object_get (de->data_editor, "var-store", &vs, NULL);
97
98   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
99
100   g_object_get (vs, "dictionary", &rd.dict, NULL);
101   g_object_set (source,
102                 "model", rd.dict, 
103                 "predicate", var_is_numeric,
104                 NULL);
105
106   rd.variables = get_widget_assert (xml, "psppire-var-view1");
107   rd.significant = get_widget_assert (xml, "button-flag-significants");
108   rd.two_tailed = get_widget_assert (xml, "button-two-tailed");
109
110   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &rd);
111
112   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
113                                       dialog_state_valid, &rd);
114
115   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
116
117   switch (response)
118     {
119     case GTK_RESPONSE_OK:
120       g_free (execute_syntax_string (de, generate_syntax (&rd)));
121       break;
122     case PSPPIRE_RESPONSE_PASTE:
123       g_free (paste_syntax_to_window (generate_syntax (&rd)));
124       break;
125     default:
126       break;
127     }
128
129   g_object_unref (xml);
130 }
131
132
133 \f
134
135 static char *
136 generate_syntax (const struct correlation *rd)
137 {
138   gchar *text;
139   GString *string = g_string_new ("CORRELATION");
140   g_string_append (string, "\n\t/VARIABLES = ");
141
142   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
143
144
145   g_string_append (string, "\n\t/PRINT =");
146
147   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->two_tailed)))
148     g_string_append (string, " TWOTAIL");
149   else
150     g_string_append (string, " ONETAIL");
151
152
153   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->significant)))
154     g_string_append (string, " NOSIG");
155   else
156     g_string_append (string, " SIG");
157
158
159   g_string_append (string, ".\n");
160
161   text = string->str;
162
163   g_string_free (string, FALSE);
164
165   return text;
166 }