LordAro

OpenTTD View Readme 2

May 10th, 2011
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 10.97 KB | None | 0 0
  1. diff -r c42a62268eee src/fileio.cpp
  2. --- a/src/fileio.cpp    Fri Apr 29 18:35:36 2011 +0100
  3. +++ b/src/fileio.cpp    Tue May 10 20:02:29 2011 +0100
  4. @@ -469,6 +469,53 @@
  5.     return true;
  6.  }
  7.  
  8. +/**
  9. + * Check a grf for a readme.txt file.
  10. + * @param cfg The GRFConfig to check.
  11. + * @return true is the GRF is packaged with a readme.txt file.
  12. + * @note a GRF can only have a readme if it is inside a tar archive.
  13. + */
  14. +bool GrfHasReadme(const GRFConfig *cfg)
  15. +{
  16. +   if (!FindReadme(cfg)) return false;
  17. +   return true;
  18. +}
  19. +
  20. +/**
  21. + * Find a readme.txt file.
  22. + * @param cfg The GRFConfig to check.
  23. + * @return The path to the readme, false if no readme found.
  24. + */
  25. +char *FindReadme(const GRFConfig *cfg)
  26. +{
  27. +   if (cfg == NULL || cfg->filename == NULL) return false;
  28. +
  29. +   const char *slash = strrchr(cfg->filename, PATHSEPCHAR);
  30. +   if (slash == NULL) return false;
  31. +   int prefix_length = slash - cfg->filename + 1; // Including the path separator.
  32. +   if (prefix_length + 10 + 1 > MAX_PATH) return false; // "foo/" + "readme.txt" + '\0' must fit.
  33. +
  34. +   char readme_path[MAX_PATH];
  35. +   memcpy(readme_path, cfg->filename, prefix_length);
  36. +   strecpy(readme_path + prefix_length, "readme.txt", lastof(readme_path));
  37. +
  38. +   TarFileList::iterator tar_entry = _tar_filelist.find(readme_path);
  39. +   if (tar_entry == _tar_filelist.end()) return false;
  40. +   return readme_path;
  41. +}
  42. +
  43. +/**
  44. + * Load a readme.
  45. + * @param cfg The GRFConfig to load the readme from.
  46. + * @return the readme in char form.
  47. + */
  48. +char *LoadReadme(const GRFConfig *cfg)
  49. +{
  50. +   char readme[MAX_README_LENGTH];
  51. +   char readme_path = FindReadme(cfg);
  52. +   return readme;
  53. +}
  54. +
  55.  /**
  56.   * Allocates and files a variable with the full path
  57.   * based on the given directory.
  58. diff -r c42a62268eee src/fileio_func.h
  59. --- a/src/fileio_func.h Fri Apr 29 18:35:36 2011 +0100
  60. +++ b/src/fileio_func.h Tue May 10 20:02:29 2011 +0100
  61. @@ -13,6 +13,7 @@
  62.  #define FILEIO_FUNC_H
  63.  
  64.  #include "fileio_type.h"
  65. +#include "newgrf_config.h"
  66.  
  67.  void FioSeekTo(size_t pos, int mode);
  68.  void FioSeekToFile(uint8 slot, size_t pos);
  69. @@ -57,6 +58,9 @@
  70.  
  71.  void SanitizeFilename(char *filename);
  72.  bool AppendPathSeparator(char *buf, size_t buflen);
  73. +bool GrfHasReadme(const GRFConfig *cfg);
  74. +char *FindReadme(const GRFConfig *cfg);
  75. +char *LoadReadme(const GRFConfig *cfg);
  76.  void DeterminePaths(const char *exe);
  77.  void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
  78.  bool FileExists(const char *filename);
  79. @@ -66,6 +70,8 @@
  80.  
  81.  extern char *_personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
  82.  
  83. +static const uint16 MAX_README_LENGTH = 8191;
  84. +
  85.  /** Helper for scanning for files with a given name */
  86.  class FileScanner
  87.  {
  88. diff -r c42a62268eee src/lang/english.txt
  89. --- a/src/lang/english.txt  Fri Apr 29 18:35:36 2011 +0100
  90. +++ b/src/lang/english.txt  Tue May 10 20:02:29 2011 +0100
  91. @@ -2384,6 +2384,7 @@
  92.  STR_NEWGRF_SETTINGS_MOVEDOWN                                    :{BLACK}Move Down
  93.  STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP                            :{BLACK}Move the selected NewGRF file down the list
  94.  STR_NEWGRF_SETTINGS_FILE_TOOLTIP                                :{BLACK}A list of the NewGRF files that are installed. Click a file to change its parameters
  95. +STR_NEWGRF_SETTINGS_VIEW_README                                 :{BLACK}View Readme
  96.  
  97.  STR_NEWGRF_SETTINGS_SET_PARAMETERS                              :{BLACK}Set parameters
  98.  STR_NEWGRF_SETTINGS_TOGGLE_PALETTE                              :{BLACK}Toggle palette
  99. @@ -2417,6 +2418,9 @@
  100.  STR_NEWGRF_PARAMETERS_SETTING                                   :{STRING1}: {ORANGE}{STRING1}
  101.  STR_NEWGRF_PARAMETERS_NUM_PARAM                                 :{LTBLUE}Number of parameters: {ORANGE}{NUM}
  102.  
  103. +# NewGRF readme window
  104. +STR_NEWGRF_README_CAPTION                                       :{WHITE}View NewGRF Readme
  105. +
  106.  # NewGRF inspect window
  107.  STR_NEWGRF_INSPECT_CAPTION                                      :{WHITE}Inspect - {STRING5}
  108.  STR_NEWGRF_INSPECT_PARENT_BUTTON                                :{BLACK}Parent
  109. diff -r c42a62268eee src/newgrf_gui.cpp
  110. --- a/src/newgrf_gui.cpp    Fri Apr 29 18:35:36 2011 +0100
  111. +++ b/src/newgrf_gui.cpp    Tue May 10 20:02:29 2011 +0100
  112. @@ -25,6 +25,7 @@
  113.  #include "core/geometry_func.hpp"
  114.  #include "newgrf_text.h"
  115.  #include "fileio_func.h"
  116. +#include "tar_type.h"
  117.  
  118.  #include "table/strings.h"
  119.  #include "table/sprites.h"
  120. @@ -455,6 +456,110 @@
  121.     new NewGRFParametersWindow(&_newgrf_parameters_desc, c);
  122.  }
  123.  
  124. +enum ShowNewGRFReadmeWidgets {
  125. +   GRW_WIDGET_BACKGROUND,       ///< Panel to draw the readme on
  126. +   GRW_WIDGET_SCROLLBAR,        ///< Scrollbar to scroll through the readme
  127. +};
  128. +
  129. +struct NewGRFReadmeWindow : public Window {
  130. +   GRFConfig *grf_config; ///< View the readme of this GRFConfig.
  131. +   uint clicked_button;   ///< The row in which a button was clicked or UINT_MAX.
  132. +   bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button.
  133. +   int timeout;           ///< How long before we unpress the last-pressed button?
  134. +   int line_height;       ///< Height of a row in the matrix widget.
  135. +   Scrollbar *vscroll;
  136. +  
  137. +  
  138. +   static const int top_spacing;    ///< Additional spacing at the top of the #MHW_BACKGROUND widget.
  139. +   static const int bottom_spacing; ///< Additional spacing at the bottom of the #MHW_BACKGROUND widget.
  140. +
  141. +   int date_width;  /// < Width needed for the date part.
  142. +
  143. +   NewGRFReadmeWindow(const WindowDesc *desc, GRFConfig *c) : Window(),
  144. +       grf_config(c),
  145. +       clicked_button(UINT_MAX),
  146. +       timeout(0)
  147. +   {
  148. +       this->CreateNestedTree(desc);
  149. +       this->vscroll = this->GetScrollbar(GRFPAR_WIDGET_SCROLLBAR);
  150. +       this->FinishInitNested(desc);  // Initializes 'this->line_height' as side effect.
  151. +       this->OnInvalidateData();
  152. +   }
  153. +
  154. +   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
  155. +   {
  156. +       switch (widget) {
  157. +           case GRW_WIDGET_BACKGROUND:
  158. +               this->line_height = FONT_HEIGHT_NORMAL + 2;
  159. +               resize->height = this->line_height;
  160. +
  161. +               size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
  162. +               size->width = max(200u, size->width); // At least 200 pixels wide.
  163. +               break;
  164. +       }
  165. +   }
  166. +
  167. +   virtual void OnPaint()
  168. +   {
  169. +       this->OnInvalidateData(0);
  170. +       this->DrawWidgets();
  171. +   }
  172. +
  173. +   virtual void DrawWidget(const Rect &r, int widget) const
  174. +   {
  175. +       if (widget != GRW_WIDGET_BACKGROUND) return;
  176. +   }
  177. +
  178. +   virtual void OnResize()
  179. +   {
  180. +   }
  181. +
  182. +   /**
  183. +    * Some data on this window has become invalid.
  184. +    * @param data Information about the changed data.
  185. +    * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
  186. +    */
  187. +   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
  188. +   {
  189. +       if (!gui_scope) return;
  190. +       DeleteChildWindows(WC_NEWGRF_README);
  191. +   }
  192. +};
  193. +
  194. +const int NewGRFReadmeWindow::top_spacing = WD_FRAMERECT_TOP + 4;
  195. +const int NewGRFReadmeWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
  196. +
  197. +static const NWidgetPart _nested_newgrf_readme_widgets[] = {
  198. +   NWidget(NWID_HORIZONTAL),
  199. +       NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
  200. +       NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_README_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
  201. +       NWidget(WWT_SHADEBOX, COLOUR_MAUVE),
  202. +       NWidget(WWT_STICKYBOX, COLOUR_MAUVE),
  203. +   EndContainer(),
  204. +   NWidget(NWID_HORIZONTAL),
  205. +       NWidget(WWT_PANEL, COLOUR_MAUVE, GRW_WIDGET_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(GRW_WIDGET_SCROLLBAR),
  206. +       EndContainer(),
  207. +       NWidget(NWID_VERTICAL),
  208. +           NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, GRW_WIDGET_SCROLLBAR),
  209. +           NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
  210. +       EndContainer(),
  211. +   EndContainer(),
  212. +};
  213. +
  214. +/* Window definition for the grf readme window */
  215. +static const WindowDesc _newgrf_readme_desc(
  216. +   WDP_CENTER, 630, 460,
  217. +   WC_NEWGRF_README, WC_NONE,
  218. +   WDF_UNCLICK_BUTTONS,
  219. +   _nested_newgrf_readme_widgets, lengthof(_nested_newgrf_readme_widgets)
  220. +);
  221. +
  222. +void OpenNewGRFReadmeWindow(GRFConfig *c)
  223. +{
  224. +   DeleteWindowByClass(WC_NEWGRF_README);
  225. +   new NewGRFReadmeWindow(&_newgrf_readme_desc, c);
  226. +}
  227. +
  228.  static GRFPresetList _grf_preset_list;
  229.  
  230.  class DropDownListPresetItem : public DropDownListItem {
  231. @@ -492,6 +597,7 @@
  232.     SNGRFS_SCROLL2BAR,
  233.     SNGRFS_NEWGRF_INFO_TITLE,
  234.     SNGRFS_NEWGRF_INFO,
  235. +   SNGRFS_NEWGRF_README,
  236.     SNGRFS_SET_PARAMETERS,
  237.     SNGRFS_TOGGLE_PALETTE,
  238.     SNGRFS_APPLY_CHANGES,
  239. @@ -570,6 +676,7 @@
  240.     ~NewGRFWindow()
  241.     {
  242.         DeleteWindowByClass(WC_NEWGRF_PARAMETERS);
  243. +       DeleteWindowByClass(WC_NEWGRF_README);
  244.  
  245.         if (this->editable && !this->execute) {
  246.             CopyGRFConfigList(this->orig_list, this->actives, true);
  247. @@ -936,6 +1043,18 @@
  248.                 }
  249.                 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
  250.                 break;
  251. +          
  252. +           case SNGRFS_NEWGRF_README: { // View GRF readme
  253. +               if (this->active_sel == NULL && this->avail_sel == NULL) break;
  254. +              
  255. +               if (this->active_sel) {
  256. +                   OpenNewGRFReadmeWindow(this->active_sel);
  257. +                   break;
  258. +               }
  259. +               GRFConfig *c = new GRFConfig(*this->avail_sel);
  260. +               OpenNewGRFReadmeWindow(c);
  261. +               break;
  262. +           }
  263.  
  264.             case SNGRFS_SET_PARAMETERS: { // Edit parameters
  265.                 if (this->active_sel == NULL || !this->editable || !this->show_params) break;
  266. @@ -986,6 +1105,7 @@
  267.                 this->avails.ForceRebuild();
  268.                 this->InvalidateData(1);
  269.                 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
  270. +               this->DeleteChildWindows(WC_NEWGRF_README); // Remove the view readme window
  271.                 InvalidateWindowClassesData(WC_SAVELOAD);
  272.                 break;
  273.         }
  274. @@ -1102,6 +1222,13 @@
  275.             SNGRFS_MOVE_DOWN,
  276.             WIDGET_LIST_END
  277.         );
  278. +
  279. +       bool disable_readme = false;
  280. +       if (this->avail_sel == NULL && this->active_sel == NULL) disable_readme = true;
  281. +       const GRFConfig *c = (this->avail_sel == NULL ? this->active_sel : this->avail_sel);
  282. +       if (!GrfHasReadme(c)) disable_readme = true;
  283. +       this->SetWidgetDisabledState(SNGRFS_NEWGRF_README, disable_readme);
  284. +
  285.         this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all);
  286.         this->SetWidgetDisabledState(SNGRFS_TOGGLE_PALETTE, disable_all);
  287.  
  288. @@ -1586,6 +1713,8 @@
  289.     NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
  290.         NWidget(WWT_EMPTY, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
  291.         NWidget(WWT_EMPTY, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
  292. +       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_NEWGRF_README), SetFill(1, 0), SetResize(1, 0),
  293. +               SetDataTip(STR_NEWGRF_SETTINGS_VIEW_README, STR_NULL), SetPadding(2, 2, 2, 2),
  294.     EndContainer(),
  295.     NWidget(NWID_SELECTION, INVALID_COLOUR, SNGRFS_SHOW_APPLY),
  296.         /* Right side, buttons. */
  297. diff -r c42a62268eee src/window_type.h
  298. --- a/src/window_type.h Fri Apr 29 18:35:36 2011 +0100
  299. +++ b/src/window_type.h Tue May 10 20:02:29 2011 +0100
  300. @@ -111,6 +111,7 @@
  301.     WC_INDUSTRY_CARGOES,
  302.     WC_NEWGRF_PARAMETERS,
  303.     WC_BUILD_OBJECT,
  304. +   WC_NEWGRF_README,
  305.  
  306.     WC_INVALID = 0xFFFF
  307.  };
Advertisement
Add Comment
Please, Sign In to add comment