diff options
Diffstat (limited to 'src/ui/controls/text_control.cpp')
-rw-r--r-- | src/ui/controls/text_control.cpp | 120 |
1 files changed, 6 insertions, 114 deletions
diff --git a/src/ui/controls/text_control.cpp b/src/ui/controls/text_control.cpp index 0b730500..da0113f3 100644 --- a/src/ui/controls/text_control.cpp +++ b/src/ui/controls/text_control.cpp @@ -130,9 +130,9 @@ namespace cru::ui::controls } } } - void TextControl::OnDraw(ID2D1DeviceContext* device_context) + void TextControl::OnDrawContent(ID2D1DeviceContext* device_context) { - Control::OnDraw(device_context); + Control::OnDrawContent(device_context); DrawSelectionRect(device_context, text_layout_.Get(), selection_brush_.Get(), selected_range_); device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); } @@ -207,103 +207,10 @@ namespace cru::ui::controls } - Size TextControl::OnMeasure(const Size& available_size) + Size TextControl::OnMeasureContent(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; - } - - inline Size ThicknessToSize(const Thickness& thickness) - { - return Size(thickness.left + thickness.right, thickness.top + thickness.bottom); - } - - inline float AtLeast0(const float value) - { - return value < 0 ? 0 : value; - } - - inline Size AtLeast0(const Size& size) - { - return Size(AtLeast0(size.width), AtLeast0(size.height)); - } - - Size TextControl::TextMeasureWithPadding(const Size& available_size, const Thickness& padding) - { - const auto layout_params = GetLayoutParams(); - const auto padding_size = ThicknessToSize(padding); - - 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(); - } - }; - - Size measure_size(get_measure_length(layout_params->width, available_size.width), - get_measure_length(layout_params->height, available_size.height)); - - measure_size = AtLeast0(measure_size - padding_size); - - ThrowIfFailed(text_layout_->SetMaxWidth(measure_size.width)); - ThrowIfFailed(text_layout_->SetMaxHeight(measure_size.height)); + ThrowIfFailed(text_layout_->SetMaxWidth(available_size.width)); + ThrowIfFailed(text_layout_->SetMaxHeight(available_size.height)); DWRITE_TEXT_METRICS metrics{}; @@ -311,22 +218,7 @@ namespace cru::ui::controls 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 + padding_size; + return measure_result; } void TextControl::RequestChangeCaretPosition(unsigned position) |