From: John Darrington Date: Fri, 27 Mar 2009 02:46:21 +0000 (+0900) Subject: Redesign the character re-encoding code. X-Git-Tag: v0.7.3~200 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dd5535725f86158e55b3fc263c17058005b0c0a5;p=pspp-builds.git Redesign the character re-encoding code. Remove the statically allocated convertors, instead, assume that the encoding of variables, data and associated metadata is stored in the dictionary. --- diff --git a/src/data/gnumeric-reader.c b/src/data/gnumeric-reader.c index 5a0c75ed..00a07d2f 100644 --- a/src/data/gnumeric-reader.c +++ b/src/data/gnumeric-reader.c @@ -314,11 +314,10 @@ static void convert_xml_string_to_value (struct ccase *c, const struct variable *var, const xmlChar *xv) { - char *text; int n_bytes = 0; union value *v = case_data_rw (c, var); - text = recode_string (CONV_UTF8_TO_PSPP, (const char *) xv, -1); + const char *text = (const char *) xv; if ( text) n_bytes = MIN (var_get_width (var), strlen (text)); @@ -335,8 +334,6 @@ convert_xml_string_to_value (struct ccase *c, const struct variable *var, if ( errno != 0 || endptr == text) v->f = SYSMIS; } - - free (text); } struct var_spec @@ -459,10 +456,8 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) if ( r->node_type == XML_READER_TYPE_TEXT ) { - char *text ; xmlChar *value = xmlTextReaderValue (r->xtr); - - text = recode_string (CONV_UTF8_TO_PSPP, (const char *) value, -1); + const char *text = (const char *) value; if ( r->row < r->start_row) { @@ -481,7 +476,6 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict) } free (value); - free (text); } else if ( r->node_type == XML_READER_TYPE_ELEMENT && r->state == STATE_CELL) diff --git a/src/libpspp/i18n.c b/src/libpspp/i18n.c index 617f7968..674a9a27 100644 --- a/src/libpspp/i18n.c +++ b/src/libpspp/i18n.c @@ -34,12 +34,7 @@ #include #endif - -static char *locale; -static char *charset; - - -static iconv_t convertor[n_CONV]; +static char *default_encoding; /* A wrapper around iconv_open */ @@ -66,7 +61,8 @@ create_iconv (const char* tocode, const char* fromcode) The returned string must be freed when no longer required. */ char * -recode_string (enum conv_id how, const char *text, int length) +recode_string (const char *to, const char *from, + const char *text, int length) { char *outbuf = 0; size_t outbufferlength; @@ -74,6 +70,7 @@ recode_string (enum conv_id how, const char *text, int length) char *op ; size_t inbytes = 0; size_t outbytes ; + iconv_t conv ; /* FIXME: Need to ensure that this char is valid in the target encoding */ const char fallbackchar = '?'; @@ -84,9 +81,17 @@ recode_string (enum conv_id how, const char *text, int length) if ( length == -1 ) length = strlen(text); - assert (how < n_CONV); - if (convertor[how] == (iconv_t) -1) + if (to == NULL) + to = default_encoding; + + if (from == NULL) + from = default_encoding; + + + fprintf (stderr, "from: %s; to %s\n", from, to); + + if ( 0 == strcmp (to, from)) return xstrndup (text, length); for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 ) @@ -99,9 +104,12 @@ recode_string (enum conv_id how, const char *text, int length) outbytes = outbufferlength; inbytes = length; + + conv = create_iconv (to, from); + do { const char *ip = text; - result = iconv (convertor[how], (ICONV_CONST char **) &text, &inbytes, + result = iconv (conv, (ICONV_CONST char **) &text, &inbytes, &op, &outbytes); if ( -1 == result ) @@ -138,6 +146,9 @@ recode_string (enum conv_id how, const char *text, int length) } } while ( -1 == result ); + + iconv_close (conv); + if (outbytes == 0 ) { char *const oldaddr = outbuf; @@ -152,44 +163,21 @@ recode_string (enum conv_id how, const char *text, int length) } -/* Returns the current PSPP locale */ -const char * -get_pspp_locale (void) -{ - assert (locale); - return locale; -} void i18n_init (void) { - assert (!locale) ; - locale = strdup (setlocale (LC_CTYPE, NULL)); - - setlocale (LC_CTYPE, locale); - - free (charset); - charset = strdup (locale_charset ()); - - convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset); - convertor[CONV_UTF8_TO_PSPP] = create_iconv (charset, "UTF-8"); + free (default_encoding); + default_encoding = strdup (locale_charset ()); } void i18n_done (void) { - int i; - free (locale); - locale = 0; - - for(i = 0 ; i < n_CONV; ++i ) - { - if ( (iconv_t) -1 == convertor[i] ) - continue; - iconv_close (convertor[i]); - } + free (default_encoding); + default_encoding = NULL; } diff --git a/src/libpspp/i18n.h b/src/libpspp/i18n.h index d0d65cf1..c167e1f9 100644 --- a/src/libpspp/i18n.h +++ b/src/libpspp/i18n.h @@ -17,22 +17,14 @@ #ifndef I18N_H #define I18N_H -const char * get_pspp_locale (void); -void set_pspp_locale (const char *locale); -const char * get_pspp_charset (void); - void i18n_done (void); void i18n_init (void); -enum conv_id - { - CONV_PSPP_TO_UTF8, - CONV_UTF8_TO_PSPP, - n_CONV - }; +#define UTF8 "UTF-8" -char * recode_string (enum conv_id how, const char *text, int len); +char * recode_string (const char *to, const char *from, + const char *text, int len); /* Return the decimal separator according to the diff --git a/src/ui/gui/compute-dialog.c b/src/ui/gui/compute-dialog.c index 7703640c..a42ce4fe 100644 --- a/src/ui/gui/compute-dialog.c +++ b/src/ui/gui/compute-dialog.c @@ -24,6 +24,7 @@ #include "psppire-var-store.h" #include "psppire-selector.h" #include "dialog-common.h" +#include #include #include @@ -631,7 +632,9 @@ insert_source_row_into_text_view (GtkTreeIter iter, gtk_tree_path_free (path); - name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL); + name = recode_string (UTF8, psppire_dict_encoding (dict), + var_get_name (var), + -1); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dest)); diff --git a/src/ui/gui/dialog-common.c b/src/ui/gui/dialog-common.c index 0aab294e..8d03bed1 100644 --- a/src/ui/gui/dialog-common.c +++ b/src/ui/gui/dialog-common.c @@ -16,6 +16,7 @@ #include +#include #include "dialog-common.h" #include "psppire-var-ptr.h" @@ -118,7 +119,8 @@ cell_var_name (GtkTreeViewColumn *tree_column, var = get_selected_variable (tree_model, iter, dict); - name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL); + name = recode_string (UTF8, psppire_dict_encoding (dict), + var_get_name (var), -1); g_object_set (cell, "text", name, NULL); g_free (name); } diff --git a/src/ui/gui/dict-display.c b/src/ui/gui/dict-display.c index 2123c3c5..d6b1bcd5 100644 --- a/src/ui/gui/dict-display.c +++ b/src/ui/gui/dict-display.c @@ -23,6 +23,7 @@ #include "dict-display.h" #include "psppire-dict.h" +#include #include "helper.h" #include #include @@ -80,7 +81,8 @@ insert_source_row_into_entry (GtkTreeIter iter, gtk_tree_path_free (path); - name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL); + name = recode_string (UTF8, psppire_dict_encoding (PSPPIRE_DICT (dict)), + var_get_name (var), -1); gtk_entry_set_text (GTK_ENTRY (dest), name); g_free (name); } @@ -142,7 +144,8 @@ is_currently_in_entry (GtkTreeModel *model, GtkTreeIter *iter, gtk_tree_path_free (path); - name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL); + name = recode_string (UTF8, psppire_dict_encoding (PSPPIRE_DICT (dict)), + var_get_name (var), -1); result = ( 0 == strcmp (text, name)); g_free (name); diff --git a/src/ui/gui/helper.c b/src/ui/gui/helper.c index 9ac9fde6..6d90cfc4 100644 --- a/src/ui/gui/helper.c +++ b/src/ui/gui/helper.c @@ -152,20 +152,6 @@ get_widget_assert (GtkBuilder *builder, const gchar *name) return GTK_WIDGET (get_object_assert (builder, name, GTK_TYPE_WIDGET)); } -/* Converts a string in the pspp locale to utf-8. - The return value must be freed when no longer required*/ -gchar * -pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err) -{ - return recode_string (CONV_PSPP_TO_UTF8, text, len); -} - -gchar * -utf8_to_pspp_locale (const gchar *text, gssize len, GError **err) -{ - return recode_string (CONV_UTF8_TO_PSPP, text, len); -} - /* This function must be used whenever a filename generated by glib, (eg, from gtk_file_chooser_get_filename) and passed to the C library, (eg through a pspp syntax string). diff --git a/src/ui/gui/helper.h b/src/ui/gui/helper.h index 30792faf..6bd610e7 100644 --- a/src/ui/gui/helper.h +++ b/src/ui/gui/helper.h @@ -43,14 +43,9 @@ GObject *get_object_assert (GtkBuilder *builder, const gchar *name, GType type); GtkAction * get_action_assert (GtkBuilder *builder, const gchar *name); GtkWidget * get_widget_assert (GtkBuilder *builder, const gchar *name); -/* Converts a string in the pspp locale to utf-8 */ -gchar * pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err); -gchar * utf8_to_pspp_locale (const gchar *text, gssize len, GError **err); - gchar * convert_glib_filename_to_system_filename (const gchar *fname, GError **err); - void connect_help (GtkBuilder *); void reference_manual (GtkMenuItem *, gpointer); diff --git a/src/ui/gui/psppire-data-editor.c b/src/ui/gui/psppire-data-editor.c index ab3b2128..1070f5c2 100644 --- a/src/ui/gui/psppire-data-editor.c +++ b/src/ui/gui/psppire-data-editor.c @@ -24,6 +24,7 @@ #include #include "psppire-data-store.h" #include +#include #include "helper.h" #include @@ -743,7 +744,9 @@ update_data_ref_entry (const PsppireSheet *sheet, gchar *text = g_strdup_printf ("%d: %s", row + FIRST_CASE_NUMBER, var_get_name (var)); - gchar *s = pspp_locale_to_utf8 (text, -1, 0); + gchar *s = recode_string (UTF8, + psppire_dict_encoding (data_store->dict), + text, -1); g_free (text); diff --git a/src/ui/gui/psppire-data-store.c b/src/ui/gui/psppire-data-store.c index 45104f5c..45fa8248 100644 --- a/src/ui/gui/psppire-data-store.c +++ b/src/ui/gui/psppire-data-store.c @@ -31,6 +31,7 @@ #include #include "psppire-data-store.h" +#include #include "helper.h" #include @@ -598,7 +599,8 @@ psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column) if (label) { free (v); - return pspp_locale_to_utf8 (label, -1, 0); + return recode_string (UTF8, psppire_dict_encoding (store->dict), + label, -1); } } @@ -616,7 +618,8 @@ psppire_data_store_get_string (PsppireDataStore *store, glong row, glong column) FP. No null terminator is appended to the buffer. */ data_out (v, fp, s->str); - text = pspp_locale_to_utf8 (s->str, fp->w, 0); + text = recode_string (UTF8, psppire_dict_encoding (store->dict), + s->str, fp->w); g_string_free (s, TRUE); g_strchomp (text); @@ -673,7 +676,7 @@ psppire_data_store_set_string (PsppireDataStore *store, if (row == n_cases) psppire_data_store_insert_new_case (store, row); - s = utf8_to_pspp_locale (text, -1, NULL); + s = recode_string (psppire_dict_encoding (store->dict), UTF8, text, -1); psppire_data_store_data_in (store, row, var_get_case_index (pv), ss_cstr (s), @@ -749,9 +752,11 @@ static const gchar null_var_name[]=N_("var"); static gchar * get_row_button_label (const PsppireSheetModel *model, gint unit) { + PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); gchar *s = g_strdup_printf (_("%d"), unit + FIRST_CASE_NUMBER); - gchar *text = pspp_locale_to_utf8 (s, -1, 0); + gchar *text = recode_string (UTF8, psppire_dict_encoding (ds->dict), + s, -1); g_free (s); @@ -787,7 +792,8 @@ get_column_subtitle (const PsppireSheetModel *model, gint col) if ( ! var_has_label (v)) return NULL; - text = pspp_locale_to_utf8 (var_get_label (v), -1, 0); + text = recode_string (UTF8, psppire_dict_encoding (ds->dict), + var_get_label (v), -1); return text; } @@ -804,7 +810,8 @@ get_column_button_label (const PsppireSheetModel *model, gint col) pv = psppire_dict_get_variable (ds->dict, col); - text = pspp_locale_to_utf8 (var_get_name (pv), -1, 0); + text = recode_string (UTF8, psppire_dict_encoding (ds->dict), + var_get_name (pv), -1); return text; } diff --git a/src/ui/gui/psppire-dict.c b/src/ui/gui/psppire-dict.c index 564257f4..c4f30783 100644 --- a/src/ui/gui/psppire-dict.c +++ b/src/ui/gui/psppire-dict.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "helper.h" #include "message-dialog.h" @@ -752,10 +753,11 @@ tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter, { case DICT_TVM_COL_NAME: { - gchar *name = pspp_locale_to_utf8(var_get_name (var), -1, NULL); - g_value_init (value, G_TYPE_STRING); - g_value_set_string (value, name); - g_free (name); + gchar *name = recode_string (UTF8, psppire_dict_encoding (dict), + var_get_name (var), -1); + g_value_init (value, G_TYPE_STRING); + g_value_set_string (value, name); + g_free (name); } break; case DICT_TVM_COL_VAR: @@ -859,3 +861,12 @@ psppire_dict_dump (const PsppireDict *dict) } } #endif + + + + +const gchar * +psppire_dict_encoding (const PsppireDict *dict) +{ + return NULL; +} diff --git a/src/ui/gui/psppire-dict.h b/src/ui/gui/psppire-dict.h index 3fd73f9a..54f3e39a 100644 --- a/src/ui/gui/psppire-dict.h +++ b/src/ui/gui/psppire-dict.h @@ -109,6 +109,8 @@ struct variable * psppire_dict_get_weight_variable (const PsppireDict *); void psppire_dict_dump (const PsppireDict *); #endif +const gchar *psppire_dict_encoding (const PsppireDict *); + G_END_DECLS #endif /* __PSPPIRE_DICT_H__ */ diff --git a/src/ui/gui/psppire-dictview.c b/src/ui/gui/psppire-dictview.c index 863365f9..37539694 100644 --- a/src/ui/gui/psppire-dictview.c +++ b/src/ui/gui/psppire-dictview.c @@ -21,6 +21,7 @@ #include "psppire-dict.h" #include "psppire-conf.h" #include +#include #include "helper.h" #include @@ -274,11 +275,15 @@ dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter, ) { *model = top_model; - *iter = *top_iter; + + if ( iter) + *iter = *top_iter; while ( ! PSPPIRE_IS_DICT (*model)) { - GtkTreeIter parent_iter = *iter; + GtkTreeIter parent_iter; + if (iter) + parent_iter = *iter; if ( GTK_IS_TREE_MODEL_FILTER (*model)) { @@ -286,9 +291,10 @@ dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter, *model = gtk_tree_model_filter_get_model (parent_model); - gtk_tree_model_filter_convert_iter_to_child_iter (parent_model, - iter, - &parent_iter); + if (iter) + gtk_tree_model_filter_convert_iter_to_child_iter (parent_model, + iter, + &parent_iter); } else if (GTK_IS_TREE_MODEL_SORT (*model)) { @@ -296,9 +302,10 @@ dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter, *model = gtk_tree_model_sort_get_model (parent_model); - gtk_tree_model_sort_convert_iter_to_child_iter (parent_model, - iter, - &parent_iter); + if (iter) + gtk_tree_model_sort_convert_iter_to_child_iter (parent_model, + iter, + &parent_iter); } } } @@ -318,11 +325,11 @@ var_description_cell_data_func (GtkTreeViewColumn *col, struct variable *var; GtkTreeIter iter; GtkTreeModel *model; - + PsppireDict *dict; dv_get_base_model (top_model, top_iter, &model, &iter); - g_assert (PSPPIRE_IS_DICT (model)); + dict = PSPPIRE_DICT (model); gtk_tree_model_get (model, &iter, DICT_TVM_COL_VAR, &var, -1); @@ -333,7 +340,8 @@ var_description_cell_data_func (GtkTreeViewColumn *col, "%s", var_get_label (var)); - char *utf8 = pspp_locale_to_utf8 (text, -1, NULL); + char *utf8 = recode_string (UTF8, psppire_dict_encoding (dict), + text, -1); g_free (text); g_object_set (cell, "markup", utf8, NULL); @@ -341,7 +349,8 @@ var_description_cell_data_func (GtkTreeViewColumn *col, } else { - char *name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL); + char *name = recode_string (UTF8, psppire_dict_encoding (dict), + var_get_name (var), -1); g_object_set (cell, "text", name, NULL); g_free (name); } @@ -406,7 +415,6 @@ set_tooltip_for_variable (GtkTreeView *treeview, struct variable *var = NULL; gboolean ok; - gtk_tree_view_convert_widget_to_bin_window_coords (treeview, x, y, &bx, &by); @@ -416,7 +424,6 @@ set_tooltip_for_variable (GtkTreeView *treeview, tree_model = gtk_tree_view_get_model (treeview); - gtk_tree_view_set_tooltip_row (treeview, tooltip, path); ok = gtk_tree_model_get_iter (tree_model, &iter, path); @@ -433,11 +440,18 @@ set_tooltip_for_variable (GtkTreeView *treeview, { gchar *tip ; + GtkTreeModel *m; + PsppireDict *dict; + + dv_get_base_model (tree_model, NULL, &m, NULL); + dict = PSPPIRE_DICT (m); if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels ) - tip = pspp_locale_to_utf8 (var_get_name (var), -1, NULL); + tip = recode_string (UTF8, psppire_dict_encoding (dict), + var_get_name (var), -1); else - tip = pspp_locale_to_utf8 (var_get_label (var), -1, NULL); + tip = recode_string (UTF8, psppire_dict_encoding (dict), + var_get_label (var), -1); gtk_tooltip_set_text (tooltip, tip); diff --git a/src/ui/gui/psppire-var-sheet.c b/src/ui/gui/psppire-var-sheet.c index 517a61e0..b742f3b7 100644 --- a/src/ui/gui/psppire-var-sheet.c +++ b/src/ui/gui/psppire-var-sheet.c @@ -489,7 +489,8 @@ psppire_var_sheet_realize (GtkWidget *w) GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (vs)); - vs->val_labs_dialog = val_labs_dialog_create (GTK_WINDOW (toplevel)); + vs->val_labs_dialog = val_labs_dialog_create (GTK_WINDOW (toplevel), + PSPPIRE_SHEET (vs)); vs->missing_val_dialog = missing_val_dialog_create (GTK_WINDOW (toplevel)); vs->var_type_dialog = var_type_dialog_create (GTK_WINDOW (toplevel)); diff --git a/src/ui/gui/psppire-var-store.c b/src/ui/gui/psppire-var-store.c index 1d6f465d..b64bc64e 100644 --- a/src/ui/gui/psppire-var-store.c +++ b/src/ui/gui/psppire-var-store.c @@ -21,7 +21,7 @@ #define _(msgid) gettext (msgid) #define N_(msgid) msgid - +#include #include @@ -53,9 +53,6 @@ static void psppire_var_store_sheet_model_init (PsppireSheetModelIface * static void psppire_var_store_finalize (GObject *object); -gchar * missing_values_to_string (const struct variable *pv, GError **err); - - static gchar *psppire_var_store_get_string (const PsppireSheetModel *sheet_model, glong row, glong column); static gboolean psppire_var_store_clear (PsppireSheetModel *model, glong row, glong col); @@ -67,7 +64,8 @@ static gboolean psppire_var_store_set_string (PsppireSheetModel *model, static glong psppire_var_store_get_row_count (const PsppireSheetModel * model); static glong psppire_var_store_get_column_count (const PsppireSheetModel * model); -static gchar *text_for_column (const struct variable *pv, gint c, GError **err); +static gchar *text_for_column (PsppireVarStore *vs, const struct variable *pv, + gint c, GError **err); static GObjectClass *parent_class = NULL; @@ -399,7 +397,8 @@ psppire_var_store_finalize (GObject *object) } static gchar * -psppire_var_store_get_string (const PsppireSheetModel *model, glong row, glong column) +psppire_var_store_get_string (const PsppireSheetModel *model, + glong row, glong column) { PsppireVarStore *store = PSPPIRE_VAR_STORE (model); @@ -410,7 +409,7 @@ psppire_var_store_get_string (const PsppireSheetModel *model, glong row, glong c pv = psppire_dict_get_variable (store->dict, row); - return text_for_column (pv, column, 0); + return text_for_column (store, pv, column, 0); } @@ -531,7 +530,9 @@ psppire_var_store_set_string (PsppireSheetModel *model, break; case PSPPIRE_VAR_STORE_COL_LABEL: { - gchar *s = utf8_to_pspp_locale (text, -1, NULL); + gchar *s = recode_string (psppire_dict_encoding (var_store->dict), + UTF8, + text, -1); var_set_label (pv, s); free (s); return TRUE; @@ -557,8 +558,10 @@ psppire_var_store_set_string (PsppireSheetModel *model, static const gchar none[] = N_("None"); static gchar * -text_for_column (const struct variable *pv, gint c, GError **err) +text_for_column (PsppireVarStore *vs, + const struct variable *pv, gint c, GError **err) { + PsppireDict *dict = vs->dict; static const gchar *const type_label[] = { N_("Numeric"), @@ -578,7 +581,8 @@ text_for_column (const struct variable *pv, gint c, GError **err) switch (c) { case PSPPIRE_VAR_STORE_COL_NAME: - return pspp_locale_to_utf8 ( var_get_name (pv), -1, err); + return recode_string (UTF8, psppire_dict_encoding (dict), + var_get_name (pv), -1); break; case PSPPIRE_VAR_STORE_COL_TYPE: { @@ -665,12 +669,13 @@ text_for_column (const struct variable *pv, gint c, GError **err) } break; case PSPPIRE_VAR_STORE_COL_LABEL: - return pspp_locale_to_utf8 (var_get_label (pv), -1, err); + return recode_string (UTF8, psppire_dict_encoding (dict), + var_get_label (pv), -1); break; case PSPPIRE_VAR_STORE_COL_MISSING: { - return missing_values_to_string (pv, err); + return missing_values_to_string (dict, pv, err); } break; case PSPPIRE_VAR_STORE_COL_VALUES: @@ -696,7 +701,8 @@ text_for_column (const struct variable *pv, gint c, GError **err) val_labs_done (&ip); - ss = pspp_locale_to_utf8 (gstr->str, gstr->len, err); + ss = recode_string (UTF8, psppire_dict_encoding (dict), + gstr->str, gstr->len); g_string_free (gstr, TRUE); return ss; } diff --git a/src/ui/gui/val-labs-dialog.c b/src/ui/gui/val-labs-dialog.c index f6d0ab73..42dd0b34 100644 --- a/src/ui/gui/val-labs-dialog.c +++ b/src/ui/gui/val-labs-dialog.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2005 Free Software Foundation + Copyright (C) 2005, 2009 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,12 +26,16 @@ #include "val-labs-dialog.h" #include #include - +#include "psppire-var-sheet.h" +#include "psppire-var-store.h" +#include struct val_labs_dialog { GtkWidget *window; + PsppireSheet *vs; + /* The variable to be updated */ struct variable *pv; @@ -325,8 +329,7 @@ on_remove (GtkWidget *w, gpointer data) /* Callback which occurs when a line item is selected in the list of value--label pairs.*/ static void -on_select_row (GtkTreeView *treeview, - gpointer data) +on_select_row (GtkTreeView *treeview, gpointer data) { gchar *labeltext; struct val_labs_dialog *dialog = data; @@ -336,6 +339,9 @@ on_select_row (GtkTreeView *treeview, gchar *const text = value_to_text (vl->value, *var_get_write_format (dialog->pv)); + PsppireVarStore *var_store = + PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs)); + g_signal_handler_block (GTK_ENTRY (dialog->value_entry), dialog->value_handler_id); @@ -348,7 +354,10 @@ on_select_row (GtkTreeView *treeview, g_signal_handler_block (GTK_ENTRY (dialog->label_entry), dialog->change_handler_id); - labeltext = pspp_locale_to_utf8 (vl->label, -1, 0); + + labeltext = recode_string (UTF8, psppire_dict_encoding (var_store->dict), + vl->label, -1); + gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), labeltext); g_free (labeltext); @@ -364,7 +373,7 @@ on_select_row (GtkTreeView *treeview, /* Create a new dialog box (there should normally be only one)*/ struct val_labs_dialog * -val_labs_dialog_create (GtkWindow *toplevel) +val_labs_dialog_create (GtkWindow *toplevel, PsppireSheet *sheet) { GtkTreeViewColumn *column; @@ -377,6 +386,7 @@ val_labs_dialog_create (GtkWindow *toplevel) dialog->window = get_widget_assert (xml,"val_labs_dialog"); dialog->value_entry = get_widget_assert (xml,"value_entry"); dialog->label_entry = get_widget_assert (xml,"label_entry"); + dialog->vs = sheet; gtk_window_set_transient_for (GTK_WINDOW (dialog->window), toplevel); @@ -461,6 +471,9 @@ repopulate_dialog (struct val_labs_dialog *dialog) GtkTreeIter iter; + PsppireVarStore *var_store = + PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs)); + GtkListStore *list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_DOUBLE); @@ -489,11 +502,12 @@ repopulate_dialog (struct val_labs_dialog *dialog) *var_get_write_format (dialog->pv)); gchar *labeltext = - pspp_locale_to_utf8 (vl->label, -1, 0); + recode_string (UTF8, + psppire_dict_encoding (var_store->dict), + vl->label, -1); gchar *const text = g_strdup_printf ("%s = \"%s\"", - vstr, labeltext); - + vstr, labeltext); gtk_list_store_append (list_store, &iter); gtk_list_store_set (list_store, &iter, diff --git a/src/ui/gui/val-labs-dialog.h b/src/ui/gui/val-labs-dialog.h index 6b1d0e37..3a09f1ca 100644 --- a/src/ui/gui/val-labs-dialog.h +++ b/src/ui/gui/val-labs-dialog.h @@ -14,8 +14,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ - - #ifndef __PSPPIRE_VAL_LABS_DIALOG_H #define __PSPPIRE_VAL_LABS_DIALOG_H @@ -26,12 +24,13 @@ #include #include +#include struct val_labs; -struct val_labs_dialog * val_labs_dialog_create (GtkWindow *); +struct val_labs_dialog * val_labs_dialog_create (GtkWindow *, PsppireSheet *); void val_labs_dialog_show (struct val_labs_dialog *); diff --git a/src/ui/gui/var-display.c b/src/ui/gui/var-display.c index 07c7a473..50e0df4b 100644 --- a/src/ui/gui/var-display.c +++ b/src/ui/gui/var-display.c @@ -4,34 +4,17 @@ #include #include #include +#include "psppire-dict.h" #include #define _(msgid) gettext (msgid) #define N_(msgid) msgid #include "helper.h" +#include static const gchar none[] = N_("None"); -gchar * -name_to_string (const struct variable *var, GError **err) -{ - const char *name = var_get_name (var); - g_assert (name); - - return pspp_locale_to_utf8 (name, -1, err); -} - - -gchar * -label_to_string (const struct variable *var, GError **err) -{ - const char *label = var_get_label (var); - - if ( ! label ) return g_strdup (none); - - return pspp_locale_to_utf8 (label, -1, err); -} gchar * measure_to_string (const struct variable *var, GError **err) @@ -45,7 +28,7 @@ measure_to_string (const struct variable *var, GError **err) gchar * -missing_values_to_string (const struct variable *pv, GError **err) +missing_values_to_string (const PsppireDict *dict, const struct variable *pv, GError **err) { const struct fmt_spec *fmt = var_get_print_format (pv); gchar *s; @@ -70,7 +53,8 @@ missing_values_to_string (const struct variable *pv, GError **err) g_string_append (gstr, mv[i]); g_free (mv[i]); } - s = pspp_locale_to_utf8 (gstr->str, gstr->len, err); + s = recode_string (UTF8, psppire_dict_encoding (dict), + gstr->str, gstr->len); g_string_free (gstr, TRUE); } else @@ -99,7 +83,8 @@ missing_values_to_string (const struct variable *pv, GError **err) g_string_append (gstr, ss); free (ss); } - s = pspp_locale_to_utf8 (gstr->str, gstr->len, err); + s = recode_string (UTF8, psppire_dict_encoding (dict), + gstr->str, gstr->len); g_string_free (gstr, TRUE); } diff --git a/src/ui/gui/var-display.h b/src/ui/gui/var-display.h index 40404b89..927e235c 100644 --- a/src/ui/gui/var-display.h +++ b/src/ui/gui/var-display.h @@ -20,24 +20,16 @@ #include #include +#include "psppire-dict.h" struct variable; #define n_ALIGNMENTS 3 extern const gchar *const alignments[n_ALIGNMENTS + 1]; - extern const gchar *const measures[n_MEASURES + 1]; - -gchar * name_to_string (const struct variable *var, GError **err); - - -gchar * missing_values_to_string (const struct variable *pv, GError **err); - -gchar * measure_to_string (const struct variable *var, GError **err); - -gchar * label_to_string (const struct variable *var, GError **err); - +gchar *missing_values_to_string (const PsppireDict *dict, const struct variable *pv, GError **err); +gchar *measure_to_string (const struct variable *var, GError **err); #endif diff --git a/src/ui/gui/variable-info-dialog.c b/src/ui/gui/variable-info-dialog.c index 612643b4..a53d6a5c 100644 --- a/src/ui/gui/variable-info-dialog.c +++ b/src/ui/gui/variable-info-dialog.c @@ -28,6 +28,7 @@ #include "helper.h" #include +#include #include "helper.h" @@ -36,12 +37,38 @@ #define N_(msgid) msgid +static const gchar none[] = N_("None"); + + +static gchar * +name_to_string (const struct variable *var, PsppireDict *dict) +{ + const char *name = var_get_name (var); + g_assert (name); + + return recode_string (UTF8, psppire_dict_encoding (dict), + name, -1); +} + + +static gchar * +label_to_string (const struct variable *var, PsppireDict *dict) +{ + const char *label = var_get_label (var); + + if (! label) return g_strdup (none); + + return recode_string (UTF8, psppire_dict_encoding (dict), + label, -1); +} + static void populate_text (PsppireDictView *treeview, gpointer data) { gchar *text = 0; GString *gstring; + PsppireDict *dict; GtkTextBuffer *textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(data)); const struct variable *var = @@ -50,6 +77,10 @@ populate_text (PsppireDictView *treeview, gpointer data) if ( var == NULL) return; + g_object_get (treeview, + "dictionary", &dict, + NULL); + gstring = g_string_sized_new (200); text = name_to_string (var, NULL); g_string_assign (gstring, text); @@ -70,7 +101,7 @@ populate_text (PsppireDictView *treeview, gpointer data) g_string_append_printf (gstring, _("Type: %s\n"), buffer); } - text = missing_values_to_string (var, NULL); + text = missing_values_to_string (dict, var, NULL); g_string_append_printf (gstring, _("Missing Values: %s\n"), text); g_free (text); @@ -92,7 +123,6 @@ populate_text (PsppireDictView *treeview, gpointer data) g_string_append (gstring, "\n"); g_string_append (gstring, _("Value Labels:\n")); -#if 1 for (vl = val_labs_first_sorted (labs, &vli); vl; vl = val_labs_next (labs, &vli)) @@ -100,14 +130,15 @@ populate_text (PsppireDictView *treeview, gpointer data) gchar *const vstr = value_to_text (vl->value, *var_get_print_format (var)); - text = pspp_locale_to_utf8 (vl->label, -1, NULL); + + text = recode_string (UTF8, psppire_dict_encoding (dict), + vl->label, -1); g_string_append_printf (gstring, _("%s %s\n"), vstr, text); g_free (text); g_free (vstr); } -#endif } gtk_text_buffer_set_text (textbuffer, gstring->str, gstring->len);