FileMenu.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using PaintDotNet.Actions;
  2. using PaintDotNet.Base;
  3. using PaintDotNet.Data.Param;
  4. using PaintDotNet.File;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. namespace PaintDotNet.Menus
  11. {
  12. /// <summary>
  13. /// 文件菜单
  14. /// </summary>
  15. internal sealed class FileMenu : PdnMenuItem
  16. {
  17. private PdnMenuItem menuFileOpen;
  18. private PdnMenuItem menuFileSave;
  19. private PdnMenuItem menuFileSaveAs;
  20. private PdnMenuItem menuFileBulkSave;
  21. private PdnMenuItem menuFileSaveAll;
  22. private ToolStripSeparator menuFileSeparator1;
  23. private PdnMenuItem menuFileForceClose;
  24. private PdnMenuItem menuFileClearAll;
  25. private ToolStripSeparator menuFileSeparator2;
  26. private PdnMenuItem menuFileOpenRecent;
  27. private PdnMenuItem menuFileOpenRecentSentinel;
  28. private ToolStripSeparator menuFileSeparator3;
  29. private PdnMenuItem menuFileClose;
  30. private PdnMenuItem menuFileCloseAll;
  31. private bool OnCtrlF4Typed(Keys keys)
  32. {
  33. this.menuFileClose.PerformClick();
  34. return true;
  35. }
  36. public FileMenu(int menuId)
  37. {
  38. PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.F4, OnCtrlF4Typed);
  39. InitializeComponent();
  40. this.MenuId = menuId;
  41. }
  42. private void InitializeComponent()
  43. {
  44. this.menuFileOpen = new PdnMenuItem(ActionType.Open);
  45. this.menuFileOpen.NeedWaitKey = true;
  46. this.menuFileOpen.AutoNextScript = false;
  47. this.menuFileOpen.AutomaticScript = false;
  48. this.menuFileSave = new PdnMenuItem(ActionType.Save);
  49. this.menuFileSave.AutomaticScript = false;
  50. this.menuFileSaveAs = new PdnMenuItem(ActionType.SaveAs);
  51. this.menuFileSaveAs.AutomaticScript = false;
  52. this.menuFileBulkSave = new PdnMenuItem(ActionType.BulkSave);
  53. this.menuFileBulkSave.AutomaticScript = false;
  54. this.menuFileSaveAll = new PdnMenuItem(ActionType.SaveAll);
  55. this.menuFileSaveAll.AutomaticScript = false;
  56. this.menuFileSeparator1 = new ToolStripSeparator();
  57. this.menuFileForceClose = new PdnMenuItem(ActionType.ForceClose);
  58. this.menuFileClearAll = new PdnMenuItem(ActionType.ClearAll);
  59. this.menuFileSeparator2 = new ToolStripSeparator();
  60. this.menuFileOpenRecent = new PdnMenuItem(ActionType.OpenRecent);
  61. this.menuFileOpenRecent.CanUseInScript = false;
  62. this.menuFileOpenRecent.AutomaticScript = false;
  63. this.menuFileOpenRecentSentinel = new PdnMenuItem();
  64. this.menuFileSeparator3 = new ToolStripSeparator();
  65. this.menuFileClose = new PdnMenuItem(ActionType.Close);
  66. this.menuFileCloseAll = new PdnMenuItem(ActionType.CloseAll);
  67. //
  68. // FileMenu
  69. //
  70. this.DropDownItems.AddRange(GetMenuItemsToAdd());
  71. this.Name = "Menu.File";
  72. this.Text = PdnResources.GetString("Menu.File.Text");
  73. //
  74. // menuFileOpen
  75. //
  76. this.menuFileOpen.Click += new EventHandler(this.MenuFileOpen_Click);
  77. //
  78. // menuFileSave
  79. //
  80. this.menuFileSave.Click += new EventHandler(this.MenuFileSave_Click);
  81. //
  82. // menuFileSaveAs
  83. //
  84. this.menuFileSaveAs.Click += new EventHandler(this.MenuFileSaveAs_Click);
  85. //
  86. // 批量保存
  87. //
  88. this.menuFileBulkSave.Click += new EventHandler(this.MenuFileBulkSave_Click);
  89. //
  90. // 保存全部
  91. //
  92. this.menuFileSaveAll.Click += new EventHandler(this.MenuFileSaveAll_Click);
  93. //
  94. // 强制关闭
  95. //
  96. this.menuFileForceClose.Click += new EventHandler(this.MenuFileForceClose_Click);
  97. //
  98. // 清空全部
  99. //
  100. this.menuFileClearAll.Click += new EventHandler(this.MenuFileClearAll_Click);
  101. //
  102. // 最近打开的文件
  103. //
  104. this.menuFileOpenRecent.Name = "OpenRecent";
  105. this.menuFileOpenRecent.DropDownItems.AddRange(
  106. new ToolStripItem[]
  107. {
  108. this.menuFileOpenRecentSentinel
  109. });
  110. this.menuFileOpenRecent.DropDownOpening += new EventHandler(this.MenuFileOpenRecent_DropDownOpening);
  111. //
  112. // menuFileOpenRecentSentinel
  113. //
  114. this.menuFileOpenRecentSentinel.Text = "sentinel";
  115. //
  116. // 关闭
  117. //
  118. this.menuFileClose.Click += new EventHandler(MenuFileClose_Click);
  119. //
  120. // 全部关闭
  121. //
  122. this.menuFileCloseAll.Click += new EventHandler(MenuFileCloseAll_Click);
  123. //
  124. // 加载菜单的文字和icon
  125. //
  126. this.LoadNames(this.Name);
  127. this.LoadIcons();
  128. }
  129. private ToolStripItem[] GetMenuItemsToAdd()
  130. {
  131. List<ToolStripItem> items = new List<ToolStripItem>();
  132. items.Add(this.menuFileOpen);
  133. items.Add(this.menuFileSave);
  134. items.Add(this.menuFileSaveAs);
  135. items.Add(this.menuFileBulkSave);
  136. items.Add(this.menuFileSaveAll);
  137. items.Add(this.menuFileSeparator1);
  138. items.Add(this.menuFileForceClose);
  139. items.Add(this.menuFileClearAll);
  140. items.Add(this.menuFileSeparator2);
  141. items.Add(this.menuFileOpenRecent);
  142. items.Add(this.menuFileSeparator3);
  143. items.Add(this.menuFileClose);
  144. items.Add(this.menuFileCloseAll);
  145. return items.ToArray();
  146. }
  147. protected override void OnDropDownOpening(EventArgs e)
  148. {
  149. this.DropDownItems.Clear();
  150. this.DropDownItems.AddRange(GetMenuItemsToAdd());
  151. this.menuFileOpen.Enabled = true/* && !AppWorkspace.ScriptRunning*/;
  152. this.menuFileOpenRecent.Enabled = true/* && !AppWorkspace.ScriptRunning*/;
  153. this.menuFileOpenRecentSentinel.Enabled = true/* && !AppWorkspace.ScriptRunning*/;
  154. if (AppWorkspace.ActiveDocumentWorkspace != null/* && !AppWorkspace.ScriptRunning*/)
  155. {
  156. this.menuFileForceClose.Enabled = true;
  157. this.menuFileClearAll.Enabled = true;
  158. this.menuFileSave.Enabled = true;
  159. this.menuFileSaveAs.Enabled = true;
  160. this.menuFileBulkSave.Enabled = true;
  161. this.menuFileSaveAll.Enabled = true;
  162. this.menuFileClose.Enabled = true;
  163. this.menuFileCloseAll.Enabled = true;
  164. }
  165. else
  166. {
  167. this.menuFileForceClose.Enabled = false;
  168. this.menuFileClearAll.Enabled = false;
  169. this.menuFileSave.Enabled = false;
  170. this.menuFileSaveAs.Enabled = false;
  171. this.menuFileBulkSave.Enabled = false;
  172. this.menuFileSaveAll.Enabled = false;
  173. this.menuFileClose.Enabled = false;
  174. this.menuFileCloseAll.Enabled = false;
  175. }
  176. base.OnDropDownOpening(e);
  177. }
  178. /// <summary>
  179. /// 打开
  180. /// </summary>
  181. /// <param name="sender"></param>
  182. /// <param name="e"></param>
  183. private void MenuFileOpen_Click(object sender, EventArgs e)
  184. {
  185. if (AppWorkspace.startScriptRecording)
  186. {
  187. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  188. }
  189. AppWorkspace.PerformAction(new OpenFileAction());
  190. }
  191. /// <summary>
  192. /// 保存
  193. /// </summary>
  194. /// <param name="sender"></param>
  195. /// <param name="e"></param>
  196. private void MenuFileSave_Click(object sender, EventArgs e)
  197. {
  198. if (AppWorkspace.ActiveDocumentWorkspace != null)
  199. {
  200. if (AppWorkspace.startScriptRecording)
  201. {
  202. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  203. }
  204. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
  205. AppWorkspace.ActiveDocumentWorkspace.DoSaveNew();
  206. }
  207. }
  208. /// <summary>
  209. /// 另存为
  210. /// </summary>
  211. /// <param name="sender"></param>
  212. /// <param name="e"></param>
  213. private void MenuFileSaveAs_Click(object sender, EventArgs e)
  214. {
  215. if (AppWorkspace.ActiveDocumentWorkspace != null)
  216. {
  217. if (AppWorkspace.startScriptRecording)
  218. {
  219. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  220. }
  221. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
  222. AppWorkspace.ActiveDocumentWorkspace.DoSaveAsNew(-1);
  223. }
  224. }
  225. /// <summary>
  226. /// 批量保存
  227. /// </summary>
  228. /// <param name="sender"></param>
  229. /// <param name="e"></param>
  230. private void MenuFileBulkSave_Click(object sender, EventArgs e)
  231. {
  232. using (BatchSaveDialog dialog = new BatchSaveDialog(AppWorkspace))
  233. {
  234. if (AppWorkspace.startScriptRecording)
  235. {
  236. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  237. }
  238. dialog.StartPosition = FormStartPosition.CenterScreen;
  239. dialog.ShowDialog();
  240. }
  241. }
  242. /// <summary>
  243. /// 保存全部
  244. /// </summary>
  245. /// <param name="sender"></param>
  246. /// <param name="e"></param>
  247. private void MenuFileSaveAll_Click(object sender, EventArgs e)
  248. {
  249. if (this.AppWorkspace != null && this.AppWorkspace.DocumentWorkspaces!=null)
  250. {
  251. foreach(DocumentWorkspace documentWorkspace in this.AppWorkspace.DocumentWorkspaces)
  252. {
  253. if (AppWorkspace.startScriptRecording)
  254. {
  255. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  256. }
  257. this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
  258. this.AppWorkspace.ActiveDocumentWorkspace = documentWorkspace;
  259. documentWorkspace.DoSaveNew();
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// 强制关闭
  265. /// </summary>
  266. /// <param name="sender"></param>
  267. /// <param name="e"></param>
  268. private void MenuFileForceClose_Click(object sender, EventArgs e)
  269. {
  270. if (this.AppWorkspace.DocumentWorkspaces.Length > 0)
  271. {
  272. if (AppWorkspace.startScriptRecording)
  273. {
  274. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  275. }
  276. this.AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
  277. this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
  278. }
  279. }
  280. /// <summary>
  281. /// 清空全部
  282. /// </summary>
  283. /// <param name="sender"></param>
  284. /// <param name="e"></param>
  285. private void MenuFileClearAll_Click(object sender, EventArgs e)
  286. {
  287. DialogResult result = MessageBox.Show(PdnResources.GetString("Menu.file.Emptyall.Areyousuodifiedpicture.text")+"!", PdnResources.GetString("Menu.Empty.text"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  288. if (result == DialogResult.Yes)
  289. {
  290. if (AppWorkspace.startScriptRecording)
  291. {
  292. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  293. }
  294. if (this.AppWorkspace.DocumentWorkspaces!=null && this.AppWorkspace.DocumentWorkspaces.Length > 0)
  295. {
  296. foreach (DocumentWorkspace document in this.AppWorkspace.DocumentWorkspaces)
  297. {
  298. this.AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
  299. this.AppWorkspace.PerformAction(new CloseWorkspaceAction()); ;
  300. }
  301. }
  302. }
  303. }
  304. /// <summary>
  305. /// 关闭
  306. /// </summary>
  307. /// <param name="sender"></param>
  308. /// <param name="e"></param>
  309. private void MenuFileClose_Click(object sender, EventArgs e)
  310. {
  311. if (this.AppWorkspace.DocumentWorkspaces.Length > 0)
  312. {
  313. if (AppWorkspace.startScriptRecording)
  314. {
  315. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  316. }
  317. this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
  318. this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
  319. }
  320. }
  321. /// <summary>
  322. /// 全部关闭
  323. /// </summary>
  324. /// <param name="sender"></param>
  325. /// <param name="e"></param>
  326. private void MenuFileCloseAll_Click(object sender, EventArgs e)
  327. {
  328. if (AppWorkspace.DocumentWorkspaces != null
  329. && AppWorkspace.DocumentWorkspaces.Length > 0)
  330. {
  331. if (AppWorkspace.startScriptRecording)
  332. {
  333. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  334. }
  335. this.AppWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
  336. foreach (DocumentWorkspace document in AppWorkspace.DocumentWorkspaces)
  337. {
  338. this.AppWorkspace.PerformAction(new CloseWorkspaceAction());
  339. }
  340. }
  341. }
  342. private void MenuFileOpenRecent_DropDownOpening(object sender, EventArgs e)
  343. {
  344. if (AppWorkspace.startScriptRecording)
  345. {
  346. AppWorkspace.SetScriptStartRecording(((PdnMenuItem)sender).MenuId, ((PdnMenuItem)sender).Text, new List<Args>());
  347. }
  348. AppWorkspace.MostRecentFiles.LoadMruList();
  349. MostRecentFile[] filesReverse = AppWorkspace.MostRecentFiles.GetFileList();
  350. MostRecentFile[] files = new MostRecentFile[filesReverse.Length];
  351. int i;
  352. for (i = 0; i < filesReverse.Length; ++i)
  353. {
  354. files[files.Length - i - 1] = filesReverse[i];
  355. }
  356. foreach (ToolStripItem mi in menuFileOpenRecent.DropDownItems)
  357. {
  358. mi.Click -= new EventHandler(MenuFileOpenRecentFile_Click);
  359. }
  360. menuFileOpenRecent.DropDownItems.Clear();
  361. i = 0;
  362. Image choise = PdnResources.GetImageResource("Icons.SwapIcon.png").Reference;
  363. Image nochoise = PdnResources.GetImageResource("Icons.SwatchIcon.png").Reference;
  364. foreach (MostRecentFile mrf in files)
  365. {
  366. string menuName;
  367. if (i < 9)
  368. {
  369. menuName = "&";
  370. }
  371. else
  372. {
  373. menuName = "";
  374. }
  375. menuName += (1 + i).ToString() + " " + Path.GetFileName(mrf.FileName);
  376. ToolStripMenuItem mi = new ToolStripMenuItem(menuName);
  377. mi.Alignment = ToolStripItemAlignment.Right;
  378. mi.Click += new EventHandler(MenuFileOpenRecentFile_Click);
  379. mi.ImageAlign = ContentAlignment.MiddleRight;
  380. mi.ImageScaling = ToolStripItemImageScaling.None;
  381. mi.Image = nochoise; //(Image)mrf.Thumb.Clone();
  382. menuFileOpenRecent.DropDownItems.Add(mi);
  383. ++i;
  384. }
  385. if (menuFileOpenRecent.DropDownItems.Count == 0)
  386. {
  387. ToolStripMenuItem none = new ToolStripMenuItem(PdnResources.GetString("Menu.File.OpenRecent.None"));
  388. none.Enabled = false;
  389. menuFileOpenRecent.DropDownItems.Add(none);
  390. }
  391. else
  392. {
  393. ToolStripSeparator separator = new ToolStripSeparator();
  394. menuFileOpenRecent.DropDownItems.Add(separator);
  395. ToolStripMenuItem clearList = new ToolStripMenuItem();
  396. clearList.Text = PdnResources.GetString("Menu.File.OpenRecent.ClearThisList");
  397. menuFileOpenRecent.DropDownItems.Add(clearList);
  398. Image deleteIcon = PdnResources.GetImageResource("Icons.MenuEditEraseSelectionIcon.png").Reference;
  399. clearList.ImageTransparentColor = Utility.TransparentKey;
  400. clearList.ImageAlign = ContentAlignment.MiddleCenter;
  401. clearList.ImageScaling = ToolStripItemImageScaling.None;
  402. /*
  403. int iconSize = AppWorkspace.MostRecentFiles.IconSize;
  404. Bitmap bitmap = new Bitmap(iconSize + 2, iconSize + 2);
  405. using (Graphics g = Graphics.FromImage(bitmap))
  406. {
  407. g.Clear(clearList.ImageTransparentColor);
  408. Point offset = new Point((bitmap.Width - deleteIcon.Width) / 2,
  409. (bitmap.Height - deleteIcon.Height) / 2);
  410. g.CompositingMode = CompositingMode.SourceCopy;
  411. g.DrawImage(deleteIcon, offset.X, offset.Y, deleteIcon.Width, deleteIcon.Height);
  412. }
  413. clearList.Image = bitmap;*/
  414. clearList.Image = deleteIcon;
  415. clearList.Click += new EventHandler(ClearList_Click);
  416. }
  417. }
  418. private void MenuFileOpenRecentFile_Click(object sender, EventArgs e)
  419. {
  420. try
  421. {
  422. ToolStripMenuItem mi = (ToolStripMenuItem)sender;
  423. int spaceIndex = mi.Text.IndexOf(" ");
  424. string indexString = mi.Text.Substring(1, spaceIndex - 1);
  425. int index = int.Parse(indexString) - 1;
  426. MostRecentFile[] recentFiles = AppWorkspace.MostRecentFiles.GetFileList();
  427. string fileName = recentFiles[recentFiles.Length - index - 1].FileName;
  428. AppWorkspace.OpenFileInNewWorkspace(fileName);
  429. }
  430. catch (Exception)
  431. {
  432. }
  433. }
  434. private void ClearList_Click(object sender, EventArgs e)
  435. {
  436. string question = PdnResources.GetString("ClearOpenRecentList.Dialog.Text");
  437. DialogResult result = Utility.AskYesNo(AppWorkspace, question);
  438. if (result == DialogResult.Yes)
  439. {
  440. AppWorkspace.MostRecentFiles.Clear();
  441. AppWorkspace.MostRecentFiles.SaveMruList();
  442. }
  443. //AppWorkspace.PerformAction(new ClearMruListAction());
  444. }
  445. }
  446. }