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