16f0f721c3703a3544fd4566a7758caab45bf0cc
[pspp] / src / ui / gui / psppire-dialog-action-k-related.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2010, 2011, 2012, 2013  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
18 #include <config.h>
19
20 #include "psppire-dialog-action-k-related.h"
21
22 #include "psppire-var-view.h"
23
24 #include "psppire-dialog.h"
25 #include "builder-wrapper.h"
26
27 static void psppire_dialog_action_k_related_init            (PsppireDialogActionKRelated      *act);
28 static void psppire_dialog_action_k_related_class_init      (PsppireDialogActionKRelatedClass *class);
29
30 G_DEFINE_TYPE (PsppireDialogActionKRelated, psppire_dialog_action_k_related, PSPPIRE_TYPE_DIALOG_ACTION);
31
32 static char *
33 generate_syntax (PsppireDialogAction *act)
34 {
35   PsppireDialogActionKRelated *krd = PSPPIRE_DIALOG_ACTION_K_RELATED (act);
36   gchar *text;
37
38   GString *string = g_string_new ("NPAR TEST");
39
40   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (krd->friedman)))
41     {
42       g_string_append (string, "\n\t/FRIEDMAN = ");
43       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (krd->var_view), 0, string);
44     }
45
46   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (krd->kendal)))
47     {
48       g_string_append (string, "\n\t/KENDALL = ");
49       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (krd->var_view), 0, string);
50     }
51
52   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (krd->cochran)))
53     {
54       g_string_append (string, "\n\t/COCHRAN = ");
55       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (krd->var_view), 0, string);
56     }
57
58   g_string_append (string, ".\n");
59
60   text = string->str;
61
62   g_string_free (string, FALSE);
63
64   return text;
65 }
66
67
68 static gboolean
69 dialog_state_valid (gpointer data)
70 {
71   PsppireDialogActionKRelated *krd = PSPPIRE_DIALOG_ACTION_K_RELATED (data);
72
73   GtkTreeModel *vars =
74     gtk_tree_view_get_model (GTK_TREE_VIEW (krd->var_view));
75
76   /* Tests using less than 3 variables are not useful */
77   if (gtk_tree_model_iter_n_children (vars, NULL) < 3)
78     return FALSE;
79
80   /* At least one checkbutton must be active */
81   if ( 
82       ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (krd->friedman))
83       && 
84       ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (krd->kendal))
85       && 
86       ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (krd->cochran))
87        )
88     return FALSE;
89
90   return TRUE;
91 }
92
93 static void
94 refresh (PsppireDialogAction *rd_)
95 {
96   PsppireDialogActionKRelated *krd = PSPPIRE_DIALOG_ACTION_K_RELATED (rd_);
97   GtkTreeModel *liststore =
98     gtk_tree_view_get_model (GTK_TREE_VIEW (krd->var_view));
99
100   gtk_list_store_clear (GTK_LIST_STORE (liststore));
101
102   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (krd->friedman), TRUE);
103   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (krd->kendal), FALSE);
104   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (krd->cochran), FALSE);
105 }
106
107 static void
108 psppire_dialog_action_k_related_activate (GtkAction *a)
109 {
110   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
111   PsppireDialogActionKRelated *act = PSPPIRE_DIALOG_ACTION_K_RELATED (a);
112
113   GHashTable *thing = psppire_dialog_action_get_pointer (pda);
114   GtkBuilder *xml = g_hash_table_lookup (thing, a);
115   if (!xml)
116     {
117       xml = builder_new ("k-related.ui");
118       g_hash_table_insert (thing, a, xml);
119     }
120
121   pda->dialog = get_widget_assert   (xml, "k-related-dialog");
122   pda->source = get_widget_assert   (xml, "dict-view");
123
124   act->var_view  = get_widget_assert (xml, "variables-treeview");
125   act->friedman =  get_widget_assert (xml, "friedman-checkbutton");
126   act->kendal =  get_widget_assert (xml, "kendal-checkbutton");
127   act->cochran =  get_widget_assert (xml, "cochran-checkbutton");
128
129   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
130   psppire_dialog_action_set_refresh (pda, refresh);
131
132   g_object_set (pda->source,
133                 "predicate", var_is_numeric,
134                 NULL);
135
136   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_k_related_parent_class)->activate)
137     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_k_related_parent_class)->activate (pda);
138 }
139
140 static void
141 psppire_dialog_action_k_related_class_init (PsppireDialogActionKRelatedClass *class)
142 {
143   psppire_dialog_action_set_activation (class, psppire_dialog_action_k_related_activate);
144   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
145 }
146
147
148 static void
149 psppire_dialog_action_k_related_init (PsppireDialogActionKRelated *act)
150 {
151 }
152