diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/console_gui.cpp | 10 | ||||
-rw-r--r-- | src/fios_gui.cpp | 2 | ||||
-rw-r--r-- | src/misc_gui.cpp | 102 | ||||
-rw-r--r-- | src/network/network_chat_gui.cpp | 4 | ||||
-rw-r--r-- | src/querystring_gui.h | 4 | ||||
-rw-r--r-- | src/textbuf_gui.h | 11 |
6 files changed, 68 insertions, 65 deletions
diff --git a/src/console_gui.cpp b/src/console_gui.cpp index 3bd50bb87..f318d1921 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -136,8 +136,8 @@ IConsoleModes _iconsole_mode; static void IConsoleClearCommand() { memset(_iconsole_cmdline.buf, 0, ICON_CMDLN_SIZE); - _iconsole_cmdline.size = 1; // only terminating zero - _iconsole_cmdline.width = 0; + _iconsole_cmdline.bytes = 1; // only terminating zero + _iconsole_cmdline.pixels = 0; _iconsole_cmdline.caretpos = 0; _iconsole_cmdline.caretxoffs = 0; SetWindowDirty(WC_CONSOLE, 0); @@ -201,7 +201,7 @@ struct IConsoleWindow : Window if (ypos <= top) break; } /* If the text is longer than the window, don't show the starting ']' */ - int delta = this->width - this->line_offset - _iconsole_cmdline.width - ICON_RIGHT_BORDERWIDTH; + int delta = this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH; if (delta > 0) { DrawString(5, right, this->height - this->line_height, "]", (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE); delta = 0; @@ -364,7 +364,7 @@ void IConsoleGUIInit() memset(_iconsole_history, 0, sizeof(_iconsole_history)); _iconsole_cmdline.buf = CallocT<char>(ICON_CMDLN_SIZE); // create buffer and zero it - _iconsole_cmdline.maxsize = ICON_CMDLN_SIZE; + _iconsole_cmdline.max_bytes = ICON_CMDLN_SIZE; IConsolePrintF(CC_WARNING, "OpenTTD Game Console Revision 7 - %s", _openttd_revision); IConsolePrint(CC_WHITE, "------------------------------------"); @@ -475,7 +475,7 @@ static void IConsoleHistoryNavigate(int direction) IConsoleClearCommand(); /* copy history to 'command prompt / bash' */ assert(_iconsole_history[i] != NULL && IsInsideMM(i, 0, ICON_HISTORY_SIZE)); - ttd_strlcpy(_iconsole_cmdline.buf, _iconsole_history[i], _iconsole_cmdline.maxsize); + ttd_strlcpy(_iconsole_cmdline.buf, _iconsole_history[i], _iconsole_cmdline.max_bytes); UpdateTextBufferSize(&_iconsole_cmdline); } diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 261a2dad4..dafae596c 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -565,7 +565,7 @@ public: } if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) { /* Copy clicked name to editbox */ - ttd_strlcpy(this->text.buf, file->title, this->text.maxsize); + ttd_strlcpy(this->text.buf, file->title, this->text.max_bytes); UpdateTextBufferSize(&this->text); this->SetWidgetDirty(SLWW_SAVE_OSK_TITLE); } diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 8721d8e80..5ddb791fc 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -928,15 +928,15 @@ static void DelChar(Textbuf *tb, bool backspace) uint16 len = (uint16)Utf8Decode(&c, s); uint width = GetCharacterWidth(FS_NORMAL, c); - tb->width -= width; + tb->pixels -= width; if (backspace) { tb->caretpos -= len; tb->caretxoffs -= width; } /* Move the remaining characters over the marker */ - memmove(s, s + len, tb->size - (s - tb->buf) - len); - tb->size -= len; + memmove(s, s + len, tb->bytes - (s - tb->buf) - len); + tb->bytes -= len; } /** @@ -951,7 +951,7 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode) if (delmode == WKC_BACKSPACE && tb->caretpos != 0) { DelChar(tb, true); return true; - } else if (delmode == WKC_DELETE && tb->caretpos < tb->size - 1) { + } else if (delmode == WKC_DELETE && tb->caretpos < tb->bytes - 1) { DelChar(tb, false); return true; } @@ -965,9 +965,9 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode) */ void DeleteTextBufferAll(Textbuf *tb) { - memset(tb->buf, 0, tb->maxsize); - tb->size = 1; - tb->width = tb->caretpos = tb->caretxoffs = 0; + memset(tb->buf, 0, tb->max_bytes); + tb->bytes = 1; + tb->pixels = tb->caretpos = tb->caretxoffs = 0; } /** @@ -982,11 +982,11 @@ bool InsertTextBufferChar(Textbuf *tb, WChar key) { const byte charwidth = GetCharacterWidth(FS_NORMAL, key); uint16 len = (uint16)Utf8CharLen(key); - if (tb->size + len <= tb->maxsize && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) { - memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->size - tb->caretpos); + if (tb->bytes + len <= tb->max_bytes && (tb->max_pixels == 0 || tb->pixels + charwidth <= tb->max_pixels)) { + memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->bytes - tb->caretpos); Utf8Encode(tb->buf + tb->caretpos, key); - tb->size += len; - tb->width += charwidth; + tb->bytes += len; + tb->pixels += charwidth; tb->caretpos += len; tb->caretxoffs += charwidth; @@ -1008,32 +1008,32 @@ bool InsertTextBufferClipboard(Textbuf *tb) if (!GetClipboardContents(utf8_buf, lengthof(utf8_buf))) return false; - uint16 width = 0, length = 0; + uint16 pixels = 0, bytes = 0; WChar c; for (const char *ptr = utf8_buf; (c = Utf8Consume(&ptr)) != '\0';) { if (!IsPrintable(c)) break; byte len = Utf8CharLen(c); - if (tb->size + length + len > tb->maxsize) break; + if (tb->bytes + bytes + len > tb->max_bytes) break; - byte charwidth = GetCharacterWidth(FS_NORMAL, c); - if (tb->maxwidth != 0 && width + tb->width + charwidth > tb->maxwidth) break; + byte char_pixels = GetCharacterWidth(FS_NORMAL, c); + if (tb->max_pixels != 0 && pixels + tb->pixels + char_pixels > tb->max_pixels) break; - width += charwidth; - length += len; + pixels += char_pixels; + bytes += len; } - if (length == 0) return false; + if (bytes == 0) return false; - memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->size - tb->caretpos); - memcpy(tb->buf + tb->caretpos, utf8_buf, length); - tb->width += width; - tb->caretxoffs += width; + memmove(tb->buf + tb->caretpos + bytes, tb->buf + tb->caretpos, tb->bytes - tb->caretpos); + memcpy(tb->buf + tb->caretpos, utf8_buf, bytes); + tb->pixels += pixels; + tb->caretxoffs += pixels; - tb->size += length; - tb->caretpos += length; - assert(tb->size <= tb->maxsize); - tb->buf[tb->size - 1] = '\0'; // terminating zero + tb->bytes += bytes; + tb->caretpos += bytes; + assert(tb->bytes <= tb->max_bytes); + tb->buf[tb->bytes - 1] = '\0'; // terminating zero return true; } @@ -1061,7 +1061,7 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode) break; case WKC_RIGHT: - if (tb->caretpos < tb->size - 1) { + if (tb->caretpos < tb->bytes - 1) { WChar c; tb->caretpos += (uint16)Utf8Decode(&c, tb->buf + tb->caretpos); @@ -1077,8 +1077,8 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode) return true; case WKC_END: - tb->caretpos = tb->size - 1; - tb->caretxoffs = tb->width; + tb->caretpos = tb->bytes - 1; + tb->caretxoffs = tb->pixels; return true; default: @@ -1093,19 +1093,19 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode) * and the maximum length of this buffer * @param tb Textbuf type which is getting initialized * @param buf the buffer that will be holding the data for input - * @param maxsize maximum size in bytes, including terminating '\0' - * @param maxwidth maximum length in pixels of this buffer. If reached, buffer + * @param max_bytes maximum size in bytes, including terminating '\0' + * @param max_pixels maximum length in pixels of this buffer. If reached, buffer * cannot grow, even if maxsize would allow because there is space. Width * of zero '0' means the buffer is only restricted by maxsize */ -void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 maxsize, uint16 maxwidth) +void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes, uint16 max_pixels) { - assert(maxsize != 0); + assert(max_bytes != 0); - tb->buf = buf; - tb->maxsize = maxsize; - tb->maxwidth = maxwidth; - tb->caret = true; + tb->buf = buf; + tb->max_bytes = max_bytes; + tb->max_pixels = max_pixels; + tb->caret = true; UpdateTextBufferSize(tb); } @@ -1119,19 +1119,19 @@ void UpdateTextBufferSize(Textbuf *tb) { const char *buf = tb->buf; - tb->width = 0; - tb->size = 1; // terminating zero + tb->pixels = 0; + tb->bytes = 1; // terminating zero WChar c; while ((c = Utf8Consume(&buf)) != '\0') { - tb->width += GetCharacterWidth(FS_NORMAL, c); - tb->size += Utf8CharLen(c); + tb->pixels += GetCharacterWidth(FS_NORMAL, c); + tb->bytes += Utf8CharLen(c); } - assert(tb->size <= tb->maxsize); + assert(tb->bytes <= tb->max_bytes); - tb->caretpos = tb->size - 1; - tb->caretxoffs = tb->width; + tb->caretpos = tb->bytes - 1; + tb->caretxoffs = tb->pixels; } bool HandleCaret(Textbuf *tb) @@ -1233,11 +1233,11 @@ void QueryString::DrawEditBox(Window *w, int wid) /* We will take the current widget length as maximum width, with a small * space reserved at the end for the caret to show */ const Textbuf *tb = &this->text; - int delta = min(0, (right - left) - tb->width - 10); + int delta = min(0, (right - left) - tb->pixels - 10); if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs; - DrawString(delta, tb->width, 0, tb->buf, TC_YELLOW); + DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW); if (HasEditBoxFocus(w, wid) && tb->caret) { int caret_width = GetStringBoundingBox("_").width; DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE); @@ -1280,18 +1280,18 @@ struct QueryStringWindow : public QueryStringBaseWindow { QueryStringFlags flags; ///< Flags controlling behaviour of the window. - QueryStringWindow(StringID str, StringID caption, uint maxsize, uint maxwidth, const WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) : - QueryStringBaseWindow(maxsize) + QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_pixels, const WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) : + QueryStringBaseWindow(max_bytes) { - GetString(this->edit_str_buf, str, &this->edit_str_buf[maxsize - 1]); - str_validate(this->edit_str_buf, &this->edit_str_buf[maxsize - 1], false, true); + GetString(this->edit_str_buf, str, &this->edit_str_buf[max_bytes - 1]); + str_validate(this->edit_str_buf, &this->edit_str_buf[max_bytes - 1], false, true); if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->orig = strdup(this->edit_str_buf); this->caption = caption; this->afilter = afilter; this->flags = flags; - InitializeTextBuffer(&this->text, this->edit_str_buf, maxsize, maxwidth); + InitializeTextBuffer(&this->text, this->edit_str_buf, max_bytes, max_pixels); this->InitNested(desc); diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 345c42a54..1d929bbb6 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -399,11 +399,11 @@ struct NetworkChatWindow : public QueryStringBaseWindow { /* If we are completing at the begin of the line, skip the ': ' we added */ if (tb_buf == pre_buf) { offset = 0; - length = (tb->size - 1) - 2; + length = (tb->max_bytes - 1) - 2; } else { /* Else, find the place we are completing at */ offset = strlen(pre_buf) + 1; - length = (tb->size - 1) - offset; + length = (tb->max_bytes - 1) - offset; } /* Compare if we have a match */ diff --git a/src/querystring_gui.h b/src/querystring_gui.h index 62810a044..ba0776318 100644 --- a/src/querystring_gui.h +++ b/src/querystring_gui.h @@ -60,8 +60,8 @@ public: }; struct QueryStringBaseWindow : public Window, public QueryString { - char *edit_str_buf; - const uint16 edit_str_size; ///< maximum length of string (in bytes), including terminating '\0' + char *edit_str_buf; ///< Buffer for string. + const uint16 edit_str_size; ///< Maximum length of string (in bytes), including terminating '\0'. QueryStringBaseWindow(uint16 size) : Window(), edit_str_size(size) { diff --git a/src/textbuf_gui.h b/src/textbuf_gui.h index a22498612..618eb4d4c 100644 --- a/src/textbuf_gui.h +++ b/src/textbuf_gui.h @@ -17,10 +17,13 @@ #include "strings_type.h" #include "core/enum_type.hpp" +/** Helper/buffer for input fields. */ struct Textbuf { char *buf; ///< buffer in which text is saved - uint16 maxsize, maxwidth; ///< the maximum size of the buffer. Maxwidth specifies screensize in pixels, maxsize is in bytes (including terminating '\0') - uint16 size, width; ///< the current size of the string. Width specifies screensize in pixels, size is in bytes + uint16 max_bytes; ///< the maximum size of the buffer in bytes (including terminating '\0') + uint16 max_pixels; ///< the maximum size of the buffer in pixels + uint16 bytes; ///< the current size of the string in bytes (including terminating '\0') + uint16 pixels; ///< the current size of the string in pixels bool caret; ///< is the caret ("_") visible or not uint16 caretpos; ///< the current position of the caret in the buffer, in bytes uint16 caretxoffs; ///< the current position of the caret in pixels @@ -33,7 +36,7 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode); bool InsertTextBufferChar(Textbuf *tb, uint32 key); bool InsertTextBufferClipboard(Textbuf *tb); bool MoveTextBufferPos(Textbuf *tb, int navmode); -void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 maxsize, uint16 maxwidth); +void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes, uint16 max_pixels); void UpdateTextBufferSize(Textbuf *tb); /** Flags used in ShowQueryString() call */ @@ -48,7 +51,7 @@ DECLARE_ENUM_AS_BIT_SET(QueryStringFlags) typedef void QueryCallbackProc(Window*, bool); -void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth, Window *parent, CharSetFilter afilter, QueryStringFlags flags); +void ShowQueryString(StringID str, StringID caption, uint max_len, uint max_pixels, Window *parent, CharSetFilter afilter, QueryStringFlags flags); void ShowQuery(StringID caption, StringID message, Window *w, QueryCallbackProc *callback); /** The number of 'characters' on the on-screen keyboard. */ |