Change license from GPLv2+ to GPLv3+.
[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 <gtksheet/gtksheet.h>
24 #include "psppire-var-store.h"
25 #include "dialog-common.h"
26 #include "dict-display.h"
27
28 #include <language/syntax-string-source.h>
29 #include "syntax-editor.h"
30
31 static void
32 refresh (PsppireDialog *dialog, GtkTreeView *dest)
33 {
34   GtkTreeModel *liststore = gtk_tree_view_get_model (dest);
35
36
37   gtk_list_store_clear (GTK_LIST_STORE (liststore));
38 }
39
40
41 struct sort_cases_dialog
42 {
43   GtkTreeView *tv;
44   PsppireDict *dict;
45   GtkToggleButton *ascending;
46 };
47
48 static char *
49 generate_syntax (const struct sort_cases_dialog *scd)
50 {
51   gchar *text;
52   GString *string = g_string_new ("SORT CASES BY ");
53   gint n_vars = append_variable_names (string,
54                                        scd->dict, GTK_TREE_VIEW (scd->tv));
55
56   if ( n_vars == 0 )
57     g_string_assign (string, "");
58   else
59     {
60       const char up_down =
61         gtk_toggle_button_get_active (scd->ascending) ? 'A' : 'D';
62       g_string_append_printf (string, "(%c)", up_down);
63       g_string_append (string, ".");
64     }
65
66
67   text = string->str;
68
69   g_string_free (string, FALSE);
70
71   return text;
72 }
73
74
75 /* Pops up the Sort Cases dialog box */
76 void
77 sort_cases_dialog (GObject *o, gpointer data)
78 {
79   gint response;
80   struct data_editor *de = data;
81
82   struct sort_cases_dialog scd;
83
84   GladeXML *xml = XML_NEW ("psppire.glade");
85
86   GtkWidget *dialog = get_widget_assert   (xml, "sort-cases-dialog");
87
88
89   GtkWidget *source = get_widget_assert   (xml, "sort-cases-treeview1");
90   GtkWidget *selector = get_widget_assert (xml, "sort-cases-selector");
91   GtkWidget *dest =   get_widget_assert   (xml, "sort-cases-treeview2");
92
93   GtkSheet *var_sheet =
94     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
95
96   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
97
98   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
99
100   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
101                                  vs->dict,
102                                  GTK_SELECTION_MULTIPLE, NULL);
103
104   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
105
106   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
107                                  source,
108                                  dest,
109                                  insert_source_row_into_tree_view,
110                                  NULL);
111
112   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  dest);
113
114   scd.tv = GTK_TREE_VIEW (dest);
115   scd.dict = vs->dict;
116   scd.ascending =
117     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sort-cases-radiobutton0"));
118
119   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
120
121
122   switch (response)
123     {
124     case GTK_RESPONSE_OK:
125       {
126         gchar *syntax = generate_syntax (&scd);
127         struct getl_interface *sss = create_syntax_string_source (syntax);
128         execute_syntax (sss);
129
130         g_free (syntax);
131       }
132       break;
133     case PSPPIRE_RESPONSE_PASTE:
134       {
135         gchar *syntax = generate_syntax (&scd);
136
137         struct syntax_editor *se =
138           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
139
140         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
141
142         g_free (syntax);
143       }
144       break;
145     default:
146       break;
147     }
148
149   g_object_unref (xml);
150 }
151