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 
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, PC_WHITE);
00339 
00340     GfxFillRect(r.left,  r.top,    r.left,  r.bottom, PC_BLACK);
00341     GfxFillRect(r.right, r.top,    r.right, r.bottom, PC_BLACK);
00342     GfxFillRect(r.left,  r.top,    r.right, r.top,    PC_BLACK);
00343     GfxFillRect(r.left,  r.bottom, r.right, r.bottom, PC_BLACK);
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_NEWSPAPER, 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, PC_GREY);
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_NEWSPAPER, 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 
00506   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00507   {
00508     if (!gui_scope) return;
00509     /* The chatbar has notified us that is was either created or closed */
00510     int newtop = this->top + this->chat_height - data;
00511     this->chat_height = data;
00512     this->SetWindowTop(newtop);
00513   }
00514 
00515   virtual void OnTick()
00516   {
00517     /* Scroll up newsmessages from the bottom in steps of 4 pixels */
00518     int newtop = max(this->top - 4, _screen.height - this->height - this->status_height - this->chat_height);
00519     this->SetWindowTop(newtop);
00520   }
00521 
00522 private:
00527   void SetWindowTop(int newtop)
00528   {
00529     if (this->top == newtop) return;
00530 
00531     int mintop = min(newtop, this->top);
00532     int maxtop = max(newtop, this->top);
00533     if (this->viewport != NULL) this->viewport->top += newtop - this->top;
00534     this->top = newtop;
00535 
00536     SetDirtyBlocks(this->left, mintop, this->left + this->width, maxtop + this->height);
00537   }
00538 
00539   StringID GetCompanyMessageString() const
00540   {
00541     switch (this->ni->subtype) {
00542       case NS_COMPANY_TROUBLE:
00543         SetDParam(0, this->ni->params[2]);
00544         return STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION;
00545 
00546       case NS_COMPANY_MERGER:
00547         SetDParam(0, this->ni->params[2]);
00548         SetDParam(1, this->ni->params[3]);
00549         SetDParam(2, this->ni->params[4]);
00550         return this->ni->params[4] == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION;
00551 
00552       case NS_COMPANY_BANKRUPT:
00553         SetDParam(0, this->ni->params[2]);
00554         return STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION;
00555 
00556       case NS_COMPANY_NEW:
00557         SetDParam(0, this->ni->params[2]);
00558         SetDParam(1, this->ni->params[3]);
00559         return STR_NEWS_COMPANY_LAUNCH_DESCRIPTION;
00560 
00561       default:
00562         NOT_REACHED();
00563     }
00564   }
00565 
00566   StringID GetNewVehicleMessageString(int widget) const
00567   {
00568     assert(this->ni->reftype1 == NR_ENGINE);
00569     EngineID engine = this->ni->ref1;
00570 
00571     switch (widget) {
00572       case NTW_VEH_TITLE:
00573         SetDParam(0, GetEngineCategoryName(engine));
00574         return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
00575 
00576       case NTW_VEH_NAME:
00577         SetDParam(0, engine);
00578         return STR_NEWS_NEW_VEHICLE_TYPE;
00579 
00580       default:
00581         NOT_REACHED();
00582     }
00583   }
00584 };
00585 
00586 /* static */ uint NewsWindow::duration = 0; // Instance creation.
00587 
00588 
00590 static void ShowNewspaper(const NewsItem *ni)
00591 {
00592   SoundFx sound = _news_type_data[_news_subtype_data[ni->subtype].type].sound;
00593   if (sound != 0) SndPlayFx(sound);
00594 
00595   new NewsWindow(_news_subtype_data[ni->subtype].desc, ni);
00596 }
00597 
00599 static void ShowTicker(const NewsItem *ni)
00600 {
00601   if (_news_ticker_sound) SndPlayFx(SND_16_MORSE);
00602 
00603   _statusbar_news_item = ni;
00604   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
00605 }
00606 
00608 void InitNewsItemStructs()
00609 {
00610   for (NewsItem *ni = _oldest_news; ni != NULL; ) {
00611     NewsItem *next = ni->next;
00612     delete ni;
00613     ni = next;
00614   }
00615 
00616   _total_news = 0;
00617   _oldest_news = NULL;
00618   _latest_news = NULL;
00619   _forced_news = NULL;
00620   _current_news = NULL;
00621   _statusbar_news_item = NULL;
00622   NewsWindow::duration = 0;
00623 }
00624 
00629 static bool ReadyForNextItem()
00630 {
00631   const NewsItem *ni = _forced_news == NULL ? _current_news : _forced_news;
00632   if (ni == NULL) return true;
00633 
00634   /* Ticker message
00635    * Check if the status bar message is still being displayed? */
00636   if (IsNewsTickerShown()) return false;
00637 
00638   /* Newspaper message, decrement duration counter */
00639   if (NewsWindow::duration != 0) NewsWindow::duration--;
00640 
00641   /* neither newsticker nor newspaper are running */
00642   return (NewsWindow::duration == 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
00643 }
00644 
00646 static void MoveToNextItem()
00647 {
00648   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
00649   DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
00650   _forced_news = NULL;
00651   _statusbar_news_item = NULL;
00652 
00653   /* if we're not at the last item, then move on */
00654   if (_current_news != _latest_news) {
00655     _current_news = (_current_news == NULL) ? _oldest_news : _current_news->next;
00656     const NewsItem *ni = _current_news;
00657     const NewsType type = _news_subtype_data[ni->subtype].type;
00658 
00659     /* check the date, don't show too old items */
00660     if (_date - _news_type_data[type].age > ni->date) return;
00661 
00662     switch (_news_type_data[type].display) {
00663       default: NOT_REACHED();
00664       case ND_OFF: // Off - show nothing only a small reminder in the status bar
00665         InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
00666         break;
00667 
00668       case ND_SUMMARY: // Summary - show ticker
00669         ShowTicker(ni);
00670         break;
00671 
00672       case ND_FULL: // Full - show newspaper
00673         ShowNewspaper(ni);
00674         break;
00675     }
00676   }
00677 }
00678 
00691 void AddNewsItem(StringID string, NewsSubtype subtype, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
00692 {
00693   if (_game_mode == GM_MENU) return;
00694 
00695   /* Create new news item node */
00696   NewsItem *ni = new NewsItem;
00697 
00698   ni->string_id = string;
00699   ni->subtype = subtype;
00700   ni->flags = _news_subtype_data[subtype].flags;
00701 
00702   /* show this news message in colour? */
00703   if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR;
00704 
00705   ni->reftype1 = reftype1;
00706   ni->reftype2 = reftype2;
00707   ni->ref1 = ref1;
00708   ni->ref2 = ref2;
00709   ni->free_data = free_data;
00710   ni->date = _date;
00711   CopyOutDParam(ni->params, 0, lengthof(ni->params));
00712 
00713   if (_total_news++ == 0) {
00714     assert(_oldest_news == NULL);
00715     _oldest_news = ni;
00716     ni->prev = NULL;
00717   } else {
00718     assert(_latest_news->next == NULL);
00719     _latest_news->next = ni;
00720     ni->prev = _latest_news;
00721   }
00722 
00723   ni->next = NULL;
00724   _latest_news = ni;
00725 
00726   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
00727 }
00728 
00730 static void DeleteNewsItem(NewsItem *ni)
00731 {
00732   /* Delete the news from the news queue. */
00733   if (ni->prev != NULL) {
00734     ni->prev->next = ni->next;
00735   } else {
00736     assert(_oldest_news == ni);
00737     _oldest_news = ni->next;
00738   }
00739 
00740   if (ni->next != NULL) {
00741     ni->next->prev = ni->prev;
00742   } else {
00743     assert(_latest_news == ni);
00744     _latest_news = ni->prev;
00745   }
00746 
00747   _total_news--;
00748 
00749   if (_forced_news == ni || _current_news == ni || _statusbar_news_item == ni) {
00750     /* When we're the current news, go to the previous item first;
00751      * we just possibly made that the last news item. */
00752     if (_current_news == ni) _current_news = ni->prev;
00753 
00754     /* About to remove the currently forced item (shown as newspapers) ||
00755      * about to remove the currently displayed item (newspapers, ticker, or just a reminder) */
00756     MoveToNextItem();
00757   }
00758 
00759   delete ni;
00760 
00761   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
00762 }
00763 
00770 void DeleteVehicleNews(VehicleID vid, StringID news)
00771 {
00772   NewsItem *ni = _oldest_news;
00773 
00774   while (ni != NULL) {
00775     NewsItem *next = ni->next;
00776     if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
00777         (news == INVALID_STRING_ID || ni->string_id == news)) {
00778       DeleteNewsItem(ni);
00779     }
00780     ni = next;
00781   }
00782 }
00783 
00789 void DeleteStationNews(StationID sid)
00790 {
00791   NewsItem *ni = _oldest_news;
00792 
00793   while (ni != NULL) {
00794     NewsItem *next = ni->next;
00795     if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
00796       DeleteNewsItem(ni);
00797     }
00798     ni = next;
00799   }
00800 }
00801 
00806 void DeleteIndustryNews(IndustryID iid)
00807 {
00808   NewsItem *ni = _oldest_news;
00809 
00810   while (ni != NULL) {
00811     NewsItem *next = ni->next;
00812     if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
00813       DeleteNewsItem(ni);
00814     }
00815     ni = next;
00816   }
00817 }
00818 
00822 void DeleteInvalidEngineNews()
00823 {
00824   NewsItem *ni = _oldest_news;
00825 
00826   while (ni != NULL) {
00827     NewsItem *next = ni->next;
00828     if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
00829         (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
00830       DeleteNewsItem(ni);
00831     }
00832     ni = next;
00833   }
00834 }
00835 
00836 static void RemoveOldNewsItems()
00837 {
00838   NewsItem *next;
00839   for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
00840     next = cur->next;
00841     if (_date - _news_type_data[_news_subtype_data[cur->subtype].type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
00842   }
00843 }
00844 
00851 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
00852 {
00853   for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
00854     if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
00855     if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
00856 
00857     /* Oh noes :(
00858      * Autoreplace is breaking the whole news-reference concept here, as we want to keep the news,
00859      * but do not know which DParams to change.
00860      *
00861      * Currently only NS_ADVICE news have vehicle IDs in their DParams.
00862      * And all NS_ADVICE news have the ID in param 0.
00863      */
00864     if (ni->subtype == NS_ADVICE && ni->params[0] == from_index) ni->params[0] = to_index;
00865   }
00866 }
00867 
00868 void NewsLoop()
00869 {
00870   /* no news item yet */
00871   if (_total_news == 0) return;
00872 
00873   /* There is no status bar, so no reason to show news;
00874    * especially important with the end game screen when
00875    * there is no status bar but possible news. */
00876   if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
00877 
00878   static byte _last_clean_month = 0;
00879 
00880   if (_last_clean_month != _cur_month) {
00881     RemoveOldNewsItems();
00882     _last_clean_month = _cur_month;
00883   }
00884 
00885   if (ReadyForNextItem()) MoveToNextItem();
00886 }
00887 
00889 static void ShowNewsMessage(const NewsItem *ni)
00890 {
00891   assert(_total_news != 0);
00892 
00893   /* Delete the news window */
00894   DeleteWindowById(WC_NEWS_WINDOW, 0);
00895 
00896   /* setup forced news item */
00897   _forced_news = ni;
00898 
00899   if (_forced_news != NULL) {
00900     DeleteWindowById(WC_NEWS_WINDOW, 0);
00901     ShowNewspaper(ni);
00902   }
00903 }
00904 
00906 void ShowLastNewsMessage()
00907 {
00908   if (_total_news == 0) {
00909     return;
00910   } else if (_forced_news == NULL) {
00911     /* Not forced any news yet, show the current one, unless a news window is
00912      * open (which can only be the current one), then show the previous item */
00913     const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
00914     ShowNewsMessage((w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev);
00915   } else if (_forced_news == _oldest_news) {
00916     /* We have reached the oldest news, start anew with the latest */
00917     ShowNewsMessage(_latest_news);
00918   } else {
00919     /* 'Scrolling' through news history show each one in turn */
00920     ShowNewsMessage(_forced_news->prev);
00921   }
00922 }
00923 
00924 
00935 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
00936 {
00937   char buffer[512], buffer2[512];
00938   StringID str;
00939 
00940   CopyInDParam(0, ni->params, lengthof(ni->params));
00941   str = ni->string_id;
00942 
00943   GetString(buffer, str, lastof(buffer));
00944   /* Copy the just gotten string to another buffer to remove any formatting
00945    * from it such as big fonts, etc. */
00946   const char *ptr = buffer;
00947   char *dest = buffer2;
00948   WChar c_last = '\0';
00949   for (;;) {
00950     WChar c = Utf8Consume(&ptr);
00951     if (c == 0) break;
00952     /* Make a space from a newline, but ignore multiple newlines */
00953     if (c == '\n' && c_last != '\n') {
00954       dest[0] = ' ';
00955       dest++;
00956     } else if (c == '\r') {
00957       dest[0] = dest[1] = dest[2] = dest[3] = ' ';
00958       dest += 4;
00959     } else if (IsPrintable(c)) {
00960       dest += Utf8Encode(dest, c);
00961     }
00962     c_last = c;
00963   }
00964 
00965   *dest = '\0';
00966   /* Truncate and show string; postfixed by '...' if neccessary */
00967   DrawString(left, right, y, buffer2, colour);
00968 }
00969 
00971 enum MessageHistoryWidgets {
00972   MHW_STICKYBOX,
00973   MHW_BACKGROUND,
00974   MHW_SCROLLBAR,
00975 };
00976 
00977 struct MessageHistoryWindow : Window {
00978   static const int top_spacing;    
00979   static const int bottom_spacing; 
00980 
00981   int line_height; 
00982   int date_width;  
00983 
00984   Scrollbar *vscroll;
00985 
00986   MessageHistoryWindow(const WindowDesc *desc) : Window()
00987   {
00988     this->CreateNestedTree(desc);
00989     this->vscroll = this->GetScrollbar(MHW_SCROLLBAR);
00990     this->FinishInitNested(desc); // Initializes 'this->line_height' and 'this->date_width'.
00991     this->OnInvalidateData(0);
00992   }
00993 
00994   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00995   {
00996     if (widget == MHW_BACKGROUND) {
00997       this->line_height = FONT_HEIGHT_NORMAL + 2;
00998       resize->height = this->line_height;
00999 
01000       SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 12, 30));
01001       this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
01002 
01003       size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
01004       size->width = max(200u, size->width); // At least 200 pixels wide.
01005     }
01006   }
01007 
01008   virtual void OnPaint()
01009   {
01010     this->OnInvalidateData(0);
01011     this->DrawWidgets();
01012   }
01013 
01014   virtual void DrawWidget(const Rect &r, int widget) const
01015   {
01016     if (widget != MHW_BACKGROUND || _total_news == 0) return;
01017 
01018     /* Find the first news item to display. */
01019     NewsItem *ni = _latest_news;
01020     for (int n = this->vscroll->GetPosition(); n > 0; n--) {
01021       ni = ni->prev;
01022       if (ni == NULL) return;
01023     }
01024 
01025     /* Fill the widget with news items. */
01026     int y = r.top + this->top_spacing;
01027     bool rtl = _current_text_dir == TD_RTL;
01028     uint date_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
01029     uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
01030     uint news_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
01031     uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
01032     for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
01033       SetDParam(0, ni->date);
01034       DrawString(date_left, date_right, y, STR_SHORT_DATE);
01035 
01036       DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
01037       y += this->line_height;
01038 
01039       ni = ni->prev;
01040       if (ni == NULL) return;
01041     }
01042   }
01043 
01049   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01050   {
01051     if (!gui_scope) return;
01052     this->vscroll->SetCount(_total_news);
01053   }
01054 
01055   virtual void OnClick(Point pt, int widget, int click_count)
01056   {
01057     if (widget == MHW_BACKGROUND) {
01058       NewsItem *ni = _latest_news;
01059       if (ni == NULL) return;
01060 
01061       for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, MHW_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
01062         ni = ni->prev;
01063         if (ni == NULL) return;
01064       }
01065 
01066       ShowNewsMessage(ni);
01067     }
01068   }
01069 
01070   virtual void OnResize()
01071   {
01072     this->vscroll->SetCapacity(this->GetWidget<NWidgetBase>(MHW_BACKGROUND)->current_y / this->line_height);
01073   }
01074 };
01075 
01076 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
01077 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
01078 
01079 static const NWidgetPart _nested_message_history[] = {
01080   NWidget(NWID_HORIZONTAL),
01081     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01082     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01083     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01084     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01085   EndContainer(),
01086 
01087   NWidget(NWID_HORIZONTAL),
01088     NWidget(WWT_PANEL, COLOUR_BROWN, MHW_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(MHW_SCROLLBAR),
01089     EndContainer(),
01090     NWidget(NWID_VERTICAL),
01091       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, MHW_SCROLLBAR),
01092       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01093     EndContainer(),
01094   EndContainer(),
01095 };
01096 
01097 static const WindowDesc _message_history_desc(
01098   WDP_AUTO, 400, 140,
01099   WC_MESSAGE_HISTORY, WC_NONE,
01100   WDF_UNCLICK_BUTTONS,
01101   _nested_message_history, lengthof(_nested_message_history)
01102 );
01103 
01105 void ShowMessageHistory()
01106 {
01107   DeleteWindowById(WC_MESSAGE_HISTORY, 0);
01108   new MessageHistoryWindow(&_message_history_desc);
01109 }
01110 
01112 enum MessageOptionsSpace {
01113   MOS_WIDG_PER_SETTING      = 4,  
01114 
01115   MOS_LEFT_EDGE             = 6,  
01116   MOS_COLUMN_SPACING        = 4,  
01117   MOS_RIGHT_EDGE            = 6,  
01118   MOS_BUTTON_SPACE          = 10, 
01119 
01120   MOS_ABOVE_GLOBAL_SETTINGS = 6,  
01121   MOS_BOTTOM_EDGE           = 6,  
01122 };
01123 
01125 enum MessageOptionWidgets {
01126   WIDGET_NEWSOPT_BACKGROUND,        
01127   WIDGET_NEWSOPT_LABEL,             
01128   WIDGET_NEWSOPT_DROP_SUMMARY,      
01129   WIDGET_NEWSOPT_LABEL_SUMMARY,     
01130   WIDGET_NEWSOPT_SOUNDTICKER,       
01131   WIDGET_NEWSOPT_SOUNDTICKER_LABEL, 
01132 
01133   WIDGET_NEWSOPT_START_OPTION,      
01134   WIDGET_NEWSOPT_END_OPTION = WIDGET_NEWSOPT_START_OPTION + NT_END * MOS_WIDG_PER_SETTING, 
01135 };
01136 
01137 struct MessageOptionsWindow : Window {
01138   static const StringID message_opt[]; 
01139   int state;                           
01140   Dimension dim_message_opt;           
01141 
01142   MessageOptionsWindow(const WindowDesc *desc) : Window()
01143   {
01144     this->InitNested(desc);
01145     /* Set up the initial disabled buttons in the case of 'off' or 'full' */
01146     NewsDisplay all_val = _news_type_data[0].display;
01147     for (int i = 0; i < NT_END; i++) {
01148       this->SetMessageButtonStates(_news_type_data[i].display, i);
01149       /* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
01150       if (_news_type_data[i].display != all_val) all_val = ND_OFF;
01151     }
01152     /* If all values are the same value, the ALL-button will take over this value */
01153     this->state = all_val;
01154     this->OnInvalidateData(0);
01155   }
01156 
01165   void SetMessageButtonStates(byte value, int element)
01166   {
01167     element *= MOS_WIDG_PER_SETTING;
01168 
01169     this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION, value == 0);
01170     this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION + 2, value == 2);
01171   }
01172 
01173   virtual void DrawWidget(const Rect &r, int widget) const
01174   {
01175     if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION && (widget -  WIDGET_NEWSOPT_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01176       /* Draw the string of each setting on each button. */
01177       int i = (widget -  WIDGET_NEWSOPT_START_OPTION) / MOS_WIDG_PER_SETTING;
01178       DrawString(r.left, r.right, r.top + 2, this->message_opt[_news_type_data[i].display], TC_BLACK, SA_HOR_CENTER);
01179     }
01180   }
01181 
01182   virtual void OnInit()
01183   {
01184     this->dim_message_opt.width  = 0;
01185     this->dim_message_opt.height = 0;
01186     for (const StringID *str = message_opt; *str != INVALID_STRING_ID; str++) this->dim_message_opt = maxdim(this->dim_message_opt, GetStringBoundingBox(*str));
01187   }
01188 
01189   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01190   {
01191     if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION) {
01192       /* Height is the biggest widget height in a row. */
01193       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
01194 
01195       /* Compute width for the label widget only. */
01196       if ((widget - WIDGET_NEWSOPT_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01197         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01198       }
01199       return;
01200     }
01201 
01202     /* Size computations for global message options. */
01203     if (widget == WIDGET_NEWSOPT_DROP_SUMMARY || widget == WIDGET_NEWSOPT_LABEL_SUMMARY || widget == WIDGET_NEWSOPT_SOUNDTICKER || widget == WIDGET_NEWSOPT_SOUNDTICKER_LABEL) {
01204       /* Height is the biggest widget height in a row. */
01205       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM);
01206 
01207       if (widget == WIDGET_NEWSOPT_DROP_SUMMARY) {
01208         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01209       } else if (widget == WIDGET_NEWSOPT_SOUNDTICKER) {
01210         size->width += MOS_BUTTON_SPACE; // A bit extra for better looks.
01211       }
01212       return;
01213     }
01214   }
01215 
01221   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01222   {
01223     if (!gui_scope) return;
01224     /* Update the dropdown value for 'set all categories'. */
01225     this->GetWidget<NWidgetCore>(WIDGET_NEWSOPT_DROP_SUMMARY)->widget_data = this->message_opt[this->state];
01226 
01227     /* Update widget to reflect the value of the #_news_ticker_sound variable. */
01228     this->SetWidgetLoweredState(WIDGET_NEWSOPT_SOUNDTICKER, _news_ticker_sound);
01229   }
01230 
01231   virtual void OnClick(Point pt, int widget, int click_count)
01232   {
01233     switch (widget) {
01234       case WIDGET_NEWSOPT_DROP_SUMMARY: // Dropdown menu for all settings
01235         ShowDropDownMenu(this, this->message_opt, this->state, WIDGET_NEWSOPT_DROP_SUMMARY, 0, 0);
01236         break;
01237 
01238       case WIDGET_NEWSOPT_SOUNDTICKER: // Change ticker sound on/off
01239         _news_ticker_sound ^= 1;
01240         this->InvalidateData();
01241         break;
01242 
01243       default: { // Clicked on the [<] .. [>] widgets
01244         if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION) {
01245           int wid = widget - WIDGET_NEWSOPT_START_OPTION;
01246           int element = wid / MOS_WIDG_PER_SETTING;
01247           byte val = (_news_type_data[element].display + ((wid % MOS_WIDG_PER_SETTING) ? 1 : -1)) % 3;
01248 
01249           this->SetMessageButtonStates(val, element);
01250           _news_type_data[element].display = (NewsDisplay)val;
01251           this->SetDirty();
01252         }
01253         break;
01254       }
01255     }
01256   }
01257 
01258   virtual void OnDropdownSelect(int widget, int index)
01259   {
01260     this->state = index;
01261 
01262     for (int i = 0; i < NT_END; i++) {
01263       this->SetMessageButtonStates(index, i);
01264       _news_type_data[i].display = (NewsDisplay)index;
01265     }
01266     this->InvalidateData();
01267   }
01268 };
01269 
01270 const StringID MessageOptionsWindow::message_opt[] = {STR_NEWS_MESSAGES_OFF, STR_NEWS_MESSAGES_SUMMARY, STR_NEWS_MESSAGES_FULL, INVALID_STRING_ID};
01271 
01273 static NWidgetBase *MakeButtonsColumn(int *biggest_index)
01274 {
01275   NWidgetVertical *vert_buttons = new NWidgetVertical;
01276 
01277   /* Top-part of the column, one row for each new category. */
01278   int widnum = WIDGET_NEWSOPT_START_OPTION;
01279   for (int i = 0; i < NT_END; i++) {
01280     NWidgetHorizontal *hor = new NWidgetHorizontal;
01281     /* [<] button. */
01282     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01283     leaf->SetFill(1, 1);
01284     hor->Add(leaf);
01285     /* Label. */
01286     leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_YELLOW, widnum + 1, STR_EMPTY, STR_NULL);
01287     leaf->SetFill(1, 1);
01288     hor->Add(leaf);
01289     /* [>] button. */
01290     leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 2, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01291     leaf->SetFill(1, 1);
01292     hor->Add(leaf);
01293     vert_buttons->Add(hor);
01294 
01295     widnum += MOS_WIDG_PER_SETTING;
01296   }
01297   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 2;
01298 
01299   /* Space between the category buttons and the global settings buttons. */
01300   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01301   vert_buttons->Add(spacer);
01302 
01303   /* Bottom part of the column with buttons for global changes. */
01304   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_DROPDOWN, COLOUR_YELLOW, WIDGET_NEWSOPT_DROP_SUMMARY, STR_EMPTY, STR_NULL);
01305   leaf->SetFill(1, 1);
01306   vert_buttons->Add(leaf);
01307 
01308   leaf = new NWidgetLeaf(WWT_TEXTBTN_2, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER, STR_STATION_BUILD_COVERAGE_OFF, STR_NULL);
01309   leaf->SetFill(1, 1);
01310   vert_buttons->Add(leaf);
01311 
01312   *biggest_index = max(*biggest_index, max<int>(WIDGET_NEWSOPT_DROP_SUMMARY, WIDGET_NEWSOPT_SOUNDTICKER));
01313   return vert_buttons;
01314 }
01315 
01317 static NWidgetBase *MakeDescriptionColumn(int *biggest_index)
01318 {
01319   NWidgetVertical *vert_desc = new NWidgetVertical;
01320 
01321   /* Top-part of the column, one row for each new category. */
01322   int widnum = WIDGET_NEWSOPT_START_OPTION;
01323   for (int i = 0; i < NT_END; i++) {
01324     NWidgetHorizontal *hor = new NWidgetHorizontal;
01325 
01326     /* Descriptive text. */
01327     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 3, _news_type_data[i].description, STR_NULL);
01328     hor->Add(leaf);
01329     /* Filling empty space to push text to the left. */
01330     NWidgetSpacer *spacer = new NWidgetSpacer(0, 0);
01331     spacer->SetFill(1, 0);
01332     hor->Add(spacer);
01333     vert_desc->Add(hor);
01334 
01335     widnum += MOS_WIDG_PER_SETTING;
01336   }
01337   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 3;
01338 
01339   /* Space between the category descriptions and the global settings descriptions. */
01340   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01341   vert_desc->Add(spacer);
01342 
01343   /* Bottom part of the column with descriptions of global changes. */
01344   NWidgetHorizontal *hor = new NWidgetHorizontal;
01345   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_LABEL_SUMMARY, STR_NEWS_MESSAGES_ALL, STR_NULL);
01346   hor->Add(leaf);
01347   /* Filling empty space to push text to the left. */
01348   spacer = new NWidgetSpacer(0, 0);
01349   spacer->SetFill(1, 0);
01350   hor->Add(spacer);
01351   vert_desc->Add(hor);
01352 
01353   hor = new NWidgetHorizontal;
01354   leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER_LABEL, STR_NEWS_MESSAGES_SOUND, STR_NULL);
01355   hor->Add(leaf);
01356   /* Filling empty space to push text to the left. */
01357   spacer = new NWidgetSpacer(0, 0);
01358   leaf->SetFill(1, 0);
01359   hor->Add(spacer);
01360   vert_desc->Add(hor);
01361 
01362   *biggest_index = max(*biggest_index, max<int>(WIDGET_NEWSOPT_LABEL_SUMMARY, WIDGET_NEWSOPT_SOUNDTICKER_LABEL));
01363   return vert_desc;
01364 }
01365 
01366 static const NWidgetPart _nested_message_options_widgets[] = {
01367   NWidget(NWID_HORIZONTAL),
01368     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01369     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_NEWS_MESSAGE_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01370   EndContainer(),
01371   NWidget(WWT_PANEL, COLOUR_BROWN, WIDGET_NEWSOPT_BACKGROUND),
01372     NWidget(NWID_HORIZONTAL),
01373       NWidget(NWID_SPACER), SetFill(1, 0),
01374       NWidget(WWT_LABEL, COLOUR_BROWN, WIDGET_NEWSOPT_LABEL), SetMinimalSize(0, 14), SetDataTip(STR_NEWS_MESSAGE_TYPES, STR_NULL),
01375       NWidget(NWID_SPACER), SetFill(1, 0),
01376     EndContainer(),
01377     NWidget(NWID_HORIZONTAL),
01378       NWidget(NWID_SPACER), SetMinimalSize(MOS_LEFT_EDGE, 0),
01379       NWidgetFunction(MakeButtonsColumn),
01380       NWidget(NWID_SPACER), SetMinimalSize(MOS_COLUMN_SPACING, 0),
01381       NWidgetFunction(MakeDescriptionColumn),
01382       NWidget(NWID_SPACER), SetMinimalSize(MOS_RIGHT_EDGE, 0),
01383     EndContainer(),
01384     NWidget(NWID_SPACER), SetMinimalSize(0, MOS_BOTTOM_EDGE),
01385   EndContainer(),
01386 };
01387 
01388 static const WindowDesc _message_options_desc(
01389   WDP_AUTO, 0, 0,
01390   WC_GAME_OPTIONS, WC_NONE,
01391   WDF_UNCLICK_BUTTONS,
01392   _nested_message_options_widgets, lengthof(_nested_message_options_widgets)
01393 );
01394 
01398 void ShowMessageOptions()
01399 {
01400   DeleteWindowById(WC_GAME_OPTIONS, 0);
01401   new MessageOptionsWindow(&_message_options_desc);
01402 }

Generated on Sun Jun 5 04:20:00 2011 for OpenTTD by  doxygen 1.6.1