Browse Source

Merge pull request #463 from jogibear9988/master

Right Click Context Menu for multiple Selected Items to Wrap them in a Container
pull/474/head 5.0-Beta4
Daniel Grunwald 11 years ago
parent
commit
6585614ee8
  1. 8
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/RotateThumb.cs
  2. 8
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/RightClickMultipleItemsContextMenu.xaml
  3. 58
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/RightClickMultipleItemsContextMenu.xaml.cs
  4. 54
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/RightClickMultipleItemsContextMenuExtension.cs
  5. 170
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/ModelTools.cs
  6. 18
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Translations.cs
  7. 5
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/WpfDesign.Designer.csproj
  8. 28
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/Extensions/SelectionExtensionServer.cs

8
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/RotateThumb.cs

@ -32,14 +32,6 @@ namespace ICSharpCode.WpfDesign.Designer.Controls
{ {
public class RotateThumb : ResizeThumb public class RotateThumb : ResizeThumb
{ {
// private double initialAngle;
// private RotateTransform rotateTransform;
// private Vector startVector;
// private Point centerPoint;
// private Control designerItem;
// private Panel canvas;
// private AdornerPanel parent;
static RotateThumb() static RotateThumb()
{ {
DefaultStyleKeyProperty.OverrideMetadata(typeof(RotateThumb), new FrameworkPropertyMetadata(typeof(RotateThumb))); DefaultStyleKeyProperty.OverrideMetadata(typeof(RotateThumb), new FrameworkPropertyMetadata(typeof(RotateThumb)));

8
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/RightClickMultipleItemsContextMenu.xaml

@ -0,0 +1,8 @@
<ContextMenu x:Class="ICSharpCode.WpfDesign.Designer.Extensions.RightClickMultipleItemsContextMenu"
xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Translation="clr-namespace:ICSharpCode.WpfDesign.Designer"
>
<MenuItem Header="{Binding WrapInCanvas, Source={x:Static Translation:Translations.Instance}}" Click="Click_WrapInCanvas" />
<MenuItem Header="{Binding WrapInGrid, Source={x:Static Translation:Translations.Instance}}" Click="Click_WrapInGrid" />
</ContextMenu>

58
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/RightClickMultipleItemsContextMenu.xaml.cs

@ -0,0 +1,58 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
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.PropertyGrid;
using ICSharpCode.WpfDesign.Designer.Xaml;
namespace ICSharpCode.WpfDesign.Designer.Extensions
{
public partial class RightClickMultipleItemsContextMenu
{
private DesignItem designItem;
public RightClickMultipleItemsContextMenu(DesignItem designItem)
{
this.designItem = designItem;
InitializeComponent();
}
void Click_WrapInCanvas(object sender, System.Windows.RoutedEventArgs e)
{
ModelTools.WrapItemsNewContainer(this.designItem.Services.Selection.SelectedItems, typeof(Canvas));
}
void Click_WrapInGrid(object sender, System.Windows.RoutedEventArgs e)
{
ModelTools.WrapItemsNewContainer(this.designItem.Services.Selection.SelectedItems, typeof(Grid));
}
}
}

54
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/RightClickMultipleItemsContextMenuExtension.cs

@ -0,0 +1,54 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using ICSharpCode.WpfDesign.Adorners;
using ICSharpCode.WpfDesign.Extensions;
using ICSharpCode.WpfDesign.Designer;
namespace ICSharpCode.WpfDesign.Designer.Extensions
{
/// <summary>
///
/// </summary>
[ExtensionServer(typeof(MultipleSelectedExtensionServer))]
[ExtensionFor(typeof(UIElement))]
public class RightClickMultipleItemsContextMenuExtension : SelectionAdornerProvider
{
DesignPanel panel;
protected override void OnInitialized()
{
base.OnInitialized();
panel = ExtendedItem.Context.Services.DesignPanel as DesignPanel;
panel.ContextMenu = new RightClickMultipleItemsContextMenu(ExtendedItem);
}
protected override void OnRemove()
{
panel.ContextMenu = null;
base.OnRemove();
}
}
}

170
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/ModelTools.cs

@ -27,6 +27,7 @@ using System.Windows.Markup;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Xps.Serialization; using System.Windows.Xps.Serialization;
using ICSharpCode.WpfDesign.Designer.Xaml;
namespace ICSharpCode.WpfDesign.Designer namespace ICSharpCode.WpfDesign.Designer
{ {
@ -198,5 +199,174 @@ namespace ICSharpCode.WpfDesign.Designer
item.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(newHeight); item.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(newHeight);
} }
} }
private class ItemPos
{
public HorizontalAlignment HorizontalAlignment{ get; set; }
public VerticalAlignment VerticalAlignment{ get; set; }
public double Xmin { get; set; }
public double Xmax { get; set; }
public double Ymin { get; set; }
public double Ymax { get; set; }
public DesignItem DesignItem { get; set; }
}
public static void WrapItemsNewContainer(IEnumerable<DesignItem> items, Type containerType)
{
var collection = items;
var _context = collection.First().Context as XamlDesignContext;
var oldContainer = collection.First().Parent;
if (collection.Any(x => x.Parent != oldContainer))
return;
var newInstance = Activator.CreateInstance(containerType);
DesignItem newPanel = _context.Services.Component.RegisterComponentForDesigner(newInstance);
var changeGroup = newPanel.OpenGroup("Wrap in Container");
List<ItemPos> itemList = new List<ItemPos>();
foreach (var item in collection) {
var itemPos = new ItemPos(){ DesignItem = item };
itemList.Add(itemPos);
if (oldContainer.Component is Canvas) {
var canvas = oldContainer.View as Canvas;
if (item.Properties.GetAttachedProperty(Canvas.RightProperty) != null && item.Properties.GetAttachedProperty(Canvas.RightProperty).IsSet) {
itemPos.HorizontalAlignment = HorizontalAlignment.Right;
itemPos.Xmax = canvas.ActualWidth - (double)item.Properties.GetAttachedProperty(Canvas.RightProperty).ValueOnInstance;
itemPos.Xmin = itemPos.Xmax - ((FrameworkElement)item.View).ActualWidth;
}
else if (item.Properties.GetAttachedProperty(Canvas.LeftProperty) != null && item.Properties.GetAttachedProperty(Canvas.LeftProperty).IsSet) {
itemPos.HorizontalAlignment = HorizontalAlignment.Left;
itemPos.Xmin = (double)item.Properties.GetAttachedProperty(Canvas.LeftProperty).ValueOnInstance;
itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth;
} else {
itemPos.HorizontalAlignment = HorizontalAlignment.Left;
itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth;
}
if (item.Properties.GetAttachedProperty(Canvas.BottomProperty) != null && item.Properties.GetAttachedProperty(Canvas.BottomProperty).IsSet) {
itemPos.VerticalAlignment = VerticalAlignment.Bottom;
itemPos.Ymax = canvas.ActualHeight - (double)item.Properties.GetAttachedProperty(Canvas.BottomProperty).ValueOnInstance;
itemPos.Ymin = itemPos.Ymax - ((FrameworkElement)item.View).ActualHeight;
}
else if (item.Properties.GetAttachedProperty(Canvas.TopProperty) != null && item.Properties.GetAttachedProperty(Canvas.TopProperty).IsSet) {
itemPos.VerticalAlignment = VerticalAlignment.Top;
itemPos.Ymin = (double)item.Properties.GetAttachedProperty(Canvas.TopProperty).ValueOnInstance;
itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight;
} else {
itemPos.VerticalAlignment = VerticalAlignment.Top;
itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight;
}
item.Properties.GetAttachedProperty(Canvas.RightProperty).Reset();
item.Properties.GetAttachedProperty(Canvas.LeftProperty).Reset();
item.Properties.GetAttachedProperty(Canvas.TopProperty).Reset();
item.Properties.GetAttachedProperty(Canvas.BottomProperty).Reset();
} else if (oldContainer.Component is Grid) {
var grid = oldContainer.View as Grid;
if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance == HorizontalAlignment.Right) {
itemPos.HorizontalAlignment = HorizontalAlignment.Right;
itemPos.Xmax = grid.ActualWidth - ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Right;
itemPos.Xmin = itemPos.Xmax - ((FrameworkElement)item.View).ActualWidth;
} else {
itemPos.HorizontalAlignment = HorizontalAlignment.Left;
itemPos.Xmin = ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Left;
itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth;
}
if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance == VerticalAlignment.Bottom) {
itemPos.VerticalAlignment = VerticalAlignment.Bottom;
itemPos.Ymax = grid.ActualHeight - ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Bottom;
itemPos.Ymin = itemPos.Ymax - ((FrameworkElement)item.View).ActualHeight;
} else {
itemPos.VerticalAlignment = VerticalAlignment.Top;
itemPos.Ymin = ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Top;
itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight;
}
item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).Reset();
item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).Reset();
item.Properties.GetProperty(FrameworkElement.MarginProperty).Reset();
}
var parCol = item.ParentProperty.CollectionElements;
parCol.Remove(item);
}
var xmin = itemList.Min(x => x.Xmin);
var xmax = itemList.Max(x => x.Xmax);
var ymin = itemList.Min(x => x.Ymin);
var ymax = itemList.Max(x => x.Ymax);
if (oldContainer.Component is Canvas) {
newPanel.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(xmax - xmin);
newPanel.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(ymax - ymin);
newPanel.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(xmin);
newPanel.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(ymin);
} else if (oldContainer.Component is Grid) {
newPanel.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Left);
newPanel.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Top);
newPanel.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(new Thickness(xmin, ymin, 0, 0));
newPanel.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(xmax - xmin);
newPanel.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(ymax - ymin);
}
foreach (var item in itemList) {
newPanel.ContentProperty.CollectionElements.Add(item.DesignItem);
if (newPanel.Component is Canvas) {
if (item.HorizontalAlignment == HorizontalAlignment.Right) {
item.DesignItem.Properties.GetAttachedProperty(Canvas.RightProperty).SetValue(xmax - item.Xmax);
} else {
item.DesignItem.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(item.Xmin - xmin);
}
if (item.VerticalAlignment == VerticalAlignment.Bottom) {
item.DesignItem.Properties.GetAttachedProperty(Canvas.BottomProperty).SetValue(ymax - item.Ymax);
} else {
item.DesignItem.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(item.Ymin - ymin);
}
} else if (newPanel.Component is Grid) {
Thickness thickness = new Thickness(0);
if (item.HorizontalAlignment == HorizontalAlignment.Right) {
item.DesignItem.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Right);
thickness.Right = xmax - item.Xmax;
} else {
item.DesignItem.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Left);
thickness.Left = item.Xmin - xmin;
}
if (item.VerticalAlignment == VerticalAlignment.Bottom) {
item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Bottom);
thickness.Bottom = ymax - item.Ymax;
} else {
item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Top);
thickness.Top = item.Ymin - ymin;
}
item.DesignItem.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(thickness);
}
}
oldContainer.ContentProperty.CollectionElements.Add(newPanel);
changeGroup.Commit();
_context.Services.Selection.SetSelectedComponents(new []{ newPanel });
}
} }
} }

18
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Translations.cs

@ -65,5 +65,23 @@ namespace ICSharpCode.WpfDesign.Designer
return "Press \"Alt\" to Enter Container"; return "Press \"Alt\" to Enter Container";
} }
} }
public virtual string WrapInCanvas {
get {
return "Wrap in Canvas";
}
}
public virtual string WrapInGrid {
get {
return "Wrap in Grid";
}
}
public virtual string WrapInBorder {
get {
return "Wrap in Border";
}
}
} }
} }

5
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/WpfDesign.Designer.csproj

@ -90,6 +90,10 @@
</Compile> </Compile>
<Compile Include="Extensions\RenderTransformOriginExtension.cs" /> <Compile Include="Extensions\RenderTransformOriginExtension.cs" />
<Compile Include="Extensions\RightClickContextMenuExtension.cs" /> <Compile Include="Extensions\RightClickContextMenuExtension.cs" />
<Compile Include="Extensions\RightClickMultipleItemsContextMenuExtension.cs" />
<Compile Include="Extensions\RightClickMultipleItemsContextMenu.xaml.cs">
<DependentUpon>RightClickMultipleItemsContextMenu.xaml</DependentUpon>
</Compile>
<Compile Include="Extensions\SkewThumbExtension.cs" /> <Compile Include="Extensions\SkewThumbExtension.cs" />
<Compile Include="Extensions\TopLeftContainerDragHandleMultipleItems.cs" /> <Compile Include="Extensions\TopLeftContainerDragHandleMultipleItems.cs" />
<Compile Include="PropertyGrid\Editors\ColorEditor.xaml.cs"> <Compile Include="PropertyGrid\Editors\ColorEditor.xaml.cs">
@ -263,6 +267,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Page Include="Extensions\RightClickContextMenu.xaml" /> <Page Include="Extensions\RightClickContextMenu.xaml" />
<Page Include="Extensions\RightClickMultipleItemsContextMenu.xaml" />
<Page Include="PropertyGrid\Editors\ColorEditor.xaml" /> <Page Include="PropertyGrid\Editors\ColorEditor.xaml" />
<Page Include="PropertyGrid\Editors\FlatCollectionEditor.xaml" /> <Page Include="PropertyGrid\Editors\FlatCollectionEditor.xaml" />
<Page Include="PropertyGrid\Editors\OpenCollectionEditor.xaml" /> <Page Include="PropertyGrid\Editors\OpenCollectionEditor.xaml" />

28
src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/Extensions/SelectionExtensionServer.cs

@ -130,6 +130,34 @@ namespace ICSharpCode.WpfDesign.Extensions
} }
} }
/// <summary>
/// Applies an extension only when multiple Items are selected!
/// </summary>
public class MultipleSelectedExtensionServer : DefaultExtensionServer
{
/// <summary>
/// Is called after the extension server is initialized and the Context property has been set.
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
this.Services.Selection.SelectionChanged += OnSelectionChanged;
}
void OnSelectionChanged(object sender, EventArgs e)
{
ReapplyExtensions(this.Services.Selection.SelectedItems);
}
/// <summary>
/// Gets if the item is in the secondary selection.
/// </summary>
public override bool ShouldApplyExtensions(DesignItem extendedItem)
{
return Services.Selection.SelectionCount > 1;
}
}
/// <summary> /// <summary>
/// Applies an extension to the primary selection if Only One Item is Selected. /// Applies an extension to the primary selection if Only One Item is Selected.
/// </summary> /// </summary>

Loading…
Cancel
Save