using PaintDotNet.Actions;
using PaintDotNet.Base;
using PaintDotNet.Data.Param;
using PaintDotNet.File;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace PaintDotNet.Menus
{
///
/// 文件菜单
///
internal sealed class FileMenu : PdnMenuItem
{
private PdnMenuItem menuFileOpen;
private PdnMenuItem menuFileSave;
private PdnMenuItem menuFileSaveAs;
private PdnMenuItem menuFileBulkSave;
private PdnMenuItem menuFileSaveAll;
private ToolStripSeparator menuFileSeparator1;
private PdnMenuItem menuFileForceClose;
private PdnMenuItem menuFileClearAll;
private ToolStripSeparator menuFileSeparator2;
private PdnMenuItem menuFileOpenRecent;
private PdnMenuItem menuFileOpenRecentSentinel;
private ToolStripSeparator menuFileSeparator3;
private PdnMenuItem menuFileClose;
private PdnMenuItem menuFileCloseAll;
private bool OnCtrlF4Typed(Keys keys)
{
this.menuFileClose.PerformClick();
return true;
}
public FileMenu(int menuId)
{
PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.F4, OnCtrlF4Typed);
InitializeComponent();
this.MenuId = menuId;
}
private void InitializeComponent()
{
this.menuFileOpen = new PdnMenuItem(ActionType.Open);
this.menuFileOpen.NeedWaitKey = true;
this.menuFileOpen.AutoNextScript = false;
this.menuFileOpen.AutomaticScript = false;
this.menuFileSave = new PdnMenuItem(ActionType.Save);
this.menuFileSave.AutomaticScript = false;
this.menuFileSaveAs = new PdnMenuItem(ActionType.SaveAs);
this.menuFileSaveAs.AutomaticScript = false;
this.menuFileBulkSave = new PdnMenuItem(ActionType.BulkSave);
this.menuFileBulkSave.AutomaticScript = false;
this.menuFileSaveAll = new PdnMenuItem(ActionType.SaveAll);
this.menuFileSaveAll.AutomaticScript = false;
this.menuFileSeparator1 = new ToolStripSeparator();
this.menuFileForceClose = new PdnMenuItem(ActionType.ForceClose);
this.menuFileClearAll = new PdnMenuItem(ActionType.ClearAll);
this.menuFileSeparator2 = new ToolStripSeparator();
this.menuFileOpenRecent = new PdnMenuItem(ActionType.OpenRecent);
this.menuFileOpenRecent.CanUseInScript = false;
this.menuFileOpenRecent.AutomaticScript = false;
this.menuFileOpenRecentSentinel = new PdnMenuItem();
this.menuFileSeparator3 = new ToolStripSeparator();
this.menuFileClose = new PdnMenuItem(ActionType.Close);
this.menuFileCloseAll = new PdnMenuItem(ActionType.CloseAll);
//
// FileMenu
//
this.DropDownItems.AddRange(GetMenuItemsToAdd());
this.Name = "Menu.File";
this.Text = PdnResources.GetString("Menu.File.Text");
//
// menuFileOpen
//
this.menuFileOpen.Click += new EventHandler(this.MenuFileOpen_Click);
//
// menuFileSave
//
this.menuFileSave.Click += new EventHandler(this.MenuFileSave_Click);
//
// menuFileSaveAs
//
this.menuFileSaveAs.Click += new EventHandler(this.MenuFileSaveAs_Click);
//
// 批量保存
//
this.menuFileBulkSave.Click += new EventHandler(this.MenuFileBulkSave_Click);
//
// 保存全部
//
this.menuFileSaveAll.Click += new EventHandler(this.MenuFileSaveAll_Click);
//
// 强制关闭
//
this.menuFileForceClose.Click += new EventHandler(this.MenuFileForceClose_Click);
//
// 清空全部
//
this.menuFileClearAll.Click += new EventHandler(this.MenuFileClearAll_Click);
//
// 最近打开的文件
//
this.menuFileOpenRecent.Name = "OpenRecent";
this.menuFileOpenRecent.DropDownItems.AddRange(
new ToolStripItem[]
{
this.menuFileOpenRecentSentinel
});
this.menuFileOpenRecent.DropDownOpening += new EventHandler(this.MenuFileOpenRecent_DropDownOpening);
//
// menuFileOpenRecentSentinel
//
this.menuFileOpenRecentSentinel.Text = "sentinel";
//
// 关闭
//
this.menuFileClose.Click += new EventHandler(MenuFileClose_Click);
//
// 全部关闭
//
this.menuFileCloseAll.Click += new EventHandler(MenuFileCloseAll_Click);
//
// 加载菜单的文字和icon
//
this.LoadNames(this.Name);
this.LoadIcons();
}
private ToolStripItem[] GetMenuItemsToAdd()
{
List items = new List();
items.Add(this.menuFileOpen);
items.Add(this.menuFileSave);
items.Add(this.menuFileSaveAs);
items.Add(this.menuFileBulkSave);
items.Add(this.menuFileSaveAll);
items.Add(this.menuFileSeparator1);
items.Add(this.menuFileForceClose);
items.Add(this.menuFileClearAll);
items.Add(this.menuFileSeparator2);
items.Add(this.menuFileOpenRecent);
items.Add(this.menuFileSeparator3);
items.Add(this.menuFileClose);
items.Add(this.menuFileCloseAll);
return items.ToArray();
}
protected override void OnDropDownOpening(EventArgs e)
{
this.DropDownItems.Clear();
this.DropDownItems.AddRange(GetMenuItemsToAdd());
this.menuFileOpen.Enabled = true/* && !AppWorkspace.ScriptRunning*/;
this.menuFileOpenRecent.Enabled = true/* && !AppWorkspace.ScriptRunning*/;
this.menuFileOpenRecentSentinel.Enabled = true/* && !AppWorkspace.ScriptRunning*/;
if (AppWorkspace.ActiveDocumentWorkspace != null/* && !AppWorkspace.ScriptRunning*/)
{
this.menuFileForceClose.Enabled = true;
this.menuFileClearAll.Enabled = true;
this.menuFileSave.Enabled = true;
this.menuFileSaveAs.Enabled = true;
this.menuFileBulkSave.Enabled = true;
this.menuFileSaveAll.Enabled = true;
this.menuFileClose.Enabled = true;
this.menuFileCloseAll.Enabled = true;
}
else
{
this.menuFileForceClose.Enabled = false;
this.menuFileClearAll.Enabled = false;
this.menuFileSave.Enabled = false;
this.menuFileSaveAs.Enabled = false;
this.menuFileBulkSave.Enabled = false;
this.menuFileSaveAll.Enabled = false;
this.menuFileClose.Enabled = false;
this.menuFileCloseAll.Enabled = false;
}
base.OnDropDownOpening(e);
}
///
/// 打开
///
///
///
private void MenuFileOpen_Click(object sender, EventArgs e)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
AppWorkspace.PerformAction(new OpenFileAction());
}
///
/// 保存
///
///
///
private void MenuFileSave_Click(object sender, EventArgs e)
{
if (AppWorkspace.ActiveDocumentWorkspace != null)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
AppWorkspace.ActiveDocumentWorkspace.DoSaveNew();
}
}
///
/// 另存为
///
///
///
private void MenuFileSaveAs_Click(object sender, EventArgs e)
{
if (AppWorkspace.ActiveDocumentWorkspace != null)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
AppWorkspace.ActiveDocumentWorkspace.DoSaveAsNew(-1);
}
}
///
/// 批量保存
///
///
///
private void MenuFileBulkSave_Click(object sender, EventArgs e)
{
using (BatchSaveDialog dialog = new BatchSaveDialog(AppWorkspace))
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
dialog.StartPosition = FormStartPosition.CenterScreen;
dialog.ShowDialog();
}
}
///
/// 保存全部
///
///
///
private void MenuFileSaveAll_Click(object sender, EventArgs e)
{
if (this.AppWorkspace != null && this.AppWorkspace.DocumentWorkspaces!=null)
{
foreach(DocumentWorkspace documentWorkspace in this.AppWorkspace.DocumentWorkspaces)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
this.AppWorkspace.ActiveDocumentWorkspace = documentWorkspace;
documentWorkspace.DoSaveNew();
}
}
}
///
/// 强制关闭
///
///
///
private void MenuFileForceClose_Click(object sender, EventArgs e)
{
if (this.AppWorkspace.DocumentWorkspaces.Length > 0)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
this.AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
}
}
///
/// 清空全部
///
///
///
private void MenuFileClearAll_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show(PdnResources.GetString("Menu.file.Emptyall.Areyousuodifiedpicture.text")+"!", PdnResources.GetString("Menu.Empty.text"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
if (this.AppWorkspace.DocumentWorkspaces!=null && this.AppWorkspace.DocumentWorkspaces.Length > 0)
{
foreach (DocumentWorkspace document in this.AppWorkspace.DocumentWorkspaces)
{
this.AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
this.AppWorkspace.PerformAction(new CloseWorkspaceAction()); ;
}
}
}
}
///
/// 关闭
///
///
///
private void MenuFileClose_Click(object sender, EventArgs e)
{
if (this.AppWorkspace.DocumentWorkspaces.Length > 0)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
}
}
///
/// 全部关闭
///
///
///
private void MenuFileCloseAll_Click(object sender, EventArgs e)
{
if (AppWorkspace.DocumentWorkspaces != null
&& AppWorkspace.DocumentWorkspaces.Length > 0)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
foreach (DocumentWorkspace document in AppWorkspace.DocumentWorkspaces)
{
this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
}
}
}
private void MenuFileOpenRecent_DropDownOpening(object sender, EventArgs e)
{
if (AppWorkspace.startScriptRecording)
{
AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List());
}
AppWorkspace.MostRecentFiles.LoadMruList();
MostRecentFile[] filesReverse = AppWorkspace.MostRecentFiles.GetFileList();
MostRecentFile[] 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 -= new EventHandler(MenuFileOpenRecentFile_Click);
}
menuFileOpenRecent.DropDownItems.Clear();
i = 0;
Image choise = PdnResources.GetImageResource("Icons.SwapIcon.png").Reference;
Image nochoise = PdnResources.GetImageResource("Icons.SwatchIcon.png").Reference;
foreach (MostRecentFile mrf in files)
{
string menuName;
if (i < 9)
{
menuName = "&";
}
else
{
menuName = "";
}
menuName += (1 + i).ToString() + " " + Path.GetFileName(mrf.FileName);
ToolStripMenuItem mi = new ToolStripMenuItem(menuName);
mi.Alignment = ToolStripItemAlignment.Right;
mi.Click += new EventHandler(MenuFileOpenRecentFile_Click);
mi.ImageAlign = ContentAlignment.MiddleRight;
mi.ImageScaling = ToolStripItemImageScaling.None;
mi.Image = nochoise; //(Image)mrf.Thumb.Clone();
menuFileOpenRecent.DropDownItems.Add(mi);
++i;
}
if (menuFileOpenRecent.DropDownItems.Count == 0)
{
ToolStripMenuItem none = new ToolStripMenuItem(PdnResources.GetString("Menu.File.OpenRecent.None"));
none.Enabled = false;
menuFileOpenRecent.DropDownItems.Add(none);
}
else
{
ToolStripSeparator separator = new ToolStripSeparator();
menuFileOpenRecent.DropDownItems.Add(separator);
ToolStripMenuItem clearList = new ToolStripMenuItem();
clearList.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;
Bitmap bitmap = new Bitmap(iconSize + 2, iconSize + 2);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.Clear(clearList.ImageTransparentColor);
Point 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.Image = deleteIcon;
clearList.Click += new EventHandler(ClearList_Click);
}
}
private void MenuFileOpenRecentFile_Click(object sender, EventArgs e)
{
try
{
ToolStripMenuItem 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 ClearList_Click(object sender, EventArgs e)
{
string question = PdnResources.GetString("ClearOpenRecentList.Dialog.Text");
DialogResult result = Utility.AskYesNo(AppWorkspace, question);
if (result == DialogResult.Yes)
{
AppWorkspace.MostRecentFiles.Clear();
AppWorkspace.MostRecentFiles.SaveMruList();
}
//AppWorkspace.PerformAction(new ClearMruListAction());
}
}
}