diff --git a/include/nana/gui/widgets/listbox.hpp b/include/nana/gui/widgets/listbox.hpp index 86184718..b6279630 100644 --- a/include/nana/gui/widgets/listbox.hpp +++ b/include/nana/gui/widgets/listbox.hpp @@ -1461,7 +1461,19 @@ the nana::detail::basic_window member pointer scheme /// Returns the number of columns size_type column_size() const; - void column_resizable(bool resizable); + /// Move column to view_position + void move_column(size_type abs_pos, size_type view_pos); + + /// Sort columns in range first_col to last_col inclusive using the values from a row + void reorder_columns(size_type first_col, + size_type last_col, + index_pair row, bool reverse, + std::function comp); + + void column_resizable(bool resizable); bool column_resizable() const; void column_movable(bool); bool column_movable() const; @@ -1517,7 +1529,8 @@ the nana::detail::basic_window member pointer scheme ///Sets a strict weak ordering comparer for a column void set_sort_compare( size_type col, - std::function strick_ordering); + std::function strick_ordering); /// sort() and ivalidate any existing reference from display position to absolute item, that is: after sort() display offset point to different items void sort_col(size_type col, bool reverse = false); diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index f9e5bd0c..4b902c43 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -17,6 +17,12 @@ * dankan1890(pr#158) * */ +#include +#include +#include +#include +#include +#include #include #include //for inline widget @@ -28,12 +34,6 @@ #include #include "skeletons/content_view.hpp" -#include -#include -#include -#include -#include - namespace nana { static void check_range(std::size_t pos, std::size_t size) @@ -130,17 +130,18 @@ namespace nana struct column : public column_interface { - native_string_type caption; - unsigned width_px; - std::pair range_width_px; + native_string_type caption; //< header title + unsigned width_px; //< column width in pixels + std::pair range_width_px; //< allowed width bool visible_state{ true }; - /// Absolute position of column when it was creating - size_type index; + + size_type index; //< Absolute position of column when it was created nana::align alignment{ nana::align::left }; - std::function weak_ordering; + std::function weak_ordering; std::shared_ptr font; ///< The exclusive column font @@ -186,18 +187,18 @@ namespace nana { } private: - //The definition is provided after essence + /// The definition is provided after essence void _m_refresh() noexcept; private: essence* const ess_; public: - //Implementation of column_interface + /// Implementation of column_interface unsigned width() const noexcept override { return width_px; } - // Sets the width and overrides the ranged width + /// Sets the width and overrides the ranged width void width(unsigned pixels) noexcept override { width_px = pixels; @@ -223,7 +224,7 @@ namespace nana } } - size_type position(bool disp_order) const noexcept override; //The definition is provided after essence + size_type position(bool disp_order) const noexcept override; //< The definition is provided after essence std::string text() const noexcept override { @@ -440,7 +441,7 @@ namespace nana throw std::invalid_argument("listbox: invalid header index"); } - /// find and return a ref to the column that originaly was at position "pos" previous to any list reorganization. + /// find and return a ref to the column that originally was at position "pos" previous to any list reorganization. column& at(size_type pos, bool disp_order = false) { check_range(pos, cont_.size()); @@ -507,7 +508,7 @@ namespace nana return{ left, 0 }; } - /// return the original index of the visible col currently before(in front of) or after the col originaly at index "index" + /// return the original index of the visible col currently before(in front of) or after the col originally at index "index" size_type next(size_type index) const noexcept { bool found_me = false; @@ -542,7 +543,25 @@ namespace nana 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 + + /// move col to view pos + void move_to_view_pos (size_type col, size_type view, bool front) noexcept + { + if (!front) view++; + if (view >= cont_.size() ) return; + + auto i = std::find_if( cont_.begin(), + cont_.end(), + [&](const column& c){return col==c.index;}); + + if (i==cont_.end()) return; + + auto col_from = *i; + cont_.erase(i); + cont_.insert(cont_.begin()+ view, col_from); + + } + /// move the col originally at "from" to the position currently in front (or after) the col originally 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())) @@ -6152,5 +6171,46 @@ namespace nana internal_scope_guard lock; return _m_ess().content_view->scroll_operation(); } + + /// Move column to view_position + void listbox::move_column(size_type abs_pos, size_type view_pos) + { + internal_scope_guard lock; + return _m_ess().header.move_to_view_pos(abs_pos, view_pos, true); + _m_ess().update(); + } + + /// Sort columns in range first_col to last_col inclusive using a row + void listbox::reorder_columns(size_type first_col, + size_type last_col, + index_pair row, bool reverse, + std::function comp) + { + if (first_col<0 || last_col<=first_col) + return; + if (last_col >= column_size()) + return; + std::vector new_idx; + for(size_type i=first_col; i<=last_col; ++i) new_idx.push_back(i); + const item_proxy & ip_row=this->at(row); + internal_scope_guard lock; + const nana::any *pnany=_m_ess().lister.anyobj(row,false); + std::sort(new_idx.begin(), new_idx.end(), [&](size_type col1, + size_type col2) + { + return comp(ip_row.text(col1), col1, + ip_row.text(col2), col2, + pnany, reverse); + }); + for(size_t i=0; i