private void ShowEditor(Editor editor)
        {
            SetupEditor (editor);
            current_editor = editor;

            buttons.Hide ();

            // Top label
            VBox vbox = new VBox (false, 4);
            Label label = new Label ();
            label.Markup = String.Format("<big><b>{0}</b></big>", editor.Label);
            vbox.PackStart (label, false, false, 5);

            // Optional config widget
            Widget config = editor.ConfigurationWidget ();
            if (config != null) {
                // This is necessary because GtkBuilder widgets need to be
                // reparented.
                if (config.Parent != null) {
                    config.Reparent (vbox);
                } else {
                    vbox.PackStart (config, false, false, 0);
                }
            }

            // Apply / Cancel buttons
            HButtonBox tool_buttons = new HButtonBox ();
            tool_buttons.LayoutStyle = ButtonBoxStyle.End;
            tool_buttons.Spacing = 5;
            tool_buttons.BorderWidth = 5;
            tool_buttons.Homogeneous = false;

            Button cancel = new Button (Stock.Cancel);
            cancel.Clicked += HandleCancel;
            tool_buttons.Add (cancel);

            Button apply = new Button (editor.ApplyLabel);
            apply.Image = new Image (GtkUtil.TryLoadIcon (FSpot.Core.Global.IconTheme, editor.IconName, 22, (Gtk.IconLookupFlags)0));
            apply.Clicked += delegate { Apply (editor); };
            tool_buttons.Add (apply);

            // Pack it all together
            vbox.PackEnd (tool_buttons, false, false, 0);
            active_editor = vbox;
            widgets.Add (active_editor);
            active_editor.ShowAll ();
        }
 void PackButton(Editor editor)
 {
     Button button = new Button (editor.Label);
     if (editor.IconName != null)
         button.Image = new Image (GtkUtil.TryLoadIcon (FSpot.Core.Global.IconTheme, editor.IconName, 22, (Gtk.IconLookupFlags)0));
     button.Clicked += delegate (object o, EventArgs e) { ChooseEditor (editor); };
     button.Show ();
     buttons.Add (button);
     editor_buttons.Add (editor, button);
 }
        private bool SetupEditor(Editor editor)
        {
            EditorState state = editor.CreateState ();

            PhotoImageView photo_view = App.Instance.Organizer.PhotoView.View;

            if (Page.InPhotoView && photo_view != null) {
                state.Selection = photo_view.Selection;
                state.PhotoImageView = photo_view;
            } else {
                state.Selection = Gdk.Rectangle.Zero;
                state.PhotoImageView = null;
            }
            if ((Page.Sidebar as Sidebar).Selection == null)
                return false;
            state.Items = (Page.Sidebar as Sidebar).Selection.Items;

            editor.Initialize (state);
            return true;
        }
        private void ChooseEditor(Editor editor)
        {
            SetupEditor (editor);

            if (!editor.CanBeApplied || editor.HasSettings)
                ShowEditor (editor);
            else
                Apply (editor); // Instant apply
        }
        private void Apply(Editor editor)
        {
            if (!SetupEditor (editor))
                return;

            if (!editor.CanBeApplied) {
                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 (App.Instance.Organizer.Window,
                                        DialogFlags.DestroyWithParent,
                                        Gtk.MessageType.Error, ButtonsType.Ok,
                                        msg,
                                        desc);

                md.Run ();
                md.Destroy ();
                return;
            }

            // TODO: Might need to do some nicer things for multiple selections (progress?)
            try {
                editor.Apply ();
            } catch (Exception e) {
                Log.DebugException (e);
                string msg = Catalog.GetPluralString ("Error saving adjusted photo", "Error saving adjusted photos",
                                    editor.State.Items.Length);
                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 (App.Instance.Organizer.Window,
                                        DialogFlags.DestroyWithParent,
                                        Gtk.MessageType.Error, ButtonsType.Ok,
                                        msg,
                                        desc);
                md.Run ();
                md.Destroy ();
            }
            ShowTools ();
        }
        public void ShowTools()
        {
            // Remove any open editor, if present.
            if (current_editor != null) {
                active_editor.Hide ();
                widgets.Remove (active_editor);
                active_editor = null;
                current_editor.Restore ();
                current_editor = null;
            }

            // No need to build the widget twice.
            if (buttons != null) {
                buttons.Show ();
                return;
            }

            if (widgets == null) {
                widgets = new VBox (false, 0);
                widgets.NoShowAll = true;
                widgets.Show ();
                Viewport widgets_port = new Viewport ();
                widgets_port.Add (widgets);
                Add (widgets_port);
                widgets_port.ShowAll ();
            }

            // Build the widget (first time we call this method).
            buttons = new VButtonBox ();
            buttons.BorderWidth = 5;
            buttons.Spacing = 5;
            buttons.LayoutStyle = ButtonBoxStyle.Start;

            foreach (Editor editor in editors)
                PackButton (editor);

            buttons.Show ();
            widgets.Add (buttons);
        }
		private bool SetupEditor (Editor editor) {
			EditorState state = editor.CreateState ();

			EditorSelection selection = new EditorSelection ();
			PhotoImageView photo_view = MainWindow.Toplevel.PhotoView.View;

			if (Page.InPhotoView && photo_view != null) {
				if (photo_view.GetSelection (out selection.x, out selection.y,
							out selection.width, out selection.height))
					state.Selection = selection;
				else
					state.Selection = null;
				state.PhotoImageView = photo_view;
			} else {
				state.Selection = null;
				state.PhotoImageView = null;
			}
			if (Page.Sidebar.Selection == null)
				return false;
			state.Items = Page.Sidebar.Selection.Items;

			editor.Initialize (state);
			return true;
		}
		private void HandleEditorDone (object sender, EventArgs args)
		{
			Editor old = sender as Editor;

			old.Done -= HandleEditorDone;
				
			if (old == editor)
				editor = null;
		}