private bool LayoutHorizontal()
{
ToolStrip toolStrip = ToolStrip;
Rectangle clientRectangle = toolStrip.ClientRectangle;
#if DEBUG
if (DebugLayoutTraceSwitch.TraceVerbose)
{
Debug.WriteLine("_________________________\r\nHorizontal Layout:" + toolStrip.ToString() + displayRectangle.ToString());
}
#endif
int lastRight = displayRectangle.Right;
int lastLeft = displayRectangle.Left;
bool needsMoreSpace = false;
Size itemSize = Size.Empty;
Rectangle alignedLeftItems = Rectangle.Empty;
Rectangle alignedRightItems = Rectangle.Empty;
// this will determine where the item should be placed.
CalculatePlacementsHorizontal();
bool needOverflow = toolStrip.CanOverflow && ((OverflowRequired) || (OverflowSpace >= OverflowButtonSize.Width));
toolStrip.OverflowButton.Visible = needOverflow;
// if we require the overflow, it should stick up against the edge of the toolstrip.
if (needOverflow)
{
if (toolStrip.RightToLeft == RightToLeft.No)
{
lastRight = clientRectangle.Right;
}
else
{
lastLeft = clientRectangle.Left;
}
}
for (int j = -1; j < toolStrip.Items.Count; j++)
{
ToolStripItem item = null;
if (j == -1)
{
// the first time through place the overflow button if its required.
if (needOverflow)
{
item = toolStrip.OverflowButton;
item.SetPlacement(ToolStripItemPlacement.Main);
itemSize = OverflowButtonSize;
}
else
{
item = toolStrip.OverflowButton;
item.SetPlacement(ToolStripItemPlacement.None);
continue;
}
}
else
{
item = toolStrip.Items[j];
if (!((IArrangedElement)item).ParticipatesInLayout)
{
// skip over items not participating in layout. E.G. not visible items
continue;
}
// since we havent parented the item yet - the auto size wont have reset the size yet.
itemSize = item.AutoSize ? item.GetPreferredSize(Size.Empty) : item.Size;
}
// if it turns out we dont need the overflow (because there are no Overflow.Always items and the width of everything
// in the overflow is less than the width of the overflow button then reset the placement of the as needed items to
// main.
if (!needOverflow && (item.Overflow == ToolStripItemOverflow.AsNeeded && item.Placement == ToolStripItemPlacement.Overflow))
{
#if DEBUG
if (DebugLayoutTraceSwitch.TraceVerbose)
{
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Resetting {0} to Main - we dont need it to overflow", item));
}
#endif
item.SetPlacement(ToolStripItemPlacement.Main);
}
// Now do the guts of setting X, Y and parenting.
// We need to honor left to right and head and tail.
// In RTL.Yes, Head is to the Right, Tail is to the Left
// In RTL.No, Head is to the Left, Tail is to the Right
if ((item != null) && (item.Placement == ToolStripItemPlacement.Main))
{
int x = displayRectangle.Left;
int y = displayRectangle.Top;
Padding itemMargin = item.Margin;
if (((item.Alignment == ToolStripItemAlignment.Right) && (toolStrip.RightToLeft == RightToLeft.No)) || ((item.Alignment == ToolStripItemAlignment.Left) && (toolStrip.RightToLeft == RightToLeft.Yes)))
{
// lastRight x Margin.Right
// [Item]<----------[Item]----------->|
// Margin.Left
// this item should be placed to the right
// we work backwards from the right edge - that is place items from right to left.
x = lastRight - (itemMargin.Right + itemSize.Width);
y += itemMargin.Top;
lastRight = x - itemMargin.Left;
alignedRightItems = (alignedRightItems == Rectangle.Empty) ? new Rectangle(x, y, itemSize.Width, itemSize.Height)
: Rectangle.Union(alignedRightItems, new Rectangle(x, y, itemSize.Width, itemSize.Height));
}
else
{
// x Margin.Right lastLeft
// |<----------[Item]------------>|
// Margin.Left
// this item should be placed to the left
// we work forwards from the left - that is place items from left to right
x = lastLeft + itemMargin.Left;
y += itemMargin.Top;
lastLeft = x + itemSize.Width + itemMargin.Right;
alignedLeftItems = (alignedLeftItems == Rectangle.Empty) ? new Rectangle(x, y, itemSize.Width, itemSize.Height)
: Rectangle.Union(alignedLeftItems, new Rectangle(x, y, itemSize.Width, itemSize.Height));
}
item.ParentInternal = ToolStrip;
Point itemLocation = new Point(x, y);
if (!clientRectangle.Contains(x, y))
{
item.SetPlacement(ToolStripItemPlacement.None);
}
else if (alignedRightItems.Width > 0 && alignedLeftItems.Width > 0 && alignedRightItems.IntersectsWith(alignedLeftItems))
{
itemLocation = noMansLand;
item.SetPlacement(ToolStripItemPlacement.None);
}
if (item.AutoSize)
{
// autosized items stretch from edge-edge
itemSize.Height = Math.Max(displayRectangle.Height - itemMargin.Vertical, 0);
}
else
{
// non autosized items are vertically centered
Rectangle bounds = LayoutUtils.VAlign(item.Size, displayRectangle, AnchorStyles.None);
itemLocation.Y = bounds.Y;
}
SetItemLocation(item, itemLocation, itemSize);
}
else
{
item.ParentInternal = (item.Placement == ToolStripItemPlacement.Overflow) ? toolStrip.OverflowButton.DropDown : null;
}
#if DEBUG
if (DebugLayoutTraceSwitch.TraceVerbose)
{
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Item {0} Placement {1} Bounds {2} Parent {3}", item.ToString(), item.Placement.ToString(), item.Bounds.ToString(), (item.ParentInternal == null) ? "null" : item.ParentInternal.ToString()));
}
#endif
}
return(needsMoreSpace);
}