Permalink
Browse files

Fix SD-1737 - Keyboard shortcuts do not work in WinForms Designer

  • Loading branch information...
dgrunwald committed Nov 27, 2010
1 parent e7fbe12 commit d6b864e5ce53fd0fc19d3efd25725ff2e2a8bb22
@@ -47,13 +47,25 @@
<Reference Include="PresentationCore">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
+ <Reference Include="PresentationFramework">
+ <RequiredTargetFramework>3.0</RequiredTargetFramework>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Drawing.Design" />
<Reference Include="System.Windows.Forms" />
+ <Reference Include="System.Xaml">
+ <RequiredTargetFramework>4.0</RequiredTargetFramework>
+ </Reference>
<Reference Include="System.XML" />
+ <Reference Include="WindowsBase">
+ <RequiredTargetFramework>3.0</RequiredTargetFramework>
+ </Reference>
+ <Reference Include="WindowsFormsIntegration">
+ <RequiredTargetFramework>3.0</RequiredTargetFramework>
+ </Reference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\AddSidebarComponentsDialog.xfrm" />
@@ -482,8 +482,28 @@ void LoadAndDisplayDesigner()
}
internal new Control UserContent {
- get { return base.UserContent as Control; }
- set { base.UserContent = value; }
+ get {
+ SDWindowsFormsHost host = base.UserContent as SDWindowsFormsHost;
+ return host != null ? host.Child : null;
+ }
+ set {
+ SDWindowsFormsHost host = base.UserContent as SDWindowsFormsHost;
+ if (value == null) {
+ base.UserContent = null;
+ if (host != null)
+ host.Dispose();
+ return;
+ }
+ if (host != null && host.Child == value)
+ return;
+ if (host == null) {
+ host = new SDWindowsFormsHost(true);
+ host.ServiceObject = this;
+ host.DisposeChild = false;
+ }
+ host.Child = value;
+ base.UserContent = host;
+ }
}
void DesignerLoading(object sender, EventArgs e)
@@ -16,28 +16,37 @@ namespace ICSharpCode.SharpDevelop.Gui
/// </summary>
public class SDWindowsFormsHost : WindowsFormsHost
{
- public SDWindowsFormsHost()
+ /// <summary>
+ /// Creates a new SDWindowsFormsHost instance.
+ /// </summary>
+ /// <param name="processShortcutsInWPF">
+ /// Determines whether the shortcuts for the default actions (Cut,Copy,Paste,Undo, etc.)
+ /// are processed by the WPF command system.
+ /// The default value is false. Pass true only if WinForms does not handle those shortcuts by itself.
+ /// See SD-1671 and SD-1737.
+ /// </param>
+ public SDWindowsFormsHost(bool processShortcutsInWPF = false)
{
this.DisposeChild = true;
- CreateBindings();
+ CreateBindings(processShortcutsInWPF);
}
#region Binding
- void CreateBindings()
+ void CreateBindings(bool processShortcutsInWPF)
{
- AddBinding(ApplicationCommands.Copy, (IClipboardHandler c) => c.Copy(), c => c.EnableCopy);
- AddBinding(ApplicationCommands.Cut, (IClipboardHandler c) => c.Cut(), c => c.EnableCut);
- AddBinding(ApplicationCommands.Paste, (IClipboardHandler c) => c.Paste(), c => c.EnablePaste);
- AddBinding(ApplicationCommands.Delete, (IClipboardHandler c) => c.Delete(), c => c.EnableDelete);
- AddBinding(ApplicationCommands.SelectAll, (IClipboardHandler c) => c.SelectAll(), c => c.EnableSelectAll);
- AddBinding(ApplicationCommands.Help, (IContextHelpProvider h) => h.ShowHelp(), h => true);
- AddBinding(ApplicationCommands.Undo, (IUndoHandler u) => u.Undo(), u => u.EnableUndo);
- AddBinding(ApplicationCommands.Redo, (IUndoHandler u) => u.Redo(), u => u.EnableRedo);
- AddBinding(ApplicationCommands.Print, (IPrintable p) => WindowsFormsPrinting.Print(p), p => true);
- AddBinding(ApplicationCommands.PrintPreview, (IPrintable p) => WindowsFormsPrinting.PrintPreview(p), p => true);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Copy, (IClipboardHandler c) => c.Copy(), c => c.EnableCopy);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Cut, (IClipboardHandler c) => c.Cut(), c => c.EnableCut);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Paste, (IClipboardHandler c) => c.Paste(), c => c.EnablePaste);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Delete, (IClipboardHandler c) => c.Delete(), c => c.EnableDelete);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.SelectAll, (IClipboardHandler c) => c.SelectAll(), c => c.EnableSelectAll);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Help, (IContextHelpProvider h) => h.ShowHelp(), h => true);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Undo, (IUndoHandler u) => u.Undo(), u => u.EnableUndo);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Redo, (IUndoHandler u) => u.Redo(), u => u.EnableRedo);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.Print, (IPrintable p) => WindowsFormsPrinting.Print(p), p => true);
+ AddBinding(processShortcutsInWPF, ApplicationCommands.PrintPreview, (IPrintable p) => WindowsFormsPrinting.PrintPreview(p), p => true);
}
- void AddBinding<T>(ICommand command, Action<T> execute, Predicate<T> canExecute) where T : class
+ void AddBinding<T>(bool processShortcutsInWPF, ICommand command, Action<T> execute, Predicate<T> canExecute) where T : class
{
ExecutedRoutedEventHandler onExecuted = (sender, e) => {
if (e.Command == command) {
@@ -58,12 +67,15 @@ void CreateBindings()
}
}
};
- //this.CommandBindings.Add(new CommandBinding(command, onExecuted, onCanExecute));
- // Don't use this.CommandBindings because CommandBindings with built-in shortcuts would handle the key press
- // before WinForms gets to see it. Using the events ensures that the command gets executed only when the user
- // clicks on the menu/toolbar item. (this fixes SD2-1671)
- CommandManager.AddCanExecuteHandler(this, onCanExecute);
- CommandManager.AddExecutedHandler(this, onExecuted);
+ if (processShortcutsInWPF) {
+ this.CommandBindings.Add(new CommandBinding(command, onExecuted, onCanExecute));
+ } else {
+ // Don't use this.CommandBindings because CommandBindings with built-in shortcuts would handle the key press
+ // before WinForms gets to see it. Using the events ensures that the command gets executed only when the user
+ // clicks on the menu/toolbar item. (this fixes SD2-1671)
+ CommandManager.AddCanExecuteHandler(this, onCanExecute);
+ CommandManager.AddExecutedHandler(this, onExecuted);
+ }
}
#endregion

0 comments on commit d6b864e

Please sign in to comment.