fix necessary copy, add comments and code review

This commit is contained in:
qPCR4vir 2015-04-27 01:00:26 +02:00
parent 585732a497
commit 4b4843bb7f
2 changed files with 49 additions and 46 deletions

View File

@ -17,6 +17,7 @@
#include "widget.hpp" #include "widget.hpp"
#include <nana/concepts.hpp> #include <nana/concepts.hpp>
#include <nana/key_type.hpp> #include <nana/key_type.hpp>
//#include <nana/paint/graphics.hpp>
#include <functional> #include <functional>
#include <initializer_list> #include <initializer_list>
@ -28,7 +29,7 @@ namespace nana
{ {
namespace listbox namespace listbox
{ {
typedef std::size_t size_type; using size_type = std::size_t ;
struct cell struct cell
{ {
@ -36,7 +37,7 @@ namespace nana
{ {
::nana::color bgcolor; ::nana::color bgcolor;
::nana::color fgcolor; ::nana::color fgcolor;
/// ::nana::paint::font font; \todo
format() = default; format() = default;
format(const ::nana::color& bgcolor, const ::nana::color& fgcolor); format(const ::nana::color& bgcolor, const ::nana::color& fgcolor);
}; };

View File

@ -313,14 +313,14 @@ namespace nana
} }
//end class iresolver/oresolver //end class iresolver/oresolver
class es_header /// Essence of the columns Header /// Essence of the columns Header
class es_header
{ {
public: public:
typedef std::size_t size_type;
struct column_t struct column_t
{ {
nana::string text; //< "text" header of the column number "index" with weigth "pixels" nana::string text; ///< "text" header of the column number "index" with weigth "pixels"
unsigned pixels; unsigned pixels;
bool visible{true}; bool visible{true};
size_type index; size_type index;
@ -337,10 +337,10 @@ namespace nana
export_options::columns_indexs all_headers(bool only_visibles) const export_options::columns_indexs all_headers(bool only_visibles) const
{ {
export_options::columns_indexs idx; export_options::columns_indexs idx;
for(auto hd : cont()) for(const auto &header : cont())
{ {
if(!only_visibles || hd.visible) if(header.visible || !only_visibles)
idx.push_back(hd.index); idx.push_back(header.index);
} }
return idx; return idx;
} }
@ -356,18 +356,7 @@ namespace nana
else else
head_str += exp_opt.sep; head_str += exp_opt.sep;
size_type index{exp_opt.columns_order[idx]}; head_str += column_ref(exp_opt.columns_order[idx]).text;
bool bad{true};
for (auto&col:cont())
if(col.index == index)
{
bad=false;
head_str += col.text;
break;
}
if(bad)
throw std::out_of_range("Trying to export from a lisboxt an inexisting column");
} }
return head_str; return head_str;
} }
@ -438,6 +427,7 @@ namespace nana
return pixels; return pixels;
} }
/// return the original order or index previous to any list reorganization of the current column "n".
size_type index(size_type n) const size_type index(size_type n) const
{ {
return (n < cont_.size() ? cont_[n].index : npos); return (n < cont_.size() ? cont_[n].index : npos);
@ -448,6 +438,7 @@ namespace nana
return cont_; return cont_;
} }
/// find and return a ref to the column that originaly was at position "pos" previous to any list reorganization.
column_t& column(size_type pos) column_t& column(size_type pos)
{ {
for(auto & m : cont_) for(auto & m : cont_)
@ -457,18 +448,29 @@ namespace nana
} }
throw std::out_of_range("Nana.GUI.Listbox: invalid header index."); throw std::out_of_range("Nana.GUI.Listbox: invalid header index.");
} }
const column_t& column_ref(size_type pos) const
{
for(const auto & m : cont_)
{
if(m.index == pos)
return m;
}
throw std::out_of_range("Nana.GUI.Listbox: invalid header index.");
}
/// return the original index of the current column at x .
size_type item_by_x(int x) const size_type item_by_x(int x) const
{ {
for(auto & m : cont_) for(const auto & col : cont_) // in current order
{ {
if(x < static_cast<int>(m.pixels)) if(x < static_cast<int>(col.pixels))
return m.index; return col.index;
x -= m.pixels; x -= col.pixels;
} }
return npos; return npos;
} }
/// return the left position of the column originaly at index "pos" .
int item_pos(size_type pos, unsigned * pixels) const int item_pos(size_type pos, unsigned * pixels) const
{ {
int left = 0; int left = 0;
@ -486,11 +488,11 @@ namespace nana
} }
return left; return left;
} }
/// 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 neighbor(size_type index, bool front) const
{ {
size_type n = npos; size_type n = npos;
for(auto i = cont_.cbegin(); i != cont_.cend(); ++i) for(auto i = cont_.cbegin(); i != cont_.cend(); ++i) // in current order
{ {
if(i->index == index) if(i->index == index)
{ {
@ -506,16 +508,17 @@ namespace nana
} }
return npos; return npos;
} }
/// return the original index of the currently first visible col
size_type begin() const size_type begin() const
{ {
for(auto & m : cont_) for(const auto & m : cont_)
{ {
if(m.visible) return m.index; if(m.visible) return m.index;
} }
return npos; return npos;
} }
/// return the original index of the currently last visible col
size_type last() const size_type last() const
{ {
for(auto i = cont_.rbegin(); i != cont_.rend(); ++i) for(auto i = cont_.rbegin(); i != cont_.rend(); ++i)
@ -524,26 +527,25 @@ namespace nana
} }
return npos; 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) void move(size_type index, size_type to, bool front)
{ {
if((index != to) && (index < cont_.size()) && (to < cont_.size())) if(index == to) return;
{ if(index >= cont_.size()) return;
auto i = std::find_if(cont_.begin(), cont_.end(), [index](container::value_type& m){ if(to >= cont_.size()) return;
return (index == m.index);
}); auto i = std::find_if(cont_.begin(), cont_.end(), [index](container::value_type& m){return (index == m.index);});
if (i == cont_.end()) if (i == cont_.end())
return; return;
auto from = *i; auto from = *i; // a temp copy
cont_.erase(i); cont_.erase(i);
i = std::find_if(cont_.begin(), cont_.end(), [to](const container::value_type& m)->bool{ return (to == m.index); } ); i = std::find_if(cont_.begin(), cont_.end(), [to](const container::value_type& m)->bool{ return (to == m.index); } );
if(i != cont_.end()) if(i != cont_.end())
cont_.insert((front ? i : ++i), from); cont_.insert((front ? i : ++i), from);
} }
}
private: private:
bool visible_{true}; bool visible_{true};
container cont_; container cont_;
@ -553,7 +555,7 @@ namespace nana
struct item_t struct item_t
{ {
typedef std::vector<cell> container; using container = std::vector<cell>;
container cells; container cells;
nana::color bgcolor; nana::color bgcolor;
@ -2463,13 +2465,13 @@ namespace nana
if(essence_->ptr_state == item_state::highlighted) if(essence_->ptr_state == item_state::highlighted)
{ {
x -= (r.x - essence_->scroll.offset_x); x -= (r.x - essence_->scroll.offset_x);
for(auto & hd : essence_->header.cont()) for(auto & hd : essence_->header.cont()) // in current order
{ {
if(hd.visible) if(hd.visible)
{ {
if((static_cast<int>(hd.pixels) - 2 < x) && (x < static_cast<int>(hd.pixels) + 3)) if((static_cast<int>(hd.pixels) - 2 < x) && (x < static_cast<int>(hd.pixels) + 3))
{ {
item_spliter_ = hd.index; item_spliter_ = hd.index; // original index
return true; return true;
} }
x -= hd.pixels; x -= hd.pixels;