diff options
Diffstat (limited to 'src/ui/window.cpp')
-rw-r--r-- | src/ui/window.cpp | 79 |
1 files changed, 68 insertions, 11 deletions
diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 2737b70b..f8e6d4f3 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -98,6 +98,15 @@ namespace cru::ui ); } + 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; + } + + namespace { Cursor::Ptr GetCursorInherit(Control* control) @@ -275,6 +284,41 @@ namespace cru::ui } } + 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(); + + auto p = DipToPi(point); + if (::ClientToScreen(GetWindowHandle(), &p) == 0) + throw Win32Error(::GetLastError(), "Failed transform point from window to screen."); + return PiToDip(p); + } + + Point Window::PointFromScreen(const Point& point) + { + if (!IsWindowValid()) + return Point::Zero(); + + auto p = DipToPi(point); + if (::ScreenToClient(GetWindowHandle(), &p) == 0) + throw Win32Error(::GetLastError(), "Failed transform point from screen to window."); + return PiToDip(p); + } + bool Window::HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT & result) { if (!native_message_event.IsNoHandler()) @@ -429,10 +473,32 @@ namespace cru::ui } + void Window::WindowInvalidateLayout() + { + if (is_layout_invalid_) + return; + + is_layout_invalid_ = true; + InvokeLater([this] + { + if (is_layout_invalid_) + Relayout(); + }); + } + void Window::Relayout() { - OnMeasureCore(GetSize()); + Measure(GetSize()); OnLayoutCore(Rect(Point::Zero(), GetSize())); + is_layout_invalid_ = false; + } + + void Window::SetSizeFitContent(const Size& max_size) + { + Measure(max_size); + SetClientSize(GetDesiredSize()); + OnLayoutCore(Rect(Point::Zero(), GetSize())); + is_layout_invalid_ = false; } void Window::RefreshControlList() { @@ -557,15 +623,6 @@ namespace cru::ui } } - Size Window::OnMeasureContent(const Size& available_size) - { - for (auto control: GetChildren()) - { - control->Measure(available_size); - } - return available_size; - } - void Window::OnDestroyInternal() { WindowManager::GetInstance()->UnregisterWindow(hwnd_); hwnd_ = nullptr; @@ -597,7 +654,7 @@ namespace cru::ui 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)) - InvalidateLayout(); + WindowInvalidateLayout(); } void Window::OnSetFocusInternal() |