Change some instances of GtkAction to PsppireDialogAction
[pspp] / src / ui / gui / psppire-dialog-action-runs.c
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
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 *
54 generate_syntax (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
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
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 void
131 psppire_dialog_action_runs_activate (PsppireDialogAction *a)
132 {
133   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
134   PsppireDialogActionRuns *act = PSPPIRE_DIALOG_ACTION_RUNS (a);
135
136   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
137   GtkBuilder *xml = g_hash_table_lookup (thing, a);
138   if (!xml)
139     {
140       xml = builder_new ("runs.ui");
141       g_hash_table_insert (thing, a, xml);
142     }
143
144   pda->dialog = get_widget_assert   (xml, "runs-dialog");
145   pda->source = get_widget_assert   (xml, "dict-view");
146
147   act->entry = get_widget_assert   (xml, "entry1");
148   act->cb[CB_MEDIAN] = get_widget_assert (xml, "checkbutton1");
149   act->cb[CB_MEAN] = get_widget_assert (xml, "checkbutton2");
150   act->cb[CB_MODE] = get_widget_assert (xml, "checkbutton4");
151   act->cb[CB_CUSTOM] = get_widget_assert (xml, "checkbutton3");
152   act->variables = get_widget_assert   (xml, "psppire-var-view1");
153
154   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
155   psppire_dialog_action_set_refresh (pda, refresh);
156
157   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_runs_parent_class)->activate)
158     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_runs_parent_class)->activate (pda);
159 }
160
161 static void
162 psppire_dialog_action_runs_class_init (PsppireDialogActionRunsClass *class)
163 {
164   psppire_dialog_action_set_activation (class, psppire_dialog_action_runs_activate);
165   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
166 }
167
168
169 static void
170 psppire_dialog_action_runs_init (PsppireDialogActionRuns *act)
171 {
172 }
173