Internationalisation.
[pspp-builds.git] / src / ui / gui / psppire-data-store.c
index 0cb719046eab964b9a78dc584c628735019979da..df36d0832e41ff600b966ae76186d86b192fdf4e 100644 (file)
 #include <config.h>
 #include <string.h>
 #include <stdlib.h>
+#include <gettext.h>
+#define _(msgid) gettext (msgid)
+#define N_(msgid) msgid
+
+
 
 #include <gtksheet/gtksheet.h>
 #include <gtksheet/gsheetmodel.h>
@@ -29,6 +34,7 @@
 
 #include "psppire-variable.h"
 #include "psppire-data-store.h"
+#include "helper.h"
 
 #include <data/dictionary.h>
 #include <data/missing-values.h>
@@ -38,8 +44,6 @@
 #include <data/file-handle-def.h>
 #include <data/sys-file-writer.h>
 
-#define _(A) A
-#define N_(A) A
 
 
 static void psppire_data_store_init            (PsppireDataStore      *data_store);
@@ -48,7 +52,7 @@ static void psppire_data_store_sheet_model_init (GSheetModelIface *iface);
 static void psppire_data_store_sheet_column_init (GSheetColumnIface *iface);
 static void psppire_data_store_finalize        (GObject           *object);
 
-static const gchar *const psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column);
+static const gchar *psppire_data_store_get_string(GSheetModel *sheet_model, gint row, gint column);
 
 static gboolean psppire_data_store_set_string(GSheetModel *model, 
                                          const gchar *text, gint row, gint column);
@@ -59,7 +63,6 @@ static gboolean psppire_data_store_clear_datum(GSheetModel *model,
 
 #define MIN_COLUMNS 10
 
-#define max(A,B) ((A>B)?A:B)
 
 static GObjectClass *parent_class = NULL;
 
@@ -127,6 +130,24 @@ psppire_data_store_class_init (PsppireDataStoreClass *class)
 }
 
 
+
+static gint
+psppire_data_store_get_var_count (const GSheetModel *model)
+{
+  const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
+  
+  return psppire_dict_get_var_cnt(store->dict);
+}
+
+static gint
+psppire_data_store_get_case_count (const GSheetModel *model)
+{
+  const PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
+
+  return psppire_case_array_get_n_cases(store->cases);
+}
+
+
 static void
 psppire_data_store_init (PsppireDataStore *data_store)
 {
@@ -147,6 +168,7 @@ psppire_data_store_get_font_desc(GSheetModel *model,
 static void
 psppire_data_store_sheet_model_init (GSheetModelIface *iface)
 {
+  iface->free_strings = TRUE;
   iface->get_string = psppire_data_store_get_string;
   iface->set_string = psppire_data_store_set_string;
   iface->clear_datum = psppire_data_store_clear_datum;
@@ -156,6 +178,8 @@ psppire_data_store_sheet_model_init (GSheetModelIface *iface)
   iface->get_background = NULL;
   iface->get_font_desc = psppire_data_store_get_font_desc;
   iface->get_cell_border = NULL;
+  iface->get_column_count = psppire_data_store_get_var_count;
+  iface->get_row_count = psppire_data_store_get_case_count;
 }
 
 static
@@ -165,7 +189,6 @@ gboolean always_true()
 }
 
 
-
 static void
 delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
 {
@@ -185,14 +208,15 @@ static void
 insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
 {
   PsppireDataStore *store  ;
+
   g_return_if_fail (data);
 
   store  = PSPPIRE_DATA_STORE(data);
   
   g_sheet_model_range_changed (G_SHEET_MODEL(store),
-                                casenum, -1,
-                                psppire_case_array_get_n_cases(store->cases),
-                                -1);
+                              casenum, -1,
+                              psppire_case_array_get_n_cases(store->cases),
+                              -1);
 }
 
 
@@ -212,7 +236,7 @@ changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
 
 
 static void
-delete_variables_callback(GtkWidget *w, gint var_num, gint n_vars, gpointer data)
+delete_variables_callback(GObject *obj, gint var_num, gint n_vars, gpointer data)
 {
   PsppireDataStore *store ;
 
@@ -222,11 +246,13 @@ delete_variables_callback(GtkWidget *w, gint var_num, gint n_vars, gpointer data
 
   g_sheet_column_columns_deleted(G_SHEET_COLUMN(store),
                                   var_num, n_vars);
+
+  g_sheet_model_columns_deleted (G_SHEET_MODEL(store), var_num, n_vars);
 }
 
 
 static void
-insert_variable_callback(GtkWidget *w, gint var_num, gpointer data)
+insert_variable_callback(GObject *obj, gint var_num, gpointer data)
 {
   PsppireDataStore *store;
 
@@ -244,6 +270,7 @@ insert_variable_callback(GtkWidget *w, gint var_num, gpointer data)
   psppire_case_array_resize(store->cases, 
                         dict_get_next_value_idx (store->dict->dict));
 
+  g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
 }
 
 
@@ -274,7 +301,6 @@ psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
   g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback), 
                   retval);
 
-
   psppire_data_store_set_dictionary(retval, dict);
 
 
@@ -312,7 +338,6 @@ psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dic
                   G_CALLBACK(delete_variables_callback), 
                   data_store);
 
-
   /* The entire model has changed */
   g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
 }
@@ -326,10 +351,10 @@ psppire_data_store_finalize (GObject *object)
 }
 
 
-static const gchar *const 
+static const gchar *
 psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
 {
-
+  const char *text;
   const struct fmt_spec *fp ;
   const struct PsppireVariable *pv ;
   const union value *v ;
@@ -358,40 +383,28 @@ psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
       const gchar *label;
       if ( (label = val_labs_find(vl, *v)) )
        {
-         return label;
+         return pspp_locale_to_utf8(label, -1, 0);
        }
     }
 
   fp = psppire_variable_get_write_spec(pv);
 
-  s = g_string_sized_new (fp->w);
+  s = g_string_sized_new (fp->w + 1);
+  g_string_set_size(s, fp->w);
+  
+  memset(s->str, 0, fp->w);
+
+  g_assert(fp->w == s->len);
     
   /* Converts binary value V into printable form in the exactly
      FP->W character in buffer S according to format specification
      FP.  No null terminator is appended to the buffer.  */
   data_out (s->str, fp, v);
 
-  return g_string_free(s, FALSE);
-#if 0
-  {
-    static gchar buf[255];
-    GError *err = NULL;
-    gchar *text = g_locale_to_utf8(s, fp->w, 0, 0, &err);
-    if ( !err ) 
-      { 
-       g_snprintf(buf, 255, text);
-       g_free(text);
-      }
-    else
-      {
-       g_warning("Cannot convert string \"%s\" to utf-8: %s\n", s, err->message);
-       g_error_free(err);
-       return NULL;
-      }
+  text = pspp_locale_to_utf8(s->str, fp->w, 0);
+  g_string_free(s, TRUE);
 
-  return buf ;
-  }
-#endif
+  return text;
 }
 
 
@@ -458,11 +471,12 @@ psppire_data_store_set_string(GSheetModel *model,
   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
 
   const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
+  g_return_val_if_fail(pv, FALSE);
 
   for(r = psppire_case_array_get_n_cases(store->cases) ; r <= row ; ++r ) 
     {
       gint c;
-      psppire_case_array_insert_case(store->cases, r);
+      psppire_case_array_insert_case(store->cases, r, 0, 0);
 
       for (c = 0 ; c < psppire_dict_get_var_cnt(store->dict); ++c ) 
        psppire_data_store_clear_datum(model, r, c);
@@ -558,13 +572,13 @@ geometry_get_column_count(const GSheetColumn *geom)
 {
   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
 
-  return max(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
+  return MAX(MIN_COLUMNS, psppire_dict_get_var_cnt(ds->dict));
 }
 
 /* Return the width that an  'M' character would occupy when typeset at
    row, col */
 static guint 
-M_width(GtkSheet *sheet, gint row, gint col)
+M_width(const GtkSheet *sheet, gint row, gint col)
 {
   GtkSheetCellAttr attributes;
   PangoRectangle rect;
@@ -648,20 +662,23 @@ geometry_get_justification(const GSheetColumn *geom, gint unit)
 }
 
 
-static const gchar null_var_name[]=_("var");
+static const gchar null_var_name[]=N_("var");
  
 static const gchar *
 geometry_get_button_label(const GSheetColumn *geom, gint unit)
 {
+  const gchar *text;
   struct PsppireVariable *pv ;
   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
 
   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
-    return null_var_name;
+    return g_locale_to_utf8(null_var_name, -1, 0, 0, 0);
 
   pv = psppire_dict_get_variable(ds->dict, unit);
 
-  return psppire_variable_get_name(pv);
+  text =  pspp_locale_to_utf8(psppire_variable_get_name(pv), -1, 0);
+
+  return text;
 }