news_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "strings_func.h"
00016 #include "window_func.h"
00017 #include "date_func.h"
00018 #include "vehicle_base.h"
00019 #include "vehicle_func.h"
00020 #include "vehicle_gui.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_base.h"
00031 #include "engine_gui.h"
00032 #include "core/geometry_func.hpp"
00033 #include "command_func.h"
00034 #include "company_base.h"
00035 
00036 #include "widgets/news_widget.h"
00037 
00038 #include "table/strings.h"
00039 
00040 const NewsItem *_statusbar_news_item = NULL;
00041 bool _news_ticker_sound; 
00042 
00043 static uint MIN_NEWS_AMOUNT = 30;           
00044 static uint _total_news = 0;                
00045 static NewsItem *_oldest_news = NULL;       
00046 static NewsItem *_latest_news = NULL;       
00047 
00054 static const NewsItem *_forced_news = NULL;       
00055 
00057 static const NewsItem *_current_news = NULL;
00058 
00059 
00066 static TileIndex GetReferenceTile(NewsReferenceType reftype, uint32 ref)
00067 {
00068   switch (reftype) {
00069     case NR_TILE:     return (TileIndex)ref;
00070     case NR_STATION:  return Station::Get((StationID)ref)->xy;
00071     case NR_INDUSTRY: return Industry::Get((IndustryID)ref)->location.tile + TileDiffXY(1, 1);
00072     case NR_TOWN:     return Town::Get((TownID)ref)->xy;
00073     default:          return INVALID_TILE;
00074   }
00075 }
00076 
00077 /* Normal news items. */
00078 static const NWidgetPart _nested_normal_news_widgets[] = {
00079   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
00080     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
00081       NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
00082       NWidget(NWID_SPACER), SetFill(1, 0),
00083       NWidget(NWID_VERTICAL),
00084         NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
00085         NWidget(NWID_SPACER), SetFill(0, 1),
00086       EndContainer(),
00087     EndContainer(),
00088     NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 154), SetPadding(0, 5, 1, 5),
00089   EndContainer(),
00090 };
00091 
00092 static const WindowDesc _normal_news_desc(
00093   WDP_MANUAL, 0, 0,
00094   WC_NEWS_WINDOW, WC_NONE,
00095   0,
00096   _nested_normal_news_widgets, lengthof(_nested_normal_news_widgets)
00097 );
00098 
00099 /* New vehicles news items. */
00100 static const NWidgetPart _nested_vehicle_news_widgets[] = {
00101   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
00102     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
00103       NWidget(NWID_VERTICAL),
00104         NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
00105         NWidget(NWID_SPACER), SetFill(0, 1),
00106       EndContainer(),
00107       NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_VEH_TITLE), SetFill(1, 1), SetMinimalSize(419, 55), SetDataTip(STR_EMPTY, STR_NULL),
00108     EndContainer(),
00109     NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_VEH_BKGND), SetPadding(0, 25, 1, 25),
00110       NWidget(NWID_VERTICAL),
00111         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_NAME), SetMinimalSize(369, 33), SetFill(1, 0),
00112         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_SPR),  SetMinimalSize(369, 32), SetFill(1, 0),
00113         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_INFO), SetMinimalSize(369, 46), SetFill(1, 0),
00114       EndContainer(),
00115     EndContainer(),
00116   EndContainer(),
00117 };
00118 
00119 static const WindowDesc _vehicle_news_desc(
00120   WDP_MANUAL, 0, 0,
00121   WC_NEWS_WINDOW, WC_NONE,
00122   0,
00123   _nested_vehicle_news_widgets, lengthof(_nested_vehicle_news_widgets)
00124 );
00125 
00126 /* Company news items. */
00127 static const NWidgetPart _nested_company_news_widgets[] = {
00128   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
00129     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
00130       NWidget(NWID_VERTICAL),
00131         NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
00132         NWidget(NWID_SPACER), SetFill(0, 1),
00133       EndContainer(),
00134       NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_TITLE), SetFill(1, 1), SetMinimalSize(410, 20), SetDataTip(STR_EMPTY, STR_NULL),
00135     EndContainer(),
00136     NWidget(NWID_HORIZONTAL), SetPadding(0, 1, 1, 1),
00137       NWidget(NWID_VERTICAL),
00138         NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_FACE), SetMinimalSize(93, 119), SetPadding(2, 6, 2, 1),
00139         NWidget(NWID_HORIZONTAL),
00140           NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_NAME), SetMinimalSize(93, 24), SetPadding(0, 0, 0, 1),
00141           NWidget(NWID_SPACER), SetFill(1, 0),
00142         EndContainer(),
00143         NWidget(NWID_SPACER), SetFill(0, 1),
00144       EndContainer(),
00145       NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_COMPANY_MSG), SetFill(1, 1), SetMinimalSize(328, 150),
00146     EndContainer(),
00147   EndContainer(),
00148 };
00149 
00150 static const WindowDesc _company_news_desc(
00151   WDP_MANUAL, 0, 0,
00152   WC_NEWS_WINDOW, WC_NONE,
00153   0,
00154   _nested_company_news_widgets, lengthof(_nested_company_news_widgets)
00155 );
00156 
00157 /* Thin news items. */
00158 static const NWidgetPart _nested_thin_news_widgets[] = {
00159   NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
00160     NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
00161       NWidget(WWT_TEXT, COLOUR_WHITE, WID_N_CLOSEBOX), SetDataTip(STR_SILVER_CROSS, STR_NULL), SetPadding(0, 0, 0, 1),
00162       NWidget(NWID_SPACER), SetFill(1, 0),
00163       NWidget(NWID_VERTICAL),
00164         NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
00165         NWidget(NWID_SPACER), SetFill(0, 1),
00166       EndContainer(),
00167     EndContainer(),
00168     NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 48), SetFill(1, 0), SetPadding(0, 5, 0, 5),
00169     NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetMinimalSize(426, 70), SetPadding(1, 2, 2, 2),
00170   EndContainer(),
00171 };
00172 
00173 static const WindowDesc _thin_news_desc(
00174   WDP_MANUAL, 0, 0,
00175   WC_NEWS_WINDOW, WC_NONE,
00176   0,
00177   _nested_thin_news_widgets, lengthof(_nested_thin_news_widgets)
00178 );
00179 
00180 /* Small news items. */
00181 static const NWidgetPart _nested_small_news_widgets[] = {
00182   /* Caption + close box. The caption is no WWT_CAPTION as the window shall not be moveable and so on. */
00183   NWidget(NWID_HORIZONTAL),
00184     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE, WID_N_CLOSEBOX),
00185     NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_N_CAPTION), SetFill(1, 0),
00186   EndContainer(),
00187 
00188   /* Main part */
00189   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_N_HEADLINE),
00190     NWidget(WWT_INSET, COLOUR_LIGHT_BLUE, WID_N_INSET), SetPadding(2, 2, 2, 2),
00191       NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetPadding(1, 1, 1, 1), SetMinimalSize(274, 47), SetFill(1, 0),
00192     EndContainer(),
00193     NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(275, 20), SetFill(1, 0), SetPadding(0, 5, 0, 5),
00194   EndContainer(),
00195 };
00196 
00197 static const WindowDesc _small_news_desc(
00198   WDP_MANUAL, 0, 0,
00199   WC_NEWS_WINDOW, WC_NONE,
00200   0,
00201   _nested_small_news_widgets, lengthof(_nested_small_news_widgets)
00202 );
00203 
00207 static const WindowDesc* _news_window_layout[] = {
00208   &_thin_news_desc,    
00209   &_small_news_desc,   
00210   &_normal_news_desc,  
00211   &_vehicle_news_desc, 
00212   &_company_news_desc, 
00213 };
00214 
00215 const WindowDesc* GetNewsWindowLayout(NewsFlag flags)
00216 {
00217   uint layout = GB(flags, NFB_WINDOW_LAYOUT, NFB_WINDOW_LAYOUT_COUNT);
00218   assert(layout < lengthof(_news_window_layout));
00219   return _news_window_layout[layout];
00220 }
00221 
00225 NewsTypeData _news_type_data[] = {
00226   /*            name,              age, sound,           display,    description */
00227   NewsTypeData("arrival_player",    60, SND_1D_APPLAUSE, ND_FULL,    STR_NEWS_MESSAGE_TYPE_ARRIVAL_OF_FIRST_VEHICLE_OWN       ),  
00228   NewsTypeData("arrival_other",     60, SND_1D_APPLAUSE, ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_ARRIVAL_OF_FIRST_VEHICLE_OTHER     ),  
00229   NewsTypeData("accident",          90, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_ACCIDENTS_DISASTERS                ),  
00230   NewsTypeData("company_info",      60, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_COMPANY_INFORMATION                ),  
00231   NewsTypeData("open",              90, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_INDUSTRY_OPEN                      ),  
00232   NewsTypeData("close",             90, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_INDUSTRY_CLOSE                     ),  
00233   NewsTypeData("economy",           30, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_ECONOMY_CHANGES                    ),  
00234   NewsTypeData("production_player", 30, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_INDUSTRY_CHANGES_SERVED_BY_COMPANY ),  
00235   NewsTypeData("production_other",  30, SND_BEGIN,       ND_OFF,     STR_NEWS_MESSAGE_TYPE_INDUSTRY_CHANGES_SERVED_BY_OTHER   ),  
00236   NewsTypeData("production_nobody", 30, SND_BEGIN,       ND_OFF,     STR_NEWS_MESSAGE_TYPE_INDUSTRY_CHANGES_UNSERVED          ),  
00237   NewsTypeData("advice",           150, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_ADVICE_INFORMATION_ON_COMPANY      ),  
00238   NewsTypeData("new_vehicles",      30, SND_1E_OOOOH,    ND_FULL,    STR_NEWS_MESSAGE_TYPE_NEW_VEHICLES                       ),  
00239   NewsTypeData("acceptance",        90, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_CHANGES_OF_CARGO_ACCEPTANCE        ),  
00240   NewsTypeData("subsidies",        180, SND_BEGIN,       ND_SUMMARY, STR_NEWS_MESSAGE_TYPE_SUBSIDIES                          ),  
00241   NewsTypeData("general",           60, SND_BEGIN,       ND_FULL,    STR_NEWS_MESSAGE_TYPE_GENERAL_INFORMATION                ),  
00242 };
00243 
00244 assert_compile(lengthof(_news_type_data) == NT_END);
00245 
00247 struct NewsWindow : Window {
00248   uint16 chat_height;   
00249   uint16 status_height; 
00250   const NewsItem *ni;   
00251   static uint duration; 
00252 
00253   NewsWindow(const WindowDesc *desc, const NewsItem *ni) : Window(), ni(ni)
00254   {
00255     NewsWindow::duration = 555;
00256     const Window *w = FindWindowByClass(WC_SEND_NETWORK_MSG);
00257     this->chat_height = (w != NULL) ? w->height : 0;
00258     this->status_height = FindWindowById(WC_STATUS_BAR, 0)->height;
00259 
00260     this->flags |= WF_DISABLE_VP_SCROLL;
00261 
00262     this->CreateNestedTree(desc);
00263 
00264     /* For company news with a face we have a separate headline in param[0] */
00265     if (desc == &_company_news_desc) this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = this->ni->params[0];
00266 
00267     this->FinishInitNested(desc, 0);
00268 
00269     /* Initialize viewport if it exists. */
00270     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_N_VIEWPORT);
00271     if (nvp != NULL) {
00272       nvp->InitializeViewport(this, ni->reftype1 == NR_VEHICLE ? 0x80000000 | ni->ref1 : GetReferenceTile(ni->reftype1, ni->ref1), ZOOM_LVL_NEWS);
00273       if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags |= ND_NO_TRANSPARENCY;
00274       if ((this->ni->flags & NF_INCOLOUR) == 0) {
00275         nvp->disp_flags |= ND_SHADE_GREY;
00276       } else if (this->ni->flags & NF_SHADE) {
00277         nvp->disp_flags |= ND_SHADE_DIMMED;
00278       }
00279     }
00280 
00281     PositionNewsMessage(this);
00282   }
00283 
00284   void DrawNewsBorder(const Rect &r) const
00285   {
00286     GfxFillRect(r.left,  r.top,    r.right, r.bottom, PC_WHITE);
00287 
00288     GfxFillRect(r.left,  r.top,    r.left,  r.bottom, PC_BLACK);
00289     GfxFillRect(r.right, r.top,    r.right, r.bottom, PC_BLACK);
00290     GfxFillRect(r.left,  r.top,    r.right, r.top,    PC_BLACK);
00291     GfxFillRect(r.left,  r.bottom, r.right, r.bottom, PC_BLACK);
00292   }
00293 
00294   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00295   {
00296     Point pt = { 0, _screen.height };
00297     return pt;
00298   }
00299 
00300   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00301   {
00302     StringID str = STR_NULL;
00303     switch (widget) {
00304       case WID_N_MESSAGE:
00305         CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
00306         str = this->ni->string_id;
00307         break;
00308 
00309       case WID_N_COMPANY_MSG:
00310         str = this->GetCompanyMessageString();
00311         break;
00312 
00313       case WID_N_VEH_NAME:
00314       case WID_N_VEH_TITLE:
00315         str = this->GetNewVehicleMessageString(widget);
00316         break;
00317 
00318       case WID_N_VEH_INFO: {
00319         assert(this->ni->reftype1 == NR_ENGINE);
00320         EngineID engine = this->ni->ref1;
00321         str = GetEngineInfoString(engine);
00322         break;
00323       }
00324       default:
00325         return; // Do nothing
00326     }
00327 
00328     /* Update minimal size with length of the multi-line string. */
00329     Dimension d = *size;
00330     d.width = (d.width >= padding.width) ? d.width - padding.width : 0;
00331     d.height = (d.height >= padding.height) ? d.height - padding.height : 0;
00332     d = GetStringMultiLineBoundingBox(str, d);
00333     d.width += padding.width;
00334     d.height += padding.height;
00335     *size = maxdim(*size, d);
00336   }
00337 
00338   virtual void SetStringParameters(int widget) const
00339   {
00340     if (widget == WID_N_DATE) SetDParam(0, this->ni->date);
00341   }
00342 
00343   virtual void DrawWidget(const Rect &r, int widget) const
00344   {
00345     switch (widget) {
00346       case WID_N_CAPTION:
00347         DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION);
00348         break;
00349 
00350       case WID_N_PANEL:
00351         this->DrawNewsBorder(r);
00352         break;
00353 
00354       case WID_N_MESSAGE:
00355         CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
00356         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->ni->string_id, TC_FROMSTRING, SA_CENTER);
00357         break;
00358 
00359       case WID_N_MGR_FACE: {
00360         const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
00361         DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top);
00362         GfxFillRect(r.left + 1, r.top, r.left + 1 + 91, r.top + 118, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
00363         break;
00364       }
00365       case WID_N_MGR_NAME: {
00366         const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
00367         SetDParamStr(0, cni->president_name);
00368         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
00369         break;
00370       }
00371       case WID_N_COMPANY_MSG:
00372         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetCompanyMessageString(), TC_FROMSTRING, SA_CENTER);
00373         break;
00374 
00375       case WID_N_VEH_BKGND:
00376         GfxFillRect(r.left, r.top, r.right, r.bottom, PC_GREY);
00377         break;
00378 
00379       case WID_N_VEH_NAME:
00380       case WID_N_VEH_TITLE:
00381         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetNewVehicleMessageString(widget), TC_FROMSTRING, SA_CENTER);
00382         break;
00383 
00384       case WID_N_VEH_SPR: {
00385         assert(this->ni->reftype1 == NR_ENGINE);
00386         EngineID engine = this->ni->ref1;
00387         DrawVehicleEngine(r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW);
00388         GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
00389         break;
00390       }
00391       case WID_N_VEH_INFO: {
00392         assert(this->ni->reftype1 == NR_ENGINE);
00393         EngineID engine = this->ni->ref1;
00394         DrawStringMultiLine(r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
00395         break;
00396       }
00397     }
00398   }
00399 
00400   virtual void OnClick(Point pt, int widget, int click_count)
00401   {
00402     switch (widget) {
00403       case WID_N_CLOSEBOX:
00404         NewsWindow::duration = 0;
00405         delete this;
00406         _forced_news = NULL;
00407         break;
00408 
00409       case WID_N_CAPTION:
00410         if (this->ni->reftype1 == NR_VEHICLE) {
00411           const Vehicle *v = Vehicle::Get(this->ni->ref1);
00412           ShowVehicleViewWindow(v);
00413         }
00414         break;
00415 
00416       case WID_N_VIEWPORT:
00417         break; // Ignore clicks
00418 
00419       default:
00420         if (this->ni->reftype1 == NR_VEHICLE) {
00421           const Vehicle *v = Vehicle::Get(this->ni->ref1);
00422           ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
00423         } else {
00424           TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
00425           TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
00426           if (_ctrl_pressed) {
00427             if (tile1 != INVALID_TILE) ShowExtraViewPortWindow(tile1);
00428             if (tile2 != INVALID_TILE) ShowExtraViewPortWindow(tile2);
00429           } else {
00430             if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
00431               ScrollMainWindowToTile(tile2);
00432             }
00433           }
00434         }
00435         break;
00436     }
00437   }
00438 
00439   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00440   {
00441     if (keycode == WKC_SPACE) {
00442       /* Don't continue. */
00443       delete this;
00444       return ES_HANDLED;
00445     }
00446     return ES_NOT_HANDLED;
00447   }
00448 
00454   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00455   {
00456     if (!gui_scope) return;
00457     /* The chatbar has notified us that is was either created or closed */
00458     int newtop = this->top + this->chat_height - data;
00459     this->chat_height = data;
00460     this->SetWindowTop(newtop);
00461   }
00462 
00463   virtual void OnTick()
00464   {
00465     /* Scroll up newsmessages from the bottom in steps of 4 pixels */
00466     int newtop = max(this->top - 4, _screen.height - this->height - this->status_height - this->chat_height);
00467     this->SetWindowTop(newtop);
00468   }
00469 
00470 private:
00475   void SetWindowTop(int newtop)
00476   {
00477     if (this->top == newtop) return;
00478 
00479     int mintop = min(newtop, this->top);
00480     int maxtop = max(newtop, this->top);
00481     if (this->viewport != NULL) this->viewport->top += newtop - this->top;
00482     this->top = newtop;
00483 
00484     SetDirtyBlocks(this->left, mintop, this->left + this->width, maxtop + this->height);
00485   }
00486 
00487   StringID GetCompanyMessageString() const
00488   {
00489     /* Company news with a face have a separate headline, so the normal message is shifted by two params */
00490     CopyInDParam(0, this->ni->params + 2, lengthof(this->ni->params) - 2);
00491     return this->ni->params[1];
00492   }
00493 
00494   StringID GetNewVehicleMessageString(int widget) const
00495   {
00496     assert(this->ni->reftype1 == NR_ENGINE);
00497     EngineID engine = this->ni->ref1;
00498 
00499     switch (widget) {
00500       case WID_N_VEH_TITLE:
00501         SetDParam(0, GetEngineCategoryName(engine));
00502         return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
00503 
00504       case WID_N_VEH_NAME:
00505         SetDParam(0, engine);
00506         return STR_NEWS_NEW_VEHICLE_TYPE;
00507 
00508       default:
00509         NOT_REACHED();
00510     }
00511   }
00512 };
00513 
00514 /* static */ uint NewsWindow::duration = 0; // Instance creation.
00515 
00516 
00518 static void ShowNewspaper(const NewsItem *ni)
00519 {
00520   SoundFx sound = _news_type_data[ni->type].sound;
00521   if (sound != 0) SndPlayFx(sound);
00522 
00523   new NewsWindow(GetNewsWindowLayout(ni->flags), ni);
00524 }
00525 
00527 static void ShowTicker(const NewsItem *ni)
00528 {
00529   if (_news_ticker_sound) SndPlayFx(SND_16_MORSE);
00530 
00531   _statusbar_news_item = ni;
00532   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
00533 }
00534 
00536 void InitNewsItemStructs()
00537 {
00538   for (NewsItem *ni = _oldest_news; ni != NULL; ) {
00539     NewsItem *next = ni->next;
00540     delete ni;
00541     ni = next;
00542   }
00543 
00544   _total_news = 0;
00545   _oldest_news = NULL;
00546   _latest_news = NULL;
00547   _forced_news = NULL;
00548   _current_news = NULL;
00549   _statusbar_news_item = NULL;
00550   NewsWindow::duration = 0;
00551 }
00552 
00557 static bool ReadyForNextItem()
00558 {
00559   const NewsItem *ni = _forced_news == NULL ? _current_news : _forced_news;
00560   if (ni == NULL) return true;
00561 
00562   /* Ticker message
00563    * Check if the status bar message is still being displayed? */
00564   if (IsNewsTickerShown()) return false;
00565 
00566   /* Newspaper message, decrement duration counter */
00567   if (NewsWindow::duration != 0) NewsWindow::duration--;
00568 
00569   /* neither newsticker nor newspaper are running */
00570   return (NewsWindow::duration == 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
00571 }
00572 
00574 static void MoveToNextItem()
00575 {
00576   InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
00577   DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
00578   _forced_news = NULL;
00579   _statusbar_news_item = NULL;
00580 
00581   /* if we're not at the last item, then move on */
00582   if (_current_news != _latest_news) {
00583     _current_news = (_current_news == NULL) ? _oldest_news : _current_news->next;
00584     const NewsItem *ni = _current_news;
00585     const NewsType type = ni->type;
00586 
00587     /* check the date, don't show too old items */
00588     if (_date - _news_type_data[type].age > ni->date) return;
00589 
00590     switch (_news_type_data[type].display) {
00591       default: NOT_REACHED();
00592       case ND_OFF: // Off - show nothing only a small reminder in the status bar
00593         InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
00594         break;
00595 
00596       case ND_SUMMARY: // Summary - show ticker
00597         ShowTicker(ni);
00598         break;
00599 
00600       case ND_FULL: // Full - show newspaper
00601         ShowNewspaper(ni);
00602         break;
00603     }
00604   }
00605 }
00606 
00620 void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
00621 {
00622   if (_game_mode == GM_MENU) return;
00623 
00624   /* Create new news item node */
00625   NewsItem *ni = new NewsItem;
00626 
00627   ni->string_id = string;
00628   ni->type = type;
00629   ni->flags = flags;
00630 
00631   /* show this news message in colour? */
00632   if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR;
00633 
00634   ni->reftype1 = reftype1;
00635   ni->reftype2 = reftype2;
00636   ni->ref1 = ref1;
00637   ni->ref2 = ref2;
00638   ni->free_data = free_data;
00639   ni->date = _date;
00640   CopyOutDParam(ni->params, 0, lengthof(ni->params));
00641 
00642   if (_total_news++ == 0) {
00643     assert(_oldest_news == NULL);
00644     _oldest_news = ni;
00645     ni->prev = NULL;
00646   } else {
00647     assert(_latest_news->next == NULL);
00648     _latest_news->next = ni;
00649     ni->prev = _latest_news;
00650   }
00651 
00652   ni->next = NULL;
00653   _latest_news = ni;
00654 
00655   SetWindowDirty(WC_MESSAGE_HISTORY, 0);
00656 }
00657 
00670 CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00671 {
00672   if (_current_company != OWNER_DEITY) return CMD_ERROR;
00673 
00674   NewsType type = (NewsType)GB(p1, 0, 8);
00675   NewsReferenceType reftype1 = (NewsReferenceType)GB(p1, 8, 8);
00676   CompanyID company = (CompanyID)GB(p1, 16, 8);
00677 
00678   if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR;
00679   if (type >= NT_END) return CMD_ERROR;
00680   if (StrEmpty(text)) return CMD_ERROR;
00681 
00682   switch (reftype1) {
00683     case NR_NONE: break;
00684     case NR_TILE:
00685       if (!IsValidTile(p2)) return CMD_ERROR;
00686       break;
00687 
00688     case NR_VEHICLE:
00689       if (!Vehicle::IsValidID(p2)) return CMD_ERROR;
00690       break;
00691 
00692     case NR_STATION:
00693       if (!Station::IsValidID(p2)) return CMD_ERROR;
00694       break;
00695 
00696     case NR_INDUSTRY:
00697       if (!Industry::IsValidID(p2)) return CMD_ERROR;
00698       break;
00699 
00700     case NR_TOWN:
00701       if (!Town::IsValidID(p2)) return CMD_ERROR;
00702       break;
00703 
00704     case NR_ENGINE:
00705       if (!Engine::IsValidID(p2)) return CMD_ERROR;
00706       break;
00707 
00708     default: return CMD_ERROR;
00709   }
00710 
00711   if (company != INVALID_OWNER && company != _local_company) return CommandCost();
00712 
00713   if (flags & DC_EXEC) {
00714     char *news = strdup(text);
00715     SetDParamStr(0, news);
00716     AddNewsItem(STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype1, p2, NR_NONE, UINT32_MAX, news);
00717   }
00718 
00719   return CommandCost();
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 
00815 void DeleteInvalidEngineNews()
00816 {
00817   NewsItem *ni = _oldest_news;
00818 
00819   while (ni != NULL) {
00820     NewsItem *next = ni->next;
00821     if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
00822         (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
00823       DeleteNewsItem(ni);
00824     }
00825     ni = next;
00826   }
00827 }
00828 
00829 static void RemoveOldNewsItems()
00830 {
00831   NewsItem *next;
00832   for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
00833     next = cur->next;
00834     if (_date - _news_type_data[cur->type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
00835   }
00836 }
00837 
00844 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
00845 {
00846   for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
00847     if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
00848     if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
00849     if (ni->flags & NF_VEHICLE_PARAM0 && ni->params[0] == from_index) ni->params[0] = to_index;
00850   }
00851 }
00852 
00853 void NewsLoop()
00854 {
00855   /* no news item yet */
00856   if (_total_news == 0) return;
00857 
00858   /* There is no status bar, so no reason to show news;
00859    * especially important with the end game screen when
00860    * there is no status bar but possible news. */
00861   if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
00862 
00863   static byte _last_clean_month = 0;
00864 
00865   if (_last_clean_month != _cur_month) {
00866     RemoveOldNewsItems();
00867     _last_clean_month = _cur_month;
00868   }
00869 
00870   if (ReadyForNextItem()) MoveToNextItem();
00871 }
00872 
00874 static void ShowNewsMessage(const NewsItem *ni)
00875 {
00876   assert(_total_news != 0);
00877 
00878   /* Delete the news window */
00879   DeleteWindowById(WC_NEWS_WINDOW, 0);
00880 
00881   /* setup forced news item */
00882   _forced_news = ni;
00883 
00884   if (_forced_news != NULL) {
00885     DeleteWindowById(WC_NEWS_WINDOW, 0);
00886     ShowNewspaper(ni);
00887   }
00888 }
00889 
00891 void ShowLastNewsMessage()
00892 {
00893   if (_total_news == 0) {
00894     return;
00895   } else if (_forced_news == NULL) {
00896     /* Not forced any news yet, show the current one, unless a news window is
00897      * open (which can only be the current one), then show the previous item */
00898     const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
00899     ShowNewsMessage((w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev);
00900   } else if (_forced_news == _oldest_news) {
00901     /* We have reached the oldest news, start anew with the latest */
00902     ShowNewsMessage(_latest_news);
00903   } else {
00904     /* 'Scrolling' through news history show each one in turn */
00905     ShowNewsMessage(_forced_news->prev);
00906   }
00907 }
00908 
00909 
00920 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
00921 {
00922   char buffer[512], buffer2[512];
00923   StringID str;
00924 
00925   CopyInDParam(0, ni->params, lengthof(ni->params));
00926   str = ni->string_id;
00927 
00928   GetString(buffer, str, lastof(buffer));
00929   /* Copy the just gotten string to another buffer to remove any formatting
00930    * from it such as big fonts, etc. */
00931   const char *ptr = buffer;
00932   char *dest = buffer2;
00933   WChar c_last = '\0';
00934   for (;;) {
00935     WChar c = Utf8Consume(&ptr);
00936     if (c == 0) break;
00937     /* Make a space from a newline, but ignore multiple newlines */
00938     if (c == '\n' && c_last != '\n') {
00939       dest[0] = ' ';
00940       dest++;
00941     } else if (c == '\r') {
00942       dest[0] = dest[1] = dest[2] = dest[3] = ' ';
00943       dest += 4;
00944     } else if (IsPrintable(c)) {
00945       dest += Utf8Encode(dest, c);
00946     }
00947     c_last = c;
00948   }
00949 
00950   *dest = '\0';
00951   /* Truncate and show string; postfixed by '...' if neccessary */
00952   DrawString(left, right, y, buffer2, colour);
00953 }
00954 
00955 struct MessageHistoryWindow : Window {
00956   static const int top_spacing;    
00957   static const int bottom_spacing; 
00958 
00959   int line_height; 
00960   int date_width;  
00961 
00962   Scrollbar *vscroll;
00963 
00964   MessageHistoryWindow(const WindowDesc *desc) : Window()
00965   {
00966     this->CreateNestedTree(desc);
00967     this->vscroll = this->GetScrollbar(WID_MH_SCROLLBAR);
00968     this->FinishInitNested(desc); // Initializes 'this->line_height' and 'this->date_width'.
00969     this->OnInvalidateData(0);
00970   }
00971 
00972   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00973   {
00974     if (widget == WID_MH_BACKGROUND) {
00975       this->line_height = FONT_HEIGHT_NORMAL + 2;
00976       resize->height = this->line_height;
00977 
00978       /* Months are off-by-one, so it's actually 8. Not using
00979        * month 12 because the 1 is usually less wide. */
00980       SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 7, 30));
00981       this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
00982 
00983       size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
00984       size->width = max(200u, size->width); // At least 200 pixels wide.
00985     }
00986   }
00987 
00988   virtual void OnPaint()
00989   {
00990     this->OnInvalidateData(0);
00991     this->DrawWidgets();
00992   }
00993 
00994   virtual void DrawWidget(const Rect &r, int widget) const
00995   {
00996     if (widget != WID_MH_BACKGROUND || _total_news == 0) return;
00997 
00998     /* Find the first news item to display. */
00999     NewsItem *ni = _latest_news;
01000     for (int n = this->vscroll->GetPosition(); n > 0; n--) {
01001       ni = ni->prev;
01002       if (ni == NULL) return;
01003     }
01004 
01005     /* Fill the widget with news items. */
01006     int y = r.top + this->top_spacing;
01007     bool rtl = _current_text_dir == TD_RTL;
01008     uint date_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
01009     uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
01010     uint news_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
01011     uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
01012     for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
01013       SetDParam(0, ni->date);
01014       DrawString(date_left, date_right, y, STR_SHORT_DATE);
01015 
01016       DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
01017       y += this->line_height;
01018 
01019       ni = ni->prev;
01020       if (ni == NULL) return;
01021     }
01022   }
01023 
01029   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01030   {
01031     if (!gui_scope) return;
01032     this->vscroll->SetCount(_total_news);
01033   }
01034 
01035   virtual void OnClick(Point pt, int widget, int click_count)
01036   {
01037     if (widget == WID_MH_BACKGROUND) {
01038       NewsItem *ni = _latest_news;
01039       if (ni == NULL) return;
01040 
01041       for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_MH_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
01042         ni = ni->prev;
01043         if (ni == NULL) return;
01044       }
01045 
01046       ShowNewsMessage(ni);
01047     }
01048   }
01049 
01050   virtual void OnResize()
01051   {
01052     this->vscroll->SetCapacity(this->GetWidget<NWidgetBase>(WID_MH_BACKGROUND)->current_y / this->line_height);
01053   }
01054 };
01055 
01056 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
01057 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
01058 
01059 static const NWidgetPart _nested_message_history[] = {
01060   NWidget(NWID_HORIZONTAL),
01061     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01062     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01063     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01064     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01065   EndContainer(),
01066 
01067   NWidget(NWID_HORIZONTAL),
01068     NWidget(WWT_PANEL, COLOUR_BROWN, WID_MH_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR),
01069     EndContainer(),
01070     NWidget(NWID_VERTICAL),
01071       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_MH_SCROLLBAR),
01072       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01073     EndContainer(),
01074   EndContainer(),
01075 };
01076 
01077 static const WindowDesc _message_history_desc(
01078   WDP_AUTO, 400, 140,
01079   WC_MESSAGE_HISTORY, WC_NONE,
01080   WDF_UNCLICK_BUTTONS,
01081   _nested_message_history, lengthof(_nested_message_history)
01082 );
01083 
01085 void ShowMessageHistory()
01086 {
01087   DeleteWindowById(WC_MESSAGE_HISTORY, 0);
01088   new MessageHistoryWindow(&_message_history_desc);
01089 }
01090 
01091 struct MessageOptionsWindow : Window {
01092   static const StringID message_opt[]; 
01093   int state;                           
01094   Dimension dim_message_opt;           
01095 
01096   MessageOptionsWindow(const WindowDesc *desc) : Window()
01097   {
01098     this->InitNested(desc, WN_GAME_OPTIONS_MESSAGE_OPTION);
01099     /* Set up the initial disabled buttons in the case of 'off' or 'full' */
01100     NewsDisplay all_val = _news_type_data[0].display;
01101     for (int i = 0; i < NT_END; i++) {
01102       this->SetMessageButtonStates(_news_type_data[i].display, i);
01103       /* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
01104       if (_news_type_data[i].display != all_val) all_val = ND_OFF;
01105     }
01106     /* If all values are the same value, the ALL-button will take over this value */
01107     this->state = all_val;
01108     this->OnInvalidateData(0);
01109   }
01110 
01119   void SetMessageButtonStates(byte value, int element)
01120   {
01121     element *= MOS_WIDG_PER_SETTING;
01122 
01123     this->SetWidgetDisabledState(element + WID_MO_START_OPTION, value == 0);
01124     this->SetWidgetDisabledState(element + WID_MO_START_OPTION + 2, value == 2);
01125   }
01126 
01127   virtual void DrawWidget(const Rect &r, int widget) const
01128   {
01129     if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION && (widget -  WID_MO_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01130       /* Draw the string of each setting on each button. */
01131       int i = (widget -  WID_MO_START_OPTION) / MOS_WIDG_PER_SETTING;
01132       DrawString(r.left, r.right, r.top + 2, this->message_opt[_news_type_data[i].display], TC_BLACK, SA_HOR_CENTER);
01133     }
01134   }
01135 
01136   virtual void OnInit()
01137   {
01138     this->dim_message_opt.width  = 0;
01139     this->dim_message_opt.height = 0;
01140     for (const StringID *str = message_opt; *str != INVALID_STRING_ID; str++) this->dim_message_opt = maxdim(this->dim_message_opt, GetStringBoundingBox(*str));
01141   }
01142 
01143   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01144   {
01145     if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION) {
01146       /* Height is the biggest widget height in a row. */
01147       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
01148 
01149       /* Compute width for the label widget only. */
01150       if ((widget - WID_MO_START_OPTION) % MOS_WIDG_PER_SETTING == 1) {
01151         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01152       }
01153       return;
01154     }
01155 
01156     /* Size computations for global message options. */
01157     if (widget == WID_MO_DROP_SUMMARY || widget == WID_MO_LABEL_SUMMARY || widget == WID_MO_SOUNDTICKER || widget == WID_MO_SOUNDTICKER_LABEL) {
01158       /* Height is the biggest widget height in a row. */
01159       size->height = FONT_HEIGHT_NORMAL + max(WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM);
01160 
01161       if (widget == WID_MO_DROP_SUMMARY) {
01162         size->width = this->dim_message_opt.width + padding.width + MOS_BUTTON_SPACE; // A bit extra for better looks.
01163       } else if (widget == WID_MO_SOUNDTICKER) {
01164         size->width += MOS_BUTTON_SPACE; // A bit extra for better looks.
01165       }
01166       return;
01167     }
01168   }
01169 
01175   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01176   {
01177     if (!gui_scope) return;
01178     /* Update the dropdown value for 'set all categories'. */
01179     this->GetWidget<NWidgetCore>(WID_MO_DROP_SUMMARY)->widget_data = this->message_opt[this->state];
01180 
01181     /* Update widget to reflect the value of the #_news_ticker_sound variable. */
01182     this->SetWidgetLoweredState(WID_MO_SOUNDTICKER, _news_ticker_sound);
01183   }
01184 
01185   virtual void OnClick(Point pt, int widget, int click_count)
01186   {
01187     switch (widget) {
01188       case WID_MO_DROP_SUMMARY: // Dropdown menu for all settings
01189         ShowDropDownMenu(this, this->message_opt, this->state, WID_MO_DROP_SUMMARY, 0, 0);
01190         break;
01191 
01192       case WID_MO_SOUNDTICKER: // Change ticker sound on/off
01193         _news_ticker_sound ^= 1;
01194         this->InvalidateData();
01195         break;
01196 
01197       default: { // Clicked on the [<] .. [>] widgets
01198         if (widget >= WID_MO_START_OPTION && widget < WID_MO_END_OPTION) {
01199           int wid = widget - WID_MO_START_OPTION;
01200           int element = wid / MOS_WIDG_PER_SETTING;
01201           byte val = (_news_type_data[element].display + ((wid % MOS_WIDG_PER_SETTING) ? 1 : -1)) % 3;
01202 
01203           this->SetMessageButtonStates(val, element);
01204           _news_type_data[element].display = (NewsDisplay)val;
01205           this->SetDirty();
01206         }
01207         break;
01208       }
01209     }
01210   }
01211 
01212   virtual void OnDropdownSelect(int widget, int index)
01213   {
01214     this->state = index;
01215 
01216     for (int i = 0; i < NT_END; i++) {
01217       this->SetMessageButtonStates(index, i);
01218       _news_type_data[i].display = (NewsDisplay)index;
01219     }
01220     this->InvalidateData();
01221   }
01222 };
01223 
01224 const StringID MessageOptionsWindow::message_opt[] = {STR_NEWS_MESSAGES_OFF, STR_NEWS_MESSAGES_SUMMARY, STR_NEWS_MESSAGES_FULL, INVALID_STRING_ID};
01225 
01227 static NWidgetBase *MakeButtonsColumn(int *biggest_index)
01228 {
01229   NWidgetVertical *vert_buttons = new NWidgetVertical;
01230 
01231   /* Top-part of the column, one row for each new category. */
01232   int widnum = WID_MO_START_OPTION;
01233   for (int i = 0; i < NT_END; i++) {
01234     NWidgetHorizontal *hor = new NWidgetHorizontal;
01235     /* [<] button. */
01236     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01237     leaf->SetFill(1, 1);
01238     hor->Add(leaf);
01239     /* Label. */
01240     leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_YELLOW, widnum + 1, STR_EMPTY, STR_NULL);
01241     leaf->SetFill(1, 1);
01242     hor->Add(leaf);
01243     /* [>] button. */
01244     leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 2, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
01245     leaf->SetFill(1, 1);
01246     hor->Add(leaf);
01247     vert_buttons->Add(hor);
01248 
01249     widnum += MOS_WIDG_PER_SETTING;
01250   }
01251   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 2;
01252 
01253   /* Space between the category buttons and the global settings buttons. */
01254   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01255   vert_buttons->Add(spacer);
01256 
01257   /* Bottom part of the column with buttons for global changes. */
01258   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_DROPDOWN, COLOUR_YELLOW, WID_MO_DROP_SUMMARY, STR_EMPTY, STR_NULL);
01259   leaf->SetFill(1, 1);
01260   vert_buttons->Add(leaf);
01261 
01262   leaf = new NWidgetLeaf(WWT_TEXTBTN_2, COLOUR_YELLOW, WID_MO_SOUNDTICKER, STR_STATION_BUILD_COVERAGE_OFF, STR_NULL);
01263   leaf->SetFill(1, 1);
01264   vert_buttons->Add(leaf);
01265 
01266   *biggest_index = max(*biggest_index, max<int>(WID_MO_DROP_SUMMARY, WID_MO_SOUNDTICKER));
01267   return vert_buttons;
01268 }
01269 
01271 static NWidgetBase *MakeDescriptionColumn(int *biggest_index)
01272 {
01273   NWidgetVertical *vert_desc = new NWidgetVertical;
01274 
01275   /* Top-part of the column, one row for each new category. */
01276   int widnum = WID_MO_START_OPTION;
01277   for (int i = 0; i < NT_END; i++) {
01278     NWidgetHorizontal *hor = new NWidgetHorizontal;
01279 
01280     /* Descriptive text. */
01281     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 3, _news_type_data[i].description, STR_NULL);
01282     hor->Add(leaf);
01283     /* Filling empty space to push text to the left. */
01284     NWidgetSpacer *spacer = new NWidgetSpacer(0, 0);
01285     spacer->SetFill(1, 0);
01286     hor->Add(spacer);
01287     vert_desc->Add(hor);
01288 
01289     widnum += MOS_WIDG_PER_SETTING;
01290   }
01291   *biggest_index = widnum - MOS_WIDG_PER_SETTING + 3;
01292 
01293   /* Space between the category descriptions and the global settings descriptions. */
01294   NWidgetSpacer *spacer = new NWidgetSpacer(0, MOS_ABOVE_GLOBAL_SETTINGS);
01295   vert_desc->Add(spacer);
01296 
01297   /* Bottom part of the column with descriptions of global changes. */
01298   NWidgetHorizontal *hor = new NWidgetHorizontal;
01299   NWidgetLeaf *leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WID_MO_LABEL_SUMMARY, STR_NEWS_MESSAGES_ALL, STR_NULL);
01300   hor->Add(leaf);
01301   /* Filling empty space to push text to the left. */
01302   spacer = new NWidgetSpacer(0, 0);
01303   spacer->SetFill(1, 0);
01304   hor->Add(spacer);
01305   vert_desc->Add(hor);
01306 
01307   hor = new NWidgetHorizontal;
01308   leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, WID_MO_SOUNDTICKER_LABEL, STR_NEWS_MESSAGES_SOUND, STR_NULL);
01309   hor->Add(leaf);
01310   /* Filling empty space to push text to the left. */
01311   spacer = new NWidgetSpacer(0, 0);
01312   leaf->SetFill(1, 0);
01313   hor->Add(spacer);
01314   vert_desc->Add(hor);
01315 
01316   *biggest_index = max(*biggest_index, max<int>(WID_MO_LABEL_SUMMARY, WID_MO_SOUNDTICKER_LABEL));
01317   return vert_desc;
01318 }
01319 
01320 static const NWidgetPart _nested_message_options_widgets[] = {
01321   NWidget(NWID_HORIZONTAL),
01322     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01323     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_NEWS_MESSAGE_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01324   EndContainer(),
01325   NWidget(WWT_PANEL, COLOUR_BROWN, WID_MO_BACKGROUND),
01326     NWidget(NWID_HORIZONTAL),
01327       NWidget(NWID_SPACER), SetFill(1, 0),
01328       NWidget(WWT_LABEL, COLOUR_BROWN, WID_MO_LABEL), SetMinimalSize(0, 14), SetDataTip(STR_NEWS_MESSAGE_TYPES, STR_NULL),
01329       NWidget(NWID_SPACER), SetFill(1, 0),
01330     EndContainer(),
01331     NWidget(NWID_HORIZONTAL),
01332       NWidget(NWID_SPACER), SetMinimalSize(MOS_LEFT_EDGE, 0),
01333       NWidgetFunction(MakeButtonsColumn),
01334       NWidget(NWID_SPACER), SetMinimalSize(MOS_COLUMN_SPACING, 0),
01335       NWidgetFunction(MakeDescriptionColumn),
01336       NWidget(NWID_SPACER), SetMinimalSize(MOS_RIGHT_EDGE, 0),
01337     EndContainer(),
01338     NWidget(NWID_SPACER), SetMinimalSize(0, MOS_BOTTOM_EDGE),
01339   EndContainer(),
01340 };
01341 
01342 static const WindowDesc _message_options_desc(
01343   WDP_AUTO, 0, 0,
01344   WC_GAME_OPTIONS, WC_NONE,
01345   WDF_UNCLICK_BUTTONS,
01346   _nested_message_options_widgets, lengthof(_nested_message_options_widgets)
01347 );
01348 
01352 void ShowMessageOptions()
01353 {
01354   DeleteWindowByClass(WC_GAME_OPTIONS);
01355   new MessageOptionsWindow(&_message_options_desc);
01356 }