diff options
author | 杨宇千 <crupest@outlook.com> | 2018-11-10 20:05:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-10 20:05:29 +0800 |
commit | 0e9e897d306c71ab46fd9b5371d811950124ee27 (patch) | |
tree | 6e7c9dcda090b0fd0153c634165393e693894c10 /src/ui/window.cpp | |
parent | 09be0d0c86a789d568addc3153917aa604e4c7d7 (diff) | |
parent | fd3f899964bb0a3cad80bbb9afc4445fac1e6412 (diff) | |
download | cru-0e9e897d306c71ab46fd9b5371d811950124ee27.tar.gz cru-0e9e897d306c71ab46fd9b5371d811950124ee27.tar.bz2 cru-0e9e897d306c71ab46fd9b5371d811950124ee27.zip |
Merge pull request #7 from crupest/parent-window
Add parent window support.
Diffstat (limited to 'src/ui/window.cpp')
-rw-r--r-- | src/ui/window.cpp | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 349b78ff..2737b70b 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -113,8 +113,19 @@ namespace cru::ui } } - Window::Window() : Control(WindowConstructorTag{}, this), control_list_({ this }) { + Window* Window::CreateOverlapped() + { + return new Window(tag_overlapped_constructor{}); + } + + Window* Window::CreatePopup(Window* parent, const bool caption) + { + return new Window(tag_popup_constructor{}, parent, caption); + } + + Window::Window(tag_overlapped_constructor) : Control(WindowConstructorTag{}, this), control_list_({ this }) { const auto window_manager = WindowManager::GetInstance(); + hwnd_ = CreateWindowEx(0, window_manager->GetGeneralWindowClass()->GetName(), L"", WS_OVERLAPPEDWINDOW, @@ -125,6 +136,34 @@ namespace cru::ui if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); + AfterCreateHwnd(window_manager); + } + + Window::Window(tag_popup_constructor, Window* parent, const bool caption) : Control(WindowConstructorTag{}, this), control_list_({ this }) + { + if (parent != nullptr && !parent->IsWindowValid()) + throw std::runtime_error("Parent window is not valid."); + + parent_window_ = parent; + + const auto window_manager = WindowManager::GetInstance(); + + hwnd_ = CreateWindowEx(0, + window_manager->GetGeneralWindowClass()->GetName(), + L"", caption ? (WS_POPUPWINDOW | WS_CAPTION) : WS_POPUP, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + parent == nullptr ? nullptr : parent->GetWindowHandle(), + nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr + ); + + if (hwnd_ == nullptr) + throw std::runtime_error("Failed to create window."); + + AfterCreateHwnd(window_manager); + } + + void Window::AfterCreateHwnd(WindowManager* window_manager) + { window_manager->RegisterWindow(hwnd_, this); render_target_ = graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); @@ -133,7 +172,11 @@ namespace cru::ui } Window::~Window() { - Close(); + if (IsWindowValid()) + { + SetDeleteThisOnDestroy(false); // avoid double delete. + Close(); + } TraverseDescendants([this](Control* control) { control->OnDetachToWindow(this); }); @@ -144,6 +187,11 @@ namespace cru::ui return control_type; } + void Window::SetDeleteThisOnDestroy(bool value) + { + delete_this_on_destroy_ = value; + } + void Window::Close() { if (IsWindowValid()) DestroyWindow(hwnd_); @@ -521,6 +569,8 @@ namespace cru::ui void Window::OnDestroyInternal() { WindowManager::GetInstance()->UnregisterWindow(hwnd_); hwnd_ = nullptr; + if (delete_this_on_destroy_) + delete this; } void Window::OnPaintInternal() { |