# news_gui.cpp

Go to the documentation of this file.

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