Shrink excess width from a set of item, by removing width from the larger items first. Set items Width to -1.0f to disable shrinking this item.
| 1608 | // Shrink excess width from a set of item, by removing width from the larger items first. |
| 1609 | // Set items Width to -1.0f to disable shrinking this item. |
| 1610 | void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess) |
| 1611 | { |
| 1612 | if (count == 1) |
| 1613 | { |
| 1614 | if (items[0].Width >= 0.0f) |
| 1615 | items[0].Width = ImMax(items[0].Width - width_excess, 1.0f); |
| 1616 | return; |
| 1617 | } |
| 1618 | ImQsort(items, (size_t)count, sizeof(ImGuiShrinkWidthItem), ShrinkWidthItemComparer); |
| 1619 | int count_same_width = 1; |
| 1620 | while (width_excess > 0.0f && count_same_width < count) |
| 1621 | { |
| 1622 | while (count_same_width < count && items[0].Width <= items[count_same_width].Width) |
| 1623 | count_same_width++; |
| 1624 | float max_width_to_remove_per_item = (count_same_width < count && items[count_same_width].Width >= 0.0f) ? (items[0].Width - items[count_same_width].Width) : (items[0].Width - 1.0f); |
| 1625 | if (max_width_to_remove_per_item <= 0.0f) |
| 1626 | break; |
| 1627 | float width_to_remove_per_item = ImMin(width_excess / count_same_width, max_width_to_remove_per_item); |
| 1628 | for (int item_n = 0; item_n < count_same_width; item_n++) |
| 1629 | items[item_n].Width -= width_to_remove_per_item; |
| 1630 | width_excess -= width_to_remove_per_item * count_same_width; |
| 1631 | } |
| 1632 | |
| 1633 | // Round width and redistribute remainder |
| 1634 | // Ensure that e.g. the right-most tab of a shrunk tab-bar always reaches exactly at the same distance from the right-most edge of the tab bar separator. |
| 1635 | width_excess = 0.0f; |
| 1636 | for (int n = 0; n < count; n++) |
| 1637 | { |
| 1638 | float width_rounded = ImFloor(items[n].Width); |
| 1639 | width_excess += items[n].Width - width_rounded; |
| 1640 | items[n].Width = width_rounded; |
| 1641 | } |
| 1642 | while (width_excess > 0.0f) |
| 1643 | for (int n = 0; n < count && width_excess > 0.0f; n++) |
| 1644 | { |
| 1645 | float width_to_add = ImMin(items[n].InitialWidth - items[n].Width, 1.0f); |
| 1646 | items[n].Width += width_to_add; |
| 1647 | width_excess -= width_to_add; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | //------------------------------------------------------------------------- |
| 1652 | // [SECTION] Widgets: ComboBox |