Fixed a lot of internationalisation issues.
[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    Written by John Darrington
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA. 
20 */
21
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-variable.h"
33 #include "psppire-data-store.h"
34 #include "helper.h"
35
36 #include <data/value-labels.h>
37 #include <data/case.h>
38 #include <data/data-in.h>
39
40 #include "menu-actions.h"
41 #include "data-sheet.h"
42
43
44
45 extern GladeXML *xml;
46
47
48 static gboolean 
49 traverse_callback (GtkSheet * sheet, 
50                         gint row, gint col, 
51                         gint *new_row, gint *new_column
52                         )
53 {
54   PsppireDataStore *data_store = PSPPIRE_DATA_STORE(gtk_sheet_get_model(sheet));
55
56   const gint n_vars = psppire_dict_get_var_cnt(data_store->dict);
57
58   if ( *new_column >= n_vars ) 
59     return FALSE;
60
61   return TRUE;
62 }
63
64
65
66 /* Callback which occurs when the column title is double clicked */
67 static gboolean
68 click2column(GtkWidget *w, gint col, gpointer data)
69 {
70   gint current_row, current_column;
71   GtkWidget *var_sheet  = get_widget_assert(xml, "variable_sheet");
72
73   select_sheet(PAGE_VAR_SHEET);
74
75   gtk_sheet_get_active_cell(GTK_SHEET(var_sheet), 
76                             &current_row, &current_column);
77
78   gtk_sheet_set_active_cell(GTK_SHEET(var_sheet), col, current_column);
79
80   return FALSE;
81 }
82
83
84 /* Update the data_ref_entry with the reference of the active cell */
85 gint 
86 update_data_ref_entry(const GtkSheet *sheet, gint row, gint col)
87 {
88
89   /* The entry where the reference to the current cell is displayed */
90   GtkEntry *cell_ref_entry;
91
92   PsppireDataStore *data_store = PSPPIRE_DATA_STORE(gtk_sheet_get_model(sheet));
93   if (data_store)
94     {
95       const struct PsppireVariable *pv = 
96         psppire_dict_get_variable(data_store->dict, col);
97
98       gchar *text ;
99       gchar *s ;
100
101       if ( !xml) 
102         return FALSE;
103
104       text = g_strdup_printf("%d: %s", row, 
105                              pv ? psppire_variable_get_name(pv) : "");
106   
107       cell_ref_entry = GTK_ENTRY(get_widget_assert(xml, "cell_ref_entry"));
108
109       s = pspp_locale_to_utf8(text, -1, 0);
110
111       g_free(text);
112
113       gtk_entry_set_text(cell_ref_entry, s);
114
115       g_free(s);
116     }
117
118   return FALSE;
119 }
120
121
122 extern PsppireDataStore *data_store ;
123
124
125 GtkWidget*
126 psppire_data_sheet_create (gchar *widget_name, gchar *string1, gchar *string2,
127                            gint int1, gint int2)
128 {
129   GtkWidget *sheet;
130
131   const gint rows = 10046;
132
133   GObject *row_geometry = g_sheet_uniform_row_new(25, rows); 
134
135   sheet = gtk_sheet_new(G_SHEET_ROW(row_geometry), 
136                         G_SHEET_COLUMN(data_store), "data sheet", 0); 
137
138
139   g_signal_connect (GTK_OBJECT (sheet), "activate",
140                     GTK_SIGNAL_FUNC (update_data_ref_entry),
141                     0);
142
143   g_signal_connect (GTK_OBJECT (sheet), "traverse",
144                     GTK_SIGNAL_FUNC (traverse_callback), 0);
145
146
147   g_signal_connect (GTK_OBJECT (sheet), "double-click-column",
148                     GTK_SIGNAL_FUNC (click2column),
149                     0);
150
151   gtk_sheet_set_active_cell(GTK_SHEET(sheet), -1, -1);
152   gtk_widget_show(sheet);
153
154   return sheet;
155 }