New objects psppire-window and psppire-syntax-window.
[pspp-builds.git] / src / ui / gui / sort-cases-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007  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 "helper.h"
21 #include "psppire-dialog.h"
22 #include "data-editor.h"
23 #include "psppire-var-store.h"
24 #include "dialog-common.h"
25 #include "dict-display.h"
26
27 #include <language/syntax-string-source.h>
28 #include "psppire-syntax-window.h"
29
30 static void
31 refresh (PsppireDialog *dialog, GtkTreeView *dest)
32 {
33   GtkTreeModel *liststore = gtk_tree_view_get_model (dest);
34
35
36   gtk_list_store_clear (GTK_LIST_STORE (liststore));
37 }
38
39
40 struct sort_cases_dialog
41 {
42   GtkTreeView *tv;
43   PsppireDict *dict;
44   GtkToggleButton *ascending;
45 };
46
47
48 static gboolean
49 dialog_state_valid (gpointer data)
50 {
51   struct sort_cases_dialog *scd = data;
52   GtkTreeModel *model = gtk_tree_view_get_model (scd->tv);
53
54   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
55
56   if ( n_rows == 0 )
57     return FALSE;
58
59   return TRUE;
60 }
61
62 static char *
63 generate_syntax (const struct sort_cases_dialog *scd)
64 {
65   gchar *text;
66   GString *string = g_string_new ("SORT CASES BY ");
67   gint n_vars = append_variable_names (string,
68                                        scd->dict, GTK_TREE_VIEW (scd->tv), 0);
69
70   if ( n_vars == 0 )
71     g_string_assign (string, "");
72   else
73     {
74       const char up_down =
75         gtk_toggle_button_get_active (scd->ascending) ? 'A' : 'D';
76       g_string_append_printf (string, "(%c)", up_down);
77       g_string_append (string, ".");
78     }
79
80
81   text = string->str;
82
83   g_string_free (string, FALSE);
84
85   return text;
86 }
87
88
89 /* Pops up the Sort Cases dialog box */
90 void
91 sort_cases_dialog (GObject *o, gpointer data)
92 {
93   gint response;
94   struct data_editor *de = data;
95
96   struct sort_cases_dialog scd;
97
98   GladeXML *xml = XML_NEW ("psppire.glade");
99
100   GtkWidget *dialog = get_widget_assert   (xml, "sort-cases-dialog");
101
102
103   GtkWidget *source = get_widget_assert   (xml, "sort-cases-treeview1");
104   GtkWidget *selector = get_widget_assert (xml, "sort-cases-selector");
105   GtkWidget *dest =   get_widget_assert   (xml, "sort-cases-treeview2");
106
107   PsppireVarStore *vs = NULL;
108
109   g_object_get (de->data_editor, "var-store", &vs, NULL);
110
111   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
112
113   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
114                                  vs->dict,
115                                  GTK_SELECTION_MULTIPLE, NULL);
116
117   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
118
119   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
120                                  source,
121                                  dest,
122                                  insert_source_row_into_tree_view,
123                                  NULL,
124                                  NULL);
125
126   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  dest);
127
128   scd.tv = GTK_TREE_VIEW (dest);
129   scd.dict = vs->dict;
130   scd.ascending =
131     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sort-cases-radiobutton0"));
132
133
134   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
135                                       dialog_state_valid, &scd);
136
137
138   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
139
140
141   switch (response)
142     {
143     case GTK_RESPONSE_OK:
144       {
145         gchar *syntax = generate_syntax (&scd);
146         struct getl_interface *sss = create_syntax_string_source (syntax);
147         execute_syntax (sss);
148
149         g_free (syntax);
150       }
151       break;
152     case PSPPIRE_RESPONSE_PASTE:
153       {
154         gchar *syntax = generate_syntax (&scd);
155
156         GtkWidget *se = psppire_syntax_window_new ();
157
158         gtk_text_buffer_insert_at_cursor (PSPPIRE_SYNTAX_WINDOW (se)->buffer, syntax, -1);
159
160         gtk_widget_show (se);
161
162         g_free (syntax);
163       }
164       break;
165     default:
166       break;
167     }
168
169   g_object_unref (xml);
170 }
171