]> saetta.ns0.it Git - libgdaex/commitdiff
Prototype ColsFormatFunc. Evaluated before standard formatting funcs.
authorAndrea Zagli <azagli@libero.it>
Fri, 28 Aug 2020 08:07:01 +0000 (10:07 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 28 Aug 2020 08:07:01 +0000 (10:07 +0200)
src/gdaex.c
src/gdaex.h
tests/fill_liststore.c

index 8f1012af76503dc8191ad02452f38af06641de28..fc1ffe7cce2d29bac1348b5015fcdbe77131d4f8 100644 (file)
@@ -3355,7 +3355,7 @@ gdaex_fill_treemodel_from_sql_with_missing_func (GdaEx *gdaex,
                                                  GtkTreeModel *store,
                                                  const gchar *sql,
                                                  guint *cols_formatted,
-                                                 gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                 GdaExColsFormatFunc format_func,
                                                  GdaExFillTreeModelMissingFunc missing_func, gpointer user_data)
 {
        GdaDataModel *dm;
@@ -3371,7 +3371,7 @@ gdaex_fill_treemodel_from_sql_with_missing_func (GdaEx *gdaex,
 
        dm = gdaex_query (gdaex, _sql);
        g_free (_sql);
-       gdaex_fill_treemodel_from_datamodel_with_missing_func (gdaex, store, dm, cols_formatted, cols_format_func, missing_func, user_data);
+       gdaex_fill_treemodel_from_datamodel_with_missing_func (gdaex, store, dm, cols_formatted, format_func, missing_func, user_data);
        g_object_unref (dm);
 }
 
@@ -3380,7 +3380,7 @@ gdaex_fill_treemodel_from_datamodel_with_missing_func (GdaEx *gdaex,
                                                        GtkTreeModel *store,
                                                        GdaDataModel *dm,
                                                        guint *cols_formatted,
-                                                       gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                       GdaExColsFormatFunc format_func,
                                                        GdaExFillTreeModelMissingFunc missing_func, gpointer user_data)
 {
        GtkTreeIter iter;
@@ -3482,45 +3482,59 @@ gdaex_fill_treemodel_from_datamodel_with_missing_func (GdaEx *gdaex,
                                                                        }
                                                                else
                                                                        {
-                                                                               switch (gcol_gtypes[col])
+                                                                               gchar *formatted;
+
+                                                                               formatted = NULL;
+
+                                                                               if (format_func != NULL)
                                                                                        {
-                                                                                               case G_TYPE_STRING:
-                                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_stringify_at (gda_iter, col));
-                                                                                                       break;
-
-                                                                                               case G_TYPE_BOOLEAN:
-                                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_boolean_at (gda_iter, col) ? "X" : "");
-                                                                                                       break;
-
-                                                                                               case G_TYPE_INT:
-                                                                                                       ival = gdaex_data_model_iter_get_value_integer_at (gda_iter, col);
-                                                                                                       g_value_set_string (&gval, gdaex_format_money ((gdouble)ival, 0, FALSE));
-                                                                                                       break;
-
-                                                                                               case G_TYPE_FLOAT:
-                                                                                               case G_TYPE_DOUBLE:
-                                                                                                       dval = gdaex_data_model_iter_get_value_double_at (gda_iter, col);
-                                                                                                       g_value_set_string (&gval, gdaex_format_money (dval, -1, FALSE));
-                                                                                                       break;
-
-                                                                                               default:
-                                                                                                       if (cols_format_func != NULL)
-                                                                                                               {
-                                                                                                                       g_value_set_string (&gval, (*cols_format_func) (gda_iter, col));
-                                                                                                               }
-                                                                                                       else if (gcol_gtypes[col] == G_TYPE_DATE
-                                                                                                                || gcol_gtypes[col] == GDA_TYPE_TIMESTAMP
-                                                                                                                || gcol_gtypes[col] == G_TYPE_DATE_TIME)
-                                                                                                               {
-                                                                                                                       gdatetime = gdaex_data_model_iter_get_value_gdatetime_at (gda_iter, col);
-                                                                                                                       /* TODO find default format from locale */
-                                                                                                                       g_value_set_string (&gval, g_date_time_format (gdatetime, gcol_gtypes[col] == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S"));
-                                                                                                               }
-                                                                                                       else
+                                                                                               formatted = (*format_func) (gda_iter, col, user_data);
+                                                                                               if (formatted != NULL)
+                                                                                                       {
+                                                                                                               g_value_set_string (&gval, formatted);
+                                                                                                       }
+                                                                                       }
+
+                                                                               if (formatted == NULL)
+                                                                                       {
+                                                                                               switch (gcol_gtypes[col])
+                                                                                                       {
+                                                                                                       case G_TYPE_STRING:
+                                                                                                               g_value_set_string (&gval, gdaex_data_model_iter_get_value_stringify_at (gda_iter, col));
+                                                                                                               break;
+
+                                                                                                       case G_TYPE_BOOLEAN:
+                                                                                                               g_value_set_string (&gval, gdaex_data_model_iter_get_value_boolean_at (gda_iter, col) ? "X" : "");
+                                                                                                               break;
+
+                                                                                                       case G_TYPE_INT:
+                                                                                                               ival = gdaex_data_model_iter_get_value_integer_at (gda_iter, col);
+                                                                                                               g_value_set_string (&gval, gdaex_format_money ((gdouble)ival, 0, FALSE));
+                                                                                                               break;
+
+                                                                                                       case G_TYPE_FLOAT:
+                                                                                                       case G_TYPE_DOUBLE:
+                                                                                                               dval = gdaex_data_model_iter_get_value_double_at (gda_iter, col);
+                                                                                                               g_value_set_string (&gval, gdaex_format_money (dval, -1, FALSE));
+                                                                                                               break;
+
+                                                                                                       default:
                                                                                                                {
-                                                                                                                       g_value_set_string (&gval, gda_value_stringify (gda_data_model_iter_get_value_at (gda_iter, col)));
+                                                                                                                       if (gcol_gtypes[col] == G_TYPE_DATE
+                                                                                                                           || gcol_gtypes[col] == GDA_TYPE_TIMESTAMP
+                                                                                                                           || gcol_gtypes[col] == G_TYPE_DATE_TIME)
+                                                                                                                               {
+                                                                                                                                       gdatetime = gdaex_data_model_iter_get_value_gdatetime_at (gda_iter, col);
+                                                                                                                                       /* TODO find default format from locale */
+                                                                                                                                       g_value_set_string (&gval, g_date_time_format (gdatetime, gcol_gtypes[col] == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S"));
+                                                                                                                               }
+                                                                                                                       else
+                                                                                                                               {
+                                                                                                                                       g_value_set_string (&gval, gda_value_stringify (gda_data_model_iter_get_value_at (gda_iter, col)));
+                                                                                                                               }
+                                                                                                                       break;
                                                                                                                }
-                                                                                                       break;
+                                                                                                       }
                                                                                        }
                                                                        }
 
@@ -3602,9 +3616,9 @@ gdaex_fill_treemodel_from_sql (GdaEx *gdaex,
                                GtkTreeModel *store,
                                const gchar *sql,
                                guint *cols_formatted,
-                               gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+                               GdaExColsFormatFunc format_func)
 {
-       gdaex_fill_treemodel_from_sql_with_missing_func (gdaex, store, sql, cols_formatted, cols_format_func, NULL, NULL);
+       gdaex_fill_treemodel_from_sql_with_missing_func (gdaex, store, sql, cols_formatted, format_func, NULL, NULL);
 }
 
 void
@@ -3612,9 +3626,9 @@ gdaex_fill_treemodel_from_datamodel (GdaEx *gdaex,
                                      GtkTreeModel *store,
                                      GdaDataModel *dm,
                                      guint *cols_formatted,
-                                     gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+                                     GdaExColsFormatFunc format_func)
 {
-       gdaex_fill_treemodel_from_datamodel_with_missing_func (gdaex, store, dm, cols_formatted, cols_format_func, NULL, NULL);
+       gdaex_fill_treemodel_from_datamodel_with_missing_func (gdaex, store, dm, cols_formatted, format_func, NULL, NULL);
 }
 
 G_DEPRECATED_FOR (gdaex_fill_treemodel_from_sql_with_missing_func)
@@ -3623,7 +3637,7 @@ gdaex_fill_liststore_from_sql_with_missing_func (GdaEx *gdaex,
                                                  GtkListStore *lstore,
                                                  const gchar *sql,
                                                  guint *cols_formatted,
-                                                 gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                 GdaExColsFormatFunc format_func,
                                                  GdaExFillListStoreMissingFunc missing_func, gpointer user_data)
 {
        GdaDataModel *dm;
@@ -3639,7 +3653,7 @@ gdaex_fill_liststore_from_sql_with_missing_func (GdaEx *gdaex,
 
        dm = gdaex_query (gdaex, _sql);
        g_free (_sql);
-       gdaex_fill_liststore_from_datamodel_with_missing_func (gdaex, lstore, dm, cols_formatted, cols_format_func, missing_func, user_data);
+       gdaex_fill_liststore_from_datamodel_with_missing_func (gdaex, lstore, dm, cols_formatted, format_func, missing_func, user_data);
        g_object_unref (dm);
 }
 
@@ -3649,10 +3663,10 @@ gdaex_fill_liststore_from_datamodel_with_missing_func (GdaEx *gdaex,
                                                        GtkListStore *lstore,
                                                        GdaDataModel *dm,
                                                        guint *cols_formatted,
-                                                       gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                       GdaExColsFormatFunc format_func,
                                                        GdaExFillListStoreMissingFunc missing_func, gpointer user_data)
 {
-       gdaex_fill_treemodel_from_datamodel_with_missing_func (gdaex, GTK_TREE_MODEL (lstore), dm, cols_formatted, cols_format_func, missing_func, user_data);
+       gdaex_fill_treemodel_from_datamodel_with_missing_func (gdaex, GTK_TREE_MODEL (lstore), dm, cols_formatted, format_func, missing_func, user_data);
 }
 
 G_DEPRECATED_FOR (gdaex_fill_treemodel_from_sql)
@@ -3661,9 +3675,9 @@ gdaex_fill_liststore_from_sql (GdaEx *gdaex,
                                GtkListStore *lstore,
                                const gchar *sql,
                                guint *cols_formatted,
-                               gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+                               GdaExColsFormatFunc format_func)
 {
-       gdaex_fill_liststore_from_sql_with_missing_func (gdaex, lstore, sql, cols_formatted, cols_format_func, NULL, NULL);
+       gdaex_fill_liststore_from_sql_with_missing_func (gdaex, lstore, sql, cols_formatted, format_func, NULL, NULL);
 }
 
 G_DEPRECATED_FOR (gdaex_fill_treemodel_from_datamodel)
@@ -3672,9 +3686,9 @@ gdaex_fill_liststore_from_datamodel (GdaEx *gdaex,
                                      GtkListStore *lstore,
                                      GdaDataModel *dm,
                                      guint *cols_formatted,
-                                     gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+                                     GdaExColsFormatFunc format_func)
 {
-       gdaex_fill_liststore_from_datamodel_with_missing_func (gdaex, lstore, dm, cols_formatted, cols_format_func, NULL, NULL);
+       gdaex_fill_liststore_from_datamodel_with_missing_func (gdaex, lstore, dm, cols_formatted, format_func, NULL, NULL);
 }
 
 const gchar
index 4167574971bae9abc930fe84901ff3e2efdb5aae..02f73c3467d84011784fc24bb64a7b66a90c7473 100644 (file)
@@ -242,30 +242,31 @@ const gchar *gdaex_get_guifile (GdaEx *gdaex);
 
 GtkBuilder *gdaex_get_gtkbuilder (GdaEx *gdaex);
 
+typedef gchar *(*GdaExColsFormatFunc) (GdaDataModelIter *iter, guint col, gpointer user_data);
 typedef void (*GdaExFillTreeModelMissingFunc) (GtkTreeModel *store, GtkTreeIter *iter, gpointer user_data);
 
 void gdaex_fill_treemodel_from_sql_with_missing_func (GdaEx *gdaex,
                                                       GtkTreeModel *store,
                                                       const gchar *sql,
                                                       guint *cols_formatted,
-                                                      gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                      GdaExColsFormatFunc format_func,
                                                       GdaExFillTreeModelMissingFunc missing_func, gpointer user_data);
 void gdaex_fill_treemodel_from_datamodel_with_missing_func (GdaEx *gdaex,
                                                             GtkTreeModel *store,
                                                             GdaDataModel *dm,
                                                             guint *cols_formatted,
-                                                            gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                            GdaExColsFormatFunc format_func,
                                                             GdaExFillTreeModelMissingFunc missing_func, gpointer user_data);
 void gdaex_fill_treemodel_from_sql (GdaEx *gdaex,
                                     GtkTreeModel *store,
                                     const gchar *sql,
                                     guint *cols_formatted,
-                                    gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+                                    GdaExColsFormatFunc format_func);
 void gdaex_fill_treemodel_from_datamodel (GdaEx *gdaex,
                                           GtkTreeModel *store,
                                           GdaDataModel *dm,
                                           guint *cols_formatted,
-                                          gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+                                          GdaExColsFormatFunc format_func);
 
 G_DEPRECATED_FOR (GdaExFillTreeModelMissingFunc)
 typedef void (*GdaExFillListStoreMissingFunc) (GtkListStore *lstore, GtkTreeIter *iter, gpointer user_data);
@@ -275,7 +276,7 @@ void gdaex_fill_liststore_from_sql_with_missing_func (GdaEx *gdaex,
                                                       GtkListStore *lstore,
                                                       const gchar *sql,
                                                       guint *cols_formatted,
-                                                      gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                      GdaExColsFormatFunc format_func,
                                                       GdaExFillListStoreMissingFunc missing_func, gpointer user_data);
 
 G_DEPRECATED_FOR (gdaex_fill_treemodel_from_datamodel_with_missing_func)
@@ -283,7 +284,7 @@ void gdaex_fill_liststore_from_datamodel_with_missing_func (GdaEx *gdaex,
                                                             GtkListStore *lstore,
                                                             GdaDataModel *dm,
                                                             guint *cols_formatted,
-                                                            gchar *(*cols_format_func) (GdaDataModelIter *, guint),
+                                                            GdaExColsFormatFunc format_func,
                                                             GdaExFillListStoreMissingFunc missing_func, gpointer user_data);
 
 G_DEPRECATED_FOR (gdaex_fill_treemodel_from_sql)
@@ -291,14 +292,14 @@ void gdaex_fill_liststore_from_sql (GdaEx *gdaex,
                                     GtkListStore *lstore,
                                     const gchar *sql,
                                     guint *cols_formatted,
-                                    gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+                                    GdaExColsFormatFunc format_func);
 
 G_DEPRECATED_FOR (gdaex_fill_treemodel_from_datamodel)
 void gdaex_fill_liststore_from_datamodel (GdaEx *gdaex,
                                           GtkListStore *lstore,
                                           GdaDataModel *dm,
                                           guint *cols_formatted,
-                                          gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+                                          GdaExColsFormatFunc format_func);
 
 typedef enum
 {
index 214d1e6696d963738eb1c85e431ba138d733e063..cc8bcc3b715cc1b07ca062b2f349d8d408b78ca2 100644 (file)
 #include <gtk/gtk.h>
 #include <libgdaex.h>
 
+gchar
+*format_func (GdaDataModelIter *iter, guint col, gpointer user_data)
+{
+       if (col == 6)
+               {
+                       gboolean val;
+
+                       val = gdaex_data_model_iter_get_value_boolean_at (iter, col);
+
+                       if (val)
+                               {
+                                       return "Yes";
+                               }
+                       else
+                               {
+                                       return "No";
+                               }
+               }
+       else
+               {
+                       return NULL;
+               }
+}
+
 int
 main (int argc, char **argv)
 {
        GtkWidget *w;
        GtkWidget *scrolledw;
-       GtkListStore *lstore;
+       GtkTreeStore *lstore;
        GtkWidget *tview;
        GtkTreeViewColumn *column;
        GtkCellRenderer *renderer;
@@ -47,7 +71,7 @@ main (int argc, char **argv)
        scrolledw = gtk_scrolled_window_new (NULL, NULL);
        gtk_container_add (GTK_CONTAINER (w), scrolledw);
 
-       lstore = gtk_list_store_new (7,
+       lstore = gtk_tree_store_new (7,
                                     G_TYPE_STRING,
                                     G_TYPE_STRING,
                                     G_TYPE_STRING,
@@ -108,7 +132,7 @@ main (int argc, char **argv)
        gtk_tree_view_append_column (GTK_TREE_VIEW (tview), column);
 
        sql = g_strdup_printf ("SELECT * FROM clients");
-       gdaex_fill_liststore_from_sql (gdaex, lstore, sql, NULL, NULL);
+       gdaex_fill_treemodel_from_sql (gdaex, GTK_TREE_MODEL (lstore), sql, NULL, format_func);
        g_free (sql);
 
        gtk_container_add (GTK_CONTAINER (scrolledw), tview);