news_gui.cpp

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

Generated on Fri Apr 30 21:55:23 2010 for OpenTTD by  doxygen 1.6.1