Rename psppire_dialog_action_get_pointer -> psppire_dialog_action_get_hash_table
[pspp] / src / ui / gui / psppire-dialog-action-histogram.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2015  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-histogram.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include <ui/syntax-gen.h>
25 #include "psppire-var-view.h"
26
27 #include "psppire-dialog.h"
28 #include "builder-wrapper.h"
29
30 #include "psppire-dict.h"
31 #include "libpspp/str.h"
32
33 static void
34 psppire_dialog_action_histogram_class_init (PsppireDialogActionHistogramClass *class);
35
36 G_DEFINE_TYPE (PsppireDialogActionHistogram, psppire_dialog_action_histogram, PSPPIRE_TYPE_DIALOG_ACTION);
37
38 static gboolean
39 dialog_state_valid (gpointer data)
40 {
41   PsppireDialogActionHistogram *rd = data;
42
43   const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (rd->variable));
44   const struct variable *var = psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION (rd)->dict, var_name);
45
46   if ( var == NULL)
47     return FALSE;
48
49
50   return TRUE;
51 }
52
53 static void
54 refresh (PsppireDialogAction *rd_)
55 {
56   PsppireDialogActionHistogram *rd = PSPPIRE_DIALOG_ACTION_HISTOGRAM (rd_);
57
58   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->curve), FALSE);
59   gtk_entry_set_text (GTK_ENTRY (rd->variable), "");
60 }
61
62 static void
63 psppire_dialog_action_histogram_activate (GtkAction *a)
64 {
65   PsppireDialogActionHistogram *act = PSPPIRE_DIALOG_ACTION_HISTOGRAM (a);
66   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
67
68   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
69   GtkBuilder *xml = g_hash_table_lookup (thing, a);
70   if (!xml)
71     {
72       xml = builder_new ("histogram.ui");
73       g_hash_table_insert (thing, a, xml);
74     }
75
76   pda->dialog = get_widget_assert (xml, "histogram-dialog");
77   pda->source = get_widget_assert (xml, "dict-view");
78
79   act->variable = get_widget_assert (xml, "entry1");
80   act->curve = get_widget_assert (xml, "curve");
81
82   psppire_dialog_action_set_refresh (pda, refresh);
83
84   psppire_dialog_action_set_valid_predicate (pda,
85                                         dialog_state_valid);
86
87   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_histogram_parent_class)->activate)
88     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_histogram_parent_class)->activate (pda);
89 }
90
91
92
93 static char *
94 generate_syntax (PsppireDialogAction *a)
95 {
96   PsppireDialogActionHistogram *rd = PSPPIRE_DIALOG_ACTION_HISTOGRAM (a);
97   gchar *text;
98   const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (rd->variable));
99   GString *string = g_string_new ("GRAPH /HISTOGRAM ");
100
101   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->curve)))
102     {
103       g_string_append (string, "(NORMAL)");
104     }
105
106   g_string_append (string, " = ");
107   g_string_append (string, var_name);
108
109   g_string_append (string, ".\n");
110
111   text = string->str;
112
113   g_string_free (string, FALSE);
114
115   return text;
116 }
117
118 static void
119 psppire_dialog_action_histogram_class_init (PsppireDialogActionHistogramClass *class)
120 {
121   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
122
123   action_class->activate = psppire_dialog_action_histogram_activate;
124
125   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
126 }
127
128
129 static void
130 psppire_dialog_action_histogram_init (PsppireDialogActionHistogram *act)
131 {
132 }
133