From 0ffa3e54ca897d7a2400037abb67c70db29320a0 Mon Sep 17 00:00:00 2001 From: Jinhao Date: Tue, 17 Feb 2015 04:03:55 +0800 Subject: [PATCH] some improvements --- include/nana/deploy.hpp | 12 +++ include/nana/gui/layout_utility.hpp | 1 + source/deploy.cpp | 99 +++++++++++++++++++++ source/gui/animation.cpp | 18 ++-- source/gui/detail/win32/bedrock.cpp | 2 - source/gui/msgbox.cpp | 43 ++------- source/gui/widgets/date_chooser.cpp | 11 +-- source/gui/widgets/listbox.cpp | 130 +++++++++------------------- source/gui/widgets/spinbox.cpp | 4 +- source/gui/widgets/textbox.cpp | 16 +--- 10 files changed, 175 insertions(+), 161 deletions(-) diff --git a/include/nana/deploy.hpp b/include/nana/deploy.hpp index d417d06d..e59e0d85 100644 --- a/include/nana/deploy.hpp +++ b/include/nana/deploy.hpp @@ -50,6 +50,18 @@ namespace nana //Workaround for no implemenation of std::stod in MinGW. double stod(const std::string&, std::size_t * pos = nullptr); double stod(const std::wstring&, std::size_t* pos = nullptr); + + //Workaround for no implemenation of std::to_wstring in MinGW. + std::wstring to_wstring(long double); + std::wstring to_wstring(double); + std::wstring to_wstring(unsigned); + std::wstring to_wstring(int); + std::wstring to_wstring(long); + std::wstring to_wstring(unsigned long); + std::wstring to_wstring(long long); + std::wstring to_wstring(unsigned long long); + std::wstring to_wstring(float); + } #if defined(NANA_WINDOWS) diff --git a/include/nana/gui/layout_utility.hpp b/include/nana/gui/layout_utility.hpp index a963bbca..fae4f4c9 100644 --- a/include/nana/gui/layout_utility.hpp +++ b/include/nana/gui/layout_utility.hpp @@ -30,6 +30,7 @@ namespace nana /// Zoom the input_s to fit for ref_s void fit_zoom(const size& input_s, const size& ref_s, size& result_s); + size fit_zoom(const size& input_s, size ref_s); //zoom //@brief: Calculate the scaled rectangle by refer dst rectangle, that scale factor is same as that between scaled and refer. diff --git a/source/deploy.cpp b/source/deploy.cpp index 6017edfd..e9e0d5d2 100644 --- a/source/deploy.cpp +++ b/source/deploy.cpp @@ -133,6 +133,105 @@ namespace nana #endif } + std::wstring to_wstring(double v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(long double v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(unsigned v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(int v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(long v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(unsigned long v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(long long v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(unsigned long long v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + + std::wstring to_wstring(float v) + { +#ifdef NANA_MINGW + std::wstringstream ss; + ss << v; + return ss.str(); +#else + return std::to_wstring(v); +#endif + } + bool is_incomplete(const nana::string& str, unsigned pos) { #ifndef NANA_UNICODE diff --git a/source/gui/animation.cpp b/source/gui/animation.cpp index 0f4c1405..9ed5a7ea 100644 --- a/source/gui/animation.cpp +++ b/source/gui/animation.cpp @@ -36,12 +36,8 @@ namespace nana struct output_t { - drawing::diehard_t diehard; + drawing::diehard_t diehard{ nullptr }; std::vector points; - - output_t() - : diehard(nullptr) - {} }; struct framebuilder @@ -443,12 +439,14 @@ namespace nana for(auto thr : threads_) { std::lock_guardmutex)> privlock(thr->mutex); - - if ((thr->fps == p->fps) && (thr->performance_parameter / (thr->animations.size() + 1) <= 43.3)) + if (thr->fps == p->fps) { - p->thr_variable = thr; - thr->animations.push_back(p); - return; + if (thr->animations.empty() || (thr->performance_parameter * (1.0 + 1.0 / thr->animations.size()) <= 43.3)) + { + p->thr_variable = thr; + thr->animations.push_back(p); + return; + } } } diff --git a/source/gui/detail/win32/bedrock.cpp b/source/gui/detail/win32/bedrock.cpp index 36c50e69..555fcbaf 100644 --- a/source/gui/detail/win32/bedrock.cpp +++ b/source/gui/detail/win32/bedrock.cpp @@ -1008,8 +1008,6 @@ namespace detail arg.evt_code = event_code::mouse_up; emit_drawer(&drawer::mouse_up, msgwnd, arg, &context); - //auto evt_ptr = msgwnd->together.events_ptr; //deprecated - if (fire_click) { arg.evt_code = event_code::click; diff --git a/source/gui/msgbox.cpp b/source/gui/msgbox.cpp index a13f3c82..09c9b776 100644 --- a/source/gui/msgbox.cpp +++ b/source/gui/msgbox.cpp @@ -611,20 +611,13 @@ namespace nana //get the longest value int longest = (std::abs((impl->begin < 0 ? impl->begin * 10 : impl->begin)) < std::abs(impl->last < 0 ? impl->last * 10 : impl->last) ? impl->last : impl->begin); - std::wstringstream ss; - ss << longest; paint::graphics graph{ ::nana::size{ 10, 10 } }; - auto value_px = graph.text_extent_size(ss.str()).width + 34; + auto value_px = graph.text_extent_size(::nana::to_wstring(longest)).width + 34; impl->spinbox.create(impl->dock, rectangle{ static_cast(label_px + 10), 0, value_px, 0 }); impl->spinbox.range(impl->begin, impl->last, impl->step); - //impl->spinbox.set_accept_integer(); //deprecated - //Workaround for no implementation of std::to_wstring by MinGW. - ss.str(L""); - ss.clear(); - ss << impl->value; - impl->spinbox.value(ss.str()); + impl->spinbox.value(::nana::to_wstring(impl->value)); impl->dock.events().resized.connect_unignorable([impl, label_px, value_px](const ::nana::arg_resized& arg) { @@ -698,20 +691,13 @@ namespace nana //get the longest value auto longest = (std::abs((impl->begin < 0 ? impl->begin * 10 : impl->begin)) < std::abs(impl->last < 0 ? impl->last * 10 : impl->last) ? impl->last : impl->begin); - std::wstringstream ss; - ss << longest; paint::graphics graph{ ::nana::size{ 10, 10 } }; - auto value_px = graph.text_extent_size(ss.str()).width + 34; + auto value_px = graph.text_extent_size(::nana::to_wstring(longest)).width + 34; impl->spinbox.create(impl->dock, rectangle{ static_cast(label_px + 10), 0, value_px, 0 }); impl->spinbox.range(impl->begin, impl->last, impl->step); - //impl->spinbox.set_accept_real(); //deprecated - //Workaround for no implementation of std::to_wstring by MinGW. - ss.str(L""); - ss.clear(); - ss << impl->value; - impl->spinbox.value(ss.str()); + impl->spinbox.value(::nana::to_wstring(impl->value)); impl->dock.events().resized.connect_unignorable([impl, label_px, value_px](const ::nana::arg_resized& arg) { @@ -859,9 +845,7 @@ namespace nana ::nana::string inputbox::date::value() const { - std::wstringstream ss; - ss << impl_->month << L'-' << impl_->day << L", " << impl_->year; - return ss.str(); + return nana::to_wstring(impl_->month) + L'-' + nana::to_wstring(impl_->day) + L", " + nana::to_wstring(impl_->year); } int inputbox::date::year() const @@ -907,22 +891,15 @@ namespace nana left += 104; impl->wdg_day.create(impl->dock, rectangle{ left, 0, 38, 0 }); impl->wdg_day.range(1, ::nana::date::month_days(today.year, today.month), 1); - //impl->wdg_day.set_accept_integer(); //deprecated left += 48; impl->wdg_year.create(impl->dock, rectangle{left, 0, 50, 0}); impl->wdg_year.range(1601, 9999, 1); - //impl->wdg_year.set_accept_integer(); //deprecated impl->wdg_month.option(today.month - 1); - std::wstringstream ss; - ss << today.day; - impl->wdg_day.value(ss.str()); - ss.str(L""); - ss.clear(); - ss << today.year; - impl->wdg_year.value(ss.str()); + impl->wdg_day.value(::nana::to_wstring(today.day)); + impl->wdg_year.value(::nana::to_wstring(today.year)); impl->dock.events().resized.connect_unignorable([impl, label_px](const ::nana::arg_resized& arg) { @@ -962,10 +939,8 @@ namespace nana if (day > days) day = days; - - std::wstringstream ss; - ss << day; - impl->wdg_day.value(ss.str()); + + impl->wdg_day.value(::nana::to_wstring(day)); }; impl->wdg_year.events().text_changed.connect_unignorable(make_days); diff --git a/source/gui/widgets/date_chooser.cpp b/source/gui/widgets/date_chooser.cpp index bb0cede5..16b493c3 100644 --- a/source/gui/widgets/date_chooser.cpp +++ b/source/gui/widgets/date_chooser.cpp @@ -127,15 +127,13 @@ namespace nana if(graph.width() > 32 + border_size * 2) { - std::stringstream ss; - ss< - std::wstring to_wstring(Int n) - { - std::wstringstream ss; - ss << n; - return ss.str(); - } - //definition of iresolver/oresolver oresolver& oresolver::operator<<(bool n) { @@ -96,77 +87,66 @@ namespace nana } oresolver& oresolver::operator<<(short n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(unsigned short n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(int n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(unsigned int n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(long n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(unsigned long n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(long long n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(unsigned long long n) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(n)); + cells_.emplace_back(::nana::to_wstring(n)); return *this; } oresolver& oresolver::operator<<(float f) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(f)); + cells_.emplace_back(::nana::to_wstring(f)); return *this; } oresolver& oresolver::operator<<(double f) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(f)); + cells_.emplace_back(::nana::to_wstring(f)); return *this; } oresolver& oresolver::operator<<(long double f) { - //A workaround, MinGW does not yet provide std::to_wstring - cells_.emplace_back(to_wstring(f)); + cells_.emplace_back(::nana::to_wstring(f)); return *this; } @@ -2373,7 +2353,7 @@ namespace nana if(x < essence_->scroll.offset_x) x = essence_->scroll.offset_x; else if(x > essence_->scroll.offset_x + static_cast(rect.width)) - x = essence_->scroll.offset_x + rect.width; + x = essence_->scroll.offset_x + static_cast(rect.width); size_type i = essence_->header.item_by_x(x); if(i == npos) @@ -2537,6 +2517,8 @@ namespace nana auto idx = essence_->scroll.offset_y; auto state = item_state::normal; + + const bool sort_enabled = (essence_->lister.sort_index() != npos); //Here draws a root categ or a first drawing is not a categ. if(idx.cat == 0 || !idx.is_category()) { @@ -2546,30 +2528,16 @@ namespace nana idx.item = 0; } - //Test whether the sort is enabled. - if(essence_->lister.sort_index() != npos) + std::size_t size = i_categ->items.size(); + for(std::size_t offs = essence_->scroll.offset_y.item; offs < size; ++offs, ++idx.item) { - std::size_t size = i_categ->items.size(); - for(std::size_t offs = essence_->scroll.offset_y.item; offs < size; ++offs, ++idx.item) - { - if(n-- == 0) break; - state = (tracker == idx ? item_state::highlighted : item_state::normal); + if(n-- == 0) break; + state = (tracker == idx ? item_state::highlighted : item_state::normal); - _m_draw_item(i_categ->items[lister.absolute(index_pair(idx.cat, offs))], x, y, txtoff, header_w, rect, subitems, bgcolor,fgcolor, state); - y += essence_->item_size; - } - } - else - { - for(auto i = i_categ->items.cbegin() + essence_->scroll.offset_y.item; i != i_categ->items.cend(); ++i, ++idx.item) - { - if(n-- == 0) break; - state = (tracker == idx ? item_state::highlighted : item_state::normal); - - _m_draw_item(*i, x, y, txtoff, header_w, rect, subitems, bgcolor, fgcolor, state); - y += essence_->item_size; - } + _m_draw_item(i_categ->items[sort_enabled ? lister.absolute(index_pair(idx.cat, offs)) : offs], x, y, txtoff, header_w, rect, subitems, bgcolor,fgcolor, state); + y += essence_->item_size; } + ++i_categ; ++idx.cat; } @@ -2584,35 +2552,18 @@ namespace nana _m_draw_categ(*i_categ, rect.x - essence_->scroll.offset_x, y, txtoff, header_w, rect, bgcolor, state); y += essence_->item_size; - if(false == i_categ->expand) continue; + if(false == i_categ->expand) + continue; - //Test whether the sort is enabled. - if(essence_->lister.sort_index() != npos) + auto size = i_categ->items.size(); + for(decltype(size) pos = 0; pos < size; ++pos) { - auto size = i_categ->items.size(); - for(decltype(size) pos = 0; pos < size; ++pos) - { - if(n-- == 0) break; - state = (idx == tracker ? item_state::highlighted : item_state::normal); + if(n-- == 0) break; + state = (idx == tracker ? item_state::highlighted : item_state::normal); - _m_draw_item(i_categ->items[lister.absolute(index_pair(idx.cat, pos))], x, y, txtoff, header_w, rect, subitems, bgcolor, fgcolor, state); - y += essence_->item_size; - ++idx.item; - } - } - else - { - for(auto & m : i_categ->items) - { - if(n-- == 0) break; - - state = (idx == tracker ? item_state::highlighted : item_state::normal); - - _m_draw_item(m, x, y, txtoff, header_w, rect, subitems, bgcolor, fgcolor, state); - y += essence_->item_size; - - ++idx.item; - } + _m_draw_item(i_categ->items[sort_enabled ? lister.absolute(index_pair(idx.cat, pos)) : pos], x, y, txtoff, header_w, rect, subitems, bgcolor, fgcolor, state); + y += essence_->item_size; + ++idx.item; } } @@ -2627,25 +2578,24 @@ namespace nana { bool sel = categ.selected(); if(sel && (categ.expand == false)) - bgcolor = nana::color(0xD5, 0xEF, 0xFC); + bgcolor = static_cast(0xD5EFFC); if (state == item_state::highlighted) - bgcolor = bgcolor.blend(::nana::color(0x99, 0xde, 0xfd), 0.8); + bgcolor = bgcolor.blend(static_cast(0x99defd), 0.8); auto graph = essence_->graph; - graph->set_color(bgcolor); - graph->rectangle(rectangle{ x, y, width, essence_->item_size }, true); + graph->rectangle(rectangle{ x, y, width, essence_->item_size }, true, bgcolor); + + color txt_color{ static_cast(0x3399) }; facade arrow("double"); arrow.direction(categ.expand ? ::nana::direction::north : ::nana::direction::south); ::nana::rectangle arrow_r{ x + 5, y + static_cast(essence_->item_size - 16) / 2, 16, 16 }; - arrow.draw(*graph, {}, static_cast(0x3399), arrow_r, element_state::normal); + arrow.draw(*graph, {}, txt_color, arrow_r, element_state::normal); - graph->string({ x + 20, y + txtoff }, categ.text, {0, 0x33, 0x99}); + graph->string({ x + 20, y + txtoff }, categ.text, txt_color); - std::stringstream ss; - ss<<'('<(categ.items.size())<<')'; - nana::string str = nana::charset(ss.str()); + ::nana::string str = L'(' + ::nana::to_wstring(categ.items.size()) + L')'; unsigned str_w = graph->text_extent_size(str).width; @@ -2655,7 +2605,7 @@ namespace nana if (x + 35 + text_s.width + str_w < x + width) { ::nana::point pos{ x + 30 + static_cast(text_s.width + str_w), y + static_cast(essence_->item_size) / 2 }; - graph->line(pos, { x + static_cast(width)-5, pos.y }, { 0x0, 0x33, 0x99 }); + graph->line(pos, { x + static_cast(width)-5, pos.y }, txt_color); } //Draw selecting inner rectangle if(sel && categ.expand == false) @@ -2845,7 +2795,7 @@ namespace nana auto & graph = *essence_->graph; auto size = graph.size(); //Draw Border - graph.rectangle(false, {0x9c, 0xb6, 0xc5}); + graph.rectangle(false, static_cast(0x9cb6c5)); graph.line({ 1, 1 }, {1, static_cast(size.height) - 2}, colors::white); graph.line({ static_cast(size.width) - 2, 1 }, { static_cast(size.width) - 2, static_cast(size.height) - 2 }); diff --git a/source/gui/widgets/spinbox.cpp b/source/gui/widgets/spinbox.cpp index 94a11715..a662d294 100644 --- a/source/gui/widgets/spinbox.cpp +++ b/source/gui/widgets/spinbox.cpp @@ -73,9 +73,7 @@ namespace nana std::wstring value() const override { - std::wstringstream ss; - ss << value_; - return ss.str(); + return ::nana::to_wstring(value_); } bool value(const std::wstring& value_str, bool & diff) override diff --git a/source/gui/widgets/textbox.cpp b/source/gui/widgets/textbox.cpp index fc4a7d1b..ffbacb05 100644 --- a/source/gui/widgets/textbox.cpp +++ b/source/gui/widgets/textbox.cpp @@ -456,25 +456,13 @@ namespace drawerbase { textbox& textbox::from(int n) { -#ifdef NANA_UNICODE - std::wstringstream ss; -#else - std::stringstream ss; -#endif - ss << n; - _m_caption(ss.str()); + _m_caption(::nana::to_wstring(n)); return *this; } textbox& textbox::from(double d) { -#ifdef NANA_UNICODE - std::wstringstream ss; -#else - std::stringstream ss; -#endif - ss << d; - _m_caption(ss.str()); + _m_caption(::nana::to_wstring(d)); return *this; }