diff options
author | crupest <crupest@outlook.com> | 2018-09-12 23:09:13 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-09-12 23:09:13 +0800 |
commit | 5f75c4f4f6736ff8efa87beab715fd9a044f666c (patch) | |
tree | a67dd8d9f6af0ff7b93776c1e3bcc2816a2250dd /CruUI/ui | |
parent | e2dab3893a8be43de0e50587c85df9e11abf87fe (diff) | |
download | cru-5f75c4f4f6736ff8efa87beab715fd9a044f666c.tar.gz cru-5f75c4f4f6736ff8efa87beab715fd9a044f666c.tar.bz2 cru-5f75c4f4f6736ff8efa87beab715fd9a044f666c.zip |
...
Never use PostMessage with hwnd as null!!!!!!!!!!!!!!!!!!!!!!!!
Diffstat (limited to 'CruUI/ui')
-rw-r--r-- | CruUI/ui/controls/text_block.cpp | 87 | ||||
-rw-r--r-- | CruUI/ui/controls/text_block.h | 8 | ||||
-rw-r--r-- | CruUI/ui/window.cpp | 139 | ||||
-rw-r--r-- | CruUI/ui/window.h | 12 |
4 files changed, 195 insertions, 51 deletions
diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp index 739cd793..c528d3ca 100644 --- a/CruUI/ui/controls/text_block.cpp +++ b/CruUI/ui/controls/text_block.cpp @@ -1,7 +1,5 @@ #include "text_block.h" -#include <chrono> - #include "ui/window.h" #include "graph/graph.h" #include "exception.h" @@ -18,12 +16,22 @@ namespace cru return graph::GraphManager::GetInstance()->GetDWriteFactory(); } + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidBrush(const D2D1_COLOR_F& color) + { + const auto device_context = graph::GraphManager::GetInstance()->GetD2D1DeviceContext(); + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> solid_color_brush; + device_context->CreateSolidColorBrush(color, &solid_color_brush); + return solid_color_brush; + } + TextBlock::TextBlock(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false) { text_format_ = init_text_format; if (init_brush == nullptr) CreateDefaultBrush(); + + selection_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Blue)); //TODO! } TextBlock::~TextBlock() = default; @@ -62,25 +70,83 @@ namespace cru void TextBlock::OnDraw(ID2D1DeviceContext* device_context) { if (text_layout_ != nullptr) + { + if (selected_range_.has_value()) + { + DWRITE_TEXT_METRICS text_metrics{}; + ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); + const auto metrics_count = text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; + + Vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count); + UINT32 actual_count; + text_layout_->HitTestTextRange( + selected_range_.value().position, selected_range_.value().count, + 0, 0, + hit_test_metrics.data(), metrics_count, &actual_count + ); + + hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, hit_test_metrics.cend()); + + for (const auto& metrics : hit_test_metrics) + { + device_context->FillRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(metrics.left, metrics.top, metrics.left + metrics.width, metrics.top + metrics.height), 3, 3), selection_brush_.Get()); + } + } device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); + } + } + + std::optional<unsigned> TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point, bool test_inside = true) + { + BOOL is_trailing, is_inside; + DWRITE_HIT_TEST_METRICS metrics{}; + text_layout->HitTestPoint(point.x, point.y, &is_trailing, &is_inside, &metrics); + if (!test_inside || is_inside) + return is_trailing == 0 ? metrics.textPosition : metrics.textPosition + 1; + else + return std::nullopt; } void TextBlock::OnMouseDownCore(events::MouseButtonEventArgs& args) { if (args.GetMouseButton() == MouseButton::Left) { - BOOL is_trailing, is_inside; - DWRITE_HIT_TEST_METRICS metrics; - text_layout_->HitTestPoint(args.GetPoint(this).x, args.GetPoint(this).y, &is_trailing, &is_inside, &metrics); - if (is_inside) + const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), true); + if (hit_test_result.has_value()) { - mouse_down_position_ = is_trailing == 0 ? metrics.textPosition : metrics.textPosition + 1; - is_mouse_down_ = true; + mouse_down_position_ = hit_test_result.value(); + is_selecting_ = true; + GetWindow()->CaptureMouseFor(this); } } Control::OnMouseDownCore(args); } + void TextBlock::OnMouseMoveCore(events::MouseEventArgs& args) + { + if (is_selecting_) + { + const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), false).value(); + if (hit_test_result > mouse_down_position_) + selected_range_ = TextRange(mouse_down_position_, hit_test_result - mouse_down_position_); + else + selected_range_ = TextRange(hit_test_result, mouse_down_position_ - hit_test_result); + Repaint(); + } + } + + void TextBlock::OnMouseUpCore(events::MouseButtonEventArgs& args) + { + if (args.GetMouseButton() == MouseButton::Left) + { + if (is_selecting_) + { + is_selecting_ = false; + GetWindow()->ReleaseCurrentMouseCapture(); + } + } + } + Size TextBlock::OnMeasure(const Size& available_size) { if (text_.empty()) @@ -147,10 +213,7 @@ namespace cru void TextBlock::CreateDefaultBrush() { - const auto device_context = graph::GraphManager::GetInstance()->GetD2D1DeviceContext(); - Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> solid_color_brush; - device_context->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &solid_color_brush); - brush_ = solid_color_brush; + brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)); } void TextBlock::CreateDefaultTextFormat() diff --git a/CruUI/ui/controls/text_block.h b/CruUI/ui/controls/text_block.h index d6d173ba..1d1166a5 100644 --- a/CruUI/ui/controls/text_block.h +++ b/CruUI/ui/controls/text_block.h @@ -102,6 +102,7 @@ namespace cru void OnMouseDownCore(events::MouseButtonEventArgs& args) override; void OnMouseMoveCore(events::MouseEventArgs& args) override; + void OnMouseUpCore(events::MouseButtonEventArgs& args) override; Size OnMeasure(const Size& available_size) override; @@ -116,15 +117,16 @@ namespace cru String text_; Microsoft::WRL::ComPtr<ID2D1Brush> brush_; + Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_; Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; Vector<std::shared_ptr<TextLayoutHandler>> text_layout_handlers_; - unsigned mouse_down_position_; - std::optional<TextRange> selected_range_; + unsigned mouse_down_position_ = 0; + std::optional<TextRange> selected_range_ = std::nullopt; - bool is_mouse_down_ = false; //TODO!!! + bool is_selecting_ = false; }; } } diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp index 3a9393ff..8430d09b 100644 --- a/CruUI/ui/window.cpp +++ b/CruUI/ui/window.cpp @@ -1,5 +1,7 @@ #include "window.h" +#include <fmt/format.h> + #include "application.h" #include "graph/graph.h" #include "exception.h" @@ -83,17 +85,19 @@ namespace cru // find descendant then erase it; find ancestor then just return. for (auto i = cache_invalid_controls_.cbegin(); i != cache_invalid_controls_.cend(); ++i) { - if (IsAncestorOrDescendant(*i, control) == control) + const auto result = IsAncestorOrDescendant(*i, control); + if (result == control) cache_invalid_controls_.erase(i); - else + else if (result != nullptr) return; // find a ancestor of "control", just return } - + cache_invalid_controls_.insert(control); if (cache_invalid_controls_.size() == 1) // when insert just now and not repeat to "InvokeLater". { InvokeLater([this] { + RefreshInvalidControlPositionCache(); // first refresh position cache. for (const auto i : cache_invalid_controls_) // traverse all descendants of position-invalid controls and notify position change event i->TraverseDescendants([](Control* control) @@ -101,6 +105,7 @@ namespace cru control->CheckAndNotifyPositionChanged(); }); cache_invalid_controls_.clear(); // after update and notify, clear the set. + }); } } @@ -128,7 +133,7 @@ namespace cru const auto position = control->GetPositionRelative(); Point lefttop( parent_lefttop_absolute.x + position.x, - parent_lefttop_absolute.y + position.x + parent_lefttop_absolute.y + position.y ); control->position_cache_.lefttop_position_absolute = lefttop; control->ForeachChild([lefttop](Control* c) { @@ -136,6 +141,14 @@ namespace cru }); } + inline Point PiToDip(const POINT& pi_point) + { + return Point( + graph::PixelToDipX(pi_point.x), + graph::PixelToDipY(pi_point.y) + ); + } + Window::Window() : Control(true), layout_manager_(new WindowLayoutManager()), control_list_({ this }) { auto app = Application::GetInstance(); hwnd_ = CreateWindowEx(0, @@ -257,6 +270,15 @@ namespace cru 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; @@ -271,7 +293,7 @@ namespace cru POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Left, point); + OnMouseUpInternal(MouseButton::Left, point); result = 0; return true; } @@ -289,7 +311,7 @@ namespace cru POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Right, point); + OnMouseUpInternal(MouseButton::Right, point); result = 0; return true; } @@ -307,7 +329,7 @@ namespace cru POINT point; point.x = GET_X_LPARAM(l_param); point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(MouseButton::Middle, point); + OnMouseUpInternal(MouseButton::Middle, point); result = 0; return true; } @@ -324,6 +346,14 @@ namespace cru } } + Point Window::GetMousePosition() + { + POINT point; + ::GetCursorPos(&point); + ::ScreenToClient(hwnd_, &point); + return PiToDip(point); + } + Point Window::GetPositionRelative() { return Point(); @@ -394,6 +424,37 @@ namespace cru 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; + } + } + RECT Window::GetClientRectPixel() { RECT rect{ }; GetClientRect(hwnd_, &rect); @@ -451,12 +512,9 @@ namespace cru DispatchEvent(focus_control_, &Control::OnLoseFocusCore, nullptr); } - void Window::OnMouseMoveInternal(POINT point) + void Window::OnMouseMoveInternal(const POINT point) { - Point dip_point( - graph::PixelToDipX(point.x), - graph::PixelToDipY(point.y) - ); + const auto dip_point = PiToDip(point); //when mouse was previous outside the window if (mouse_hover_control_ == nullptr) { @@ -471,21 +529,18 @@ namespace cru //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 (new_control_mouse_hover != mouse_hover_control_) //if the mouse-hover-on control changed + if (mouse_capture_control_) // if mouse is captured { - const auto lowest_common_ancestor = FindLowestCommonAncestor(mouse_hover_control_, new_control_mouse_hover); - if (mouse_hover_control_ != nullptr) // if last mouse-hover-on control exists - { - // dispatch mouse leave event. - DispatchEvent(mouse_hover_control_, &Control::OnMouseLeaveCore, lowest_common_ancestor); - } - mouse_hover_control_ = new_control_mouse_hover; - // dispatch mouse enter event. - DispatchEvent(new_control_mouse_hover, &Control::OnMouseEnterCore, lowest_common_ancestor, dip_point); + DispatchEvent(mouse_capture_control_, &Control::OnMouseMoveCore, nullptr, dip_point); + } + else + { + DispatchMouseHoverControlChangeEvent(old_control_mouse_hover, new_control_mouse_hover, dip_point); + DispatchEvent(new_control_mouse_hover, &Control::OnMouseMoveCore, nullptr, dip_point); } - - DispatchEvent(new_control_mouse_hover, &Control::OnMouseMoveCore, nullptr, dip_point); } void Window::OnMouseLeaveInternal() @@ -496,26 +551,42 @@ namespace cru void Window::OnMouseDownInternal(MouseButton button, POINT point) { - Point dip_point( - graph::PixelToDipX(point.x), - graph::PixelToDipY(point.y) - ); + const auto dip_point = PiToDip(point); - const auto control = HitTest(dip_point); + Control* control; + + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); DispatchEvent(control, &Control::OnMouseDownCore, nullptr, dip_point, button); } void Window::OnMouseUpInternal(MouseButton button, POINT point) { - Point dip_point( - graph::PixelToDipX(point.x), - graph::PixelToDipY(point.y) - ); + const auto dip_point = PiToDip(point); - const auto control = HitTest(dip_point); + Control* control; + + if (mouse_capture_control_) + control = mouse_capture_control_; + else + control = HitTest(dip_point); DispatchEvent(control, &Control::OnMouseUpCore, nullptr, dip_point, button); } + + 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::OnMouseLeaveCore, lowest_common_ancestor); // dispatch mouse leave event. + if (new_control != nullptr) + DispatchEvent(new_control, &Control::OnMouseEnterCore, lowest_common_ancestor, point); // dispatch mouse enter event. + } + } } } diff --git a/CruUI/ui/window.h b/CruUI/ui/window.h index 9ffa4dc6..551e2048 100644 --- a/CruUI/ui/window.h +++ b/CruUI/ui/window.h @@ -172,6 +172,7 @@ namespace cru { //Return false if the message is not handled. bool HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT& result); + Point GetMousePosition(); //*************** region: position and size *************** @@ -207,6 +208,11 @@ namespace cru { Control* GetFocusControl(); + //*************** region: mouse capture *************** + + Control* CaptureMouseFor(Control* control); + Control* ReleaseCurrentMouseCapture(); + private: //*************** region: native operations *************** @@ -230,8 +236,6 @@ namespace cru { void OnMouseDownInternal(MouseButton button, POINT point); void OnMouseUpInternal(MouseButton button, POINT point); - - //*************** region: event dispatcher helper *************** template<typename EventArgs> @@ -258,6 +262,8 @@ namespace cru { } } + void DispatchMouseHoverControlChangeEvent(Control* old_control, Control * new_control, const Point& point); + private: std::unique_ptr<WindowLayoutManager> layout_manager_; @@ -270,6 +276,8 @@ namespace cru { bool window_focus_ = false; Control* focus_control_ = this; // "focus_control_" can't be nullptr + + Control* mouse_capture_control_ = nullptr; }; } } |