3d018d0f2c5c40f6440194f1649625badac663d3
[pspp-builds.git] / src / ui / gui / data-sheet.c
1 /*
2    PSPPIRE --- A Graphical User Interface for PSPP
3    Copyright (C) 2004, 2005, 2006  Free Software Foundation
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA.
19 */
20
21 #include <config.h>
22 #include <gtk/gtk.h>
23 #include <glade/glade.h>
24
25 #include <ctype.h>
26
27 #include <gtksheet/gtksheet.h>
28
29 #include <gtksheet/gsheet-uniform-row.h>
30
31 #include "psppire-dict.h"
32 #include "psppire-data-store.h"
33 #include "helper.h"
34
35 #include <data/value-labels.h>
36 #include <data/case.h>
37 #include <data/data-in.h>
38
39 #include "data-sheet.h"
40
41
42 static gboolean
43 traverse_callback (GtkSheet * sheet,
44                    gint row, gint col,
45                    gint *new_row, gint *new_column
46                    )
47 {
48   gint case_count;
49   gint n_vars;
50
51   PsppireDataStore *data_store =
52     PSPPIRE_DATA_STORE (gtk_sheet_get_model (sheet));
53
54
55   g_assert (data_store);
56
57   n_vars = psppire_dict_get_var_cnt (data_store->dict);
58
59   if ( *new_column >= n_vars )
60     return FALSE;
61
62   case_count = psppire_case_file_get_case_count (data_store->case_file);
63
64   if ( *new_row >= case_count )
65     {
66       gint i;
67
68       for ( i = case_count ; i <= *new_row; ++i )
69         psppire_data_store_insert_new_case (data_store, i);
70
71       return TRUE;
72     }
73
74   return TRUE;
75 }
76
77 extern PsppireDataStore *the_data_store ;
78
79
80 /* Return the width that an  'M' character would occupy when typeset in WIDGET */
81 static guint
82 calc_m_width (GtkWidget *widget, const PangoFontDescription *font_desc)
83 {
84   PangoRectangle rect;
85   PangoLayout *layout ;
86   PangoContext * context;
87
88   context = gtk_widget_create_pango_context (widget);
89   g_assert (context);
90   layout = pango_layout_new (context);
91   g_assert (layout);
92
93   pango_layout_set_text (layout, "M", 1);
94
95   pango_layout_set_font_description (layout, font_desc);
96
97   pango_layout_get_extents (layout, NULL, &rect);
98
99   g_object_unref (G_OBJECT (layout));
100   g_object_unref (G_OBJECT (context));
101
102   return PANGO_PIXELS (rect.width);
103 }
104
105
106
107 void
108 font_change_callback (GObject *obj, gpointer data)
109 {
110   GtkWidget *sheet  = data;
111   PsppireDataStore *ds = PSPPIRE_DATA_STORE (obj);
112
113   ds->width_of_m = calc_m_width (sheet, ds->font_desc);
114 }
115
116
117
118 G_MODULE_EXPORT GtkWidget*
119 psppire_data_sheet_create (gchar *widget_name, gchar *string1, gchar *string2,
120                            gint int1, gint int2)
121 {
122   GtkWidget *sheet;
123
124   sheet = gtk_sheet_new (G_SHEET_ROW (the_data_store),
125                         G_SHEET_COLUMN (the_data_store), "data sheet", 0);
126
127   the_data_store->width_of_m = calc_m_width (sheet, the_data_store->font_desc);
128
129   g_signal_connect (G_OBJECT (sheet), "traverse",
130                     G_CALLBACK (traverse_callback), 0);
131
132
133   g_signal_connect (G_OBJECT (the_data_store), "font-changed",
134                     G_CALLBACK (font_change_callback), sheet);
135
136   gtk_sheet_set_active_cell (GTK_SHEET (sheet), -1, -1);
137
138   gtk_sheet_set_model (GTK_SHEET (sheet), G_SHEET_MODEL (the_data_store));
139
140   gtk_sheet_set_autoscroll (GTK_SHEET (sheet), FALSE);
141
142   gtk_widget_show (sheet);
143
144   return sheet;
145 }