From a74ce0fc3c68734e49a8f0f16b94d28357ba556a Mon Sep 17 00:00:00 2001
From: koroal <98806009+koroal@users.noreply.github.com>
Date: Tue, 29 Nov 2022 15:03:09 +0300
Subject: [PATCH 2/4] FIX window position not being saved in some cases

When a window is closed by Backspace (destroy all windows).
When a window is closed by the [X] button in its title bar.
---
 src/simutrans/gui/simwin.cc | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/simutrans/gui/simwin.cc b/src/simutrans/gui/simwin.cc
index 3d7ec009a..9d1439f9f 100644
--- a/src/simutrans/gui/simwin.cc
+++ b/src/simutrans/gui/simwin.cc
@@ -905,6 +905,17 @@ int top_win(int win, bool keep_state )
 	return notify_top_win();
 }
 
+static void save_win_position(const simwin_t &win)
+{
+	if(  win.magic_number < magic_max  ) {
+		if(  scr_coord *k = old_win_pos.access(win.magic_number)  ) {
+			*k = win.pos;
+		}
+		else {
+			old_win_pos.put( win.magic_number, win.pos );
+		}
+	}
+}
 
 /* sometimes a window cannot destroyed while it is still handled;
  * in those cases it will added to kill list and it is only destructed
@@ -915,6 +926,7 @@ static void process_kill_list()
 	bool needs_top = !kill_list.empty();
 	for(simwin_t & i : kill_list) {
 		if (inside_event_handling != i.gui) {
+			save_win_position(i);
 			destroy_framed_win(&i); // we call this first, otherwise the focus may not be recognized
 			wins.remove(i);
 		}
@@ -1007,15 +1019,7 @@ bool destroy_win(const gui_frame_t *gui)
 			else {
 				simwin_t win = wins[i];
 				wins.remove_at(i);
-				if(  win.magic_number < magic_max  ) {
-					// save last pos
-					if(  scr_coord *k = old_win_pos.access(win.magic_number)  ) {
-						*k = win.pos;
-					}
-					else {
-						old_win_pos.put( win.magic_number, win.pos );
-					}
-				}
+				save_win_position(win);
 				destroy_framed_win(&win);
 				if(  wins.get_count()>1  ) {
 					notify_top_win();
@@ -1032,6 +1036,7 @@ void destroy_all_win(bool destroy_sticky)
 {
 	for(  sint32 curWin = 0;  curWin < (sint32)wins.get_count();  curWin++  ) {
 		if(  destroy_sticky  ||  !wins[curWin].sticky  ) {
+			save_win_position(wins[curWin]);
 			if(  inside_event_handling == wins[curWin].gui  ) {
 				// only add this, if not already added
 				kill_list.append_unique(wins[curWin]);
-- 
2.38.1

