From 5917b1eb36a1228b6870c938dc353098ffd3bc33 Mon Sep 17 00:00:00 2001 From: Jinhao Date: Wed, 29 Mar 2017 04:43:00 +0800 Subject: [PATCH] refactor --- include/nana/gui/detail/bedrock.hpp | 6 ++- source/gui/detail/bedrock_pi.cpp | 47 +++++++++---------- source/gui/detail/bedrock_windows.cpp | 4 +- source/gui/programming_interface.cpp | 65 ++------------------------- 4 files changed, 31 insertions(+), 91 deletions(-) diff --git a/include/nana/gui/detail/bedrock.hpp b/include/nana/gui/detail/bedrock.hpp index cda91c98..5462857d 100644 --- a/include/nana/gui/detail/bedrock.hpp +++ b/include/nana/gui/detail/bedrock.hpp @@ -1,7 +1,7 @@ /** * A Bedrock Implementation * Nana C++ Library(http://www.nanapro.org) - * Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com) + * Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com) * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at @@ -70,6 +70,9 @@ namespace detail element_store& get_element_store() const; void map_through_widgets(core_window_t*, native_drawable_type); + + //Closes the windows which are associated with the specified thread. If the given thread_id is 0, it closes all windows + void close_thread_window(unsigned thread_id); public: void event_expose(core_window_t *, bool exposed); void event_move(core_window_t*, int x, int y); @@ -92,7 +95,6 @@ namespace detail private: void _m_emit_core(event_code, core_window_t*, bool draw_only, const event_arg&); void _m_event_filter(event_code, core_window_t*, thread_context*); - void _m_except_handler(); private: static bedrock bedrock_object; diff --git a/source/gui/detail/bedrock_pi.cpp b/source/gui/detail/bedrock_pi.cpp index c6617bd7..2532e15d 100644 --- a/source/gui/detail/bedrock_pi.cpp +++ b/source/gui/detail/bedrock_pi.cpp @@ -1,7 +1,7 @@ /* * A Bedrock Platform-Independent Implementation * Nana C++ Library(http://www.nanapro.org) -* Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com) +* Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com) * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at @@ -119,6 +119,27 @@ namespace nana } } + void bedrock::close_thread_window(unsigned thread_id) + { + std::vector v; + wd_manager().all_handles(v); + + std::vector roots; + native_window_type root = nullptr; + for (auto wd : v) + { + if (((0 == thread_id) || (wd->thread_id == thread_id)) && (wd->root != root)) + { + root = wd->root; + if (roots.cend() == std::find(roots.cbegin(), roots.cend(), root)) + roots.emplace_back(root); + } + } + + for (auto i : roots) + native_interface::close_window(i); + } + void bedrock::event_expose(core_window_t * wd, bool exposed) { if (nullptr == wd) return; @@ -585,29 +606,5 @@ namespace nana throw std::runtime_error("Invalid event code"); } } - - void bedrock::_m_except_handler() - { - std::vector v; - wd_manager().all_handles(v); - if (v.size()) - { - std::vector roots; - native_window_type root = nullptr; - unsigned tid = nana::system::this_thread_id(); - for (auto wd : v) - { - if ((wd->thread_id == tid) && (wd->root != root)) - { - root = wd->root; - if (roots.cend() == std::find(roots.cbegin(), roots.cend(), root)) - roots.emplace_back(root); - } - } - - for (auto i : roots) - native_interface::close_window(i); - } - } }//end namespace detail }//end namespace nana \ No newline at end of file diff --git a/source/gui/detail/bedrock_windows.cpp b/source/gui/detail/bedrock_windows.cpp index 80a8fd12..4518863b 100644 --- a/source/gui/detail/bedrock_windows.cpp +++ b/source/gui/detail/bedrock_windows.cpp @@ -443,7 +443,7 @@ namespace detail ).show(); internal_scope_guard lock; - _m_except_handler(); + this->close_thread_window(nana::system::this_thread_id()); intr_locker.forward(); if (0 == --(context->event_pump_ref_count)) @@ -460,7 +460,7 @@ namespace detail << "\n in form: " << API::window_caption(condition_wd) ).show(); internal_scope_guard lock; - _m_except_handler(); + this->close_thread_window(nana::system::this_thread_id()); intr_locker.forward(); if(0 == --(context->event_pump_ref_count)) diff --git a/source/gui/programming_interface.cpp b/source/gui/programming_interface.cpp index 10763d4b..cc68ff4c 100644 --- a/source/gui/programming_interface.cpp +++ b/source/gui/programming_interface.cpp @@ -348,74 +348,15 @@ namespace API //close all windows in current thread void exit() { - std::vector v; - internal_scope_guard lock; - restrict::wd_manager().all_handles(v); - if(v.size()) - { - std::vector roots; - native_window_type root = nullptr; - unsigned tid = nana::system::this_thread_id(); - for(auto wd : v) - { - if((wd->thread_id == tid) && (wd->root != root)) - { - root = wd->root; - bool exists = false; - for (auto i = roots.cbegin(); i != roots.cend(); ++i) - { - if (*i == root) - { - exists = true; - break; - } - } - - if (!exists) - roots.emplace_back(root); - } - } - - for(auto i : roots) - interface_type::close_window(i); - } + restrict::bedrock.close_thread_window(nana::system::this_thread_id()); } + //close all windows void exit_all() { - std::vector v; - internal_scope_guard lock; - restrict::wd_manager().all_handles(v); - if (v.size()) - { - std::vector roots; - native_window_type root = nullptr; - //unsigned tid = nana::system::this_thread_id(); - for (auto wd : v) - { - if (/*(wd->thread_id == tid) &&*/ (wd->root != root)) - { - root = wd->root; - bool exists = false; - for (auto i = roots.cbegin(); i != roots.cend(); ++i) - { - if (*i == root) - { - exists = true; - break; - } - } - - if (!exists) - roots.emplace_back(root); - } - } - - for (auto i : roots) - interface_type::close_window(i); - } + restrict::bedrock.close_thread_window(0); } //transform_shortkey_text