Converted Sort dialog to a PsppireDialogAction object
[pspp-builds.git] / 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   gint n_vars = psppire_var_view_append_names (scd->variables, 0, string);
40
41   if ( n_vars == 0 )
42     {
43       g_string_assign (string, "");
44     }
45   else
46     {
47       const char up_down =
48         gtk_toggle_button_get_active (scd->ascending) ? 'A' : 'D';
49       g_string_append_printf (string, "(%c)", up_down);
50       g_string_append (string, ".");
51     }
52
53   text = string->str;
54
55   g_string_free (string, FALSE);
56
57   return text;
58 }
59
60 static void
61 reset (PsppireDialogAction *act)
62 {
63   PsppireDialogActionSort *scd = PSPPIRE_DIALOG_ACTION_SORT (act);
64
65   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (scd->variables));
66
67   gtk_list_store_clear (GTK_LIST_STORE (liststore));
68
69   gtk_toggle_button_set_active (scd->ascending, TRUE);
70 }
71
72
73
74
75 static gboolean
76 dialog_state_valid (PsppireDialogAction *act)
77 {
78   PsppireDialogActionSort *scd = PSPPIRE_DIALOG_ACTION_SORT (act);
79   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (scd->variables));
80
81   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
82
83   if ( n_rows == 0 )
84     return FALSE;
85
86   return TRUE;
87 }
88
89
90 static void
91 psppire_dialog_action_sort_activate (GtkAction *a)
92 {
93   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
94   PsppireDialogActionSort *act = PSPPIRE_DIALOG_ACTION_SORT (a);
95
96   GtkBuilder *xml = builder_new ("sort.ui");
97   pda->dialog = get_widget_assert   (xml, "sort-cases-dialog");
98   pda->source = get_widget_assert   (xml, "sort-cases-treeview1");
99   
100   act->variables =  get_widget_assert   (xml, "sort-cases-treeview2");
101   act->ascending = get_widget_assert (xml, "sort-cases-radiobutton0");
102
103   psppire_dialog_action_set_refresh (pda, reset);
104
105   psppire_dialog_action_set_valid_predicate (pda,
106                                       dialog_state_valid);
107
108   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_sort_parent_class)->activate)
109     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_sort_parent_class)->activate (pda);
110 }
111
112 static void
113 psppire_dialog_action_sort_class_init (PsppireDialogActionSortClass *class)
114 {
115   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
116   PsppireDialogActionClass *pdac = PSPPIRE_DIALOG_ACTION_CLASS (class);
117
118   action_class->activate = psppire_dialog_action_sort_activate;
119
120   pdac->generate_syntax = generate_syntax;
121 }
122
123
124 static void
125 psppire_dialog_action_sort_init (PsppireDialogActionSort *act)
126 {
127 }
128