Component::focusLost() in the parent Component


#1

Hello. I have two components, A and B, with hundreds of juce widgets. When A or B gains/losts the keyboard focus it gets repainted.

If A is focused, and one of his children components gains the focus A::focusLost() is called (and I’d paint it using Component::hasKeyboardFocus(true)), but then if I swich to B, A::focusLost() won’t be called (just A::childComp::focusLost()) , and A won’t be repainted.

Is it possible to call A::focusLost() when none of A’s children neither A have the focus?


#2

the focus behaviour there looks correct. However, if you need to repaint A when a child loses the focus, can you have the child component signal repaint to the parent.

ie override Component::focusGained and focusLost and make them do that you want.


#3

That means I might redefine all juce widgets overriding their focusGained() and focusLost(), and add a pointer to the parent (or grand-parent) I want to repaint. Too messy.

Must be something simpler and less alike spaghetti code, isn’t it?


#4

See the FocusChangeListener class?


#5

Yes, that did the trick, however I was expecting something like to a function in Component that could deal with that.

Anyway I post the code here, maybe someone can need it:

[code]class A: public Component,
public FocusChangeListener
{
public:
A();
~A();
virtual void globalFocusChanged(Component* focusedComponent);

private:
bool m_bIHaveTheFocus;
JUCE_LEAK_DETECTOR(A)
};[/code]

[code]A::A():
m_bIHaveTheFocus(false)
{
Desktop::getInstance().addFocusChangeListener(this);
}

A::~A()
{
Desktop::getInstance().removeFocusChangeListener(this);
}

void A::globalFocusChanged(Component* focusedComponent)
{
Component* comp = focusedComponent;

bool IhaveFocus = false;

while (comp != 0)
{
if (comp == this)
{
IhaveFocus = true;
comp = 0;
}
else
{
comp = comp->getParentComponent();
}
}

if (IhaveFocus != m_bIHaveTheFocus)
{
m_bIHaveTheFocus = IhaveFocus;
repaint();
}
}

[/code]