BizHawk/BizHawk.MultiClient/RecentFiles.cs

153 lines
3.5 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
2013-04-15 02:14:14 +00:00
using System.Linq;
using System.Windows.Forms;
2011-01-17 01:58:19 +00:00
namespace BizHawk.MultiClient
{
public class RecentFiles : IEnumerable
{
2013-04-15 02:14:14 +00:00
private readonly int MAX_RECENT_FILES; //Maximum number of files
private readonly List<string> recentlist; //List of recent files
2011-01-17 01:58:19 +00:00
public bool AutoLoad = false;
public RecentFiles() : this(8) { }
public RecentFiles(int max)
{
recentlist = new List<string>();
MAX_RECENT_FILES = max;
}
2011-01-17 01:58:19 +00:00
public IEnumerator<string> GetEnumerator()
{
return recentlist.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Clear()
{
recentlist.Clear();
}
2011-01-17 01:58:19 +00:00
public bool Empty
{
get { return recentlist.Count == 0; }
}
2011-01-17 02:34:52 +00:00
public int Count
{
get { return recentlist.Count; }
}
2011-01-17 01:58:19 +00:00
public void Add(string newFile)
{
for (int x = 0; x < recentlist.Count; x++)
{
if (string.Compare(newFile, recentlist[x]) == 0)
{
recentlist.Remove(newFile); //intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
}
}
recentlist.Insert(0, newFile);
if (recentlist.Count > MAX_RECENT_FILES)
{
recentlist.Remove(recentlist[recentlist.Count - 1]);
}
}
2011-01-17 01:58:19 +00:00
public bool Remove(string newFile)
{
bool removed = false;
for (int x = 0; x < recentlist.Count; x++)
{
if (string.Compare(newFile, recentlist[x]) == 0)
{
recentlist.Remove(newFile); //intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
removed = true;
}
}
return removed;
}
2011-01-17 01:58:19 +00:00
public List<string> GetRecentListTruncated(int length)
{
//iterate through list, truncating each item to length, and return the result in a List<string>
return recentlist.Select(t => t.Substring(0, length)).ToList();
}
public string this[int index]
{
get
{
if (recentlist.Any())
{
return recentlist[index];
}
else
{
return "";
}
}
}
public void HandleLoadError(string path)
{
Global.Sound.StopSound();
DialogResult result = MessageBox.Show("Could not open " + path + "\nRemove from list?", "File not found", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (result == DialogResult.Yes)
{
Remove(path);
}
Global.Sound.StartSound();
}
public ToolStripItem[] GenerateRecentMenu(Action<string> loadFileCallback)
{
var items = new List<ToolStripItem>();
if (Empty)
{
var none = new ToolStripMenuItem { Enabled = false, Text = "None" };
items.Add(none);
}
else
{
foreach (string filename in recentlist)
{
string temp = filename;
var item = new ToolStripMenuItem { Text = temp };
item.Click += (o, ev) => loadFileCallback(temp);
items.Add(item);
}
}
items.Add(new ToolStripSeparator());
var clearitem = new ToolStripMenuItem { Text = "&Clear" };
clearitem.Click += (o, ev) => recentlist.Clear();
items.Add(clearitem);
return items.ToArray();
}
public ToolStripMenuItem GenerateAutoLoadItem()
{
var auto = new ToolStripMenuItem { Text = "&Auto-Load", Checked = AutoLoad };
auto.Click += (o, ev) => ToggleAutoLoad();
return auto;
}
private void ToggleAutoLoad()
{
AutoLoad ^= true;
}
}
2011-01-17 01:58:19 +00:00
}