/// <summary> /// Insert an item at the given index. /// </summary> /// <param name="item"></param> public VCItemPlacement Insert(int index, VCItem item, Layout.LayoutConstraints constraints) { VCItemPlacement placement = new VCItemPlacement(item, constraints); items.Insert(index, placement); item.ContainerEvent += new EventHandler <ContainerEventArgs>(item_ContainerEvent); if (Added != null) { Added(this, new VCItemPlacementEventArgs(placement)); } return(placement); }
/// <summary> /// Replace the item at given index with given item /// </summary> /// <param name="item"></param> public VCItemPlacement Replace(int index, VCItem item, Layout.LayoutConstraints constraints) { VCItemPlacement oldPlacement = items[index]; oldPlacement.Item.ContainerEvent -= new EventHandler <ContainerEventArgs>(item_ContainerEvent); VCItemPlacement placement = new VCItemPlacement(item, constraints); item.ContainerEvent += new EventHandler <ContainerEventArgs>(item_ContainerEvent); items[index] = placement; if (Removed != null) { Removed(this, new VCItemPlacementEventArgs(oldPlacement)); } if (Added != null) { Added(this, new VCItemPlacementEventArgs(placement)); } return(placement); }
/// <summary> /// Set the locations of the given placements. /// </summary> /// <param name="placements">All placements that must be layed out.</param> /// <param name="controlSize">The size of the control (in virtual units)</param> public virtual void Layout(ICollection <VCItemPlacement> placements, SizeF controlSize) { if (placements.Count > 0) { // Calculate preferred size int maxHeight = 0; int width = padding.Horizontal + (Math.Max(0, placements.Count - 1) * itemSpacing); foreach (VCItemPlacement placement in placements) { Size itemSz = placement.Item.PreferredSize; maxHeight = Math.Max(maxHeight, itemSz.Height + padding.Vertical); width += itemSz.Width; } this.preferredSize = new Size(width, maxHeight); // Calculate horizontal displacement to deal with horizontal alignment int x = padding.Left; if (width < controlSize.Width) { switch (vAlignment) { default: case VerticalAlignment.Top: break; case VerticalAlignment.Middle: x += (int)((controlSize.Width - width - padding.Horizontal) / 2); break; case VerticalAlignment.Bottom: x += (int)(controlSize.Width - width - padding.Right); break; } } // Calculate final size this.size = new Size(Math.Max(width, (int)controlSize.Width), maxHeight); // Calculate item bounds foreach (VCItemPlacement placement in placements) { Size itemSize = placement.Item.PreferredSize; LayoutConstraints constraints = placement.Constraints; if (constraints != null) { if ((constraints.Fill & FillDirection.Vertical) != 0) { itemSize.Height = maxHeight - padding.Vertical; } } int y; switch (vAlignment) { default: case VerticalAlignment.Top: y = padding.Top; break; case VerticalAlignment.Middle: y = Math.Max(padding.Top, (int)((Math.Max(controlSize.Height, maxHeight) - itemSize.Height) / 2)); break; case VerticalAlignment.Bottom: y = Math.Max(padding.Top, (int)(Math.Max(controlSize.Height, maxHeight) - padding.Bottom)); break; } placement.Bounds = new Rectangle(new Point(x, y), itemSize); x += itemSize.Width + itemSpacing; } } else { this.preferredSize = Size.Empty; this.size = Size.Empty; } }
/// <summary> /// Default ctor /// </summary> /// <param name="item"></param> internal VCItemPlacement(VCItem item, Layout.LayoutConstraints constraints) { this.item = item; this.constraints = constraints; transform = Identity; }
/// <summary> /// Set the locations of the given placements. /// </summary> /// <param name="placements">All placements that must be layed out.</param> /// <param name="controlSize">The size of the control (in virtual units)</param> public virtual void Layout(ICollection <VCItemPlacement> placements, SizeF controlSize) { if (placements.Count > 0) { // Calculate maximum width and total height int maxWidth = 0; int maxHeight = 0; foreach (VCItemPlacement placement in placements) { Size itemSz = placement.Item.PreferredSize; maxWidth = Math.Max(maxWidth, itemSz.Width + padding.Horizontal); maxHeight = Math.Max(maxHeight, itemSz.Height + padding.Vertical); } // Calculate final size this.preferredSize = this.size = new Size(maxWidth, maxHeight); // Calculate item bounds foreach (VCItemPlacement placement in placements) { Size itemSize = placement.Item.PreferredSize; LayoutConstraints constraints = placement.Constraints; if (constraints != null) { if ((constraints.Fill & FillDirection.Horizontal) != 0) { itemSize.Width = maxWidth - padding.Horizontal; } if ((constraints.Fill & FillDirection.Vertical) != 0) { itemSize.Height = maxHeight - padding.Vertical; } } int x; switch (hAlignment) { default: case HorizontalAlignment.Left: x = padding.Left; break; case HorizontalAlignment.Center: x = Math.Max(padding.Left, (int)((Math.Max(controlSize.Width, maxWidth) - itemSize.Width) / 2)); break; case HorizontalAlignment.Right: x = Math.Max(padding.Left, (int)(Math.Max(controlSize.Width, maxWidth) - padding.Right)); break; } int y; switch (vAlignment) { default: case VerticalAlignment.Top: y = padding.Top; break; case VerticalAlignment.Middle: y = Math.Max(padding.Top, (int)((Math.Max(controlSize.Height, maxHeight) - itemSize.Height) / 2)); break; case VerticalAlignment.Bottom: y = Math.Max(padding.Top, (int)(Math.Max(controlSize.Height, maxHeight) - padding.Bottom)); break; } Rectangle bounds = new Rectangle(new Point(x, y), itemSize); placement.Bounds = bounds; size.Width = Math.Max(size.Width, bounds.Right); size.Height = Math.Max(size.Height, bounds.Bottom); } } else { this.preferredSize = Size.Empty; this.size = Size.Empty; } }