/src/AppWorkspace.cs
C# | 2324 lines | 1847 code | 387 blank | 90 comment | 271 complexity | 2692eb01900f1c52eb6630b8e0400cf2 MD5 | raw file
Large files files are truncated, but you can click here to view the full 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)
- /…
Large files files are truncated, but you can click here to view the full file