[baobab/wip/vala] Implement Drag-n-drop



commit e202142cc1f0cea4b1f135cabe8182743faa35fc
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Jan 6 02:30:27 2012 +0100

    Implement Drag-n-drop
    
    It is now possible to drop dirs on the window to start a scan.

 src/baobab-window.vala |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 135a1f0..b07c1c2 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -3,6 +3,14 @@ namespace Baobab {
 		Gtk.TreeModel? model;
 		Gtk.Builder builder;
 
+		enum DndTarget {
+			URI_LIST
+		}
+
+		const Gtk.TargetEntry dnd_target_list[] = {
+			{ "text/uri-list", 0, DndTarget.URI_LIST }
+		};
+
 		public Window (Application app) {
 			Object (application: app);
 
@@ -26,6 +34,10 @@ namespace Baobab {
 			var statusbar = builder.get_object ("statusbar") as Gtk.Widget;
 			statusbar.visible = ui_settings.get_boolean ("statusbar-visible");
 
+			// Setup drag-n-drop
+			drag_data_received.connect (on_drag_data_received);
+			set_drop_target (true);
+
 			// Go live.
 			add (builder.get_object ("window-contents") as Gtk.Widget);
 			title = _("Disk Usage Analyzer");
@@ -33,6 +45,36 @@ namespace Baobab {
 			show ();
 		}
 
+		void set_drop_target (bool active) {
+			if (active) {
+				Gtk.drag_dest_set (this,
+				                   Gtk.DestDefaults.DROP | Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT,
+				                   dnd_target_list,
+				                   Gdk.DragAction.COPY);
+			} else {
+				Gtk.drag_dest_unset (this);
+			}
+		}
+
+		void on_drag_data_received (Gtk.Widget widget, Gdk.DragContext context, int x, int y,
+		                            Gtk.SelectionData selection_data, uint target_type, uint time) {
+
+			File dir = null;
+			if (selection_data != null && (selection_data.get_length () > 0)) {
+				var uri_list = GLib.Uri.list_extract_uris ((string) selection_data.get_data ());
+				if (uri_list != null && uri_list.length == 1) {
+					dir = File.new_for_uri (uri_list[0]);
+				}
+			}
+
+			if (dir != null) {
+				// finish the drop before scanning, as the drag-n-drop can time out
+				Gtk.drag_finish (context, true, false, time);
+				scan_directory (dir);
+			} else {
+				Gtk.drag_finish (context, false, false, time);
+			}
+		}
 
 		void message (string primary_msg, string secondary_msg, Gtk.MessageType type) {
 			var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT, type,
@@ -67,6 +109,8 @@ namespace Baobab {
 				return;
 			}
 
+			set_drop_target (false);
+
 			var scanner = new ThreadedScanner ();
 			scanner.scan (directory);
 			model = scanner;
@@ -88,6 +132,8 @@ namespace Baobab {
 			                                    Scanner.Columns.ELEMENTS, null);
 			var treeview = builder.get_object ("treeview") as Gtk.TreeView;
 			treeview.model = model;
+
+			set_drop_target (true);
 		}
 	}
 }



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