Codechange: use Textbuf directly, instead via several virtual functions in Window

This commit is contained in:
Rubidium 2023-07-01 22:05:07 +02:00 committed by rubidium42
parent 0316940fe8
commit 8c742b456f
7 changed files with 37 additions and 95 deletions

View File

@ -280,22 +280,9 @@ struct IConsoleWindow : Window
}
}
const char *GetFocusedText() const override
Textbuf *GetFocusedTextbuf() const override
{
return _iconsole_cmdline.buf;
}
const char *GetCaret() const override
{
return _iconsole_cmdline.buf + _iconsole_cmdline.caretpos;
}
const char *GetMarkedText(size_t *length) const override
{
if (_iconsole_cmdline.markend == 0) return nullptr;
*length = _iconsole_cmdline.markend - _iconsole_cmdline.markpos;
return _iconsole_cmdline.buf + _iconsole_cmdline.markpos;
return &_iconsole_cmdline;
}
Point GetCaretPosition() const override

View File

@ -47,37 +47,6 @@ public:
Point GetCaretPosition(const Window *w, int wid) const;
Rect GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const;
ptrdiff_t GetCharAtPosition(const Window *w, int wid, const Point &pt) const;
/**
* Get the current text.
* @return Current text.
*/
const char *GetText() const
{
return this->text.buf;
}
/**
* Get the position of the caret in the text buffer.
* @return Pointer to the caret in the text buffer.
*/
const char *GetCaret() const
{
return this->text.buf + this->text.caretpos;
}
/**
* Get the currently marked text.
* @param[out] length Length of the marked text.
* @return Beginning of the marked area or nullptr if no text is marked.
*/
const char *GetMarkedText(size_t *length) const
{
if (this->text.markend == 0) return nullptr;
*length = this->text.markend - this->text.markpos;
return this->text.buf + this->text.markpos;
}
};
void ShowOnScreenKeyboard(Window *parent, int button);

View File

@ -275,6 +275,15 @@ void Textbuf::DiscardMarkedText(bool update)
this->markpos = this->markend = this->markxoffs = this->marklength = 0;
}
/**
* Get the current text.
* @return Current text.
*/
const char *Textbuf::GetText() const
{
return this->buf;
}
/** Update the character iter after the text has changed. */
void Textbuf::UpdateStringIter()
{

View File

@ -65,6 +65,8 @@ struct Textbuf {
void DiscardMarkedText(bool update = true);
const char *GetText() const;
private:
std::unique_ptr<StringIterator> char_iter;

View File

@ -32,6 +32,7 @@
#include "../../window_func.h"
#include "../../window_gui.h"
#include "../../spritecache.h"
#include "../../textbuf_type.h"
#include "../../toolbar_gui.h"
#include "table/sprites.h"
@ -932,7 +933,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
const char *replace_range = NULL;
if (replacementRange.location != NSNotFound) {
/* Calculate the part to be replaced. */
insert_point = Utf8AdvanceByUtf16Units(_focused_window->GetFocusedText(), replacementRange.location);
insert_point = Utf8AdvanceByUtf16Units(_focused_window->GetFocusedTextbuf()->GetText(), replacementRange.location);
replace_range = Utf8AdvanceByUtf16Units(insert_point, replacementRange.length);
}
@ -960,7 +961,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
if (replacementRange.location != NSNotFound) {
/* Calculate the part to be replaced. */
NSRange marked = [ self markedRange ];
insert_point = Utf8AdvanceByUtf16Units(_focused_window->GetFocusedText(), replacementRange.location + (marked.location != NSNotFound ? marked.location : 0u));
insert_point = Utf8AdvanceByUtf16Units(_focused_window->GetFocusedTextbuf()->GetText(), replacementRange.location + (marked.location != NSNotFound ? marked.location : 0u));
replace_range = Utf8AdvanceByUtf16Units(insert_point, replacementRange.length);
}
@ -988,7 +989,9 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
{
if (!EditBoxInGlobalFocus()) return NSMakeRange(NSNotFound, 0);
NSUInteger start = CountUtf16Units(_focused_window->GetFocusedText(), _focused_window->GetCaret());
const Textbuf *text_buf = _focused_window->GetFocusedTextbuf();
const char *text = text_buf->GetText();
NSUInteger start = CountUtf16Units(text, text + text_buf->caretpos);
return NSMakeRange(start, 0);
}
@ -997,11 +1000,12 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
{
if (!EditBoxInGlobalFocus()) return NSMakeRange(NSNotFound, 0);
size_t mark_len;
const char *mark = _focused_window->GetMarkedText(&mark_len);
if (mark != nullptr) {
NSUInteger start = CountUtf16Units(_focused_window->GetFocusedText(), mark);
NSUInteger len = CountUtf16Units(mark, mark + mark_len);
const Textbuf *text_buf = _focused_window->GetFocusedTextbuf();
if (text_buf->markend != 0) {
const char *text = text_buf->GetText();
const char *mark = text + text_buf->markpos;
NSUInteger start = CountUtf16Units(text, mark);
NSUInteger len = CountUtf16Units(mark, text + text_buf->markend);
return NSMakeRange(start, len);
}
@ -1014,8 +1018,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
{
if (!EditBoxInGlobalFocus()) return NO;
size_t len;
return _focused_window->GetMarkedText(&len) != nullptr;
return _focused_window->GetFocusedTextbuf()->markend != 0;
}
/** Get a string corresponding to the given range. */
@ -1023,7 +1026,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
{
if (!EditBoxInGlobalFocus()) return nil;
NSString *s = [ NSString stringWithUTF8String:_focused_window->GetFocusedText() ];
NSString *s = [ NSString stringWithUTF8String:_focused_window->GetFocusedTextbuf()->GetText() ];
NSRange valid_range = NSIntersectionRange(NSMakeRange(0, [ s length ]), theRange);
if (actualRange != nullptr) *actualRange = valid_range;
@ -1043,7 +1046,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
{
if (!EditBoxInGlobalFocus()) return [ [ [ NSAttributedString alloc ] initWithString:@"" ] autorelease ];
return [ [ [ NSAttributedString alloc ] initWithString:[ NSString stringWithUTF8String:_focused_window->GetFocusedText() ] ] autorelease ];
return [ [ [ NSAttributedString alloc ] initWithString:[ NSString stringWithUTF8String:_focused_window->GetFocusedTextbuf()->GetText() ] ] autorelease ];
}
/** Get the character that is rendered at the given point. */
@ -1058,7 +1061,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
auto index = _focused_window->GetTextCharacterAtPosition(pt);
if (index == -1) return NSNotFound;
auto text = _focused_window->GetFocusedText();
auto text = _focused_window->GetFocusedTextbuf()->GetText();
return CountUtf16Units(text, text + index);
}
@ -1067,9 +1070,10 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
{
if (!EditBoxInGlobalFocus()) return NSMakeRect(0, 0, 0, 0);
const char *focused_text = _focused_window->GetFocusedTextbuf()->GetText();
/* Convert range to UTF-8 string pointers. */
const char *start = Utf8AdvanceByUtf16Units(_focused_window->GetFocusedText(), aRange.location);
const char *end = aRange.length != 0 ? Utf8AdvanceByUtf16Units(_focused_window->GetFocusedText(), aRange.location + aRange.length) : start;
const char *start = Utf8AdvanceByUtf16Units(focused_text, aRange.location);
const char *end = aRange.length != 0 ? Utf8AdvanceByUtf16Units(focused_text, aRange.location + aRange.length) : start;
/* Get the bounding rect for the text range.*/
Rect r = _focused_window->GetTextBoundingRect(start, end);

View File

@ -365,40 +365,13 @@ void Window::UpdateQueryStringSize()
}
/**
* Get the current input text if an edit box has the focus.
* @return The currently focused input text or nullptr if no input focused.
* Get the current input text buffer.
* @return The currently focused input text buffer or nullptr if no input focused.
*/
/* virtual */ const char *Window::GetFocusedText() const
/* virtual */ const Textbuf *Window::GetFocusedTextbuf() const
{
if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetText();
}
return nullptr;
}
/**
* Get the string at the caret if an edit box has the focus.
* @return The text at the caret or nullptr if no edit box is focused.
*/
/* virtual */ const char *Window::GetCaret() const
{
if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetCaret();
}
return nullptr;
}
/**
* Get the range of the currently marked input text.
* @param[out] length Length of the marked text.
* @return Pointer to the start of the marked text or nullptr if no text is marked.
*/
/* virtual */ const char *Window::GetMarkedText(size_t *length) const
{
if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetMarkedText(length);
return &this->GetQueryString(this->nested_focus->index)->text;
}
return nullptr;

View File

@ -273,9 +273,7 @@ public:
QueryString *GetQueryString(uint widnum);
void UpdateQueryStringSize();
virtual const char *GetFocusedText() const;
virtual const char *GetCaret() const;
virtual const char *GetMarkedText(size_t *length) const;
virtual const struct Textbuf *GetFocusedTextbuf() const;
virtual Point GetCaretPosition() const;
virtual Rect GetTextBoundingRect(const char *from, const char *to) const;
virtual ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const;