Browse Source

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

pull/14/head
Daniel Grunwald 15 years ago
parent
commit
d6b864e5ce
  1. 12
      src/AddIns/DisplayBindings/FormsDesigner/Project/FormsDesigner.csproj
  2. 24
      src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerViewContent.cs
  3. 46
      src/Main/Base/Project/Src/Util/SDWindowsFormsHost.cs

12
src/AddIns/DisplayBindings/FormsDesigner/Project/FormsDesigner.csproj

@ -47,13 +47,25 @@ @@ -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" />

24
src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerViewContent.cs

@ -482,8 +482,28 @@ namespace ICSharpCode.FormsDesigner @@ -482,8 +482,28 @@ namespace ICSharpCode.FormsDesigner
}
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)

46
src/Main/Base/Project/Src/Util/SDWindowsFormsHost.cs

@ -16,28 +16,37 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -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);
}
void AddBinding<T>(ICommand command, Action<T> execute, Predicate<T> canExecute) where T : class
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>(bool processShortcutsInWPF, ICommand command, Action<T> execute, Predicate<T> canExecute) where T : class
{
ExecutedRoutedEventHandler onExecuted = (sender, e) => {
if (e.Command == command) {
@ -58,13 +67,16 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -58,13 +67,16 @@ namespace ICSharpCode.SharpDevelop.Gui
}
}
};
//this.CommandBindings.Add(new CommandBinding(command, onExecuted, onCanExecute));
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
public override string ToString()

Loading…
Cancel
Save