/src/AppWorkspace.cs
C# | 2447 lines | 1971 code | 392 blank | 84 comment | 288 complexity | b9bd44fe47e89855f6c5b93526aa35c6 MD5 | raw file
Possible License(s): Unlicense
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.Effects;
- using PaintDotNet.HistoryFunctions;
- using PaintDotNet.HistoryMementos;
- using PaintDotNet.SystemLayer;
- using PaintDotNet.Tools;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.Globalization;
- using System.IO;
- using System.Reflection;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Security;
- using System.Text;
- 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 = null;
- private bool globalRulersChoice = false;
-
- private AppEnvironment appEnvironment;
- 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 List<DocumentWorkspace> documentWorkspaces = new List<DocumentWorkspace>();
- private WorkspaceWidgets widgets;
-
- private Panel workspacePanel;
- private PdnToolBar toolBar;
- private PdnStatusBar statusBar;
-
- private ToolsForm mainToolBarForm;
- private LayerForm layerForm;
- private HistoryForm historyForm;
- private ColorsForm colorsForm;
-
- private MostRecentFiles mostRecentFiles = null;
- private const int defaultMostRecentFilesMax = 8;
-
- private SnapObstacleController snapObstacle;
- private bool addedToSnapManager = false;
- private int ignoreUpdateSnapObstacle = 0;
- private int suspendThumbnailUpdates = 0;
-
- public void CheckForUpdates()
- {
- this.toolBar.MainMenu.CheckForUpdates();
- }
-
- public IDisposable SuspendThumbnailUpdates()
- {
- CallbackOnDispose resumeFn = new CallbackOnDispose(ResumeThumbnailUpdates);
-
- ++this.suspendThumbnailUpdates;
-
- if (this.suspendThumbnailUpdates == 1)
- {
- Widgets.DocumentStrip.SuspendThumbnailUpdates();
- Widgets.LayerControl.SuspendLayerPreviewUpdates();
- }
-
- return resumeFn;
- }
-
- private void ResumeThumbnailUpdates()
- {
- --this.suspendThumbnailUpdates;
-
- if (this.suspendThumbnailUpdates == 0)
- {
- Widgets.DocumentStrip.ResumeThumbnailUpdates();
- Widgets.LayerControl.ResumeLayerPreviewUpdates();
- }
- }
-
- public Type DefaultToolType
- {
- get
- {
- return this.defaultToolTypeChoice;
- }
-
- set
- {
- this.defaultToolTypeChoice = value;
- Settings.CurrentUser.SetString(SettingNames.DefaultToolTypeName, value.Name);
- }
- }
-
- public Type GlobalToolTypeChoice
- {
- get
- {
- return this.globalToolTypeChoice;
- }
-
- set
- {
- this.globalToolTypeChoice = value;
-
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.SetToolFromType(value);
- }
- }
- }
-
- public DocumentWorkspace InitialWorkspace
- {
- set
- {
- this.initialWorkspace = value;
- }
- }
-
- public event EventHandler RulersEnabledChanged;
- protected virtual void OnRulersEnabledChanged()
- {
- if (RulersEnabledChanged != null)
- {
- RulersEnabledChanged(this, EventArgs.Empty);
- }
- }
-
- public bool RulersEnabled
- {
- get
- {
- return this.globalRulersChoice;
- }
-
- set
- {
- if (this.globalRulersChoice != value)
- {
- this.globalRulersChoice = value;
-
- if (ActiveDocumentWorkspace != null)
- {
- ActiveDocumentWorkspace.RulersEnabled = value;
- }
-
- OnRulersEnabledChanged();
- }
- }
- }
-
- private void DocumentWorkspace_DrawGridChanged(object sender, EventArgs e)
- {
- DrawGrid = this.activeDocumentWorkspace.DrawGrid;
- }
-
- private void ViewConfigStrip_DrawGridChanged(object sender, EventArgs e)
- {
- DrawGrid = ((ViewConfigStrip)sender).DrawGrid;
- }
-
- private bool DrawGrid
- {
- get
- {
- return this.Widgets.ViewConfigStrip.DrawGrid;
- }
-
- set
- {
- if (this.Widgets.ViewConfigStrip.DrawGrid != value)
- {
- this.Widgets.ViewConfigStrip.DrawGrid = value;
- }
-
- if (this.activeDocumentWorkspace != null && this.activeDocumentWorkspace.DrawGrid != value)
- {
- this.activeDocumentWorkspace.DrawGrid = value;
- }
-
- Settings.CurrentUser.SetBoolean(SettingNames.DrawGrid, this.DrawGrid);
- }
- }
-
- public event EventHandler UnitsChanged;
- protected virtual void OnUnitsChanged()
- {
- if (UnitsChanged != null)
- {
- UnitsChanged(this, EventArgs.Empty);
- }
- }
-
- public MeasurementUnit Units
- {
- get
- {
- return this.widgets.ViewConfigStrip.Units;
- }
-
- set
- {
- this.widgets.ViewConfigStrip.Units = value;
- }
- }
-
- public SnapObstacle SnapObstacle
- {
- get
- {
- if (this.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
- this.snapObstacle = new SnapObstacleController(
- this.Name,
- Rectangle.Empty,
- SnapRegion.Interior,
- true);
-
- this.snapObstacle.EnableSave = false;
-
- PdnBaseForm pdbForm = FindForm() as PdnBaseForm;
- pdbForm.Moving += new MovingEventHandler(ParentForm_Moving);
- pdbForm.Move += new EventHandler(ParentForm_Move);
- pdbForm.ResizeEnd += new EventHandler(ParentForm_ResizeEnd);
- pdbForm.Layout += new LayoutEventHandler(ParentForm_Layout);
- pdbForm.SizeChanged += new EventHandler(ParentForm_SizeChanged);
-
- UpdateSnapObstacle();
- }
-
- return this.snapObstacle;
- }
- }
-
- private void ParentForm_Move(object sender, EventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentForm_SizeChanged(object sender, EventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentForm_Layout(object sender, LayoutEventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentForm_ResizeEnd(object sender, EventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void ParentForm_Moving(object sender, MovingEventArgs e)
- {
- UpdateSnapObstacle();
- }
-
- private void SuspendUpdateSnapObstacle()
- {
- ++this.ignoreUpdateSnapObstacle;
- }
-
- private void ResumeUpdateSnapObstacle()
- {
- --this.ignoreUpdateSnapObstacle;
- }
-
- private void UpdateSnapObstacle()
- {
- if (this.ignoreUpdateSnapObstacle > 0)
- {
- return;
- }
-
- if (this.snapObstacle == null)
- {
- return;
- }
-
- if (!this.addedToSnapManager)
- {
- SnapManager sm = SnapManager.FindMySnapManager(this);
-
- if (sm != null)
- {
- SnapObstacle so = this.SnapObstacle;
-
- if (!this.addedToSnapManager)
- {
- sm.AddSnapObstacle(this.SnapObstacle);
- this.addedToSnapManager = true;
-
- FindForm().Shown += new EventHandler(AppWorkspace_Shown);
- }
- }
- }
-
- if (this.snapObstacle != null)
- {
- Rectangle clientRect;
-
- if (ActiveDocumentWorkspace != null)
- {
- clientRect = ActiveDocumentWorkspace.VisibleViewRectangle;
- }
- else
- {
- clientRect = this.workspacePanel.ClientRectangle;
- }
-
- Rectangle screenRect = this.workspacePanel.RectangleToScreen(clientRect);
- this.snapObstacle.SetBounds(screenRect);
- this.snapObstacle.Enabled = this.Visible && this.Enabled;
- }
- }
-
- private void AppWorkspace_Shown(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 Set<Triple<Assembly, Type, Exception>> effectLoadErrors = new Set<Triple<Assembly, Type, Exception>>();
-
- public void ReportEffectLoadError(Triple<Assembly, Type, Exception> error)
- {
- lock (this.effectLoadErrors)
- {
- if (!this.effectLoadErrors.Contains(error))
- {
- this.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)
- {
- TypeLoadException 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 ?? notSuppliedText,
- typeName ?? notSuppliedText,
- exception.ToString());
- }
- else
- {
- errorText = string.Format(
- fullErrorFormat,
- fileName ?? notSuppliedText,
- typeName ?? supportInfo.DisplayName ?? notSuppliedText,
- (supportInfo.Version ?? new Version()).ToString(),
- supportInfo.Author ?? notSuppliedText,
- supportInfo.Copyright ?? notSuppliedText,
- (supportInfo.WebsiteUri == null ? notSuppliedText : supportInfo.WebsiteUri.ToString()),
- exception.ToString());
- }
-
- return errorText;
- }
-
- public IList<Triple<Assembly, Type, Exception>> GetEffectLoadErrors()
- {
- return this.effectLoadErrors.ToArray();
- }
-
- public void RunEffect(Type effectType)
- {
- // TODO: this is kind of a hack
- this.toolBar.MainMenu.RunEffect(effectType);
- }
-
- public PdnToolBar ToolBar
- {
- get
- {
- return this.toolBar;
- }
- }
-
- private ImageResource FileNewIcon
- {
- get
- {
- return PdnResources.GetImageResource("Icons.MenuFileNewIcon.png");
- }
- }
-
- private ImageResource ImageFromDiskIcon
- {
- get
- {
- return PdnResources.GetImageResource("Icons.ImageFromDiskIcon.png");
- }
- }
-
- public MostRecentFiles MostRecentFiles
- {
- get
- {
- if (this.mostRecentFiles == null)
- {
- this.mostRecentFiles = new MostRecentFiles(defaultMostRecentFilesMax);
- }
-
- return this.mostRecentFiles;
- }
- }
-
- private void DocumentWorkspace_DocumentChanging(object sender, EventArgs<Document> e)
- {
- UI.SuspendControlPainting(this);
- }
-
- private void DocumentWorkspace_DocumentChanged(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)
- {
- this.activeDocumentWorkspace.Document.CoordinatesToStrings(this.Units, x, y, out xString, out yString, out unitsString);
- }
-
- private void UpdateCursorInfoInStatusBar(int cursorX, int cursorY)
- {
- SuspendLayout();
-
- if (this.activeDocumentWorkspace == null ||
- this.activeDocumentWorkspace.Document == null)
- {
- this.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,
- this.cursorInfoStatusBarFormat,
- xString,
- units,
- yString,
- units);
-
- this.statusBar.CursorInfoText = cursorText;
- }
-
- ResumeLayout(false);
- }
-
- private void UpdateDocInfoInStatusBar()
- {
- if (this.activeDocumentWorkspace == null ||
- this.activeDocumentWorkspace.Document == null)
- {
- this.statusBar.ImageInfoStatusText = string.Empty;
- }
- else if (this.activeDocumentWorkspace != null &&
- this.activeDocumentWorkspace.Document != null)
- {
- string widthString;
- string heightString;
- string units;
-
- CoordinatesToStrings(
- this.activeDocumentWorkspace.Document.Width,
- this.activeDocumentWorkspace.Document.Height,
- out widthString,
- out heightString,
- out units);
-
- string imageText = string.Format(
- CultureInfo.InvariantCulture,
- this.imageInfoStatusBarFormat,
- widthString,
- units,
- heightString,
- units);
-
- this.statusBar.ImageInfoStatusText = imageText;
- }
- }
-
- [Browsable(false)]
- public WorkspaceWidgets Widgets
- {
- get
- {
- return this.widgets;
- }
- }
-
- [Browsable(false)]
- public AppEnvironment AppEnvironment
- {
- get
- {
- return this.appEnvironment;
- }
- }
-
- [Browsable(false)]
- public DocumentWorkspace ActiveDocumentWorkspace
- {
- get
- {
- return this.activeDocumentWorkspace;
- }
-
- set
- {
- if (value != this.activeDocumentWorkspace)
- {
- if (value != null &&
- this.documentWorkspaces.IndexOf(value) == -1)
- {
- throw new ArgumentException("DocumentWorkspace was not created with AddNewDocumentWorkspace");
- }
-
- bool focused = false;
- if (this.activeDocumentWorkspace != null)
- {
- focused = this.activeDocumentWorkspace.Focused;
- }
-
- UI.SuspendControlPainting(this);
- OnActiveDocumentWorkspaceChanging();
- this.activeDocumentWorkspace = value;
- OnActiveDocumentWorkspaceChanged();
- UI.ResumeControlPainting(this);
-
- Refresh();
-
- if (value != null)
- {
- value.Focus();
- }
- }
- }
- }
-
- private void ActiveDocumentWorkspace_FirstInputAfterGotFocus(object sender, EventArgs e)
- {
- this.toolBar.DocumentStrip.EnsureItemFullyVisible(this.toolBar.DocumentStrip.SelectedDocumentIndex);
- }
-
- public DocumentWorkspace[] DocumentWorkspaces
- {
- get
- {
- return this.documentWorkspaces.ToArray();
- }
- }
-
- public DocumentWorkspace AddNewDocumentWorkspace()
- {
- if (this.initialWorkspace != null)
- {
- if (this.initialWorkspace.Document == null || !this.initialWorkspace.Document.Dirty)
- {
- this.globalToolTypeChoice = this.initialWorkspace.GetToolType();
- RemoveDocumentWorkspace(this.initialWorkspace);
- this.initialWorkspace = null;
- }
- }
-
- DocumentWorkspace dw = new DocumentWorkspace();
-
- dw.AppWorkspace = this;
- this.documentWorkspaces.Add(dw);
- this.toolBar.DocumentStrip.AddDocumentWorkspace(dw);
-
- return dw;
- }
-
- public Image GetDocumentWorkspaceThumbnail(DocumentWorkspace dw)
- {
- this.toolBar.DocumentStrip.SyncThumbnails();
- Image[] images = this.toolBar.DocumentStrip.DocumentThumbnails;
- DocumentWorkspace[] documents = this.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 = this.documentWorkspaces.IndexOf(documentWorkspace);
-
- if (dwIndex == -1)
- {
- throw new ArgumentException("DocumentWorkspace was not created with AddNewDocumentWorkspace");
- }
-
- bool removingCurrentDW;
- if (this.ActiveDocumentWorkspace == documentWorkspace)
- {
- removingCurrentDW = true;
- this.globalToolTypeChoice = documentWorkspace.GetToolType();
- }
- else
- {
- removingCurrentDW = false;
- }
-
- documentWorkspace.SetTool(null);
-
- // Choose new active DW if removing the current DW
- if (removingCurrentDW)
- {
- if (this.documentWorkspaces.Count == 1)
- {
- this.ActiveDocumentWorkspace = null;
- }
- else if (dwIndex == 0)
- {
- this.ActiveDocumentWorkspace = this.documentWorkspaces[1];
- }
- else
- {
- this.ActiveDocumentWorkspace = this.documentWorkspaces[dwIndex - 1];
- }
- }
-
- this.documentWorkspaces.Remove(documentWorkspace);
- this.toolBar.DocumentStrip.RemoveDocumentWorkspace(documentWorkspace);
-
- if (this.initialWorkspace == documentWorkspace)
- {
- this.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
- {
- if (ActiveDocumentWorkspace.History.UndoStack.Count > 1)
- {
- widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Undo, true);
- }
- else
- {
- widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Undo, false);
- }
-
- if (ActiveDocumentWorkspace.History.RedoStack.Count > 0)
- {
- widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Redo, true);
- }
- else
- {
- widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Redo, false);
- }
- }
- }
-
- 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 (this.activeDocumentWorkspace != null)
- {
- this.activeDocumentWorkspace.FirstInputAfterGotFocus +=
- ActiveDocumentWorkspace_FirstInputAfterGotFocus;
-
- this.activeDocumentWorkspace.RulersEnabledChanged -= this.DocumentWorkspace_RulersEnabledChanged;
- this.activeDocumentWorkspace.DocumentMouseEnter -= this.DocumentMouseEnterHandler;
- this.activeDocumentWorkspace.DocumentMouseLeave -= this.DocumentMouseLeaveHandler;
- this.activeDocumentWorkspace.DocumentMouseMove -= this.DocumentMouseMoveHandler;
- this.activeDocumentWorkspace.DocumentMouseDown -= this.DocumentMouseDownHandler;
- this.activeDocumentWorkspace.Scroll -= this.DocumentWorkspace_Scroll;
- this.activeDocumentWorkspace.Layout -= this.DocumentWorkspace_Layout;
- this.activeDocumentWorkspace.DrawGridChanged -= this.DocumentWorkspace_DrawGridChanged;
- this.activeDocumentWorkspace.DocumentClick -= this.DocumentClick;
- this.activeDocumentWorkspace.DocumentMouseUp -= this.DocumentMouseUpHandler;
- this.activeDocumentWorkspace.DocumentKeyPress -= this.DocumentKeyPress;
- this.activeDocumentWorkspace.DocumentKeyUp -= this.DocumenKeyUp;
- this.activeDocumentWorkspace.DocumentKeyDown -= this.DocumentKeyDown;
-
- this.activeDocumentWorkspace.History.Changed -= HistoryChangedHandler;
- this.activeDocumentWorkspace.StatusChanged -= OnDocumentWorkspaceStatusChanged;
- this.activeDocumentWorkspace.DocumentChanging -= DocumentWorkspace_DocumentChanging;
- this.activeDocumentWorkspace.DocumentChanged -= DocumentWorkspace_DocumentChanged;
- this.activeDocumentWorkspace.Selection.Changing -= SelectedPathChangingHandler;
- this.activeDocumentWorkspace.Selection.Changed -= SelectedPathChangedHandler;
- this.activeDocumentWorkspace.ScaleFactorChanged -= ZoomChangedHandler;
- this.activeDocumentWorkspace.ZoomBasisChanged -= DocumentWorkspace_ZoomBasisChanged;
-
- this.activeDocumentWorkspace.Visible = false;
- this.historyForm.HistoryControl.HistoryStack = null;
-
- this.activeDocumentWorkspace.ToolChanging -= this.ToolChangingHandler;
- this.activeDocumentWorkspace.ToolChanged -= this.ToolChangedHandler;
-
- if (this.activeDocumentWorkspace.Tool != null)
- {
- while (this.activeDocumentWorkspace.Tool.IsMouseEntered)
- {
- this.activeDocumentWorkspace.Tool.PerformMouseLeave();
- }
- }
-
- Type toolType = this.activeDocumentWorkspace.GetToolType();
-
- if (toolType != null)
- {
- this.globalToolTypeChoice = this.activeDocumentWorkspace.GetToolType();
- }
- }
-
- ResumeUpdateSnapObstacle();
- UpdateSnapObstacle();
- }
-
- public event EventHandler ActiveDocumentWorkspaceChanged;
- protected virtual void OnActiveDocumentWorkspaceChanged()
- {
- SuspendUpdateSnapObstacle();
-
- if (this.activeDocumentWorkspace == null)
- {
- this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Print, false);
- this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Save, false);
- }
- else
- {
- this.activeDocumentWorkspace.SuspendLayout();
-
- this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Print, true);
- this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Save, true);
-
- this.activeDocumentWorkspace.BackColor = System.Drawing.SystemColors.ControlDark;
- this.activeDocumentWorkspace.Dock = System.Windows.Forms.DockStyle.Fill;
- this.activeDocumentWorkspace.DrawGrid = this.DrawGrid;
- this.activeDocumentWorkspace.PanelAutoScroll = true;
- this.activeDocumentWorkspace.RulersEnabled = this.globalRulersChoice;
- this.activeDocumentWorkspace.TabIndex = 0;
- this.activeDocumentWorkspace.TabStop = false;
- this.activeDocumentWorkspace.RulersEnabledChanged += this.DocumentWorkspace_RulersEnabledChanged;
- this.activeDocumentWorkspace.DocumentMouseEnter += this.DocumentMouseEnterHandler;
- this.activeDocumentWorkspace.DocumentMouseLeave += this.DocumentMouseLeaveHandler;
- this.activeDocumentWorkspace.DocumentMouseMove += this.DocumentMouseMoveHandler;
- this.activeDocumentWorkspace.DocumentMouseDown += this.DocumentMouseDownHandler;
- this.activeDocumentWorkspace.Scroll += this.DocumentWorkspace_Scroll;
- this.activeDocumentWorkspace.DrawGridChanged += this.DocumentWorkspace_DrawGridChanged;
- this.activeDocumentWorkspace.DocumentClick += this.DocumentClick;
- this.activeDocumentWorkspace.DocumentMouseUp += this.DocumentMouseUpHandler;
- this.activeDocumentWorkspace.DocumentKeyPress += this.DocumentKeyPress;
- this.activeDocumentWorkspace.DocumentKeyUp += this.DocumenKeyUp;
- this.activeDocumentWorkspace.DocumentKeyDown += this.DocumentKeyDown;
-
- if (this.workspacePanel.Controls.Contains(this.activeDocumentWorkspace))
- {
- this.activeDocumentWorkspace.Visible = true;
- }
- else
- {
- this.activeDocumentWorkspace.Dock = DockStyle.Fill;
- this.workspacePanel.Controls.Add(this.activeDocumentWorkspace);
- }
-
- this.activeDocumentWorkspace.Layout += this.DocumentWorkspace_Layout;
- this.toolBar.ViewConfigStrip.ScaleFactor = this.activeDocumentWorkspace.ScaleFactor;
- this.toolBar.ViewConfigStrip.ZoomBasis = this.activeDocumentWorkspace.ZoomBasis;
-
- this.activeDocumentWorkspace.AppWorkspace = this;
- this.activeDocumentWorkspace.History.Changed += HistoryChangedHandler;
- this.activeDocumentWorkspace.StatusChanged += OnDocumentWorkspaceStatusChanged;
- this.activeDocumentWorkspace.DocumentChanging += DocumentWorkspace_DocumentChanging;
- this.activeDocumentWorkspace.DocumentChanged += DocumentWorkspace_DocumentChanged;
- this.activeDocumentWorkspace.Selection.Changing += SelectedPathChangingHandler;
- this.activeDocumentWorkspace.Selection.Changed += SelectedPathChangedHandler;
- this.activeDocumentWorkspace.ScaleFactorChanged += ZoomChangedHandler;
- this.activeDocumentWorkspace.ZoomBasisChanged += DocumentWorkspace_ZoomBasisChanged;
-
- this.activeDocumentWorkspace.Units = this.widgets.ViewConfigStrip.Units;
-
- this.historyForm.HistoryControl.HistoryStack = this.ActiveDocumentWorkspace.History;
-
- this.activeDocumentWorkspace.ToolChanging += this.ToolChangingHandler;
- this.activeDocumentWorkspace.ToolChanged += this.ToolChangedHandler;
-
- this.toolBar.ViewConfigStrip.RulersEnabled = this.activeDocumentWorkspace.RulersEnabled;
- this.toolBar.DocumentStrip.SelectDocumentWorkspace(this.activeDocumentWorkspace);
-
- this.activeDocumentWorkspace.SetToolFromType(this.globalToolTypeChoice);
-
- UpdateSelectionToolbarButtons();
- UpdateHistoryButtons();
- UpdateDocInfoInStatusBar();
-
- this.activeDocumentWorkspace.ResumeLayout();
- this.activeDocumentWorkspace.PerformLayout();
-
- this.activeDocumentWorkspace.FirstInputAfterGotFocus +=
- ActiveDocumentWorkspace_FirstInputAfterGotFocus;
- }
-
- if (ActiveDocumentWorkspaceChanged != null)
- {
- ActiveDocumentWorkspaceChanged(this, EventArgs.Empty);
- }
-
- UpdateStatusBarContextStatus();
- ResumeUpdateSnapObstacle();
- UpdateSnapObstacle();
- }
-
- public AppWorkspace()
- {
- SuspendLayout();
-
- // initialize!
- InitializeComponent();
- InitializeFloatingForms();
-
- this.mainToolBarForm.ToolsControl.SetTools(DocumentWorkspace.ToolInfos);
- this.mainToolBarForm.ToolsControl.ToolClicked += new ToolClickedEventHandler(this.MainToolBar_ToolClicked);
-
- this.toolBar.ToolChooserStrip.SetTools(DocumentWorkspace.ToolInfos);
- this.toolBar.ToolChooserStrip.ToolClicked += new ToolClickedEventHandler(this.MainToolBar_ToolClicked);
-
- this.toolBar.AppWorkspace = this;
-
- // init the Widgets container
- this.widgets = new WorkspaceWidgets(this);
- this.widgets.ViewConfigStrip = this.toolBar.ViewConfigStrip;
- this.widgets.CommonActionsStrip = this.toolBar.CommonActionsStrip;
- this.widgets.ToolConfigStrip = this.toolBar.ToolConfigStrip;
- this.widgets.ToolsForm = this.mainToolBarForm;
- this.widgets.LayerForm = this.layerForm;
- this.widgets.HistoryForm = this.historyForm;
- this.widgets.ColorsForm = this.colorsForm;
- this.widgets.StatusBarProgress = this.statusBar;
- this.widgets.DocumentStrip = this.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 = this.toolBar.ToolConfigStrip.FontInfo;
- AppEnvironment.TextAlignment = this.toolBar.ToolConfigStrip.FontAlignment;
- AppEnvironment.AntiAliasingChanged += Environment_AntiAliasingChanged;
- AppEnvironment.FontInfoChanged += Environment_FontInfoChanged;
- AppEnvironment.FontSmoothingChanged += Environment_FontSmoothingChanged;
- AppEnvironment.TextAlignmentChanged += Environment_TextAlignmentChanged;
- AppEnvironment.PenInfoChanged += Environment_PenInfoChanged;
- AppEnvironment.BrushInfoChanged += Environment_BrushInfoChanged;
- AppEnvironment.ColorPickerClickBehaviorChanged += Environment_ColorPickerClickBehaviorChanged;
- AppEnvironment.ResamplingAlgorithmChanged += Environment_ResamplingAlgorithmChanged;
- AppEnvironment.SelectionCombineModeChanged += Environment_SelectionCombineModeChanged;
- AppEnvironment.FloodModeChanged += Environment_FloodModeChanged;
- AppEnvironment.SelectionDrawModeInfoChanged += Environment_SelectionDrawModeInfoChanged;
-
- this.toolBar.DocumentStrip.RelinquishFocus += RelinquishFocusHandler;
-
- this.toolBar.ToolConfigStrip.ToleranceChanged += OnToolBarToleranceChanged;
- this.toolBar.ToolConfigStrip.FontAlignmentChanged += ToolConfigStrip_TextAlignmentChanged;
- this.toolBar.ToolConfigStrip.FontInfoChanged += ToolConfigStrip_FontTextChanged;
- this.toolBar.ToolConfigStrip.FontSmoothingChanged += ToolConfigStrip_FontSmoothingChanged;
- this.toolBar.ToolConfigStrip.RelinquishFocus += RelinquishFocusHandler2;
-
- this.toolBar.CommonActionsStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- this.toolBar.CommonActionsStrip.MouseWheel += OnToolStripMouseWheel;
- this.toolBar.CommonActionsStrip.ButtonClick += CommonActionsStrip_ButtonClick;
-
- this.toolBar.ViewConfigStrip.DrawGridChanged += ViewConfigStrip_DrawGridChanged;
- this.toolBar.ViewConfigStrip.RulersEnabledChanged += ViewConfigStrip_RulersEnabledChanged;
- this.toolBar.ViewConfigStrip.ZoomBasisChanged += ViewConfigStrip_ZoomBasisChanged;
- this.toolBar.ViewConfigStrip.ZoomScaleChanged += ViewConfigStrip_ZoomScaleChanged;
- this.toolBar.ViewConfigStrip.ZoomIn += ViewConfigStrip_ZoomIn;
- this.toolBar.ViewConfigStrip.ZoomOut += ViewConfigStrip_ZoomOut;
- this.toolBar.ViewConfigStrip.UnitsChanged += ViewConfigStrip_UnitsChanged;
- this.toolBar.ViewConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- this.toolBar.ViewConfigStrip.MouseWheel += OnToolStripMouseWheel;
-
- this.toolBar.ToolConfigStrip.BrushInfoChanged += DrawConfigStrip_BrushChanged;
- this.toolBar.ToolConfigStrip.ShapeDrawTypeChanged += DrawConfigStrip_ShapeDrawTypeChanged;
- this.toolBar.ToolConfigStrip.PenInfoChanged += DrawConfigStrip_PenChanged;
- this.toolBar.ToolConfigStrip.GradientInfoChanged += ToolConfigStrip_GradientInfoChanged;
- this.toolBar.ToolConfigStrip.AlphaBlendingChanged += OnDrawConfigStripAlphaBlendingChanged;
- this.toolBar.ToolConfigStrip.AntiAliasingChanged += DrawConfigStrip_AntiAliasingChanged;
- this.toolBar.ToolConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- this.toolBar.ToolConfigStrip.ColorPickerClickBehaviorChanged += ToolConfigStrip_ColorPickerClickBehaviorChanged;
- this.toolBar.ToolConfigStrip.ResamplingAlgorithmChanged += ToolConfigStrip_ResamplingAlgorithmChanged;
- this.toolBar.ToolConfigStrip.SelectionCombineModeChanged += ToolConfigStrip_SelectionCombineModeChanged;
- this.toolBar.ToolConfigStrip.FloodModeChanged += ToolConfigStrip_FloodModeChanged;
- this.toolBar.ToolConfigStrip.SelectionDrawModeInfoChanged += ToolConfigStrip_SelectionDrawModeInfoChanged;
- this.toolBar.ToolConfigStrip.SelectionDrawModeUnitsChanging += ToolConfigStrip_SelectionDrawModeUnitsChanging;
-
- this.toolBar.ToolConfigStrip.MouseWheel += OnToolStripMouseWheel;
-
- this.toolBar.DocumentStrip.RelinquishFocus += OnToolStripRelinquishFocus;
- this.toolBar.DocumentStrip.DocumentClicked += DocumentStrip_DocumentTabClicked;
- this.toolBar.DocumentStrip.DocumentListChanged += DocumentStrip_DocumentListChanged;
-
- // Synchronize
- AppEnvironment.PerformAllChanged();
-
- this.globalToolTypeChoice = this.defaultToolTypeChoice;
- this.toolBar.ToolConfigStrip.ToolBarConfigItems = ToolBarConfigItems.None;
-
- this.layerForm.LayerControl.AppWorkspace = this;
-
- ResumeLayout();
- PerformLayout();
- }
-
- private void ToolConfigStrip_ColorPickerClickBehaviorChanged(object sender, EventArgs e)
- {
- this.appEnvironment.ColorPickerClickBehavior = this.widgets.ToolConfigStrip.ColorPickerClickBehavior;
- }
-
- private void Environment_ColorPickerClickBehaviorChanged(object sender, EventArgs e)
- {
- this.widgets.ToolConfigStrip.ColorPickerClickBehavior = this.appEnvironment.ColorPickerClickBehavior;
- }
-
- private void ToolConfigStrip_ResamplingAlgorithmChanged(object sender, EventArgs e)
- {
- this.appEnvironment.ResamplingAlgorithm = this.widgets.ToolConfigStrip.ResamplingAlgorithm;
- }
-
- private void Environment_ResamplingAlgorithmChanged(object sender, EventArgs e)
- {
- this.widgets.ToolConfigStrip.ResamplingAlgorithm = this.appEnvironment.ResamplingAlgorithm;
- }
-
- private void ToolConfigStrip_SelectionCombineModeChanged(object sender, EventArgs e)
- {
- this.appEnvironment.SelectionCombineMode = this.widgets.ToolConfigStrip.SelectionCombineMode;
- }
-
- private void Environment_SelectionCombineModeChanged(object sender, EventArgs e)
- {
- this.widgets.ToolConfigStrip.SelectionCombineMode = this.appEnvironment.SelectionCombineMode;
- }
-
- private void ToolConfigStrip_FloodModeChanged(object sender, EventArgs e)
- {
- this.appEnvironment.FloodMode = this.widgets.ToolConfigStrip.FloodMode;
- }
-
- private void Environment_FloodModeChanged(object sender, EventArgs e)
- {
- this.widgets.ToolConfigStrip.FloodMode = this.appEnvironment.FloodMode;
- }
-
- private void ToolConfigStrip_SelectionDrawModeInfoChanged(object sender, EventArgs e)
- {
- this.appEnvironment.SelectionDrawModeInfo = this.widgets.ToolConfigStrip.SelectionDrawModeInfo;
- }
-
- private void Environment_SelectionDrawModeInfoChanged(object sender, EventArgs e)
- {
- this.widgets.ToolConfigStrip.SelectionDrawModeInfo = this.appEnvironment.SelectionDrawModeInfo;
- }
-
- private sealed class ToolConfigStrip_SelectionDrawModeUnitsChangeHandler
- {
- private ToolConfigStrip toolConfigStrip;
- private Document activeDocument;
- private MeasurementUnit oldUnits;
-
- public ToolConfigStrip_SelectionDrawModeUnitsChangeHandler(ToolConfigStrip toolConfigStrip, Document activeDocument)
- {
- this.toolConfigStrip = toolConfigStrip;
- this.activeDocument = activeDocument;
- this.oldUnits = toolConfigStrip.SelectionDrawModeInfo.Units;
- }
-
- public void Initialize()
- {
- this.toolConfigStrip.SelectionDrawModeUnitsChanged += ToolConfigStrip_SelectionDrawModeUnitsChanged;
- }
-
- public void ToolConfigStrip_SelectionDrawModeUnitsChanged(object sender, EventArgs e)
- {
- try
- {
- SelectionDrawModeInfo sdmi = this.toolConfigStrip.SelectionDrawModeInfo;
- MeasurementUnit newUnits = sdmi.Units;
-
- double oldWidth = sdmi.Width;
- double oldHeight = sdmi.Height;
-
- double newWidth;
- double newHeight;
-
- newWidth = Document.ConvertMeasurement(oldWidth, this.oldUnits, this.activeDocument.DpuUnit, this.activeDocument.DpuX, newUnits);
- newHeight = Document.ConvertMeasurement(oldHeight, this.oldUnits, this.activeDocument.DpuUnit, this.activeDocument.DpuY, newUnits);
-
- SelectionDrawModeInfo newSdmi = sdmi.CloneWithNewWidthAndHeight(newWidth, newHeight);
- this.toolConfigStrip.SelectionDrawModeInfo = newSdmi;
- }
-
- finally
- {
- this.toolConfigStrip.SelectionDrawModeUnitsChanged -= ToolConfigStrip_SelectionDrawModeUnitsChanged;
- }
- }
- }
-
- private void ToolConfigStrip_SelectionDrawModeUnitsChanging(object sender, EventArgs e)
- {
- if (this.ActiveDocumentWorkspace != null && this.ActiveDocumentWorkspace.Document != null)
- {
- ToolConfigStrip_SelectionDrawModeUnitsChangeHandler tcsSdmuch = new ToolConfigStrip_SelectionDrawModeUnitsChangeHandler(
- this.toolBar.ToolConfigStrip, this.ActiveDocumentWorkspace.Document);
-
- tcsSdmuch.Initialize();
- }
- }
-
- private void DocumentStrip_DocumentListChanged(object sender, EventArgs e)
- {
- bool enableThem = (this.widgets.DocumentStrip.DocumentCount != 0);
-
- this.widgets.ToolsForm.Enabled = enableThem;
- this.widgets.HistoryForm.Enabled = enableThem;
- this.widgets.LayerForm.Enabled = enableThem;
- this.widgets.ColorsForm.Enabled = enableThem;
- this.widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Paste, enableThem);
-
- UpdateHistoryButtons();
- UpdateDocInfoInStatusBar();
- UpdateCursorInfoInStatusBar(0, 0);
- }
-
- public void SaveSettings()
- {
- Settings.CurrentUser.SetBoolean(SettingNames.Rulers, this.globalRulersChoice);
- Settings.CurrentUser.SetBoolean(SettingNames.DrawGrid, this.DrawGrid);
- Settings.CurrentUser.SetString(SettingNames.DefaultToolTypeName, this.defaultToolTypeChoice.Name);
- this.MostRecentFiles.SaveMruList();
- }
-
- private void LoadDefaultToolType()
- {
- string defaultToolTypeName = Settings.CurrentUser.GetString(SettingNames.DefaultToolTypeName, Tool.DefaultToolType.Name);
-
- ToolInfo[] tis = DocumentWorkspace.ToolInfos;
- ToolInfo ti = Array.Find(
- tis,
- delegate(ToolInfo check)
- {
- if (string.Compare(defaultToolTypeName, check.ToolType.Name, StringComparison.InvariantCultureIgnoreCase) == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- });
-
- if (ti == null)
- {
- this.defaultToolTypeChoice = Tool.DefaultToolType;
- }
- else
- {
- this.defaultToolTypeChoice = ti.ToolType;
- }
- }
-
- public void LoadSettings()
- {
- try
- {
- LoadDefaultToolType();
-
- this.globalToolTypeChoice = this.defaultToolTypeChoice;
- this.globalRulersChoice = Settings.CurrentUser.GetBoolean(SettingNames.Rule…
Large files files are truncated, but you can click here to view the full file