1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2012 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-means.h"
21
22 #include "psppire-means-layer.h"
23
24 #include "psppire-var-view.h"
25 #include "psppire-dict.h"
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_dialog_action_means_class_init (PsppireDialogActionMeansClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionMeans, psppire_dialog_action_means, PSPPIRE_TYPE_DIALOG_ACTION);
36
37
38 static char *
generate_syntax(const PsppireDialogAction * act)39 generate_syntax (const PsppireDialogAction *act)
40 {
41 gint l;
42 PsppireDialogActionMeans *scd = PSPPIRE_DIALOG_ACTION_MEANS (act);
43 gchar *text;
44 GString *string = g_string_new ("MEANS TABLES = ");
45 PsppireMeansLayer *layer = PSPPIRE_MEANS_LAYER (scd->layer);
46 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (scd->variables), 0, string);
47
48 for (l = 0; l < layer->n_layers; ++l)
49 {
50 GtkTreeIter iter;
51
52 GtkTreeModel *m = psppire_means_layer_get_model_n (layer, l);
53 gboolean ok = gtk_tree_model_get_iter_first (m, &iter);
54 if (ok)
55 g_string_append (string, "\n\tBY");
56 for (; ok; ok = gtk_tree_model_iter_next (m, &iter))
57 {
58 const struct variable *var = psppire_var_view_get_var_from_model (m, 0, &iter);
59 g_string_append (string, " ");
60 g_string_append (string, var_get_name (var));
61 }
62 }
63
64 g_string_append (string, ".\n");
65 text = string->str;
66
67 g_string_free (string, FALSE);
68
69 return text;
70 }
71
72 static gboolean
dialog_state_valid(PsppireDialogAction * da)73 dialog_state_valid (PsppireDialogAction *da)
74 {
75 PsppireDialogActionMeans *pdm = PSPPIRE_DIALOG_ACTION_MEANS (da);
76 GtkTreeIter notused;
77 GtkTreeModel *vars =
78 gtk_tree_view_get_model (GTK_TREE_VIEW (pdm->variables));
79
80 return gtk_tree_model_get_iter_first (vars, ¬used);
81 }
82
83 static void
dialog_refresh(PsppireDialogAction * da)84 dialog_refresh (PsppireDialogAction *da)
85 {
86 PsppireDialogActionMeans *pdm = PSPPIRE_DIALOG_ACTION_MEANS (da);
87 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (pdm->variables));
88
89 gtk_list_store_clear (GTK_LIST_STORE (liststore));
90
91 psppire_means_layer_clear (PSPPIRE_MEANS_LAYER (pdm->layer));
92 }
93
94 static GtkBuilder *
psppire_dialog_action_means_activate(PsppireDialogAction * a,GVariant * param)95 psppire_dialog_action_means_activate (PsppireDialogAction *a, GVariant *param)
96 {
97 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
98 PsppireDialogActionMeans *act = PSPPIRE_DIALOG_ACTION_MEANS (a);
99
100 GtkBuilder *xml = builder_new ("means.ui");
101
102 GtkWidget *vb = get_widget_assert (xml, "frame2");
103 act->layer = psppire_means_layer_new ();
104 gtk_container_add (GTK_CONTAINER (vb), act->layer);
105 gtk_widget_show (act->layer);
106
107 GtkWidget *selector = get_widget_assert (xml, "layer-selector");
108
109 pda->dialog = get_widget_assert (xml, "means-dialog");
110 pda->source = get_widget_assert (xml, "all-variables");
111 act->variables = get_widget_assert (xml, "stat-variables");
112
113 g_object_set (pda->source,
114 "predicate", var_is_numeric,
115 NULL);
116
117 g_object_set (selector,
118 "dest-widget", act->layer,
119 NULL);
120
121 psppire_dialog_action_set_valid_predicate (pda, (void *) dialog_state_valid);
122 psppire_dialog_action_set_refresh (pda, dialog_refresh);
123 return xml;
124 }
125
126 static void
psppire_dialog_action_means_class_init(PsppireDialogActionMeansClass * class)127 psppire_dialog_action_means_class_init (PsppireDialogActionMeansClass *class)
128 {
129 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_means_activate;
130
131 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
132 }
133
134 static void
psppire_dialog_action_means_init(PsppireDialogActionMeans * act)135 psppire_dialog_action_means_init (PsppireDialogActionMeans *act)
136 {
137 }
138