mirror of https://github.com/icsharpcode/ILSpy.git
18 changed files with 283 additions and 68 deletions
@ -0,0 +1,122 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.ComponentModel.Composition; |
||||||
|
using System.Linq; |
||||||
|
using System.Windows.Controls; |
||||||
|
|
||||||
|
using ICSharpCode.TreeView; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy |
||||||
|
{ |
||||||
|
public interface IContextMenuEntry |
||||||
|
{ |
||||||
|
bool IsVisible(SharpTreeNode[] selectedNodes); |
||||||
|
bool IsEnabled(SharpTreeNode[] selectedNodes); |
||||||
|
void Execute(SharpTreeNode[] selectedNodes); |
||||||
|
} |
||||||
|
|
||||||
|
public interface IContextMenuEntryMetadata |
||||||
|
{ |
||||||
|
string Icon { get; } |
||||||
|
string Header { get; } |
||||||
|
string Category { get; } |
||||||
|
|
||||||
|
double Order { get; } |
||||||
|
} |
||||||
|
|
||||||
|
[MetadataAttribute] |
||||||
|
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)] |
||||||
|
public class ExportContextMenuEntryAttribute : ExportAttribute, IContextMenuEntryMetadata |
||||||
|
{ |
||||||
|
public ExportContextMenuEntryAttribute() |
||||||
|
: base(typeof(IContextMenuEntry)) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public string Icon { get; set; } |
||||||
|
public string Header { get; set; } |
||||||
|
public string Category { get; set; } |
||||||
|
public double Order { get; set; } |
||||||
|
} |
||||||
|
|
||||||
|
internal class ContextMenuProvider |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Enables extensible context menu support for the specified tree view.
|
||||||
|
/// </summary>
|
||||||
|
public static void Add(SharpTreeView treeView) |
||||||
|
{ |
||||||
|
var provider = new ContextMenuProvider(treeView); |
||||||
|
treeView.ContextMenuOpening += provider.treeView_ContextMenuOpening; |
||||||
|
treeView.ContextMenuClosing -= provider.treeView_ContextMenuClosing; |
||||||
|
} |
||||||
|
|
||||||
|
readonly SharpTreeView treeView; |
||||||
|
|
||||||
|
[ImportMany(typeof(IContextMenuEntry))] |
||||||
|
Lazy<IContextMenuEntry, IContextMenuEntryMetadata>[] entries; |
||||||
|
|
||||||
|
private ContextMenuProvider(SharpTreeView treeView) |
||||||
|
{ |
||||||
|
this.treeView = treeView; |
||||||
|
App.CompositionContainer.ComposeParts(this); |
||||||
|
} |
||||||
|
|
||||||
|
void treeView_ContextMenuOpening(object sender, ContextMenuEventArgs e) |
||||||
|
{ |
||||||
|
SharpTreeNode[] selectedNodes = treeView.GetTopLevelSelection().ToArray(); |
||||||
|
if (selectedNodes.Length == 0) |
||||||
|
return; |
||||||
|
ContextMenu menu = new ContextMenu(); |
||||||
|
foreach (var category in entries.OrderBy(c => c.Metadata.Order).GroupBy(c => c.Metadata.Category)) { |
||||||
|
if (menu.Items.Count > 0) { |
||||||
|
menu.Items.Add(new Separator()); |
||||||
|
} |
||||||
|
foreach (var entryPair in category) { |
||||||
|
IContextMenuEntry entry = entryPair.Value; |
||||||
|
if (entry.IsVisible(selectedNodes)) { |
||||||
|
MenuItem menuItem = new MenuItem(); |
||||||
|
menuItem.Header = entryPair.Metadata.Header; |
||||||
|
if (!string.IsNullOrEmpty(entryPair.Metadata.Icon)) { |
||||||
|
menuItem.Icon = new Image { |
||||||
|
Width = 16, |
||||||
|
Height = 16, |
||||||
|
Source = Images.LoadImage(entry, entryPair.Metadata.Icon) |
||||||
|
}; |
||||||
|
} |
||||||
|
if (entryPair.Value.IsEnabled(selectedNodes)) { |
||||||
|
menuItem.Click += delegate { |
||||||
|
entry.Execute(selectedNodes); |
||||||
|
}; |
||||||
|
} |
||||||
|
menu.Items.Add(menuItem); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (menu.Items.Count > 0) |
||||||
|
treeView.ContextMenu = menu; |
||||||
|
} |
||||||
|
|
||||||
|
void treeView_ContextMenuClosing(object sender, ContextMenuEventArgs e) |
||||||
|
{ |
||||||
|
treeView.ContextMenu = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Linq; |
||||||
|
using ICSharpCode.TreeView; |
||||||
|
using Mono.Cecil; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer |
||||||
|
{ |
||||||
|
[ExportContextMenuEntry(Header = "Analyze", Icon = "images/Search.png")] |
||||||
|
sealed class AnalyzeContextMenuEntry : IContextMenuEntry |
||||||
|
{ |
||||||
|
public bool IsVisible(SharpTreeNode[] selectedNodes) |
||||||
|
{ |
||||||
|
return selectedNodes.All(n => n is IMemberTreeNode); |
||||||
|
} |
||||||
|
|
||||||
|
public bool IsEnabled(SharpTreeNode[] selectedNodes) |
||||||
|
{ |
||||||
|
foreach (IMemberTreeNode node in selectedNodes) { |
||||||
|
if (!(node.Member is FieldDefinition || node.Member is MethodDefinition)) |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public void Execute(SharpTreeNode[] selectedNodes) |
||||||
|
{ |
||||||
|
// TODO: figure out when equivalent nodes are already present
|
||||||
|
// and focus those instead.
|
||||||
|
foreach (IMemberTreeNode node in selectedNodes) { |
||||||
|
FieldDefinition field = node.Member as FieldDefinition; |
||||||
|
if (field != null) |
||||||
|
MainWindow.Instance.AddToAnalyzer(new AnalyzedFieldNode(field)); |
||||||
|
MethodDefinition method = node.Member as MethodDefinition; |
||||||
|
if (method != null) |
||||||
|
MainWindow.Instance.AddToAnalyzer(new AnalyzedMethodTreeNode(method)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using Mono.Cecil; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Interface implemented by all tree nodes
|
||||||
|
/// (both in main tree view and in analyzer)
|
||||||
|
/// that represent Cecil members.
|
||||||
|
/// </summary>
|
||||||
|
public interface IMemberTreeNode |
||||||
|
{ |
||||||
|
MemberReference Member { get; } |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue