diff --git a/include/nana/gui/widgets/listbox.hpp b/include/nana/gui/widgets/listbox.hpp index ac3fc9f7..717d6d91 100644 --- a/include/nana/gui/widgets/listbox.hpp +++ b/include/nana/gui/widgets/listbox.hpp @@ -1593,6 +1593,16 @@ the nana::detail::basic_window member pointer scheme * @return index_pairs containing all visible items. */ index_pairs visibles() const; + + /// Sets a predicate that indicates whether to deselect items when mouse_up is triggered. + /** + * The predicate is called before the listbox attempts to deselect the selected items in the mouse_up event. Other situations, + * the predicates isn't called, for example, releasing mouse button after user performed a box selection, because listbox doesn't deselect the items during this operation. + * @param predicate Decides to deselect the items. + * The paramater of predicate indicates the mouse button which is releasing. + * It returns true to deselect the selected items. It returns false to cancel to deselect the selected items. + */ + void set_deselect(std::function predicate); private: drawerbase::listbox::essence & _m_ess() const; nana::any* _m_anyobj(size_type cat, size_type index, bool allocate_if_empty) const override; diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index f387140f..9d372746 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -2011,6 +2011,8 @@ namespace nana std::function ctg_icon_renderer; ///< Renderer for the category icon + std::function pred_msup_deselect; + struct operation_rep { operation_states state{operation_states::none}; @@ -4470,7 +4472,10 @@ namespace nana if (operation_states::msup_deselect == essence_->operation.state) { essence_->operation.state = operation_states::none; - need_refresh |= essence_->lister.select_for_all(false, essence_->operation.item); + + //Don't deselect if the predicate returns false + if(!(essence_->pred_msup_deselect && !essence_->pred_msup_deselect(arg.button))) + need_refresh |= essence_->lister.select_for_all(false, essence_->operation.item); } if (need_refresh) @@ -6102,6 +6107,11 @@ namespace nana return indexes; } + void listbox::set_deselect(std::function predicate) + { + _m_ess().pred_msup_deselect = std::move(predicate); + } + drawerbase::listbox::essence & listbox::_m_ess() const { return get_drawer_trigger().ess();