Administrative source import: native x86 compilation passed for 423 authored units. Source placement and submission states are migrated separately; this commit does not imply peer acceptance.
Previous head: 35d2a8cb36
Payload SHA256: 592f016d2730c5593bb653aa054c1e5f76a6df72c8f5a09e66f74876e2e33dee
67 lines
2.7 KiB
C++
67 lines
2.7 KiB
C++
#include "Client/Input/TextInputBuffer.h"
|
|
#include "Render/Text/TextContext.h"
|
|
#include "UI/Edit/EditTextState.h"
|
|
#include "UI/Edit/EditViewport.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
|
|
// OFFSET: 0x00862FB0
|
|
// FUNCTION: fn_10905
|
|
void EditViewport::Synchronize() {
|
|
const int previousCursor = this->cursor;
|
|
this->input.CopyDisplayText(this->text);
|
|
this->input.Selection(this->anchor, this->cursor);
|
|
this->state.Trim();
|
|
const auto length = static_cast<std::uint32_t>(this->text.size());
|
|
bool clamped = false;
|
|
|
|
if (static_cast<std::uint32_t>(this->cursor) > length || static_cast<std::uint32_t>(this->anchor) > length) {
|
|
this->anchor = static_cast<int>(std::min(static_cast<std::uint32_t>(this->anchor), length));
|
|
this->cursor = static_cast<int>(std::min(static_cast<std::uint32_t>(this->cursor), length));
|
|
this->input.Select(this->anchor, this->cursor);
|
|
clamped = true;
|
|
}
|
|
|
|
if (this->cursor == 0) {
|
|
this->firstVisible = 0;
|
|
return;
|
|
}
|
|
|
|
const auto selection =
|
|
static_cast<std::uint32_t>(std::max(this->input.cursor, this->input.anchor)) - static_cast<std::uint32_t>(std::min(this->input.cursor, this->input.anchor));
|
|
|
|
if (selection >= length) {
|
|
this->firstVisible = 0;
|
|
}
|
|
|
|
TextContext context(this->surface, this->fonts, this->locale);
|
|
context.SetFont(0, 12, 0, 0);
|
|
const auto measure = [&](const char* text, unsigned bytes) { return context.Measure(text, static_cast<int>(bytes ? bytes : std::strlen(text))).cx; };
|
|
const int available = this->width - 7;
|
|
|
|
if (this->cursor > previousCursor || clamped) {
|
|
const int before = this->firstVisible == 0 ? 0 : measure(this->text.c_str(), static_cast<unsigned>(this->firstVisible));
|
|
const int position = measure(this->text.c_str(), static_cast<unsigned>(this->cursor)) + 3 - before;
|
|
|
|
if (position > available) {
|
|
const char* end = this->text.c_str() + this->text.size();
|
|
const char* next = CharNextExA(static_cast<WORD>(this->codePage), this->text.c_str(), 0);
|
|
|
|
while (measure(next, 0) + 3 > available && next < end) {
|
|
next = CharNextExA(static_cast<WORD>(this->codePage), next, 0);
|
|
}
|
|
|
|
const auto tail = static_cast<std::uint32_t>(std::strlen(next));
|
|
const int candidate = this->firstVisible + this->cursor - previousCursor;
|
|
this->firstVisible = static_cast<int>(std::min(static_cast<std::uint32_t>(this->cursor) - tail, static_cast<std::uint32_t>(candidate)));
|
|
}
|
|
} else if (this->cursor < previousCursor) {
|
|
if (measure(this->text.c_str(), static_cast<unsigned>(this->cursor)) + 3 < available && this->firstVisible > this->cursor) {
|
|
this->firstVisible += this->cursor - previousCursor;
|
|
}
|
|
|
|
this->firstVisible = std::max(this->firstVisible, 0);
|
|
}
|
|
}
|