As I’m learning this framework, I’m questioning a lot of things.
At the moment, I’m struggling with what seems a trivial task.
In this example, I have a ‘ChannelContainer’ component, which has 8 children, called ‘Channel. Each child, has a ’ mouseDown (const MouseEvent& e)’ method which simply updates a selected Boolean variable.
The intended selection style is a radio button.
void Channel::mouseDown(const MouseEvent& e)
{
isSelected = true;
}
What I want is for the parent to register which ‘child’ has been selected and change its background accordingly.
My following example works… however I’m not sure if this is the correct way about it.
This is my ‘ChannelContainer’ constructor, where I’m initializing each channel:
ChannelContainer::ChannelContainer()
{
for (int i = 0; i < sizeof(channel) / sizeof(Channel); i++)
{
addAndMakeVisible(channel[i]);
channel[i].addMouseListener(this, false);
}
}
Then in the ‘mouseDown’ callback, I’m doing the following:
void ChannelContainer::mouseDown (const MouseEvent& e)
{
if (!(e.eventComponent == this))
{
for (int i = 0; i < sizeof(channel) / sizeof(Channel); i++)
{
if (channel[i].isSelected) {
channel[i].isSelected = false;
channel[i].setColor(Colour(90,90,90));
} else {
channel[i].setColor(Colour(67,67,67));
}
channel[i].repaint();
}
}
}
I’m restricting the selection to only each channel, and not the container itself by:
if (!(e.eventComponent == this))
Any suggestions / critiques welcome.
Thanks