diff --git a/include/nana/gui/widgets/menubar.hpp b/include/nana/gui/widgets/menubar.hpp index bec4dd5d..9b736149 100644 --- a/include/nana/gui/widgets/menubar.hpp +++ b/include/nana/gui/widgets/menubar.hpp @@ -59,7 +59,7 @@ namespace nana private: void _m_move(bool to_left); bool _m_popup_menu(); - void _m_total_close(); + void _m_total_close(bool try_restore); bool _m_close_menu(); void _m_unload_menu_window(); std::size_t _m_item_by_pos(const ::nana::point&); @@ -90,6 +90,11 @@ namespace nana nana::menu *menu; nana::point mouse_pos; + + //The menu will restore the focus of taken window. But the restoring during + //key_press and key_release resets the focus to the taken window, it causes + //the taken window to receive a key_char which should be received by menubar. + bool delay_restore; }state_; }; }//end namespace menubar diff --git a/source/gui/detail/window_manager.cpp b/source/gui/detail/window_manager.cpp index f594e6a0..fb0d0491 100644 --- a/source/gui/detail/window_manager.cpp +++ b/source/gui/detail/window_manager.cpp @@ -8,7 +8,8 @@ * http://www.boost.org/LICENSE_1_0.txt) * * @file: nana/gui/detail/window_manager.cpp - * + * @author: Jinhao + * @contributors: Katsuhisa Yuasa */ #include @@ -875,6 +876,14 @@ namespace detail if (!root_has_been_focused) native_interface::set_focus(root_wd->root); + //A fix by Katsuhisa Yuasa + //The menubar token window will be redirected to the prev focus window when the new + //focus window is a menubar. + //The focus window will be restore to the prev focus which losts the focus becuase of + //memberbar. + if (wd == wd->root_widget->other.attribute.root->menubar) + wd = prev_focus; + brock.set_menubar_taken(wd); } return prev_focus; diff --git a/source/gui/widgets/menubar.cpp b/source/gui/widgets/menubar.cpp index e6d34cb5..8a19b321 100644 --- a/source/gui/widgets/menubar.cpp +++ b/source/gui/widgets/menubar.cpp @@ -232,10 +232,10 @@ namespace nana _m_popup_menu(); } else - _m_total_close(); + _m_total_close(true); } else if(npos == state_.active) - _m_total_close(); + _m_total_close(true); else _m_popup_menu(); @@ -255,7 +255,7 @@ namespace nana else { state_.behavior = state_.behavior_none; - _m_total_close(); + _m_total_close(true); _m_draw(); API::lazy_refresh(); } @@ -305,15 +305,17 @@ namespace nana } break; case keyboard::enter: + state_.delay_restore = true; state_.menu->pick(); break; default: if(2 != state_.menu->send_shortkey(arg.key)) { - if(state_.active != npos) + if (state_.active != npos) { - _m_total_close(); - if(arg.key == 18) //ALT + state_.delay_restore = true; + _m_total_close(false); + if (arg.key == 18) //ALT state_.behavior = state_.behavior_focus; } } @@ -367,6 +369,12 @@ namespace nana _m_draw(); API::lazy_refresh(); } + + if (state_.delay_restore) + { + API::restore_menubar_taken_window(); + state_.delay_restore = false; + } } void trigger::shortkey(graph_reference graph, const arg_keyboard& arg) @@ -440,13 +448,14 @@ namespace nana return false; } - void trigger::_m_total_close() + void trigger::_m_total_close(bool try_restore) { _m_close_menu(); state_.menu_active = false; state_.behavior = state_.behavior_none; - API::restore_menubar_taken_window(); + if (try_restore) + API::restore_menubar_taken_window(); auto pos = API::cursor_position(); API::calc_window_point(widget_->handle(), pos); @@ -471,7 +480,8 @@ namespace nana state_.menu = nullptr; if(state_.passive_close) { - _m_total_close(); + _m_total_close(false); + _m_draw(); API::update_window(widget_->handle()); } @@ -569,7 +579,13 @@ namespace nana //struct state_type trigger::state_type::state_type() - :active(npos), behavior(behavior_none), menu_active(false), passive_close(true), nullify_mouse(false), menu(nullptr) + : active(npos), + behavior(behavior_none), + menu_active(false), + passive_close(true), + nullify_mouse(false), + menu(nullptr), + delay_restore(false) {} //end struct state_type //end class trigger