]> saetta.ns0.it Git - libgdaex/commitdiff
Added function GdaExGrid::get_widget.
authorAndrea Zagli <azagli@libero.it>
Sat, 4 Jun 2011 07:44:41 +0000 (09:44 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 4 Jun 2011 07:44:41 +0000 (09:44 +0200)
Bugfixes.

.gitignore
src/grid.c
src/grid.h
src/gridcolumn.c
src/gridcolumn.h
tests/Makefile.am
tests/grid.c [new file with mode: 0644]

index 2b69f3227a22d6df7e4d33e7bcc004fbb8fe90bf..fe2982b923fe835a6dcbc45dd421e187a3a97d1d 100644 (file)
@@ -33,3 +33,5 @@ tests/.deps/
 *.bak
 libgdaex*tar*
 tests/test_prefix*
+tests/grid
+tests/grid.exe
index 56e6b2c8955c28648646ca878bf44d8a5265987e..55ea846cb570e96b444a459dba6720e4400cee1b 100644 (file)
@@ -1,9 +1,9 @@
 /*
  *  grid.c
  *
- *  Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2010-2011 Andrea Zagli <azagli@libero.it>
  *
- *  This file is part of libgdaex_grid.
+ *  This file is part of libgdaex.
  *  
  *  libgdaex_grid is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -41,18 +41,15 @@ static void gdaex_grid_get_property (GObject *object,
                                GValue *value,
                                GParamSpec *pspec);
 
-#define GDAEX_GRID_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GDAEX_TYPE_GRID, GdaExGridPrivate))
+static GtkTreeModel *gdaex_grid_get_model (GdaExGrid *grid);
+static GtkTreeView *gdaex_grid_get_view (GdaExGrid *grid);
 
-typedef struct
-       {
-               GtkTreeViewColumn *view_col;
-               GdaExGridColumn *grid_col;
-       } _Column;
+#define GDAEX_GRID_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GDAEX_TYPE_GRID, GdaExGridPrivate))
 
 typedef struct _GdaExGridPrivate GdaExGridPrivate;
 struct _GdaExGridPrivate
        {
-               GPtrArray *columns; /* _Column */
+               GPtrArray *columns; /* GdaExGridColumn */
        };
 
 G_DEFINE_TYPE (GdaExGrid, gdaex_grid, G_TYPE_OBJECT)
@@ -91,18 +88,12 @@ gdaex_grid_add_column (GdaExGrid *grid, GdaExGridColumn *column)
 {
        GdaExGridPrivate *priv;
 
-       _Column *col;
-
        g_return_if_fail (GDAEX_IS_GRID (grid));
        g_return_if_fail (GDAEX_IS_GRID_COLUMN (column));
 
        priv = GDAEX_GRID_GET_PRIVATE (grid);
 
-       col = g_new0 (_Column, 1);
-       col->view_col = NULL;
-       col->grid_col = g_memdup (column, sizeof (GdaExGridColumn));
-
-       g_ptr_array_add (priv->columns, col);
+       g_ptr_array_add (priv->columns, g_object_ref (column));
 }
 
 void
@@ -142,12 +133,19 @@ gdaex_grid_clear (GdaExGrid *grid)
        priv->columns = g_ptr_array_new ();
 }
 
+GtkWidget
+*gdaex_grid_get_widget (GdaExGrid *grid)
+{
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), NULL);
+
+       return GTK_WIDGET (gdaex_grid_get_view (grid));
+}
+
 /* PRIVATE */
 static void
 gdaex_grid_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
 {
        GdaExGrid *gdaex_grid = GDAEX_GRID (object);
-
        GdaExGridPrivate *priv = GDAEX_GRID_GET_PRIVATE (gdaex_grid);
 
        switch (property_id)
@@ -162,7 +160,6 @@ static void
 gdaex_grid_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
 {
        GdaExGrid *gdaex_grid = GDAEX_GRID (object);
-
        GdaExGridPrivate *priv = GDAEX_GRID_GET_PRIVATE (gdaex_grid);
 
        switch (property_id)
@@ -172,3 +169,62 @@ gdaex_grid_get_property (GObject *object, guint property_id, GValue *value, GPar
                                break;
                }
 }
+
+static GtkTreeModel
+*gdaex_grid_get_model (GdaExGrid *grid)
+{
+       /* TODO for now it returns always a GtkListStore */
+
+       GtkListStore *store;
+
+       GdaExGridPrivate *priv;
+
+       guint col;
+       GType *gtype;
+
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), NULL);
+
+       priv = GDAEX_GRID_GET_PRIVATE (grid);
+
+       gtype = g_new0 (GType, priv->columns->len);
+
+       for (col = 0; col < priv->columns->len; col++)
+               {
+                       gtype[col] = gdaex_grid_column_get_gtype ((GdaExGridColumn *)g_ptr_array_index (priv->columns, col));
+               }
+
+       store = gtk_list_store_newv (priv->columns->len, gtype);
+
+       return GTK_TREE_MODEL (store);
+}
+
+static GtkTreeView
+*gdaex_grid_get_view (GdaExGrid *grid)
+{
+       GtkWidget *view;
+
+       GdaExGridPrivate *priv;
+
+       GtkTreeModel *model;
+       GdaExGridColumn *gcolumn;
+
+       guint col;
+
+       g_return_val_if_fail (GDAEX_IS_GRID (grid), NULL);
+
+       priv = GDAEX_GRID_GET_PRIVATE (grid);
+
+       model = gdaex_grid_get_model (grid);
+       view = gtk_tree_view_new_with_model (model);
+
+       for (col = 0; col < priv->columns->len; col++)
+               {
+                       gcolumn = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
+                       if (gdaex_grid_column_get_visible (gcolumn))
+                               {
+                                       gtk_tree_view_append_column (GTK_TREE_VIEW (view), gdaex_grid_column_get_column (gcolumn, col));
+                               }
+               }
+
+       return GTK_TREE_VIEW (view);
+}
index 7dac15ecb444e43c2d1ce7f8d1db273033734c12..7439fb3ff4678e8183fcaec656ab4436aab7a2f7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  grid.h
  *
- *  Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2010-2011 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
  *  
@@ -32,7 +32,7 @@
 G_BEGIN_DECLS
 
 
-#define GDAEX_TYPE_GRID                 (gdaex_get_type ())
+#define GDAEX_TYPE_GRID                 (gdaex_grid_get_type ())
 #define GDAEX_GRID(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDAEX_TYPE_GRID, GdaExGrid))
 #define GDAEX_GRID_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GDAEX_TYPE_GRID, GdaExGridClass))
 #define GDAEX_IS_GRID(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDAEX_TYPE_GRID))
@@ -63,6 +63,8 @@ void gdaex_grid_add_columns (GdaExGrid *grid, GSList *columns);
 
 void gdaex_grid_clear (GdaExGrid *grid);
 
+GtkWidget *gdaex_grid_get_widget (GdaExGrid *grid);
+
 
 G_END_DECLS
 
index d280f0895aa0fae827a988b6a836086a5352562c..c9d51fc3c054ed3b4893d3307ff5e7aa1978f1ee 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  gridcolumn.c
  *
- *  Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2010-2011 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex_grid_column.
  *  
@@ -51,6 +51,7 @@ struct _GdaExGridColumnPrivate
                gboolean resizable;
                gboolean sortable;
                gboolean reorderable;
+               guint decimals;
        };
 
 G_DEFINE_TYPE (GdaExGridColumn, gdaex_grid_column, G_TYPE_OBJECT)
@@ -78,6 +79,7 @@ gdaex_grid_column_init (GdaExGridColumn *gdaex_grid_column)
        priv->resizable = FALSE;
        priv->sortable = FALSE;
        priv->reorderable = FALSE;
+       priv->decimals = 0;
 }
 
 GdaExGridColumn
@@ -87,7 +89,8 @@ GdaExGridColumn
                         gboolean visible,
                         gboolean resizable,
                         gboolean sortable,
-                        gboolean reorderable)
+                        gboolean reorderable,
+                        guint decimals)
 {
        GdaExGridColumn *gdaex_grid_column = GDAEX_GRID_COLUMN (g_object_new (gdaex_grid_column_get_type (), NULL));
 
@@ -100,6 +103,7 @@ GdaExGridColumn
        gdaex_grid_column_set_resizable (gdaex_grid_column, resizable);
        gdaex_grid_column_set_sortable (gdaex_grid_column, sortable);
        gdaex_grid_column_set_reorderable (gdaex_grid_column, reorderable);
+       gdaex_grid_column_set_decimals (gdaex_grid_column, decimals);
 
        return gdaex_grid_column;
 }
@@ -246,6 +250,72 @@ gdaex_grid_column_get_reorderable (GdaExGridColumn *column)
        return priv->reorderable;
 }
 
+void
+gdaex_grid_column_set_decimals (GdaExGridColumn *column, guint decimals)
+{
+       g_return_if_fail (GDAEX_IS_GRID_COLUMN (column));
+
+       GdaExGridColumnPrivate *priv = GDAEX_GRID_COLUMN_GET_PRIVATE (column);
+
+       priv->decimals = decimals;
+}
+
+guint
+gdaex_grid_column_get_decimals (GdaExGridColumn *column)
+{
+       g_return_val_if_fail (GDAEX_IS_GRID_COLUMN (column), FALSE);
+
+       GdaExGridColumnPrivate *priv = GDAEX_GRID_COLUMN_GET_PRIVATE (column);
+
+       return priv->decimals;
+}
+
+GtkTreeViewColumn
+*gdaex_grid_column_get_column (GdaExGridColumn *column, gint model_column_number)
+{
+       GtkTreeViewColumn *col;
+
+       GdaExGridColumnPrivate *priv;
+
+       GtkCellRenderer *renderer;
+
+       g_return_val_if_fail (GDAEX_IS_GRID_COLUMN (column), NULL);
+
+       priv = GDAEX_GRID_COLUMN_GET_PRIVATE (column);
+
+       renderer = NULL;
+       switch (priv->type)
+               {
+                       case G_TYPE_STRING:
+                               renderer = gtk_cell_renderer_text_new ();
+                               break;
+
+                       case G_TYPE_INT:
+                       case G_TYPE_FLOAT:
+                       case G_TYPE_DOUBLE:
+                               renderer = gtk_cell_renderer_spin_new ();
+                               break;
+               }
+       if (renderer == NULL)
+               {
+                       g_warning ("Error on creating the renderer.");
+                       return NULL;
+               }
+
+       col = gtk_tree_view_column_new ();
+
+       gtk_tree_view_column_pack_start (col, renderer, TRUE);
+
+       gtk_tree_view_column_add_attribute (col, renderer, "text", model_column_number);
+
+       gtk_tree_view_column_set_title (col, priv->title);
+       gtk_tree_view_column_set_resizable (col, priv->resizable);
+       gtk_tree_view_column_set_clickable (col, priv->sortable);
+       gtk_tree_view_column_set_reorderable (col, priv->reorderable);
+
+       return col;
+}
+
 /* PRIVATE */
 static void
 gdaex_grid_column_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
index bfba21049622120014f2e4b3f2b76900b0035b0d..fa2acb17881d954c62a82ef6eede8c7e19569ada 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  gridcolumn.h
  *
- *  Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2010-2011 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
  *
 #include <glib.h>
 #include <glib-object.h>
 
+#include <gtk/gtk.h>
+
 G_BEGIN_DECLS
 
 
-#define GDAEX_TYPE_GRID_COLUMN                 (gdaex_get_type ())
+#define GDAEX_TYPE_GRID_COLUMN                 (gdaex_grid_column_get_type ())
 #define GDAEX_GRID_COLUMN(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDAEX_TYPE_GRID_COLUMN, GdaExGridColumn))
 #define GDAEX_GRID_COLUMN_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GDAEX_TYPE_GRID_COLUMN, GdaExGridColumnClass))
 #define GDAEX_IS_GRID_COLUMN(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDAEX_TYPE_GRID_COLUMN))
@@ -60,7 +62,8 @@ GdaExGridColumn *gdaex_grid_column_new (const gchar *title,
                                         gboolean visible,
                                         gboolean resizable,
                                         gboolean sortable,
-                                        gboolean reorderable);
+                                        gboolean reorderable,
+                                        guint decimals);
 
 void gdaex_grid_column_set_title (GdaExGridColumn *column, const gchar *title);
 const gchar *gdaex_grid_column_get_title (GdaExGridColumn *column);
@@ -83,6 +86,11 @@ gboolean gdaex_grid_column_get_sortable (GdaExGridColumn *column);
 void gdaex_grid_column_set_reorderable (GdaExGridColumn *column, gboolean reorderable);
 gboolean gdaex_grid_column_get_reorderable (GdaExGridColumn *column);
 
+void gdaex_grid_column_set_decimals (GdaExGridColumn *column, guint decimals);
+guint gdaex_grid_column_get_decimals (GdaExGridColumn *column);
+
+GtkTreeViewColumn *gdaex_grid_column_get_column (GdaExGridColumn *column, gint model_column_number);
+
 
 G_END_DECLS
 
index 403f842178c291f7e14f1988f5fe15f53826cb10..0d65354418288b65c8f0c16c8c086fa3c59b69aa 100644 (file)
@@ -5,7 +5,8 @@ AM_CPPFLAGS = $(GDAEX_CFLAGS) \
               -I$(top_srcdir)/src \
               -DTESTSDIR="\"@abs_builddir@\""
 
-noinst_PROGRAMS = test_prefix
+noinst_PROGRAMS = test_prefix \
+                  grid
 
 LDADD = $(top_builddir)/src/libgdaex.la
 
diff --git a/tests/grid.c b/tests/grid.c
new file mode 100644 (file)
index 0000000..775c13e
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2011 Andrea Zagli <azagli@libero.it>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <libgdaex.h>
+
+int
+main (int argc, char **argv)
+{
+       GtkWidget *w;
+
+       GdaExGrid *grid;
+       GdaExGridColumn *gcol;
+       GtkWidget *wgrid;
+
+       gtk_init (&argc, &argv);
+
+       w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+
+       grid = gdaex_grid_new ();
+
+       gcol = gdaex_grid_column_new ("ID", "id", G_TYPE_INT, TRUE, FALSE, FALSE, FALSE, 0);
+       gdaex_grid_add_column (grid, gcol);
+
+       gcol = gdaex_grid_column_new ("Name", "name", G_TYPE_STRING, TRUE, TRUE, TRUE, TRUE, 0);
+       gdaex_grid_add_column (grid, gcol);
+
+       wgrid = gdaex_grid_get_widget (grid);
+       gtk_container_add (GTK_CONTAINER (w), wgrid);
+
+       gtk_widget_show_all (w);
+
+       gtk_main ();
+
+       return 0;
+}