From 86af4fad3bb560767c2eb356c194f07570a22773 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Thu, 3 Oct 2013 16:06:47 +0200 Subject: [PATCH] GdaExGrid: added chance, with contestual menu, to toggle columns visibility. --- src/grid.c | 129 +++++++++++++++++++++++++++++++++++++++-------- src/grid.h | 2 +- src/gridcolumn.c | 30 ++++++++++- tests/grid.c | 30 +++++++++-- 4 files changed, 166 insertions(+), 25 deletions(-) diff --git a/src/grid.c b/src/grid.c index d2700ba..10dfe70 100644 --- a/src/grid.c +++ b/src/grid.c @@ -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)); +} diff --git a/src/grid.h b/src/grid.h index 3abc5e2..8d28880 100644 --- a/src/grid.h +++ b/src/grid.h @@ -1,7 +1,7 @@ /* * grid.h * - * Copyright (C) 2010-2011 Andrea Zagli + * Copyright (C) 2010-2013 Andrea Zagli * * This file is part of libgdaex. * diff --git a/src/gridcolumn.c b/src/gridcolumn.c index 5d4f2f2..ed84b67 100644 --- a/src/gridcolumn.c +++ b/src/gridcolumn.c @@ -3,7 +3,7 @@ * * Copyright (C) 2010-2013 Andrea Zagli * - * 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; } diff --git a/tests/grid.c b/tests/grid.c index 0519b3b..134c03f 100644 --- a/tests/grid.c +++ b/tests/grid.c @@ -18,13 +18,25 @@ #include +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 (); -- 2.49.0