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