[libgda] Put in one place the tree view selection handling and 3rd button click



commit 6dc0336f640b0c63ef3f712c632c15e324b7e6fd
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sat Sep 11 11:00:56 2010 +0200

    Put in one place the tree view selection handling and 3rd button click

 libgda-ui/gdaui-raw-grid.c    |   10 +-------
 libgda-ui/internal/utility.c  |   48 +++++++++++++++++++++++++++++++++++++++++
 libgda-ui/internal/utility.h  |    5 ++++
 tools/browser/Makefile.am     |    1 +
 tools/browser/doc/Makefile.am |    1 +
 tools/browser/support.c       |   29 +-----------------------
 6 files changed, 58 insertions(+), 36 deletions(-)
---
diff --git a/libgda-ui/gdaui-raw-grid.c b/libgda-ui/gdaui-raw-grid.c
index ba1d68c..7f2688f 100644
--- a/libgda-ui/gdaui-raw-grid.c
+++ b/libgda-ui/gdaui-raw-grid.c
@@ -362,6 +362,7 @@ gdaui_raw_grid_init (GdauiRawGrid *grid)
 	gtk_tree_view_set_enable_search (GTK_TREE_VIEW (tree_view), TRUE);
 	g_signal_connect (G_OBJECT (tree_view), "event",
 			  G_CALLBACK (tree_view_event_cb), grid);
+	_gdaui_setup_right_click_selection_on_treeview (tree_view);
 	g_signal_connect (G_OBJECT (tree_view), "button-press-event",
                           G_CALLBACK (tree_view_popup_button_pressed_cb), grid);
 	gtk_tree_view_set_enable_search (tree_view, FALSE);
@@ -1746,15 +1747,6 @@ tree_view_popup_button_pressed_cb (GtkWidget *widget, GdkEventButton *event, Gda
 	selection = gtk_tree_view_get_selection (tree_view);
 	sel_mode = gtk_tree_selection_get_mode (selection);
 
-	/* force selection of row on which clicked occurred */
-	GtkTreePath *path;
-	if ((event->window == gtk_tree_view_get_bin_window (tree_view)) &&
-	    gtk_tree_view_get_path_at_pos (tree_view, event->x, event->y, &path, NULL, NULL, NULL)) {
-		gtk_tree_selection_unselect_all (selection);
-		gtk_tree_selection_select_path (selection, path);
-		gtk_tree_path_free (path);
-	}
-
 	/* create the menu */
 	menu = gtk_menu_new ();
 	if (sel_mode == GTK_SELECTION_MULTIPLE)
diff --git a/libgda-ui/internal/utility.c b/libgda-ui/internal/utility.c
index 099f664..4471337 100644
--- a/libgda-ui/internal/utility.c
+++ b/libgda-ui/internal/utility.c
@@ -584,3 +584,51 @@ _gdaui_utility_iter_differ (GdaDataModelIter *iter1, GdaDataModelIter *iter2)
  out:
         return retval;
 }
+
+
+static gboolean tree_view_button_pressed_cb (GtkWidget *widget, GdkEventButton *event, gpointer unuseddata);
+/*
+ * Setup a callback on the treeview to, on right click:
+ *   - if there is no row under the cursor, then force an empty selection
+ *   OR
+ *   - if the row under the cursor is not selected, then force the selection of only that row
+ *   OR
+ *   - otherwise don't change anything
+ */
+void
+_gdaui_setup_right_click_selection_on_treeview (GtkTreeView *tview)
+{
+	g_return_if_fail (GTK_IS_TREE_VIEW (tview));
+	g_signal_connect (G_OBJECT (tview), "button-press-event",
+                          G_CALLBACK (tree_view_button_pressed_cb), NULL);
+}
+
+static gboolean
+tree_view_button_pressed_cb (GtkWidget *widget, GdkEventButton *event, gpointer unuseddata)
+{
+	GtkTreeView *tree_view;
+	GtkTreeSelection *selection;
+
+	if (event->button != 3)
+		return FALSE;
+
+	tree_view = GTK_TREE_VIEW (widget);
+	selection = gtk_tree_view_get_selection (tree_view);
+
+	/* force selection of row on which clicked occurred */
+	if (event->window == gtk_tree_view_get_bin_window (tree_view)) {
+		GtkTreePath *path;
+		if (gtk_tree_view_get_path_at_pos (tree_view, event->x, event->y, &path,
+						   NULL, NULL, NULL)) {
+			if (! gtk_tree_selection_path_is_selected (selection, path)) {
+				gtk_tree_selection_unselect_all (selection);
+				gtk_tree_selection_select_path (selection, path);
+			}
+			gtk_tree_path_free (path);
+		}
+		else
+			gtk_tree_selection_unselect_all (selection);
+	}
+
+	return FALSE;
+}
diff --git a/libgda-ui/internal/utility.h b/libgda-ui/internal/utility.h
index 88b6d22..7d2de09 100644
--- a/libgda-ui/internal/utility.h
+++ b/libgda-ui/internal/utility.h
@@ -58,3 +58,8 @@ void             _gdaui_utility_show_error (GtkWindow *parent, const gchar *form
  * Misc
  */
 gboolean         _gdaui_utility_iter_differ (GdaDataModelIter *iter1, GdaDataModelIter *iter2);
+
+/*
+ * Handle selection and right click on treeviews
+ */
+void             _gdaui_setup_right_click_selection_on_treeview (GtkTreeView *tview);
diff --git a/tools/browser/Makefile.am b/tools/browser/Makefile.am
index b0f3695..5fe4d93 100644
--- a/tools/browser/Makefile.am
+++ b/tools/browser/Makefile.am
@@ -100,6 +100,7 @@ gda_browser_4_0_LDADD=\
 	query-exec/libperspective.la \
 	data-manager/libperspective.la \
 	libbrowser.la \
+	$(top_builddir)/libgda-ui/internal/libgda-ui-internal.la \
 	$(CANVAS_LDADD) \
 	common/libcommon.la \
 	$(top_builddir)/libgda/libgda-4.0.la \
diff --git a/tools/browser/doc/Makefile.am b/tools/browser/doc/Makefile.am
index a9d7ee7..1c75c64 100644
--- a/tools/browser/doc/Makefile.am
+++ b/tools/browser/doc/Makefile.am
@@ -40,6 +40,7 @@ GTKDOC_CFLAGS = -I$(top_srcdir) \
 
 GTKDOC_LIBS = $(top_builddir)/tools/browser/data-manager/libperspective.la \
 	$(top_builddir)/tools/browser/libbrowser.la \
+	$(top_builddir)/libgda-ui/internal/libgda-ui-internal.la \
 	$(top_builddir)/tools/browser/common/libcommon.la \
 	$(top_builddir)/libgda/libgda-4.0.la \
 	$(top_builddir)/libgda-ui/libgda-ui-4.0.la \
diff --git a/tools/browser/support.c b/tools/browser/support.c
index 750f4eb..743b15a 100644
--- a/tools/browser/support.c
+++ b/tools/browser/support.c
@@ -26,6 +26,7 @@
 #include "browser-core.h"
 #include "browser-window.h"
 #include "login-dialog.h"
+#include <libgda-ui/internal/utility.h>
 
 #ifdef HAVE_MAC_INTEGRATION
 #include <gtkosxapplication.h>
@@ -508,30 +509,6 @@ browser_make_small_button (gboolean is_toggle, const gchar *label, const gchar *
 	return button;
 }
 
-static gboolean
-tree_view_button_pressed_cb (GtkWidget *widget, GdkEventButton *event, gpointer data)
-{
-	GtkTreeView *tree_view;
-	GtkTreeSelection *selection;
-
-	if (event->button != 3)
-		return FALSE;
-
-	tree_view = GTK_TREE_VIEW (widget);
-	selection = gtk_tree_view_get_selection (tree_view);
-
-	/* force selection of row on which clicked occurred */
-	GtkTreePath *path;
-	if ((event->window == gtk_tree_view_get_bin_window (tree_view)) &&
-	    gtk_tree_view_get_path_at_pos (tree_view, event->x, event->y, &path, NULL, NULL, NULL)) {
-		gtk_tree_selection_unselect_all (selection);
-		gtk_tree_selection_select_path (selection, path);
-		gtk_tree_path_free (path);
-	}
-
-	return FALSE;
-}
-
 /**
  * browser_make_tree_view
  * @model: a #GtkTreeModel
@@ -548,8 +525,6 @@ browser_make_tree_view (GtkTreeModel *model)
 	g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
 	tv = gtk_tree_view_new_with_model (model);
 
-	g_signal_connect (G_OBJECT (tv), "button-press-event",
-                          G_CALLBACK (tree_view_button_pressed_cb), NULL);
-
+	_gdaui_setup_right_click_selection_on_treeview (GTK_TREE_VIEW (tv));
 	return tv;
 }



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