Use a more reliable way of setting the initial state of PsppireDialogAction objects.
[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 (const 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 GtkBuilder *
93 psppire_dialog_action_sort_activate (PsppireDialogAction *a, GVariant *param)
94 {
95   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
96   PsppireDialogActionSort *act = PSPPIRE_DIALOG_ACTION_SORT (a);
97
98   GtkBuilder *xml = builder_new ( "sort.ui");
99
100   pda->dialog = get_widget_assert (xml, "sort-cases-dialog");
101   pda->source = get_widget_assert (xml, "sort-cases-treeview1");
102
103   act->variables =  get_widget_assert (xml, "sort-cases-treeview2");
104   act->ascending = get_widget_assert (xml, "sort-cases-radiobutton0");
105
106   psppire_dialog_action_set_refresh (pda, reset);
107
108   psppire_dialog_action_set_valid_predicate (pda,
109                                       dialog_state_valid);
110   return xml;
111 }
112
113 static void
114 psppire_dialog_action_sort_class_init (PsppireDialogActionSortClass *class)
115 {
116  PsppireDialogActionClass *pdac = PSPPIRE_DIALOG_ACTION_CLASS (class);
117   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_sort_activate;
118
119  pdac->generate_syntax = generate_syntax;
120 }
121
122
123 static void
124 psppire_dialog_action_sort_init (PsppireDialogActionSort *act)
125 {
126 }
127