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