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