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   this->GetWidget<NWidgetCore>(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
00059 }
00060 
00061 /* virtual */ TextfileWindow::~TextfileWindow()
00062 {
00063   free(this->text);
00064 }
00065 
00066 /* virtual */ void TextfileWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00067 {
00068   switch (widget) {
00069     case WID_TF_BACKGROUND:
00070       this->line_height = FONT_HEIGHT_MONO + 2;
00071       resize->height = this->line_height;
00072 
00073       size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible.
00074       size->width = max(200u, size->width); // At least 200 pixels wide.
00075       break;
00076   }
00077 }
00078 
00079 /* virtual */ void TextfileWindow::DrawWidget(const Rect &r, int widget) const
00080 {
00081   if (widget != WID_TF_BACKGROUND) return;
00082 
00083   int width = r.right - r.left + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
00084   int height = r.bottom - r.top + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
00085 
00086   DrawPixelInfo new_dpi;
00087   if (!FillDrawPixelInfo(&new_dpi, r.left + WD_BEVEL_LEFT, r.top, width, height)) return;
00088   DrawPixelInfo *old_dpi = _cur_dpi;
00089   _cur_dpi = &new_dpi;
00090 
00091   int left, right;
00092   if (_current_text_dir == TD_RTL) {
00093     left = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - this->hscroll->GetCount();
00094     right = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - 1 + this->hscroll->GetPosition();
00095   } else {
00096     left = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT - this->hscroll->GetPosition();
00097     right = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT + this->hscroll->GetCount() - 1;
00098   }
00099   int top = TOP_SPACING;
00100   for (uint i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->lines.Length(); i++) {
00101     DrawString(left, right, top + i * this->line_height, this->lines[i + this->vscroll->GetPosition()], TC_WHITE, SA_LEFT, false, FS_MONO);
00102   }
00103 
00104   _cur_dpi = old_dpi;
00105 }
00106 
00107 /* virtual */ void TextfileWindow::OnResize()
00108 {
00109   this->vscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND, TOP_SPACING + BOTTOM_SPACING);
00110   this->hscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND);
00111 }
00112 
00113 /* virtual */ void TextfileWindow::Reset()
00114 {
00115   this->search_iterator = 0;
00116 }
00117 
00118 /* virtual */ FontSize TextfileWindow::DefaultSize()
00119 {
00120   return FS_MONO;
00121 }
00122 
00123 /* virtual */ const char *TextfileWindow::NextString()
00124 {
00125   if (this->search_iterator >= this->lines.Length()) return NULL;
00126 
00127   return this->lines[this->search_iterator++];
00128 }
00129 
00130 /* virtual */ bool TextfileWindow::Monospace()
00131 {
00132   return true;
00133 }
00134 
00135 /* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name)
00136 {
00137 #ifdef WITH_FREETYPE
00138   strecpy(settings->mono_font, font_name, lastof(settings->mono_font));
00139 #endif /* WITH_FREETYPE */
00140 }
00141 
00145 /* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir)
00146 {
00147   if (textfile == NULL) return;
00148 
00149   this->lines.Clear();
00150 
00151   /* Get text from file */
00152   size_t filesize;
00153   FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
00154   if (handle == NULL) return;
00155 
00156   this->text = ReallocT(this->text, filesize + 1);
00157   size_t read = fread(this->text, 1, filesize, handle);
00158   fclose(handle);
00159 
00160   if (read != filesize) return;
00161 
00162   this->text[filesize] = '\0';
00163 
00164   /* Replace tabs and line feeds with a space since str_validate removes those. */
00165   for (char *p = this->text; *p != '\0'; p++) {
00166     if (*p == '\t' || *p == '\r') *p = ' ';
00167   }
00168 
00169   /* Check for the byte-order-mark, and skip it if needed. */
00170   char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
00171 
00172   /* Make sure the string is a valid UTF-8 sequence. */
00173   str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
00174 
00175   /* Split the string on newlines. */
00176   *this->lines.Append() = p;
00177   for (; *p != '\0'; p++) {
00178     if (*p == '\n') {
00179       *p = '\0';
00180       *this->lines.Append() = p + 1;
00181     }
00182   }
00183 
00184   CheckForMissingGlyphs(true, this);
00185 
00186   /* Initialize scrollbars */
00187   this->vscroll->SetCount(this->lines.Length());
00188 
00189   this->max_length = 0;
00190   for (uint i = 0; i < this->lines.Length(); i++) {
00191     this->max_length = max(this->max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width);
00192   }
00193   this->hscroll->SetCount(this->max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
00194   this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
00195 }
00196 
00204 const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
00205 {
00206   static const char * const prefixes[] = {
00207     "readme",
00208     "changelog",
00209     "license",
00210   };
00211   assert_compile(lengthof(prefixes) == TFT_END);
00212 
00213   const char *prefix = prefixes[type];
00214 
00215   if (filename == NULL) return NULL;
00216 
00217   static char file_path[MAX_PATH];
00218   strecpy(file_path, filename, lastof(file_path));
00219 
00220   char *slash = strrchr(file_path, PATHSEPCHAR);
00221   if (slash == NULL) return NULL;
00222 
00223   seprintf(slash + 1, lastof(file_path), "%s_%s.txt", prefix, GetCurrentLanguageIsoCode());
00224   if (FioCheckFileExists(file_path, dir)) return file_path;
00225 
00226   seprintf(slash + 1, lastof(file_path), "%s_%.2s.txt", prefix, GetCurrentLanguageIsoCode());
00227   if (FioCheckFileExists(file_path, dir)) return file_path;
00228 
00229   seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
00230   return FioCheckFileExists(file_path, dir) ? file_path : NULL;
00231 }