/src/Menus/FileMenu.cs
C# | 509 lines | 373 code | 65 blank | 71 comment | 19 complexity | 1949eba409de5de3be79de6d741c3fea 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.SystemLayer;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using System.Windows.Forms;
-
- namespace PaintDotNet.Menus
- {
- internal sealed class FileMenu
- : PdnMenuItem
- {
- private PdnMenuItem _menuFileNew;
- private PdnMenuItem _menuFileOpen;
- private PdnMenuItem _menuFileOpenRecent;
- private PdnMenuItem _menuFileOpenRecentSentinel;
- private PdnMenuItem _menuFileAcquire;
- private PdnMenuItem _menuFileAcquireFromScannerOrCamera;
- private PdnMenuItem _menuFileClose;
- private ToolStripSeparator _menuFileSeparator1;
- private PdnMenuItem _menuFileSave;
- private PdnMenuItem _menuFileSaveAs;
- private ToolStripSeparator _menuFileSeparator2;
- private PdnMenuItem _menuFilePrint;
- private ToolStripSeparator _menuFileSeparator3;
- private PdnMenuItem _menuFileViewPluginLoadErrors;
- private ToolStripSeparator _menuFileSeparator4;
- private PdnMenuItem _menuFileExit;
-
- private bool OnCtrlF4Typed(Keys keys)
- {
- _menuFileClose.PerformClick();
- return true;
- }
-
- public FileMenu()
- {
- PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.F4, OnCtrlF4Typed);
- InitializeComponent();
- }
-
- private void InitializeComponent()
- {
- _menuFileNew = new PdnMenuItem();
- _menuFileOpen = new PdnMenuItem();
- _menuFileOpenRecent = new PdnMenuItem();
- _menuFileOpenRecentSentinel = new PdnMenuItem();
- _menuFileAcquire = new PdnMenuItem();
- _menuFileAcquireFromScannerOrCamera = new PdnMenuItem();
- _menuFileClose = new PdnMenuItem();
- _menuFileSeparator1 = new ToolStripSeparator();
- _menuFileSave = new PdnMenuItem();
- _menuFileSaveAs = new PdnMenuItem();
- _menuFileSeparator2 = new ToolStripSeparator();
- _menuFilePrint = new PdnMenuItem();
- _menuFileSeparator3 = new ToolStripSeparator();
- _menuFileViewPluginLoadErrors = new PdnMenuItem();
- _menuFileSeparator4 = new ToolStripSeparator();
- _menuFileExit = new PdnMenuItem();
- //
- // FileMenu
- //
- DropDownItems.AddRange(GetMenuItemsToAdd(true));
- Name = "Menu.File";
- Text = PdnResources.GetString("Menu.File.Text");
- //
- // menuFileNew
- //
- _menuFileNew.Name = "New";
- _menuFileNew.ShortcutKeys = Keys.Control | Keys.N;
- _menuFileNew.Click += MenuFileNewClick;
- //
- // menuFileOpen
- //
- _menuFileOpen.Name = "Open";
- _menuFileOpen.ShortcutKeys = Keys.Control | Keys.O;
- _menuFileOpen.Click += MenuFileOpenClick;
- //
- // menuFileOpenRecent
- //
- _menuFileOpenRecent.Name = "OpenRecent";
- _menuFileOpenRecent.DropDownItems.AddRange(
- new ToolStripItem[]
- {
- _menuFileOpenRecentSentinel
- });
- _menuFileOpenRecent.DropDownOpening += MenuFileOpenRecentDropDownOpening;
- //
- // menuFileOpenRecentSentinel
- //
- _menuFileOpenRecentSentinel.Text = "sentinel";
- //
- // menuFileAcquire
- //
- _menuFileAcquire.Name = "Acquire";
- _menuFileAcquire.DropDownItems.AddRange(
- new ToolStripItem[]
- {
- _menuFileAcquireFromScannerOrCamera
- });
- _menuFileAcquire.DropDownOpening += MenuFileAcquireDropDownOpening;
- //
- // menuFileAcquireFromScannerOrCamera
- //
- _menuFileAcquireFromScannerOrCamera.Name = "FromScannerOrCamera";
- _menuFileAcquireFromScannerOrCamera.Click += MenuFileAcquireFromScannerOrCameraClick;
- //
- // menuFileClose
- //
- _menuFileClose.Name = "Close";
- _menuFileClose.Click += MenuFileCloseClick;
- _menuFileClose.ShortcutKeys = Keys.Control | Keys.W;
- //
- // menuFileSave
- //
- _menuFileSave.Name = "Save";
- _menuFileSave.ShortcutKeys = Keys.Control | Keys.S;
- _menuFileSave.Click += MenuFileSaveClick;
- //
- // menuFileSaveAs
- //
- _menuFileSaveAs.Name = "SaveAs";
- _menuFileSaveAs.ShortcutKeys = Keys.Control | Keys.Shift | Keys.S;
- _menuFileSaveAs.Click += MenuFileSaveAsClick;
- //
- // menuFilePrint
- //
- _menuFilePrint.Name = "Print";
- _menuFilePrint.ShortcutKeys = Keys.Control | Keys.P;
- _menuFilePrint.Click += MenuFilePrintClick;
- //
- // menuFileViewPluginLoadErrors
- //
- _menuFileViewPluginLoadErrors.Name = "ViewPluginLoadErrors";
- _menuFileViewPluginLoadErrors.Click += MenuFileViewPluginLoadErrorsClick;
- //
- // menuFileExit
- //
- _menuFileExit.Name = "Exit";
- _menuFileExit.Click += MenuFileExitClick;
- }
-
- private ToolStripItem[] GetMenuItemsToAdd(bool includeLoadErrors)
- {
- var items = new List<ToolStripItem>
- {
- _menuFileNew,
- _menuFileOpen,
- _menuFileOpenRecent,
- _menuFileAcquire,
- _menuFileClose,
- _menuFileSeparator1,
- _menuFileSave,
- _menuFileSaveAs,
- _menuFileSeparator2,
- _menuFilePrint,
- _menuFileSeparator3
- };
-
- if (includeLoadErrors)
- {
- items.Add(_menuFileViewPluginLoadErrors);
- items.Add(_menuFileSeparator4);
- }
-
- items.Add(_menuFileExit);
-
- return items.ToArray();
- }
-
- private static List<Triple<Assembly, Type, Exception>> RemoveDuplicates(IEnumerable<Triple<Assembly, Type, Exception>> allErrors)
- {
- // Exception has reference identity, but we want to collate based on the message contents
-
- var internedList = new Set<Triple<Assembly, Type, string>>();
- var noDupesList = new List<Triple<Assembly, Type, Exception>>();
-
- foreach (Triple<Assembly, Type, Exception> t in allErrors)
- {
- Triple<Assembly, Type, string> interned = Triple.Create(
- t.First, t.Second, string.Intern(t.Third.ToString()));
-
- if (internedList.Contains(interned)) continue;
- internedList.Add(interned);
- noDupesList.Add(t);
- }
-
- return noDupesList;
- }
-
- private void MenuFileViewPluginLoadErrorsClick(object sender, EventArgs e)
- {
- IList<Triple<Assembly, Type, Exception>> allErrors = AppWorkspace.GetEffectLoadErrors();
- IList<Triple<Assembly, Type, Exception>> errors = RemoveDuplicates(allErrors);
-
- using (var errorsDialog = new Form())
- {
- errorsDialog.Icon = Utility.ImageToIcon(PdnResources.GetImageResource("Icons.MenuFileViewPluginLoadErrorsIcon.png").Reference);
- errorsDialog.Text = PdnResources.GetString("Effects.PluginLoadErrorsDialog.Text");
-
- var messageLabel = new Label
- {
- Name = "messageLabel",
- Text = PdnResources.GetString("Effects.PluginLoadErrorsDialog.Message.Text")
- };
-
- var errorsBox = new TextBox();
- errorsBox.Font = new Font(FontFamily.GenericMonospace, errorsBox.Font.Size);
- errorsBox.ReadOnly = true;
- errorsBox.Multiline = true;
- errorsBox.ScrollBars = ScrollBars.Vertical;
-
- var allErrorsText = new StringBuilder();
- string headerTextFormat = PdnResources.GetString("EffectErrorMessage.HeaderFormat");
-
- for (int i = 0; i < errors.Count; ++i)
- {
- Assembly assembly = errors[i].First;
- Type type = errors[i].Second;
- Exception exception = errors[i].Third;
-
- string headerText = string.Format(headerTextFormat, i + 1, errors.Count);
- string errorText = AppWorkspace.GetLocalizedEffectErrorMessage(assembly, type, exception);
-
- allErrorsText.Append(headerText);
- allErrorsText.Append(Environment.NewLine);
- allErrorsText.Append(errorText);
-
- if (i != errors.Count - 1)
- {
- allErrorsText.Append(Environment.NewLine);
- }
- }
-
- errorsBox.Text = allErrorsText.ToString();
-
- errorsDialog.Layout +=
- delegate
- {
- int hMargin = UI.ScaleWidth(8);
- int vMargin = UI.ScaleHeight(8);
- int insetWidth = errorsDialog.ClientSize.Width - (hMargin * 2);
-
- messageLabel.Location = new Point(hMargin, vMargin);
- messageLabel.Width = insetWidth;
- messageLabel.Size = messageLabel.GetPreferredSize(new Size(messageLabel.Width, 1));
-
- errorsBox.Location = new Point(hMargin, messageLabel.Bottom + vMargin);
- errorsBox.Width = insetWidth;
- errorsBox.Height = errorsDialog.ClientSize.Height - vMargin - errorsBox.Top;
- };
-
- errorsDialog.StartPosition = FormStartPosition.CenterParent;
- errorsDialog.ShowInTaskbar = false;
- errorsDialog.MinimizeBox = false;
- errorsDialog.Width *= 2;
- errorsDialog.Size = UI.ScaleSize(errorsDialog.Size);
- errorsDialog.Controls.Add(messageLabel);
- errorsDialog.Controls.Add(errorsBox);
-
- errorsDialog.ShowDialog(AppWorkspace);
- }
- }
-
- protected override void OnDropDownOpening(EventArgs e)
- {
- DropDownItems.Clear();
-
- IList<Triple<Assembly, Type, Exception>> pluginLoadErrors = AppWorkspace.GetEffectLoadErrors();
-
- DropDownItems.AddRange(GetMenuItemsToAdd(pluginLoadErrors.Count > 0));
-
- _menuFileNew.Enabled = true;
- _menuFileOpen.Enabled = true;
- _menuFileOpenRecent.Enabled = true;
- _menuFileOpenRecentSentinel.Enabled = true;
- _menuFileAcquire.Enabled = true;
- _menuFileAcquireFromScannerOrCamera.Enabled = true;
- _menuFileExit.Enabled = true;
-
- if (AppWorkspace.ActiveDocumentWorkspace != null)
- {
- _menuFileSave.Enabled = true;
- _menuFileSaveAs.Enabled = true;
- _menuFileClose.Enabled = true;
- _menuFilePrint.Enabled = true;
- }
- else
- {
- _menuFileSave.Enabled = false;
- _menuFileSaveAs.Enabled = false;
- _menuFileClose.Enabled = false;
- _menuFilePrint.Enabled = false;
- }
-
- base.OnDropDownOpening(e);
- }
-
- private void MenuFileOpenClick(object sender, EventArgs e)
- {
- AppWorkspace.PerformAction(new OpenFileAction());
- }
-
- private static void DoExit()
- {
- Startup.CloseApplication();
- }
-
- private static void MenuFileExitClick(object sender, EventArgs e)
- {
- DoExit();
- }
-
- private void MenuFileCloseClick(object sender, EventArgs e)
- {
- if (AppWorkspace.DocumentWorkspaces.Length > 0)
- {
- AppWorkspace.PerformAction(new CloseWorkspaceAction());
- }
- else
- {
- DoExit();
- }
- }
-
- private void MenuFileSaveAsClick(object sender, EventArgs e)
- {
- if (AppWorkspace.ActiveDocumentWorkspace != null)
- {
- AppWorkspace.ActiveDocumentWorkspace.DoSaveAs();
- }
- }
-
- private void MenuFileSaveClick(object sender, EventArgs e)
- {
- if (AppWorkspace.ActiveDocumentWorkspace != null)
- {
- AppWorkspace.ActiveDocumentWorkspace.DoSave();
- }
- }
-
- private void MenuFileAcquireDropDownOpening(object sender, EventArgs e)
- {
- // We only disable the scanner menu item if we know for sure a scanner is not available
- // If WIA isn't available we leave the menu item enabled. That way we can give an
- // informative error message when the user clicks on it and say "scanning requires XP SP1"
- // Otherwise the user is confused and will make scathing posts on our forum.
- bool scannerEnabled = true;
-
- if (ScanningAndPrinting.IsComponentAvailable)
- {
- if (!ScanningAndPrinting.CanScan)
- {
- scannerEnabled = false;
- }
- }
-
- _menuFileAcquireFromScannerOrCamera.Enabled = scannerEnabled;
- }
-
- private void MenuFilePrintClick(object sender, EventArgs e)
- {
- if (AppWorkspace.ActiveDocumentWorkspace != null)
- {
- AppWorkspace.ActiveDocumentWorkspace.PerformAction(new PrintAction());
- }
- }
-
- /*
- private void MenuFileOpenInNewWindow_Click(object sender, System.EventArgs e)
- {
- string fileName;
- string startingDir = Path.GetDirectoryName(AppWorkspace.ActiveDocumentWorkspace.FilePath);
- DialogResult result = DocumentWorkspace.ChooseFile(AppWorkspace, out fileName, startingDir);
-
- if (result == DialogResult.OK)
- {
- Startup.StartNewInstance(AppWorkspace, fileName);
- }
- }
- */ //SUSPECTED UNUSED METHOD
-
- /*
- private void MenuFileNewWindow_Click(object sender, System.EventArgs e)
- {
- Startup.StartNewInstance(AppWorkspace, null);
- }
- */ //SUSPECTED UNUSED METHOD
-
- private void MenuFileOpenRecentDropDownOpening(object sender, EventArgs e)
- {
- AppWorkspace.MostRecentFiles.LoadMruList();
- MostRecentFile[] filesReverse = AppWorkspace.MostRecentFiles.GetFileList();
- var files = new MostRecentFile[filesReverse.Length];
- int i;
-
- for (i = 0; i < filesReverse.Length; ++i)
- {
- files[files.Length - i - 1] = filesReverse[i];
- }
-
- foreach (ToolStripItem mi in _menuFileOpenRecent.DropDownItems)
- {
- mi.Click -= MenuFileOpenRecentFileClick;
- }
-
- _menuFileOpenRecent.DropDownItems.Clear();
-
- i = 0;
-
- foreach (MostRecentFile mrf in files)
- {
- string menuName = i < 9 ? "&" : "";
-
- menuName += (1 + i) + " " + Path.GetFileName(mrf.FileName);
- var mi = new ToolStripMenuItem(menuName);
- mi.Click += MenuFileOpenRecentFileClick;
- mi.ImageScaling = ToolStripItemImageScaling.None;
- mi.Image = (Image)mrf.Thumb.Clone();
- _menuFileOpenRecent.DropDownItems.Add(mi);
- ++i;
- }
-
- if (_menuFileOpenRecent.DropDownItems.Count == 0)
- {
- var none = new ToolStripMenuItem(PdnResources.GetString("Menu.File.OpenRecent.None")) {Enabled = false};
- _menuFileOpenRecent.DropDownItems.Add(none);
- }
- else
- {
- var separator = new ToolStripSeparator();
- _menuFileOpenRecent.DropDownItems.Add(separator);
-
- var clearList = new ToolStripMenuItem
- {
- Text = PdnResources.GetString("Menu.File.OpenRecent.ClearThisList")
- };
- _menuFileOpenRecent.DropDownItems.Add(clearList);
- Image deleteIcon = PdnResources.GetImageResource("Icons.MenuEditEraseSelectionIcon.png").Reference;
- clearList.ImageTransparentColor = Utility.TransparentKey;
- clearList.ImageAlign = ContentAlignment.MiddleCenter;
- clearList.ImageScaling = ToolStripItemImageScaling.None;
- int iconSize = AppWorkspace.MostRecentFiles.IconSize;
- var bitmap = new Bitmap(iconSize + 2, iconSize + 2);
-
- using (Graphics g = Graphics.FromImage(bitmap))
- {
- g.Clear(clearList.ImageTransparentColor);
-
- var offset = new Point((bitmap.Width - deleteIcon.Width) / 2,
- (bitmap.Height - deleteIcon.Height) / 2);
-
- g.CompositingMode = CompositingMode.SourceCopy;
- g.DrawImage(deleteIcon, offset.X, offset.Y, deleteIcon.Width, deleteIcon.Height);
- }
-
- clearList.Image = bitmap;
- clearList.Click += ClearListClick;
- }
- }
-
- private void MenuFileOpenRecentFileClick(object sender, EventArgs e)
- {
- try
- {
- var mi = (ToolStripMenuItem)sender;
- int spaceIndex = mi.Text.IndexOf(" ");
- string indexString = mi.Text.Substring(1, spaceIndex - 1);
- int index = int.Parse(indexString) - 1;
- MostRecentFile[] recentFiles = AppWorkspace.MostRecentFiles.GetFileList();
- string fileName = recentFiles[recentFiles.Length - index - 1].FileName;
- AppWorkspace.OpenFileInNewWorkspace(fileName);
- }
-
- catch (Exception)
- {
- }
- }
-
- private void MenuFileNewClick(object sender, EventArgs e)
- {
- AppWorkspace.PerformAction(new NewImageAction());
- }
-
- private void MenuFileAcquireFromScannerOrCameraClick(object sender, EventArgs e)
- {
- AppWorkspace.PerformAction(new AcquireFromScannerOrCameraAction());
- }
-
- private void ClearListClick(object sender, EventArgs e)
- {
- AppWorkspace.PerformAction(new ClearMruListAction());
- }
- }
- }