(svn r27950) -Merge: Documentation updates from 1.7 branch
[openttd.git] / src / textfile_gui.cpp
blobb0f9b1125a045aae658d22d65924ed5808bb5cb8
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file textfile_gui.cpp Implementation of textfile window. */
12 #include "stdafx.h"
13 #include "fileio_func.h"
14 #include "fontcache.h"
15 #include "gfx_type.h"
16 #include "gfx_func.h"
17 #include "string_func.h"
18 #include "textfile_gui.h"
20 #include "widgets/misc_widget.h"
22 #include "table/strings.h"
24 #if defined(WITH_ZLIB)
25 #include <zlib.h>
26 #endif
28 #if defined(WITH_LZMA)
29 #include <lzma.h>
30 #endif
32 #include "safeguards.h"
34 /** Widgets for the textfile window. */
35 static const NWidgetPart _nested_textfile_widgets[] = {
36 NWidget(NWID_HORIZONTAL),
37 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
38 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_TF_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
39 NWidget(WWT_TEXTBTN, COLOUR_MAUVE, WID_TF_WRAPTEXT), SetDataTip(STR_TEXTFILE_WRAP_TEXT, STR_TEXTFILE_WRAP_TEXT_TOOLTIP),
40 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
41 EndContainer(),
42 NWidget(NWID_HORIZONTAL),
43 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_TF_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_TF_VSCROLLBAR),
44 EndContainer(),
45 NWidget(NWID_VERTICAL),
46 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_TF_VSCROLLBAR),
47 EndContainer(),
48 EndContainer(),
49 NWidget(NWID_HORIZONTAL),
50 NWidget(NWID_HSCROLLBAR, COLOUR_MAUVE, WID_TF_HSCROLLBAR),
51 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
52 EndContainer(),
55 /** Window definition for the textfile window */
56 static WindowDesc _textfile_desc(
57 WDP_CENTER, "textfile", 630, 460,
58 WC_TEXTFILE, WC_NONE,
60 _nested_textfile_widgets, lengthof(_nested_textfile_widgets)
63 TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc), file_type(file_type)
65 this->CreateNestedTree();
66 this->vscroll = this->GetScrollbar(WID_TF_VSCROLLBAR);
67 this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR);
68 this->FinishInitNested();
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); // Speed up horizontal scrollbar
72 this->vscroll->SetStepSize(FONT_HEIGHT_MONO);
75 /* virtual */ TextfileWindow::~TextfileWindow()
77 free(this->text);
80 /**
81 * Get the total height of the content displayed in this window, if wrapping is disabled.
82 * @return the height in pixels
84 uint TextfileWindow::GetContentHeight()
86 int max_width = this->GetWidget<NWidgetCore>(WID_TF_BACKGROUND)->current_x - WD_FRAMETEXT_LEFT - WD_FRAMERECT_RIGHT;
88 uint height = 0;
89 for (uint i = 0; i < this->lines.Length(); i++) {
90 height += GetStringHeight(this->lines[i], max_width, FS_MONO);
93 return height;
96 /* virtual */ void TextfileWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
98 switch (widget) {
99 case WID_TF_BACKGROUND:
100 resize->height = 1;
102 size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible.
103 size->width = max(200u, size->width); // At least 200 pixels wide.
104 break;
108 /** Set scrollbars to the right lengths. */
109 void TextfileWindow::SetupScrollbars()
111 if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
112 this->vscroll->SetCount(this->GetContentHeight());
113 this->hscroll->SetCount(0);
114 } else {
115 uint max_length = 0;
116 for (uint i = 0; i < this->lines.Length(); i++) {
117 max_length = max(max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width);
119 this->vscroll->SetCount(this->lines.Length() * FONT_HEIGHT_MONO);
120 this->hscroll->SetCount(max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
123 this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, IsWidgetLowered(WID_TF_WRAPTEXT));
126 /* virtual */ void TextfileWindow::OnClick(Point pt, int widget, int click_count)
128 switch (widget) {
129 case WID_TF_WRAPTEXT:
130 this->ToggleWidgetLoweredState(WID_TF_WRAPTEXT);
131 this->SetupScrollbars();
132 this->InvalidateData();
133 break;
137 /* virtual */ void TextfileWindow::DrawWidget(const Rect &r, int widget) const
139 if (widget != WID_TF_BACKGROUND) return;
141 const int x = r.left + WD_FRAMETEXT_LEFT;
142 const int y = r.top + WD_FRAMETEXT_TOP;
143 const int right = r.right - WD_FRAMETEXT_RIGHT;
144 const int bottom = r.bottom - WD_FRAMETEXT_BOTTOM;
146 DrawPixelInfo new_dpi;
147 if (!FillDrawPixelInfo(&new_dpi, x, y, right - x + 1, bottom - y + 1)) return;
148 DrawPixelInfo *old_dpi = _cur_dpi;
149 _cur_dpi = &new_dpi;
151 /* Draw content (now coordinates given to DrawString* are local to the new clipping region). */
152 int line_height = FONT_HEIGHT_MONO;
153 int y_offset = -this->vscroll->GetPosition();
155 for (uint i = 0; i < this->lines.Length(); i++) {
156 if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
157 y_offset = DrawStringMultiLine(0, right - x, y_offset, bottom - y, this->lines[i], TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO);
158 } else {
159 DrawString(-this->hscroll->GetPosition(), right - x, y_offset, this->lines[i], TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO);
160 y_offset += line_height; // margin to previous element
164 _cur_dpi = old_dpi;
167 /* virtual */ void TextfileWindow::OnResize()
169 this->vscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND, TOP_SPACING + BOTTOM_SPACING);
170 this->hscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND);
172 this->SetupScrollbars();
175 /* virtual */ void TextfileWindow::Reset()
177 this->search_iterator = 0;
180 /* virtual */ FontSize TextfileWindow::DefaultSize()
182 return FS_MONO;
185 /* virtual */ const char *TextfileWindow::NextString()
187 if (this->search_iterator >= this->lines.Length()) return NULL;
189 return this->lines[this->search_iterator++];
192 /* virtual */ bool TextfileWindow::Monospace()
194 return true;
197 /* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name)
199 #ifdef WITH_FREETYPE
200 strecpy(settings->mono.font, font_name, lastof(settings->mono.font));
201 #endif /* WITH_FREETYPE */
204 #if defined(WITH_ZLIB)
207 * Do an in-memory gunzip operation. This works on a raw deflate stream,
208 * or a file with gzip or zlib header.
209 * @param bufp A pointer to a buffer containing the input data. This
210 * buffer will be freed and replaced by a buffer containing
211 * the uncompressed data.
212 * @param sizep A pointer to the buffer size. Before the call, the value
213 * pointed to should contain the size of the input buffer.
214 * After the call, it contains the size of the uncompressed
215 * data.
217 * When decompressing fails, *bufp is set to NULL and *sizep to 0. The
218 * compressed buffer passed in is still freed in this case.
220 static void Gunzip(byte **bufp, size_t *sizep)
222 static const int BLOCKSIZE = 8192;
223 byte *buf = NULL;
224 size_t alloc_size = 0;
225 z_stream z;
226 int res;
228 memset(&z, 0, sizeof(z));
229 z.next_in = *bufp;
230 z.avail_in = (uInt)*sizep;
232 /* window size = 15, add 32 to enable gzip or zlib header processing */
233 res = inflateInit2(&z, 15 + 32);
234 /* Z_BUF_ERROR just means we need more space */
235 while (res == Z_OK || (res == Z_BUF_ERROR && z.avail_out == 0)) {
236 /* When we get here, we're either just starting, or
237 * inflate is out of output space - allocate more */
238 alloc_size += BLOCKSIZE;
239 z.avail_out += BLOCKSIZE;
240 buf = ReallocT(buf, alloc_size);
241 z.next_out = buf + alloc_size - z.avail_out;
242 res = inflate(&z, Z_FINISH);
245 free(*bufp);
246 inflateEnd(&z);
248 if (res == Z_STREAM_END) {
249 *bufp = buf;
250 *sizep = alloc_size - z.avail_out;
251 } else {
252 /* Something went wrong */
253 *bufp = NULL;
254 *sizep = 0;
255 free(buf);
258 #endif
260 #if defined(WITH_LZMA)
263 * Do an in-memory xunzip operation. This works on a .xz or (legacy)
264 * .lzma file.
265 * @param bufp A pointer to a buffer containing the input data. This
266 * buffer will be freed and replaced by a buffer containing
267 * the uncompressed data.
268 * @param sizep A pointer to the buffer size. Before the call, the value
269 * pointed to should contain the size of the input buffer.
270 * After the call, it contains the size of the uncompressed
271 * data.
273 * When decompressing fails, *bufp is set to NULL and *sizep to 0. The
274 * compressed buffer passed in is still freed in this case.
276 static void Xunzip(byte **bufp, size_t *sizep)
278 static const int BLOCKSIZE = 8192;
279 byte *buf = NULL;
280 size_t alloc_size = 0;
281 lzma_stream z = LZMA_STREAM_INIT;
282 int res;
284 z.next_in = *bufp;
285 z.avail_in = *sizep;
287 res = lzma_auto_decoder(&z, UINT64_MAX, LZMA_CONCATENATED);
288 /* Z_BUF_ERROR just means we need more space */
289 while (res == LZMA_OK || (res == LZMA_BUF_ERROR && z.avail_out == 0)) {
290 /* When we get here, we're either just starting, or
291 * inflate is out of output space - allocate more */
292 alloc_size += BLOCKSIZE;
293 z.avail_out += BLOCKSIZE;
294 buf = ReallocT(buf, alloc_size);
295 z.next_out = buf + alloc_size - z.avail_out;
296 res = lzma_code(&z, LZMA_FINISH);
299 free(*bufp);
300 lzma_end(&z);
302 if (res == LZMA_STREAM_END) {
303 *bufp = buf;
304 *sizep = alloc_size - z.avail_out;
305 } else {
306 /* Something went wrong */
307 *bufp = NULL;
308 *sizep = 0;
309 free(buf);
312 #endif
316 * Loads the textfile text from file and setup #lines.
318 /* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir)
320 if (textfile == NULL) return;
322 this->lines.Clear();
324 /* Get text from file */
325 size_t filesize;
326 FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
327 if (handle == NULL) return;
329 this->text = ReallocT(this->text, filesize);
330 size_t read = fread(this->text, 1, filesize, handle);
331 fclose(handle);
333 if (read != filesize) return;
335 #if defined(WITH_ZLIB) || defined(WITH_LZMA)
336 const char *suffix = strrchr(textfile, '.');
337 if (suffix == NULL) return;
338 #endif
340 #if defined(WITH_ZLIB)
341 /* In-place gunzip */
342 if (strcmp(suffix, ".gz") == 0) Gunzip((byte**)&this->text, &filesize);
343 #endif
345 #if defined(WITH_LZMA)
346 /* In-place xunzip */
347 if (strcmp(suffix, ".xz") == 0) Xunzip((byte**)&this->text, &filesize);
348 #endif
350 if (!this->text) return;
352 /* Add space for trailing \0 */
353 this->text = ReallocT(this->text, filesize + 1);
354 this->text[filesize] = '\0';
356 /* Replace tabs and line feeds with a space since str_validate removes those. */
357 for (char *p = this->text; *p != '\0'; p++) {
358 if (*p == '\t' || *p == '\r') *p = ' ';
361 /* Check for the byte-order-mark, and skip it if needed. */
362 char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
364 /* Make sure the string is a valid UTF-8 sequence. */
365 str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
367 /* Split the string on newlines. */
368 *this->lines.Append() = p;
369 for (; *p != '\0'; p++) {
370 if (*p == '\n') {
371 *p = '\0';
372 *this->lines.Append() = p + 1;
376 CheckForMissingGlyphs(true, this);
380 * Search a textfile file next to the given content.
381 * @param type The type of the textfile to search for.
382 * @param dir The subdirectory to search in.
383 * @param filename The filename of the content to look for.
384 * @return The path to the textfile, \c NULL otherwise.
386 const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
388 static const char * const prefixes[] = {
389 "readme",
390 "changelog",
391 "license",
393 assert_compile(lengthof(prefixes) == TFT_END);
395 const char *prefix = prefixes[type];
397 if (filename == NULL) return NULL;
399 static char file_path[MAX_PATH];
400 strecpy(file_path, filename, lastof(file_path));
402 char *slash = strrchr(file_path, PATHSEPCHAR);
403 if (slash == NULL) return NULL;
405 static const char * const exts[] = {
406 "txt",
407 #if defined(WITH_ZLIB)
408 "txt.gz",
409 #endif
410 #if defined(WITH_LZMA)
411 "txt.xz",
412 #endif
415 for (size_t i = 0; i < lengthof(exts); i++) {
416 seprintf(slash + 1, lastof(file_path), "%s_%s.%s", prefix, GetCurrentLanguageIsoCode(), exts[i]);
417 if (FioCheckFileExists(file_path, dir)) return file_path;
419 seprintf(slash + 1, lastof(file_path), "%s_%.2s.%s", prefix, GetCurrentLanguageIsoCode(), exts[i]);
420 if (FioCheckFileExists(file_path, dir)) return file_path;
422 seprintf(slash + 1, lastof(file_path), "%s.%s", prefix, exts[i]);
423 if (FioCheckFileExists(file_path, dir)) return file_path;
425 return NULL;