FileMenu.cs 17 KB

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