123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- 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
- {
- /// <summary>
- /// 文件菜单
- /// </summary>
- 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<ToolStripItem> items = new List<ToolStripItem>();
- 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);
- }
- /// <summary>
- /// 打开
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MenuFileOpen_Click(object sender, EventArgs e)
- {
- if (AppWorkspace.startScriptRecording)
- {
- AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
- }
- AppWorkspace.PerformAction(new OpenFileAction());
- }
- /// <summary>
- /// 保存
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
- AppWorkspace.ActiveDocumentWorkspace.DoSaveNew();
- }
- }
- /// <summary>
- /// 另存为
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
- AppWorkspace.ActiveDocumentWorkspace.DoSaveAsNew(-1);
- }
- }
- /// <summary>
- /// 批量保存
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- dialog.StartPosition = FormStartPosition.CenterScreen;
- dialog.ShowDialog();
- }
- }
- /// <summary>
- /// 保存全部
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
- this.AppWorkspace.ActiveDocumentWorkspace = documentWorkspace;
- documentWorkspace.DoSaveNew();
- }
- }
- }
- /// <summary>
- /// 强制关闭
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- this.AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
- this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
- }
- }
- /// <summary>
- /// 清空全部
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- 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()); ;
- }
- }
- }
- }
- /// <summary>
- /// 关闭
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
- this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
- }
- }
- /// <summary>
- /// 全部关闭
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<Args>());
- }
- 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<Args>());
- }
- 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());
- }
- }
- }
|