forked from VaIeroK/Omf-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentFiles.cs
More file actions
133 lines (113 loc) · 4.38 KB
/
Copy pathRecentFiles.cs
File metadata and controls
133 lines (113 loc) · 4.38 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace OMF_Editor
{
// The last few OMF files, kept in the ini next to the editor and offered
// straight from the File menu.
public partial class OMF_Editor
{
const int RecentFilesCount = 5;
private IniFile recentSettings;
private ToolStripMenuItem recentFilesItem;
private List<string> recentFiles = new List<string>();
// Called from the form constructor.
private void InitRecentFiles()
{
recentSettings = new IniFile(Path.Combine(
Path.GetDirectoryName(Application.ExecutablePath), "OMF_Editor.ini"));
recentFilesItem = new ToolStripMenuItem("Recent files");
// right below Load, where it is looked for
int at = fileToolStripMenuItem.DropDownItems.IndexOf(loasToolStripMenuItem);
fileToolStripMenuItem.DropDownItems.Insert(at + 1, recentFilesItem);
LoadRecentFiles();
UpdateRecentFilesMenu();
}
private void LoadRecentFiles()
{
recentFiles.Clear();
for (int i = 0; i != RecentFilesCount; ++i)
{
string path = recentSettings.Read("Recent" + i);
if (!string.IsNullOrEmpty(path) && !recentFiles.Contains(path))
recentFiles.Add(path);
}
}
private void SaveRecentFiles()
{
for (int i = 0; i != RecentFilesCount; ++i)
recentSettings.Write("Recent" + i, i < recentFiles.Count ? recentFiles[i] : "");
}
// Newly opened files go first, and the list never grows past its size.
private void AddRecentFile(string path)
{
if (string.IsNullOrEmpty(path) || recentFilesItem == null)
return;
recentFiles.RemoveAll(delegate(string known)
{
return string.Equals(known, path, StringComparison.OrdinalIgnoreCase);
});
recentFiles.Insert(0, path);
while (recentFiles.Count > RecentFilesCount)
recentFiles.RemoveAt(recentFiles.Count - 1);
SaveRecentFiles();
UpdateRecentFilesMenu();
}
private void UpdateRecentFilesMenu()
{
recentFilesItem.DropDownItems.Clear();
if (recentFiles.Count == 0)
{
ToolStripMenuItem empty = new ToolStripMenuItem("(empty)");
empty.Enabled = false;
recentFilesItem.DropDownItems.Add(empty);
return;
}
for (int i = 0; i != recentFiles.Count; ++i)
{
string path = recentFiles[i];
// the file name reads well enough, the full path goes in the tip
ToolStripMenuItem item = new ToolStripMenuItem(
string.Format("&{0} {1}", i + 1, Path.GetFileName(path)));
item.ToolTipText = path;
item.Tag = path;
item.Click += RecentFileClick;
recentFilesItem.DropDownItems.Add(item);
}
recentFilesItem.DropDownItems.Add(new ToolStripSeparator());
ToolStripMenuItem clear = new ToolStripMenuItem("Clear list");
clear.Click += RecentFilesClearClick;
recentFilesItem.DropDownItems.Add(clear);
}
private void RecentFileClick(object sender, EventArgs e)
{
string path = (string)((ToolStripMenuItem)sender).Tag;
if (!File.Exists(path))
{
if (MessageBox.Show("File not found:\n" + path + "\n\nRemove it from the list?",
"Recent files", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
recentFiles.Remove(path);
SaveRecentFiles();
UpdateRecentFilesMenu();
}
return;
}
try
{
OpenFile(path);
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
private void RecentFilesClearClick(object sender, EventArgs e)
{
recentFiles.Clear();
SaveRecentFiles();
UpdateRecentFilesMenu();
}
}
}