[gedit/wip/tab-dnd] Move tab DND support to GeditNotebook
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/tab-dnd] Move tab DND support to GeditNotebook
- Date: Thu, 25 Jun 2015 11:55:25 +0000 (UTC)
commit 48e714a0a6f89a0d147036937092290b134a00b5
Author: Sébastien Wilmet <swilmet gnome org>
Date: Thu Jun 25 13:39:25 2015 +0200
Move tab DND support to GeditNotebook
So GeditView doesn't depend on GeditNotebook. A class should not be
aware of who contains it.
It is more logical to implement the tab DND support (to drag and drop a
tab to another notebook) in GeditNotebook.
gedit/gedit-notebook.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
gedit/gedit-view.c | 57 +--------------------------------
2 files changed, 85 insertions(+), 56 deletions(-)
---
diff --git a/gedit/gedit-notebook.c b/gedit/gedit-notebook.c
index ec0deec..6189411 100644
--- a/gedit/gedit-notebook.c
+++ b/gedit/gedit-notebook.c
@@ -33,6 +33,11 @@
#define GEDIT_NOTEBOOK_GROUP_NAME "GeditNotebookGroup"
+/* The DND targets defined in GeditView start at 100.
+ * Those defined in GtkSourceView start at 200.
+ */
+#define TARGET_TAB 102
+
struct _GeditNotebookPrivate
{
/* History of focused pages. The first element contains the most recent
@@ -460,6 +465,66 @@ gedit_notebook_init (GeditNotebook *notebook)
gtk_container_set_border_width (GTK_CONTAINER (notebook), 0);
}
+static GtkWidget *
+get_notebook_from_view (GtkWidget *view)
+{
+ GtkWidget *widget;
+
+ widget = view;
+
+ do
+ {
+ widget = gtk_widget_get_parent (widget);
+ }
+ while (!GEDIT_IS_NOTEBOOK (widget));
+
+ return widget;
+}
+
+static void
+drag_data_received_cb (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ GtkSelectionData *selection_data,
+ guint info,
+ guint timestamp)
+{
+ GtkWidget *notebook;
+ GtkWidget *new_notebook;
+ GtkWidget *page;
+
+ if (info != TARGET_TAB)
+ {
+ return;
+ }
+
+ notebook = gtk_drag_get_source_widget (context);
+
+ if (!GTK_IS_WIDGET (notebook))
+ {
+ return;
+ }
+
+ page = *(GtkWidget **) gtk_selection_data_get_data (selection_data);
+ g_return_if_fail (page != NULL);
+
+ /* We need to iterate and get the notebook of the target view
+ * because we can have several notebooks per window.
+ */
+ new_notebook = get_notebook_from_view (widget);
+
+ if (notebook != new_notebook)
+ {
+ gedit_notebook_move_tab (GEDIT_NOTEBOOK (notebook),
+ GEDIT_NOTEBOOK (new_notebook),
+ GEDIT_TAB (page),
+ 0);
+ }
+
+ gtk_drag_finish (context, TRUE, TRUE, timestamp);
+}
+
/**
* gedit_notebook_add_tab:
* @notebook: a #GeditNotebook
@@ -476,6 +541,8 @@ gedit_notebook_add_tab (GeditNotebook *notebook,
gboolean jump_to)
{
GtkWidget *tab_label;
+ GeditView *view;
+ GtkTargetList *target_list;
g_return_if_fail (GEDIT_IS_NOTEBOOK (notebook));
g_return_if_fail (GEDIT_IS_TAB (tab));
@@ -500,6 +567,23 @@ gedit_notebook_add_tab (GeditNotebook *notebook,
"tab-expand", TRUE,
NULL);
+ /* Drag and drop support: move a tab to another notebook. */
+ view = gedit_tab_get_view (tab);
+ target_list = gtk_drag_dest_get_target_list (GTK_WIDGET (view));
+
+ if (target_list != NULL)
+ {
+ gtk_target_list_add (target_list,
+ gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB"),
+ GTK_TARGET_SAME_APP,
+ TARGET_TAB);
+ }
+
+ g_signal_connect (view,
+ "drag-data-received",
+ G_CALLBACK (drag_data_received_cb),
+ NULL);
+
/* The signal handler may have reordered the tabs */
position = gtk_notebook_page_num (GTK_NOTEBOOK (notebook),
GTK_WIDGET (tab));
diff --git a/gedit/gedit-view.c b/gedit/gedit-view.c
index 9ba45da..b4dc063 100644
--- a/gedit/gedit-view.c
+++ b/gedit/gedit-view.c
@@ -33,15 +33,13 @@
#include "gedit-settings.h"
#include "gedit-app.h"
#include "gedit-app-private.h"
-#include "gedit-notebook.h"
#define GEDIT_VIEW_SCROLL_MARGIN 0.02
enum
{
TARGET_URI_LIST = 100,
- TARGET_XDNDDIRECTSAVE,
- TARGET_TAB
+ TARGET_XDNDDIRECTSAVE
};
struct _GeditViewPrivate
@@ -142,10 +140,6 @@ gedit_view_init (GeditView *view)
0,
TARGET_XDNDDIRECTSAVE);
gtk_target_list_add_uri_targets (target_list, TARGET_URI_LIST);
- gtk_target_list_add (target_list,
- gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB"),
- GTK_TARGET_SAME_APP,
- TARGET_TAB);
}
view->priv->extensions =
@@ -333,22 +327,6 @@ gedit_view_drag_motion (GtkWidget *widget,
return drop_zone;
}
-static GtkWidget *
-get_notebook_from_view (GtkWidget *view)
-{
- GtkWidget *widget;
-
- widget = view;
-
- do
- {
- widget = gtk_widget_get_parent (widget);
- }
- while (!GEDIT_IS_NOTEBOOK (widget));
-
- return widget;
-}
-
static void
gedit_view_drag_data_received (GtkWidget *widget,
GdkDragContext *context,
@@ -378,39 +356,6 @@ gedit_view_drag_data_received (GtkWidget *widget,
break;
}
- case TARGET_TAB:
- {
- GtkWidget *notebook;
- GtkWidget *new_notebook;
- GtkWidget *page;
-
- notebook = gtk_drag_get_source_widget (context);
-
- if (!GTK_IS_WIDGET (notebook))
- {
- return;
- }
-
- page = *(GtkWidget **) gtk_selection_data_get_data (selection_data);
- g_return_if_fail (page != NULL);
-
- /* We need to iterate and get the notebook of the target view
- * because we can have several notebooks per window.
- */
- new_notebook = get_notebook_from_view (widget);
-
- if (notebook != new_notebook)
- {
- gedit_notebook_move_tab (GEDIT_NOTEBOOK (notebook),
- GEDIT_NOTEBOOK (new_notebook),
- GEDIT_TAB (page),
- 0);
- }
-
- gtk_drag_finish (context, TRUE, TRUE, timestamp);
-
- break;
- }
case TARGET_XDNDDIRECTSAVE:
{
GeditView *view;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]