/[pspp]/psppire/src/data_sheet.c
ViewVC logotype

Diff of /psppire/src/data_sheet.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.20 by jmd, Thu Nov 17 11:19:17 2005 UTC revision 1.21 by jmd, Sun Nov 20 12:26:14 2005 UTC
# Line 162  data_sheet_set_cell(GtkSheet *sheet, gin Line 162  data_sheet_set_cell(GtkSheet *sheet, gin
162  }  }
163    
164    
165    /* This nasty piece of work is a temporary hack.
166       Converting data between variable types ought to be a
167       transformation, and not the responsibility of the GUI
168    */
169    static void
170    convert(GType to, gint row, gint col, GValue *value)
171    {
172      const struct variable *var = pspp_dict_get_var(pspp_dictionary, col);
173      
174      const struct fmt_spec *fp = &var->write;
175    
176      double v;
177    
178      switch  (G_VALUE_TYPE(value))
179        {
180        case G_TYPE_STRING:
181          if ( to == G_TYPE_DOUBLE )
182            {
183              const gchar *s = g_value_get_string(value);
184              if ( !s )
185                v = SYSMIS;
186              else
187                {
188                  bool found_digit = false;
189                  const gchar *ss = s;
190                  while (*ss)
191                    {
192                      if (isdigit(*ss++))
193                        {
194                          found_digit = true;
195                          break;
196                        }
197                    }
198                  if ( !found_digit )
199                    v = SYSMIS;
200                  else if ( 0 == sscanf(s, "%lf", &v) )
201                    v = SYSMIS;
202                }
203    
204              g_value_unset(value);
205              g_value_init(value, to);
206    
207              g_value_set_double(value, v);
208            }
209          break;
210        case G_TYPE_DOUBLE:
211          if ( to == G_TYPE_STRING )
212            {
213              gchar *s=0;
214              v = g_value_get_double(value);
215              if ( v == SYSMIS)
216                s = g_strdup("");
217              else
218                s = g_strdup_printf("%g", v);
219              g_value_unset(value);
220              g_value_init(value, to);
221    
222              g_value_set_string(value, s);
223              g_free(s);
224            }
225          break;
226        default:
227          g_assert_not_reached();
228          break;
229        }
230    
231    
232    }
233    
234    
235  /* Update COLUMN of the data sheet */  /* Update COLUMN of the data sheet */
236  static gint  static gint
237  update_variable(GObject *obj, gint column, gpointer data)  update_column(GtkSheet *sheet, gint column, PSPP_Dict *dict)
238  {  {
   PSPP_Dict *dict = PSPP_DICT(obj);  
   
239    GtkSheetRange range;    GtkSheetRange range;
   GtkSheet *sheet = GTK_SHEET(data);  
240    
241    if ( column >= gtk_sheet_get_columns_count(sheet) )    if ( column >= gtk_sheet_get_columns_count(sheet) )
242      {      {
# Line 185  update_variable(GObject *obj, gint colum Line 252  update_variable(GObject *obj, gint colum
252    
253    
254    gtk_sheet_range_set_type(sheet, &range,    gtk_sheet_range_set_type(sheet, &range,
255                             (var->type == ALPHA)?G_TYPE_STRING:G_TYPE_DOUBLE);                             (var->type == ALPHA)?G_TYPE_STRING:G_TYPE_DOUBLE,
256                               convert);
257    
258    
259    gtk_sheet_column_button_add_label(sheet, column, var->name);    gtk_sheet_column_button_add_label(sheet, column, var->name);
# Line 198  update_variable(GObject *obj, gint colum Line 266  update_variable(GObject *obj, gint colum
266    return FALSE;    return FALSE;
267  }  }
268    
269    /* Callback for when a variable has been added */
270    static gint
271    add_variable(GObject *obj, gint column, gpointer data)
272    {
273      gint r;
274    
275      PSPP_Dict *dict = PSPP_DICT(obj);
276    
277      GtkSheetRange range;
278      GtkSheet *sheet = GTK_SHEET(data);
279    
280      const struct variable *var = pspp_dict_get_var(dict, column);
281    
282      update_column(sheet, column, dict);
283    
284      for ( r = 0 ; r <= last_populated_row; ++r)
285        data_sheet_set_cell_sysmis(sheet, r, column);
286    
287      return FALSE;
288    }
289    
290    
291    /* Callback for when a variable has changed */
292    static gint
293    update_variable(GObject *obj, gint column, gpointer data)
294    {
295      GtkSheet *sheet = GTK_SHEET(data);
296      PSPP_Dict *dict = PSPP_DICT(obj);
297    
298      g_assert ( column < gtk_sheet_get_columns_count(sheet));
299    
300      return update_column(sheet, column, dict);
301    }
302    
303    
304  /* Repopulate the columns starting at position FROM, to position TO inclusive*/  /* Repopulate the columns starting at position FROM, to position TO inclusive*/
# Line 312  psppire_data_sheet_set_dictionary(GtkWid Line 412  psppire_data_sheet_set_dictionary(GtkWid
412    g_signal_connect(pspp_dictionary, "variable_changed",    g_signal_connect(pspp_dictionary, "variable_changed",
413                     G_CALLBACK(update_variable), sheet);                     G_CALLBACK(update_variable), sheet);
414    
415      g_signal_connect(pspp_dictionary, "variable_added",
416                       G_CALLBACK(add_variable), sheet);
417    
418    
419    g_signal_connect(pspp_dictionary, "variable_deleted",    g_signal_connect(pspp_dictionary, "variable_deleted",
420                     G_CALLBACK(remove_variable), sheet);                     G_CALLBACK(remove_variable), sheet);
421  }  }

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.21

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26