improve listbox methods

listbox::hovered() and listbox::insert_item()
This commit is contained in:
Jinhao 2018-11-01 06:37:26 +08:00
parent 711dff56de
commit 91675cb32d
2 changed files with 68 additions and 15 deletions

View File

@ -1481,9 +1481,19 @@ the nana::detail::basic_window member pointer scheme
*/ */
void insert_item(const index_pair& abs_pos, const ::std::wstring& text); void insert_item(const index_pair& abs_pos, const ::std::wstring& text);
void insert_item(index_pair abs_pos, const listbox& rhs, const index_pairs& indexes);
/// Returns an index of item which contains the specified point. /// Returns an index of item which contains the specified point.
index_pair cast(const point & screen_pos) const; index_pair cast(const point & screen_pos) const;
/// Returns the item which is hovered
/**
* @param return_end Indicates whether to return an end position instead of empty position if an item is not hovered.
* @return The position of the hovered item. If return_end is true, it returns the position next to the last item of last category if an item is not hovered.
*/
index_pair hovered(bool return_end) const;
/// Returns the absolute position of column which contains the specified point. /// Returns the absolute position of column which contains the specified point.
size_type column_from_pos(const point & pos) const; size_type column_from_pos(const point & pos) const;
@ -1497,9 +1507,6 @@ the nana::detail::basic_window member pointer scheme
void erase(index_pairs indexes); ///<Erases specified items. void erase(index_pairs indexes); ///<Erases specified items.
item_proxy erase(item_proxy); item_proxy erase(item_proxy);
/// Returns the item which is hovered
index_pair hovered() const;
bool sortable() const; bool sortable() const;
void sortable(bool enable); void sortable(bool enable);

View File

@ -5624,6 +5624,35 @@ namespace nana
insert_item(pos, to_utf8(text)); insert_item(pos, to_utf8(text));
} }
void listbox::insert_item(index_pair abs_pos, const listbox& rhs, const index_pairs& indexes)
{
auto const columns = (std::min)(this->column_size(), rhs.column_size());
if (0 == columns)
return;
item_proxy it_new = this->at(abs_pos.cat).end();
for (auto & idx : indexes)
{
auto it_src = rhs.at(idx.cat).at(idx.item);
if (abs_pos.item < this->at(abs_pos.cat).size())
{
this->insert_item(abs_pos, it_src.text(0));
it_new = this->at(abs_pos);
}
else
{
it_new = this->at(abs_pos.cat).append(it_src.text(0));
}
for (std::size_t col = 1; col < columns; ++col)
it_new.text(col, it_src.text(col));
++abs_pos.item;
}
}
listbox::cat_proxy listbox::at(size_type pos) listbox::cat_proxy listbox::at(size_type pos)
{ {
internal_scope_guard lock; internal_scope_guard lock;
@ -5663,6 +5692,35 @@ namespace nana
return index_pair{ npos, npos }; return index_pair{ npos, npos };
} }
listbox::index_pair listbox::hovered(bool return_end) const
{
using parts = drawerbase::listbox::essence::parts;
internal_scope_guard lock;
auto cur_pos = API::cursor_position();
API::calc_window_point(handle(), cur_pos);
auto pt_where = _m_ess().where(cur_pos);
if ((pt_where.first == parts::list || pt_where.first == parts::checker) && pt_where.second != npos)
{
auto pos = _m_ess().lister.advance(_m_ess().first_display(), static_cast<int>(pt_where.second));
if (return_end && pos.is_category())
{
if (0 < pos.cat)
--pos.cat;
pos.item = this->size_item(pos.cat);
}
return pos;
}
else if (return_end)
return index_pair{ this->size_categ() - 1, this->size_item(this->size_categ() - 1) };
return index_pair{ npos, npos };
}
auto listbox::column_at(size_type pos, bool disp_order) -> column_interface& auto listbox::column_at(size_type pos, bool disp_order) -> column_interface&
{ {
internal_scope_guard lock; internal_scope_guard lock;
@ -5852,18 +5910,6 @@ namespace nana
return item_proxy(ess); return item_proxy(ess);
} }
listbox::index_pair listbox::hovered() const
{
internal_scope_guard lock;
using parts = drawerbase::listbox::essence::parts;
auto & ptr_where = _m_ess().pointer_where;
if ((ptr_where.first == parts::list || ptr_where.first == parts::checker) && ptr_where.second != npos)
return _m_ess().lister.advance(_m_ess().first_display(), static_cast<int>(ptr_where.second));
return index_pair{ npos, npos };
}
bool listbox::sortable() const bool listbox::sortable() const
{ {
internal_scope_guard lock; internal_scope_guard lock;