From 4cfe366758c9126eb7e246df8f87e76bad049080 Mon Sep 17 00:00:00 2001 From: Jinhao Date: Thu, 1 Oct 2015 02:51:30 +0800 Subject: [PATCH] code review of listbox --- include/nana/gui/widgets/listbox.hpp | 2 - source/gui/widgets/listbox.cpp | 267 ++++++++++++--------------- 2 files changed, 122 insertions(+), 147 deletions(-) diff --git a/include/nana/gui/widgets/listbox.hpp b/include/nana/gui/widgets/listbox.hpp index 140d6dd2..e2a6482c 100644 --- a/include/nana/gui/widgets/listbox.hpp +++ b/include/nana/gui/widgets/listbox.hpp @@ -185,9 +185,7 @@ namespace nana public: trigger(); ~trigger(); - //essence_t& essence(); //deprecated essence_t& essence() const; - void draw(); private: void _m_draw_border(); private: diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index 85646f3e..b9e0df55 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -388,15 +388,14 @@ namespace nana sortable_ = enable; } - std::function fetch_comp(std::size_t index) const + std::function fetch_comp(std::size_t pos) const { - if(index < cont_.size()) + try + { + return column_ref(pos).weak_ordering; + } + catch (...) { - for(auto & m : cont_) - { - if(m.index == index) - return m.weak_ordering; - } } return{}; } @@ -409,20 +408,17 @@ namespace nana void item_width(size_type pos, unsigned width) { - for(auto & m : cont_) - if(m.index == pos) - { - m.pixels = width; - return; - } + column(pos).pixels = width; } - unsigned item_width(size_type pos) const + unsigned item_width(size_type pos) const throw() { - for (auto & m : cont_) + try + { + return column_ref(pos).pixels; + } + catch (...) { - if (m.index == pos) - return m.pixels; } return 0; } @@ -535,9 +531,8 @@ namespace nana /// 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) return; - if(index >= cont_.size()) return; - if(to >= cont_.size()) return; + if ((index == to) || (index >= cont_.size()) || (to >= cont_.size())) + return; for (auto i = cont_.begin(); i != cont_.end(); ++i) { @@ -711,7 +706,7 @@ namespace nana nana::any * anyobj(const index_pair& id, bool allocate_if_empty) const { - auto& catobj = *_m_at(id.cat); + auto& catobj = *get(id.cat); if(id.item < catobj.items.size()) { auto& item = catobj.items[id.item]; @@ -880,22 +875,13 @@ namespace nana /// add a new cat created at "pos" and return a ref to it category_t* create_cat(std::size_t pos, nana::string&& text) { -#if defined(NANA_LINUX) || defined(NANA_MINGW) - //Call begin instead of cbegin, because the first parameter - //of emplace is not const_iterator in GCC's C++ standard - //library implementation. - auto i = list_.begin(); -#else - auto i = list_.cbegin(); -#endif - std::advance(i, pos); - return &(*list_.emplace(i, std::move(text))); + return &(*list_.emplace(this->get(pos), std::move(text))); } /// Insert before item in absolute "pos" a new item with "text" in column 0, and place it in last display position of this cat bool insert(const index_pair& pos, nana::string&& text) { - auto & catobj = *_m_at(pos.cat); + auto & catobj = *get(pos.cat); const auto n = catobj.items.size(); if (pos.item > n) @@ -914,7 +900,7 @@ namespace nana /// 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 { - auto & catobj = *_m_at(cat); + auto & catobj = *get(cat); if (display_order_pos >= catobj.sorted.size()) throw std::out_of_range("listbox: Invalid item position."); @@ -924,7 +910,7 @@ namespace nana /// 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 = *_m_at(cat); + auto& catobj = *get(cat); if (pos >= catobj.sorted.size()) throw std::out_of_range("listbox: Invalid item position."); @@ -934,15 +920,16 @@ namespace nana return npos ; } - + /* category_t& at(std::size_t cat_pos) { return *(_m_at(cat_pos)); } + */ category_t::container::value_type& at_abs(const index_pair& pos) { - return _m_at(pos.cat)->items.at(pos.item); + return get(pos.cat)->items.at(pos.item); } /// 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. @@ -953,7 +940,7 @@ namespace nana if (sorted_index_ != npos) index = absolute(pos); - return _m_at(pos.cat)->items.at(index); + return get(pos.cat)->items.at(index); } const category_t::container::value_type& at(const index_pair& pos) const @@ -963,12 +950,12 @@ namespace nana if (sorted_index_ != npos) index = absolute(pos); - return _m_at(pos.cat)->items.at(index); + return get(pos.cat)->items.at(index); } void clear(size_type cat) { - auto& catobj = *_m_at(cat); + auto& catobj = *get(cat); catobj.items.clear(); catobj.sorted.clear(); } @@ -1051,7 +1038,7 @@ namespace nana std::swap(from, to); size_type n = 0; - auto i = _m_at(from.cat); + auto i = get(from.cat); if(from.item == npos) { if(i->expand) @@ -1130,7 +1117,7 @@ namespace nana void erase(size_type cat) { - auto i = _m_at(cat); + auto i = get(cat); //If the category is the first one, it just clears the items instead of removing whole category. if(0 == cat) @@ -1156,7 +1143,7 @@ namespace nana { if(good(cat)) { - auto & expanded = _m_at(cat)->expand; + auto & expanded = get(cat)->expand; if(expanded != exp) { expanded = exp; @@ -1168,7 +1155,7 @@ namespace nana bool expand(size_type cat) const { - return (good(cat) ? _m_at(cat)->expand : false); + return (good(cat) ? get(cat)->expand : false); } container& cat_container() @@ -1394,8 +1381,7 @@ namespace nana if (for_selection ? single_selection_category_limited_ : single_check_category_limited_) { - auto i = list_.begin(); - std::advance(i, except.cat); + auto i = this->get(except.cat); std::size_t item_pos = 0; for (auto & m : i->items) @@ -1541,12 +1527,12 @@ namespace nana size_type size_item(size_type cat) const { - return _m_at(cat)->items.size(); + return get(cat)->items.size(); } bool categ_checked(size_type cat) const { - auto & items = _m_at(cat)->items; + auto& items = get(cat)->items; for(auto & m : items) { if(m.flags.checked == false) @@ -1558,7 +1544,7 @@ namespace nana bool categ_checked(size_type cat, bool ck) { bool changed = false; - auto & items = _m_at(cat)->items; + auto & items = get(cat)->items; size_type index = 0; for(auto & m : items) { @@ -1585,7 +1571,7 @@ namespace nana bool categ_selected(size_type cat) const { - auto & items = _m_at(cat)->items; + auto & items = get(cat)->items; for(auto & m : items) if(m.flags.selected == false) return false; @@ -1661,7 +1647,7 @@ namespace nana return true; } - auto i = _m_at(pos.cat); // pos is not a cat and i point to it cat + auto i = get(pos.cat); // pos is not a cat and i point to it cat if (pos.item < i->items.size()) { item = pos; // good item, return it @@ -1682,8 +1668,7 @@ namespace nana if(sorted_index_ == npos || display_pos.item == npos) return display_pos.item ; - auto & catobj = *_m_at(display_pos.cat); - + auto & catobj = *get(display_pos.cat); if(catobj.items.empty()) return (display_pos == index_pair{0,0} ? 0 : npos); @@ -1702,7 +1687,7 @@ namespace nana if (sorted_index_ == npos) return pos.item ; - auto & catobj = *_m_at(pos.cat); + auto& catobj = *get(pos.cat); for (size_type i=0; iexpand ? cat->items.size() : 0); @@ -1768,7 +1753,7 @@ namespace nana if(good(from.cat)) { - auto i = _m_at(from.cat); + auto i = get(from.cat); size_type n = (from.is_category() ? 1 : from.item + 2); // ?? if(n <= offs) { @@ -1802,25 +1787,25 @@ namespace nana } return false; } - private: + /// categories iterator - container::iterator _m_at(size_type index) + container::iterator get(size_type pos) { - if(index >= list_.size()) + if (pos >= list_.size()) throw std::out_of_range("nana::listbox: invalid category index"); auto i = list_.begin(); - std::advance(i, index); + std::advance(i, pos); return i; } - - container::const_iterator _m_at(size_type index) const + + container::const_iterator get(size_type pos) const { - if(index >= list_.size()) + if (pos >= list_.size()) throw std::out_of_range("nana::listbox: invalid category index"); auto i = list_.cbegin(); - std::advance(i, index); + std::advance(i, pos); return i; } public: @@ -2065,6 +2050,25 @@ namespace nana unsigned width = sz.width - 2 - (v ? scroll.scale : 0); unsigned height = sz.height - 2 - (h ? scroll.scale : 0); + //event hander for scrollbars + auto evt_fn = [this](const arg_scroll& arg) + { + 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) + return; + + set_scroll_y_dpl(item); + } + else + scroll.offset_x = static_cast(scroll.h.value()); + + API::refresh_window(this->lister.wd_ptr()->handle()); + }; + if(h) { rectangle r(1, sz.height - scroll.scale - 1, width, scroll.scale); @@ -2072,12 +2076,7 @@ namespace nana { scroll.h.create(wd, r); API::take_active(scroll.h.handle(), false, wd); - - scroll.h.events().value_changed.connect_unignorable([this](const ::nana::arg_scroll& arg) - { - scroll.offset_x = static_cast(scroll.h.value()); - API::refresh_window(this->lister.wd_ptr()->handle()); - }); + scroll.h.events().value_changed.connect_unignorable(evt_fn); } else scroll.h.move(r); @@ -2092,19 +2091,7 @@ namespace nana { scroll.v.create(wd, r); API::take_active(scroll.v.handle(), false, wd); - - scroll.v.events().value_changed([this](const ::nana::arg_scroll& arg) - { - index_pair item; - if (!lister.forward(item, scroll.v.value(), item)) return; - - if (item == scroll.offset_y_dpl) - return; - - set_scroll_y_dpl(item); - - API::refresh_window(this->lister.wd_ptr()->handle()); - }); + scroll.v.events().value_changed.connect_unignorable(evt_fn); } else scroll.v.move(r); @@ -2150,8 +2137,7 @@ namespace nana int item_xpos(const nana::rectangle& r) const { - std::vector seq; - header_seq(seq, r.width); + auto seq = header_seq(r.width); return (seq.size() ? (header.item_pos(seq[0], nullptr) - scroll.offset_x + r.x) : 0); } @@ -2277,18 +2263,21 @@ namespace nana return true; } - void header_seq(std::vector &seqs, unsigned lister_w)const + std::vector header_seq(unsigned lister_w)const { - int x = - (scroll.offset_x); - for(const auto& hd : header.cont()) + std::vector seqs; + int x = -(scroll.offset_x); + for (const auto& hd : header.cont()) { - if(false == hd.visible) continue; + if (false == hd.visible) continue; x += static_cast(hd.pixels); - if(x > 0) + if (x > 0) seqs.push_back(hd.index); - if(x >= static_cast(lister_w)) + if (x >= static_cast(lister_w)) break; } + + return seqs; } unsigned auto_width(size_type pos, unsigned max = 3000) /// \todo introduce parametr max_header_width @@ -2398,7 +2387,7 @@ namespace nana void es_lister::erase(const index_pair& pos) { - auto & catobj = *_m_at(pos.cat); + auto & catobj = *get(pos.cat); if (pos.item < catobj.items.size()) { catobj.items.erase(catobj.items.begin() + pos.item); @@ -2782,8 +2771,7 @@ namespace nana 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); - std::vector subitems; - essence_->header_seq(subitems, rect.width); + auto subitems = essence_->header_seq(rect.width); if(subitems.empty()) return; @@ -2792,8 +2780,7 @@ namespace nana int y = rect.y; int txtoff = (essence_->item_size - essence_->text_height) / 2; - auto i_categ = lister.cat_container().cbegin(); - std::advance(i_categ, essence_->scroll.offset_y_dpl.cat); + auto i_categ = lister.get(essence_->scroll.offset_y_dpl.cat); auto idx = essence_->scroll.offset_y_dpl; @@ -2859,17 +2846,14 @@ namespace nana essence_->inline_buffered_table.clear(); - if (y < rect.y + static_cast(rect.height)) - { - essence_->graph->set_color(bgcolor); - essence_->graph->rectangle(rectangle{ rect.x, y, rect.width, rect.y + rect.height - y }, true); - } + if (y < rect.bottom()) + essence_->graph->rectangle(rectangle{ rect.x, y, rect.width, static_cast(rect.bottom() - y) }, true, bgcolor); } 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 { - bool sel = categ.selected(); - if(sel && (categ.expand == false)) + const bool sel = categ.selected(); + if (sel && (categ.expand == false)) bgcolor = static_cast(0xD5EFFC); if (state == item_state::highlighted) @@ -2889,18 +2873,19 @@ namespace nana ::nana::string str = L'(' + std::to_wstring(categ.items.size()) + L')'; - unsigned str_w = graph->text_extent_size(str).width; + auto text_s = graph->text_extent_size(categ.text).width; + auto extend_text_w = text_s + graph->text_extent_size(str).width; - auto text_s = graph->text_extent_size(categ.text); - graph->string({ x + 25 + static_cast(text_s.width), y + txtoff }, str); + graph->string({ x + 25 + static_cast(text_s), y + txtoff }, str); - if (x + 35 + text_s.width + str_w < x + width) + if (x + 35 + extend_text_w < x + width) { - ::nana::point pos{ x + 30 + static_cast(text_s.width + str_w), y + static_cast(essence_->item_size) / 2 }; + ::nana::point pos{ x + 30 + static_cast(extend_text_w), y + static_cast(essence_->item_size) / 2 }; graph->line(pos, { x + static_cast(width)-5, pos.y }, txt_color); } + //Draw selecting inner rectangle - if(sel && categ.expand == false) + if (sel && (categ.expand == false)) { width -= essence_->scroll.offset_x; _m_draw_border(r.x, y, (r.width < width ? r.width : width)); @@ -2934,8 +2919,7 @@ namespace nana auto graph = essence_->graph; //draw the background - graph->set_color(bgcolor); - graph->rectangle(rectangle{ content_r.x, y, show_w, essence_->item_size }, true); + graph->rectangle(rectangle{ content_r.x, y, show_w, essence_->item_size }, true, bgcolor); int item_xpos = x; unsigned extreme_text = x; @@ -3113,7 +3097,7 @@ namespace nana essence_t::inline_pane* _m_find_inline_pane(const index_pair& pos, std::size_t column_pos) const { - auto & cat = essence_->lister.at(pos.cat); + auto & cat = *essence_->lister.get(pos.cat); if (column_pos >= cat.factories.size()) return nullptr; @@ -3138,10 +3122,9 @@ namespace nana { //Draw selecting inner rectangle auto graph = essence_->graph; - graph->rectangle({ x, y, width, essence_->item_size }, false, { 0x99, 0xDE, 0xFD }); + graph->rectangle({ x, y, width, essence_->item_size }, false, static_cast(0x99defd)); - graph->set_color(colors::white); - graph->rectangle({ x + 1, y + 1, width - 2, essence_->item_size - 2 }, false); + graph->rectangle({ x + 1, y + 1, width - 2, essence_->item_size - 2 }, false, colors::white); graph->set_pixel(x, y); graph->set_pixel(x, y + essence_->item_size - 1); graph->set_pixel(x + width - 1, y); @@ -3171,20 +3154,6 @@ namespace nana return *essence_; } - void trigger::draw() - { - if (API::is_destroying(essence_->lister.wd_ptr()->handle())) - return; - - nana::rectangle r; - - if(essence_->header.visible() && essence_->rect_header(r)) - drawer_header_->draw(r); - if(essence_->rect_lister(r)) - drawer_lister_->draw(r); - _m_draw_border(); - } - void trigger::_m_draw_border() { if (API::widget_borderless(*essence_->lister.wd_ptr())) @@ -3229,7 +3198,16 @@ namespace nana void trigger::refresh(graph_reference) { - draw(); + if (API::is_destroying(essence_->lister.wd_ptr()->handle())) + return; + + nana::rectangle r; + + if (essence_->header.visible() && essence_->rect_header(r)) + drawer_header_->draw(r); + if (essence_->rect_lister(r)) + drawer_lister_->draw(r); + _m_draw_border(); } void trigger::mouse_move(graph_reference graph, const arg_mouse& arg) @@ -3291,7 +3269,7 @@ namespace nana if (update) { if (2 == update) - draw(); + refresh(graph); API::lazy_refresh(); } @@ -3309,7 +3287,7 @@ namespace nana essence_->ptr_state = item_state::normal; } - draw(); + refresh(graph); API::lazy_refresh(); } } @@ -3425,7 +3403,7 @@ namespace nana if(essence_->lister.sort_index(essence_->pointer_where.second)) { essence_->trace_item_dpl({0,0}); - draw(); + refresh(graph); API::lazy_refresh(); } } @@ -3435,7 +3413,7 @@ namespace nana nana::point pos = arg.pos; essence_->widget_to_header(pos); drawer_header_->grab(pos, false); - draw(); + refresh(graph); API::lazy_refresh(); API::capture_window(essence_->lister.wd_ptr()->handle(), false); } @@ -3445,7 +3423,7 @@ namespace nana { if(essence_->wheel(arg.upwards)) { - draw(); + refresh(graph); essence_->adjust_scroll_value(); API::lazy_refresh(); } @@ -3484,7 +3462,7 @@ namespace nana offset_y = last; } essence_->adjust_scroll_life(); - draw(); + refresh(graph); API::lazy_refresh(); } } @@ -3492,7 +3470,7 @@ namespace nana void trigger::resized(graph_reference graph, const arg_resized&) { essence_->adjust_scroll_life(); - draw(); + refresh(graph); API::lazy_refresh(); } @@ -3565,7 +3543,7 @@ namespace nana default: return; } - draw(); + refresh(graph); API::lazy_refresh(); } @@ -3578,12 +3556,12 @@ namespace nana export_options exp_opt {essence_->def_export_options()}; exp_opt.columns_order = essence_->header.all_headers(true); exp_opt.only_selected_items = true; - nana::system::dataexch().set(essence_->to_string(exp_opt)); + ::nana::system::dataexch().set(essence_->to_string(exp_opt)); return; } case keyboard::select_all : essence_->lister.select_for_all(true); - draw(); + refresh(graph); API::lazy_refresh(); break; @@ -3605,8 +3583,9 @@ namespace nana { if (ess) { - auto i = ess_->lister.cat_container().begin(); - std::advance(i, pos.cat); + //auto i = ess_->lister.cat_container().begin(); + //std::advance(i, pos.cat); + auto i = ess_->lister.get(pos.cat); cat_ = &(*i); // what is pos is a cat? } } @@ -3949,8 +3928,7 @@ namespace nana //Behavior of a container item_proxy cat_proxy::begin() const { - auto i = ess_->lister.cat_container().begin(); - std::advance(i, pos_); + auto i = ess_->lister.get(pos_); if (i->items.empty()) return end(); @@ -4137,8 +4115,7 @@ namespace nana return; } - auto i = ess_->lister.cat_container().begin(); - std::advance(i, pos_); + auto i = ess_->lister.get(pos_); cat_ = &(*i); } //class cat_proxy