BizHawk/BizHawk.MultiClient/tools/Watch/RamWatch.cs

1213 lines
31 KiB
C#
Raw Normal View History

2011-01-18 03:36:43 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
2011-01-18 17:17:14 +00:00
using System.IO;
2011-01-18 03:36:43 +00:00
using BizHawk.Client.Common;
2011-01-18 03:36:43 +00:00
namespace BizHawk.MultiClient
{
public partial class RamWatch : Form
{
2013-09-14 02:31:21 +00:00
public const string ADDRESS = "AddressColumn";
public const string VALUE = "ValueColumn";
public const string PREV = "PrevColumn";
public const string CHANGES = "ChangesColumn";
public const string DIFF = "DiffColumn";
public const string DOMAIN = "DomainColumn";
public const string NOTES = "NotesColumn";
private readonly Dictionary<string, int> DefaultColumnWidths = new Dictionary<string, int>
{
{ ADDRESS, 60 },
{ VALUE, 59 },
{ PREV, 59 },
{ CHANGES, 55 },
{ DIFF, 59 },
{ DOMAIN, 55 },
{ NOTES, 128 },
};
private int defaultWidth;
private int defaultHeight;
private readonly WatchList Watches = new WatchList(GlobalWinF.Emulator.MainMemory);
2013-09-14 02:31:21 +00:00
private string _sortedColumn = "";
private bool _sortReverse = false;
public RamWatch()
{
InitializeComponent();
WatchListView.QueryItemText += WatchListView_QueryItemText;
WatchListView.QueryItemBkColor += WatchListView_QueryItemBkColor;
WatchListView.VirtualMode = true;
Closing += (o, e) => SaveConfigSettings();
_sortedColumn = "";
_sortReverse = false;
TopMost = Global.Config.RamWatchAlwaysOnTop;
2013-09-14 02:31:21 +00:00
}
public void UpdateValues()
{
if ((!IsHandleCreated || IsDisposed) && !Global.Config.DisplayRamWatch)
{
return;
}
if (Watches.Any())
2013-09-14 02:31:21 +00:00
{
Watches.UpdateValues();
if (Global.Config.DisplayRamWatch)
2013-09-14 02:31:21 +00:00
{
for (int x = 0; x < Watches.Count; x++)
{
bool alert = Watches[x].IsSeparator ? false : GlobalWinF.CheatList.IsActive(Watches[x].Domain, Watches[x].Address.Value);
GlobalWinF.OSD.AddGUIText(
Watches[x].ToString(),
Global.Config.DispRamWatchx,
(Global.Config.DispRamWatchy + (x * 14)),
alert,
Color.Black,
Color.White,
0
);
}
2013-09-14 02:31:21 +00:00
}
if (!IsHandleCreated || IsDisposed) return;
WatchListView.BlazingFast = true;
WatchListView.Refresh();
WatchListView.BlazingFast = false;
}
2013-09-14 02:31:21 +00:00
}
public void Restart()
{
if ((!IsHandleCreated || IsDisposed) && !Global.Config.DisplayRamWatch)
{
return;
}
2013-09-14 02:31:21 +00:00
if (!String.IsNullOrWhiteSpace(Watches.CurrentFileName))
{
2013-09-14 02:31:21 +00:00
Watches.Reload();
}
else
{
NewWatchList(true);
}
}
2013-09-14 02:31:21 +00:00
public void AddWatch(Watch watch)
{
Watches.Add(watch);
DisplayWatches();
UpdateValues();
UpdateWatchCount();
Changes();
}
public void LoadWatchFile(FileInfo file, bool append)
{
2013-09-14 02:31:21 +00:00
if (file != null)
{
bool result = true;
if (Watches.Changes)
{
result = AskSave();
}
if (result)
{
Watches.Load(file.FullName, append);
2013-09-14 02:31:21 +00:00
DisplayWatches();
UpdateMessageLabel();
UpdateWatchCount();
Global.Config.RecentWatches.Add(Watches.CurrentFileName);
SetMemoryDomain(GetDomainPos(Watches.Domain.ToString()));
}
}
}
private int GetDomainPos(string name)
{
//Attempts to find the memory domain by name, if it fails, it defaults to index 0
for (int i = 0; i < GlobalWinF.Emulator.MemoryDomains.Count; i++)
{
if (GlobalWinF.Emulator.MemoryDomains[i].Name == name)
{
return i;
2013-09-14 02:31:21 +00:00
}
}
return 0;
}
2013-09-14 02:31:21 +00:00
public List<int> AddressList
{
2013-09-14 02:31:21 +00:00
get
{
2013-09-14 02:31:21 +00:00
return Watches.Where(x => !x.IsSeparator).Select(x => x.Address.Value).ToList();
}
2013-09-14 02:31:21 +00:00
}
2013-09-14 02:31:21 +00:00
public bool AskSave()
{
if (Global.Config.SupressAskSave) //User has elected to not be nagged
{
2013-09-14 02:31:21 +00:00
return true;
}
2013-09-14 02:31:21 +00:00
if (Watches.Changes)
{
GlobalWinF.Sound.StopSound();
2013-09-14 02:31:21 +00:00
DialogResult result = MessageBox.Show("Save Changes?", "Ram Watch", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
GlobalWinF.Sound.StartSound();
2013-09-14 02:31:21 +00:00
if (result == DialogResult.Yes)
{
Watches.Save();
}
else if (result == DialogResult.No)
{
return true;
}
else if (result == DialogResult.Cancel)
{
2013-09-14 02:31:21 +00:00
return false;
}
}
2013-09-14 02:31:21 +00:00
return true;
}
2013-09-14 02:31:21 +00:00
public void SaveConfigSettings()
{
2013-09-14 02:31:21 +00:00
SaveColumnInfo();
Global.Config.RamWatchWndx = Location.X;
Global.Config.RamWatchWndy = Location.Y;
Global.Config.RamWatchWidth = Right - Left;
Global.Config.RamWatchHeight = Bottom - Top;
}
2013-09-14 02:31:21 +00:00
private void SaveColumnInfo()
{
2013-09-14 02:31:21 +00:00
if (WatchListView.Columns[ADDRESS] != null)
2012-09-10 02:37:44 +00:00
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[ADDRESS] = WatchListView.Columns[ADDRESS].DisplayIndex;
Global.Config.RamWatchColumnWidths[ADDRESS] = WatchListView.Columns[ADDRESS].Width;
2012-09-10 02:37:44 +00:00
}
2013-09-14 02:31:21 +00:00
if (WatchListView.Columns[VALUE] != null)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[VALUE] = WatchListView.Columns[VALUE].DisplayIndex;
Global.Config.RamWatchColumnWidths[VALUE] = WatchListView.Columns[VALUE].Width;
}
2012-09-10 02:37:44 +00:00
2013-09-14 02:31:21 +00:00
if (WatchListView.Columns[PREV] != null)
2012-09-10 02:37:44 +00:00
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[PREV] = WatchListView.Columns[PREV].DisplayIndex;
Global.Config.RamWatchColumnWidths[PREV] = WatchListView.Columns[PREV].Width;
2012-09-10 02:37:44 +00:00
}
2013-09-14 02:31:21 +00:00
if (WatchListView.Columns[CHANGES] != null)
2012-09-10 02:37:44 +00:00
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[CHANGES] = WatchListView.Columns[CHANGES].DisplayIndex;
Global.Config.RamWatchColumnWidths[CHANGES] = WatchListView.Columns[CHANGES].Width;
2012-09-10 02:37:44 +00:00
}
2013-09-14 02:31:21 +00:00
if (WatchListView.Columns[DIFF] != null)
2012-09-10 02:37:44 +00:00
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[DIFF] = WatchListView.Columns[DIFF].DisplayIndex;
Global.Config.RamWatchColumnWidths[DIFF] = WatchListView.Columns[DIFF].Width;
2012-09-10 02:37:44 +00:00
}
2013-09-14 02:31:21 +00:00
if (WatchListView.Columns[DOMAIN] != null)
2012-09-10 02:37:44 +00:00
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[DOMAIN] = WatchListView.Columns[DOMAIN].DisplayIndex;
Global.Config.RamWatchColumnWidths[DOMAIN] = WatchListView.Columns[DOMAIN].Width;
2012-09-10 02:37:44 +00:00
}
2013-09-14 02:31:21 +00:00
if (WatchListView.Columns[NOTES] != null)
2012-09-10 02:37:44 +00:00
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[NOTES] = WatchListView.Columns[NOTES].Index;
Global.Config.RamWatchColumnWidths[NOTES] = WatchListView.Columns[NOTES].Width;
2012-09-10 02:37:44 +00:00
}
}
protected override void OnClosing(CancelEventArgs e)
{
if (!AskSave())
e.Cancel = true;
base.OnClosing(e);
}
2013-09-14 02:31:21 +00:00
private int GetColumnWidth(string columnName)
{
var width = Global.Config.RamWatchColumnWidths[columnName];
if (width == -1)
{
width = DefaultColumnWidths[columnName];
}
return width;
}
private void WatchListView_QueryItemBkColor(int index, int column, ref Color color)
{
2013-09-14 02:31:21 +00:00
if (index >= Watches.ItemCount)
{
return;
}
if (column == 0)
{
2013-09-14 02:31:21 +00:00
if (Watches[index].IsSeparator)
{
color = BackColor;
}
else if (GlobalWinF.CheatList.IsActive(Watches.Domain, Watches[index].Address.Value))
{
color = Color.LightCyan;
}
}
}
2013-09-14 02:31:21 +00:00
private void WatchListView_QueryItemText(int index, int column, out string text)
{
text = "";
2013-09-14 02:31:21 +00:00
if (index >= Watches.ItemCount || Watches[index].IsSeparator)
{
return;
}
2013-09-14 02:31:21 +00:00
string columnName = WatchListView.Columns[column].Name;
2013-09-14 02:31:21 +00:00
switch (columnName)
{
2013-09-14 02:31:21 +00:00
case ADDRESS:
text = Watches[index].AddressString;
2012-09-10 02:37:44 +00:00
break;
2013-09-14 02:31:21 +00:00
case VALUE:
text = Watches[index].ValueString;
2012-09-10 02:37:44 +00:00
break;
2013-09-14 02:31:21 +00:00
case PREV:
text = Watches[index].PreviousStr;
2012-09-10 02:37:44 +00:00
break;
2013-09-14 02:31:21 +00:00
case CHANGES:
if (!Watches[index].IsSeparator)
2013-09-14 02:31:21 +00:00
{
text = Watches[index].ChangeCount.ToString();
2013-09-14 02:31:21 +00:00
}
2012-09-10 02:37:44 +00:00
break;
2013-09-14 02:31:21 +00:00
case DIFF:
text = Watches[index].Diff;
2012-09-10 02:37:44 +00:00
break;
2013-09-14 02:31:21 +00:00
case DOMAIN:
2012-09-10 02:37:44 +00:00
text = Watches[index].Domain.Name;
break;
2013-09-14 02:31:21 +00:00
case NOTES:
text = Watches[index].Notes;
2012-09-10 02:37:44 +00:00
break;
}
}
2013-09-14 02:31:21 +00:00
private void DisplayWatches()
{
2013-09-14 02:31:21 +00:00
WatchListView.ItemCount = Watches.ItemCount;
}
2013-09-14 02:31:21 +00:00
private void UpdateWatchCount()
{
2013-09-14 02:31:21 +00:00
WatchCountLabel.Text = Watches.WatchCount.ToString() + (Watches.WatchCount == 1 ? " watch" : " watches");
}
2013-09-14 02:31:21 +00:00
private void SetPlatformAndMemoryDomainLabel()
{
MemDomainLabel.Text = GlobalWinF.Emulator.SystemId + " " + Watches.Domain.Name;
}
private void NewWatchList(bool suppressAsk)
{
bool result = true;
2013-09-14 02:31:21 +00:00
if (Watches.Changes)
{
result = AskSave();
}
if (result || suppressAsk)
{
Watches.Clear();
2013-09-14 02:31:21 +00:00
DisplayWatches();
UpdateWatchCount();
2013-09-14 02:31:21 +00:00
UpdateMessageLabel();
_sortReverse = false;
_sortedColumn = String.Empty;
}
}
2013-09-14 02:31:21 +00:00
public void LoadFileFromRecent(string path)
{
2013-09-14 02:31:21 +00:00
bool ask_result = true;
if (Watches.Changes)
{
ask_result = AskSave();
}
2013-09-14 02:31:21 +00:00
if (ask_result)
{
bool load_result = Watches.Load(path, append: false);
2013-09-14 02:31:21 +00:00
if (!load_result)
{
ToolHelpers.HandleLoadError(Global.Config.RecentWatches, path);
2013-09-14 02:31:21 +00:00
}
else
{
Global.Config.RecentWatches.Add(path);
DisplayWatches();
UpdateWatchCount();
UpdateMessageLabel();
Watches.Changes = false;
}
}
}
2013-09-14 02:31:21 +00:00
private void UpdateMessageLabel(bool saved = false)
{
2013-09-14 02:31:21 +00:00
string message = String.Empty;
if (!String.IsNullOrWhiteSpace(Watches.CurrentFileName))
{
2013-09-14 02:31:21 +00:00
if (saved)
{
2013-09-14 02:31:21 +00:00
message = Path.GetFileName(Watches.CurrentFileName) + " saved.";
}
2013-09-14 02:31:21 +00:00
else
{
2013-09-14 02:31:21 +00:00
message = Path.GetFileName(Watches.CurrentFileName) + (Watches.Changes ? " *" : String.Empty);
}
}
2013-09-14 02:31:21 +00:00
MessageLabel.Text = message;
}
2013-09-14 02:31:21 +00:00
private void SetMemoryDomain(int pos)
{
if (pos < GlobalWinF.Emulator.MemoryDomains.Count) //Sanity check
{
Watches.Domain = GlobalWinF.Emulator.MemoryDomains[pos];
}
2013-09-14 02:31:21 +00:00
SetPlatformAndMemoryDomainLabel();
Update();
}
2013-09-14 02:31:21 +00:00
private void SelectAll()
{
2013-09-14 02:31:21 +00:00
for (int i = 0; i < Watches.Count; i++)
{
WatchListView.SelectItem(i, true);
}
}
2013-09-14 02:31:21 +00:00
private void Changes()
{
2013-09-14 02:31:21 +00:00
Watches.Changes = true;
UpdateMessageLabel();
}
2013-09-14 02:31:21 +00:00
private void MoveUp()
{
2013-09-14 02:31:21 +00:00
ListView.SelectedIndexCollection indexes = WatchListView.SelectedIndices;
if (indexes.Count == 0 || indexes[0] == 0)
{
return;
}
2013-09-14 02:31:21 +00:00
foreach (int index in indexes)
{
2013-09-14 02:31:21 +00:00
var watch = Watches[index];
Watches.Remove(Watches[index]);
Watches.Insert(index - 1, watch);
//Note: here it will get flagged many times redundantly potentially,
//but this avoids it being flagged falsely when the user did not select an index
Changes();
}
var indices = new List<int>();
2013-09-14 02:31:21 +00:00
for (int i = 0; i < indexes.Count; i++)
{
indices.Add(indexes[i] - 1);
}
2013-09-14 02:31:21 +00:00
WatchListView.SelectedIndices.Clear();
foreach (int t in indices)
{
2013-09-14 02:31:21 +00:00
WatchListView.SelectItem(t, true);
}
2013-09-14 02:31:21 +00:00
DisplayWatches();
}
2013-09-14 02:31:21 +00:00
private void MoveDown()
{
var indexes = WatchListView.SelectedIndices;
2013-09-14 02:31:21 +00:00
if (indexes.Count == 0)
{
2013-09-14 02:31:21 +00:00
return;
}
2013-09-14 02:31:21 +00:00
foreach (int index in indexes)
{
2013-09-14 02:31:21 +00:00
var watch = Watches[index];
2013-09-14 02:31:21 +00:00
if (index < Watches.Count - 1)
{
2013-09-14 02:31:21 +00:00
Watches.Remove(Watches[index]);
Watches.Insert(index + 1, watch);
}
//Note: here it will get flagged many times redundantly potentially,
//but this avoids it being flagged falsely when the user did not select an index
Changes();
}
2013-09-14 02:31:21 +00:00
var indices = new List<int>();
2013-09-14 02:31:21 +00:00
for (int i = 0; i < indexes.Count; i++)
{
2013-09-14 02:31:21 +00:00
indices.Add(indexes[i] + 1);
}
WatchListView.SelectedIndices.Clear();
2013-09-14 02:31:21 +00:00
foreach (int t in indices)
{
WatchListView.SelectItem(t, true);
}
2013-09-14 02:31:21 +00:00
DisplayWatches();
}
2013-09-14 02:31:21 +00:00
private void InsertSeparator()
{
2013-09-14 02:31:21 +00:00
var indexes = WatchListView.SelectedIndices;
if (indexes.Count > 0)
{
2013-09-14 02:31:21 +00:00
Watches.Insert(indexes[0], SeparatorWatch.Instance);
}
else
{
Watches.Add(SeparatorWatch.Instance);
}
DisplayWatches();
Changes();
UpdateWatchCount();
}
2013-09-14 02:31:21 +00:00
private Point GetPromptPoint()
{
return PointToScreen(new Point(WatchListView.Location.X, WatchListView.Location.Y));
}
2013-09-14 02:31:21 +00:00
private void AddNewWatch()
{
WatchEditor we = new WatchEditor
{
InitialLocation = GetPromptPoint()
};
we.SetWatch(Watches.Domain);
GlobalWinF.Sound.StopSound();
2013-09-14 02:31:21 +00:00
we.ShowDialog();
GlobalWinF.Sound.StartSound();
2013-09-14 02:31:21 +00:00
if (we.DialogResult == DialogResult.OK)
{
Watches.Add(we.Watches[0]);
Changes();
2013-09-14 02:31:21 +00:00
UpdateWatchCount();
DisplayWatches();
}
2013-09-14 02:31:21 +00:00
}
2013-09-14 02:31:21 +00:00
private void EditWatch(bool duplicate = false)
{
var indexes = WatchListView.SelectedIndices;
2013-09-14 02:31:21 +00:00
if (indexes.Count > 0)
{
2013-09-14 02:31:21 +00:00
WatchEditor we = new WatchEditor
{
InitialLocation = GetPromptPoint(),
};
if (!SelectedWatches.Any())
{
return;
}
we.SetWatch(Watches.Domain, SelectedWatches, duplicate ? WatchEditor.Mode.Duplicate : WatchEditor.Mode.Edit);
GlobalWinF.Sound.StopSound();
2013-09-14 02:31:21 +00:00
var result = we.ShowDialog();
if (result == DialogResult.OK)
{
Changes();
if (duplicate)
{
Watches.AddRange(we.Watches);
DisplayWatches();
}
else
{
for (int i = 0; i < we.Watches.Count; i++)
{
Watches[indexes[i]] = we.Watches[i];
}
}
}
GlobalWinF.Sound.StartSound();
2013-09-14 02:31:21 +00:00
UpdateValues();
}
}
2013-09-14 02:31:21 +00:00
private void PokeAddress()
{
2013-09-14 02:31:21 +00:00
if (SelectedWatches.Any())
{
RamPoke poke = new RamPoke
2013-09-14 02:31:21 +00:00
{
InitialLocation = GetPromptPoint()
};
if (SelectedWatches.Any())
{
poke.SetWatch(SelectedWatches);
}
GlobalWinF.Sound.StopSound();
2013-09-14 02:31:21 +00:00
var result = poke.ShowDialog();
if (result == DialogResult.OK)
{
UpdateValues();
}
GlobalWinF.Sound.StartSound();
2013-09-14 02:31:21 +00:00
}
}
2013-09-14 02:31:21 +00:00
private List<Watch> SelectedWatches
{
2013-09-14 02:31:21 +00:00
get
{
var selected = new List<Watch>();
ListView.SelectedIndexCollection indices = WatchListView.SelectedIndices;
if (indices.Count > 0)
2013-09-14 02:31:21 +00:00
{
foreach (int index in indices)
2013-09-14 02:31:21 +00:00
{
if (!Watches[index].IsSeparator)
{
selected.Add(Watches[index]);
}
}
}
return selected;
}
}
2013-09-14 02:31:21 +00:00
private void ColumnPositions()
{
List<KeyValuePair<string, int>> Columns =
Global.Config.RamWatchColumnIndexes
.Where(x => WatchListView.Columns.ContainsKey(x.Key))
.OrderBy(x => x.Value).ToList();
for (int i = 0; i < Columns.Count; i++)
{
if (WatchListView.Columns.ContainsKey(Columns[i].Key))
{
WatchListView.Columns[Columns[i].Key].DisplayIndex = i;
}
}
}
2013-09-14 02:31:21 +00:00
private void LoadConfigSettings()
{
2013-09-14 02:31:21 +00:00
//Size and Positioning
defaultWidth = Size.Width; //Save these first so that the user can restore to its original size
defaultHeight = Size.Height;
2013-09-14 02:31:21 +00:00
if (Global.Config.RamWatchSaveWindowPosition && Global.Config.RamWatchWndx >= 0 && Global.Config.RamWatchWndy >= 0)
{
2013-09-14 02:31:21 +00:00
Location = new Point(Global.Config.RamWatchWndx, Global.Config.RamWatchWndy);
}
2013-09-14 02:31:21 +00:00
if (Global.Config.RamWatchWidth >= 0 && Global.Config.RamWatchHeight >= 0)
{
2013-09-14 02:31:21 +00:00
Size = new Size(Global.Config.RamWatchWidth, Global.Config.RamWatchHeight);
}
2013-09-14 02:31:21 +00:00
LoadColumnInfo();
}
2013-09-14 02:31:21 +00:00
private void LoadColumnInfo()
{
2013-09-14 02:31:21 +00:00
WatchListView.Columns.Clear();
ToolHelpers.AddColumn(WatchListView, ADDRESS, true, GetColumnWidth(ADDRESS));
ToolHelpers.AddColumn(WatchListView, VALUE, true, GetColumnWidth(VALUE));
ToolHelpers.AddColumn(WatchListView, PREV, Global.Config.RamWatchShowPrevColumn, GetColumnWidth(PREV));
ToolHelpers.AddColumn(WatchListView, CHANGES, Global.Config.RamWatchShowChangeColumn, GetColumnWidth(CHANGES));
ToolHelpers.AddColumn(WatchListView, DIFF, Global.Config.RamWatchShowDiffColumn, GetColumnWidth(DIFF));
ToolHelpers.AddColumn(WatchListView, DOMAIN, Global.Config.RamWatchShowDomainColumn, GetColumnWidth(DOMAIN));
ToolHelpers.AddColumn(WatchListView, NOTES, true, GetColumnWidth(NOTES));
2013-09-14 02:31:21 +00:00
ColumnPositions();
}
2013-09-14 02:31:21 +00:00
private void RemoveWatch()
{
2013-09-14 02:31:21 +00:00
var indexes = WatchListView.SelectedIndices;
if (indexes.Count > 0)
{
foreach (int index in indexes)
{
Watches.Remove(Watches[indexes[0]]); //index[0] used since each iteration will make this the correct list index
}
indexes.Clear();
DisplayWatches();
}
UpdateValues();
UpdateWatchCount();
}
2013-09-14 02:31:21 +00:00
private string GetColumnValue(string name, int index)
{
2013-09-14 02:31:21 +00:00
switch (name)
{
default:
return String.Empty;
case ADDRESS:
return Watches[index].AddressString;
case VALUE:
return Watches[index].ValueString;
case PREV:
return Watches[index].PreviousStr;
2013-09-14 02:31:21 +00:00
case CHANGES:
return Watches[index].ChangeCount.ToString();
2013-09-14 02:31:21 +00:00
case DIFF:
return Watches[index].Diff;
2013-09-14 02:31:21 +00:00
case DOMAIN:
return Watches[index].Domain.Name;
case NOTES:
return Watches[index].Notes;
2013-09-14 02:31:21 +00:00
}
}
2013-09-14 02:31:21 +00:00
private void CopyWatchesToClipBoard()
{
2013-09-14 02:31:21 +00:00
var indexes = WatchListView.SelectedIndices;
if (indexes.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (int index in indexes)
{
foreach (ColumnHeader column in WatchListView.Columns)
{
sb.Append(GetColumnValue(column.Name, index)).Append('\t');
}
sb.Remove(sb.Length - 1, 1);
sb.AppendLine();
}
if (sb.Length > 0)
{
Clipboard.SetDataObject(sb.ToString());
}
}
}
2013-09-14 02:31:21 +00:00
private void OrderColumn(int index)
{
2013-09-14 02:31:21 +00:00
var column = WatchListView.Columns[index];
if (column.Name != _sortedColumn)
{
_sortReverse = false;
}
Watches.OrderWatches(column.Name, _sortReverse);
_sortedColumn = column.Name;
_sortReverse ^= true;
WatchListView.Refresh();
}
2013-09-14 02:31:21 +00:00
#region Winform Events
private void NewRamWatch_Load(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
LoadConfigSettings();
}
2013-09-14 02:31:21 +00:00
private void NewRamWatch_Activated(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
WatchListView.Refresh();
}
2013-09-14 02:31:21 +00:00
private void NewRamWatch_DragEnter(object sender, DragEventArgs e)
{
2013-09-14 02:31:21 +00:00
e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
}
2013-09-14 02:31:21 +00:00
private void NewRamWatch_DragDrop(object sender, DragEventArgs e)
{
2013-09-14 02:31:21 +00:00
string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
if (Path.GetExtension(filePaths[0]) == (".wch"))
{
Watches.Load(filePaths[0], append:false);
2013-09-14 02:31:21 +00:00
DisplayWatches();
}
}
2013-09-14 02:31:21 +00:00
private void NewRamWatch_Enter(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
WatchListView.Focus();
}
2013-09-14 02:31:21 +00:00
/*************File***********************/
private void filesToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
saveToolStripMenuItem.Enabled = Watches.Changes;
}
2013-09-14 02:31:21 +00:00
private void newListToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
NewWatchList(false);
}
2013-09-14 02:31:21 +00:00
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
bool append = sender == appendFileToolStripMenuItem;
LoadWatchFile(WatchList.GetFileFromUser(Watches.CurrentFileName), append);
}
2013-09-14 02:31:21 +00:00
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
if (Watches.Save())
{
2013-09-14 02:31:21 +00:00
UpdateMessageLabel(saved: true);
}
}
2013-09-14 02:31:21 +00:00
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
bool result = Watches.SaveAs();
if (result)
{
UpdateMessageLabel(saved: true);
Global.Config.RecentWatches.Add(Watches.CurrentFileName);
}
}
2013-09-14 02:31:21 +00:00
private void recentToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
recentToolStripMenuItem.DropDownItems.Clear();
recentToolStripMenuItem.DropDownItems.AddRange(
ToolHelpers.GenerateRecentMenu(Global.Config.RecentWatches, LoadFileFromRecent)
);
recentToolStripMenuItem.DropDownItems.Add(
ToolHelpers.GenerateAutoLoadItem(Global.Config.RecentWatches)
);
}
2013-09-14 02:31:21 +00:00
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
if (!AskSave())
{
2013-09-14 02:31:21 +00:00
return;
}
else
{
2013-09-14 02:31:21 +00:00
Close();
}
}
2013-09-14 02:31:21 +00:00
/*************Watches***********************/
private void watchesToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
ListView.SelectedIndexCollection indexes = WatchListView.SelectedIndices;
if (indexes.Count > 0)
{
2013-09-14 02:31:21 +00:00
editWatchToolStripMenuItem.Enabled = true;
duplicateWatchToolStripMenuItem.Enabled = true;
removeWatchToolStripMenuItem.Enabled = true;
moveUpToolStripMenuItem.Enabled = true;
moveDownToolStripMenuItem.Enabled = true;
pokeAddressToolStripMenuItem.Enabled = true;
freezeAddressToolStripMenuItem.Enabled = true;
}
else
{
2013-09-14 02:31:21 +00:00
editWatchToolStripMenuItem.Enabled = false;
duplicateWatchToolStripMenuItem.Enabled = false;
removeWatchToolStripMenuItem.Enabled = false;
moveUpToolStripMenuItem.Enabled = false;
moveDownToolStripMenuItem.Enabled = false;
pokeAddressToolStripMenuItem.Enabled = false;
freezeAddressToolStripMenuItem.Enabled = false;
}
}
2013-09-14 02:31:21 +00:00
private void memoryDomainsToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
memoryDomainsToolStripMenuItem.DropDownItems.Clear();
memoryDomainsToolStripMenuItem.DropDownItems.AddRange(ToolHelpers.GenerateMemoryDomainMenuItems(SetMemoryDomain, Watches.Domain.Name));
}
2013-09-14 02:31:21 +00:00
private void newWatchToolStripMenuItem_Click(object sender, EventArgs e)
{
AddNewWatch();
}
2013-09-14 02:31:21 +00:00
private void editWatchToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
EditWatch();
}
2013-09-14 02:31:21 +00:00
private void removeWatchToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
RemoveWatch();
}
2013-09-14 02:31:21 +00:00
private void duplicateWatchToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
EditWatch(duplicate: true);
}
2013-09-14 02:31:21 +00:00
private void pokeAddressToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
PokeAddress();
}
2013-09-14 02:31:21 +00:00
private void freezeAddressToolStripMenuItem_Click(object sender, EventArgs e)
{
bool allCheats = true;
foreach (var watch in SelectedWatches)
{
if (!watch.IsSeparator)
{
if (!GlobalWinF.CheatList.IsActive(watch.Domain, watch.Address.Value))
{
allCheats = false;
}
}
}
if (allCheats)
{
ToolHelpers.UnfreezeAddress(SelectedWatches);
}
else
{
ToolHelpers.FreezeAddress(SelectedWatches);
}
}
private void insertSeparatorToolStripMenuItem_Click(object sender, EventArgs e)
{
InsertSeparator();
}
2013-09-14 02:31:21 +00:00
private void clearChangeCountsToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Watches.ClearChangeCounts();
}
2013-09-14 02:31:21 +00:00
private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
MoveUp();
}
2013-09-14 02:31:21 +00:00
private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
MoveDown();
}
2013-09-14 02:31:21 +00:00
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
SelectAll();
}
/*************Columns***********************/
private void ColumnsSubMenu_DropDownOpened(object sender, EventArgs e)
{
ShowPreviousMenuItem.Checked = Global.Config.RamWatchShowPrevColumn;
ShowChangesMenuItem.Checked = Global.Config.RamWatchShowChangeColumn;
ShowDiffMenuItem.Checked = Global.Config.RamWatchShowDiffColumn;
ShowDomainMenuItem.Checked = Global.Config.RamWatchShowDomainColumn;
}
2013-09-14 02:31:21 +00:00
private void showPreviousValueToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchShowPrevColumn ^= true;
SaveColumnInfo();
LoadColumnInfo();
}
2013-09-14 02:31:21 +00:00
private void showChangeCountsToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchShowChangeColumn ^= true;
2013-09-14 02:31:21 +00:00
SaveColumnInfo();
LoadColumnInfo();
}
2013-09-14 02:31:21 +00:00
private void diffToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchShowDiffColumn ^= true;
2013-09-14 02:31:21 +00:00
SaveColumnInfo();
LoadColumnInfo();
}
2013-09-14 02:31:21 +00:00
private void domainToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchShowDomainColumn ^= true;
2013-09-14 02:31:21 +00:00
SaveColumnInfo();
LoadColumnInfo();
}
2013-09-14 02:31:21 +00:00
/*************Options***********************/
private void optionsToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
displayWatchesOnScreenToolStripMenuItem.Checked = Global.Config.DisplayRamWatch;
saveWindowPositionToolStripMenuItem.Checked = Global.Config.RamWatchSaveWindowPosition;
AlwaysOnTopMenuItem.Checked = Global.Config.RamWatchAlwaysOnTop;
}
2013-09-14 02:31:21 +00:00
private void definePreviousValueAsToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
lastChangeToolStripMenuItem.Checked = false;
previousFrameToolStripMenuItem.Checked = false;
originalToolStripMenuItem.Checked = false;
2013-09-14 02:31:21 +00:00
switch (Global.Config.RamWatchDefinePrevious)
{
2013-09-14 02:31:21 +00:00
default:
case Watch.PreviousType.LastFrame:
previousFrameToolStripMenuItem.Checked = true;
break;
case Watch.PreviousType.LastChange:
lastChangeToolStripMenuItem.Checked = true;
break;
case Watch.PreviousType.Original:
2013-09-14 02:31:21 +00:00
originalToolStripMenuItem.Checked = true;
break;
}
}
2013-09-14 02:31:21 +00:00
private void previousFrameToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchDefinePrevious = Watch.PreviousType.LastFrame;
}
2013-09-14 02:31:21 +00:00
private void lastChangeToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchDefinePrevious = Watch.PreviousType.LastChange;
}
2013-09-14 02:31:21 +00:00
private void originalToolStripMenuItem_Click(object sender, EventArgs e)
{
Global.Config.RamWatchDefinePrevious = Watch.PreviousType.Original;
}
2013-09-14 02:31:21 +00:00
private void displayWatchesOnScreenToolStripMenuItem_Click(object sender, EventArgs e)
2012-07-04 23:11:17 +00:00
{
2013-09-14 02:31:21 +00:00
Global.Config.DisplayRamWatch ^= true;
2013-09-14 02:31:21 +00:00
if (!Global.Config.DisplayRamWatch)
{
GlobalWinF.OSD.ClearGUIText();
}
else
{
2013-09-14 02:31:21 +00:00
UpdateValues();
}
}
2013-09-14 02:31:21 +00:00
private void saveWindowPositionToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchSaveWindowPosition ^= true;
}
private void AlwaysOnTopMenuItem_Click(object sender, EventArgs e)
{
Global.Config.RamWatchAlwaysOnTop ^= true;
TopMost = Global.Config.RamWatchAlwaysOnTop;
}
2013-09-14 02:31:21 +00:00
private void restoreWindowSizeToolStripMenuItem_Click(object sender, EventArgs e)
{
2013-09-14 02:31:21 +00:00
Size = new Size(defaultWidth, defaultHeight);
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes = new Dictionary<string, int>
{
{ "AddressColumn", 0 },
{ "ValueColumn", 1 },
{ "PrevColumn", 2 },
{ "ChangesColumn", 3 },
{ "DiffColumn", 4 },
{ "DomainColumn", 5 },
{ "NotesColumn", 6 },
};
2013-09-14 02:31:21 +00:00
ColumnPositions();
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchShowChangeColumn = true;
Global.Config.RamWatchShowDomainColumn = true;
Global.Config.RamWatchShowPrevColumn = false;
Global.Config.RamWatchShowDiffColumn = false;
2013-09-14 02:31:21 +00:00
WatchListView.Columns[ADDRESS].Width = DefaultColumnWidths[ADDRESS];
WatchListView.Columns[VALUE].Width = DefaultColumnWidths[VALUE];
//WatchListView.Columns[PREV].Width = DefaultColumnWidths[PREV];
WatchListView.Columns[CHANGES].Width = DefaultColumnWidths[CHANGES];
//WatchListView.Columns[DIFF].Width = DefaultColumnWidths[DIFF];
WatchListView.Columns[DOMAIN].Width = DefaultColumnWidths[DOMAIN];
WatchListView.Columns[NOTES].Width = DefaultColumnWidths[NOTES];
2013-09-14 02:31:21 +00:00
Global.Config.DisplayRamWatch = false;
Global.Config.RamWatchSaveWindowPosition = true;
Global.Config.RamWatchAlwaysOnTop = TopMost = false;
2013-09-14 02:31:21 +00:00
LoadColumnInfo();
}
2013-09-14 02:31:21 +00:00
/*************Context Menu***********************/
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
ListView.SelectedIndexCollection indexes = WatchListView.SelectedIndices;
2013-09-14 02:31:21 +00:00
EditContextMenuItem.Visible =
RemoveContextMenuItem.Visible =
DuplicateContextMenuItem.Visible =
PokeContextMenuItem.Visible =
FreezeContextMenuItem.Visible =
Separator6.Visible =
InsertSeperatorContextMenuItem.Visible =
MoveUpContextMenuItem.Visible =
MoveDownContextMenuItem.Visible =
Separator6.Visible =
toolStripSeparator4.Visible =
2013-09-14 02:31:21 +00:00
indexes.Count > 0;
bool allCheats = true;
foreach (int i in indexes)
{
if (!Watches[i].IsSeparator)
{
if (!GlobalWinF.CheatList.IsActive(Watches[i].Domain, Watches[i].Address.Value))
{
2013-09-14 02:31:21 +00:00
allCheats = false;
}
}
}
2013-09-14 02:31:21 +00:00
if (allCheats)
{
2013-09-14 02:31:21 +00:00
FreezeContextMenuItem.Text = "&Unfreeze address";
FreezeContextMenuItem.Image = Properties.Resources.Unfreeze;
}
else
{
2013-09-14 02:31:21 +00:00
FreezeContextMenuItem.Text = "&Freeze address";
FreezeContextMenuItem.Image = Properties.Resources.Freeze;
}
2013-09-14 02:31:21 +00:00
ShowChangeCountsContextMenuItem.Text = Global.Config.RamWatchShowChangeColumn ? "Hide change counts" : "Show change counts";
ShowPreviousValueContextMenuItem.Text = Global.Config.RamWatchShowPrevColumn ? "Hide previous value" : "Show previous value";
ShowDiffContextMenuItem.Text = Global.Config.RamWatchShowDiffColumn ? "Hide difference value" : "Show difference value";
ShowDomainContextMenuItem.Text = Global.Config.RamWatchShowDomainColumn ? "Hide domain" : "Show domain";
UnfreezeAllContextMenuItem.Visible = GlobalWinF.CheatList.ActiveCount > 0;
2013-09-14 02:31:21 +00:00
ViewInHexEditorContextMenuItem.Visible = SelectedWatches.Count == 1;
}
2013-09-14 02:31:21 +00:00
private void UnfreezeAllContextMenuItem_Click(object sender, EventArgs e)
{
ToolHelpers.UnfreezeAll();
}
2013-09-14 02:31:21 +00:00
private void ViewInHexEditorContextMenuItem_Click(object sender, EventArgs e)
{
var selected = SelectedWatches;
if (selected.Any())
2012-09-10 02:37:44 +00:00
{
GlobalWinF.MainForm.LoadHexEditor();
if (selected.Select(x => x.Domain).Distinct().Count() > 1)
{
ToolHelpers.ViewInHexEditor(selected[0].Domain, new List<int> { selected.First().Address.Value });
}
else
{
ToolHelpers.ViewInHexEditor(selected[0].Domain, selected.Select(x => x.Address.Value));
}
2012-09-10 02:37:44 +00:00
}
}
2013-09-14 02:31:21 +00:00
/*************ListView Events***********************/
private void WatchListView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete && !e.Control && !e.Alt && !e.Shift)
{
RemoveWatch();
}
else if (e.KeyCode == Keys.A && e.Control && !e.Alt && !e.Shift) //Select All
{
for (int x = 0; x < Watches.Count; x++)
{
WatchListView.SelectItem(x, true);
}
}
else if (e.KeyCode == Keys.C && e.Control && !e.Alt && !e.Shift) //Copy
{
2013-09-14 02:31:21 +00:00
CopyWatchesToClipBoard();
}
2013-09-14 02:31:21 +00:00
else if (e.KeyCode == Keys.Enter && !e.Control && !e.Alt && !e.Shift) //Enter
{
2013-09-14 02:31:21 +00:00
EditWatch();
}
}
2013-09-14 02:31:21 +00:00
private void WatchListView_MouseDoubleClick(object sender, MouseEventArgs e)
{
2013-09-14 02:31:21 +00:00
EditWatch();
}
2013-09-14 02:31:21 +00:00
private void WatchListView_ColumnClick(object sender, ColumnClickEventArgs e)
{
2013-09-14 02:31:21 +00:00
OrderColumn(e.Column);
}
2013-09-14 02:31:21 +00:00
private void WatchListView_ColumnReordered(object sender, ColumnReorderedEventArgs e)
{
2013-09-14 02:31:21 +00:00
Global.Config.RamWatchColumnIndexes[ADDRESS] = WatchListView.Columns[ADDRESS].DisplayIndex;
Global.Config.RamWatchColumnIndexes[VALUE] = WatchListView.Columns[VALUE].DisplayIndex;
Global.Config.RamWatchColumnIndexes[PREV] = WatchListView.Columns[ADDRESS].DisplayIndex;
Global.Config.RamWatchColumnIndexes[CHANGES] = WatchListView.Columns[CHANGES].DisplayIndex;
Global.Config.RamWatchColumnIndexes[DIFF] = WatchListView.Columns[DIFF].DisplayIndex;
Global.Config.RamWatchColumnIndexes[DOMAIN] = WatchListView.Columns[DOMAIN].DisplayIndex;
Global.Config.RamWatchColumnIndexes[NOTES] = WatchListView.Columns[NOTES].DisplayIndex;
}
2013-09-14 02:31:21 +00:00
#endregion
}
2013-09-14 02:31:21 +00:00
}