Cleaned up GUI, by objectifying the data editor. Removed a number of global variables.
[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
78
79 /* Update the data_ref_entry with the reference of the active cell */
80 gint
81 update_data_ref_entry(const GtkSheet *sheet, gint row, gint col)
82 {
83  
84   GladeXML *data_editor_xml = NULL; /* FIXME !!!! */
85
86
87   /* The entry where the reference to the current cell is displayed */
88   GtkEntry *cell_ref_entry;
89
90   PsppireDataStore *data_store = PSPPIRE_DATA_STORE(gtk_sheet_get_model(sheet));
91   if (data_store)
92     {
93       const struct variable *pv =
94         psppire_dict_get_variable(data_store->dict, col);
95
96       gchar *text ;
97       gchar *s ;
98
99       if ( !data_editor_xml)
100         return FALSE;
101
102       text = g_strdup_printf("%d: %s", row,
103                              pv ? var_get_name (pv) : "");
104  
105       cell_ref_entry = GTK_ENTRY(get_widget_assert (data_editor_xml,
106                                                    "cell_ref_entry"));
107
108       s = pspp_locale_to_utf8(text, -1, 0);
109
110       g_free(text);
111
112       gtk_entry_set_text(cell_ref_entry, s);
113
114       g_free(s);
115     }
116
117   return FALSE;
118 }
119
120 extern PsppireDataStore *the_data_store ;
121
122
123 /* Return the width that an  'M' character would occupy when typeset in WIDGET */
124 static guint
125 calc_m_width(GtkWidget *widget, const PangoFontDescription *font_desc)
126 {
127   PangoRectangle rect;
128   PangoLayout *layout ;
129   PangoContext * context;
130
131   context = gtk_widget_create_pango_context (widget);
132   g_assert (context);
133   layout = pango_layout_new (context);
134   g_assert (layout);
135
136   pango_layout_set_text (layout, "M", 1);
137  
138   pango_layout_set_font_description (layout, font_desc);
139
140   pango_layout_get_extents (layout, NULL, &rect);
141
142   g_object_unref(G_OBJECT(layout));
143   g_object_unref(G_OBJECT(context));
144
145   return PANGO_PIXELS(rect.width);
146 }
147
148
149
150 void
151 font_change_callback(GObject *obj, gpointer data)
152 {
153   GtkWidget *sheet  = data;
154   PsppireDataStore *ds = PSPPIRE_DATA_STORE(obj);
155
156   ds->width_of_m = calc_m_width(sheet, ds->font_desc);
157 }
158
159 GtkWidget*
160 psppire_data_sheet_create (gchar *widget_name, gchar *string1, gchar *string2,
161                            gint int1, gint int2)
162 {
163   GtkWidget *sheet;
164
165   sheet = gtk_sheet_new(G_SHEET_ROW(the_data_store),
166                         G_SHEET_COLUMN(the_data_store), "data sheet", 0);
167
168   the_data_store->width_of_m = calc_m_width(sheet, the_data_store->font_desc);
169
170   g_signal_connect (G_OBJECT (sheet), "activate",
171                     G_CALLBACK (update_data_ref_entry),
172                     0);
173
174   g_signal_connect (G_OBJECT (sheet), "traverse",
175                     G_CALLBACK (traverse_callback), 0);
176
177
178   g_signal_connect (G_OBJECT (the_data_store), "font-changed",
179                     G_CALLBACK (font_change_callback), sheet);
180
181   gtk_sheet_set_active_cell(GTK_SHEET(sheet), -1, -1);
182
183
184   gtk_sheet_set_model(sheet, G_SHEET_MODEL(the_data_store));
185
186   gtk_widget_show(sheet);
187
188   return sheet;
189 }