news_gui.cpp

Go to the documentation of this file.
00001 /* $Id: news_gui.cpp 21886 2011-01-22 09:53:15Z 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 
00763 void DeleteVehicleNews(VehicleID vid, StringID news)
00764 {
00765   NewsItem *ni = _oldest_news;
00766 
00767   while (ni != NULL) {
00768     NewsItem *next = ni->next;
00769     if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
00770         (news == INVALID_STRING_ID || ni->string_id == news)) {
00771       DeleteNewsItem(ni);
00772     }
00773     ni = next;
00774   }
00775 }
00776 
00782 void DeleteStationNews(StationID sid)
00783 {
00784   NewsItem *ni = _oldest_news;
00785 
00786   while (ni != NULL) {
00787     NewsItem *next = ni->next;
00788     if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
00789       DeleteNewsItem(ni);
00790     }
00791     ni = next;
00792   }
00793 }
00794 
00799 void DeleteIndustryNews(IndustryID iid)
00800 {
00801   NewsItem *ni = _oldest_news;
00802 
00803   while (ni != NULL) {
00804     NewsItem *next = ni->next;
00805     if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
00806       DeleteNewsItem(ni);
00807     }
00808     ni = next;
00809   }
00810 }
00811 
00812 static void RemoveOldNewsItems()
00813 {
00814   NewsItem *next;
00815   for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
00816     next = cur->next;
00817     if (_date - _news_type_data[_news_subtype_data[cur->subtype].type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
00818   }
00819 }
00820 
00827 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
00828 {
00829   for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
00830     if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
00831     if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
00832 
00833     /* Oh noes :(
00834      * Autoreplace is breaking the whole news-reference concept here, as we want to keep the news,
00835      * but do not know which DParams to change.
00836      *
00837      * Currently only NS_ADVICE news have vehicle IDs in their DParams.
00838      * And all NS_ADVICE news have the ID in param 0.
00839      */
00840     if (ni->subtype == NS_ADVICE && ni->params[0] == from_index) ni->params[0] = to_index;
00841   }
00842 }
00843 
00844 void NewsLoop()
00845 {
00846   /* no news item yet */
00847   if (_total_news == 0) return;
00848 
00849   /* There is no status bar, so no reason to show news;
00850    * especially important with the end game screen when
00851    * there is no status bar but possible news. */
00852   if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
00853 
00854   static byte _last_clean_month = 0;
00855 
00856   if (_last_clean_month != _cur_month) {
00857     RemoveOldNewsItems();
00858     _last_clean_month = _cur_month;
00859   }
00860 
00861   if (ReadyForNextItem()) MoveToNextItem();
00862 }
00863 
00865 static void ShowNewsMessage(const NewsItem *ni)
00866 {
00867   assert(_total_news != 0);
00868 
00869   /* Delete the news window */
00870   DeleteWindowById(WC_NEWS_WINDOW, 0);
00871 
00872   /* setup forced news item */
00873   _forced_news = ni;
00874 
00875   if (_forced_news != NULL) {
00876     DeleteWindowById(WC_NEWS_WINDOW, 0);
00877     ShowNewspaper(ni);
00878   }
00879 }
00880 
00882 void ShowLastNewsMessage()
00883 {
00884   if (_total_news == 0) {
00885     return;
00886   } else if (_forced_news == NULL) {
00887     /* Not forced any news yet, show the current one, unless a news window is
00888      * open (which can only be the current one), then show the previous item */
00889     const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
00890     ShowNewsMessage((w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev);
00891   } else if (_forced_news == _oldest_news) {
00892     /* We have reached the oldest news, start anew with the latest */
00893     ShowNewsMessage(_latest_news);
00894   } else {
00895     /* 'Scrolling' through news history show each one in turn */
00896     ShowNewsMessage(_forced_news->prev);
00897   }
00898 }
00899 
00900 
00911 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
00912 {
00913   char buffer[512], buffer2[512];
00914   StringID str;
00915 
00916   CopyInDParam(0, ni->params, lengthof(ni->params));
00917   str = ni->string_id;
00918 
00919   GetString(buffer, str, lastof(buffer));
00920   /* Copy the just gotten string to another buffer to remove any formatting
00921    * from it such as big fonts, etc. */
00922   const char *ptr = buffer;
00923   char *dest = buffer2;
00924   WChar c_last = '\0';
00925   for (;;) {
00926     WChar c = Utf8Consume(&ptr);
00927     if (c == 0) break;
00928     /* Make a space from a newline, but ignore multiple newlines */
00929     if (c == '\n' && c_last != '\n') {
00930       dest[0] = ' ';
00931       dest++;
00932     } else if (c == '\r') {
00933       dest[0] = dest[1] = dest[2] = dest[3] = ' ';
00934       dest += 4;
00935     } else if (IsPrintable(c)) {
00936       dest += Utf8Encode(dest, c);
00937     }
00938     c_last = c;
00939   }
00940 
00941   *dest = '\0';
00942   /* Truncate and show string; postfixed by '...' if neccessary */
00943   DrawString(left, right, y, buffer2, colour);
00944 }
00945 
00947 enum MessageHistoryWidgets {
00948   MHW_STICKYBOX,
00949   MHW_BACKGROUND,
00950   MHW_SCROLLBAR,
00951 };
00952 
00953 struct MessageHistoryWindow : Window {
00954   static const int top_spacing;    
00955   static const int bottom_spacing; 
00956 
00957   int line_height; 
00958   int date_width;  
00959 
00960   Scrollbar *vscroll;
00961 
00962   MessageHistoryWindow(const WindowDesc *desc) : Window()
00963   {
00964     this->CreateNestedTree(desc);
00965     this->vscroll = this->GetScrollbar(MHW_SCROLLBAR);
00966     this->FinishInitNested(desc); // Initializes 'this->line_height' and 'this->date_width'.
00967     this->OnInvalidateData(0);
00968   }
00969 
00970   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00971   {
00972     if (widget == MHW_BACKGROUND) {
00973       this->line_height = FONT_HEIGHT_NORMAL + 2;
00974       resize->height = this->line_height;
00975 
00976       SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 12, 30));
00977       this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
00978 
00979       size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
00980       size->width = max(200u, size->width); // At least 200 pixels wide.
00981     }
00982   }
00983 
00984   virtual void OnPaint()
00985   {
00986     this->OnInvalidateData(0);
00987     this->DrawWidgets();
00988   }
00989 
00990   virtual void DrawWidget(const Rect &r, int widget) const
00991   {
00992     if (widget != MHW_BACKGROUND || _total_news == 0) return;
00993 
00994     /* Find the first news item to display. */
00995     NewsItem *ni = _latest_news;
00996     for (int n = this->vscroll->GetPosition(); n > 0; n--) {
00997       ni = ni->prev;
00998       if (ni == NULL) return;
00999     }
01000 
01001     /* Fill the widget with news items. */
01002     int y = r.top + this->top_spacing;
01003     bool rtl = _current_text_dir == TD_RTL;
01004     uint date_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
01005     uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
01006     uint news_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
01007     uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
01008     for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
01009       SetDParam(0, ni->date);
01010       DrawString(date_left, date_right, y, STR_SHORT_DATE);
01011 
01012       DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
01013       y += this->line_height;
01014 
01015       ni = ni->prev;
01016       if (ni == NULL) return;
01017     }
01018   }
01019 
01020   virtual void OnInvalidateData(int data)
01021   {
01022     this->vscroll->SetCount(_total_news);
01023   }
01024 
01025   virtual void OnClick(Point pt, int widget, int click_count)
01026   {
01027     if (widget == MHW_BACKGROUND) {
01028       NewsItem *ni = _latest_news;
01029       if (ni == NULL) return;
01030 
01031       for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, MHW_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
01032         ni = ni->prev;
01033         if (ni == NULL) return;
01034       }
01035 
01036       ShowNewsMessage(ni);
01037     }
01038   }
01039 
01040   virtual void OnResize()
01041   {
01042     this->vscroll->SetCapacity(this->GetWidget<NWidgetBase>(MHW_BACKGROUND)->current_y / this->line_height);
01043   }
01044 };
01045 
01046 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
01047 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
01048 
01049 static const NWidgetPart _nested_message_history[] = {
01050   NWidget(NWID_HORIZONTAL),
01051     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01052     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01053     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01054     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01055   EndContainer(),
01056 
01057   NWidget(NWID_HORIZONTAL),
01058     NWidget(WWT_PANEL, COLOUR_BROWN, MHW_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(MHW_SCROLLBAR),
01059     EndContainer(),
01060     NWidget(NWID_VERTICAL),
01061       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, MHW_SCROLLBAR),
01062       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01063     EndContainer(),
01064   EndContainer(),
01065 };
01066 
01067 static const WindowDesc _message_history_desc(
01068   WDP_AUTO, 400, 140,
01069   WC_MESSAGE_HISTORY, WC_NONE,
01070   WDF_UNCLICK_BUTTONS,
01071   _nested_message_history, lengthof(_nested_message_history)
01072 );
01073 
01075 void ShowMessageHistory()
01076 {
01077   DeleteWindowById(WC_MESSAGE_HISTORY, 0);
01078   new MessageHistoryWindow(&_message_history_desc);
01079 }
01080 
01082 enum MessageOptionsSpace {
01083   MOS_WIDG_PER_SETTING      = 4,  
01084 
01085   MOS_LEFT_EDGE             = 6,  
01086   MOS_COLUMN_SPACING        = 4,  
01087   MOS_RIGHT_EDGE            = 6,  
01088   MOS_BUTTON_SPACE          = 10, 
01089 
01090   MOS_ABOVE_GLOBAL_SETTINGS = 6,  
01091   MOS_BOTTOM_EDGE           = 6,  
01092 };
01093 
01095 enum MessageOptionWidgets {
01096   WIDGET_NEWSOPT_BACKGROUND,        
01097   WIDGET_NEWSOPT_LABEL,             
01098   WIDGET_NEWSOPT_DROP_SUMMARY,      
01099   WIDGET_NEWSOPT_LABEL_SUMMARY,     
01100   WIDGET_NEWSOPT_SOUNDTICKER,       
01101   WIDGET_NEWSOPT_SOUNDTICKER_LABEL, 
01102 
01103   WIDGET_NEWSOPT_START_OPTION,      
01104   WIDGET_NEWSOPT_END_OPTION = WIDGET_NEWSOPT_START_OPTION + NT_END * MOS_WIDG_PER_SETTING, 
01105 };
01106 
01107 struct MessageOptionsWindow : Window {
01108   static const StringID message_opt[]; 
01109   int state;                           
01110   Dimension dim_message_opt;           
01111 
01112   MessageOptionsWindow(const WindowDesc *desc) : Window()
01113   {
01114     this->InitNested(desc);
01115     /* Set up the initial disabled buttons in the case of 'off' or 'full' */
01116     NewsDisplay all_val = _news_type_data[0].display;
01117     for (int i = 0; i < NT_END; i++) {
01118       this->SetMessageButtonStates(_news_type_data[i].display, i);
01119       /* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
01120       if (_news_type_data[i].display != all_val) all_val = ND_OFF;
01121     }
01122     /* If all values are the same value, the ALL-button will take over this value */
01123     this->state = all_val;
01124     this->OnInvalidateData(0);
01125   }
01126 
01135   void SetMessageButtonStates(byte value, int element)
01136   {
01137     element *= MOS_WIDG_PER_SETTING;
01138 
01139     this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION, value == 0);
01140     this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION + 2, value == 2);
01141   }
01142 
01143   virtual void DrawWidget(const Rect &r, int widget) const
01144   {
01145     if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION && (widget -  WIDGET_NEWSOPT_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01146       /* Draw the string of each setting on each button. */
01147       int i = (widget -  WIDGET_NEWSOPT_START_OPTION) / MOS_WIDG_PER_SETTING;
01148       DrawString(r.left, r.right, r.top + 2, this->message_opt[_news_type_data[i].display], TC_BLACK, SA_HOR_CENTER);
01149     }
01150   }
01151 
01152   virtual void OnInit()
01153   {
01154     this->dim_message_opt.width  = 0;
01155     this->dim_message_opt.height = 0;
01156     for (const StringID *str = message_opt; *str != INVALID_STRING_ID; str++) this->dim_message_opt = maxdim(this->dim_message_opt, GetStringBoundingBox(*str));
01157   }
01158 
01159   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01160   {
01161     if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION) {
01162       /* Height is the biggest widget height in a row. */
01163       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
01164 
01165       /* Compute width for the label widget only. */
01166       if ((widget - WIDGET_NEWSOPT_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01167         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01168       }
01169       return;
01170     }
01171 
01172     /* Size computations for global message options. */
01173     if (widget == WIDGET_NEWSOPT_DROP_SUMMARY || widget == WIDGET_NEWSOPT_LABEL_SUMMARY || widget == WIDGET_NEWSOPT_SOUNDTICKER || widget == WIDGET_NEWSOPT_SOUNDTICKER_LABEL) {
01174       /* Height is the biggest widget height in a row. */
01175       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM);
01176 
01177       if (widget == WIDGET_NEWSOPT_DROP_SUMMARY) {
01178         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01179       } else if (widget == WIDGET_NEWSOPT_SOUNDTICKER) {
01180         size->width += MOS_BUTTON_SPACE; // A bit extra for better looks.
01181       }
01182       return;
01183     }
01184   }
01185 
01186   virtual void OnInvalidateData(int data)
01187   {
01188     /* Update the dropdown value for 'set all categories'. */
01189     this->GetWidget<NWidgetCore>(WIDGET_NEWSOPT_DROP_SUMMARY)->widget_data = this->message_opt[this->state];
01190 
01191     /* Update widget to reflect the value of the #_news_ticker_sound variable. */
01192     this->SetWidgetLoweredState(WIDGET_NEWSOPT_SOUNDTICKER, _news_ticker_sound);
01193   }
01194 
01195   virtual void OnClick(Point pt, int widget, int click_count)
01196   {
01197     switch (widget) {
01198       case WIDGET_NEWSOPT_DROP_SUMMARY: // Dropdown menu for all settings
01199         ShowDropDownMenu(this, this->message_opt, this->state, WIDGET_NEWSOPT_DROP_SUMMARY, 0, 0);
01200         break;
01201 
01202       case WIDGET_NEWSOPT_SOUNDTICKER: // Change ticker sound on/off
01203         _news_ticker_sound ^= 1;
01204         this->InvalidateData();
01205         break;
01206 
01207       default: { // Clicked on the [<] .. [>] widgets
01208         if (widget >= WIDGET_NEWSOPT_START_OPTION && widget < WIDGET_NEWSOPT_END_OPTION) {
01209           int wid = widget - WIDGET_NEWSOPT_START_OPTION;
01210           int element = wid / MOS_WIDG_PER_SETTING;
01211           byte val = (_news_type_data[element].display + ((wid % MOS_WIDG_PER_SETTING) ? 1 : -1)) % 3;
01212 
01213           this->SetMessageButtonStates(val, element);
01214           _news_type_data[element].display = (NewsDisplay)val;
01215           this->SetDirty();
01216         }
01217         break;
01218       }
01219     }
01220   }
01221 
01222   virtual void OnDropdownSelect(int widget, int index)
01223   {
01224     this->state = index;
01225 
01226     for (int i = 0; i < NT_END; i++) {
01227       this->SetMessageButtonStates(index, i);
01228       _news_type_data[i].display = (NewsDisplay)index;
01229     }
01230     this->InvalidateData();
01231   }
01232 };
01233 
01234 const StringID MessageOptionsWindow::message_opt[] = {STR_NEWS_MESSAGES_OFF, STR_NEWS_MESSAGES_SUMMARY, STR_NEWS_MESSAGES_FULL, INVALID_STRING_ID};
01235 
01237 static NWidgetBase *MakeButtonsColumn(int *biggest_index)
01238 {
01239   NWidgetVertical *vert_buttons = new NWidgetVertical;
01240 
01241   /* Top-part of the column, one row for each new category. */
01242   int widnum = WIDGET_NEWSOPT_START_OPTION;
01243   for (int i = 0; i < NT_END; i++) {
01244     NWidgetHorizontal *hor = new NWidgetHorizontal;
01245     /* [<] button. */
01246     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01247     leaf->SetFill(1, 1);
01248     hor->Add(leaf);
01249     /* Label. */
01250     leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_YELLOW, widnum + 1, STR_EMPTY, STR_NULL);
01251     leaf->SetFill(1, 1);
01252     hor->Add(leaf);
01253     /* [>] button. */
01254     leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 2, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01255     leaf->SetFill(1, 1);
01256     hor->Add(leaf);
01257     vert_buttons->Add(hor);
01258 
01259     widnum += MOS_WIDG_PER_SETTING;
01260   }
01261   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 2;
01262 
01263   /* Space between the category buttons and the global settings buttons. */
01264   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01265   vert_buttons->Add(spacer);
01266 
01267   /* Bottom part of the column with buttons for global changes. */
01268   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_DROPDOWN, COLOUR_YELLOW, WIDGET_NEWSOPT_DROP_SUMMARY, STR_EMPTY, STR_NULL);
01269   leaf->SetFill(1, 1);
01270   vert_buttons->Add(leaf);
01271 
01272   leaf = new NWidgetLeaf(WWT_TEXTBTN_2, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER, STR_STATION_BUILD_COVERAGE_OFF, STR_NULL);
01273   leaf->SetFill(1, 1);
01274   vert_buttons->Add(leaf);
01275 
01276   *biggest_index = max(*biggest_index, max<int>(WIDGET_NEWSOPT_DROP_SUMMARY, WIDGET_NEWSOPT_SOUNDTICKER));
01277   return vert_buttons;
01278 }
01279 
01281 static NWidgetBase *MakeDescriptionColumn(int *biggest_index)
01282 {
01283   NWidgetVertical *vert_desc = new NWidgetVertical;
01284 
01285   /* Top-part of the column, one row for each new category. */
01286   int widnum = WIDGET_NEWSOPT_START_OPTION;
01287   for (int i = 0; i < NT_END; i++) {
01288     NWidgetHorizontal *hor = new NWidgetHorizontal;
01289 
01290     /* Descriptive text. */
01291     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 3, _news_type_data[i].description, STR_NULL);
01292     hor->Add(leaf);
01293     /* Filling empty space to push text to the left. */
01294     NWidgetSpacer *spacer = new NWidgetSpacer(0, 0);
01295     spacer->SetFill(1, 0);
01296     hor->Add(spacer);
01297     vert_desc->Add(hor);
01298 
01299     widnum += MOS_WIDG_PER_SETTING;
01300   }
01301   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 3;
01302 
01303   /* Space between the category descriptions and the global settings descriptions. */
01304   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01305   vert_desc->Add(spacer);
01306 
01307   /* Bottom part of the column with descriptions of global changes. */
01308   NWidgetHorizontal *hor = new NWidgetHorizontal;
01309   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_LABEL_SUMMARY, STR_NEWS_MESSAGES_ALL, STR_NULL);
01310   hor->Add(leaf);
01311   /* Filling empty space to push text to the left. */
01312   spacer = new NWidgetSpacer(0, 0);
01313   spacer->SetFill(1, 0);
01314   hor->Add(spacer);
01315   vert_desc->Add(hor);
01316 
01317   hor = new NWidgetHorizontal;
01318   leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER_LABEL, STR_NEWS_MESSAGES_SOUND, STR_NULL);
01319   hor->Add(leaf);
01320   /* Filling empty space to push text to the left. */
01321   spacer = new NWidgetSpacer(0, 0);
01322   leaf->SetFill(1, 0);
01323   hor->Add(spacer);
01324   vert_desc->Add(hor);
01325 
01326   *biggest_index = max(*biggest_index, max<int>(WIDGET_NEWSOPT_LABEL_SUMMARY, WIDGET_NEWSOPT_SOUNDTICKER_LABEL));
01327   return vert_desc;
01328 }
01329 
01330 static const NWidgetPart _nested_message_options_widgets[] = {
01331   NWidget(NWID_HORIZONTAL),
01332     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01333     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_NEWS_MESSAGE_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01334   EndContainer(),
01335   NWidget(WWT_PANEL, COLOUR_BROWN, WIDGET_NEWSOPT_BACKGROUND),
01336     NWidget(NWID_HORIZONTAL),
01337       NWidget(NWID_SPACER), SetFill(1, 0),
01338       NWidget(WWT_LABEL, COLOUR_BROWN, WIDGET_NEWSOPT_LABEL), SetMinimalSize(0, 14), SetDataTip(STR_NEWS_MESSAGE_TYPES, STR_NULL),
01339       NWidget(NWID_SPACER), SetFill(1, 0),
01340     EndContainer(),
01341     NWidget(NWID_HORIZONTAL),
01342       NWidget(NWID_SPACER), SetMinimalSize(MOS_LEFT_EDGE, 0),
01343       NWidgetFunction(MakeButtonsColumn),
01344       NWidget(NWID_SPACER), SetMinimalSize(MOS_COLUMN_SPACING, 0),
01345       NWidgetFunction(MakeDescriptionColumn),
01346       NWidget(NWID_SPACER), SetMinimalSize(MOS_RIGHT_EDGE, 0),
01347     EndContainer(),
01348     NWidget(NWID_SPACER), SetMinimalSize(0, MOS_BOTTOM_EDGE),
01349   EndContainer(),
01350 };
01351 
01352 static const WindowDesc _message_options_desc(
01353   WDP_AUTO, 0, 0,
01354   WC_GAME_OPTIONS, WC_NONE,
01355   WDF_UNCLICK_BUTTONS,
01356   _nested_message_options_widgets, lengthof(_nested_message_options_widgets)
01357 );
01358 
01359 void ShowMessageOptions()
01360 {
01361   DeleteWindowById(WC_GAME_OPTIONS, 0);
01362   new MessageOptionsWindow(&_message_options_desc);
01363 }

Generated on Fri Feb 4 20:53:44 2011 for OpenTTD by  doxygen 1.6.1