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