[gnome-subtitles] Completed patch from Valmir Sena, fixes #605402 and #453220



commit 16d2072a172e6b30255e69879e682245c50f3d7b
Author: Pedro Castro <mail pedrocastro org>
Date:   Fri Jun 11 12:52:49 2010 +0100

    Completed patch from Valmir Sena, fixes #605402 and #453220

 src/Glade/MainWindow.glade                         |   20 +++--
 src/GnomeSubtitles/Core/EventHandlers.cs           |    5 +-
 src/GnomeSubtitles/Ui/Menus.cs                     |   17 +++-
 .../Ui/VideoPreview/SubtitleOverlay.cs             |   27 ++++---
 .../Ui/VideoPreview/SubtitleTracker.cs             |   78 +++++++++++--------
 src/GnomeSubtitles/Ui/VideoPreview/Video.cs        |    4 +-
 src/GnomeSubtitles/Ui/View/SubtitleSelection.cs    |   23 +++---
 src/GnomeSubtitles/Ui/View/SubtitleView.cs         |   19 +++--
 src/GnomeSubtitles/Ui/WidgetNames.cs               |    2 +-
 src/SubLib/Core/Search/SearchOperator.cs           |   35 +++++----
 10 files changed, 133 insertions(+), 97 deletions(-)
---
diff --git a/src/Glade/MainWindow.glade b/src/Glade/MainWindow.glade
index da23853..741e09e 100644
--- a/src/Glade/MainWindow.glade
+++ b/src/Glade/MainWindow.glade
@@ -248,8 +248,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">True</property>
                         <signal name="activate" handler="OnEditRedo"/>
-                        <accelerator key="Z" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                         <accelerator key="Y" signal="activate" modifiers="GDK_CONTROL_MASK"/>
+                        <accelerator key="Z" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -532,8 +532,8 @@
                         <property name="label" translatable="yes">Find Ne_xt</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnSearchFindNext"/>
-                        <accelerator key="g" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F3" signal="activate"/>
+                        <accelerator key="g" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -543,8 +543,8 @@
                         <property name="label" translatable="yes">Find Pre_vious</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnSearchFindPrevious"/>
-                        <accelerator key="g" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                         <accelerator key="F3" signal="activate" modifiers="GDK_SHIFT_MASK"/>
+                        <accelerator key="g" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -803,8 +803,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">False</property>
                         <signal name="activate" handler="OnVideoPlayPause"/>
-                        <accelerator key="p" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F5" signal="activate"/>
+                        <accelerator key="p" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <child internal-child="image">
                           <widget class="GtkImage" id="videoPlayPauseImage">
                             <property name="visible">True</property>
@@ -822,8 +822,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">False</property>
                         <signal name="activate" handler="OnVideoRewind"/>
-                        <accelerator key="k" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F6" signal="activate"/>
+                        <accelerator key="k" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <child internal-child="image">
                           <widget class="GtkImage" id="videoRewindImage">
                             <property name="visible">True</property>
@@ -841,8 +841,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">False</property>
                         <signal name="activate" handler="OnVideoForward"/>
-                        <accelerator key="l" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F7" signal="activate"/>
+                        <accelerator key="l" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <child internal-child="image">
                           <widget class="GtkImage" id="videoForwardImage">
                             <property name="visible">True</property>
@@ -882,7 +882,8 @@
                         <property name="label" translatable="yes">Seek _to Selection</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnVideoSeekToSelection"/>
-                        <accelerator key="r" signal="activate" modifiers="GDK_CONTROL_MASK"/>
+                        <accelerator key="F10" signal="activate"/>
+                        <accelerator key="r" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -892,7 +893,8 @@
                         <property name="label" translatable="yes">Select Nearest Subtitle</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnVideoSelectNearestSubtitle"/>
-                        <accelerator key="j" signal="activate" modifiers="GDK_CONTROL_MASK"/>
+                        <accelerator key="F9" signal="activate"/>
+                        <accelerator key="r" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -901,6 +903,8 @@
                         <property name="label" translatable="yes">Auto Select Subtitles</property>
                         <property name="use_underline">True</property>
                         <signal name="toggled" handler="OnVideoAutoSelectSubtitles"/>
+                        <accelerator key="F11" signal="activate"/>
+                        <accelerator key="j" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
diff --git a/src/GnomeSubtitles/Core/EventHandlers.cs b/src/GnomeSubtitles/Core/EventHandlers.cs
index 860572e..6181ded 100644
--- a/src/GnomeSubtitles/Core/EventHandlers.cs
+++ b/src/GnomeSubtitles/Core/EventHandlers.cs
@@ -261,15 +261,16 @@ public class EventHandlers {
 		Base.Dialogs.Get(typeof(VideoSeekToDialog)).Show();
 	}
 	
+	//TODO allow to seek when multiple subtitles are selected (use first)
 	public void OnVideoSeekToSelection (object o, EventArgs args) {
 		Base.Ui.Video.SeekToSelection();	
 	}
 		
-	public void OnVideoSelectNearestSubtitle(object o, EventArgs args) {
+	public void OnVideoSelectNearestSubtitle (object o, EventArgs args) {
 		Base.Ui.Video.SelectNearestSubtitle();
 	}
 		
-	public void OnVideoAutoSelectSubtitles(object o, EventArgs args) {
+	public void OnVideoAutoSelectSubtitles (object o, EventArgs args) {
 		Base.Ui.View.SetAutoSelectSubtitles();
  	}
 		
diff --git a/src/GnomeSubtitles/Ui/Menus.cs b/src/GnomeSubtitles/Ui/Menus.cs
index 8b849c4..a10759a 100644
--- a/src/GnomeSubtitles/Ui/Menus.cs
+++ b/src/GnomeSubtitles/Ui/Menus.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2009 Pedro Castro
+ * Copyright (C) 2006-2010 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
@@ -169,6 +169,8 @@ public class Menus {
 		SetSensitivity(WidgetNames.SearchReplace, documentLoaded);
 		/* Timings Menu */
 		SetSensitivity(WidgetNames.TimingsSynchronize, documentLoaded);
+		/* Video Menu */
+		SetVideoDocumentLoadedSensitivity(documentLoaded);
 		/* Tools Menu */
 		SetToolsAutocheckSpellingSensitivity(documentLoaded);
 		SetSensitivity(WidgetNames.ToolsSetTextLanguage, documentLoaded);
@@ -177,7 +179,7 @@ public class Menus {
 		SetSensitivity(WidgetNames.SaveButton, documentLoaded);
 		SetSensitivity(WidgetNames.InsertSubtitleButton, documentLoaded);
 		
-		/* Set sensitivity that only applies to when the document is not loaded */
+		/* Set sensitivity that only applies when the document is not loaded */
 		
 		if (!documentLoaded) {
 			/* Edit menu */
@@ -259,6 +261,7 @@ public class Menus {
 		SetSensitivity(WidgetNames.VideoRewind, sensitivity);
 		SetSensitivity(WidgetNames.VideoForward, sensitivity);
 		SetSensitivity(WidgetNames.VideoSeekTo, sensitivity);
+		SetVideoDocumentLoadedSensitivity(Base.IsDocumentLoaded);
 		
 		/* Set video menu dependent sensitivity if there is 1 selected subtitle. */
 		if ((Core.Base.Ui.View.Selection.Count == 1) && sensitivity)
@@ -282,16 +285,14 @@ public class Menus {
 	/// <param name="sensitivity">Whether to set the menu items sensitive.</param>
 	/// <remarks>The menu items are only set sensitive if the video is loaded.</remarks>
 	private void SetVideoSelectionDependentSensitivity (bool sensitivity) {
-		if (Core.Base.Ui.Video.IsLoaded && sensitivity) {
+		if (Core.Base.Ui.Video.IsLoaded && sensitivity) {//TODO improve this
 			SetSensitivity(WidgetNames.VideoSeekToSelection, true);
-			SetSensitivity(WidgetNames.VideoSelectNearestSubtitle, true);
 			SetSensitivity(WidgetNames.VideoSetSubtitleStart, true);
 			SetSensitivity(WidgetNames.VideoSetSubtitleEnd, true);
 			SetSensitivity(WidgetNames.VideoAutoSelectSubtitles, true);
 		}
 		else {
 			SetSensitivity(WidgetNames.VideoSeekToSelection, false);
-			SetSensitivity(WidgetNames.VideoSelectNearestSubtitle, false);
 			SetSensitivity(WidgetNames.VideoSetSubtitleStart, false);
 			SetSensitivity(WidgetNames.VideoSetSubtitleEnd, false);
 			SetSensitivity(WidgetNames.VideoAutoSelectSubtitles, false);
@@ -310,6 +311,12 @@ public class Menus {
 		SetSensitivity(WidgetNames.ViewVideoSubtitlesTranslation, translationSensitivity);
 	}
 	
+	private void SetVideoDocumentLoadedSensitivity (bool isDocumentLoaded) {
+		bool sensitivity = isDocumentLoaded && (Base.Ui != null) && Base.Ui.Video.IsLoaded;
+		SetSensitivity(WidgetNames.VideoSelectNearestSubtitle, sensitivity);
+		SetSensitivity(WidgetNames.VideoAutoSelectSubtitles, sensitivity);
+	}
+	
 	private void SetCheckMenuItemActivity (string menuItemName, bool isActive) {
 		(Base.GetWidget(menuItemName) as CheckMenuItem).Active = isActive;
 	}
diff --git a/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs b/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs
index fd4ed1e..fd05daa 100644
--- a/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs
+++ b/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2009 Pedro Castro
+ * Copyright (C) 2007-2010 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
@@ -49,7 +49,8 @@ public class SubtitleOverlay {
 		get { return toShowText; }
 		set { this.toShowText = value; }
 	}
-	
+
+
 	/* Public methods */
 
 	public void Close () {
@@ -97,19 +98,21 @@ public class SubtitleOverlay {
 	private void ClearText () {
 		label.Text = String.Empty;
 	}
-	
+
+
 	/* Event members */		
-		
-	
-	private void OnBaseInitFinished () {			
-			Base.Ui.Video.Tracker.SubtitleChanged += OnCurrentSubtitleChanged;		
+
+	private void OnBaseInitFinished () {
+		Base.Ui.Video.Tracker.SubtitleChanged += OnCurrentSubtitleChanged;		
 	}
-	
-	private void OnCurrentSubtitleChanged(int indexSubtitle) {
-			if( indexSubtitle == -1 ) UnloadSubtitle();
-			else LoadSubtitle(indexSubtitle);		
+
+	private void OnCurrentSubtitleChanged (int indexSubtitle) {
+		if (indexSubtitle == -1)
+			UnloadSubtitle();
+		else
+			LoadSubtitle(indexSubtitle);		
 	}
-	
+
 }
 
 }
diff --git a/src/GnomeSubtitles/Ui/VideoPreview/SubtitleTracker.cs b/src/GnomeSubtitles/Ui/VideoPreview/SubtitleTracker.cs
index 90dbc76..eea65e8 100755
--- a/src/GnomeSubtitles/Ui/VideoPreview/SubtitleTracker.cs
+++ b/src/GnomeSubtitles/Ui/VideoPreview/SubtitleTracker.cs
@@ -1,86 +1,100 @@
+/*
+ * This file is part of Gnome Subtitles.
+ * Copyright (C) 2007-2010 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 GnomeSubtitles.Core;
 using Gtk;
 using SubLib.Core.Domain;
 using SubLib.Core.Search;
 using System;
 
-namespace GnomeSubtitles.Ui.VideoPreview
-{
-
+namespace GnomeSubtitles.Ui.VideoPreview {
 
-public class SubtitleTracker{
-		
-		
+public class SubtitleTracker {
 	private SearchOperator searchOp = null;
 	private TimeSpan subtitleStart = TimeSpan.Zero;
-	private TimeSpan subtitleEnd = TimeSpan.Zero;	
+	private TimeSpan subtitleEnd = TimeSpan.Zero;
 	private int currentSubtitleIndex = 0;
 	private Subtitle subtitle = null;
-		
+
 	/* Delegates */
 	public delegate void VideoCurrentSubtitleChangedHandler (int indexSubtitle);
 		
 	/* Events */
-		
 	public event VideoCurrentSubtitleChangedHandler SubtitleChanged;
 
-	public SubtitleTracker ()
-	{
+
+	public SubtitleTracker () {
 		Base.InitFinished += OnBaseInitFinished;
 	}
-		
-	public int FindSubtitleNearPosition(TimeSpan position){
-		if (IsTimeInCurrentSubtitle(position)) 
-				return currentSubtitleIndex;
-		else {
-				int foundSubtitle = searchOp.FindNearTime((float)position.TotalSeconds);				
-				return foundSubtitle;
-		}
 
+	/* Public methods */
+
+	public int FindSubtitleNearPosition (TimeSpan position) {
+		if (IsTimeInCurrentSubtitle(position))
+			return currentSubtitleIndex;
+		else
+			return searchOp.FindNearTime((float)position.TotalSeconds); //TODO write method in SubLib that accepts TimeSpans
  	}
 		
 	public void Close(){
-		if( IsSubtitleLoaded )
+		if (IsSubtitleLoaded)
 			UnSetCurrentSubtitle();
 	}
-	
+
+
 	/* Private properties */
 	
 	private bool IsSubtitleLoaded {
 		get { return subtitle != null; }
 	}
-		
+
+
 	/* Private methods */
 	
 	private bool IsTimeInCurrentSubtitle (TimeSpan time) {
 		return IsSubtitleLoaded && (time >= subtitleStart) && (time <= subtitleEnd);	
 	}
 	
-		
 	private void SetCurrentSubtitle (int index) {
-		
-		if( index != currentSubtitleIndex ){
+		if (index != currentSubtitleIndex) {
 			subtitle = Base.Document.Subtitles[index];
 			subtitleStart = subtitle.Times.Start;
 			subtitleEnd = subtitle.Times.End;			
 			currentSubtitleIndex = index;			
-			}
+		}
 	}
 	
 	private void UnSetCurrentSubtitle () {
-		if( currentSubtitleIndex != -1 ){			
+		if (currentSubtitleIndex != -1) {
 			currentSubtitleIndex = -1;				
 			subtitle = null;
 			subtitleStart = TimeSpan.Zero;
 			subtitleEnd = TimeSpan.Zero;			
 		}
 	}
-		
+
 	private void EmitCurrentSubtitleChanged(int newIndex) {
 		if (SubtitleChanged != null)
 			SubtitleChanged(newIndex);
 	}
-		
+
+
 	/* Event members */
 	
 	private void OnBaseInitFinished () {
@@ -91,8 +105,7 @@ public class SubtitleTracker{
 	private void OnBaseDocumentLoaded (Document document) {
 		searchOp = new SearchOperator(document.Subtitles);
 	}
-	
-		
+
 	private void OnVideoPositionChanged (TimeSpan newPosition) {
 		if (!(Base.IsDocumentLoaded))
 			return;
@@ -107,8 +120,7 @@ public class SubtitleTracker{
 			EmitCurrentSubtitleChanged(currentSubtitleIndex);
 		}
 	}		
-	
-	
+
 	}
 	
 }
diff --git a/src/GnomeSubtitles/Ui/VideoPreview/Video.cs b/src/GnomeSubtitles/Ui/VideoPreview/Video.cs
index 8aa7f00..7081b3c 100644
--- a/src/GnomeSubtitles/Ui/VideoPreview/Video.cs
+++ b/src/GnomeSubtitles/Ui/VideoPreview/Video.cs
@@ -178,8 +178,8 @@ public class Video {
 	}
 	
 	public void SelectNearestSubtitle () {		
-		int index = tracker.FindSubtitleNearPosition(position.CurrentTime);
-		Base.Ui.View.Selection.SelectToIndex(index);
+		int indexToSelect = tracker.FindSubtitleNearPosition(position.CurrentTime);
+		Base.Ui.View.Selection.Select(indexToSelect, false, false);
 	}
 	
 	/* Private methods */
diff --git a/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs b/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs
index 7cec752..784a7ac 100644
--- a/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs
+++ b/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs
@@ -37,11 +37,12 @@ public class SubtitleSelection {
 	public SubtitleSelection (TreeView tree) {
 		this.tree = tree;
 		this.selection = tree.Selection;
-		
+
 		selection.Mode = SelectionMode.Multiple;
-		
+
 		Base.InitFinished += OnBaseInitFinished;
 	}
+
 	
 	/* Events */
 	
@@ -225,6 +226,16 @@ public class SubtitleSelection {
 		SetFocus(path, align);
 	}
 	
+	/// <summary>Selects the specified index, possibly aligning it to the center and/or reselecting it.</summary>
+	/// <param name="index">The index of the subtitle to select.</param>
+    /// <param name="align">Whether to align the selected path to the center if the path isn't visible and scrolling is needed.</param>
+    /// <param name="reselect">Whether to reselect the path if it's already the only selected path.</param>
+    /// <remarks>The subtitle is only selected if it exists. The input focus will be placed on the path.</remarks>
+    public void Select (int index, bool align, bool reselect) {
+    	if ((index >= 0) && (index < Base.Document.Subtitles.Count))
+    		Select(Util.IntToPath(index), false, false);
+	}
+	
 	/// <summary>Selects a <see cref="TreePath" />, activates it and selects text in the subtitle it refers to.</summary>
 	/// <param name="path">The path to select. If it's null, all paths will be unselected.</param>
     /// <param name="align">Whether to align the selected path to the center if the path isn't visible and scrolling is needed.</param>
@@ -240,14 +251,6 @@ public class SubtitleSelection {
 		Select(path, align, reselect);
 		Core.Base.Ui.Edit.TextFocusOnSelection(start, end, textType);		
 	}
-    
-	/// <summary>Selects the subtitle based in your index.</summary>
-    /// <remarks>The subtitle is only selected if it exists.</remarks>
-    public void SelectToIndex (int index) {
-    	if (Base.Document.Subtitles.Count > 0 && index > 0){			
-    		Select(Util.IntToPath(index), false, false);
-		}
-    }
 		
     /// <summary>Selects the first subtitle.</summary>
     /// <remarks>The subtitle is only selected if it exists.</remarks>
diff --git a/src/GnomeSubtitles/Ui/View/SubtitleView.cs b/src/GnomeSubtitles/Ui/View/SubtitleView.cs
index d6938c6..492b6c1 100644
--- a/src/GnomeSubtitles/Ui/View/SubtitleView.cs
+++ b/src/GnomeSubtitles/Ui/View/SubtitleView.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2009 Pedro Castro
+ * Copyright (C) 2006-2010 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
@@ -70,13 +70,14 @@ public class SubtitleView {
     }
     
     /* Public methods */ 
-		
+	
 	public void SetAutoSelectSubtitles(){
-		CheckMenuItem item = Base.GetWidget(WidgetNames.VideoAutoSelectSubtitles) as CheckMenuItem;
-		bool follow = item.Active;
-		if( follow ) Base.Ui.Video.Tracker.SubtitleChanged += OnCurrentSubtitleChanged;
-		else Base.Ui.Video.Tracker.SubtitleChanged -= OnCurrentSubtitleChanged;
-	}		
+		CheckMenuItem autoSelectSubtitles = Base.GetWidget(WidgetNames.VideoAutoSelectSubtitles) as CheckMenuItem;
+		if (autoSelectSubtitles.Active)
+			Base.Ui.Video.Tracker.SubtitleChanged += OnCurrentSubtitleChanged;
+		else
+			Base.Ui.Video.Tracker.SubtitleChanged -= OnCurrentSubtitleChanged;
+	}
 
 	
 	/// <summary>Instructs the <see cref="TreeView" /> to redraw a row.</summary>
@@ -387,8 +388,8 @@ public class SubtitleView {
 			SubtitleCountChanged(subtitles.Count);
 	}
 	
-	private void OnCurrentSubtitleChanged(int indexSubtitle) {			
-			if( indexSubtitle != -1 ) selection.SelectToIndex(indexSubtitle);		
+	private void OnCurrentSubtitleChanged (int subtitleIndex) {			
+		selection.Select(subtitleIndex, false, false);
 	}
 
     private void SetEmptyModel () {
diff --git a/src/GnomeSubtitles/Ui/WidgetNames.cs b/src/GnomeSubtitles/Ui/WidgetNames.cs
index d3503ab..dc03c42 100644
--- a/src/GnomeSubtitles/Ui/WidgetNames.cs
+++ b/src/GnomeSubtitles/Ui/WidgetNames.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2009 Pedro Castro
+ * Copyright (C) 2006-2010 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
diff --git a/src/SubLib/Core/Search/SearchOperator.cs b/src/SubLib/Core/Search/SearchOperator.cs
index 2255990..327900e 100644
--- a/src/SubLib/Core/Search/SearchOperator.cs
+++ b/src/SubLib/Core/Search/SearchOperator.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2006-2009 Pedro Castro
+ * Copyright (C) 2006-2010 Pedro Castro
  *
  * SubLib is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -110,7 +110,7 @@ public class SearchOperator {
 			
 			double end = subtitle.Times.End.TotalSeconds;
 			if (time <= end)
-				return subtitleNumber;
+				return subtitleNumber; //TODO optimize: else return -1;
 		}
 		return -1; // No subtitles were found 
 	}
@@ -120,32 +120,37 @@ public class SearchOperator {
 	/// <returns>The found subtitle number, or -1 if no subtitle was found.</returns>
 	public int FindNearTime (float time) {
 		SubtitleCollection collection = subtitles.Collection;
-		if( time < collection[0].Times.Start.Seconds ) return 0;
 		if (collection.Count == 0)
 			return -1;
-		
-		for (int subtitleNumber = 0 ; subtitleNumber < collection.Count ; subtitleNumber++) {
+
+		/* Check if before the first subtitle */
+		if (time < collection[0].Times.Start.Seconds)
+			return 0;
+
+		/* Iterate subtitles two by two - the last subtitle is handled in pair and individually afterwards */
+		for (int subtitleNumber = 0 ; subtitleNumber < collection.Count - 1 ; subtitleNumber++) {
 			Subtitle subtitle = collection[subtitleNumber];
-			Subtitle nextSubtitle;
-			if( subtitleNumber != collection.Count -1 ) nextSubtitle = collection[subtitleNumber+1];
-			else nextSubtitle = collection[collection.Count-1];
 			
+			/* Continue iterating if didn't reach subtitle start yet */
 			double start = subtitle.Times.Start.TotalSeconds;
 			if (time < start)
 				continue;
 			
+			/* Check if time is contained by the subtitle */
 			double end = subtitle.Times.End.TotalSeconds;
 			if (time <= end)
 				return subtitleNumber;
-			
+				
+			/* Check if contained between this and the next subtitle, and which is nearest */
+			int nextSubtitleIndex = subtitleNumber + 1;
+			Subtitle nextSubtitle = collection[nextSubtitleIndex];
 			double nextSubtitleStart = nextSubtitle.Times.Start.TotalSeconds;
-			if( time > end && time < nextSubtitleStart ){					
-					if( time - end < nextSubtitleStart - time ) return subtitleNumber;
-					else return subtitleNumber+1;
-			}
-			
+			if (time < nextSubtitleStart)
+				return ((time - end) < (nextSubtitleStart - time)) ? subtitleNumber : nextSubtitleIndex;
 		}
-		return -1; // No subtitles were found 
+		
+		/* If no rule matched before, time must be after last subtitle */
+		return collection.Count - 1;
 	}
 	
 	/* Private members */



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