paintBackgroundAndSelection issue


Hi Jules,


I was wondering what would be the correct way to draw a rect on the bounds of a TreeViewItem (only the rect outline)

with setDrawsInLeftMargin to true.


paintBackgroundAndSelection is called without the x and y, and using the graphics local bounds could be wrong if for example

the treeview item is redrawn partially because of some tooltip.

You won't notice the issue if only using fillAll for a solid colored background, but trying to only use a drawRect will exhibits the issue.


So far I've been using

 g.drawRect(g.getClipBounds().getX(), 0, width - g.getClipBounds().getX(), height);

but this won't work with partial redraws.


Any idea ?





I think paintBackgroundAndSelection must be your function, not a JUCE one?

But don't you just mean drawRect (0, 0, width, height) ?


The real juce function name is juce::TreeViewItem::paintItem (Graphics& g, int width, int height). My bad.


The issue here is that if you want to draw the whole line like fillAll will do when setDrawsInLeftMargin is true, there is no easy way

I finaly found a way by redefining

int GetIndentX() const noexcept
  int x = getOwnerView()->isRootItemVisible() ? 1 : 0;
  if (!getOwnerView()->areOpenCloseButtonsVisible())
  for (juce::TreeViewItem* p = getParentItem(); p != nullptr; p = p->getParentItem())
  return x * getOwnerView()->getIndentSize();

currently private and implement paintItem like that

void paintItem(Graphics &g, int width, int height)
  g.drawRect(-GetIndentX(), 0, width + GetIndentX(), height);


Ah, I see what you mean now. Hmm.. Yeah, can't think of a better way than your workaround - I could add more parameters to the paintItem call, but that'd break a lot of code out there!


Maybe giving access to getIndentX() would lead to smaller code and add some doc regarding using this function to achieve this results could be of some help.

I had a hard time founding a way to make this work hence my try with the clipBounds :)




Yep, that'd also work.