Fixed bug where gui would crash when columns were resized.
[pspp-builds.git] / src / ui / gui / psppire-data-store.c
index 4471a866ca0374d5f4491a1d73e520a5a087b21c..16f709ff302996c0ecab4f80fc8f309dd8892894 100644 (file)
@@ -33,6 +33,8 @@
 #include <gtksheet/gsheetmodel.h>
 #include <gtksheet/gsheet-column-iface.h>
 
+#include <pango/pango-context.h>
+
 #include "psppire-variable.h"
 #include "psppire-data-store.h"
 #include "helper.h"
@@ -70,6 +72,13 @@ static gboolean psppire_data_store_clear_datum(GSheetModel *model,
 
 static GObjectClass *parent_class = NULL;
 
+
+enum  {FONT_CHANGED,
+       n_SIGNALS};
+
+static guint signal[n_SIGNALS];
+
+
 inline GType
 psppire_data_store_get_type (void)
 {
@@ -131,6 +140,7 @@ psppire_data_store_get_type (void)
   return data_store_type;
 }
 
+
 static void
 psppire_data_store_class_init (PsppireDataStoreClass *class)
 {
@@ -140,6 +150,16 @@ psppire_data_store_class_init (PsppireDataStoreClass *class)
   object_class = (GObjectClass*) class;
 
   object_class->finalize = psppire_data_store_finalize;
+
+  signal[FONT_CHANGED] =
+    g_signal_new ("font_changed",
+                 G_TYPE_FROM_CLASS(class),
+                 G_SIGNAL_RUN_FIRST,
+                 0,
+                 NULL, NULL,
+                 g_cclosure_marshal_VOID__VOID,
+                 G_TYPE_NONE, 
+                 0);
 }
 
 
@@ -166,6 +186,7 @@ psppire_data_store_init (PsppireDataStore *data_store)
 {
   data_store->dict = 0;
   data_store->case_file = 0;
+  data_store->width_of_m = 10;
 }
 
 const PangoFontDescription *
@@ -268,31 +289,49 @@ static void
 insert_variable_callback(GObject *obj, gint var_num, gpointer data)
 {
   PsppireDataStore *store;
+  gint posn;
 
   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);
-  */
+  if ( var_num > 0 ) 
+    {
+      struct PsppireVariable *variable;
+      variable = psppire_dict_get_variable(store->dict, var_num);
 
-#if 0
-  psppire_case_array_resize(store->cases, 
-                        dict_get_next_value_idx (store->dict->dict));
-#endif
+      posn = psppire_variable_get_fv(variable);
+    }
+  else
+    {
+      posn = 0;
+    }
+
+  psppire_case_file_insert_values(store->case_file, 1, posn);
 
   g_sheet_column_columns_changed(G_SHEET_COLUMN(store),
                                  var_num, 1);
 
-
   g_sheet_model_columns_inserted (G_SHEET_MODEL(store), var_num, 1);
 }
 
 
+static void
+dict_size_change_callback(GObject *obj, 
+                         gint posn, gint adjustment, gpointer data)
+{
+  PsppireDataStore *store ;
+
+  g_return_if_fail (data);
+
+  store  = PSPPIRE_DATA_STORE(data);
+
+  /* 
+  if ( adjustment > 0 )
+  */
+  psppire_case_file_insert_values (store->case_file, adjustment, posn);
+}
+
 
 
 /**
@@ -356,7 +395,6 @@ psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dic
                   G_CALLBACK(changed_case_callback), 
                   data_store);
 
-
   g_signal_connect(dict, "variable-inserted", 
                   G_CALLBACK(insert_variable_callback), 
                   data_store);
@@ -365,6 +403,10 @@ psppire_data_store_set_dictionary(PsppireDataStore *data_store, PsppireDict *dic
                   G_CALLBACK(delete_variables_callback), 
                   data_store);
 
+  g_signal_connect (dict, "dict-size-changed", 
+                   G_CALLBACK(dict_size_change_callback),
+                   data_store);
+
   /* The entire model has changed */
   g_sheet_model_range_changed (G_SHEET_MODEL(data_store), -1, -1, -1, -1);
   
@@ -402,10 +444,12 @@ psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
 
   pv = psppire_dict_get_variable(store->dict, column);
 
-  idx = psppire_variable_get_index(pv);
+  idx = psppire_variable_get_fv(pv);
 
   v = psppire_case_file_get_value(store->case_file, row, idx);
 
+  g_return_val_if_fail(v, NULL);
+
   if ( store->show_labels) 
     {
       const struct val_labs * vl = psppire_variable_get_value_labels(pv);
@@ -448,15 +492,15 @@ psppire_data_store_clear_datum(GSheetModel *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) 
     v.f = SYSMIS;
   else
     memcpy(v.s, "", MAX_SHORT_STRING);
 
-  psppire_case_file_set_value(store->case_file, row, index, &v);
-
+  psppire_case_file_set_value(store->case_file, row, index, &v
+                             psppire_variable_get_width(pv));
   return TRUE;
 }
 
@@ -490,7 +534,7 @@ psppire_data_store_set_string(GSheetModel *model,
 #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;
@@ -500,15 +544,7 @@ psppire_data_store_set_string(GSheetModel *model,
     d_in.format = * psppire_variable_get_write_spec(pv);
     d_in.flags = 0;
 
-    /* 
-    if ( ! data_in(&d_in) ) 
-      {
-       g_warning("Cannot encode string");
-       return FALSE;
-      }
-    */
-
-    psppire_case_file_set_value(store->case_file, row, index, &d_in) ;
+    psppire_case_file_data_in(store->case_file, row, index, &d_in) ;
   }
 
   return TRUE;
@@ -516,12 +552,19 @@ psppire_data_store_set_string(GSheetModel *model,
 
 
 void
-psppire_data_store_set_font(PsppireDataStore *store, PangoFontDescription *fd)
+psppire_data_store_set_font(PsppireDataStore *store, 
+                           const PangoFontDescription *fd)
 {
   g_return_if_fail (store);
   g_return_if_fail (PSPPIRE_IS_DATA_STORE (store));
 
   store->font_desc = fd;
+#if 0
+  store->width_of_m = calc_m_width(fd);
+#endif
+  g_signal_emit(store, signal[FONT_CHANGED], 0);  
+
+
   g_sheet_model_range_changed (G_SHEET_MODEL(store),
                                 -1, -1, -1, -1);
 }
@@ -541,19 +584,6 @@ psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
 
 
 
-static gboolean 
-write_case(const struct ccase *cc, 
-          gpointer aux)
-{
-  struct sfm_writer *writer = aux;
-
-  if ( ! sfm_write_case(writer, cc) )
-    return FALSE;
-
-
-  return TRUE;
-}
-
 void
 psppire_data_store_create_system_file(PsppireDataStore *store,
                              struct file_handle *handle)
@@ -596,84 +626,46 @@ psppire_data_store_clear(PsppireDataStore *data_store)
 /* Column related funcs */
 
 static gint
-geometry_get_column_count(const GSheetColumn *geom, gpointer data)
+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 the width that an  'M' character would occupy when typeset at
-   row, col */
-static guint 
-M_width(const GtkSheet *sheet, gint row, gint col)
-{
-  GtkSheetCellAttr 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);
-
-  pango_layout_get_extents (layout, NULL, &rect);
-
-#if 0
-  g_object_unref(G_OBJECT(layout));
-#endif
-
-  return PANGO_PIXELS(rect.width);
-}
-
-
-/* Return the number of pixels corresponding to a column of 
-   WIDTH characters */
-static inline guint 
-columnWidthToPixels(GtkSheet *sheet, gint column, guint width)
-{
-  return (M_width(sheet, 0, column) * width);
-}
 
 
 static gint
-geometry_get_width(const GSheetColumn *geom, gint unit, GtkSheet *sheet)
+geometry_get_width(const GSheetColumn *geom, gint unit)
 {
   const struct PsppireVariable *pv ;
   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
 
   if ( unit >= psppire_dict_get_var_cnt(ds->dict) )
-    return 75;
+    return ds->width_of_m * 8 ;
 
-  /* FIXME: We can optimise this by caching the widths until they're resized */
   pv = psppire_dict_get_variable(ds->dict, unit);
 
-  return columnWidthToPixels(sheet, unit, psppire_variable_get_columns(pv));
-}
-
-
+  if ( pv == NULL ) 
+    return ds->width_of_m * 8 ;
 
+  return ds->width_of_m * psppire_variable_get_columns(pv);
+}
 
 static void
-geometry_set_width(GSheetColumn *geom, gint unit, gint width, GtkSheet *sheet)
+geometry_set_width(GSheetColumn *geom, gint unit, gint width)
 {
   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
 
   struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, unit);
 
-  psppire_variable_set_columns(pv, width / M_width(sheet, 1, unit));
+  psppire_variable_set_columns(pv, width / ds->width_of_m );
 }
 
 
 
 static GtkJustification
-geometry_get_justification(const GSheetColumn *geom, gint unit, gpointer data)
+geometry_get_justification(const GSheetColumn *geom, gint unit)
 {
   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
   const struct PsppireVariable *pv ;
@@ -693,8 +685,7 @@ geometry_get_justification(const GSheetColumn *geom, gint unit, gpointer data)
 static const gchar null_var_name[]=N_("var");
  
 static gchar *
-geometry_get_column_button_label(const GSheetColumn *geom, gint unit, 
-                                gpointer data)
+geometry_get_column_button_label(const GSheetColumn *geom, gint unit)
 {
   gchar *text;
   struct PsppireVariable *pv ;
@@ -712,7 +703,7 @@ geometry_get_column_button_label(const GSheetColumn *geom, gint unit,
 
 
 static gboolean
-geometry_get_sensitivity(const GSheetColumn *geom, gint unit, gpointer data)
+geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
 {
   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
 
@@ -796,7 +787,3 @@ psppire_data_store_sheet_row_init (GSheetRowIface *iface)
 
   iface->get_button_label = geometry_get_row_button_label;
 }
-
-
-
-