]> saetta.ns0.it Git - libgdaex/commitdiff
GdaExGrid: added chance, with contestual menu, to toggle columns visibility.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 3 Oct 2013 14:06:47 +0000 (16:06 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 3 Oct 2013 14:06:47 +0000 (16:06 +0200)
src/grid.c
src/grid.h
src/gridcolumn.c
tests/grid.c

index d2700ba97099e82f0ba2897cc14f117d4075ed24..10dfe70d9a68b6c5cbc74d6cf03c164ef9c9a00c 100644 (file)
@@ -52,6 +52,14 @@ static gboolean gdaex_grid_on_key_release_event (GtkWidget *widget,
                             gpointer user_data);
 #endif
 
+static gboolean gdaex_grid_on_button_press_event (GtkWidget *widget,
+                                                  GdkEventButton *event,
+                                                  gpointer user_data);
+static gboolean gdaex_grid_on_popup_menu (GtkWidget *widget,
+                                          gpointer user_data);
+static void gdaex_grid_on_menu_item_toggled (GtkCheckMenuItem *checkmenuitem,
+                                             gpointer user_data);
+
 static GtkTreeModel *gdaex_grid_get_model (GdaExGrid *grid);
 static GtkTreeView *gdaex_grid_get_view (GdaExGrid *grid);
 
@@ -66,6 +74,7 @@ struct _GdaExGridPrivate
 
                GtkTreeModel *model;
                GtkWidget *view;
+               GtkWidget *menu;
        };
 
 G_DEFINE_TYPE (GdaExGrid, gdaex_grid, G_TYPE_OBJECT)
@@ -89,6 +98,7 @@ gdaex_grid_init (GdaExGrid *gdaex_grid)
        priv->columns = g_ptr_array_new ();
        priv->title = NULL;
        priv->view = NULL;
+       priv->menu = NULL;
 }
 
 GdaExGrid
@@ -438,6 +448,8 @@ static GtkTreeView
 
        guint col;
 
+       GtkWidget *mitem;
+
        g_return_val_if_fail (GDAEX_IS_GRID (grid), NULL);
 
        priv = GDAEX_GRID_GET_PRIVATE (grid);
@@ -447,44 +459,75 @@ static GtkTreeView
 
        gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), TRUE);
 
+       if (priv->menu != NULL)
+               {
+                       g_free (priv->menu);
+               }
+       if (priv->columns->len > 0)
+               {
+                       priv->menu = gtk_menu_new ();
+               }
+       else
+               {
+                       priv->menu = NULL;
+               }
+
        for (col = 0; col < priv->columns->len; col++)
                {
                        gcolumn = (GdaExGridColumn *)g_ptr_array_index (priv->columns, col);
-                       if (gdaex_grid_column_get_visible (gcolumn))
+                       vcolumn = gdaex_grid_column_get_column (gcolumn);
+                       if (vcolumn)
                                {
-                                       vcolumn = gdaex_grid_column_get_column (gcolumn);
-                                       if (vcolumn)
+                                       cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (vcolumn));
+                                       cells = g_list_first (cells);
+
+                                       if (gdaex_grid_column_get_gtype (gcolumn) == G_TYPE_BOOLEAN)
                                                {
-                                                       cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (vcolumn));
-                                                       cells = g_list_first (cells);
+                                                       gtk_tree_view_column_add_attribute (vcolumn, (GtkCellRenderer *)cells->data, "active", col);
+                                               }
+                                       else
+                                               {
+                                                       gtk_tree_view_column_add_attribute (vcolumn, (GtkCellRenderer *)cells->data, "text", col);
+                                               }
 
-                                                       if (gdaex_grid_column_get_gtype (gcolumn) == G_TYPE_BOOLEAN)
-                                                               {
-                                                                       gtk_tree_view_column_add_attribute (vcolumn, (GtkCellRenderer *)cells->data, "active", col);
-                                                               }
-                                                       else
-                                                               {
-                                                                       gtk_tree_view_column_add_attribute (vcolumn, (GtkCellRenderer *)cells->data, "text", col);
-                                                               }
+                                       if (gdaex_grid_column_get_reorderable (gcolumn))
+                                               {
+                                                       gtk_tree_view_column_set_sort_column_id (vcolumn, col);
+                                               }
 
-                                                       if (gdaex_grid_column_get_reorderable (gcolumn))
-                                                               {
-                                                                       gtk_tree_view_column_set_sort_column_id (vcolumn, col);
-                                                               }
+                                       gtk_tree_view_append_column (GTK_TREE_VIEW (view), vcolumn);
 
-                                                       gtk_tree_view_append_column (GTK_TREE_VIEW (view), vcolumn);
-                                               }
+                                       /* popup menu */
+                                       mitem = gtk_check_menu_item_new_with_label (gdaex_grid_column_get_title (gcolumn));
+                                       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mitem), gdaex_grid_column_get_visible (gcolumn));
+
+                                       g_object_set_data (G_OBJECT (mitem), "GdaExGridColumn", gcolumn);
+
+                                       g_signal_connect (G_OBJECT (mitem), "toggled",
+                                                         G_CALLBACK (gdaex_grid_on_menu_item_toggled), (gpointer)grid);
+
+                                       gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), mitem);
+                                       gtk_widget_show (mitem);
                                }
                }
 
        priv->view = view;
+       if (priv->menu != NULL)
+               {
+                       gtk_menu_attach_to_widget (GTK_MENU (priv->menu), priv->view, NULL);
+
+                       g_signal_connect (G_OBJECT (priv->view), "button-press-event",
+                                         G_CALLBACK (gdaex_grid_on_button_press_event), (gpointer)grid);
+                       g_signal_connect (G_OBJECT (priv->view), "popup-menu",
+                                         G_CALLBACK (gdaex_grid_on_popup_menu), (gpointer)grid);
+               }
 
 #ifdef REPTOOL_FOUND
        g_signal_connect (view,
                          "key-release-event", G_CALLBACK (gdaex_grid_on_key_release_event), (gpointer)grid);
 #endif
 
-       return GTK_TREE_VIEW (view);
+       return GTK_TREE_VIEW (priv->view);
 }
 
 #ifdef REPTOOL_FOUND
@@ -546,3 +589,49 @@ gdaex_grid_on_key_release_event (GtkWidget *widget,
        return FALSE;
 }
 #endif
+
+static gboolean
+gdaex_grid_on_button_press_event (GtkWidget *widget,
+                                  GdkEventButton *event,
+                                  gpointer user_data)
+{
+       GdaExGridPrivate *priv;
+
+       if (event->type == GDK_BUTTON_PRESS && event->button == 3)
+               {
+                       priv = GDAEX_GRID_GET_PRIVATE (user_data);
+
+                       gtk_menu_popup (GTK_MENU (priv->menu), NULL, NULL, NULL, NULL,
+                                       event->button, event->time);
+                       return TRUE;
+               }
+
+       return FALSE;
+}
+
+static gboolean
+gdaex_grid_on_popup_menu (GtkWidget *widget,
+                          gpointer user_data)
+{
+       GdaExGridPrivate *priv;
+
+       priv = GDAEX_GRID_GET_PRIVATE (user_data);
+       gtk_menu_popup (GTK_MENU (priv->menu), NULL, NULL, NULL, NULL,
+                       0, gtk_get_current_event_time ());
+
+       return TRUE;
+}
+
+static void
+gdaex_grid_on_menu_item_toggled (GtkCheckMenuItem *checkmenuitem,
+                                 gpointer user_data)
+{
+       GdaExGridPrivate *priv;
+
+       priv = GDAEX_GRID_GET_PRIVATE (user_data);
+
+       GdaExGridColumn *gcolumn = g_object_get_data (G_OBJECT (checkmenuitem), "GdaExGridColumn");
+
+       gdaex_grid_column_set_visible (gcolumn,
+                                      gtk_check_menu_item_get_active (checkmenuitem));
+}
index 3abc5e26b0d76ecdcae62e66c3e7e1386581f0ed..8d288801cc82d714eda133484bb66f8a375c0335 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  grid.h
  *
- *  Copyright (C) 2010-2011 Andrea Zagli <azagli@libero.it>
+ *  Copyright (C) 2010-2013 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
  *  
index 5d4f2f2b093034f0578f54a61677b2a1866f3196..ed84b67dc88fd8cabfadaa3fcd39a8a500f2ee95 100644 (file)
@@ -3,7 +3,7 @@
  *
  *  Copyright (C) 2010-2013 Andrea Zagli <azagli@libero.it>
  *
- *  This file is part of libgdaex_grid_column.
+ *  This file is part of libgdaex.
  *  
  *  libgdaex_grid_column is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -84,6 +84,7 @@ gdaex_grid_column_init (GdaExGridColumn *gdaex_grid_column)
        priv->sortable = FALSE;
        priv->reorderable = FALSE;
        priv->decimals = -1;
+       priv->vcolumn = NULL;
 }
 
 GdaExGridColumn
@@ -136,6 +137,11 @@ gdaex_grid_column_set_title (GdaExGridColumn *column, const gchar *title)
 
        g_free (priv->title);
        priv->title = g_strdup (title);
+
+       if (priv->vcolumn != NULL)
+               {
+                       gtk_tree_view_column_set_title (priv->vcolumn, priv->title);
+               }
 }
 
 const gchar
@@ -197,6 +203,11 @@ gdaex_grid_column_set_visible (GdaExGridColumn *column, gboolean visible)
        GdaExGridColumnPrivate *priv = GDAEX_GRID_COLUMN_GET_PRIVATE (column);
 
        priv->visible = visible;
+
+       if (priv->vcolumn != NULL)
+               {
+                       gtk_tree_view_column_set_visible (priv->vcolumn, priv->visible);
+               }
 }
 
 gboolean
@@ -217,6 +228,11 @@ gdaex_grid_column_set_resizable (GdaExGridColumn *column, gboolean resizable)
        GdaExGridColumnPrivate *priv = GDAEX_GRID_COLUMN_GET_PRIVATE (column);
 
        priv->resizable = resizable;
+
+       if (priv->vcolumn != NULL)
+               {
+                       gtk_tree_view_column_set_resizable (priv->vcolumn, priv->resizable);
+               }
 }
 
 gboolean
@@ -237,6 +253,11 @@ gdaex_grid_column_set_sortable (GdaExGridColumn *column, gboolean sortable)
        GdaExGridColumnPrivate *priv = GDAEX_GRID_COLUMN_GET_PRIVATE (column);
 
        priv->sortable = sortable;
+
+       if (priv->vcolumn != NULL)
+               {
+                       gtk_tree_view_column_set_clickable (priv->vcolumn, priv->sortable);
+               }
 }
 
 gboolean
@@ -257,6 +278,11 @@ gdaex_grid_column_set_reorderable (GdaExGridColumn *column, gboolean reorderable
        GdaExGridColumnPrivate *priv = GDAEX_GRID_COLUMN_GET_PRIVATE (column);
 
        priv->reorderable = reorderable;
+
+       if (priv->vcolumn != NULL)
+               {
+                       gtk_tree_view_column_set_reorderable (priv->vcolumn, priv->reorderable);
+               }
 }
 
 gboolean
@@ -346,6 +372,8 @@ GtkTreeViewColumn
        gtk_tree_view_column_set_clickable (priv->vcolumn, priv->sortable);
        gtk_tree_view_column_set_reorderable (priv->vcolumn, priv->reorderable);
 
+       gtk_tree_view_column_set_visible (priv->vcolumn, priv->visible);
+
        return priv->vcolumn;
 }
 
index 0519b3bca0c3dae8fa868bbea57ac1fa33efe9bf..134c03fe928cc5151c3a2388c7c0ab32c1973114 100644 (file)
 
 #include <libgdaex.h>
 
+GdaExGridColumn *gcol_birthday;
+
+static void
+on_btn_birthday_clicked (GtkButton *button,
+                         gpointer user_data)
+{
+       gdaex_grid_column_set_visible (gcol_birthday, !gdaex_grid_column_get_visible (gcol_birthday));
+}
+
 int
 main (int argc, char **argv)
 {
        GdaEx *gdaex;
 
        GtkWidget *w;
+       GtkWidget *vbox;
        GtkWidget *scrolledw;
+       GtkWidget *hbtnbox;
+       GtkWidget *btn;
 
        GdaExGrid *grid;
        GdaExGridColumn *gcol;
@@ -45,8 +57,11 @@ main (int argc, char **argv)
        g_signal_connect (w, "destroy",
                          G_CALLBACK (gtk_main_quit), NULL);
 
+       vbox = gtk_vbox_new (FALSE, 0);
+       gtk_container_add (GTK_CONTAINER (w), vbox);
+
        scrolledw = gtk_scrolled_window_new (NULL, NULL);
-       gtk_container_add (GTK_CONTAINER (w), scrolledw);
+       gtk_box_pack_start (GTK_BOX (vbox), scrolledw, TRUE, TRUE, 0);
 
        grid = gdaex_grid_new ();
        gdaex_grid_set_title (grid, "The grid title");
@@ -63,8 +78,8 @@ main (int argc, char **argv)
        gcol = gdaex_grid_column_new_defaults ("Age", "age", G_TYPE_INT);
        gdaex_grid_add_column (grid, gcol);
 
-       gcol = gdaex_grid_column_new ("Birthday", "birthday", G_TYPE_DATE, TRUE, TRUE, TRUE, TRUE, -1);
-       gdaex_grid_add_column (grid, gcol);
+       gcol_birthday = gdaex_grid_column_new ("Birthday", "birthday", G_TYPE_DATE, FALSE, TRUE, TRUE, TRUE, -1);
+       gdaex_grid_add_column (grid, gcol_birthday);
 
        gcol = gdaex_grid_column_new_defaults ("Incoming", "incoming", G_TYPE_DOUBLE);
        gdaex_grid_add_column (grid, gcol);
@@ -77,6 +92,15 @@ main (int argc, char **argv)
 
        gdaex_grid_fill_from_sql (grid, gdaex, "SELECT * FROM clients", NULL);
 
+       hbtnbox = gtk_hbutton_box_new ();
+       gtk_box_pack_start (GTK_BOX (vbox), hbtnbox, TRUE, TRUE, 0);
+
+       btn = gtk_button_new_with_label ("Hide/Show Birthday");
+       gtk_box_pack_start (GTK_BOX (hbtnbox), btn, TRUE, TRUE, 0);
+
+       g_signal_connect (G_OBJECT (btn), "clicked",
+                         G_CALLBACK (on_btn_birthday_clicked), NULL);
+
        gtk_widget_show_all (w);
 
        gtk_main ();