// Get the preferred size of each child widget, including the margins Size[] GetPreferredChildrenSizes(TablePlacement[] visibleChildren, bool useWidthConstraint, bool useHeightConstraint) { var sizes = new Size [visibleChildren.Length]; for (int n = 0; n < visibleChildren.Length; n++) { var bp = visibleChildren[n]; Size s; if (useWidthConstraint) { s = bp.Child.Surface.GetPreferredSize(SizeConstraint.WithSize(bp.NextWidth - bp.Child.Margin.HorizontalSpacing), SizeConstraint.Unconstrained); } else if (useHeightConstraint) { s = bp.Child.Surface.GetPreferredSize(SizeConstraint.Unconstrained, SizeConstraint.WithSize(bp.NextHeight - bp.Child.Margin.VerticalSpacing)); } else { s = bp.Child.Surface.GetPreferredSize(SizeConstraint.Unconstrained, SizeConstraint.Unconstrained); } s.Width += bp.Child.Margin.HorizontalSpacing; s.Height += bp.Child.Margin.VerticalSpacing; sizes [n] = s; } return(sizes); }
void CalcDefaultSizes(double width, double height) { bool vertical = direction == Orientation.Vertical; int nexpands = 0; double requiredSize = 0; double availableSize = vertical ? height : width; var widthConstraint = vertical ? SizeConstraint.WithSize(width) : SizeConstraint.Unconstrained; var heightConstraint = vertical ? SizeConstraint.Unconstrained : SizeConstraint.WithSize(height); var visibleChildren = children.Where(b => b.Child.Visible).ToArray(); var sizes = new Dictionary <BoxPlacement, double> (); // Get the natural size of each child foreach (var bp in visibleChildren) { Size s; s = bp.Child.Surface.GetPreferredSize(widthConstraint, heightConstraint, true); bp.NextSize = vertical ? s.Height : s.Width; sizes [bp] = bp.NextSize; requiredSize += bp.NextSize; if (bp.Child.ExpandsForOrientation(direction)) { nexpands++; } } double remaining = availableSize - requiredSize - (spacing * (double)(visibleChildren.Length - 1)); if (remaining < 0) { // The box is not big enough to fit the widgets using its natural size. // We have to shrink the widgets. // The total amount we have to shrink double shrinkSize = -remaining; var sizePart = new SizeSplitter(shrinkSize, visibleChildren.Length); foreach (var bp in visibleChildren) { bp.NextSize -= sizePart.NextSizePart(); } } else { var expandRemaining = new SizeSplitter(remaining, nexpands); foreach (var bp in visibleChildren) { if (bp.Child.ExpandsForOrientation(direction)) { bp.NextSize += expandRemaining.NextSizePart(); } } } }
public static SizeConstraint operator +(SizeConstraint c, double s) { if (!c.IsConstrained) { return(c); } else { return(SizeConstraint.WithSize(c.AvailableSize + s)); } }
public static SizeConstraint operator -(SizeConstraint c, double s) { if (!c.IsConstrained) { return(c); } else { return(SizeConstraint.WithSize(Math.Max(c.AvailableSize - s, 0))); } }
internal override void AdjustSize() { Size mMinSize, mDecorationsSize; Backend.GetMetrics(out mMinSize, out mDecorationsSize); var size = shown ? Size : initialBounds.Size; var wc = (shown || widthSet) ? SizeConstraint.WithSize(Math.Max(size.Width - padding.HorizontalSpacing - mDecorationsSize.Width, mMinSize.Width)) : SizeConstraint.Unconstrained; var hc = (shown || heightSet) ? SizeConstraint.WithSize(Math.Max(size.Height - padding.VerticalSpacing - mDecorationsSize.Height, mMinSize.Height)) : SizeConstraint.Unconstrained; var ws = mDecorationsSize; if (child != null) { IWidgetSurface s = child.Surface; ws += s.GetPreferredSize(wc, hc, true); } ws.Width += padding.HorizontalSpacing; ws.Height += padding.VerticalSpacing; if (!shown) { if (!widthSet) { size.Width = ws.Width; } if (!heightSet) { size.Height = ws.Height; } } if (ws.Width < mMinSize.Width) { ws.Width = mMinSize.Width; } if (ws.Height < mMinSize.Height) { ws.Height = mMinSize.Height; } if (ws.Width > size.Width) { size.Width = ws.Width; } if (ws.Height > size.Height) { size.Height = ws.Height; } if (!shown) { shown = true; if (!locationSet && initialLocation != WindowLocation.Manual) { Point center; if (initialLocation == WindowLocation.CenterScreen || TransientFor == null) { center = Desktop.PrimaryScreen.VisibleBounds.Center; } else { center = TransientFor.ScreenBounds.Center; } initialBounds.X = Math.Round(center.X - size.Width / 2); initialBounds.Y = Math.Round(center.Y - size.Height / 2); locationSet = true; } if (size != Size) { if (locationSet) { Backend.Bounds = new Rectangle(initialBounds.X, initialBounds.Y, size.Width, size.Height); } else { Backend.SetSize(size.Width, size.Height); } } else if (locationSet && !shown) { Backend.Move(initialBounds.X, initialBounds.Y); } } else { if (size != Size) { Backend.SetSize(size.Width, size.Height); } } Backend.SetMinSize(new Size(ws.Width, ws.Height)); }
internal Size GetPreferredSize(double width, double height) { var wc = double.IsPositiveInfinity(width) ? SizeConstraint.Unconstrained : SizeConstraint.WithSize(width); var hc = double.IsPositiveInfinity(height) ? SizeConstraint.Unconstrained : SizeConstraint.WithSize(height); return(Child.Surface.GetPreferredSize(wc, hc)); }
internal override void AdjustSize() { if (child == null) { return; } IWidgetSurface s = child.Surface; var size = shown ? Size : initialBounds.Size; var wc = (shown || widthSet) ? SizeConstraint.WithSize(size.Width - padding.HorizontalSpacing) : SizeConstraint.Unconstrained; var hc = (shown || heightSet) ? SizeConstraint.WithSize(size.Height - padding.VerticalSpacing) : SizeConstraint.Unconstrained; var ws = s.GetPreferredSize(wc, hc, true); if (!shown) { if (!widthSet) { size.Width = ws.Width + padding.HorizontalSpacing; } if (!heightSet) { size.Height = ws.Height + padding.VerticalSpacing; } } if (ws.Width + padding.HorizontalSpacing > size.Width) { size.Width = ws.Width + padding.HorizontalSpacing; } if (ws.Height + padding.VerticalSpacing > size.Height) { size.Height = ws.Height + padding.VerticalSpacing; } size += Backend.ImplicitMinSize; if (!shown) { shown = true; if (size != Size) { if (locationSet) { Backend.Bounds = new Rectangle(initialBounds.X, initialBounds.Y, size.Width, size.Height); } else { Backend.SetSize(size.Width, size.Height); } } else if (locationSet && !shown) { Backend.Move(initialBounds.X, initialBounds.Y); } Backend.SetMinSize(Backend.ImplicitMinSize + new Size(ws.Width + padding.HorizontalSpacing, ws.Height + padding.VerticalSpacing)); } else { if (size != Size) { Backend.SetSize(size.Width, size.Height); } } }