From e4e8e6bb23a734634da1f3f19a03c247adcfe476 Mon Sep 17 00:00:00 2001 From: Alexander Kojevnikov Date: Wed, 19 May 2010 19:25:48 +1000 Subject: [PATCH] DnD support (issue 4) --- src/spek-window.vala | 23 +++++++++++++++++++++++ vapi/config.vapi | 6 ++++++ 2 files changed, 29 insertions(+) diff --git a/src/spek-window.vala b/src/spek-window.vala index 496291d..a1be294 100644 --- a/src/spek-window.vala +++ b/src/spek-window.vala @@ -27,6 +27,9 @@ namespace Spek { private FileFilter filter_all; private FileFilter filter_audio; private FileFilter filter_png; + private const Gtk.TargetEntry[] DEST_TARGET_ENTRIES = { + { "text/uri-list", 0, 0 } + }; public Window (string? file_name) { title = _("Spek - Acoustic Spectrum Analyser"); @@ -96,11 +99,31 @@ namespace Spek { add (vbox); show_all (); + // Set up Drag and Drop + drag_dest_set (this, DestDefaults.ALL, DEST_TARGET_ENTRIES, DragAction.COPY); + drag_data_received.connect (on_dropped); + if (file_name != null) { open_file (file_name); } } + void on_dropped (DragContext cx, int x, int y, SelectionData data, uint info, uint time) { + if (data.length > 0 && data.format == 8) { + string[] files = data.get_uris (); + if (files.length > 0) { + try { + string hostname; + var file = filename_from_uri (files[0], out hostname); + open_file (file); + drag_finish (cx, true, false, time); + return; + } catch {} + } + } + drag_finish (cx, false, false, time); + } + private void open_file (string file_name) { cur_dir = Path.get_dirname (file_name); spectrogram.open (file_name); diff --git a/vapi/config.vapi b/vapi/config.vapi index 7cc4968..d3dc5f8 100644 --- a/vapi/config.vapi +++ b/vapi/config.vapi @@ -14,3 +14,9 @@ namespace Config { public const string PKGDATADIR; /* /usr/local/share/spek */ public const string PKGLIBDIR; /* /usr/local/lib/spek */ } + +// TODO: file a bug to have this included +[CCode (cprefix = "G", lower_case_cprefix = "g_", cheader_filename = "glib.h", gir_namespace = "GLib", gir_version = "2.0")] +namespace GLib { + public static string filename_from_uri (string uri, out string hostname) throws Error; +}