[gnome-subtitles] Support Drag and Drop (fixes #585477, based on a patch from Arx Cruz)



commit d3d7bd42498e0d10ad3610d4e7e80854836e2ccb
Author: Pedro Castro <mail pedrocastro org>
Date:   Mon Aug 10 02:02:10 2009 +0100

    Support Drag and Drop (fixes #585477, based on a patch from Arx Cruz)
    
    Subtitle and video files can now be dragged to the subtitle and video areas, respectively. A confirmation dialog is presented, as usual, if the current subtitle file has unsaved changes.

 gnome-subtitles.mdp                      |    1 +
 src/Glade/MainWindow.glade               |    2 +
 src/GnomeSubtitles/Core/Base.cs          |    6 +++
 src/GnomeSubtitles/Core/DragDrop.cs      |   52 ++++++++++++++++++++++++++++
 src/GnomeSubtitles/Core/EventHandlers.cs |   54 +++++++++++++++++++++++++----
 src/GnomeSubtitles/Ui/MainUi.cs          |   14 +++++++-
 src/GnomeSubtitles/Ui/WidgetNames.cs     |    6 +++-
 7 files changed, 124 insertions(+), 11 deletions(-)
---
diff --git a/gnome-subtitles.mdp b/gnome-subtitles.mdp
index f35f6e9..0a2a588 100644
--- a/gnome-subtitles.mdp
+++ b/gnome-subtitles.mdp
@@ -243,6 +243,7 @@
     <File name="src/SubLib/IO/SubtitleFormats/SubtitleFormatSubViewer2.cs" subtype="Code" buildaction="Compile" />
     <File name="src/SubLib/IO/SubtitleFormats/SubtitleFormatViPlaySubtitleFile.cs" subtype="Code" buildaction="Compile" />
     <File name="src/SubLib/Core/Search/SubtitleReplaceResult.cs" subtype="Code" buildaction="Compile" />
+    <File name="src/GnomeSubtitles/Core/DragDrop.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
diff --git a/src/Glade/MainWindow.glade b/src/Glade/MainWindow.glade
index 4111a12..ba0a82d 100644
--- a/src/Glade/MainWindow.glade
+++ b/src/Glade/MainWindow.glade
@@ -1250,6 +1250,7 @@
                 <property name="height_request">200</property>
                 <property name="border_width">5</property>
                 <property name="spacing">7</property>
+                <signal name="drag_data_received" handler="OnVideoAreaDragDataReceived"/>
                 <child>
                   <widget class="GtkVBox" id="videoTimingsVBox">
                     <property name="visible">True</property>
@@ -1578,6 +1579,7 @@
                 <property name="visible">True</property>
                 <property name="border_width">4</property>
                 <property name="spacing">5</property>
+                <signal name="drag_data_received" handler="OnSubtitleAreaDragDataReceived"/>
                 <child>
                   <widget class="GtkScrolledWindow" id="subtitleViewScrolledWindow">
                     <property name="visible">True</property>
diff --git a/src/GnomeSubtitles/Core/Base.cs b/src/GnomeSubtitles/Core/Base.cs
index 187d763..0487483 100644
--- a/src/GnomeSubtitles/Core/Base.cs
+++ b/src/GnomeSubtitles/Core/Base.cs
@@ -44,6 +44,7 @@ public class Base {
 	private static EventHandlers handlers = null;
 	private static CommandManager commandManager = null;
 	private static Clipboards clipboards = null;
+	private static DragDrop dragDrop = null;
 	private static Config config = null;
 	private static Dialogs dialogs = null;
 	private static SpellLanguages spellLanguages = null;
@@ -84,6 +85,10 @@ public class Base {
 	public static Clipboards Clipboards {
 		get { return clipboards; }
 	}
+
+	public static DragDrop DragDrop {
+		get { return dragDrop; }
+	}
 	
 	public static Config Config {
 		get { return config; }
@@ -261,6 +266,7 @@ public class Base {
 		
 		/* Initialize misc */
 		clipboards = new Clipboards();
+		dragDrop = new DragDrop();
 		config = new Config();
 		dialogs = new Dialogs();
 		spellLanguages = new SpellLanguages();
diff --git a/src/GnomeSubtitles/Core/DragDrop.cs b/src/GnomeSubtitles/Core/DragDrop.cs
new file mode 100644
index 0000000..ecff500
--- /dev/null
+++ b/src/GnomeSubtitles/Core/DragDrop.cs
@@ -0,0 +1,52 @@
+/*
+ * This file is part of Gnome Subtitles.
+ * Copyright (C) 2009 Pedro Castro
+ *
+ * Gnome Subtitles is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Gnome Subtitles is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+using Gdk;
+using GnomeSubtitles.Ui;
+using Gtk;
+
+namespace GnomeSubtitles.Core {
+
+public class DragDrop {
+	private TargetEntry targetUriList = new TargetEntry("text/uri-list", 0, DragDropTargetUriList);
+    
+	/* Public constants */
+	public const uint DragDropTargetUriList = 0;
+
+
+	public DragDrop () {
+		Base.InitFinished += OnBaseInitFinished;
+	}
+
+
+	/* Private members */
+	
+	private void SetDropTargets () {
+		TargetEntry[] targetEntries = new TargetEntry[] { targetUriList };
+		Gtk.Drag.DestSet(Base.GetWidget(WidgetNames.SubtitleAreaVBox), DestDefaults.All, targetEntries, DragAction.Copy);
+		Gtk.Drag.DestSet(Base.GetWidget(WidgetNames.VideoAreaHBox), DestDefaults.All, targetEntries, DragAction.Copy);
+	}
+
+	private void OnBaseInitFinished () {
+		SetDropTargets();
+    }
+
+}
+
+}
diff --git a/src/GnomeSubtitles/Core/EventHandlers.cs b/src/GnomeSubtitles/Core/EventHandlers.cs
index ed00f69..8b67468 100644
--- a/src/GnomeSubtitles/Core/EventHandlers.cs
+++ b/src/GnomeSubtitles/Core/EventHandlers.cs
@@ -25,11 +25,13 @@ using Gtk;
 using Mono.Unix;
 using SubLib.Core.Domain;
 using System;
+using System.Text;
 
 namespace GnomeSubtitles.Core {
 
 public class EventHandlers {
 
+
 	/* File Menu */
 	
 	public void OnFileNew (object o, EventArgs args) {
@@ -83,8 +85,8 @@ public class EventHandlers {
     public void OnFileQuit (object o, EventArgs args) {
 		Base.Ui.Quit();
 	}
-	
-	
+
+
 	/* Edit Menu */
 	
 	public void OnEditUndo (object o, EventArgs args) {
@@ -142,6 +144,7 @@ public class EventHandlers {
 		Base.Dialogs.Get(typeof(PreferencesDialog)).Show();
 	}
 
+
 	/* View Menu */
 	
 	public void OnViewTimes (object o, EventArgs args) {
@@ -171,7 +174,8 @@ public class EventHandlers {
 		if ((o as RadioMenuItem).Active)
 			Base.Ui.Video.Overlay.ToShowText = false;
 	}
-	
+
+
 	/* Search Menu */
 	
 	public void OnSearchFind (object o, EventArgs args) {
@@ -189,7 +193,8 @@ public class EventHandlers {
 	public void OnSearchReplace (object o, EventArgs args) {
 		Base.Ui.View.Search.ShowReplace();
 	}
-	
+
+
 	/*	Timings Menu */
 	
 	public void OnTimingsInputFrameRate (object o, EventArgs args) {
@@ -219,8 +224,8 @@ public class EventHandlers {
 	public void OnTimingsSynchronize (object o, EventArgs args) {
 		Base.Dialogs.Get(typeof(TimingsSynchronizeDialog)).Show();
 	}
-	
-	
+
+
 	/* Video Menu */
 		
 	public void OnVideoOpen (object o, EventArgs args) {
@@ -273,7 +278,8 @@ public class EventHandlers {
 			Base.CommandManager.Execute(new VideoSetSubtitleEndCommand(frames));
 		}
 	}
-	
+
+
 	/* Tools Menu */
 		
 	public void OnToolsAutocheckSpelling (object o, EventArgs args) {
@@ -326,7 +332,23 @@ public class EventHandlers {
     }
 
 
-		/*	Subtitle View	*/
+	/* Subtitle Area */
+
+	public void OnSubtitleAreaDragDataReceived (object o, DragDataReceivedArgs args) {
+		string uriString = Encoding.UTF8.GetString(args.SelectionData.Data);
+		bool success = false;
+		Uri fileUri;
+
+		if (Uri.TryCreate(uriString, UriKind.Absolute, out fileUri) && (args.Info == DragDrop.DragDropTargetUriList)) {
+			Base.Ui.Open(fileUri.AbsolutePath);
+			success = true;
+		}
+
+		Gtk.Drag.Finish(args.Context, success, false, args.Time);
+	}
+
+
+	/*	Subtitle View	*/
     
     public void OnRowActivated (object o, RowActivatedArgs args) {
     	Base.Ui.Video.SeekToSelection();
@@ -340,6 +362,22 @@ public class EventHandlers {
 			OnEditInsertSubtitleAfter(o, EventArgs.Empty);
     }
 
+
+	/* Video Area */
+
+	public void OnVideoAreaDragDataReceived (object o, DragDataReceivedArgs args) {
+		string uriString = Encoding.UTF8.GetString(args.SelectionData.Data);
+		bool success = false;
+		Uri fileUri;
+
+		if (Uri.TryCreate(uriString, UriKind.Absolute, out fileUri) && (args.Info == DragDrop.DragDropTargetUriList)) {
+			Base.OpenVideo(fileUri);
+			success = true;
+		}
+
+		Gtk.Drag.Finish(args.Context, success, false, args.Time);
+	}
+
 }
 
 }
diff --git a/src/GnomeSubtitles/Ui/MainUi.cs b/src/GnomeSubtitles/Ui/MainUi.cs
index 615c243..5024c33 100644
--- a/src/GnomeSubtitles/Ui/MainUi.cs
+++ b/src/GnomeSubtitles/Ui/MainUi.cs
@@ -41,7 +41,7 @@ public class MainUi {
 	private SubtitleView view = null;
 	private SubtitleEdit edit = null;
 	private Status status = null;
-	
+
 	/* Constant strings */
 	private const string gladeFilename = "MainWindow.glade";
 	private const string iconFilename = "gnome-subtitles.png";
@@ -52,7 +52,7 @@ public class MainUi {
 		window = glade.GetWidget("window") as Window;
 		window.Icon = new Gdk.Pixbuf(null, iconFilename);
 		window.SetDefaultSize(Base.Config.PrefsWindowWidth, Base.Config.PrefsWindowHeight);
-		
+
 		video = new Video();
 		view = new SubtitleView();
 		edit = new SubtitleEdit();
@@ -152,6 +152,16 @@ public class MainUi {
     		Open(filename, codePage, videoUri);
     	}
     }
+
+	/// <summary>Opens a subtitle.</summary>
+	/// <param name="filename">The path of the subtitles file to open.</param>
+    /// <remarks>If there's a document currently open with unsaved changes, a warning dialog
+    /// is shown before opening the new file.</remarks>
+    public void Open (string filename) {
+		if (ToOpenAfterWarning()) {
+			Open(filename, -1, null);
+		}
+    }
         
     public void OpenVideo () {
 		
diff --git a/src/GnomeSubtitles/Ui/WidgetNames.cs b/src/GnomeSubtitles/Ui/WidgetNames.cs
index 93c8c13..e9f23de 100644
--- a/src/GnomeSubtitles/Ui/WidgetNames.cs
+++ b/src/GnomeSubtitles/Ui/WidgetNames.cs
@@ -118,7 +118,11 @@ public class WidgetNames {
 	public const string VideoPlaybackHBox = "videoPlaybackHBox";
 	public const string VideoSlider = "videoSlider";
 	public const string VideoPlayPauseButton = "videoPlayPauseButton";
-	
+
+
+	/* Subtitle area */
+	public const string SubtitleAreaVBox = "subtitleAreaVBox";
+
 	/* Subtitle View */
 	public const string SubtitleView = "subtitleView";
 	



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