[libgda/gtk3] GdauiBasicForm & GdauiRawGrid: added popup menu to hide/show data entries & columns



commit d90bed9322f47f6ed73ff643204774a0fd170360
Author: Vivien Malerba <malerba gnome-db org>
Date:   Mon Feb 14 18:04:25 2011 +0100

    GdauiBasicForm & GdauiRawGrid: added popup menu to hide/show data entries & columns

 libgda-ui/gdaui-basic-form.c |  166 +++++++++++++++++++++++++++++++++---------
 libgda-ui/gdaui-raw-grid.c   |   36 +++++++++-
 2 files changed, 165 insertions(+), 37 deletions(-)
---
diff --git a/libgda-ui/gdaui-basic-form.c b/libgda-ui/gdaui-basic-form.c
index fce5e9c..2499166 100644
--- a/libgda-ui/gdaui-basic-form.c
+++ b/libgda-ui/gdaui-basic-form.c
@@ -1,6 +1,6 @@
 /* gdaui-basic-form.c
  *
- * Copyright (C) 2002 - 2010 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2002 - 2011 Vivien Malerba <malerba gnome-db org>
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
@@ -91,6 +91,8 @@ typedef struct {
 	}               pack;
 	
 } SingleEntry;
+static void real_gdaui_basic_form_entry_set_visible (GdauiBasicForm *form,
+						      SingleEntry *sentry, gboolean show);
 static SingleEntry *get_single_entry_for_holder (GdauiBasicForm *form, GdaHolder *param);
 static SingleEntry *get_single_entry_for_id (GdauiBasicForm *form, const gchar *id);
 static void create_entry_widget (SingleEntry *sentry);
@@ -149,18 +151,20 @@ size_group_free (SizeGroup *sg)
 
 struct _GdauiBasicFormPriv
 {
-	GdaSet                 *set;
-	GdauiSet               *set_info;
-	GSList                 *s_entries;/* list of SingleEntry pointers */
-	GHashTable             *place_holders; /* key = place holder ID, value = a GtkWidget pointer */
+	GdaSet     *set;
+	GdauiSet   *set_info;
+	GSList     *s_entries;/* list of SingleEntry pointers */
+	GHashTable *place_holders; /* key = place holder ID, value = a GtkWidget pointer */
 
-	GtkWidget              *top_container;
+	GtkWidget  *top_container;
 
-	gboolean                headers_sensitive;
-	gboolean                show_actions;
-	gboolean                entries_auto_default;
+	gboolean    headers_sensitive;
+	gboolean    show_actions;
+	gboolean    entries_auto_default;
 
-	GSList                 *size_groups; /* list of SizeGroup pointers */
+	GSList     *size_groups; /* list of SizeGroup pointers */
+
+	GtkWidget  *mainbox;
 };
 
 
@@ -288,8 +292,78 @@ gdaui_basic_form_class_init (GdauiBasicFormClass * class)
 }
 
 static void
-gdaui_basic_form_init (GdauiBasicForm * wid)
+hidden_entry_mitem_toggled_cb (GtkCheckMenuItem *check, GdauiBasicForm *form)
+{
+	SingleEntry *sentry;
+	sentry = g_object_get_data (G_OBJECT (check), "s");
+	g_assert (sentry);
+	real_gdaui_basic_form_entry_set_visible (form, sentry,
+						 gtk_check_menu_item_get_active (check));
+}
+
+static void
+do_popup_menu (GdauiBasicForm *form, GdkEventButton *event)
+{
+	int button, event_time;
+	GtkWidget *menu, *submenu, *mitem;
+	GSList *list;
+	
+	menu = gtk_menu_new ();
+	mitem = gtk_menu_item_new_with_label (_("Shown data entries"));
+	gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
+	gtk_widget_show (mitem);
+	
+	submenu = gtk_menu_new ();
+	gtk_widget_show (submenu);
+	gtk_menu_item_set_submenu (GTK_MENU_ITEM (mitem), submenu);
+	
+	for (list = form->priv->s_entries; list; list = list->next) {
+		SingleEntry *sentry = (SingleEntry*) list->data;
+		mitem = gtk_check_menu_item_new_with_label (sentry->label_title);
+		gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mitem), !sentry->hidden);
+		gtk_menu_shell_append (GTK_MENU_SHELL (submenu), mitem);
+		gtk_widget_show (mitem);
+
+		g_object_set_data (G_OBJECT (mitem), "s", sentry);
+		g_signal_connect (mitem, "toggled",
+				  G_CALLBACK (hidden_entry_mitem_toggled_cb), form);
+	}
+		
+	if (event) {
+		button = event->button;
+		event_time = event->time;
+	}
+	else {
+		button = 0;
+		event_time = gtk_get_current_event_time ();
+	}
+
+	gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 
+			button, event_time);
+}
+
+static gboolean
+popup_menu_cb (G_GNUC_UNUSED GtkWidget *wid, GdauiBasicForm *form)
+{
+	do_popup_menu (form, NULL);
+	return TRUE;
+}
+
+static gboolean
+button_press_event_cb (G_GNUC_UNUSED GdauiBasicForm *wid, GdkEventButton *event, GdauiBasicForm *form)
+{
+	if (event->button == 3 && event->type == GDK_BUTTON_PRESS) {
+		do_popup_menu (form, event);
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
+static void
+gdaui_basic_form_init (GdauiBasicForm *wid)
 {
+	GtkWidget *evbox;
 	wid->priv = g_new0 (GdauiBasicFormPriv, 1);
 	wid->priv->set = NULL;
 	wid->priv->s_entries = NULL;
@@ -299,6 +373,18 @@ gdaui_basic_form_init (GdauiBasicForm * wid)
 	wid->priv->headers_sensitive = FALSE;
 	wid->priv->show_actions = FALSE;
 	wid->priv->entries_auto_default = FALSE;
+
+	evbox = gtk_event_box_new ();
+	gtk_widget_show (evbox);
+	gtk_box_pack_start (GTK_BOX (wid), evbox, TRUE, TRUE, 0);
+	wid->priv->mainbox = gtk_vbox_new (FALSE, 0);
+	gtk_widget_show (wid->priv->mainbox);
+	gtk_container_add (GTK_CONTAINER (evbox), wid->priv->mainbox);
+
+	g_signal_connect (evbox, "popup-menu",
+			  G_CALLBACK (popup_menu_cb), wid);
+	g_signal_connect (evbox, "button-press-event",
+			  G_CALLBACK (button_press_event_cb), wid);
 }
 
 /**
@@ -1070,7 +1156,7 @@ pack_entries_in_table (GdauiBasicForm *form)
 	gtk_table_set_row_spacings (GTK_TABLE (table), SPACING);
 	gtk_table_set_col_spacings (GTK_TABLE (table), SPACING);
 	form->priv->top_container = table;
-	gtk_box_pack_start (GTK_BOX (form), table, TRUE, TRUE, 0);
+	gtk_box_pack_start (GTK_BOX (form->priv->mainbox), table, TRUE, TRUE, 0);
 	for (list = form->priv->s_entries, i = 0;
 	     list;
 	     list = list->next, i++) {
@@ -1106,7 +1192,7 @@ pack_entries_in_xml_layout (GdauiBasicForm *form, xmlNodePtr form_node)
 	}
 
 	top = load_xml_layout_children (form, form_node);
-	gtk_box_pack_start (GTK_BOX (form), top, TRUE, TRUE, 0);
+	gtk_box_pack_start (GTK_BOX (form->priv->mainbox), top, TRUE, TRUE, 0);
 	gtk_widget_show_all (top);
 	mark_not_null_entry_labels (form, TRUE);
 }
@@ -1714,31 +1800,11 @@ gdaui_basic_form_reset (GdauiBasicForm *form)
 	}
 }
 
-
-/**
- * gdaui_basic_form_entry_set_visible:
- * @form: a #GdauiBasicForm widget
- * @param: a #GdaHolder object
- * @show: set to %TRUE to show the data entry, and to %FALSE to hide it
- *
- * Shows or hides the #GdauiDataEntry in @form which corresponds to the
- * @param parameter
- *
- * Since: 4.2
- */
-void
-gdaui_basic_form_entry_set_visible (GdauiBasicForm *form, GdaHolder *param, gboolean show)
+static void
+real_gdaui_basic_form_entry_set_visible (GdauiBasicForm *form, SingleEntry *sentry, gboolean show)
 {
-	SingleEntry *sentry;
-
 	g_return_if_fail (GDAUI_IS_BASIC_FORM (form));
-	g_return_if_fail (GDA_IS_HOLDER (param));
-
-	sentry = get_single_entry_for_holder (form, param);
-	if (!sentry) {
-		g_warning (_("Can't find data entry for GdaHolder"));
-		return;
-	}
+	g_return_if_fail (sentry);
 
 	if (sentry->entry) {
 		if (show) {
@@ -1779,6 +1845,34 @@ gdaui_basic_form_entry_set_visible (GdauiBasicForm *form, GdaHolder *param, gboo
 }
 
 /**
+ * gdaui_basic_form_entry_set_visible:
+ * @form: a #GdauiBasicForm widget
+ * @param: a #GdaHolder object
+ * @show: set to %TRUE to show the data entry, and to %FALSE to hide it
+ *
+ * Shows or hides the #GdauiDataEntry in @form which corresponds to the
+ * @param parameter
+ *
+ * Since: 4.2
+ */
+void
+gdaui_basic_form_entry_set_visible (GdauiBasicForm *form, GdaHolder *param, gboolean show)
+{
+	SingleEntry *sentry;
+
+	g_return_if_fail (GDAUI_IS_BASIC_FORM (form));
+	g_return_if_fail (GDA_IS_HOLDER (param));
+
+	sentry = get_single_entry_for_holder (form, param);
+	if (!sentry) {
+		g_warning (_("Can't find data entry for GdaHolder"));
+		return;
+	}
+
+	real_gdaui_basic_form_entry_set_visible (form, sentry, show);
+}
+
+/**
  * gdaui_basic_form_entry_grab_focus:
  * @form: a #GdauiBasicForm widget
  * @param: a #GdaHolder object
diff --git a/libgda-ui/gdaui-raw-grid.c b/libgda-ui/gdaui-raw-grid.c
index 8a436d5..0e090b9 100644
--- a/libgda-ui/gdaui-raw-grid.c
+++ b/libgda-ui/gdaui-raw-grid.c
@@ -1706,13 +1706,27 @@ static GtkWidget *new_check_menu_item (const gchar *label,
 				       GCallback cb_func,
 				       gpointer user_data);
 
+static void
+hidden_column_mitem_toggled_cb (GtkCheckMenuItem *check, GdauiRawGrid *grid)
+{
+	ColumnData *cdata;
+	gboolean act;
+	cdata = g_object_get_data (G_OBJECT (check), "c");
+	g_assert (cdata);
+	act = gtk_check_menu_item_get_active (check);
+	gtk_tree_view_column_set_visible (cdata->column, act);
+	cdata->hidden = !act;
+}
+
 static gint
 tree_view_popup_button_pressed_cb (G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event, GdauiRawGrid *grid)
 {
-	GtkWidget *menu;
+	GtkWidget *menu, *submenu;
 	GtkTreeView *tree_view;
 	GtkTreeSelection *selection;
 	GtkSelectionMode sel_mode;
+	GSList *list;
+	GtkWidget *mitem;
 
 	if (event->button != 3)
 		return FALSE;
@@ -1723,6 +1737,26 @@ tree_view_popup_button_pressed_cb (G_GNUC_UNUSED GtkWidget *widget, GdkEventButt
 
 	/* create the menu */
 	menu = gtk_menu_new ();
+	mitem = gtk_menu_item_new_with_label (_("Shown columns"));
+	gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
+	gtk_widget_show (mitem);
+	
+	submenu = gtk_menu_new ();
+	gtk_widget_show (submenu);
+	gtk_menu_item_set_submenu (GTK_MENU_ITEM (mitem), submenu);
+	for (list = grid->priv->columns_data; list; list = list->next) {
+		ColumnData *cdata = (ColumnData*) list->data;
+
+		mitem = gtk_check_menu_item_new_with_label (cdata->title);
+		gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mitem), !cdata->hidden);
+		gtk_menu_shell_append (GTK_MENU_SHELL (submenu), mitem);
+		gtk_widget_show (mitem);
+
+		g_object_set_data (G_OBJECT (mitem), "c", cdata);
+		g_signal_connect (mitem, "toggled",
+				  G_CALLBACK (hidden_column_mitem_toggled_cb), grid);
+	}
+
 	if (sel_mode == GTK_SELECTION_MULTIPLE)
 		gtk_menu_shell_append (GTK_MENU_SHELL (menu),
 				       new_menu_item (_("Select _All"), FALSE,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]