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