# news_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 "gui.h"
    00014 #include "viewport_func.h"
    00015 #include "strings_func.h"
    00016 #include "window_func.h"
    00017 #include "date_func.h"
    00018 #include "vehicle_base.h"
    00019 #include "vehicle_func.h"
    00020 #include "vehicle_gui.h"
    00021 #include "station_base.h"
    00022 #include "industry.h"
    00023 #include "town.h"
    00024 #include "sound_func.h"
    00025 #include "string_func.h"
    00026 #include "widgets/dropdown_func.h"
    00027 #include "statusbar_gui.h"
    00028 #include "company_manager_face.h"
    00029 #include "company_func.h"
    00030 #include "engine_base.h"
    00031 #include "engine_gui.h"
    00032 #include "core/geometry_func.hpp"
    00033 #include "command_func.h"
    00034 #include "company_base.h"
    00035
    00036 #include "widgets/news_widget.h"
    00037
    00038 #include "table/strings.h"
    00039
    00040 const NewsItem *_statusbar_news_item = NULL;
    00041 bool _news_ticker_sound;
    00042
    00043 static uint MIN_NEWS_AMOUNT = 30;
    00044 static uint _total_news = 0;
    00045 static NewsItem *_oldest_news = NULL;
    00046 static NewsItem *_latest_news = NULL;
    00047
    00054 static const NewsItem *_forced_news = NULL;
    00055
    00057 static const NewsItem *_current_news = NULL;
    00058
    00059
    00066 static TileIndex GetReferenceTile(NewsReferenceType reftype, uint32 ref)
    00067 {
    00068   switch (reftype) {
    00069     case NR_TILE:     return (TileIndex)ref;
    00070     case NR_STATION:  return Station::Get((StationID)ref)->xy;
    00071     case NR_INDUSTRY: return Industry::Get((IndustryID)ref)->location.tile + TileDiffXY(1, 1);
    00072     case NR_TOWN:     return Town::Get((TownID)ref)->xy;
    00073     default:          return INVALID_TILE;
    00074   }
    00075 }
    00076
    00077 /* Normal news items. */
    00078 static const NWidgetPart _nested_normal_news_widgets[] = {
    00079   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
    00080     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
    00081       NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
    00082       NWidget(NWID_SPACER), SetFill(1, 0),
    00083       NWidget(NWID_VERTICAL),
    00084         NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
    00085         NWidget(NWID_SPACER), SetFill(0, 1),
    00086       EndContainer(),
    00087     EndContainer(),
    00088     NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 154), SetPadding(0, 5, 1, 5),
    00089   EndContainer(),
    00090 };
    00091
    00092 static const WindowDesc _normal_news_desc(
    00093   WDP_MANUAL, 0, 0,
    00094   WC_NEWS_WINDOW, WC_NONE,
    00095   0,
    00096   _nested_normal_news_widgets, lengthof(_nested_normal_news_widgets)
    00097 );
    00098
    00099 /* New vehicles news items. */
    00100 static const NWidgetPart _nested_vehicle_news_widgets[] = {
    00101   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
    00102     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
    00103       NWidget(NWID_VERTICAL),
    00104         NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
    00105         NWidget(NWID_SPACER), SetFill(0, 1),
    00106       EndContainer(),
    00107       NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_VEH_TITLE), SetFill(1, 1), SetMinimalSize(419, 55), SetDataTip(STR_EMPTY, STR_NULL),
    00108     EndContainer(),
    00109     NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_VEH_BKGND), SetPadding(0, 25, 1, 25),
    00110       NWidget(NWID_VERTICAL),
    00111         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_NAME), SetMinimalSize(369, 33), SetFill(1, 0),
    00112         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_SPR),  SetMinimalSize(369, 32), SetFill(1, 0),
    00113         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_INFO), SetMinimalSize(369, 46), SetFill(1, 0),
    00114       EndContainer(),
    00115     EndContainer(),
    00116   EndContainer(),
    00117 };
    00118
    00119 static const WindowDesc _vehicle_news_desc(
    00120   WDP_MANUAL, 0, 0,
    00121   WC_NEWS_WINDOW, WC_NONE,
    00122   0,
    00123   _nested_vehicle_news_widgets, lengthof(_nested_vehicle_news_widgets)
    00124 );
    00125
    00126 /* Company news items. */
    00127 static const NWidgetPart _nested_company_news_widgets[] = {
    00128   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
    00129     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
    00130       NWidget(NWID_VERTICAL),
    00131         NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
    00132         NWidget(NWID_SPACER), SetFill(0, 1),
    00133       EndContainer(),
    00134       NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_TITLE), SetFill(1, 1), SetMinimalSize(410, 20), SetDataTip(STR_EMPTY, STR_NULL),
    00135     EndContainer(),
    00136     NWidget(NWID_HORIZONTAL), SetPadding(0, 1, 1, 1),
    00137       NWidget(NWID_VERTICAL),
    00138         NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_FACE), SetMinimalSize(93, 119), SetPadding(2, 6, 2, 1),
    00139         NWidget(NWID_HORIZONTAL),
    00140           NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_NAME), SetMinimalSize(93, 24), SetPadding(0, 0, 0, 1),
    00141           NWidget(NWID_SPACER), SetFill(1, 0),
    00142         EndContainer(),
    00143         NWidget(NWID_SPACER), SetFill(0, 1),
    00144       EndContainer(),
    00145       NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_COMPANY_MSG), SetFill(1, 1), SetMinimalSize(328, 150),
    00146     EndContainer(),
    00147   EndContainer(),
    00148 };
    00149
    00150 static const WindowDesc _company_news_desc(
    00151   WDP_MANUAL, 0, 0,
    00152   WC_NEWS_WINDOW, WC_NONE,
    00153   0,
    00154   _nested_company_news_widgets, lengthof(_nested_company_news_widgets)
    00155 );
    00156
    00157 /* Thin news items. */
    00158 static const NWidgetPart _nested_thin_news_widgets[] = {
    00159   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
    00160     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
    00161       NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
    00162       NWidget(NWID_SPACER), SetFill(1, 0),
    00163       NWidget(NWID_VERTICAL),
    00164         NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
    00165         NWidget(NWID_SPACER), SetFill(0, 1),
    00166       EndContainer(),
    00167     EndContainer(),
    00168     NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 48), SetFill(1, 0), SetPadding(0, 5, 0, 5),
    00169     NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetMinimalSize(426, 70), SetPadding(1, 2, 2, 2),
    00170   EndContainer(),
    00171 };
    00172
    00173 static const WindowDesc _thin_news_desc(
    00174   WDP_MANUAL, 0, 0,
    00175   WC_NEWS_WINDOW, WC_NONE,
    00176   0,
    00177   _nested_thin_news_widgets, lengthof(_nested_thin_news_widgets)
    00178 );
    00179
    00180 /* Small news items. */
    00181 static const NWidgetPart _nested_small_news_widgets[] = {
    00182   /* Caption + close box. The caption is no WWT_CAPTION as the window shall not be moveable and so on. */
    00183   NWidget(NWID_HORIZONTAL),
    00184     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE, WID_N_CLOSEBOX),
    00185     NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_N_CAPTION), SetFill(1, 0),
    00186   EndContainer(),
    00187
    00188   /* Main part */
    00189   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_N_HEADLINE),
    00190     NWidget(WWT_INSET, COLOUR_LIGHT_BLUE, WID_N_INSET), SetPadding(2, 2, 2, 2),
    00191       NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetPadding(1, 1, 1, 1), SetMinimalSize(274, 47), SetFill(1, 0),
    00192     EndContainer(),
    00193     NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(275, 20), SetFill(1, 0), SetPadding(0, 5, 0, 5),
    00194   EndContainer(),
    00195 };
    00196
    00197 static const WindowDesc _small_news_desc(
    00198   WDP_MANUAL, 0, 0,
    00199   WC_NEWS_WINDOW, WC_NONE,
    00200   0,
    00201   _nested_small_news_widgets, lengthof(_nested_small_news_widgets)
    00202 );
    00203
    00207 struct NewsSubtypeData {
    00208   NewsType type;
    00209   NewsFlag flags;
    00210   const WindowDesc *desc;
    00211 };
    00212
    00216 static const NewsSubtypeData _news_subtype_data[] = {
    00217   /* type,               display_mode, flags,                         window description,            callback */
    00218   { NT_ARRIVAL_COMPANY,  (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00219   { NT_ARRIVAL_OTHER,    (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00220   { NT_ACCIDENT,         (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00221   { NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc },
    00222   { NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc },
    00223   { NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc },
    00224   { NT_COMPANY_INFO,     NF_NONE,                        &_company_news_desc },
    00225   { NT_INDUSTRY_OPEN,    (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00226   { NT_INDUSTRY_CLOSE,   (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00227   { NT_ECONOMY,          NF_NONE,                        &_normal_news_desc  },
    00228   { NT_INDUSTRY_COMPANY, (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00229   { NT_INDUSTRY_OTHER,   (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00230   { NT_INDUSTRY_NOBODY,  (NF_NO_TRANSPARENT | NF_SHADE), &_thin_news_desc    },
    00231   { NT_ADVICE,           NF_INCOLOUR,                    &_small_news_desc   },
    00232   { NT_NEW_VEHICLES,     NF_NONE,                        &_vehicle_news_desc },
    00233   { NT_ACCEPTANCE,       NF_INCOLOUR,                    &_small_news_desc   },
    00234   { NT_SUBSIDIES,        NF_NONE,                        &_normal_news_desc  },
    00235   { NT_GENERAL,          NF_NONE,                        &_normal_news_desc  },
    00236 };
    00237
    00238 assert_compile(lengthof(_news_subtype_data) == NS_END);
    00239
    00243 NewsTypeData _news_type_data[] = {
    00244   /*            name,              age, sound,           display,    description */
    00245   NewsTypeData("arrival_player",    60, SND_1D_APPLAUSE, ND_FULL,    STR_NEWS_MESSAGE_TYPE_ARRIVAL_OF_FIRST_VEHICLE_OWN       ),
    00246   NewsTypeData("arrival_other",     60, SND_1D_APPLAUSE, ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_ARRIVAL_OF_FIRST_VEHICLE_OTHER     ),
    00247   NewsTypeData("accident",          90, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_ACCIDENTS_DISASTERS                ),
    00248   NewsTypeData("company_info",      60, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_COMPANY_INFORMATION                ),
    00249   NewsTypeData("open",              90, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_INDUSTRY_OPEN                      ),
    00250   NewsTypeData("close",             90, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_INDUSTRY_CLOSE                     ),
    00251   NewsTypeData("economy",           30, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_ECONOMY_CHANGES                    ),
    00252   NewsTypeData("production_player", 30, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_INDUSTRY_CHANGES_SERVED_BY_COMPANY ),
    00253   NewsTypeData("production_other",  30, SND_BEGIN,       ND_OFF,     STR_NEWS_MESSAGE_TYPE_INDUSTRY_CHANGES_SERVED_BY_OTHER   ),
    00254   NewsTypeData("production_nobody", 30, SND_BEGIN,       ND_OFF,     STR_NEWS_MESSAGE_TYPE_INDUSTRY_CHANGES_UNSERVED          ),
    00255   NewsTypeData("advice",           150, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_ADVICE_INFORMATION_ON_COMPANY      ),
    00256   NewsTypeData("new_vehicles",      30, SND_1E_OOOOH,    ND_FULL,    STR_NEWS_MESSAGE_TYPE_NEW_VEHICLES                       ),
    00257   NewsTypeData("acceptance",        90, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_CHANGES_OF_CARGO_ACCEPTANCE        ),
    00258   NewsTypeData("subsidies",        180, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_SUBSIDIES                          ),
    00259   NewsTypeData("general",           60, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_GENERAL_INFORMATION                ),
    00260 };
    00261
    00262 assert_compile(lengthof(_news_type_data) == NT_END);
    00263
    00265 struct NewsWindow : Window {
    00266   uint16 chat_height;
    00267   uint16 status_height;
    00268   const NewsItem *ni;
    00269   static uint duration;
    00270
    00271   NewsWindow(const WindowDesc *desc, const NewsItem *ni) : Window(), ni(ni)
    00272   {
    00273     NewsWindow::duration = 555;
    00274     const Window *w = FindWindowByClass(WC_SEND_NETWORK_MSG);
    00275     this->chat_height = (w != NULL) ? w->height : 0;
    00276     this->status_height = FindWindowById(WC_STATUS_BAR, 0)->height;
    00277
    00278     this->flags |= WF_DISABLE_VP_SCROLL;
    00279
    00280     this->CreateNestedTree(desc);
    00281     switch (this->ni->subtype) {
    00282       case NS_COMPANY_TROUBLE:
    00283         this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = STR_NEWS_COMPANY_IN_TROUBLE_TITLE;
    00284         break;
    00285
    00286       case NS_COMPANY_MERGER:
    00287         this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = STR_NEWS_COMPANY_MERGER_TITLE;
    00288         break;
    00289
    00290       case NS_COMPANY_BANKRUPT:
    00291         this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = STR_NEWS_COMPANY_BANKRUPT_TITLE;
    00292         break;
    00293
    00294       case NS_COMPANY_NEW:
    00295         this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = STR_NEWS_COMPANY_LAUNCH_TITLE;
    00296         break;
    00297
    00298       default:
    00299         break;
    00300     }
    00301     this->FinishInitNested(desc, 0);
    00302
    00303     /* Initialize viewport if it exists. */
    00304     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_N_VIEWPORT);
    00305     if (nvp != NULL) {
    00306       nvp->InitializeViewport(this, ni->reftype1 == NR_VEHICLE ? 0x80000000 | ni->ref1 : GetReferenceTile(ni->reftype1, ni->ref1), ZOOM_LVL_NEWS);
    00307       if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags |= ND_NO_TRANSPARENCY;
    00308       if ((this->ni->flags & NF_INCOLOUR) == 0) {
    00309         nvp->disp_flags |= ND_SHADE_GREY;
    00310       } else if (this->ni->flags & NF_SHADE) {
    00311         nvp->disp_flags |= ND_SHADE_DIMMED;
    00312       }
    00313     }
    00314
    00315     PositionNewsMessage(this);
    00316   }
    00317
    00318   void DrawNewsBorder(const Rect &r) const
    00319   {
    00320     GfxFillRect(r.left,  r.top,    r.right, r.bottom, PC_WHITE);
    00321
    00322     GfxFillRect(r.left,  r.top,    r.left,  r.bottom, PC_BLACK);
    00323     GfxFillRect(r.right, r.top,    r.right, r.bottom, PC_BLACK);
    00324     GfxFillRect(r.left,  r.top,    r.right, r.top,    PC_BLACK);
    00325     GfxFillRect(r.left,  r.bottom, r.right, r.bottom, PC_BLACK);
    00326   }
    00327
    00328   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00329   {
    00330     Point pt = { 0, _screen.height };
    00331     return pt;
    00332   }
    00333
    00334   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00335   {
    00336     StringID str = STR_NULL;
    00337     switch (widget) {
    00338       case WID_N_MESSAGE:
    00339         CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
    00340         str = this->ni->string_id;
    00341         break;
    00342
    00343       case WID_N_COMPANY_MSG:
    00344         str = this->GetCompanyMessageString();
    00345         break;
    00346
    00347       case WID_N_VEH_NAME:
    00348       case WID_N_VEH_TITLE:
    00349         str = this->GetNewVehicleMessageString(widget);
    00350         break;
    00351
    00352       case WID_N_VEH_INFO: {
    00353         assert(this->ni->reftype1 == NR_ENGINE);
    00354         EngineID engine = this->ni->ref1;
    00355         str = GetEngineInfoString(engine);
    00356         break;
    00357       }
    00358       default:
    00359         return; // Do nothing
    00360     }
    00361
    00362     /* Update minimal size with length of the multi-line string. */
    00363     Dimension d = *size;
    00364     d.width = (d.width >= padding.width) ? d.width - padding.width : 0;
    00365     d.height = (d.height >= padding.height) ? d.height - padding.height : 0;
    00366     d = GetStringMultiLineBoundingBox(str, d);
    00367     d.width += padding.width;
    00368     d.height += padding.height;
    00369     *size = maxdim(*size, d);
    00370   }
    00371
    00372   virtual void SetStringParameters(int widget) const
    00373   {
    00374     if (widget == WID_N_DATE) SetDParam(0, this->ni->date);
    00375   }
    00376
    00377   virtual void DrawWidget(const Rect &r, int widget) const
    00378   {
    00379     switch (widget) {
    00380       case WID_N_CAPTION:
    00381         DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION);
    00382         break;
    00383
    00384       case WID_N_PANEL:
    00385         this->DrawNewsBorder(r);
    00386         break;
    00387
    00388       case WID_N_MESSAGE:
    00389         CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
    00390         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->ni->string_id, TC_FROMSTRING, SA_CENTER);
    00391         break;
    00392
    00393       case WID_N_MGR_FACE: {
    00394         const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
    00395         DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top);
    00396         GfxFillRect(r.left + 1, r.top, r.left + 1 + 91, r.top + 118, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
    00397         break;
    00398       }
    00399       case WID_N_MGR_NAME: {
    00400         const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
    00401         SetDParamStr(0, cni->president_name);
    00402         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
    00403         break;
    00404       }
    00405       case WID_N_COMPANY_MSG:
    00406         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetCompanyMessageString(), TC_FROMSTRING, SA_CENTER);
    00407         break;
    00408
    00409       case WID_N_VEH_BKGND:
    00410         GfxFillRect(r.left, r.top, r.right, r.bottom, PC_GREY);
    00411         break;
    00412
    00413       case WID_N_VEH_NAME:
    00414       case WID_N_VEH_TITLE:
    00415         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetNewVehicleMessageString(widget), TC_FROMSTRING, SA_CENTER);
    00416         break;
    00417
    00418       case WID_N_VEH_SPR: {
    00419         assert(this->ni->reftype1 == NR_ENGINE);
    00420         EngineID engine = this->ni->ref1;
    00421         DrawVehicleEngine(r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW);
    00422         GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
    00423         break;
    00424       }
    00425       case WID_N_VEH_INFO: {
    00426         assert(this->ni->reftype1 == NR_ENGINE);
    00427         EngineID engine = this->ni->ref1;
    00428         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
    00429         break;
    00430       }
    00431     }
    00432   }
    00433
    00434   virtual void OnClick(Point pt, int widget, int click_count)
    00435   {
    00436     switch (widget) {
    00437       case WID_N_CLOSEBOX:
    00438         NewsWindow::duration = 0;
    00439         delete this;
    00440         _forced_news = NULL;
    00441         break;
    00442
    00443       case WID_N_CAPTION:
    00444         if (this->ni->reftype1 == NR_VEHICLE) {
    00445           const Vehicle *v = Vehicle::Get(this->ni->ref1);
    00446           ShowVehicleViewWindow(v);
    00447         }
    00448         break;
    00449
    00450       case WID_N_VIEWPORT:
    00451         break; // Ignore clicks
    00452
    00453       default:
    00454         if (this->ni->reftype1 == NR_VEHICLE) {
    00455           const Vehicle *v = Vehicle::Get(this->ni->ref1);
    00456           ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
    00457         } else {
    00458           TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
    00459           TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
    00460           if (_ctrl_pressed) {
    00461             if (tile1 != INVALID_TILE) ShowExtraViewPortWindow(tile1);
    00462             if (tile2 != INVALID_TILE) ShowExtraViewPortWindow(tile2);
    00463           } else {
    00464             if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
    00465               ScrollMainWindowToTile(tile2);
    00466             }
    00467           }
    00468         }
    00469         break;
    00470     }
    00471   }
    00472
    00473   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00474   {
    00475     if (keycode == WKC_SPACE) {
    00476       /* Don't continue. */
    00477       delete this;
    00478       return ES_HANDLED;
    00479     }
    00480     return ES_NOT_HANDLED;
    00481   }
    00482
    00488   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00489   {
    00490     if (!gui_scope) return;
    00491     /* The chatbar has notified us that is was either created or closed */
    00492     int newtop = this->top + this->chat_height - data;
    00493     this->chat_height = data;
    00494     this->SetWindowTop(newtop);
    00495   }
    00496
    00497   virtual void OnTick()
    00498   {
    00499     /* Scroll up newsmessages from the bottom in steps of 4 pixels */
    00500     int newtop = max(this->top - 4, _screen.height - this->height - this->status_height - this->chat_height);
    00501     this->SetWindowTop(newtop);
    00502   }
    00503
    00504 private:
    00509   void SetWindowTop(int newtop)
    00510   {
    00511     if (this->top == newtop) return;
    00512
    00513     int mintop = min(newtop, this->top);
    00514     int maxtop = max(newtop, this->top);
    00515     if (this->viewport != NULL) this->viewport->top += newtop - this->top;
    00516     this->top = newtop;
    00517
    00518     SetDirtyBlocks(this->left, mintop, this->left + this->width, maxtop + this->height);
    00519   }
    00520
    00521   StringID GetCompanyMessageString() const
    00522   {
    00523     switch (this->ni->subtype) {
    00524       case NS_COMPANY_TROUBLE:
    00525         SetDParam(0, this->ni->params[2]);
    00526         return STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION;
    00527
    00528       case NS_COMPANY_MERGER:
    00529         SetDParam(0, this->ni->params[2]);
    00530         SetDParam(1, this->ni->params[3]);
    00531         SetDParam(2, this->ni->params[4]);
    00532         return this->ni->params[4] == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION;
    00533
    00534       case NS_COMPANY_BANKRUPT:
    00535         SetDParam(0, this->ni->params[2]);
    00536         return STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION;
    00537
    00538       case NS_COMPANY_NEW:
    00539         SetDParam(0, this->ni->params[2]);
    00540         SetDParam(1, this->ni->params[3]);
    00541         return STR_NEWS_COMPANY_LAUNCH_DESCRIPTION;
    00542
    00543       default:
    00544         NOT_REACHED();
    00545     }
    00546   }
    00547
    00548   StringID GetNewVehicleMessageString(int widget) const
    00549   {
    00550     assert(this->ni->reftype1 == NR_ENGINE);
    00551     EngineID engine = this->ni->ref1;
    00552
    00553     switch (widget) {
    00554       case WID_N_VEH_TITLE:
    00555         SetDParam(0, GetEngineCategoryName(engine));
    00556         return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
    00557
    00558       case WID_N_VEH_NAME:
    00559         SetDParam(0, engine);
    00560         return STR_NEWS_NEW_VEHICLE_TYPE;
    00561
    00562       default:
    00563         NOT_REACHED();
    00564     }
    00565   }
    00566 };
    00567
    00568 /* static */ uint NewsWindow::duration = 0; // Instance creation.
    00569
    00570
    00572 static void ShowNewspaper(const NewsItem *ni)
    00573 {
    00574   SoundFx sound = _news_type_data[_news_subtype_data[ni->subtype].type].sound;
    00575   if (sound != 0) SndPlayFx(sound);
    00576
    00577   new NewsWindow(_news_subtype_data[ni->subtype].desc, ni);
    00578 }
    00579
    00581 static void ShowTicker(const NewsItem *ni)
    00582 {
    00583   if (_news_ticker_sound) SndPlayFx(SND_16_MORSE);
    00584
    00585   _statusbar_news_item = ni;
    00586   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
    00587 }
    00588
    00590 void InitNewsItemStructs()
    00591 {
    00592   for (NewsItem *ni = _oldest_news; ni != NULL; ) {
    00593     NewsItem *next = ni->next;
    00594     delete ni;
    00595     ni = next;
    00596   }
    00597
    00598   _total_news = 0;
    00599   _oldest_news = NULL;
    00600   _latest_news = NULL;
    00601   _forced_news = NULL;
    00602   _current_news = NULL;
    00603   _statusbar_news_item = NULL;
    00604   NewsWindow::duration = 0;
    00605 }
    00606
    00611 static bool ReadyForNextItem()
    00612 {
    00613   const NewsItem *ni = _forced_news == NULL ? _current_news : _forced_news;
    00614   if (ni == NULL) return true;
    00615
    00616   /* Ticker message
    00617    * Check if the status bar message is still being displayed? */
    00618   if (IsNewsTickerShown()) return false;
    00619
    00620   /* Newspaper message, decrement duration counter */
    00621   if (NewsWindow::duration != 0) NewsWindow::duration--;
    00622
    00623   /* neither newsticker nor newspaper are running */
    00624   return (NewsWindow::duration == 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
    00625 }
    00626
    00628 static void MoveToNextItem()
    00629 {
    00630   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
    00631   DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
    00632   _forced_news = NULL;
    00633   _statusbar_news_item = NULL;
    00634
    00635   /* if we're not at the last item, then move on */
    00636   if (_current_news != _latest_news) {
    00637     _current_news = (_current_news == NULL) ? _oldest_news : _current_news->next;
    00638     const NewsItem *ni = _current_news;
    00639     const NewsType type = _news_subtype_data[ni->subtype].type;
    00640
    00641     /* check the date, don't show too old items */
    00642     if (_date - _news_type_data[type].age > ni->date) return;
    00643
    00644     switch (_news_type_data[type].display) {
    00645       default: NOT_REACHED();
    00646       case ND_OFF: // Off - show nothing only a small reminder in the status bar
    00647         InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
    00648         break;
    00649
    00650       case ND_SUMMARY: // Summary - show ticker
    00651         ShowTicker(ni);
    00652         break;
    00653
    00654       case ND_FULL: // Full - show newspaper
    00655         ShowNewspaper(ni);
    00656         break;
    00657     }
    00658   }
    00659 }
    00660
    00673 void AddNewsItem(StringID string, NewsSubtype subtype, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
    00674 {
    00675   if (_game_mode == GM_MENU) return;
    00676
    00677   /* Create new news item node */
    00678   NewsItem *ni = new NewsItem;
    00679
    00680   ni->string_id = string;
    00681   ni->subtype = subtype;
    00682   ni->flags = _news_subtype_data[subtype].flags;
    00683
    00684   /* show this news message in colour? */
    00685   if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR;
    00686
    00687   ni->reftype1 = reftype1;
    00688   ni->reftype2 = reftype2;
    00689   ni->ref1 = ref1;
    00690   ni->ref2 = ref2;
    00691   ni->free_data = free_data;
    00692   ni->date = _date;
    00693   CopyOutDParam(ni->params, 0, lengthof(ni->params));
    00694
    00695   if (_total_news++ == 0) {
    00696     assert(_oldest_news == NULL);
    00697     _oldest_news = ni;
    00698     ni->prev = NULL;
    00699   } else {
    00700     assert(_latest_news->next == NULL);
    00701     _latest_news->next = ni;
    00702     ni->prev = _latest_news;
    00703   }
    00704
    00705   ni->next = NULL;
    00706   _latest_news = ni;
    00707
    00708   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
    00709 }
    00710
    00723 CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00724 {
    00725   if (_current_company != OWNER_DEITY) return CMD_ERROR;
    00726
    00727   NewsSubtype subtype = (NewsSubtype)GB(p1, 0, 8);
    00728   NewsReferenceType reftype1 = (NewsReferenceType)GB(p1, 8, 8);
    00729   CompanyID company = (CompanyID)GB(p1, 16, 8);
    00730
    00731   if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR;
    00732   if (subtype >= NS_END) return CMD_ERROR;
    00733   if (StrEmpty(text)) return CMD_ERROR;
    00734
    00735   switch (reftype1) {
    00736     case NR_NONE: break;
    00737     case NR_TILE:
    00738       if (!IsValidTile(p2)) return CMD_ERROR;
    00739       break;
    00740
    00741     case NR_VEHICLE:
    00742       if (!Vehicle::IsValidID(p2)) return CMD_ERROR;
    00743       break;
    00744
    00745     case NR_STATION:
    00746       if (!Station::IsValidID(p2)) return CMD_ERROR;
    00747       break;
    00748
    00749     case NR_INDUSTRY:
    00750       if (!Industry::IsValidID(p2)) return CMD_ERROR;
    00751       break;
    00752
    00753     case NR_TOWN:
    00754       if (!Town::IsValidID(p2)) return CMD_ERROR;
    00755       break;
    00756
    00757     case NR_ENGINE:
    00758       if (!Engine::IsValidID(p2)) return CMD_ERROR;
    00759       break;
    00760
    00761     default: return CMD_ERROR;
    00762   }
    00763
    00764   if (company != INVALID_OWNER && company != _local_company) return CommandCost();
    00765
    00766   if (flags & DC_EXEC) {
    00767     char *news = strdup(text);
    00768     SetDParamStr(0, news);
    00769     AddNewsItem(STR_NEWS_CUSTOM_ITEM, subtype, reftype1, p2, NR_NONE, UINT32_MAX, news);
    00770   }
    00771
    00772   return CommandCost();
    00773 }
    00774
    00776 static void DeleteNewsItem(NewsItem *ni)
    00777 {
    00778   /* Delete the news from the news queue. */
    00779   if (ni->prev != NULL) {
    00780     ni->prev->next = ni->next;
    00781   } else {
    00782     assert(_oldest_news == ni);
    00783     _oldest_news = ni->next;
    00784   }
    00785
    00786   if (ni->next != NULL) {
    00787     ni->next->prev = ni->prev;
    00788   } else {
    00789     assert(_latest_news == ni);
    00790     _latest_news = ni->prev;
    00791   }
    00792
    00793   _total_news--;
    00794
    00795   if (_forced_news == ni || _current_news == ni || _statusbar_news_item == ni) {
    00796     /* When we're the current news, go to the previous item first;
    00797      * we just possibly made that the last news item. */
    00798     if (_current_news == ni) _current_news = ni->prev;
    00799
    00800     /* About to remove the currently forced item (shown as newspapers) ||
    00801      * about to remove the currently displayed item (newspapers, ticker, or just a reminder) */
    00802     MoveToNextItem();
    00803   }
    00804
    00805   delete ni;
    00806
    00807   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
    00808 }
    00809
    00816 void DeleteVehicleNews(VehicleID vid, StringID news)
    00817 {
    00818   NewsItem *ni = _oldest_news;
    00819
    00820   while (ni != NULL) {
    00821     NewsItem *next = ni->next;
    00822     if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
    00823         (news == INVALID_STRING_ID || ni->string_id == news)) {
    00824       DeleteNewsItem(ni);
    00825     }
    00826     ni = next;
    00827   }
    00828 }
    00829
    00835 void DeleteStationNews(StationID sid)
    00836 {
    00837   NewsItem *ni = _oldest_news;
    00838
    00839   while (ni != NULL) {
    00840     NewsItem *next = ni->next;
    00841     if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
    00842       DeleteNewsItem(ni);
    00843     }
    00844     ni = next;
    00845   }
    00846 }
    00847
    00852 void DeleteIndustryNews(IndustryID iid)
    00853 {
    00854   NewsItem *ni = _oldest_news;
    00855
    00856   while (ni != NULL) {
    00857     NewsItem *next = ni->next;
    00858     if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
    00859       DeleteNewsItem(ni);
    00860     }
    00861     ni = next;
    00862   }
    00863 }
    00864
    00868 void DeleteInvalidEngineNews()
    00869 {
    00870   NewsItem *ni = _oldest_news;
    00871
    00872   while (ni != NULL) {
    00873     NewsItem *next = ni->next;
    00874     if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
    00875         (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
    00876       DeleteNewsItem(ni);
    00877     }
    00878     ni = next;
    00879   }
    00880 }
    00881
    00882 static void RemoveOldNewsItems()
    00883 {
    00884   NewsItem *next;
    00885   for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
    00886     next = cur->next;
    00887     if (_date - _news_type_data[_news_subtype_data[cur->subtype].type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
    00888   }
    00889 }
    00890
    00897 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
    00898 {
    00899   for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
    00900     if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
    00901     if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
    00902
    00903     /* Oh noes :(
    00904      * Autoreplace is breaking the whole news-reference concept here, as we want to keep the news,
    00905      * but do not know which DParams to change.
    00906      *
    00907      * Currently only NS_ADVICE news have vehicle IDs in their DParams.
    00908      * And all NS_ADVICE news have the ID in param 0.
    00909      */
    00910     if (ni->subtype == NS_ADVICE && ni->params[0] == from_index) ni->params[0] = to_index;
    00911   }
    00912 }
    00913
    00914 void NewsLoop()
    00915 {
    00916   /* no news item yet */
    00917   if (_total_news == 0) return;
    00918
    00919   /* There is no status bar, so no reason to show news;
    00920    * especially important with the end game screen when
    00921    * there is no status bar but possible news. */
    00922   if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
    00923
    00924   static byte _last_clean_month = 0;
    00925
    00926   if (_last_clean_month != _cur_month) {
    00927     RemoveOldNewsItems();
    00928     _last_clean_month = _cur_month;
    00929   }
    00930
    00931   if (ReadyForNextItem()) MoveToNextItem();
    00932 }
    00933
    00935 static void ShowNewsMessage(const NewsItem *ni)
    00936 {
    00937   assert(_total_news != 0);
    00938
    00939   /* Delete the news window */
    00940   DeleteWindowById(WC_NEWS_WINDOW, 0);
    00941
    00942   /* setup forced news item */
    00943   _forced_news = ni;
    00944
    00945   if (_forced_news != NULL) {
    00946     DeleteWindowById(WC_NEWS_WINDOW, 0);
    00947     ShowNewspaper(ni);
    00948   }
    00949 }
    00950
    00952 void ShowLastNewsMessage()
    00953 {
    00954   if (_total_news == 0) {
    00955     return;
    00956   } else if (_forced_news == NULL) {
    00957     /* Not forced any news yet, show the current one, unless a news window is
    00958      * open (which can only be the current one), then show the previous item */
    00959     const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
    00960     ShowNewsMessage((w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev);
    00961   } else if (_forced_news == _oldest_news) {
    00962     /* We have reached the oldest news, start anew with the latest */
    00963     ShowNewsMessage(_latest_news);
    00964   } else {
    00965     /* 'Scrolling' through news history show each one in turn */
    00966     ShowNewsMessage(_forced_news->prev);
    00967   }
    00968 }
    00969
    00970
    00981 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
    00982 {
    00983   char buffer[512], buffer2[512];
    00984   StringID str;
    00985
    00986   CopyInDParam(0, ni->params, lengthof(ni->params));
    00987   str = ni->string_id;
    00988
    00989   GetString(buffer, str, lastof(buffer));
    00990   /* Copy the just gotten string to another buffer to remove any formatting
    00991    * from it such as big fonts, etc. */
    00992   const char *ptr = buffer;
    00993   char *dest = buffer2;
    00994   WChar c_last = '\0';
    00995   for (;;) {
    00996     WChar c = Utf8Consume(&ptr);
    00997     if (c == 0) break;
    00998     /* Make a space from a newline, but ignore multiple newlines */
    00999     if (c == '\n' && c_last != '\n') {
    01000       dest[0] = ' ';
    01001       dest++;
    01002     } else if (c == '\r') {
    01003       dest[0] = dest[1] = dest[2] = dest[3] = ' ';
    01004       dest += 4;
    01005     } else if (IsPrintable(c)) {
    01006       dest += Utf8Encode(dest, c);
    01007     }
    01008     c_last = c;
    01009   }
    01010
    01011   *dest = '\0';
    01012   /* Truncate and show string; postfixed by '...' if neccessary */
    01013   DrawString(left, right, y, buffer2, colour);
    01014 }
    01015
    01016 struct MessageHistoryWindow : Window {
    01017   static const int top_spacing;
    01018   static const int bottom_spacing;
    01019
    01020   int line_height;
    01021   int date_width;
    01022
    01023   Scrollbar *vscroll;
    01024
    01025   MessageHistoryWindow(const WindowDesc *desc) : Window()
    01026   {
    01027     this->CreateNestedTree(desc);
    01028     this->vscroll = this->GetScrollbar(WID_MH_SCROLLBAR);
    01029     this->FinishInitNested(desc); // Initializes 'this->line_height' and 'this->date_width'.
    01030     this->OnInvalidateData(0);
    01031   }
    01032
    01033   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01034   {
    01035     if (widget == WID_MH_BACKGROUND) {
    01036       this->line_height = FONT_HEIGHT_NORMAL + 2;
    01037       resize->height = this->line_height;
    01038
    01039       /* Months are off-by-one, so it's actually 8. Not using
    01040        * month 12 because the 1 is usually less wide. */
    01041       SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 7, 30));
    01042       this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
    01043
    01044       size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
    01045       size->width = max(200u, size->width); // At least 200 pixels wide.
    01046     }
    01047   }
    01048
    01049   virtual void OnPaint()
    01050   {
    01051     this->OnInvalidateData(0);
    01052     this->DrawWidgets();
    01053   }
    01054
    01055   virtual void DrawWidget(const Rect &r, int widget) const
    01056   {
    01057     if (widget != WID_MH_BACKGROUND || _total_news == 0) return;
    01058
    01059     /* Find the first news item to display. */
    01060     NewsItem *ni = _latest_news;
    01061     for (int n = this->vscroll->GetPosition(); n > 0; n--) {
    01062       ni = ni->prev;
    01063       if (ni == NULL) return;
    01064     }
    01065
    01066     /* Fill the widget with news items. */
    01067     int y = r.top + this->top_spacing;
    01068     bool rtl = _current_text_dir == TD_RTL;
    01069     uint date_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
    01070     uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
    01071     uint news_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
    01072     uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
    01073     for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
    01074       SetDParam(0, ni->date);
    01075       DrawString(date_left, date_right, y, STR_SHORT_DATE);
    01076
    01077       DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
    01078       y += this->line_height;
    01079
    01080       ni = ni->prev;
    01081       if (ni == NULL) return;
    01082     }
    01083   }
    01084
    01090   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01091   {
    01092     if (!gui_scope) return;
    01093     this->vscroll->SetCount(_total_news);
    01094   }
    01095
    01096   virtual void OnClick(Point pt, int widget, int click_count)
    01097   {
    01098     if (widget == WID_MH_BACKGROUND) {
    01099       NewsItem *ni = _latest_news;
    01100       if (ni == NULL) return;
    01101
    01102       for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_MH_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
    01103         ni = ni->prev;
    01104         if (ni == NULL) return;
    01105       }
    01106
    01107       ShowNewsMessage(ni);
    01108     }
    01109   }
    01110
    01111   virtual void OnResize()
    01112   {
    01113     this->vscroll->SetCapacity(this->GetWidget<NWidgetBase>(WID_MH_BACKGROUND)->current_y / this->line_height);
    01114   }
    01115 };
    01116
    01117 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
    01118 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
    01119
    01120 static const NWidgetPart _nested_message_history[] = {
    01121   NWidget(NWID_HORIZONTAL),
    01122     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    01123     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01124     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    01125     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    01126   EndContainer(),
    01127
    01128   NWidget(NWID_HORIZONTAL),
    01129     NWidget(WWT_PANEL, COLOUR_BROWN, WID_MH_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR),
    01130     EndContainer(),
    01131     NWidget(NWID_VERTICAL),
    01132       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_MH_SCROLLBAR),
    01133       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    01134     EndContainer(),
    01135   EndContainer(),
    01136 };
    01137
    01138 static const WindowDesc _message_history_desc(
    01139   WDP_AUTO, 400, 140,
    01140   WC_MESSAGE_HISTORY, WC_NONE,
    01141   WDF_UNCLICK_BUTTONS,
    01142   _nested_message_history, lengthof(_nested_message_history)
    01143 );
    01144
    01146 void ShowMessageHistory()
    01147 {
    01148   DeleteWindowById(WC_MESSAGE_HISTORY, 0);
    01149   new MessageHistoryWindow(&_message_history_desc);
    01150 }
    01151
    01152 struct MessageOptionsWindow : Window {
    01153   static const StringID message_opt[];
    01154   int state;
    01155   Dimension dim_message_opt;
    01156
    01157   MessageOptionsWindow(const WindowDesc *desc) : Window()
    01158   {
    01159     this->InitNested(desc, WN_GAME_OPTIONS_MESSAGE_OPTION);
    01160     /* Set up the initial disabled buttons in the case of 'off' or 'full' */
    01161     NewsDisplay all_val = _news_type_data[0].display;
    01162     for (int i = 0; i < NT_END; i++) {
    01163       this->SetMessageButtonStates(_news_type_data[i].display, i);
    01164       /* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
    01165       if (_news_type_data[i].display != all_val) all_val = ND_OFF;
    01166     }
    01167     /* If all values are the same value, the ALL-button will take over this value */
    01168     this->state = all_val;
    01169     this->OnInvalidateData(0);
    01170   }
    01171
    01180   void SetMessageButtonStates(byte value, int element)
    01181   {
    01182     element *= MOS_WIDG_PER_SETTING;
    01183
    01184     this->SetWidgetDisabledState(element + WID_MO_START_OPTION, value == 0);
    01185     this->SetWidgetDisabledState(element + WID_MO_START_OPTION + 2, value == 2);
    01186   }
    01187
    01188   virtual void DrawWidget(const Rect &r, int widget) const
    01189   {
    01190     if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION && (widget -  WID_MO_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
    01191       /* Draw the string of each setting on each button. */
    01192       int i = (widget -  WID_MO_START_OPTION) / MOS_WIDG_PER_SETTING;
    01193       DrawString(r.left, r.right, r.top + 2, this->message_opt[_news_type_data[i].display], TC_BLACK, SA_HOR_CENTER);
    01194     }
    01195   }
    01196
    01197   virtual void OnInit()
    01198   {
    01199     this->dim_message_opt.width  = 0;
    01200     this->dim_message_opt.height = 0;
    01201     for (const StringID *str = message_opt; *str != INVALID_STRING_ID; str++) this->dim_message_opt = maxdim(this->dim_message_opt, GetStringBoundingBox(*str));
    01202   }
    01203
    01204   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01205   {
    01206     if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION) {
    01207       /* Height is the biggest widget height in a row. */
    01208       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
    01209
    01210       /* Compute width for the label widget only. */
    01211       if ((widget - WID_MO_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
    01212         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
    01213       }
    01214       return;
    01215     }
    01216
    01217     /* Size computations for global message options. */
    01218     if (widget == WID_MO_DROP_SUMMARY || widget == WID_MO_LABEL_SUMMARY || widget == WID_MO_SOUNDTICKER || widget == WID_MO_SOUNDTICKER_LABEL) {
    01219       /* Height is the biggest widget height in a row. */
    01220       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM);
    01221
    01222       if (widget == WID_MO_DROP_SUMMARY) {
    01223         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
    01224       } else if (widget == WID_MO_SOUNDTICKER) {
    01225         size->width += MOS_BUTTON_SPACE; // A bit extra for better looks.
    01226       }
    01227       return;
    01228     }
    01229   }
    01230
    01236   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01237   {
    01238     if (!gui_scope) return;
    01239     /* Update the dropdown value for 'set all categories'. */
    01240     this->GetWidget<NWidgetCore>(WID_MO_DROP_SUMMARY)->widget_data = this->message_opt[this->state];
    01241
    01242     /* Update widget to reflect the value of the #_news_ticker_sound variable. */
    01243     this->SetWidgetLoweredState(WID_MO_SOUNDTICKER, _news_ticker_sound);
    01244   }
    01245
    01246   virtual void OnClick(Point pt, int widget, int click_count)
    01247   {
    01248     switch (widget) {
    01249       case WID_MO_DROP_SUMMARY: // Dropdown menu for all settings
    01250         ShowDropDownMenu(this, this->message_opt, this->state, WID_MO_DROP_SUMMARY, 0, 0);
    01251         break;
    01252
    01253       case WID_MO_SOUNDTICKER: // Change ticker sound on/off
    01254         _news_ticker_sound ^= 1;
    01255         this->InvalidateData();
    01256         break;
    01257
    01258       default: { // Clicked on the [<] .. [>] widgets
    01259         if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION) {
    01260           int wid = widget - WID_MO_START_OPTION;
    01261           int element = wid / MOS_WIDG_PER_SETTING;
    01262           byte val = (_news_type_data[element].display + ((wid % MOS_WIDG_PER_SETTING) ? 1 : -1)) % 3;
    01263
    01264           this->SetMessageButtonStates(val, element);
    01265           _news_type_data[element].display = (NewsDisplay)val;
    01266           this->SetDirty();
    01267         }
    01268         break;
    01269       }
    01270     }
    01271   }
    01272
    01273   virtual void OnDropdownSelect(int widget, int index)
    01274   {
    01275     this->state = index;
    01276
    01277     for (int i = 0; i < NT_END; i++) {
    01278       this->SetMessageButtonStates(index, i);
    01279       _news_type_data[i].display = (NewsDisplay)index;
    01280     }
    01281     this->InvalidateData();
    01282   }
    01283 };
    01284
    01285 const StringID MessageOptionsWindow::message_opt[] = {STR_NEWS_MESSAGES_OFF, STR_NEWS_MESSAGES_SUMMARY, STR_NEWS_MESSAGES_FULL, INVALID_STRING_ID};
    01286
    01288 static NWidgetBase *MakeButtonsColumn(int *biggest_index)
    01289 {
    01290   NWidgetVertical *vert_buttons = new NWidgetVertical;
    01291
    01292   /* Top-part of the column, one row for each new category. */
    01293   int widnum = WID_MO_START_OPTION;
    01294   for (int i = 0; i < NT_END; i++) {
    01295     NWidgetHorizontal *hor = new NWidgetHorizontal;
    01296     /* [<] button. */
    01297     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01298     leaf->SetFill(1, 1);
    01299     hor->Add(leaf);
    01300     /* Label. */
    01301     leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_YELLOW, widnum + 1, STR_EMPTY, STR_NULL);
    01302     leaf->SetFill(1, 1);
    01303     hor->Add(leaf);
    01304     /* [>] button. */
    01305     leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 2, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
    01306     leaf->SetFill(1, 1);
    01307     hor->Add(leaf);
    01308     vert_buttons->Add(hor);
    01309
    01310     widnum += MOS_WIDG_PER_SETTING;
    01311   }
    01312   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 2;
    01313
    01314   /* Space between the category buttons and the global settings buttons. */
    01315   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
    01316   vert_buttons->Add(spacer);
    01317
    01318   /* Bottom part of the column with buttons for global changes. */
    01319   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_DROPDOWN, COLOUR_YELLOW, WID_MO_DROP_SUMMARY, STR_EMPTY, STR_NULL);
    01320   leaf->SetFill(1, 1);
    01321   vert_buttons->Add(leaf);
    01322
    01323   leaf = new NWidgetLeaf(WWT_TEXTBTN_2, COLOUR_YELLOW, WID_MO_SOUNDTICKER, STR_STATION_BUILD_COVERAGE_OFF, STR_NULL);
    01324   leaf->SetFill(1, 1);
    01325   vert_buttons->Add(leaf);
    01326
    01327   *biggest_index = max(*biggest_index, max<int>(WID_MO_DROP_SUMMARY, WID_MO_SOUNDTICKER));
    01328   return vert_buttons;
    01329 }
    01330
    01332 static NWidgetBase *MakeDescriptionColumn(int *biggest_index)
    01333 {
    01334   NWidgetVertical *vert_desc = new NWidgetVertical;
    01335
    01336   /* Top-part of the column, one row for each new category. */
    01337   int widnum = WID_MO_START_OPTION;
    01338   for (int i = 0; i < NT_END; i++) {
    01339     NWidgetHorizontal *hor = new NWidgetHorizontal;
    01340
    01341     /* Descriptive text. */
    01342     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 3, _news_type_data[i].description, STR_NULL);
    01343     hor->Add(leaf);
    01344     /* Filling empty space to push text to the left. */
    01345     NWidgetSpacer *spacer = new NWidgetSpacer(0, 0);
    01346     spacer->SetFill(1, 0);
    01347     hor->Add(spacer);
    01348     vert_desc->Add(hor);
    01349
    01350     widnum += MOS_WIDG_PER_SETTING;
    01351   }
    01352   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 3;
    01353
    01354   /* Space between the category descriptions and the global settings descriptions. */
    01355   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
    01356   vert_desc->Add(spacer);
    01357
    01358   /* Bottom part of the column with descriptions of global changes. */
    01359   NWidgetHorizontal *hor = new NWidgetHorizontal;
    01360   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WID_MO_LABEL_SUMMARY, STR_NEWS_MESSAGES_ALL, STR_NULL);
    01361   hor->Add(leaf);
    01362   /* Filling empty space to push text to the left. */
    01363   spacer = new NWidgetSpacer(0, 0);
    01364   spacer->SetFill(1, 0);
    01365   hor->Add(spacer);
    01366   vert_desc->Add(hor);
    01367
    01368   hor = new NWidgetHorizontal;
    01369   leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WID_MO_SOUNDTICKER_LABEL, STR_NEWS_MESSAGES_SOUND, STR_NULL);
    01370   hor->Add(leaf);
    01371   /* Filling empty space to push text to the left. */
    01372   spacer = new NWidgetSpacer(0, 0);
    01373   leaf->SetFill(1, 0);
    01374   hor->Add(spacer);
    01375   vert_desc->Add(hor);
    01376
    01377   *biggest_index = max(*biggest_index, max<int>(WID_MO_LABEL_SUMMARY, WID_MO_SOUNDTICKER_LABEL));
    01378   return vert_desc;
    01379 }
    01380
    01381 static const NWidgetPart _nested_message_options_widgets[] = {
    01382   NWidget(NWID_HORIZONTAL),
    01383     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    01384     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_NEWS_MESSAGE_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01385   EndContainer(),
    01386   NWidget(WWT_PANEL, COLOUR_BROWN, WID_MO_BACKGROUND),
    01387     NWidget(NWID_HORIZONTAL),
    01388       NWidget(NWID_SPACER), SetFill(1, 0),
    01389       NWidget(WWT_LABEL, COLOUR_BROWN, WID_MO_LABEL), SetMinimalSize(0, 14), SetDataTip(STR_NEWS_MESSAGE_TYPES, STR_NULL),
    01390       NWidget(NWID_SPACER), SetFill(1, 0),
    01391     EndContainer(),
    01392     NWidget(NWID_HORIZONTAL),
    01393       NWidget(NWID_SPACER), SetMinimalSize(MOS_LEFT_EDGE, 0),
    01394       NWidgetFunction(MakeButtonsColumn),
    01395       NWidget(NWID_SPACER), SetMinimalSize(MOS_COLUMN_SPACING, 0),
    01396       NWidgetFunction(MakeDescriptionColumn),
    01397       NWidget(NWID_SPACER), SetMinimalSize(MOS_RIGHT_EDGE, 0),
    01398     EndContainer(),
    01399     NWidget(NWID_SPACER), SetMinimalSize(0, MOS_BOTTOM_EDGE),
    01400   EndContainer(),
    01401 };
    01402
    01403 static const WindowDesc _message_options_desc(
    01404   WDP_AUTO, 0, 0,
    01405   WC_GAME_OPTIONS, WC_NONE,
    01406   WDF_UNCLICK_BUTTONS,
    01407   _nested_message_options_widgets, lengthof(_nested_message_options_widgets)
    01408 );
    01409
    01413 void ShowMessageOptions()
    01414 {
    01415   DeleteWindowByClass(WC_GAME_OPTIONS);
    01416   new MessageOptionsWindow(&_message_options_desc);
    01417 }