1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2015 Free Software Foundation
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.
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.
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/>. */
20 #include "psppire-dialog-action-weight.h"
21 #include "psppire-selector.h"
22 #include "psppire-var-view.h"
23 #include "dict-display.h"
25 #include "psppire-dialog.h"
26 #include "builder-wrapper.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
32 static void psppire_dialog_action_weight_init (PsppireDialogActionWeight *act);
33 static void psppire_dialog_action_weight_class_init (PsppireDialogActionWeightClass *class);
35 G_DEFINE_TYPE (PsppireDialogActionWeight, psppire_dialog_action_weight, PSPPIRE_TYPE_DIALOG_ACTION);
39 generate_syntax (const PsppireDialogAction *pda)
42 PsppireDialogActionWeight *wcd = PSPPIRE_DIALOG_ACTION_WEIGHT (pda);
44 const gchar *text = gtk_entry_get_text (GTK_ENTRY (wcd->entry));
46 const struct variable *var = psppire_dict_lookup_var (pda->dict, text);
49 syntax = g_strdup ("WEIGHT OFF.\n");
51 syntax = g_strdup_printf ("WEIGHT BY %s.\n",
59 dialog_state_valid (gpointer data)
65 refresh (PsppireDialogAction *pda)
67 PsppireDialogActionWeight *wcd = PSPPIRE_DIALOG_ACTION_WEIGHT (pda);
69 const struct variable *var = dict_get_weight (pda->dict->dict);
73 gtk_entry_set_text (GTK_ENTRY (wcd->entry), "");
74 gtk_label_set_text (GTK_LABEL (wcd->status), _("Do not weight cases"));
75 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE);
80 g_strdup_printf (_("Weight cases by %s"), var_get_name (var));
82 gtk_entry_set_text (GTK_ENTRY (wcd->entry), var_get_name (var));
83 gtk_label_set_text (GTK_LABEL (wcd->status), text);
86 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE);
89 g_signal_emit_by_name (wcd->entry, "activate");
93 on_select (PsppireSelector *sel, gpointer data)
95 PsppireDialogActionWeight *wcd = data;
97 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->on), TRUE);
98 gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), TRUE);
102 on_deselect (PsppireSelector *sel, gpointer data)
104 PsppireDialogActionWeight *wcd = data;
106 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wcd->off), TRUE);
107 gtk_widget_set_sensitive (GTK_WIDGET (wcd->on), FALSE);
111 on_toggle (GtkToggleButton *off, gpointer data)
113 PsppireDialogActionWeight *wcd = data;
115 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wcd->off)))
117 gtk_entry_set_text (GTK_ENTRY (wcd->entry), "");
123 psppire_dialog_action_weight_activate (PsppireDialogAction *pda)
125 PsppireDialogActionWeight *act = PSPPIRE_DIALOG_ACTION_WEIGHT (pda);
127 GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
128 GtkBuilder *xml = g_hash_table_lookup (thing, pda);
131 xml = builder_new ("weight.ui");
132 g_hash_table_insert (thing, pda, xml);
134 pda->dialog = get_widget_assert (xml, "weight-cases-dialog");
135 pda->source = get_widget_assert (xml, "weight-cases-treeview");
137 act->entry = get_widget_assert (xml, "weight-cases-entry");
138 act->off = get_widget_assert (xml,"weight-cases-radiobutton1");
139 act->on = get_widget_assert (xml, "radiobutton2");
140 act->status = get_widget_assert (xml, "weight-status-label");
141 GtkWidget *selector = get_widget_assert (xml, "weight-cases-selector");
143 g_signal_connect (selector, "selected", G_CALLBACK (on_select), act);
144 g_signal_connect (selector, "de-selected", G_CALLBACK (on_deselect), act);
145 g_signal_connect (act->off, "toggled", G_CALLBACK (on_toggle), act);
147 g_object_set (pda->source,
148 "selection-mode", GTK_SELECTION_SINGLE,
149 "predicate", var_is_numeric,
152 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector),
153 is_currently_in_entry);
156 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
157 psppire_dialog_action_set_refresh (pda, refresh);
162 psppire_dialog_action_weight_class_init (PsppireDialogActionWeightClass *class)
164 psppire_dialog_action_set_activation (class, psppire_dialog_action_weight_activate);
165 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
170 psppire_dialog_action_weight_init (PsppireDialogActionWeight *act)