664dd36e43b89218f8837e651169d58e690e1184
[pspp] / src / ui / gui / psppire-dialog-action-univariate.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-univariate.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_univariate_init            (PsppireDialogActionUnivariate      *act);
28 static void psppire_dialog_action_univariate_class_init      (PsppireDialogActionUnivariateClass *class);
29
30 G_DEFINE_TYPE (PsppireDialogActionUnivariate, psppire_dialog_action_univariate, PSPPIRE_TYPE_DIALOG_ACTION);
31
32 static char *
33 generate_syntax (PsppireDialogAction *act)
34 {
35   PsppireDialogActionUnivariate *uvd = PSPPIRE_DIALOG_ACTION_UNIVARIATE (act);
36
37   gchar *text = NULL;
38   GString *str = g_string_new ("GLM ");
39
40   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (uvd->dep_entry)));
41   
42   g_string_append (str, " BY ");
43
44   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (uvd->factor_list), 0, str);
45
46   g_string_append (str, ".\n");
47
48   text = str->str;
49
50   g_string_free (str, FALSE);
51
52   return text;
53 }
54
55
56 static gboolean
57 dialog_state_valid (gpointer data)
58 {
59   PsppireDialogActionUnivariate *ud = PSPPIRE_DIALOG_ACTION_UNIVARIATE (data);
60   GtkTreeModel *vars;
61   GtkTreeIter notused;
62
63   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ud->dep_entry))))
64     return false;
65
66   vars =
67     gtk_tree_view_get_model (GTK_TREE_VIEW (ud->factor_list));
68
69   return gtk_tree_model_get_iter_first (vars, &notused);
70 }
71
72 static void
73 refresh (PsppireDialogAction *rd_)
74 {
75   PsppireDialogActionUnivariate *uv = PSPPIRE_DIALOG_ACTION_UNIVARIATE (rd_);
76   GtkTreeModel *liststore ;
77
78   gtk_entry_set_text (GTK_ENTRY (uv->dep_entry), "");
79
80   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (uv->factor_list));
81
82   gtk_list_store_clear (GTK_LIST_STORE (liststore));
83 }
84
85 static void
86 psppire_dialog_action_univariate_activate (GtkAction *a)
87 {
88   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
89   PsppireDialogActionUnivariate *act = PSPPIRE_DIALOG_ACTION_UNIVARIATE (a);
90
91   GHashTable *thing = psppire_dialog_action_get_pointer (pda);
92   GtkBuilder *xml = g_hash_table_lookup (thing, a);
93   if (!xml)
94     {
95       xml = builder_new ("univariate.ui");
96       g_hash_table_insert (thing, a, xml);
97     }
98
99   pda->dialog = get_widget_assert   (xml, "univariate-dialog");
100   pda->source = get_widget_assert   (xml, "dict-view");
101
102   act->dep_entry = get_widget_assert (xml, "dep-entry");
103   act->factor_list = get_widget_assert (xml, "factors-view");
104
105   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
106   psppire_dialog_action_set_refresh (pda, refresh);
107
108   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_univariate_parent_class)->activate)
109     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_univariate_parent_class)->activate (pda);
110 }
111
112 static void
113 psppire_dialog_action_univariate_class_init (PsppireDialogActionUnivariateClass *class)
114 {
115   psppire_dialog_action_set_activation (class, psppire_dialog_action_univariate_activate);
116   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
117 }
118
119
120 static void
121 psppire_dialog_action_univariate_init (PsppireDialogActionUnivariate *act)
122 {
123 }
124