Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 33189a8

Browse files
committed
ICSharpCode.Core.WinForms: Support WPF commands in WinForms menus.
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4501 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
1 parent 0aa7af2 commit 33189a8

5 files changed

Lines changed: 70 additions & 11 deletions

File tree

src/Main/Base/Project/Src/Gui/Workbench/WpfWorkbench.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,23 @@ public void Initialize()
9494
UpdateMenu();
9595

9696
AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(OnRequestNavigate));
97+
Project.ProjectService.CurrentProjectChanged += SetProjectTitle;
9798

9899
requerySuggestedEventHandler = new EventHandler(CommandManager_RequerySuggested);
99100
CommandManager.RequerySuggested += requerySuggestedEventHandler;
100101

101102
StatusBarService.SetMessage("${res:MainWindow.StatusBar.ReadyMessage}");
102103
}
103104

105+
// keep a reference to the event handler to prevent it from being garbage collected
106+
// (CommandManager.RequerySuggested only keeps weak references to the event handlers)
107+
EventHandler requerySuggestedEventHandler;
108+
109+
void CommandManager_RequerySuggested(object sender, EventArgs e)
110+
{
111+
UpdateMenu();
112+
}
113+
104114
void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
105115
{
106116
if (e.Uri.Scheme == "mailto") {
@@ -114,13 +124,13 @@ void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
114124
}
115125
}
116126

117-
// keep a reference to the event handler to prevent it from being garbage collected
118-
// (CommandManager.RequerySuggested only keeps weak references to the event handlers)
119-
EventHandler requerySuggestedEventHandler;
120-
121-
void CommandManager_RequerySuggested(object sender, EventArgs e)
127+
void SetProjectTitle(object sender, Project.ProjectEventArgs e)
122128
{
123-
UpdateMenu();
129+
if (e.Project != null) {
130+
Title = e.Project.Name + " - " + ResourceService.GetString("MainWindow.DialogName");
131+
} else {
132+
Title = ResourceService.GetString("MainWindow.DialogName");
133+
}
124134
}
125135

126136
void UpdateMenu()

src/Main/Base/Project/Src/Gui/Workbench/WpfWorkbench.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Window x:Class = "ICSharpCode.SharpDevelop.Gui.WpfWorkbench"
22
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
4-
Title = "SharpDevelop (experimental WPF build)"
4+
xmlns:core = "http://icsharpcode.net/sharpdevelop/core"
5+
Title = "{core:Localize MainWindow.DialogName}"
56
WindowStartupLocation = "Manual"
67
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
78
>

src/Main/ICSharpCode.Core.Presentation/Menu/MenuService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public static string ConvertLabel(string label)
217217
}
218218

219219
// HACK: find a better way to allow the host app to process link commands
220-
public static Converter<string, ICommand> LinkCommandCreator { get; set; }
220+
public static Func<string, ICommand> LinkCommandCreator { get; set; }
221221

222222
/// <summary>
223223
/// Creates an KeyGesture for a shortcut.

src/Main/ICSharpCode.Core.WinForms/Menu/MenuCommand.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,28 @@ public ICommand Command {
3737
}
3838

3939
// HACK: find a better way to allow the host app to process link commands
40-
public static Converter<string, ICommand> LinkCommandCreator;
40+
public static Func<string, ICommand> LinkCommandCreator { get; set; }
41+
42+
/// <summary>
43+
/// Callback that creates ICommand instances when the new syntax for known WPF commands (command="Copy") is used.
44+
/// </summary>
45+
public static Func<AddIn, string, ICommand> KnownCommandCreator { get; set; }
4146

4247
void CreateCommand()
4348
{
4449
try {
4550
string link = codon.Properties["link"];
51+
string command = codon.Properties["command"];
4652
if (link != null && link.Length > 0) {
47-
if (LinkCommandCreator == null)
53+
var callback = LinkCommandCreator;
54+
if (callback == null)
4855
throw new NotSupportedException("MenuCommand.LinkCommandCreator is not set, cannot create LinkCommands.");
49-
menuCommand = LinkCommandCreator(codon.Properties["link"]);
56+
menuCommand = callback(link);
57+
} else if (command != null && command.Length > 0) {
58+
var callback = KnownCommandCreator;
59+
if (callback == null)
60+
throw new NotSupportedException("MenuCommand.KnownCommandCreator is not set, cannot create commands.");
61+
menuCommand = callback(codon.AddIn, command);
5062
} else {
5163
menuCommand = (ICommand)codon.AddIn.CreateObject(codon.Properties["class"]);
5264
}

src/Main/ICSharpCode.SharpDevelop.Sda/Src/CallHelper.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void InitSharpDevelopCore(SharpDevelopHost.CallbackHelper callback, Start
6767
ResourceService.RegisterNeutralImages(new ResourceManager("Resources.BitmapResources", exe));
6868

6969
MenuCommand.LinkCommandCreator = delegate(string link) { return new LinkCommand(link); };
70+
MenuCommand.KnownCommandCreator = CreateICommandForWPFCommand;
7071
Core.Presentation.MenuService.LinkCommandCreator = MenuCommand.LinkCommandCreator;
7172
StringParser.RegisterStringTagProvider(new SharpDevelopStringTagProvider());
7273

@@ -100,6 +101,41 @@ public void InitSharpDevelopCore(SharpDevelopHost.CallbackHelper callback, Start
100101

101102
LoggingService.Info("InitSharpDevelop finished");
102103
}
104+
105+
static ICommand CreateICommandForWPFCommand(AddIn addIn, string commandName)
106+
{
107+
var wpfCommand = Core.Presentation.MenuService.GetRegisteredCommand(addIn, commandName);
108+
if (wpfCommand != null)
109+
return new WpfCommandWrapper(wpfCommand);
110+
else
111+
return null;
112+
}
113+
114+
sealed class WpfCommandWrapper : AbstractCommand
115+
{
116+
readonly System.Windows.Input.ICommand wpfCommand;
117+
118+
public WpfCommandWrapper(System.Windows.Input.ICommand wpfCommand)
119+
{
120+
this.wpfCommand = wpfCommand;
121+
}
122+
123+
public override void Run()
124+
{
125+
var routedCommand = wpfCommand as System.Windows.Input.RoutedCommand;
126+
if (routedCommand != null) {
127+
var target = System.Windows.Input.FocusManager.GetFocusedElement(WorkbenchSingleton.MainWindow);
128+
routedCommand.Execute(this.Owner, target);
129+
} else {
130+
wpfCommand.Execute(this.Owner);
131+
}
132+
}
133+
134+
public override string ToString()
135+
{
136+
return "[WpfCommandWrapper " + wpfCommand + "]";
137+
}
138+
}
103139
#endregion
104140

105141
#region Initialize and run Workbench

0 commit comments

Comments
 (0)