#include <config.h>
#include <string.h>
#include <stdlib.h>
+#include <gettext.h>
+#define _(msgid) gettext(msgid)
+#define N_(msgid) msgid
-#include "gtksheet.h"
-#include "gsheetmodel.h"
-#include "gsheet-column-iface.h"
+#include <data/casefile.h>
+#include <data/case.h>
+
+#include <gtksheet/gtksheet.h>
+#include <gtksheet/gsheetmodel.h>
+#include <gtksheet/gsheet-column-iface.h>
#include "psppire-variable.h"
#include "psppire-data-store.h"
+#include "helper.h"
-#include "dictionary.h"
-#include "missing-values.h"
-#include "value-labels.h"
-#include "data-in.h"
+#include <data/dictionary.h>
+#include <data/missing-values.h>
+#include <data/value-labels.h>
+#include <data/data-in.h>
-#include "file-handle-def.h"
-#include "sys-file-writer.h"
+#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);
static void psppire_data_store_class_init (PsppireDataStoreClass *class);
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_sheet_row_init (GSheetRowIface *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 gchar *psppire_data_store_get_string(const GSheetModel *sheet_model, gint row, gint column);
static gboolean psppire_data_store_set_string(GSheetModel *model,
const gchar *text, gint row, gint column);
#define MIN_COLUMNS 10
-#define max(A,B) ((A>B)?A:B)
+#define TRAILING_ROWS 10
static GObjectClass *parent_class = NULL;
NULL
};
+ static const GInterfaceInfo sheet_row_info =
+ {
+ (GInterfaceInitFunc) psppire_data_store_sheet_row_init,
+ NULL,
+ NULL
+ };
data_store_type = g_type_register_static (G_TYPE_OBJECT, "PsppireDataStore",
G_TYPE_SHEET_COLUMN,
&sheet_column_info);
+ g_type_add_interface_static (data_store_type,
+ G_TYPE_SHEET_ROW,
+ &sheet_row_info);
}
return data_store_type;
}
+
+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_file_get_case_count(store->case_file);
+}
+
+
static void
psppire_data_store_init (PsppireDataStore *data_store)
{
data_store->dict = 0;
- data_store->cases = 0;
+ data_store->case_file = 0;
}
const PangoFontDescription *
-psppire_data_store_get_font_desc(GSheetModel *model,
+psppire_data_store_get_font_desc(const GSheetModel *model,
gint row, gint column)
{
PsppireDataStore *store = PSPPIRE_DATA_STORE(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;
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
}
-
static void
delete_cases_callback(GtkWidget *w, gint first, gint n_cases, gpointer data)
{
+ PsppireDataStore *store ;
+
g_return_if_fail (data);
- PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
+ store = PSPPIRE_DATA_STORE(data);
g_assert(first >= 0);
static void
insert_case_callback(GtkWidget *w, gint casenum, gpointer data)
{
+ PsppireDataStore *store ;
+
g_return_if_fail (data);
- PsppireDataStore *store = PSPPIRE_DATA_STORE(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_file_get_case_count(store->case_file),
+ -1);
+
+ g_sheet_model_rows_inserted (G_SHEET_MODEL(store), casenum, 1);
}
static void
changed_case_callback(GtkWidget *w, gint casenum, gpointer data)
{
+ PsppireDataStore *store ;
g_return_if_fail (data);
- PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
+ store = PSPPIRE_DATA_STORE(data);
-
g_sheet_model_range_changed (G_SHEET_MODEL(store),
casenum, -1,
casenum, -1);
-
}
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 ;
+
g_return_if_fail (data);
- PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
+ store = PSPPIRE_DATA_STORE(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);
+ g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
+ var_num, -1);
+}
static void
-insert_variable_callback(GtkWidget *w, gint var_num, gpointer data)
+insert_variable_callback(GObject *obj, gint var_num, gpointer data)
{
+ PsppireDataStore *store;
+
g_return_if_fail (data);
- PsppireDataStore *store = PSPPIRE_DATA_STORE(data);
+ store = PSPPIRE_DATA_STORE(data);
/*
g_sheet_model_range_changed (G_SHEET_MODEL(store),
-1);
*/
+#if 0
psppire_case_array_resize(store->cases,
dict_get_next_value_idx (store->dict->dict));
+#endif
+
+ g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
+ var_num, 1);
+
+ g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
}
* Return value: a new #PsppireDataStore
**/
PsppireDataStore *
-psppire_data_store_new (PsppireDict *dict, PsppireCaseArray *cases)
+psppire_data_store_new (PsppireDict *dict)
{
PsppireDataStore *retval;
retval = g_object_new (GTK_TYPE_DATA_STORE, NULL);
- retval->cases = cases;
- g_signal_connect(cases, "cases-deleted", G_CALLBACK(delete_cases_callback),
- retval);
-
- g_signal_connect(cases, "case-inserted", G_CALLBACK(insert_case_callback),
- retval);
-
-
- g_signal_connect(cases, "case-changed", G_CALLBACK(changed_case_callback),
- retval);
-
-
psppire_data_store_set_dictionary(retval, dict);
void
psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dict)
{
+ gint var_cnt = psppire_dict_get_next_value_idx(dict);
#if 0
if ( data_store->dict ) g_object_unref(data_store->dict);
#endif
data_store->dict = dict;
- psppire_case_array_resize(data_store->cases,
- dict_get_next_value_idx (data_store->dict->dict));
+ if ( data_store->case_file)
+ {
+ g_object_unref(data_store->case_file);
+ data_store->case_file = 0;
+ }
+
+ data_store->case_file = psppire_case_file_new(var_cnt);
+
+ g_signal_connect(data_store->case_file, "cases-deleted",
+ G_CALLBACK(delete_cases_callback),
+ data_store);
+
+ g_signal_connect(data_store->case_file, "case-inserted",
+ G_CALLBACK(insert_case_callback),
+ data_store);
+
+
+ g_signal_connect(data_store->case_file, "case-changed",
+ G_CALLBACK(changed_case_callback),
+ data_store);
g_signal_connect(dict, "variable-inserted",
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);
+
+ g_sheet_column_columns_changed(G_SHEET_COLUMN(data_store), 0, -1);
}
static void
}
-static const gchar *const
-psppire_data_store_get_string(GSheetModel *model, gint row, gint column)
+static gchar *
+psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
{
- static gchar s[255];
+ gint idx;
+ char *text;
+ const struct fmt_spec *fp ;
+ const struct PsppireVariable *pv ;
+ const union value *v ;
+ GString *s;
PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
g_return_val_if_fail(store->dict, NULL);
- g_return_val_if_fail(store->cases, NULL);
+ g_return_val_if_fail(store->case_file, NULL);
if (column >= psppire_dict_get_var_cnt(store->dict))
return NULL;
- if ( row >= psppire_case_array_get_n_cases(store->cases))
+ if ( row >= psppire_case_file_get_case_count(store->case_file))
return NULL;
- const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, column);
-
+ pv = psppire_dict_get_variable(store->dict, column);
- const union value *v =
- psppire_case_array_get_value(store->cases, row,
- psppire_variable_get_index(pv));
+ idx = psppire_variable_get_fv(pv);
+ v = psppire_case_file_get_value(store->case_file, row, idx);
if ( store->show_labels)
{
const gchar *label;
if ( (label = val_labs_find(vl, *v)) )
{
- return label;
+ return pspp_locale_to_utf8(label, -1, 0);
}
}
- const struct fmt_spec *fp = psppire_variable_get_write_spec(pv);
+ fp = psppire_variable_get_write_spec(pv);
- if ( psppire_variable_get_type(pv) == NUMERIC )
- {
- /* 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, fp, v);
- s[fp->w] = '\0';
- }
- else
- {
- const gint len = psppire_variable_get_width(pv);
- memcpy(s, v->s, len);
- s[len] = '\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;
- }
+ s = g_string_sized_new (fp->w + 1);
+ g_string_set_size(s, fp->w);
+
+ memset(s->str, 0, fp->w);
- return buf ;
-}
+ 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);
+ text = pspp_locale_to_utf8(s->str, fp->w, 0);
+ g_string_free(s, TRUE);
-static gboolean
-set_null_string_value(union value *val, gpointer data)
-{
- strcpy(val->s, "");
- return TRUE;
-}
-
-static gboolean
-set_sysmis_value(union value *val, gpointer data)
-{
- val->f = SYSMIS;
- return TRUE;
+ return text;
}
{
PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
+ union value v;
const struct PsppireVariable *pv = psppire_dict_get_variable(store->dict, col);
- const gint index = psppire_variable_get_index(pv) ;
+ const gint index = psppire_variable_get_fv(pv) ;
if ( psppire_variable_get_type(pv) == NUMERIC)
- psppire_case_array_set_value(store->cases, row, index, set_sysmis_value,0);
+ v.f = SYSMIS;
else
- psppire_case_array_set_value(store->cases, row, index, set_null_string_value,0);
- return TRUE;
-}
+ memcpy(v.s, "", MAX_SHORT_STRING);
-
-static gboolean
-fillit(union value *val, gpointer data)
-{
- struct data_in *d_in = data;
-
- d_in->v = val;
-
- if ( ! data_in(d_in) )
- {
- g_warning("Cant encode string\n");
- return FALSE;
- }
+ psppire_case_file_set_value(store->case_file, row, index, &v);
return TRUE;
}
psppire_data_store_set_string(GSheetModel *model,
const gchar *text, gint row, gint col)
{
- gint r;
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 )
+#if 0
+ /* Allow the user to insert a lot of blank cases, simply by skipping rows */
+ for(r = psppire_case_file_get_case_count(store->case_file); 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);
}
+#endif
- const gint index = psppire_variable_get_index(pv);
+ {
+ const gint index = psppire_variable_get_fv(pv);
- struct data_in d_in;
- d_in.s = text;
- d_in.e = text + strlen(text);
- d_in.v = 0;
- d_in.f1 = d_in.f2 = 0;
- d_in.format = * psppire_variable_get_write_spec(pv);
- d_in.flags = 0;
+ struct data_in d_in;
+ d_in.s = text;
+ d_in.e = text + strlen(text);
+ d_in.v = 0;
+ d_in.f1 = d_in.f2 = 0;
+ d_in.format = * psppire_variable_get_write_spec(pv);
+ d_in.flags = 0;
- psppire_case_array_set_value(store->cases, row, index, fillit, &d_in);
+ /*
+ if ( ! data_in(&d_in) )
+ {
+ g_warning("Cannot encode string");
+ return FALSE;
+ }
+ */
+
+ psppire_case_file_set_value(store->case_file, row, index, &d_in) ;
+ }
return TRUE;
}
psppire_data_store_create_system_file(PsppireDataStore *store,
struct file_handle *handle)
{
- g_assert(handle);
-
const struct sfm_write_options wo = {
true, /* writeable */
false, /* dont compress */
3 /* version */
};
- struct sfm_writer *writer = sfm_open_writer(handle, store->dict->dict, wo);
+ struct sfm_writer *writer ;
+
+ g_assert(handle);
+
+ writer = sfm_open_writer(handle, store->dict->dict, wo);
if ( ! writer)
return;
+#if 0
psppire_case_array_iterate_case(store->cases, write_case, writer);
+#endif
sfm_close_writer(writer);
}
+void
+psppire_data_store_clear(PsppireDataStore *data_store)
+{
+ psppire_case_file_clear(data_store->case_file);
+
+ psppire_dict_clear(data_store->dict);
+}
+
+
+
+
/* Column related funcs */
static gint
-geometry_get_column_count(const GSheetColumn *geom)
+geometry_get_column_count(const GSheetColumn *geom, gpointer data)
{
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;
-
- gtk_sheet_get_attributes(sheet, row, col, &attributes);
-
PangoRectangle rect;
/* FIXME: make this a member of the data store */
static PangoLayout *layout = 0;
+ gtk_sheet_get_attributes(sheet, row, col, &attributes);
+
if (! layout )
layout = gtk_widget_create_pango_layout (GTK_WIDGET(sheet), "M");
+
+ g_assert(layout);
pango_layout_set_font_description (layout,
attributes.font_desc);
static gint
geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
{
+ const struct PsppireVariable *pv ;
PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
return 75;
/* FIXME: We can optimise this by caching the widths until they're resized */
- const struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
+ pv = psppire_dict_get_variable(ds->dict, unit);
return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
}
static GtkJustification
-geometry_get_justification(const GSheetColumn *geom, gint unit)
+geometry_get_justification(const GSheetColumn *geom, gint unit, gpointer data)
{
PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
+ const struct PsppireVariable *pv ;
if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
return GTK_JUSTIFY_LEFT;
- const struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
+ pv = psppire_dict_get_variable(ds->dict, unit);
/* Kludge: Happily GtkJustification is defined similarly
to enum alignment from pspp/variable.h */
}
-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)
+static gchar *
+geometry_get_column_button_label(const GSheetColumn *geom, gint unit,
+ gpointer data)
{
+ 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);
- struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
+ 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;
}
static gboolean
-geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
+geometry_get_sensitivity(const GSheetColumn *geom, gint unit, gpointer data)
{
PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
iface->get_sensitivity = geometry_get_sensitivity;
iface->get_justification = geometry_get_justification;
- iface->get_button_label = geometry_get_button_label;
+ iface->get_button_label = geometry_get_column_button_label;
}
+
+
+/* Row related funcs */
+
+static gint
+geometry_get_row_count(const GSheetRow *geom, gpointer data)
+{
+ PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
+
+ return TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file);
+}
+
+
+static gint
+geometry_get_height(const GSheetRow *geom, gint unit, gpointer data)
+{
+ return 25;
+}
+
+
+static gboolean
+geometry_get_row_sensitivity(const GSheetRow *geom, gint unit, gpointer data)
+{
+ PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
+
+
+ return (unit < psppire_case_file_get_case_count(ds->case_file));
+}
+
+
+static gchar *
+geometry_get_row_button_label(const GSheetRow *geom, gint unit, gpointer data)
+{
+ gchar *text;
+ gchar *s;
+ PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
+
+ if ( unit >
+ TRAILING_ROWS + psppire_case_file_get_case_count(ds->case_file))
+ return 0;
+
+ s = g_strdup_printf(_("%d"), unit);
+
+ text = pspp_locale_to_utf8(s, -1, 0);
+
+ g_free(s);
+
+ return text;
+}
+
+
+static void
+psppire_data_store_sheet_row_init (GSheetRowIface *iface)
+{
+ iface->get_row_count = geometry_get_row_count;
+
+ iface->get_height = geometry_get_height;
+ iface->set_height = 0;
+ iface->get_visibility = always_true;
+ iface->get_sensitivity = geometry_get_row_sensitivity;
+
+ iface->get_button_label = geometry_get_row_button_label;
+}
+
+
+
+