Browse Source

Enable changing margins through mouse-click on the Handle.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/wpfdesigner@6265 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Kumar Devvrat 16 years ago
parent
commit
9787dc9e13
  1. 4
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/ControlStyles.xaml
  2. 41
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/MarginHandle.cs
  3. 173
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/MarginHandleExtension.cs

4
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/ControlStyles.xaml

@ -353,6 +353,7 @@ @@ -353,6 +353,7 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="8" />
</Grid.ColumnDefinitions>
<Rectangle Fill="Transparent" Height="10" Grid.Column="0" Grid.ColumnSpan="4"/>
<Path Name="line1"
Stretch="Fill"
Stroke="{StaticResource HandleBrush}"
@ -398,7 +399,8 @@ @@ -398,7 +399,8 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:MarginStub}">
<Grid SnapsToDevicePixels="True">
<Grid SnapsToDevicePixels="True" Height="10" Width="10">
<Rectangle Fill="Transparent"/>
<Ellipse
Fill="{TemplateBinding Panel.Background}"
Height="7"

41
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/MarginHandle.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Windows.Input;
using System.Globalization;
using System.ComponentModel;
using System.Diagnostics;
@ -104,6 +105,13 @@ namespace ICSharpCode.WpfDesign.Designer.Controls @@ -104,6 +105,13 @@ namespace ICSharpCode.WpfDesign.Designer.Controls
/// Decides whether stub has to be only displayed.
/// </summary>
public bool DisplayOnlyStub { get; set; }
/// <summary>
/// Gets the orientation of the handle.
/// </summary>
public HandleOrientation Orientation {
get { return orientation; }
}
public MarginHandle(DesignItem adornedControlItem, AdornerPanel adornerPanel, HandleOrientation orientation)
{
@ -220,7 +228,19 @@ namespace ICSharpCode.WpfDesign.Designer.Controls @@ -220,7 +228,19 @@ namespace ICSharpCode.WpfDesign.Designer.Controls
DisplayOnlyStub = va.ToString() != orientation.ToString();
}
DecideVisiblity(this.HandleLength);
}
}
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
{
base.OnMouseEnter(e);
this.Cursor = Cursors.Hand;
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
this.Cursor = Cursors.Arrow;
}
public override void OnApplyTemplate()
{
@ -238,6 +258,14 @@ namespace ICSharpCode.WpfDesign.Designer.Controls @@ -238,6 +258,14 @@ namespace ICSharpCode.WpfDesign.Designer.Controls
public class MarginStub : Control
{
MarginHandle marginHandle;
/// <summary>
/// Gets the margin handle using this stub.
/// </summary>
public MarginHandle Handle{
get { return marginHandle; }
}
static MarginStub()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MarginStub), new FrameworkPropertyMetadata(typeof(MarginStub)));
@ -254,6 +282,17 @@ namespace ICSharpCode.WpfDesign.Designer.Controls @@ -254,6 +282,17 @@ namespace ICSharpCode.WpfDesign.Designer.Controls
marginHandle.DecideVisiblity(marginHandle.HandleLength);
}
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
{
base.OnMouseEnter(e);
this.Cursor = Cursors.Hand;
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
this.Cursor = Cursors.Arrow;
}
}
/// <summary>

173
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/MarginHandleExtension.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
@ -16,10 +17,12 @@ using ICSharpCode.WpfDesign.Extensions; @@ -16,10 +17,12 @@ using ICSharpCode.WpfDesign.Extensions;
namespace ICSharpCode.WpfDesign.Designer.Extensions
{
[ExtensionFor(typeof(FrameworkElement))]
[ExtensionServer(typeof(PrimarySelectionExtensionServer))]
[ExtensionServer(typeof(PrimarySelectionExtensionServer))]
public class MarginHandleExtension : AdornerProvider
{
//TODO : Use array to store all the handles.
private MarginHandle _leftHandle, _topHandle, _rightHandle, _bottomHandle;
private Grid _grid;
protected override void OnInitialized()
{
@ -33,10 +36,24 @@ namespace ICSharpCode.WpfDesign.Designer.Extensions @@ -33,10 +36,24 @@ namespace ICSharpCode.WpfDesign.Designer.Extensions
// If the Element is rotated/skewed in the grid, then margin handles do not appear
if (extendedControl.LayoutTransform.Value == Matrix.Identity && extendedControl.RenderTransform.Value == Matrix.Identity)
{
_grid = this.ExtendedItem.Parent.View as Grid;
_leftHandle = new MarginHandle(this.ExtendedItem, adornerPanel, HandleOrientation.Left);
_topHandle = new MarginHandle(this.ExtendedItem, adornerPanel, HandleOrientation.Top);
_rightHandle = new MarginHandle(this.ExtendedItem, adornerPanel, HandleOrientation.Right);
_bottomHandle = new MarginHandle(this.ExtendedItem, adornerPanel, HandleOrientation.Bottom);
_leftHandle.MouseLeftButtonDown += OnMouseDown;
_leftHandle.Stub.MouseLeftButtonDown += OnMouseDown;
_topHandle.MouseLeftButtonDown += OnMouseDown;
_topHandle.Stub.MouseLeftButtonDown += OnMouseDown;
_rightHandle.MouseLeftButtonDown += OnMouseDown;
_rightHandle.Stub.MouseLeftButtonDown += OnMouseDown;
_bottomHandle.MouseLeftButtonDown += OnMouseDown;
_bottomHandle.Stub.MouseLeftButtonDown += OnMouseDown;
}
if (adornerPanel != null)
@ -45,6 +62,160 @@ namespace ICSharpCode.WpfDesign.Designer.Extensions @@ -45,6 +62,160 @@ namespace ICSharpCode.WpfDesign.Designer.Extensions
}
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
var row = (int) this.ExtendedItem.Properties.GetAttachedProperty(Grid.RowProperty).ValueOnInstance;
var rowSpan = (int) this.ExtendedItem.Properties.GetAttachedProperty(Grid.RowSpanProperty).ValueOnInstance;
var column = (int) this.ExtendedItem.Properties.GetAttachedProperty(Grid.ColumnProperty).ValueOnInstance;
var columnSpan = (int) this.ExtendedItem.Properties.GetAttachedProperty(Grid.ColumnSpanProperty).ValueOnInstance;
var margin = (Thickness) this.ExtendedItem.Properties[FrameworkElement.MarginProperty].ValueOnInstance;
var point = this.ExtendedItem.View.TranslatePoint(new Point(), _grid);
var position = new Rect(point, this.ExtendedItem.View.RenderSize);
MarginHandle handle = null;
if (sender is MarginHandle)
handle = sender as MarginHandle;
if (sender is MarginStub)
handle = ((MarginStub) sender).Handle;
if (handle != null) {
switch (handle.Orientation) {
case HandleOrientation.Left:
if (_rightHandle.Visibility == Visibility.Visible) {
if (_leftHandle.Visibility == Visibility.Visible) {
margin.Left = 0;
this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Right);
} else {
var leftMargin = position.Left - GetColumnOffset(column);
margin.Left = leftMargin;
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Stretch);
}
} else {
if (_leftHandle.Visibility == Visibility.Visible) {
margin.Left = 0;
var rightMargin = GetColumnOffset(column + columnSpan) - position.Right;
margin.Right = rightMargin;
this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Right);
} else {
var leftMargin = position.Left - GetColumnOffset(column);
margin.Left = leftMargin;
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Left);
}
}
break;
case HandleOrientation.Top:
if (_bottomHandle.Visibility == Visibility.Visible) {
if (_topHandle.Visibility == Visibility.Visible) {
margin.Top = 0;
this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Bottom);
} else {
var topMargin = position.Top - GetRowOffset(row);
margin.Top = topMargin;
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Stretch);
}
} else {
if (_topHandle.Visibility == Visibility.Visible) {
margin.Top = 0;
var bottomMargin = GetRowOffset(row + rowSpan) - position.Bottom;
margin.Bottom = bottomMargin;
this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Bottom);
} else {
var topMargin = position.Top - GetRowOffset(row);
margin.Top = topMargin;
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Top);
}
}
break;
case HandleOrientation.Right:
if (_leftHandle.Visibility == Visibility.Visible) {
if (_rightHandle.Visibility == Visibility.Visible) {
margin.Right = 0;
this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Left);
} else {
var rightMargin = GetColumnOffset(column + columnSpan) - position.Right;
margin.Right = rightMargin;
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Stretch);
}
} else {
if (_rightHandle.Visibility == Visibility.Visible) {
margin.Right = 0;
var leftMargin = position.Left - GetColumnOffset(column);
margin.Left = leftMargin;
this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Left);
} else {
var rightMargin = GetColumnOffset(column + columnSpan) - position.Right;
margin.Right = rightMargin;
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Right);
}
}
break;
case HandleOrientation.Bottom:
if (_topHandle.Visibility == Visibility.Visible) {
if (_bottomHandle.Visibility == Visibility.Visible) {
margin.Bottom = 0;
this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Top);
} else {
var bottomMargin = GetRowOffset(row + rowSpan) - position.Bottom;
margin.Bottom = bottomMargin;
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Stretch);
}
} else {
if (_bottomHandle.Visibility == Visibility.Visible) {
margin.Bottom = 0;
var topMargin = position.Top - GetRowOffset(row);
margin.Top = topMargin;
this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Top);
} else {
var bottomMargin = GetRowOffset(row + rowSpan) - position.Bottom;
margin.Bottom = bottomMargin;
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Bottom);
}
}
break;
}
}
this.ExtendedItem.Properties[FrameworkElement.MarginProperty].SetValue(margin);
}
private double GetColumnOffset(int index)
{
if (_grid != null) {
// when the grid has no columns, we still need to return 0 for index=0 and grid.Width for index=1
if (index == 0)
return 0;
if (index < _grid.ColumnDefinitions.Count)
return _grid.ColumnDefinitions[index].Offset;
return _grid.ActualWidth;
}
return 0;
}
private double GetRowOffset(int index)
{
if (_grid != null) {
if (index == 0)
return 0;
if (index < _grid.RowDefinitions.Count)
return _grid.RowDefinitions[index].Offset;
return _grid.ActualHeight;
}
return 0;
}
public void HideHandles()
{
if (_leftHandle != null && _topHandle != null && _rightHandle != null && _bottomHandle != null) {

Loading…
Cancel
Save