news_gui.cpp

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

Generated on Sun Nov 14 14:41:53 2010 for OpenTTD by  doxygen 1.6.1