/// <summary> /// Deletes the specified components from their parent containers. /// If the deleted components are currently selected, they are deselected before they are deleted. /// </summary> public static void DeleteComponents(ICollection <DesignItem> deleteItems) { if (deleteItems.Count > 0) { var changeGroup = deleteItems.First().OpenGroup("Delete Items"); try { var itemsGrpParent = deleteItems.GroupBy(x => x.Parent); foreach (var itemsList in itemsGrpParent) { var items = itemsList.ToList(); DesignItem parent = items.First().Parent; PlacementOperation operation = PlacementOperation.Start(items, PlacementType.Delete); try { ISelectionService selectionService = items.First().Services.Selection; selectionService.SetSelectedComponents(items, SelectionTypes.Remove); // if the selection is empty after deleting some components, select the parent of the deleted component if (selectionService.SelectionCount == 0 && !items.Contains(parent)) { selectionService.SetSelectedComponents(new[] { parent }); } foreach (var designItem in items) { designItem.Name = null; } var service = parent.Services.Component as XamlComponentService; foreach (var item in items) { service.RaiseComponentRemoved(item); } operation.DeleteItemsAndCommit(); } catch { operation.Abort(); throw; } } changeGroup.Commit(); } catch { changeGroup.Abort(); throw; } } }
/// <summary> /// Deletes the specified components from their parent containers. /// If the deleted components are currently selected, they are deselected before they are deleted. /// </summary> public static void DeleteComponents(ICollection <DesignItem> items) { DesignItem parent = items.First().Parent; PlacementOperation operation = PlacementOperation.Start(items, PlacementType.Delete); try { ISelectionService selectionService = items.First().Services.Selection; selectionService.SetSelectedComponents(items, SelectionTypes.Remove); // if the selection is empty after deleting some components, select the parent of the deleted component if (selectionService.SelectionCount == 0 && !items.Contains(parent)) { selectionService.SetSelectedComponents(new DesignItem[] { parent }); } operation.DeleteItemsAndCommit(); } catch { operation.Abort(); throw; } }
void DesignPanel_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down) { e.Handled = true; if (placementOp == null) { dx = 0; dy = 0; placementOp = PlacementOperation.Start(Context.Services.Selection.SelectedItems, PlacementType.Move); } dx = (e.Key == Key.Left) ? Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1 : 0; dy = (e.Key == Key.Up) ? Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1 : 0; dx = (e.Key == Key.Right) ? Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1 : (dx != 0 ? dx : 0); dy = (e.Key == Key.Down) ? Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1 : (dy != 0 ? dy : 0); double left, top; foreach (PlacementInformation info in placementOp.PlacedItems) { //Let canvas position preceed bounds definition since there can be a discrepancy between them. left = IsPropertySet(info.Item.View, Canvas.LeftProperty)?(double)info.Item.Properties.GetAttachedProperty(Canvas.LeftProperty).ValueOnInstance: info.OriginalBounds.Left; top = IsPropertySet(info.Item.View, Canvas.TopProperty) ? (double)info.Item.Properties.GetAttachedProperty(Canvas.TopProperty).ValueOnInstance : info.OriginalBounds.Top; if (!Keyboard.IsKeyDown(Key.LeftCtrl)) { info.Bounds = new Rect(left + dx, top + dy, info.OriginalBounds.Width, info.OriginalBounds.Height); } else { info.Bounds = new Rect(left, top, info.OriginalBounds.Width + dx, info.OriginalBounds.Height + dy); } placementOp.CurrentContainerBehavior.SetPosition(info); } } }
public bool CanInsert(IEnumerable <IOutlineNode> nodes, IOutlineNode after, bool copy) { var placementBehavior = DesignItem.GetBehavior <IPlacementBehavior>(); if (placementBehavior == null) { return(false); } var operation = PlacementOperation.Start(nodes.Select(node => node.DesignItem).ToArray(), DummyPlacementType); if (operation != null) { bool canEnter = placementBehavior.CanEnterContainer(operation, true); operation.Abort(); return(canEnter); } return(false); }
// TODO : Remove all hide/show extensions from here. protected virtual void drag_Started(DragListener drag) { Line al = ExtendedItem.View as Line; CurrentX2 = al.X2; CurrentY2 = al.Y2; CurrentLeft = al.Margin.Left; CurrentTop = al.Margin.Top; if (ExtendedItem.Parent.View is Canvas) { var lp = (double)al.GetValue(Canvas.LeftProperty); if (!double.IsNaN(lp)) { CurrentLeft += lp; } var tp = (double)al.GetValue(Canvas.TopProperty); if (!double.IsNaN(tp)) { CurrentTop += tp; } } var designPanel = ExtendedItem.Services.DesignPanel as DesignPanel; zoom = designPanel.TryFindParent <ZoomControl>(); if (resizeBehavior != null) { operation = PlacementOperation.Start(extendedItemArray, PlacementType.Resize); } else { changeGroup = this.ExtendedItem.Context.OpenGroup("Resize", extendedItemArray); } _isResizing = true; (drag.Target as DesignerThumb).IsPrimarySelection = false; }
// TODO : Remove all hide/show extensions from here. void drag_Started(DragListener drag) { /* Abort editing Text if it was editing, because it interferes with the undo stack. */ //foreach(var extension in this.ExtendedItem.Extensions){ // if(extension is InPlaceEditorExtension){ // ((InPlaceEditorExtension)extension).AbortEdit(); // } //} drag.Transform = this.ExtendedItem.GetCompleteAppliedTransformationToView(); oldSize = new Size(ModelTools.GetWidth(ExtendedItem.View), ModelTools.GetHeight(ExtendedItem.View)); if (resizeBehavior != null) { operation = PlacementOperation.Start(extendedItemArray, PlacementType.Resize); } else { changeGroup = this.ExtendedItem.Context.OpenGroup("Resize", extendedItemArray); } _isResizing = true; ShowSizeAndHideHandles(); }
void DesignPanel_KeyDown(object sender, KeyEventArgs e) { //pass the key event down to the underlying objects if they have implemented IKeyUp interface //OBS!!!! this call needs to be here, before the PlacementOperation.Start. //In case the underlying object has a operation of its own this operation needs to be set first foreach (DesignItem di in Context.Services.Selection.SelectedItems) { foreach (Extension ext in di.Extensions) { var keyDown = ext as IKeyDown; if (keyDown != null) { keyDown.KeyDownAction(sender, e); } } } if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down) { e.Handled = true; PlacementType placementType = Keyboard.IsKeyDown(Key.LeftCtrl) ? PlacementType.Resize : PlacementType.MoveAndIgnoreOtherContainers; if (placementOp != null && placementOp.Type != placementType) { placementOp.Commit(); placementOp = null; } if (placementOp == null) { //check if any objects don't want the default action to be invoked List <DesignItem> placedItems = Context.Services.Selection.SelectedItems.Where(x => x.Extensions.All(InvokeDefaultKeyDownAction)).ToList(); //if no remaining objects, break if (placedItems.Count < 1) { return; } dx = 0; dy = 0; placementOp = PlacementOperation.Start(placedItems, placementType); } switch (e.Key) { case Key.Left: dx += Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1; break; case Key.Up: dy += Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1; break; case Key.Right: dx += Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1; break; case Key.Down: dy += Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1; break; } foreach (PlacementInformation info in placementOp.PlacedItems) { var bounds = info.OriginalBounds; if (placementType == PlacementType.Move || info.Operation.Type == PlacementType.MoveAndIgnoreOtherContainers) { info.Bounds = new Rect(bounds.Left + dx, bounds.Top + dy, bounds.Width, bounds.Height); } else if (placementType == PlacementType.Resize) { if (bounds.Width + dx >= 0 && bounds.Height + dy >= 0) { info.Bounds = new Rect(bounds.Left, bounds.Top, bounds.Width + dx, bounds.Height + dy); } } placementOp.CurrentContainerBehavior.SetPosition(info); } } }
private void ConvertPart(PathThumb senderThumb, PathPartConvertType convertType) { if (senderThumb.PathPoint.ParentObject is PathFigure) { var pathFigure = senderThumb.PathPoint.ParentObject as PathFigure; var pathSegment = senderThumb.PathPoint.Object as PathSegment; var idx = pathFigure.Segments.IndexOf(pathSegment); var point = senderThumb.PathPoint.Point; if (pathSegment is PolyLineSegment) { var poly = pathSegment as PolyLineSegment; var lst = poly.Points.Take(senderThumb.PathPoint.PolyLineIndex); var lst2 = poly.Points.Skip(senderThumb.PathPoint.PolyLineIndex + 1); var p = poly.Points[senderThumb.PathPoint.PolyLineIndex]; pathFigure.Segments.RemoveAt(idx); var p1 = new PolyLineSegment(); p1.Points.AddRange(lst); pathFigure.Segments.Insert(idx, p1); pathSegment = new LineSegment() { Point = p }; pathFigure.Segments.Insert(idx + 1, pathSegment); var p2 = new PolyLineSegment(); p2.Points.AddRange(lst2); pathFigure.Segments.Insert(idx + 2, p2); idx++; } else if (pathSegment is PolyBezierSegment) { //TODO } else if (pathSegment is PolyQuadraticBezierSegment) { //TODO } pathFigure.Segments.RemoveAt(idx); var midp = senderThumb.PathPoint.ParentPathPoint.Point - ((senderThumb.PathPoint.ParentPathPoint.Point - point) / 2); PathSegment newSegment = null; switch (convertType) { case PathPartConvertType.ToBezierSegment: newSegment = new BezierSegment() { Point1 = midp - new Vector(40, 40), Point2 = midp + new Vector(-40, 40), Point3 = point }; break; case PathPartConvertType.ToQuadricBezierSegment: newSegment = new QuadraticBezierSegment() { Point1 = point - new Vector(40, 40), Point2 = point }; break; case PathPartConvertType.ToArcSegment: newSegment = new ArcSegment() { Point = point, Size = new Size(20, 20) }; break; case PathPartConvertType.insertPoint: pathFigure.Segments.Insert(idx, pathSegment); newSegment = new LineSegment() { Point = midp, }; break; default: newSegment = new LineSegment() { Point = point }; break; } pathFigure.Segments.Insert(idx, newSegment); operation = PlacementOperation.Start(extendedItemArray, PlacementType.MovePoint); CommitOperation(); } this.ExtendedItem.ReapplyAllExtensions(); }
public static void ArrangeItems(IEnumerable <DesignItem> items, ArrangeDirection arrangeDirection) { var collection = items; var _context = collection.First().Context as XamlDesignContext; var container = collection.First().Parent; if (collection.Any(x => x.Parent != container)) { return; } var placement = container.Extensions.OfType <IPlacementBehavior>().FirstOrDefault(); if (placement == null) { return; } var operation = PlacementOperation.Start(items.ToList(), PlacementType.Move); //var changeGroup = container.OpenGroup("Arrange Elements"); List <ItemPos> itemList = new List <ItemPos>(); foreach (var item in collection) { itemList.Add(GetItemPos(operation, item)); } var xmin = itemList.Min(x => x.Xmin); var xmax = itemList.Max(x => x.Xmax); var mpos = (xmax - xmin) / 2 + xmin; var ymin = itemList.Min(x => x.Ymin); var ymax = itemList.Max(x => x.Ymax); var ympos = (ymax - ymin) / 2 + ymin; foreach (var item in collection) { switch (arrangeDirection) { case ArrangeDirection.Left: { if (container.Component is Canvas) { if (!item.Properties.GetAttachedProperty(Canvas.RightProperty).IsSet) { item.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(xmin); } else { var pos = (double)((Panel)item.Parent.Component).ActualWidth - (xmin + (double)((FrameworkElement)item.Component).ActualWidth); item.Properties.GetAttachedProperty(Canvas.RightProperty).SetValue(pos); } } else if (container.Component is Grid) { if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance != HorizontalAlignment.Right) { var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Left = xmin; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } else { var pos = (double)((Panel)item.Parent.Component).ActualWidth - (xmin + (double)((FrameworkElement)item.Component).ActualWidth); var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Right = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } } } break; case ArrangeDirection.HorizontalMiddle: { if (container.Component is Canvas) { if (!item.Properties.GetAttachedProperty(Canvas.RightProperty).IsSet) { if (!item.Properties.GetAttachedProperty(Canvas.RightProperty).IsSet) { item.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(mpos - (((FrameworkElement)item.Component).ActualWidth) / 2); } else { var pp = mpos - (((FrameworkElement)item.Component).ActualWidth) / 2; var pos = (double)((Panel)item.Parent.Component).ActualWidth - pp - (((FrameworkElement)item.Component).ActualWidth); item.Properties.GetAttachedProperty(Canvas.RightProperty).SetValue(pos); } } } else if (container.Component is Grid) { if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance != HorizontalAlignment.Right) { var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Left = mpos - (((FrameworkElement)item.Component).ActualWidth) / 2; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } else { var pp = mpos - (((FrameworkElement)item.Component).ActualWidth) / 2; var pos = (double)((Panel)item.Parent.Component).ActualWidth - pp - (((FrameworkElement)item.Component).ActualWidth); var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Right = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } } } break; case ArrangeDirection.Right: { if (container.Component is Canvas) { if (!item.Properties.GetAttachedProperty(Canvas.RightProperty).IsSet) { var pos = xmax - (double)((FrameworkElement)item.Component).ActualWidth; item.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(pos); } else { var pos = (double)((Panel)item.Parent.Component).ActualWidth - xmax; item.Properties.GetAttachedProperty(Canvas.RightProperty).SetValue(pos); } } else if (container.Component is Grid) { if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance != HorizontalAlignment.Right) { var pos = xmax - (double)((FrameworkElement)item.Component).ActualWidth; var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Left = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } else { var pos = (double)((Panel)item.Parent.Component).ActualWidth - xmax; var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Right = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } } } break; case ArrangeDirection.Top: { if (container.Component is Canvas) { if (!item.Properties.GetAttachedProperty(Canvas.BottomProperty).IsSet) { item.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(ymin); } else { var pos = (double)((Panel)item.Parent.Component).ActualHeight - (ymin + (double)((FrameworkElement)item.Component).ActualHeight); item.Properties.GetAttachedProperty(Canvas.BottomProperty).SetValue(pos); } } else if (container.Component is Grid) { if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance != VerticalAlignment.Bottom) { item.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(ymin); var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Top = ymin; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } else { var pos = (double)((Panel)item.Parent.Component).ActualHeight - (ymin + (double)((FrameworkElement)item.Component).ActualHeight); var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Bottom = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } } } break; case ArrangeDirection.VerticalMiddle: { if (container.Component is Canvas) { if (!item.Properties.GetAttachedProperty(Canvas.BottomProperty).IsSet) { item.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(ympos - (((FrameworkElement)item.Component).ActualHeight) / 2); } else { var pp = mpos - (((FrameworkElement)item.Component).ActualHeight) / 2; var pos = (double)((Panel)item.Parent.Component).ActualHeight - pp - (((FrameworkElement)item.Component).ActualHeight); item.Properties.GetAttachedProperty(Canvas.BottomProperty).SetValue(pos); } } else if (container.Component is Grid) { if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance != VerticalAlignment.Bottom) { var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Top = ympos - (((FrameworkElement)item.Component).ActualHeight) / 2; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } else { var pp = mpos - (((FrameworkElement)item.Component).ActualHeight) / 2; var pos = (double)((Panel)item.Parent.Component).ActualHeight - pp - (((FrameworkElement)item.Component).ActualHeight); var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Bottom = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } } } break; case ArrangeDirection.Bottom: { if (container.Component is Canvas) { if (!item.Properties.GetAttachedProperty(Canvas.BottomProperty).IsSet) { var pos = ymax - (double)((FrameworkElement)item.Component).ActualHeight; item.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(pos); } else { var pos = (double)((Panel)item.Parent.Component).ActualHeight - ymax; item.Properties.GetAttachedProperty(Canvas.BottomProperty).SetValue(pos); } } else if (container.Component is Grid) { if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance != VerticalAlignment.Bottom) { var pos = ymax - (double)((FrameworkElement)item.Component).ActualHeight; var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Top = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } else { var pos = (double)((Panel)item.Parent.Component).ActualHeight - ymax; var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance; margin.Bottom = pos; item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin); } } } break; } } operation.Commit(); }
public static Tuple <DesignItem, Rect> WrapItemsNewContainer(IEnumerable <DesignItem> items, Type containerType, bool doInsert = true) { var collection = items; var _context = collection.First().Context as XamlDesignContext; var container = collection.First().Parent; if (collection.Any(x => x.Parent != container)) { return(null); } //Change Code to use the Placment Operation! var placement = container.Extensions.OfType <IPlacementBehavior>().FirstOrDefault(); if (placement == null) { return(null); } var operation = PlacementOperation.Start(items.ToList(), PlacementType.Move); var newInstance = _context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(containerType, null); DesignItem newPanel = _context.Services.Component.RegisterComponentForDesigner(newInstance); List <ItemPos> itemList = new List <ItemPos>(); foreach (var item in collection) { itemList.Add(GetItemPos(operation, item)); //var pos = placement.GetPosition(null, item); if (container.Component is Canvas) { 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 (container.Component is Grid) { 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); foreach (var item in itemList) { 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); } newPanel.ContentProperty.CollectionElements.Add(item.DesignItem); } 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); newPanel.ContentProperty.CollectionElements.Add(item.DesignItem); } else if (newPanel.Component is Viewbox) { newPanel.ContentProperty.SetValue(item.DesignItem); } } if (doInsert) { PlacementOperation operation2 = PlacementOperation.TryStartInsertNewComponents( container, new[] { newPanel }, new[] { new Rect(xmin, ymin, xmax - xmin, ymax - ymin).Round() }, PlacementType.AddItem ); operation2.Commit(); _context.Services.Selection.SetSelectedComponents(new[] { newPanel }); } operation.Commit(); return(new Tuple <DesignItem, Rect>(newPanel, new Rect(xmin, ymin, xmax - xmin, ymax - ymin).Round())); }
public static void UnwrapItemsFromContainer(DesignItem container) { var collection = container.ContentProperty.CollectionElements.ToList(); var newPanel = container.Parent; if (collection.Any(x => x.Parent != container)) { return; } //Change Code to use the Placment Operation! var placement = container.Extensions.OfType <IPlacementBehavior>().FirstOrDefault(); if (placement == null) { return; } var operation = PlacementOperation.Start(collection.ToList(), PlacementType.Move); List <ItemPos> itemList = new List <ItemPos>(); int?firstIndex = null; var containerPos = GetItemPos(operation, container); foreach (var item in collection) { itemList.Add(GetItemPos(operation, item)); if (container.Component is Canvas) { 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 (container.Component is Grid) { item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).Reset(); item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).Reset(); item.Properties.GetProperty(FrameworkElement.MarginProperty).Reset(); } if (item.ParentProperty.IsCollection) { var parCol = item.ParentProperty.CollectionElements; if (!firstIndex.HasValue) { firstIndex = parCol.IndexOf(item); } parCol.Remove(item); } else { item.ParentProperty.Reset(); } } newPanel.ContentProperty.CollectionElements.Remove(container); foreach (var item in itemList) { if (newPanel.Component is Canvas) { if (item.HorizontalAlignment == HorizontalAlignment.Right) { item.DesignItem.Properties.GetAttachedProperty(Canvas.RightProperty).SetValue(containerPos.Xmax - item.Xmax); } else { item.DesignItem.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(item.Xmin + containerPos.Xmin); } if (item.VerticalAlignment == VerticalAlignment.Bottom) { item.DesignItem.Properties.GetAttachedProperty(Canvas.BottomProperty).SetValue(containerPos.Ymax - item.Ymax); } else { item.DesignItem.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(item.Ymin + containerPos.Ymin); } newPanel.ContentProperty.CollectionElements.Add(item.DesignItem); } 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 = containerPos.Xmax - item.Xmax; } else { item.DesignItem.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Left); thickness.Left = item.Xmin; } if (item.VerticalAlignment == VerticalAlignment.Bottom) { item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Bottom); thickness.Bottom = containerPos.Ymax - item.Ymax; } else { item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Top); thickness.Top = item.Ymin; } item.DesignItem.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(thickness); newPanel.ContentProperty.CollectionElements.Add(item.DesignItem); } else if (newPanel.Component is Viewbox) { newPanel.ContentProperty.SetValue(item.DesignItem); } else if (newPanel.Component is ContentControl) { newPanel.ContentProperty.SetValue(item.DesignItem); } } operation.Commit(); }
void DesignPanel_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down) { e.Handled = true; PlacementType placementType = Keyboard.IsKeyDown(Key.LeftCtrl) ? PlacementType.Resize : PlacementType.Move; if (placementOp != null && placementOp.Type != placementType) { placementOp.Commit(); placementOp = null; } if (placementOp == null) { dx = 0; dy = 0; placementOp = PlacementOperation.Start(Context.Services.Selection.SelectedItems, placementType); } switch (e.Key) { case Key.Left: dx += Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1; break; case Key.Up: dy += Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1; break; case Key.Right: dx += Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1; break; case Key.Down: dy += Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1; break; } foreach (PlacementInformation info in placementOp.PlacedItems) { var bounds = info.OriginalBounds; if (placementType == PlacementType.Move) { info.Bounds = new Rect(bounds.Left + dx, bounds.Top + dy, bounds.Width, bounds.Height); } else if (placementType == PlacementType.Resize) { if (bounds.Width + dx >= 0 && bounds.Height + dy >= 0) { info.Bounds = new Rect(bounds.Left, bounds.Top, bounds.Width + dx, bounds.Height + dy); } } placementOp.CurrentContainerBehavior.SetPosition(info); } } }
void DesignPanel_KeyDown(object sender, KeyEventArgs e) { //pass the key event down to the underlying objects if they have implemented IKeyUp interface //OBS!!!! this call needs to be here, before the PlacementOperation.Start. //In case the underlying object has a operation of its own this operation needs to be set first foreach (DesignItem di in Context.Services.Selection.SelectedItems) { foreach (Extension ext in di.Extensions) { var keyDown = ext as IKeyDown; if (keyDown != null) { keyDown.KeyDownAction(sender, e); } } } if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down) { bool initialEvent = false; e.Handled = true; PlacementType placementType = Keyboard.IsKeyDown(Key.LeftCtrl) ? PlacementType.Resize : PlacementType.MoveAndIgnoreOtherContainers; if (placementOp != null && placementOp.Type != placementType) { placementOp.Commit(); placementOp = null; dx.Clear(); dy.Clear(); } if (placementOp == null) { //check if any objects don't want the default action to be invoked List <DesignItem> placedItems = Context.Services.Selection.SelectedItems.Where(x => x.Extensions.All(InvokeDefaultKeyDownAction)).ToList(); //if no remaining objects, break if (placedItems.Count < 1) { return; } placementOp = PlacementOperation.Start(placedItems, placementType); dx.Clear(); dy.Clear(); } int odx = 0, ody = 0; switch (e.Key) { case Key.Left: odx = Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1; break; case Key.Up: ody = Keyboard.IsKeyDown(Key.LeftShift) ? -10 : -1; break; case Key.Right: odx = Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1; break; case Key.Down: ody = Keyboard.IsKeyDown(Key.LeftShift) ? 10 : 1; break; } foreach (PlacementInformation info in placementOp.PlacedItems) { if (!dx.ContainsKey(info)) { dx[info] = 0; dy[info] = 0; } var transform = info.Item.Parent.View.TransformToVisual(this); var mt = transform as MatrixTransform; if (mt != null) { var angle = Math.Atan2(mt.Matrix.M21, mt.Matrix.M11) * 180 / Math.PI; if (angle > 45.0 && angle < 135.0) { dx[info] += ody * -1; dy[info] += odx; } else if (angle < -45.0 && angle > -135.0) { dx[info] += ody; dy[info] += odx * -1; } else if (angle > 135.0 || angle < -135.0) { dx[info] += odx * -1; dy[info] += ody * -1; } else { dx[info] += odx; dy[info] += ody; } } var bounds = info.OriginalBounds; if (placementType == PlacementType.Move || info.Operation.Type == PlacementType.MoveAndIgnoreOtherContainers) { info.Bounds = new Rect(bounds.Left + dx[info], bounds.Top + dy[info], bounds.Width, bounds.Height); } else if (placementType == PlacementType.Resize) { if (bounds.Width + dx[info] >= 0 && bounds.Height + dy[info] >= 0) { info.Bounds = new Rect(bounds.Left, bounds.Top, bounds.Width + dx[info], bounds.Height + dy[info]); } } placementOp.CurrentContainerBehavior.SetPosition(info); } } }