From e720866a94f6c55c29ccda45cc99b845c1426bbb Mon Sep 17 00:00:00 2001 From: Jinhao Date: Fri, 1 May 2015 18:01:32 +0800 Subject: [PATCH] refactor class place --- source/gui/place.cpp | 128 +++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 85 deletions(-) diff --git a/source/gui/place.cpp b/source/gui/place.cpp index d42b6b2c..e7294ce9 100644 --- a/source/gui/place.cpp +++ b/source/gui/place.cpp @@ -559,18 +559,22 @@ namespace nana return token::identifier; } - std::string err = "an invalid character '"; - err += *sp_; - err += "'"; - - _m_throw_error(err); + _m_throw_error(*sp_); return token::error; //Useless, just for syntax correction. } private: void _m_throw_error(char err_char) + { + std::string str = "place: invalid character '"; + str += err_char; + str += '\''; + _m_throw_error(str); + } + + void _m_throw_error(const std::string& err) { std::stringstream ss; - ss << "place: invalid character '" << err_char << "' at " << static_cast(sp_ - divstr_); + ss << "place: " << err << " at " << static_cast(sp_ - divstr_); throw std::runtime_error(ss.str()); } @@ -579,8 +583,7 @@ namespace nana if (token::equal != read()) _m_throw_error("an equal sign is required after '" + idstr_ + "'"); - const char* p = sp_; - for (; *p == ' '; ++p); + const char* p = _m_eat_whitespace(sp_); auto neg_ptr = p; if ('-' == *p) @@ -599,8 +602,7 @@ namespace nana if (token::equal != read()) _m_throw_error("an equal sign is required after '" + idstr + "'"); - const char* p = sp_; - for (; *p == ' ' || *p == '\t'; ++p); + const char* p = _m_eat_whitespace(sp_); reparray_.reset(); auto tk = read(); @@ -620,14 +622,7 @@ namespace nana } } - void _m_throw_error(const std::string& err) - { - std::stringstream ss; - ss << "place: " << err << " at " << static_cast(sp_ - divstr_); - throw std::runtime_error(ss.str()); - } - - const char* _m_eat_whitespace(const char* sp) + static const char* _m_eat_whitespace(const char* sp) { while (*sp && !isgraph(*sp)) ++sp; @@ -678,7 +673,7 @@ namespace nana if (gotcha) { - for (; *sp == ' ' || *sp == '\t'; ++sp); + sp = _m_eat_whitespace(sp); if ('%' == *sp) { if (number_t::kind::integer == number_.kind_of()) @@ -758,6 +753,20 @@ namespace nana for (auto & e : fastened) API::show_window(e.handle, vsb); } + + static event_handle erase_element(std::vector& elements, window handle) + { + for (auto i = elements.begin(), end = elements.end(); i != end; ++i) + { + if (i->handle == handle) + { + auto evt_destroy = i->evt_destroy; + elements.erase(i); + return evt_destroy; + } + } + return nullptr; + } private: //The defintion is moved after the definition of class division template @@ -769,15 +778,8 @@ namespace nana { return API::events(wd).destroy.connect([this](const arg_destroy& arg) { - for (auto i = elements.begin(), end = elements.end(); i != end; ++i) - { - if (arg.window_handle == i->handle) - { - elements.erase(i); - break; - } - } - place_ptr_->collocate(); + if (erase_element(elements, arg.window_handle)) + place_ptr_->collocate(); }); } @@ -816,13 +818,7 @@ namespace nana //does not change the layout. auto evt = API::events(wd).destroy([this](const arg_destroy& arg) { - auto destroyed_wd = arg.window_handle; - auto i = std::find_if(fastened.begin(), fastened.end(), [destroyed_wd](element_t& e){ - return (e.handle == destroyed_wd); - }); - - if (i != fastened.end()) - fastened.erase(i); + erase_element(fastened, arg.window_handle); }); fastened.emplace_back(wd, evt); @@ -930,16 +926,9 @@ namespace nana { for (auto & child : div->children) { - if (child->field) - { - if (vsb) - { - if (child->visible) - child->field->visible(true); - } - else - child->field->visible(false); - } + if (child->field && (!vsb || child->visible)) + child->field->visible(vsb); + _m_visible_for_child(child.get(), vsb); } } @@ -1511,17 +1500,6 @@ namespace nana class place::implement::div_splitter : public division { - struct div_block - { - division * div; - int pixels; - double scale; - - div_block(division* d, int px) - : div(d), pixels(px) - {} - }; - enum{splitter_px = 4}; public: div_splitter(place_parts::number_t init_weight) @@ -1534,14 +1512,9 @@ namespace nana this->weight.assign(splitter_px); } - void leaf_left(division * d) + void set_leaf(bool is_left, division * d) { - leaf_left_ = d; - } - - void leaf_right(division * d) - { - leaf_right_ = d; + (is_left ? leaf_left_ : leaf_right_) = d; } void direction(bool horizontal) @@ -1787,7 +1760,7 @@ namespace nana if (!children.empty() && (division::kind::splitter != children.back()->kind_of_division)) { auto splitter = new div_splitter(tknizer.number()); - splitter->leaf_left(children.back().get()); + splitter->set_leaf(true, children.back().get()); children.back()->div_next = splitter; children.emplace_back(splitter); } @@ -1799,7 +1772,7 @@ namespace nana { children.back()->div_next = div.get(); if (division::kind::splitter == children.back()->kind_of_division) - dynamic_cast(*children.back()).leaf_right(div.get()); + dynamic_cast(*children.back()).set_leaf(false, div.get()); } children.emplace_back(div.release()); } @@ -2184,29 +2157,14 @@ namespace nana bool recollocate = false; for (auto & fld : impl_->fields) { - auto & elements = fld.second->elements; - for (auto i = elements.begin(); i != elements.end();) + auto evt = fld.second->erase_element(fld.second->elements, handle); + if (evt) { - if (i->handle == handle) - { - API::umake_event(i->evt_destroy); - i = elements.erase(i); - recollocate |= (nullptr != fld.second->attached); - } - else - ++i; + API::umake_event(evt); + recollocate |= (nullptr != fld.second->attached); } - auto i = std::find_if(fld.second->fastened.begin(), fld.second->fastened.end(), [handle](implement::field_impl::element_t& e) - { - return (e.handle == handle); - }); - - if (i != fld.second->fastened.end()) - { - API::umake_event(i->evt_destroy); - fld.second->fastened.erase(i); - } + API::umake_event( fld.second->erase_element(fld.second->fastened, handle)); } if (recollocate)