diff options
Diffstat (limited to 'src/ui/window.cpp')
-rw-r--r-- | src/ui/window.cpp | 1194 |
1 files changed, 536 insertions, 658 deletions
diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 51b3f628..f92811a7 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,767 +1,645 @@ #include "window.hpp" #include "application.hpp" -#include "graph/graph.hpp" -#include "exception.hpp" #include "cursor.hpp" +#include "exception.hpp" +#include "graph/graph.hpp" +#include "render/window_render_object.hpp" -namespace cru::ui -{ - LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { - auto window = WindowManager::GetInstance()->FromHandle(hWnd); - - LRESULT result; - if (window != nullptr && window->HandleWindowMessage(hWnd, Msg, wParam, lParam, result)) - return result; - - return DefWindowProc(hWnd, Msg, wParam, lParam); - } - - WindowManager* WindowManager::GetInstance() - { - return Application::GetInstance()->ResolveSingleton<WindowManager>([](auto) - { - return new WindowManager{}; - }); - } - - WindowManager::WindowManager() { - general_window_class_ = std::make_unique<WindowClass>( - L"CruUIWindowClass", - GeneralWndProc, - Application::GetInstance()->GetInstanceHandle() - ); - } - - void WindowManager::RegisterWindow(HWND hwnd, Window * window) { - const auto find_result = window_map_.find(hwnd); - if (find_result != window_map_.end()) - throw std::runtime_error("The hwnd is already in the map."); - - window_map_.emplace(hwnd, window); - } - - void WindowManager::UnregisterWindow(HWND hwnd) { - const auto find_result = window_map_.find(hwnd); - if (find_result == window_map_.end()) - throw std::runtime_error("The hwnd is not in the map."); - window_map_.erase(find_result); - - if (window_map_.empty()) - Application::GetInstance()->Quit(0); - } - - Window* WindowManager::FromHandle(HWND hwnd) { - const auto find_result = window_map_.find(hwnd); - if (find_result == window_map_.end()) - return nullptr; - else - return find_result->second; - } +namespace cru::ui { +LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, + LPARAM lParam) { + auto window = WindowManager::GetInstance()->FromHandle(hWnd); - std::vector<Window*> WindowManager::GetAllWindows() const - { - std::vector<Window*> windows; - for (auto [key, value] : window_map_) - windows.push_back(value); - return windows; - } + LRESULT result; + if (window != nullptr && + window->HandleWindowMessage(hWnd, Msg, wParam, lParam, result)) + return result; - inline Point PiToDip(const POINT& pi_point) - { - return Point( - graph::PixelToDipX(pi_point.x), - graph::PixelToDipY(pi_point.y) - ); - } + return DefWindowProc(hWnd, Msg, wParam, lParam); +} - inline POINT DipToPi(const Point& dip_point) - { - POINT result; - result.x = graph::DipToPixelX(dip_point.x); - result.y = graph::DipToPixelY(dip_point.y); - return result; - } +WindowManager* WindowManager::GetInstance() { + return Application::GetInstance()->ResolveSingleton<WindowManager>( + [](auto) { return new WindowManager{}; }); +} +WindowManager::WindowManager() { + general_window_class_ = std::make_unique<WindowClass>( + L"CruUIWindowClass", GeneralWndProc, + Application::GetInstance()->GetInstanceHandle()); +} - namespace - { - Cursor::Ptr GetCursorInherit(Control* control) - { - while (control != nullptr) - { - const auto cursor = control->GetCursor(); - if (cursor != nullptr) - return cursor; - control = control->GetInternalParent(); - } - return cursors::arrow; - } - } +void WindowManager::RegisterWindow(HWND hwnd, Window* window) { + const auto find_result = window_map_.find(hwnd); + if (find_result != window_map_.end()) + throw std::runtime_error("The hwnd is already in the map."); - Window* Window::CreateOverlapped() - { - return new Window(tag_overlapped_constructor{}); - } + window_map_.emplace(hwnd, window); +} - Window* Window::CreatePopup(Window* parent, const bool caption) - { - return new Window(tag_popup_constructor{}, parent, caption); - } +void WindowManager::UnregisterWindow(HWND hwnd) { + const auto find_result = window_map_.find(hwnd); + if (find_result == window_map_.end()) + throw std::runtime_error("The hwnd is not in the map."); + window_map_.erase(find_result); + if (window_map_.empty()) Application::GetInstance()->Quit(0); +} - Window::Window(tag_overlapped_constructor) - { - BeforeCreateHwnd(); +Window* WindowManager::FromHandle(HWND hwnd) { + const auto find_result = window_map_.find(hwnd); + if (find_result == window_map_.end()) + return nullptr; + else + return find_result->second; +} - const auto window_manager = WindowManager::GetInstance(); +std::vector<Window*> WindowManager::GetAllWindows() const { + std::vector<Window*> windows; + for (auto [key, value] : window_map_) windows.push_back(value); + return windows; +} - hwnd_ = CreateWindowEx(0, - window_manager->GetGeneralWindowClass()->GetName(), - L"", WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - nullptr, nullptr, Application::GetInstance()->GetInstanceHandle(), nullptr - ); +inline Point PiToDip(const POINT& pi_point) { + return Point(graph::PixelToDipX(pi_point.x), graph::PixelToDipY(pi_point.y)); +} - if (hwnd_ == nullptr) - throw std::runtime_error("Failed to create window."); +inline POINT DipToPi(const Point& dip_point) { + POINT result; + result.x = graph::DipToPixelX(dip_point.x); + result.y = graph::DipToPixelY(dip_point.y); + return result; +} - AfterCreateHwnd(window_manager); - } +namespace { +Cursor::Ptr GetCursorInherit(Control* control) { + while (control != nullptr) { + const auto cursor = control->GetCursor(); + if (cursor != nullptr) return cursor; + control = control->GetParent(); + } + return cursors::arrow; +} +} // namespace - Window::Window(tag_popup_constructor, Window* parent, const bool caption) - { - if (parent != nullptr && !parent->IsWindowValid()) - throw std::runtime_error("Parent window is not valid."); +Window* Window::CreateOverlapped() { + return new Window(tag_overlapped_constructor{}); +} - BeforeCreateHwnd(); +Window* Window::CreatePopup(Window* parent, const bool caption) { + return new Window(tag_popup_constructor{}, parent, caption); +} - parent_window_ = parent; +Window::Window(tag_overlapped_constructor) { + BeforeCreateHwnd(); - const auto window_manager = WindowManager::GetInstance(); + 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 - ); + hwnd_ = + CreateWindowEx(0, window_manager->GetGeneralWindowClass()->GetName(), L"", + WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, + Application::GetInstance()->GetInstanceHandle(), nullptr); - if (hwnd_ == nullptr) - throw std::runtime_error("Failed to create window."); + if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); - AfterCreateHwnd(window_manager); - } + AfterCreateHwnd(window_manager); +} - void Window::BeforeCreateHwnd() - { - window_ = this; - } +Window::Window(tag_popup_constructor, Window* parent, const bool caption) { + if (parent != nullptr && !parent->IsWindowValid()) + throw std::runtime_error("Parent window is not valid."); - void Window::AfterCreateHwnd(WindowManager* window_manager) - { - window_manager->RegisterWindow(hwnd_, this); + BeforeCreateHwnd(); - render_target_ = graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); + parent_window_ = parent; - SetCursor(cursors::arrow); - } + const auto window_manager = WindowManager::GetInstance(); - Window::~Window() { - if (IsWindowValid()) - { - SetDeleteThisOnDestroy(false); // avoid double delete. - Close(); - } - TraverseDescendants([this](Control* control) { - control->OnDetachToWindow(this); - }); - } + 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); - StringView Window::GetControlType() const - { - return control_type; - } + if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); - void Window::SetDeleteThisOnDestroy(bool value) - { - delete_this_on_destroy_ = value; - } + AfterCreateHwnd(window_manager); +} - void Window::Close() { - if (IsWindowValid()) - DestroyWindow(hwnd_); - } +void Window::BeforeCreateHwnd() { window_ = this; } - void Window::InvalidateDraw() { - if (IsWindowValid()) { - InvalidateRect(hwnd_, nullptr, false); - } - } +void Window::AfterCreateHwnd(WindowManager* window_manager) { + window_manager->RegisterWindow(hwnd_, this); - void Window::Show() { - if (IsWindowValid()) { - ShowWindow(hwnd_, SW_SHOWNORMAL); - } - } + render_target_ = + graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); - void Window::Hide() { - if (IsWindowValid()) { - ShowWindow(hwnd_, SW_HIDE); - } - } + SetCursor(cursors::arrow); - Size Window::GetClientSize() { - if (!IsWindowValid()) - return Size(); + render_object_ = new render::WindowRenderObject(this); +} - const auto pixel_rect = GetClientRectPixel(); - return Size( - graph::PixelToDipX(pixel_rect.right), - graph::PixelToDipY(pixel_rect.bottom) - ); - } +Window::~Window() { + if (IsWindowValid()) { + SetDeleteThisOnDestroy(false); // avoid double delete. + Close(); + } + TraverseDescendants( + [this](Control* control) { control->OnDetachToWindow(this); }); + delete render_object_; +} - void Window::SetClientSize(const Size & size) { - if (IsWindowValid()) { - const auto window_style = static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_STYLE)); - const auto window_ex_style = static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); - - RECT rect; - rect.left = 0; - rect.top = 0; - rect.right = graph::DipToPixelX(size.width); - rect.bottom = graph::DipToPixelY(size.height); - AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style); - - SetWindowPos( - hwnd_, nullptr, 0, 0, - rect.right - rect.left, - rect.bottom - rect.top, - SWP_NOZORDER | SWP_NOMOVE - ); - } - } +StringView Window::GetControlType() const { return control_type; } - Rect Window::GetWindowRect() { - if (!IsWindowValid()) - return Rect(); +render::RenderObject* Window::GetRenderObject() const { return render_object_; } - RECT rect; - ::GetWindowRect(hwnd_, &rect); +void Window::SetDeleteThisOnDestroy(bool value) { + delete_this_on_destroy_ = value; +} - return Rect::FromVertices( - graph::PixelToDipX(rect.left), - graph::PixelToDipY(rect.top), - graph::PixelToDipX(rect.right), - graph::PixelToDipY(rect.bottom) - ); - } +void Window::Close() { + if (IsWindowValid()) DestroyWindow(hwnd_); +} - void Window::SetWindowRect(const Rect & rect) { - if (IsWindowValid()) { - SetWindowPos( - hwnd_, nullptr, - graph::DipToPixelX(rect.left), - graph::DipToPixelY(rect.top), - graph::DipToPixelX(rect.GetRight()), - graph::DipToPixelY(rect.GetBottom()), - SWP_NOZORDER - ); - } - } +void Window::InvalidateDraw() { + if (IsWindowValid()) { + InvalidateRect(hwnd_, nullptr, false); + } +} - void Window::SetWindowPosition(const Point& position) - { - if (IsWindowValid()) { - SetWindowPos( - hwnd_, nullptr, - graph::DipToPixelX(position.x), - graph::DipToPixelY(position.y), - 0, 0, - SWP_NOZORDER | SWP_NOSIZE - ); - } - } +void Window::Show() { + if (IsWindowValid()) { + ShowWindow(hwnd_, SW_SHOWNORMAL); + } +} - Point Window::PointToScreen(const Point& point) - { - if (!IsWindowValid()) - return Point::Zero(); +void Window::Hide() { + if (IsWindowValid()) { + ShowWindow(hwnd_, SW_HIDE); + } +} - auto p = DipToPi(point); - if (::ClientToScreen(GetWindowHandle(), &p) == 0) - throw Win32Error(::GetLastError(), "Failed transform point from window to screen."); - return PiToDip(p); - } +Size Window::GetClientSize() { + if (!IsWindowValid()) return Size(); - Point Window::PointFromScreen(const Point& point) - { - if (!IsWindowValid()) - return Point::Zero(); + const auto pixel_rect = GetClientRectPixel(); + return Size(graph::PixelToDipX(pixel_rect.right), + graph::PixelToDipY(pixel_rect.bottom)); +} - auto p = DipToPi(point); - if (::ScreenToClient(GetWindowHandle(), &p) == 0) - throw Win32Error(::GetLastError(), "Failed transform point from screen to window."); - return PiToDip(p); - } +void Window::SetClientSize(const Size& size) { + if (IsWindowValid()) { + const auto window_style = + static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_STYLE)); + const auto window_ex_style = + static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); + + RECT rect; + rect.left = 0; + rect.top = 0; + rect.right = graph::DipToPixelX(size.width); + rect.bottom = graph::DipToPixelY(size.height); + AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style); + + SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left, + rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE); + } +} - bool Window::HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT & result) { - - events::WindowNativeMessageEventArgs args(this, this, {hwnd, msg, w_param, l_param}); - native_message_event.Raise(args); - if (args.GetResult().has_value()) - { - result = args.GetResult().value(); - return true; - } - - switch (msg) { - case WM_PAINT: - OnPaintInternal(); - result = 0; - return true; - case WM_ERASEBKGND: - result = 1; - return true; - case WM_SETFOCUS: - OnSetFocusInternal(); - result = 0; - return true; - case WM_KILLFOCUS: - OnKillFocusInternal(); - result = 0; - return true; - case WM_MOUSEMOVE: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseMoveInternal(point); - result = 0; - return true; - } - case WM_LBUTTONDOWN: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Left, point); - result = 0; - return true; - } - case WM_LBUTTONUP: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Left, point); - result = 0; - return true; - } - case WM_RBUTTONDOWN: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Right, point); - result = 0; - return true; - } - case WM_RBUTTONUP: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Right, point); - result = 0; - return true; - } - case WM_MBUTTONDOWN: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Middle, point); - result = 0; - return true; - } - case WM_MBUTTONUP: - { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseUpInternal(MouseButton::Middle, point); - result = 0; - return true; - } - case WM_MOUSEWHEEL: - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - ScreenToClient(hwnd, &point); - OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point); - result = 0; - return true; - case WM_KEYDOWN: - OnKeyDownInternal(static_cast<int>(w_param)); - result = 0; - return true; - case WM_KEYUP: - OnKeyUpInternal(static_cast<int>(w_param)); - result = 0; - return true; - case WM_CHAR: - OnCharInternal(static_cast<wchar_t>(w_param)); - result = 0; - return true; - case WM_SIZE: - OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); - result = 0; - return true; - case WM_ACTIVATE: - if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) - OnActivatedInternal(); - else if (w_param == WA_INACTIVE) - OnDeactivatedInternal(); - result = 0; - return true; - case WM_DESTROY: - OnDestroyInternal(); - result = 0; - return true; - default: - return false; - } - } +Rect Window::GetWindowRect() { + if (!IsWindowValid()) return Rect(); - Point Window::GetMousePosition() - { - if (!IsWindowValid()) - return Point::Zero(); - POINT point; - ::GetCursorPos(&point); - ::ScreenToClient(hwnd_, &point); - return PiToDip(point); - } + RECT rect; + ::GetWindowRect(hwnd_, &rect); - Point Window::GetOffset() - { - return Point(); - } + return Rect::FromVertices( + graph::PixelToDipX(rect.left), graph::PixelToDipY(rect.top), + graph::PixelToDipX(rect.right), graph::PixelToDipY(rect.bottom)); +} - Size Window::GetSize() - { - return GetClientSize(); - } +void Window::SetWindowRect(const Rect& rect) { + if (IsWindowValid()) { + SetWindowPos(hwnd_, nullptr, graph::DipToPixelX(rect.left), + graph::DipToPixelY(rect.top), + graph::DipToPixelX(rect.GetRight()), + graph::DipToPixelY(rect.GetBottom()), SWP_NOZORDER); + } +} - void Window::SetRect(const Rect& size) - { +void Window::SetWindowPosition(const Point& position) { + if (IsWindowValid()) { + SetWindowPos(hwnd_, nullptr, graph::DipToPixelX(position.x), + graph::DipToPixelY(position.y), 0, 0, + SWP_NOZORDER | SWP_NOSIZE); + } +} - } +Point Window::PointToScreen(const Point& point) { + if (!IsWindowValid()) return Point::Zero(); - bool Window::IsPointInside(const Point& point) - { - return Rect(Point::Zero(), GetClientSize()).IsPointInside(point); - } + auto p = DipToPi(point); + if (::ClientToScreen(GetWindowHandle(), &p) == 0) + throw Win32Error(::GetLastError(), + "Failed transform point from window to screen."); + return PiToDip(p); +} - void Window::WindowInvalidateLayout() - { - if (is_layout_invalid_) - return; - - is_layout_invalid_ = true; - InvokeLater([this] - { - if (is_layout_invalid_) - Relayout(); - }); - } +Point Window::PointFromScreen(const Point& point) { + if (!IsWindowValid()) return Point::Zero(); - void Window::Relayout() - { - Measure(GetSize(), AdditionalMeasureInfo{}); - OnLayoutCore(Rect(Point::Zero(), GetSize()), AdditionalLayoutInfo{}); - is_layout_invalid_ = false; - } + auto p = DipToPi(point); + if (::ScreenToClient(GetWindowHandle(), &p) == 0) + throw Win32Error(::GetLastError(), + "Failed transform point from screen to window."); + return PiToDip(p); +} - void Window::SetSizeFitContent(const Size& max_size) - { - Measure(max_size, AdditionalMeasureInfo{}); - SetClientSize(GetDesiredSize()); - OnLayoutCore(Rect(Point::Zero(), GetSize()), AdditionalLayoutInfo{}); - is_layout_invalid_ = false; - } +bool Window::HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, + LPARAM l_param, LRESULT& result) { + events::WindowNativeMessageEventArgs args(this, this, + {hwnd, msg, w_param, l_param}); + native_message_event.Raise(args); + if (args.GetResult().has_value()) { + result = args.GetResult().value(); + return true; + } + + switch (msg) { + case WM_PAINT: + OnPaintInternal(); + result = 0; + return true; + case WM_ERASEBKGND: + result = 1; + return true; + case WM_SETFOCUS: + OnSetFocusInternal(); + result = 0; + return true; + case WM_KILLFOCUS: + OnKillFocusInternal(); + result = 0; + return true; + case WM_MOUSEMOVE: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseMoveInternal(point); + result = 0; + return true; + } + case WM_LBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(MouseButton::Left, point); + result = 0; + return true; + } + case WM_LBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(MouseButton::Left, point); + result = 0; + return true; + } + case WM_RBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(MouseButton::Right, point); + result = 0; + return true; + } + case WM_RBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(MouseButton::Right, point); + result = 0; + return true; + } + case WM_MBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(MouseButton::Middle, point); + result = 0; + return true; + } + case WM_MBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(MouseButton::Middle, point); + result = 0; + return true; + } + case WM_MOUSEWHEEL: + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + ScreenToClient(hwnd, &point); + OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point); + result = 0; + return true; + case WM_KEYDOWN: + OnKeyDownInternal(static_cast<int>(w_param)); + result = 0; + return true; + case WM_KEYUP: + OnKeyUpInternal(static_cast<int>(w_param)); + result = 0; + return true; + case WM_CHAR: + OnCharInternal(static_cast<wchar_t>(w_param)); + result = 0; + return true; + case WM_SIZE: + OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); + result = 0; + return true; + case WM_ACTIVATE: + if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) + OnActivatedInternal(); + else if (w_param == WA_INACTIVE) + OnDeactivatedInternal(); + result = 0; + return true; + case WM_DESTROY: + OnDestroyInternal(); + result = 0; + return true; + default: + return false; + } +} - bool Window::RequestFocusFor(Control * control) - { - if (control == nullptr) - throw std::invalid_argument("The control to request focus can't be null. You can set it as the window."); +Point Window::GetMousePosition() { + if (!IsWindowValid()) return Point::Zero(); + POINT point; + ::GetCursorPos(&point); + ::ScreenToClient(hwnd_, &point); + return PiToDip(point); +} - if (!IsWindowValid()) - return false; +bool Window::RequestFocusFor(Control* control) { + if (control == nullptr) + throw std::invalid_argument( + "The control to request focus can't be null. You can set it as the " + "window."); - if (!window_focus_) - { - focus_control_ = control; - ::SetFocus(hwnd_); - return true; // event dispatch will be done in window message handling function "OnSetFocusInternal". - } + if (!IsWindowValid()) return false; - if (focus_control_ == control) - return true; + if (!window_focus_) { + focus_control_ = control; + ::SetFocus(hwnd_); + return true; // event dispatch will be done in window message handling + // function "OnSetFocusInternal". + } - DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, false); + if (focus_control_ == control) return true; - focus_control_ = control; + DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, false); - DispatchEvent(control, &Control::get_focus_event, nullptr, false); + focus_control_ = control; - return true; - } + DispatchEvent(control, &Control::get_focus_event, nullptr, false); - Control* Window::GetFocusControl() - { - return focus_control_; - } + return true; +} - Control* Window::CaptureMouseFor(Control* control) - { - if (control != nullptr) - { - ::SetCapture(hwnd_); - std::swap(mouse_capture_control_, control); - DispatchMouseHoverControlChangeEvent(control ? control : mouse_hover_control_, mouse_capture_control_, GetMousePosition()); - return control; - } - else - { - return ReleaseCurrentMouseCapture(); - } - } +Control* Window::GetFocusControl() { return focus_control_; } + +Control* Window::CaptureMouseFor(Control* control) { + if (control != nullptr) { + ::SetCapture(hwnd_); + std::swap(mouse_capture_control_, control); + DispatchMouseHoverControlChangeEvent( + control ? control : mouse_hover_control_, mouse_capture_control_, + GetMousePosition()); + return control; + } else { + return ReleaseCurrentMouseCapture(); + } +} - Control* Window::ReleaseCurrentMouseCapture() - { - if (mouse_capture_control_) - { - const auto previous = mouse_capture_control_; - mouse_capture_control_ = nullptr; - ::ReleaseCapture(); - DispatchMouseHoverControlChangeEvent(previous, mouse_hover_control_, GetMousePosition()); - return previous; - } - else - { - return nullptr; - } - } +Control* Window::ReleaseCurrentMouseCapture() { + if (mouse_capture_control_) { + const auto previous = mouse_capture_control_; + mouse_capture_control_ = nullptr; + ::ReleaseCapture(); + DispatchMouseHoverControlChangeEvent(previous, mouse_hover_control_, + GetMousePosition()); + return previous; + } else { + return nullptr; + } +} - void Window::UpdateCursor() - { - if (IsWindowValid() && mouse_hover_control_ != nullptr) - { - SetCursorInternal(GetCursorInherit(mouse_hover_control_)->GetHandle()); - } - } +void Window::UpdateCursor() { + if (IsWindowValid() && mouse_hover_control_ != nullptr) { + SetCursorInternal(GetCursorInherit(mouse_hover_control_)->GetHandle()); + } +} #ifdef CRU_DEBUG_LAYOUT - void Window::SetDebugLayout(const bool value) - { - if (debug_layout_ != value) - { - debug_layout_ = value; - InvalidateDraw(); - } - } +void Window::SetDebugLayout(const bool value) { + if (debug_layout_ != value) { + debug_layout_ = value; + InvalidateDraw(); + } +} #endif - RECT Window::GetClientRectPixel() { - RECT rect{ }; - GetClientRect(hwnd_, &rect); - return rect; - } +void Window::OnChildChanged(Control* old_child, Control* new_child) { + if (old_child) render_object_->RemoveChild(0); + if (new_child) render_object_->AddChild(new_child->GetRenderObject(), 0); +} - bool Window::IsMessageInQueue(UINT message) - { - MSG msg; - return ::PeekMessageW(&msg, hwnd_, message, message, PM_NOREMOVE) != 0; - } +Control* Window::HitTest(const Point& point) { + return render_object_->HitTest(point)->GetAttachedControl(); +} - void Window::SetCursorInternal(HCURSOR cursor) - { - if (IsWindowValid()) - { - ::SetClassLongPtrW(GetWindowHandle(), GCLP_HCURSOR, reinterpret_cast<LONG_PTR>(cursor)); - if (mouse_hover_control_ != nullptr) - ::SetCursor(cursor); - } - } +RECT Window::GetClientRectPixel() { + RECT rect{}; + GetClientRect(hwnd_, &rect); + return rect; +} - void Window::OnDestroyInternal() { - WindowManager::GetInstance()->UnregisterWindow(hwnd_); - hwnd_ = nullptr; - if (delete_this_on_destroy_) - InvokeLater([this]{ delete this; }); - } +bool Window::IsMessageInQueue(UINT message) { + MSG msg; + return ::PeekMessageW(&msg, hwnd_, message, message, PM_NOREMOVE) != 0; +} - void Window::OnPaintInternal() { - render_target_->SetAsTarget(); +void Window::SetCursorInternal(HCURSOR cursor) { + if (IsWindowValid()) { + ::SetClassLongPtrW(GetWindowHandle(), GCLP_HCURSOR, + reinterpret_cast<LONG_PTR>(cursor)); + if (mouse_hover_control_ != nullptr) ::SetCursor(cursor); + } +} - auto device_context = render_target_->GetD2DDeviceContext(); +void Window::OnDestroyInternal() { + WindowManager::GetInstance()->UnregisterWindow(hwnd_); + hwnd_ = nullptr; + if (delete_this_on_destroy_) InvokeLater([this] { delete this; }); +} - device_context->BeginDraw(); +void Window::OnPaintInternal() { + render_target_->SetAsTarget(); - //Clear the background. - device_context->Clear(D2D1::ColorF(D2D1::ColorF::White)); + auto device_context = render_target_->GetD2DDeviceContext(); - Draw(device_context.Get()); + device_context->BeginDraw(); - ThrowIfFailed( - device_context->EndDraw(), "Failed to draw window." - ); + // Clear the background. + device_context->Clear(D2D1::ColorF(D2D1::ColorF::White)); - render_target_->Present(); + render_object_->Draw(device_context.Get()); - ValidateRect(hwnd_, nullptr); - } + ThrowIfFailed(device_context->EndDraw(), "Failed to draw window."); - void Window::OnResizeInternal(const int new_width, const int new_height) { - render_target_->ResizeBuffer(new_width, new_height); - if (!(new_width == 0 && new_height == 0)) - WindowInvalidateLayout(); - } + render_target_->Present(); - void Window::OnSetFocusInternal() - { - window_focus_ = true; - DispatchEvent(focus_control_, &Control::get_focus_event, nullptr, true); - } + ValidateRect(hwnd_, nullptr); +} - void Window::OnKillFocusInternal() - { - window_focus_ = false; - DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, true); - } +void Window::OnResizeInternal(const int new_width, const int new_height) { + render_target_->ResizeBuffer(new_width, new_height); + if (!(new_width == 0 && new_height == 0)) render_object_->MeasureAndLayout(); +} - void Window::OnMouseMoveInternal(const POINT point) - { - const auto dip_point = PiToDip(point); - - //when mouse was previous outside the window - if (mouse_hover_control_ == nullptr) { - //invoke TrackMouseEvent to have WM_MOUSELEAVE sent. - TRACKMOUSEEVENT tme; - tme.cbSize = sizeof tme; - tme.dwFlags = TME_LEAVE; - tme.hwndTrack = hwnd_; - - TrackMouseEvent(&tme); - } - - //Find the first control that hit test succeed. - const auto new_control_mouse_hover = HitTest(dip_point); - const auto old_control_mouse_hover = mouse_hover_control_; - mouse_hover_control_ = new_control_mouse_hover; - - if (mouse_capture_control_) // if mouse is captured - { - DispatchEvent(mouse_capture_control_, &Control::mouse_move_event, nullptr, dip_point); - } - else - { - DispatchMouseHoverControlChangeEvent(old_control_mouse_hover, new_control_mouse_hover, dip_point); - DispatchEvent(new_control_mouse_hover, &Control::mouse_move_event, nullptr, dip_point); - } - } +void Window::OnSetFocusInternal() { + window_focus_ = true; + DispatchEvent(focus_control_, &Control::get_focus_event, nullptr, true); +} - void Window::OnMouseLeaveInternal() - { - DispatchEvent(mouse_hover_control_, &Control::mouse_leave_event, nullptr); - mouse_hover_control_ = nullptr; - } +void Window::OnKillFocusInternal() { + window_focus_ = false; + DispatchEvent(focus_control_, &Control::lose_focus_event, nullptr, true); +} - void Window::OnMouseDownInternal(MouseButton button, POINT point) - { - const auto dip_point = PiToDip(point); +void Window::OnMouseMoveInternal(const POINT point) { + const auto dip_point = PiToDip(point); + + // when mouse was previous outside the window + if (mouse_hover_control_ == nullptr) { + // invoke TrackMouseEvent to have WM_MOUSELEAVE sent. + TRACKMOUSEEVENT tme; + tme.cbSize = sizeof tme; + tme.dwFlags = TME_LEAVE; + tme.hwndTrack = hwnd_; + + TrackMouseEvent(&tme); + } + + // Find the first control that hit test succeed. + const auto new_control_mouse_hover = HitTest(dip_point); + const auto old_control_mouse_hover = mouse_hover_control_; + mouse_hover_control_ = new_control_mouse_hover; + + if (mouse_capture_control_) // if mouse is captured + { + DispatchEvent(mouse_capture_control_, &Control::mouse_move_event, nullptr, + dip_point); + } else { + DispatchMouseHoverControlChangeEvent(old_control_mouse_hover, + new_control_mouse_hover, dip_point); + DispatchEvent(new_control_mouse_hover, &Control::mouse_move_event, nullptr, + dip_point); + } +} - Control* control; +void Window::OnMouseLeaveInternal() { + DispatchEvent(mouse_hover_control_, &Control::mouse_leave_event, nullptr); + mouse_hover_control_ = nullptr; +} - if (mouse_capture_control_) - control = mouse_capture_control_; - else - control = HitTest(dip_point); +void Window::OnMouseDownInternal(MouseButton button, POINT point) { + const auto dip_point = PiToDip(point); - DispatchEvent(control, &Control::mouse_down_event, nullptr, dip_point, button); - } + Control* control; - void Window::OnMouseUpInternal(MouseButton button, POINT point) - { - const auto dip_point = PiToDip(point); + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); - Control* control; + DispatchEvent(control, &Control::mouse_down_event, nullptr, dip_point, + button); +} - if (mouse_capture_control_) - control = mouse_capture_control_; - else - control = HitTest(dip_point); +void Window::OnMouseUpInternal(MouseButton button, POINT point) { + const auto dip_point = PiToDip(point); - DispatchEvent(control, &Control::mouse_up_event, nullptr, dip_point, button); - } + Control* control; - void Window::OnMouseWheelInternal(short delta, POINT point) - { - const auto dip_point = PiToDip(point); + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); - Control* control; + DispatchEvent(control, &Control::mouse_up_event, nullptr, dip_point, button); +} - if (mouse_capture_control_) - control = mouse_capture_control_; - else - control = HitTest(dip_point); +void Window::OnMouseWheelInternal(short delta, POINT point) { + const auto dip_point = PiToDip(point); - DispatchEvent(control, &Control::mouse_wheel_event, nullptr, dip_point, static_cast<float>(delta)); - } + Control* control; - void Window::OnKeyDownInternal(int virtual_code) - { - DispatchEvent(focus_control_, &Control::key_down_event, nullptr, virtual_code); - } + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); - void Window::OnKeyUpInternal(int virtual_code) - { - DispatchEvent(focus_control_, &Control::key_up_event, nullptr, virtual_code); - } + DispatchEvent(control, &Control::mouse_wheel_event, nullptr, dip_point, + static_cast<float>(delta)); +} - void Window::OnCharInternal(wchar_t c) - { - DispatchEvent(focus_control_, &Control::char_event, nullptr, c); - } +void Window::OnKeyDownInternal(int virtual_code) { + DispatchEvent(focus_control_, &Control::key_down_event, nullptr, + virtual_code); +} - void Window::OnActivatedInternal() - { - events::UiEventArgs args(this, this); - activated_event.Raise(args); - } +void Window::OnKeyUpInternal(int virtual_code) { + DispatchEvent(focus_control_, &Control::key_up_event, nullptr, virtual_code); +} - void Window::OnDeactivatedInternal() - { - events::UiEventArgs args(this, this); - deactivated_event.Raise(args); - } +void Window::OnCharInternal(wchar_t c) { + DispatchEvent(focus_control_, &Control::char_event, nullptr, c); +} - void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, Control* new_control, const Point& point) - { - if (new_control != old_control) //if the mouse-hover-on control changed - { - const auto lowest_common_ancestor = FindLowestCommonAncestor(old_control, new_control); - if (old_control != nullptr) // if last mouse-hover-on control exists - DispatchEvent(old_control, &Control::mouse_leave_event, lowest_common_ancestor); // dispatch mouse leave event. - if (new_control != nullptr) - { - DispatchEvent(new_control, &Control::mouse_enter_event, lowest_common_ancestor, point); // dispatch mouse enter event. - UpdateCursor(); - } - } - } +void Window::OnActivatedInternal() { + events::UiEventArgs args(this, this); + activated_event.Raise(args); +} + +void Window::OnDeactivatedInternal() { + events::UiEventArgs args(this, this); + deactivated_event.Raise(args); +} + +void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, + Control* new_control, + const Point& point) { + if (new_control != old_control) // if the mouse-hover-on control changed + { + const auto lowest_common_ancestor = + FindLowestCommonAncestor(old_control, new_control); + if (old_control != nullptr) // if last mouse-hover-on control exists + DispatchEvent(old_control, &Control::mouse_leave_event, + lowest_common_ancestor); // dispatch mouse leave event. + if (new_control != nullptr) { + DispatchEvent(new_control, &Control::mouse_enter_event, + lowest_common_ancestor, + point); // dispatch mouse enter event. + UpdateCursor(); + } + } } +} // namespace cru::ui |