Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / ui / gui / data-sheet.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006  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 #include <config.h>
18 #include <gtk/gtk.h>
19 #include <glade/glade.h>
20
21 #include <ctype.h>
22
23 #include <gtksheet/gtksheet.h>
24
25 #include <gtksheet/gsheet-uniform-row.h>
26
27 #include "psppire-dict.h"
28 #include "psppire-data-store.h"
29 #include "helper.h"
30
31 #include <data/value-labels.h>
32 #include <data/case.h>
33 #include <data/data-in.h>
34
35 #include "data-sheet.h"
36
37
38 static gboolean
39 traverse_callback (GtkSheet * sheet,
40                    gint row, gint col,
41                    gint *new_row, gint *new_column
42                    )
43 {
44   gint case_count;
45   gint n_vars;
46
47   PsppireDataStore *data_store =
48     PSPPIRE_DATA_STORE (gtk_sheet_get_model (sheet));
49
50
51   g_assert (data_store);
52
53   n_vars = psppire_dict_get_var_cnt (data_store->dict);
54
55   if ( *new_column >= n_vars )
56     return FALSE;
57
58   case_count = psppire_case_file_get_case_count (data_store->case_file);
59
60   if ( *new_row >= case_count )
61     {
62       gint i;
63
64       for ( i = case_count ; i <= *new_row; ++i )
65         psppire_data_store_insert_new_case (data_store, i);
66
67       return TRUE;
68     }
69
70   return TRUE;
71 }
72
73 extern PsppireDataStore *the_data_store ;
74
75
76 /* Return the width that an  'M' character would occupy when typeset in WIDGET */
77 static guint
78 calc_m_width (GtkWidget *widget, const PangoFontDescription *font_desc)
79 {
80   PangoRectangle rect;
81   PangoLayout *layout ;
82   PangoContext * context;
83
84   context = gtk_widget_create_pango_context (widget);
85   g_assert (context);
86   layout = pango_layout_new (context);
87   g_assert (layout);
88
89   pango_layout_set_text (layout, "M", 1);
90
91   pango_layout_set_font_description (layout, font_desc);
92
93   pango_layout_get_extents (layout, NULL, &rect);
94
95   g_object_unref (G_OBJECT (layout));
96   g_object_unref (G_OBJECT (context));
97
98   return PANGO_PIXELS (rect.width);
99 }
100
101
102
103 void
104 font_change_callback (GObject *obj, gpointer data)
105 {
106   GtkWidget *sheet  = data;
107   PsppireDataStore *ds = PSPPIRE_DATA_STORE (obj);
108
109   ds->width_of_m = calc_m_width (sheet, ds->font_desc);
110 }
111
112
113
114 G_MODULE_EXPORT GtkWidget*
115 psppire_data_sheet_create (gchar *widget_name, gchar *string1, gchar *string2,
116                            gint int1, gint int2)
117 {
118   GtkWidget *sheet;
119
120   sheet = gtk_sheet_new (G_SHEET_ROW (the_data_store),
121                         G_SHEET_COLUMN (the_data_store), "data sheet", 0);
122
123   the_data_store->width_of_m = calc_m_width (sheet, the_data_store->font_desc);
124
125   g_signal_connect (G_OBJECT (sheet), "traverse",
126                     G_CALLBACK (traverse_callback), 0);
127
128
129   g_signal_connect (G_OBJECT (the_data_store), "font-changed",
130                     G_CALLBACK (font_change_callback), sheet);
131
132   gtk_sheet_set_active_cell (GTK_SHEET (sheet), -1, -1);
133
134   gtk_sheet_set_model (GTK_SHEET (sheet), G_SHEET_MODEL (the_data_store));
135
136   gtk_sheet_set_autoscroll (GTK_SHEET (sheet), FALSE);
137
138   gtk_widget_show (sheet);
139
140   return sheet;
141 }