New file: builder-wrapper.h and builder-wrapper.c
[pspp-builds.git] / src / ui / gui / sort-cases-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011, 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 #include <config.h>
18 #include <gtk/gtk.h>
19 #include "sort-cases-dialog.h"
20 #include "executor.h"
21 #include "psppire-dialog.h"
22 #include "psppire-data-window.h"
23 #include "psppire-var-store.h"
24 #include "dialog-common.h"
25 #include "psppire-selector.h"
26 #include "dict-display.h"
27 #include "psppire-var-view.h"
28
29 #include "builder-wrapper.h"
30 #include "helper.h"
31
32
33 static void
34 refresh (PsppireDialog *dialog, GtkTreeView *dest)
35 {
36   GtkTreeModel *liststore = gtk_tree_view_get_model (dest);
37
38   gtk_list_store_clear (GTK_LIST_STORE (liststore));
39 }
40
41
42 struct sort_cases_dialog
43 {
44   PsppireVarView *tv;
45   PsppireDict *dict;
46   GtkToggleButton *ascending;
47 };
48
49
50 static gboolean
51 dialog_state_valid (gpointer data)
52 {
53   struct sort_cases_dialog *scd = data;
54   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (scd->tv));
55
56   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
57
58   if ( n_rows == 0 )
59     return FALSE;
60
61   return TRUE;
62 }
63
64 static char *
65 generate_syntax (const struct sort_cases_dialog *scd)
66 {
67   gchar *text;
68   GString *string = g_string_new ("SORT CASES BY ");
69
70   gint n_vars = psppire_var_view_append_names (scd->tv, 0, string);
71
72   if ( n_vars == 0 )
73     g_string_assign (string, "");
74   else
75     {
76       const char up_down =
77         gtk_toggle_button_get_active (scd->ascending) ? 'A' : 'D';
78       g_string_append_printf (string, "(%c)", up_down);
79       g_string_append (string, ".");
80     }
81
82
83   text = string->str;
84
85   g_string_free (string, FALSE);
86
87   return text;
88 }
89
90
91 /* Pops up the Sort Cases dialog box */
92 void
93 sort_cases_dialog (PsppireDataWindow *de)
94 {
95   gint response;
96
97   struct sort_cases_dialog scd;
98
99   GtkBuilder *xml = builder_new ("sort.ui");
100
101   GtkWidget *dialog = get_widget_assert   (xml, "sort-cases-dialog");
102
103
104   GtkWidget *source = get_widget_assert   (xml, "sort-cases-treeview1");
105   GtkWidget *dest =   get_widget_assert   (xml, "sort-cases-treeview2");
106   PsppireVarStore *vs = NULL;
107
108   g_object_get (de->data_editor, "var-store", &vs, NULL);
109
110   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
111
112   g_object_get (vs, "dictionary", &scd.dict, NULL);
113   g_object_set (source, "model", scd.dict, NULL);
114
115   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  dest);
116
117   scd.tv = PSPPIRE_VAR_VIEW (dest);
118   scd.ascending =
119     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sort-cases-radiobutton0"));
120
121
122   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
123                                       dialog_state_valid, &scd);
124
125
126   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
127
128
129   switch (response)
130     {
131     case GTK_RESPONSE_OK:
132       g_free (execute_syntax_string (de, generate_syntax (&scd)));
133       break;
134     case PSPPIRE_RESPONSE_PASTE:
135       g_free (paste_syntax_to_window (generate_syntax (&scd)));
136       break;
137     default:
138       break;
139     }
140
141   g_object_unref (xml);
142 }
143