Removed my authorship lines.
[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 <gtk/gtk.h>
22 #include <glade/glade.h>
23
24 #include <ctype.h>
25
26 #include <gtksheet/gtksheet.h>
27
28 #include <gtksheet/gsheet-uniform-row.h>
29
30 #include "psppire-dict.h"
31 #include "psppire-data-store.h"
32 #include "helper.h"
33
34 #include <data/value-labels.h>
35 #include <data/case.h>
36 #include <data/data-in.h>
37
38 #include "menu-actions.h"
39 #include "data-sheet.h"
40
41
42
43 extern GladeXML *xml;
44
45 static gboolean 
46 traverse_callback (GtkSheet * sheet, 
47                    gint row, gint col, 
48                    gint *new_row, gint *new_column
49                    )
50 {
51   gint case_count;
52   PsppireDataStore *data_store = PSPPIRE_DATA_STORE(gtk_sheet_get_model(sheet));
53   const gint 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
74
75 /* Callback which occurs when the column title is double clicked */
76 static gboolean
77 click2column(GtkWidget *w, gint col, gpointer data)
78 {
79   gint current_row, current_column;
80   GtkWidget *var_sheet  = get_widget_assert(xml, "variable_sheet");
81
82   select_sheet(PAGE_VAR_SHEET);
83
84   gtk_sheet_get_active_cell(GTK_SHEET(var_sheet), 
85                             &current_row, &current_column);
86
87   gtk_sheet_set_active_cell(GTK_SHEET(var_sheet), col, current_column);
88
89   return FALSE;
90 }
91
92
93 /* Update the data_ref_entry with the reference of the active cell */
94 gint 
95 update_data_ref_entry(const GtkSheet *sheet, gint row, gint col)
96 {
97
98   /* The entry where the reference to the current cell is displayed */
99   GtkEntry *cell_ref_entry;
100
101   PsppireDataStore *data_store = PSPPIRE_DATA_STORE(gtk_sheet_get_model(sheet));
102   if (data_store)
103     {
104       const struct variable *pv = 
105         psppire_dict_get_variable(data_store->dict, col);
106
107       gchar *text ;
108       gchar *s ;
109
110       if ( !xml) 
111         return FALSE;
112
113       text = g_strdup_printf("%d: %s", row, 
114                              pv ? var_get_name (pv) : "");
115   
116       cell_ref_entry = GTK_ENTRY(get_widget_assert(xml, "cell_ref_entry"));
117
118       s = pspp_locale_to_utf8(text, -1, 0);
119
120       g_free(text);
121
122       gtk_entry_set_text(cell_ref_entry, s);
123
124       g_free(s);
125     }
126
127   return FALSE;
128 }
129
130 extern PsppireDataStore *data_store ;
131
132
133 /* Return the width that an  'M' character would occupy when typeset in WIDGET */
134 static guint 
135 calc_m_width(GtkWidget *widget, const PangoFontDescription *font_desc)
136 {
137   PangoRectangle rect;
138   PangoLayout *layout ;
139   PangoContext * context;
140
141   context = gtk_widget_create_pango_context (widget);
142   g_assert (context);
143   layout = pango_layout_new (context);
144   g_assert (layout);
145
146   pango_layout_set_text (layout, "M", 1);
147   
148   pango_layout_set_font_description (layout, font_desc);
149
150   pango_layout_get_extents (layout, NULL, &rect);
151
152   g_object_unref(G_OBJECT(layout));
153   g_object_unref(G_OBJECT(context));
154
155   return PANGO_PIXELS(rect.width);
156 }
157
158
159
160 void
161 font_change_callback(GObject *obj, gpointer data)
162 {
163   GtkWidget *sheet  = data;
164   PsppireDataStore *ds = PSPPIRE_DATA_STORE(obj);
165
166   ds->width_of_m = calc_m_width(sheet, ds->font_desc);
167 }
168
169 GtkWidget*
170 psppire_data_sheet_create (gchar *widget_name, gchar *string1, gchar *string2,
171                            gint int1, gint int2)
172 {
173   GtkWidget *sheet;
174
175   sheet = gtk_sheet_new(G_SHEET_ROW(data_store), 
176                         G_SHEET_COLUMN(data_store), "data sheet", 0); 
177
178   data_store->width_of_m = calc_m_width(sheet, data_store->font_desc);
179
180   g_signal_connect (G_OBJECT (sheet), "activate",
181                     G_CALLBACK (update_data_ref_entry),
182                     0);
183
184   g_signal_connect (G_OBJECT (sheet), "traverse",
185                     G_CALLBACK (traverse_callback), 0);
186
187
188   g_signal_connect (G_OBJECT (sheet), "double-click-column",
189                     G_CALLBACK (click2column),
190                     0);
191
192   g_signal_connect (G_OBJECT (data_store), "font-changed",
193                     G_CALLBACK (font_change_callback), sheet);
194
195   gtk_sheet_set_active_cell(GTK_SHEET(sheet), -1, -1);
196   gtk_widget_show(sheet);
197
198   return sheet;
199 }