/************************************************************************* Set whether the list should allow multiple selections or just a single selection *************************************************************************/ void Listbox::setMultiselectEnabled(bool setting) { // only react if the setting is changed if (d_multiselect != setting) { d_multiselect = setting; // if we change to single-select, deselect all except the first selected item. WindowEventArgs args(this); if ((!d_multiselect) && (getSelectedCount() > 1)) { ListboxItem* itm = getFirstSelectedItem(); while ((itm = getNextSelected(itm))) { itm->setSelected(false); } onSelectionChanged(args); } onMultiselectModeChanged(args); } }
/************************************************************************* Handler for when mouse button is pressed *************************************************************************/ void Listbox::onMouseButtonDown(MouseEventArgs& e) { // base class processing Window::onMouseButtonDown(e); if (e.button == LeftButton) { bool modified = false; // clear old selections if no control key is pressed or if multi-select is off if (!(e.sysKeys & Control) || !d_multiselect) { modified = clearAllSelections_impl(); } Point localPos(screenToWindow(e.position)); if (getMetricsMode() == Relative) { localPos = relativeToAbsolute(localPos); } ListboxItem* item = getItemAtPoint(localPos); if (item != NULL) { modified = true; // select range or item, depending upon keys and last selected item if (((e.sysKeys & Shift) && (d_lastSelected != NULL)) && d_multiselect) { selectRange(getItemIndex(item), getItemIndex(d_lastSelected)); } else { item->setSelected(item->isSelected() ^ true); } // update last selected item d_lastSelected = item->isSelected() ? item : NULL; } // fire event if needed if (modified) { WindowEventArgs args(this); onSelectionChanged(args); } e.handled = true; } }