[Xfce4-commits] [panel-plugins/xfce4-whiskermenu-plugin] 02/10: Move drag code into page.

noreply at xfce.org noreply at xfce.org
Tue Dec 31 11:39:51 CET 2019


This is an automated email from the git hooks/post-receive script.

g   o   t   t   c   o   d   e       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository panel-plugins/xfce4-whiskermenu-plugin.

commit 42697d2ed231028f3e1dee78ffbf9917f2c5e8c8
Author: Graeme Gott <graeme at gottcode.org>
Date:   Mon Dec 30 07:29:35 2019 -0500

    Move drag code into page.
---
 panel-plugin/favorites-page.cpp |   4 +-
 panel-plugin/launcher-view.cpp  | 173 +++++++++-------------------------------
 panel-plugin/launcher-view.h    |  22 ++---
 panel-plugin/page.cpp           | 132 ++++++++++++++++++++++++++++--
 panel-plugin/page.h             |   8 ++
 5 files changed, 178 insertions(+), 161 deletions(-)

diff --git a/panel-plugin/favorites-page.cpp b/panel-plugin/favorites-page.cpp
index 93f3d56..89d57b8 100644
--- a/panel-plugin/favorites-page.cpp
+++ b/panel-plugin/favorites-page.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013, 2015, 2016 Graeme Gott <graeme at gottcode.org>
+ * Copyright (C) 2013, 2015, 2016, 2019 Graeme Gott <graeme at gottcode.org>
  *
  * This library is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -35,7 +35,7 @@ using namespace WhiskerMenu;
 FavoritesPage::FavoritesPage(Window* window) :
 	Page(window)
 {
-	get_view()->set_reorderable(true);
+	set_reorderable(true);
 }
 
 //-----------------------------------------------------------------------------
diff --git a/panel-plugin/launcher-view.cpp b/panel-plugin/launcher-view.cpp
index 6ebdd6a..42a0bf1 100644
--- a/panel-plugin/launcher-view.cpp
+++ b/panel-plugin/launcher-view.cpp
@@ -17,12 +17,9 @@
 
 #include "launcher-view.h"
 
-#include "launcher.h"
+#include "category.h"
 #include "settings.h"
 #include "slot.h"
-#include "window.h"
-
-#include <algorithm>
 
 #include <exo/exo.h>
 #include <gdk/gdkkeysyms.h>
@@ -40,15 +37,10 @@ static gboolean is_separator(GtkTreeModel* model, GtkTreeIter* iter, gpointer)
 
 //-----------------------------------------------------------------------------
 
-LauncherView::LauncherView(Window* window) :
-	m_window(window),
+LauncherView::LauncherView() :
 	m_model(NULL),
 	m_icon_size(0),
-	m_pressed_launcher(NULL),
-	m_drag_enabled(true),
-	m_launcher_dragged(false),
-	m_row_activated(false),
-	m_reorderable(false)
+	m_row_activated(false)
 {
 	// Create the view
 	m_view = GTK_TREE_VIEW(exo_tree_view_new());
@@ -73,13 +65,9 @@ LauncherView::LauncherView(Window* window) :
 
 	// Handle drag-and-drop
 	g_signal_connect_slot(m_view, "button-press-event", &LauncherView::on_button_press_event, this);
-	g_signal_connect_slot(m_view, "button-release-event", &LauncherView::on_button_release_event, this);
-	g_signal_connect_slot(m_view, "drag-data-get", &LauncherView::on_drag_data_get, this);
-	g_signal_connect_slot(m_view, "drag-end", &LauncherView::on_drag_end, this);
 	g_signal_connect_slot(m_view, "row-activated", &LauncherView::on_row_activated, this);
 	g_signal_connect_slot<GtkTreeView*,GtkTreeIter*,GtkTreePath*>(m_view, "test-collapse-row", &LauncherView::test_row_toggle, this);
 	g_signal_connect_slot<GtkTreeView*,GtkTreeIter*,GtkTreePath*>(m_view, "test-expand-row", &LauncherView::test_row_toggle, this);
-	set_reorderable(false);
 }
 
 //-----------------------------------------------------------------------------
@@ -167,47 +155,6 @@ void LauncherView::set_fixed_height_mode(bool fixed_height)
 
 //-----------------------------------------------------------------------------
 
-void LauncherView::set_reorderable(bool reorderable)
-{
-	m_reorderable = reorderable;
-	if (m_reorderable)
-	{
-		const GtkTargetEntry row_targets[] = {
-			{ g_strdup("GTK_TREE_MODEL_ROW"), GTK_TARGET_SAME_WIDGET, 0 },
-			{ g_strdup("text/uri-list"), GTK_TARGET_OTHER_APP, 1 }
-		};
-
-		gtk_tree_view_enable_model_drag_source(m_view,
-				GDK_BUTTON1_MASK,
-				row_targets, 2,
-				GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY));
-
-		gtk_tree_view_enable_model_drag_dest(m_view,
-				row_targets, 1,
-				GDK_ACTION_MOVE);
-
-		g_free(row_targets[0].target);
-		g_free(row_targets[1].target);
-	}
-	else
-	{
-		const GtkTargetEntry row_targets[] = {
-			{ g_strdup("text/uri-list"), GTK_TARGET_OTHER_APP, 1 }
-		};
-
-		gtk_tree_view_enable_model_drag_source(m_view,
-				GDK_BUTTON1_MASK,
-				row_targets, 1,
-				GDK_ACTION_COPY);
-
-		gtk_tree_view_unset_rows_drag_dest(m_view);
-
-		g_free(row_targets[0].target);
-	}
-}
-
-//-----------------------------------------------------------------------------
-
 void LauncherView::set_selection_mode(GtkSelectionMode mode)
 {
 	GtkTreeSelection* selection = gtk_tree_view_get_selection(m_view);
@@ -253,6 +200,34 @@ void LauncherView::unset_model()
 
 //-----------------------------------------------------------------------------
 
+void LauncherView::set_drag_source(GdkModifierType start_button_mask, const GtkTargetEntry* targets, gint n_targets, GdkDragAction actions)
+{
+	gtk_tree_view_enable_model_drag_source(m_view, start_button_mask, targets, n_targets, actions);
+}
+
+//-----------------------------------------------------------------------------
+
+void LauncherView::set_drag_dest(const GtkTargetEntry* targets, gint n_targets, GdkDragAction actions)
+{
+	gtk_tree_view_enable_model_drag_dest(m_view, targets, n_targets, actions);
+}
+
+//-----------------------------------------------------------------------------
+
+void LauncherView::unset_drag_source()
+{
+	gtk_tree_view_unset_rows_drag_source(m_view);
+}
+
+//-----------------------------------------------------------------------------
+
+void LauncherView::unset_drag_dest()
+{
+	gtk_tree_view_unset_rows_drag_dest(m_view);
+}
+
+//-----------------------------------------------------------------------------
+
 void LauncherView::reload_icon_size()
 {
 	// Force exo to reload SVG icons
@@ -318,96 +293,22 @@ gboolean LauncherView::on_key_release_event(GtkWidget*, GdkEvent* event)
 
 //-----------------------------------------------------------------------------
 
-gboolean LauncherView::on_button_press_event(GtkWidget*, GdkEvent* event)
+gboolean LauncherView::on_button_press_event(GtkWidget*, GdkEvent*)
 {
 	m_row_activated = false;
 
-	GdkEventButton* button_event = reinterpret_cast<GdkEventButton*>(event);
-	if (button_event->button != 1)
-	{
-		return false;
-	}
-
-	m_launcher_dragged = false;
-	m_pressed_launcher = NULL;
-
-	GtkTreeIter iter;
-	if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(m_view), NULL, &iter))
-	{
-		return false;
-	}
-
-	gtk_tree_model_get(m_model, &iter, LauncherView::COLUMN_LAUNCHER, &m_pressed_launcher, -1);
-	if (!m_pressed_launcher || (m_pressed_launcher->get_type() != Launcher::Type))
-	{
-		m_pressed_launcher = NULL;
-		m_drag_enabled = false;
-		gtk_tree_view_unset_rows_drag_source(m_view);
-		gtk_tree_view_unset_rows_drag_dest(m_view);
-	}
-	else if (!m_drag_enabled)
-	{
-		m_drag_enabled = true;
-		set_reorderable(m_reorderable);
-	}
-
-	return false;
-}
-
-//-----------------------------------------------------------------------------
-
-gboolean LauncherView::on_button_release_event(GtkWidget*, GdkEvent* event)
-{
-	GdkEventButton* button_event = reinterpret_cast<GdkEventButton*>(event);
-	if (button_event->button != 1)
-	{
-		return false;
-	}
-
-	if (m_launcher_dragged)
-	{
-		m_window->hide();
-		m_launcher_dragged = false;
-	}
 	return false;
 }
 
 //-----------------------------------------------------------------------------
 
-void LauncherView::on_drag_data_get(GtkWidget*, GdkDragContext*, GtkSelectionData* data, guint info, guint)
-{
-	if ((info != 1) || !m_pressed_launcher)
-	{
-		return;
-	}
-
-	gchar* uris[2] = { m_pressed_launcher->get_uri(), NULL };
-	if (uris[0] != NULL)
-	{
-		gtk_selection_data_set_uris(data, uris);
-		g_free(uris[0]);
-	}
-
-	m_launcher_dragged = true;
-}
-
-//-----------------------------------------------------------------------------
-
-void LauncherView::on_drag_end(GtkWidget*, GdkDragContext*)
-{
-	if (m_launcher_dragged)
-	{
-		m_window->hide();
-		m_launcher_dragged = false;
-	}
-	m_pressed_launcher = NULL;
-}
-
-//-----------------------------------------------------------------------------
-
 void LauncherView::on_row_activated(GtkTreeView* tree_view, GtkTreePath* path, GtkTreeViewColumn*)
 {
-	if (m_pressed_launcher)
+	Element* element = NULL;
+	GtkTreeIter iter;
+	gtk_tree_model_get_iter(m_model, &iter, path);
+	gtk_tree_model_get(m_model, &iter, COLUMN_LAUNCHER, &element, -1);
+	if (element && (element->get_type() != Category::Type))
 	{
 		return;
 	}
diff --git a/panel-plugin/launcher-view.h b/panel-plugin/launcher-view.h
index a80f131..95c28a4 100644
--- a/panel-plugin/launcher-view.h
+++ b/panel-plugin/launcher-view.h
@@ -23,14 +23,10 @@
 namespace WhiskerMenu
 {
 
-class IconSize;
-class Launcher;
-class Window;
-
 class LauncherView
 {
 public:
-	LauncherView(Window* window);
+	LauncherView();
 	~LauncherView();
 
 	GtkWidget* get_widget() const
@@ -47,7 +43,6 @@ public:
 	void set_cursor(GtkTreePath* path);
 
 	void set_fixed_height_mode(bool fixed_height);
-	void set_reorderable(bool reorderable);
 	void set_selection_mode(GtkSelectionMode mode);
 
 	void hide_tooltips();
@@ -63,6 +58,11 @@ public:
 	void set_model(GtkTreeModel* model);
 	void unset_model();
 
+	void set_drag_source(GdkModifierType start_button_mask, const GtkTargetEntry* targets, gint n_targets, GdkDragAction actions);
+	void set_drag_dest(const GtkTargetEntry* targets, gint n_targets, GdkDragAction actions);
+	void unset_drag_source();
+	void unset_drag_dest();
+
 	void reload_icon_size();
 
 	enum Columns
@@ -79,25 +79,15 @@ private:
 	gboolean on_key_press_event(GtkWidget*, GdkEvent* event);
 	gboolean on_key_release_event(GtkWidget*, GdkEvent* event);
 	gboolean on_button_press_event(GtkWidget*, GdkEvent* event);
-	gboolean on_button_release_event(GtkWidget*, GdkEvent* event);
-	void on_drag_data_get(GtkWidget*, GdkDragContext*, GtkSelectionData* data, guint info, guint);
-	void on_drag_end(GtkWidget*, GdkDragContext*);
 	void on_row_activated(GtkTreeView* tree_view, GtkTreePath* path, GtkTreeViewColumn* column);
 	gboolean test_row_toggle();
 
 private:
-	Window* m_window;
-
 	GtkTreeModel* m_model;
 	GtkTreeView* m_view;
 	GtkTreeViewColumn* m_column;
 	int m_icon_size;
-
-	Launcher* m_pressed_launcher;
-	bool m_drag_enabled;
-	bool m_launcher_dragged;
 	bool m_row_activated;
-	bool m_reorderable;
 };
 
 }
diff --git a/panel-plugin/page.cpp b/panel-plugin/page.cpp
index 45c66ce..3b84091 100644
--- a/panel-plugin/page.cpp
+++ b/panel-plugin/page.cpp
@@ -34,11 +34,17 @@ using namespace WhiskerMenu;
 
 Page::Page(Window* window) :
 	m_window(window),
-	m_selected_launcher(NULL)
+	m_selected_launcher(NULL),
+	m_drag_enabled(true),
+	m_launcher_dragged(false),
+	m_reorderable(false)
 {
 	// Create view
-	m_view = new LauncherView(window);
+	m_view = new LauncherView();
 	g_signal_connect_slot(m_view->get_widget(), "button-press-event", &Page::view_button_press_event, this);
+	g_signal_connect_slot(m_view->get_widget(), "button-release-event", &Page::view_button_release_event, this);
+	g_signal_connect_slot(m_view->get_widget(), "drag-data-get", &Page::view_drag_data_get, this);
+	g_signal_connect_slot(m_view->get_widget(), "drag-end", &Page::view_drag_end, this);
 	g_signal_connect_slot(m_view->get_widget(), "popup-menu", &Page::view_popup_menu_event, this);
 	g_signal_connect_slot(m_view->get_widget(), "row-activated", &Page::item_activated, this);
 	g_signal_connect_swapped(m_view->get_widget(), "start-interactive-search", G_CALLBACK(gtk_widget_grab_focus), m_window->get_search_entry());
@@ -80,6 +86,44 @@ void Page::reset_selection()
 
 //-----------------------------------------------------------------------------
 
+void Page::set_reorderable(bool reorderable)
+{
+	m_reorderable = reorderable;
+	if (m_reorderable)
+	{
+		const GtkTargetEntry row_targets[] = {
+			{ g_strdup("GTK_TREE_MODEL_ROW"), GTK_TARGET_SAME_WIDGET, 0 },
+			{ g_strdup("text/uri-list"), GTK_TARGET_OTHER_APP, 1 }
+		};
+
+		m_view->set_drag_source(GDK_BUTTON1_MASK,
+				row_targets, 2,
+				GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY));
+
+		m_view->set_drag_dest(row_targets, 1,
+				GDK_ACTION_MOVE);
+
+		g_free(row_targets[0].target);
+		g_free(row_targets[1].target);
+	}
+	else
+	{
+		const GtkTargetEntry row_targets[] = {
+			{ g_strdup("text/uri-list"), GTK_TARGET_OTHER_APP, 1 }
+		};
+
+		m_view->set_drag_source(GDK_BUTTON1_MASK,
+				row_targets, 1,
+				GDK_ACTION_COPY);
+
+		m_view->unset_drag_dest();
+
+		g_free(row_targets[0].target);
+	}
+}
+
+//-----------------------------------------------------------------------------
+
 bool Page::remember_launcher(Launcher*)
 {
 	return true;
@@ -142,20 +186,94 @@ void Page::item_action_activated(GtkMenuItem* menuitem, DesktopAction* action)
 gboolean Page::view_button_press_event(GtkWidget*, GdkEvent* event)
 {
 	GdkEventButton* button_event = reinterpret_cast<GdkEventButton*>(event);
-	if (button_event->button != 3)
+
+	m_launcher_dragged = false;
+
+	GtkTreePath* path = m_view->get_path_at_pos(button_event->x, button_event->y);
+	if (!path)
 	{
 		return false;
 	}
 
-	GtkTreePath* path = m_view->get_path_at_pos(button_event->x, button_event->y);
-	if (!path)
+	if (button_event->button == 3)
 	{
+		create_context_menu(path, event);
+		return true;
+	}
+	else if (button_event->button != 1)
+	{
+		gtk_tree_path_free(path);
 		return false;
 	}
 
-	create_context_menu(path, event);
+	GtkTreeModel* model = m_view->get_model();
+	GtkTreeIter iter;
+	gtk_tree_model_get_iter(model, &iter, path);
+	gtk_tree_path_free(path);
+	gtk_tree_model_get(model, &iter, LauncherView::COLUMN_LAUNCHER, &m_selected_launcher, -1);
+	if (!m_selected_launcher || (m_selected_launcher->get_type() != Launcher::Type))
+	{
+		m_selected_launcher = NULL;
+		m_drag_enabled = false;
+		m_view->unset_drag_source();
+		m_view->unset_drag_dest();
+	}
+	else if (!m_drag_enabled)
+	{
+		m_drag_enabled = true;
+		set_reorderable(m_reorderable);
+	}
 
-	return true;
+	return false;
+}
+
+//-----------------------------------------------------------------------------
+
+gboolean Page::view_button_release_event(GtkWidget*, GdkEvent* event)
+{
+	GdkEventButton* button_event = reinterpret_cast<GdkEventButton*>(event);
+	if (button_event->button != 1)
+	{
+		return false;
+	}
+
+	if (m_launcher_dragged)
+	{
+		m_window->hide();
+		m_launcher_dragged = false;
+	}
+
+	return false;
+}
+
+//-----------------------------------------------------------------------------
+
+void Page::view_drag_data_get(GtkWidget*, GdkDragContext*, GtkSelectionData* data, guint info, guint)
+{
+	if ((info != 1) || !m_selected_launcher)
+	{
+		return;
+	}
+
+	gchar* uris[2] = { m_selected_launcher->get_uri(), NULL };
+	if (uris[0] != NULL)
+	{
+		gtk_selection_data_set_uris(data, uris);
+		g_free(uris[0]);
+	}
+
+	m_launcher_dragged = true;
+}
+
+//-----------------------------------------------------------------------------
+
+void Page::view_drag_end(GtkWidget*, GdkDragContext*)
+{
+	if (m_launcher_dragged)
+	{
+		m_window->hide();
+		m_launcher_dragged = false;
+	}
 }
 
 //-----------------------------------------------------------------------------
diff --git a/panel-plugin/page.h b/panel-plugin/page.h
index 58968bb..cdfcaca 100644
--- a/panel-plugin/page.h
+++ b/panel-plugin/page.h
@@ -52,11 +52,16 @@ protected:
 		return m_window;
 	}
 
+	void set_reorderable(bool reorderable);
+
 private:
 	virtual bool remember_launcher(Launcher* launcher);
 	void item_activated(GtkTreeView* view, GtkTreePath* path, GtkTreeViewColumn*);
 	void item_action_activated(GtkMenuItem* menuitem, DesktopAction* action);
 	gboolean view_button_press_event(GtkWidget* view, GdkEvent* event);
+	gboolean view_button_release_event(GtkWidget*, GdkEvent* event);
+	void view_drag_data_get(GtkWidget*, GdkDragContext*, GtkSelectionData* data, guint info, guint);
+	void view_drag_end(GtkWidget*, GdkDragContext*);
 	gboolean view_popup_menu_event(GtkWidget* view);
 	void on_unmap();
 	void destroy_context_menu(GtkMenuShell* menu);
@@ -73,6 +78,9 @@ private:
 	GtkWidget* m_widget;
 	LauncherView* m_view;
 	Launcher* m_selected_launcher;
+	bool m_drag_enabled;
+	bool m_launcher_dragged;
+	bool m_reorderable;
 };
 
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list