1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 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-runs.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_runs_init (PsppireDialogActionRuns *act);
28 static void psppire_dialog_action_runs_class_init (PsppireDialogActionRunsClass *class);
29
30 G_DEFINE_TYPE (PsppireDialogActionRuns, psppire_dialog_action_runs, PSPPIRE_TYPE_DIALOG_ACTION);
31
32 enum
33 {
34 CB_MEDIAN,
35 CB_MEAN,
36 CB_MODE,
37 CB_CUSTOM
38 };
39
40
41 static void
append_fragment(GString * string,const gchar * cut,PsppireVarView * vv)42 append_fragment (GString *string, const gchar *cut, PsppireVarView *vv)
43 {
44 g_string_append (string, "\n\t/RUNS");
45
46 g_string_append (string, " (");
47 g_string_append (string, cut);
48 g_string_append (string, ") = ");
49
50 psppire_var_view_append_names (vv, 0, string);
51 }
52
53 static char *
generate_syntax(const PsppireDialogAction * act)54 generate_syntax (const PsppireDialogAction *act)
55 {
56 PsppireDialogActionRuns *rd = PSPPIRE_DIALOG_ACTION_RUNS (act);
57 gchar *text;
58
59 GString *string = g_string_new ("NPAR TEST");
60
61 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEAN])))
62 append_fragment (string, "MEAN", PSPPIRE_VAR_VIEW (rd->variables));
63
64 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MEDIAN])))
65 append_fragment (string, "MEDIAN", PSPPIRE_VAR_VIEW (rd->variables));
66
67 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_MODE])))
68 append_fragment (string, "MODE", PSPPIRE_VAR_VIEW (rd->variables));
69
70 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_CUSTOM])))
71 {
72 const char *text = gtk_entry_get_text (GTK_ENTRY (rd->entry));
73 append_fragment (string, text, PSPPIRE_VAR_VIEW (rd->variables));
74 }
75
76 g_string_append (string, ".\n");
77
78 text = string->str;
79
80 g_string_free (string, FALSE);
81
82 return text;
83 }
84
85
86 static gboolean
dialog_state_valid(gpointer data)87 dialog_state_valid (gpointer data)
88 {
89 int i;
90 PsppireDialogActionRuns *fd = PSPPIRE_DIALOG_ACTION_RUNS (data);
91
92 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
93
94 if (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
95 return FALSE;
96
97 for (i = 0; i < 4; ++i)
98 {
99 if (TRUE == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[i])))
100 break;
101 }
102 if (i >= 4)
103 return FALSE;
104
105
106 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[CB_CUSTOM])))
107 {
108 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (fd->entry))))
109 return FALSE;
110 }
111
112 return TRUE;
113 }
114
115 static void
refresh(PsppireDialogAction * rd_)116 refresh (PsppireDialogAction *rd_)
117 {
118 PsppireDialogActionRuns *rd = PSPPIRE_DIALOG_ACTION_RUNS (rd_);
119 int i;
120 GtkTreeModel *liststore =
121 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
122 gtk_list_store_clear (GTK_LIST_STORE (liststore));
123
124 gtk_entry_set_text (GTK_ENTRY (rd->entry), "");
125
126 for (i = 0; i < 4; ++i)
127 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->cb[i]), FALSE);
128 }
129
130 static GtkBuilder *
psppire_dialog_action_runs_activate(PsppireDialogAction * a,GVariant * param)131 psppire_dialog_action_runs_activate (PsppireDialogAction *a, GVariant *param)
132 {
133 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
134 PsppireDialogActionRuns *act = PSPPIRE_DIALOG_ACTION_RUNS (a);
135
136 GtkBuilder *xml = builder_new ("runs.ui");
137
138 pda->dialog = get_widget_assert (xml, "runs-dialog");
139 pda->source = get_widget_assert (xml, "dict-view");
140
141 act->entry = get_widget_assert (xml, "entry1");
142 act->cb[CB_MEDIAN] = get_widget_assert (xml, "checkbutton1");
143 act->cb[CB_MEAN] = get_widget_assert (xml, "checkbutton2");
144 act->cb[CB_MODE] = get_widget_assert (xml, "checkbutton4");
145 act->cb[CB_CUSTOM] = get_widget_assert (xml, "checkbutton3");
146 act->variables = get_widget_assert (xml, "psppire-var-view1");
147
148 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
149 psppire_dialog_action_set_refresh (pda, refresh);
150 return xml;
151 }
152
153 static void
psppire_dialog_action_runs_class_init(PsppireDialogActionRunsClass * class)154 psppire_dialog_action_runs_class_init (PsppireDialogActionRunsClass *class)
155 {
156 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_runs_activate;
157 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
158 }
159
160
161 static void
psppire_dialog_action_runs_init(PsppireDialogActionRuns * act)162 psppire_dialog_action_runs_init (PsppireDialogActionRuns *act)
163 {
164 }
165
166