news_gui.cpp

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

Generated on Sun Jan 9 16:01:58 2011 for OpenTTD by  doxygen 1.6.1