I’ve got several ListBoxes in an app. I have it setup that double-clicking an item in a ListBox will add it to another ListBox. The way I did this was simply using the mouseDoubleClick event in the main component. It checks if myListBox->isParentOf( MouseEvent.eventComponent ). It has to do this because the eventComponent isn’t pointing to myListBox – it’s pointing to the item which is the child of the child of the child of the child of the actual ListBox object. None of these items actually have names, so I can’t even use getName() to see what was clicked on. If I wanted to give the items names, that would be a big hassle because they are dynamically created and destroyed by JUCE.
Anyway, that was working fine, except if an item is selected, double-clicking on the ListBox’s scrollbar would also add the item. My code was simply looking for a double-click in any of the children of the ListBox then adding the selected items. I don’t want to have to deselect everything in the ListBox when the scrollbar is clicked on, so I thought, “OK, I have to make sure you’re actually clicking on the item in the double-click event.”
This is pretty complicated since the ListBox item objects have no names, and using typeid( MouseEvent.eventComponent ) just returns a generic Component.
What I ended up doing was making a constant LISTBOX_ITEM_HEIGHT, then I just added this to the beginning of the mouseDoubleClick event:
if( e.eventComponent->getHeight() != LISTBOX_ITEM_HEIGHT ) return;
So that way it only processes the double-click event if you’re actually clicking on a ListBox item. This seems like a pretty bogus solution to me, though, and I can see in the future something changing in my application that caused LISTBOX_ITEM_HEIGHT to be the same value as the height of the up/down scroll buttons on the scrollbar.
I really think I must be missing something obvious that exists in JUCE to make this much simpler otherwise ListBoxes just seem kinda ridiculous. I’m sure there’s a better way to determine what I’m clicking on.