MRU.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using Microsoft.Win32;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Windows.Forms;
  19. namespace MobiusEditor.Utility
  20. {
  21. public class MRU
  22. {
  23. private readonly RegistryKey registryKey;
  24. private readonly int maxFiles;
  25. private readonly List<FileInfo> files = new List<FileInfo>();
  26. private readonly ToolStripMenuItem menu;
  27. private readonly ToolStripMenuItem[] fileItems;
  28. public event EventHandler<FileInfo> FileSelected;
  29. public MRU(string registryPath, int maxFiles, ToolStripMenuItem menu)
  30. {
  31. var subKey = Registry.CurrentUser;
  32. foreach (var key in registryPath.Split('\\'))
  33. {
  34. subKey = subKey.CreateSubKey(key, true);
  35. }
  36. registryKey = subKey.CreateSubKey("MRU");
  37. this.maxFiles = maxFiles;
  38. this.menu = menu;
  39. this.menu.DropDownItems.Clear();
  40. fileItems = new ToolStripMenuItem[maxFiles];
  41. for (var i = 0; i < fileItems.Length; ++i)
  42. {
  43. var fileItem = fileItems[i] = new ToolStripMenuItem();
  44. fileItem.Visible = false;
  45. menu.DropDownItems.Add(fileItem);
  46. }
  47. LoadMRU();
  48. ShowMRU();
  49. }
  50. public void Add(FileInfo file)
  51. {
  52. files.RemoveAll(f => f.FullName == file.FullName);
  53. files.Insert(0, file);
  54. if (files.Count > maxFiles)
  55. {
  56. files.RemoveAt(files.Count - 1);
  57. }
  58. SaveMRU();
  59. ShowMRU();
  60. }
  61. public void Remove(FileInfo file)
  62. {
  63. files.RemoveAll(f => f.FullName == file.FullName);
  64. SaveMRU();
  65. ShowMRU();
  66. }
  67. private void LoadMRU()
  68. {
  69. files.Clear();
  70. for (var i = 0; i < maxFiles; ++i)
  71. {
  72. string fileName = registryKey.GetValue(i.ToString()) as string;
  73. if (!string.IsNullOrEmpty(fileName))
  74. {
  75. files.Add(new FileInfo(fileName));
  76. }
  77. }
  78. }
  79. private void SaveMRU()
  80. {
  81. for (var i = 0; i < files.Count; ++i)
  82. {
  83. registryKey.SetValue(i.ToString(), files[i].FullName);
  84. }
  85. for (var i = files.Count; i < maxFiles; ++i)
  86. {
  87. registryKey.DeleteValue(i.ToString(), false);
  88. }
  89. }
  90. private void ShowMRU()
  91. {
  92. for (var i = 0; i < files.Count; ++i)
  93. {
  94. var file = files[i];
  95. var fileItem = fileItems[i];
  96. fileItem.Text = string.Format("&{0} {1}", i + 1, file.FullName);
  97. fileItem.Tag = file;
  98. fileItem.Click -= FileItem_Click;
  99. fileItem.Click += FileItem_Click;
  100. fileItem.Visible = true;
  101. }
  102. for (var i = files.Count; i < maxFiles; ++i)
  103. {
  104. var fileItem = fileItems[i];
  105. fileItem.Visible = false;
  106. fileItem.Click -= FileItem_Click;
  107. }
  108. menu.Enabled = files.Count > 0;
  109. }
  110. private void FileItem_Click(object sender, EventArgs e)
  111. {
  112. FileSelected?.Invoke(this, (sender as ToolStripMenuItem).Tag as FileInfo);
  113. }
  114. }
  115. }