Made a start at canonicalising the interface
[pspp] / src / ui / gui / text-data-import-dialog.h
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2010, 2011, 2013  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 #ifndef TEXT_DATA_IMPORT_DIALOG_H
18 #define TEXT_DATA_IMPORT_DIALOG_H
19
20 #include <glib-object.h>
21 #include "ui/gui/psppire-data-window.h"
22 #include "data/spreadsheet-reader.h"
23
24 #include "libpspp/str.h"
25
26 enum file_type
27   {
28     FTYPE_TEXT,
29     FTYPE_GNUMERIC,
30     FTYPE_ODS
31   };
32
33 /* The file to be imported. */
34 struct file
35   {
36     char *file_name;        /* File name. */
37
38     enum file_type type;
39
40     /* Relevant only for text files */
41
42     gchar *encoding;        /* Encoding. */
43     unsigned long int total_lines; /* Number of lines in file. */
44     bool total_is_exact;    /* Is total_lines exact (or an estimate)? */
45
46     /* The first several lines of the file. */
47     struct string *lines;
48     size_t line_cnt;
49   };
50
51 /* The main body of the GTK+ assistant and related data. */
52 struct assistant
53   {
54     GtkBuilder *builder;
55     GtkAssistant *assistant;
56     GMainLoop *main_loop;
57     GtkWidget *paste_button;
58     GtkWidget *reset_button;
59     int response;
60     int watch_cursor;
61
62     GtkCellRenderer *prop_renderer;
63     GtkCellRenderer *fixed_renderer;
64   };
65
66
67 /* The sheet_spec page of the assistant (only relevant for spreadsheet imports). */
68 struct sheet_spec_page
69   {
70     GtkWidget *page;
71     struct casereader *reader;
72     struct dictionary *dict;
73     struct spreadsheet *spreadsheet;
74     
75     struct spreadsheet_read_info sri;
76     struct spreadsheet_read_options opts;
77   };
78
79
80 /* The introduction page of the assistant. */
81 struct intro_page
82   {
83     GtkWidget *page;
84     GtkWidget *all_cases_button;
85     GtkWidget *n_cases_button;
86     GtkWidget *n_cases_spin;
87     GtkWidget *percent_button;
88     GtkWidget *percent_spin;
89   };
90
91 /* Page where the user chooses the first line of data. */
92 struct first_line_page
93   {
94     int skip_lines;    /* Number of initial lines to skip? */
95     bool variable_names; /* Variable names above first line of data? */
96
97     GtkWidget *page;
98     GtkTreeView *tree_view;
99     GtkWidget *variable_names_cb;
100   };
101
102
103 /* Page where the user chooses field separators. */
104 struct separators_page
105   {
106     /* How to break lines into columns. */
107     struct string separators;   /* Field separators. */
108     struct string quotes;       /* Quote characters. */
109     bool escape;                /* Doubled quotes yield a quote mark? */
110
111     /* The columns produced thereby. */
112     struct column *columns;     /* Information about each column. */
113     size_t column_cnt;          /* Number of columns. */
114
115     GtkWidget *page;
116     GtkWidget *custom_cb;
117     GtkWidget *custom_entry;
118     GtkWidget *quote_cb;
119     GtkWidget *quote_combo;
120     GtkEntry *quote_entry;
121     GtkWidget *escape_cb;
122     GtkTreeView *fields_tree_view;
123   };
124
125 /* Page where the user verifies and adjusts input formats. */
126 struct formats_page
127   {
128     struct dictionary *dict;
129
130     GtkWidget *page;
131     GtkTreeView *data_tree_view;
132     PsppireDict *psppire_dict;
133     struct variable **modified_vars;
134     size_t modified_var_cnt;
135   };
136
137
138 struct import_assistant
139   {
140     struct file file;
141     struct assistant asst;
142     struct intro_page intro;
143     struct sheet_spec_page sheet_spec;
144     struct first_line_page first_line;
145     struct separators_page separators;
146     struct formats_page formats;
147   };
148
149
150
151
152 struct column
153   {
154     /* Variable name for this column.  This is the variable name
155        used on the separators page; it can be overridden by the
156        user on the formats page. */
157     char *name;
158
159     /* Maximum length of any row in this column. */
160     size_t width;
161
162     /* Contents of this column: contents[row] is the contents for
163        the given row.
164
165        A null substring indicates a missing column for that row
166        (because the line contains an insufficient number of
167        separators).
168
169        contents[] elements may be substrings of the lines[]
170        strings that represent the whole lines of the file, to
171        save memory.  Other elements are dynamically allocated
172        with ss_alloc_substring. */
173     struct substring *contents;
174   };
175
176
177 GtkWidget * add_page_to_assistant (struct import_assistant *ia,
178                                    GtkWidget *page, GtkAssistantPageType type);
179
180 void text_data_import_assistant (PsppireDataWindow *);
181
182 /* FIXME: Should this be private to first line page ? */
183 void make_tree_view (const struct import_assistant *ia,
184                             size_t first_line,
185                             GtkTreeView **tree_view);
186
187 gint get_monospace_width (GtkTreeView *, GtkCellRenderer *,
188                                  size_t char_cnt);
189 gint get_string_width (GtkTreeView *, GtkCellRenderer *,
190                               const char *string);
191
192
193
194 void push_watch_cursor (struct import_assistant *);
195 void pop_watch_cursor (struct import_assistant *);
196
197
198 GtkTreeView *create_data_tree_view (bool input, GtkContainer *parent,
199                                            struct import_assistant *);
200
201 GtkTreeViewColumn *make_data_column (struct import_assistant *,
202                                             GtkTreeView *, bool input,
203                                             gint column_idx);
204
205
206 void  update_assistant (struct import_assistant *ia);
207
208
209 bool init_file (struct import_assistant *ia, GtkWindow *parent_window);
210 void destroy_file (struct import_assistant *ia);
211
212
213 void init_intro_page (struct import_assistant *);
214 void reset_intro_page (struct import_assistant *);
215
216 void init_sheet_spec_page (struct import_assistant *);
217 void prepare_sheet_spec_page (struct import_assistant *ia);
218 void reset_sheet_spec_page (struct import_assistant *);
219 void post_sheet_spec_page (struct import_assistant *ia);
220
221 void init_first_line_page (struct import_assistant *ia);
222 void prepare_first_line_page (struct import_assistant *ia);
223 void reset_first_line_page (struct import_assistant *);
224
225 void init_separators_page (struct import_assistant *ia);
226 void prepare_separators_page (struct import_assistant *ia);
227 void reset_separators_page (struct import_assistant *);
228 void destroy_separators_page (struct import_assistant *ia);
229
230 void init_formats_page (struct import_assistant *ia);
231 void prepare_formats_page (struct import_assistant *ia);
232 void reset_formats_page (struct import_assistant *);
233 void destroy_formats_page (struct import_assistant *ia);
234
235 void init_assistant (struct import_assistant *, GtkWindow *);
236 void destroy_assistant (struct import_assistant *);
237
238
239 #endif