aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CruUI/main.cpp7
-rw-r--r--CruUI/ui/controls/text_block.cpp22
-rw-r--r--CruUI/ui/controls/text_block.h17
3 files changed, 44 insertions, 2 deletions
diff --git a/CruUI/main.cpp b/CruUI/main.cpp
index 6c96c833..3c10f73b 100644
--- a/CruUI/main.cpp
+++ b/CruUI/main.cpp
@@ -113,7 +113,12 @@ int APIENTRY wWinMain(
});
layout->AddChild(text_block);
- layout->AddChild(create_text_block(L"This is a very very very very very long sentence!!!", MeasureLength::Stretch(), MeasureLength::Stretch()));
+ }
+
+ {
+ const auto text_block = create_text_block(L"This is a very very very very very long sentence!!!", MeasureLength::Stretch(), MeasureLength::Stretch());
+ text_block->SetSelectable(true);
+ layout->AddChild(text_block);
}
{
diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp
index 5fabb3f4..45f0d574 100644
--- a/CruUI/ui/controls/text_block.cpp
+++ b/CruUI/ui/controls/text_block.cpp
@@ -65,6 +65,26 @@ namespace cru
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)
+ {
+ if (is_selectable_)
+ {
+ selected_range_ = text_range;
+ Repaint();
+ }
+ }
+
void TextBlock::OnSizeChangedCore(events::SizeChangedEventArgs& args)
{
Control::OnSizeChangedCore(args);
@@ -117,7 +137,7 @@ namespace cru
void TextBlock::OnMouseDownCore(events::MouseButtonEventArgs& args)
{
Control::OnMouseDownCore(args);
- if (args.GetMouseButton() == MouseButton::Left)
+ if (is_selectable_ && args.GetMouseButton() == MouseButton::Left)
{
RequestFocus();
selected_range_ = std::nullopt;
diff --git a/CruUI/ui/controls/text_block.h b/CruUI/ui/controls/text_block.h
index dce83427..b05e7ff2 100644
--- a/CruUI/ui/controls/text_block.h
+++ b/CruUI/ui/controls/text_block.h
@@ -1,6 +1,7 @@
#pragma once
#include <memory>
+#include <optional>
#include "ui/control.h"
@@ -86,6 +87,20 @@ namespace cru
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;
@@ -114,6 +129,8 @@ namespace cru
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;