allow processing of window close event - e.g. to disable closing if there is unsaved data - issue #60 implemented for win32 backend

This commit is contained in:
Vadim Lopatin 2015-02-13 14:20:53 +03:00
parent 26d9481506
commit d34ffd75a1
2 changed files with 23 additions and 0 deletions

View File

@ -276,6 +276,22 @@ class Window {
/// set handler for files dropped to app window
@property Window onFilesDropped(void delegate(string[]) handler) { _onFilesDropped = handler; return this; }
protected bool delegate() _onCanClose;
/// get handler for closing of app (it must return true to allow immediate close, false to cancel close or close window later)
@property bool delegate() onCanClose() { return _onCanClose; }
/// set handler for closing of app (it must return true to allow immediate close, false to cancel close or close window later)
@property Window onCanClose(bool delegate() handler) { _onCanClose = handler; return this; }
/// calls onCanClose handler if set to check if system may close window
bool handleCanClose() {
if (!_onCanClose)
return true;
bool res = _onCanClose();
if (!res)
update(true); // redraw window if it was decided to not close immediately
return res;
}
/// hide tooltip if shown and cancel tooltip timer if set
void hideTooltip() {

View File

@ -1164,6 +1164,13 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
DragFinish(hdrop);
}
return 0;
case WM_CLOSE:
if (window !is null) {
if (!window.handleCanClose())
return 0; // prevent closing
}
// default handler inside DefWindowProc will close window
break;
case WM_GETMINMAXINFO:
case WM_NCCREATE:
case WM_NCCALCSIZE: