Change some instances of GtkAction to PsppireDialogAction
[pspp] / src / ui / gui / psppire-dialog-action-sort.c
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-sort.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_sort_init            (PsppireDialogActionSort      *act);
28 static void psppire_dialog_action_sort_class_init      (PsppireDialogActionSortClass *class);
29
30 G_DEFINE_TYPE (PsppireDialogActionSort, psppire_dialog_action_sort, PSPPIRE_TYPE_DIALOG_ACTION);
31
32 static char *
33 generate_syntax (PsppireDialogAction *act)
34 {
35   PsppireDialogActionSort *scd = PSPPIRE_DIALOG_ACTION_SORT (act);
36   gchar *text;
37   GString *string = g_string_new ("SORT CASES BY ");
38
39   PsppireVarView *var_view = PSPPIRE_VAR_VIEW (scd->variables);
40   gint n_vars = psppire_var_view_append_names (var_view, 0, string);
41
42   if ( n_vars == 0 )
43     {
44       g_string_assign (string, "");
45     }
46   else
47     {
48       const char up_down =
49         (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->ascending))
50          ? 'A' : 'D');
51       g_string_append_printf (string, "(%c)", up_down);
52       g_string_append (string, ".");
53     }
54
55   text = string->str;
56
57   g_string_free (string, FALSE);
58
59   return text;
60 }
61
62 static void
63 reset (PsppireDialogAction *act)
64 {
65   PsppireDialogActionSort *scd = PSPPIRE_DIALOG_ACTION_SORT (act);
66
67   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (scd->variables));
68
69   gtk_list_store_clear (GTK_LIST_STORE (liststore));
70
71   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scd->ascending), TRUE);
72 }
73
74
75
76
77 static gboolean
78 dialog_state_valid (gpointer act)
79 {
80   PsppireDialogActionSort *scd = PSPPIRE_DIALOG_ACTION_SORT (act);
81   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (scd->variables));
82
83   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
84
85   if ( n_rows == 0 )
86     return FALSE;
87
88   return TRUE;
89 }
90
91
92 static void
93 psppire_dialog_action_sort_activate (PsppireDialogAction *a)
94 {
95   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
96   PsppireDialogActionSort *act = PSPPIRE_DIALOG_ACTION_SORT (a);
97
98   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
99   GtkBuilder *xml = g_hash_table_lookup (thing, a);
100   if (!xml)
101     {
102       xml = builder_new ("sort.ui");
103       g_hash_table_insert (thing, a, xml);
104     }
105
106   pda->dialog = get_widget_assert (xml, "sort-cases-dialog");
107   pda->source = get_widget_assert (xml, "sort-cases-treeview1");
108   
109   act->variables =  get_widget_assert (xml, "sort-cases-treeview2");
110   act->ascending = get_widget_assert (xml, "sort-cases-radiobutton0");
111
112   psppire_dialog_action_set_refresh (pda, reset);
113
114   psppire_dialog_action_set_valid_predicate (pda,
115                                       dialog_state_valid);
116
117   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_sort_parent_class)->activate)
118     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_sort_parent_class)->activate (pda);
119 }
120
121 static void
122 psppire_dialog_action_sort_class_init (PsppireDialogActionSortClass *class)
123 {
124  PsppireDialogActionClass *pdac = PSPPIRE_DIALOG_ACTION_CLASS (class);
125   psppire_dialog_action_set_activation (class, psppire_dialog_action_sort_activate);
126
127   pdac->generate_syntax = generate_syntax;
128 }
129
130
131 static void
132 psppire_dialog_action_sort_init (PsppireDialogActionSort *act)
133 {
134 }
135