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   const WindowDesc *desc = _news_subtype_data[subtype].desc;
00765   for (int i = 0; i < desc->nwid_length; i++) {
00766     if (desc->nwid_parts[i].type == NWID_VIEWPORT && reftype1 != NR_VEHICLE && GetReferenceTile(reftype1, p2) == INVALID_TILE) {
00767       return CMD_ERROR;
00768     }
00769   }
00770 
00771   switch (subtype) {
00772     /* These sub types require more parameters that are never passed. */
00773     case NS_COMPANY_TROUBLE:
00774     case NS_COMPANY_MERGER:
00775     case NS_COMPANY_BANKRUPT:
00776     case NS_COMPANY_NEW:
00777       return CMD_ERROR;
00778 
00779     /* This one only accepts engines. */
00780     case NS_NEW_VEHICLES:
00781       if (reftype1 != NR_ENGINE) return CMD_ERROR;
00782       break;
00783 
00784     /* The rest, in theory, accepts everything. */
00785     default:
00786       break;
00787   }
00788 
00789   if (company != INVALID_OWNER && company != _local_company) return CommandCost();
00790 
00791   if (flags & DC_EXEC) {
00792     char *news = strdup(text);
00793     SetDParamStr(0, news);
00794     AddNewsItem(STR_NEWS_CUSTOM_ITEM, subtype, reftype1, p2, NR_NONE, UINT32_MAX, news);
00795   }
00796 
00797   return CommandCost();
00798 }
00799 
00801 static void DeleteNewsItem(NewsItem *ni)
00802 {
00803   /* Delete the news from the news queue. */
00804   if (ni->prev != NULL) {
00805     ni->prev->next = ni->next;
00806   } else {
00807     assert(_oldest_news == ni);
00808     _oldest_news = ni->next;
00809   }
00810 
00811   if (ni->next != NULL) {
00812     ni->next->prev = ni->prev;
00813   } else {
00814     assert(_latest_news == ni);
00815     _latest_news = ni->prev;
00816   }
00817 
00818   _total_news--;
00819 
00820   if (_forced_news == ni || _current_news == ni || _statusbar_news_item == ni) {
00821     /* When we're the current news, go to the previous item first;
00822      * we just possibly made that the last news item. */
00823     if (_current_news == ni) _current_news = ni->prev;
00824 
00825     /* About to remove the currently forced item (shown as newspapers) ||
00826      * about to remove the currently displayed item (newspapers, ticker, or just a reminder) */
00827     MoveToNextItem();
00828   }
00829 
00830   delete ni;
00831 
00832   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
00833 }
00834 
00841 void DeleteVehicleNews(VehicleID vid, StringID news)
00842 {
00843   NewsItem *ni = _oldest_news;
00844 
00845   while (ni != NULL) {
00846     NewsItem *next = ni->next;
00847     if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
00848         (news == INVALID_STRING_ID || ni->string_id == news)) {
00849       DeleteNewsItem(ni);
00850     }
00851     ni = next;
00852   }
00853 }
00854 
00860 void DeleteStationNews(StationID sid)
00861 {
00862   NewsItem *ni = _oldest_news;
00863 
00864   while (ni != NULL) {
00865     NewsItem *next = ni->next;
00866     if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
00867       DeleteNewsItem(ni);
00868     }
00869     ni = next;
00870   }
00871 }
00872 
00877 void DeleteIndustryNews(IndustryID iid)
00878 {
00879   NewsItem *ni = _oldest_news;
00880 
00881   while (ni != NULL) {
00882     NewsItem *next = ni->next;
00883     if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
00884       DeleteNewsItem(ni);
00885     }
00886     ni = next;
00887   }
00888 }
00889 
00893 void DeleteInvalidEngineNews()
00894 {
00895   NewsItem *ni = _oldest_news;
00896 
00897   while (ni != NULL) {
00898     NewsItem *next = ni->next;
00899     if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
00900         (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
00901       DeleteNewsItem(ni);
00902     }
00903     ni = next;
00904   }
00905 }
00906 
00907 static void RemoveOldNewsItems()
00908 {
00909   NewsItem *next;
00910   for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
00911     next = cur->next;
00912     if (_date - _news_type_data[_news_subtype_data[cur->subtype].type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
00913   }
00914 }
00915 
00922 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
00923 {
00924   for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
00925     if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
00926     if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
00927 
00928     /* Oh noes :(
00929      * Autoreplace is breaking the whole news-reference concept here, as we want to keep the news,
00930      * but do not know which DParams to change.
00931      *
00932      * Currently only NS_ADVICE news have vehicle IDs in their DParams.
00933      * And all NS_ADVICE news have the ID in param 0.
00934      */
00935     if (ni->subtype == NS_ADVICE && ni->params[0] == from_index) ni->params[0] = to_index;
00936   }
00937 }
00938 
00939 void NewsLoop()
00940 {
00941   /* no news item yet */
00942   if (_total_news == 0) return;
00943 
00944   /* There is no status bar, so no reason to show news;
00945    * especially important with the end game screen when
00946    * there is no status bar but possible news. */
00947   if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
00948 
00949   static byte _last_clean_month = 0;
00950 
00951   if (_last_clean_month != _cur_month) {
00952     RemoveOldNewsItems();
00953     _last_clean_month = _cur_month;
00954   }
00955 
00956   if (ReadyForNextItem()) MoveToNextItem();
00957 }
00958 
00960 static void ShowNewsMessage(const NewsItem *ni)
00961 {
00962   assert(_total_news != 0);
00963 
00964   /* Delete the news window */
00965   DeleteWindowById(WC_NEWS_WINDOW, 0);
00966 
00967   /* setup forced news item */
00968   _forced_news = ni;
00969 
00970   if (_forced_news != NULL) {
00971     DeleteWindowById(WC_NEWS_WINDOW, 0);
00972     ShowNewspaper(ni);
00973   }
00974 }
00975 
00977 void ShowLastNewsMessage()
00978 {
00979   if (_total_news == 0) {
00980     return;
00981   } else if (_forced_news == NULL) {
00982     /* Not forced any news yet, show the current one, unless a news window is
00983      * open (which can only be the current one), then show the previous item */
00984     const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
00985     ShowNewsMessage((w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev);
00986   } else if (_forced_news == _oldest_news) {
00987     /* We have reached the oldest news, start anew with the latest */
00988     ShowNewsMessage(_latest_news);
00989   } else {
00990     /* 'Scrolling' through news history show each one in turn */
00991     ShowNewsMessage(_forced_news->prev);
00992   }
00993 }
00994 
00995 
01006 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
01007 {
01008   char buffer[512], buffer2[512];
01009   StringID str;
01010 
01011   CopyInDParam(0, ni->params, lengthof(ni->params));
01012   str = ni->string_id;
01013 
01014   GetString(buffer, str, lastof(buffer));
01015   /* Copy the just gotten string to another buffer to remove any formatting
01016    * from it such as big fonts, etc. */
01017   const char *ptr = buffer;
01018   char *dest = buffer2;
01019   WChar c_last = '\0';
01020   for (;;) {
01021     WChar c = Utf8Consume(&ptr);
01022     if (c == 0) break;
01023     /* Make a space from a newline, but ignore multiple newlines */
01024     if (c == '\n' && c_last != '\n') {
01025       dest[0] = ' ';
01026       dest++;
01027     } else if (c == '\r') {
01028       dest[0] = dest[1] = dest[2] = dest[3] = ' ';
01029       dest += 4;
01030     } else if (IsPrintable(c)) {
01031       dest += Utf8Encode(dest, c);
01032     }
01033     c_last = c;
01034   }
01035 
01036   *dest = '\0';
01037   /* Truncate and show string; postfixed by '...' if neccessary */
01038   DrawString(left, right, y, buffer2, colour);
01039 }
01040 
01041 struct MessageHistoryWindow : Window {
01042   static const int top_spacing;    
01043   static const int bottom_spacing; 
01044 
01045   int line_height; 
01046   int date_width;  
01047 
01048   Scrollbar *vscroll;
01049 
01050   MessageHistoryWindow(const WindowDesc *desc) : Window()
01051   {
01052     this->CreateNestedTree(desc);
01053     this->vscroll = this->GetScrollbar(WID_MH_SCROLLBAR);
01054     this->FinishInitNested(desc); // Initializes 'this->line_height' and 'this->date_width'.
01055     this->OnInvalidateData(0);
01056   }
01057 
01058   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01059   {
01060     if (widget == WID_MH_BACKGROUND) {
01061       this->line_height = FONT_HEIGHT_NORMAL + 2;
01062       resize->height = this->line_height;
01063 
01064       /* Months are off-by-one, so it's actually 8. Not using
01065        * month 12 because the 1 is usually less wide. */
01066       SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 7, 30));
01067       this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
01068 
01069       size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
01070       size->width = max(200u, size->width); // At least 200 pixels wide.
01071     }
01072   }
01073 
01074   virtual void OnPaint()
01075   {
01076     this->OnInvalidateData(0);
01077     this->DrawWidgets();
01078   }
01079 
01080   virtual void DrawWidget(const Rect &r, int widget) const
01081   {
01082     if (widget != WID_MH_BACKGROUND || _total_news == 0) return;
01083 
01084     /* Find the first news item to display. */
01085     NewsItem *ni = _latest_news;
01086     for (int n = this->vscroll->GetPosition(); n > 0; n--) {
01087       ni = ni->prev;
01088       if (ni == NULL) return;
01089     }
01090 
01091     /* Fill the widget with news items. */
01092     int y = r.top + this->top_spacing;
01093     bool rtl = _current_text_dir == TD_RTL;
01094     uint date_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
01095     uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
01096     uint news_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
01097     uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
01098     for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
01099       SetDParam(0, ni->date);
01100       DrawString(date_left, date_right, y, STR_SHORT_DATE);
01101 
01102       DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
01103       y += this->line_height;
01104 
01105       ni = ni->prev;
01106       if (ni == NULL) return;
01107     }
01108   }
01109 
01115   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01116   {
01117     if (!gui_scope) return;
01118     this->vscroll->SetCount(_total_news);
01119   }
01120 
01121   virtual void OnClick(Point pt, int widget, int click_count)
01122   {
01123     if (widget == WID_MH_BACKGROUND) {
01124       NewsItem *ni = _latest_news;
01125       if (ni == NULL) return;
01126 
01127       for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_MH_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
01128         ni = ni->prev;
01129         if (ni == NULL) return;
01130       }
01131 
01132       ShowNewsMessage(ni);
01133     }
01134   }
01135 
01136   virtual void OnResize()
01137   {
01138     this->vscroll->SetCapacity(this->GetWidget<NWidgetBase>(WID_MH_BACKGROUND)->current_y / this->line_height);
01139   }
01140 };
01141 
01142 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
01143 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
01144 
01145 static const NWidgetPart _nested_message_history[] = {
01146   NWidget(NWID_HORIZONTAL),
01147     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01148     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01149     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01150     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01151   EndContainer(),
01152 
01153   NWidget(NWID_HORIZONTAL),
01154     NWidget(WWT_PANEL, COLOUR_BROWN, WID_MH_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR),
01155     EndContainer(),
01156     NWidget(NWID_VERTICAL),
01157       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_MH_SCROLLBAR),
01158       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01159     EndContainer(),
01160   EndContainer(),
01161 };
01162 
01163 static const WindowDesc _message_history_desc(
01164   WDP_AUTO, 400, 140,
01165   WC_MESSAGE_HISTORY, WC_NONE,
01166   WDF_UNCLICK_BUTTONS,
01167   _nested_message_history, lengthof(_nested_message_history)
01168 );
01169 
01171 void ShowMessageHistory()
01172 {
01173   DeleteWindowById(WC_MESSAGE_HISTORY, 0);
01174   new MessageHistoryWindow(&_message_history_desc);
01175 }
01176 
01177 struct MessageOptionsWindow : Window {
01178   static const StringID message_opt[]; 
01179   int state;                           
01180   Dimension dim_message_opt;           
01181 
01182   MessageOptionsWindow(const WindowDesc *desc) : Window()
01183   {
01184     this->InitNested(desc, WN_GAME_OPTIONS_MESSAGE_OPTION);
01185     /* Set up the initial disabled buttons in the case of 'off' or 'full' */
01186     NewsDisplay all_val = _news_type_data[0].display;
01187     for (int i = 0; i < NT_END; i++) {
01188       this->SetMessageButtonStates(_news_type_data[i].display, i);
01189       /* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
01190       if (_news_type_data[i].display != all_val) all_val = ND_OFF;
01191     }
01192     /* If all values are the same value, the ALL-button will take over this value */
01193     this->state = all_val;
01194     this->OnInvalidateData(0);
01195   }
01196 
01205   void SetMessageButtonStates(byte value, int element)
01206   {
01207     element *= MOS_WIDG_PER_SETTING;
01208 
01209     this->SetWidgetDisabledState(element + WID_MO_START_OPTION, value == 0);
01210     this->SetWidgetDisabledState(element + WID_MO_START_OPTION + 2, value == 2);
01211   }
01212 
01213   virtual void DrawWidget(const Rect &r, int widget) const
01214   {
01215     if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION && (widget -  WID_MO_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01216       /* Draw the string of each setting on each button. */
01217       int i = (widget -  WID_MO_START_OPTION) / MOS_WIDG_PER_SETTING;
01218       DrawString(r.left, r.right, r.top + 2, this->message_opt[_news_type_data[i].display], TC_BLACK, SA_HOR_CENTER);
01219     }
01220   }
01221 
01222   virtual void OnInit()
01223   {
01224     this->dim_message_opt.width  = 0;
01225     this->dim_message_opt.height = 0;
01226     for (const StringID *str = message_opt; *str != INVALID_STRING_ID; str++) this->dim_message_opt = maxdim(this->dim_message_opt, GetStringBoundingBox(*str));
01227   }
01228 
01229   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01230   {
01231     if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION) {
01232       /* Height is the biggest widget height in a row. */
01233       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
01234 
01235       /* Compute width for the label widget only. */
01236       if ((widget - WID_MO_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01237         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01238       }
01239       return;
01240     }
01241 
01242     /* Size computations for global message options. */
01243     if (widget == WID_MO_DROP_SUMMARY || widget == WID_MO_LABEL_SUMMARY || widget == WID_MO_SOUNDTICKER || widget == WID_MO_SOUNDTICKER_LABEL) {
01244       /* Height is the biggest widget height in a row. */
01245       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM);
01246 
01247       if (widget == WID_MO_DROP_SUMMARY) {
01248         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01249       } else if (widget == WID_MO_SOUNDTICKER) {
01250         size->width += MOS_BUTTON_SPACE; // A bit extra for better looks.
01251       }
01252       return;
01253     }
01254   }
01255 
01261   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01262   {
01263     if (!gui_scope) return;
01264     /* Update the dropdown value for 'set all categories'. */
01265     this->GetWidget<NWidgetCore>(WID_MO_DROP_SUMMARY)->widget_data = this->message_opt[this->state];
01266 
01267     /* Update widget to reflect the value of the #_news_ticker_sound variable. */
01268     this->SetWidgetLoweredState(WID_MO_SOUNDTICKER, _news_ticker_sound);
01269   }
01270 
01271   virtual void OnClick(Point pt, int widget, int click_count)
01272   {
01273     switch (widget) {
01274       case WID_MO_DROP_SUMMARY: // Dropdown menu for all settings
01275         ShowDropDownMenu(this, this->message_opt, this->state, WID_MO_DROP_SUMMARY, 0, 0);
01276         break;
01277 
01278       case WID_MO_SOUNDTICKER: // Change ticker sound on/off
01279         _news_ticker_sound ^= 1;
01280         this->InvalidateData();
01281         break;
01282 
01283       default: { // Clicked on the [<] .. [>] widgets
01284         if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION) {
01285           int wid = widget - WID_MO_START_OPTION;
01286           int element = wid / MOS_WIDG_PER_SETTING;
01287           byte val = (_news_type_data[element].display + ((wid % MOS_WIDG_PER_SETTING) ? 1 : -1)) % 3;
01288 
01289           this->SetMessageButtonStates(val, element);
01290           _news_type_data[element].display = (NewsDisplay)val;
01291           this->SetDirty();
01292         }
01293         break;
01294       }
01295     }
01296   }
01297 
01298   virtual void OnDropdownSelect(int widget, int index)
01299   {
01300     this->state = index;
01301 
01302     for (int i = 0; i < NT_END; i++) {
01303       this->SetMessageButtonStates(index, i);
01304       _news_type_data[i].display = (NewsDisplay)index;
01305     }
01306     this->InvalidateData();
01307   }
01308 };
01309 
01310 const StringID MessageOptionsWindow::message_opt[] = {STR_NEWS_MESSAGES_OFF, STR_NEWS_MESSAGES_SUMMARY, STR_NEWS_MESSAGES_FULL, INVALID_STRING_ID};
01311 
01313 static NWidgetBase *MakeButtonsColumn(int *biggest_index)
01314 {
01315   NWidgetVertical *vert_buttons = new NWidgetVertical;
01316 
01317   /* Top-part of the column, one row for each new category. */
01318   int widnum = WID_MO_START_OPTION;
01319   for (int i = 0; i < NT_END; i++) {
01320     NWidgetHorizontal *hor = new NWidgetHorizontal;
01321     /* [<] button. */
01322     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01323     leaf->SetFill(1, 1);
01324     hor->Add(leaf);
01325     /* Label. */
01326     leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_YELLOW, widnum + 1, STR_EMPTY, STR_NULL);
01327     leaf->SetFill(1, 1);
01328     hor->Add(leaf);
01329     /* [>] button. */
01330     leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 2, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01331     leaf->SetFill(1, 1);
01332     hor->Add(leaf);
01333     vert_buttons->Add(hor);
01334 
01335     widnum += MOS_WIDG_PER_SETTING;
01336   }
01337   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 2;
01338 
01339   /* Space between the category buttons and the global settings buttons. */
01340   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01341   vert_buttons->Add(spacer);
01342 
01343   /* Bottom part of the column with buttons for global changes. */
01344   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_DROPDOWN, COLOUR_YELLOW, WID_MO_DROP_SUMMARY, STR_EMPTY, STR_NULL);
01345   leaf->SetFill(1, 1);
01346   vert_buttons->Add(leaf);
01347 
01348   leaf = new NWidgetLeaf(WWT_TEXTBTN_2, COLOUR_YELLOW, WID_MO_SOUNDTICKER, STR_STATION_BUILD_COVERAGE_OFF, STR_NULL);
01349   leaf->SetFill(1, 1);
01350   vert_buttons->Add(leaf);
01351 
01352   *biggest_index = max(*biggest_index, max<int>(WID_MO_DROP_SUMMARY, WID_MO_SOUNDTICKER));
01353   return vert_buttons;
01354 }
01355 
01357 static NWidgetBase *MakeDescriptionColumn(int *biggest_index)
01358 {
01359   NWidgetVertical *vert_desc = new NWidgetVertical;
01360 
01361   /* Top-part of the column, one row for each new category. */
01362   int widnum = WID_MO_START_OPTION;
01363   for (int i = 0; i < NT_END; i++) {
01364     NWidgetHorizontal *hor = new NWidgetHorizontal;
01365 
01366     /* Descriptive text. */
01367     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 3, _news_type_data[i].description, STR_NULL);
01368     hor->Add(leaf);
01369     /* Filling empty space to push text to the left. */
01370     NWidgetSpacer *spacer = new NWidgetSpacer(0, 0);
01371     spacer->SetFill(1, 0);
01372     hor->Add(spacer);
01373     vert_desc->Add(hor);
01374 
01375     widnum += MOS_WIDG_PER_SETTING;
01376   }
01377   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 3;
01378 
01379   /* Space between the category descriptions and the global settings descriptions. */
01380   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01381   vert_desc->Add(spacer);
01382 
01383   /* Bottom part of the column with descriptions of global changes. */
01384   NWidgetHorizontal *hor = new NWidgetHorizontal;
01385   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WID_MO_LABEL_SUMMARY, STR_NEWS_MESSAGES_ALL, STR_NULL);
01386   hor->Add(leaf);
01387   /* Filling empty space to push text to the left. */
01388   spacer = new NWidgetSpacer(0, 0);
01389   spacer->SetFill(1, 0);
01390   hor->Add(spacer);
01391   vert_desc->Add(hor);
01392 
01393   hor = new NWidgetHorizontal;
01394   leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WID_MO_SOUNDTICKER_LABEL, STR_NEWS_MESSAGES_SOUND, STR_NULL);
01395   hor->Add(leaf);
01396   /* Filling empty space to push text to the left. */
01397   spacer = new NWidgetSpacer(0, 0);
01398   leaf->SetFill(1, 0);
01399   hor->Add(spacer);
01400   vert_desc->Add(hor);
01401 
01402   *biggest_index = max(*biggest_index, max<int>(WID_MO_LABEL_SUMMARY, WID_MO_SOUNDTICKER_LABEL));
01403   return vert_desc;
01404 }
01405 
01406 static const NWidgetPart _nested_message_options_widgets[] = {
01407   NWidget(NWID_HORIZONTAL),
01408     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01409     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_NEWS_MESSAGE_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01410   EndContainer(),
01411   NWidget(WWT_PANEL, COLOUR_BROWN, WID_MO_BACKGROUND),
01412     NWidget(NWID_HORIZONTAL),
01413       NWidget(NWID_SPACER), SetFill(1, 0),
01414       NWidget(WWT_LABEL, COLOUR_BROWN, WID_MO_LABEL), SetMinimalSize(0, 14), SetDataTip(STR_NEWS_MESSAGE_TYPES, STR_NULL),
01415       NWidget(NWID_SPACER), SetFill(1, 0),
01416     EndContainer(),
01417     NWidget(NWID_HORIZONTAL),
01418       NWidget(NWID_SPACER), SetMinimalSize(MOS_LEFT_EDGE, 0),
01419       NWidgetFunction(MakeButtonsColumn),
01420       NWidget(NWID_SPACER), SetMinimalSize(MOS_COLUMN_SPACING, 0),
01421       NWidgetFunction(MakeDescriptionColumn),
01422       NWidget(NWID_SPACER), SetMinimalSize(MOS_RIGHT_EDGE, 0),
01423     EndContainer(),
01424     NWidget(NWID_SPACER), SetMinimalSize(0, MOS_BOTTOM_EDGE),
01425   EndContainer(),
01426 };
01427 
01428 static const WindowDesc _message_options_desc(
01429   WDP_AUTO, 0, 0,
01430   WC_GAME_OPTIONS, WC_NONE,
01431   WDF_UNCLICK_BUTTONS,
01432   _nested_message_options_widgets, lengthof(_nested_message_options_widgets)
01433 );
01434 
01438 void ShowMessageOptions()
01439 {
01440   DeleteWindowByClass(WC_GAME_OPTIONS);
01441   new MessageOptionsWindow(&_message_options_desc);
01442 }