news_gui.cpp

Go to the documentation of this file.
00001 /* $Id: news_gui.cpp 19928 2010-06-04 21:14:22Z 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 */
00197   NWidget(NWID_HORIZONTAL),
00198     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE, NTW_CLOSEBOX),
00199     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, NTW_CAPTION), SetDataTip(STR_NEWS_MESSAGE_CAPTION, STR_NULL),
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_PANEL:
00398         this->DrawNewsBorder(r);
00399         return;
00400 
00401       case NTW_MESSAGE:
00402         CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
00403         DrawStringMultiLine(r.left + 2, r.right - 2, r.top, r.bottom, this->ni->string_id, TC_FROMSTRING, SA_CENTER);
00404         break;
00405 
00406       case NTW_MGR_FACE: {
00407         const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
00408         DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top);
00409         GfxFillRect(r.left + 1, r.top, r.left + 1 + 91, r.top + 118, PALETTE_TO_STRUCT_GREY, FILLRECT_RECOLOUR);
00410         break;
00411       }
00412       case NTW_MGR_NAME: {
00413         const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
00414         SetDParamStr(0, cni->president_name);
00415         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
00416         break;
00417       }
00418       case NTW_COMPANY_MSG:
00419         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetCompanyMessageString(), TC_FROMSTRING, SA_CENTER);
00420         break;
00421 
00422       case NTW_VEH_BKGND:
00423         GfxFillRect(r.left, r.top, r.right, r.bottom, 10);
00424         break;
00425 
00426       case NTW_VEH_NAME:
00427       case NTW_VEH_TITLE:
00428         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetNewVehicleMessageString(widget), TC_FROMSTRING, SA_CENTER);
00429         break;
00430 
00431       case NTW_VEH_SPR: {
00432         assert(this->ni->reftype1 == NR_ENGINE);
00433         EngineID engine = this->ni->ref1;
00434         DrawVehicleEngine(r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette(engine, _local_company));
00435         GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_TO_STRUCT_GREY, FILLRECT_RECOLOUR);
00436         break;
00437       }
00438       case NTW_VEH_INFO: {
00439         assert(this->ni->reftype1 == NR_ENGINE);
00440         EngineID engine = this->ni->ref1;
00441         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
00442         break;
00443       }
00444     }
00445   }
00446 
00447   virtual void OnClick(Point pt, int widget, int click_count)
00448   {
00449     switch (widget) {
00450       case NTW_CLOSEBOX:
00451         NewsWindow::duration = 0;
00452         delete this;
00453         _forced_news = NULL;
00454         break;
00455 
00456       case NTW_CAPTION:
00457       case NTW_VIEWPORT:
00458         break; // Ignore clicks
00459 
00460       default:
00461         if (this->ni->reftype1 == NR_VEHICLE) {
00462           const Vehicle *v = Vehicle::Get(this->ni->ref1);
00463           ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
00464         } else {
00465           TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
00466           TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
00467           if (_ctrl_pressed) {
00468             if (tile1 != INVALID_TILE) ShowExtraViewPortWindow(tile1);
00469             if (tile2 != INVALID_TILE) ShowExtraViewPortWindow(tile2);
00470           } else {
00471             if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
00472               ScrollMainWindowToTile(tile2);
00473             }
00474           }
00475         }
00476         break;
00477     }
00478   }
00479 
00480   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00481   {
00482     if (keycode == WKC_SPACE) {
00483       /* Don't continue. */
00484       delete this;
00485       return ES_HANDLED;
00486     }
00487     return ES_NOT_HANDLED;
00488   }
00489 
00490   virtual void OnInvalidateData(int data)
00491   {
00492     /* The chatbar has notified us that is was either created or closed */
00493     int newtop = this->top + this->chat_height - data;
00494     this->chat_height = data;
00495     this->SetWindowTop(newtop);
00496   }
00497 
00498   virtual void OnTick()
00499   {
00500     /* Scroll up newsmessages from the bottom in steps of 4 pixels */
00501     int newtop = max(this->top - 4, _screen.height - this->height - this->status_height - this->chat_height);
00502     this->SetWindowTop(newtop);
00503   }
00504 
00505 private:
00510   void SetWindowTop(int newtop)
00511   {
00512     if (this->top == newtop) return;
00513 
00514     int mintop = min(newtop, this->top);
00515     int maxtop = max(newtop, this->top);
00516     if (this->viewport != NULL) this->viewport->top += newtop - this->top;
00517     this->top = newtop;
00518 
00519     SetDirtyBlocks(this->left, mintop, this->left + this->width, maxtop + this->height);
00520   }
00521 
00522   StringID GetCompanyMessageString() const
00523   {
00524     switch (this->ni->subtype) {
00525       case NS_COMPANY_TROUBLE:
00526         SetDParam(0, this->ni->params[2]);
00527         return STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION;
00528 
00529       case NS_COMPANY_MERGER:
00530         SetDParam(0, this->ni->params[2]);
00531         SetDParam(1, this->ni->params[3]);
00532         SetDParam(2, this->ni->params[4]);
00533         return this->ni->params[4] == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION;
00534 
00535       case NS_COMPANY_BANKRUPT:
00536         SetDParam(0, this->ni->params[2]);
00537         return STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION;
00538 
00539       case NS_COMPANY_NEW:
00540         SetDParam(0, this->ni->params[2]);
00541         SetDParam(1, this->ni->params[3]);
00542         return STR_NEWS_COMPANY_LAUNCH_DESCRIPTION;
00543 
00544       default:
00545         NOT_REACHED();
00546     }
00547   }
00548 
00549   StringID GetNewVehicleMessageString(int widget) const
00550   {
00551     assert(this->ni->reftype1 == NR_ENGINE);
00552     EngineID engine = this->ni->ref1;
00553 
00554     switch (widget) {
00555       case NTW_VEH_TITLE:
00556         SetDParam(0, GetEngineCategoryName(engine));
00557         return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
00558 
00559       case NTW_VEH_NAME:
00560         SetDParam(0, engine);
00561         return STR_NEWS_NEW_VEHICLE_TYPE;
00562 
00563       default:
00564         NOT_REACHED();
00565     }
00566   }
00567 };
00568 
00569 /* static */ uint NewsWindow::duration = 0; // Instance creation.
00570 
00571 
00573 static void ShowNewspaper(const NewsItem *ni)
00574 {
00575   SoundFx sound = _news_type_data[_news_subtype_data[ni->subtype].type].sound;
00576   if (sound != 0) SndPlayFx(sound);
00577 
00578   new NewsWindow(_news_subtype_data[ni->subtype].desc, ni);
00579 }
00580 
00582 static void ShowTicker(const NewsItem *ni)
00583 {
00584   if (_news_ticker_sound) SndPlayFx(SND_16_MORSE);
00585 
00586   _statusbar_news_item = ni;
00587   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
00588 }
00589 
00591 void InitNewsItemStructs()
00592 {
00593   for (NewsItem *ni = _oldest_news; ni != NULL; ) {
00594     NewsItem *next = ni->next;
00595     delete ni;
00596     ni = next;
00597   }
00598 
00599   _total_news = 0;
00600   _oldest_news = NULL;
00601   _latest_news = NULL;
00602   _forced_news = NULL;
00603   _current_news = NULL;
00604   _statusbar_news_item = NULL;
00605   NewsWindow::duration = 0;
00606 }
00607 
00612 static bool ReadyForNextItem()
00613 {
00614   const NewsItem *ni = _forced_news == NULL ? _current_news : _forced_news;
00615   if (ni == NULL) return true;
00616 
00617   /* Ticker message
00618    * Check if the status bar message is still being displayed? */
00619   if (IsNewsTickerShown()) return false;
00620 
00621   /* Newspaper message, decrement duration counter */
00622   if (NewsWindow::duration != 0) NewsWindow::duration--;
00623 
00624   /* neither newsticker nor newspaper are running */
00625   return (NewsWindow::duration == 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
00626 }
00627 
00629 static void MoveToNextItem()
00630 {
00631   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
00632   DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
00633   _forced_news = NULL;
00634   _statusbar_news_item = NULL;
00635 
00636   /* if we're not at the last item, then move on */
00637   if (_current_news != _latest_news) {
00638     _current_news = (_current_news == NULL) ? _oldest_news : _current_news->next;
00639     const NewsItem *ni = _current_news;
00640     const NewsType type = _news_subtype_data[ni->subtype].type;
00641 
00642     /* check the date, don't show too old items */
00643     if (_date - _news_type_data[type].age > ni->date) return;
00644 
00645     switch (_news_type_data[type].display) {
00646       default: NOT_REACHED();
00647       case ND_OFF: // Off - show nothing only a small reminder in the status bar
00648         InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
00649         break;
00650 
00651       case ND_SUMMARY: // Summary - show ticker
00652         ShowTicker(ni);
00653         break;
00654 
00655       case ND_FULL: // Full - show newspaper
00656         ShowNewspaper(ni);
00657         break;
00658     }
00659   }
00660 }
00661 
00674 void AddNewsItem(StringID string, NewsSubtype subtype, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
00675 {
00676   if (_game_mode == GM_MENU) return;
00677 
00678   /* Create new news item node */
00679   NewsItem *ni = new NewsItem;
00680 
00681   ni->string_id = string;
00682   ni->subtype = subtype;
00683   ni->flags = _news_subtype_data[subtype].flags;
00684 
00685   /* show this news message in colour? */
00686   if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR;
00687 
00688   ni->reftype1 = reftype1;
00689   ni->reftype2 = reftype2;
00690   ni->ref1 = ref1;
00691   ni->ref2 = ref2;
00692   ni->free_data = free_data;
00693   ni->date = _date;
00694   CopyOutDParam(ni->params, 0, lengthof(ni->params));
00695 
00696   if (_total_news++ == 0) {
00697     assert(_oldest_news == NULL);
00698     _oldest_news = ni;
00699     ni->prev = NULL;
00700   } else {
00701     assert(_latest_news->next == NULL);
00702     _latest_news->next = ni;
00703     ni->prev = _latest_news;
00704   }
00705 
00706   ni->next = NULL;
00707   _latest_news = ni;
00708 
00709   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
00710 }
00711 
00713 static void DeleteNewsItem(NewsItem *ni)
00714 {
00715   if (_forced_news == ni || _current_news == ni) {
00716     /* about to remove the currently forced item (shown as newspapers) ||
00717      * about to remove the currently displayed item (newspapers, ticker, or just a reminder) */
00718     MoveToNextItem();
00719   }
00720 
00721   /* delete item */
00722 
00723   if (ni->prev != NULL) {
00724     ni->prev->next = ni->next;
00725   } else {
00726     assert(_oldest_news == ni);
00727     _oldest_news = ni->next;
00728   }
00729 
00730   if (ni->next != NULL) {
00731     ni->next->prev = ni->prev;
00732   } else {
00733     assert(_latest_news == ni);
00734     _latest_news = ni->prev;
00735   }
00736 
00737   if (_current_news == ni) _current_news = ni->prev;
00738   _total_news--;
00739   delete ni;
00740 
00741   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
00742 }
00743 
00744 void DeleteVehicleNews(VehicleID vid, StringID news)
00745 {
00746   NewsItem *ni = _oldest_news;
00747 
00748   while (ni != NULL) {
00749     NewsItem *next = ni->next;
00750     if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
00751         (news == INVALID_STRING_ID || ni->string_id == news)) {
00752       DeleteNewsItem(ni);
00753     }
00754     ni = next;
00755   }
00756 }
00757 
00762 void DeleteStationNews(StationID sid)
00763 {
00764   NewsItem *ni = _oldest_news;
00765 
00766   while (ni != NULL) {
00767     NewsItem *next = ni->next;
00768     if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
00769       DeleteNewsItem(ni);
00770     }
00771     ni = next;
00772   }
00773 }
00774 
00778 void DeleteIndustryNews(IndustryID iid)
00779 {
00780   NewsItem *ni = _oldest_news;
00781 
00782   while (ni != NULL) {
00783     NewsItem *next = ni->next;
00784     if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
00785       DeleteNewsItem(ni);
00786     }
00787     ni = next;
00788   }
00789 }
00790 
00791 static void RemoveOldNewsItems()
00792 {
00793   NewsItem *next;
00794   for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
00795     next = cur->next;
00796     if (_date - _news_type_data[_news_subtype_data[cur->subtype].type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
00797   }
00798 }
00799 
00806 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
00807 {
00808   for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
00809     if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
00810     if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
00811 
00812     /* Oh noes :(
00813      * Autoreplace is breaking the whole news-reference concept here, as we want to keep the news,
00814      * but do not know which DParams to change.
00815      *
00816      * Currently only NS_ADVICE news have vehicle IDs in their DParams.
00817      * And all NS_ADVICE news have the ID in param 0.
00818      */
00819     if (ni->subtype == NS_ADVICE && ni->params[0] == from_index) ni->params[0] = to_index;
00820   }
00821 }
00822 
00823 void NewsLoop()
00824 {
00825   /* no news item yet */
00826   if (_total_news == 0) return;
00827 
00828   /* There is no status bar, so no reason to show news;
00829    * especially important with the end game screen when
00830    * there is no status bar but possible news. */
00831   if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
00832 
00833   static byte _last_clean_month = 0;
00834 
00835   if (_last_clean_month != _cur_month) {
00836     RemoveOldNewsItems();
00837     _last_clean_month = _cur_month;
00838   }
00839 
00840   if (ReadyForNextItem()) MoveToNextItem();
00841 }
00842 
00844 static void ShowNewsMessage(const NewsItem *ni)
00845 {
00846   assert(_total_news != 0);
00847 
00848   /* Delete the news window */
00849   DeleteWindowById(WC_NEWS_WINDOW, 0);
00850 
00851   /* setup forced news item */
00852   _forced_news = ni;
00853 
00854   if (_forced_news != NULL) {
00855     DeleteWindowById(WC_NEWS_WINDOW, 0);
00856     ShowNewspaper(ni);
00857   }
00858 }
00859 
00861 void ShowLastNewsMessage()
00862 {
00863   if (_total_news == 0) {
00864     return;
00865   } else if (_forced_news == NULL) {
00866     /* Not forced any news yet, show the current one, unless a news window is
00867      * open (which can only be the current one), then show the previous item */
00868     const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
00869     ShowNewsMessage((w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev);
00870   } else if (_forced_news == _oldest_news) {
00871     /* We have reached the oldest news, start anew with the latest */
00872     ShowNewsMessage(_latest_news);
00873   } else {
00874     /* 'Scrolling' through news history show each one in turn */
00875     ShowNewsMessage(_forced_news->prev);
00876   }
00877 }
00878 
00879 
00890 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
00891 {
00892   char buffer[512], buffer2[512];
00893   StringID str;
00894 
00895   CopyInDParam(0, ni->params, lengthof(ni->params));
00896   str = ni->string_id;
00897 
00898   GetString(buffer, str, lastof(buffer));
00899   /* Copy the just gotten string to another buffer to remove any formatting
00900    * from it such as big fonts, etc. */
00901   const char *ptr = buffer;
00902   char *dest = buffer2;
00903   WChar c_last = '\0';
00904   for (;;) {
00905     WChar c = Utf8Consume(&ptr);
00906     if (c == 0) break;
00907     /* Make a space from a newline, but ignore multiple newlines */
00908     if (c == '\n' && c_last != '\n') {
00909       dest[0] = ' ';
00910       dest++;
00911     } else if (c == '\r') {
00912       dest[0] = dest[1] = dest[2] = dest[3] = ' ';
00913       dest += 4;
00914     } else if (IsPrintable(c)) {
00915       dest += Utf8Encode(dest, c);
00916     }
00917     c_last = c;
00918   }
00919 
00920   *dest = '\0';
00921   /* Truncate and show string; postfixed by '...' if neccessary */
00922   DrawString(left, right, y, buffer2, colour);
00923 }
00924 
00926 enum MessageHistoryWidgets {
00927   MHW_STICKYBOX,
00928   MHW_BACKGROUND,
00929   MHW_SCROLLBAR,
00930 };
00931 
00932 struct MessageHistoryWindow : Window {
00933   static const int top_spacing;    
00934   static const int bottom_spacing; 
00935 
00936   int line_height; 
00937   int date_width;  
00938 
00939   MessageHistoryWindow(const WindowDesc *desc) : Window()
00940   {
00941     this->InitNested(desc); // Initializes 'this->line_height' and 'this->date_width'.
00942     this->OnInvalidateData(0);
00943   }
00944 
00945   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00946   {
00947     if (widget == MHW_BACKGROUND) {
00948       this->line_height = FONT_HEIGHT_NORMAL + 2;
00949       resize->height = this->line_height;
00950 
00951       SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 12, 30));
00952       this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
00953 
00954       size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
00955       size->width = max(200u, size->width); // At least 200 pixels wide.
00956     }
00957   }
00958 
00959   virtual void OnPaint()
00960   {
00961     this->OnInvalidateData(0);
00962     this->DrawWidgets();
00963   }
00964 
00965   virtual void DrawWidget(const Rect &r, int widget) const
00966   {
00967     if (widget != MHW_BACKGROUND || _total_news == 0) return;
00968 
00969     /* Find the first news item to display. */
00970     NewsItem *ni = _latest_news;
00971     for (int n = this->vscroll.GetPosition(); n > 0; n--) {
00972       ni = ni->prev;
00973       if (ni == NULL) return;
00974     }
00975 
00976     /* Fill the widget with news items. */
00977     int y = r.top + this->top_spacing;
00978     bool rtl = _dynlang.text_dir == TD_RTL;
00979     uint date_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
00980     uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
00981     uint news_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
00982     uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
00983     for (int n = this->vscroll.GetCapacity(); n > 0; n--) {
00984       SetDParam(0, ni->date);
00985       DrawString(date_left, date_right, y, STR_SHORT_DATE);
00986 
00987       DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
00988       y += this->line_height;
00989 
00990       ni = ni->prev;
00991       if (ni == NULL) return;
00992     }
00993   }
00994 
00995   virtual void OnInvalidateData(int data)
00996   {
00997     this->vscroll.SetCount(_total_news);
00998   }
00999 
01000   virtual void OnClick(Point pt, int widget, int click_count)
01001   {
01002     if (widget == MHW_BACKGROUND) {
01003       NewsItem *ni = _latest_news;
01004       if (ni == NULL) return;
01005 
01006       for (int n = (pt.y - this->GetWidget<NWidgetBase>(MHW_BACKGROUND)->pos_y - WD_FRAMERECT_TOP) / this->line_height + this->vscroll.GetPosition(); n > 0; n--) {
01007         ni = ni->prev;
01008         if (ni == NULL) return;
01009       }
01010 
01011       ShowNewsMessage(ni);
01012     }
01013   }
01014 
01015   virtual void OnResize()
01016   {
01017     this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(MHW_BACKGROUND)->current_y / this->line_height);
01018   }
01019 };
01020 
01021 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
01022 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
01023 
01024 static const NWidgetPart _nested_message_history[] = {
01025   NWidget(NWID_HORIZONTAL),
01026     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01027     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01028     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01029     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01030   EndContainer(),
01031 
01032   NWidget(NWID_HORIZONTAL),
01033     NWidget(WWT_PANEL, COLOUR_BROWN, MHW_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12),
01034     EndContainer(),
01035     NWidget(NWID_VERTICAL),
01036       NWidget(WWT_SCROLLBAR, COLOUR_BROWN, MHW_SCROLLBAR),
01037       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01038     EndContainer(),
01039   EndContainer(),
01040 };
01041 
01042 static const WindowDesc _message_history_desc(
01043   WDP_AUTO, 400, 140,
01044   WC_MESSAGE_HISTORY, WC_NONE,
01045   WDF_UNCLICK_BUTTONS,
01046   _nested_message_history, lengthof(_nested_message_history)
01047 );
01048 
01050 void ShowMessageHistory()
01051 {
01052   DeleteWindowById(WC_MESSAGE_HISTORY, 0);
01053   new MessageHistoryWindow(&_message_history_desc);
01054 }
01055 
01057 enum MessageOptionsSpace {
01058   MOS_WIDG_PER_SETTING      = 4,  
01059 
01060   MOS_LEFT_EDGE             = 6,  
01061   MOS_COLUMN_SPACING        = 4,  
01062   MOS_RIGHT_EDGE            = 6,  
01063   MOS_BUTTON_SPACE          = 10, 
01064 
01065   MOS_ABOVE_GLOBAL_SETTINGS = 6,  
01066   MOS_BOTTOM_EDGE           = 6,  
01067 };
01068 
01070 enum MessageOptionWidgets {
01071   WIDGET_NEWSOPT_BACKGROUND,        
01072   WIDGET_NEWSOPT_LABEL,             
01073   WIDGET_NEWSOPT_DROP_SUMMARY,      
01074   WIDGET_NEWSOPT_LABEL_SUMMARY,     
01075   WIDGET_NEWSOPT_SOUNDTICKER,       
01076   WIDGET_NEWSOPT_SOUNDTICKER_LABEL, 
01077 
01078   WIDGET_NEWSOPT_START_OPTION,      
01079   WIDGET_NEWSOPT_END_OPTION = WIDGET_NEWSOPT_START_OPTION + NT_END * MOS_WIDG_PER_SETTING, 
01080 };
01081 
01082 struct MessageOptionsWindow : Window {
01083   static const StringID message_opt[]; 
01084   int state;                           
01085   Dimension dim_message_opt;           
01086 
01087   MessageOptionsWindow(const WindowDesc *desc) : Window()
01088   {
01089     this->InitNested(desc);
01090     /* Set up the initial disabled buttons in the case of 'off' or 'full' */
01091     NewsDisplay all_val = _news_type_data[0].display;
01092     for (int i = 0; i < NT_END; i++) {
01093       this->SetMessageButtonStates(_news_type_data[i].display, i);
01094       /* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
01095       if (_news_type_data[i].display != all_val) all_val = ND_OFF;
01096     }
01097     /* If all values are the same value, the ALL-button will take over this value */
01098     this->state = all_val;
01099     this->OnInvalidateData(0);
01100   }
01101 
01110   void SetMessageButtonStates(byte value, int element)
01111   {
01112     element *= MOS_WIDG_PER_SETTING;
01113 
01114     this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION, value == 0);
01115     this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION + 2, value == 2);
01116   }
01117 
01118   virtual void OnPaint()
01119   {
01120     this->DrawWidgets();
01121   }
01122 
01123   virtual void DrawWidget(const Rect &r, int widget) const
01124   {
01125     if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION && (widget -  WIDGET_NEWSOPT_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01126       /* Draw the string of each setting on each button. */
01127       int i = (widget -  WIDGET_NEWSOPT_START_OPTION) / MOS_WIDG_PER_SETTING;
01128       DrawString(r.left, r.right, r.top + 2, this->message_opt[_news_type_data[i].display], TC_BLACK, SA_CENTER);
01129     }
01130   }
01131 
01132   virtual void OnInit()
01133   {
01134     this->dim_message_opt.width  = 0;
01135     this->dim_message_opt.height = 0;
01136     for (const StringID *str = message_opt; *str != INVALID_STRING_ID; str++) this->dim_message_opt = maxdim(this->dim_message_opt, GetStringBoundingBox(*str));
01137   }
01138 
01139   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01140   {
01141     if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION) {
01142       /* Height is the biggest widget height in a row. */
01143       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
01144 
01145       /* Compute width for the label widget only. */
01146       if ((widget - WIDGET_NEWSOPT_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01147         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01148       }
01149       return;
01150     }
01151 
01152     /* Size computations for global message options. */
01153     if (widget == WIDGET_NEWSOPT_DROP_SUMMARY || widget == WIDGET_NEWSOPT_LABEL_SUMMARY || widget == WIDGET_NEWSOPT_SOUNDTICKER || widget == WIDGET_NEWSOPT_SOUNDTICKER_LABEL) {
01154       /* Height is the biggest widget height in a row. */
01155       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM);
01156 
01157       if (widget == WIDGET_NEWSOPT_DROP_SUMMARY) {
01158         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01159       } else if (widget == WIDGET_NEWSOPT_SOUNDTICKER) {
01160         size->width += MOS_BUTTON_SPACE; // A bit extra for better looks.
01161       }
01162       return;
01163     }
01164   }
01165 
01166   virtual void OnInvalidateData(int data)
01167   {
01168     /* Update the dropdown value for 'set all categories'. */
01169     this->GetWidget<NWidgetCore>(WIDGET_NEWSOPT_DROP_SUMMARY)->widget_data = this->message_opt[this->state];
01170 
01171     /* Update widget to reflect the value of the #_news_ticker_sound variable. */
01172     this->SetWidgetLoweredState(WIDGET_NEWSOPT_SOUNDTICKER, _news_ticker_sound);
01173   }
01174 
01175   virtual void OnClick(Point pt, int widget, int click_count)
01176   {
01177     switch (widget) {
01178       case WIDGET_NEWSOPT_DROP_SUMMARY: // Dropdown menu for all settings
01179         ShowDropDownMenu(this, this->message_opt, this->state, WIDGET_NEWSOPT_DROP_SUMMARY, 0, 0);
01180         break;
01181 
01182       case WIDGET_NEWSOPT_SOUNDTICKER: // Change ticker sound on/off
01183         _news_ticker_sound ^= 1;
01184         this->InvalidateData();
01185         break;
01186 
01187       default: { // Clicked on the [<] .. [>] widgets
01188         if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION) {
01189           int wid = widget - WIDGET_NEWSOPT_START_OPTION;
01190           int element = wid / MOS_WIDG_PER_SETTING;
01191           byte val = (_news_type_data[element].display + ((wid % MOS_WIDG_PER_SETTING) ? 1 : -1)) % 3;
01192 
01193           this->SetMessageButtonStates(val, element);
01194           _news_type_data[element].display = (NewsDisplay)val;
01195           this->SetDirty();
01196         }
01197         break;
01198       }
01199     }
01200   }
01201 
01202   virtual void OnDropdownSelect(int widget, int index)
01203   {
01204     this->state = index;
01205 
01206     for (int i = 0; i < NT_END; i++) {
01207       this->SetMessageButtonStates(index, i);
01208       _news_type_data[i].display = (NewsDisplay)index;
01209     }
01210     this->InvalidateData();
01211   }
01212 };
01213 
01214 const StringID MessageOptionsWindow::message_opt[] = {STR_NEWS_MESSAGES_OFF, STR_NEWS_MESSAGES_SUMMARY, STR_NEWS_MESSAGES_FULL, INVALID_STRING_ID};
01215 
01217 static NWidgetBase *MakeButtonsColumn(int *biggest_index)
01218 {
01219   NWidgetVertical *vert_buttons = new NWidgetVertical;
01220 
01221   /* Top-part of the column, one row for each new category. */
01222   int widnum = WIDGET_NEWSOPT_START_OPTION;
01223   for (int i = 0; i < NT_END; i++) {
01224     NWidgetHorizontal *hor = new NWidgetHorizontal;
01225     /* [<] button. */
01226     NWidgetLeaf *leaf = new NWidgetLeaf(NWID_BUTTON_ARROW, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01227     leaf->SetFill(1, 1);
01228     hor->Add(leaf);
01229     /* Label. */
01230     leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_YELLOW, widnum + 1, STR_EMPTY, STR_NULL);
01231     leaf->SetFill(1, 1);
01232     hor->Add(leaf);
01233     /* [>] button. */
01234     leaf = new NWidgetLeaf(NWID_BUTTON_ARROW, COLOUR_YELLOW, widnum + 2, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01235     leaf->SetFill(1, 1);
01236     hor->Add(leaf);
01237     vert_buttons->Add(hor);
01238 
01239     widnum += MOS_WIDG_PER_SETTING;
01240   }
01241   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 2;
01242 
01243   /* Space between the category buttons and the global settings buttons. */
01244   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01245   vert_buttons->Add(spacer);
01246 
01247   /* Bottom part of the column with buttons for global changes. */
01248   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_DROPDOWN, COLOUR_YELLOW, WIDGET_NEWSOPT_DROP_SUMMARY, STR_EMPTY, STR_NULL);
01249   leaf->SetFill(1, 1);
01250   vert_buttons->Add(leaf);
01251 
01252   leaf = new NWidgetLeaf(WWT_TEXTBTN_2, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER, STR_STATION_BUILD_COVERAGE_OFF, STR_NULL);
01253   leaf->SetFill(1, 1);
01254   vert_buttons->Add(leaf);
01255 
01256   *biggest_index = max(*biggest_index, max<int>(WIDGET_NEWSOPT_DROP_SUMMARY, WIDGET_NEWSOPT_SOUNDTICKER));
01257   return vert_buttons;
01258 }
01259 
01261 static NWidgetBase *MakeDescriptionColumn(int *biggest_index)
01262 {
01263   NWidgetVertical *vert_desc = new NWidgetVertical;
01264 
01265   /* Top-part of the column, one row for each new category. */
01266   int widnum = WIDGET_NEWSOPT_START_OPTION;
01267   for (int i = 0; i < NT_END; i++) {
01268     NWidgetHorizontal *hor = new NWidgetHorizontal;
01269 
01270     /* Descriptive text. */
01271     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 3, _news_type_data[i].description, STR_NULL);
01272     hor->Add(leaf);
01273     /* Filling empty space to push text to the left. */
01274     NWidgetSpacer *spacer = new NWidgetSpacer(0, 0);
01275     spacer->SetFill(1, 0);
01276     hor->Add(spacer);
01277     vert_desc->Add(hor);
01278 
01279     widnum += MOS_WIDG_PER_SETTING;
01280   }
01281   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 3;
01282 
01283   /* Space between the category descriptions and the global settings descriptions. */
01284   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01285   vert_desc->Add(spacer);
01286 
01287   /* Bottom part of the column with descriptions of global changes. */
01288   NWidgetHorizontal *hor = new NWidgetHorizontal;
01289   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_LABEL_SUMMARY, STR_NEWS_MESSAGES_ALL, STR_NULL);
01290   hor->Add(leaf);
01291   /* Filling empty space to push text to the left. */
01292   spacer = new NWidgetSpacer(0, 0);
01293   spacer->SetFill(1, 0);
01294   hor->Add(spacer);
01295   vert_desc->Add(hor);
01296 
01297   hor = new NWidgetHorizontal;
01298   leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER_LABEL, STR_NEWS_MESSAGES_SOUND, STR_NULL);
01299   hor->Add(leaf);
01300   /* Filling empty space to push text to the left. */
01301   spacer = new NWidgetSpacer(0, 0);
01302   leaf->SetFill(1, 0);
01303   hor->Add(spacer);
01304   vert_desc->Add(hor);
01305 
01306   *biggest_index = max(*biggest_index, max<int>(WIDGET_NEWSOPT_LABEL_SUMMARY, WIDGET_NEWSOPT_SOUNDTICKER_LABEL));
01307   return vert_desc;
01308 }
01309 
01310 static const NWidgetPart _nested_message_options_widgets[] = {
01311   NWidget(NWID_HORIZONTAL),
01312     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01313     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_NEWS_MESSAGE_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01314   EndContainer(),
01315   NWidget(WWT_PANEL, COLOUR_BROWN, WIDGET_NEWSOPT_BACKGROUND),
01316     NWidget(NWID_HORIZONTAL),
01317       NWidget(NWID_SPACER), SetFill(1, 0),
01318       NWidget(WWT_LABEL, COLOUR_BROWN, WIDGET_NEWSOPT_LABEL), SetMinimalSize(0, 14), SetDataTip(STR_NEWS_MESSAGE_TYPES, STR_NULL),
01319       NWidget(NWID_SPACER), SetFill(1, 0),
01320     EndContainer(),
01321     NWidget(NWID_HORIZONTAL),
01322       NWidget(NWID_SPACER), SetMinimalSize(MOS_LEFT_EDGE, 0),
01323       NWidgetFunction(MakeButtonsColumn),
01324       NWidget(NWID_SPACER), SetMinimalSize(MOS_COLUMN_SPACING, 0),
01325       NWidgetFunction(MakeDescriptionColumn),
01326       NWidget(NWID_SPACER), SetMinimalSize(MOS_RIGHT_EDGE, 0),
01327     EndContainer(),
01328     NWidget(NWID_SPACER), SetMinimalSize(0, MOS_BOTTOM_EDGE),
01329   EndContainer(),
01330 };
01331 
01332 static const WindowDesc _message_options_desc(
01333   WDP_AUTO, 0, 0,
01334   WC_GAME_OPTIONS, WC_NONE,
01335   WDF_UNCLICK_BUTTONS,
01336   _nested_message_options_widgets, lengthof(_nested_message_options_widgets)
01337 );
01338 
01339 void ShowMessageOptions()
01340 {
01341   DeleteWindowById(WC_GAME_OPTIONS, 0);
01342   new MessageOptionsWindow(&_message_options_desc);
01343 }

Generated on Sat Jun 19 17:14:51 2010 for OpenTTD by  doxygen 1.6.1