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