/src/AppWorkspace.cs
C# | 2324 lines | 1847 code | 387 blank | 90 comment | 271 complexity | 2692eb01900f1c52eb6630b8e0400cf2 MD5 | raw file
- /////////////////////////////////////////////////////////////////////////////////
- // Paint.NET //
- // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
- // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
- // See src/Resources/Files/License.txt for full licensing and attribution //
- // details. //
- // . //
- /////////////////////////////////////////////////////////////////////////////////
-
- using PaintDotNet.Actions;
- using PaintDotNet.Base;
- using PaintDotNet.HistoryFunctions;
- using PaintDotNet.HistoryMementos;
- using PaintDotNet.SystemLayer;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Globalization;
- using System.Reflection;
- using System.Windows.Forms;
-
- namespace PaintDotNet
- {
- internal class AppWorkspace
- : UserControl,
- ISnapObstacleHost
- {
- private readonly string _cursorInfoStatusBarFormat = PdnResources.GetString("StatusBar.CursorInfo.Format");
- private readonly string _imageInfoStatusBarFormat = PdnResources.GetString("StatusBar.Size.Format");
-
- private Type _defaultToolTypeChoice;
-
- private Type _globalToolTypeChoice;
- private bool _globalRulersChoice;
-
- private DocumentWorkspace _activeDocumentWorkspace;
-
- // if a new workspace is added, and this workspace is not dirty, then it will be removed.
- // This keeps track of the last workspace added via CreateBlankDocumentInNewWorkspace (if
- // true was passed for its 2nd parameter)
- private DocumentWorkspace _initialWorkspace;
-
- private readonly List<DocumentWorkspace> _documentWorkspaces = new List<DocumentWorkspace>();
- private readonly WorkspaceWidgets _widgets;
-
- private Panel _workspacePanel;
- private PdnStatusBar _statusBar;
-
- private ToolsForm _mainToolBarForm;
- private LayerForm _layerForm;
- private HistoryForm _historyForm;
- private ColorsForm _colorsForm;
-
- private MostRecentFiles _mostRecentFiles;
- private const int DefaultMostRecentFilesMax = 8;
-
- private SnapObstacleController _snapObstacle;
- private bool _addedToSnapManager;
- private int _ignoreUpdateSnapObstacle;
- private int _suspendThumbnailUpdates;
-
- public void CheckForUpdates()
- {
- ToolBar.MainMenu.CheckForUpdates();
- }
-
- public IDisposable SuspendThumbnailUpdates()
- {
- var resumeFn = new CallbackOnDispose(ResumeThumbnailUpdates);
-
- ++_suspendThumbnailUpdates;
-
- if (_suspendThumbnailUpdates == 1)
- {
- Widgets.DocumentStrip.SuspendThumbnailUpdates();
- Widgets.LayerControl.SuspendLayerPreviewUpdates();
- }
-
- return resumeFn;
- }
-
- private void ResumeThumbnailUpdates()
- {
- --_suspendThumbnailUpdates;
-
- if (_suspendThumbnailUpdates != 0) return;
- Widgets.DocumentStrip.ResumeThumbnailUpdates();
- Widgets.LayerControl.ResumeLayerPreviewUpdates();
- }
-
- public Type DefaultToolType
- {
- get
- {
- return _defaultToolTypeChoice;
- }
-
- set
- {
- _defaultToolTypeChoice = value;
- Settings.CurrentUser.SetString(SettingNames.DefaultToolTypeName, value.Name);
- }
- }
-
- public Type GlobalToolTypeChoice
- {
- get
- {
- return _globalToolTypeChoice;
- }
-
- set
- {
- _globalToolTypeChoice = value;
-
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.SetToolFromType(value);
- }
- }
- }
-
- public DocumentWorkspace InitialWorkspace
- {
- set
- {
- _initialWorkspace = value;
- }
- }
-
- public event EventHandler RulersEnabledChanged;
- protected virtual void OnRulersEnabledChanged()
- {
- if (RulersEnabledChanged != null)
- {
- RulersEnabledChanged(this, EventArgs.Empty);
- }
- }
-
- public bool RulersEnabled
- {
- get
- {
- return _globalRulersChoice;
- }
-
- set
- {
- if (_globalRulersChoice == value) return;
- _globalRulersChoice = value;
-
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.RulersEnabled = value;
- }
-
- OnRulersEnabledChanged();
- }
- }
-
- private void DocumentWorkspaceDrawGridChanged(object sender, EventArgs e)
- {
- DrawGrid = _activeDocumentWorkspace.DrawGrid;
- }
-
- private void ViewConfigStripDrawGridChanged(object sender, EventArgs e)
- {
- DrawGrid = ((ViewConfigStrip)sender).DrawGrid;
- }
-
- private bool DrawGrid
- {
- get
- {
- return Widgets.ViewConfigStrip.DrawGrid;
- }
-
- set
- {
- if (Widgets.ViewConfigStrip.DrawGrid != value)
- {
- Widgets.ViewConfigStrip.DrawGrid = value;
- }
-
- if (_activeDocumentWorkspace != null && _activeDocumentWorkspace.DrawGrid != value)
- {
- _activeDocumentWorkspace.DrawGrid = value;
- }
-
- Settings.CurrentUser.SetBoolean(SettingNames.DrawGrid, DrawGrid);
- }
- }
-
- public event EventHandler UnitsChanged;
- protected virtual void OnUnitsChanged()
- {
- if (UnitsChanged != null)
- {
- UnitsChanged(this, EventArgs.Empty);
- }
- }
-
- public MeasurementUnit Units
- {
- get
- {
- return _widgets.ViewConfigStrip.Units;
- }
-
- set
- {
- _widgets.ViewConfigStrip.Units = value;
- }
- }
-
- public SnapObstacle SnapObstacle
- {
- get
- {
- if (_snapObstacle == null)
- {
- // HACK: for some reason retrieving the ClientRectangle can raise a VisibleChanged event
- // so we initially pass in Rectangle.Empty for the rectangle bounds
- _snapObstacle = new SnapObstacleController(
- Name,
- Rectangle.Empty,
- SnapRegion.Interior,
- true) {EnableSave = false};
-
- var pdbForm = FindForm() as PdnBaseForm;
- if (pdbForm != null)
- {
- pdbForm.Moving += ParentFormMoving;
- pdbForm.Move += ParentFormMove;
- pdbForm.ResizeEnd += ParentFormResizeEnd;
- pdbForm.Layout += ParentFormLayout;
- pdbForm.SizeChanged += ParentFormSizeChanged;
- }
-
- UpdateSnapObstacle();
- }
-
- return _snapObstacle;
- }
- }
-
- private void ParentFormMove(object sender, EventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentFormSizeChanged(object sender, EventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentFormLayout(object sender, LayoutEventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentFormResizeEnd(object sender, EventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentFormMoving(object sender, MovingEventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void SuspendUpdateSnapObstacle()
- {
- ++_ignoreUpdateSnapObstacle;
- }
-
- private void ResumeUpdateSnapObstacle()
- {
- --_ignoreUpdateSnapObstacle;
- }
-
- private void UpdateSnapObstacle()
- {
- if (_ignoreUpdateSnapObstacle > 0)
- {
- return;
- }
-
- if (_snapObstacle == null)
- {
- return;
- }
-
- if (!_addedToSnapManager)
- {
- SnapManager sm = SnapManager.FindMySnapManager(this);
-
- if (sm != null)
- {
- SnapObstacle so = SnapObstacle;
-
- if (!_addedToSnapManager)
- {
- sm.AddSnapObstacle(SnapObstacle);
- _addedToSnapManager = true;
-
- FindForm().Shown += AppWorkspaceShown;
- }
- }
- }
-
- {
- Rectangle clientRect = ActiveDocumentWorkspace != null ? ActiveDocumentWorkspace.VisibleViewRectangle : _workspacePanel.ClientRectangle;
-
- Rectangle screenRect = _workspacePanel.RectangleToScreen(clientRect);
- _snapObstacle.SetBounds(screenRect);
- _snapObstacle.Enabled = Visible && Enabled;
- }
- }
-
- private void AppWorkspaceShown(object sender, EventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- protected override void OnLayout(LayoutEventArgs levent)
- {
- UpdateSnapObstacle();
- base.OnLayout(levent);
- }
-
- protected override void OnLocationChanged(EventArgs e)
- {
- UpdateSnapObstacle();
- base.OnLocationChanged(e);
- }
-
- protected override void OnSizeChanged(EventArgs e)
- {
- UpdateSnapObstacle();
- base.OnSizeChanged(e);
- }
-
- protected override void OnEnabledChanged(EventArgs e)
- {
- UpdateSnapObstacle();
- base.OnEnabledChanged(e);
- }
-
- protected override void OnVisibleChanged(EventArgs e)
- {
- UpdateSnapObstacle();
- base.OnVisibleChanged(e);
- }
-
- public void ResetFloatingForms()
- {
- ResetFloatingForm(Widgets.ToolsForm);
- ResetFloatingForm(Widgets.HistoryForm);
- ResetFloatingForm(Widgets.LayerForm);
- ResetFloatingForm(Widgets.ColorsForm);
- }
-
- public void ResetFloatingForm(FloatingToolForm ftf)
- {
- SnapManager sm = SnapManager.FindMySnapManager(this);
-
- if (ftf == Widgets.ToolsForm)
- {
- sm.ParkObstacle(Widgets.ToolsForm, this, HorizontalSnapEdge.Top, VerticalSnapEdge.Left);
- }
- else if (ftf == Widgets.HistoryForm)
- {
- sm.ParkObstacle(Widgets.HistoryForm, this, HorizontalSnapEdge.Top, VerticalSnapEdge.Right);
- }
- else if (ftf == Widgets.LayerForm)
- {
- sm.ParkObstacle(Widgets.LayerForm, this, HorizontalSnapEdge.Bottom, VerticalSnapEdge.Right);
- }
- else if (ftf == Widgets.ColorsForm)
- {
- sm.ParkObstacle(Widgets.ColorsForm, this, HorizontalSnapEdge.Bottom, VerticalSnapEdge.Left);
- }
- else
- {
- throw new ArgumentException();
- }
- }
-
- private readonly Set<Triple<Assembly, Type, Exception>> _effectLoadErrors = new Set<Triple<Assembly, Type, Exception>>();
-
- public void ReportEffectLoadError(Triple<Assembly, Type, Exception> error)
- {
- lock (_effectLoadErrors)
- {
- if (!_effectLoadErrors.Contains(error))
- {
- _effectLoadErrors.Add(error);
- }
- }
- }
-
- public static string GetLocalizedEffectErrorMessage(Assembly assembly, Type type, Exception exception)
- {
- IPluginSupportInfo supportInfo;
- string typeName;
-
- if (type != null)
- {
- typeName = type.FullName;
- supportInfo = PluginSupportInfo.GetPluginSupportInfo(type);
- }
- else if (exception is TypeLoadException)
- {
- var asTlex = exception as TypeLoadException;
- typeName = asTlex.TypeName;
- supportInfo = PluginSupportInfo.GetPluginSupportInfo(assembly);
- }
- else
- {
- supportInfo = PluginSupportInfo.GetPluginSupportInfo(assembly);
- typeName = null;
- }
-
- return GetLocalizedEffectErrorMessage(assembly, typeName, supportInfo, exception);
- }
-
- public static string GetLocalizedEffectErrorMessage(Assembly assembly, string typeName, Exception exception)
- {
- IPluginSupportInfo supportInfo = PluginSupportInfo.GetPluginSupportInfo(assembly);
- return GetLocalizedEffectErrorMessage(assembly, typeName, supportInfo, exception);
- }
-
- private static string GetLocalizedEffectErrorMessage(Assembly assembly, string typeName, IPluginSupportInfo supportInfo, Exception exception)
- {
- string fileName = assembly.Location;
- string shortErrorFormat = PdnResources.GetString("EffectErrorMessage.ShortFormat");
- string fullErrorFormat = PdnResources.GetString("EffectErrorMessage.FullFormat");
- string notSuppliedText = PdnResources.GetString("EffectErrorMessage.InfoNotSupplied");
-
- string errorText;
-
- if (supportInfo == null)
- {
- errorText = string.Format(
- shortErrorFormat,
- fileName,
- typeName ?? notSuppliedText,
- exception);
- }
- else
- {
- errorText = string.Format(
- fullErrorFormat,
- fileName,
- typeName ?? supportInfo.DisplayName ?? notSuppliedText,
- (supportInfo.Version ?? new Version()),
- supportInfo.Author ?? notSuppliedText,
- supportInfo.Copyright ?? notSuppliedText,
- (supportInfo.WebsiteUri == null ? notSuppliedText : supportInfo.WebsiteUri.ToString()),
- exception);
- }
-
- return errorText;
- }
-
- public IList<Triple<Assembly, Type, Exception>> GetEffectLoadErrors()
- {
- return _effectLoadErrors.ToArray();
- }
-
- public void RunEffect(Type effectType)
- {
- // TODO: this is kind of a hack
- ToolBar.MainMenu.RunEffect(effectType);
- }
-
- public PdnToolBar ToolBar { get; private set; }
-
- private static ImageResource FileNewIcon
- {
- get
- {
- return PdnResources.GetImageResource("Icons.MenuFileNewIcon.png");
- }
- }
-
- private static ImageResource ImageFromDiskIcon
- {
- get
- {
- return PdnResources.GetImageResource("Icons.ImageFromDiskIcon.png");
- }
- }
-
- public MostRecentFiles MostRecentFiles
- {
- get { return _mostRecentFiles ?? (_mostRecentFiles = new MostRecentFiles(DefaultMostRecentFilesMax)); }
- }
-
- private void DocumentWorkspaceDocumentChanging(object sender, EventArgs<Document> e)
- {
- UI.SuspendControlPainting(this);
- }
-
- private void DocumentWorkspaceDocumentChanged(object sender, EventArgs e)
- {
- UpdateDocInfoInStatusBar();
-
- UI.ResumeControlPainting(this);
- Invalidate(true);
- }
-
- private void CoordinatesToStrings(int x, int y, out string xString, out string yString, out string unitsString)
- {
- _activeDocumentWorkspace.Document.CoordinatesToStrings(Units, x, y, out xString, out yString, out unitsString);
- }
-
- private void UpdateCursorInfoInStatusBar(int cursorX, int cursorY)
- {
- SuspendLayout();
-
- if (_activeDocumentWorkspace == null ||
- _activeDocumentWorkspace.Document == null)
- {
- _statusBar.CursorInfoText = string.Empty;
- }
- else
- {
- string xString;
- string yString;
- string units;
-
- CoordinatesToStrings(cursorX, cursorY, out xString, out yString, out units);
-
- string cursorText = string.Format(
- CultureInfo.InvariantCulture,
- _cursorInfoStatusBarFormat,
- xString,
- units,
- yString,
- units);
-
- _statusBar.CursorInfoText = cursorText;
- }
-
- ResumeLayout(false);
- }
-
- private void UpdateDocInfoInStatusBar()
- {
- if (_activeDocumentWorkspace == null ||
- _activeDocumentWorkspace.Document == null)
- {
- _statusBar.ImageInfoStatusText = string.Empty;
- }
- else
- {
- string widthString;
- string heightString;
- string units;
-
- CoordinatesToStrings(
- _activeDocumentWorkspace.Document.Width,
- _activeDocumentWorkspace.Document.Height,
- out widthString,
- out heightString,
- out units);
-
- string imageText = string.Format(
- CultureInfo.InvariantCulture,
- _imageInfoStatusBarFormat,
- widthString,
- units,
- heightString,
- units);
-
- _statusBar.ImageInfoStatusText = imageText;
- }
- }
-
- [Browsable(false)]
- public WorkspaceWidgets Widgets
- {
- get
- {
- return _widgets;
- }
- }
-
- [Browsable(false)]
- public AppEnvironment AppEnvironment { get; private set; }
-
- [Browsable(false)]
- public DocumentWorkspace ActiveDocumentWorkspace
- {
- get
- {
- return _activeDocumentWorkspace;
- }
-
- set
- {
- if (value == _activeDocumentWorkspace) return;
- if (value != null &&
- _documentWorkspaces.IndexOf(value) == -1)
- {
- throw new ArgumentException("DocumentWorkspace was not created with AddNewDocumentWorkspace");
- }
-
- bool focused = false;
- if (_activeDocumentWorkspace != null)
- {
- focused = _activeDocumentWorkspace.Focused;
- }
-
- UI.SuspendControlPainting(this);
- OnActiveDocumentWorkspaceChanging();
- _activeDocumentWorkspace = value;
- OnActiveDocumentWorkspaceChanged();
- UI.ResumeControlPainting(this);
-
- Refresh();
-
- if (value != null)
- {
- value.Focus();
- }
- }
- }
-
- private void ActiveDocumentWorkspaceFirstInputAfterGotFocus(object sender, EventArgs e)
- {
- ToolBar.DocumentStrip.EnsureItemFullyVisible(ToolBar.DocumentStrip.SelectedDocumentIndex);
- }
-
- public DocumentWorkspace[] DocumentWorkspaces
- {
- get
- {
- return _documentWorkspaces.ToArray();
- }
- }
-
- public DocumentWorkspace AddNewDocumentWorkspace()
- {
- if (_initialWorkspace != null)
- {
- if (_initialWorkspace.Document == null || !_initialWorkspace.Document.Dirty)
- {
- _globalToolTypeChoice = _initialWorkspace.GetToolType();
- RemoveDocumentWorkspace(_initialWorkspace);
- _initialWorkspace = null;
- }
- }
-
- var dw = new DocumentWorkspace {AppWorkspace = this};
-
- _documentWorkspaces.Add(dw);
- ToolBar.DocumentStrip.AddDocumentWorkspace(dw);
-
- return dw;
- }
-
- public Image GetDocumentWorkspaceThumbnail(DocumentWorkspace dw)
- {
- ToolBar.DocumentStrip.SyncThumbnails();
- Image[] images = ToolBar.DocumentStrip.DocumentThumbnails;
- DocumentWorkspace[] documents = ToolBar.DocumentStrip.DocumentList;
-
- for (int i = 0; i < documents.Length; ++i)
- {
- if (documents[i] == dw)
- {
- return images[i];
- }
- }
-
- throw new ArgumentException("The requested DocumentWorkspace doesn't exist in this AppWorkspace");
- }
-
- public void RemoveDocumentWorkspace(DocumentWorkspace documentWorkspace)
- {
- int dwIndex = _documentWorkspaces.IndexOf(documentWorkspace);
-
- if (dwIndex == -1)
- {
- throw new ArgumentException("DocumentWorkspace was not created with AddNewDocumentWorkspace");
- }
-
- bool removingCurrentDW;
- if (ActiveDocumentWorkspace == documentWorkspace)
- {
- removingCurrentDW = true;
- _globalToolTypeChoice = documentWorkspace.GetToolType();
- }
- else
- {
- removingCurrentDW = false;
- }
-
- documentWorkspace.SetTool(null);
-
- // Choose new active DW if removing the current DW
- if (removingCurrentDW)
- {
- if (_documentWorkspaces.Count == 1)
- {
- ActiveDocumentWorkspace = null;
- }
- else if (dwIndex == 0)
- {
- ActiveDocumentWorkspace = _documentWorkspaces[1];
- }
- else
- {
- ActiveDocumentWorkspace = _documentWorkspaces[dwIndex - 1];
- }
- }
-
- _documentWorkspaces.Remove(documentWorkspace);
- ToolBar.DocumentStrip.RemoveDocumentWorkspace(documentWorkspace);
-
- if (_initialWorkspace == documentWorkspace)
- {
- _initialWorkspace = null;
- }
-
- // Clean up the DocumentWorkspace
- Document document = documentWorkspace.Document;
-
- documentWorkspace.Document = null;
- document.Dispose();
-
- documentWorkspace.Dispose();
- documentWorkspace = null;
- }
-
- private void UpdateHistoryButtons()
- {
- if (ActiveDocumentWorkspace == null)
- {
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Undo, false);
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Redo, false);
- }
- else
- {
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Undo,
- ActiveDocumentWorkspace.History.UndoStack.Count > 1);
-
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Redo,
- ActiveDocumentWorkspace.History.RedoStack.Count > 0);
- }
- }
-
- private void HistoryChangedHandler(object sender, EventArgs e)
- {
- UpdateHistoryButtons();
-
- // some actions change the document size: make sure we update our status bar panel
- // TODO: shouldn't this be handled by our DocumentWorkspace.DocumentChanged handler...?
- UpdateDocInfoInStatusBar();
- }
-
- public event EventHandler ActiveDocumentWorkspaceChanging;
- protected virtual void OnActiveDocumentWorkspaceChanging()
- {
- SuspendUpdateSnapObstacle();
-
- if (ActiveDocumentWorkspaceChanging != null)
- {
- ActiveDocumentWorkspaceChanging(this, EventArgs.Empty);
- }
-
- if (_activeDocumentWorkspace != null)
- {
- _activeDocumentWorkspace.FirstInputAfterGotFocus +=
- ActiveDocumentWorkspaceFirstInputAfterGotFocus;
-
- _activeDocumentWorkspace.RulersEnabledChanged -= DocumentWorkspaceRulersEnabledChanged;
- _activeDocumentWorkspace.DocumentMouseEnter -= DocumentMouseEnterHandler;
- _activeDocumentWorkspace.DocumentMouseLeave -= DocumentMouseLeaveHandler;
- _activeDocumentWorkspace.DocumentMouseMove -= DocumentMouseMoveHandler;
- _activeDocumentWorkspace.DocumentMouseDown -= DocumentMouseDownHandler;
- _activeDocumentWorkspace.Scroll -= DocumentWorkspaceScroll;
- _activeDocumentWorkspace.Layout -= DocumentWorkspaceLayout;
- _activeDocumentWorkspace.DrawGridChanged -= DocumentWorkspaceDrawGridChanged;
- _activeDocumentWorkspace.DocumentClick -= DocumentClick;
- _activeDocumentWorkspace.DocumentMouseUp -= DocumentMouseUpHandler;
- _activeDocumentWorkspace.DocumentKeyPress -= DocumentKeyPress;
- _activeDocumentWorkspace.DocumentKeyUp -= DocumenKeyUp;
- _activeDocumentWorkspace.DocumentKeyDown -= DocumentKeyDown;
-
- _activeDocumentWorkspace.History.Changed -= HistoryChangedHandler;
- _activeDocumentWorkspace.StatusChanged -= OnDocumentWorkspaceStatusChanged;
- _activeDocumentWorkspace.DocumentChanging -= DocumentWorkspaceDocumentChanging;
- _activeDocumentWorkspace.DocumentChanged -= DocumentWorkspaceDocumentChanged;
- _activeDocumentWorkspace.Selection.Changing -= SelectedPathChangingHandler;
- _activeDocumentWorkspace.Selection.Changed -= SelectedPathChangedHandler;
- _activeDocumentWorkspace.ScaleFactorChanged -= ZoomChangedHandler;
- _activeDocumentWorkspace.ZoomBasisChanged -= DocumentWorkspaceZoomBasisChanged;
-
- _activeDocumentWorkspace.Visible = false;
- _historyForm.HistoryControl.HistoryStack = null;
-
- _activeDocumentWorkspace.ToolChanging -= ToolChangingHandler;
- _activeDocumentWorkspace.ToolChanged -= ToolChangedHandler;
-
- if (_activeDocumentWorkspace.Tool != null)
- {
- while (_activeDocumentWorkspace.Tool.IsMouseEntered)
- {
- _activeDocumentWorkspace.Tool.PerformMouseLeave();
- }
- }
-
- Type toolType = _activeDocumentWorkspace.GetToolType();
-
- if (toolType != null)
- {
- _globalToolTypeChoice = _activeDocumentWorkspace.GetToolType();
- }
- }
-
- ResumeUpdateSnapObstacle();
- UpdateSnapObstacle();
- }
-
- public event EventHandler ActiveDocumentWorkspaceChanged;
- protected virtual void OnActiveDocumentWorkspaceChanged()
- {
- SuspendUpdateSnapObstacle();
-
- if (_activeDocumentWorkspace == null)
- {
- ToolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Print, false);
- ToolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Save, false);
- }
- else
- {
- _activeDocumentWorkspace.SuspendLayout();
-
- ToolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Print, true);
- ToolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Save, true);
-
- _activeDocumentWorkspace.BackColor = SystemColors.ControlDark;
- _activeDocumentWorkspace.Dock = DockStyle.Fill;
- _activeDocumentWorkspace.DrawGrid = DrawGrid;
- _activeDocumentWorkspace.PanelAutoScroll = true;
- _activeDocumentWorkspace.RulersEnabled = _globalRulersChoice;
- _activeDocumentWorkspace.TabIndex = 0;
- _activeDocumentWorkspace.TabStop = false;
- _activeDocumentWorkspace.RulersEnabledChanged += DocumentWorkspaceRulersEnabledChanged;
- _activeDocumentWorkspace.DocumentMouseEnter += DocumentMouseEnterHandler;
- _activeDocumentWorkspace.DocumentMouseLeave += DocumentMouseLeaveHandler;
- _activeDocumentWorkspace.DocumentMouseMove += DocumentMouseMoveHandler;
- _activeDocumentWorkspace.DocumentMouseDown += DocumentMouseDownHandler;
- _activeDocumentWorkspace.Scroll += DocumentWorkspaceScroll;
- _activeDocumentWorkspace.DrawGridChanged += DocumentWorkspaceDrawGridChanged;
- _activeDocumentWorkspace.DocumentClick += DocumentClick;
- _activeDocumentWorkspace.DocumentMouseUp += DocumentMouseUpHandler;
- _activeDocumentWorkspace.DocumentKeyPress += DocumentKeyPress;
- _activeDocumentWorkspace.DocumentKeyUp += DocumenKeyUp;
- _activeDocumentWorkspace.DocumentKeyDown += DocumentKeyDown;
-
- if (_workspacePanel.Controls.Contains(_activeDocumentWorkspace))
- {
- _activeDocumentWorkspace.Visible = true;
- }
- else
- {
- _activeDocumentWorkspace.Dock = DockStyle.Fill;
- _workspacePanel.Controls.Add(_activeDocumentWorkspace);
- }
-
- _activeDocumentWorkspace.Layout += DocumentWorkspaceLayout;
- ToolBar.ViewConfigStrip.ScaleFactor = _activeDocumentWorkspace.ScaleFactor;
- ToolBar.ViewConfigStrip.ZoomBasis = _activeDocumentWorkspace.ZoomBasis;
-
- _activeDocumentWorkspace.AppWorkspace = this;
- _activeDocumentWorkspace.History.Changed += HistoryChangedHandler;
- _activeDocumentWorkspace.StatusChanged += OnDocumentWorkspaceStatusChanged;
- _activeDocumentWorkspace.DocumentChanging += DocumentWorkspaceDocumentChanging;
- _activeDocumentWorkspace.DocumentChanged += DocumentWorkspaceDocumentChanged;
- _activeDocumentWorkspace.Selection.Changing += SelectedPathChangingHandler;
- _activeDocumentWorkspace.Selection.Changed += SelectedPathChangedHandler;
- _activeDocumentWorkspace.ScaleFactorChanged += ZoomChangedHandler;
- _activeDocumentWorkspace.ZoomBasisChanged += DocumentWorkspaceZoomBasisChanged;
-
- _activeDocumentWorkspace.Units = _widgets.ViewConfigStrip.Units;
-
- _historyForm.HistoryControl.HistoryStack = ActiveDocumentWorkspace.History;
-
- _activeDocumentWorkspace.ToolChanging += ToolChangingHandler;
- _activeDocumentWorkspace.ToolChanged += ToolChangedHandler;
-
- ToolBar.ViewConfigStrip.RulersEnabled = _activeDocumentWorkspace.RulersEnabled;
- ToolBar.DocumentStrip.SelectDocumentWorkspace(_activeDocumentWorkspace);
-
- _activeDocumentWorkspace.SetToolFromType(_globalToolTypeChoice);
-
- UpdateSelectionToolbarButtons();
- UpdateHistoryButtons();
- UpdateDocInfoInStatusBar();
-
- _activeDocumentWorkspace.ResumeLayout();
- _activeDocumentWorkspace.PerformLayout();
-
- _activeDocumentWorkspace.FirstInputAfterGotFocus +=
- ActiveDocumentWorkspaceFirstInputAfterGotFocus;
- }
-
- if (ActiveDocumentWorkspaceChanged != null)
- {
- ActiveDocumentWorkspaceChanged(this, EventArgs.Empty);
- }
-
- UpdateStatusBarContextStatus();
- ResumeUpdateSnapObstacle();
- UpdateSnapObstacle();
- }
-
- public AppWorkspace()
- {
- SuspendLayout();
-
- // initialize!
- InitializeComponent();
- InitializeFloatingForms();
-
- _mainToolBarForm.ToolsControl.SetTools(DocumentWorkspace.ToolInfos);
- _mainToolBarForm.ToolsControl.ToolClicked += MainToolBarToolClicked;
-
- ToolBar.ToolChooserStrip.SetTools(DocumentWorkspace.ToolInfos);
- ToolBar.ToolChooserStrip.ToolClicked += MainToolBarToolClicked;
-
- ToolBar.AppWorkspace = this;
-
- // init the Widgets container
- _widgets = new WorkspaceWidgets(this)
- {
- ViewConfigStrip = ToolBar.ViewConfigStrip,
- CommonActionsStrip = ToolBar.CommonActionsStrip,
- ToolConfigStrip = ToolBar.ToolConfigStrip,
- ToolsForm = _mainToolBarForm,
- LayerForm = _layerForm,
- HistoryForm = _historyForm,
- ColorsForm = _colorsForm,
- StatusBarProgress = _statusBar,
- DocumentStrip = ToolBar.DocumentStrip
- };
-
- // Load our settings and initialize the AppEnvironment
- LoadSettings();
-
- // hook into Environment *Changed events
- AppEnvironment.PrimaryColorChanged += PrimaryColorChangedHandler;
- AppEnvironment.SecondaryColorChanged += SecondaryColorChangedHandler;
- AppEnvironment.ShapeDrawTypeChanged += ShapeDrawTypeChangedHandler;
- AppEnvironment.GradientInfoChanged += GradientInfoChangedHandler;
- AppEnvironment.ToleranceChanged += OnEnvironmentToleranceChanged;
- AppEnvironment.AlphaBlendingChanged += AlphaBlendingChangedHandler;
- AppEnvironment.FontInfo = ToolBar.ToolConfigStrip.FontInfo;
- AppEnvironment.TextAlignment = ToolBar.ToolConfigStrip.FontAlignment;
- AppEnvironment.AntiAliasingChanged += EnvironmentAntiAliasingChanged;
- AppEnvironment.FontInfoChanged += EnvironmentFontInfoChanged;
- AppEnvironment.FontSmoothingChanged += EnvironmentFontSmoothingChanged;
- AppEnvironment.TextAlignmentChanged += EnvironmentTextAlignmentChanged;
- AppEnvironment.PenInfoChanged += EnvironmentPenInfoChanged;
- AppEnvironment.BrushInfoChanged += EnvironmentBrushInfoChanged;
- AppEnvironment.ColorPickerClickBehaviorChanged += EnvironmentColorPickerClickBehaviorChanged;
- AppEnvironment.ResamplingAlgorithmChanged += EnvironmentResamplingAlgorithmChanged;
- AppEnvironment.SelectionCombineModeChanged += EnvironmentSelectionCombineModeChanged;
- AppEnvironment.FloodModeChanged += EnvironmentFloodModeChanged;
- AppEnvironment.SelectionDrawModeInfoChanged += EnvironmentSelectionDrawModeInfoChanged;
-
- ToolBar.DocumentStrip.RelinquishFocus += RelinquishFocusHandler;
-
- ToolBar.ToolConfigStrip.ToleranceChanged += OnToolBarToleranceChanged;
- ToolBar.ToolConfigStrip.FontAlignmentChanged += ToolConfigStripTextAlignmentChanged;
- ToolBar.ToolConfigStrip.FontInfoChanged += ToolConfigStripFontTextChanged;
- ToolBar.ToolConfigStrip.FontSmoothingChanged += ToolConfigStripFontSmoothingChanged;
- ToolBar.ToolConfigStrip.RelinquishFocus += RelinquishFocusHandler2;
-
- ToolBar.CommonActionsStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- ToolBar.CommonActionsStrip.MouseWheel += OnToolStripMouseWheel;
- ToolBar.CommonActionsStrip.ButtonClick += CommonActionsStripButtonClick;
-
- ToolBar.ViewConfigStrip.DrawGridChanged += ViewConfigStripDrawGridChanged;
- ToolBar.ViewConfigStrip.RulersEnabledChanged += ViewConfigStripRulersEnabledChanged;
- ToolBar.ViewConfigStrip.ZoomBasisChanged += ViewConfigStripZoomBasisChanged;
- ToolBar.ViewConfigStrip.ZoomScaleChanged += ViewConfigStripZoomScaleChanged;
- ToolBar.ViewConfigStrip.ZoomIn += ViewConfigStripZoomIn;
- ToolBar.ViewConfigStrip.ZoomOut += ViewConfigStripZoomOut;
- ToolBar.ViewConfigStrip.UnitsChanged += ViewConfigStripUnitsChanged;
- ToolBar.ViewConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- ToolBar.ViewConfigStrip.MouseWheel += OnToolStripMouseWheel;
-
- ToolBar.ToolConfigStrip.BrushInfoChanged += DrawConfigStripBrushChanged;
- ToolBar.ToolConfigStrip.ShapeDrawTypeChanged += DrawConfigStripShapeDrawTypeChanged;
- ToolBar.ToolConfigStrip.PenInfoChanged += DrawConfigStripPenChanged;
- ToolBar.ToolConfigStrip.GradientInfoChanged += ToolConfigStrip_GradientInfoChanged;
- ToolBar.ToolConfigStrip.AlphaBlendingChanged += OnDrawConfigStripAlphaBlendingChanged;
- ToolBar.ToolConfigStrip.AntiAliasingChanged += DrawConfigStripAntiAliasingChanged;
- ToolBar.ToolConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- ToolBar.ToolConfigStrip.ColorPickerClickBehaviorChanged += ToolConfigStripColorPickerClickBehaviorChanged;
- ToolBar.ToolConfigStrip.ResamplingAlgorithmChanged += ToolConfigStripResamplingAlgorithmChanged;
- ToolBar.ToolConfigStrip.SelectionCombineModeChanged += ToolConfigStripSelectionCombineModeChanged;
- ToolBar.ToolConfigStrip.FloodModeChanged += ToolConfigStripFloodModeChanged;
- ToolBar.ToolConfigStrip.SelectionDrawModeInfoChanged += ToolConfigStripSelectionDrawModeInfoChanged;
- ToolBar.ToolConfigStrip.SelectionDrawModeUnitsChanging += ToolConfigStripSelectionDrawModeUnitsChanging;
-
- ToolBar.ToolConfigStrip.MouseWheel += OnToolStripMouseWheel;
-
- ToolBar.DocumentStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- ToolBar.DocumentStrip.DocumentClicked += DocumentStripDocumentTabClicked;
- ToolBar.DocumentStrip.DocumentListChanged += DocumentStripDocumentListChanged;
-
- // Synchronize
- AppEnvironment.PerformAllChanged();
-
- _globalToolTypeChoice = _defaultToolTypeChoice;
- ToolBar.ToolConfigStrip.ToolBarConfigItems = ToolBarConfigItems.None;
-
- _layerForm.LayerControl.AppWorkspace = this;
-
- ResumeLayout();
- PerformLayout();
- }
-
- private void ToolConfigStripColorPickerClickBehaviorChanged(object sender, EventArgs e)
- {
- AppEnvironment.ColorPickerClickBehavior = _widgets.ToolConfigStrip.ColorPickerClickBehavior;
- }
-
- private void EnvironmentColorPickerClickBehaviorChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.ColorPickerClickBehavior = AppEnvironment.ColorPickerClickBehavior;
- }
-
- private void ToolConfigStripResamplingAlgorithmChanged(object sender, EventArgs e)
- {
- AppEnvironment.ResamplingAlgorithm = _widgets.ToolConfigStrip.ResamplingAlgorithm;
- }
-
- private void EnvironmentResamplingAlgorithmChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.ResamplingAlgorithm = AppEnvironment.ResamplingAlgorithm;
- }
-
- private void ToolConfigStripSelectionCombineModeChanged(object sender, EventArgs e)
- {
- AppEnvironment.SelectionCombineMode = _widgets.ToolConfigStrip.SelectionCombineMode;
- }
-
- private void EnvironmentSelectionCombineModeChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.SelectionCombineMode = AppEnvironment.SelectionCombineMode;
- }
-
- private void ToolConfigStripFloodModeChanged(object sender, EventArgs e)
- {
- AppEnvironment.FloodMode = _widgets.ToolConfigStrip.FloodMode;
- }
-
- private void EnvironmentFloodModeChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.FloodMode = AppEnvironment.FloodMode;
- }
-
- private void ToolConfigStripSelectionDrawModeInfoChanged(object sender, EventArgs e)
- {
- AppEnvironment.SelectionDrawModeInfo = _widgets.ToolConfigStrip.SelectionDrawModeInfo;
- }
-
- private void EnvironmentSelectionDrawModeInfoChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.SelectionDrawModeInfo = AppEnvironment.SelectionDrawModeInfo;
- }
-
- private sealed class ToolConfigStripSelectionDrawModeUnitsChangeHandler
- {
- private readonly ToolConfigStrip _toolConfigStrip;
- private readonly Document _activeDocument;
- private readonly MeasurementUnit _oldUnits;
-
- public ToolConfigStripSelectionDrawModeUnitsChangeHandler(ToolConfigStrip toolConfigStrip, Document activeDocument)
- {
- _toolConfigStrip = toolConfigStrip;
- _activeDocument = activeDocument;
- _oldUnits = toolConfigStrip.SelectionDrawModeInfo.Units;
- }
-
- public void Initialize()
- {
- _toolConfigStrip.SelectionDrawModeUnitsChanged += ToolConfigStripSelectionDrawModeUnitsChanged;
- }
-
- private void ToolConfigStripSelectionDrawModeUnitsChanged(object sender, EventArgs e)
- {
- try
- {
- SelectionDrawModeInfo sdmi = _toolConfigStrip.SelectionDrawModeInfo;
- MeasurementUnit newUnits = sdmi.Units;
-
- double oldWidth = sdmi.Width;
- double oldHeight = sdmi.Height;
-
- double newWidth = Document.ConvertMeasurement(oldWidth, _oldUnits, _activeDocument.DpuUnit, _activeDocument.DpuX, newUnits);
- double newHeight = Document.ConvertMeasurement(oldHeight, _oldUnits, _activeDocument.DpuUnit, _activeDocument.DpuY, newUnits);
-
- SelectionDrawModeInfo newSdmi = sdmi.CloneWithNewWidthAndHeight(newWidth, newHeight);
- _toolConfigStrip.SelectionDrawModeInfo = newSdmi;
- }
-
- finally
- {
- _toolConfigStrip.SelectionDrawModeUnitsChanged -= ToolConfigStripSelectionDrawModeUnitsChanged;
- }
- }
- }
-
- private void ToolConfigStripSelectionDrawModeUnitsChanging(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace == null || ActiveDocumentWorkspace.Document == null) return;
- var tcsSdmuch = new ToolConfigStripSelectionDrawModeUnitsChangeHandler(
- ToolBar.ToolConfigStrip, ActiveDocumentWorkspace.Document);
-
- tcsSdmuch.Initialize();
- }
-
- private void DocumentStripDocumentListChanged(object sender, EventArgs e)
- {
- bool enableThem = (_widgets.DocumentStrip.DocumentCount != 0);
-
- _widgets.ToolsForm.Enabled = enableThem;
- _widgets.HistoryForm.Enabled = enableThem;
- _widgets.LayerForm.Enabled = enableThem;
- _widgets.ColorsForm.Enabled = enableThem;
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Paste, enableThem);
-
- UpdateHistoryButtons();
- UpdateDocInfoInStatusBar();
- UpdateCursorInfoInStatusBar(0, 0);
- }
-
- public void SaveSettings()
- {
- Settings.CurrentUser.SetBoolean(SettingNames.Rulers, _globalRulersChoice);
- Settings.CurrentUser.SetBoolean(SettingNames.DrawGrid, DrawGrid);
- Settings.CurrentUser.SetString(SettingNames.DefaultToolTypeName, _defaultToolTypeChoice.Name);
- MostRecentFiles.SaveMruList();
- }
-
- private void LoadDefaultToolType()
- {
- string defaultToolTypeName = Settings.CurrentUser.GetString(SettingNames.DefaultToolTypeName, Tool.DefaultToolType.Name);
-
- ToolInfo[] tis = DocumentWorkspace.ToolInfos;
- ToolInfo ti = Array.Find(
- tis,
- check =>
- string.Compare(defaultToolTypeName, check.ToolType.Name, StringComparison.InvariantCultureIgnoreCase) ==
- 0);
-
- _defaultToolTypeChoice = ti == null ? Tool.DefaultToolType : ti.ToolType;
- }
-
- public void LoadSettings()
- {
- try
- {
- LoadDefaultToolType();
-
- _globalToolTypeChoice = _defaultToolTypeChoice;
- _globalRulersChoice = Settings.CurrentUser.GetBoolean(SettingNames.Rulers, false);
- DrawGrid = Settings.CurrentUser.GetBoolean(SettingNames.DrawGrid, false);
-
- AppEnvironment = AppEnvironment.GetDefaultAppEnvironment();
-
- _widgets.ViewConfigStrip.Units = (MeasurementUnit)Enum.Parse(typeof(MeasurementUnit),
- Settings.CurrentUser.GetString(SettingNames.Units, MeasurementUnit.Pixel.ToString()), true);
- }
-
- catch (Exception)
- {
- AppEnvironment = new AppEnvironment();
- AppEnvironment.SetToDefaults();
-
- try
- {
- Settings.CurrentUser.Delete(
- new[]
- {
- SettingNames.Rulers,
- SettingNames.DrawGrid,
- SettingNames.Units,
- SettingNames.DefaultAppEnvironment,
- SettingNames.DefaultToolTypeName,
- });
- }
-
- catch (Exception)
- {
- }
- }
-
- try
- {
- ToolBar.ToolConfigStrip.LoadFromAppEnvironment(AppEnvironment);
- }
-
- catch (Exception)
- {
- AppEnvironment = new AppEnvironment();
- AppEnvironment.SetToDefaults();
- ToolBar.ToolConfigStrip.LoadFromAppEnvironment(AppEnvironment);
- }
- }
-
- protected override void OnLoad(EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.Select();
- }
-
- UpdateSnapObstacle();
-
- base.OnLoad(e);
- }
-
- public void RefreshTool()
- {
- Type toolType = _activeDocumentWorkspace.GetToolType();
- Widgets.ToolsControl.SelectTool(toolType);
- }
-
- private void GradientInfoChangedHandler(object sender, EventArgs e)
- {
- if (_widgets.ToolConfigStrip.GradientInfo != AppEnvironment.GradientInfo)
- {
- _widgets.ToolConfigStrip.GradientInfo = AppEnvironment.GradientInfo;
- }
- }
-
- private void ToolConfigStrip_GradientInfoChanged(object sender, EventArgs e)
- {
- if (AppEnvironment.GradientInfo != _widgets.ToolConfigStrip.GradientInfo)
- {
- AppEnvironment.GradientInfo = _widgets.ToolConfigStrip.GradientInfo;
- }
- }
-
- /// <summary>
- /// Keeps the Environment's ShapeDrawType and the corresponding widget synchronized
- /// </summary>
- private void ShapeDrawTypeChangedHandler(object sender, EventArgs e)
- {
- if (_widgets.ToolConfigStrip.ShapeDrawType != AppEnvironment.ShapeDrawType)
- {
- _widgets.ToolConfigStrip.ShapeDrawType = AppEnvironment.ShapeDrawType;
- }
- }
-
- /// <summary>
- /// Keeps the Environment's alpha blending value and the corresponding widget synchronized
- /// </summary>
- private void AlphaBlendingChangedHandler(object sender, EventArgs e)
- {
- if (_widgets.ToolConfigStrip.AlphaBlending != AppEnvironment.AlphaBlending)
- {
- _widgets.ToolConfigStrip.AlphaBlending = AppEnvironment.AlphaBlending;
- }
- }
-
- private void ColorDisplay_UserPrimaryAndSecondaryColorsChanged(object sender, EventArgs e)
- {
- // We need to make sure that we don't change which user color is selected (primary vs. secondary)
- // To do this we choose the ordering based on which one is currently active (primary vs. secondary)
- if (_widgets.ColorsForm.WhichUserColor == WhichUserColor.Primary)
- {
- _widgets.ColorsForm.SetColorControlsRedraw(false);
- SecondaryColorChangedHandler(sender, e);
- PrimaryColorChangedHandler(sender, e);
- _widgets.ColorsForm.SetColorControlsRedraw(true);
- _widgets.ColorsForm.WhichUserColor = WhichUserColor.Primary;
- }
- else //if (widgets.ColorsForm.WhichUserColor == WhichUserColor.Background)
- {
- _widgets.ColorsForm.SetColorControlsRedraw(false);
- PrimaryColorChangedHandler(sender, e);
- SecondaryColorChangedHandler(sender, e);
- _widgets.ColorsForm.SetColorControlsRedraw(true);
- _widgets.ColorsForm.WhichUserColor = WhichUserColor.Secondary;
- }
- }
-
- private void PrimaryColorChangedHandler(object sender, EventArgs e)
- {
- if (sender == AppEnvironment)
- {
- _widgets.ColorsForm.UserPrimaryColor = AppEnvironment.PrimaryColor;
- }
- }
-
- private void OnToolBarToleranceChanged(object sender, EventArgs e)
- {
- AppEnvironment.Tolerance = _widgets.ToolConfigStrip.Tolerance;
- Focus();
- }
-
- private void OnEnvironmentToleranceChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.Tolerance = AppEnvironment.Tolerance;
- Focus();
- }
-
- private void SecondaryColorChangedHandler(object sender, EventArgs e)
- {
- if (sender == AppEnvironment)
- {
- _widgets.ColorsForm.UserSecondaryColor = AppEnvironment.SecondaryColor;
- }
- }
-
- private void RelinquishFocusHandler(object sender, EventArgs e)
- {
- Focus();
- }
-
- private void RelinquishFocusHandler2(object sender, EventArgs e)
- {
- if (_activeDocumentWorkspace != null)
- {
- _activeDocumentWorkspace.Focus();
- }
- }
-
- private void ColorsFormUserPrimaryColorChanged(object sender, ColorEventArgs e)
- {
- var cf = (ColorsForm)sender;
- AppEnvironment.PrimaryColor = e.Color;
- }
-
- private void ColorsFormUserSecondaryColorChanged(object sender, ColorEventArgs e)
- {
- var cf = (ColorsForm)sender;
- AppEnvironment.SecondaryColor = e.Color;
- }
-
- /// <summary>
- /// Handles the SelectedPathChanging event that is raised by the AppEnvironment.
- /// </summary>
- private static void SelectedPathChangingHandler(object sender, EventArgs e)
- {
- }
-
- private void UpdateSelectionToolbarButtons()
- {
- if (ActiveDocumentWorkspace == null || ActiveDocumentWorkspace.Selection.IsEmpty)
- {
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Cut, false);
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Copy, false);
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Deselect, false);
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.CropToSelection, false);
- }
- else
- {
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Cut, true);
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Copy, true);
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Deselect, true);
- _widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.CropToSelection, true);
- }
- }
-
- /// <summary>
- /// Handles the SelectedPathChanged event that is raised by the AppEnvironment.
- /// </summary>
- private void SelectedPathChangedHandler(object sender, EventArgs e)
- {
- UpdateSelectionToolbarButtons();
- }
-
- private void ZoomChangedHandler(object sender, EventArgs e)
- {
- ScaleFactor sf = _activeDocumentWorkspace.ScaleFactor;
- ToolBar.ViewConfigStrip.SuspendEvents();
- ToolBar.ViewConfigStrip.ZoomBasis = _activeDocumentWorkspace.ZoomBasis;
- ToolBar.ViewConfigStrip.ScaleFactor = sf;
- ToolBar.ViewConfigStrip.ResumeEvents();
- }
-
- private void InitializeComponent()
- {
- ToolBar = new PdnToolBar();
- _statusBar = new PdnStatusBar();
- _workspacePanel = new Panel();
- _workspacePanel.SuspendLayout();
- _statusBar.SuspendLayout();
- SuspendLayout();
- //
- // toolBar
- //
- ToolBar.Name = "ToolBar";
- ToolBar.Dock = DockStyle.Top;
- //
- // statusBar
- //
- _statusBar.Name = "_statusBar";
- //
- // workspacePanel
- //
- _workspacePanel.Name = "_workspacePanel";
- _workspacePanel.Dock = DockStyle.Fill;
- //
- // AppWorkspace
- //
- Controls.Add(_workspacePanel);
- Controls.Add(_statusBar);
- Controls.Add(ToolBar);
- Name = "AppWorkspace";
- Size = new Size(872, 640);
- _workspacePanel.ResumeLayout(false);
- _statusBar.ResumeLayout(false);
- _statusBar.PerformLayout();
- ResumeLayout(false);
- }
-
- private void DocumentStripDocumentTabClicked(
- object sender,
- EventArgs<Pair<DocumentWorkspace, DocumentClickAction>> e)
- {
- switch (e.Data.Second)
- {
- case DocumentClickAction.Select:
- ActiveDocumentWorkspace = e.Data.First;
- break;
-
- case DocumentClickAction.Close:
- var cwa = new CloseWorkspaceAction(e.Data.First);
- PerformAction(cwa);
- break;
-
- default:
- throw new NotImplementedException("Code for DocumentClickAction." + e.Data.Second + " not implemented");
- }
-
- Update();
- }
-
- private void OnToolStripMouseWheel(object sender, MouseEventArgs e)
- {
- if (_activeDocumentWorkspace != null)
- {
- _activeDocumentWorkspace.PerformMouseWheel((Control)sender, e);
- }
- }
-
- private void OnToolStripRelinquishFocus(object sender, EventArgs e)
- {
- if (_activeDocumentWorkspace != null)
- {
- _activeDocumentWorkspace.Focus();
- }
- }
-
- // The Document* events are raised by the Document class, handled here,
- // and relayed as necessary. For instance, for the DocumentMouse* events,
- // these are all relayed to the active tool.
-
- private void DocumentMouseEnterHandler(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformMouseEnter();
- }
- }
-
- private void DocumentMouseLeaveHandler(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformMouseLeave();
- }
- }
-
- private void DocumentMouseUpHandler(object sender, MouseEventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformMouseUp(e);
- }
- }
-
- private void DocumentMouseDownHandler(object sender, MouseEventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformMouseDown(e);
- }
- }
-
- private void DocumentMouseMoveHandler(object sender, MouseEventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformMouseMove(e);
- }
-
- UpdateCursorInfoInStatusBar(e.X, e.Y);
- }
-
- private void DocumentClick(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformClick();
- }
- }
-
- private void DocumentKeyPress(object sender, KeyPressEventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformKeyPress(e);
- }
- }
-
- private void DocumentKeyDown(object sender, KeyEventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformKeyDown(e);
- }
- }
-
- private void DocumenKeyUp(object sender, KeyEventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- ActiveDocumentWorkspace.Tool.PerformKeyUp(e);
- }
- }
-
- private void InitializeFloatingForms()
- {
- // MainToolBarForm
- _mainToolBarForm = new ToolsForm();
- _mainToolBarForm.RelinquishFocus += RelinquishFocusHandler;
- _mainToolBarForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
-
- // LayerForm
- _layerForm = new LayerForm {LayerControl = {AppWorkspace = this}};
- _layerForm.LayerControl.ClickedOnLayer += LayerControlClickedOnLayer;
- _layerForm.NewLayerButtonClick += LayerFormNewLayerButtonClicked;
- _layerForm.DeleteLayerButtonClick += LayerFormDeleteLayerButtonClicked;
- _layerForm.DuplicateLayerButtonClick += LayerFormDuplicateLayerButtonClick;
- _layerForm.MergeLayerDownClick += LayerFormMergeLayerDownClick;
- _layerForm.MoveLayerUpButtonClick += LayerFormMoveLayerUpButtonClicked;
- _layerForm.MoveLayerDownButtonClick += LayerFormMoveLayerDownButtonClicked;
- _layerForm.PropertiesButtonClick += LayerFormPropertiesButtonClick;
- _layerForm.RelinquishFocus += RelinquishFocusHandler;
- _layerForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
-
- // HistoryForm
- _historyForm = new HistoryForm();
- _historyForm.RewindButtonClicked += HistoryFormRewindButtonClicked;
- _historyForm.UndoButtonClicked += HistoryFormUndoButtonClicked;
- _historyForm.RedoButtonClicked += HistoryFormRedoButtonClicked;
- _historyForm.FastForwardButtonClicked += HistoryFormFastForwardButtonClicked;
- _historyForm.RelinquishFocus += RelinquishFocusHandler;
- _historyForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
-
- // ColorsForm
- _colorsForm = new ColorsForm
- {
- PaletteCollection = new PaletteCollection(),
- WhichUserColor = WhichUserColor.Primary
- };
- _colorsForm.UserPrimaryColorChanged += ColorsFormUserPrimaryColorChanged;
- _colorsForm.UserSecondaryColorChanged += ColorsFormUserSecondaryColorChanged;
- _colorsForm.RelinquishFocus += RelinquishFocusHandler;
- _colorsForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
- }
-
- // TODO: put at correct scope
- public event CmdKeysEventHandler ProcessCmdKeyEvent;
-
- private bool OnToolFormProcessCmdKeyEvent(object sender, ref Message msg, Keys keyData)
- {
- return ProcessCmdKeyEvent != null && ProcessCmdKeyEvent(sender, ref msg, keyData);
- }
-
- public void PerformActionAsync(AppWorkspaceAction performMe)
- {
- BeginInvoke(new Procedure<AppWorkspaceAction>(PerformAction), new object[] { performMe });
- }
-
- public void PerformAction(AppWorkspaceAction performMe)
- {
- Update();
-
- using (new WaitCursorChanger(this))
- {
- performMe.PerformAction(this);
- }
-
- Update();
- }
-
- private void MainToolBarToolClicked(object sender, ToolClickedEventArgs e)
- {
- if (ActiveDocumentWorkspace == null) return;
- ActiveDocumentWorkspace.Focus();
- ActiveDocumentWorkspace.SetToolFromType(e.ToolType);
- }
-
- private void ToolChangingHandler(object sender, EventArgs e)
- {
- UI.SuspendControlPainting(ToolBar);
-
- if (ActiveDocumentWorkspace.Tool != null)
- {
- // unregister for events here (none at this time)
- }
- }
-
- private void ToolChangedHandler(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace.Tool != null)
- {
- _widgets.ToolsControl.SelectTool(ActiveDocumentWorkspace.GetToolType(), false);
- ToolBar.ToolChooserStrip.SelectTool(ActiveDocumentWorkspace.GetToolType(), false);
- ToolBar.ToolConfigStrip.Visible = true; // HACK: see bug #2702
- ToolBar.ToolConfigStrip.ToolBarConfigItems = ActiveDocumentWorkspace.Tool.ToolBarConfigItems;
- _globalToolTypeChoice = ActiveDocumentWorkspace.GetToolType();
- }
-
- UpdateStatusBarContextStatus();
-
- UI.ResumeControlPainting(ToolBar);
- ToolBar.Refresh();
- }
-
- private void DrawConfigStripAntiAliasingChanged(object sender, EventArgs e)
- {
- AppEnvironment.AntiAliasing = ((ToolConfigStrip)sender).AntiAliasing;
- }
-
- private void DrawConfigStripPenChanged(object sender, EventArgs e)
- {
- AppEnvironment.PenInfo = ToolBar.ToolConfigStrip.PenInfo;
- }
-
- private void DrawConfigStripBrushChanged(object sender, EventArgs e)
- {
- AppEnvironment.BrushInfo = ToolBar.ToolConfigStrip.BrushInfo;
- }
-
- private void LayerControlClickedOnLayer(object sender, EventArgs<Layer> ce)
- {
- if (ActiveDocumentWorkspace != null)
- {
- if (ce.Data != ActiveDocumentWorkspace.ActiveLayer)
- {
- ActiveDocumentWorkspace.ActiveLayer = ce.Data;
- }
- }
-
- RelinquishFocusHandler(sender, EventArgs.Empty);
- }
-
- private void LayerFormNewLayerButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.ExecuteFunction(new AddNewBlankLayerFunction());
- }
- }
-
- private void LayerFormDeleteLayerButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null && ActiveDocumentWorkspace.Document.Layers.Count > 1)
- {
- ActiveDocumentWorkspace.ExecuteFunction(new DeleteLayerFunction(ActiveDocumentWorkspace.ActiveLayerIndex));
- }
- }
-
- private void LayerFormMergeLayerDownClick(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace == null) return;
- if (ActiveDocumentWorkspace.ActiveLayerIndex <= 0) return;
- // TODO: keep this in sync with LayersMenu. not appropriate to refactor into an Action for a 'dot' release
- int newLayerIndex = Utility.Clamp(
- ActiveDocumentWorkspace.ActiveLayerIndex - 1,
- 0,
- ActiveDocumentWorkspace.Document.Layers.Count - 1);
-
- ActiveDocumentWorkspace.ExecuteFunction(
- new MergeLayerDownFunction(ActiveDocumentWorkspace.ActiveLayerIndex));
-
- ActiveDocumentWorkspace.ActiveLayerIndex = newLayerIndex;
- }
-
- private void LayerFormDuplicateLayerButtonClick(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.ExecuteFunction(new DuplicateLayerFunction(ActiveDocumentWorkspace.ActiveLayerIndex));
- }
- }
-
- private void LayerFormMoveLayerUpButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null && ActiveDocumentWorkspace.Document.Layers.Count >= 2)
- {
- ActiveDocumentWorkspace.PerformAction(new MoveActiveLayerUpAction());
- }
- }
-
- private void LayerFormMoveLayerDownButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null && ActiveDocumentWorkspace.Document.Layers.Count >= 2)
- {
- ActiveDocumentWorkspace.PerformAction(new MoveActiveLayerDownAction());
- }
- }
-
- private void DrawConfigStripShapeDrawTypeChanged(object sender, EventArgs e)
- {
- if (AppEnvironment.ShapeDrawType != _widgets.ToolConfigStrip.ShapeDrawType)
- {
- AppEnvironment.ShapeDrawType = _widgets.ToolConfigStrip.ShapeDrawType;
- }
- }
-
- private void HistoryFormUndoButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.PerformAction(new HistoryUndoAction());
- }
- }
-
- private void HistoryFormRedoButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.PerformAction(new HistoryRedoAction());
- }
- }
-
- private void ViewConfigStripRulersEnabledChanged(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.RulersEnabled = ToolBar.ViewConfigStrip.RulersEnabled;
- }
- }
-
- private void HistoryFormRewindButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.PerformAction(new HistoryRewindAction());
- }
- }
-
- private void HistoryFormFastForwardButtonClicked(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.PerformAction(new HistoryFastForwardAction());
- }
- }
-
- private void LayerFormPropertiesButtonClick(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.PerformAction(new OpenActiveLayerPropertiesAction());
- }
- }
-
- private void EnvironmentFontInfoChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.FontInfo = AppEnvironment.FontInfo;
- }
-
- private void EnvironmentFontSmoothingChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.FontSmoothing = AppEnvironment.FontSmoothing;
- }
-
- private void EnvironmentTextAlignmentChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.FontAlignment = AppEnvironment.TextAlignment;
- }
-
- private void EnvironmentPenInfoChanged(object sender, EventArgs e)
- {
- _widgets.ToolConfigStrip.PenInfo = AppEnvironment.PenInfo;
- }
-
- private static void EnvironmentBrushInfoChanged(object sender, EventArgs e)
- {
- }
-
- private void ToolConfigStripTextAlignmentChanged(object sender, EventArgs e)
- {
- AppEnvironment.TextAlignment = _widgets.ToolConfigStrip.FontAlignment;
- }
-
- private void ToolConfigStripFontTextChanged(object sender, EventArgs e)
- {
- AppEnvironment.FontInfo = _widgets.ToolConfigStrip.FontInfo;
- }
-
- private void ToolConfigStripFontSmoothingChanged(object sender, EventArgs e)
- {
- AppEnvironment.FontSmoothing = _widgets.ToolConfigStrip.FontSmoothing;
- }
-
- protected override void OnResize(EventArgs e)
- {
- UpdateSnapObstacle();
-
- base.OnResize(e);
-
- if (ParentForm != null && ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.EnableToolPulse = ParentForm.WindowState != FormWindowState.Minimized;
- }
- }
-
- private void DocumentWorkspaceScroll(object sender, ScrollEventArgs e)
- {
- OnScroll(e);
- }
-
- private void DocumentWorkspaceLayout(object sender, LayoutEventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ViewConfigStripZoomBasisChanged(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace == null) return;
- if (ActiveDocumentWorkspace.ZoomBasis != ToolBar.ViewConfigStrip.ZoomBasis)
- {
- ActiveDocumentWorkspace.ZoomBasis = ToolBar.ViewConfigStrip.ZoomBasis;
- }
- }
-
- private void DocumentWorkspaceZoomBasisChanged(object sender, EventArgs e)
- {
- if (ToolBar.ViewConfigStrip.ZoomBasis != ActiveDocumentWorkspace.ZoomBasis)
- {
- ToolBar.ViewConfigStrip.ZoomBasis = ActiveDocumentWorkspace.ZoomBasis;
- }
- }
-
- private void ViewConfigStripZoomScaleChanged(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace == null) return;
- if (ToolBar.ViewConfigStrip.ZoomBasis == ZoomBasis.ScaleFactor)
- {
- _activeDocumentWorkspace.ScaleFactor = ToolBar.ViewConfigStrip.ScaleFactor;
- }
- }
-
- private void DocumentWorkspaceRulersEnabledChanged(object sender, EventArgs e)
- {
- ToolBar.ViewConfigStrip.RulersEnabled = _activeDocumentWorkspace.RulersEnabled;
- _globalRulersChoice = _activeDocumentWorkspace.RulersEnabled;
- PerformLayout();
- ActiveDocumentWorkspace.UpdateRulerSelectionTinting();
-
- Settings.CurrentUser.SetBoolean(SettingNames.Rulers, _activeDocumentWorkspace.RulersEnabled);
- }
-
- private void EnvironmentAntiAliasingChanged(object sender, EventArgs e)
- {
- ToolBar.ToolConfigStrip.AntiAliasing = AppEnvironment.AntiAliasing;
- }
-
- private void ViewConfigStripZoomIn(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.ZoomIn();
- }
- }
-
- private void ViewConfigStripZoomOut(object sender, EventArgs e)
- {
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.ZoomOut();
- }
- }
-
- private void ViewConfigStripUnitsChanged(object sender, EventArgs e)
- {
- if (ToolBar.ViewConfigStrip.Units != MeasurementUnit.Pixel)
- {
- Settings.CurrentUser.SetString(SettingNames.LastNonPixelUnits, ToolBar.ViewConfigStrip.Units.ToString());
- }
-
- if (_activeDocumentWorkspace != null)
- {
- _activeDocumentWorkspace.Units = Units;
- }
-
- Settings.CurrentUser.SetString(SettingNames.Units, ToolBar.ViewConfigStrip.Units.ToString());
-
- UpdateDocInfoInStatusBar();
- _statusBar.CursorInfoText = string.Empty;
-
- OnUnitsChanged();
- }
-
- private void OnDrawConfigStripAlphaBlendingChanged(object sender, EventArgs e)
- {
- if (AppEnvironment.AlphaBlending != _widgets.ToolConfigStrip.AlphaBlending)
- {
- AppEnvironment.AlphaBlending = _widgets.ToolConfigStrip.AlphaBlending;
- }
- }
-
- public event EventHandler StatusChanged;
- private void OnStatusChanged()
- {
- if (StatusChanged != null)
- {
- StatusChanged(this, EventArgs.Empty);
- }
- }
-
- private void OnDocumentWorkspaceStatusChanged(object sender, EventArgs e)
- {
- OnStatusChanged();
- UpdateStatusBarContextStatus();
- }
-
- private void UpdateStatusBarContextStatus()
- {
- if (ActiveDocumentWorkspace != null)
- {
- _statusBar.ContextStatusText = _activeDocumentWorkspace.StatusText;
- _statusBar.ContextStatusImage = _activeDocumentWorkspace.StatusIcon;
- }
- else
- {
- _statusBar.ContextStatusText = string.Empty;
- _statusBar.ContextStatusImage = null;
- }
- }
-
- /*
- private static bool NullGetThumbnailImageAbort()
- {
- return false;
- } //POTENTIAL UNUSED METHOD
- */
-
- /// <summary>
- /// Creates a blank document of the given size in a new workspace, and activates that workspace.
- /// </summary>
- /// <remarks>
- /// If isInitial=true, then last workspace added by this method is kept track of, and if it is not modified by
- /// the time the next workspace is added, then it will be removed.
- /// </remarks>
- /// <returns>true if everything was successful, false if there wasn't enough memory</returns>
- public bool CreateBlankDocumentInNewWorkspace(Size size, MeasurementUnit dpuUnit, double dpu, bool isInitial)
- {
- DocumentWorkspace dw1 = _activeDocumentWorkspace;
- if (dw1 != null)
- {
- dw1.SuspendRefresh();
- }
-
- try
- {
- var untitled = new Document(size.Width, size.Height) {DpuUnit = dpuUnit, DpuX = dpu, DpuY = dpu};
-
- BitmapLayer bitmapLayer;
-
- try
- {
- using (new WaitCursorChanger(this))
- {
- bitmapLayer = Layer.CreateBackgroundLayer(size.Width, size.Height);
- }
- }
-
- catch (OutOfMemoryException)
- {
- Utility.ErrorBox(this, PdnResources.GetString("NewImageAction.Error.OutOfMemory"));
- return false;
- }
-
- using (new WaitCursorChanger(this))
- {
- bool focused = false;
-
- if (ActiveDocumentWorkspace != null && ActiveDocumentWorkspace.Focused)
- {
- focused = true;
- }
-
- untitled.Layers.Add(bitmapLayer);
-
- DocumentWorkspace dw = AddNewDocumentWorkspace();
- Widgets.DocumentStrip.LockDocumentWorkspaceDirtyValue(dw, false);
- dw.SuspendRefresh();
-
- try
- {
- dw.Document = untitled;
- }
-
- catch (OutOfMemoryException)
- {
- Utility.ErrorBox(this, PdnResources.GetString("NewImageAction.Error.OutOfMemory"));
- RemoveDocumentWorkspace(dw);
- untitled.Dispose();
- return false;
- }
-
- dw.ActiveLayer = (Layer)dw.Document.Layers[0];
-
- ActiveDocumentWorkspace = dw;
-
- dw.SetDocumentSaveOptions(null, null, null);
- dw.History.ClearAll();
- dw.History.PushNewMemento(
- new NullHistoryMemento(PdnResources.GetString("NewImageAction.Name"),
- FileNewIcon));
-
- dw.Document.Dirty = false;
- dw.ResumeRefresh();
-
- if (isInitial)
- {
- _initialWorkspace = dw;
- }
-
- if (focused)
- {
- ActiveDocumentWorkspace.Focus();
- }
-
- Widgets.DocumentStrip.UnlockDocumentWorkspaceDirtyValue(dw);
- }
- }
-
- finally
- {
- if (dw1 != null)
- {
- dw1.ResumeRefresh();
- }
- }
-
- return true;
- }
-
- public bool OpenFilesInNewWorkspace(string[] fileNames)
- {
- if (IsDisposed)
- {
- return false;
- }
-
- bool result = true;
-
- foreach (string fileName in fileNames)
- {
- result &= OpenFileInNewWorkspace(fileName);
-
- if (!result)
- {
- break;
- }
- }
-
- return result;
- }
-
- public bool OpenFileInNewWorkspace(string fileName)
- {
- return OpenFileInNewWorkspace(fileName, true);
- }
-
- public bool OpenFileInNewWorkspace(string fileName, bool addToMruList)
- {
- if (fileName == null)
- {
- throw new ArgumentNullException("fileName");
- }
-
- if (fileName.Length == 0)
- {
- throw new ArgumentOutOfRangeException("fileName.Length == 0");
- }
-
- PdnBaseForm.UpdateAllForms();
-
- FileType fileType;
-
- _widgets.StatusBarProgress.ResetProgressStatusBar();
-
- ProgressEventHandler progressCallback =
- (sender, e) => _widgets.StatusBarProgress.SetProgressStatusBar(e.Percent);
-
- Document document = DocumentWorkspace.LoadDocument(this, fileName, out fileType, progressCallback);
- _widgets.StatusBarProgress.EraseProgressStatusBar();
-
- if (document == null)
- {
- Cursor = Cursors.Default;
- }
- else
- {
- using (new WaitCursorChanger(this))
- {
- DocumentWorkspace dw = AddNewDocumentWorkspace();
- Widgets.DocumentStrip.LockDocumentWorkspaceDirtyValue(dw, false);
-
- try
- {
- dw.Document = document;
- }
-
- catch (OutOfMemoryException)
- {
- Utility.ErrorBox(this, PdnResources.GetString("LoadImage.Error.OutOfMemoryException"));
- RemoveDocumentWorkspace(dw);
- document.Dispose();
- return false;
- }
-
- dw.ActiveLayer = (Layer)document.Layers[0];
-
- dw.SetDocumentSaveOptions(fileName, fileType, null);
-
- ActiveDocumentWorkspace = dw;
-
- dw.History.ClearAll();
-
- dw.History.PushNewMemento(
- new NullHistoryMemento(
- PdnResources.GetString("OpenImageAction.Name"),
- ImageFromDiskIcon));
-
- document.Dirty = false;
- Widgets.DocumentStrip.UnlockDocumentWorkspaceDirtyValue(dw);
- }
-
- {
- ActiveDocumentWorkspace.ZoomBasis = ZoomBasis.FitToWindow;
- }
-
- // add to MRU list
- if (addToMruList)
- {
- ActiveDocumentWorkspace.AddToMruList();
- }
-
- ToolBar.DocumentStrip.SyncThumbnails();
-
- WarnAboutSavedWithVersion(document.SavedWithVersion);
- }
-
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.Focus();
- }
-
- return document != null;
- }
-
- private static void WarnAboutSavedWithVersion(Version savedWith)
- {
- // warn about version?
- // 2.1 Build 1897 signifies when the file format changed and broke backwards compatibility (for saving)
- // 2.1 Build 1921 signifies when MemoryBlock was upgraded to support 64-bits, which broke it again
- // 2.1 Build 1924 upgraded to "unimportant ordering" for MemoryBlock serialization so we can to faster multiproc saves
- // (in v2.5 we always save in order, although that doesn't change the file format's laxness)
- // 2.5 Build 2105 changed the way PropertyItems are serialized
- // 2.6 Build upgrade to .NET 2.0, does not appear to be compatible with 2.5 and earlier files as a result
- if (savedWith >= new Version(2, 6, 0)) return;
- Version ourVersion = PdnInfo.GetVersion();
- var ourVersion2 = new Version(ourVersion.Major, ourVersion.Minor);
- var ourVersion3 = new Version(ourVersion.Major, ourVersion.Minor, ourVersion.Build);
-
- int fields = savedWith < ourVersion2 ? 2 : 3;
-
- string format = PdnResources.GetString("SavedWithOlderVersion.Format");
- string text = string.Format(format, savedWith.ToString(fields), ourVersion.ToString(fields));
-
- // TODO: should we even bother to inform them? It is probably more annoying than not,
- // especially since older versions will say "Hey this file is corrupt OR saved with a newer version"
- //Utility.InfoBox(this, text);
- }
-
- /// <summary>
- /// Computes what the size of a new document should be. If the screen is in a normal,
- /// wider-than-tall (landscape) mode then it returns 800x600. If the screen is in a
- /// taller-than-wide (portrait) mode then it retusn 600x800. If the screen is square
- /// then it returns 800x600.
- /// </summary>
- public Size GetNewDocumentSize()
- {
- var findForm = FindForm() as PdnBaseForm;
-
- if (findForm != null && findForm.ScreenAspect < 1.0)
- {
- return new Size(600, 800);
- }
- return new Size(800, 600);
- }
-
- private void CommonActionsStripButtonClick(object sender, EventArgs<CommonAction> e)
- {
- CommonAction ca = e.Data;
-
- switch (ca)
- {
- case CommonAction.New:
- PerformAction(new NewImageAction());
- break;
-
- case CommonAction.Open:
- PerformAction(new OpenFileAction());
- break;
-
- case CommonAction.Save:
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.DoSave();
- }
- break;
-
- case CommonAction.Print:
- if (ActiveDocumentWorkspace != null)
- {
- var pa = new PrintAction();
- ActiveDocumentWorkspace.PerformAction(pa);
- }
- break;
-
- case CommonAction.Cut:
- if (ActiveDocumentWorkspace != null)
- {
- var cutAction = new CutAction();
- cutAction.PerformAction(ActiveDocumentWorkspace);
- }
-
- break;
-
- case CommonAction.Copy:
- if (ActiveDocumentWorkspace != null)
- {
- var ctca = new CopyToClipboardAction(ActiveDocumentWorkspace);
- ctca.PerformAction();
- }
- break;
-
- case CommonAction.Paste:
- if (ActiveDocumentWorkspace != null)
- {
- var pa = new PasteAction(ActiveDocumentWorkspace);
- pa.PerformAction();
- }
-
- break;
-
- case CommonAction.CropToSelection:
- if (ActiveDocumentWorkspace != null)
- {
- using (new PushNullToolMode(ActiveDocumentWorkspace))
- {
- ActiveDocumentWorkspace.ExecuteFunction(new CropToSelectionFunction());
- }
- }
-
- break;
-
- case CommonAction.Deselect:
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.ExecuteFunction(new DeselectFunction());
- }
- break;
-
- case CommonAction.Undo:
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.PerformAction(new HistoryUndoAction());
- }
- break;
-
- case CommonAction.Redo:
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.PerformAction(new HistoryRedoAction());
- }
- break;
-
- default:
- throw new InvalidEnumArgumentException("e.Data");
- }
-
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.Focus();
- }
- }
- }
- }