diff --git a/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp b/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp index 4dc53dc32c..11075ace7e 100644 --- a/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp +++ b/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp @@ -500,6 +500,33 @@ void TableHeaderComponent::reactToMenuItem (const int menuReturnId, const int /* setColumnVisible (menuReturnId, ! isColumnVisible (menuReturnId)); } +void TableHeaderComponent::drawColumnHeader (Graphics& g, LookAndFeel& lf, const ColumnInfo& ci) +{ + // Only paint columns that are visible + if (! ci.isVisible()) + return; + + // If this column is being dragged, it shouldn't be drawn in the table header + if (ci.id == columnIdBeingDragged && dragOverlayComp != nullptr && dragOverlayComp->isVisible()) + return; + + // There's no point drawing this column header if no part of it is visible + if (! g.getClipBounds() + .getHorizontalRange() + .intersects (Range::withStartAndLength (ci.getX(), ci.width))) + return; + + Graphics::ScopedSaveState ss (g); + + g.setOrigin (ci.getX(), ci.getY()); + g.reduceClipRegion (0, 0, ci.width, ci.getHeight()); + + lf.drawTableHeaderColumn (g, *this, ci.getTitle(), ci.id, ci.width, getHeight(), + ci.id == columnIdUnderMouse, + ci.id == columnIdUnderMouse && isMouseButtonDown(), + ci.propertyFlags); +} + void TableHeaderComponent::paint (Graphics& g) { auto& lf = getLookAndFeel(); @@ -507,48 +534,18 @@ void TableHeaderComponent::paint (Graphics& g) lf.drawTableHeaderBackground (g, *this); for (auto* ci : columns) - { - if (ci->isVisible() && ci->getWidth() > 0) - { - Graphics::ScopedSaveState ss (g); - - g.setOrigin (ci->getX(), ci->getY()); - g.reduceClipRegion (0, 0, ci->getWidth(), ci->getHeight()); - - lf.drawTableHeaderColumn (g, *this, ci->getTitle(), ci->id, ci->width, getHeight(), - ci->id == columnIdUnderMouse, - ci->id == columnIdUnderMouse && isMouseButtonDown(), - ci->propertyFlags); - } - } + drawColumnHeader (g, lf, *ci); } void TableHeaderComponent::resized() { - auto clip = getBounds(); - int x = 0; - for (auto* ci : columns) - ci->setBounds (0, 0, 0, 0); - for (auto* ci : columns) { - if (ci->isVisible()) - { - if (x + ci->width > clip.getX() - && (ci->id != columnIdBeingDragged - || dragOverlayComp == nullptr - || ! dragOverlayComp->isVisible())) - { - ci->setBounds (x, 0, ci->width, getHeight()); - } - - x += ci->width; - - if (x >= clip.getRight()) - break; - } + const auto widthToUse = ci->isVisible() ? ci->width : 0; + ci->setBounds (x, 0, widthToUse, getHeight()); + x += widthToUse; } } diff --git a/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.h b/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.h index 3d1b017350..5afa5a3854 100644 --- a/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.h +++ b/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.h @@ -453,6 +453,7 @@ private: void updateColumnUnderMouse (const MouseEvent&); void setColumnUnderMouse (int columnId); void resizeColumnsToFit (int firstColumnIndex, int targetTotalWidth); + void drawColumnHeader (Graphics&, LookAndFeel&, const ColumnInfo&); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableHeaderComponent) };