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