diff --git a/src/dialogxml/dialogs/dialog.cpp b/src/dialogxml/dialogs/dialog.cpp index 48898a4f..63de84ae 100644 --- a/src/dialogxml/dialogs/dialog.cpp +++ b/src/dialogxml/dialogs/dialog.cpp @@ -764,9 +764,6 @@ void cDialog::handle_one_event(const sf::Event& currentEvent, cFramerateLimiter& for(int i = dialog_stack.size() - 1; i >= 0; --i){ makeFrontWindow(*(dialog_stack[i])); } - // that generates more LostFocus and GainedFocus event(s) - sf::Event evt; - while(pollEvent(win, evt)); } BOOST_FALLTHROUGH; case sf::Event::MouseMoved:{ diff --git a/src/game/boe.items.cpp b/src/game/boe.items.cpp index fea8a6bb..b0cb68e8 100644 --- a/src/game/boe.items.cpp +++ b/src/game/boe.items.cpp @@ -692,7 +692,7 @@ void init_mini_map() { mini_map().setVisible(false); map_visible=false; setWindowFloating(mini_map(), true); - makeFrontWindow(mainPtr()); + makeFrontWindow(mainPtr(), mini_map()); // Create and initialize map gworld if(!map_gworld().create(384, 384)) { diff --git a/src/game/boe.main.cpp b/src/game/boe.main.cpp index a354da3a..c70aa5cb 100644 --- a/src/game/boe.main.cpp +++ b/src/game/boe.main.cpp @@ -233,13 +233,10 @@ void dialog_lost_focus(sf::RenderWindow& win) { void dialog_gained_focus(sf::RenderWindow& win) { setWindowFloating(mini_map(), true); - makeFrontWindow(mainPtr()); if(map_visible){ makeFrontWindow(mini_map()); } - // that generates more LostFocus and GainedFocus event(s) - sf::Event evt; - while(pollEvent(win, evt)); + makeFrontWindow(mainPtr(), mini_map()); } // Comment this line out for exact exception callstacks: @@ -1294,8 +1291,8 @@ void handle_events() { setWindowFloating(mini_map(), true); if(map_visible){ makeFrontWindow(mini_map()); - makeFrontWindow(mainPtr()); } + makeFrontWindow(mainPtr(), mini_map()); } main_window_lost_focus = main_window_gained_focus = @@ -1375,7 +1372,7 @@ void handle_one_event(const sf::Event& event, cFramerateLimiter& fps_limiter) { case sf::Event::GainedFocus: check_window_moved(mainPtr(), last_window_x, last_window_y, "MainWindow"); main_window_gained_focus = true; - makeFrontWindow(mainPtr()); + makeFrontWindow(mainPtr(), mini_map()); change_cursor(); return; @@ -1415,7 +1412,7 @@ void handle_one_minimap_event(const sf::Event& event) { }else if(event.type == sf::Event::GainedFocus){ map_window_gained_focus = true; check_window_moved(mini_map(), last_map_x, last_map_y, "MapWindow"); - makeFrontWindow(mainPtr()); + makeFrontWindow(mainPtr(), mini_map()); }else if(event.type == sf::Event::LostFocus){ map_window_lost_focus = true; }else if(event.type == sf::Event::MouseMoved){ diff --git a/src/game/boe.town.cpp b/src/game/boe.town.cpp index a07c4fcd..b8cd73a1 100644 --- a/src/game/boe.town.cpp +++ b/src/game/boe.town.cpp @@ -1575,7 +1575,7 @@ void display_map() { mini_map().setVisible(true); map_visible = true; draw_map(true); - makeFrontWindow(mainPtr()); + makeFrontWindow(mainPtr(), mini_map()); set_cursor(sword_curs); } diff --git a/src/tools/winutil.cpp b/src/tools/winutil.cpp index 9af85def..2cafde6b 100644 --- a/src/tools/winutil.cpp +++ b/src/tools/winutil.cpp @@ -89,4 +89,26 @@ bool check_window_moved(sf::RenderWindow& win, int& winLastX, int& winLastY, std winLastX = winPosition.x; winLastY = winPosition.y; return moved; -} \ No newline at end of file +} + +void makeFrontWindow(sf::Window& win) { + static sf::Event evt; + _makeFrontWindow(win); + // Discard GainedFocus events generated by our own meddling: + while(pollEvent(win, evt)); +} + +void makeFrontWindow(sf::Window& win, sf::Window& prev) { + static sf::Event evt; + _makeFrontWindow(win); + // Discard GainedFocus and LostFocus events generated by our own meddling: + while(pollEvent(win, evt)); + while(pollEvent(prev, evt)); +} + +void setWindowFloating(sf::Window& win, bool floating) { + static sf::Event evt; + _setWindowFloating(win, floating); + // Discard GainedFocus and LostFocus events generated by our own meddling: + while(pollEvent(win, evt)); +} diff --git a/src/tools/winutil.hpp b/src/tools/winutil.hpp index d4431f21..070c266e 100644 --- a/src/tools/winutil.hpp +++ b/src/tools/winutil.hpp @@ -23,8 +23,14 @@ sf::RenderWindow& mainPtr(); char keyToChar(sf::Keyboard::Key key, bool isShift); +// public-facing overloads which discard events generated by the operation: void makeFrontWindow(sf::Window& win); +void makeFrontWindow(sf::Window& win, sf::Window& prev); void setWindowFloating(sf::Window& win, bool floating); +// OS-specific implementations: +void _makeFrontWindow(sf::Window& win); +void _setWindowFloating(sf::Window& win, bool floating); + // Necessary wrapper for sf::Window.pollEvent() bool pollEvent(sf::Window& win, sf::Event& event); diff --git a/src/tools/winutil.linux.cpp b/src/tools/winutil.linux.cpp index 1c9aab9d..4f23f402 100644 --- a/src/tools/winutil.linux.cpp +++ b/src/tools/winutil.linux.cpp @@ -91,10 +91,10 @@ std::string get_os_version() { return version.str(); } -void makeFrontWindow(sf::Window& win) { +void _makeFrontWindow(sf::Window& win) { } -void setWindowFloating(sf::Window& win, bool floating) { +void _setWindowFloating(sf::Window& win, bool floating) { // Code adapted from auto display = XOpenDisplay(NULL); Atom wmStateAbove = XInternAtom(display, "_NET_WM_STATE_ABOVE", true); diff --git a/src/tools/winutil.mac.mm b/src/tools/winutil.mac.mm index a90a9654..7038cee8 100644 --- a/src/tools/winutil.mac.mm +++ b/src/tools/winutil.mac.mm @@ -83,7 +83,7 @@ std::string get_os_version() { return os.str(); } -void makeFrontWindow(sf::Window& win) { +void _makeFrontWindow(sf::Window& win) { sf::WindowHandle handle = win.getSystemHandle(); id nsHandle = id(handle); if([nsHandle isKindOfClass: [NSWindow class]]) { @@ -92,7 +92,7 @@ void makeFrontWindow(sf::Window& win) { } } -void setWindowFloating(sf::Window& win, bool floating) { +void _setWindowFloating(sf::Window& win, bool floating) { sf::WindowHandle handle = win.getSystemHandle(); id nsHandle = id(handle); if([nsHandle isKindOfClass: [NSWindow class]]) { diff --git a/src/tools/winutil.win.cpp b/src/tools/winutil.win.cpp index d3a1139e..b9d0d083 100644 --- a/src/tools/winutil.win.cpp +++ b/src/tools/winutil.win.cpp @@ -144,15 +144,15 @@ std::string get_os_version() { return version.str(); } -void makeFrontWindow(sf::Window& win) { +void _makeFrontWindow(sf::Window& win) { HWND win_handle = win.getSystemHandle(); BringWindowToTop(win_handle); } -void setWindowFloating(sf::Window& win, bool floating) { +void _setWindowFloating(sf::Window& win, bool floating) { HWND win_handle = win.getSystemHandle(); - UINT flags = SWP_NOMOVE | SWP_NOSIZE; - HWND newPos = floating ? HWND_TOPMOST : HWND_TOP; + UINT flags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE; + HWND newPos = floating ? HWND_TOPMOST : HWND_BOTTOM; // The flags param specifies that these 0's are ignored. SetWindowPos(win_handle, newPos, 0, 0, 0, 0, flags); }