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