Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2425 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
32 changed files with 758 additions and 252 deletions
After Width: | Height: | Size: 575 B |
@ -0,0 +1,115 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.Text; |
||||||
|
using System.Windows; |
||||||
|
using System.Windows.Controls; |
||||||
|
using System.Windows.Data; |
||||||
|
using System.Windows.Documents; |
||||||
|
using System.Windows.Input; |
||||||
|
using System.Windows.Media; |
||||||
|
using System.Windows.Media.Imaging; |
||||||
|
using System.Windows.Navigation; |
||||||
|
using System.Windows.Shapes; |
||||||
|
using ICSharpCode.WpfDesign; |
||||||
|
using ICSharpCode.WpfDesign.Designer.Services; |
||||||
|
|
||||||
|
namespace StandaloneDesigner |
||||||
|
{ |
||||||
|
public partial class Toolbox : ListBox |
||||||
|
{ |
||||||
|
public Toolbox() |
||||||
|
{ |
||||||
|
this.SelectionMode = SelectionMode.Single; |
||||||
|
} |
||||||
|
|
||||||
|
IToolService toolService; |
||||||
|
|
||||||
|
public IToolService ToolService { |
||||||
|
get { return toolService; } |
||||||
|
set { |
||||||
|
if (toolService != null) { |
||||||
|
toolService.CurrentToolChanged -= OnCurrentToolChanged; |
||||||
|
} |
||||||
|
toolService = value; |
||||||
|
this.Items.Clear(); |
||||||
|
if (toolService != null) { |
||||||
|
AddTool("Pointer", toolService.PointerTool); |
||||||
|
AddTool(typeof(Button)); |
||||||
|
AddTool(typeof(TextBox)); |
||||||
|
AddTool(typeof(CheckBox)); |
||||||
|
|
||||||
|
toolService.CurrentToolChanged += OnCurrentToolChanged; |
||||||
|
OnCurrentToolChanged(null, null); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void AddTool(Type componentType) |
||||||
|
{ |
||||||
|
AddTool(componentType.Name, new CreateComponentTool(componentType)); |
||||||
|
} |
||||||
|
|
||||||
|
void AddTool(string title, ITool tool) |
||||||
|
{ |
||||||
|
ListBoxItem item = new ListBoxItem(); |
||||||
|
item.Content = title; |
||||||
|
item.Tag = tool; |
||||||
|
this.Items.Add(item); |
||||||
|
} |
||||||
|
|
||||||
|
void OnCurrentToolChanged(object sender, EventArgs e) |
||||||
|
{ |
||||||
|
Debug.WriteLine("Toolbox.OnCurrentToolChanged"); |
||||||
|
for (int i = 0; i < this.Items.Count; i++) { |
||||||
|
if (((ListBoxItem)this.Items[i]).Tag == toolService.CurrentTool) { |
||||||
|
this.SelectedIndex = i; |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
this.SelectedIndex = -1; |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnSelectionChanged(SelectionChangedEventArgs e) |
||||||
|
{ |
||||||
|
base.OnSelectionChanged(e); |
||||||
|
|
||||||
|
Debug.WriteLine("Toolbox.OnSelectionChanged"); |
||||||
|
if (toolService != null && this.SelectedItem is ListBoxItem) { |
||||||
|
toolService.CurrentTool = (ITool)(this.SelectedItem as ListBoxItem).Tag; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Point startPos; |
||||||
|
bool canStartDrag; |
||||||
|
|
||||||
|
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) |
||||||
|
{ |
||||||
|
startPos = e.GetPosition(this); |
||||||
|
canStartDrag = true; |
||||||
|
|
||||||
|
base.OnPreviewMouseLeftButtonDown(e); |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnMouseMove(MouseEventArgs e) |
||||||
|
{ |
||||||
|
if (canStartDrag && e.LeftButton == MouseButtonState.Pressed) { |
||||||
|
if ((e.GetPosition(this) - startPos).LengthSquared > 4) { |
||||||
|
canStartDrag = false; |
||||||
|
|
||||||
|
if (this.SelectedItem == null) |
||||||
|
return; |
||||||
|
|
||||||
|
if (toolService != null && this.SelectedItem is ListBoxItem) { |
||||||
|
ITool tool = (ITool)(this.SelectedItem as ListBoxItem).Tag; |
||||||
|
if (tool is CreateComponentTool) { |
||||||
|
DragDrop.DoDragDrop((ListBoxItem)this.SelectedItem, tool, DragDropEffects.Copy); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
base.OnMouseMove(e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,182 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System.Windows; |
||||||
|
using System; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.Windows.Input; |
||||||
|
using ICSharpCode.WpfDesign.Adorners; |
||||||
|
using ICSharpCode.WpfDesign.Designer.Controls; |
||||||
|
|
||||||
|
namespace ICSharpCode.WpfDesign.Designer.Services |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// A tool that creates a component when used.
|
||||||
|
/// </summary>
|
||||||
|
public class CreateComponentTool : ITool |
||||||
|
{ |
||||||
|
readonly Type componentType; |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new CreateComponentTool instance.
|
||||||
|
/// </summary>
|
||||||
|
public CreateComponentTool(Type componentType) |
||||||
|
{ |
||||||
|
if (componentType == null) |
||||||
|
throw new ArgumentNullException("componentType"); |
||||||
|
this.componentType = componentType; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of the component to be created.
|
||||||
|
/// </summary>
|
||||||
|
public Type ComponentType { |
||||||
|
get { return componentType; } |
||||||
|
} |
||||||
|
|
||||||
|
/// <inherits/>
|
||||||
|
public Cursor Cursor { |
||||||
|
get { return null; } |
||||||
|
} |
||||||
|
|
||||||
|
/// <inherits/>
|
||||||
|
public void Activate(IDesignPanel designPanel) |
||||||
|
{ |
||||||
|
designPanel.MouseDown += OnMouseDown; |
||||||
|
designPanel.DragOver += OnDragOver; |
||||||
|
designPanel.Drop += OnDrop; |
||||||
|
} |
||||||
|
|
||||||
|
/// <inherits/>
|
||||||
|
public void Deactivate(IDesignPanel designPanel) |
||||||
|
{ |
||||||
|
designPanel.MouseDown -= OnMouseDown; |
||||||
|
designPanel.DragOver -= OnDragOver; |
||||||
|
designPanel.Drop -= OnDrop; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is called to create the item used by the CreateComponentTool.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual DesignItem CreateItem(DesignContext context) |
||||||
|
{ |
||||||
|
object newInstance = Activator.CreateInstance(componentType); |
||||||
|
return context.Services.Component.RegisterComponentForDesigner(newInstance); |
||||||
|
} |
||||||
|
|
||||||
|
void OnDragOver(object sender, DragEventArgs e) |
||||||
|
{ |
||||||
|
if (e.Data.GetData(typeof(CreateComponentTool)) == this) { |
||||||
|
e.Effects = DragDropEffects.Copy; |
||||||
|
e.Handled = true; |
||||||
|
} else { |
||||||
|
e.Effects = DragDropEffects.None; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void OnDrop(object sender, DragEventArgs e) |
||||||
|
{ |
||||||
|
if (e.Data.GetData(typeof(CreateComponentTool)) != this) |
||||||
|
return; |
||||||
|
e.Handled = true; |
||||||
|
MessageBox.Show("Not implemented"); |
||||||
|
} |
||||||
|
|
||||||
|
void OnMouseDown(object sender, MouseButtonEventArgs e) |
||||||
|
{ |
||||||
|
if (e.ChangedButton == MouseButton.Left && MouseGestureBase.IsOnlyButtonPressed(e, MouseButton.Left)) { |
||||||
|
e.Handled = true; |
||||||
|
IDesignPanel designPanel = (IDesignPanel)sender; |
||||||
|
DesignPanelHitTestResult result = designPanel.HitTest(e, false, true); |
||||||
|
if (result.ModelHit != null) { |
||||||
|
IPlacementBehavior behavior = result.ModelHit.GetBehavior<IPlacementBehavior>(); |
||||||
|
if (behavior != null) { |
||||||
|
// ensure the design panel has the focus - otherwise pressing Escape to abort creating doesn't work
|
||||||
|
designPanel.Focus(); |
||||||
|
|
||||||
|
DesignItem createdItem = CreateItem(designPanel.Context); |
||||||
|
|
||||||
|
new CreateComponentMouseGesture(result.ModelHit, createdItem).Start(designPanel, e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
sealed class CreateComponentMouseGesture : ClickOrDragMouseGesture |
||||||
|
{ |
||||||
|
DesignItem createdItem; |
||||||
|
PlacementOperation operation; |
||||||
|
DesignItem container; |
||||||
|
|
||||||
|
public CreateComponentMouseGesture(DesignItem clickedOn, DesignItem createdItem) |
||||||
|
{ |
||||||
|
this.container = clickedOn; |
||||||
|
this.createdItem = createdItem; |
||||||
|
this.positionRelativeTo = clickedOn.View; |
||||||
|
} |
||||||
|
|
||||||
|
// GrayOutDesignerExceptActiveArea grayOut;
|
||||||
|
// SelectionFrame frame;
|
||||||
|
// AdornerPanel adornerPanel;
|
||||||
|
|
||||||
|
Rect GetStartToEndRect(MouseEventArgs e) |
||||||
|
{ |
||||||
|
Point endPoint = e.GetPosition(positionRelativeTo); |
||||||
|
return new Rect( |
||||||
|
Math.Min(startPoint.X, endPoint.X), |
||||||
|
Math.Min(startPoint.Y, endPoint.Y), |
||||||
|
Math.Abs(startPoint.X - endPoint.X), |
||||||
|
Math.Abs(startPoint.Y - endPoint.Y) |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnDragStarted(MouseEventArgs e) |
||||||
|
{ |
||||||
|
operation = PlacementOperation.TryStartInsertNewComponents(container, |
||||||
|
new DesignItem[] { createdItem }, |
||||||
|
new Rect[] { GetStartToEndRect(e) }, |
||||||
|
PlacementType.Resize); |
||||||
|
if (operation != null) { |
||||||
|
services.Selection.SetSelectedComponents(new DesignItem[] { createdItem }); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnMouseMove(object sender, MouseEventArgs e) |
||||||
|
{ |
||||||
|
base.OnMouseMove(sender, e); |
||||||
|
if (operation != null) { |
||||||
|
foreach (PlacementInformation info in operation.PlacedItems) { |
||||||
|
info.Bounds = GetStartToEndRect(e); |
||||||
|
operation.CurrentContainerBehavior.SetPosition(info); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnMouseUp(object sender, MouseButtonEventArgs e) |
||||||
|
{ |
||||||
|
if (hasDragStarted) { |
||||||
|
if (operation != null) { |
||||||
|
operation.Commit(); |
||||||
|
operation = null; |
||||||
|
} |
||||||
|
} else { |
||||||
|
|
||||||
|
} |
||||||
|
base.OnMouseUp(sender, e); |
||||||
|
} |
||||||
|
|
||||||
|
protected override void OnStopped() |
||||||
|
{ |
||||||
|
if (operation != null) { |
||||||
|
operation.Abort(); |
||||||
|
operation = null; |
||||||
|
} |
||||||
|
base.OnStopped(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,114 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.Windows.Input; |
||||||
|
|
||||||
|
namespace ICSharpCode.WpfDesign.Designer.Services |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Base class for classes handling mouse gestures on the design surface.
|
||||||
|
/// </summary>
|
||||||
|
abstract class MouseGestureBase |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Checks if <paramref name="button"/> is the only button that is currently pressed.
|
||||||
|
/// </summary>
|
||||||
|
internal static bool IsOnlyButtonPressed(MouseEventArgs e, MouseButton button) |
||||||
|
{ |
||||||
|
return e.LeftButton == (button == MouseButton.Left ? MouseButtonState.Pressed : MouseButtonState.Released) |
||||||
|
&& e.MiddleButton == (button == MouseButton.Middle ? MouseButtonState.Pressed : MouseButtonState.Released) |
||||||
|
&& e.RightButton == (button == MouseButton.Right ? MouseButtonState.Pressed : MouseButtonState.Released) |
||||||
|
&& e.XButton1 == (button == MouseButton.XButton1 ? MouseButtonState.Pressed : MouseButtonState.Released) |
||||||
|
&& e.XButton2 == (button == MouseButton.XButton2 ? MouseButtonState.Pressed : MouseButtonState.Released); |
||||||
|
} |
||||||
|
|
||||||
|
protected IDesignPanel designPanel; |
||||||
|
protected ServiceContainer services; |
||||||
|
protected bool canAbortWithEscape = true; |
||||||
|
bool isStarted; |
||||||
|
|
||||||
|
public void Start(IDesignPanel designPanel, MouseButtonEventArgs e) |
||||||
|
{ |
||||||
|
if (designPanel == null) |
||||||
|
throw new ArgumentNullException("designPanel"); |
||||||
|
if (e == null) |
||||||
|
throw new ArgumentNullException("e"); |
||||||
|
if (isStarted) |
||||||
|
throw new InvalidOperationException("Gesture already was started"); |
||||||
|
|
||||||
|
isStarted = true; |
||||||
|
this.designPanel = designPanel; |
||||||
|
this.services = designPanel.Context.Services; |
||||||
|
designPanel.IsAdornerLayerHitTestVisible = false; |
||||||
|
if (designPanel.CaptureMouse()) { |
||||||
|
RegisterEvents(); |
||||||
|
OnStarted(e); |
||||||
|
} else { |
||||||
|
Stop(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void RegisterEvents() |
||||||
|
{ |
||||||
|
designPanel.LostMouseCapture += OnLostMouseCapture; |
||||||
|
designPanel.MouseDown += OnMouseDown; |
||||||
|
designPanel.MouseMove += OnMouseMove; |
||||||
|
designPanel.MouseUp += OnMouseUp; |
||||||
|
designPanel.KeyDown += OnKeyDown; |
||||||
|
} |
||||||
|
|
||||||
|
void UnRegisterEvents() |
||||||
|
{ |
||||||
|
designPanel.LostMouseCapture -= OnLostMouseCapture; |
||||||
|
designPanel.MouseDown -= OnMouseDown; |
||||||
|
designPanel.MouseMove -= OnMouseMove; |
||||||
|
designPanel.MouseUp -= OnMouseUp; |
||||||
|
designPanel.KeyDown -= OnKeyDown; |
||||||
|
} |
||||||
|
|
||||||
|
void OnKeyDown(object sender, KeyEventArgs e) |
||||||
|
{ |
||||||
|
if (canAbortWithEscape && e.Key == Key.Escape) { |
||||||
|
e.Handled = true; |
||||||
|
Stop(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void OnLostMouseCapture(object sender, MouseEventArgs e) |
||||||
|
{ |
||||||
|
Stop(); |
||||||
|
} |
||||||
|
|
||||||
|
protected virtual void OnMouseDown(object sender, MouseButtonEventArgs e) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
protected virtual void OnMouseMove(object sender, MouseEventArgs e) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
protected virtual void OnMouseUp(object sender, MouseButtonEventArgs e) |
||||||
|
{ |
||||||
|
Stop(); |
||||||
|
} |
||||||
|
|
||||||
|
protected void Stop() |
||||||
|
{ |
||||||
|
if (!isStarted) return; |
||||||
|
isStarted = false; |
||||||
|
designPanel.ReleaseMouseCapture(); |
||||||
|
UnRegisterEvents(); |
||||||
|
designPanel.IsAdornerLayerHitTestVisible = true; |
||||||
|
OnStopped(); |
||||||
|
} |
||||||
|
|
||||||
|
protected virtual void OnStarted(MouseButtonEventArgs e) {} |
||||||
|
protected virtual void OnStopped() {} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,52 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Windows.Input; |
||||||
|
|
||||||
|
namespace ICSharpCode.WpfDesign.Designer.Services |
||||||
|
{ |
||||||
|
sealed class PointerTool : ITool |
||||||
|
{ |
||||||
|
internal static readonly PointerTool Instance = new PointerTool(); |
||||||
|
|
||||||
|
public Cursor Cursor { |
||||||
|
get { return null; } |
||||||
|
} |
||||||
|
|
||||||
|
public void Activate(IDesignPanel designPanel) |
||||||
|
{ |
||||||
|
designPanel.MouseDown += OnMouseDown; |
||||||
|
} |
||||||
|
|
||||||
|
public void Deactivate(IDesignPanel designPanel) |
||||||
|
{ |
||||||
|
designPanel.MouseDown -= OnMouseDown; |
||||||
|
} |
||||||
|
|
||||||
|
void OnMouseDown(object sender, MouseButtonEventArgs e) |
||||||
|
{ |
||||||
|
if (e.ChangedButton == MouseButton.Left && MouseGestureBase.IsOnlyButtonPressed(e, MouseButton.Left)) { |
||||||
|
e.Handled = true; |
||||||
|
IDesignPanel designPanel = (IDesignPanel)sender; |
||||||
|
DesignPanelHitTestResult result = designPanel.HitTest(e, false, true); |
||||||
|
if (result.ModelHit != null) { |
||||||
|
IHandlePointerToolMouseDown b = result.ModelHit.GetBehavior<IHandlePointerToolMouseDown>(); |
||||||
|
if (b != null) { |
||||||
|
b.HandleSelectionMouseDown(designPanel, e, result); |
||||||
|
} else { |
||||||
|
ISelectionService selectionService = designPanel.Context.Services.Selection; |
||||||
|
selectionService.SetSelectedComponents(new DesignItem[] { result.ModelHit }, SelectionTypes.Auto); |
||||||
|
if (selectionService.IsComponentSelected(result.ModelHit)) { |
||||||
|
new DragMoveMouseGesture(result.ModelHit).Start(designPanel, e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue