From b7833b2d3243b008a2a726a72408f818070d8238 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 23 Sep 2018 20:31:50 +0800 Subject: ... --- CruUI/ui/controls/text_box.cpp | 215 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 CruUI/ui/controls/text_box.cpp (limited to 'CruUI/ui/controls/text_box.cpp') diff --git a/CruUI/ui/controls/text_box.cpp b/CruUI/ui/controls/text_box.cpp new file mode 100644 index 00000000..ce834ef6 --- /dev/null +++ b/CruUI/ui/controls/text_box.cpp @@ -0,0 +1,215 @@ +#include "text_box.h" + +#include "graph/graph.h" +#include "exception.h" + +namespace cru::ui::controls +{ + using graph::CreateSolidBrush; + + inline Microsoft::WRL::ComPtr GetDWriteFactory() + { + return graph::GraphManager::GetInstance()->GetDWriteFactory(); + } + + TextBox::TextBox(const Microsoft::WRL::ComPtr& init_text_format, + const Microsoft::WRL::ComPtr& init_brush) : Control(false) + { + text_format_ = init_text_format; + + if (init_brush == nullptr) + brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)); + else + brush_ = init_brush; + + caret_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)); + + //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& brush) + { + brush_ = brush; + Repaint(); + } + + void TextBox::SetTextFormat(const Microsoft::WRL::ComPtr& 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) + { + //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 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 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 TextBox::OnMouseDownCore(events::MouseButtonEventArgs& args) + { + Control::OnMouseDownCore(args); + if (args.GetMouseButton() == MouseButton::Left) + { + position_ = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), false).value(); + + Repaint(); + } + } + + void TextBox::OnGetFocusCore(events::FocusChangeEventArgs& args) + { + Control::OnGetFocusCore(args); + assert(caret_timer_ == nullptr); + caret_timer_ = SetInterval(Application::GetInstance()->GetCaretBlinkDuration(), caret_action_); + } + + void TextBox::OnLoseFocusCore(events::FocusChangeEventArgs& args) + { + Control::OnLoseFocusCore(args); + assert(caret_timer_ != nullptr); + caret_timer_->Cancel(); + } + + Size TextBox::OnMeasure(const Size& available_size) + { + if (text_.empty()) + return Size::Zero(); + + 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 TextBox::OnTextChangedCore(const String& old_text, const String& new_text) + { + RecreateTextLayout(); + Repaint(); + } + + void TextBox::RecreateTextLayout() + { + if (text_.empty()) + { + text_layout_ = nullptr; + return; + } + + const auto dwrite_factory = GetDWriteFactory(); + + if (text_format_ == nullptr) + text_format_ = graph::CreateDefaultTextFormat(); + + const auto&& size = GetSize(); + + ThrowIfFailed(dwrite_factory->CreateTextLayout( + text_.c_str(), static_cast(text_.size()), + text_format_.Get(), + size.width, size.height, + &text_layout_ + )); + } +} -- cgit v1.2.3 From be84ddd03d3b59c0c27aa538d5ef5129f94d511c Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 23 Sep 2018 23:08:54 +0800 Subject: Add keyboard events to control. --- CruUI/ui/control.cpp | 45 +++++++++++++++++++++++++++++++++++++++ CruUI/ui/control.h | 17 +++++++++++++++ CruUI/ui/controls/text_block.cpp | 37 ++++++++++++++------------------ CruUI/ui/controls/text_box.cpp | 42 +++++++++++------------------------- CruUI/ui/events/ui_event.h | 46 ++++++++++++++++++++++++++++++++++++++++ CruUI/ui/window.cpp | 30 ++++++++++++++++++++++++-- CruUI/ui/window.h | 5 +++++ 7 files changed, 169 insertions(+), 53 deletions(-) (limited to 'CruUI/ui/controls/text_box.cpp') diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp index 25f7c028..eaf206ac 100644 --- a/CruUI/ui/control.cpp +++ b/CruUI/ui/control.cpp @@ -472,6 +472,51 @@ namespace cru { } + void Control::OnKeyDown(KeyEventArgs& args) + { + } + + void Control::OnKeyUp(KeyEventArgs& args) + { + } + + void Control::OnChar(events::CharEvent& args) + { + } + + void Control::OnKeyDownCore(KeyEventArgs& args) + { + } + + void Control::OnKeyUpCore(KeyEventArgs& args) + { + } + + void Control::OnCharCore(events::CharEvent& args) + { + } + + void Control::RaiseKeyDownEvent(KeyEventArgs& args) + { + OnKeyDownCore(args); + OnKeyDown(args); + key_down_event.Raise(args); + } + + void Control::RaiseKeyUpEvent(KeyEventArgs& args) + { + OnKeyUpCore(args); + OnKeyUp(args); + key_up_event.Raise(args); + } + + void Control::RaiseCharEvent(CharEvent& args) + { + OnCharCore(args); + OnChar(args); + char_event.Raise(args); + } + void Control::OnGetFocus(FocusChangeEventArgs& args) { diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h index 78261a80..fa1158a4 100644 --- a/CruUI/ui/control.h +++ b/CruUI/ui/control.h @@ -214,6 +214,10 @@ namespace cru //Raised when a mouse button is pressed in the control and released in the control with mouse not leaving it between two operations. events::MouseButtonEvent mouse_click_event; + events::KeyEvent key_down_event; + events::KeyEvent key_up_event; + events::CharEvent char_event; + events::FocusChangeEvent get_focus_event; events::FocusChangeEvent lose_focus_event; @@ -278,6 +282,19 @@ namespace cru virtual void OnMouseClickBegin(MouseButton button); virtual void OnMouseClickEnd(MouseButton button); + //*************** region: keyboard event *************** + virtual void OnKeyDown(events::KeyEventArgs& args); + virtual void OnKeyUp(events::KeyEventArgs& args); + virtual void OnChar(events::CharEvent& args); + + virtual void OnKeyDownCore(events::KeyEventArgs& args); + virtual void OnKeyUpCore(events::KeyEventArgs& args); + virtual void OnCharCore(events::CharEvent& args); + + void RaiseKeyDownEvent(events::KeyEventArgs& args); + void RaiseKeyUpEvent(events::KeyEventArgs& args); + void RaiseCharEvent(events::CharEvent& args); + //*************** region: focus event *************** virtual void OnGetFocus(events::FocusChangeEventArgs& args); virtual void OnLoseFocus(events::FocusChangeEventArgs& args); diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp index f0ef41d9..4ecbb672 100644 --- a/CruUI/ui/controls/text_block.cpp +++ b/CruUI/ui/controls/text_block.cpp @@ -20,12 +20,11 @@ namespace cru TextBlock::TextBlock(const Microsoft::WRL::ComPtr& init_text_format, const Microsoft::WRL::ComPtr& init_brush) : Control(false) { - text_format_ = init_text_format; + text_format_ = init_text_format == nullptr ? graph::CreateDefaultTextFormat() : init_text_format; - if (init_brush == nullptr) - brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)); - else - brush_ = init_brush; + RecreateTextLayout(); + + brush_ = init_brush == nullptr ? CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)) : init_brush; selection_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue)); } @@ -126,15 +125,18 @@ namespace cru } } - std::optional TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point, bool test_inside = true) + namespace { - 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; + std::optional 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) @@ -264,17 +266,10 @@ namespace cru void TextBlock::RecreateTextLayout() { - if (text_.empty()) - { - text_layout_ = nullptr; - return; - } + assert(text_format_ != nullptr); const auto dwrite_factory = GetDWriteFactory(); - if (text_format_ == nullptr) - text_format_ = graph::CreateDefaultTextFormat(); - const auto&& size = GetSize(); ThrowIfFailed(dwrite_factory->CreateTextLayout( diff --git a/CruUI/ui/controls/text_box.cpp b/CruUI/ui/controls/text_box.cpp index ce834ef6..586bc017 100644 --- a/CruUI/ui/controls/text_box.cpp +++ b/CruUI/ui/controls/text_box.cpp @@ -65,40 +65,22 @@ namespace cru::ui::controls Control::OnDraw(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 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 TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point, bool test_inside = true) + namespace { - 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; + std::optional 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 TextBox::OnMouseDownCore(events::MouseButtonEventArgs& args) @@ -107,7 +89,7 @@ namespace cru::ui::controls if (args.GetMouseButton() == MouseButton::Left) { position_ = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), false).value(); - + Repaint(); } } diff --git a/CruUI/ui/events/ui_event.h b/CruUI/ui/events/ui_event.h index a17067c7..b042b706 100644 --- a/CruUI/ui/events/ui_event.h +++ b/CruUI/ui/events/ui_event.h @@ -257,6 +257,50 @@ namespace cru std::optional result_; }; + class KeyEventArgs : public UiEventArgs + { + public: + KeyEventArgs(Object* sender, Object* original_sender, int virtual_code) + : UiEventArgs(sender, original_sender), virtual_code_(virtual_code) + { + } + KeyEventArgs(const KeyEventArgs& other) = default; + KeyEventArgs(KeyEventArgs&& other) = default; + KeyEventArgs& operator=(const KeyEventArgs& other) = default; + KeyEventArgs& operator=(KeyEventArgs&& other) = default; + ~KeyEventArgs() override = default; + + int GetVirtualCode() const + { + return virtual_code_; + } + + private: + int virtual_code_; + }; + + class CharEventArgs : public UiEventArgs + { + public: + CharEventArgs(Object* sender, Object* original_sender, wchar_t c) + : UiEventArgs(sender, original_sender), c_(c) + { + } + CharEventArgs(const CharEventArgs& other) = default; + CharEventArgs(CharEventArgs&& other) = default; + CharEventArgs& operator=(const CharEventArgs& other) = default; + CharEventArgs& operator=(CharEventArgs&& other) = default; + ~CharEventArgs() override = default; + + wchar_t GetChar() const + { + return c_; + } + + private: + wchar_t c_; + }; + using UiEvent = Event; using MouseEvent = Event; using MouseButtonEvent = Event; @@ -266,6 +310,8 @@ namespace cru using FocusChangeEvent = Event; using ToggleEvent = Event; using WindowNativeMessageEvent = Event; + using KeyEvent = Event; + using CharEvent = Event; } } } \ No newline at end of file diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp index 6ff962b6..34a54512 100644 --- a/CruUI/ui/window.cpp +++ b/CruUI/ui/window.cpp @@ -204,8 +204,7 @@ namespace cru if (!native_message_event.IsNoHandler()) { - const events::WindowNativeMessage message{hwnd, msg, w_param, l_param}; - events::WindowNativeMessageEventArgs args(this, this, message); + events::WindowNativeMessageEventArgs args(this, this, {hwnd, msg, w_param, l_param}); native_message_event.Raise(args); if (args.GetResult().has_value()) { @@ -293,6 +292,18 @@ namespace cru result = 0; return true; } + case WM_KEYDOWN: + OnKeyDownInternal(static_cast(w_param)); + result = 0; + return true; + case WM_KEYUP: + OnKeyUpInternal(static_cast(w_param)); + result = 0; + return true; + case WM_CHAR: + OnCharInternal(static_cast(w_param)); + result = 0; + return true; case WM_SIZE: OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); result = 0; @@ -553,6 +564,21 @@ namespace cru DispatchEvent(control, &Control::RaiseMouseUpEvent, nullptr, dip_point, button); } + void Window::OnKeyDownInternal(int virtual_code) + { + DispatchEvent(focus_control_, &Control::RaiseKeyDownEvent, nullptr, virtual_code); + } + + void Window::OnKeyUpInternal(int virtual_code) + { + DispatchEvent(focus_control_, &Control::RaiseKeyUpEvent, nullptr, virtual_code); + } + + void Window::OnCharInternal(wchar_t c) + { + DispatchEvent(focus_control_, &Control::RaiseCharEvent, nullptr, c); + } + void Window::OnActivatedInternal() { events::UiEventArgs args(this, this); diff --git a/CruUI/ui/window.h b/CruUI/ui/window.h index 42ebf477..40d81a06 100644 --- a/CruUI/ui/window.h +++ b/CruUI/ui/window.h @@ -6,6 +6,7 @@ #include #include "control.h" +#include "events/ui_event.h" namespace cru { namespace graph { @@ -219,6 +220,10 @@ namespace cru { void OnMouseDownInternal(MouseButton button, POINT point); void OnMouseUpInternal(MouseButton button, POINT point); + void OnKeyDownInternal(int virtual_code); + void OnKeyUpInternal(int virtual_code); + void OnCharInternal(wchar_t c); + void OnActivatedInternal(); void OnDeactivatedInternal(); -- cgit v1.2.3 From 977c766e2337fea238804b8d8b97659361391ed0 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 24 Sep 2018 00:16:53 +0800 Subject: Develop basic function of textbox. --- CruUI/application.cpp | 6 +++- CruUI/application.h | 11 +++++-- CruUI/base.cpp | 2 +- CruUI/main.cpp | 8 ++++- CruUI/ui/control.cpp | 6 ++-- CruUI/ui/control.h | 6 ++-- CruUI/ui/controls/text_block.cpp | 42 +++++++++++--------------- CruUI/ui/controls/text_box.cpp | 65 +++++++++++++++++++++++++++++----------- CruUI/ui/controls/text_box.h | 13 +++++++- 9 files changed, 105 insertions(+), 54 deletions(-) (limited to 'CruUI/ui/controls/text_box.cpp') diff --git a/CruUI/application.cpp b/CruUI/application.cpp index 2d949edb..af38116f 100644 --- a/CruUI/application.cpp +++ b/CruUI/application.cpp @@ -105,7 +105,11 @@ namespace cru { debug_border_brush_ = graph::CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Crimson)); #endif - caret_blink_duration_ = std::chrono::milliseconds(::GetCaretBlinkTime()); + caret_info_.caret_blink_duration = std::chrono::milliseconds(::GetCaretBlinkTime()); + DWORD caret_width; + if (!::SystemParametersInfoW(SPI_GETCARETWIDTH, 0 , &caret_width, 0)) + throw Win32Error(::GetLastError(), "Failed to get system caret width."); + caret_info_.half_caret_width = caret_width / 2.0f; } Application::~Application() diff --git a/CruUI/application.h b/CruUI/application.h index 521c3436..106e7171 100644 --- a/CruUI/application.h +++ b/CruUI/application.h @@ -28,6 +28,11 @@ namespace cru class TimerManager; + struct CaretInfo + { + std::chrono::milliseconds caret_blink_duration; + float half_caret_width; + }; class GodWindow : public Object { @@ -101,9 +106,9 @@ namespace cru return god_window_.get(); } - std::chrono::milliseconds GetCaretBlinkDuration() const + CaretInfo GetCaretInfo() const { - return caret_blink_duration_; + return caret_info_; } #ifdef CRU_DEBUG_DRAW_CONTROL_BORDER @@ -127,7 +132,7 @@ namespace cru Microsoft::WRL::ComPtr debug_border_brush_; #endif - std::chrono::milliseconds caret_blink_duration_; + CaretInfo caret_info_; }; diff --git a/CruUI/base.cpp b/CruUI/base.cpp index c6b57e33..f5868170 100644 --- a/CruUI/base.cpp +++ b/CruUI/base.cpp @@ -13,7 +13,7 @@ namespace cru const auto length = ::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, nullptr, 0, nullptr, nullptr); MultiByteString result; result.reserve(length); - if (::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, result.data(), result.capacity(), nullptr, nullptr) == 0) + if (::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, result.data(), static_cast(result.capacity()), nullptr, nullptr) == 0) throw Win32Error(::GetLastError(), "Failed to convert wide string to UTF-8."); return result; } diff --git a/CruUI/main.cpp b/CruUI/main.cpp index 56d42894..3d801964 100644 --- a/CruUI/main.cpp +++ b/CruUI/main.cpp @@ -6,6 +6,7 @@ #include "ui/controls/button.h" #include "ui/controls/margin_container.h" #include "ui/events/ui_event.h" +#include "ui/controls/text_box.h" using cru::String; @@ -20,6 +21,7 @@ using cru::ui::controls::TextBlock; using cru::ui::controls::ToggleButton; using cru::ui::controls::Button; using cru::ui::controls::MarginContainer; +using cru::ui::controls::TextBox; int APIENTRY wWinMain( HINSTANCE hInstance, @@ -84,8 +86,9 @@ int APIENTRY wWinMain( }); */ + /* //test 2 - + const auto layout = CreateWithLayout(LayoutSideParams::Exactly(500), LayoutSideParams::Content()); layout->mouse_click_event.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args) @@ -137,6 +140,9 @@ int APIENTRY wWinMain( window.AddChild(layout); + */ + + window.AddChild(CreateWithLayout(LayoutSideParams::Stretch(), LayoutSideParams::Stretch())); window.Show(); diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp index eaf206ac..8aec8640 100644 --- a/CruUI/ui/control.cpp +++ b/CruUI/ui/control.cpp @@ -480,7 +480,7 @@ namespace cru { { } - void Control::OnChar(events::CharEvent& args) + void Control::OnChar(CharEventArgs& args) { } @@ -492,7 +492,7 @@ namespace cru { { } - void Control::OnCharCore(events::CharEvent& args) + void Control::OnCharCore(CharEventArgs& args) { } @@ -510,7 +510,7 @@ namespace cru { key_up_event.Raise(args); } - void Control::RaiseCharEvent(CharEvent& args) + void Control::RaiseCharEvent(CharEventArgs& args) { OnCharCore(args); OnChar(args); diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h index fa1158a4..d6cbae40 100644 --- a/CruUI/ui/control.h +++ b/CruUI/ui/control.h @@ -285,15 +285,15 @@ namespace cru //*************** region: keyboard event *************** virtual void OnKeyDown(events::KeyEventArgs& args); virtual void OnKeyUp(events::KeyEventArgs& args); - virtual void OnChar(events::CharEvent& args); + virtual void OnChar(events::CharEventArgs& args); virtual void OnKeyDownCore(events::KeyEventArgs& args); virtual void OnKeyUpCore(events::KeyEventArgs& args); - virtual void OnCharCore(events::CharEvent& args); + virtual void OnCharCore(events::CharEventArgs& args); void RaiseKeyDownEvent(events::KeyEventArgs& args); void RaiseKeyUpEvent(events::KeyEventArgs& args); - void RaiseCharEvent(events::CharEvent& args); + void RaiseCharEvent(events::CharEventArgs& args); //*************** region: focus event *************** virtual void OnGetFocus(events::FocusChangeEventArgs& args); diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp index 4ecbb672..93d66ba6 100644 --- a/CruUI/ui/controls/text_block.cpp +++ b/CruUI/ui/controls/text_block.cpp @@ -98,31 +98,28 @@ namespace cru void TextBlock::OnDraw(ID2D1DeviceContext* device_context) { Control::OnDraw(device_context); - if (text_layout_ != nullptr) + if (selected_range_.has_value()) { - 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 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) { - DWRITE_TEXT_METRICS text_metrics{}; - ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); - const auto metrics_count = text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; - - Vector 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->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()); } + device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); } namespace @@ -202,9 +199,6 @@ namespace cru Size TextBlock::OnMeasure(const Size& available_size) { - if (text_.empty()) - return Size::Zero(); - const auto layout_params = GetLayoutParams(); if (layout_params->width.mode == MeasureMode::Stretch && layout_params->height.mode == MeasureMode::Stretch) diff --git a/CruUI/ui/controls/text_box.cpp b/CruUI/ui/controls/text_box.cpp index 586bc017..53417d40 100644 --- a/CruUI/ui/controls/text_box.cpp +++ b/CruUI/ui/controls/text_box.cpp @@ -1,5 +1,7 @@ #include "text_box.h" +#include + #include "graph/graph.h" #include "exception.h" @@ -15,15 +17,20 @@ namespace cru::ui::controls TextBox::TextBox(const Microsoft::WRL::ComPtr& init_text_format, const Microsoft::WRL::ComPtr& init_brush) : Control(false) { - text_format_ = init_text_format; + text_format_ = init_text_format == nullptr ? graph::CreateDefaultTextFormat() : init_text_format; + + RecreateTextLayout(); - if (init_brush == nullptr) - brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)); - else - brush_ = init_brush; + brush_ = init_brush == nullptr ? CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)) : init_brush; caret_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black)); + caret_action_ = CreateActionPtr([this] + { + is_caret_show_ = !is_caret_show_; + Repaint(); + }); + //selection_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue)); } @@ -66,6 +73,14 @@ namespace cru::ui::controls if (text_layout_ != nullptr) { 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()); + } } } @@ -98,7 +113,8 @@ namespace cru::ui::controls { Control::OnGetFocusCore(args); assert(caret_timer_ == nullptr); - caret_timer_ = SetInterval(Application::GetInstance()->GetCaretBlinkDuration(), caret_action_); + is_caret_show_ = true; + caret_timer_ = SetInterval(Application::GetInstance()->GetCaretInfo().caret_blink_duration, caret_action_); } void TextBox::OnLoseFocusCore(events::FocusChangeEventArgs& args) @@ -106,13 +122,35 @@ namespace cru::ui::controls Control::OnLoseFocusCore(args); assert(caret_timer_ != nullptr); caret_timer_->Cancel(); + is_caret_show_ = false; } - Size TextBox::OnMeasure(const Size& available_size) + void TextBox::OnCharCore(events::CharEventArgs& args) { - if (text_.empty()) - return Size::Zero(); + Control::OnCharCore(args); + if (args.GetChar() == L'\b') + { + auto text = GetText(); + if (!text.empty() && position_ > 0) + { + const auto position = --position_; + text.erase(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) @@ -174,17 +212,10 @@ namespace cru::ui::controls void TextBox::RecreateTextLayout() { - if (text_.empty()) - { - text_layout_ = nullptr; - return; - } + assert(text_format_ != nullptr); const auto dwrite_factory = GetDWriteFactory(); - if (text_format_ == nullptr) - text_format_ = graph::CreateDefaultTextFormat(); - const auto&& size = GetSize(); ThrowIfFailed(dwrite_factory->CreateTextLayout( diff --git a/CruUI/ui/controls/text_box.h b/CruUI/ui/controls/text_box.h index 68235c67..5fe14782 100644 --- a/CruUI/ui/controls/text_box.h +++ b/CruUI/ui/controls/text_box.h @@ -7,6 +7,14 @@ namespace cru::ui::controls { class TextBox : public Control { + public: + static TextBox* Create( + const Microsoft::WRL::ComPtr& init_text_format = nullptr, + const Microsoft::WRL::ComPtr& init_brush = nullptr) + { + return new TextBox(init_text_format, init_brush); + } + protected: explicit TextBox( const Microsoft::WRL::ComPtr& init_text_format = nullptr, @@ -46,9 +54,11 @@ namespace cru::ui::controls void OnMouseDownCore(events::MouseButtonEventArgs& args) override final; - void OnGetFocusCore(events::FocusChangeEventArgs& args) override; + void OnGetFocusCore(events::FocusChangeEventArgs& args) override final; void OnLoseFocusCore(events::FocusChangeEventArgs& args) override final; + void OnCharCore(events::CharEventArgs& args) override final; + Size OnMeasure(const Size& available_size) override final; private: @@ -69,5 +79,6 @@ namespace cru::ui::controls TimerTask caret_timer_; ActionPtr caret_action_; + bool is_caret_show_; }; } -- cgit v1.2.3 From 9049f7674e0808cc3676543e0a95330a1657d10e Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 24 Sep 2018 00:22:04 +0800 Subject: Add left and right support. --- CruUI/main.cpp | 6 +++--- CruUI/ui/controls/text_box.cpp | 16 ++++++++++++++++ CruUI/ui/controls/text_box.h | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) (limited to 'CruUI/ui/controls/text_box.cpp') diff --git a/CruUI/main.cpp b/CruUI/main.cpp index 3d801964..67b35406 100644 --- a/CruUI/main.cpp +++ b/CruUI/main.cpp @@ -31,7 +31,7 @@ int APIENTRY wWinMain( Application application(hInstance); Window window; - + /* window.native_message_event.AddHandler([](cru::ui::events::WindowNativeMessageEventArgs& args) { if (args.GetWindowMessage().msg == WM_PAINT) @@ -40,7 +40,7 @@ int APIENTRY wWinMain( //args.SetResult(0); } }); - + */ /* // test1 cru::ui::controls::TextBlock text_block; @@ -88,7 +88,7 @@ int APIENTRY wWinMain( /* //test 2 - + const auto layout = CreateWithLayout(LayoutSideParams::Exactly(500), LayoutSideParams::Content()); layout->mouse_click_event.AddHandler([layout](cru::ui::events::MouseButtonEventArgs& args) diff --git a/CruUI/ui/controls/text_box.cpp b/CruUI/ui/controls/text_box.cpp index 53417d40..a8d78398 100644 --- a/CruUI/ui/controls/text_box.cpp +++ b/CruUI/ui/controls/text_box.cpp @@ -125,6 +125,22 @@ namespace cru::ui::controls is_caret_show_ = false; } + void TextBox::OnKeyDownCore(events::KeyEventArgs& args) + { + Control::OnKeyDownCore(args); + if (args.GetVirtualCode() == VK_LEFT && position_ > 0) + { + position_--; + Repaint(); + } + + if (args.GetVirtualCode() == VK_RIGHT && position_ < GetText().size()) + { + position_++; + Repaint(); + } + } + void TextBox::OnCharCore(events::CharEventArgs& args) { Control::OnCharCore(args); diff --git a/CruUI/ui/controls/text_box.h b/CruUI/ui/controls/text_box.h index 5fe14782..b815ed1f 100644 --- a/CruUI/ui/controls/text_box.h +++ b/CruUI/ui/controls/text_box.h @@ -57,6 +57,7 @@ namespace cru::ui::controls 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; -- cgit v1.2.3