// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Xml.Linq; using ICSharpCode.AvalonEdit.Document; using ICSharpCode.ILSpy.Bookmarks; using ICSharpCode.ILSpy.Debugger; using ICSharpCode.ILSpy.Debugger.Bookmarks; using ICSharpCode.ILSpy.Debugger.Services; using ICSharpCode.ILSpy.Debugger.UI; using ICSharpCode.ILSpy.TreeNodes; using ICSharpCode.TreeView; using Microsoft.Win32; namespace ICSharpCode.ILSpy.Debugger.Commands { public abstract class DebuggerCommand : SimpleCommand { public DebuggerCommand() { MainWindow.Instance.KeyUp += OnKeyUp; } void OnKeyUp(object sender, KeyEventArgs e) { switch (e.Key) { case Key.F5: if (this is ContinueDebuggingCommand) { ((ContinueDebuggingCommand)this).Execute(null); e.Handled = true; } break; case Key.System: if (this is StepOverCommand) { ((StepOverCommand)this).Execute(null); e.Handled = true; } break; case Key.F11: if (this is StepIntoCommand) { ((StepIntoCommand)this).Execute(null); e.Handled = true; } break; default: // do nothing break; } } #region Static members [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool SetWindowPos( IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); const UInt32 SWP_NOSIZE = 0x0001; const UInt32 SWP_NOMOVE = 0x0002; static readonly IntPtr HWND_BOTTOM = new IntPtr(1); static readonly IntPtr HWND_TOP = new IntPtr(0); static void SendWpfWindowPos(Window window, IntPtr place) { var hWnd = new WindowInteropHelper(window).Handle; SetWindowPos(hWnd, place, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); } #endregion public override void Execute(object parameter) { DebugInformation.LoadedAssemblies = MainWindow.Instance.CurrentAssemblyList.GetAssemblies().Select(a => a.AssemblyDefinition); } protected static IDebugger CurrentDebugger { get { return DebuggerService.CurrentDebugger; } } protected void StartExecutable(string fileName, string workingDirectory, string arguments) { CurrentDebugger.Start(new ProcessStartInfo { FileName = fileName, WorkingDirectory = workingDirectory ?? Path.GetDirectoryName(fileName), Arguments = arguments }); Finish(); } protected void StartAttaching(Process process) { CurrentDebugger.Attach(process); Finish(); } protected void Finish() { EnableDebuggerUI(false); CurrentDebugger.DebugStopped += OnDebugStopped; CurrentDebugger.IsProcessRunningChanged += CurrentDebugger_IsProcessRunningChanged; DebugInformation.IsDebuggerLoaded = true; MainWindow.Instance.SetStatus("Running...", Brushes.Black); } protected void OnDebugStopped(object sender, EventArgs e) { EnableDebuggerUI(true); CurrentDebugger.DebugStopped -= OnDebugStopped; CurrentDebugger.IsProcessRunningChanged -= CurrentDebugger_IsProcessRunningChanged; DebugInformation.IsDebuggerLoaded = false; MainWindow.Instance.SetStatus("Stand by...", Brushes.Black); } protected void EnableDebuggerUI(bool enable) { var menuItems = MainWindow.Instance.GetMainMenuItems(); var toolbarItems = MainWindow.Instance.GetToolBarItems(); // menu var items = menuItems.OfType().Where(m => (m.Header as string) == "_Debugger"); foreach (var item in items.First().Items.OfType()) { string header = (string)item.Header; if (header.StartsWith("Remove")) continue; if (header.StartsWith("Attach") || header.StartsWith("Debug")) item.IsEnabled = enable; else item.IsEnabled = !enable; } //toolbar var buttons = toolbarItems.OfType