Committed patch #5636
[pspp-builds.git] / src / ui / gui / var-type-dialog.c
index 3a6f67eb13bcdbba372400b05b523c54c8c3f0f6..0d58026df083b139b56dd4979d4a5b6588cdb9b3 100644 (file)
@@ -1,6 +1,6 @@
 /* 
     PSPPIRE --- A Graphical User Interface for PSPP
-    Copyright (C) 2005  Free Software Foundation
+    Copyright (C) 2005, 2006  Free Software Foundation
     Written by John Darrington
 
     This program is free software; you can redistribute it and/or modify
@@ -22,6 +22,8 @@
 /*  This module describes the behaviour of the Variable Type dialog box used
     for inputing the variable type in the var sheet */
 
+#include <config.h>
+
 #include <gtk/gtk.h>
 #include <glade/glade.h>
 
 
 #include "var-type-dialog.h"
 
-#include "psppire-variable.h"
+#include "helper.h"
 
 #include <data/variable.h>
 #include <data/settings.h>
+#include <libpspp/message.h>
 
 
 struct tgs
@@ -126,7 +129,7 @@ static void update_width_decimals(const struct var_type_dialog *dialog);
 static void
 set_local_width_decimals(struct var_type_dialog *dialog)
 {
-  dialog->fmt_l = * psppire_variable_get_write_spec(dialog->pv);
+  dialog->fmt_l = * var_get_write_format (dialog->pv);
 
   switch (dialog->active_button) 
     {
@@ -208,41 +211,6 @@ static gint on_var_type_ok_clicked(GtkWidget *w, gpointer data);
 
 #define LEN 20
 
-/* return a string of the form "$#,###.##" according to FMT. 
-   FMT must be of type FMT_DOLLAR
- */
-static const gchar *
-dollar_format_template(const struct fmt_spec *fmt)
-{
-  static gchar buf[LEN];
-  g_assert( fmt->type == FMT_DOLLAR);
-
-  gint int_part = fmt->w - fmt->d;
-  if ( fmt->d > 0 ) --int_part;
-  g_assert(int_part > 0);
-
-  g_strlcpy(buf, "$", LEN);
-
-  gint c = int_part - 1;
-  while(c > 0)
-    {
-      g_strlcat(buf, "#", LEN);
-      if(--c % 4 == 0 && c > 0 ) 
-      {
-       g_strlcat(buf, ",", LEN);
-       --c;
-      }
-    }
-  if ( fmt->d > 0 ) 
-    {
-      g_strlcat(buf, ".", LEN);
-      for ( c = 0 ; c < fmt->d ; ++c ) 
-       g_strlcat(buf, "#", LEN);
-    }
-
-  return buf;
-}
-
 static void
 add_to_group(GtkWidget *w, gpointer data)
 {
@@ -255,9 +223,10 @@ add_to_group(GtkWidget *w, gpointer data)
 static void
 update_width_decimals(const struct var_type_dialog *dialog)
 {
+  gchar *text;
   g_assert(dialog);
 
-  gchar *text = g_strdup_printf("%d", dialog->fmt_l.w);
+  text = g_strdup_printf("%d", dialog->fmt_l.w);
   gtk_entry_set_text(GTK_ENTRY(dialog->entry_width), text);
   g_free(text);
 
@@ -271,18 +240,21 @@ update_width_decimals(const struct var_type_dialog *dialog)
 static void
 preview_custom(GtkWidget *w, gpointer data)
 {
+  const gchar *text ;
+
   struct var_type_dialog *dialog = data;
 
   if ( dialog->active_button != BUTTON_CUSTOM ) 
     return;
 
-  const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
+  text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_decimals));
   dialog->fmt_l.d = atoi(text);
 
   text = gtk_entry_get_text(GTK_ENTRY(dialog->entry_width));
   dialog->fmt_l.w = atoi(text);
 
-  if ( ! check_output_specifier(&dialog->fmt_l, 0))
+  msg_disable ();
+  if ( ! fmt_check_output(&dialog->fmt_l))
     {
       gtk_label_set_text(GTK_LABEL(dialog->label_psample), "---");
       gtk_label_set_text(GTK_LABEL(dialog->label_nsample), "---");
@@ -302,6 +274,7 @@ preview_custom(GtkWidget *w, gpointer data)
       gtk_label_set_text(GTK_LABEL(dialog->label_nsample), sample_text);
       g_free(sample_text);
     }
+  msg_enable ();
 }
 
 /* Callback for when a treeview row is changed.
@@ -356,10 +329,10 @@ struct var_type_dialog *
 var_type_dialog_create(GladeXML *xml)
 {
   gint i;
-  g_assert(xml);
-
   struct var_type_dialog *dialog = g_malloc(sizeof(struct var_type_dialog));
 
+  g_assert(xml);
+
   dialog->window = get_widget_assert(xml,"var_type_dialog");
 
   gtk_window_set_transient_for(GTK_WINDOW(dialog->window), 
@@ -408,16 +381,24 @@ var_type_dialog_create(GladeXML *xml)
   dialog->ok = get_widget_assert(xml,"var_type_ok");
 
 
+  {
+  GtkTreeIter iter;
+  GtkListStore *list_store ;
+
+  GtkTreeViewColumn *column;
+  GtkCellRenderer *renderer ;
+
+  static struct tgs tgs[num_BUTTONS];
   /* The "middle_box" is a vbox with serveral children.
      However only one child is ever shown at a time.
      We need to make sure that they all have the same width, to avoid
      upleasant resizing effects */
   GtkSizeGroup *sizeGroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
+
   gtk_container_foreach(GTK_CONTAINER(get_widget_assert(xml, "middle_box")), 
                        add_to_group, sizeGroup);
 
-  
-  static struct tgs tgs[num_BUTTONS];
+
   for (i = 0 ; i < num_BUTTONS; ++i ) 
     {
       tgs[i].dialog = dialog;
@@ -433,9 +414,7 @@ var_type_dialog_create(GladeXML *xml)
   dialog->date_format_treeview = GTK_TREE_VIEW(get_widget_assert(xml, 
                                              "date_format_list_view"));
 
-  GtkTreeViewColumn *column;
-
-  GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
+  renderer = gtk_cell_renderer_text_new();
   
   column = gtk_tree_view_column_new_with_attributes ("Title",
                                                     renderer,
@@ -447,9 +426,7 @@ var_type_dialog_create(GladeXML *xml)
                               column);
 
 
-  GtkTreeIter iter;
-  GtkListStore *list_store = gtk_list_store_new (2, G_TYPE_STRING, 
-                                                G_TYPE_POINTER);
+  list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
 
   for ( i = 0 ; i < sizeof(format_option) / sizeof(format_option[0]) ; ++i ) 
     {
@@ -488,11 +465,13 @@ var_type_dialog_create(GladeXML *xml)
 
   for ( i = 0 ; i < sizeof(dollar_format)/sizeof(dollar_format[0]) ; ++i ) 
     {
+      char *template = fmt_dollar_template (&dollar_format[i]);
       gtk_list_store_append (list_store, &iter);
       gtk_list_store_set (list_store, &iter,
-                          0, dollar_format_template(&dollar_format[i]),
+                          0, template,
                          1, &dollar_format[i],
                          -1);
+      free (template);
     }
 
   gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->dollar_treeview), 
@@ -526,10 +505,11 @@ var_type_dialog_create(GladeXML *xml)
   list_store = gtk_list_store_new (2, G_TYPE_STRING, 
                                                 G_TYPE_POINTER);
 
-  for ( i = 0 ; i < CC_CNT ; ++i ) 
+  for ( i = 0 ; i < 5 ; ++i ) 
     {
+      enum fmt_type cc_fmts[5] = {FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE};
       gchar text[4];
-      g_snprintf(text, 4, "CC%c", 'A' + i);
+      g_snprintf(text, 4, "%s", fmt_name (cc_fmts[i]));
       gtk_list_store_append (list_store, &iter);
       gtk_list_store_set (list_store, &iter,
                           0, text,
@@ -567,6 +547,9 @@ var_type_dialog_create(GladeXML *xml)
   g_signal_connect(dialog->ok, "clicked", G_CALLBACK(on_var_type_ok_clicked), 
                   dialog);
 
+
+  }
+
   return dialog;
 }
 
@@ -586,6 +569,8 @@ var_type_dialog_set_active_button(struct var_type_dialog *dialog, gint b)
 static void
 select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
 {
+  GtkTreePath *path ;
+
   /*
     We do this with a linear search through the model --- hardly 
     efficient, but the list is short ... */
@@ -598,11 +583,13 @@ select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
        success;
        success = gtk_tree_model_iter_next(model, &iter))
     {
+      const struct fmt_spec *spec;
+
       GValue value = {0};
 
       gtk_tree_model_get_value(model, &iter, 1, &value);
          
-      const struct fmt_spec *spec = g_value_get_pointer(&value);
+      spec = g_value_get_pointer(&value);
 
       if ( 0 == memcmp(spec, fmt, sizeof (struct fmt_spec)))
        {
@@ -610,15 +597,17 @@ select_treeview_from_format(GtkTreeView *treeview, const struct fmt_spec *fmt)
        }
     }
        
-  GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
+  path = gtk_tree_model_get_path(model, &iter);
   if ( path ) 
     {
       gtk_tree_view_set_cursor(treeview, path, 0, 0);
       gtk_tree_path_free(path);
     }
-  else
-    g_warning("Unusual date format: %s\n", fmt_to_string(fmt));
-
+  else 
+    {
+      char str[FMT_STRING_LEN_MAX + 1];
+      g_warning("Unusual date format: %s\n", fmt_to_string(fmt, str));
+    }
 }
 
 
@@ -627,7 +616,9 @@ static void
 select_treeview_from_format_type(GtkTreeView *treeview, 
                                 const int fmt_type)
 {
-  /*
+  GtkTreePath *path ;
+ /*
     We do this with a linear search through the model --- hardly 
     efficient, but the list is short ... */
   GtkTreeIter iter;
@@ -639,17 +630,19 @@ select_treeview_from_format_type(GtkTreeView *treeview,
        success;
        success = gtk_tree_model_iter_next(model, &iter))
     {
+      int spec ;
+      
       GValue value = {0};
 
       gtk_tree_model_get_value(model, &iter, 1, &value);
          
-      const int spec = * ((int *) g_value_get_pointer(&value));
+      spec = * ((int *) g_value_get_pointer(&value));
 
       if ( spec == fmt_type)
        break;
     }
        
-  GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
+  path = gtk_tree_model_get_path(model, &iter);
   if ( path ) 
     {
       gtk_tree_view_set_cursor(treeview, path, 0, 0);
@@ -664,14 +657,15 @@ select_treeview_from_format_type(GtkTreeView *treeview,
 static void
 var_type_dialog_set_state(struct var_type_dialog *dialog)
 {
+  const struct fmt_spec *write_spec ;
+  GString *str = g_string_new("");
+
   g_assert(dialog);
   g_assert(dialog->pv);
 
   /* Populate width and decimals */
-  const struct fmt_spec *write_spec = psppire_variable_get_write_spec(dialog->pv);
+  write_spec = var_get_write_format (dialog->pv);
 
-  GString *str = g_string_new("");
-  
   g_string_printf(str, "%d", write_spec->d);
 
   gtk_entry_set_text(GTK_ENTRY(dialog->entry_decimals), 
@@ -763,7 +757,7 @@ make_output_format_try (struct fmt_spec *f, int type, int w, int d)
   f->type = type;
   f->w = w;
   f->d = d;
-  return check_output_specifier (f, true);
+  return fmt_check_output (f);
 }
 
 
@@ -781,57 +775,57 @@ on_var_type_ok_clicked(GtkWidget *w, gpointer data)
   g_assert(dialog);
   g_assert(dialog->pv);
 
-  gint width = atoi(gtk_entry_get_text
-              (GTK_ENTRY(dialog->entry_width)));
+  {
+    gint width = atoi(gtk_entry_get_text
+                     (GTK_ENTRY(dialog->entry_width)));
 
-  gint decimals = atoi(gtk_entry_get_text
-                (GTK_ENTRY(dialog->entry_decimals)));
+    gint decimals = atoi(gtk_entry_get_text
+                        (GTK_ENTRY(dialog->entry_decimals)));
 
-  gint new_type = NUMERIC;
-  gint new_width = 0;
-  bool result = false;
-  struct fmt_spec spec;
-  switch (dialog->active_button) 
-    {
-    case BUTTON_STRING:
-      new_type = ALPHA;
-      new_width = width;
-      result = make_output_format_try(&spec, FMT_A, width, 0);
-      break;
-    case BUTTON_NUMERIC:
-      result = make_output_format_try(&spec, FMT_F, width, decimals);
-      break;
-    case BUTTON_COMMA:
-      result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
-      break;
-    case BUTTON_DOT:
-      result = make_output_format_try(&spec, FMT_DOT, width, decimals);
-      break;
-    case BUTTON_SCIENTIFIC:
-      result = make_output_format_try(&spec, FMT_E, width, decimals);
-      break;
-    case BUTTON_DATE:
-    case BUTTON_CUSTOM:
-      g_assert(check_output_specifier(&dialog->fmt_l, TRUE));
-      result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
-      break;
-    case BUTTON_DOLLAR:
-      result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
-      break;
-    default:
-      g_print("Unknown variable type: %d\n", dialog->active_button) ;
-      result = false;
-      break;
-    }
+    gint new_type = VAR_NUMERIC;
+    gint new_width = 0;
+    bool result = false;
+    struct fmt_spec spec;
+    switch (dialog->active_button) 
+      {
+      case BUTTON_STRING:
+       new_type = VAR_STRING;
+       new_width = width;
+       result = make_output_format_try(&spec, FMT_A, width, 0);
+       break;
+      case BUTTON_NUMERIC:
+       result = make_output_format_try(&spec, FMT_F, width, decimals);
+       break;
+      case BUTTON_COMMA:
+       result = make_output_format_try(&spec, FMT_COMMA, width, decimals);
+       break;
+      case BUTTON_DOT:
+       result = make_output_format_try(&spec, FMT_DOT, width, decimals);
+       break;
+      case BUTTON_SCIENTIFIC:
+       result = make_output_format_try(&spec, FMT_E, width, decimals);
+       break;
+      case BUTTON_DATE:
+      case BUTTON_CUSTOM:
+       g_assert(fmt_check_output(&dialog->fmt_l));
+       result = memcpy(&spec, &dialog->fmt_l, sizeof(struct fmt_spec));
+       break;
+      case BUTTON_DOLLAR:
+       result = make_output_format_try(&spec, FMT_DOLLAR, width, decimals);
+       break;
+      default:
+       g_print("Unknown variable type: %d\n", dialog->active_button) ;
+       result = false;
+       break;
+      }
 
-  if ( result == true ) 
-    {
-      psppire_variable_set_type(dialog->pv, new_type);
-      psppire_variable_set_width(dialog->pv, new_width);
-      psppire_variable_set_write_spec(dialog->pv, spec);
-      psppire_variable_set_print_spec(dialog->pv, spec);
-    }
+    if ( result == true )
+      {
+       var_set_width (dialog->pv, new_width);
+       var_set_both_formats (dialog->pv, &spec);
+      }
 
+  }
   gtk_widget_hide(dialog->window);
 
   return FALSE;