internal void SetWindowPosition(ExtensionWindow window, Point position) { Func <double, double, double, double, double> CalculateInBoundsValue = (pos, size, limit, modifier) => { if (this.AutoExpandCanvas) { return(pos - modifier); } else { pos = Math.Max(0.0, pos); return(pos + size > limit ? limit - size : pos); } }; //in case of AutoExpandCanvas == false: // - do not allow placing window outside surface bounds //in case of AutoExpandCanvas == true: // - include possible negative canvas offset position.X = CalculateInBoundsValue(position.X, window.DesiredSize.Width, this.ActualWidth, this.selectedChild.Value.X); position.Y = CalculateInBoundsValue(position.Y, window.DesiredSize.Height, this.ActualHeight, this.selectedChild.Value.Y); //update its position on canvas ExtensionSurface.SetPosition(window, position); bool requiresMeasure = false; if (this.AutoExpandCanvas) { requiresMeasure = true; this.canvasOffset.X = 0; this.canvasOffset.Y = 0; foreach (UIElement item in this.Children) { FrameworkElement child = item as FrameworkElement; if (null != child) { Point p = ExtensionSurface.GetPosition(child); this.canvasOffset.X = Math.Min(this.canvasOffset.X, p.X); this.canvasOffset.Y = Math.Min(this.canvasOffset.Y, p.Y); } } this.canvasOffset.X = Math.Abs(this.canvasOffset.X); this.canvasOffset.Y = Math.Abs(this.canvasOffset.Y); } if (requiresMeasure) { this.InvalidateMeasure(); } else { this.InvalidateArrange(); } }
void CalculateSize(bool changeX, bool changeY, bool changeWidth, bool changeHeight) { Point current = Mouse.GetPosition(this); Point absolutePosition = Mouse.GetPosition(this.surface); Point topLeft = ExtensionSurface.GetPosition(this); double initialHeight = this.bottomRight.Y - topLeft.Y; double initialWidth = this.bottomRight.X - topLeft.X; Size size = new Size(initialWidth, initialHeight); if (changeX) { if (bottomRight.X > absolutePosition.X) { if ((double.IsNaN(MinWidth) || double.IsInfinity(MinWidth) || bottomRight.X - absolutePosition.X >= MinWidth) && (double.IsNaN(MaxWidth) || double.IsInfinity(MaxWidth) || bottomRight.X - absolutePosition.X <= MaxWidth)) { size.Width = this.bottomRight.X - absolutePosition.X; topLeft.X = absolutePosition.X; } } } else { if (changeWidth) { size.Width = Math.Min(Math.Max(MinWidth, current.X), MaxWidth); } } if (changeY) { if (bottomRight.Y > absolutePosition.Y) { if ((double.IsNaN(MinHeight) || double.IsInfinity(MinHeight) || bottomRight.Y - absolutePosition.Y >= MinHeight) && (double.IsNaN(MaxHeight) || double.IsInfinity(MaxHeight) || bottomRight.Y - absolutePosition.Y <= MaxHeight)) { size.Height = this.bottomRight.Y - absolutePosition.Y; topLeft.Y = absolutePosition.Y; } } } else { if (changeHeight) { size.Height = Math.Min(Math.Max(MinHeight, current.Y), MaxHeight); } } if (changeX || changeY) { this.surface.SetWindowPosition(this, topLeft); } this.surface.SetSize(this, size); }
protected override Size MeasureOverride(Size constraint) { Size result; if (this.AutoExpandCanvas) { double panelWidth = 0.0; double panelHeight = 0.0; //initially assume that whole content fits in rectangle with coordinates (0,0, ActualWidth, ActualHeight) double offsetMinusX = 0.0; double offsetMinusY = 0.0; double offsetPlusX = this.rearangeStartSize.Width; double offsetPlusY = this.rearangeStartSize.Height; foreach (UIElement item in this.Children) { FrameworkElement child = item as FrameworkElement; if (null != child) { child.Measure(constraint); //get child's position Point pos = ExtensionSurface.GetPosition(child); //calculate the minimum value of panel's (left,top) corner offsetMinusX = Math.Min(offsetMinusX, pos.X); offsetMinusY = Math.Min(offsetMinusY, pos.Y); //calculate the maximum value of panel's (right, bottom) corner offsetPlusX = Math.Max(offsetPlusX, pos.X + child.DesiredSize.Width); offsetPlusY = Math.Max(offsetPlusY, pos.Y + child.DesiredSize.Height); } } //get required panel's width and height panelWidth = Math.Abs(offsetPlusX - offsetMinusX); panelHeight = Math.Abs(offsetPlusY - offsetMinusY); this.actualPanelRect.Location = new Point(offsetMinusX, offsetMinusY); this.actualPanelRect.Size = new Size(panelWidth, panelHeight); //return it as result result = new Size(panelWidth, panelHeight); } else { result = base.MeasureOverride(constraint); } System.Diagnostics.Debug.WriteLine("MO constraint:" + constraint.Width + "," + constraint.Height + " new: " + result.Width + "," + result.Height); return(result); }
protected override Size ArrangeOverride(Size arrangeSize) { foreach (UIElement child in this.Children) { //get (left, top) coorinates Point pos = ExtensionSurface.GetPosition(child); //include eventual negative offset (panel wouldn't display elements with negative coorinates by default) pos.Offset(this.canvasOffset.X, this.canvasOffset.Y); //request child to rearange itself in given rectangle child.Arrange(new Rect(pos, child.DesiredSize)); } System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "ArrangeOverride Size({0},{1})", arrangeSize.Width, arrangeSize.Height)); return(arrangeSize); }
public Point GetPlacementTargetOffset() { Point offset = new Point(); FrameworkElement target = ExtensionSurface.GetPlacementTarget(this); if (null != target) { FrameworkElement commonRoot = target.FindCommonVisualAncestor(this) as FrameworkElement; MatrixTransform transform = (MatrixTransform)target.TransformToAncestor(commonRoot); Point targetPosition = transform.Transform(new Point()); Point windowPosition = ExtensionSurface.GetPosition(this); offset.X = targetPosition.X - windowPosition.X; offset.Y = targetPosition.Y - windowPosition.Y; } return(offset); }
internal void SetSize(ExtensionWindow window, Size size) { Point pos = ExtensionSurface.GetPosition(window); if (!this.AutoExpandCanvas) { if (IsGreater(pos.X, size.Width, this.ActualWidth)) { size.Width = this.ActualWidth - pos.X; } if (IsGreater(pos.Y, size.Height, this.ActualHeight)) { size.Height = this.ActualHeight - pos.Y; } } System.Diagnostics.Debug.WriteLine("SetSize oldSize (" + window.Width + "," + window.Height + ") newSize (" + size.Width + "," + size.Height + ")"); window.Width = size.Width; window.Height = size.Height; if (this.AutoExpandCanvas) { // this.InvalidateMeasure(); } }
void OnBorderMouseMove(object sender, MouseEventArgs e) { if (!this.border.IsMouseCaptured) { if (this.border.IsMouseDirectlyOver && this.IsResizable && ExtensionSurface.GetMode(this) == ExtensionSurface.PlacementMode.Absolute) { Point position = e.GetPosition(this.border); if (position.X <= BorderOffset && position.Y <= BorderOffset) { this.resizeOption = ResizeValues.TopLeft; Mouse.OverrideCursor = Cursors.SizeNWSE; } else if (position.X >= this.border.ActualWidth - BorderOffset && position.Y <= BorderOffset) { this.resizeOption = ResizeValues.TopRight; Mouse.OverrideCursor = Cursors.SizeNESW; } else if (position.X <= BorderOffset && position.Y >= this.border.ActualHeight - BorderOffset) { this.resizeOption = ResizeValues.BottomLeft; Mouse.OverrideCursor = Cursors.SizeNESW; } else if (position.X >= this.border.ActualWidth - BorderOffset && position.Y >= this.border.ActualHeight - BorderOffset) { this.resizeOption = ResizeValues.BottomRight; Mouse.OverrideCursor = Cursors.SizeNWSE; } else if (position.Y <= (BorderOffset / 2.0)) { this.resizeOption = ResizeValues.Top; Mouse.OverrideCursor = Cursors.SizeNS; } else if (position.Y >= this.border.ActualHeight - (BorderOffset / 2.0)) { this.resizeOption = ResizeValues.Bottom; Mouse.OverrideCursor = Cursors.SizeNS; } else if (position.X <= (BorderOffset / 2.0)) { this.resizeOption = ResizeValues.Left; Mouse.OverrideCursor = Cursors.SizeWE; } else if (position.X >= this.border.ActualWidth - (BorderOffset / 2.0)) { this.resizeOption = ResizeValues.Right; Mouse.OverrideCursor = Cursors.SizeWE; } else { Mouse.OverrideCursor = null; this.resizeOption = ResizeValues.NONE; } Point topLeft = ExtensionSurface.GetPosition(this); this.bottomRight = new Point(topLeft.X + Width, topLeft.Y + Height); } else if (Mouse.OverrideCursor != null) { Mouse.OverrideCursor = null; this.resizeOption = ResizeValues.NONE; } } else if (e.LeftButton == MouseButtonState.Pressed) { this.HandleWindowResize(); } }
void EnsureWindowIsVisible(ExtensionWindow window) { SetWindowPosition(window, ExtensionSurface.GetPosition(window)); }
void PlaceWindow(ExtensionWindow window) { if (null != window) { FrameworkElement target = ExtensionSurface.GetPlacementTarget(window); PositionAlignment alignment = ExtensionSurface.GetAlignment(window); PlacementMode mode = ExtensionSurface.GetMode(window); Point position = ExtensionSurface.GetPosition(window); Point calculatedPosition = new Point(); FrameworkElement commonRoot = null; MatrixTransform transform = null; switch (mode) { case PlacementMode.Relative: if (null != target) { commonRoot = target.FindCommonVisualAncestor(this) as FrameworkElement; if (null == commonRoot) { return; } transform = (MatrixTransform)target.TransformToAncestor(commonRoot); } else { if (!DesignerProperties.GetIsInDesignMode(this)) { Fx.Assert(string.Format(CultureInfo.InvariantCulture, "PlacementTarget must be set in RelativeMode on ExtensionSurface '{0}'", this.Name)); } } break; case PlacementMode.Absolute: calculatedPosition = position; break; default: Fx.Assert(string.Format(CultureInfo.CurrentCulture, "ExtensionWindowPlacement.Mode {0} specified in ExtensionWindow '{1}' is not supported for ExtensionSurface", mode, window.Name)); return; } if (PlacementMode.Relative == mode) { if (null != target) { double x; double y; switch (alignment) { case PositionAlignment.LeftTop: calculatedPosition = transform.Transform(calculatedPosition); break; case PositionAlignment.LeftBottom: calculatedPosition = transform.Transform(new Point(0.0, target.ActualHeight)); break; case PositionAlignment.RightTop: calculatedPosition = transform.Transform(new Point(target.ActualWidth, 0.0)); break; case PositionAlignment.RightBottom: calculatedPosition = transform.Transform(new Point(target.ActualWidth, target.ActualHeight)); break; case PositionAlignment.Center: calculatedPosition = transform.Transform(calculatedPosition); x = ((target.ActualWidth * transform.Matrix.M11) - window.Width) / 2.0; y = ((target.ActualHeight * transform.Matrix.M22) - window.Height) / 2.0; calculatedPosition.Offset(x, y); break; case PositionAlignment.CenterHorizontal: calculatedPosition = transform.Transform(calculatedPosition); x = ((target.ActualWidth * transform.Matrix.M11) - window.Width) / 2.0; calculatedPosition.Offset(x, 0.0); break; case PositionAlignment.CenterVertical: calculatedPosition = transform.Transform(calculatedPosition); y = ((target.ActualHeight * transform.Matrix.M22) - window.Height) / 2.0; calculatedPosition.Offset(0.0, y); break; default: Fx.Assert(string.Format(CultureInfo.CurrentCulture, "ExtensionWindowPlacement.Position = '{0}' is not supported", alignment)); return; } } } SetWindowPosition(window, calculatedPosition); } }