OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
20 #include "table/strings.h"
22 #if defined(WITH_ZLIB)
26 #if defined(WITH_LIBLZMA)
63 this->CreateNestedTree();
66 this->FinishInitNested(file_type);
67 this->GetWidget<NWidgetCore>(
WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
69 this->hscroll->SetStepSize(10);
72 TextfileWindow::~TextfileWindow()
85 for (
auto &line : this->
lines) {
92 for (
auto &line : this->
lines) {
102 uint TextfileWindow::GetContentHeight()
104 if (this->
lines.size() == 0)
return 0;
105 return this->
lines.back().bottom;
115 size->width = std::max(200u, size->width);
166 for (
auto &line : this->
lines) {
167 if (line.bottom < pos)
continue;
168 if (line.top > pos + cap)
break;
170 int y_offset = (line.top - pos) * line_height;
191 if (!gui_scope)
return;
220 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
226 #if defined(WITH_ZLIB)
242 static void Gunzip(
byte **bufp,
size_t *sizep)
244 static const int BLOCKSIZE = 8192;
246 size_t alloc_size = 0;
250 memset(&z, 0,
sizeof(z));
252 z.avail_in = (uInt)*sizep;
255 res = inflateInit2(&z, 15 + 32);
257 while (res == Z_OK || (res == Z_BUF_ERROR && z.avail_out == 0)) {
260 alloc_size += BLOCKSIZE;
261 z.avail_out += BLOCKSIZE;
263 z.next_out = buf + alloc_size - z.avail_out;
264 res = inflate(&z, Z_FINISH);
270 if (res == Z_STREAM_END) {
272 *sizep = alloc_size - z.avail_out;
282 #if defined(WITH_LIBLZMA)
298 static void Xunzip(
byte **bufp,
size_t *sizep)
300 static const int BLOCKSIZE = 8192;
302 size_t alloc_size = 0;
303 lzma_stream z = LZMA_STREAM_INIT;
309 res = lzma_auto_decoder(&z, UINT64_MAX, LZMA_CONCATENATED);
311 while (res == LZMA_OK || (res == LZMA_BUF_ERROR && z.avail_out == 0)) {
314 alloc_size += BLOCKSIZE;
315 z.avail_out += BLOCKSIZE;
317 z.next_out = buf + alloc_size - z.avail_out;
318 res = lzma_code(&z, LZMA_FINISH);
324 if (res == LZMA_STREAM_END) {
326 *sizep = alloc_size - z.avail_out;
342 if (textfile ==
nullptr)
return;
348 FILE *handle =
FioFOpenFile(textfile,
"rb", dir, &filesize);
349 if (handle ==
nullptr)
return;
352 size_t read = fread(this->
text, 1, filesize, handle);
355 if (read != filesize)
return;
357 #if defined(WITH_ZLIB) || defined(WITH_LIBLZMA)
358 const char *suffix = strrchr(textfile,
'.');
359 if (suffix ==
nullptr)
return;
362 #if defined(WITH_ZLIB)
364 if (strcmp(suffix,
".gz") == 0)
Gunzip((
byte**)&this->
text, &filesize);
367 #if defined(WITH_LIBLZMA)
369 if (strcmp(suffix,
".xz") == 0)
Xunzip((
byte**)&this->text, &filesize);
372 if (!this->text)
return;
375 this->text =
ReallocT(this->text, filesize + 1);
376 this->text[filesize] =
'\0';
379 for (
char *p = this->text; *p !=
'\0'; p++) {
380 if (*p ==
'\t' || *p ==
'\r') *p =
' ';
384 char *p = this->text + (strncmp(u8
"\ufeff", this->text, 3) == 0 ? 3 : 0);
391 this->
lines.emplace_back(row, p);
392 for (; *p !=
'\0'; p++) {
395 this->
lines.emplace_back(++row, p + 1);
401 for (
auto &line : this->
lines) {
418 static const char *
const prefixes[] = {
423 static_assert(
lengthof(prefixes) == TFT_END);
425 const char *prefix = prefixes[type];
427 if (filename ==
nullptr)
return nullptr;
429 static char file_path[MAX_PATH];
432 char *slash = strrchr(file_path, PATHSEPCHAR);
433 if (slash ==
nullptr)
return nullptr;
435 static const char *
const exts[] = {
437 #if defined(WITH_ZLIB)
440 #if defined(WITH_LIBLZMA)
445 for (
size_t i = 0; i <
lengthof(exts); i++) {
FontSize DefaultSize() override
Get the default (font) size of the string.
virtual void LoadTextfile(const char *textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
@ SVS_ALLOW_NEWLINE
Allow newlines.
const char * GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Dimensions (a width and height) of a rectangle in 2D.
static void Xunzip(byte **bufp, size_t *sizep)
Do an in-memory xunzip operation.
static void Gunzip(byte **bufp, size_t *sizep)
Do an in-memory gunzip operation.
uint ReflowContent()
Get the total height of the content displayed in this window, if wrapping is disabled.
void OnResize() override
Called after the window got resized.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
static const int BOTTOM_SPACING
Additional spacing at the bottom of the WID_TF_BACKGROUND widget.
@ WD_FRAMETEXT_TOP
Top offset of the text of the frame.
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
static const NWidgetPart _nested_textfile_widgets[]
Widgets for the textfile window.
@ WD_FRAMETEXT_LEFT
Left offset of the text of the frame.
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
High level window description.
void SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) override
Set the right font names.
ResizeInfo resize
Resize information.
static const int TOP_SPACING
Additional spacing at the top of the WID_TF_BACKGROUND widget.
@ SA_TOP
Top align the text.
int height
Height of the window (number of pixels down in y direction)
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
@ WD_FRAMERECT_RIGHT
Offset at right to draw the frame rectangular area.
@ WD_FRAMETEXT_BOTTOM
Bottom offset of the text of the frame.
#define FONT_HEIGHT_MONO
Height of characters in the large (FS_MONO) font.
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
void SetupScrollbars(bool force_reflow)
Set scrollbars to the right lengths.
const char * NextString() override
Get the next string to search through.
fluid_settings_t * settings
FluidSynth settings handle.
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Coordinates of a point in 2D.
@ WD_FRAMETEXT_RIGHT
Right offset of the text of the frame.
char * text
Lines of text from the NewGRF's textfile.
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
int GetStringHeight(const char *str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Scrollbar * hscroll
Horizontal scrollbar.
uint max_length
Maximum length of unwrapped text line.
void Reset() override
Reset the search, i.e.
uint search_iterator
Iterator for the font check search.
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
bool IsWidgetLowered(byte widget_index) const
Gets the lowered state of a widget.
bool Monospace() override
Whether to search for a monospace font or not.
std::vector< Line > lines
text, split into lines in a table with lines.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Subdirectory
The different kinds of subdirectories OpenTTD uses.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
@ SA_LEFT
Left align the text.
void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
void ToggleWidgetLoweredState(byte widget_index)
Invert the lowered/raised status of a widget.
#define lengthof(x)
Return the length of an fixed size array.
@ FS_MONO
Index of the monospaced font in the font tables.
FontSize
Available font sizes.
Data structure for an opened window.
TextfileType
Additional text files accompanying Tar archives.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Specification of a rectangle with absolute coordinates of all edges.
@ WC_TEXTFILE
textfile; Window numbers:
#define lastof(x)
Get the last element of an fixed size array.
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
@ WDP_CENTER
Center the window.
static WindowDesc _textfile_desc(WDP_CENTER, "textfile", 630, 460, WC_TEXTFILE, WC_NONE, 0, _nested_textfile_widgets, lengthof(_nested_textfile_widgets))
Window definition for the textfile window.
Settings for the freetype fonts.
Scrollbar * vscroll
Vertical scrollbar.
Data about how and where to blit pixels.