Description: Enable editing in View mode
Author: Christopher Halse Rogers <christopher.halse.rogers@canonical.com>
Author: Ken VanDine <ken.vandine@canonical.com>
Bug-Ubuntu: https://bugs.edge.launchpad.net/ubuntu/+source/f-spot/+bug/484888
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=513561	

=== modified file 'src/Editors/Editor.cs'
--- a/src/Editors/Editor.cs	2009-08-10 17:42:29 +0000
+++ b/src/Editors/Editor.cs	2010-03-03 07:34:36 +0000
@@ -18,6 +18,7 @@
 using Mono.Unix;
 
 using System;
+using System.IO;
 
 namespace FSpot.Editors {
 	[ExtensionNode ("Editor")]
@@ -61,6 +62,9 @@
 		public event ProcessingStepHandler ProcessingStep;
 		public event ProcessingFinishedHandler ProcessingFinished;
 
+		public PhotoImageView View { get; set; }
+		public Gtk.Window ParentWindow {get; set; }
+
 		// Contains the current selection, the items being edited, ...
 		private EditorState state;
 		public EditorState State {
@@ -96,7 +100,7 @@
 		}
 
 
-		protected void LoadPhoto (Photo photo, out Pixbuf photo_pixbuf, out Cms.Profile photo_profile) {
+		protected void LoadPhoto (IBrowsableItem photo, out Pixbuf photo_pixbuf, out Cms.Profile photo_profile) {
 			// FIXME: We might get this value from the PhotoImageView.
 			using (ImageFile img = ImageFile.Create (photo.DefaultVersionUri)) {
 				photo_pixbuf = img.Load ();
@@ -143,18 +147,29 @@
 			}
 
 			int done = 0;
-			foreach (Photo photo in State.Items) {
+			foreach (IBrowsableItem item in State.Items) {
 				Pixbuf input;
 				Cms.Profile input_profile;
-				LoadPhoto (photo, out input, out input_profile);
+				LoadPhoto (item, out input, out input_profile);
 
 				Pixbuf edited = Process (input, input_profile);
 				input.Dispose ();
 
-				bool create_version = photo.DefaultVersion.IsProtected;
-				photo.SaveVersion (edited, create_version);
-				photo.Changes.DataChanged = true;
-				Core.Database.Photos.Commit (photo);
+				if (item is Photo) {
+					var photo = item as Photo;
+					bool create_version = photo.DefaultVersion.IsProtected;
+					photo.SaveVersion (edited, create_version);
+					photo.Changes.DataChanged = true;
+					Core.Database.Photos.Commit (photo);
+				} else {
+					var pb = edited.Copy ();
+					using (ImageFile img = ImageFile.Create (item.DefaultVersionUri)) {
+						using (Stream stream = System.IO.File.OpenWrite (item.DefaultVersionUri.LocalPath)) {
+							img.Save (edited, stream);
+						}
+					}
+					State.PhotoImageView.Pixbuf = pb; 
+				}
 
 				done++;
 				if (ProcessingStep != null) {
@@ -205,7 +220,11 @@
 			Pixbuf previewed = ProcessFast (preview, null);
 			State.PhotoImageView.Pixbuf = previewed;
 			State.PhotoImageView.ZoomFit (false);
-			MainWindow.Toplevel.InfoBox.UpdateHistogram (previewed);
+			if (MainWindow.Toplevel != null) {
+				//MainWindow.Toplevel is null if we're in View mode.
+				//If we're in View mode we don't have a histogram, so we don't need to update it.
+				MainWindow.Toplevel.InfoBox.UpdateHistogram (previewed);
+			}
 
 			if (old_preview != null) {
 				old_preview.Dispose ();
@@ -238,7 +257,11 @@
 				State.PhotoImageView.Pixbuf = original;
 				State.PhotoImageView.ZoomFit (false);
 
-				MainWindow.Toplevel.InfoBox.UpdateHistogram (null);
+				if (MainWindow.Toplevel != null) {
+					//MainWindow.Toplevel is null if we're in View mode.
+					//If we're in View mode we don't have a histogram, so we dont' need to update it.
+					MainWindow.Toplevel.InfoBox.UpdateHistogram (null);
+				}
 			}
 
 			Reset ();

=== modified file 'src/FSpot.addin.xml'
--- a/src/FSpot.addin.xml	2009-11-09 18:12:08 +0000
+++ b/src/FSpot.addin.xml	2010-03-03 03:14:16 +0000
@@ -61,8 +61,8 @@
 
 	<Extension path = "/FSpot/Sidebar">
 		<SidebarPage sidebar_page_type = "FSpot.Widgets.MetadataDisplayPage" />
+		<SidebarPage sidebar_page_type = "FSpot.Widgets.EditorPage" />
 		<Condition id="ViewMode" mode="library">
-			<SidebarPage sidebar_page_type = "FSpot.Widgets.EditorPage" />
 			<SidebarPage sidebar_page_type = "FSpot.Widgets.FolderTreePage" />
 		</Condition>
 	</Extension>

=== modified file 'src/MainWindow.cs'
--- a/src/MainWindow.cs	2009-11-05 17:17:47 +0000
+++ b/src/MainWindow.cs	2010-03-03 23:03:13 +0000
@@ -341,8 +341,6 @@
 
 		Sidebar.AppendPage (tag_selection_scrolled, Catalog.GetString ("Tags"), "tag");
 
-		AddinManager.AddExtensionNodeHandler ("/FSpot/Sidebar", OnSidebarExtensionChanged);
-
 		Sidebar.Context = ViewContext.Library;
  		
 		Sidebar.CloseRequested += HideSidebar;
@@ -453,6 +451,9 @@
 
 		photo_view.View.ZoomChanged += HandleZoomChanged;
 
+		// Sidebar extensions need access to PhotoView, so this has to be delayed until after we've constructed photo_view
+		AddinManager.AddExtensionNodeHandler ("/FSpot/Sidebar", OnSidebarExtensionChanged);
+
 		// Tag typing: focus the tag entry if the user starts typing a tag
 		icon_view.KeyPressEvent += HandlePossibleTagTyping;
 		photo_view.KeyPressEvent += HandlePossibleTagTyping;
@@ -531,8 +532,12 @@
 
 	private void OnSidebarExtensionChanged (object s, ExtensionNodeEventArgs args) {
 		// FIXME: No sidebar page removal yet!
-		if (args.Change == ExtensionChange.Add)
-			Sidebar.AppendPage ((args.ExtensionNode as SidebarPageNode).GetSidebarPage ());
+		if (args.Change == ExtensionChange.Add) {
+			var page = ((SidebarPageNode)args.ExtensionNode).GetSidebarPage ();
+			page.PhotoImageView = PhotoView.View;
+			page.ParentWindow = Window;
+			Sidebar.AppendPage (page);
+		}
 	}
 
 	private Photo CurrentPhoto {

=== modified file 'src/SingleView.cs'
--- a/src/SingleView.cs	2009-09-30 14:45:16 +0000
+++ b/src/SingleView.cs	2010-03-03 03:14:16 +0000
@@ -127,9 +127,6 @@
 			info_vbox.Add (sidebar);
 			sidebar.AppendPage (directory_scrolled, Catalog.GetString ("Folder"), "gtk-directory");
 
-			AddinManager.AddExtensionNodeHandler ("/FSpot/Sidebar", OnSidebarExtensionChanged);
- 		
-			sidebar.Context = ViewContext.Single;
 
 			sidebar.CloseRequested += HandleHideSidePane;
 			sidebar.Show ();
@@ -149,6 +146,9 @@
 			
 			Window.ShowAll ();
 
+			AddinManager.AddExtensionNodeHandler ("/FSpot/Sidebar", OnSidebarExtensionChanged);	
+			sidebar.Context = ViewContext.Single;
+
 			zoom_scale.ValueChanged += HandleZoomScaleValueChanged;
 		
 			LoadPreference (Preferences.VIEWER_SHOW_TOOLBAR);
@@ -189,8 +189,13 @@
 
 		private void OnSidebarExtensionChanged (object s, ExtensionNodeEventArgs args) {
 			// FIXME: No sidebar page removal yet!
-			if (args.Change == ExtensionChange.Add)
-				sidebar.AppendPage ((args.ExtensionNode as SidebarPageNode).GetSidebarPage ());
+			if (args.Change == ExtensionChange.Add) {
+				var page = (args.ExtensionNode as SidebarPageNode).GetSidebarPage ();
+Log.Debug ("OnSidebarExtensionAdded {0}  / {1} / {2}", page, image_view, window);
+				page.PhotoImageView = image_view;
+				page.ParentWindow = window;
+				sidebar.AppendPage (page);
+			}
 		}
 
 		void HandleExportActivated (object o, EventArgs e)

=== modified file 'src/Widgets/EditorPage.cs'
--- a/src/Widgets/EditorPage.cs	2009-08-10 17:42:29 +0000
+++ b/src/Widgets/EditorPage.cs	2010-03-03 23:22:33 +0000
@@ -23,7 +23,7 @@
 namespace FSpot.Widgets {
 	public class EditorPage : SidebarPage {
 		internal bool InPhotoView;
-		private readonly EditorPageWidget EditorPageWidget;
+		readonly EditorPageWidget EditorPageWidget;
 
 		public EditorPage () : base (new EditorPageWidget (),
 									   Catalog.GetString ("Edit"),
@@ -34,6 +34,16 @@
 			EditorPageWidget.Page = this;
 		}
 
+		public override PhotoImageView PhotoImageView {
+			get { return EditorPageWidget.PhotoImageView; }
+			set { EditorPageWidget.PhotoImageView = value; }
+		}
+
+		public override Gtk.Window ParentWindow {
+			get { return EditorPageWidget.ParentWindow; }
+			set { EditorPageWidget.ParentWindow = value ;}
+		}
+
 		protected override void AddedToSidebar () {
 			Sidebar.SelectionChanged += delegate (IBrowsableCollection collection) { EditorPageWidget.ShowTools (); };
 			Sidebar.ContextChanged += HandleContextChanged;
@@ -41,18 +51,22 @@
 
 		private void HandleContextChanged (object sender, EventArgs args)
 		{
-			InPhotoView = (Sidebar.Context == ViewContext.Edit);
+Log.Debug ("ContextChanged {0}", Sidebar.Context);
+			InPhotoView = (Sidebar.Context == ViewContext.Edit) || (Sidebar.Context == ViewContext.Single);
 			EditorPageWidget.ChangeButtonVisibility ();
 		}
 	}
 
 	public class EditorPageWidget : ScrolledWindow {
-		private VBox widgets;
-		private VButtonBox buttons;
-		private Widget active_editor;
-
-		private List<Editor> editors;
-		private Editor current_editor;
+		VBox widgets;
+		VButtonBox buttons;
+		Widget active_editor;
+
+		List<Editor> editors;
+		Editor current_editor;
+
+		public PhotoImageView PhotoImageView { get; set;}
+		public Gtk.Window ParentWindow { get; set; }
 
 		// Used to make buttons insensitive when selecting multiple images.
 		private Dictionary<Editor, Button> editor_buttons;
@@ -78,6 +92,8 @@
 				editor.ProcessingStarted += OnProcessingStarted;
 				editor.ProcessingStep += OnProcessingStep;
 				editor.ProcessingFinished += OnProcessingFinished;
+				editor.View = PhotoImageView;
+				editor.ParentWindow = ParentWindow;
 				editors.Add (editor);
 				PackButton (editor);
 			}
@@ -86,7 +102,7 @@
 		private ProgressDialog progress;
 
 		private void OnProcessingStarted (string name, int count) {
-			progress = new ProgressDialog (name, ProgressDialog.CancelButtonType.None, count, MainWindow.Toplevel.Window);
+			progress = new ProgressDialog (name, ProgressDialog.CancelButtonType.None, count, ParentWindow);
 		}
 
 		private void OnProcessingStep (int done) {
@@ -171,8 +187,7 @@
 		private bool SetupEditor (Editor editor) {
 			EditorState state = editor.CreateState ();
 
-			PhotoImageView photo_view = MainWindow.Toplevel.PhotoView.View;
-
+			PhotoImageView photo_view = PhotoImageView;
 			if (Page.InPhotoView && photo_view != null) {
 				state.Selection = photo_view.Selection;
 				state.PhotoImageView = photo_view;
@@ -196,7 +211,7 @@
 				string msg = Catalog.GetString ("No selection available");
 				string desc = Catalog.GetString ("This tool requires an active selection. Please select a region of the photo and try the operation again");
 
-				HigMessageDialog md = new HigMessageDialog (MainWindow.Toplevel.Window,
+				HigMessageDialog md = new HigMessageDialog (ParentWindow,
 										DialogFlags.DestroyWithParent,
 										Gtk.MessageType.Error, ButtonsType.Ok,
 										msg,
@@ -217,7 +232,7 @@
 				string desc = String.Format (Catalog.GetString ("Received exception \"{0}\". Note that you have to develop RAW files into JPEG before you can edit them."),
 							     e.Message);
 
-				HigMessageDialog md = new HigMessageDialog (MainWindow.Toplevel.Window,
+				HigMessageDialog md = new HigMessageDialog (ParentWindow,
 									    DialogFlags.DestroyWithParent,
 									    Gtk.MessageType.Error, ButtonsType.Ok,
 									    msg,

=== modified file 'src/Widgets/Sidebar.cs'
--- a/src/Widgets/Sidebar.cs	2009-08-10 17:42:29 +0000
+++ b/src/Widgets/Sidebar.cs	2010-03-03 03:14:16 +0000
@@ -110,6 +110,9 @@
 		// Can be overriden to get notified as soon as we're added to a sidebar.
 		protected virtual void AddedToSidebar () { }
 
+		public virtual PhotoImageView PhotoImageView { get; set; }
+		public virtual Gtk.Window ParentWindow { get; set; }
+
 		// Whether this page is currently visible
 		public bool IsActive {
 			get { return Sidebar.IsActive (this); }

