Merge remote branch 'origin/master' into import-gui
[pspp] / src / ui / gui / spreadsheet-test.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 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
18 /* This program is useful for testing the spreadsheet readers */
19
20 #include <config.h>
21
22 #include <gtk/gtk.h>
23
24 #include "psppire-spreadsheet-model.h"
25
26 #include "data/gnumeric-reader.h"
27 #include "data/ods-reader.h"
28 #include "data/spreadsheet-reader.h"
29 #include "data/casereader.h"
30 #include "data/case.h"
31 #include "libpspp/message.h"
32 #include "gl/xalloc.h"
33
34
35 struct xxx
36 {
37   struct spreadsheet *sp;
38   GtkWidget *combo_box;
39 };
40
41
42
43 static void
44 on_clicked (GtkButton *button, struct xxx *stuff)
45 {
46   const struct caseproto *proto;
47   int nvals;
48   struct ccase *c;
49   gint x = gtk_combo_box_get_active (GTK_COMBO_BOX (stuff->combo_box));
50   struct casereader *reader ;
51   struct spreadsheet_read_options opts;
52
53   opts.sheet_index = -1;
54   opts.cell_range = spreadsheet_get_sheet_range (stuff->sp, x);
55   opts.sheet_name = CONST_CAST (char *,
56                                 spreadsheet_get_sheet_name (stuff->sp, x));
57   opts.read_names = TRUE;
58   opts.asw = -1;
59
60   reader = spreadsheet_make_reader (stuff->sp, &opts);
61
62   if (reader == NULL)
63     return;
64
65   proto = casereader_get_proto (reader);
66
67   nvals = caseproto_get_n_widths (proto);
68   
69   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
70     {
71       int i;
72
73       for (i = 0; i < nvals ; ++i)
74       {
75         const int width = caseproto_get_width (proto, i);
76         const union value *val = case_data_idx (c, i);
77         if (0 == width)
78           printf ("%g ", val->f);
79         else
80           {
81             char *ss = xzalloc (width + 1);
82             memcpy (ss, value_str (val, width), width);
83             
84             printf ("%s ", ss);
85             free (ss);
86           }
87       }
88       printf ("\n");
89     }
90
91   casereader_destroy (reader);
92 }
93
94 static void 
95 print_msg (const struct msg *m, void *aux UNUSED)
96 {
97   fprintf (stderr, "%s\n", m->text);
98 }
99
100
101 int
102 main (int argc, char *argv[] )
103 {
104   GtkWidget *window;
105   GtkWidget *hbox;
106   GtkWidget *vbox;
107   GtkWidget *treeview;
108
109   GtkTreeModel *tm;
110   GtkWidget *button;
111   struct xxx stuff;
112
113   gtk_init (&argc, &argv);
114     
115   if ( argc < 2)
116     g_error ("Usage: prog file\n");
117
118   msg_set_handler (print_msg, 0);
119
120   stuff.sp = NULL;
121
122   if (stuff.sp == NULL)
123     stuff.sp = gnumeric_probe (argv[1], false);
124
125   if (stuff.sp == NULL)
126     stuff.sp = ods_probe (argv[1], false);
127   
128   if (stuff.sp == NULL)
129     {
130       g_error ("%s is neither a gnumeric nor a ods file\n", argv[1]);
131       return 0;
132     }
133
134   tm = psppire_spreadsheet_model_new (stuff.sp);
135   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
136   hbox = gtk_hbox_new (FALSE, 5);
137   vbox = gtk_vbox_new (FALSE, 5);
138
139   button = gtk_button_new_with_label ("Test reader");
140   g_signal_connect (button, "clicked", G_CALLBACK (on_clicked), &stuff);
141    
142   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
143   
144   stuff.combo_box = gtk_combo_box_new();
145
146   {
147     GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
148     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (stuff.combo_box), renderer, TRUE);
149     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (stuff.combo_box), renderer,
150                                     "text", 0,
151                                     NULL);
152   }
153
154   gtk_combo_box_set_model (GTK_COMBO_BOX (stuff.combo_box), tm);
155
156   gtk_combo_box_set_active (GTK_COMBO_BOX (stuff.combo_box), 0);
157
158   treeview = gtk_tree_view_new_with_model (tm);
159
160   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
161                                                0, "sheet name",
162                                                gtk_cell_renderer_text_new (),
163                                                "text", 0,
164                                                NULL);
165
166
167   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
168                                                1, "range",
169                                                gtk_cell_renderer_text_new (),
170                                                "text", 1,
171                                                NULL);
172
173
174   gtk_box_pack_start (GTK_BOX (hbox), treeview, TRUE, TRUE, 5);
175
176   gtk_box_pack_start (GTK_BOX (vbox), stuff.combo_box, FALSE, FALSE, 5);
177   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 5);
178   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 5);
179
180   gtk_container_add (GTK_CONTAINER (window), hbox);
181
182   g_signal_connect (window, "destroy", gtk_main_quit, 0);
183
184   gtk_widget_show_all (window);
185
186   gtk_main ();
187
188   spreadsheet_destroy (stuff.sp);
189
190   return 0;
191 }