2013-09-06 22:06:44 +00:00
using System ;
using System.Collections ;
using System.Collections.Generic ;
2013-04-15 02:14:14 +00:00
using System.Linq ;
2013-09-06 22:06:44 +00:00
using System.Windows.Forms ;
2011-01-17 01:58:19 +00:00
namespace BizHawk.MultiClient
{
2013-09-06 22:06:44 +00:00
public class RecentFiles : IEnumerable
2011-06-19 23:39:25 +00:00
{
2013-04-15 02:14:14 +00:00
private readonly int MAX_RECENT_FILES ; //Maximum number of files
2013-08-01 00:37:18 +00:00
private readonly List < string > recentlist ; //List of recent files
2011-01-17 01:58:19 +00:00
2013-09-07 01:38:24 +00:00
public bool AutoLoad = false ;
2013-08-01 00:37:18 +00:00
public RecentFiles ( ) : this ( 8 ) { }
2011-06-19 23:39:25 +00:00
public RecentFiles ( int max )
{
2013-08-01 00:37:18 +00:00
recentlist = new List < string > ( ) ;
2011-06-19 23:39:25 +00:00
MAX_RECENT_FILES = max ;
}
2011-01-17 01:58:19 +00:00
2013-09-07 01:38:24 +00:00
public IEnumerator < string > GetEnumerator ( )
{
return recentlist . GetEnumerator ( ) ;
}
2013-09-06 22:06:44 +00:00
2013-09-07 01:38:24 +00:00
System . Collections . IEnumerator System . Collections . IEnumerable . GetEnumerator ( )
{
return GetEnumerator ( ) ;
}
2013-09-06 22:06:44 +00:00
2011-06-19 23:39:25 +00:00
public void Clear ( )
{
2013-08-01 00:37:18 +00:00
recentlist . Clear ( ) ;
2011-06-19 23:39:25 +00:00
}
2011-01-17 01:58:19 +00:00
2013-07-16 01:59:59 +00:00
public bool Empty
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
get { return recentlist . Count = = 0 ; }
2011-06-19 23:39:25 +00:00
}
2011-01-17 02:34:52 +00:00
2013-03-10 22:42:54 +00:00
public int Count
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
get { return recentlist . Count ; }
2011-06-19 23:39:25 +00:00
}
2011-01-17 01:58:19 +00:00
2011-06-19 23:39:25 +00:00
public void Add ( string newFile )
{
2013-08-01 00:37:18 +00:00
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 )
2013-03-10 22:42:54 +00:00
{
2013-08-01 00:37:18 +00:00
recentlist . Remove ( recentlist [ recentlist . Count - 1 ] ) ;
2013-03-10 22:42:54 +00:00
}
2011-06-19 23:39:25 +00:00
}
2011-01-17 01:58:19 +00:00
2011-06-19 23:39:25 +00:00
public bool Remove ( string newFile )
{
bool removed = false ;
2013-08-01 00:37:18 +00:00
for ( int x = 0 ; x < recentlist . Count ; x + + )
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
if ( string . Compare ( newFile , recentlist [ x ] ) = = 0 )
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
recentlist . Remove ( newFile ) ; //intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
2011-06-19 23:39:25 +00:00
removed = true ;
}
}
return removed ;
}
2011-01-17 01:58:19 +00:00
2011-06-19 23:39:25 +00:00
public List < string > GetRecentListTruncated ( int length )
{
//iterate through list, truncating each item to length, and return the result in a List<string>
2013-08-01 00:37:18 +00:00
return recentlist . Select ( t = > t . Substring ( 0 , length ) ) . ToList ( ) ;
2011-06-19 23:39:25 +00:00
}
2013-09-07 01:38:24 +00:00
public string this [ int index ]
{
get
{
if ( recentlist . Any ( ) )
{
return recentlist [ index ] ;
}
else
{
return "" ;
}
}
}
2013-09-09 21:36:26 +00:00
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 ( ) ;
}
2013-09-07 03:15:29 +00:00
public ToolStripItem [ ] GenerateRecentMenu ( Action < string > loadFileCallback )
2013-09-07 01:38:24 +00:00
{
var items = new List < ToolStripItem > ( ) ;
if ( Empty )
{
var none = new ToolStripMenuItem { Enabled = false , Text = "None" } ;
items . Add ( none ) ;
}
else
{
foreach ( string filename in recentlist )
{
2013-09-07 03:15:29 +00:00
string temp = filename ;
var item = new ToolStripMenuItem { Text = temp } ;
item . Click + = ( o , ev ) = > loadFileCallback ( temp ) ;
2013-09-07 01:38:24 +00:00
items . Add ( item ) ;
}
}
items . Add ( new ToolStripSeparator ( ) ) ;
var clearitem = new ToolStripMenuItem { Text = "&Clear" } ;
clearitem . Click + = ( o , ev ) = > recentlist . Clear ( ) ;
items . Add ( clearitem ) ;
2013-09-07 03:15:29 +00:00
return items . ToArray ( ) ;
2013-09-07 01:38:24 +00:00
}
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-06-19 23:39:25 +00:00
}
2011-01-17 01:58:19 +00:00
}