gnome-subtitles r1126 - in trunk/src/GnomeSubtitles: Core Core/Command Dialog Ui Ui/Edit Ui/VideoPreview Ui/View



Author: pcastro
Date: Sun Mar  8 20:39:18 2009
New Revision: 1126
URL: http://svn.gnome.org/viewvc/gnome-subtitles?rev=1126&view=rev

Log:
Major refactoring to use events instead of static GUI update propagation.
Added File Close functionality.
Don't change timing mode, after save, to reflect the timing mode of the chosen subtitle format

Modified:
   trunk/src/GnomeSubtitles/Core/Base.cs
   trunk/src/GnomeSubtitles/Core/Command/CommandManager.cs
   trunk/src/GnomeSubtitles/Core/Command/FixedMultipleSelectionCommand.cs
   trunk/src/GnomeSubtitles/Core/Document.cs
   trunk/src/GnomeSubtitles/Core/EventHandlers.cs
   trunk/src/GnomeSubtitles/Core/SpellLanguages.cs
   trunk/src/GnomeSubtitles/Dialog/FileOpenDialog.cs
   trunk/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs
   trunk/src/GnomeSubtitles/Dialog/VideoOpenDialog.cs
   trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEdit.cs
   trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditSpinButtons.cs
   trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditText.cs
   trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextView.cs
   trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTranslation.cs
   trunk/src/GnomeSubtitles/Ui/MainUi.cs
   trunk/src/GnomeSubtitles/Ui/Menus.cs
   trunk/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
   trunk/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs
   trunk/src/GnomeSubtitles/Ui/VideoPreview/Video.cs
   trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoFiles.cs
   trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoPosition.cs
   trunk/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs
   trunk/src/GnomeSubtitles/Ui/View/SubtitleView.cs
   trunk/src/GnomeSubtitles/Ui/View/Subtitles.cs
   trunk/src/GnomeSubtitles/Ui/WidgetNames.cs

Modified: trunk/src/GnomeSubtitles/Core/Base.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Core/Base.cs	(original)
+++ trunk/src/GnomeSubtitles/Core/Base.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -30,6 +30,12 @@
 
 namespace GnomeSubtitles.Core {
 
+/* Delegates */
+public delegate void DocumentHandler (Document document);
+public delegate void VideoLoadedHandler (Uri videoUri);
+public delegate void TimingModeChangedHandler (TimingMode timingMode);
+public delegate void BasicEventHandler ();
+
 public class Base {
 	private static Glade.XML glade = null;
 	
@@ -43,8 +49,19 @@
 	private static SpellLanguages spellLanguages = null;
 	
 	private static Document document = null;
+	private static Uri videoUri = null;
 	private static TimingMode timingMode = TimingMode.Times;
 
+	/* Events */
+	public static event BasicEventHandler InitFinished;
+	public static event DocumentHandler DocumentLoaded;
+	public static event DocumentHandler DocumentUnloaded;
+	public static event BasicEventHandler TranslationLoaded;
+	public static event BasicEventHandler TranslationUnloaded;
+	public static event VideoLoadedHandler VideoLoaded;
+	public static event BasicEventHandler VideoUnloaded;
+	public static event TimingModeChangedHandler TimingModeChanged;
+	
 	
 	/* Public properties */
 	
@@ -84,16 +101,24 @@
 		get { return document; }
 	}
 	
+	public static Uri VideoUri {
+		get { return videoUri; }
+	}
+	
 	public static bool IsDocumentLoaded {
 		get { return document != null; }
 	}
 	
+	public static bool IsVideoLoaded {
+		get { return videoUri != null; }
+	}
+	
 	public static TimingMode TimingMode {
 		get { return timingMode; }
 		set {
 			if (timingMode != value) {
 				timingMode = value;
-				ui.UpdateFromTimingMode(value);
+				EmitTimingModeChangedEvent();
 			}		
 		}
 	}
@@ -111,15 +136,15 @@
 	
 	/// <summary>Runs the main GUI, after initialization.</summary> 
 	public static void Run (ExecutionContext executionContext) {
-		if (!Init(executionContext))
-			throw new Exception("The Base environment was already initialized.");
-			
+		Init(executionContext);
+		
 		ui.Start();
 		executionContext.RunApplication();
 	}
 	
 	/// <summary>Quits the program.</summary>
 	public static void Quit () {
+		ui.Video.Quit();
 		executionContext.QuitApplication();
 	}
 	
@@ -129,23 +154,87 @@
 		executionContext.QuitApplication();
 	}
 	
-	public static void CreateDocumentNew (string path) {
-		bool wasLoaded = IsDocumentLoaded;
-		document = new Document(path, wasLoaded);
+	public static void NewDocument (string path) {
+		if (IsDocumentLoaded)
+			CloseDocument();
+			
+		document = new Document(path);
+		EmitDocumentLoadedEvent();
 		
-		CommandManager.Clear();
-		Ui.UpdateFromDocumentModified(false);
-		Ui.UpdateFromNewDocument(wasLoaded);		
+		if (document.Subtitles.Count == 0)
+			commandManager.Execute(new InsertFirstSubtitleCommand());
 	}
 	
-	public static void CreateDocumentOpen (string path, Encoding encoding) {
-		bool wasLoaded = IsDocumentLoaded;
-		document = new Document(path, encoding, wasLoaded);
-
-		CommandManager.Clear();
+	public static void OpenDocument (string path, Encoding encoding) {
+		if (IsDocumentLoaded)
+			CloseDocument();
+	
+		document = new Document(path, encoding);
 		TimingMode = document.TextFile.TimingMode;
-		Ui.UpdateFromDocumentModified(false);
-		Ui.UpdateFromNewDocument(wasLoaded);
+		EmitDocumentLoadedEvent();
+	}
+	
+	public static void CloseDocument () {
+		if (!IsDocumentLoaded)
+			return;
+		
+		if (document.IsTranslationLoaded)
+			CloseTranslation();
+	
+		document.Close();
+		CommandManager.Clear();		
+		EmitDocumentUnloadedEvent();
+		
+		document = null;
+	}
+	
+	public static void OpenVideo (Uri uri) {
+		if (uri == null)
+			return;
+	
+		if (IsVideoLoaded)
+			CloseVideo();
+
+		ui.Video.Open(uri);
+	}
+	
+	public static void UpdateFromVideoLoaded (Uri uri) {
+		videoUri = uri;
+	
+		EmitVideoLoadedEvent();
+	}
+	
+	public static void CloseVideo () {
+		ui.Video.Close();
+		videoUri = null;
+		
+		EmitVideoUnloadedEvent();
+	}
+	
+	public static void Open (string path, Encoding encoding, Uri videoUri) {
+		OpenDocument(path, encoding);
+		OpenVideo(videoUri);
+	}
+	
+	public static void OpenTranslation (string path, Encoding encoding) {
+		if (document.IsTranslationLoaded)
+			CloseTranslation();
+	
+		document.OpenTranslation(path, encoding);
+		EmitTranslationLoadedEvent();
+	}
+	
+	public static void NewTranslation () {
+		if (document.IsTranslationLoaded)
+			CloseTranslation();
+	
+		document.NewTranslation();
+		EmitTranslationLoadedEvent();
+	}
+	
+	public static void CloseTranslation () {
+		document.CloseTranslation();
+		EmitTranslationUnloadedEvent();
 	}
 	
 	public static Widget GetWidget (string name) {
@@ -157,10 +246,9 @@
 	/// <summary>Initializes the base program structure.</summary>
 	/// <remarks>Nothing is done if initialization has already occured. The core value is checked for this,
 	/// if it's null then initialization hasn't occured yet.</remarks>
-	/// <returns>Whether initialization succeeded.</returns>
-	private static bool Init (ExecutionContext newExecutionContext) {
+	private static void Init (ExecutionContext newExecutionContext) {
 		if ((executionContext != null) && (executionContext.Initialized))
-			return false;
+			throw new Exception("The Base environment was already initialized.");
 
 		executionContext = newExecutionContext;
 		executionContext.InitApplication();
@@ -181,8 +269,50 @@
 		ui = new MainUi(handlers, out glade);
 		clipboards.WatchPrimaryChanges = true;
 		Catalog.Init(ExecutionContext.TranslationDomain, ConfigureDefines.LocaleDir);
-
-		return true;
+		
+		EmitInitFinishedEvent();
+	}
+	
+	/* Event members */
+	
+	private static void EmitInitFinishedEvent () {
+		if (InitFinished != null)
+			InitFinished();
+	}
+	
+	private static void EmitDocumentLoadedEvent () {
+		if (DocumentLoaded != null)
+			DocumentLoaded(document);
+	}
+	
+	private static void EmitDocumentUnloadedEvent () {
+		if (DocumentUnloaded != null)
+			DocumentUnloaded(document);
+	}
+	
+	private static void EmitTranslationLoadedEvent () {
+		if (TranslationLoaded != null)
+			TranslationLoaded();
+	}
+	
+	private static void EmitTranslationUnloadedEvent () {
+		if (TranslationUnloaded != null)
+			TranslationUnloaded();
+	}
+	
+	private static void EmitVideoLoadedEvent () {
+		if (VideoLoaded != null)
+			VideoLoaded(videoUri);
+	}
+	
+	private static void EmitVideoUnloadedEvent () {
+		if (VideoUnloaded != null)
+			VideoUnloaded();
+	}
+	
+	private static void EmitTimingModeChangedEvent () {
+		if (TimingModeChanged != null)
+			TimingModeChanged(timingMode);
 	}
 
 }

Modified: trunk/src/GnomeSubtitles/Core/Command/CommandManager.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Core/Command/CommandManager.cs	(original)
+++ trunk/src/GnomeSubtitles/Core/Command/CommandManager.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -44,6 +44,8 @@
 	public CommandManager (int undoLimit) {
 		limit = undoLimit;
 		commands = new Command[undoLimit];
+		
+		Base.InitFinished += OnBaseInitFinished;
 	}
 	
 	public void Clear () {
@@ -53,63 +55,7 @@
 		iterator = 0;
 	}
 	
-	public void ClearTarget (CommandTarget target) {
-
-		/* Create new collection of commands */
-		Command[] newCommands = new Command[limit];
-		int newIterator = 0;
-		int newUndoCount = 0;
-		int newRedoCount = 0;
-		
-		/* Go through the undo commands */
-		if (undoCount > 0) {
-			int lastUndoIter = iterator - undoCount;
-			if (lastUndoIter < 0)
-				lastUndoIter = limit + lastUndoIter;
-	
-			int undoIter = lastUndoIter;
-			while (undoIter != iterator) {
-				Command undoCommand = commands[undoIter];
-				if (undoCommand.Target != target) {
-					newCommands[newIterator] = undoCommand;
-					newIterator++;
-					newUndoCount++;
-				}		
-				undoIter = (undoIter == limit - 1 ? 0 : undoIter + 1);
-			}
-		}
-		
-		/* Go through the redo commands */
-		if (redoCount > 0) {
-			int redoIter = iterator;
-			int newRedoIterator = newIterator; //Because newIterator cannot be changed now
-			for (int redoNum = 0 ; redoNum < redoCount ; redoNum++) {
-				Command redoCommand = commands[redoIter];
-				if (redoCommand.Target != target) {
-					newCommands[newRedoIterator] = redoCommand;
-					newRedoIterator++;
-					newRedoCount++;				
-				}			
-				redoIter = (redoIter == limit - 1 ? 0 : redoIter + 1);
-			}
-		}
-		
-		/* Check whether to toggle undo and redo */
-		bool toToggleUndo = ((undoCount > 0) && (newUndoCount == 0));
-		bool toToggleRedo = ((redoCount > 0) && (newRedoCount == 0));
-		
-		/* Update state */
-		commands = newCommands;
-		undoCount = newUndoCount;
-		redoCount = newRedoCount;
-		iterator = newIterator;
-		
-		/* Issue possible events */
-		if (toToggleUndo)
-			EmitUndoToggled();
-		if (toToggleRedo)
-			EmitRedoToggled();
-	}
+	/* Public properties */
 	
 	public bool CanUndo {
 		get { return undoCount > 0; }
@@ -288,6 +234,74 @@
 	private void ClearRedo () {
 		redoCount = 0;
 	}
+	
+	private void ClearTarget (CommandTarget target) {
+
+		/* Create new collection of commands */
+		Command[] newCommands = new Command[limit];
+		int newIterator = 0;
+		int newUndoCount = 0;
+		int newRedoCount = 0;
+		
+		/* Go through the undo commands */
+		if (undoCount > 0) {
+			int lastUndoIter = iterator - undoCount;
+			if (lastUndoIter < 0)
+				lastUndoIter = limit + lastUndoIter;
+	
+			int undoIter = lastUndoIter;
+			while (undoIter != iterator) {
+				Command undoCommand = commands[undoIter];
+				if (undoCommand.Target != target) {
+					newCommands[newIterator] = undoCommand;
+					newIterator++;
+					newUndoCount++;
+				}		
+				undoIter = (undoIter == limit - 1 ? 0 : undoIter + 1);
+			}
+		}
+		
+		/* Go through the redo commands */
+		if (redoCount > 0) {
+			int redoIter = iterator;
+			int newRedoIterator = newIterator; //Because newIterator cannot be changed now
+			for (int redoNum = 0 ; redoNum < redoCount ; redoNum++) {
+				Command redoCommand = commands[redoIter];
+				if (redoCommand.Target != target) {
+					newCommands[newRedoIterator] = redoCommand;
+					newRedoIterator++;
+					newRedoCount++;				
+				}			
+				redoIter = (redoIter == limit - 1 ? 0 : redoIter + 1);
+			}
+		}
+		
+		/* Check whether to toggle undo and redo */
+		bool toToggleUndo = ((undoCount > 0) && (newUndoCount == 0));
+		bool toToggleRedo = ((redoCount > 0) && (newRedoCount == 0));
+		
+		/* Update state */
+		commands = newCommands;
+		undoCount = newUndoCount;
+		redoCount = newRedoCount;
+		iterator = newIterator;
+		
+		/* Issue possible events */
+		if (toToggleUndo)
+			EmitUndoToggled();
+		if (toToggleRedo)
+			EmitRedoToggled();
+	}
+	
+	/* Event members */
+	
+	private void OnBaseInitFinished () {
+		Base.TranslationUnloaded += OnBaseTranslationUnloaded;
+	}
+	
+	private void OnBaseTranslationUnloaded () {
+		ClearTarget(CommandTarget.Translation);
+	}
 
 }
 

Modified: trunk/src/GnomeSubtitles/Core/Command/FixedMultipleSelectionCommand.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Core/Command/FixedMultipleSelectionCommand.cs	(original)
+++ trunk/src/GnomeSubtitles/Core/Command/FixedMultipleSelectionCommand.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -61,7 +61,7 @@
 		}
 		Base.Ui.View.Refresh();
 		if (reselect)
-			Base.Ui.UpdateFromSelection();
+			Base.Ui.View.Selection.Reselect(); //TODO Check why this is needed
 		
 		PostProcess();
 		return true;

Modified: trunk/src/GnomeSubtitles/Core/Document.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Core/Document.cs	(original)
+++ trunk/src/GnomeSubtitles/Core/Document.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -26,6 +26,9 @@
 
 namespace GnomeSubtitles.Core {
 
+/* Delegates */
+public delegate void DocumentModificationStatusChangedHandler (bool modified);
+
 public class Document {
 	private Ui.View.Subtitles subtitles = null;
 	private bool wasTextModified = false;
@@ -37,16 +40,22 @@
 	private bool canTranslationBeSaved = false; //Whether the translation document can be saved with existing translationFile properties
 
 
-	public Document (string path, bool wasLoaded) {
-		New(path, wasLoaded);
+	public Document (string path) {
+		New(path);
+		ConnectInitSignals();
 	}
 	
-	public Document (string path, Encoding encoding, bool wasLoaded) {
-		Open(path, encoding, wasLoaded);
+	public Document (string path, Encoding encoding) {
+		Open(path, encoding);
+		ConnectInitSignals();
 	}
 	
+	/* Events */
+	
+	public event DocumentModificationStatusChangedHandler ModificationStatusChanged;
+	
 	/* Public properties */
-
+	
 	public FileProperties TextFile {
 		get { return textFile; }
 	}
@@ -79,6 +88,7 @@
 		get { return wasTranslationModified; }
 	}
 	
+
 	/* Public methods */
 
 	public bool Save (FileProperties newFileProperties) {
@@ -87,28 +97,22 @@
 		
 		textFile = saver.FileProperties;		
 		canTextBeSaved = true;
-
-		Base.Ui.Menus.SetActiveTimingMode(textFile.TimingMode);
-		
+	
 		ClearTextModified();
 		return true;
 	}
 
 	public void NewTranslation () {
-		RemoveTranslationFromSubtitles();
-		ClearTranslationStatus();
-		CreateNewTranslationFileProperties();
+		if (this.IsTranslationLoaded)
+			CloseTranslation();
 
-		Base.Ui.UpdateFromNewTranslationDocument();
-	}
-	
-	public void CloseTranslation () {
-		RemoveTranslationFromSubtitles();
-		ClearTranslationStatus();
-		Base.Ui.UpdateFromCloseTranslation();
+		CreateNewTranslationFileProperties();
 	}
 
 	public void OpenTranslation (string path, Encoding encoding) {
+		if (this.IsTranslationLoaded)
+			CloseTranslation();
+
 		SubtitleFactory factory = new SubtitleFactory();
 		factory.Verbose = true;
 		factory.Encoding = encoding;
@@ -120,14 +124,21 @@
 		Translations translations = new Translations();
 		translations.Import(subtitles, openedTranslation);
 
-		ClearTranslationStatus();
 		if (newTranslationFile.SubtitleType != SubtitleType.Unknown)
 			canTranslationBeSaved = true;
 	
 		translationFile = newTranslationFile;
-		Base.Ui.UpdateFromNewTranslationDocument();
 	}
 	
+	public void Close () {
+		DisconnectInitSignals();		
+	}
+	
+	public void CloseTranslation () {
+		RemoveTranslationFromSubtitles();
+		ClearTranslationStatus();
+	}
+	  
 	public bool SaveTranslation (FileProperties newFileProperties) {
 		SubtitleSaver saver = new SubtitleSaver();
 		saver.Save(subtitles, newFileProperties, SubtitleTextType.Translation);
@@ -138,23 +149,12 @@
 		ClearTranslationModified();
 		return true;
 	}
-	
-	public void UpdateFromCommandActivated (CommandTarget target) {
-		if ((target == CommandTarget.Normal) && (!wasTextModified)) {
-			wasTextModified = true;
-			Base.Ui.UpdateFromDocumentModified(true);
-		}
-		else if ((target == CommandTarget.Translation) && (!wasTranslationModified)) {
-			wasTranslationModified = true;
-			Base.Ui.UpdateFromDocumentModified(true);
-		}
-	}
 
 
 	/* Private methods */
 	
 	/* Used in the object construction */
-	private void New (string path, bool wasLoaded) {
+	private void New (string path) {
 		SubtitleFactory factory = new SubtitleFactory();
 		factory.Verbose = true;
 		
@@ -163,7 +163,7 @@
 	}
 	
 	/* Used in the object construction */
-	private void Open (string path, Encoding encoding, bool wasLoaded) {
+	private void Open (string path, Encoding encoding) {
 		SubtitleFactory factory = new SubtitleFactory();
 		factory.Verbose = true;
 		factory.Encoding = encoding;
@@ -173,7 +173,7 @@
 			openedSubtitles = factory.Open(path);
 		}
 		catch (FileNotFoundException) {
-			New(path, wasLoaded);
+			New(path);
 			return;
 		}
 
@@ -183,17 +183,17 @@
 		if (textFile.SubtitleType != SubtitleType.Unknown)
 			canTextBeSaved = true;
 	}
-	
+
 	private void ClearTextModified () {
 		wasTextModified = false;
-		if (!wasTranslationModified) //Update the GUI if translation is also not in modified state
-			Base.Ui.UpdateFromDocumentModified(false);
+		if (!wasTranslationModified) //Emit the event if translation is also not in modified state
+			EmitModificationStatusChangedEvent(false);
 	}
-	
+
 	private void ClearTranslationModified () {
 		wasTranslationModified = false;
-		if (!wasTextModified) //Update the GUI if text is also not in modified state
-			Base.Ui.UpdateFromDocumentModified(false);
+		if (!wasTextModified) //Emit the event if text is also not in modified state
+			EmitModificationStatusChangedEvent(false);
 	}
 	
 	private void CreateNewTranslationFileProperties () {
@@ -212,7 +212,6 @@
 		translationFile = null;
 		canTranslationBeSaved = false;
 		
-		Base.CommandManager.ClearTarget(CommandTarget.Translation);
 		ClearTranslationModified();
 	}
 	
@@ -221,6 +220,32 @@
 		if (extraCount > 0)
 			subtitles.AddExtra(extraCount);	
 	}
+	
+	/* Event members */
+	
+	private void ConnectInitSignals () {
+		Base.CommandManager.CommandActivated += OnCommandManagerCommandActivated;
+	}
+	
+	private void DisconnectInitSignals () {
+		Base.CommandManager.CommandActivated -= OnCommandManagerCommandActivated;
+	}
+	
+	private void OnCommandManagerCommandActivated (object o, CommandActivatedArgs args) {
+    	if ((args.Target == CommandTarget.Normal) && (!wasTextModified)) {
+			wasTextModified = true;
+			EmitModificationStatusChangedEvent(true);
+		}
+		else if ((args.Target == CommandTarget.Translation) && (!wasTranslationModified)) {
+			wasTranslationModified = true;
+			EmitModificationStatusChangedEvent(true);
+		}
+    }
+    	
+	private void EmitModificationStatusChangedEvent (bool modified) {
+		if (ModificationStatusChanged != null)
+			ModificationStatusChanged(modified);
+	}
 
 }
 

Modified: trunk/src/GnomeSubtitles/Core/EventHandlers.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Core/EventHandlers.cs	(original)
+++ trunk/src/GnomeSubtitles/Core/EventHandlers.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -29,11 +29,6 @@
 
 public class EventHandlers {
 
-	public EventHandlers () {
-		ConnectSignals();
-    }
-
-
 	/* File Menu */
 	
 	public void OnFileNew (object o, EventArgs args) {
@@ -79,6 +74,10 @@
 	public void OnFileProperties (object o, EventArgs args) {
 		new FilePropertiesDialog().Show();
 	}
+	
+	public void OnFileClose (object o, EventArgs args) {
+		Base.Ui.Close();
+	}
 
     public void OnFileQuit (object o, EventArgs args) {
 		Base.Ui.Quit();
@@ -164,12 +163,12 @@
 		
 	public void OnViewVideoSubtitlesText (object o, EventArgs args) {
 		if ((o as RadioMenuItem).Active)
-			Base.Ui.Video.Subtitle.ToShowText = true;
+			Base.Ui.Video.Overlay.ToShowText = true;
 	}
 	
 	public void OnViewVideoSubtitlesTranslation (object o, EventArgs args) {
 		if ((o as RadioMenuItem).Active)
-			Base.Ui.Video.Subtitle.ToShowText = false;
+			Base.Ui.Video.Overlay.ToShowText = false;
 	}
 	
 	/* Search Menu */
@@ -228,7 +227,7 @@
 	}
 	
 	public void OnVideoClose (object o, EventArgs args) {
-		Base.Ui.CloseVideo();
+		Base.CloseVideo();
 	}
 
 	public void OnVideoPlayPause (object o, EventArgs args) {
@@ -339,30 +338,6 @@
 		else if (key == Gdk.Key.Insert)
 			OnEditInsertSubtitleAfter(o, EventArgs.Empty);
     }
-    
-    
-    /*	Command Manager */
-
-    private void OnUndoToggled (object o, EventArgs args) {
-    	Base.Ui.Menus.UpdateFromUndoToggled();
-    }
-    
-    private void OnRedoToggled (object o, EventArgs args) {
-    	Base.Ui.Menus.UpdateFromRedoToggled();
-    }
-    
-    private void OnCommandActivated (object o, CommandActivatedArgs args) {
-    	Base.Ui.Menus.UpdateFromCommandActivated();
-    	Base.Document.UpdateFromCommandActivated(args.Target);
-    }
-    
-    /* Private members */
-    
-    private void ConnectSignals () {
-    	Base.CommandManager.UndoToggled += OnUndoToggled;
-    	Base.CommandManager.RedoToggled += OnRedoToggled;
-    	Base.CommandManager.CommandActivated += OnCommandActivated;
-    }
 
 }
 

Modified: trunk/src/GnomeSubtitles/Core/SpellLanguages.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Core/SpellLanguages.cs	(original)
+++ trunk/src/GnomeSubtitles/Core/SpellLanguages.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2008 Pedro Castro
+ * Copyright (C) 2008-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
@@ -43,9 +43,9 @@
 	}
 	
 	/* Events */
-	public event EventHandler ToggleEnabled = null;
-	public event EventHandler TextLanguageChanged = null;
-	public event EventHandler TranslationLanguageChanged = null;
+	public event BasicEventHandler ToggleEnabled = null;
+	public event BasicEventHandler TextLanguageChanged = null;
+	public event BasicEventHandler TranslationLanguageChanged = null;
 	
 	
 	/* Public members */
@@ -139,8 +139,6 @@
 		SetActiveLanguageInConfig(textType, activeLanguageID);
 
 		EmitLanguageChanged(textType);
-		if (!isEmpty)
-			Base.Ui.Menus.SetToolsAutocheckSpellingSensitivity(true);
 	}
 	
 	/* LibEnchant imports */
@@ -222,7 +220,7 @@
 	
 	private void EmitToggleEnabled () {
     	if (this.ToggleEnabled != null)
-    		this.ToggleEnabled(this, EventArgs.Empty);
+    		this.ToggleEnabled();
     }
     
     private void EmitLanguageChanged (SubtitleTextType textType) {
@@ -234,12 +232,12 @@
     
     private void EmitTextLanguageChanged () {
     	if (this.TextLanguageChanged != null)
-    		this.TextLanguageChanged(this, EventArgs.Empty);
+    		this.TextLanguageChanged();
     }
     
     private void EmitTranslationLanguageChanged () {
     	if (this.TranslationLanguageChanged != null)
-    		this.TranslationLanguageChanged(this, EventArgs.Empty);
+    		this.TranslationLanguageChanged();
     }
 
 }

Modified: trunk/src/GnomeSubtitles/Dialog/FileOpenDialog.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Dialog/FileOpenDialog.cs	(original)
+++ trunk/src/GnomeSubtitles/Dialog/FileOpenDialog.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -35,7 +35,7 @@
 	private ArrayList videoFiles = null; //The full paths of the video files in the current fir
 	private ArrayList videoFilenames = null; //The filenames of videoFiles, without extensions
 	
-	private string chosenVideoFilename = String.Empty;
+	private Uri chosenVideoUri = null;
 	private bool autoChooseVideoFile = true;
 
 	/* Constant strings */
@@ -75,11 +75,11 @@
 	/* Public properties */
 	
 	public bool HasVideoFilename {
-		get { return chosenVideoFilename != String.Empty; }
+		get { return chosenVideoUri != null; }
 	}
 	
-	public string VideoFilename {
-		get { return chosenVideoFilename; }
+	public Uri VideoUri {
+		get { return chosenVideoUri; }
 	}
 	
 	/* Protected members */
@@ -254,7 +254,7 @@
 			}
 			if (videoComboBox.Active > 0) {
 				int videoFileIndex = videoComboBox.Active - 2;
-				chosenVideoFilename = videoFiles[videoFileIndex] as string;
+				chosenVideoUri = new Uri(videoFiles[videoFileIndex] as string);
 			}			
 			returnValue = true;
 		}

Modified: trunk/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs	(original)
+++ trunk/src/GnomeSubtitles/Dialog/TimingsSynchronizeDialog.cs	Sun Mar  8 20:39:18 2009
@@ -117,7 +117,7 @@
 		
 		TreePath path = selectedPaths[0];
 		int subtitleNumber = Core.Util.PathToInt(path);
-		Subtitle subtitle = Base.Ui.View.Selection.Subtitle;
+		Subtitle subtitle = Base.Ui.View.Selection.Subtitle; //TODO refactor: change to FirstSubtitle?
 		
 		/* Get current start */
 		Timing currentTiming = new Timing(subtitle.Frames.Start, subtitle.Times.Start);

Modified: trunk/src/GnomeSubtitles/Dialog/VideoOpenDialog.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Dialog/VideoOpenDialog.cs	(original)
+++ trunk/src/GnomeSubtitles/Dialog/VideoOpenDialog.cs	Sun Mar  8 20:39:18 2009
@@ -27,7 +27,7 @@
 public class VideoOpenDialog : GladeDialog {
 	protected new FileChooserDialog dialog = null;
 	private static string[] extensions = { "avi", "mpeg", "mpg", "mp4", "ogm", "divx", "xvid", "mov", "ogg", "mkv" };
-	private string chosenUri = String.Empty;
+	private Uri chosenUri = null;
 	
 	/* Constant strings */
 	private const string gladeFilename = "VideoOpenDialog.glade";
@@ -46,7 +46,7 @@
 	
 	/* Public properties */
 
-	public string Uri {
+	public Uri Uri {
 		get { return chosenUri; }
 	}
 	
@@ -77,7 +77,9 @@
 	
 	private void OnResponse (object o, ResponseArgs args) {
 		if (args.ResponseId == ResponseType.Ok) {
-			chosenUri = (dialog as FileChooserDialog).Uri;
+			if (dialog.Uri != null)
+				chosenUri = new Uri(dialog.Uri);
+
 			returnValue = true;
 		}
 		Close();

Modified: trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEdit.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEdit.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEdit.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -33,9 +33,6 @@
 	private SubtitleEditText textEdit = null;
 	private SubtitleEditTranslation translationEdit = null;
 
-	/* Other */
-	private Subtitle subtitle = null;
-	
 
 	public SubtitleEdit() {
 		hBox = Base.GetWidget(WidgetNames.SubtitleEdit) as HBox;
@@ -43,7 +40,7 @@
 		textEdit = new SubtitleEditText(Base.GetWidget(WidgetNames.SubtitleEditText) as TextView);
 		translationEdit = new SubtitleEditTranslation(Base.GetWidget(WidgetNames.SubtitleEditTranslation) as TextView);
 		
-		ConnectSignals();
+		Base.InitFinished += OnBaseInitFinished;
     }
 
 
@@ -52,13 +49,8 @@
     public bool Enabled {
     	get { return hBox.Sensitive; }
     	set {
-			if (hBox.Sensitive == value)
-				return;
-
-			if (value == false)
-				ClearFields();
-
-			hBox.Sensitive = value;
+			if (hBox.Sensitive != value)
+				hBox.Sensitive = value;
     	}
     }
     
@@ -95,39 +87,6 @@
     	translationEdit = this.translationEdit.TextView;
     }
 	
-	public void BlankStartUp () {
-    	ClearFields();
-    }
-    
-    public void UpdateFromNewDocument (bool wasLoaded) {
-		spinButtons.UpdateFromTimingMode(Base.TimingMode, subtitle);
-		translationEdit.Visible = false;
-	}
-	
-	public void UpdateFromNewTranslationDocument () {
-		if (Enabled)
-			translationEdit.LoadSubtitle(subtitle);
-
-    	translationEdit.Visible = true;
-    }
-    
-    public void UpdateFromCloseTranslation () {
-    	translationEdit.ClearFields();
-    	translationEdit.Visible = false;
-    }
-	
-	public void UpdateFromSelection (Subtitle subtitle) {
-	   	this.Enabled = true;
-    	this.subtitle = subtitle;
-    	spinButtons.LoadTimings(subtitle);
-    	textEdit.LoadSubtitle(subtitle);
-    	translationEdit.LoadSubtitle(subtitle);
-    }
-
-	public void UpdateFromTimingMode (TimingMode mode) {
-		spinButtons.UpdateFromTimingMode(mode, subtitle);
-	}
-	
 	public bool GetTextSelectionBounds (out int start, out int end, out SubtitleTextType textType) {
     	if (textEdit.IsFocus) {
     		textType = SubtitleTextType.Text;
@@ -159,30 +118,16 @@
     		translationEdit.ReplaceSelection(replacement);
     }
 
-
-    /* Private Methods */
-
-    private void ClearFields () {
-    	spinButtons.ClearFields();
-    	textEdit.ClearFields();
-    	translationEdit.ClearFields();
-    }
-
-
-    /* Event methods */
     
-    private void OnTextViewToggleOverwrite (object o, EventArgs args) {
-    	/* Reflect the update to the other respective TextView */
-    	if (o == textEdit)
-    		translationEdit.ToggleOverwriteSilent();
-    	else
-    		textEdit.ToggleOverwriteSilent();
-    }
-
-    private void ConnectSignals () {
-    	textEdit.ToggleOverwrite += OnTextViewToggleOverwrite;
-    	translationEdit.ToggleOverwrite += OnTextViewToggleOverwrite;
+    /* Event members */
+    
+    private void OnBaseInitFinished () {
+    	Base.Ui.View.Selection.Changed += OnSubtitleSelectionChanged;
     }
+    
+    private void OnSubtitleSelectionChanged (TreePath[] paths, Subtitle subtitle) {
+		this.Enabled = (subtitle != null);
+	}
 
 }
 

Modified: trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditSpinButtons.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditSpinButtons.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditSpinButtons.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -38,7 +38,6 @@
 	private const int timeStepIncrement = 100; //milliseconds
 	private const int maxFrames = 3000000;
 	private const int framesStepIncrement = 1;
-	
 
 
 	public SubtitleEditSpinButtons () {
@@ -49,25 +48,17 @@
 		
 		/* Initialize */
 		startSpinButton.WidthRequest = Util.SpinButtonTimeWidth(startSpinButton); //Only need to set one of the spin buttons' width
-    	startSpinButton.Alignment = 0.5f;
-    	endSpinButton.Alignment = 0.5f;
-    	durationSpinButton.Alignment = 0.5f; 
     	
     	/* Set timing mode to Times */
     	SetTimingMode(TimingMode.Times); //Initial timing mode is Times
+    	
+    	Base.InitFinished += OnBaseInitFinished;
 	}
 	
 	/* Public methods */
-	
-	public void UpdateFromTimingMode (TimingMode mode, Subtitle subtitle) {
-		if (mode == timingMode)
-			return;
 
-		SetTimingMode(mode);
-		LoadTimings(subtitle);
-	}
-
-	public void LoadTimings (Subtitle subtitle) {
+	public void LoadTimings () {
+		Subtitle subtitle = Base.Ui.View.Selection.Subtitle;
     	if (subtitle == null)
     		return;
 
@@ -76,14 +67,6 @@
 		LoadDurationTiming(subtitle);
     }
     
-    public void ClearFields () {
-        DisconnectSpinButtonsChangedSignals();
-		startSpinButton.Text = String.Empty;
-		endSpinButton.Text = String.Empty;
-		durationSpinButton.Text = String.Empty;
-    	ConnectSpinButtonsChangedSignals();
-    }
-    
     public void GetWidgets (out SpinButton startSpinButton, out SpinButton endSpinButton, out SpinButton durationSpinButton) {
     	startSpinButton = this.startSpinButton;
     	endSpinButton = this.endSpinButton;
@@ -165,6 +148,15 @@
     	durationSpinButton.ValueChanged += OnDurationValueChanged;
     }
     
+	private void ClearFields () {
+        DisconnectSpinButtonsChangedSignals();
+		startSpinButton.Text = String.Empty;
+		endSpinButton.Text = String.Empty;
+		durationSpinButton.Text = String.Empty;
+    	ConnectSpinButtonsChangedSignals();
+    }
+    
+    
     /* Event methods */
     
 	private void ConnectSpinButtonsChangedSignals () {
@@ -199,6 +191,26 @@
 		else
 			Base.CommandManager.Execute(new ChangeDurationCommand(TimeSpan.FromMilliseconds(durationSpinButton.Value)));
 	}
+	
+	private void OnBaseInitFinished () {
+		Base.TimingModeChanged += OnBaseTimingModeChanged;
+		Base.Ui.View.Selection.Changed += OnSubtitleSelectionChanged;
+	}
+	
+	private void OnBaseTimingModeChanged (TimingMode newTimingMode) {
+    	if (timingMode == newTimingMode)
+			return;
+
+		SetTimingMode(newTimingMode);
+		LoadTimings();
+    }
+    
+    private void OnSubtitleSelectionChanged (TreePath[] paths, Subtitle subtitle) {
+    	if (subtitle != null)
+    		LoadTimings();
+    	else
+    		ClearFields();
+    }
 
 }
 

Modified: trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditText.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditText.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditText.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2008 Pedro Castro
+ * Copyright (C) 2007-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
@@ -21,12 +21,14 @@
 using GnomeSubtitles.Core.Command;
 using Gtk;
 using SubLib.Core.Domain;
+using System;
 
 namespace GnomeSubtitles.Ui.Edit {
 
 public class SubtitleEditText : SubtitleEditTextView {
 
 	public SubtitleEditText (TextView textView) : base(textView) {
+		Base.InitFinished += OnBaseInitFinished;
 	}
 	
 	protected override SubtitleTextType GetTextType () {
@@ -53,9 +55,19 @@
 		return Base.SpellLanguages.ActiveTextLanguage;
 	}
 	
+	/* Event members */
+	
 	protected override void ConnectLanguageChangedSignal () {
 		Base.SpellLanguages.TextLanguageChanged += OnSpellLanguageChanged;
 	}
+	
+	private void OnBaseInitFinished () {
+		Base.Ui.Edit.TranslationEdit.ToggleOverwrite += OnTranslationEditToggleOverwrite;
+	}
+	
+	private void OnTranslationEditToggleOverwrite (object o, EventArgs args) {
+		ToggleOverwriteSilent();
+	}
 }
 
 }
\ No newline at end of file

Modified: trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextView.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextView.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextView.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -60,7 +60,7 @@
     	textView.Buffer.TagTable.Add(italicTag);
     	textView.Buffer.TagTable.Add(underlineTag);
 
-    	ConnectSignals();
+		Base.InitFinished += OnBaseInitFinished;
 	}
 	
 	/* Abstract members */
@@ -90,11 +90,6 @@
     	get { return textView.IsFocus; }    
     }
     
-    /// <summary>The visibility of the scrolled window this <see cref="TextView" /> is inside of.</summary>
-    public bool Visible {
-    	set { Base.GetWidget(WidgetNames.SubtitleEditTranslationScrolledWindow).Visible = value; }
-    }
-    
     public bool OverwriteStatus {
     	get { return textView.Overwrite; }
     }
@@ -115,10 +110,6 @@
 	
 	/* Public methods */
 	
-	public void ClearFields () {
-		SetText(String.Empty);	
-	}
-		
 	public void LoadSubtitle (Subtitle subtitle) {
 		this.subtitle = subtitle;
 		LoadTags(subtitle.Style);
@@ -187,13 +178,6 @@
 			return false;
 		}
 	}
-    
-    /// <summary>Toggles the overwrite status without emitting its event.</summary>
-    public void ToggleOverwriteSilent () {
-    	isToggleOverwriteSilent = true;
-    	textView.Overwrite = !textView.Overwrite;
-    	isToggleOverwriteSilent = false;
-    }
 
     /* GtkSpell */
 	[DllImport ("libgtkspell")]
@@ -327,8 +311,19 @@
 		textView.GrabFocus();
 	}
 	
+	private ScrolledWindow GetScrolledWindow () {
+		return textView.Parent as ScrolledWindow;
+	}
+	
+	
+	/* Event members */
 	
-	/* Event methods */
+	/// <summary>Toggles the overwrite status without emitting its event.</summary>
+    protected void ToggleOverwriteSilent () {
+    	isToggleOverwriteSilent = true;
+    	textView.Overwrite = !textView.Overwrite;
+    	isToggleOverwriteSilent = false;
+    }
 
 	private void OnBufferChanged (object o, EventArgs args) {
 		if (!isBufferChangeSilent)
@@ -385,7 +380,7 @@
 			EmitToggleOverwrite();
 	}
 	
-	private void OnSpellToggleEnabled (object o, EventArgs args) {
+	private void OnSpellToggleEnabled () {
 		bool enabled = Base.SpellLanguages.Enabled;
 		if (enabled) {
 			GtkSpellAttach();
@@ -422,7 +417,7 @@
     	}
     }
 
-    private void ConnectSignals () {
+    private void OnBaseInitFinished () {
 		/* Buffer signals */
 		textView.Buffer.Changed += OnBufferChanged;
 		textView.Buffer.MarkSet += OnBufferMarkSet;
@@ -439,6 +434,15 @@
 		/* Spell signals */
 		Base.SpellLanguages.ToggleEnabled += OnSpellToggleEnabled;
 		ConnectLanguageChangedSignal();
+		
+		Base.Ui.View.Selection.Changed += OnSubtitleSelectionChanged;
+    }
+    
+    private void OnSubtitleSelectionChanged (TreePath[] paths, Subtitle subtitle) {
+    	if (subtitle != null)
+    		LoadSubtitle(subtitle);
+    	else
+    		ClearFields();
     }
     
     private void EmitToggleOverwrite () {
@@ -448,12 +452,21 @@
     
     /* Protected members */
     
-    protected void OnSpellLanguageChanged (object o, EventArgs args) {
+    protected void OnSpellLanguageChanged () {
 		if (Base.SpellLanguages.Enabled) {
 			SpellLanguage language = GetSpellActiveLanguage();
 			GtkSpellSetLanguage(language);
 		}
 	}
+	
+	protected void SetVisibility (bool visible) {
+		GetScrolledWindow().Visible = visible;
+	}
+	
+	protected void ClearFields () {
+		SetText(String.Empty);	
+	}
+
 
 }
 

Modified: trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTranslation.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTranslation.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/Edit/SubtitleEditTranslation.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2008 Pedro Castro
+ * Copyright (C) 2007-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
@@ -21,12 +21,14 @@
 using GnomeSubtitles.Core.Command;
 using Gtk;
 using SubLib.Core.Domain;
+using System;
 
 namespace GnomeSubtitles.Ui.Edit {
 
 public class SubtitleEditTranslation : SubtitleEditTextView {
 
 	public SubtitleEditTranslation (TextView textView) : base(textView) {
+		Base.InitFinished += OnBaseInitFinished;
 	}
 	
 	protected override SubtitleTextType GetTextType () {
@@ -53,10 +55,36 @@
 		return Base.SpellLanguages.ActiveTranslationLanguage;
 	}
 	
+	/* Event members */
+	
 	protected override void ConnectLanguageChangedSignal () {
 		Base.SpellLanguages.TranslationLanguageChanged += OnSpellLanguageChanged;
 	}
 
+	private void OnBaseInitFinished () {
+		Base.Ui.Edit.TextEdit.ToggleOverwrite += OnTextEditToggleOverwrite;
+		Base.TranslationLoaded += OnBaseTranslationLoaded;
+		Base.TranslationUnloaded += OnBaseTranslationUnloaded;
+	}
+	
+	private void OnBaseTranslationLoaded () {
+		Subtitle subtitle = Base.Ui.View.Selection.Subtitle;
+		if (subtitle != null)
+			LoadSubtitle(subtitle);
+
+    	SetVisibility(true);
+	}
+	
+	private void OnBaseTranslationUnloaded () {
+		ClearFields();
+    	SetVisibility(false);
+	}
+	
+	private void OnTextEditToggleOverwrite (object o, EventArgs args) {
+		ToggleOverwriteSilent();
+	}
+	
+
 }
 
 }
\ No newline at end of file

Modified: trunk/src/GnomeSubtitles/Ui/MainUi.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/MainUi.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/MainUi.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -59,9 +59,11 @@
 		status = new Status();
 
 		glade.Autoconnect(handlers);
+		Base.InitFinished += OnBaseInitFinished;
 		
 		window.Visible = true;
     }
+    
 
 	/* Public properties */
 
@@ -98,19 +100,21 @@
     	string[] args = Base.ExecutionContext.Args;
     	if (args.Length > 0) {
     		string subtitleFile = args[0];
-    		string videoFile = Base.Config.PrefsVideoAutoChooseFile ? VideoFiles.FindMatchingVideo(subtitleFile) : String.Empty;
-			Open(subtitleFile, -1, videoFile);
+    		Uri videoUri = Base.Config.PrefsVideoAutoChooseFile ? VideoFiles.FindMatchingVideo(subtitleFile) : null;
+			Open(subtitleFile, -1, videoUri);
 		}
-		else
-			BlankStartUp();
+    }
+    
+    /// <summary>Closes the open file.</summary>
+    public void Close () {
+		if (ToCloseAfterWarning())
+			Base.CloseDocument();
     }
     
     /// <summary>Quits the application.</summary>
     public void Quit () {
-		if (ToCloseAfterWarning()) {
-			video.Quit();
+		if (ToCloseAfterWarning())
 			Base.Quit();
-		}
     }
     
 	/// <summary>Kills the window in the most quick and unfriendly way.</summary>
@@ -126,15 +130,11 @@
     		return;
 
 		if (path == String.Empty) {
-			//To translators: this is the filename for new files (before being saved)
+			//To translators: this is the filename for new files (before being saved for the first time)
 			path = Catalog.GetString("Unsaved Subtitles");
 		}
 
-		Base.CreateDocumentNew(path);
-
-		if (Base.Document.Subtitles.Count == 0) {
-			Base.CommandManager.Execute(new InsertFirstSubtitleCommand());
-		}
+		Base.NewDocument(path);
     }
     
     /// <summary>Shows the open dialog and possibly opens a subtitle.</summary>
@@ -143,12 +143,12 @@
     public void Open () {
     	FileOpenDialog dialog = new FileOpenDialog();
     	dialog.Show();
-    	bool toOpen = dialog.WaitForResponse();
-    	if (toOpen && ToOpenAfterWarning()) {
+    	bool gotOpenResponse = dialog.WaitForResponse();
+    	if (gotOpenResponse && ToOpenAfterWarning()) {
     		string filename = dialog.Filename;
     		int codePage = (dialog.HasChosenEncoding ? dialog.ChosenEncoding.CodePage : -1);
-    		string videoFilename = dialog.VideoFilename;
-    		Open(filename, codePage, videoFilename);
+    		Uri videoUri = dialog.VideoUri;
+    		Open(filename, codePage, videoUri);
     	}
     }
         
@@ -157,16 +157,11 @@
     	dialog.Show();
 		bool toOpen = dialog.WaitForResponse();
 		if (toOpen) {
-			string videoUri = dialog.Uri;
-			OpenVideo(videoUri);
+			Menus.SetViewVideoActivity(true);
+			Base.OpenVideo(dialog.Uri);
 		}
     }
     
-    public void CloseVideo () {
-    	Video.Close();
-    	UpdateFromCloseVideo();
-    }
-    
     /// <summary>Executes a Save operation.</summary>
     /// <remarks>If the document hasn't been saved before, a SaveAs is executed.</remarks>
     /// <returns>Whether the file was saved or not.</returns>
@@ -199,7 +194,7 @@
     	if (!ToCreateNewTranslationAfterWarning())
     		return;
 
-		Base.Document.NewTranslation();
+		Base.NewTranslation();
     }
     
     /// <summary>Shows the open translation dialog and possibly opens a file.</summary>
@@ -247,78 +242,21 @@
     	if (!ToCloseTranslationAfterWarning())
     		return;
 
-		Base.Document.CloseTranslation();
+		Base.CloseTranslation();
     }
 
-	public void UpdateFromDocumentModified (bool modified) {
-		string prefix = (modified ? "*" : String.Empty);
-		window.Title = prefix + Base.Document.TextFile.Filename +
-			" - " + Base.ExecutionContext.ApplicationName;
-	}
-	
-	public void UpdateFromNewDocument (bool wasLoaded) {
-		view.UpdateFromNewDocument(wasLoaded);
-		edit.UpdateFromNewDocument(wasLoaded);
-		menus.UpdateFromNewDocument(wasLoaded);
-		video.UpdateFromNewDocument(wasLoaded);
-	}
-	
-	public void UpdateFromNewTranslationDocument () {
-		view.UpdateFromNewTranslationDocument();
-		edit.UpdateFromNewTranslationDocument();
-		menus.UpdateFromNewTranslationDocument();
-	}
-	
-	public void UpdateFromCloseTranslation () {
-		view.UpdateFromCloseTranslation();
-		edit.UpdateFromCloseTranslation();
-		menus.UpdateFromCloseTranslation();
-	}
-	
-	public void UpdateFromTimingMode (TimingMode mode) {
-		view.UpdateFromTimingMode(mode);
-		edit.UpdateFromTimingMode(mode);
-		video.UpdateFromTimingMode(mode);
-	}
-	
-	public void UpdateFromOpenVideo () {
-		menus.UpdateFromOpenVideo();
-	}
-	
-	public void UpdateFromCloseVideo () {
-		menus.UpdateFromCloseVideo();
-	}
-	
-	/// <summary>Updates the various parts of the GUI based on the current selection.</summary>
-	public void UpdateFromSelection () {
-		if (view.Selection.Count == 1)
-			UpdateFromSelection(view.Selection.Subtitle);
-		else
-			UpdateFromSelection(view.Selection.Paths);
-	}
-	
-	/// <summary>Updates the various parts of the GUI based on the current subtitle count.</summary>
-	public void UpdateFromSubtitleCount () {
-		int count = Base.Document.Subtitles.Collection.Count;
-		menus.UpdateFromSubtitleCount(count);
-	}
 
-	
 	/* Private members */
 	
 	/// <summary>Opens a subtitle file, given its filename, code page and video filename.</summary>
 	/// <param name="path">The path of the subtitles file to open.</param>
 	/// <param name="codePage">The code page of the filename. To use autodetection, set it to -1.</param>
-	/// <param name="videoFilename">The videoFilename to open. If <see cref="String.Empty" />, no video will be opened.</param>
+	/// <param name="videoUri">The URI of the video to open. If null, no video will be opened.</param>
 	/// <remarks>An error dialog is presented if an exception is caught during open.</remarks>
-    private void Open (string path, int codePage, string videoFilename) {
+    private void Open (string path, int codePage, Uri videoUri) {
     	try {
-    		Encoding encoding =  CodePageToEncoding(codePage);
-    		Base.CreateDocumentOpen(path, encoding);
-			view.Selection.SelectFirst();
-		
-			if (videoFilename != String.Empty)
-				OpenVideo(videoFilename);
+    		Encoding encoding = CodePageToEncoding(codePage);
+    		Base.Open(path, encoding, videoUri);
 		}
 		catch (Exception exception) {
 			SubtitleFileOpenErrorDialog errorDialog = new SubtitleFileOpenErrorDialog(path, exception);
@@ -333,6 +271,7 @@
     /// <param name="codePage">The code page.</param>
     /// <returns>The respective <see cref="Encoding" />, or null if codePage == -1.</returns>
     /// <exception cref="EncodingNotSupportedException">Thrown if a detected encoding is not supported by the platform.</exception>
+    //TODO move to util
     private Encoding CodePageToEncoding (int codePage) {
     	if (codePage == -1)
     		return null;
@@ -352,7 +291,7 @@
     private void OpenTranslation (string path, int codePage) {
     	try {
     		Encoding encoding = (codePage == -1 ? null : Encoding.GetEncoding(codePage));
-    		Base.Document.OpenTranslation(path, encoding);
+    		Base.OpenTranslation(path, encoding);
 		}
 		catch (Exception exception) {
 			SubtitleFileOpenErrorDialog errorDialog = new SubtitleFileOpenErrorDialog(path, exception);
@@ -362,15 +301,7 @@
 				Open();
 		}
     }
-    
-    private void OpenVideo (string videoUriString) {
-    	Menus.SetViewVideoActivity(true);
-    	Uri videoUri = null;
 
-		videoUri = new Uri(videoUriString);
-		Video.Open(videoUri);
-    }
-    
     private void Save (FileProperties fileProperties) {
 		try {
 			Base.Document.Save(fileProperties);
@@ -380,7 +311,7 @@
 			errorDialog.Show();
 			bool toSaveAgain = errorDialog.WaitForResponse();
 	    	if (toSaveAgain)
-	    		SaveAs();			
+	    		SaveAs();	
 		}
 	}
 	
@@ -413,34 +344,6 @@
 		TimingMode timingMode = Base.TimingMode;
 		return new FileProperties(path, encoding, subtitleType, timingMode, newlineType);
 	}
-	
-	/// <summary>Executes a blank startup operation.</summary>
-	/// <remarks>This is used when no document is loaded.</remarks>
-	private void BlankStartUp () {
-    	menus.BlankStartUp();
-    	view.BlankStartUp();
-    	edit.BlankStartUp();
-    }
-		
-	/// <summary>Updates the GUI from the specified selected Subtitle.</summary>
-	/// <param name="subtitle">The subtitle that is currently selected.</param>
-	/// <remarks>This is only used when there is only one selected path. When there are zero or more than one
-	/// paths selected, <see cref="UpdateFromSelection(TreePath[])" /> must be used.</remarks>
-	private void UpdateFromSelection (Subtitle subtitle) {
-		menus.UpdateFromSelection(subtitle);
-		edit.UpdateFromSelection(subtitle);
-		video.UpdateFromSelection(true);
-	}
-
-	/// <summary>Updates the GUI from the specified selected paths.</summary>
-	/// <param name="paths">The paths from which the GUI should be updated.</param>
-	/// <remarks>This is only used when there are either zero or more than one selected paths. When there is only
-	/// one path selected, <see cref="UpdateFromSelection(Subtitle)" /> must be used.</remarks>
-	private void UpdateFromSelection (TreePath[] paths) {
-		menus.UpdateFromSelection(paths);
-		edit.Enabled = false;
-		video.UpdateFromSelection(false);
-	}    
 
 	/// <summary>Whether there are unsaved normal changes.</summary>
 	private bool ExistTextUnsavedChanges () {
@@ -523,6 +426,35 @@
    		else
     		return true; 
 	}
+	
+	private void UpdateTitleModificationStatus (bool modified) {
+		string prefix = (modified ? "*" : String.Empty);
+		window.Title = prefix + Base.Document.TextFile.Filename +
+			" - " + Base.ExecutionContext.ApplicationName;
+	}
+	
+	/* Event members */
+	
+	private void OnBaseInitFinished () {
+		Base.DocumentLoaded += OnBaseDocumentLoaded;
+		Base.DocumentUnloaded += OnBaseDocumentUnloaded;
+	}
+	
+	private void OnBaseDocumentLoaded (Document document) {
+   		document.ModificationStatusChanged += OnBaseDocumentModificationStatusChanged;
+    }
+    
+    private void OnBaseDocumentUnloaded (Document document) {
+    	if (document != null) {
+    		document.ModificationStatusChanged -= OnBaseDocumentModificationStatusChanged;
+    	}
+    	UpdateTitleModificationStatus(false);
+    }
+    
+    private void OnBaseDocumentModificationStatusChanged (bool modified) {
+    	UpdateTitleModificationStatus(modified);
+	}
+
 
 }
 

Modified: trunk/src/GnomeSubtitles/Ui/Menus.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/Menus.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/Menus.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -36,92 +36,10 @@
 
 	public Menus () {
 		SetToolbarHomogeneity(); //TODO needed until homogeneity definition in glade starts working
-	}
-	
-	public void BlankStartUp () {
-		SetBlankSensitivity();
+		SetDocumentSensitivity(false);
 		SetBlankActivity();
-	}
-
-	public void UpdateFromNewDocument (bool wasLoaded) {
-		SetNewDocumentSensitivity(wasLoaded);
-		SetSubtitleCountDependentSensitivity(Base.Document.Subtitles.Collection.Count);
-		SetFrameRateMenus();
-		SetActiveTimingMode(Base.TimingMode);
-		
-		SetToolsAutocheckSpellingSensitivity(true);
-		SetCheckMenuItemActivity(WidgetNames.ToolsAutocheckSpelling, Base.SpellLanguages.Enabled);
-	}
-	
-	public void UpdateFromNewTranslationDocument () {
-		SetTranslationSensitivity(true);
-		UpdateUndoRedoMessages();
-	}
-	
-	public void UpdateFromCloseTranslation () {
-    	SetTranslationSensitivity(false);
-    	SetViewVideoSubtitlesActivity(true);
-    	UpdateUndoRedoMessages();
-    }
-	
-	public void UpdateFromSelection (Subtitle subtitle) { 
-		SetStylesActivity(subtitle.Style.Bold, subtitle.Style.Italic, subtitle.Style.Underline);
-		SetNonZeroSelectionDependentSensitivity(true);
-		SetOneSelectionDependentSensitivity(true);
-	}
 	
-	public void UpdateFromSelection (TreePath[] paths) {
-		SetOneSelectionDependentSensitivity(false);
-	
-		if (paths.Length == 0)
-			UpdateFromNoSelection();
-		else
-			UpdateFromSelectedPaths(paths);
-	}
-	
-	public void UpdateFromSubtitleCount (int count) {
-		SetSubtitleCountDependentSensitivity(count);
-	}
-	
-	public void UpdateFromUndoToggled()  {
-   		Widget button = Base.GetWidget(WidgetNames.UndoButton);
-   		button.Sensitive = !button.Sensitive;
-    		
-		MenuItem menuItem = Base.GetWidget(WidgetNames.EditUndo) as MenuItem;
-		menuItem.Sensitive = !menuItem.Sensitive;
-		if (!menuItem.Sensitive)
-			(menuItem.Child as Label).Text = Catalog.GetString("Undo");
-	}
-    
-     public void UpdateFromRedoToggled () {
-    	Widget button = Base.GetWidget(WidgetNames.RedoButton);
-    	button.Sensitive = !button.Sensitive;
-    		
-		MenuItem menuItem = Base.GetWidget(WidgetNames.EditRedo) as MenuItem;
-    	menuItem.Sensitive = !menuItem.Sensitive;
-    	if (!menuItem.Sensitive)
-			(menuItem.Child as Label).Text = Catalog.GetString("Redo");
-    }
-    
-    public void UpdateFromCommandActivated () {
-    	UpdateUndoRedoMessages();
-    }
-    
-    public void UpdateFromOpenVideo () {
-    	SetVideoSensitivity(true);
-    	SetViewVideoSubtitlesSensitivity();
-    }
-    
-    public void UpdateFromCloseVideo () {
-    	SetVideoSensitivity(false);
-    	SetViewVideoSubtitlesSensitivity(false, false);
-    }
-
-	public void SetActiveTimingMode (TimingMode mode) {
-		if (mode == TimingMode.Times)
-			SetCheckMenuItemActivity(WidgetNames.ViewTimes, true);
-		else
-			SetCheckMenuItemActivity(WidgetNames.ViewFrames, true);
+		Base.InitFinished += OnBaseInitFinished;
 	}
 	
 	public void SetCutCopySensitivity (bool sensitivity) {
@@ -151,10 +69,6 @@
 		SetSensitivity(WidgetNames.SearchFindPrevious, true);
 	}
 	
-	public void SetToolsAutocheckSpellingSensitivity (bool sensitivity) {
-		SetSensitivity(WidgetNames.ToolsAutocheckSpelling, sensitivity);
-	}
-	
 	public void SetViewVideoActivity (bool activity) {
 		SetCheckMenuItemActivity(WidgetNames.ViewVideo, activity);
 	}
@@ -199,18 +113,6 @@
 
 	/* Private members */
 	
-	private void UpdateFromNoSelection () {
-		SetNonZeroSelectionDependentSensitivity(false);
-		SetStylesActivity(false, false, false);
-	}
-	
-	private void UpdateFromSelectedPaths (TreePath[] paths) {
-		SetNonZeroSelectionDependentSensitivity(true);
-		bool bold, italic, underline;
-		GetGlobalStyles(paths, out bold, out italic, out underline);
-		SetStylesActivity(bold, italic, underline);		
-	}
-	
 	/// <summary>Sets the sensitivity depending on 1 or more selected subtitles.</summary>
 	/// <param name="sensitivity">Whether the items are set sensitive.</param>
 	private void SetNonZeroSelectionDependentSensitivity (bool sensitivity) {
@@ -241,102 +143,65 @@
 		}	
 	}
 	
-	private void SetBlankSensitivity () {
+	private void SetBlankActivity () {
+		SetCheckMenuItemActivity(WidgetNames.ToolsAutocheckSpelling, Base.SpellLanguages.Enabled);
+	}
+	
+	private void SetDocumentSensitivity (bool documentLoaded) {
+		/* Set Sensitivity that is equal to the document loaded status */
+
 		/* File Menu */
-		SetSensitivity(WidgetNames.FileSave, false);
-		SetSensitivity(WidgetNames.FileSaveAs, false);
-		SetSensitivity(WidgetNames.FileHeaders, false);
+		SetSensitivity(WidgetNames.FileSave, documentLoaded);
+		SetSensitivity(WidgetNames.FileSaveAs, documentLoaded);
+		SetSensitivity(WidgetNames.FileHeaders, documentLoaded);
+		SetSensitivity(WidgetNames.FileProperties, documentLoaded);
+		SetSensitivity(WidgetNames.FileTranslationNew, documentLoaded);
+		SetSensitivity(WidgetNames.FileTranslationOpen, documentLoaded);
+		SetSensitivity(WidgetNames.FileClose, documentLoaded);
 		/* Edit Menu */
-		SetSensitivity(WidgetNames.EditUndo, false);
-		SetSensitivity(WidgetNames.EditRedo, false);	
-		SetSensitivity(WidgetNames.EditCut, false);
-		SetSensitivity(WidgetNames.EditCopy, false);
-		SetSensitivity(WidgetNames.EditPaste, false);
+		SetMenuSensitivity(WidgetNames.EditInsertSubtitleMenu, documentLoaded);
+		/* View Menu */
+		SetSensitivity(WidgetNames.ViewTimes, documentLoaded); //TODO always visible
+		SetSensitivity(WidgetNames.ViewFrames, documentLoaded); //TODO always visible
+		SetViewVideoSubtitlesSensitivity();
 		/* Search Menu */
-		SetSensitivity(WidgetNames.SearchFind, false);
-		SetSensitivity(WidgetNames.SearchFindNext, false);
-		SetSensitivity(WidgetNames.SearchFindPrevious, false);
-		SetSensitivity(WidgetNames.SearchReplace, false);
+		SetSensitivity(WidgetNames.SearchFind, documentLoaded);
+		SetSensitivity(WidgetNames.SearchReplace, documentLoaded);
 		/* Timings Menu */
-		SetSensitivity(WidgetNames.TimingsSynchronize, false);
+		SetSensitivity(WidgetNames.TimingsSynchronize, documentLoaded);
 		/* Tools Menu */
-		SetToolsAutocheckSpellingSensitivity(false);
+		SetToolsAutocheckSpellingSensitivity(documentLoaded);
+		SetSensitivity(WidgetNames.ToolsSetTextLanguage, documentLoaded);
+		SetSensitivity(WidgetNames.ToolsSetTranslationLanguage, documentLoaded);
 		/* Toolbar */
-		SetSensitivity(WidgetNames.SaveButton, false);
-		SetSensitivity(WidgetNames.UndoButton, false);
-		SetSensitivity(WidgetNames.RedoButton, false);
-		SetSensitivity(WidgetNames.CutButton, false);
-		SetSensitivity(WidgetNames.CopyButton, false);
-		SetSensitivity(WidgetNames.PasteButton, false);
-		SetSensitivity(WidgetNames.InsertSubtitleButton, false);
-		SetSensitivity(WidgetNames.BoldButton, false);
-		SetSensitivity(WidgetNames.ItalicButton, false);
-		SetSensitivity(WidgetNames.UnderlineButton, false);
-	}
-	
-	private void SetBlankActivity () {
-		SetCheckMenuItemActivity(WidgetNames.ToolsAutocheckSpelling, Base.SpellLanguages.Enabled);
-	}
-	
-	private void SetNewDocumentSensitivity (bool wasLoaded) {
-		if (!wasLoaded) {	
-			/* File Menu */
-			SetSensitivity(WidgetNames.FileSave, true);
-			SetSensitivity(WidgetNames.FileSaveAs, true);
-			SetSensitivity(WidgetNames.FileHeaders, true);
-			SetSensitivity(WidgetNames.FileProperties, true);
-			SetSensitivity(WidgetNames.FileTranslationNew, true);
-			SetSensitivity(WidgetNames.FileTranslationOpen, true);
-			/* Edit Menu */
-			SetMenuSensitivity(WidgetNames.EditInsertSubtitleMenu, true);
-			SetSensitivity(WidgetNames.EditDeleteSubtitles, true);
-			/* View Menu */
-			SetSensitivity(WidgetNames.ViewTimes, true);
-			SetSensitivity(WidgetNames.ViewFrames, true);
-			SetViewVideoSubtitlesSensitivity();
-			/* Search Menu */
-			SetSensitivity(WidgetNames.SearchFind, true);
-			SetSensitivity(WidgetNames.SearchReplace, true);
-			/* Timings Menu */
-			SetSensitivity(WidgetNames.TimingsSynchronize, true);
-			/* Tools Menu */
-			SetSensitivity(WidgetNames.ToolsSetTextLanguage, true);
-			SetSensitivity(WidgetNames.ToolsSetTranslationLanguage, false);
-			
-			/* Toolbar */
-			SetSensitivity(WidgetNames.SaveButton, true);
-			SetSensitivity(WidgetNames.InsertSubtitleButton, true);
-			SetSensitivity(WidgetNames.DeleteSubtitlesButton, true);
-			/* Common for Format Menu and Toolbar*/
-			SetStylesSensitivity(true);
-		}
-		else {
-			/* File Menu */
-			SetSensitivity(WidgetNames.FileTranslationSave, false);
-			SetSensitivity(WidgetNames.FileTranslationSaveAs, false);
-			SetSensitivity(WidgetNames.FileTranslationClose, false);
-			/* Edit Menu */
+		SetSensitivity(WidgetNames.SaveButton, documentLoaded);
+		SetSensitivity(WidgetNames.InsertSubtitleButton, documentLoaded);
+		
+		/* Set sensitivity that only applies to when the document is not loaded */
+		
+		if (!documentLoaded) {
+			/* Edit menu */
+			SetSensitivity(WidgetNames.EditDeleteSubtitles, false);
 			SetSensitivity(WidgetNames.EditUndo, false);
 			SetSensitivity(WidgetNames.EditRedo, false);
 			SetSensitivity(WidgetNames.EditCut, false);
 			SetSensitivity(WidgetNames.EditCopy, false);
 			SetSensitivity(WidgetNames.EditPaste, false);
-			/* View Menu */
-			SetViewVideoSubtitlesSensitivity();
-			/* Search Menu */
+			/* Search menu */
 			SetSensitivity(WidgetNames.SearchFindNext, false);
 			SetSensitivity(WidgetNames.SearchFindPrevious, false);
-			/* Tools Menu */
-			SetSensitivity(WidgetNames.ToolsSetTranslationLanguage, false);
-			
+			/* Timings Menu */
+			SetSensitivity(WidgetNames.TimingsShift, false);
 			/* Toolbar */
+			SetSensitivity(WidgetNames.DeleteSubtitlesButton, false);
 			SetSensitivity(WidgetNames.UndoButton, false);
 			SetSensitivity(WidgetNames.RedoButton, false);
-			SetSensitivity(WidgetNames.DeleteSubtitlesButton, false);
 			SetSensitivity(WidgetNames.CutButton, false);
 			SetSensitivity(WidgetNames.CopyButton, false);
 			SetSensitivity(WidgetNames.PasteButton, false);
-		}	
+			/* Common for Format Menu and Toolbar */
+			SetStylesSensitivity(false);
+		}
 	}
 	
 	private void SetTranslationSensitivity (bool sensitivity) {
@@ -346,6 +211,10 @@
 		SetSensitivity(WidgetNames.ToolsSetTranslationLanguage, sensitivity);
 		SetViewVideoSubtitlesSensitivity();
 	}
+		
+	private void SetToolsAutocheckSpellingSensitivity (bool sensitivity) {
+		SetSensitivity(WidgetNames.ToolsAutocheckSpelling, sensitivity);
+	}
 	
 	private void SetFrameRateMenus () {
 		if (Base.TimingMode == TimingMode.Frames) {
@@ -370,6 +239,12 @@
 		SetToggleToolButtonActivity(WidgetNames.UnderlineButton, underline, Base.Handlers.OnEditFormatUnderline);
 	}
 	
+	private void SetActiveTimingMode (TimingMode mode) {
+		if (mode == TimingMode.Times)
+			SetCheckMenuItemActivity(WidgetNames.ViewTimes, true);
+		else
+			SetCheckMenuItemActivity(WidgetNames.ViewFrames, true);
+	}	
 		
 	private void SetViewVideoSubtitlesActivity (bool isTextActive) {
 		if (isTextActive)
@@ -420,7 +295,7 @@
 	}
 	
 	private void SetViewVideoSubtitlesSensitivity () {
-		bool isVideoLoaded = Core.Base.Ui.Video.IsLoaded;
+		bool isVideoLoaded = (Base.Ui != null) && Base.Ui.Video.IsLoaded;
 		bool textSensitivity = isVideoLoaded && Base.IsDocumentLoaded;
 		bool translationSensitivity = isVideoLoaded && textSensitivity && Base.Document.IsTranslationLoaded;
 		SetViewVideoSubtitlesSensitivity(textSensitivity, translationSensitivity);	
@@ -566,6 +441,113 @@
     	SetTooltip(widget, null);
     }
 
+	/* Event members */
+	
+	private void OnBaseInitFinished () {
+		Base.DocumentLoaded += OnBaseDocumentLoaded;
+		Base.DocumentUnloaded += OnBaseDocumentUnloaded;
+		Base.VideoLoaded += OnBaseVideoLoaded;
+		Base.VideoUnloaded += OnBaseVideoUnloaded;
+		Base.TranslationLoaded += OnBaseTranslationLoaded;
+		Base.TranslationUnloaded += OnBaseTranslationUnloaded;
+		Base.Ui.View.Selection.Changed += OnSubtitleViewSelectionChanged;
+		Base.Ui.View.SubtitleCountChanged += OnSubtitleViewCountChanged;
+		Base.SpellLanguages.TextLanguageChanged += OnSpellLanguagesLanguageChanged;
+		Base.SpellLanguages.TranslationLanguageChanged += OnSpellLanguagesLanguageChanged;
+		Base.CommandManager.UndoToggled += OnCommandManagerUndoToggled;
+		Base.CommandManager.RedoToggled += OnCommandManagerRedoToggled;
+		Base.CommandManager.CommandActivated += OnCommandManagerCommandActivated;
+	}
+	
+	private void OnBaseDocumentLoaded (Document document) {
+		SetDocumentSensitivity(true);
+		SetFrameRateMenus();
+		SetActiveTimingMode(Base.TimingMode);
+		SetCheckMenuItemActivity(WidgetNames.ToolsAutocheckSpelling, Base.SpellLanguages.Enabled);
+	}
+	
+	private void OnBaseDocumentUnloaded (Document document) {
+		SetDocumentSensitivity(false);
+	}
+	
+	private void OnBaseVideoLoaded (Uri videoUri) {
+		SetVideoSensitivity(true);
+    	SetViewVideoSubtitlesSensitivity();
+	}
+
+	private void OnBaseVideoUnloaded () {
+		SetVideoSensitivity(false);
+    	SetViewVideoSubtitlesSensitivity(false, false);
+	}
+
+	private void OnSubtitleViewCountChanged (int count) {
+		SetSubtitleCountDependentSensitivity(count);
+	}
+	
+	private void OnBaseTranslationLoaded () {
+		SetTranslationSensitivity(true);
+		UpdateUndoRedoMessages();
+	}
+	
+	private void OnBaseTranslationUnloaded () {
+		SetTranslationSensitivity(false);
+    	SetViewVideoSubtitlesActivity(true);
+    	UpdateUndoRedoMessages();
+	}
+	
+	private void OnSubtitleViewSelectionChanged (TreePath[] paths, Subtitle subtitle) {
+		if (subtitle != null) {
+			/* One subtitle selected */
+			SetStylesActivity(subtitle.Style.Bold, subtitle.Style.Italic, subtitle.Style.Underline);
+			SetNonZeroSelectionDependentSensitivity(true);
+			SetOneSelectionDependentSensitivity(true);
+		}
+		else {
+			SetOneSelectionDependentSensitivity(false);
+	
+			if (paths.Length == 0) {
+				/* No selection */
+				SetNonZeroSelectionDependentSensitivity(false);
+				SetStylesActivity(false, false, false);
+			}
+			else {
+				/* Multiple paths selected */
+				SetNonZeroSelectionDependentSensitivity(true);
+				bool bold, italic, underline;
+				GetGlobalStyles(paths, out bold, out italic, out underline);
+				SetStylesActivity(bold, italic, underline);	
+			}
+		}
+	}
+	
+	private void OnSpellLanguagesLanguageChanged () {
+		SetToolsAutocheckSpellingSensitivity(true);
+	}
+		
+	private void OnCommandManagerUndoToggled (object o, EventArgs args)  {
+   		Widget button = Base.GetWidget(WidgetNames.UndoButton);
+   		button.Sensitive = !button.Sensitive;
+    		
+		MenuItem menuItem = Base.GetWidget(WidgetNames.EditUndo) as MenuItem;
+		menuItem.Sensitive = !menuItem.Sensitive;
+		if (!menuItem.Sensitive)
+			(menuItem.Child as Label).Text = Catalog.GetString("Undo");
+	}
+    
+	private void OnCommandManagerRedoToggled (object o, EventArgs args) {
+    	Widget button = Base.GetWidget(WidgetNames.RedoButton);
+    	button.Sensitive = !button.Sensitive;
+    		
+		MenuItem menuItem = Base.GetWidget(WidgetNames.EditRedo) as MenuItem;
+    	menuItem.Sensitive = !menuItem.Sensitive;
+    	if (!menuItem.Sensitive)
+			(menuItem.Child as Label).Text = Catalog.GetString("Redo");
+    }
+    
+    private void OnCommandManagerCommandActivated (object o, CommandActivatedArgs args) {
+    	UpdateUndoRedoMessages();
+    }
+
 }
 
 }

Modified: trunk/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/VideoPreview/Player.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/VideoPreview/Player.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2008 Pedro Castro
+ * Copyright (C) 2007-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
@@ -84,6 +84,10 @@
 	public float FrameRate {
 		get { return playbin.VideoInfo.FrameRate; }
 	}
+	
+	public Uri VideoUri {
+		get { return videoUri; }
+	}
 
 
 	/* Public methods */

Modified: trunk/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/VideoPreview/SubtitleOverlay.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2008 Pedro Castro
+ * Copyright (C) 2007-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
@@ -35,15 +35,16 @@
 	private TimeSpan subtitleEnd = TimeSpan.Zero;
 	private bool toShowText = true;
 	
-	public SubtitleOverlay (VideoPosition position) {
+	public SubtitleOverlay () {
 		EventBox box = Base.GetWidget(WidgetNames.VideoSubtitleLabelEventBox) as EventBox;
 		box.ModifyBg(StateType.Normal, box.Style.Black);
 
 		label = Base.GetWidget(WidgetNames.VideoSubtitleLabel) as Label;
 		label.ModifyFg(StateType.Normal, new Gdk.Color(255, 255, 0));
 
-		position.Changed += OnVideoPositionChanged;
+		Base.InitFinished += OnBaseInitFinished;
 	}
+
 	
 	/* Public properties */
 	
@@ -57,25 +58,7 @@
 	public void Close () {
 		UnloadSubtitle();
 	}
-	
-	public void UpdateFromNewDocument (bool wasLoaded) {
-    	searchOp = new SearchOperator(Base.Document.Subtitles);
-    }
 
-	/* Event members */
-	
-	private void OnVideoPositionChanged (TimeSpan newPosition) {
-		if (!(Base.IsDocumentLoaded))
-			return;
-	
-		if (!(IsTimeInCurrentSubtitle(newPosition))) {
-			int foundSubtitle = searchOp.FindWithTime((float)newPosition.TotalSeconds); //TODO write method in SubLib that accepts TimeSpans
-			if (foundSubtitle == -1)
-				UnloadSubtitle();
-			else
-				LoadSubtitle(foundSubtitle);
-		}
-	}
 	
 	/* Private properties */
 	
@@ -131,6 +114,32 @@
 	private void ClearText () {
 		label.Text = String.Empty;
 	}
+
+	
+	/* Event members */
+	
+	private void OnBaseInitFinished () {
+		Base.Ui.Video.Position.Changed += OnVideoPositionChanged;
+		
+		Base.DocumentLoaded += OnBaseDocumentLoaded;
+	}
+	
+	private void OnBaseDocumentLoaded (Document document) {
+		searchOp = new SearchOperator(document.Subtitles);
+	}
+	
+	private void OnVideoPositionChanged (TimeSpan newPosition) {
+		if (!(Base.IsDocumentLoaded))
+			return;
+	
+		if (!(IsTimeInCurrentSubtitle(newPosition))) {
+			int foundSubtitle = searchOp.FindWithTime((float)newPosition.TotalSeconds); //TODO write method in SubLib that accepts TimeSpans
+			if (foundSubtitle == -1)
+				UnloadSubtitle();
+			else
+				LoadSubtitle(foundSubtitle);
+		}
+	}
 	
 }
 

Modified: trunk/src/GnomeSubtitles/Ui/VideoPreview/Video.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/VideoPreview/Video.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/VideoPreview/Video.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -35,7 +35,7 @@
 	
 	private Player player = null;
 	private VideoPosition position = null;
-	private SubtitleOverlay subtitle = null;
+	private SubtitleOverlay overlay = null;
 	
 	private bool isLoaded = false;
 	private bool playPauseToggleIsSilent = false; //Used to indicate whether toggling the button should not issue the toggled signal
@@ -51,10 +51,10 @@
 		InitializePlayer();
 		
 		position = new VideoPosition(player);
-		subtitle = new SubtitleOverlay(position);
+		overlay = new SubtitleOverlay();
 
 		SetCustomIcons();
-		ConnectPlayPauseButtonSignals();
+		Base.InitFinished += OnBaseInitFinished;
 	}
 	
 	/* Public properties */
@@ -63,8 +63,8 @@
 		get { return position; }
 	}
 	
-	public SubtitleOverlay Subtitle {
-		get { return subtitle; }
+	public SubtitleOverlay Overlay {
+		get { return overlay; }
 	}
 	
 	public bool IsLoaded {
@@ -100,7 +100,7 @@
 		isLoaded = false;
 
 		player.Close();
-		subtitle.Close();
+		overlay.Close();
 		position.Disable();
 		
 		/* Update the frame */
@@ -113,23 +113,6 @@
 
 		Core.Base.Ui.Menus.RemoveFrameRateVideoTag();
 	}
-
-	public void UpdateFromTimingMode (TimingMode newMode) {
-		position.ToggleTimingMode(newMode);
-	}
-	
-	public void UpdateFromNewDocument (bool wasLoaded) {
-    	subtitle.UpdateFromNewDocument(wasLoaded);
-    }
-
-	/// <summary>Updates the controls for a subtitle selection change.</summary>
-	/// <param name="isSingle">Whether there is only 1 selected subtitle.</param>
-	public void UpdateFromSelection (bool isSingle) {
-		if (isSingle && IsLoaded)
-			SetSelectionDependentControlsSensitivity(true);
-		else
-			SetSelectionDependentControlsSensitivity(false);
-	}
 	
 	public void Quit () {
 		player.Close();
@@ -161,7 +144,7 @@
 	}
 	
 	public void SeekToSelection () { //TODO check out
-		Subtitle subtitle = Core.Base.Ui.View.Selection.Subtitle;
+		Subtitle subtitle = Core.Base.Ui.View.Selection.FirstSubtitle;
     	TimeSpan time = subtitle.Times.Start;
     	Seek(time);
 	}
@@ -238,9 +221,11 @@
 	
 	/* Event members */
 	
-	private void ConnectPlayPauseButtonSignals () {
+	private void OnBaseInitFinished () {
 		ToggleButton button = Base.GetWidget(WidgetNames.VideoPlayPauseButton) as ToggleButton;
 		button.Toggled += OnPlayPauseButtonToggled;
+		
+		Base.Ui.View.Selection.Changed += OnSubtitleSelectionChanged;
 	}
 	
 	private void OnPlayPauseButtonToggled (object o, EventArgs args) {
@@ -263,7 +248,8 @@
 		if (args.State == MediaStatus.Loaded) {
 			SetControlsSensitivity(true);
 			isLoaded = true;
-			Base.Ui.UpdateFromOpenVideo();
+			
+			Base.UpdateFromVideoLoaded(player.VideoUri);
 		}
 	}
 	
@@ -280,6 +266,13 @@
 			Base.Ui.OpenVideo();
 	}
 
+	private void OnSubtitleSelectionChanged (TreePath[] paths, Subtitle subtitle) {
+		if ((subtitle != null) && IsLoaded)
+			SetSelectionDependentControlsSensitivity(true);
+		else
+			SetSelectionDependentControlsSensitivity(false);
+	}
+
 }
 
 }

Modified: trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoFiles.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoFiles.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoFiles.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2008 Pedro Castro
+ * Copyright (C) 2007-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
@@ -43,17 +43,17 @@
 		return videoFiles;
 	}
 	
-	public static string FindMatchingVideo (string file) {
+	public static Uri FindMatchingVideo (string file) {
 		ArrayList videoFiles = GetVideoFilesAtPath(Path.GetDirectoryName(file));
 		string filename = Path.GetFileNameWithoutExtension(file);
 		
 		foreach (string videoFile in videoFiles) {
 			string video = Path.GetFileNameWithoutExtension(videoFile);
 			if (video == filename)
-				return videoFile;
+				return new Uri(videoFile);
 		}
 
-		return String.Empty;
+		return null;
 	}
 
 

Modified: trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoPosition.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoPosition.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/VideoPreview/VideoPosition.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -44,7 +44,7 @@
 	private const int userUpdateTimeout = 100; //Milliseconds
 	private TimeSpan seekIncrement = TimeSpan.FromMilliseconds(500);
 
-	/* Handlers */
+	/* Delegates */
 	public delegate void VideoPositionChangedHandler (TimeSpan position);
 
 	/* Events */
@@ -57,10 +57,10 @@
 		lengthValueLabel = Base.GetWidget(WidgetNames.VideoLengthValueLabel) as Label;
 
 		this.player = player;
-		player.PositionChanged += OnPlayerPositionChanged;
-		player.FoundDuration += OnPlayerFoundDuration;
+		Base.InitFinished += OnBaseInitFinished;
 	}
 
+
 	/* Public properties */
 
 	public TimeSpan SeekIncrement {
@@ -96,13 +96,7 @@
 		
 		slider.Sensitive = false;	
 	}
-	
-	public void ToggleTimingMode (TimingMode newMode) {
-		UpdatePositionLabel(newMode);
-		UpdatePositionValueLabel(position);
-		TimeSpan length = player.Duration;
-		UpdateLengthLabel(newMode, length);
-	}
+
 
 	/* Event members */
 	
@@ -161,6 +155,20 @@
 	private void DisconnectSliderSignals () {
 		slider.ValueChanged -= OnSliderValueChanged;
 	}
+	
+	private void OnBaseInitFinished () {
+		player.PositionChanged += OnPlayerPositionChanged;
+		player.FoundDuration += OnPlayerFoundDuration;
+
+		Base.TimingModeChanged += OnBaseTimingModeChanged;
+	}
+	
+	private void OnBaseTimingModeChanged (TimingMode timingMode) {
+		UpdatePositionLabel(timingMode);
+		UpdatePositionValueLabel(position);
+		TimeSpan length = player.Duration;
+		UpdateLengthLabel(timingMode, length);
+	}
 
 	/* Private members */
 	

Modified: trunk/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/View/SubtitleSelection.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -24,18 +24,28 @@
 
 namespace GnomeSubtitles.Ui.View {
 
+/* Delegates */
+public delegate void SubtitleSelectionChangedHandler (TreePath[] paths, Subtitle subtitle);
+
 public class SubtitleSelection {
 
 	private TreeView tree = null;
 	private TreeSelection selection = null;
+	//TODO use this subtitle, and don't store it in individual widgets
+	private Subtitle subtitle = null; //The selected subtitle, if only 1 row is selected
 
 	public SubtitleSelection (TreeView tree) {
 		this.tree = tree;
 		this.selection = tree.Selection;
 		
 		selection.Mode = SelectionMode.Multiple;
-		ConnectSelectionChanged();
+		
+		Base.InitFinished += OnBaseInitFinished;
 	}
+	
+	/* Events */
+	
+	public SubtitleSelectionChangedHandler Changed;
 
 	/* Public properties */
     
@@ -121,7 +131,17 @@
     
     /// <summary>The selected subtitle. If there is more than one selected, the first is returned.</summary>
     public Subtitle Subtitle {
-    	get { return Base.Document.Subtitles[Path]; }
+    	get { return subtitle; }
+    }
+    
+    public Subtitle FirstSubtitle {
+    	get {
+    		TreePath path = this.FirstPath;
+    		if (path != null)
+    			return Base.Document.Subtitles[path];
+    		else
+    			return null;
+    	}
     }
 
     /// <summary>The number of selected paths.</summary>
@@ -264,6 +284,11 @@
 	public void SelectAll () {
 		selection.SelectAll();
 	}
+	
+	//TODO Check why this is needed
+    public void Reselect () {
+    	EmitChangedEvent();
+    }
 
 	/// <summary>Scrolls to the specified path and optionally aligns it to the center of the <see cref="TreeView" />.</summary>
 	/// <remarks>This should only be used to scroll to the input focus.</remarks>
@@ -272,24 +297,6 @@
 	public void ScrollToFocus (TreePath focus, bool align) {
 		Scroll(focus, align);	
 	}
-
-    /* Event members */
-	
-	private void DisconnectSelectionChanged () {
-		selection.Changed -= OnSelectionChanged;
-	}
-	
-	private void ConnectSelectionChanged () {
-		selection.Changed += OnSelectionChanged;
-	}
-	
-	private void EmitSelectionChanged () {
-		OnSelectionChanged(tree.Selection, EventArgs.Empty);
-	}
-	
-	private void OnSelectionChanged (object o, EventArgs args) {
-		Base.Ui.UpdateFromSelection();
-	}
 	
 	/* Private members */
 
@@ -300,7 +307,7 @@
 	/// <param name="focus">The path to give input focus to. It must be one of the specified paths or null, in which case no focus will be given.</param>
 	/// <param name="align">Whether to align the selected path to the center if the path isn't visible and scrolling is needed.</param>
 	private void Select (TreePath[] paths, SelectionType selectionType, TreePath focus, bool align) {
-		DisconnectSelectionChanged();
+		DisconnectSelectionChangedSignal();
 		
 		if (focus != null)
 			SetFocus(focus, align);
@@ -315,8 +322,8 @@
 			selection.SelectRange(paths[0], paths[1]);
 		}
 		
-		ConnectSelectionChanged();		
-		EmitSelectionChanged();
+		ConnectSelectionChangedSignal();		
+		OnSelectionChanged(this, EventArgs.Empty); //Need to simulate this event because the signal was disabled during selection change
    	}
 	   	
    	/// <summary>Sets the input to the specified path and selects it, possibly aligning it to the center.</summary>
@@ -359,6 +366,35 @@
 			ScrollToCell(path, align);
     }
     
+	/* Event members */
+	
+	private void OnBaseInitFinished () {
+		ConnectSelectionChangedSignal();
+		Base.DocumentLoaded += OnBaseDocumentLoaded;
+	}
+	
+	private void OnBaseDocumentLoaded (Document document) {
+		SelectFirst();
+	}
+	
+	private void ConnectSelectionChangedSignal () {
+		selection.Changed += OnSelectionChanged;
+	}
+	
+	private void DisconnectSelectionChangedSignal () {
+		selection.Changed -= OnSelectionChanged;
+	}
+	
+	private void OnSelectionChanged (object o, EventArgs args) {
+		subtitle = (this.Count == 1 ? Base.Document.Subtitles[Path] : null);
+		EmitChangedEvent();
+	}
+	
+	private void EmitChangedEvent () {
+		if (Changed != null)
+			Changed(Paths, subtitle);
+	}
+    
 }
 
 }

Modified: trunk/src/GnomeSubtitles/Ui/View/SubtitleView.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/View/SubtitleView.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/View/SubtitleView.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -26,6 +26,9 @@
 
 namespace GnomeSubtitles.Ui.View {
 
+/* Delegates */
+public delegate void SubtitleCountChangedHandler (int count);
+
 public class SubtitleView {
 	private Subtitles subtitles = null; //A reference to Global.Subtitles, kept here because it's often accessed by CellRenderers.
 
@@ -47,7 +50,14 @@
 		search = new Search();
 
 		CreateColumns();
+		SetEmptyModel();
+		
+		Base.InitFinished += OnBaseInitFinished;
     }
+    
+	/* Events */
+	
+	public event SubtitleCountChangedHandler SubtitleCountChanged;
 
 	/* Public properties */
 
@@ -60,35 +70,6 @@
     }
     
     /* Public methods */ 
-    
-    /// <summary>Used in a blank startup. A blank startup refers to when no document is loaded.</summary>
-    public void BlankStartUp () {
-    	tree.Model = new ListStore(typeof(Subtitle));
-    }
-    
-	public void UpdateFromNewDocument (bool wasLoaded) {
-    	if (!wasLoaded)
-    		tree.Sensitive = true;
-    	else {
-    		search.Clear();
-    		SetTranslationVisible(false);
-    	}
-
-    	Load(Base.Document.Subtitles);
-    }
-    
-    public void UpdateFromNewTranslationDocument () {
-    	SetTranslationVisible(true);
-    	Refresh();
-    }
-    
-    public void UpdateFromCloseTranslation () {
-    	SetTranslationVisible(false);
-    }
-    
-	public void UpdateFromTimingMode (TimingMode mode) {
-		Refresh();
-	}
 	
 	/// <summary>Instructs the <see cref="TreeView" /> to redraw a row.</summary>
 	/// <remarks>This is useful when a row changes its width, for instance.</remarks>
@@ -219,6 +200,9 @@
    		this.subtitles = subtitles;
    		tree.Model = subtitles.Model;
     	Refresh();
+    	
+    	tree.Model.RowInserted += OnModelRowInserted;
+    	tree.Model.RowDeleted += OnModelRowDeleted;
     }
 	
     private void CreateColumns() {
@@ -331,6 +315,65 @@
 		else
 			renderer.Underline = Pango.Underline.None;
 	}
+	
+	/* Event members */
+	
+	private void OnBaseInitFinished () {
+		Base.DocumentLoaded += OnBaseDocumentLoaded;
+		Base.DocumentUnloaded += OnBaseDocumentUnloaded;
+		Base.TranslationLoaded += OnBaseTranslationLoaded;
+		Base.TranslationUnloaded += OnBaseTranslationUnloaded;
+		Base.TimingModeChanged += OnBaseTimingModeChanged;
+	}
+	
+	private void OnBaseDocumentLoaded (Document document) {
+   		tree.Sensitive = true;
+    	Load(document.Subtitles);
+    }
+    
+    private void OnBaseDocumentUnloaded (Document document) {
+    	if (document == null)
+    		return;
+    	
+    	tree.Sensitive = false;
+    	search.Clear();
+   		SetTranslationVisible(false);
+   		SetEmptyModel();
+    	
+    	tree.Model.RowInserted -= OnModelRowInserted;
+		tree.Model.RowDeleted -= OnModelRowDeleted;
+    }
+    
+	private void OnModelRowInserted (object o, RowInsertedArgs args) {
+		EmitSubtitleCountChangedEvent();
+	}
+	
+	private void OnModelRowDeleted (object o, RowDeletedArgs args) {
+		EmitSubtitleCountChangedEvent();
+	}
+
+        
+    private void OnBaseTranslationLoaded () {
+    	SetTranslationVisible(true);
+    	Refresh();
+    }
+    
+    private void OnBaseTranslationUnloaded () {
+    	SetTranslationVisible(false);
+    }
+    
+    private void OnBaseTimingModeChanged (TimingMode timingMode) {
+    	Refresh();
+    }
+	
+	private void EmitSubtitleCountChangedEvent () {
+		if (SubtitleCountChanged != null)
+			SubtitleCountChanged(subtitles.Count);
+	}
+
+    private void SetEmptyModel () {
+    	tree.Model = new ListStore(typeof(Subtitle));
+    }
 		
 }
 

Modified: trunk/src/GnomeSubtitles/Ui/View/Subtitles.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/View/Subtitles.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/View/Subtitles.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -28,12 +28,10 @@
 	private ListStore model = new ListStore(typeof(Subtitle));
 	
 	
-	public Subtitles (SubLib.Core.Domain.Subtitles subtitles)
-			: base(subtitles.Collection, subtitles.Properties) {
-
+	public Subtitles (SubLib.Core.Domain.Subtitles subtitles) : base(subtitles.Collection, subtitles.Properties) {
 		LoadModelFromCollection();
-		ConnectHandlers();
 	}
+
 	
 	/* Indexers */
 	
@@ -154,22 +152,6 @@
 		for (int index = startIndex ; index < startIndex + count ; index++)
 			AddNewAfter(index);
 	}
-	
-	
-	/* Event members */
-	
-	private void ConnectHandlers () {
-		model.RowInserted += OnRowInserted;
-		model.RowDeleted += OnRowDeleted;
-	}
-
-	private void OnRowInserted (object o, RowInsertedArgs args) {
-		Core.Base.Ui.UpdateFromSubtitleCount();
-	}
-	
-	private void OnRowDeleted (object o, RowDeletedArgs args) {
-		Core.Base.Ui.UpdateFromSubtitleCount();
-	}
 
 
 	/* Private members */

Modified: trunk/src/GnomeSubtitles/Ui/WidgetNames.cs
==============================================================================
--- trunk/src/GnomeSubtitles/Ui/WidgetNames.cs	(original)
+++ trunk/src/GnomeSubtitles/Ui/WidgetNames.cs	Sun Mar  8 20:39:18 2009
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2008 Pedro Castro
+ * Copyright (C) 2006-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
@@ -37,6 +37,7 @@
 	public const string FileTranslationSave = "fileTranslationSave";
 	public const string FileTranslationSaveAs = "fileTranslationSaveAs";
 	public const string FileTranslationClose = "fileTranslationClose";
+	public const string FileClose = "fileClose";
 	
 	/* Edit Menu */
 	public const string EditUndo = "editUndo";



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