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