-
Notifications
You must be signed in to change notification settings - Fork 6
/
MruList.cs
223 lines (190 loc) · 7.95 KB
/
MruList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace KMZ_Viewer
{
public class MruPathList
{
private bool isDirList = false;
private string MRUListSavedFileName;
private int MRUFilesCount;
private List<FileInfo> MRUFilesInfos;
private ToolStripMenuItem MyMenu;
private bool UseSeparator = false;
private ToolStripSeparator Separator = null;
private ToolStripMenuItem[] MenuItems;
// Raised when the user selects a file from the MRU list.
public delegate void FileSelectedEventHandler(string file_name);
public event FileSelectedEventHandler FileSelected;
public int Count { get { return MRUFilesInfos.Count; } }
// Constructors
#region Constructors
public MruPathList(string MRUFileName, ToolStripMenuItem menu)
{
this.Init(MRUFileName, menu, 10, false);
}
public MruPathList(string MRUFileName, ToolStripMenuItem menu, int num_files)
{
this.Init(MRUFileName, menu, num_files, false);
}
public MruPathList(string MRUFileName, ToolStripMenuItem menu, bool isDirList)
{
this.Init(MRUFileName, menu, 10, isDirList);
}
public MruPathList(string MRUFileName, ToolStripMenuItem menu, int num_files, bool isDirList)
{
this.Init(MRUFileName, menu, num_files, isDirList);
}
#endregion Constructors
// Init
private void Init(string MRUFileName, ToolStripMenuItem menu, int num_files, bool isDirList)
{
this.MRUListSavedFileName = MRUFileName;
this.isDirList = isDirList;
MyMenu = menu;
MRUFilesCount = num_files;
MRUFilesInfos = new List<FileInfo>();
// Make a separator
Separator = new ToolStripSeparator();
Separator.Visible = false;
if (UseSeparator) MyMenu.DropDownItems.Add(Separator);
// Make the menu items we may later need.
MenuItems = new ToolStripMenuItem[MRUFilesCount + 1];
for (int i = 0; i < MRUFilesCount; i++)
{
MenuItems[i] = new ToolStripMenuItem();
MenuItems[i].Visible = false;
MyMenu.DropDownItems.Add(MenuItems[i]);
};
// Reload items from the registry.
LoadFiles();
// Display the items.
ShowFiles();
}
private void LoadFiles()
{
string filemru = this.MRUListSavedFileName;
if (!File.Exists(filemru)) return;
FileStream fs = new FileStream(filemru, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(1251));
while (!sr.EndOfStream)
{
string fullpath = sr.ReadLine();
if (String.IsNullOrEmpty(fullpath)) continue;
if (fullpath.StartsWith("#")) continue;
if (fullpath.StartsWith("@")) continue;
if (!isDirList)
{
if (File.Exists(fullpath))
MRUFilesInfos.Add(new FileInfo(fullpath));
}
else
{
if (Directory.Exists(fullpath))
MRUFilesInfos.Add(new FileInfo(fullpath));
};
};
sr.Close();
fs.Close();
}
// Save the current items in the Registry.
private void SaveFiles()
{
string filemru = this.MRUListSavedFileName;
if (filemru == null) return;
FileStream fs = new FileStream(filemru, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(1251));
sw.WriteLine("#");
sw.WriteLine("# MRU Path List");
sw.WriteLine("#");
sw.WriteLine("# One path per line");
sw.WriteLine("#");
foreach (FileInfo file_info in MRUFilesInfos)
sw.WriteLine(file_info.FullName);
sw.Close();
fs.Close();
}
// Remove a file's info from the list.
private void RemoveFileInfo(string file_name)
{
// Remove occurrences of the file's information from the list.
for (int i = MRUFilesInfos.Count - 1; i >= 0; i--)
{
if (MRUFilesInfos[i].FullName == file_name) MRUFilesInfos.RemoveAt(i);
}
}
// Add a file to the list, rearranging if necessary.
public void AddFile(string file_name)
{
// Remove the file from the list.
RemoveFileInfo(file_name);
// Add the file to the beginning of the list.
MRUFilesInfos.Insert(0, new FileInfo(file_name));
// If we have too many items, remove the last one.
if (MRUFilesInfos.Count > MRUFilesCount) MRUFilesInfos.RemoveAt(MRUFilesCount);
// Display the files.
ShowFiles();
// Update the Registry.
SaveFiles();
}
// Remove a file from the list, rearranging if necessary.
public void RemoveFile(string file_name)
{
// Remove the file from the list.
RemoveFileInfo(file_name);
// Display the files.
ShowFiles();
// Update the Registry.
SaveFiles();
}
public delegate string FormatMenuItemTextEvent(int index, FileInfo FileInfo, ref System.Drawing.Color TextColor);
public event FormatMenuItemTextEvent FormatMenuItem;
// Display the files in the menu items.
private void ShowFiles()
{
Separator.Visible = (MRUFilesInfos.Count > 0);
for (int i = 0; i < MRUFilesInfos.Count; i++)
{
string mit = string.Format("&{0}: `{1}`", i+1, MRUFilesInfos[i].Name);
mit += " at .. " + MRUFilesInfos[i].FullName.Remove(MRUFilesInfos[i].FullName.Length - MRUFilesInfos[i].Name.Length);
while (mit.Length > 90) mit = mit.Remove(mit.IndexOf("` at .. ") + 8, 1);
if (FormatMenuItem != null)
{
System.Drawing.Color color = MenuItems[i].ForeColor = System.Drawing.Color.Black;
string MIT = FormatMenuItem(i + 1, MRUFilesInfos[i], ref color);
mit = String.IsNullOrEmpty(MIT) ? mit : MIT;
MenuItems[i].ForeColor = color;
};
MenuItems[i].Text = mit;
MenuItems[i].Visible = true;
MenuItems[i].Tag = MRUFilesInfos[i];
MenuItems[i].Click -= File_Click;
MenuItems[i].Click += File_Click;
}
for (int i = MRUFilesInfos.Count; i < MRUFilesCount; i++)
{
MenuItems[i].Visible = false;
MenuItems[i].Click -= File_Click;
}
}
public void UpdateNames()
{
ShowFiles();
}
// The user selected a file from the menu.
private void File_Click(object sender, EventArgs e)
{
// Don't bother if no one wants to catch the event.
if (FileSelected != null)
{
// Get the corresponding FileInfo object.
ToolStripMenuItem menu_item = sender as ToolStripMenuItem;
FileInfo file_info = menu_item.Tag as FileInfo;
// Raise the event.
FileSelected(file_info.FullName);
}
}
}
}