internal static bool TryGetParentExtensionWindow(FrameworkElement element, out ExtensionWindow window, out ExtensionSurface surface) { window = null; surface = null; if (null != element) { FrameworkElement current = element; window = element.TemplatedParent as ExtensionWindow; while (null == window && null != current) { window = current as ExtensionWindow; current = (FrameworkElement)current.Parent; } if (null != window) { current = window; surface = window.TemplatedParent as ExtensionSurface; while (null == surface && null != current) { surface = current as ExtensionSurface; current = (FrameworkElement)current.Parent; } } } return(null != window && null != surface); }
void OnChildWindowLoaded(object sender, EventArgs e) { ExtensionWindow window = (ExtensionWindow)sender; this.OnWindowVisibilityChanged(window, null); window.Loaded -= OnChildWindowLoaded; }
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved) { ExtensionWindow window = visualRemoved as ExtensionWindow; if (null != window) { window.VisibilityChanged -= OnWindowVisibilityChanged; // window.SizeChanged -= OnWindowSizeChanged; this.rearangeStartSize.Width = 0; this.rearangeStartSize.Height = 0; } base.OnVisualChildrenChanged(visualAdded, visualRemoved); window = visualAdded as ExtensionWindow; if (null != window) { window.VisibilityChanged += OnWindowVisibilityChanged; // window.SizeChanged += OnWindowSizeChanged; if (!window.IsLoaded) { window.Loaded += OnChildWindowLoaded; } } }
static void OnPlacementModeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { ExtensionWindow window = sender as ExtensionWindow; if (null != window && null != window.Surface && window.Visibility == Visibility.Visible) { window.Surface.PlaceWindow(window); } }
protected override void OnVisualParentChanged(DependencyObject oldParent) { base.OnVisualParentChanged(oldParent); if (!DesignerProperties.GetIsInDesignMode(this) && !ExtensionWindow.TryGetParentExtensionWindow(this, out this.parent, out this.surface)) { Fx.Assert("ExtensionWindowHeader cannot be used outside ExtensionWindow"); } }
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(); } }
public void SelectWindow(ExtensionWindow window) { if (null != window && this.Children.Contains(window)) { this.selectedChild = new KeyValuePair <FrameworkElement, Point>(window, this.canvasOffset); this.rearangeStartSize.Width = this.ActualWidth; this.rearangeStartSize.Height = this.ActualHeight; Panel.SetZIndex(window, ++this.currentZIndex); } }
static internal void RaiseWindowCloseEvent(ExtensionWindow sender) { ExtensionWindowClosingRoutedEventArgs args = new ExtensionWindowClosingRoutedEventArgs(ClosingEvent, sender); sender.RaiseEvent(args); if (!args.Cancel) { sender.RaiseEvent(new RoutedEventArgs(CloseEvent, sender)); } }
void OnExtensionWindowClosed(object sender, RoutedEventArgs args) { ExtensionWindow window = args.Source as ExtensionWindow; if (null != window) { //remove window from children collection this.Children.Remove(window); } }
public override void OnApplyTemplate() { base.OnApplyTemplate(); this.closeButton = this.Template.FindName("PART_CloseButton", this) as Button; if (null != this.closeButton) { this.closeButton.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e) { ExtensionWindow.RaiseWindowCloseEvent(this.parent); } ); } }
//void OnWindowSizeChanged(object sender, SizeChangedEventArgs e) //{ // ExtensionWindow window = (ExtensionWindow)sender; // // EnsureWindowIsVisible(window); //} void OnWindowVisibilityChanged(object sender, RoutedEventArgs args) { ExtensionWindow window = (ExtensionWindow)sender; if (window.IsVisible) { Func <double, bool> IsInvalid = x => (double.IsInfinity(x) || double.IsNaN(x) || double.Epsilon > x); if (IsInvalid(window.ActualWidth) || IsInvalid(window.ActualWidth) || IsInvalid(window.DesiredSize.Width) || IsInvalid(window.DesiredSize.Height)) { window.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); } PlaceWindow(window); } }
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) { base.OnRenderSizeChanged(sizeInfo); foreach (FrameworkElement child in this.Children) { ExtensionWindow window = child as ExtensionWindow; if (null != window) { if (PlacementMode.Relative == GetMode(window) && null != GetPlacementTarget(window)) { this.PlaceWindow(window); continue; } if (!this.AutoExpandCanvas) { this.EnsureWindowIsVisible(window); } } } }
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(); } }
internal static bool TryGetParentExtensionWindow(FrameworkElement element, out ExtensionWindow window, out ExtensionSurface surface) { window = null; surface = null; if (null != element) { FrameworkElement current = element; window = element.TemplatedParent as ExtensionWindow; while (null == window && null != current) { window = current as ExtensionWindow; current = (FrameworkElement)current.Parent; } if (null != window) { current = window; surface = window.TemplatedParent as ExtensionSurface; while (null == surface && null != current) { surface = current as ExtensionSurface; current = (FrameworkElement)current.Parent; } } } return (null != window && null != surface); }
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); } }
public void SelectWindow(ExtensionWindow window) { if (null != window && this.Children.Contains(window)) { this.selectedChild = new KeyValuePair<FrameworkElement, Point>(window, this.canvasOffset); this.rearangeStartSize.Width = this.ActualWidth; this.rearangeStartSize.Height = this.ActualHeight; Panel.SetZIndex(window, ++this.currentZIndex); } }
void EnsureWindowIsVisible(ExtensionWindow window) { SetWindowPosition(window, ExtensionSurface.GetPosition(window)); }
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(); } }