private static bool AllocateAlignmentSpace(int alignHIndex,
int alignVIndex,
Size[,] allocation,
Rectangle displayRect,
int spacingGap,
PaletteTextTrim trim,
ref Size requiredSize)
{
// Cache the current target value
Size cacheSize = allocation[alignHIndex, alignVIndex];
// Track the width needed to show the item
bool applyGap = false;
int allocateWidth = requiredSize.Width;
// If there is already something in the cell
if (allocation[alignHIndex, alignVIndex].Width > 0)
{
// Then add the spacing gap to the required size width
allocateWidth += spacingGap;
applyGap = true;
}
// Find the current allocated total width
int totalWidth = AllocatedTotalWidth(allocation, alignHIndex, alignVIndex, spacingGap);
// How much space is available for allocation?
int freeSpace = displayRect.Width - totalWidth;
// If not enough room then we failed
if (freeSpace < allocateWidth)
{
// Should we try and trim the text into the space?
if (trim != PaletteTextTrim.Hide)
{
// If there is some room available after taking
// into account the need for a spacing gap
if ((allocateWidth == requiredSize.Width) ||
((allocateWidth > requiredSize.Width) && applyGap))
{
// Allocate just the available space
allocateWidth = freeSpace;
// Reduce the reported size back to the caller
if (applyGap)
requiredSize.Width = allocateWidth - spacingGap;
else
requiredSize.Width = allocateWidth;
}
else
return false;
}
else
return false;
}
// There is enough space for all the content, so add into the cell width
allocation[alignHIndex, alignVIndex].Width += allocateWidth;
// If the required height is greater than the current cell height
if (requiredSize.Height > allocation[alignHIndex, alignVIndex].Height)
{
// Then use the required height instead
allocation[alignHIndex, alignVIndex].Height = requiredSize.Height;
}
// Find the allocated total height as a result
int totalHeight = AllocatedTotalHeight(allocation);
// If not enough height then we failed
if (totalHeight > displayRect.Height)
{
// Restore the original cell
allocation[alignHIndex, alignVIndex] = cacheSize;
return false;
}
return true;
}