# textfile_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "fileio_func.h"
    00014 #include "fontcache.h"
    00015 #include "gfx_type.h"
    00016 #include "gfx_func.h"
    00017 #include "string_func.h"
    00018 #include "textfile_gui.h"
    00019
    00020 #include "widgets/misc_widget.h"
    00021
    00022 #include "table/strings.h"
    00023
    00024
    00026 static const NWidgetPart _nested_textfile_widgets[] = {
    00027   NWidget(NWID_HORIZONTAL),
    00028     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00029     NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_TF_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00030   EndContainer(),
    00031   NWidget(NWID_HORIZONTAL),
    00032     NWidget(WWT_PANEL, COLOUR_MAUVE, WID_TF_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_TF_VSCROLLBAR),
    00033     EndContainer(),
    00034     NWidget(NWID_VERTICAL),
    00035       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_TF_VSCROLLBAR),
    00036     EndContainer(),
    00037   EndContainer(),
    00038   NWidget(NWID_HORIZONTAL),
    00039     NWidget(NWID_HSCROLLBAR, COLOUR_MAUVE, WID_TF_HSCROLLBAR),
    00040     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00041   EndContainer(),
    00042 };
    00043
    00045 static const WindowDesc _textfile_desc(
    00046   WDP_CENTER, 630, 460,
    00047   WC_TEXTFILE, WC_NONE,
    00048   WDF_UNCLICK_BUTTONS,
    00049   _nested_textfile_widgets, lengthof(_nested_textfile_widgets)
    00050 );
    00051
    00052 TextfileWindow::TextfileWindow(TextfileType file_type) : Window(), file_type(file_type)
    00053 {
    00054   this->CreateNestedTree(&_textfile_desc);
    00055   this->vscroll = this->GetScrollbar(WID_TF_VSCROLLBAR);
    00056   this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR);
    00057   this->FinishInitNested(&_textfile_desc);
    00058 }
    00059
    00060 /* virtual */ TextfileWindow::~TextfileWindow()
    00061 {
    00062   free(this->text);
    00063 }
    00064
    00065 /* virtual */ void TextfileWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00066 {
    00067   switch (widget) {
    00068     case WID_TF_BACKGROUND:
    00069       this->line_height = FONT_HEIGHT_MONO + 2;
    00070       resize->height = this->line_height;
    00071
    00072       size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible.
    00073       size->width = max(200u, size->width); // At least 200 pixels wide.
    00074       break;
    00075   }
    00076 }
    00077
    00078 /* virtual */ void TextfileWindow::DrawWidget(const Rect &r, int widget) const
    00079 {
    00080   if (widget != WID_TF_BACKGROUND) return;
    00081
    00082   int width = r.right - r.left + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
    00083   int height = r.bottom - r.top + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
    00084
    00085   DrawPixelInfo new_dpi;
    00086   if (!FillDrawPixelInfo(&new_dpi, r.left + WD_BEVEL_LEFT, r.top, width, height)) return;
    00087   DrawPixelInfo *old_dpi = _cur_dpi;
    00088   _cur_dpi = &new_dpi;
    00089
    00090   int left, right;
    00091   if (_current_text_dir == TD_RTL) {
    00092     left = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - this->hscroll->GetCount();
    00093     right = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - 1 + this->hscroll->GetPosition();
    00094   } else {
    00095     left = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT - this->hscroll->GetPosition();
    00096     right = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT + this->hscroll->GetCount() - 1;
    00097   }
    00098   int top = TOP_SPACING;
    00099   for (uint i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->lines.Length(); i++) {
    00100     DrawString(left, right, top + i * this->line_height, this->lines[i + this->vscroll->GetPosition()], TC_WHITE, SA_LEFT, false, FS_MONO);
    00101   }
    00102
    00103   _cur_dpi = old_dpi;
    00104 }
    00105
    00106 /* virtual */ void TextfileWindow::OnResize()
    00107 {
    00108   this->vscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND, TOP_SPACING + BOTTOM_SPACING);
    00109   this->hscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND);
    00110 }
    00111
    00112 /* virtual */ void TextfileWindow::Reset()
    00113 {
    00114   this->search_iterator = 0;
    00115 }
    00116
    00117 /* virtual */ FontSize TextfileWindow::DefaultSize()
    00118 {
    00119   return FS_MONO;
    00120 }
    00121
    00122 /* virtual */ const char *TextfileWindow::NextString()
    00123 {
    00124   if (this->search_iterator >= this->lines.Length()) return NULL;
    00125
    00126   return this->lines[this->search_iterator++];
    00127 }
    00128
    00129 /* virtual */ bool TextfileWindow::Monospace()
    00130 {
    00131   return true;
    00132 }
    00133
    00134 /* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name)
    00135 {
    00136 #ifdef WITH_FREETYPE
    00137   strecpy(settings->mono_font, font_name, lastof(settings->mono_font));
    00138 #endif /* WITH_FREETYPE */
    00139 }
    00140
    00144 /* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir)
    00145 {
    00146   if (textfile == NULL) return;
    00147
    00148   this->lines.Clear();
    00149
    00150   /* Get text from file */
    00151   size_t filesize;
    00152   FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
    00153   if (handle == NULL) return;
    00154
    00155   this->text = ReallocT(this->text, filesize + 1);
    00156   size_t read = fread(this->text, 1, filesize, handle);
    00157   fclose(handle);
    00158
    00159   if (read != filesize) return;
    00160
    00161   this->text[filesize] = '\0';
    00162
    00163   /* Replace tabs and line feeds with a space since str_validate removes those. */
    00164   for (char *p = this->text; *p != '\0'; p++) {
    00165     if (*p == '\t' || *p == '\r') *p = ' ';
    00166   }
    00167
    00168   /* Check for the byte-order-mark, and skip it if needed. */
    00169   char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
    00170
    00171   /* Make sure the string is a valid UTF-8 sequence. */
    00172   str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
    00173
    00174   /* Split the string on newlines. */
    00175   *this->lines.Append() = p;
    00176   for (; *p != '\0'; p++) {
    00177     if (*p == '\n') {
    00178       *p = '\0';
    00179       *this->lines.Append() = p + 1;
    00180     }
    00181   }
    00182
    00183   CheckForMissingGlyphs(true, this);
    00184
    00185   /* Initialize scrollbars */
    00186   this->vscroll->SetCount(this->lines.Length());
    00187
    00188   this->max_length = 0;
    00189   for (uint i = 0; i < this->lines.Length(); i++) {
    00190     this->max_length = max(this->max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width);
    00191   }
    00192   this->hscroll->SetCount(this->max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    00193   this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
    00194 }
    00195
    00203 const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
    00204 {
    00205   static const char * const prefixes[] = {
    00206     "readme",
    00207     "changelog",
    00208     "license",
    00209   };
    00210   assert_compile(lengthof(prefixes) == TFT_END);
    00211
    00212   const char *prefix = prefixes[type];
    00213
    00214   if (filename == NULL) return NULL;
    00215
    00216   static char file_path[MAX_PATH];
    00217   strecpy(file_path, filename, lastof(file_path));
    00218
    00219   char *slash = strrchr(file_path, PATHSEPCHAR);
    00220   if (slash == NULL) return NULL;
    00221
    00222   seprintf(slash + 1, lastof(file_path), "%s_%s.txt", prefix, GetCurrentLanguageIsoCode());
    00223   if (FioCheckFileExists(file_path, dir)) return file_path;
    00224
    00225   seprintf(slash + 1, lastof(file_path), "%s_%.2s.txt", prefix, GetCurrentLanguageIsoCode());
    00226   if (FioCheckFileExists(file_path, dir)) return file_path;
    00227
    00228   seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
    00229   return FioCheckFileExists(file_path, dir) ? file_path : NULL;
    00230 }