Browse Source

Modernised some code in TableListBox

tags/2021-05-28
jules 8 years ago
parent
commit
e30178418c
4 changed files with 155 additions and 196 deletions
  1. +106
    -141
      modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp
  2. +4
    -5
      modules/juce_gui_basics/widgets/juce_TableHeaderComponent.h
  3. +42
    -47
      modules/juce_gui_basics/widgets/juce_TableListBox.cpp
  4. +3
    -3
      modules/juce_gui_basics/widgets/juce_TableListBox.h

+ 106
- 141
modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp View File

@@ -27,8 +27,7 @@
class TableHeaderComponent::DragOverlayComp : public Component
{
public:
DragOverlayComp (const Image& image_)
: image (image_)
DragOverlayComp (const Image& i) : image (i)
{
image.duplicateIfShared();
image.multiplyAllAlphas (0.8f);
@@ -40,7 +39,6 @@ public:
g.drawImageAt (image, 0, 0);
}
private:
Image image;
JUCE_DECLARE_NON_COPYABLE (DragOverlayComp)
@@ -49,15 +47,6 @@ private:
//==============================================================================
TableHeaderComponent::TableHeaderComponent()
: columnsChanged (false),
columnsResized (false),
sortChanged (false),
menuActive (true),
stretchToFit (false),
columnIdBeingResized (0),
columnIdBeingDragged (0),
columnIdUnderMouse (0),
lastDeliberateWidth (0)
{
}
@@ -67,12 +56,12 @@ TableHeaderComponent::~TableHeaderComponent()
}
//==============================================================================
void TableHeaderComponent::setPopupMenuActive (const bool hasMenu)
void TableHeaderComponent::setPopupMenuActive (bool hasMenu)
{
menuActive = hasMenu;
}
bool TableHeaderComponent::isPopupMenuActive() const { return menuActive; }
bool TableHeaderComponent::isPopupMenuActive() const { return menuActive; }
//==============================================================================
@@ -82,8 +71,8 @@ int TableHeaderComponent::getNumColumns (const bool onlyCountVisibleColumns) con
{
int num = 0;
for (int i = columns.size(); --i >= 0;)
if (columns.getUnchecked(i)->isVisible())
for (auto* c : columns)
if (c->isVisible())
++num;
return num;
@@ -113,26 +102,24 @@ void TableHeaderComponent::setColumnName (const int columnId, const String& newN
}
void TableHeaderComponent::addColumn (const String& columnName,
const int columnId,
const int width,
const int minimumWidth,
const int maximumWidth,
const int propertyFlags,
const int insertIndex)
{
// can't have a duplicate or null ID!
int columnId,
int width,
int minimumWidth,
int maximumWidth,
int propertyFlags,
int insertIndex)
{
// can't have a duplicate or zero ID!
jassert (columnId != 0 && getIndexOfColumnId (columnId, false) < 0);
jassert (width > 0);
ColumnInfo* const ci = new ColumnInfo();
auto ci = new ColumnInfo();
ci->name = columnName;
ci->id = columnId;
ci->width = width;
ci->lastDeliberateWidth = width;
ci->minimumWidth = minimumWidth;
ci->maximumWidth = maximumWidth;
if (ci->maximumWidth < 0)
ci->maximumWidth = std::numeric_limits<int>::max();
ci->maximumWidth = maximumWidth >= 0 ? maximumWidth : std::numeric_limits<int>::max();
jassert (ci->maximumWidth >= ci->minimumWidth);
ci->propertyFlags = propertyFlags;
@@ -142,7 +129,7 @@ void TableHeaderComponent::addColumn (const String& columnName,
void TableHeaderComponent::removeColumn (const int columnIdToRemove)
{
const int index = getIndexOfColumnId (columnIdToRemove, false);
auto index = getIndexOfColumnId (columnIdToRemove, false);
if (index >= 0)
{
@@ -163,7 +150,7 @@ void TableHeaderComponent::removeAllColumns()
void TableHeaderComponent::moveColumn (const int columnId, int newIndex)
{
const int currentIndex = getIndexOfColumnId (columnId, false);
auto currentIndex = getIndexOfColumnId (columnId, false);
newIndex = visibleIndexToTotalIndex (newIndex);
if (columns [currentIndex] != 0 && currentIndex != newIndex)
@@ -175,7 +162,7 @@ void TableHeaderComponent::moveColumn (const int columnId, int newIndex)
int TableHeaderComponent::getColumnWidth (const int columnId) const
{
if (const ColumnInfo* const ci = getInfoForId (columnId))
if (auto* ci = getInfoForId (columnId))
return ci->width;
return 0;
@@ -183,33 +170,34 @@ int TableHeaderComponent::getColumnWidth (const int columnId) const
void TableHeaderComponent::setColumnWidth (const int columnId, const int newWidth)
{
ColumnInfo* const ci = getInfoForId (columnId);
if (ci != nullptr && ci->width != newWidth)
if (auto* ci = getInfoForId (columnId))
{
const int numColumns = getNumColumns (true);
ci->lastDeliberateWidth = ci->width
= jlimit (ci->minimumWidth, ci->maximumWidth, newWidth);
if (stretchToFit)
if (ci->width != newWidth)
{
const int index = getIndexOfColumnId (columnId, true) + 1;
auto numColumns = getNumColumns (true);
ci->lastDeliberateWidth = ci->width
= jlimit (ci->minimumWidth, ci->maximumWidth, newWidth);
if (isPositiveAndBelow (index, numColumns))
if (stretchToFit)
{
const int x = getColumnPosition (index).getX();
auto index = getIndexOfColumnId (columnId, true) + 1;
if (lastDeliberateWidth == 0)
lastDeliberateWidth = getTotalWidth();
if (isPositiveAndBelow (index, numColumns))
{
auto x = getColumnPosition (index).getX();
if (lastDeliberateWidth == 0)
lastDeliberateWidth = getTotalWidth();
resizeColumnsToFit (visibleIndexToTotalIndex (index), lastDeliberateWidth - x);
resizeColumnsToFit (visibleIndexToTotalIndex (index), lastDeliberateWidth - x);
}
}
}
repaint();
columnsResized = true;
triggerAsyncUpdate();
repaint();
columnsResized = true;
triggerAsyncUpdate();
}
}
}
@@ -218,11 +206,11 @@ int TableHeaderComponent::getIndexOfColumnId (const int columnId, const bool onl
{
int n = 0;
for (int i = 0; i < columns.size(); ++i)
for (auto* c : columns)
{
if ((! onlyCountVisibleColumns) || columns.getUnchecked(i)->isVisible())
if ((! onlyCountVisibleColumns) || c->isVisible())
{
if (columns.getUnchecked(i)->id == columnId)
if (c->id == columnId)
return n;
++n;
@@ -237,7 +225,7 @@ int TableHeaderComponent::getColumnIdOfIndex (int index, const bool onlyCountVis
if (onlyCountVisibleColumns)
index = visibleIndexToTotalIndex (index);
if (const ColumnInfo* const ci = columns [index])
if (auto* ci = columns [index])
return ci->id;
return 0;
@@ -247,13 +235,13 @@ Rectangle<int> TableHeaderComponent::getColumnPosition (const int index) const
{
int x = 0, width = 0, n = 0;
for (int i = 0; i < columns.size(); ++i)
for (auto* c : columns)
{
x += width;
if (columns.getUnchecked(i)->isVisible())
if (c->isVisible())
{
width = columns.getUnchecked(i)->width;
width = c->width;
if (n++ == index)
break;
@@ -264,7 +252,7 @@ Rectangle<int> TableHeaderComponent::getColumnPosition (const int index) const
}
}
return Rectangle<int> (x, 0, width, getHeight());
return { x, 0, width, getHeight() };
}
int TableHeaderComponent::getColumnIdAtX (const int xToFind) const
@@ -273,10 +261,8 @@ int TableHeaderComponent::getColumnIdAtX (const int xToFind) const
{
int x = 0;
for (int i = 0; i < columns.size(); ++i)
for (auto* ci : columns)
{
const ColumnInfo* const ci = columns.getUnchecked(i);
if (ci->isVisible())
{
x += ci->width;
@@ -294,9 +280,9 @@ int TableHeaderComponent::getTotalWidth() const
{
int w = 0;
for (int i = columns.size(); --i >= 0;)
if (columns.getUnchecked(i)->isVisible())
w += columns.getUnchecked(i)->width;
for (auto* c : columns)
if (c->isVisible())
w += c->width;
return w;
}
@@ -326,27 +312,27 @@ void TableHeaderComponent::resizeAllColumnsToFit (int targetTotalWidth)
void TableHeaderComponent::resizeColumnsToFit (int firstColumnIndex, int targetTotalWidth)
{
targetTotalWidth = jmax (targetTotalWidth, 0);
StretchableObjectResizer sor;
for (int i = firstColumnIndex; i < columns.size(); ++i)
{
ColumnInfo* const ci = columns.getUnchecked(i);
auto* ci = columns.getUnchecked(i);
if (ci->isVisible())
sor.addItem (ci->lastDeliberateWidth, ci->minimumWidth, ci->maximumWidth);
}
sor.resizeToFit (targetTotalWidth);
int visIndex = 0;
for (int i = firstColumnIndex; i < columns.size(); ++i)
{
ColumnInfo* const ci = columns.getUnchecked(i);
auto* ci = columns.getUnchecked(i);
if (ci->isVisible())
{
const int newWidth = jlimit (ci->minimumWidth, ci->maximumWidth,
(int) std::floor (sor.getItemSize (visIndex++)));
auto newWidth = jlimit (ci->minimumWidth, ci->maximumWidth,
(int) std::floor (sor.getItemSize (visIndex++)));
if (newWidth != ci->width)
{
@@ -361,7 +347,7 @@ void TableHeaderComponent::resizeColumnsToFit (int firstColumnIndex, int targetT
void TableHeaderComponent::setColumnVisible (const int columnId, const bool shouldBeVisible)
{
if (ColumnInfo* const ci = getInfoForId (columnId))
if (auto* ci = getInfoForId (columnId))
{
if (shouldBeVisible != ci->isVisible())
{
@@ -378,8 +364,10 @@ void TableHeaderComponent::setColumnVisible (const int columnId, const bool shou
bool TableHeaderComponent::isColumnVisible (const int columnId) const
{
const ColumnInfo* const ci = getInfoForId (columnId);
return ci != nullptr && ci->isVisible();
if (auto* ci = getInfoForId (columnId))
return ci->isVisible();
return false;
}
//==============================================================================
@@ -387,10 +375,10 @@ void TableHeaderComponent::setSortColumnId (const int columnId, const bool sortF
{
if (getSortColumnId() != columnId || isSortedForwards() != sortForwards)
{
for (int i = columns.size(); --i >= 0;)
columns.getUnchecked(i)->propertyFlags &= ~(sortedForwards | sortedBackwards);
for (auto* c : columns)
c->propertyFlags &= ~(sortedForwards | sortedBackwards);
if (ColumnInfo* const ci = getInfoForId (columnId))
if (auto* ci = getInfoForId (columnId))
ci->propertyFlags |= (sortForwards ? sortedForwards : sortedBackwards);
reSortTable();
@@ -399,18 +387,18 @@ void TableHeaderComponent::setSortColumnId (const int columnId, const bool sortF
int TableHeaderComponent::getSortColumnId() const
{
for (int i = columns.size(); --i >= 0;)
if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
return columns.getUnchecked(i)->id;
for (auto* c : columns)
if ((c->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
return c->id;
return 0;
}
bool TableHeaderComponent::isSortedForwards() const
{
for (int i = columns.size(); --i >= 0;)
if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
return (columns.getUnchecked(i)->propertyFlags & sortedForwards) != 0;
for (auto* c : columns)
if ((c->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
return (c->propertyFlags & sortedForwards) != 0;
return true;
}
@@ -432,17 +420,15 @@ String TableHeaderComponent::toString() const
doc.setAttribute ("sortedCol", getSortColumnId());
doc.setAttribute ("sortForwards", isSortedForwards());
for (int i = 0; i < columns.size(); ++i)
for (auto* ci : columns)
{
const ColumnInfo* const ci = columns.getUnchecked (i);
XmlElement* const e = doc.createNewChildElement ("COLUMN");
auto* e = doc.createNewChildElement ("COLUMN");
e->setAttribute ("id", ci->id);
e->setAttribute ("visible", ci->isVisible());
e->setAttribute ("width", ci->width);
}
return doc.createDocument ("", true, false);
return doc.createDocument ({}, true, false);
}
void TableHeaderComponent::restoreFromString (const String& storedVersion)
@@ -454,9 +440,9 @@ void TableHeaderComponent::restoreFromString (const String& storedVersion)
{
forEachXmlChildElement (*storedXml, col)
{
const int tabId = col->getIntAttribute ("id");
auto tabId = col->getIntAttribute ("id");
if (ColumnInfo* const ci = getInfoForId (tabId))
if (auto* ci = getInfoForId (tabId))
{
columns.move (columns.indexOf (ci), index);
ci->width = col->getIntAttribute ("width");
@@ -488,22 +474,18 @@ void TableHeaderComponent::removeListener (Listener* const listenerToRemove)
//==============================================================================
void TableHeaderComponent::columnClicked (int columnId, const ModifierKeys& mods)
{
if (const ColumnInfo* const ci = getInfoForId (columnId))
if (auto* ci = getInfoForId (columnId))
if ((ci->propertyFlags & sortable) != 0 && ! mods.isPopupMenu())
setSortColumnId (columnId, (ci->propertyFlags & sortedForwards) == 0);
}
void TableHeaderComponent::addMenuItems (PopupMenu& menu, const int /*columnIdClicked*/)
{
for (int i = 0; i < columns.size(); ++i)
{
const ColumnInfo* const ci = columns.getUnchecked(i);
for (auto* ci : columns)
if ((ci->propertyFlags & appearsOnColumnMenu) != 0)
menu.addItem (ci->id, ci->name,
(ci->propertyFlags & (sortedForwards | sortedBackwards)) == 0,
isColumnVisible (ci->id));
}
}
void TableHeaderComponent::reactToMenuItem (const int menuReturnId, const int /*columnIdClicked*/)
@@ -521,10 +503,9 @@ void TableHeaderComponent::paint (Graphics& g)
const Rectangle<int> clip (g.getClipBounds());
int x = 0;
for (int i = 0; i < columns.size(); ++i)
{
const ColumnInfo* const ci = columns.getUnchecked(i);
for (auto* ci : columns)
{
if (ci->isVisible())
{
if (x + ci->width > clip.getX()
@@ -551,24 +532,9 @@ void TableHeaderComponent::paint (Graphics& g)
}
}
void TableHeaderComponent::resized()
{
}
void TableHeaderComponent::mouseMove (const MouseEvent& e)
{
updateColumnUnderMouse (e);
}
void TableHeaderComponent::mouseEnter (const MouseEvent& e)
{
updateColumnUnderMouse (e);
}
void TableHeaderComponent::mouseExit (const MouseEvent&)
{
setColumnUnderMouse (0);
}
void TableHeaderComponent::mouseMove (const MouseEvent& e) { updateColumnUnderMouse (e); }
void TableHeaderComponent::mouseEnter (const MouseEvent& e) { updateColumnUnderMouse (e); }
void TableHeaderComponent::mouseExit (const MouseEvent&) { setColumnUnderMouse (0); }
void TableHeaderComponent::mouseDown (const MouseEvent& e)
{
@@ -601,9 +567,10 @@ void TableHeaderComponent::mouseDrag (const MouseEvent& e)
if (columnIdBeingResized != 0)
{
const ColumnInfo* const ci = getInfoForId (columnIdBeingResized);
jassert (ci != nullptr);
initialColumnWidth = ci->width;
if (auto* ci = getInfoForId (columnIdBeingResized))
initialColumnWidth = ci->width;
else
jassertfalse;
}
else
{
@@ -613,20 +580,21 @@ void TableHeaderComponent::mouseDrag (const MouseEvent& e)
if (columnIdBeingResized != 0)
{
if (const ColumnInfo* const ci = getInfoForId (columnIdBeingResized))
if (auto* ci = getInfoForId (columnIdBeingResized))
{
int w = jlimit (ci->minimumWidth, ci->maximumWidth,
initialColumnWidth + e.getDistanceFromDragStartX());
auto w = jlimit (ci->minimumWidth, ci->maximumWidth,
initialColumnWidth + e.getDistanceFromDragStartX());
if (stretchToFit)
{
// prevent us dragging a column too far right if we're in stretch-to-fit mode
int minWidthOnRight = 0;
for (int i = getIndexOfColumnId (columnIdBeingResized, false) + 1; i < columns.size(); ++i)
if (columns.getUnchecked (i)->isVisible())
minWidthOnRight += columns.getUnchecked (i)->minimumWidth;
const Rectangle<int> currentPos (getColumnPosition (getIndexOfColumnId (columnIdBeingResized, true)));
auto currentPos = getColumnPosition (getIndexOfColumnId (columnIdBeingResized, true));
w = jmax (ci->minimumWidth, jmin (w, lastDeliberateWidth - minWidthOnRight - currentPos.getX()));
}
@@ -656,15 +624,15 @@ void TableHeaderComponent::mouseDrag (const MouseEvent& e)
{
// if the previous column isn't draggable, we can't move our column
// past it, because that'd change the undraggable column's position..
const ColumnInfo* const previous = columns.getUnchecked (newIndex - 1);
auto* previous = columns.getUnchecked (newIndex - 1);
if ((previous->propertyFlags & draggable) != 0)
{
const int leftOfPrevious = getColumnPosition (newIndex - 1).getX();
const int rightOfCurrent = getColumnPosition (newIndex).getRight();
auto leftOfPrevious = getColumnPosition (newIndex - 1).getX();
auto rightOfCurrent = getColumnPosition (newIndex).getRight();
if (abs (dragOverlayComp->getX() - leftOfPrevious)
< abs (dragOverlayComp->getRight() - rightOfCurrent))
if (std::abs (dragOverlayComp->getX() - leftOfPrevious)
< std::abs (dragOverlayComp->getRight() - rightOfCurrent))
{
--newIndex;
}
@@ -675,15 +643,15 @@ void TableHeaderComponent::mouseDrag (const MouseEvent& e)
{
// if the next column isn't draggable, we can't move our column
// past it, because that'd change the undraggable column's position..
const ColumnInfo* const nextCol = columns.getUnchecked (newIndex + 1);
auto* nextCol = columns.getUnchecked (newIndex + 1);
if ((nextCol->propertyFlags & draggable) != 0)
{
const int leftOfCurrent = getColumnPosition (newIndex).getX();
const int rightOfNext = getColumnPosition (newIndex + 1).getRight();
auto leftOfCurrent = getColumnPosition (newIndex).getX();
auto rightOfNext = getColumnPosition (newIndex + 1).getRight();
if (abs (dragOverlayComp->getX() - leftOfCurrent)
> abs (dragOverlayComp->getRight() - rightOfNext))
if (std::abs (dragOverlayComp->getX() - leftOfCurrent)
> std::abs (dragOverlayComp->getRight() - rightOfNext))
{
++newIndex;
}
@@ -710,7 +678,7 @@ void TableHeaderComponent::beginDrag (const MouseEvent& e)
{
columnIdBeingDragged = getColumnIdAtX (e.getMouseDownX());
const ColumnInfo* const ci = getInfoForId (columnIdBeingDragged);
auto* ci = getInfoForId (columnIdBeingDragged);
if (ci == nullptr || (ci->propertyFlags & draggable) == 0)
{
@@ -720,9 +688,8 @@ void TableHeaderComponent::beginDrag (const MouseEvent& e)
{
draggingColumnOriginalIndex = getIndexOfColumnId (columnIdBeingDragged, true);
const Rectangle<int> columnRect (getColumnPosition (draggingColumnOriginalIndex));
const int temp = columnIdBeingDragged;
auto columnRect = getColumnPosition (draggingColumnOriginalIndex);
auto temp = columnIdBeingDragged;
columnIdBeingDragged = 0;
addAndMakeVisible (dragOverlayComp = new DragOverlayComp (createComponentSnapshot (columnRect, false)));
@@ -872,13 +839,11 @@ int TableHeaderComponent::getResizeDraggerAt (const int mouseX) const
const int draggableDistance = 3;
int x = 0;
for (int i = 0; i < columns.size(); ++i)
for (auto* ci : columns)
{
const ColumnInfo* const ci = columns.getUnchecked(i);
if (ci->isVisible())
{
if (abs (mouseX - (x + ci->width)) <= draggableDistance
if (std::abs (mouseX - (x + ci->width)) <= draggableDistance
&& (ci->propertyFlags & resizable) != 0)
return ci->id;


+ 4
- 5
modules/juce_gui_basics/widgets/juce_TableHeaderComponent.h View File

@@ -402,8 +402,6 @@ public:
/** @internal */
void paint (Graphics&) override;
/** @internal */
void resized() override;
/** @internal */
void mouseMove (const MouseEvent&) override;
/** @internal */
void mouseEnter (const MouseEvent&) override;
@@ -436,9 +434,10 @@ private:
ScopedPointer<Component> dragOverlayComp;
class DragOverlayComp;
bool columnsChanged, columnsResized, sortChanged, menuActive, stretchToFit;
int columnIdBeingResized, columnIdBeingDragged, initialColumnWidth;
int columnIdUnderMouse, draggingColumnOffset, draggingColumnOriginalIndex, lastDeliberateWidth;
bool columnsChanged = false, columnsResized = false, sortChanged = false;
bool menuActive = true, stretchToFit = false;
int columnIdBeingResized = 0, columnIdBeingDragged = 0, initialColumnWidth = 0;
int columnIdUnderMouse = 0, draggingColumnOffset = 0, draggingColumnOriginalIndex = 0, lastDeliberateWidth = 0;
ColumnInfo* getInfoForId (int columnId) const;
int visibleIndexToTotalIndex (int visibleIndex) const;


+ 42
- 47
modules/juce_gui_basics/widgets/juce_TableListBox.cpp View File

@@ -28,25 +28,23 @@ class TableListBox::RowComp : public Component,
public TooltipClient
{
public:
RowComp (TableListBox& tlb) noexcept : owner (tlb), row (-1), isSelected (false)
{
}
RowComp (TableListBox& tlb) noexcept : owner (tlb) {}
void paint (Graphics& g) override
{
if (TableListBoxModel* const tableModel = owner.getModel())
if (auto* tableModel = owner.getModel())
{
tableModel->paintRowBackground (g, row, getWidth(), getHeight(), isSelected);
const TableHeaderComponent& headerComp = owner.getHeader();
const int numColumns = headerComp.getNumColumns (true);
const Rectangle<int> clipBounds (g.getClipBounds());
auto& headerComp = owner.getHeader();
auto numColumns = headerComp.getNumColumns (true);
auto clipBounds = g.getClipBounds();
for (int i = 0; i < numColumns; ++i)
{
if (columnComponents[i] == nullptr)
{
const Rectangle<int> columnRect (headerComp.getColumnPosition(i).withHeight (getHeight()));
auto columnRect = headerComp.getColumnPosition(i).withHeight (getHeight());
if (columnRect.getX() >= clipBounds.getRight())
break;
@@ -67,7 +65,7 @@ public:
}
}
void update (const int newRow, const bool isNowSelected)
void update (int newRow, bool isNowSelected)
{
jassert (newRow >= 0);
@@ -78,19 +76,19 @@ public:
repaint();
}
TableListBoxModel* const tableModel = owner.getModel();
auto* tableModel = owner.getModel();
if (tableModel != nullptr && row < owner.getNumRows())
{
const Identifier columnProperty ("_tableColumnId");
const int numColumns = owner.getHeader().getNumColumns (true);
auto numColumns = owner.getHeader().getNumColumns (true);
for (int i = 0; i < numColumns; ++i)
{
const int columnId = owner.getHeader().getColumnIdOfIndex (i, true);
Component* comp = columnComponents[i];
auto columnId = owner.getHeader().getColumnIdOfIndex (i, true);
auto* comp = columnComponents[i];
if (comp != nullptr && columnId != (int) comp->getProperties() [columnProperty])
if (comp != nullptr && columnId != static_cast<int> (comp->getProperties() [columnProperty]))
{
columnComponents.set (i, nullptr);
comp = nullptr;
@@ -122,9 +120,9 @@ public:
resizeCustomComp (i);
}
void resizeCustomComp (const int index)
void resizeCustomComp (int index)
{
if (Component* const c = columnComponents.getUnchecked (index))
if (auto* c = columnComponents.getUnchecked (index))
c->setBounds (owner.getHeader().getColumnPosition (index)
.withY (0).withHeight (getHeight()));
}
@@ -140,10 +138,10 @@ public:
{
owner.selectRowsBasedOnModifierKeys (row, e.mods, false);
const int columnId = owner.getHeader().getColumnIdAtX (e.x);
auto columnId = owner.getHeader().getColumnIdAtX (e.x);
if (columnId != 0)
if (TableListBoxModel* m = owner.getModel())
if (auto* m = owner.getModel())
m->cellClicked (row, columnId, e);
}
else
@@ -169,7 +167,7 @@ public:
if (rowsToDrag.size() > 0)
{
const var dragDescription (owner.getModel()->getDragSourceDescription (rowsToDrag));
auto dragDescription = owner.getModel()->getDragSourceDescription (rowsToDrag);
if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
{
@@ -186,7 +184,7 @@ public:
{
owner.selectRowsBasedOnModifierKeys (row, e.mods, true);
const int columnId = owner.getHeader().getColumnIdAtX (e.x);
auto columnId = owner.getHeader().getColumnIdAtX (e.x);
if (columnId != 0)
if (TableListBoxModel* m = owner.getModel())
@@ -196,16 +194,16 @@ public:
void mouseDoubleClick (const MouseEvent& e) override
{
const int columnId = owner.getHeader().getColumnIdAtX (e.x);
auto columnId = owner.getHeader().getColumnIdAtX (e.x);
if (columnId != 0)
if (TableListBoxModel* m = owner.getModel())
if (auto* m = owner.getModel())
m->cellDoubleClicked (row, columnId, e);
}
String getTooltip() override
{
const int columnId = owner.getHeader().getColumnIdAtX (getMouseXYRelative().getX());
auto columnId = owner.getHeader().getColumnIdAtX (getMouseXYRelative().getX());
if (columnId != 0)
if (auto* m = owner.getModel())
@@ -214,7 +212,7 @@ public:
return {};
}
Component* findChildComponentForColumn (const int columnId) const
Component* findChildComponentForColumn (int columnId) const
{
return columnComponents [owner.getHeader().getIndexOfColumnId (columnId, true)];
}
@@ -222,8 +220,8 @@ public:
private:
TableListBox& owner;
OwnedArray<Component> columnComponents;
int row;
bool isSelected, isDragging, selectRowOnMouseUp;
int row = -1;
bool isSelected = false, isDragging = false, selectRowOnMouseUp = false;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RowComp)
};
@@ -267,10 +265,7 @@ private:
//==============================================================================
TableListBox::TableListBox (const String& name, TableListBoxModel* const m)
: ListBox (name, nullptr),
header (nullptr),
model (m),
autoSizeOptionsShown (true)
: ListBox (name, nullptr), model (m)
{
ListBox::model = this;
@@ -281,7 +276,7 @@ TableListBox::~TableListBox()
{
}
void TableListBox::setModel (TableListBoxModel* const newModel)
void TableListBox::setModel (TableListBoxModel* newModel)
{
if (model != newModel)
{
@@ -295,6 +290,7 @@ void TableListBox::setHeader (TableHeaderComponent* newHeader)
jassert (newHeader != nullptr); // you need to supply a real header for a table!
Rectangle<int> newBounds (100, 28);
if (header != nullptr)
newBounds = header->getBounds();
@@ -311,15 +307,15 @@ int TableListBox::getHeaderHeight() const noexcept
return header->getHeight();
}
void TableListBox::setHeaderHeight (const int newHeight)
void TableListBox::setHeaderHeight (int newHeight)
{
header->setSize (header->getWidth(), newHeight);
resized();
}
void TableListBox::autoSizeColumn (const int columnId)
void TableListBox::autoSizeColumn (int columnId)
{
const int width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
auto width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
if (width > 0)
header->setColumnWidth (columnId, width);
@@ -331,15 +327,14 @@ void TableListBox::autoSizeAllColumns()
autoSizeColumn (header->getColumnIdOfIndex (i, true));
}
void TableListBox::setAutoSizeMenuOptionShown (const bool shouldBeShown) noexcept
void TableListBox::setAutoSizeMenuOptionShown (bool shouldBeShown) noexcept
{
autoSizeOptionsShown = shouldBeShown;
}
Rectangle<int> TableListBox::getCellPosition (const int columnId, const int rowNumber,
const bool relativeToComponentTopLeft) const
Rectangle<int> TableListBox::getCellPosition (int columnId, int rowNumber, bool relativeToComponentTopLeft) const
{
Rectangle<int> headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
auto headerCell = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
if (relativeToComponentTopLeft)
headerCell.translate (header->getX(), 0);
@@ -351,20 +346,20 @@ Rectangle<int> TableListBox::getCellPosition (const int columnId, const int rowN
Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
{
if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
if (auto* rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
return rowComp->findChildComponentForColumn (columnId);
return nullptr;
}
void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
void TableListBox::scrollToEnsureColumnIsOnscreen (int columnId)
{
if (ScrollBar* const scrollbar = getHorizontalScrollBar())
if (auto* scrollbar = getHorizontalScrollBar())
{
const Rectangle<int> pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
auto pos = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
double x = scrollbar->getCurrentRangeStart();
const double w = scrollbar->getCurrentRangeSize();
auto x = scrollbar->getCurrentRangeStart();
auto w = scrollbar->getCurrentRangeSize();
if (pos.getX() < x)
x = pos.getX();
@@ -461,10 +456,10 @@ void TableListBox::resized()
void TableListBox::updateColumnComponents() const
{
const int firstRow = getRowContainingPosition (0, 0);
auto firstRow = getRowContainingPosition (0, 0);
for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (i)))
if (auto* rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (i)))
rowComp->resized();
}
@@ -472,7 +467,7 @@ void TableListBox::updateColumnComponents() const
void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
void TableListBoxModel::backgroundClicked (const MouseEvent&) {}
void TableListBoxModel::sortOrderChanged (int, const bool) {}
void TableListBoxModel::sortOrderChanged (int, bool) {}
int TableListBoxModel::getColumnAutoSizeWidth (int) { return 0; }
void TableListBoxModel::selectedRowsChanged (int) {}
void TableListBoxModel::deleteKeyPressed (int) {}


+ 3
- 3
modules/juce_gui_basics/widgets/juce_TableListBox.h View File

@@ -336,10 +336,10 @@ private:
class Header;
class RowComp;
TableHeaderComponent* header;
TableHeaderComponent* header = nullptr;
TableListBoxModel* model;
int columnIdNowBeingDragged;
bool autoSizeOptionsShown;
int columnIdNowBeingDragged = 0;
bool autoSizeOptionsShown = true;
void updateColumnComponents() const;


Loading…
Cancel
Save