The issue you're encountering—where the sum of all grid column widths plus the vertical scrollbar width is less than the total grid width—stems from how the ResizeColumnsProportionally procedure handles column resizing, particularly in scenarios involving minimum column widths and rounding errors. Let's break down the potential causes and suggest improvements to ensure the columns fully utilize the available grid width.
1. Rounding Errors
Issue:
- The procedure calculates new widths for each column by proportionally distributing the
AvailableWidth. However, since it uses the Round function, small fractional parts (RoundingError) can accumulate and result in the total column widths being slightly less than AvailableWidth.
Example:
- Suppose
AvailableWidth is 500 pixels, and two columns are supposed to be 250 pixels each. Due to rounding, one column might be set to 250 pixels and the other to 249 pixels, leaving 1 pixel unaccounted for.
Impact:
- These tiny discrepancies can accumulate, especially with multiple columns, leading to a noticeable gap between the total column widths and the
AvailableWidth.
Solution:
- Distribute the RoundingError Across Columns:
- Instead of only tracking the
RoundingError, adjust multiple columns to compensate for the accumulated error. This ensures that the total width matches AvailableWidth.
2. Minimum Column Width Enforcement
Issue:
- The procedure enforces a
MinColWidth (20 pixels) for each column. When multiple columns are forced to this minimum width, especially the last column, the remaining space (AvailableWidth - AccumulatedWidth) might still be insufficient to accommodate all columns properly.
Example:
- Consider
AvailableWidth = 300 pixels, with three columns originally proportional to 100 pixels each.
- If the first two columns are set to the
MinColWidth of 20 pixels each, the remaining width for the last column would be 260 pixels (300 - 40). If 260 - VertScrollWidth is still less than the required proportional width, adjustments may lead to gaps.
Impact:
- Enforcing minimum widths can cause the last column to be either too small or leave unallocated space, especially when combined with the scrollbar width adjustment.
Solution:
3. Handling the Vertical Scrollbar
Issue:
- The current approach subtracts the vertical scrollbar width (
VertScrollWidth) only from the last column if the scrollbar is needed. However, AvailableWidth is initially set to the entire ClientWidth, which already includes the scrollbar area. This might lead to miscalculations where the scrollbar space is effectively double-accounted.
Impact:
- This can result in the total column widths plus scrollbar width being less than
ClientWidth, as the scrollbar space is reserved within the AvailableWidth and then subtracted again from the last column.
Solution:
- Adjust AvailableWidth Based on Scrollbar Presence:
- Instead of reserving the full
AvailableWidth for columns, adjust it by subtracting the scrollbar width before distributing widths to columns.
Revised Procedure
Here's a revised version of your ResizeColumnsProportionally procedure that incorporates the solutions mentioned above:
procedure TORMBinding.ResizeColumnsProportionally;
var
i: Integer;
AvailableWidth: Integer;
VertScrollWidth: Integer;
AccumulatedWidth: Integer;
NewWidth: Integer;
RoundingError: Extended;
LastVisibleColumn: Integer;
TotalRowsHeight: Integer;
FixedRowsHeight: Integer;
NeedsVerticalScrollBar: Boolean;
ClientHeight: Integer;
ClientWidth: Integer;
MinColWidth: Integer;
LastColumnWidth: Integer;
OriginalRatio: Extended;
begin
if not Assigned(FOriginalWidths) or (FOriginalWidths.Count = 0) or
(FInitialTotalWidth = 0) then
Exit;
// Get grid dimensions and scrollbar width
ClientWidth := FGrid.ClientRect.Width;
ClientHeight := FGrid.ClientRect.Height;
VertScrollWidth := GetSystemMetrics(SM_CXVSCROLL);
MinColWidth := 20;
// Calculate total height
TotalRowsHeight := 0;
FixedRowsHeight := 0;
for i := 0 to FGrid.FixedRows - 1 do
Inc(FixedRowsHeight, FGrid.RowHeights[i]);
for i := FGrid.FixedRows to FGrid.RowCount - 1 do
Inc(TotalRowsHeight, FGrid.RowHeights[i]);
// Check if vertical scrollbar is needed
NeedsVerticalScrollBar := (TotalRowsHeight + FixedRowsHeight > ClientHeight) and
(FGrid.ScrollBars in [ssVertical, ssBoth]);
// Adjust AvailableWidth based on scrollbar
AvailableWidth := ClientWidth;
if NeedsVerticalScrollBar then
AvailableWidth := AvailableWidth - VertScrollWidth;
// Find last visible column
LastVisibleColumn := -1;
for i := FGrid.ColCount - 1 downto 0 do
begin
if FGrid.ColWidths[i] > 0 then
begin
LastVisibleColumn := i;
Break;
end;
end;
if LastVisibleColumn = -1 then
Exit;
// Calculate and apply new column widths
AccumulatedWidth := 0;
RoundingError := 0;
// Process all columns except the last one
for i := 0 to LastVisibleColumn - 1 do
begin
if FGrid.ColWidths[i] > 0 then
begin
OriginalRatio := FOriginalWidths[i] / FInitialTotalWidth;
NewWidth := Round(OriginalRatio * AvailableWidth + RoundingError);
RoundingError := (OriginalRatio * AvailableWidth + RoundingError) - NewWidth;
if NewWidth < MinColWidth then
NewWidth := MinColWidth;
FGrid.ColWidths[i] := NewWidth;
Inc(AccumulatedWidth, NewWidth);
end;
end;
// Calculate last column width to fill remaining space
LastColumnWidth := AvailableWidth - AccumulatedWidth;
// Ensure minimum width for the last column
if LastColumnWidth < MinColWidth then
LastColumnWidth := MinColWidth;
// Adjust last column width
FGrid.ColWidths[LastVisibleColumn] := LastColumnWidth;
// Optional: Distribute any remaining pixels due to rounding errors
// This ensures that total column widths exactly match AvailableWidth
AccumulatedWidth := 0;
for i := 0 to LastVisibleColumn do
Inc(AccumulatedWidth, FGrid.ColWidths[i]);
// If there's a discrepancy, adjust the last column
if AccumulatedWidth < AvailableWidth then
FGrid.ColWidths[LastVisibleColumn] := FGrid.ColWidths[LastVisibleColumn] + (AvailableWidth - AccumulatedWidth);
end;
Key Changes and Explanations
-
Adjusting AvailableWidth Based on Scrollbar Presence:
if NeedsVerticalScrollBar then
AvailableWidth := AvailableWidth - VertScrollWidth;
- Subtracting the scrollbar width upfront ensures that the columns don't inadvertently reserve space for the scrollbar, preventing double subtraction.
-
Ensuring the Last Column Fills Remaining Space:
- After distributing widths to all columns except the last, the last column is set to exactly fill the remaining
AvailableWidth.
- An additional check ensures the last column meets the
MinColWidth.
-
Distributing Rounding Errors:
- After setting all column widths, the total accumulated width is recalculated.
- If there's any discrepancy due to rounding, the remaining pixels are added to the last column to ensure the total matches
AvailableWidth.
-
Simplifying Scrollbar Adjustment:
- By adjusting
AvailableWidth upfront, there's no need to subtract VertScrollWidth from the last column later, reducing complexity and potential errors.
Additional Recommendations
By implementing these changes, the procedure should more accurately distribute column widths, fully utilize the available grid space, and account for scrollbar presence without leaving unintended gaps.