diff --git a/include/nana/gui/widgets/listbox.hpp b/include/nana/gui/widgets/listbox.hpp index d334e8fd..6c1e8b83 100644 --- a/include/nana/gui/widgets/listbox.hpp +++ b/include/nana/gui/widgets/listbox.hpp @@ -8,7 +8,13 @@ * http://www.boost.org/LICENSE_1_0.txt) * * @file: nana/gui/widgets/listbox.hpp - * + * @contributors: + * Hiroshi Seki + * Ariel Vina-Rodriguez + * leobackes(pr#86,pr#97) + * Benjamin Navarro(pr#81) + * besh81(pr#130) + * dankan1890(pr#158) */ #ifndef NANA_GUI_WIDGETS_LISTBOX_HPP @@ -87,7 +93,7 @@ namespace nana struct intern { public: - virtual ~intern() = default; + virtual ~intern() noexcept = default; }; template @@ -96,7 +102,7 @@ namespace nana { const T * ptr; - real_pointer(const T* p) + real_pointer(const T* p) noexcept : ptr(p) {} }; @@ -113,13 +119,13 @@ namespace nana { } - ~const_virtual_pointer() + ~const_virtual_pointer() noexcept { delete intern_; } template - const typename std::remove_const::type *get() const + const typename std::remove_const::type *get() const noexcept { using value_type = typename std::remove_const::type; auto target = dynamic_cast*>(intern_); @@ -136,8 +142,8 @@ namespace nana ::nana::color bgcolor; ::nana::color fgcolor; - format() = default; - format(const ::nana::color& bgcolor, const ::nana::color& fgcolor); + format() noexcept = default; + format(const ::nana::color& bgcolor, const ::nana::color& fgcolor) noexcept; }; using format_ptr = ::std::unique_ptr; @@ -145,15 +151,15 @@ namespace nana ::std::string text; format_ptr custom_format; - cell() = default; + cell() noexcept = default; cell(const cell&); - cell(cell&&); - cell(::std::string); + cell(cell&&) noexcept; + cell(::std::string) noexcept; cell(::std::string, const format&); cell(::std::string, const ::nana::color& bgcolor, const ::nana::color& fgcolor); cell& operator=(const cell&); - cell& operator=(cell&&); + cell& operator=(cell&&) noexcept; }; class container_interface @@ -162,7 +168,7 @@ namespace nana public: virtual ~container_interface() = default; - virtual void clear() = 0; + virtual void clear() noexcept = 0; virtual void erase(std::size_t pos) = 0; virtual std::size_t size() const = 0; @@ -214,7 +220,7 @@ namespace nana translator_({ vtrans, ctrans }) {} private: - void clear() override + void clear() noexcept override { container_.clear(); } @@ -457,8 +463,8 @@ namespace nana virtual void lock() = 0; virtual void unlock() = 0; - virtual container_interface* container() = 0; - virtual const container_interface* container() const = 0; + virtual container_interface* container() noexcept = 0; + virtual const container_interface* container() const noexcept = 0; }; class model_guard @@ -563,12 +569,12 @@ namespace nana mutex_.unlock(); } - container_interface* container() override + container_interface* container() noexcept override { return &container_; } - const container_interface* container() const override + const container_interface* container() const noexcept override { return &container_; } @@ -605,12 +611,12 @@ namespace nana mutex_.unlock(); } - container_interface* container() override + container_interface* container() noexcept override { return container_ptr_.get(); } - const container_interface* container() const override + const container_interface* container() const noexcept override { return container_ptr_.get(); } @@ -625,47 +631,48 @@ namespace nana /// usefull for both absolute and display (sorted) positions struct index_pair { + constexpr static size_type npos = ::nana::npos; + size_type cat; //The pos of category size_type item; //the pos of item in a category. - explicit index_pair(size_type cat_pos = 0, size_type item_pos = 0) - : cat(cat_pos), - item(item_pos) + explicit index_pair(size_type cat_pos = 0, size_type item_pos = 0) noexcept + : cat(cat_pos), item(item_pos) {} - bool empty() const + bool empty() const noexcept { return (npos == cat); } - void set_both(size_type n) + void set_both(size_type n) noexcept { cat = item = n; } - bool is_category() const + bool is_category() const noexcept { return (npos != cat && npos == item); } - bool is_item() const - { - return (npos != cat && npos != item); - } - - bool operator==(const index_pair& r) const + bool operator==(const index_pair& r) const noexcept { return (r.cat == cat && r.item == item); } - bool operator!=(const index_pair& r) const + bool operator!=(const index_pair& r) const noexcept { return !this->operator==(r); } - bool operator>(const index_pair& r) const + bool operator<(const index_pair& r) const noexcept { - return (cat > r.cat) || (cat == r.cat && item > r.item); + return (cat < r.cat) || ((cat == r.cat) && (r.item != npos) && ((item == npos) || (item < r.item))); + } + + bool operator>(const index_pair& r) const noexcept + { + return (cat > r.cat) || ((cat == r.cat) && (item != npos) && ((r.item == npos) || (item > r.item))); } }; @@ -688,7 +695,7 @@ namespace nana class oresolver { public: - oresolver(essence*); + oresolver(essence*) noexcept; oresolver& operator<<(bool); oresolver& operator<<(short); oresolver& operator<<(unsigned short); @@ -710,9 +717,9 @@ namespace nana oresolver& operator<<(cell); oresolver& operator<<(std::nullptr_t); - std::vector && move_cells(); + std::vector && move_cells() noexcept; - ::nana::listbox& listbox(); + ::nana::listbox& listbox() noexcept; private: essence* const ess_; std::vector cells_; @@ -721,7 +728,7 @@ namespace nana class iresolver { public: - iresolver(std::vector); + iresolver(std::vector) noexcept; iresolver& operator>>(bool&); iresolver& operator>>(short&); @@ -739,7 +746,7 @@ namespace nana iresolver& operator>>(std::string& text_utf8); iresolver& operator>>(std::wstring&); iresolver& operator>>(cell&); - iresolver& operator>>(std::nullptr_t); + iresolver& operator>>(std::nullptr_t) noexcept; private: std::vector cells_; std::size_t pos_{0}; @@ -756,7 +763,9 @@ namespace nana public: trigger(); ~trigger(); - essence& ess() const; + essence& ess() const noexcept; + private: + void _m_draw_border(); private: void attached(widget_reference, graph_reference) override; void detached() override; @@ -783,7 +792,7 @@ namespace nana : public std::iterator { public: - item_proxy(essence*); + item_proxy(essence*) noexcept; item_proxy(essence*, const index_pair&); /// the main porpose of this it to make obvious that item_proxy operate with absolute positions, and dont get moved during sort() @@ -796,7 +805,7 @@ namespace nana /// Determines whether the item is displayed on the screen bool displayed() const; - bool empty() const; + bool empty() const noexcept; /// Checks/unchecks the item /** @@ -826,9 +835,9 @@ namespace nana item_proxy& fgcolor(const nana::color&); nana::color fgcolor() const; - index_pair pos() const; + index_pair pos() const noexcept; - size_type columns() const; + size_type columns() const noexcept; item_proxy& text(size_type col, cell); item_proxy& text(size_type col, std::string); @@ -934,7 +943,7 @@ namespace nana bool operator!=(const item_proxy&) const; //Undocumented method - essence * _m_ess() const; + essence * _m_ess() const noexcept; private: std::vector _m_cells() const; nana::any * _m_value(bool alloc_if_empty); @@ -954,9 +963,9 @@ namespace nana template using value_translator = typename container_translator::value_translator; template using cell_translator = typename container_translator::cell_translator; - cat_proxy() = default; - cat_proxy(essence*, size_type pos); - cat_proxy(essence*, category_t*); + cat_proxy() noexcept = default; + cat_proxy(essence*, size_type pos) noexcept; + cat_proxy(essence*, category_t*) noexcept; /// Append an item at abs end of the category, set_value determines whether assign T object to the value of item. template @@ -1034,12 +1043,14 @@ namespace nana item_proxy at(size_type pos_abs) const; item_proxy back() const; - /// Returns the absolute index of a item by its display pos, the index of the item isn't changed after sorting. - /// convert from display order to absolute (find the real item in that display pos) but without check from current active sorting, in fact using just the last sorting !!! - size_type index_by_display_order(size_type disp_order) const; - - /// find display order for the real item but without check from current active sorting, in fact using just the last sorting !!! - size_type display_order(size_type pos) const; + /// Converts the index between absolute position and display position + /** + * @param from The index to be converted + * @param from_display_order If this parameter is true, the method converts a display position to an absolute position. + * If the parameter is false, the method converts an absolute position to a display position. + * @return a display position or an absolute position that are depending on from_display_order. + */ + size_type index_cast(size_type from, bool from_display_order) const; /// this cat position size_type position() const; @@ -1078,8 +1089,8 @@ namespace nana private: void _m_append(std::vector && cells); void _m_try_append_model(const const_virtual_pointer&); - void _m_cat_by_pos(); - void _m_update(); + void _m_cat_by_pos() noexcept; + void _m_update() noexcept; void _m_reset_model(model_interface*); private: essence* ess_{nullptr}; @@ -1158,7 +1169,6 @@ namespace nana unsigned header_height { 25 }; ///< def=25 . header height header_size unsigned text_height { 14 }; ///< the trigger will set this to the height of the text font unsigned item_height_ex { 6 }; ///< Set !=0 !!!! def=6. item_height = text_height + item_height_ex - unsigned item_height { 24 }; ///< def=24 . the trigger will set this TO item_height = text_height + item_height_ex unsigned header_splitter_area_before{ 2 }; ///< def=2. But 4 is better... IMO unsigned header_splitter_area_after { 3 }; ///< def=3. But 4 is better... @@ -1338,7 +1348,7 @@ the nana::detail::basic_window member pointer scheme bool assoc_ordered(bool); - void auto_draw(bool); ///< Set state: Redraw automatically after an operation + void auto_draw(bool) noexcept; ///< Set state: Redraw automatically after an operation template void avoid_drawing(Function fn) @@ -1383,10 +1393,10 @@ the nana::detail::basic_window member pointer scheme /// Returns the number of columns size_type column_size() const; - cat_proxy append(std::string); ///< Appends a new category to the end - cat_proxy append(std::wstring); ///< Appends a new category to the end - void append(std::initializer_list); ///< Appends categories to the end - void append(std::initializer_list); ///< Appends categories to the end + cat_proxy append(std::string category); ///< Appends a new category to the end + cat_proxy append(std::wstring category); ///< Appends a new category to the end + void append(std::initializer_list categories); ///< Appends categories to the end + void append(std::initializer_list categories); ///< Appends categories to the end cat_proxy insert(cat_proxy, ::std::string); cat_proxy insert(cat_proxy, ::std::wstring); @@ -1406,7 +1416,7 @@ the nana::detail::basic_window member pointer scheme void insert_item(const index_pair& abs_pos, const ::std::wstring& text); /// Returns an index of item which contains the specified point. - index_pair cast(const point & pos) const; + index_pair cast(const point & screen_pos) const; /// Returns the column which contains the specified point. size_type column_from_pos(const point & pos); @@ -1414,10 +1424,11 @@ the nana::detail::basic_window member pointer scheme void checkable(bool); index_pairs checked() const; ///, bool create_if_not_exists); - void _m_erase_key(nana::detail::key_interface*); + void _m_erase_key(nana::detail::key_interface*) noexcept; }; }//end namespace nana diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index 226c3ae5..73ae31e2 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -25,14 +25,12 @@ #include #include #include +#include #include #include #include #include -#include -#include -#include #include namespace nana @@ -49,11 +47,11 @@ namespace nana model_lock_guard(model_interface* model) : model_ptr_(model) { - if (model_ptr_) - model_ptr_->lock(); + if (model) + model->lock(); } - ~model_lock_guard() + ~model_lock_guard() noexcept { if (model_ptr_) model_ptr_->unlock(); @@ -64,7 +62,7 @@ namespace nana //struct cell - cell::format::format(const ::nana::color& bgcolor, const ::nana::color& fgcolor) + cell::format::format(const ::nana::color& bgcolor, const ::nana::color& fgcolor) noexcept : bgcolor{ bgcolor }, fgcolor{ fgcolor } {} @@ -74,13 +72,13 @@ namespace nana {} //A workaround that VS2013 does not support to define an explicit default move constructor - cell::cell(cell&& other) + cell::cell(cell&& other) noexcept : text(std::move(other.text)), custom_format{ std::move(other.custom_format) } { } - cell::cell(std::string text) + cell::cell(std::string text) noexcept : text(std::move(text)) {} @@ -91,7 +89,7 @@ namespace nana cell::cell(std::string text, const ::nana::color& bgcolor, const ::nana::color& fgcolor) : text(std::move(text)), - custom_format{std::make_unique( bgcolor, fgcolor ) } //custom_format{ new format{ bgcolor, fgcolor } } + custom_format{std::make_unique( bgcolor, fgcolor ) } {} cell& cell::operator=(const cell& rhs) @@ -104,7 +102,7 @@ namespace nana return *this; } - cell& cell::operator=(cell&& other) + cell& cell::operator=(cell&& other) noexcept { if (this != &other) { @@ -135,9 +133,6 @@ namespace nana std::function weak_ordering; - column() = default; - - column(const column&) = default; column& operator=(const column& other) @@ -168,22 +163,7 @@ namespace nana { } - column& operator=(column&& other) - { - if (this != &other) - { - text = std::move(other.text); - width_px = other.width_px; - range_width_px = other.range_width_px; - visible_state = other.visible_state; - index = other.index; - alignment = other.alignment; - weak_ordering = std::move(other.weak_ordering); - } - return *this; - } - - column(essence* ess, native_string_type&& text, unsigned px, size_type pos) : + column(essence* ess, native_string_type&& text, unsigned px, size_type pos) noexcept : text(std::move(text)), width_px(px), index(pos), @@ -192,7 +172,7 @@ namespace nana } private: //The definition is provided after essence - void _m_refresh(); + void _m_refresh() noexcept; private: essence* const ess_; public: @@ -221,15 +201,13 @@ namespace nana range_width_px.second = maximum; if (width_px < range_width_px.first) - { width_px = range_width_px.first; - _m_refresh(); - } else if (range_width_px.second < width_px) - { width_px = range_width_px.second; - _m_refresh(); - } + else + return; + + _m_refresh(); } void text_align(::nana::align align) noexcept override @@ -255,13 +233,13 @@ namespace nana _m_refresh(); } }; - + public: using container = std::vector; - export_options::columns_indexs all_headers(bool only_visibles) const + export_options::columns_indexs get_headers(bool only_visibles) const { export_options::columns_indexs idx; - for(const auto &col : cont()) + for(const auto &col : cont_) { if(col.visible_state || !only_visibles) idx.push_back(col.index); @@ -279,17 +257,18 @@ namespace nana first=false; else head_str += exp_opt.sep; + head_str += to_utf8(at(exp_opt.columns_order[idx]).text); } return head_str; } - bool visible() const + bool visible() const noexcept { return visible_; } - bool visible(bool v) + bool visible(bool v) noexcept { if (visible_ == v) return false; @@ -298,12 +277,12 @@ namespace nana return true; } - bool sortable() const + bool sortable() const noexcept { return sortable_; } - void sortable(bool enable) + void sortable(bool enable) noexcept { sortable_ = enable; } @@ -314,7 +293,7 @@ namespace nana return cont_.back().index; } - unsigned pixels() const ///< the visible width of the whole header + unsigned pixels() const noexcept ///< the visible width of the whole header { unsigned pixels = 0; for(auto & col : cont_) @@ -330,7 +309,7 @@ namespace nana * @param width The width to be fittd * @return true if the ranged columns is adjusted for the width, false otherwise. */ - bool calc_ranged_columns(unsigned width) + bool calc_ranged_columns(unsigned width) noexcept { unsigned fixed_px = 0; unsigned minimal_px = 0; @@ -362,23 +341,24 @@ namespace nana return true; //Don't calculate the ranged columns if - //There isn't a ranged column while maximal_px == 0, or + //there isn't a ranged column while maximal_px == 0, or //the minimal ranged size is larger than width if ((0 == maximal_px) || (fixed_px + minimal_px > width)) return false; - if (ranged_px + fixed_px > width) + const bool beyond = (ranged_px + fixed_px > width); + unsigned delta_px = (beyond ? ranged_px + fixed_px - width : width - (ranged_px + fixed_px)); + + while (delta_px) { - auto delta_px = ranged_px + fixed_px - width; - - while (delta_px) + for (auto & col : cont_) { - for (auto & col : cont_) - { - if (0 == delta_px) - break; + if (0 == delta_px) + break; - if (col.visible_state && (col.range_width_px.first < col.range_width_px.second)) + if (col.visible_state && (col.range_width_px.first < col.range_width_px.second)) + { + if (beyond) { if (col.range_width_px.first < col.width_px) { @@ -386,20 +366,7 @@ namespace nana --delta_px; } } - } - } - } - else - { - auto delta_px = width - (ranged_px + fixed_px); - while (delta_px) - { - for (auto & col : cont_) - { - if (0 == delta_px) - break; - - if (col.visible_state && (col.range_width_px.first < col.range_width_px.second)) + else { if (col.width_px < col.range_width_px.second) { @@ -410,11 +377,11 @@ namespace nana } } } - + return true; } - const container& cont() const + const container& cont() const noexcept { return cont_; } @@ -427,6 +394,7 @@ namespace nana if (m.index == pos) return m; } + throw std::out_of_range("Nana.GUI.Listbox: invalid header index."); } @@ -441,7 +409,7 @@ namespace nana } /// Returns the position(original index when it is creating) of the current column at point x - size_type column_from_point(int x) const + size_type column_from_point(int x) const noexcept { for (const auto & col : cont_) { @@ -451,14 +419,13 @@ namespace nana return col.index; x -= static_cast(col.width_px); - continue; } } return npos; } /// Returns the left point position and width(in variable *pixels) of column originaly at position pos. - int position(size_type pos, unsigned * pixels) const + int position(size_type pos, unsigned * pixels) const noexcept { int left = 0; for (auto & m : cont_) @@ -477,60 +444,54 @@ namespace nana } /// return the original index of the visible col currently before(in front of) or after the col originaly at index "index" - size_type neighbor(size_type index, bool front) const + size_type next(size_type index) const noexcept { - size_type n = npos; + bool found_me = false; for(auto i = cont_.cbegin(); i != cont_.cend(); ++i) // in current order { - if(i->index == index) + if (!found_me) { - if(front) return n; - for(++i; i != cont_.cend(); ++i) - { - if(i->visible_state) return i->index; - } - break; + if (i->index == index) + found_me = true; } else if(i->visible_state) - n = i->index; - } - return npos; - } - - /// return the original index of the currently first visible col - size_type begin() const - { - for(const auto & m : cont_) - if(m.visible_state) return m.index; - - return npos; - } - - /// return the original index of the currently last visible col - size_type last() const - { - for(auto i = cont_.rbegin(); i != cont_.rend(); ++i) - { - if(i->visible_state) return i->index; } return npos; } - - /// move the col originaly at "index" to the position currently in front (or after) the col originaly at index "to" invalidating some current index - void move(size_type index, size_type to, bool front) throw() - { - if ((index == to) || (index >= cont_.size()) || (to >= cont_.size())) - return; + /// Returns the absolute position of the first/last visible column. + size_type boundary(bool get_front) const noexcept + { + size_type pos = npos; + for (const auto & m : cont_) + { + if (m.visible_state) + { + if (get_front) + return m.index; + else + pos = m.index; + } + } + + return pos; + } + + /// move the col originaly at "from" to the position currently in front (or after) the col originaly at index "to" invalidating some current index + void move(size_type from, size_type to, bool front) noexcept + { + if ((from == to) || (from >= cont_.size()) || (to >= cont_.size())) + return; + for (auto i = cont_.begin(); i != cont_.end(); ++i) { - if (index == i->index) + if (from == i->index) { auto col_from = std::move(*i); cont_.erase(i); - for (auto u = cont_.begin(); u != cont_.end(); ++u) + for (auto u = cont_.cbegin(); u != cont_.cend(); ++u) { if (to == u->index) { @@ -559,7 +520,7 @@ namespace nana paint::image img; nana::size img_show_size; - struct flags_tag + struct inner_flags { bool selected :1; bool checked :1; @@ -567,7 +528,7 @@ namespace nana mutable std::unique_ptr anyobj; - item_data() + item_data() noexcept { flags.selected = flags.checked = false; } @@ -594,15 +555,6 @@ namespace nana cells->emplace_back(std::move(s)); } - item_data(std::string&& s, const nana::color& bg, const nana::color& fg): - cells(std::make_unique()), - bgcolor(bg), - fgcolor(fg) - { - flags.selected = flags.checked = false; - cells->emplace_back(std::move(s)); - } - item_data& operator=(const item_data& r) { if (this != &r) @@ -611,7 +563,10 @@ namespace nana cells = std::make_unique(*r.cells); flags = r.flags; - anyobj.reset(r.anyobj ? new nana::any(*r.anyobj) : nullptr); + + if (r.anyobj) + anyobj.reset(new ::nana::any(*r.anyobj)); + bgcolor = r.bgcolor; fgcolor = r.fgcolor; img = r.img; @@ -633,10 +588,7 @@ namespace nana item_str += exp_opt.sep; //Use the model cells instead if model cells is avaiable - if (model_cells) - item_str += model_cells->operator[](col).text; - else - item_str += (*cells)[col].text; + item_str += (model_cells ? model_cells : cells.get())->operator[](col).text; } return item_str; @@ -663,13 +615,11 @@ namespace nana std::deque>> factories; std::deque> indicators; - category_t() = default; - - category_t(native_string_type str) + category_t(native_string_type str = {}) noexcept :text(std::move(str)) {} - bool selected() const + bool selected() const noexcept { for (auto & m : items) { @@ -678,6 +628,21 @@ namespace nana } return !items.empty(); } + + void make_sort_order() + { + sorted.clear(); + for (std::size_t i = 0; i < items.size(); ++i) + sorted.push_back(i); + } + + std::vector cells(size_type pos) const + { + if (model_ptr) + return model_ptr->container()->to_cells(pos); + + return *(items.at(pos).cells); + } }; struct inline_pane @@ -694,25 +659,35 @@ namespace nana { public: using container = std::list; - using item_type = item_data; std::function(std::size_t) > fetch_ordering_comparer; + struct sort_attributes + { + std::size_t column; ///< The position of the column to be sorted + bool resort; + bool reverse; + }; + es_lister() { //#0 is a default category categories_.emplace_back(); + + sort_attrs_.column = npos; + sort_attrs_.resort = true; + sort_attrs_.reverse = false; } - void bind(essence* ess, widget& wd) + void bind(essence* ess, widget& wd) noexcept { ess_ = ess; widget_ = dynamic_cast(&wd); } - nana::listbox* wd_ptr() const + nana::listbox* wd_ptr() const noexcept { return widget_; } @@ -729,7 +704,7 @@ namespace nana if (allocate_if_empty) { - item.anyobj.reset(new any); + item.anyobj.reset(new ::nana::any); return item.anyobj.get(); } } @@ -751,217 +726,200 @@ namespace nana return panes; } - void emit_checked(index_pair pos) + void emit_cs(const index_pair& pos, bool for_selection) { item_proxy i(ess_, pos); arg_listbox arg{ i }; - wd_ptr()->events().checked.emit(arg, wd_ptr()->handle()); + + auto & events = wd_ptr()->events(); + + if (for_selection) + events.selected.emit(arg, wd_ptr()->handle()); + else + events.checked.emit(arg, wd_ptr()->handle()); auto panes = get_inline_pane(pos); for (auto p : panes) - p->inline_ptr->notify_status(inline_widget_status::checking, i.checked()); - } - - void emit_selected(index_pair pos) - { - item_proxy i(ess_, pos); - arg_listbox arg{ i }; - wd_ptr()->events().selected.emit(arg, wd_ptr()->handle()); - - auto panes = get_inline_pane(pos); - for (auto p : panes) - p->inline_ptr->notify_status(inline_widget_status::selecting, i.selected()); + { + if(for_selection) + p->inline_ptr->notify_status(inline_widget_status::selecting, i.selected()); + else + p->inline_ptr->notify_status(inline_widget_status::checking, i.checked()); + } } // Definition is provided after struct essence unsigned column_content_pixels(size_type pos) const; + const sort_attributes& sort_attrs() const noexcept + { + return sort_attrs_; + } + /// each sort() ivalidate any existing reference from display position to absolute item, that is after sort() display offset point to different items void sort() { - if((sorted_index_ == npos) || (!resort_)) + if((npos == sort_attrs_.column) || (!sort_attrs_.resort)) return; - auto weak_ordering_comp = fetch_ordering_comparer(sorted_index_); + auto weak_ordering_comp = fetch_ordering_comparer(sort_attrs_.column); if(weak_ordering_comp) { for (auto & cat : categories_) { - auto bi = std::begin(cat.sorted); - auto ei = std::end(cat.sorted); + const bool use_model = (cat.model_ptr != nullptr); - if (cat.model_ptr) - { - std::stable_sort(bi, ei, [&cat, &weak_ordering_comp, this](std::size_t x, std::size_t y){ - //The predicate must be a strict weak ordering. - //!comp(x, y) != comp(x, y) + std::stable_sort(cat.sorted.begin(), cat.sorted.end(), [&cat, &weak_ordering_comp, use_model, this](std::size_t x, std::size_t y){ + //The predicate must be a strict weak ordering. + //!comp(x, y) != comp(x, y) + if (use_model) + { auto & mx = cat.items[x]; auto & my = cat.items[y]; auto mx_cells = cat.model_ptr->container()->to_cells(x); auto my_cells = cat.model_ptr->container()->to_cells(y); - if (mx_cells.size() <= sorted_index_ || my_cells.size() <= sorted_index_) + if (mx_cells.size() <= sort_attrs_.column || my_cells.size() <= sort_attrs_.column) { std::string a; - if (mx_cells.size() > sorted_index_) - a = mx_cells[sorted_index_].text; + if (mx_cells.size() > sort_attrs_.column) + a = mx_cells[sort_attrs_.column].text; std::string b; - if (my_cells.size() > sorted_index_) - b = my_cells[sorted_index_].text; + if (my_cells.size() > sort_attrs_.column) + b = my_cells[sort_attrs_.column].text; - return weak_ordering_comp(a, mx.anyobj.get(), b, my.anyobj.get(), sorted_reverse_); + return weak_ordering_comp(a, mx.anyobj.get(), b, my.anyobj.get(), sort_attrs_.reverse); } - return weak_ordering_comp(mx_cells[sorted_index_].text, mx.anyobj.get(), my_cells[sorted_index_].text, my.anyobj.get(), sorted_reverse_); - }); - } - else - { - std::stable_sort(bi, ei, [&cat, &weak_ordering_comp, this](std::size_t x, std::size_t y){ - //The predicate must be a strict weak ordering. - //!comp(x, y) != comp(x, y) + return weak_ordering_comp(mx_cells[sort_attrs_.column].text, mx.anyobj.get(), my_cells[sort_attrs_.column].text, my.anyobj.get(), sort_attrs_.reverse); + } + + auto & mx = cat.items[x]; + auto & my = cat.items[y]; - auto & mx = cat.items[x]; - auto & my = cat.items[y]; + if (mx.cells->size() <= sort_attrs_.column || my.cells->size() <= sort_attrs_.column) + { + std::string a; + if (mx.cells->size() > sort_attrs_.column) + a = (*mx.cells)[sort_attrs_.column].text; - if (mx.cells->size() <= sorted_index_ || my.cells->size() <= sorted_index_) - { - std::string a; - if (mx.cells->size() > sorted_index_) - a = (*mx.cells)[sorted_index_].text; + std::string b; + if (my.cells->size() > sort_attrs_.column) + b = (*my.cells)[sort_attrs_.column].text; - std::string b; - if (my.cells->size() > sorted_index_) - b = (*my.cells)[sorted_index_].text; + return weak_ordering_comp(a, mx.anyobj.get(), b, my.anyobj.get(), sort_attrs_.reverse); + } - return weak_ordering_comp(a, mx.anyobj.get(), b, my.anyobj.get(), sorted_reverse_); - } - - return weak_ordering_comp((*mx.cells)[sorted_index_].text, mx.anyobj.get(), (*my.cells)[sorted_index_].text, my.anyobj.get(), sorted_reverse_); - }); - } + return weak_ordering_comp((*mx.cells)[sort_attrs_.column].text, mx.anyobj.get(), (*my.cells)[sort_attrs_.column].text, my.anyobj.get(), sort_attrs_.reverse); + }); } } else { //No user-defined comparer is provided, and default comparer is applying. for (auto & cat : categories_) { - if (cat.model_ptr) - { - std::stable_sort(std::begin(cat.sorted), std::end(cat.sorted), [&cat, this](std::size_t x, std::size_t y){ + const bool use_model = (cat.model_ptr != nullptr); + + std::stable_sort(cat.sorted.begin(), cat.sorted.end(), [this, &cat, use_model](std::size_t x, std::size_t y){ + //The predicate must be a strict weak ordering. + //!comp(x, y) != comp(x, y) + if (use_model) + { auto mx_cells = cat.model_ptr->container()->to_cells(x); auto my_cells = cat.model_ptr->container()->to_cells(y); - if (mx_cells.size() <= sorted_index_ || my_cells.size() <= sorted_index_) + if (mx_cells.size() <= sort_attrs_.column || my_cells.size() <= sort_attrs_.column) { std::string a; - if (mx_cells.size() > sorted_index_) - a = mx_cells[sorted_index_].text; + if (mx_cells.size() > sort_attrs_.column) + a = mx_cells[sort_attrs_.column].text; std::string b; - if (my_cells.size() > sorted_index_) - b = my_cells[sorted_index_].text; + if (my_cells.size() > sort_attrs_.column) + b = my_cells[sort_attrs_.column].text; - return (sorted_reverse_ ? a > b : a < b); + return (sort_attrs_.reverse ? a > b : a < b); } - auto & a = mx_cells[sorted_index_].text; - auto & b = my_cells[sorted_index_].text; - return (sorted_reverse_ ? a > b : a < b); - }); - } - else - { - std::stable_sort(std::begin(cat.sorted), std::end(cat.sorted), [&cat, this](std::size_t x, std::size_t y){ + auto & a = mx_cells[sort_attrs_.column].text; + auto & b = my_cells[sort_attrs_.column].text; + return (sort_attrs_.reverse ? a > b : a < b); + } - auto & mx = cat.items[x]; - auto & my = cat.items[y]; + auto & mx = cat.items[x]; + auto & my = cat.items[y]; - if (mx.cells->size() <= sorted_index_ || my.cells->size() <= sorted_index_) - { - std::string a; - if (mx.cells->size() > sorted_index_) - a = (*mx.cells)[sorted_index_].text; + if (mx.cells->size() <= sort_attrs_.column || my.cells->size() <= sort_attrs_.column) + { + std::string a; + if (mx.cells->size() > sort_attrs_.column) + a = (*mx.cells)[sort_attrs_.column].text; - std::string b; - if (my.cells->size() > sorted_index_) - b = (*my.cells)[sorted_index_].text; + std::string b; + if (my.cells->size() > sort_attrs_.column) + b = (*my.cells)[sort_attrs_.column].text; - return (sorted_reverse_ ? a > b : a < b); - } + return (sort_attrs_.reverse ? a > b : a < b); + } - auto & a = (*mx.cells)[sorted_index_].text; - auto & b = (*my.cells)[sorted_index_].text; - return (sorted_reverse_ ? a > b : a < b); - }); - } + auto & a = (*mx.cells)[sort_attrs_.column].text; + auto & b = (*my.cells)[sort_attrs_.column].text; + return (sort_attrs_.reverse ? a > b : a < b); + }); } } - scroll_refresh(); } - void scroll_refresh(); - - /// sort() and ivalidate any existing reference from display position to absolute item, that is after sort() display offset point to different items - bool sort_index(size_type index) + /// Sorts the specified column + /** + * It sorts the specified column and invalidates all existing item reference from display position to absolute position. + * The side effect of this method is that all display positions point to different absolute positions. + * @param pos The position of the specified column. + * @param reverse A pointer to a boolean which indicates whether to reverse sort. If this parameter is nullptr, the sort is negated to the current reverse state. + * @return true if the column is sorted, false otherwise. + */ + bool sort_column(std::size_t pos, const bool * reverse) { - if (npos == index) + if (npos == pos) { - sorted_index_ = npos; - scroll_refresh(); + sort_attrs_.column = npos; return false; } - if(index != sorted_index_) + if (reverse) { - sorted_index_ = index; - sorted_reverse_ = false; + if (pos != sort_attrs_.column || *reverse != sort_attrs_.reverse) + { + sort_attrs_.column = pos; + sort_attrs_.reverse = *reverse; + + sort(); + } } else - sorted_reverse_ = !sorted_reverse_; - - sort(); - return true; - } - - /// sort() and ivalidate any existing reference from display position to absolute item, that is: after sort() display offset point to different items - bool set_sort_index(std::size_t index, bool reverse) - { - if (npos == index) { - sorted_index_ = npos; - scroll_refresh(); - return false; - } + if (pos != sort_attrs_.column) + { + sort_attrs_.column = pos; + sort_attrs_.reverse = false; + } + else + sort_attrs_.reverse = !sort_attrs_.reverse; - if(index != sorted_index_ || reverse != sorted_reverse_) - { - sorted_index_ = index; - sorted_reverse_ = reverse; sort(); } return true; } - std::size_t sort_index() const + bool active_sort(bool resort) noexcept { - return sorted_index_; - } - - bool active_sort(bool resort) - { - bool prstatus = resort; - resort_ = resort; + bool prstatus = sort_attrs_.resort; + sort_attrs_.resort = resort; return prstatus; } - bool sort_reverse() const - { - return sorted_reverse_; - } - void scroll(const index_pair& pos, bool to_bottom); /// Append a new category with a specified name and return a pointer to it. @@ -971,11 +929,10 @@ namespace nana return &categories_.back(); } - /// will use the key to insert new cat before the first cat with compare less than the key, or at the end of the list of cat and return a ref to that new cat. ? - category_t* create_cat(std::shared_ptr& ptr) + category_t* create_cat(std::shared_ptr& ptr) { - for (auto i = categories_.begin(); i != categories_.end(); ++i) + for (auto i = categories_.cbegin(); i != categories_.cend(); ++i) { if (i->key_ptr) { @@ -986,16 +943,16 @@ namespace nana } else if (ptr->compare(i->key_ptr.get())) { - i = categories_.emplace(i); - i->key_ptr = ptr; - return &(*i); + auto & catobj = *categories_.emplace(i); + catobj.key_ptr = ptr; + return &catobj; } } } categories_.emplace_back(); categories_.back().key_ptr = ptr; - return &categories_.back(); + return &(categories_.back()); } /// add a new cat created at "pos" and return a ref to it @@ -1005,16 +962,15 @@ namespace nana } /// Insert before item in absolute "pos" a new item with "text" in column 0, and place it in last display position of this cat - void insert(const index_pair& pos, std::string&& text, std::size_t columns) + void insert(const index_pair& pos, std::string&& text, const std::size_t columns) { auto & catobj = *get(pos.cat); - const auto n = catobj.items.size(); - if (pos.item > n) + const auto item_count = catobj.items.size(); + if (pos.item > item_count) throw std::out_of_range("listbox: insert an item at invalid position"); - catobj.sorted.push_back(n); - + catobj.sorted.push_back(item_count); if (catobj.model_ptr) { @@ -1022,7 +978,7 @@ namespace nana auto container = catobj.model_ptr->container(); std::size_t item_index; // - if (pos.item < n) + if (pos.item < item_count) { catobj.items.emplace(catobj.items.begin() + pos.item); container->emplace(pos.item); @@ -1035,43 +991,62 @@ namespace nana container->emplace_back(); } - std::vector cells{ columns }; - cells[0] = std::move(text); + std::vector cells; + cells.emplace_back(std::move(text)); + cells.resize(columns); container->assign(item_index, cells); + + return; } - else + + catobj.items.emplace(catobj.items.begin() + (pos.item < item_count ? pos.item : item_count), std::move(text)); + } + + /// Converts an index between display position and absolute real position. + /** + * @param from An index to be converted + * @param from_display_order If this parameter is true, the method convert a display position to the absolute position. If the parameter + * is false, the method converts an absolute position to the display position. + * @return A display position or absolute position that are depending on from_display_order. + */ + index_pair index_cast(const index_pair& from, bool from_display_order) const + { + auto cat = get(from.cat); + if (from.item < cat->sorted.size()) { - if (pos.item < n) - catobj.items.emplace(catobj.items.begin() + pos.item, std::move(text)); - else - catobj.items.emplace_back(std::move(text)); + if (from_display_order) + return index_pair{ from.cat, static_cast(cat->sorted[from.item]) }; - catobj.items.back().cells->emplace_back(std::move(text)); + for (size_type i = 0; i < cat->sorted.size(); ++i) + { + if (from.item == cat->sorted[i]) + return index_pair{ from.cat, i }; + } } + throw std::out_of_range("listbox: invalid item position"); } - - /// convert from display order to absolute (find the real item in that display pos) but without check from current active sorting, in fact using just the last sorting !!! - size_type index_by_display_order(size_type cat, size_type display_order_pos) const + + index_pair index_cast_noexcpt(const index_pair& from, bool from_display_order, const index_pair& default_value = index_pair{npos, npos}) const noexcept { - auto & catobj = *get(cat); - if (display_order_pos >= catobj.sorted.size()) - throw std::out_of_range("listbox: Invalid item position."); + if (from.cat < categories_.size()) + { + auto i = categories_.cbegin(); + std::advance(i, from.cat); - return catobj.sorted[display_order_pos]; - } + auto & cat = *i; + if (from.item < cat.sorted.size()) + { + if (from_display_order) + return index_pair{ from.cat, static_cast(cat.sorted[from.item]) }; - /// find display order for the real item but without check from current active sorting, in fact using just the last sorting !!! - size_type display_order(size_type cat, size_type pos) const - { - auto& catobj = *get(cat); - if (pos >= catobj.sorted.size()) - throw std::out_of_range("listbox: Invalid item position."); - - for (size_type i=0; imodel_ptr) { throw_if_immutable_model(i->model_ptr.get()); - i->model_ptr->container()->assign(pos.item, cells); } } } - bool have_model(const index_pair& pos) const + bool has_model(const index_pair& pos) const { return (get(pos.cat)->model_ptr != nullptr); } @@ -1133,12 +1107,20 @@ namespace nana /// return a ref to the real item object at display!!! position pos using current sorting only if it is active, and at absolute position if no sorting is currently active. category_t::container::value_type& at(const index_pair& pos) { - auto index = pos.item; + auto acc_pos = pos.item; + if (npos != sort_attrs_.column) + acc_pos = index_cast(pos, true).item; //convert display position to absolute position - if (sorted_index_ != npos) - index = absolute(pos); + return get(pos.cat)->items.at(acc_pos); + } - return get(pos.cat)->items.at(index); + const category_t::container::value_type& at(const index_pair& pos) const + { + auto acc_pos = pos.item; + if (npos != sort_attrs_.column) + acc_pos = index_cast(pos, true).item; //convert display position to absolute position + + return get(pos.cat)->items.at(acc_pos); } std::vector at_model(const index_pair& pos) const @@ -1149,22 +1131,11 @@ namespace nana model_lock_guard lock(model_ptr); - auto index = pos.item; + auto acc_pos = pos.item; + if (npos != sort_attrs_.column) + acc_pos = index_cast(pos, true).item; //convert display position to absolute position - if (sorted_index_ != npos) - index = absolute(pos); - - return model_ptr->container()->to_cells(index); - } - - const category_t::container::value_type& at(const index_pair& pos) const - { - auto index = pos.item; - - if (sorted_index_ != npos) - index = absolute(pos); - - return get(pos.cat)->items.at(index); + return model_ptr->container()->to_cells(acc_pos); } void append_active_panes(inline_pane* p) @@ -1197,21 +1168,19 @@ namespace nana // Clears all items in all cat, but not the container of cat self. void clear() { - // Check whether there is a immutable model + // Check whether there is a immutable model before performing clear. for (auto & cat : categories_) throw_if_immutable_model(cat.model_ptr.get()); - auto n = categories_.size(); - for (decltype(n) i = 0; i < n; ++i) - { + for (size_type i = 0; i < categories_.size(); ++i) clear(i); - } } - index_pair advance(index_pair from, size_type offset) // <------------- index + index_pair advance(const index_pair& from, size_type offset) const { + const auto cat_size = categories_.size(); index_pair dpos{npos, npos}; - if(from.cat >= size_categ() || (from.item != npos && from.item >= size_item(from.cat))) + if (from.cat >= cat_size || (from.item != npos && from.item >= size_item(from.cat))) return dpos; dpos = from; @@ -1222,7 +1191,7 @@ namespace nana { if(expand(dpos.cat) == false) { - if(dpos.cat + 1 == size_categ()) + if (dpos.cat + 1 == cat_size) break; ++dpos.cat; } @@ -1235,7 +1204,7 @@ namespace nana size_type rest = size_item(dpos.cat) - dpos.item - 1; if(rest == 0) { - if(dpos.cat + 1 == size_categ()) + if (dpos.cat + 1 == cat_size) break; ++dpos.cat; dpos.item = npos; @@ -1244,7 +1213,7 @@ namespace nana else if(rest < offset) { offset -= rest; - if(dpos.cat + 1 >= size_categ()) + if (dpos.cat + 1 >= cat_size) { dpos.item += rest; break; @@ -1261,6 +1230,7 @@ namespace nana } return dpos; } + /// change to index arg size_type distance(index_pair from, index_pair to) const { @@ -1269,7 +1239,7 @@ namespace nana if(to.cat == from.cat) { if(from.item > to.item && from.item != npos) - std::swap(from.item, to.item); + std::swap(from, to); return (from.item == npos ? to.item + 1 : to.item - from.item); } @@ -1286,7 +1256,7 @@ namespace nana else n = i->items.size() - (from.item + 1); - for(++i, ++from.cat; i != categories_.end(); ++i, ++from.cat) + for(++i, ++from.cat; i != categories_.cend(); ++i, ++from.cat) { ++n; //this is a category if(from.cat != to.cat) @@ -1304,17 +1274,6 @@ namespace nana return n; } - std::vector get_cells(category_t * cat, std::size_t pos) const - { - if (!cat) - throw std::out_of_range("nana::listbox: category is null"); - - if (cat->model_ptr) - return cat->model_ptr->container()->to_cells(pos); - - return *(cat->items.at(pos).cells); - } - void text(category_t* cat, size_type pos, size_type col, cell&& cl, size_type columns) { if ((col < columns) && (pos < cat->items.size())) @@ -1333,7 +1292,7 @@ namespace nana if (col < cells.size()) { cells[col] = std::move(cl); - if (sorted_index_ == col) + if (sort_attrs_.column == col) sort(); } else @@ -1365,8 +1324,8 @@ namespace nana if (col < cells.size()) { - cells[col].text.swap(str); - if (sorted_index_ == col) + cells[col].text = std::move(str); + if (sort_attrs_.column == col) sort(); } else @@ -1406,22 +1365,17 @@ namespace nana void erase() { //Do not remove the first category. - auto i = categories_.begin(); - if (i->model_ptr) - { - throw_if_immutable_model(i->model_ptr.get()); - i->model_ptr->container()->clear(); - } - - i->items.clear(); - i->sorted.clear(); + this->erase(0); if (categories_.size() > 1) - categories_.erase(++i, categories_.end()); + { + auto i = categories_.cbegin(); + categories_.erase(++i, categories_.cend()); + } } - bool expand(size_type cat, bool exp) + bool expand(size_type cat, bool exp) noexcept { if(good(cat)) { @@ -1435,23 +1389,23 @@ namespace nana return false; } - bool expand(size_type cat) const + bool expand(size_type cat) const noexcept { return (good(cat) ? get(cat)->expand : false); } - container& cat_container() + container& cat_container() noexcept { return categories_; } - const container& cat_container() const + const container& cat_container() const noexcept { return categories_; } //Enable/Disable the ordered categories - bool enable_ordered(bool enb) + bool enable_ordered(bool enb) noexcept { if (ordered_categories_ != enb) { @@ -1479,12 +1433,12 @@ namespace nana return true; } - bool enable_ordered() const + bool enable_ordered() const noexcept { return ordered_categories_; } - size_type the_number_of_expanded() const + size_type the_number_of_expanded() const noexcept { size_type n = categories_.size() - 1; for (auto & i : categories_) @@ -1495,98 +1449,91 @@ namespace nana return n; } - void check_for_all(bool ck) - { - index_pair pos; - for (auto & cat : categories_) - { - pos.item = 0; - for(auto & m : cat.items) - { - if(m.flags.checked != ck) - { - m.flags.checked = ck; - emit_checked(pos); - } - ++pos.item; - } - ++pos.cat; - } - } - - void select_range(index_pair fr, index_pair to, bool sel) - { - if (fr > to) - std::swap(fr, to); - - for (; fr != to; forward(fr, 1, fr)) - { - if (fr.is_item()) - item_proxy(ess_, fr).select(sel); - } - - if (to.is_item()) - item_proxy(ess_, to).select(sel); - } - - void select_display_range(index_pair fr_abs, index_pair to_dpl, bool sel) + template + std::vector> select_display_range_if(index_pair fr_abs, index_pair to_dpl, bool unselect_others, Pred pred) { const auto already_selected = this->pick_items(true); - index_pair fr_dpl (fr_abs.cat, this->display_order(fr_abs.cat, fr_abs.item)); + auto fr_dpl = this->index_cast(fr_abs, false); //Converts an absolute position to display position if (fr_dpl > to_dpl) std::swap(fr_dpl, to_dpl); const auto begin = fr_dpl; const auto last = to_dpl; + //pair first: index in the range of [begin, last] + //pair second: indicates whether the index is selected before selection. + std::vector> pairs; + for (; fr_dpl != to_dpl; forward(fr_dpl, 1, fr_dpl)) { - if (fr_dpl.is_item()) - item_proxy(ess_, index_pair(fr_dpl.cat, absolute( fr_dpl ) )).select(sel); + if (!fr_dpl.is_category()) + { + auto abs_pos = index_cast(fr_dpl, true); //convert display position to absolute position + item_proxy m{ ess_, abs_pos }; + pairs.emplace_back(abs_pos, m.selected()); + + if (pred(abs_pos)) + m.select(true); + } } - if (to_dpl.is_item()) - item_proxy(ess_, index_pair(to_dpl.cat, absolute( to_dpl ) )).select(sel); - - //Unselects the already selected which is out of range [begin, last] - for (auto index : already_selected) + if (!to_dpl.is_category()) { - index_pair disp_order{ index.cat, this->display_order(index.cat, index.item) }; - if (begin > disp_order || disp_order > last) - item_proxy(ess_, index_pair(index.cat, absolute(disp_order))).select(false); + auto abs_pos = index_cast(to_dpl, true); //convert display position to absolute position + + item_proxy m(ess_, abs_pos); + pairs.emplace_back(abs_pos, m.selected()); + + if (pred(abs_pos)) + m.select(true); } + + if (unselect_others) + { + //Unselects the already selected which is out of range [begin, last] + for (auto index : already_selected) + { + auto disp_order = this->index_cast(index, false); //converts an absolute position to a display position + if (begin > disp_order || disp_order > last) + item_proxy{ ess_, index }.select(false); + } + } + + return pairs; } - bool select_for_all(bool sel) + bool select_for_all(bool sel, const index_pair& except = index_pair{npos, npos}) { bool changed = false; - index_pair i; + index_pair pos; for (auto & cat : categories_) { - i.item = 0; + pos.item = 0; for(auto & m : cat.items) { - if(m.flags.selected != sel) + if (except != pos) { - changed = true; - m.flags.selected = sel; + if (m.flags.selected != sel) + { + changed = true; + m.flags.selected = sel; - this->emit_selected(i); + this->emit_cs(pos, true); - if (m.flags.selected) - last_selected_abs = i; - else if (last_selected_abs == i) - last_selected_abs.set_both(npos); //make empty + if (m.flags.selected) + last_selected_abs = pos; + else if (last_selected_abs == pos) + last_selected_abs.set_both(npos); //make empty + } } - ++i.item; + ++pos.item; } - ++i.cat; + ++pos.cat; } return changed; } - /// return absolute positions, no relative to display index_pairs pick_items(bool for_selection) const { @@ -1607,23 +1554,6 @@ namespace nana return results; } - index_pair find_first_selected() - { - index_pair id; - for (auto & cat : categories_) - { - id.item = 0; - for(auto & m : cat.items) - { - if(m.flags.selected) - return id; // absolute positions, no relative to display - ++id.item; - } - ++id.cat; - } - return index_pair{npos,npos}; - } - /// return absolute positions, no relative to display bool item_selected_all_checked(index_pairs& vec) const { @@ -1651,30 +1581,45 @@ namespace nana ///emit_cs(item_pos, for_selection); + } + }; void cancel_others_if_single_enabled(bool for_selection, const index_pair& except) { if (!(for_selection ? single_selection_ : single_check_)) return; - auto pred = [for_selection](category_t::container::value_type & m){ - return (for_selection ? m.flags.selected : m.flags.checked); - }; - - auto do_cancel = [this, for_selection](category_t::container::value_type& m, const index_pair& item_pos) - { - if (for_selection) - { - m.flags.selected = false; - this->emit_selected(item_pos); - } - else - { - m.flags.checked = false; - this->emit_checked(item_pos); - } - }; + pred_cancel pred{ for_selection }; + emit_cancel do_cancel{ this, for_selection }; if (for_selection ? single_selection_category_limited_ : single_check_category_limited_) { @@ -1691,42 +1636,26 @@ namespace nana } else { - std::size_t cat_pos = 0; + index_pair cancel_pos; for (auto & cat : categories_) { - if (cat_pos != except.cat) + for (auto & m : cat.items) { - std::size_t item_pos = 0; - for (auto & m : cat.items) - { - if (pred(m)) - do_cancel(m, index_pair{ cat_pos, item_pos }); - ++item_pos; - } + if ((cancel_pos != except) && pred(m)) + do_cancel(m, cancel_pos); + + ++cancel_pos.item; } - else - { - std::size_t item_pos = 0; - for (auto & m : cat.items) - { - if ((item_pos != except.item) && pred(m)) - do_cancel(m, index_pair{ cat_pos, item_pos }); - ++item_pos; - } - } - ++cat_pos; + + ++cancel_pos.cat; + cancel_pos.item = 0; } } } - bool single_selection() const + bool single_status(bool for_selection) const noexcept { - return single_selection_; - } - - bool single_check() const - { - return single_check_; + return (for_selection ? single_selection_ : single_check_); } void enable_single(bool for_selection, bool category_limited) @@ -1740,152 +1669,90 @@ namespace nana single = true; limited = category_limited; - auto pred = [for_selection](category_t::container::value_type & m){ - return (for_selection ? m.flags.selected : m.flags.checked); - }; - - auto cancel = [this, for_selection](category_t::container::value_type& m, const index_pair& item_pos) - { - if (for_selection) - { - m.flags.selected = false; - this->emit_selected(item_pos); - } - else - { - m.flags.checked = false; - this->emit_checked(item_pos); - } - }; + pred_cancel pred{ for_selection }; + emit_cancel cancel{ this, for_selection }; std::size_t cat_pos = 0; - if (category_limited) - { - for (auto & cat : categories_) - { - auto i = std::find_if(cat.items.begin(), cat.items.end(), pred); - if (i != cat.items.end()) - { - ++i; - for (auto end = cat.items.end(); i != end; ++i) - { - if (pred(*i)) - cancel(*i, index_pair{ cat_pos, static_cast(i - cat.items.begin()) }); - } - } - ++cat_pos; - } - } - else - { - bool selected = false; - for (auto & cat : categories_) - { - if(!selected) - { - const auto end = cat.items.end(); - auto i = std::find_if(cat.items.begin(), end, pred); - if (i != end) + bool selected = false; + + for (auto & cat : categories_) + { + if ((category_limited) || (!selected)) + { + bool ignore = true; //Ignore the first matched item + std::size_t pos = 0; + for (auto & m : cat.items) + { + if (pred(m)) { selected = true; - for (++i; i != end; ++i) - { - if (pred(*i)) - cancel(*i, index_pair{ cat_pos, static_cast(i - cat.items.begin()) }); - } + if (ignore) + ignore = false; + else + cancel(m, index_pair{ cat_pos, pos }); } + ++pos; } - else + ++cat_pos; + } + else + { + std::size_t skip_cat = 0; + for (auto & cat : categories_) { - for (auto & cat : categories_) - { - std::size_t item_pos = 0; - for (auto & m : cat.items) - { - if (pred(m)) - cancel(m, index_pair{ cat_pos, item_pos }); + if (skip_cat++ < cat_pos) + continue; - ++item_pos; - } + std::size_t pos = 0; + for (auto & m : cat.items) + { + if (pred(m)) + cancel(m, index_pair{ cat_pos, pos }); + ++pos; } ++cat_pos; } + break; } } } - void disable_single(bool for_selection) + void disable_single(bool for_selection) noexcept { (for_selection ? single_selection_ : single_check_) = false; } - size_type size_categ() const - { - return categories_.size(); - } - size_type size_item(size_type cat) const { return get(cat)->items.size(); } - bool categ_checked(size_type cat) const + bool cat_status(size_type pos, bool for_selection) const { - auto& items = get(cat)->items; - for(auto & m : items) + auto & items = get(pos)->items; + for (auto & m : items) { - if(m.flags.checked == false) + if ((for_selection ? m.flags.selected : m.flags.checked) == false) return false; } return true; } - bool categ_checked(size_type cat, bool ck) - { - bool changed = false; - auto & items = get(cat)->items; - size_type index = 0; - for(auto & m : items) - { - if(m.flags.checked != ck) - { - m.flags.checked = ck; - this->emit_checked(index_pair{cat, index}); - changed = true; - } - ++index; - } - return changed; - } + bool cat_status(size_type pos, bool for_selection, bool value); - bool categ_checked_reverse(size_type cat_index) + bool cat_status_reverse(size_type pos, bool for_selection) noexcept { - if (categories_.size() > cat_index) - return categ_checked(cat_index, !categ_checked(cat_index)); + if (pos < categories_.size()) + { + return cat_status(pos, !cat_status(pos, for_selection)); + } return false; } - bool categ_selected(size_type cat) const - { - auto & items = get(cat)->items; - for(auto & m : items) - if(m.flags.selected == false) - return false; - return true; - } - - /// set all items in cat to selection sel, emiting events, actualizing last_selected_abs, but not check for single_selection_ - void categ_selected(size_type cat, bool sel); - - void reverse_categ_selected(size_type categ) - { - categ_selected(categ, ! categ_selected(categ)); - } - /// can be used as the absolute position of the last absolute item, or as the display pos of the last displayed item - index_pair last() const + index_pair last() const noexcept { index_pair i{ categories_.size() - 1, categories_.back().items.size() }; @@ -1902,37 +1769,26 @@ namespace nana return i; } - /// absolute position of the last displayed item - index_pair last_displ() const - { - return index_pair{ absolute(last()) }; - } - /// can be used as the absolute position of the first absolute item, or as the display pos of the first displayed item - index_pair first() const + index_pair first() const noexcept { index_pair fst{0,npos}; good_item(fst,fst); return fst; } - /// absolute position of the first displayed item - index_pair first_displ() const - { - return index_pair{ absolute(first()) }; - } - bool good(size_type cat) const + bool good(size_type cat) const noexcept { return (cat < categories_.size()); } - bool good(const index_pair& pos) const + bool good(const index_pair& pos) const noexcept { return ((pos.cat < categories_.size()) && (pos.item < size_item(pos.cat))); } /// if good return the same item (in arg item), or just the next cat and true, but If fail return false - bool good_item(index_pair pos, index_pair& item) const + bool good_item(const index_pair& pos, index_pair& item) const noexcept { if (!good(pos.cat)) return false; // cat out of range @@ -1953,7 +1809,7 @@ namespace nana return true; } - if (++i == categories_.end()) // item out of range and no more cat + if (++i == categories_.cend()) // item out of range and no more cat return false; item.cat = pos.cat + 1; // select the next cat @@ -1961,59 +1817,13 @@ namespace nana return true; } - ///Translate relative position (position in display) into absolute position (original data order) - size_type absolute(const index_pair& display_pos) const - { - if(sorted_index_ == npos || display_pos.item == npos) - return display_pos.item ; - - auto & catobj = *get(display_pos.cat); - if(catobj.items.empty()) - return (display_pos == index_pair{0,0} ? 0 : npos); - - return (display_pos.item < catobj.sorted.size() ? catobj.sorted[display_pos.item] : npos); - } - - index_pair absolute_pair(const index_pair& display_pos) const - { - //Returns an empty pos if item pos npos - auto item_pos = absolute(display_pos); - return index_pair{item_pos != npos ? display_pos.cat : npos, item_pos}; - } - - ///Translate absolute position (original data order) into relative position (position in display) - size_type relative(const index_pair& pos) const - { - if (sorted_index_ == npos) - return pos.item ; - - auto& catobj = *get(pos.cat); - - for (size_type i=0; iexpand ? cat->items.size() : 0); @@ -2034,7 +1844,7 @@ namespace nana } offs -= items_left; - if (++cat == cat_end) + if (++cat == categories_.cend()) return false; ++from.cat; @@ -2047,41 +1857,33 @@ namespace nana } /// all arg are relative to display order, or all are absolute, but not mixed - bool backward(index_pair from, size_type offs, index_pair& item) const + bool backward(index_pair from, size_type offs, index_pair& item) const noexcept { if(offs == 0) item = from; if(good(from.cat)) { + size_type n = (from.is_category() ? 1 : from.item + 2); auto i = get(from.cat); - size_type n = (from.is_category() ? 1 : from.item + 2); // ?? - - if (n > offs) + while(true) { - n -=offs; - item.cat = from.cat; - item.item = (n == 1 ? npos : n - 2); - return true; - } - - offs -= n; - while (i != categories_.cbegin()) - { - --i; - --from.cat; - - n = (i->expand ? i->items.size() : 0) + 1; - - if(n > offs) + if (n > offs) { - n -=offs; + n -= offs; item.cat = from.cat; item.item = (n == 1 ? npos : n - 2); return true; } - else - offs -= n; + + offs -= n; + + if (i == categories_.cbegin()) + break; + + --i; + --from.cat; + n = (i->expand ? i->items.size() : 0) + 1; } } return false; @@ -2112,9 +1914,9 @@ namespace nana private: essence * ess_{nullptr}; nana::listbox * widget_{nullptr}; - std::size_t sorted_index_{npos}; ///< The index of the column used to sort - bool resort_{true}; - bool sorted_reverse_{false}; + + sort_attributes sort_attrs_; //Attributes of sort + bool ordered_categories_{false}; ///< A switch indicates whether the categories are ordered. /// The ordered categories always creates a new category at a proper position(before the first one which is larger than it). container categories_; @@ -2154,19 +1956,30 @@ namespace nana struct scroll_part { - static const unsigned scale = 16; // ? + static const unsigned scale = 16; //pixels of scrollbar fixed edge - unsigned x_offset() const + unsigned x_offset() const noexcept { return static_cast(h.empty() ? 0 : h.value()); } - index_pair offset_y_abs, offset_y_dpl; //cat stands for category, item stands for item. "item == npos" means that is a category. - // need to be abs??? to see the same item after sort() ?? + std::size_t offset_display{ 0 }; + nana::scroll v; nana::scroll h; }scroll; + struct mouse_selection_part + { + bool started{ false }; + bool reverse_selection{ false }; + + point begin_position; ///< Logical position to the + point end_position; + index_pairs already_selected; + index_pairs selections; + }mouse_selection; + std::map>> inline_table, inline_buffered_table; @@ -2176,15 +1989,7 @@ namespace nana lister.fetch_ordering_comparer = [this](std::size_t pos) -> std::function { - try - { - return header.at(pos).weak_ordering; - } - catch (...) - { - } - - return {}; + return header.at(pos).weak_ordering; }; } @@ -2193,117 +1998,236 @@ namespace nana return header.to_string(exp_opt) + exp_opt.endl + lister.to_string(exp_opt) ; } - const index_pair& scroll_y_abs() const + index_pair first_display() const noexcept { - return scroll.offset_y_abs; - } - const index_pair& scroll_y_dpl() const - { - return scroll.offset_y_dpl; - } - const index_pair& scroll_y_dpl_refresh() - { - return scroll.offset_y_dpl = lister.relative_pair(scroll.offset_y_abs); + return lister.advance(lister.first(), scroll.offset_display); } - void scroll_y_abs(const index_pair& pos_abs) + void first_display(const index_pair& disp_pos) { - if (!lister.good(pos_abs.cat)) - return; + scroll.offset_display = lister.distance(lister.first(), disp_pos); + } - scroll.offset_y_abs.cat = pos_abs.cat; - - size_type number = lister.size_item(pos_abs.cat); - if(pos_abs.item < number) - scroll.offset_y_abs.item = pos_abs.item; - else if(number) - scroll.offset_y_abs.item = number - 1; - else + void first_display_move(long long delta) noexcept + { + if (delta < 0) { - scroll.offset_y_abs.item = (pos_abs.cat > 0 ? npos : 0); - scroll.offset_y_dpl = scroll.offset_y_abs ; - return ; + auto move = static_cast(-delta); + if (move > scroll.offset_display) + scroll.offset_display = 0; + else + scroll.offset_display -= move; } - scroll_y_dpl_refresh() ; + else + scroll.offset_display += static_cast(delta); } - /// directly set a tested relative display pos - void set_scroll_y_dpl(index_pair pos_dpl) + unsigned item_height() const noexcept { - if (lister.first() != pos_dpl) + return (std::max)(scheme_ptr->item_height_ex + scheme_ptr->text_height, unsigned(1)); + } + + point coordinate_cast(const point& from, bool from_screen) noexcept + { + rectangle orignal; + if (!rect_lister(orignal)) + return{}; + + const auto offset_pixels = static_cast(scroll.offset_display * item_height()); + + if (from_screen) { - //check the pos_dpl to make sure the last item is at bottom of listbox - const auto numbers = this->number_of_lister_items(false); - const auto distance = lister.distance(pos_dpl, lister.last()); - if (numbers > 1 && distance < numbers) - lister.backward(lister.last(), numbers - 1, pos_dpl); + auto logic_pos = from - orignal.position(); + logic_pos.x += static_cast(scroll.x_offset()); + logic_pos.y += offset_pixels; + + return logic_pos; } - scroll.offset_y_dpl = pos_dpl; - if (pos_dpl.is_category()) - scroll.offset_y_abs = pos_dpl; - else - scroll.offset_y_abs = lister.absolute_pair(pos_dpl); + auto screen_pos = from + orignal.position(); + screen_pos.x -= static_cast(scroll.x_offset()); + screen_pos.y -= offset_pixels; - if (scroll.offset_y_abs.empty()) - throw std::invalid_argument("nana.listbox.set_scroll_y_dpl's exception is due to invalid item, please report a bug"); + return screen_pos; } - - //number_of_lister_item - /// @brief Returns the number of items that are contained on screen. - /// @param with_rest: Means whether including extra one item that is not completely contained in reset pixels. - size_type number_of_lister_items(bool with_rest) const + void start_mouse_selection(const point& screen_pos) { - unsigned exposed_px = content_area().height - header_visible_px() - (scroll.h.empty() ? 0 : scroll.scale); - auto const item_px = (std::max)(static_castitem_height)>(1), scheme_ptr->item_height); - return (exposed_px / item_px) + (with_rest && (exposed_px % item_px) ? 1 : 0); + auto logic_pos = coordinate_cast(screen_pos, true); + + mouse_selection.started = true; + mouse_selection.begin_position = logic_pos; + mouse_selection.end_position = logic_pos; + + mouse_selection.already_selected = lister.pick_items(true); + + API::set_capture(*listbox_ptr, true); + } + + struct pred_mouse_selection + { + index_pair pos_; + pred_mouse_selection(index_pair pos) noexcept + : pos_(pos) + {} + + bool operator()(const std::pair& m) const noexcept + { + return (pos_ == m.first); + } + }; + + void update_mouse_selection(const point& screen_pos) + { + auto logic_pos = coordinate_cast(screen_pos, true); + mouse_selection.end_position = logic_pos; + + auto begin_off = (std::max)((std::min)(mouse_selection.begin_position.y, mouse_selection.end_position.y), 0) / item_height(); + auto last_off = (std::max)(mouse_selection.begin_position.y, mouse_selection.end_position.y) / item_height(); + + auto begin = lister.advance(lister.first(), begin_off); + auto last = lister.advance(lister.first(), last_off); + + if (!lister.good(last)) + last = lister.last(); + + if (lister.good(begin) && ((mouse_selection.end_position.y < 0) || (lister.distance(lister.first(), begin) == begin_off))) + { + auto selections = lister.select_display_range_if(begin, last, false, [this](const index_pair& abs_pos) { + if (this->mouse_selection.reverse_selection) + { + if(mouse_selection.already_selected.cend() != std::find(mouse_selection.already_selected.cbegin(), mouse_selection.already_selected.cend(), abs_pos)) + { + item_proxy{ this, abs_pos }.select(false); + return false; + } + } + return true; + }); + + for (auto & pair : selections) + { + if (pair.second) + continue; + + if (mouse_selection.selections.cend() == + std::find(mouse_selection.selections.cbegin(), mouse_selection.selections.cend(), pair.first)) + { + mouse_selection.selections.push_back(pair.first); + } + } + + for(auto i = mouse_selection.selections.cbegin(); i != mouse_selection.selections.cend();) + { + if (selections.cend() == std::find_if(selections.cbegin(), selections.cend(), pred_mouse_selection{*i})) + { + item_proxy{ this, *i }.select(false); + i = mouse_selection.selections.erase(i); + } + else + ++i; + } + + if (mouse_selection.reverse_selection) + { + for (auto & abs_pos : mouse_selection.already_selected) + { + if (selections.cend() == std::find_if(selections.cbegin(), selections.cend(), pred_mouse_selection{abs_pos})) + { + item_proxy{ this, abs_pos }.select(true); + } + } + } + } + else + { + for (auto & pos : mouse_selection.selections) + { + item_proxy{ this, pos }.select(false); + } + + mouse_selection.selections.clear(); + + if (mouse_selection.reverse_selection) + { + for (auto & abs_pos : mouse_selection.already_selected) + { + item_proxy{ this, abs_pos }.select(true); + } + } + } + } + + void stop_mouse_selection() noexcept + { + mouse_selection.started = false; + API::release_capture(*listbox_ptr); + mouse_selection.begin_position = mouse_selection.end_position; + mouse_selection.already_selected.clear(); + mouse_selection.selections.clear(); + } + + /// Returns the number of items that are contained on on screen + /** + * @pram with_rest Indicates whether the extra one item which is not completely contained in rest pixels to be included. + * @return The number of items + */ + size_type count_of_exposed(bool with_rest) const + { + unsigned lister_s = graph->height() - 2 - header_visible_px() - (scroll.h.empty() ? 0 : scroll.scale); + return (lister_s / item_height()) + (with_rest && (lister_s % item_height()) ? 1 : 0); } //keep the first selected item in the display area: the distances are in display positions! void trace_item_dpl( index_pair dpl_pos ) { - if( dpl_pos.cat < scroll.offset_y_dpl.cat // in prevoious cat ---------------- up ----> we need to move - || ((dpl_pos.cat == scroll.offset_y_dpl.cat) && ( scroll.offset_y_dpl.item != npos) // is our cat, where we are an item - && (dpl_pos.item == npos || dpl_pos.item < scroll.offset_y_dpl.item))) - // problem!!!!!! + auto first_disp_pos = first_display(); + + if(dpl_pos < first_disp_pos) { if(lister.expand(dpl_pos.cat) == false) { - if(lister.categ_selected(dpl_pos.cat)) + if(lister.cat_status(dpl_pos.cat, true)) dpl_pos.item = static_cast(npos); else lister.expand(dpl_pos.cat, true); } - set_scroll_y_dpl(dpl_pos); // <------------------------- set scroll.offset_y_dpl & scroll.offset_y_abs + + first_display(dpl_pos); } else { - size_type numbers = number_of_lister_items(false); // revise ... ok - size_type off = lister.distance(scroll.offset_y_dpl, dpl_pos); - if(numbers > off) return; - index_pair n_off = lister.advance(scroll.offset_y_dpl, (off - numbers) + 1); + size_type numbers = count_of_exposed(false); // revise ... ok - if(n_off.cat != npos) // <------------------------- set scroll.offset_y_dpl & scroll.offset_y_abs - set_scroll_y_dpl(n_off); + auto off = lister.distance(first_disp_pos, dpl_pos); + + if(numbers > off) return; + + index_pair n_off = lister.advance(first_disp_pos, (off - numbers) + 1); + + if (n_off.cat != npos) + first_display(n_off); } adjust_scroll_life(); // call adjust_scroll_value(); //adjust_scroll_value(); // again? } - void trace_last_selected_item( ) - { + void trace_last_selected_item() + { + auto first_abs = lister.index_cast(first_display(), true); //convert display position to absolute position + if (lister.last_selected_abs.item == npos && - lister.last_selected_abs.cat == scroll.offset_y_abs.cat && - scroll.offset_y_abs.item == npos) // if item==off y and is a cat + lister.last_selected_abs.cat == first_abs.cat && + first_abs.item == npos) // if item==off y and is a cat return; - trace_item_dpl(lister.relative_pair(lister.last_selected_abs)); // ??? scroll_y_dpl_refresh() ; - } + //trace_item_dpl(lister.relative_pair(lister.last_selected_abs)); // ??? scroll_y_dpl_refresh() ; + trace_item_dpl(lister.index_cast(lister.last_selected_abs, false)); //convert absolute position to display position + } - void update() + void update(bool ignore_auto_draw = false) noexcept { - if(auto_draw && lister.wd_ptr()) + if((auto_draw || ignore_auto_draw) && lister.wd_ptr()) { adjust_scroll_life(); API::refresh_window(lister.wd_ptr()->handle()); @@ -2321,14 +2245,13 @@ namespace nana const auto header_px = header.pixels(); const unsigned window_px = graph_size.width - ext_px; - - auto offset_x = scroll.x_offset(); - if (header_px < window_px + offset_x) - offset_x = header_px - window_px; scroll.h.amount(header_px); scroll.h.range(window_px); - scroll.h.value(offset_x); + + const auto offset_x = scroll.x_offset(); + + scroll.h.value(header_px < window_px + offset_x ? header_px - window_px : offset_x); scroll.h.step(graph->text_extent_size(L"W").width); } @@ -2339,22 +2262,22 @@ namespace nana return; const auto items = lister.the_number_of_expanded(); - const auto disp_items = number_of_lister_items(false); + const auto disp_items = count_of_exposed(false); - size_type off = lister.distance(index_pair{ 0, 0 }, scroll.offset_y_dpl); + auto off = scroll.offset_display; if (items < disp_items + off) { index_pair pos; - if (lister.forward(index_pair{ 0, 0 }, items - disp_items, pos)) + if (lister.forward(lister.first(), items - disp_items, pos)) { off = items - disp_items; - set_scroll_y_dpl(pos); + first_display(pos); } } scroll.v.amount(lister.the_number_of_expanded()); - scroll.v.range(number_of_lister_items(false)); + scroll.v.range(count_of_exposed(false)); scroll.v.value(off); } } @@ -2363,31 +2286,42 @@ namespace nana { internal_scope_guard lock; - auto const ctt_area = this->content_area(); + const unsigned border_px = 1; + const unsigned border_px_twice = (border_px << 1); + + const nana::size sz = graph->size(); + + if ((sz.width <= border_px_twice) || (sz.height <= border_px_twice)) + { + scroll.h.close(); + scroll.v.close(); + return; + } // Adjust the ranged column assume the vertical scrollbar is enabled. - auto range_adjusted = this->header.calc_ranged_columns(ctt_area.width - scroll.scale); - auto const columns_pixels = header.pixels(); + auto range_adjusted = this->header.calc_ranged_columns(sz.width - border_px_twice - scroll.scale); + auto columns_pixels = header.pixels(); //H scroll enabled //If range_adjusted is true, it indicates no horzontal scroll bar is enabled. - bool enable_horz = ((!range_adjusted) && (columns_pixels + 2 > ctt_area.width)); // 2px = left and right gaps(2px) + bool enable_horz = ((!range_adjusted) && (columns_pixels + 4 > sz.width)); // 4px = left and right borders(2px) + left and right gaps(2px) unsigned head_scroll = 2 + header_visible_px() + (enable_horz ? scroll.scale : 0); // 2px left and right gaps(2px) - size_type exposed_count = ((ctt_area.height > head_scroll ? ctt_area.height - head_scroll : 0) / scheme_ptr->item_height); + unsigned lister_s = sz.height > head_scroll ? sz.height - head_scroll : 0; + size_type screen_number = (lister_s / item_height()); //V scroll enabled - auto enable_vert = (lister.the_number_of_expanded() > exposed_count); + auto enable_vert = (lister.the_number_of_expanded() > screen_number); if (enable_vert) { if (!enable_horz) - enable_horz = (columns_pixels + scroll.scale > ctt_area.width); + enable_horz = ((columns_pixels + 2 + scroll.scale) > sz.width); } else if (range_adjusted) { //No vertical scrollbar, then re-adjust the range columns for a new width that excludes vert scroll. - this->header.calc_ranged_columns(ctt_area.width); + this->header.calc_ranged_columns(sz.width - border_px_twice); } //event hander for scrollbars @@ -2395,19 +2329,20 @@ namespace nana { if (scroll.h.empty() || (scroll.h.handle() != arg.window_handle)) { - index_pair item; - if (!lister.forward(item, scroll.v.value(), item)) return; - - if (item == scroll.offset_y_dpl) + if (scroll.v.value() == scroll.offset_display) return; - set_scroll_y_dpl(item); + index_pair item; + if (!lister.forward(lister.first(), scroll.v.value(), item)) + return; + + first_display(item); } API::refresh_window(this->lister.wd_ptr()->handle()); }; - auto horz_px = ctt_area.width; + unsigned horz_px = sz.width - border_px_twice; if (enable_vert) { if (horz_px < scroll.scale) @@ -2416,7 +2351,7 @@ namespace nana horz_px -= scroll.scale; } - auto vert_px = ctt_area.height; + unsigned vert_px = sz.height - border_px_twice; if (enable_horz) { if (vert_px < scroll.scale) @@ -2428,7 +2363,7 @@ namespace nana const auto wd_handle = lister.wd_ptr()->handle(); if (enable_horz && horz_px) { - rectangle r(ctt_area.x, ctt_area.bottom() - static_cast(scroll.scale), horz_px, scroll.scale); + rectangle r(border_px, static_cast(sz.height - border_px) - static_cast(scroll.scale), horz_px, scroll.scale); if(scroll.h.empty()) { scroll.h.create(wd_handle, r); @@ -2443,7 +2378,7 @@ namespace nana if (enable_vert && vert_px) { - rectangle r(ctt_area.right() - static_cast(scroll.scale), ctt_area.y, scroll.scale, vert_px); + rectangle r(static_cast(sz.width - border_px) - static_cast(scroll.scale), border_px, scroll.scale, vert_px); if(scroll.v.empty()) { scroll.v.create(wd_handle, r); @@ -2457,27 +2392,15 @@ namespace nana else if(!scroll.v.empty()) { scroll.v.close(); - set_scroll_y_dpl(index_pair{0,0}); + first_display(lister.first()); + } adjust_scroll_value(); } - void set_auto_draw(bool ad) + nana::rectangle checkarea(int x, int y) const noexcept /// move to scheme ?? 16 ? { - if(auto_draw != ad) - { - auto_draw = ad; - if(ad) - { - adjust_scroll_life(); - API::refresh_window(lister.wd_ptr()->handle()); - } - } - } - - nana::rectangle checkarea(int x, int y) const /// move to scheme ?? 16 ? - { - return nana::rectangle(x + 4, y + (static_cast(scheme_ptr->item_height) - 16) / 2, 16, 16); + return nana::rectangle(x + 4, y + (static_cast(item_height()) - 16) / 2, 16, 16); } int item_xpos(const nana::rectangle& r) const @@ -2490,30 +2413,30 @@ namespace nana return (header.position(seq[0], nullptr) - static_cast(scroll.x_offset()) + r.x); } - std::pair where(int x, int y) + std::pair where(const nana::point& pos) const noexcept { - std::pair new_where; + std::pair new_where; - if(2 < x && x < static_cast(graph->width()) - 2 && 1 < y && y < static_cast(graph->height()) - 1) + if(2 < pos.x && pos.x < static_cast(graph->width()) - 2 && 1 < pos.y && pos.y < static_cast(graph->height()) - 1) { /// we are inside - if(header.visible() && y < static_cast(scheme_ptr->header_height + 1)) + if(header.visible() && pos.y < static_cast(scheme_ptr->header_height + 1)) { /// we are in the header - x += static_cast(scroll.x_offset()) - 2; new_where.first = parts::header; - new_where.second = header.column_from_point(x); + new_where.second = header.column_from_point(pos.x + static_cast(scroll.x_offset()) - 2); } else { - new_where.second = ((y + 1) - header_visible_px()) / scheme_ptr->item_height; // y>1 ! new_where.first = parts::lister; + new_where.second = (pos.y + 1 - header_visible_px()) / item_height(); // y>1 ! + if(checkable) { nana::rectangle r; if(rect_lister(r)) { - auto top = new_where.second * scheme_ptr->item_height + header_visible_px(); - if(checkarea(item_xpos(r), static_cast(top)).is_hit(x, y)) + auto top = new_where.second * item_height() + header_visible_px(); + if(checkarea(item_xpos(r), static_cast(top)).is_hit(pos)) new_where.first = parts::checker; } } @@ -2524,12 +2447,12 @@ namespace nana new_where.first = parts::unknown; new_where.second = npos; } - return new_where; + return new_where; } - bool calc_where(const point& pos) + bool calc_where(const point& pos) noexcept { - auto new_where = where(pos.x, pos.y); + auto new_where = where(pos); if (new_where == pointer_where) return false; @@ -2537,96 +2460,66 @@ namespace nana return true; } - void widget_to_header(nana::point& pos) + void widget_to_header(nana::point& pos) noexcept { --pos.y; - pos.x += static_cast(scroll.x_offset()) - 2; } - void draw_peripheral() - { - auto ctt_area = this->content_area(); - - if (!API::widget_borderless(*lister.wd_ptr())) - { - //Draw Border - graph->rectangle(false, static_cast(0x9cb6c5)); - - graph->line({ ctt_area.x, ctt_area.y }, { ctt_area.x, ctt_area.bottom() - 1 }, colors::white); - graph->line({ ctt_area.right() - 1, ctt_area.y }, { ctt_area.right() - 1, ctt_area.bottom() - 1 }); - - } - - if ((scroll.h.empty() == false) && (scroll.v.empty() == false)) - { - graph->rectangle({ ctt_area.right() - static_cast(scroll.scale), - ctt_area.bottom() - static_cast(scroll.scale), - scroll.scale, - scroll.scale }, - true, colors::button_face); - } - } - - rectangle content_area() const - { - rectangle r{ graph->size() }; - - if (!this->listbox_ptr->borderless()) - { - r.x = 1; - r.width -= (r.width > 2 ? 2 : r.width); - - r.y = 1; - r.height -= (r.height > 2 ? 2 : r.height); - } - return r; - } - - unsigned header_visible_px() const - { - return (header.visible() ? scheme_ptr->header_height : 0); - } - - bool rect_header(nana::rectangle& r) const + bool rect_header(nana::rectangle& r) const noexcept { if(header.visible()) { - r = this->content_area(); - - r.height = scheme_ptr->header_height; - if (lister.wd_ptr()->borderless()) - return !r.empty(); - - const unsigned ex_width = 2 + (scroll.v.empty() ? 0 : scroll.scale - 1); - if(r.width > ex_width) { - r.x += 1; - r.width -= ex_width; + r.dimension(graph->size()); + r.height = scheme_ptr->header_height; + return !r.empty(); + } + + const unsigned ex_width = 4 + (scroll.v.empty() ? 0 : scroll.scale - 1); + if(graph->width() > ex_width) + { + r.x = 2; + r.y = 1; + r.width = graph->width() - ex_width; + r.height = scheme_ptr->header_height; return true; } } return false; } - bool rect_lister(nana::rectangle& r) const + unsigned header_visible_px() const noexcept + { + return (header.visible() ? scheme_ptr->header_height : 0); + } + + bool rect_lister(nana::rectangle& r) const noexcept { auto head_pixels = header_visible_px(); - unsigned extr_w = (scroll.v.empty() ? 0 : scroll.scale - 1); - unsigned extr_h = (scroll.h.empty() ? 0 : scroll.scale) + head_pixels; + unsigned width = (scroll.v.empty() ? 0 : scroll.scale - 1); + unsigned height = (scroll.h.empty() ? 0 : scroll.scale) + head_pixels; - r = this->content_area(); - r.y += head_pixels; if (!lister.wd_ptr()->borderless()) { - extr_w += 2; - r.x += 1; + width += 4; + height += 2; + + r.x = 2; + r.y = head_pixels + 1; + } + else + { + r.x = 0; + r.y = head_pixels; } - if(r.width <= extr_w || r.height <= extr_h) - return false; + nana::size gsz = graph->size(); + if(gsz.width <= width || gsz.height <= height) return false; + r.width = gsz.width - width; + r.height = gsz.height - height; return true; } @@ -2635,23 +2528,25 @@ namespace nana if(scroll.v.empty() || !scroll.v.scrollable(upwards)) return false; + auto first_pos = first_display(); + index_pair target; if (upwards == false) { - if (!lister.forward(scroll.offset_y_dpl, this->scheme_ptr->mouse_wheel.lines, target)) + if (!lister.forward(first_pos, this->scheme_ptr->mouse_wheel.lines, target)) return false; } else - lister.backward(scroll.offset_y_dpl, this->scheme_ptr->mouse_wheel.lines, target); + lister.backward(first_pos, this->scheme_ptr->mouse_wheel.lines, target); - if (target == scroll.offset_y_dpl) + if (target == first_pos) return false; - set_scroll_y_dpl ( target ); + first_display(target); return true; } - std::vector header_seq(unsigned lister_w)const + std::vector header_seq(unsigned lister_w) const { std::vector seqs; int x = -static_cast(scroll.x_offset()); @@ -2702,7 +2597,7 @@ namespace nana }; //definition of iresolver/oresolver - oresolver::oresolver(essence* ess) + oresolver::oresolver(essence* ess) noexcept : ess_(ess) {} @@ -2819,12 +2714,12 @@ namespace nana return *this; } - std::vector&& oresolver::move_cells() + std::vector&& oresolver::move_cells() noexcept { return std::move(cells_); } - ::nana::listbox& oresolver::listbox() + ::nana::listbox& oresolver::listbox() noexcept { return *ess_->listbox_ptr; } @@ -2890,6 +2785,7 @@ namespace nana n = std::stoull(cells_[pos_++].text); return *this; } + iresolver& iresolver::operator>>(float& f) { if (pos_ < cells_.size()) @@ -2897,7 +2793,6 @@ namespace nana return *this; } - iresolver& iresolver::operator>>(double& f) { if (pos_ < cells_.size()) @@ -2927,7 +2822,7 @@ namespace nana return *this; } - iresolver::iresolver(std::vector cl) + iresolver::iresolver(std::vector cl) noexcept : cells_(std::move(cl)) {} @@ -2938,7 +2833,7 @@ namespace nana return *this; } - iresolver& iresolver::operator>>(std::nullptr_t) + iresolver& iresolver::operator>>(std::nullptr_t) noexcept { ++pos_; return *this; @@ -2950,40 +2845,36 @@ namespace nana unsigned max_px = 0; for (auto & cat : categories_) { - if (cat.model_ptr) + for (std::size_t i = 0; i < cat.items.size(); ++i) { - for (std::size_t i = 0; i < cat.items.size(); ++i) + unsigned content_px = 0; + if (cat.model_ptr) { auto model_cells = cat.model_ptr->container()->to_cells(i); if (pos >= model_cells.size()) continue; - auto content_px = ess_->graph->text_extent_size(model_cells[pos].text).width; - if (content_px > max_px) - max_px = content_px; + content_px = ess_->graph->text_extent_size(model_cells[pos].text).width; } - } - else - { - for (auto & m : cat.items) + else { - if (pos >= m.cells->size()) + if (pos >= cat.items[i].cells->size()) continue; - auto content_px = ess_->graph->text_extent_size((*m.cells)[pos].text).width; - if (content_px > max_px) - max_px = content_px; + content_px = ess_->graph->text_extent_size((*cat.items[i].cells)[pos].text).width; } + + if (content_px > max_px) + max_px = content_px; } } return max_px; } //es_header::column member functions - void es_header::column::_m_refresh() + void es_header::column::_m_refresh() noexcept { - ess_->adjust_scroll_life(); - API::refresh_window(ess_->lister.wd_ptr()->handle()); + ess_->update(true); } void es_header::column::fit_content(unsigned maximize) noexcept @@ -3045,7 +2936,7 @@ namespace nana panes_.emplace_back(std::make_pair(pos, pane)); } - void detach() + void detach() noexcept { panes_.clear(); } @@ -3066,7 +2957,7 @@ namespace nana ess_->lister.throw_if_immutable_model(pos); auto model_cells = ess_->lister.at_model_abs(pos); - auto & cells = ess_->lister.have_model(pos) ? model_cells : (*ess_->lister.at_abs(pos).cells); + auto & cells = ess_->lister.has_model(pos) ? model_cells : (*ess_->lister.at_abs(pos).cells); if (cells.size() <= column_pos_) cells.resize(column_pos_ + 1); @@ -3092,8 +2983,7 @@ namespace nana void hovered(index_type pos) override { - auto offset = ess_->lister.distance(ess_->scroll.offset_y_dpl, pos); - + auto offset = ess_->lister.distance(ess_->first_display(), pos); if (ess_->pointer_where.first != parts::lister || ess_->pointer_where.second != offset) { ess_->pointer_where.first = parts::lister; @@ -3121,7 +3011,7 @@ namespace nana } //The number of items can be displayed on screen - auto view_items = ess_->number_of_lister_items(false) - 1; + auto view_items = ess_->count_of_exposed(false) - 1; index_pair start_pos; if (to_bottom) @@ -3129,29 +3019,21 @@ namespace nana //start_pos will be (0,0) if backward fails backward(pos, view_items, start_pos); } + else if (forward(pos, view_items, start_pos)) + { + start_pos = pos; + } else { - if (forward(pos, view_items, start_pos)) - start_pos = pos; - else - { - index_pair last(categories_.size() - 1); + index_pair last(categories_.size() - 1, ::nana::npos); - if (categories_.back().expand) - { - if (categories_.back().items.empty()) - last.item = nana::npos; - else - last.item = categories_.back().items.size() - 1; - } - else - last.item = nana::npos; + if (categories_.back().expand && (!categories_.back().items.empty())) + last.item = categories_.back().items.size() - 1; - backward(last, view_items, start_pos); - } + backward(last, view_items, start_pos); } - ess_->set_scroll_y_dpl(start_pos); + ess_->first_display(start_pos); ess_->adjust_scroll_value(); } @@ -3173,14 +3055,10 @@ namespace nana } } - void es_lister::scroll_refresh() + void es_lister::move_select(bool upwards, bool unselect_previous, bool trace_selected) noexcept { - ess_->scroll_y_dpl_refresh(); - } + auto next_selected_dpl = index_cast_noexcpt(last_selected_abs, false); //convert absolute position to display position - void es_lister::move_select(bool upwards, bool unselect_previous, bool trace_selected) - { - auto next_selected_dpl = relative_pair ( last_selected_abs); if (next_selected_dpl.empty()) // has no cat ? (cat == npos) => beging from first cat { bool good = false; @@ -3212,7 +3090,7 @@ namespace nana else { next_selected_dpl.item = 0; - if (size_categ() > next_selected_dpl.cat + 1) + if (categories_.size() > next_selected_dpl.cat + 1) ++next_selected_dpl.cat; else next_selected_dpl.cat = 0; @@ -3229,7 +3107,7 @@ namespace nana do { if (0 == next_selected_dpl.cat) - next_selected_dpl.cat = size_categ() - 1; + next_selected_dpl.cat = categories_.size() - 1; else --next_selected_dpl.cat; @@ -3247,14 +3125,14 @@ namespace nana if (good(next_selected_dpl)) { - if (unselect_previous && !single_selection_ ) - select_for_all(false); + if (unselect_previous && !single_selection_) + select_for_all(false); - /// is ignored if no change (maybe set last_selected anyway??), but if change emit event, deselect others if need ans set/unset last_selected - item_proxy::from_display(ess_, next_selected_dpl).select(true); + /// is ignored if no change (maybe set last_selected anyway??), but if change emit event, deselect others if need ans set/unset last_selected + item_proxy::from_display(ess_, next_selected_dpl).select(true); - if (trace_selected) - ess_->trace_item_dpl(next_selected_dpl); + if (trace_selected) + ess_->trace_item_dpl(next_selected_dpl); } break; } @@ -3293,15 +3171,37 @@ namespace nana return list_str ; } - void es_lister::categ_selected(size_type cat, bool sel) + bool es_lister::cat_status(size_type pos, bool for_selection, bool value) { - cat_proxy cpx{ess_,cat}; - for (item_proxy &it : cpx ) - { - if (it.selected() != sel) - it.select(sel); - } - last_selected_abs = index_pair{cat, npos}; + bool changed = false; + + if (for_selection) + { + cat_proxy cpx{ ess_, pos }; + for (item_proxy &it : cpx) + it.select(value); + + last_selected_abs.cat = pos; + last_selected_abs.item = npos; + + return true; + } + else + { + auto & items = get(pos)->items; + size_type index = 0; + for (auto & m : items) + { + if (m.flags.checked != value) + { + m.flags.checked = value; + this->emit_cs(index_pair{ pos, index }, false); + changed = true; + } + ++index; + } + } + return changed; } class drawer_header_impl @@ -3311,20 +3211,20 @@ namespace nana using item_state = essence::item_state; using parts = essence::parts; - drawer_header_impl(essence* es): essence_(es){} + drawer_header_impl(essence* es) noexcept: essence_(es){} - size_type splitter() const + size_type splitter() const noexcept { return grabs_.splitter; } - void cancel_splitter() + void cancel_splitter() noexcept { grabs_.splitter = npos; } // Detects a header spliter, return true if x is in the splitter area after that header item (column) - bool detect_splitter(const nana::rectangle& r, int x) + bool detect_splitter(const nana::rectangle& r, int x) noexcept { if(essence_->ptr_state == item_state::highlighted) { @@ -3359,7 +3259,7 @@ namespace nana if(grabs_.splitter != npos) // resize header item, not move it grabs_.item_width = essence_->header.at(grabs_.splitter).width_px; } - else if(grab_terminal_.index != npos && grab_terminal_.index != essence_->pointer_where.second) + else if((grab_terminal_.index != npos) && (grab_terminal_.index != essence_->pointer_where.second)) essence_->header.move(essence_->pointer_where.second, grab_terminal_.index, grab_terminal_.place_front); } @@ -3414,7 +3314,6 @@ namespace nana int text_top = (r.height - essence_->scheme_ptr->text_height) / 2 + r.y; auto text_color = essence_->scheme_ptr->header_fgcolor.get_color(); - auto state = item_state::normal; //check whether grabing an item, if item_spliter_ != npos, that indicates the grab item is a spliter. @@ -3438,7 +3337,7 @@ namespace nana if (right_pos > r.x) { _m_draw_header_item(graph, column_r, text_top, text_color, col, (col.index == essence_->pointer_where.second ? state : item_state::normal)); - graph.line({ right_pos - 1, r.y }, { right_pos - 1, r.bottom() - 2 }, /*_m_border_color()*/ border_color); + graph.line({ right_pos - 1, r.y }, { right_pos - 1, r.bottom() - 2 }, border_color); } column_r.x = right_pos; @@ -3447,7 +3346,7 @@ namespace nana } } - //If the last rendered column's right is less than r.right, fill the spare space. + //If the last rendered column's right point doesn't reach at r.right, fill the spare space. if (column_r.x < r.right()) { column_r.width = (r.right() - column_r.x); @@ -3455,7 +3354,7 @@ namespace nana } const int y = r.bottom() - 1; - graph.line({ r.x, y }, { r.right(), y }, /*_m_border_color()*/ border_color); + graph.line({ r.x, y }, { r.right(), y }, border_color); if (options_.grab_column) { @@ -3478,10 +3377,10 @@ namespace nana x = x_offset + static_cast(rect.width); auto i = essence_->header.column_from_point(x); + if(i == npos) - { - i = (essence_->header.position(grab, nullptr) < x ? essence_->header.last() : essence_->header.begin()); - } + i = essence_->header.boundary(essence_->header.position(grab, nullptr) >= x); + if(grab != i) { unsigned item_pixels = 0; @@ -3490,7 +3389,7 @@ namespace nana //Get the item pos //if mouse pos is at left of an item middle, the pos of itself otherwise the pos of the next. place_front = (x <= (item_x + static_cast(item_pixels / 2))); - x = (place_front ? item_x : essence_->header.position(essence_->header.neighbor(i, false), nullptr)); + x = (place_front ? item_x : essence_->header.position(essence_->header.next(i), nullptr)); if (npos != i) essence_->graph->rectangle({x - x_offset + rect.x, rect.y, 2, rect.height}, true, colors::red); @@ -3524,18 +3423,26 @@ namespace nana point text_pos{ column_r.x, text_top }; - if (align::left == column.alignment) + switch (column.alignment) + { + case align::left: text_pos.x += text_margin; - else if (align::center == column.alignment) + break; + case align::center: text_margin = 0; + default: + break; + } text_aligner.draw(column.text, text_pos, column_r.width - text_margin); } - if (column.index == essence_->lister.sort_index()) + auto & sort = essence_->lister.sort_attrs(); + + if (column.index == sort.column) { facade arrow("hollow_triangle"); - arrow.direction(essence_->lister.sort_reverse() ? ::nana::direction::south : ::nana::direction::north); + arrow.direction(sort.reverse ? ::nana::direction::south : ::nana::direction::north); arrow.draw(graph, {}, colors::black, { column_r.x + (static_cast(column_r.width) - 16) / 2, -4, 16, 16 }, element_state::normal); // geometric scheme? } } @@ -3557,7 +3464,7 @@ namespace nana } private: - essence * essence_; + essence * const essence_; struct grab_variables { @@ -3587,7 +3494,7 @@ namespace nana using parts = essence::parts; using status_type = inline_notifier_interface::status_type; - drawer_lister_impl(essence * es) + drawer_lister_impl(essence * es) noexcept :essence_(es) {} @@ -3598,14 +3505,9 @@ namespace nana //clear active panes essence_->lister.append_active_panes(nullptr); - //The count of items to be drawn - auto item_count = essence_->number_of_lister_items(true); - if (0 == item_count) - return; - widget * wdptr = essence_->lister.wd_ptr(); - auto bgcolor = wdptr->bgcolor(); - auto fgcolor = wdptr->fgcolor(); + const auto bgcolor = wdptr->bgcolor(); + const auto fgcolor = wdptr->fgcolor(); essence_->graph->palette(false, bgcolor); @@ -3616,114 +3518,137 @@ namespace nana size{rect.width + x_offset - header_w, rect.height} }, true); es_lister & lister = essence_->lister; - //The Tracker indicates the item where mouse placed. - index_pair tracker(npos, npos); + auto & ptr_where = essence_->pointer_where; - //if where == lister || where == checker, 'second' indicates the offset to the relative display-order pos of the scroll offset_y which stands for the first item to be displayed in lister. - if((ptr_where.first == parts::lister || ptr_where.first == parts::checker) && ptr_where.second != npos) - lister.forward(essence_->scroll.offset_y_dpl, ptr_where.second, tracker); + int item_top = rect.y; - auto subitems = essence_->header_seq(rect.width); + auto first_disp = essence_->first_display(); - if(subitems.empty()) - return; - - int x = essence_->item_xpos(rect); - int y = rect.y; - int txtoff = (essence_->scheme_ptr->item_height - essence_->scheme_ptr->text_height) / 2; - - auto i_categ = lister.get(essence_->scroll.offset_y_dpl.cat); - - auto idx = essence_->scroll.offset_y_dpl; - - auto state = item_state::normal; - - essence_->inline_buffered_table.swap(essence_->inline_table); - - for(auto & cat : lister.cat_container()) - for (auto & ind : cat.indicators) - { - if (ind) - ind->detach(); - } - - //Here we draw the root categ (0) or a first item if the first drawing is not a categ.(item!=npos)) - if(idx.cat == 0 || !idx.is_category()) + // The first display is empty when the listbox is empty. + if (!first_disp.empty()) { - if (idx.cat == 0 && idx.is_category()) // the 0 cat + + index_pair hoverred_pos(npos, npos); //the hoverred item. + + //if where == lister || where == checker, 'second' indicates the offset to the relative display-order pos of the scroll offset_y which stands for the first item to be displayed in lister. + if ((ptr_where.first == parts::lister || ptr_where.first == parts::checker) && ptr_where.second != npos) + lister.forward(first_disp, ptr_where.second, hoverred_pos); + + auto subitems = essence_->header_seq(rect.width); + + if (subitems.empty()) + return; + + int txtoff = (essence_->item_height() - essence_->scheme_ptr->text_height) / 2; + + auto i_categ = lister.get(first_disp.cat); + + auto idx = first_disp; + + essence_->inline_buffered_table.swap(essence_->inline_table); + + for (auto & cat : lister.cat_container()) + for (auto & ind : cat.indicators) + { + if (ind) + ind->detach(); + } + + const int x = essence_->item_xpos(rect); + + //Here we draw the root categ (0) or a first item if the first drawing is not a categ.(item!=npos)) + if (idx.cat == 0 || !idx.is_category()) { - essence_->scroll.offset_y_dpl.item = 0; // no, we draw the first item of cat 0, not the 0 cat itself - idx.item = 0; + if (idx.cat == 0 && idx.is_category()) // the 0 cat + { + first_disp.item = 0; + essence_->first_display(first_disp); + + idx.item = 0; + } + + std::size_t size = i_categ->items.size(); + for (std::size_t offs = first_disp.item; offs < size; ++offs, ++idx.item) + { + if (item_top >= rect.bottom()) + break; + + auto item_pos = lister.index_cast(index_pair{ idx.cat, offs }, true); //convert display position to absolute position + + _m_draw_item(*i_categ, item_pos, x, item_top, txtoff, header_w, rect, subitems, bgcolor, fgcolor, + (hoverred_pos == idx ? item_state::highlighted : item_state::normal) + ); + + item_top += essence_->item_height(); + } + + ++i_categ; + ++idx.cat; } - std::size_t size = i_categ->items.size(); - index_pair item_index{ idx.cat, 0 }; - - for (std::size_t offs = essence_->scroll.offset_y_dpl.item; offs < size; ++offs, ++idx.item) - { - if (0 == item_count--) - break; - - state = (tracker == idx ? item_state::highlighted : item_state::normal); - - item_index.item = offs; - item_index = lister.absolute_pair(item_index); - - _m_draw_item(*i_categ, item_index, x, y, txtoff, header_w, rect, subitems, bgcolor,fgcolor, state); - y += essence_->scheme_ptr->item_height; - } - - ++i_categ; - ++idx.cat; - } - - if (item_count > 0) - { for (; i_categ != lister.cat_container().end(); ++i_categ, ++idx.cat) { - if (0 == item_count--) + if (item_top > rect.bottom()) break; idx.item = 0; - state = (tracker.is_category() && (idx.cat == tracker.cat) ? item_state::highlighted : item_state::normal); - - _m_draw_categ(*i_categ, rect.x - static_cast(x_offset), y, txtoff, header_w, rect, bgcolor, state); - y += essence_->scheme_ptr->item_height; + _m_draw_categ(*i_categ, rect.x - static_cast(x_offset), item_top, txtoff, header_w, rect, bgcolor, + (hoverred_pos.is_category() && (idx.cat == hoverred_pos.cat) ? item_state::highlighted : item_state::normal) + ); + item_top += essence_->item_height(); if (false == i_categ->expand) continue; - if (item_count > 0) + auto size = i_categ->items.size(); + for (decltype(size) pos = 0; pos < size; ++pos) { - auto size = i_categ->items.size(); - index_pair item_pos{ idx.cat, 0 }; - for (decltype(size) pos = 0; pos < size; ++pos) - { - if (0 == item_count--) - break; + if (item_top > rect.bottom()) + break; - state = (idx == tracker ? item_state::highlighted : item_state::normal); + auto item_pos = lister.index_cast(index_pair{ idx.cat, pos }, true); //convert display position to absolute position - item_pos.item = pos; - item_pos.item = lister.absolute(item_pos); + _m_draw_item(*i_categ, item_pos, x, item_top, txtoff, header_w, rect, subitems, bgcolor, fgcolor, + (idx == hoverred_pos ? item_state::highlighted : item_state::normal) + ); - _m_draw_item(*i_categ, item_pos, x, y, txtoff, header_w, rect, subitems, bgcolor, fgcolor, state); - y += essence_->scheme_ptr->item_height; - if (y >= rect.bottom()) - break; + item_top += essence_->item_height(); + if (item_top >= rect.bottom()) + break; - ++idx.item; - } + ++idx.item; } } + + essence_->inline_buffered_table.clear(); } - essence_->inline_buffered_table.clear(); + if (item_top < rect.bottom()) + essence_->graph->rectangle(rectangle{ rect.x, item_top, rect.width, static_cast(rect.bottom() - item_top) }, true, bgcolor); - if (y < rect.bottom()) - essence_->graph->rectangle(rectangle{ rect.x, y, rect.width, static_cast(rect.bottom() - y) }, true, bgcolor); + //Draw mouse selection + //Check if the mouse selection box is present. + if (essence_->mouse_selection.begin_position != essence_->mouse_selection.end_position) + { + rectangle box{ + point{ + std::min(essence_->mouse_selection.begin_position.x, essence_->mouse_selection.end_position.x), + std::min(essence_->mouse_selection.begin_position.y, essence_->mouse_selection.end_position.y) + }, + size{ + static_cast(std::abs(essence_->mouse_selection.begin_position.x - essence_->mouse_selection.end_position.x)), + static_cast(std::abs(essence_->mouse_selection.begin_position.y - essence_->mouse_selection.end_position.y)) + } + }; + + box.position(essence_->coordinate_cast(box.position(), false)); + + rectangle visual_box; + if(overlap(box, rect, visual_box)) + essence_->graph->rectangle(visual_box, false, colors::blue); + } } private: void _m_draw_categ(const category_t& categ, int x, int y, int txtoff, unsigned width, const nana::rectangle& r, nana::color bgcolor, item_state state) const @@ -3735,15 +3660,17 @@ namespace nana if (state == item_state::highlighted) bgcolor = bgcolor.blend(static_cast(0x99defd), 0.8); + const auto item_height = essence_->item_height(); + auto graph = essence_->graph; - graph->rectangle(rectangle{ x, y, width, essence_->scheme_ptr->item_height }, true, bgcolor); + graph->rectangle(rectangle{ x, y, width, item_height }, true, bgcolor); color txt_color{ static_cast(0x3399) }; facade arrow("double"); arrow.direction(categ.expand ? ::nana::direction::north : ::nana::direction::south); arrow.draw( *graph, {}, txt_color, - { x + 5, y + static_cast(essence_->scheme_ptr->item_height - 16) / 2, 16, 16 }, + { x + 5, y + static_cast(item_height - 16) / 2, 16, 16 }, element_state::normal); graph->string({ x + 20, y + txtoff }, categ.text, txt_color); @@ -3755,21 +3682,23 @@ namespace nana graph->string({ x + 25 + static_cast(text_s), y + txtoff }, str); - if (x + 35 + static_cast(extend_text_w) < x + static_cast(width)) + if (35 + extend_text_w < width) { - ::nana::point pos{ x + 30 + static_cast(extend_text_w), y + static_cast(essence_->scheme_ptr->item_height) / 2 }; - graph->line(pos, { x + static_cast(width)-5, pos.y }, txt_color); + ::nana::point pos{ x + 30 + static_cast(extend_text_w), y + static_cast(item_height) / 2 }; + + graph->line(pos, { x + static_cast(width) - 5, pos.y }, + txt_color); } //Draw selecting inner rectangle if (sel && (categ.expand == false)) { - _m_draw_item_border(r.x, y, (std::min)(r.width, width - essence_->scroll.x_offset())); + _m_draw_border(r.x, y, (std::min)(r.width, width - essence_->scroll.x_offset())); } } /// Draws an item - void _m_draw_item(const category_t& cat, + void _m_draw_item(const category_t& cat, const index_pair& item_pos, const int x, ///< left coordinate ? const int y, ///< top coordinate @@ -3805,15 +3734,15 @@ namespace nana if (item.flags.selected) bgcolor = bgcolor.blend(colors::black, 0.98); // or "selected" else - bgcolor = bgcolor.blend(essence_->scheme_ptr->item_highlighted, 0.7); /// \todo create a parametre for amount of blend + bgcolor = bgcolor.blend(essence_->scheme_ptr->item_highlighted, 0.7);/// \todo create a parametre for amount of blend } - unsigned show_w = (std::min)(content_r.width, width - essence_->scroll.x_offset()); + const unsigned show_w = (std::min)(content_r.width, width - essence_->scroll.x_offset()); auto graph = essence_->graph; //draw the background for the whole item - graph->rectangle(rectangle{ content_r.x, y, show_w, essence_->scheme_ptr->item_height }, true, bgcolor); + graph->rectangle(rectangle{ content_r.x, y, show_w, essence_->item_height() }, true, bgcolor); int column_x = x; @@ -3859,7 +3788,7 @@ namespace nana { nana::rectangle img_r(item.img_show_size); img_r.x = content_pos + column_x + (16 - static_cast(item.img_show_size.width)) / 2; // center in 16 - geom scheme? - img_r.y = y + (static_cast(essence_->scheme_ptr->item_height) - static_cast(item.img_show_size.height)) / 2; // center + img_r.y = y + (static_cast(essence_->item_height()) - static_cast(item.img_show_size.height)) / 2; // center item.img.stretch(rectangle{ item.img.size() }, *graph, img_r); } content_pos += 18; // image width, geom scheme? @@ -3873,15 +3802,17 @@ namespace nana auto inline_wdg = _m_get_inline_pane(cat, column_pos); if (inline_wdg) { - //Make sure the user-define inline widgets in right visible rectangle. + //Make sure the user-define inline widgets is in the right visible rectangle. rectangle pane_r; - auto wdg_x = column_x + content_pos; - auto wdg_w = col.width_px - static_cast(content_pos); + + const auto wdg_x = column_x + content_pos; + const auto wdg_w = col.width_px - static_cast(content_pos); bool visible_state = true; - if (::nana::overlap(content_r, { wdg_x, y, wdg_w, essence_->scheme_ptr->item_height }, pane_r)) + if (::nana::overlap(content_r, { wdg_x, y, wdg_w, essence_->item_height() }, pane_r)) { ::nana::point pane_pos; + if (wdg_x < content_r.x) pane_pos.x = wdg_x - content_r.x; @@ -3894,14 +3825,13 @@ namespace nana else visible_state = false; - draw_column = inline_wdg->inline_ptr->whether_to_draw(); inline_wdg->item_pos = item_pos; inline_wdg->column_pos = column_pos; inline_wdg->inline_ptr->activate(*inline_wdg->indicator, item_pos); - ::nana::size sz{ wdg_w, essence_->scheme_ptr->item_height }; + ::nana::size sz{ wdg_w, essence_->item_height() }; inline_wdg->pane_widget.size(sz); inline_wdg->inline_ptr->resize(sz); @@ -3929,7 +3859,7 @@ namespace nana auto & m_cell = cells[column_pos]; review_utf8(m_cell.text); - if (m_cell.custom_format && (!m_cell.custom_format->bgcolor.invisible())) // adapt to costum format if need + if (m_cell.custom_format && (!m_cell.custom_format->bgcolor.invisible())) // adapt to custom format if need { it_bgcolor = m_cell.custom_format->bgcolor; if (item.flags.selected) @@ -3937,7 +3867,7 @@ namespace nana if (item_state::highlighted == state) it_bgcolor = it_bgcolor.blend(static_cast(0x99defd), 0.8); - graph->rectangle(rectangle{ column_x, y, col.width_px, essence_->scheme_ptr->item_height }, true, it_bgcolor); + graph->rectangle(rectangle{ column_x, y, col.width_px, essence_->item_height() }, true, it_bgcolor); cell_txtcolor = m_cell.custom_format->fgcolor; } @@ -3957,7 +3887,7 @@ namespace nana } } - graph->line({ column_x - 1, y }, { column_x - 1, y + static_cast(essence_->scheme_ptr->item_height) - 1 }, static_cast(0xEBF4F9)); + graph->line({ column_x - 1, y }, { column_x - 1, y + static_cast(essence_->item_height()) - 1 }, static_cast(0xEBF4F9)); } column_x += col.width_px; @@ -3965,7 +3895,7 @@ namespace nana //Draw selecting inner rectangle if(item.flags.selected) - _m_draw_item_border(content_r.x, y, show_w); + _m_draw_border(content_r.x, y, show_w); } inline_pane * _m_get_inline_pane(const category_t& cat, std::size_t column_pos) const @@ -3981,33 +3911,10 @@ namespace nana return nullptr; } - inline_pane* _m_find_inline_pane(const index_pair& pos, std::size_t column_pos) const - { - auto & cat = *essence_->lister.get(pos.cat); - - if (column_pos >= cat.factories.size()) - return nullptr; - - auto& factory = cat.factories[column_pos]; - if (!factory) - return nullptr; - - auto i = essence_->inline_table.find(factory.get()); - if (i == essence_->inline_table.end()) - return nullptr; - - for (auto & inl_widget : i->second) - { - if (inl_widget->item_pos == pos && inl_widget->column_pos == column_pos) - return inl_widget.get(); - } - return nullptr; - } - - void _m_draw_item_border(int x, int y, unsigned width) const + void _m_draw_border(int x, int y, unsigned width) const { //Draw selecting inner rectangle - rectangle r{ x, y, width, essence_->scheme_ptr->item_height }; + rectangle r{ x, y, width, essence_->item_height() }; essence_->graph->rectangle(r, false, static_cast(0x99defd)); @@ -4035,11 +3942,35 @@ namespace nana delete essence_; } - essence& trigger::ess() const + essence& trigger::ess() const noexcept { return *essence_; } + void trigger::_m_draw_border() + { + if (API::widget_borderless(*essence_->lister.wd_ptr())) + return; + + auto & graph = *essence_->graph; + + int right = static_cast(graph.width()) - 1; + int bottom = static_cast(graph.height()) - 1; + + //Draw Border + graph.rectangle(false, static_cast(0x9cb6c5)); + + graph.line({ 1, 1 }, { 1, bottom - 1}, colors::white); + graph.line({ right - 1, 1 }, { right - 1, bottom - 1 }); + + if ((essence_->scroll.h.empty() == false) && (essence_->scroll.v.empty() == false)) + graph.rectangle({ right - static_cast(essence_->scroll.scale), + bottom - static_cast(essence_->scroll.scale), + essence_->scroll.scale, + essence_->scroll.scale }, + true, colors::button_face); + } + void trigger::attached(widget_reference widget, graph_reference graph) { essence_->listbox_ptr = static_cast(&widget); @@ -4049,7 +3980,6 @@ namespace nana essence_->lister.bind(essence_, widget); widget.bgcolor(colors::white); - } void trigger::detached() @@ -4061,7 +3991,6 @@ namespace nana void trigger::typeface_changed(graph_reference graph) { essence_->scheme_ptr->text_height = graph.text_extent_size(L"jHWn0123456789/scheme_ptr->item_height = essence_->scheme_ptr->text_height + essence_->scheme_ptr->item_height_ex; essence_->scheme_ptr->suspension_width = graph.text_extent_size("...").width; } @@ -4076,8 +4005,7 @@ namespace nana drawer_header_->draw(graph, r); if (essence_->rect_lister(r)) drawer_lister_->draw(r); - - essence_->draw_peripheral(); + _m_draw_border(); } void trigger::mouse_move(graph_reference graph, const arg_mouse& arg) @@ -4087,16 +4015,17 @@ namespace nana bool need_refresh = false; + point pos_in_header = arg.pos; + essence_->widget_to_header(pos_in_header); + if(essence_->ptr_state == item_state::pressed) { if(essence_->pointer_where.first == parts::header) { // moving a pressed header : grab it essence_->ptr_state = item_state::grabbed; - nana::point pos = arg.pos; - essence_->widget_to_header(pos); //Start to move a header column or resize a header column(depends on item_spliter_) - drawer_header_->grab(pos, true); + drawer_header_->grab(pos_in_header, true); essence_->lister.wd_ptr()->set_capture(true); need_refresh = true; @@ -4104,10 +4033,9 @@ namespace nana } if(essence_->ptr_state == item_state::grabbed) - { // moving a grabbed header - nana::point pos = arg.pos; - essence_->widget_to_header(pos); - need_refresh = drawer_header_->grab_move(pos); + { + // moving a grabbed header + need_refresh = drawer_header_->grab_move(pos_in_header); } else if(essence_->calc_where(arg.pos)) { @@ -4139,6 +4067,12 @@ namespace nana } } + if (essence_->mouse_selection.started) + { + essence_->update_mouse_selection(arg.pos); + need_refresh = true; + } + if (need_refresh) { refresh(graph); @@ -4150,6 +4084,7 @@ namespace nana { using item_state = essence::item_state; using parts = essence::parts; + if((essence_->pointer_where.first != parts::unknown) || (essence_->ptr_state != item_state::normal)) { if (essence_->ptr_state != item_state::grabbed) @@ -4168,6 +4103,9 @@ namespace nana using item_state = essence::item_state; using parts = essence::parts; bool update = false; + + essence_->mouse_selection.reverse_selection = false; + auto & ptr_where = essence_->pointer_where; if((ptr_where.first == parts::header) && (ptr_where.second != npos || (drawer_header_->splitter() != npos))) { @@ -4183,16 +4121,20 @@ namespace nana { auto & lister = essence_->lister; index_pair item_pos; - if (lister.forward(essence_->scroll.offset_y_dpl, ptr_where.second, item_pos)) - { - auto * item_ptr = (item_pos.is_item() ? &lister.at(item_pos) : nullptr); + auto first_disp = essence_->first_display(); - const index_pair abs_item_pos{ item_pos.cat, lister.absolute(item_pos) }; + if (lister.forward(first_disp, ptr_where.second, item_pos)) + { + auto * item_ptr = (item_pos.is_category() ? nullptr : &lister.at(item_pos)); + + const auto abs_item_pos = lister.index_cast_noexcpt(item_pos, true, item_pos); //convert display position to absolute position if(ptr_where.first == parts::lister) { bool sel = true; - if (!lister.single_selection()) + + //no single selected + if (!lister.single_status(true)) { if (arg.shift) { @@ -4203,14 +4145,33 @@ namespace nana auto before = lister.last_selected_abs; - lister.select_display_range(lister.last_selected_abs, item_pos, sel); + lister.select_display_range_if(lister.last_selected_abs, item_pos, true, [](const index_pair&) + { + return true; + }); lister.last_selected_abs = before; } else if (arg.ctrl) + { + essence_->mouse_selection.reverse_selection = true; sel = !item_proxy(essence_, abs_item_pos).selected(); + } else - lister.select_for_all(false); //cancel all selections + { + if (nana::mouse::right_button == arg.button) + { + //Unselects all selected items if the current item is not selected before selecting. + auto selected = lister.pick_items(true); + if (selected.cend() == std::find(selected.cbegin(), selected.cend(), item_pos)) + lister.select_for_all(false, item_pos); + } + else + { + //Unselects all selected items except current item if right button clicked. + lister.select_for_all(false, item_pos); //cancel all selections + } + } } else { @@ -4225,8 +4186,7 @@ namespace nana if (item_ptr->flags.selected != sel) { item_ptr->flags.selected = sel; - - lister.emit_selected(abs_item_pos); + lister.emit_cs(abs_item_pos, true); if (item_ptr->flags.selected) { @@ -4237,27 +4197,46 @@ namespace nana essence_->lister.last_selected_abs.set_both(npos); } } - else if(!lister.single_selection()) - lister.categ_selected(item_pos.cat, true); + else + { + lister.cat_status(item_pos.cat, true, true); + } } else { - if(item_ptr) + if (item_ptr) { - item_ptr->flags.checked = ! item_ptr->flags.checked; - - lister.emit_checked(abs_item_pos); + item_ptr->flags.checked = !item_ptr->flags.checked; + lister.emit_cs(abs_item_pos, false); if (item_ptr->flags.checked) lister.cancel_others_if_single_enabled(false, abs_item_pos); } - else if (! lister.single_check()) - lister.categ_checked_reverse(item_pos.cat); + else if (!lister.single_status(false)) //not single checked + lister.cat_status_reverse(item_pos.cat, false); } update = true; } else - update = lister.select_for_all(false); //unselect all items due to the blank area being clicked + { + //Blank area is clicked + + bool unselect_all = true; + if (!lister.single_status(true)) //not single selected + { + if (arg.ctrl || arg.shift) + { + essence_->mouse_selection.reverse_selection = arg.ctrl; + unselect_all = false; + } + } + + if(unselect_all) + update = lister.select_for_all(false); //unselect all items due to the blank area being clicked + } + + if(arg.is_left_button() && (!lister.single_status(true))) //not selected + essence_->start_mouse_selection(arg.pos); if(update) { @@ -4270,12 +4249,11 @@ namespace nana if(update) { - essence_->draw_peripheral(); + _m_draw_border(); API::dev::lazy_refresh(); } } - void trigger::mouse_up(graph_reference graph, const arg_mouse& arg) { using item_state = essence::item_state; @@ -4283,27 +4261,39 @@ namespace nana auto prev_state = essence_->ptr_state; essence_->ptr_state = item_state::highlighted; + + bool need_refresh = false; //Do sort if (essence_->header.sortable() && essence_->pointer_where.first == parts::header && prev_state == item_state::pressed) { if(essence_->pointer_where.second < essence_->header.cont().size()) { - if(essence_->lister.sort_index(essence_->pointer_where.second)) + if(essence_->lister.sort_column(essence_->pointer_where.second, nullptr)) { essence_->trace_item_dpl(index_pair{0,0}); - refresh(graph); - API::dev::lazy_refresh(); + need_refresh = true; } } } - else if (prev_state == item_state::grabbed) + else if (item_state::grabbed == prev_state) { nana::point pos = arg.pos; essence_->widget_to_header(pos); drawer_header_->grab(pos, false); + need_refresh = true; + essence_->lister.wd_ptr()->release_capture(); + } + + if (essence_->mouse_selection.started) + { + essence_->stop_mouse_selection(); + need_refresh = true; + } + + if (need_refresh) + { refresh(graph); API::dev::lazy_refresh(); - essence_->lister.wd_ptr()->release_capture(); } } @@ -4341,7 +4331,9 @@ namespace nana return; index_pair item_pos; - auto & offset_y = essence_->scroll.offset_y_dpl; + + auto offset_y = essence_->first_display(); + auto & lister = essence_->lister; //Get the item which the mouse is placed. if (lister.forward(offset_y, essence_->pointer_where.second, item_pos)) @@ -4360,9 +4352,12 @@ namespace nana if(false == do_expand) { auto last = lister.last(); - size_type n = essence_->number_of_lister_items(false); - if (lister.backward(last, n, last)) - offset_y = last; + size_type n = essence_->count_of_exposed(false); + if (lister.backward(last, n, last)) + { + offset_y = last; + essence_->first_display(last); + } } essence_->adjust_scroll_life(); refresh(graph); @@ -4382,7 +4377,7 @@ namespace nana { bool up = false; - if (essence_->lister.size_categ()==1 && essence_->lister.size_item(0)==0) + if (essence_->lister.cat_container().size() == 1 && essence_->lister.size_item(0)==0) return ; switch(arg.key) @@ -4400,7 +4395,6 @@ namespace nana item_proxy(essence_, i).check(ck); } break; - case keyboard::os_pageup : up = true; case keyboard::os_pagedown: @@ -4410,14 +4404,15 @@ namespace nana return; essence_->lister.select_for_all(false); - index_pair idx{essence_->scroll_y_dpl()}; + auto idx = essence_->first_display(); + if (!up) essence_->lister.forward(idx, scrl.range()-1, idx); - if (idx.is_item()) + if (!idx.is_category()) item_proxy::from_display(essence_, idx).select(true); - else if(!essence_->lister.single_selection()) - essence_->lister.categ_selected(idx.cat, true); + else if (!essence_->lister.single_status(true)) //not selected + essence_->lister.cat_status(idx.cat, true, true); essence_->trace_last_selected_item (); @@ -4428,10 +4423,10 @@ namespace nana essence_->lister.select_for_all(false); index_pair frst{essence_->lister.first()}; - if (frst.is_item()) + if (! frst.is_category()) item_proxy::from_display(essence_, frst).select(true); - else if(!essence_->lister.single_selection()) - essence_->lister.categ_selected(frst.cat, true); + else if (!essence_->lister.single_status(true)) //not selected + essence_->lister.cat_status(frst.cat, true, true); essence_->trace_last_selected_item (); break; @@ -4454,8 +4449,8 @@ namespace nana { case keyboard::copy: { - export_options exp_opt {essence_->def_exp_options}; - exp_opt.columns_order = essence_->header.all_headers(true); + auto exp_opt = essence_->def_exp_options; + exp_opt.columns_order = essence_->header.get_headers(true); exp_opt.only_selected_items = true; ::nana::system::dataexch().set(essence_->to_string(exp_opt)); return; @@ -4469,12 +4464,11 @@ namespace nana return; } } - //end class trigger //class item_proxy - item_proxy::item_proxy(essence * ess) + item_proxy::item_proxy(essence * ess) noexcept : ess_(ess) {} @@ -4490,18 +4484,18 @@ namespace nana /// the main porpose of this it to make obvious that item_proxy operate with absolute positions, and dont get moved during sort() item_proxy item_proxy::from_display(essence *ess, const index_pair &relative) { - return item_proxy{ess, ess->lister.absolute_pair(relative)}; + return item_proxy{ ess, ess->lister.index_cast(relative, true) }; } item_proxy item_proxy::from_display(const index_pair &relative) const { - return item_proxy{ess_, ess_->lister.absolute_pair(relative)}; + return item_proxy{ess_, ess_->lister.index_cast(relative, true)}; } /// posible use: last_selected_display = last_selected.to_display().item; use with caution, it get invalidated after a sort() index_pair item_proxy::to_display() const { - return ess_->lister.relative_pair(pos_); + return ess_->lister.index_cast(pos_, false); //convert absolute position to display position } bool item_proxy::displayed() const @@ -4510,17 +4504,15 @@ namespace nana return false; auto pos = to_display(); - if (ess_->scroll.offset_y_dpl > pos) + if (ess_->first_display() > pos) return false; - auto size = ess_->number_of_lister_items(false); - - auto last = ess_->lister.advance(ess_->scroll.offset_y_dpl, size); + auto last = ess_->lister.advance(ess_->first_display(), ess_->count_of_exposed(false)); return (last > pos || last == pos); } - bool item_proxy::empty() const + bool item_proxy::empty() const noexcept { return !ess_; } @@ -4532,15 +4524,14 @@ namespace nana if(m.flags.checked != ck) { m.flags.checked = ck; - ess_->lister.emit_checked(pos_); - + ess_->lister.emit_cs(pos_, false); if (scroll_view) { if (ess_->lister.get(pos_.cat)->expand) ess_->lister.get(pos_.cat)->expand = false; if (!this->displayed()) - ess_->lister.scroll(pos_, !(ess_->scroll.offset_y_dpl > this->to_display())); + ess_->lister.scroll(pos_, !(ess_->first_display() > this->to_display())); } ess_->update(); @@ -4554,37 +4545,37 @@ namespace nana } /// is ignored if no change (maybe set last_selected anyway??), but if change emit event, deselect others if need ans set/unset last_selected - item_proxy & item_proxy::select(bool sel, bool scroll_view) + item_proxy & item_proxy::select(bool s, bool scroll_view) { internal_scope_guard lock; //pos_ never represents a category if this item_proxy is available. auto & m = cat_->items.at(pos_.item); // a ref to the real item - if (m.flags.selected != sel) + if(m.flags.selected == s) + return *this; // ignore if no change + + m.flags.selected = s; // actually change selection + + ess_->lister.emit_cs(this->pos_, true); + + if (m.flags.selected) { - m.flags.selected = sel; // actually change selection - - ess_->lister.emit_selected(this->pos_); - - if (m.flags.selected) - { - ess_->lister.cancel_others_if_single_enabled(true, pos_); //Cancel all selections except pos_ if single_selection is enabled. - ess_->lister.last_selected_abs = pos_; - } - else if (ess_->lister.last_selected_abs == pos_) + ess_->lister.cancel_others_if_single_enabled(true, pos_); //Cancel all selections except pos_ if single_selection is enabled. + ess_->lister.last_selected_abs = pos_; + } + else if (ess_->lister.last_selected_abs == pos_) ess_->lister.last_selected_abs.set_both(npos); - if (scroll_view) - { - if (ess_->lister.get(pos_.cat)->expand) - ess_->lister.get(pos_.cat)->expand = false; + if (scroll_view) + { + if (ess_->lister.get(pos_.cat)->expand) + ess_->lister.get(pos_.cat)->expand = false; - if (!this->displayed()) - ess_->lister.scroll(pos_, !(ess_->scroll.offset_y_dpl > this->to_display())); - } - - ess_->update(); + if (!this->displayed()) + ess_->lister.scroll(pos_, !(ess_->first_display() > this->to_display())); } + + ess_->update(); return *this; } @@ -4617,7 +4608,7 @@ namespace nana return cat_->items.at(pos_.item).fgcolor; } - std::size_t item_proxy::columns() const + std::size_t item_proxy::columns() const noexcept { return ess_->header.cont().size(); } @@ -4645,7 +4636,7 @@ namespace nana std::string item_proxy::text(size_type col) const { - return ess_->lister.get_cells(cat_, pos_.item).at(col).text; + return cat_->cells(pos_.item).at(col).text; } void item_proxy::icon(const nana::paint::image& img) @@ -4661,7 +4652,6 @@ namespace nana } } - //Behavior of Iterator's value_type bool item_proxy::operator==(const char * s) const { @@ -4675,12 +4665,12 @@ namespace nana bool item_proxy::operator==(const std::string& s) const { - return (ess_->lister.get_cells(cat_, pos_.item).at(0).text == s); + return (text(pos_.item) == s); } bool item_proxy::operator==(const std::wstring& s) const { - return (ess_->lister.get_cells(cat_, pos_.item).at(0).text == to_utf8(s)); + return (text(pos_.item) == to_utf8(s)); } item_proxy & item_proxy::operator=(const item_proxy& rhs) @@ -4754,19 +4744,19 @@ namespace nana } //Undocumented methods - essence * item_proxy::_m_ess() const + essence * item_proxy::_m_ess() const noexcept { return ess_; } - index_pair item_proxy::pos() const + index_pair item_proxy::pos() const noexcept { return pos_; } auto item_proxy::_m_cells() const -> std::vector { - return ess_->lister.get_cells(cat_, pos_.item); + return cat_->cells(pos_.item); } nana::any * item_proxy::_m_value(bool alloc_if_empty) @@ -4783,14 +4773,14 @@ namespace nana //class cat_proxy //the member cat_ is used for fast accessing to the category - cat_proxy::cat_proxy(essence * ess, size_type pos) + cat_proxy::cat_proxy(essence * ess, size_type pos) noexcept : ess_(ess), pos_(pos) { _m_cat_by_pos(); } - cat_proxy::cat_proxy(essence* ess, category_t* cat) + cat_proxy::cat_proxy(essence* ess, category_t* cat) noexcept : ess_(ess), cat_(cat) { @@ -4813,7 +4803,7 @@ namespace nana void cat_proxy::append(std::initializer_list arg) { const auto items = columns(); - push_back(std::string{}); + push_back({}); item_proxy ip{ ess_, index_pair(pos_, size() - 1) }; size_type pos = 0; for (auto & txt : arg) @@ -4827,7 +4817,7 @@ namespace nana void cat_proxy::append(std::initializer_list arg) { const auto items = columns(); - push_back(std::string{}); + push_back({}); item_proxy ip{ ess_, index_pair(pos_, size() - 1) }; size_type pos = 0; for (auto & txt : arg) @@ -4867,7 +4857,7 @@ namespace nana internal_scope_guard lock; if (text != cat_->text) { - cat_->text.swap(text); + cat_->text = std::move(text); ess_->update(); } return *this; @@ -4879,7 +4869,7 @@ namespace nana internal_scope_guard lock; if (text != cat_->text) { - cat_->text.swap(text); + cat_->text = std::move(text); ess_->update(); } return *this; @@ -4915,15 +4905,16 @@ namespace nana if (cat_->model_ptr) { - auto pos = cat_->model_ptr->container()->size(); - cat_->model_ptr->container()->emplace_back(); - auto cells = cat_->model_ptr->container()->to_cells(pos); + const auto cont = cat_->model_ptr->container(); + auto pos = cont->size(); + cont->emplace_back(); + auto cells = cont->to_cells(pos); if (cells.size()) - cells.front().text.swap(s); + cells.front().text = std::move(s); else cells.emplace_back(std::move(s)); - cat_->model_ptr->container()->assign(pos, cells); + cont->assign(pos, cells); cat_->items.emplace_back(); } else @@ -4976,16 +4967,9 @@ namespace nana return item_proxy(ess_, index_pair(pos_, cat_->items.size() - 1)); } - /// convert from display order to absolute (find the real item in that display pos) but without check from current active sorting, in fact using just the last sorting !!! - size_type cat_proxy::index_by_display_order(size_type display_order_pos) const + size_type cat_proxy::index_cast(size_type from, bool from_display_order) const { - return ess_->lister.index_by_display_order(pos_, display_order_pos); - } - - /// find display order for the real item but without check from current active sorting, in fact using just the last sorting !!! - size_type cat_proxy::display_order(size_type pos) const - { - return ess_->lister.display_order(pos_, pos); + return ess_->lister.index_cast(index_pair{ pos_, from }, from_display_order).item; } size_type cat_proxy::position() const @@ -5073,7 +5057,7 @@ namespace nana void cat_proxy::inline_factory(size_type column, pat::cloneable> factory) { - if (column >= ess_->header.cont().size()) + if (column >= this->columns()) throw std::out_of_range("listbox.cat_proxy.inline_factory: invalid column index"); if (column >= cat_->factories.size()) @@ -5124,30 +5108,23 @@ namespace nana void cat_proxy::_m_try_append_model(const const_virtual_pointer& dptr) { + //Throws when appends an object to a listbox which should have a model. if (!cat_->model_ptr) - { - //Throws when appends an object to a listbox which should have a model. throw std::runtime_error("nana::listbox hasn't a model"); - } ess_->lister.throw_if_immutable_model(cat_->model_ptr.get()); - if (cat_->model_ptr->container()->push_back(dptr)) - { - cat_->sorted.push_back(cat_->items.size()); - cat_->items.emplace_back(); - - assign_colors_for_last(ess_, cat_); - } - else - { + if (!cat_->model_ptr->container()->push_back(dptr)) throw std::invalid_argument("nana::listbox, the type of operand object is mismatched with model container value_type"); - } + + cat_->sorted.push_back(cat_->items.size()); + cat_->items.emplace_back(); + assign_colors_for_last(ess_, cat_); } - void cat_proxy::_m_cat_by_pos() + void cat_proxy::_m_cat_by_pos() noexcept { - if (pos_ >= ess_->lister.size_categ()) + if (pos_ >= ess_->lister.cat_container().size()) { ess_ = nullptr; cat_ = nullptr; @@ -5160,7 +5137,7 @@ namespace nana //A fix for auto_draw, to make sure the inline widget set() issued after value() and value_ptr() are actually set. //Fixed by leobackes(pr#86) - void cat_proxy::_m_update() + void cat_proxy::_m_update() noexcept { ess_->update(); } @@ -5171,19 +5148,13 @@ namespace nana { cat_->model_ptr.reset(p); cat_->items.clear(); - cat_->sorted.clear(); cat_->items.resize(cat_->model_ptr->container()->size()); - const auto item_size = cat_->items.size(); - cat_->sorted.reserve(item_size + 100); - for (std::size_t pos = 0; pos != item_size; ++pos) - cat_->sorted.push_back(pos); - + cat_->make_sort_order(); ess_->lister.sort(); - ess_->adjust_scroll_life(); - API::refresh_window(ess_->listbox_ptr->handle()); + ess_->update(true); } } //end class cat_proxy @@ -5219,22 +5190,26 @@ namespace nana { internal_scope_guard lock; - auto & ess = _m_ess(); - if (ess.lister.enable_ordered(enable)) - ess.update(); + if (_m_ess().lister.enable_ordered(enable)) + _m_ess().update(); return true; } - void listbox::auto_draw(bool ad) + void listbox::auto_draw(bool enabled) noexcept { - _m_ess().set_auto_draw(ad); + auto & ess = _m_ess(); + if (ess.auto_draw != enabled) + { + ess.auto_draw = enabled; + ess.update(); + } } void listbox::scroll(bool to_bottom, size_type cat_pos) { auto & ess = _m_ess(); - auto cats = ess.lister.size_categ(); + auto cats = this->size_categ(); if (::nana::npos != cat_pos) { @@ -5248,10 +5223,7 @@ namespace nana if (to_bottom) { auto items = ess.lister.size_item(cat_pos); - if (0 == items) - pos.item = ::nana::npos; - else - pos.item = items - 1; + pos.item = (0 == items ? ::nana::npos : items - 1); } else pos.item = ess.lister.size_item(cat_pos) ? 0 : ::nana::npos; @@ -5262,9 +5234,8 @@ namespace nana void listbox::scroll(bool to_bottom, const index_pair& pos) { - auto & ess = _m_ess(); - ess.lister.scroll(pos, to_bottom); - ess.update(); + _m_ess().lister.scroll(pos, to_bottom); + _m_ess().update(); } listbox::size_type listbox::append_header(std::string s, unsigned width) @@ -5304,22 +5275,22 @@ namespace nana return cat_proxy{ &ess, new_cat_ptr }; } - void listbox::append(std::initializer_list args) + void listbox::append(std::initializer_list categories) { internal_scope_guard lock; auto & ess = _m_ess(); - for (auto & arg : args) + for (auto & arg : categories) ess.lister.create_cat(native_string_type(to_nstring(arg))); ess.update(); } - void listbox::append(std::initializer_list args) + void listbox::append(std::initializer_list categories) { internal_scope_guard lock; auto & ess = _m_ess(); - for (auto & arg : args) + for (auto & arg : categories) ess.lister.create_cat(native_string_type(to_nstring(arg))); ess.update(); } @@ -5345,7 +5316,7 @@ namespace nana { internal_scope_guard lock; auto & ess = _m_ess(); - ess.lister.insert(pos, std::move(text), ess.header.cont().size()); + ess.lister.insert(pos, std::move(text), this->column_size()); if (!empty()) { @@ -5363,20 +5334,18 @@ namespace nana listbox::cat_proxy listbox::at(size_type pos) { - auto & ess = _m_ess(); - if (pos >= ess.lister.size_categ()) + if (pos >= this->size_categ()) throw std::out_of_range("Nana.Listbox.at(): invalid position"); - return{ &ess, pos }; + return{ &_m_ess(), pos }; } const listbox::cat_proxy listbox::at(size_type pos) const { - auto & ess = _m_ess(); - if(pos >= ess.lister.size_categ()) + if(pos >= this->size_categ()) throw std::out_of_range("Nana.Listbox.at(): invalid position"); - return{ &ess, pos }; + return{ &_m_ess(), pos }; } listbox::item_proxy listbox::at(const index_pair& abs_pos) @@ -5393,13 +5362,13 @@ namespace nana listbox::index_pair listbox::cast( const point& pos ) const { auto & ess=_m_ess(); - auto _where=ess.where(pos.x, pos.y); + auto _where = ess.where(pos); + index_pair item_pos{npos,npos}; - if(_where.first==drawerbase::listbox::essence::parts::lister) - { - auto & offset_y = ess.scroll.offset_y_dpl; - ess.lister.forward(offset_y, _where.second, item_pos); - } + + if (drawerbase::listbox::essence::parts::lister == _where.first) + ess.lister.forward(ess.first_display(), _where.second, item_pos); + return item_pos; } @@ -5421,8 +5390,7 @@ namespace nana //Contributed by leobackes(pr#97) listbox::size_type listbox::column_from_pos ( const point& pos ) { - auto & ess=_m_ess(); - return ess.header.column_from_point(pos.x - 2 - static_cast(ess.scroll.x_offset())); + return _m_ess().header.column_from_point(pos.x - 2 - static_cast(_m_ess().scroll.x_offset())); } void listbox::checkable(bool chkable) @@ -5443,27 +5411,23 @@ namespace nana void listbox::clear(size_type cat) { auto & ess = _m_ess(); + + auto move = ess.lister.size_item(cat); + ess.lister.clear(cat); - // from current display position - // move to the cat self if not in first cat - // move to first item ?? if in first cat - ess.scroll_y_abs(ess.scroll_y_abs()); - + ess.first_display_move(-static_cast(move)); ess.update(); } void listbox::clear() { auto & ess = _m_ess(); + ess.lister.clear(); unsort(); // apperar to be espected - // from current display position - // move to the cat self if not in first cat - // move to first item ?? if in first cat - ess.scroll_y_abs(ess.scroll_y_abs()); - + ess.first_display(ess.lister.first()); ess.update(); } @@ -5471,19 +5435,20 @@ namespace nana { auto & ess = _m_ess(); ess.lister.erase(cat); - if(cat) + if (cat) { - auto pos = ess.scroll_y_dpl(); - if(cat <= pos.cat) + auto pos = ess.first_display(); + if (cat <= pos.cat) { - if(pos.cat == ess.lister.size_categ()) + if (pos.cat == this->size_categ()) --pos.cat; pos.item = npos; - ess.set_scroll_y_dpl(pos); + ess.first_display(pos); } } else - ess.set_scroll_y_dpl(index_pair()); + ess.first_display(ess.lister.first()); + ess.update(); } @@ -5491,7 +5456,56 @@ namespace nana { auto & ess = _m_ess(); ess.lister.erase(); - ess.scroll_y_abs(index_pair()); + ess.first_display(ess.lister.first()); + ess.update(); + } + + void listbox::erase(index_pairs indexes) + { + std::sort(indexes.begin(), indexes.end(), [](const index_pair& pos1, const index_pair& pos2) + { + return (pos1 > pos2); + }); + + auto & ess = _m_ess(); + + const auto disp_distance = ess.scroll.offset_display; + + for (auto & pos : indexes) + { + auto & cat = *ess.lister.get(pos.cat); + if (pos.item < cat.items.size()) + { + if (cat.model_ptr) + { + drawerbase::listbox::es_lister::throw_if_immutable_model(cat.model_ptr.get()); + cat.model_ptr->container()->erase(pos.item); + } + + cat.items.erase(cat.items.begin() + pos.item); + } + } + + //Rebuild the sorted order + std::size_t this_cat = this->size_categ(); + for (auto & pos : indexes) + { + if (this_cat != pos.cat) + { + this_cat = pos.cat; + ess.lister.get(this_cat)->make_sort_order(); + } + } + + auto new_disp = ess.lister.advance(index_pair{}, disp_distance); + + if (new_disp.empty()) + new_disp.cat = new_disp.item = 0; + + ess.first_display(new_disp); + + ess.lister.sort(); + ess.update(); } @@ -5503,9 +5517,10 @@ namespace nana auto * ess = ip._m_ess(); auto _where = ip.pos(); - auto pos_before = ess->scroll_y_dpl(); + auto pos_before = ess->first_display(); ess->lister.erase(_where); - auto pos = ess->scroll_y_dpl(); + + auto pos = ess->first_display(); if (!pos.empty()) { if ((pos.cat == _where.cat) && (_where.item <= pos.item)) @@ -5517,14 +5532,16 @@ namespace nana } else --pos.item; - ess->set_scroll_y_dpl(pos); + + ess->first_display(pos); } } else { if (pos_before.item) --pos_before.item; - ess->set_scroll_y_dpl(pos_before); + + ess->first_display(pos_before); } ess->update(); if(_where.item < ess->lister.size_item(_where.cat)) @@ -5550,18 +5567,18 @@ namespace nana /// sort() and ivalidate any existing reference from display position to absolute item, that is: after sort() display offset point to different items void listbox::sort_col(size_type col, bool reverse) { - _m_ess().lister.set_sort_index(col, reverse); + _m_ess().lister.sort_column(col, &reverse); } auto listbox::sort_col() const -> size_type { - return _m_ess().lister.sort_index(); + return _m_ess().lister.sort_attrs().column; } /// potencially ivalidate any existing reference from display position to absolute item, that is: after sort() display offset point to different items void listbox::unsort() { - _m_ess().lister.set_sort_index(npos, false); + this->sort_col(npos, false); } bool listbox::freeze_sort(bool freeze) @@ -5576,9 +5593,8 @@ namespace nana void listbox::show_header(bool sh) { - auto & ess = _m_ess(); - ess.header.visible(sh); - ess.update(); + _m_ess().header.visible(sh); + _m_ess().update(); } bool listbox::visible_header() const @@ -5588,14 +5604,13 @@ namespace nana void listbox::move_select(bool upwards) ///