[baobab/wip/vala: 15/53] Implement Drag and Drop.



commit 776222089f1ec5cdac0148d7ecedb7ad099bd1ec
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Jan 6 10:51:56 2012 +0100

    Implement Drag and Drop.
    
    Start scanning when a directory uri is dropped on the window.

 src/baobab-window.vala |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 47 insertions(+), 0 deletions(-)
---
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 0404279..3738ac7 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -4,6 +4,14 @@ namespace Baobab {
 		Chart rings_chart;
 		Chart treemap;
 
+		enum DndTargets {
+			URI_LIST
+		}
+
+		const Gtk.TargetEntry dnd_target_list[1] = {
+		    {"text/uri-list", 0, DndTargets.URI_LIST}
+		};
+
 		public Window (Application app) {
 			Object (application: app);
 
@@ -34,6 +42,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);
+			enable_drop ();
+
 			// Go live.
 			add (builder.get_object ("window-contents") as Gtk.Widget);
 			title = _("Disk Usage Analyzer");
@@ -65,6 +77,37 @@ namespace Baobab {
 			print ("r\n");
 		}
 
+		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) && (target_type == DndTargets.URI_LIST)) {
+				var uris = GLib.Uri.list_extract_uris ((string) selection_data.get_data ());
+				if (uris != null && uris.length == 1) {
+					dir = File.new_for_uri (uris[0]);
+				}
+			}
+
+			if (dir != null) {
+				// finish drop before scanning, since the it can time out
+				Gtk.drag_finish (context, true, false, time);
+				scan_directory (dir);
+			} else {
+				Gtk.drag_finish (context, false, false, time);
+			}
+		}
+
+		void enable_drop () {
+			Gtk.drag_dest_set (this,
+			                   Gtk.DestDefaults.DROP | Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT,
+			                   dnd_target_list,
+			                   Gdk.DragAction.COPY);
+		}
+
+		void disable_drop () {
+			Gtk.drag_dest_unset (this);
+		}
+
 		private const GLib.ActionEntry[] action_entries = {
 			{ "scan-home",       scan_home_activated       },
 			{ "scan-filesystem", scan_filesystem_activated },
@@ -139,10 +182,14 @@ namespace Baobab {
 				return;
 			}
 
+			disable_drop ();
+
 			var scanner = new ThreadedScanner ();
 			scanner.scan (directory);
 
 			set_model (scanner);
+
+			enable_drop ();
 		}
 	}
 }



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