Listbox deselected problems?


#1

at the moment i’m having a hard time getting my program to know when nothing is selected. even if i call deselectAllRows(), a call to getSelectedRow() returns 0 instead of the -1 stated in the docs. am i doing something wrong here or is there a bug?

EDIT: well for now instead of testing for -1 for the selected row index i’m just testing against the number of selected rows.


#2

This is definitely a bug. The documentation states that ListBox::getSelectedRow() will return the row number or -1 if the index was out of range or if there aren’t any rows selected but a quick look at the code shows us that the -1 will never be returned:

int ListBox::getSelectedRow (const int index) const { return selected [index]; }

The [] operator returns 0 for out of range values, so one way to make this work as advertised is to tweak the method in ListBox (or do it in a subclass if you don’t want to mess with JUCE code) like this:

int ListBox::getSelectedRow (const int index) const { return (index >= 0 && index < selected.size()) ? selected [index] : -1; }


#3

Hmm - it does say in the header that it should return -1, so I’ll change that, though checking the number of rows is the right way to see if anything is selected.


#4

The problem is evident when you call ListBox::deselectRow() and it clears your selection:

[code]void ListBox::deselectRow (const int row)
{
if (selected.contains (row))
{
selected.removeRange (row, 1);

    if (row == lastRowSelected)
        lastRowSelected = getSelectedRow();

    viewport->updateContents();
    selectedRowsChanged (lastRowSelected);
}

}[/code]

The call to getSelectedRow() sets lastRowSelected to 0, which isn’t right, because row zero isn’t selected – nothing is! This also messes up the call to selectedRowsChanged(lastRowSelected). Again, according to the docs, lastRowSelected should be -1 if nothing is selected, but right now it comes in incorrectly as 0. (Actually, the docs say it may be -1, so you’re off the hook here! :stuck_out_tongue: )


#5

Ah - yes, I spotted that one too, when I was looking at getSelectedRows() just now. All sorted for the next release.


#6

Super.

Thanks!


#7