news_gui.cpp

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

Generated on Fri Mar 4 21:37:02 2011 for OpenTTD by  doxygen 1.6.1