From 0a3ef886b2748ef15acbf7ab42fa1ec6c30e13cb Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 2 Nov 2015 11:10:58 +0300 Subject: [PATCH] fix #95 - window.close behavior on Win32 --- examples/dmledit/dmledit.visualdproj | 4 +++- examples/example1/example1.visualdproj | 2 ++ src/dlangui/platforms/windows/winapp.d | 33 +++++++++++++++++--------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/dmledit/dmledit.visualdproj b/examples/dmledit/dmledit.visualdproj index 8737dadb..219d7295 100644 --- a/examples/dmledit/dmledit.visualdproj +++ b/examples/dmledit/dmledit.visualdproj @@ -72,7 +72,7 @@ 0 0 - EmbedStandardResources Unicode USE_FREETYPE USE_OPENGL + EmbedStandardResources Unicode USE_FREETYPE 0 0 0 @@ -96,6 +96,7 @@ $(OutDir)\$(ProjectName).exe 1 2 + 0 @@ -197,6 +198,7 @@ $(OutDir)\$(ProjectName).exe 1 1 + 0 diff --git a/examples/example1/example1.visualdproj b/examples/example1/example1.visualdproj index 1a8dd99c..9d805d93 100644 --- a/examples/example1/example1.visualdproj +++ b/examples/example1/example1.visualdproj @@ -96,6 +96,7 @@ $(OutDir)\$(ProjectName).exe 1 2 + 0 -profile @@ -197,6 +198,7 @@ $(OutDir)\$(ProjectName).exe 1 1 + 0 diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index 55dc1ff8..302d8bfa 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -773,6 +773,7 @@ class Win32Platform : Platform { { TranslateMessage(&msg); DispatchMessage(&msg); + destroyClosedWindows(); } return cast(int)msg.wParam; } @@ -788,7 +789,8 @@ class Win32Platform : Platform { Win32Window wnd = getWindow(hwnd); if (wnd) { _windowMap.remove(cast(ulong)hwnd); - destroy(window); + _windowsToDestroy ~= window; + //destroy(window); } return _windowMap.length > 0; } @@ -823,14 +825,25 @@ class Win32Platform : Platform { w.dispatchThemeChanged(); } - Win32Window _windowToClose; + /// list of windows for deferred destroy in message loop + Win32Window[] _windowsToDestroy; /// close window override void closeWindow(Window w) { Win32Window window = cast(Win32Window)w; - _windowToClose = window; + _windowsToDestroy ~= window; + SendMessage(window._hwnd, WM_CLOSE, 0, 0); + //window } + /// destroy window objects planned for destroy + void destroyClosedWindows() { + foreach(Window w; _windowsToDestroy) { + destroy(w); + } + _windowsToDestroy.length = 0; + } + /// retrieves text from clipboard (when mouseBuffer == true, use mouse selection clipboard - under linux) override dstring getClipboardText(bool mouseBuffer = false) { dstring res = null; @@ -1156,8 +1169,13 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) return 0; case WM_CLOSE: if (window !is null) { - if (!window.handleCanClose()) + bool canClose = window.handleCanClose(); + if (!canClose) { + Log.d("WM_CLOSE: canClose is false"); return 0; // prevent closing + } + Log.d("WM_CLOSE: closing window "); + //destroy(window); } // default handler inside DefWindowProc will close window break; @@ -1168,13 +1186,6 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) //Log.d("Unhandled message ", message); break; } - if (w32platform._windowToClose) { - Win32Window wnd = w32platform._windowToClose; - w32platform._windowToClose = null; - destroy(wnd); - //HWND w = w32platform._windowToClose._hwnd; - //CloseWindow(w); - } return DefWindowProc(hwnd, message, wParam, lParam); }