diff options
-rw-r--r-- | CruUI.vcxproj | 2 | ||||
-rw-r--r-- | CruUI.vcxproj.filters | 62 | ||||
-rw-r--r-- | src/ui/controls/text_block.cpp | 267 | ||||
-rw-r--r-- | src/ui/controls/text_block.h | 90 | ||||
-rw-r--r-- | src/ui/controls/text_box.cpp | 209 | ||||
-rw-r--r-- | src/ui/controls/text_box.h | 53 | ||||
-rw-r--r-- | src/ui/controls/text_control.cpp | 293 | ||||
-rw-r--r-- | src/ui/controls/text_control.h | 106 |
8 files changed, 530 insertions, 552 deletions
diff --git a/CruUI.vcxproj b/CruUI.vcxproj index bb1e1f63..4c10ad6a 100644 --- a/CruUI.vcxproj +++ b/CruUI.vcxproj @@ -126,6 +126,7 @@ <ClCompile Include="src\ui\controls\margin_container.cpp" /> <ClCompile Include="src\ui\controls\text_block.cpp" /> <ClCompile Include="src\ui\controls\text_box.cpp" /> + <ClInclude Include="src\ui\controls\text_control.h" /> <ClCompile Include="src\ui\controls\toggle_button.cpp" /> <ClCompile Include="src\ui\events\ui_event.cpp" /> <ClCompile Include="src\ui\layout_base.cpp" /> @@ -149,6 +150,7 @@ <ClInclude Include="src\ui\controls\margin_container.h" /> <ClInclude Include="src\ui\controls\text_block.h" /> <ClInclude Include="src\ui\controls\text_box.h" /> + <ClCompile Include="src\ui\controls\text_control.cpp" /> <ClInclude Include="src\ui\controls\toggle_button.h" /> <ClInclude Include="src\ui\events\ui_event.h" /> <ClInclude Include="src\ui\layout_base.h" /> diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters index 0933ffc9..4d94b6e5 100644 --- a/CruUI.vcxproj.filters +++ b/CruUI.vcxproj.filters @@ -134,5 +134,67 @@ <ClInclude Include="src\ui\events\ui_event.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="src\ui\controls\text_control.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <ClCompile Include="src\application.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\control.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\window.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\graph\graph.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\main.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\exception.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\timer.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\ui_base.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\events\ui_event.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\controls\text_block.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\controls\linear_layout.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\controls\toggle_button.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\layout_base.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\animations\animation.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\base.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\controls\button.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\controls\margin_container.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\controls\text_box.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="src\ui\controls\text_control.cpp"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> </Project>
\ No newline at end of file diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index 93d66ba6..ecffb69e 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -1,8 +1,6 @@ #include "text_block.h" #include "ui/window.h" -#include "graph/graph.h" -#include "exception.h" namespace cru { @@ -10,273 +8,10 @@ namespace cru { namespace controls { - using graph::CreateSolidBrush; - - inline Microsoft::WRL::ComPtr<IDWriteFactory> GetDWriteFactory() - { - return graph::GraphManager::GetInstance()->GetDWriteFactory(); - } - TextBlock::TextBlock(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, - const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false) - { - text_format_ = init_text_format == nullptr ? graph::CreateDefaultTextFormat() : init_text_format; - - RecreateTextLayout(); - - brush_ = init_brush == nullptr ? CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)) : init_brush; - - selection_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue)); - } - - TextBlock::~TextBlock() = default; - - void TextBlock::SetText(const String& text) - { - if (text_ != text) - { - const auto old_text = text_; - text_ = text; - OnTextChangedCore(old_text, text); - } - } - - void TextBlock::SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush) - { - brush_ = brush; - Repaint(); - } - - void TextBlock::SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format) - { - text_format_ = text_format; - RecreateTextLayout(); - Repaint(); - } - - void TextBlock::AddTextLayoutHandler(TextLayoutHandlerPtr handler) - { - text_layout_handlers_.push_back(std::move(handler)); - } - - void TextBlock::RemoveTextLayoutHandler(const TextLayoutHandlerPtr& handler) - { - const auto find_result = std::find(text_layout_handlers_.cbegin(), text_layout_handlers_.cend(), - handler); - if (find_result != text_layout_handlers_.cend()) - text_layout_handlers_.erase(find_result); - } - - void TextBlock::SetSelectable(const bool is_selectable) - { - if (!is_selectable) - { - is_selecting_ = false; - selected_range_ = std::nullopt; - Repaint(); - } - is_selectable_ = is_selectable; - } - - void TextBlock::SetSelectedRange(std::optional<TextRange> text_range) + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : TextControl(init_text_format, init_brush) { - if (is_selectable_) - { - selected_range_ = text_range; - Repaint(); - } - } - - void TextBlock::OnSizeChangedCore(events::SizeChangedEventArgs& args) - { - Control::OnSizeChangedCore(args); - text_layout_->SetMaxWidth(args.GetNewSize().width); - text_layout_->SetMaxHeight(args.GetNewSize().height); - Repaint(); - } - - void TextBlock::OnDraw(ID2D1DeviceContext* device_context) - { - Control::OnDraw(device_context); - 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()); - } - - namespace - { - std::optional<unsigned> TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point, const 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) - { - Control::OnMouseDownCore(args); - if (is_selectable_ && args.GetMouseButton() == MouseButton::Left) - { - selected_range_ = std::nullopt; - const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), true); - if (hit_test_result.has_value()) - { - mouse_down_position_ = hit_test_result.value(); - is_selecting_ = true; - GetWindow()->CaptureMouseFor(this); - } - Repaint(); - } - } - - void TextBlock::OnMouseMoveCore(events::MouseEventArgs& args) - { - Control::OnMouseMoveCore(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 if (hit_test_result < mouse_down_position_) - selected_range_ = TextRange(hit_test_result, mouse_down_position_ - hit_test_result); - else - selected_range_ = std::nullopt; - Repaint(); - } - } - - void TextBlock::OnMouseUpCore(events::MouseButtonEventArgs& args) - { - Control::OnMouseUpCore(args); - if (args.GetMouseButton() == MouseButton::Left) - { - if (is_selecting_) - { - is_selecting_ = false; - GetWindow()->ReleaseCurrentMouseCapture(); - } - } - } - - void TextBlock::OnLoseFocusCore(events::FocusChangeEventArgs& args) - { - Control::OnLoseFocusCore(args); - if (is_selecting_) - { - is_selecting_ = false; - GetWindow()->ReleaseCurrentMouseCapture(); - } - if (!args.IsWindow()) // If the focus lose is triggered window-wide, then save the selection state. Otherwise, clear selection. - { - selected_range_ = std::nullopt; - Repaint(); - } - } - - Size TextBlock::OnMeasure(const Size& available_size) - { - const auto layout_params = GetLayoutParams(); - - if (layout_params->width.mode == MeasureMode::Stretch && layout_params->height.mode == MeasureMode::Stretch) - return available_size; - - auto&& get_measure_length = [](const LayoutSideParams& layout_length, const float available_length) -> float - { - switch (layout_length.mode) - { - case MeasureMode::Exactly: - { - return std::min(layout_length.length, available_length); - } - case MeasureMode::Stretch: - case MeasureMode::Content: - { - return available_length; - } - default: - UnreachableCode(); - } - }; - - const Size measure_size(get_measure_length(layout_params->width, available_size.width), - get_measure_length(layout_params->height, available_size.height)); - - ThrowIfFailed(text_layout_->SetMaxWidth(measure_size.width)); - ThrowIfFailed(text_layout_->SetMaxHeight(measure_size.height)); - - DWRITE_TEXT_METRICS metrics{}; - - ThrowIfFailed(text_layout_->GetMetrics(&metrics)); - - const Size measure_result(metrics.width, metrics.height); - - auto&& calculate_final_length = [](const LayoutSideParams& layout_length, const float measure_length, const float measure_result_length) -> float - { - if ((layout_length.mode == MeasureMode::Stretch || - layout_length.mode == MeasureMode::Exactly) - && measure_result_length < measure_length) - return measure_length; - else - return measure_result_length; - }; - - const Size result_size( - calculate_final_length(layout_params->width, measure_size.width, measure_result.width), - calculate_final_length(layout_params->height, measure_size.height, measure_result.height) - ); - - return result_size; - } - - void TextBlock::OnTextChangedCore(const String& old_text, const String& new_text) - { - RecreateTextLayout(); - Repaint(); - } - - void TextBlock::RecreateTextLayout() - { - assert(text_format_ != nullptr); - - const auto dwrite_factory = GetDWriteFactory(); - - const auto&& size = GetSize(); - - ThrowIfFailed(dwrite_factory->CreateTextLayout( - text_.c_str(), static_cast<UINT32>(text_.size()), - text_format_.Get(), - size.width, size.height, - &text_layout_ - )); - std::for_each(text_layout_handlers_.cbegin(), text_layout_handlers_.cend(), [this](const TextLayoutHandlerPtr& handler) - { - (*handler)(text_layout_); - }); } } } diff --git a/src/ui/controls/text_block.h b/src/ui/controls/text_block.h index c87ffc51..c307fd2f 100644 --- a/src/ui/controls/text_block.h +++ b/src/ui/controls/text_block.h @@ -1,9 +1,6 @@ #pragma once -#include <memory> -#include <optional> - -#include "ui/control.h" +#include "text_control.h" namespace cru { @@ -11,11 +8,9 @@ namespace cru { namespace controls { - class TextBlock : public Control + class TextBlock : public TextControl { public: - using TextLayoutHandlerPtr = FunctionPtr<void(Microsoft::WRL::ComPtr<IDWriteTextLayout>)>; - static TextBlock* Create( const String& text = L"", const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format = nullptr, @@ -27,89 +22,16 @@ namespace cru } protected: - explicit TextBlock( - const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format = nullptr, - const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush = nullptr + TextBlock( + const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush ); public: TextBlock(const TextBlock& other) = delete; TextBlock(TextBlock&& other) = delete; TextBlock& operator=(const TextBlock& other) = delete; TextBlock& operator=(TextBlock&& other) = delete; - ~TextBlock() override; - - String GetText() const - { - return text_; - } - - void SetText(const String& text); - - Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const - { - return brush_; - } - - void SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush); - - Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const - { - return text_format_; - } - - void SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format); - - - void AddTextLayoutHandler(TextLayoutHandlerPtr handler); - - void RemoveTextLayoutHandler(const TextLayoutHandlerPtr& handler); - - bool IsSelectable() const - { - return is_selectable_; - } - - void SetSelectable(bool is_selectable); - - std::optional<TextRange> GetSelectedRange() const - { - return selected_range_; - } - - void SetSelectedRange(std::optional<TextRange> text_range); - - protected: - void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final; - void OnDraw(ID2D1DeviceContext* device_context) override; - - void OnMouseDownCore(events::MouseButtonEventArgs& args) override final; - void OnMouseMoveCore(events::MouseEventArgs& args) override final; - void OnMouseUpCore(events::MouseButtonEventArgs& args) override final; - - void OnLoseFocusCore(events::FocusChangeEventArgs& args) override final; - - Size OnMeasure(const Size& available_size) override final; - - private: - void OnTextChangedCore(const String& old_text, const String& new_text); - - void RecreateTextLayout(); - - private: - 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<TextLayoutHandlerPtr> text_layout_handlers_; - - bool is_selectable_ = false; - - bool is_selecting_ = false; - unsigned mouse_down_position_ = 0; - std::optional<TextRange> selected_range_ = std::nullopt; + ~TextBlock() override = default; }; } } diff --git a/src/ui/controls/text_box.cpp b/src/ui/controls/text_box.cpp index a8d78398..4a4114ab 100644 --- a/src/ui/controls/text_box.cpp +++ b/src/ui/controls/text_box.cpp @@ -15,13 +15,9 @@ namespace cru::ui::controls } TextBox::TextBox(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, - const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false) + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : TextControl(init_text_format, init_brush) { - text_format_ = init_text_format == nullptr ? graph::CreateDefaultTextFormat() : init_text_format; - - RecreateTextLayout(); - - brush_ = init_brush == nullptr ? CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)) : init_brush; + SetSelectable(true); caret_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)); @@ -30,88 +26,26 @@ namespace cru::ui::controls is_caret_show_ = !is_caret_show_; Repaint(); }); - - //selection_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue)); } TextBox::~TextBox() = default; - void TextBox::SetText(const String& text) - { - if (text_ != text) - { - const auto old_text = text_; - text_ = text; - OnTextChangedCore(old_text, text); - } - } - - void TextBox::SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush) - { - brush_ = brush; - Repaint(); - } - - void TextBox::SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format) - { - text_format_ = text_format; - RecreateTextLayout(); - Repaint(); - } - - void TextBox::OnSizeChangedCore(events::SizeChangedEventArgs& args) - { - Control::OnSizeChangedCore(args); - text_layout_->SetMaxWidth(args.GetNewSize().width); - text_layout_->SetMaxHeight(args.GetNewSize().height); - Repaint(); - } - void TextBox::OnDraw(ID2D1DeviceContext* device_context) { - Control::OnDraw(device_context); - if (text_layout_ != nullptr) + TextControl::OnDraw(device_context); + if (is_caret_show_) { - device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); - if (is_caret_show_) - { - const auto caret_half_width = Application::GetInstance()->GetCaretInfo().half_caret_width; - FLOAT x, y; - DWRITE_HIT_TEST_METRICS metrics{}; - ThrowIfFailed(text_layout_->HitTestTextPosition(position_, FALSE, &x, &y, &metrics)); - device_context->FillRectangle(D2D1::RectF(metrics.left - caret_half_width, metrics.top, metrics.left + caret_half_width, metrics.top + metrics.height), caret_brush_.Get()); - } - } - } - - namespace - { - std::optional<unsigned> TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point, bool test_inside = true) - { - BOOL is_trailing, is_inside; + const auto caret_half_width = Application::GetInstance()->GetCaretInfo().half_caret_width; + FLOAT x, y; 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 TextBox::OnMouseDownCore(events::MouseButtonEventArgs& args) - { - Control::OnMouseDownCore(args); - if (args.GetMouseButton() == MouseButton::Left) - { - position_ = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), false).value(); - - Repaint(); + ThrowIfFailed(text_layout_->HitTestTextPosition(caret_position_, FALSE, &x, &y, &metrics)); + device_context->FillRectangle(D2D1::RectF(metrics.left - caret_half_width, metrics.top, metrics.left + caret_half_width, metrics.top + metrics.height), caret_brush_.Get()); } } void TextBox::OnGetFocusCore(events::FocusChangeEventArgs& args) { - Control::OnGetFocusCore(args); + TextControl::OnGetFocusCore(args); assert(caret_timer_ == nullptr); is_caret_show_ = true; caret_timer_ = SetInterval(Application::GetInstance()->GetCaretInfo().caret_blink_duration, caret_action_); @@ -122,21 +56,24 @@ namespace cru::ui::controls Control::OnLoseFocusCore(args); assert(caret_timer_ != nullptr); caret_timer_->Cancel(); + caret_timer_ = nullptr; is_caret_show_ = false; } void TextBox::OnKeyDownCore(events::KeyEventArgs& args) { Control::OnKeyDownCore(args); - if (args.GetVirtualCode() == VK_LEFT && position_ > 0) + if (args.GetVirtualCode() == VK_LEFT && caret_position_ > 0) { - position_--; + ClearSelection(); + caret_position_--; Repaint(); } - if (args.GetVirtualCode() == VK_RIGHT && position_ < GetText().size()) + if (args.GetVirtualCode() == VK_RIGHT && caret_position_ < GetText().size()) { - position_++; + ClearSelection(); + caret_position_++; Repaint(); } } @@ -146,99 +83,57 @@ namespace cru::ui::controls Control::OnCharCore(args); if (args.GetChar() == L'\b') { - auto text = GetText(); - if (!text.empty() && position_ > 0) + if (GetSelectedRange().has_value()) { - const auto position = --position_; - text.erase(position); + const auto selection_range = GetSelectedRange().value(); + auto text = GetText(); + text.erase(text.cbegin() + selection_range.position, text.cbegin() + selection_range.position + selection_range.count); SetText(text); + caret_position_ = selection_range.position; + ClearSelection(); + } + else + { + if (caret_position_ > 0) + { + auto text = GetText(); + if (!text.empty()) + { + const auto position = --caret_position_; + text.erase(text.cbegin() + position); + SetText(text); + } + } } return; } if (std::iswprint(args.GetChar())) { - const auto position = position_++; - auto text = GetText(); - text.insert(text.cbegin() + position, { args.GetChar() }); - SetText(text); - } - } - - Size TextBox::OnMeasure(const Size& available_size) - { - const auto layout_params = GetLayoutParams(); - - if (layout_params->width.mode == MeasureMode::Stretch && layout_params->height.mode == MeasureMode::Stretch) - return available_size; - - auto&& get_measure_length = [](const LayoutSideParams& layout_length, const float available_length) -> float - { - switch (layout_length.mode) + if (GetSelectedRange().has_value()) { - case MeasureMode::Exactly: - { - return std::min(layout_length.length, available_length); + const auto selection_range = GetSelectedRange().value(); + auto text = GetText(); + text.erase(selection_range.position, selection_range.count); + text.insert(text.cbegin() + selection_range.position, args.GetChar()); + SetText(text); + caret_position_ = selection_range.position + 1; + ClearSelection(); } - case MeasureMode::Stretch: - case MeasureMode::Content: + else { - return available_length; - } - default: - UnreachableCode(); + ClearSelection(); + const auto position = caret_position_++; + auto text = GetText(); + text.insert(text.cbegin() + position, { args.GetChar() }); + SetText(text); } - }; - - const Size measure_size(get_measure_length(layout_params->width, available_size.width), - get_measure_length(layout_params->height, available_size.height)); - - ThrowIfFailed(text_layout_->SetMaxWidth(measure_size.width)); - ThrowIfFailed(text_layout_->SetMaxHeight(measure_size.height)); - - DWRITE_TEXT_METRICS metrics{}; - - ThrowIfFailed(text_layout_->GetMetrics(&metrics)); - - const Size measure_result(metrics.width, metrics.height); - - auto&& calculate_final_length = [](const LayoutSideParams& layout_length, const float measure_length, const float measure_result_length) -> float - { - if ((layout_length.mode == MeasureMode::Stretch || - layout_length.mode == MeasureMode::Exactly) - && measure_result_length < measure_length) - return measure_length; - else - return measure_result_length; - }; - - const Size result_size( - calculate_final_length(layout_params->width, measure_size.width, measure_result.width), - calculate_final_length(layout_params->height, measure_size.height, measure_result.height) - ); - - return result_size; + } } - void TextBox::OnTextChangedCore(const String& old_text, const String& new_text) + void TextBox::RequestChangeCaretPosition(const unsigned position) { - RecreateTextLayout(); + caret_position_ = position; Repaint(); } - - void TextBox::RecreateTextLayout() - { - assert(text_format_ != nullptr); - - const auto dwrite_factory = GetDWriteFactory(); - - const auto&& size = GetSize(); - - ThrowIfFailed(dwrite_factory->CreateTextLayout( - text_.c_str(), static_cast<UINT32>(text_.size()), - text_format_.Get(), - size.width, size.height, - &text_layout_ - )); - } } diff --git a/src/ui/controls/text_box.h b/src/ui/controls/text_box.h index b815ed1f..07c4abe4 100644 --- a/src/ui/controls/text_box.h +++ b/src/ui/controls/text_box.h @@ -1,11 +1,11 @@ #pragma once -#include "ui/control.h" +#include "text_control.h" #include "timer.h" namespace cru::ui::controls { - class TextBox : public Control + class TextBox : public TextControl { public: static TextBox* Create( @@ -17,8 +17,8 @@ namespace cru::ui::controls protected: explicit TextBox( - const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format = nullptr, - const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush = nullptr + const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush ); public: TextBox(const TextBox& other) = delete; @@ -27,59 +27,22 @@ namespace cru::ui::controls TextBox& operator=(TextBox&& other) = delete; ~TextBox() override; - String GetText() const - { - return text_; - } - - void SetText(const String& text); - - Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const - { - return brush_; - } - - void SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush); - - Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const - { - return text_format_; - } - - void SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format); - protected: - void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final; void OnDraw(ID2D1DeviceContext* device_context) override; - void OnMouseDownCore(events::MouseButtonEventArgs& args) override final; - void OnGetFocusCore(events::FocusChangeEventArgs& args) override final; void OnLoseFocusCore(events::FocusChangeEventArgs& args) override final; void OnKeyDownCore(events::KeyEventArgs& args) override final; void OnCharCore(events::CharEventArgs& args) override final; - Size OnMeasure(const Size& available_size) override final; - - private: - void OnTextChangedCore(const String& old_text, const String& new_text); - - void RecreateTextLayout(); + void RequestChangeCaretPosition(unsigned position) override; private: - String text_; - - Microsoft::WRL::ComPtr<ID2D1Brush> brush_; - Microsoft::WRL::ComPtr<ID2D1Brush> caret_brush_; - //Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_; - Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; - Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; - - unsigned position_ = 0; - + unsigned caret_position_ = 0; TimerTask caret_timer_; ActionPtr caret_action_; - bool is_caret_show_; + Microsoft::WRL::ComPtr<ID2D1Brush> caret_brush_; + bool is_caret_show_ = false; }; } diff --git a/src/ui/controls/text_control.cpp b/src/ui/controls/text_control.cpp new file mode 100644 index 00000000..845b090a --- /dev/null +++ b/src/ui/controls/text_control.cpp @@ -0,0 +1,293 @@ +#include "text_control.h" + +#include "ui/window.h" +#include "graph/graph.h" +#include "exception.h" + +namespace cru::ui::controls +{ + using graph::CreateSolidBrush; + + inline Microsoft::WRL::ComPtr<IDWriteFactory> GetDWriteFactory() + { + return graph::GraphManager::GetInstance()->GetDWriteFactory(); + } + + TextControl::TextControl(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false) + { + text_format_ = init_text_format == nullptr ? graph::CreateDefaultTextFormat() : init_text_format; + + RecreateTextLayout(); + + brush_ = init_brush == nullptr ? CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)) : init_brush; + + selection_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue)); + } + + + void TextControl::SetText(const String& text) + { + if (text_ != text) + { + const auto old_text = text_; + text_ = text; + OnTextChangedCore(old_text, text); + } + } + + void TextControl::SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush) + { + brush_ = brush; + Repaint(); + } + + void TextControl::SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format) + { + text_format_ = text_format; + RecreateTextLayout(); + Repaint(); + } + + void TextControl::AddTextLayoutHandler(TextLayoutHandlerPtr handler) + { + text_layout_handlers_.push_back(std::move(handler)); + } + + void TextControl::RemoveTextLayoutHandler(const TextLayoutHandlerPtr& handler) + { + const auto find_result = std::find(text_layout_handlers_.cbegin(), text_layout_handlers_.cend(), + handler); + if (find_result != text_layout_handlers_.cend()) + text_layout_handlers_.erase(find_result); + } + + void TextControl::SetSelectable(const bool is_selectable) + { + if (!is_selectable) + { + if (is_selecting_) + { + is_selecting_ = false; + GetWindow()->ReleaseCurrentMouseCapture(); + } + selected_range_ = std::nullopt; + Repaint(); + } + is_selectable_ = is_selectable; + } + + void TextControl::SetSelectedRange(std::optional<TextRange> text_range) + { + if (is_selectable_) + { + selected_range_ = text_range; + Repaint(); + } + } + + void TextControl::OnSizeChangedCore(events::SizeChangedEventArgs& args) + { + Control::OnSizeChangedCore(args); + ThrowIfFailed(text_layout_->SetMaxWidth(args.GetNewSize().width)); + ThrowIfFailed(text_layout_->SetMaxHeight(args.GetNewSize().height)); + Repaint(); + } + + namespace + { + std::optional<unsigned> TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point, const 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 DrawSelectionRect(ID2D1DeviceContext* device_context, IDWriteTextLayout* layout, ID2D1Brush* brush, const std::optional<TextRange> range) + { + if (range.has_value()) + { + DWRITE_TEXT_METRICS text_metrics{}; + ThrowIfFailed(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; + layout->HitTestTextRange( + range.value().position, 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), brush); + } + } + } + void TextControl::OnDraw(ID2D1DeviceContext* device_context) + { + Control::OnDraw(device_context); + DrawSelectionRect(device_context, text_layout_.Get(), selection_brush_.Get(), selected_range_); + device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); + } + + void TextControl::OnMouseDownCore(events::MouseButtonEventArgs& args) + { + Control::OnMouseDownCore(args); + if (is_selectable_ && args.GetMouseButton() == MouseButton::Left) + { + selected_range_ = std::nullopt; + const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), true); + if (hit_test_result.has_value()) + { + RequestChangeCaretPosition(hit_test_result.value()); + mouse_down_position_ = hit_test_result.value(); + is_selecting_ = true; + GetWindow()->CaptureMouseFor(this); + } + Repaint(); + } + } + + void TextControl::OnMouseMoveCore(events::MouseEventArgs& args) + { + Control::OnMouseMoveCore(args); + if (is_selecting_) + { + const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), false).value(); + RequestChangeCaretPosition(hit_test_result); + if (hit_test_result > mouse_down_position_) + { + selected_range_ = TextRange(mouse_down_position_, hit_test_result - mouse_down_position_); + } + else if (hit_test_result < mouse_down_position_) + { + selected_range_ = TextRange(hit_test_result, mouse_down_position_ - hit_test_result); + } + else + { + selected_range_ = std::nullopt; + } + Repaint(); + } + } + + void TextControl::OnMouseUpCore(events::MouseButtonEventArgs& args) + { + Control::OnMouseUpCore(args); + if (args.GetMouseButton() == MouseButton::Left) + { + if (is_selecting_) + { + is_selecting_ = false; + GetWindow()->ReleaseCurrentMouseCapture(); + } + } + } + + void TextControl::OnLoseFocusCore(events::FocusChangeEventArgs& args) + { + Control::OnLoseFocusCore(args); + if (is_selecting_) + { + is_selecting_ = false; + GetWindow()->ReleaseCurrentMouseCapture(); + } + if (!args.IsWindow()) // If the focus lose is triggered window-wide, then save the selection state. Otherwise, clear selection. + { + selected_range_ = std::nullopt; + Repaint(); + } + } + + + Size TextControl::OnMeasure(const Size& available_size) + { + const auto layout_params = GetLayoutParams(); + + auto&& get_measure_length = [](const LayoutSideParams& layout_length, const float available_length) -> float + { + switch (layout_length.mode) + { + case MeasureMode::Exactly: + { + return std::min(layout_length.length, available_length); + } + case MeasureMode::Stretch: + case MeasureMode::Content: + { + return available_length; + } + default: + UnreachableCode(); + } + }; + + const Size measure_size(get_measure_length(layout_params->width, available_size.width), + get_measure_length(layout_params->height, available_size.height)); + + ThrowIfFailed(text_layout_->SetMaxWidth(measure_size.width)); + ThrowIfFailed(text_layout_->SetMaxHeight(measure_size.height)); + + DWRITE_TEXT_METRICS metrics{}; + + ThrowIfFailed(text_layout_->GetMetrics(&metrics)); + + const Size measure_result(metrics.width, metrics.height); + + auto&& calculate_final_length = [](const LayoutSideParams& layout_length, const float measure_length, const float measure_result_length) -> float + { + if ((layout_length.mode == MeasureMode::Stretch || + layout_length.mode == MeasureMode::Exactly) + && measure_result_length < measure_length) + return measure_length; + else + return measure_result_length; + }; + + const Size result_size( + calculate_final_length(layout_params->width, measure_size.width, measure_result.width), + calculate_final_length(layout_params->height, measure_size.height, measure_result.height) + ); + + return result_size; + } + + void TextControl::RequestChangeCaretPosition(unsigned position) + { + + } + + void TextControl::OnTextChangedCore(const String& old_text, const String& new_text) + { + RecreateTextLayout(); + Repaint(); + } + + void TextControl::RecreateTextLayout() + { + assert(text_format_ != nullptr); + + text_layout_ = nullptr; + + const auto dwrite_factory = GetDWriteFactory(); + + const auto&& size = GetSize(); + + ThrowIfFailed(dwrite_factory->CreateTextLayout( + text_.c_str(), static_cast<UINT32>(text_.size()), + text_format_.Get(), + size.width, size.height, + &text_layout_ + )); + + for (const auto& handler : text_layout_handlers_) + (*handler)(text_layout_); + } +} diff --git a/src/ui/controls/text_control.h b/src/ui/controls/text_control.h new file mode 100644 index 00000000..a24766dc --- /dev/null +++ b/src/ui/controls/text_control.h @@ -0,0 +1,106 @@ +#pragma once + +#include "ui/control.h" + +namespace cru::ui::controls +{ + class TextControl : public Control + { + protected: + TextControl( + const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush + ); + public: + using TextLayoutHandlerPtr = FunctionPtr<void(Microsoft::WRL::ComPtr<IDWriteTextLayout>)>; + + TextControl(const TextControl& other) = delete; + TextControl(TextControl&& other) = delete; + TextControl& operator=(const TextControl& other) = delete; + TextControl& operator=(TextControl&& other) = delete; + ~TextControl() override = default; + + String GetText() const + { + return text_; + } + + void SetText(const String& text); + + Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const + { + return brush_; + } + + void SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush); + + Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const + { + return text_format_; + } + + void SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format); + + + void AddTextLayoutHandler(TextLayoutHandlerPtr handler); + + void RemoveTextLayoutHandler(const TextLayoutHandlerPtr& handler); + + bool IsSelectable() const + { + return is_selectable_; + } + + std::optional<TextRange> GetSelectedRange() const + { + return selected_range_; + } + + void SetSelectedRange(std::optional<TextRange> text_range); + + void ClearSelection() + { + SetSelectedRange(std::nullopt); + } + + protected: + void SetSelectable(bool is_selectable); + + protected: + void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final; + void OnDraw(ID2D1DeviceContext* device_context) override; + + void OnMouseDownCore(events::MouseButtonEventArgs& args) override final; + void OnMouseMoveCore(events::MouseEventArgs& args) override final; + void OnMouseUpCore(events::MouseButtonEventArgs& args) override final; + + void OnLoseFocusCore(events::FocusChangeEventArgs& args) override; + + Size OnMeasure(const Size& available_size) override final; + + virtual void RequestChangeCaretPosition(unsigned position); + + private: + void OnTextChangedCore(const String& old_text, const String& new_text); + + void RecreateTextLayout(); + + private: + String text_; + + Microsoft::WRL::ComPtr<ID2D1Brush> brush_; + Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_; + Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; + protected: + Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; + + private: + Vector<TextLayoutHandlerPtr> text_layout_handlers_; + + bool is_selectable_ = false; + + bool is_selecting_ = false; + unsigned mouse_down_position_ = 0; + std::optional<TextRange> selected_range_ = std::nullopt; + }; +} |