Added the Compute dialog box.
[pspp-builds.git] / src / ui / gui / sort-cases-dialog.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2007  Free Software Foundation
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20
21 #include <gtk/gtk.h>
22 #include "sort-cases-dialog.h"
23 #include "helper.h"
24 #include "psppire-dialog.h"
25 #include "data-editor.h"
26 #include <gtksheet/gtksheet.h>
27 #include "psppire-var-store.h"
28 #include "dialog-common.h"
29 #include "dict-display.h"
30
31 #include <language/syntax-string-source.h>
32 #include "syntax-editor.h"
33
34 static void
35 refresh (PsppireDialog *dialog, GtkTreeView *dest)
36 {
37   GtkTreeModel *liststore = gtk_tree_view_get_model (dest);
38
39
40   gtk_list_store_clear (GTK_LIST_STORE (liststore));
41 }
42
43
44 struct sort_cases_dialog
45 {
46   GtkTreeView *tv;
47   PsppireDict *dict;
48   GtkToggleButton *ascending;
49 };
50
51 static char *
52 generate_syntax (const struct sort_cases_dialog *scd)
53 {
54   gchar *text;
55   GString *string = g_string_new ("SORT CASES BY ");
56   gint n_vars = append_variable_names (string,
57                                        scd->dict, GTK_TREE_VIEW (scd->tv));
58
59   if ( n_vars == 0 )
60     g_string_assign (string, "");
61   else
62     {
63       const char up_down =
64         gtk_toggle_button_get_active (scd->ascending) ? 'A' : 'D';
65       g_string_append_printf (string, "(%c)", up_down);
66       g_string_append (string, ".");
67     }
68
69
70   text = string->str;
71
72   g_string_free (string, FALSE);
73
74   return text;
75 }
76
77
78 /* Pops up the Sort Cases dialog box */
79 void
80 sort_cases_dialog (GObject *o, gpointer data)
81 {
82   gint response;
83   struct data_editor *de = data;
84
85   struct sort_cases_dialog scd;
86
87   GladeXML *xml = XML_NEW ("psppire.glade");
88
89   GtkWidget *dialog = get_widget_assert   (xml, "sort-cases-dialog");
90
91
92   GtkWidget *source = get_widget_assert   (xml, "sort-cases-treeview1");
93   GtkWidget *selector = get_widget_assert (xml, "sort-cases-selector");
94   GtkWidget *dest =   get_widget_assert   (xml, "sort-cases-treeview2");
95
96   GtkSheet *var_sheet =
97     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
98
99   PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
100
101   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
102
103   attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
104                                  vs->dict,
105                                  GTK_SELECTION_MULTIPLE, NULL);
106
107   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
108
109   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
110                                  source,
111                                  dest,
112                                  insert_source_row_into_tree_view,
113                                  NULL);
114
115   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  dest);
116
117   scd.tv = GTK_TREE_VIEW (dest);
118   scd.dict = vs->dict;
119   scd.ascending =
120     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sort-cases-radiobutton0"));
121
122   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
123
124
125   switch (response)
126     {
127     case GTK_RESPONSE_OK:
128       {
129         gchar *syntax = generate_syntax (&scd);
130         struct getl_interface *sss = create_syntax_string_source (syntax);
131         execute_syntax (sss);
132
133         g_free (syntax);
134       }
135       break;
136     case PSPPIRE_RESPONSE_PASTE:
137       {
138         gchar *syntax = generate_syntax (&scd);
139
140         struct syntax_editor *se =
141           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
142
143         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
144
145         g_free (syntax);
146       }
147       break;
148     default:
149       break;
150     }
151
152   g_object_unref (xml);
153 }
154