22 #include "table/strings.h" 24 #if defined(WITH_ZLIB) 28 #if defined(WITH_LIBLZMA) 60 _nested_textfile_widgets,
lengthof(_nested_textfile_widgets)
63 TextfileWindow::TextfileWindow(
TextfileType file_type) :
Window(&_textfile_desc), file_type(file_type)
65 this->CreateNestedTree();
68 this->FinishInitNested(file_type);
69 this->GetWidget<NWidgetCore>(
WID_TF_CAPTION)->
SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
71 this->hscroll->SetStepSize(10);
75 TextfileWindow::~TextfileWindow()
89 for (uint i = 0; i < this->
lines.size(); i++) {
103 size->width =
max(200u, size->width);
116 for (uint i = 0; i < this->
lines.size(); i++) {
155 for (uint i = 0; i < this->
lines.size(); i++) {
160 y_offset += line_height;
199 #if defined(WITH_FREETYPE) || defined(_WIN32) 206 #if defined(WITH_ZLIB) 222 static void Gunzip(byte **bufp,
size_t *sizep)
224 static const int BLOCKSIZE = 8192;
226 size_t alloc_size = 0;
230 memset(&z, 0,
sizeof(z));
232 z.avail_in = (uInt)*sizep;
235 res = inflateInit2(&z, 15 + 32);
237 while (res == Z_OK || (res == Z_BUF_ERROR && z.avail_out == 0)) {
240 alloc_size += BLOCKSIZE;
241 z.avail_out += BLOCKSIZE;
243 z.next_out = buf + alloc_size - z.avail_out;
244 res = inflate(&z, Z_FINISH);
250 if (res == Z_STREAM_END) {
252 *sizep = alloc_size - z.avail_out;
262 #if defined(WITH_LIBLZMA) 278 static void Xunzip(byte **bufp,
size_t *sizep)
280 static const int BLOCKSIZE = 8192;
282 size_t alloc_size = 0;
283 lzma_stream z = LZMA_STREAM_INIT;
289 res = lzma_auto_decoder(&z, UINT64_MAX, LZMA_CONCATENATED);
291 while (res == LZMA_OK || (res == LZMA_BUF_ERROR && z.avail_out == 0)) {
294 alloc_size += BLOCKSIZE;
295 z.avail_out += BLOCKSIZE;
297 z.next_out = buf + alloc_size - z.avail_out;
298 res = lzma_code(&z, LZMA_FINISH);
304 if (res == LZMA_STREAM_END) {
306 *sizep = alloc_size - z.avail_out;
322 if (textfile ==
nullptr)
return;
328 FILE *handle =
FioFOpenFile(textfile,
"rb", dir, &filesize);
329 if (handle ==
nullptr)
return;
332 size_t read = fread(this->
text, 1, filesize, handle);
335 if (read != filesize)
return;
337 #if defined(WITH_ZLIB) || defined(WITH_LIBLZMA) 338 const char *suffix = strrchr(textfile,
'.');
339 if (suffix ==
nullptr)
return;
342 #if defined(WITH_ZLIB) 344 if (strcmp(suffix,
".gz") == 0)
Gunzip((byte**)&this->
text, &filesize);
347 #if defined(WITH_LIBLZMA) 349 if (strcmp(suffix,
".xz") == 0)
Xunzip((byte**)&this->text, &filesize);
352 if (!this->text)
return;
355 this->text =
ReallocT(this->text, filesize + 1);
356 this->text[filesize] =
'\0';
359 for (
char *p = this->text; *p !=
'\0'; p++) {
360 if (*p ==
'\t' || *p ==
'\r') *p =
' ';
364 char *p = this->text + (strncmp(
"\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
370 this->
lines.push_back(p);
371 for (; *p !=
'\0'; p++) {
374 this->
lines.push_back(p + 1);
390 static const char *
const prefixes[] = {
395 assert_compile(
lengthof(prefixes) == TFT_END);
397 const char *prefix = prefixes[type];
399 if (filename ==
nullptr)
return nullptr;
401 static char file_path[MAX_PATH];
404 char *slash = strrchr(file_path, PATHSEPCHAR);
405 if (slash ==
nullptr)
return nullptr;
407 static const char *
const exts[] = {
409 #if defined(WITH_ZLIB) 412 #if defined(WITH_LIBLZMA) 417 for (
size_t i = 0; i <
lengthof(exts); i++) {
bool Monospace() override
Whether to search for a monospace font or not.
void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Data about how and where to blit pixels.
ResizeInfo resize
Resize information.
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
static void Xunzip(byte **bufp, size_t *sizep)
Do an in-memory xunzip operation.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
High level window description.
char * text
Lines of text from the NewGRF's textfile.
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.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
textfile; Window numbers:
#define FONT_HEIGHT_MONO
Height of characters in the large (FS_MONO) font.
Index of the monospaced font in the font tables.
static void Gunzip(byte **bufp, size_t *sizep)
Do an in-memory gunzip operation.
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
fluid_settings_t * settings
FluidSynth settings handle.
void ToggleWidgetLoweredState(byte widget_index)
Invert the lowered/raised status of a widget.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
int GetStringHeight(const char *str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
static T max(const T a, const T b)
Returns the maximum of two values.
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Settings for the freetype fonts.
Data structure for an opened window.
Functions to read fonts from files and cache them.
Bottom offset of the text of the frame.
uint GetContentHeight()
Get the total height of the content displayed in this window, if wrapping is disabled.
Functions related to low-level strings.
FontSize DefaultSize() override
Get the default (font) size of the string.
uint search_iterator
Iterator for the font check search.
bool IsWidgetLowered(byte widget_index) const
Gets the lowered state of a widget.
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
const char * NextString() override
Get the next string to search through.
void SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) override
Set the right font names.
Functions related to the gfx engine.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition of base types and functions in a cross-platform compatible way.
static const int BOTTOM_SPACING
Additional spacing at the bottom of the WID_TF_BACKGROUND widget.
A number of safeguards to prevent using unsafe methods.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type...
virtual void LoadTextfile(const char *textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
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.
Right offset of the text of the frame.
#define lengthof(x)
Return the length of an fixed size array.
TextfileType
Additional text files accompanying Tar archives.
Top offset of the text of the frame.
Left offset of the text of the frame.
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
static const int TOP_SPACING
Additional spacing at the top of the WID_TF_BACKGROUND widget.
Scrollbar * hscroll
Horizontal scrollbar.
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.
void SetupScrollbars()
Set scrollbars to the right lengths.
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
No window, redirects to WC_MAIN_WINDOW.
char font[MAX_PATH]
The name of the font, or path to the font.
const char * GetCurrentLanguageIsoCode()
Get the ISO language code of the currently loaded language.
static const NWidgetPart _nested_textfile_widgets[]
Widgets for the textfile window.
const void * os_handle
Optional native OS font info.
Replace the unknown/bad bits with question marks.
void Reset() override
Reset the search, i.e.
FontSize
Available font sizes.
Scrollbar * vscroll
Vertical scrollbar.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Coordinates of a point in 2D.
std::vector< const char * > lines
text, split into lines in a table with lines.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Offset at right to draw the frame rectangular area.
int width
width of the window (number of pixels to the right in x direction)
Specification of a rectangle with absolute coordinates of all edges.
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Dimensions (a width and height) of a rectangle in 2D.
Types related to the graphics and/or input devices.
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
int height
Height of the window (number of pixels down in y direction)
GUI functions related to textfiles.
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.
void OnResize() override
Called after the window got resized.