2014-07-28 01:51:11 +00:00
using System ;
2014-09-29 04:47:21 +00:00
using System.IO ;
2014-07-28 01:51:11 +00:00
using System.Collections.Generic ;
using System.Windows.Forms ;
2014-07-28 01:55:20 +00:00
using System.Linq ;
2014-07-28 01:51:11 +00:00
2014-09-29 04:47:21 +00:00
using BizHawk.Common ;
2014-07-28 02:10:31 +00:00
using BizHawk.Emulation.Common ;
2014-07-28 01:51:11 +00:00
using BizHawk.Client.Common ;
namespace BizHawk.Client.EmuHawk.ToolExtensions
{
public static class ToolExtensions
{
public static ToolStripItem [ ] RecentMenu ( this RecentFiles recent , Action < string > loadFileCallback , bool autoload = false )
{
var items = new List < ToolStripItem > ( ) ;
if ( recent . Empty )
{
var none = new ToolStripMenuItem { Enabled = false , Text = "None" } ;
items . Add ( none ) ;
}
else
{
foreach ( var filename in recent )
{
2014-09-29 04:47:21 +00:00
//TODO - do TSMI and TSDD need disposing? yuck
2014-07-28 01:51:11 +00:00
var temp = filename ;
var item = new ToolStripMenuItem { Text = temp } ;
items . Add ( item ) ;
2014-09-29 04:47:21 +00:00
item . Click + = ( o , ev ) = >
{
loadFileCallback ( temp ) ;
} ;
//TODO - use standard methods to split filename (hawkfile acquire?)
2014-09-30 08:51:48 +00:00
var hf = new HawkFile ( ) ;
hf . Parse ( temp ) ;
2014-09-29 04:47:21 +00:00
bool canExplore = true ;
if ( ! File . Exists ( hf . FullPathWithoutMember ) )
canExplore = false ;
var tsdd = new ToolStripDropDownMenu ( ) ;
if ( canExplore )
{
//make a menuitem to show the last modified timestamp
var timestamp = File . GetLastWriteTime ( hf . FullPathWithoutMember ) ;
var tsmiTimestamp = new ToolStripLabel { Text = timestamp . ToString ( ) } ;
2014-09-30 08:51:48 +00:00
tsdd . Items . Add ( tsmiTimestamp ) ;
tsdd . Items . Add ( new ToolStripSeparator ( ) ) ;
2014-09-29 04:47:21 +00:00
if ( hf . IsArchive )
{
2014-09-30 08:51:48 +00:00
//make a menuitem to let you copy the path
var tsmiCopyCanonicalPath = new ToolStripMenuItem { Text = "&Copy Canonical Path" } ;
tsmiCopyCanonicalPath . Click + = ( o , ev ) = > { System . Windows . Forms . Clipboard . SetText ( temp ) ; } ;
tsdd . Items . Add ( tsmiCopyCanonicalPath ) ;
2014-09-29 04:47:21 +00:00
var tsmiCopyArchivePath = new ToolStripMenuItem { Text = "Copy Archive Path" } ;
tsmiCopyArchivePath . Click + = ( o , ev ) = > { System . Windows . Forms . Clipboard . SetText ( hf . FullPathWithoutMember ) ; } ;
tsdd . Items . Add ( tsmiCopyArchivePath ) ;
2014-09-30 08:51:48 +00:00
var tsmiOpenArchive = new ToolStripMenuItem { Text = "Open &Archive" } ;
tsmiOpenArchive . Click + = ( o , ev ) = > { System . Diagnostics . Process . Start ( hf . FullPathWithoutMember ) ; } ;
tsdd . Items . Add ( tsmiOpenArchive ) ;
}
else
{
//make a menuitem to let you copy the path
var tsmiCopyPath = new ToolStripMenuItem { Text = "&Copy Path" } ;
tsmiCopyPath . Click + = ( o , ev ) = > { System . Windows . Forms . Clipboard . SetText ( temp ) ; } ;
tsdd . Items . Add ( tsmiCopyPath ) ;
2014-09-29 04:47:21 +00:00
}
tsdd . Items . Add ( new ToolStripSeparator ( ) ) ;
2014-09-30 08:51:48 +00:00
//make a menuitem to let you explore to it
var tsmiExplore = new ToolStripMenuItem { Text = "&Explore" } ;
tsmiExplore . Click + = ( o , ev ) = > { System . Diagnostics . Process . Start ( "explorer.exe" , "/select, " + hf . FullPathWithoutMember ) ; } ;
tsdd . Items . Add ( tsmiExplore ) ;
var tsmiCopyFile = new ToolStripMenuItem { Text = "Copy &File" } ;
var lame = new System . Collections . Specialized . StringCollection ( ) ;
lame . Add ( hf . FullPathWithoutMember ) ;
tsmiCopyFile . Click + = ( o , ev ) = > { System . Windows . Forms . Clipboard . SetFileDropList ( lame ) ; } ;
tsdd . Items . Add ( tsmiCopyFile ) ;
var tsmiTest = new ToolStripMenuItem { Text = "&Shell Context Menu" } ;
tsmiTest . Click + = ( o , ev ) = > {
var si = new GongSolutions . Shell . ShellItem ( hf . FullPathWithoutMember ) ;
var scm = new GongSolutions . Shell . ShellContextMenu ( si ) ;
var tsddi = o as ToolStripDropDownItem ;
tsddi . Owner . Update ( ) ;
scm . ShowContextMenu ( tsddi . Owner , new System . Drawing . Point ( 0 , 0 ) ) ;
} ;
tsdd . Items . Add ( tsmiTest ) ;
tsdd . Items . Add ( new ToolStripSeparator ( ) ) ;
2014-09-29 04:47:21 +00:00
}
else
{
//make a menuitem to show the last modified timestamp
var tsmiMissingFile = new ToolStripLabel { Text = "-Missing-" } ;
tsdd . Items . Add ( tsmiMissingFile ) ;
tsdd . Items . Add ( new ToolStripSeparator ( ) ) ;
}
//in either case, make a menuitem to let you remove the path
var tsmiRemovePath = new ToolStripMenuItem { Text = "&Remove" } ;
tsmiRemovePath . Click + = ( o , ev ) = > { recent . Remove ( temp ) ; } ;
tsdd . Items . Add ( tsmiRemovePath ) ;
////experiment of popping open a submenu. doesnt work well.
//item.MouseDown += (o, mev) =>
//{
// if (mev.Button != MouseButtons.Right) return;
// //location of the menu containing this item that was just rightclicked
// var pos = item.Owner.Bounds.Location;
// //the offset within that menu of this item
// var tsddi = item as ToolStripDropDownItem;
// pos.Offset(tsddi.Bounds.Location);
// //the offset of the click
// pos.Offset(mev.Location);
// //tsdd.OwnerItem = item; //has interesting promise, but breaks things otherwise
// tsdd.Show(pos);
//};
//just add it to the submenu for now
item . MouseDown + = ( o , mev ) = >
{
if ( mev . Button ! = MouseButtons . Right ) return ;
if ( item . DropDown ! = null )
item . DropDown = tsdd ;
item . ShowDropDown ( ) ;
} ;
2014-07-28 01:51:11 +00:00
}
}
items . Add ( new ToolStripSeparator ( ) ) ;
2014-08-02 22:52:22 +00:00
var clearitem = new ToolStripMenuItem { Text = "&Clear" , Enabled = ! recent . Frozen } ;
2014-07-28 01:51:11 +00:00
clearitem . Click + = ( o , ev ) = > recent . Clear ( ) ;
items . Add ( clearitem ) ;
2014-08-02 22:49:27 +00:00
var freezeitem = new ToolStripMenuItem { Text = recent . Frozen ? "&Unfreeze" : "&Freeze" } ;
freezeitem . Click + = ( o , ev ) = > recent . Frozen ^ = true ;
items . Add ( freezeitem ) ;
2014-07-28 01:51:11 +00:00
if ( autoload )
{
var auto = new ToolStripMenuItem { Text = "&Autoload" , Checked = recent . AutoLoad } ;
auto . Click + = ( o , ev ) = > recent . ToggleAutoLoad ( ) ;
items . Add ( auto ) ;
}
return items . ToArray ( ) ;
}
public static void HandleLoadError ( this RecentFiles recent , string path )
{
GlobalWin . Sound . StopSound ( ) ;
2014-08-02 23:03:29 +00:00
if ( recent . Frozen )
2014-07-28 01:51:11 +00:00
{
2014-08-02 23:03:29 +00:00
var result = MessageBox . Show ( "Could not open " + path , "File not found" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
else
{
var result = MessageBox . Show ( "Could not open " + path + "\nRemove from list?" , "File not found" , MessageBoxButtons . YesNo , MessageBoxIcon . Error ) ;
if ( result = = DialogResult . Yes )
{
recent . Remove ( path ) ;
}
2014-07-28 01:51:11 +00:00
}
GlobalWin . Sound . StartSound ( ) ;
}
2014-07-28 01:55:20 +00:00
public static void FreezeAll ( this IEnumerable < Watch > watches )
{
Global . CheatList . AddRange (
watches
. Where ( w = > ! w . IsSeparator )
. Select ( w = > new Cheat ( w , w . Value ? ? 0 ) ) ) ;
}
public static void UnfreezeAll ( this IEnumerable < Watch > watches )
{
Global . CheatList . RemoveRange ( watches . Where ( watch = > ! watch . IsSeparator ) ) ;
}
2014-07-28 02:10:31 +00:00
public static IEnumerable < ToolStripItem > MenuItems ( this MemoryDomainList domains , Action < string > setCallback , string selected = "" , int? maxSize = null )
{
foreach ( var domain in domains )
{
var name = domain . Name ;
var item = new ToolStripMenuItem
{
Text = name ,
2014-09-11 21:50:06 +00:00
Enabled = ! ( maxSize . HasValue & & domain . Size > maxSize . Value ) & & ! ( maxSize . HasValue & & domain . Size = = 0 ) , // 0 denotes a full 32bit size, which is definiately greater than max size!
2014-07-28 02:10:31 +00:00
Checked = name = = selected
} ;
item . Click + = ( o , ev ) = > setCallback ( name ) ;
yield return item ;
}
}
2014-07-28 01:51:11 +00:00
}
}