Fix 03cc0d6: Mark level crossings dirty when removing road from them, not from bridge...
[openttd-github.git] / src / news_gui.cpp
blob1a12dad8033b7c2a52da5ecb9ac07a60cd48fdc5
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file news_gui.cpp GUI functions related to news messages. */
10 #include "stdafx.h"
11 #include "gui.h"
12 #include "viewport_func.h"
13 #include "strings_func.h"
14 #include "window_func.h"
15 #include "date_func.h"
16 #include "vehicle_base.h"
17 #include "vehicle_func.h"
18 #include "vehicle_gui.h"
19 #include "roadveh.h"
20 #include "station_base.h"
21 #include "industry.h"
22 #include "town.h"
23 #include "sound_func.h"
24 #include "string_func.h"
25 #include "widgets/dropdown_func.h"
26 #include "statusbar_gui.h"
27 #include "company_manager_face.h"
28 #include "company_func.h"
29 #include "engine_base.h"
30 #include "engine_gui.h"
31 #include "core/geometry_func.hpp"
32 #include "command_func.h"
33 #include "company_base.h"
34 #include "settings_internal.h"
35 #include "guitimer_func.h"
36 #include "group_gui.h"
37 #include "zoom_func.h"
38 #include "news_cmd.h"
40 #include "widgets/news_widget.h"
42 #include "table/strings.h"
44 #include "safeguards.h"
46 const NewsItem *_statusbar_news_item = nullptr;
48 static uint MIN_NEWS_AMOUNT = 30; ///< preferred minimum amount of news messages
49 static uint MAX_NEWS_AMOUNT = 1 << 10; ///< Do not exceed this number of news messages
50 static uint _total_news = 0; ///< current number of news items
51 static NewsItem *_oldest_news = nullptr; ///< head of news items queue
52 NewsItem *_latest_news = nullptr; ///< tail of news items queue
54 /**
55 * Forced news item.
56 * Users can force an item by accessing the history or "last message".
57 * If the message being shown was forced by the user, a pointer is stored
58 * in _forced_news. Otherwise, \a _forced_news variable is nullptr.
60 static const NewsItem *_forced_news = nullptr;
62 /** Current news item (last item shown regularly). */
63 static const NewsItem *_current_news = nullptr;
66 /**
67 * Get the position a news-reference is referencing.
68 * @param reftype The type of reference.
69 * @param ref The reference.
70 * @return A tile for the referenced object, or INVALID_TILE if none.
72 static TileIndex GetReferenceTile(NewsReferenceType reftype, uint32 ref)
74 switch (reftype) {
75 case NR_TILE: return (TileIndex)ref;
76 case NR_STATION: return Station::Get((StationID)ref)->xy;
77 case NR_INDUSTRY: return Industry::Get((IndustryID)ref)->location.tile + TileDiffXY(1, 1);
78 case NR_TOWN: return Town::Get((TownID)ref)->xy;
79 default: return INVALID_TILE;
83 /* Normal news items. */
84 static const NWidgetPart _nested_normal_news_widgets[] = {
85 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
86 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
87 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
88 NWidget(NWID_SPACER), SetFill(1, 0),
89 NWidget(NWID_VERTICAL),
90 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
91 NWidget(NWID_SPACER), SetFill(0, 1),
92 EndContainer(),
93 EndContainer(),
94 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 154), SetPadding(0, 5, 1, 5),
95 EndContainer(),
98 static WindowDesc _normal_news_desc(
99 WDP_MANUAL, nullptr, 0, 0,
100 WC_NEWS_WINDOW, WC_NONE,
102 _nested_normal_news_widgets, lengthof(_nested_normal_news_widgets)
105 /* New vehicles news items. */
106 static const NWidgetPart _nested_vehicle_news_widgets[] = {
107 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
108 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
109 NWidget(NWID_VERTICAL),
110 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
111 NWidget(NWID_SPACER), SetFill(0, 1),
112 EndContainer(),
113 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_VEH_TITLE), SetFill(1, 1), SetMinimalSize(419, 55), SetDataTip(STR_EMPTY, STR_NULL),
114 EndContainer(),
115 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_VEH_BKGND), SetPadding(0, 25, 1, 25),
116 NWidget(NWID_VERTICAL),
117 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_NAME), SetMinimalSize(369, 33), SetFill(1, 0),
118 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_SPR), SetMinimalSize(369, 32), SetFill(1, 0),
119 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_INFO), SetMinimalSize(369, 46), SetFill(1, 0),
120 EndContainer(),
121 EndContainer(),
122 EndContainer(),
125 static WindowDesc _vehicle_news_desc(
126 WDP_MANUAL, nullptr, 0, 0,
127 WC_NEWS_WINDOW, WC_NONE,
129 _nested_vehicle_news_widgets, lengthof(_nested_vehicle_news_widgets)
132 /* Company news items. */
133 static const NWidgetPart _nested_company_news_widgets[] = {
134 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
135 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
136 NWidget(NWID_VERTICAL),
137 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
138 NWidget(NWID_SPACER), SetFill(0, 1),
139 EndContainer(),
140 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_TITLE), SetFill(1, 1), SetMinimalSize(410, 20), SetDataTip(STR_EMPTY, STR_NULL),
141 EndContainer(),
142 NWidget(NWID_HORIZONTAL), SetPadding(0, 1, 1, 1),
143 NWidget(NWID_VERTICAL),
144 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_FACE), SetMinimalSize(93, 119), SetPadding(2, 6, 2, 1),
145 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_NAME), SetMinimalSize(93, 24), SetPadding(0, 0, 0, 1),
146 NWidget(NWID_SPACER), SetFill(0, 1),
147 EndContainer(),
148 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_COMPANY_MSG), SetFill(1, 1), SetMinimalSize(328, 150),
149 EndContainer(),
150 EndContainer(),
153 static WindowDesc _company_news_desc(
154 WDP_MANUAL, nullptr, 0, 0,
155 WC_NEWS_WINDOW, WC_NONE,
157 _nested_company_news_widgets, lengthof(_nested_company_news_widgets)
160 /* Thin news items. */
161 static const NWidgetPart _nested_thin_news_widgets[] = {
162 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
163 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
164 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
165 NWidget(NWID_SPACER), SetFill(1, 0),
166 NWidget(NWID_VERTICAL),
167 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
168 NWidget(NWID_SPACER), SetFill(0, 1),
169 EndContainer(),
170 EndContainer(),
171 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 48), SetFill(1, 0), SetPadding(0, 5, 0, 5),
172 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetMinimalSize(426, 70), SetPadding(1, 2, 2, 2),
173 EndContainer(),
176 static WindowDesc _thin_news_desc(
177 WDP_MANUAL, nullptr, 0, 0,
178 WC_NEWS_WINDOW, WC_NONE,
180 _nested_thin_news_widgets, lengthof(_nested_thin_news_widgets)
183 /* Small news items. */
184 static const NWidgetPart _nested_small_news_widgets[] = {
185 /* Caption + close box. The caption is no WWT_CAPTION as the window shall not be moveable and so on. */
186 NWidget(NWID_HORIZONTAL),
187 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE, WID_N_CLOSEBOX),
188 NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_N_CAPTION), SetFill(1, 0),
189 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_N_SHOW_GROUP), SetMinimalSize(14, 11), SetResize(1, 0),
190 SetDataTip(STR_NULL /* filled in later */, STR_NEWS_SHOW_VEHICLE_GROUP_TOOLTIP),
191 EndContainer(),
193 /* Main part */
194 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_N_HEADLINE),
195 NWidget(WWT_INSET, COLOUR_LIGHT_BLUE, WID_N_INSET), SetPadding(2, 2, 2, 2),
196 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetMinimalSize(274, 47), SetFill(1, 0),
197 EndContainer(),
198 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(275, 20), SetFill(1, 0), SetPadding(0, 5, 0, 5),
199 EndContainer(),
202 static WindowDesc _small_news_desc(
203 WDP_MANUAL, nullptr, 0, 0,
204 WC_NEWS_WINDOW, WC_NONE,
206 _nested_small_news_widgets, lengthof(_nested_small_news_widgets)
210 * Window layouts for news items.
212 static WindowDesc* _news_window_layout[] = {
213 &_thin_news_desc, ///< NF_THIN
214 &_small_news_desc, ///< NF_SMALL
215 &_normal_news_desc, ///< NF_NORMAL
216 &_vehicle_news_desc, ///< NF_VEHICLE
217 &_company_news_desc, ///< NF_COMPANY
220 WindowDesc* GetNewsWindowLayout(NewsFlag flags)
222 uint layout = GB(flags, NFB_WINDOW_LAYOUT, NFB_WINDOW_LAYOUT_COUNT);
223 assert(layout < lengthof(_news_window_layout));
224 return _news_window_layout[layout];
228 * Per-NewsType data
230 static NewsTypeData _news_type_data[] = {
231 /* name, age, sound, */
232 NewsTypeData("news_display.arrival_player", 60, SND_1D_APPLAUSE ), ///< NT_ARRIVAL_COMPANY
233 NewsTypeData("news_display.arrival_other", 60, SND_1D_APPLAUSE ), ///< NT_ARRIVAL_OTHER
234 NewsTypeData("news_display.accident", 90, SND_BEGIN ), ///< NT_ACCIDENT
235 NewsTypeData("news_display.accident_other", 90, SND_BEGIN ), ///< NT_ACCIDENT_OTHER
236 NewsTypeData("news_display.company_info", 60, SND_BEGIN ), ///< NT_COMPANY_INFO
237 NewsTypeData("news_display.open", 90, SND_BEGIN ), ///< NT_INDUSTRY_OPEN
238 NewsTypeData("news_display.close", 90, SND_BEGIN ), ///< NT_INDUSTRY_CLOSE
239 NewsTypeData("news_display.economy", 30, SND_BEGIN ), ///< NT_ECONOMY
240 NewsTypeData("news_display.production_player", 30, SND_BEGIN ), ///< NT_INDUSTRY_COMPANY
241 NewsTypeData("news_display.production_other", 30, SND_BEGIN ), ///< NT_INDUSTRY_OTHER
242 NewsTypeData("news_display.production_nobody", 30, SND_BEGIN ), ///< NT_INDUSTRY_NOBODY
243 NewsTypeData("news_display.advice", 150, SND_BEGIN ), ///< NT_ADVICE
244 NewsTypeData("news_display.new_vehicles", 30, SND_1E_NEW_ENGINE), ///< NT_NEW_VEHICLES
245 NewsTypeData("news_display.acceptance", 90, SND_BEGIN ), ///< NT_ACCEPTANCE
246 NewsTypeData("news_display.subsidies", 180, SND_BEGIN ), ///< NT_SUBSIDIES
247 NewsTypeData("news_display.general", 60, SND_BEGIN ), ///< NT_GENERAL
250 static_assert(lengthof(_news_type_data) == NT_END);
253 * Return the news display option.
254 * @return display options
256 NewsDisplay NewsTypeData::GetDisplay() const
258 const SettingDesc *sd = GetSettingFromName(this->name);
259 assert(sd != nullptr && sd->IsIntSetting());
260 return (NewsDisplay)sd->AsIntSetting()->Read(nullptr);
263 /** Window class displaying a news item. */
264 struct NewsWindow : Window {
265 uint16 chat_height; ///< Height of the chat window.
266 uint16 status_height; ///< Height of the status bar window
267 const NewsItem *ni; ///< News item to display.
268 static int duration; ///< Remaining time for showing the current news message (may only be access while a news item is displayed).
270 static const uint TIMER_INTERVAL = 210; ///< Scrolling interval, scaled by line text line height. This value chosen to maintain the 15ms at normal zoom.
271 GUITimer timer;
273 NewsWindow(WindowDesc *desc, const NewsItem *ni) : Window(desc), ni(ni)
275 NewsWindow::duration = 16650;
276 const Window *w = FindWindowByClass(WC_SEND_NETWORK_MSG);
277 this->chat_height = (w != nullptr) ? w->height : 0;
278 this->status_height = FindWindowById(WC_STATUS_BAR, 0)->height;
280 this->flags |= WF_DISABLE_VP_SCROLL;
282 this->CreateNestedTree();
284 /* For company news with a face we have a separate headline in param[0] */
285 if (desc == &_company_news_desc) this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = this->ni->params[0];
287 NWidgetCore *nwid = this->GetWidget<NWidgetCore>(WID_N_SHOW_GROUP);
288 if (ni->reftype1 == NR_VEHICLE && nwid != nullptr) {
289 const Vehicle *v = Vehicle::Get(ni->ref1);
290 switch (v->type) {
291 case VEH_TRAIN:
292 nwid->widget_data = STR_TRAIN;
293 break;
294 case VEH_ROAD:
295 nwid->widget_data = RoadVehicle::From(v)->IsBus() ? STR_BUS : STR_LORRY;
296 break;
297 case VEH_SHIP:
298 nwid->widget_data = STR_SHIP;
299 break;
300 case VEH_AIRCRAFT:
301 nwid->widget_data = STR_PLANE;
302 break;
303 default:
304 break; // Do nothing
308 this->FinishInitNested(0);
310 /* Initialize viewport if it exists. */
311 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_N_VIEWPORT);
312 if (nvp != nullptr) {
313 nvp->InitializeViewport(this, ni->reftype1 == NR_VEHICLE ? 0x80000000 | ni->ref1 : (uint32)GetReferenceTile(ni->reftype1, ni->ref1),ScaleZoomGUI(ZOOM_LVL_NEWS));
314 if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags |= ND_NO_TRANSPARENCY;
315 if ((this->ni->flags & NF_INCOLOUR) == 0) {
316 nvp->disp_flags |= ND_SHADE_GREY;
317 } else if (this->ni->flags & NF_SHADE) {
318 nvp->disp_flags |= ND_SHADE_DIMMED;
322 PositionNewsMessage(this);
325 void OnInit() override
327 this->timer.SetInterval(TIMER_INTERVAL / FONT_HEIGHT_NORMAL);
330 void DrawNewsBorder(const Rect &r) const
332 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_WHITE);
334 GfxFillRect(r.left, r.top, r.left, r.bottom, PC_BLACK);
335 GfxFillRect(r.right, r.top, r.right, r.bottom, PC_BLACK);
336 GfxFillRect(r.left, r.top, r.right, r.top, PC_BLACK);
337 GfxFillRect(r.left, r.bottom, r.right, r.bottom, PC_BLACK);
340 Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
342 Point pt = { 0, _screen.height };
343 return pt;
346 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
348 StringID str = STR_NULL;
349 switch (widget) {
350 case WID_N_CAPTION: {
351 /* Caption is not a real caption (so that the window cannot be moved)
352 * thus it doesn't get the default sizing of a caption. */
353 Dimension d2 = GetStringBoundingBox(STR_NEWS_MESSAGE_CAPTION);
354 d2.height += WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM;
355 *size = maxdim(*size, d2);
356 return;
359 case WID_N_MGR_FACE:
360 *size = maxdim(*size, GetSpriteSize(SPR_GRADIENT));
361 break;
363 case WID_N_MGR_NAME:
364 SetDParamStr(0, static_cast<const CompanyNewsInformation *>(this->ni->data.get())->president_name);
365 str = STR_JUST_RAW_STRING;
366 break;
368 case WID_N_MESSAGE:
369 CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
370 str = this->ni->string_id;
371 break;
373 case WID_N_COMPANY_MSG:
374 str = this->GetCompanyMessageString();
375 break;
377 case WID_N_VEH_NAME:
378 case WID_N_VEH_TITLE:
379 str = this->GetNewVehicleMessageString(widget);
380 break;
382 case WID_N_VEH_INFO: {
383 assert(this->ni->reftype1 == NR_ENGINE);
384 EngineID engine = this->ni->ref1;
385 str = GetEngineInfoString(engine);
386 break;
389 case WID_N_SHOW_GROUP:
390 if (this->ni->reftype1 == NR_VEHICLE) {
391 Dimension d2 = GetStringBoundingBox(this->GetWidget<NWidgetCore>(WID_N_SHOW_GROUP)->widget_data);
392 d2.height += WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM;
393 d2.width += WD_CAPTIONTEXT_LEFT + WD_CAPTIONTEXT_RIGHT;
394 *size = d2;
395 } else {
396 /* Hide 'Show group window' button if this news is not about a vehicle. */
397 size->width = 0;
398 size->height = 0;
399 resize->width = 0;
400 resize->height = 0;
401 fill->width = 0;
402 fill->height = 0;
404 return;
406 default:
407 return; // Do nothing
410 /* Update minimal size with length of the multi-line string. */
411 Dimension d = *size;
412 d.width = (d.width >= padding.width) ? d.width - padding.width : 0;
413 d.height = (d.height >= padding.height) ? d.height - padding.height : 0;
414 d = GetStringMultiLineBoundingBox(str, d);
415 d.width += padding.width;
416 d.height += padding.height;
417 *size = maxdim(*size, d);
420 void SetStringParameters(int widget) const override
422 if (widget == WID_N_DATE) SetDParam(0, this->ni->date);
425 void DrawWidget(const Rect &r, int widget) const override
427 switch (widget) {
428 case WID_N_CAPTION:
429 DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, TC_FROMSTRING, STR_NEWS_MESSAGE_CAPTION, SA_CENTER);
430 break;
432 case WID_N_PANEL:
433 this->DrawNewsBorder(r);
434 break;
436 case WID_N_MESSAGE:
437 CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
438 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->ni->string_id, TC_FROMSTRING, SA_CENTER);
439 break;
441 case WID_N_MGR_FACE: {
442 const CompanyNewsInformation *cni = static_cast<const CompanyNewsInformation*>(this->ni->data.get());
443 DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top);
444 GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
445 break;
447 case WID_N_MGR_NAME: {
448 const CompanyNewsInformation *cni = static_cast<const CompanyNewsInformation*>(this->ni->data.get());
449 SetDParamStr(0, cni->president_name);
450 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
451 break;
453 case WID_N_COMPANY_MSG:
454 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetCompanyMessageString(), TC_FROMSTRING, SA_CENTER);
455 break;
457 case WID_N_VEH_BKGND:
458 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_GREY);
459 break;
461 case WID_N_VEH_NAME:
462 case WID_N_VEH_TITLE:
463 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetNewVehicleMessageString(widget), TC_FROMSTRING, SA_CENTER);
464 break;
466 case WID_N_VEH_SPR: {
467 assert(this->ni->reftype1 == NR_ENGINE);
468 EngineID engine = this->ni->ref1;
469 DrawVehicleEngine(r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW);
470 GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
471 break;
473 case WID_N_VEH_INFO: {
474 assert(this->ni->reftype1 == NR_ENGINE);
475 EngineID engine = this->ni->ref1;
476 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
477 break;
482 void OnClick(Point pt, int widget, int click_count) override
484 switch (widget) {
485 case WID_N_CLOSEBOX:
486 NewsWindow::duration = 0;
487 this->Close();
488 _forced_news = nullptr;
489 break;
491 case WID_N_CAPTION:
492 if (this->ni->reftype1 == NR_VEHICLE) {
493 const Vehicle *v = Vehicle::Get(this->ni->ref1);
494 ShowVehicleViewWindow(v);
496 break;
498 case WID_N_VIEWPORT:
499 break; // Ignore clicks
501 case WID_N_SHOW_GROUP:
502 if (this->ni->reftype1 == NR_VEHICLE) {
503 const Vehicle *v = Vehicle::Get(this->ni->ref1);
504 ShowCompanyGroupForVehicle(v);
506 break;
507 default:
508 if (this->ni->reftype1 == NR_VEHICLE) {
509 const Vehicle *v = Vehicle::Get(this->ni->ref1);
510 ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
511 } else {
512 TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
513 TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
514 if (_ctrl_pressed) {
515 if (tile1 != INVALID_TILE) ShowExtraViewportWindow(tile1);
516 if (tile2 != INVALID_TILE) ShowExtraViewportWindow(tile2);
517 } else {
518 if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
519 ScrollMainWindowToTile(tile2);
523 break;
527 void OnResize() override
529 if (this->viewport != nullptr) {
530 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_N_VIEWPORT);
531 nvp->UpdateViewportCoordinates(this);
533 if (ni->reftype1 != NR_VEHICLE) {
534 ScrollWindowToTile(GetReferenceTile(ni->reftype1, ni->ref1), this, true); // Re-center viewport.
540 * Some data on this window has become invalid.
541 * @param data Information about the changed data.
542 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
544 void OnInvalidateData(int data = 0, bool gui_scope = true) override
546 if (!gui_scope) return;
547 /* The chatbar has notified us that is was either created or closed */
548 int newtop = this->top + this->chat_height - data;
549 this->chat_height = data;
550 this->SetWindowTop(newtop);
553 void OnRealtimeTick(uint delta_ms) override
555 int count = this->timer.CountElapsed(delta_ms);
556 if (count > 0) {
557 /* Scroll up newsmessages from the bottom */
558 int newtop = std::max(this->top - 2 * count, _screen.height - this->height - this->status_height - this->chat_height);
559 this->SetWindowTop(newtop);
562 /* Decrement the news timer. We don't need to action an elapsed event here,
563 * so no need to use TimerElapsed(). */
564 if (NewsWindow::duration > 0) NewsWindow::duration -= delta_ms;
567 private:
569 * Moves the window to a new #top coordinate. Makes screen dirty where needed.
570 * @param newtop new top coordinate
572 void SetWindowTop(int newtop)
574 if (this->top == newtop) return;
576 int mintop = std::min(newtop, this->top);
577 int maxtop = std::max(newtop, this->top);
578 if (this->viewport != nullptr) this->viewport->top += newtop - this->top;
579 this->top = newtop;
581 AddDirtyBlock(this->left, mintop, this->left + this->width, maxtop + this->height);
584 StringID GetCompanyMessageString() const
586 /* Company news with a face have a separate headline, so the normal message is shifted by two params */
587 CopyInDParam(0, this->ni->params + 2, lengthof(this->ni->params) - 2);
588 return this->ni->params[1];
591 StringID GetNewVehicleMessageString(int widget) const
593 assert(this->ni->reftype1 == NR_ENGINE);
594 EngineID engine = this->ni->ref1;
596 switch (widget) {
597 case WID_N_VEH_TITLE:
598 SetDParam(0, GetEngineCategoryName(engine));
599 return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
601 case WID_N_VEH_NAME:
602 SetDParam(0, engine);
603 return STR_NEWS_NEW_VEHICLE_TYPE;
605 default:
606 NOT_REACHED();
611 /* static */ int NewsWindow::duration = 0; // Instance creation.
613 /** Open up an own newspaper window for the news item */
614 static void ShowNewspaper(const NewsItem *ni)
616 SoundFx sound = _news_type_data[ni->type].sound;
617 if (sound != 0 && _settings_client.sound.news_full) SndPlayFx(sound);
619 new NewsWindow(GetNewsWindowLayout(ni->flags), ni);
622 /** Show news item in the ticker */
623 static void ShowTicker(const NewsItem *ni)
625 if (_settings_client.sound.news_ticker) SndPlayFx(SND_16_NEWS_TICKER);
627 _statusbar_news_item = ni;
628 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
631 /** Initialize the news-items data structures */
632 void InitNewsItemStructs()
634 for (NewsItem *ni = _oldest_news; ni != nullptr; ) {
635 NewsItem *next = ni->next;
636 delete ni;
637 ni = next;
640 _total_news = 0;
641 _oldest_news = nullptr;
642 _latest_news = nullptr;
643 _forced_news = nullptr;
644 _current_news = nullptr;
645 _statusbar_news_item = nullptr;
646 NewsWindow::duration = 0;
650 * Are we ready to show another ticker item?
651 * Only if nothing is in the newsticker is displayed
653 static bool ReadyForNextTickerItem()
655 const NewsItem *ni = _statusbar_news_item;
656 if (ni == nullptr) return true;
658 /* Ticker message
659 * Check if the status bar message is still being displayed? */
660 return !IsNewsTickerShown();
664 * Are we ready to show another news item?
665 * Only if no newspaper is displayed
667 static bool ReadyForNextNewsItem()
669 const NewsItem *ni = _forced_news == nullptr ? _current_news : _forced_news;
670 if (ni == nullptr) return true;
672 /* neither newsticker nor newspaper are running */
673 return (NewsWindow::duration <= 0 || FindWindowById(WC_NEWS_WINDOW, 0) == nullptr);
676 /** Move to the next ticker item */
677 static void MoveToNextTickerItem()
679 /* There is no status bar, so no reason to show news;
680 * especially important with the end game screen when
681 * there is no status bar but possible news. */
682 if (FindWindowById(WC_STATUS_BAR, 0) == nullptr) return;
684 /* if we're not at the last item, then move on */
685 while (_statusbar_news_item != _latest_news) {
686 _statusbar_news_item = (_statusbar_news_item == nullptr) ? _oldest_news : _statusbar_news_item->next;
687 const NewsItem *ni = _statusbar_news_item;
688 const NewsType type = ni->type;
690 /* check the date, don't show too old items */
691 if (_date - _news_type_data[type].age > ni->date) continue;
693 switch (_news_type_data[type].GetDisplay()) {
694 default: NOT_REACHED();
695 case ND_OFF: // Off - show nothing only a small reminder in the status bar
696 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
697 break;
699 case ND_SUMMARY: // Summary - show ticker
700 ShowTicker(ni);
701 break;
703 case ND_FULL: // Full - show newspaper, skipped here
704 continue;
706 return;
710 /** Move to the next news item */
711 static void MoveToNextNewsItem()
713 /* There is no status bar, so no reason to show news;
714 * especially important with the end game screen when
715 * there is no status bar but possible news. */
716 if (FindWindowById(WC_STATUS_BAR, 0) == nullptr) return;
718 CloseWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
719 _forced_news = nullptr;
721 /* if we're not at the last item, then move on */
722 while (_current_news != _latest_news) {
723 _current_news = (_current_news == nullptr) ? _oldest_news : _current_news->next;
724 const NewsItem *ni = _current_news;
725 const NewsType type = ni->type;
727 /* check the date, don't show too old items */
728 if (_date - _news_type_data[type].age > ni->date) continue;
730 switch (_news_type_data[type].GetDisplay()) {
731 default: NOT_REACHED();
732 case ND_OFF: // Off - show nothing only a small reminder in the status bar, skipped here
733 continue;
735 case ND_SUMMARY: // Summary - show ticker, skipped here
736 continue;
738 case ND_FULL: // Full - show newspaper
739 ShowNewspaper(ni);
740 break;
742 return;
746 /** Delete a news item from the queue */
747 static void DeleteNewsItem(NewsItem *ni)
749 /* Delete the news from the news queue. */
750 if (ni->prev != nullptr) {
751 ni->prev->next = ni->next;
752 } else {
753 assert(_oldest_news == ni);
754 _oldest_news = ni->next;
757 if (ni->next != nullptr) {
758 ni->next->prev = ni->prev;
759 } else {
760 assert(_latest_news == ni);
761 _latest_news = ni->prev;
764 _total_news--;
766 if (_forced_news == ni || _current_news == ni) {
767 /* When we're the current news, go to the previous item first;
768 * we just possibly made that the last news item. */
769 if (_current_news == ni) _current_news = ni->prev;
771 /* About to remove the currently forced item (shown as newspapers) ||
772 * about to remove the currently displayed item (newspapers) */
773 MoveToNextNewsItem();
776 if (_statusbar_news_item == ni) {
777 /* When we're the current news, go to the previous item first;
778 * we just possibly made that the last news item. */
779 _statusbar_news_item = ni->prev;
781 /* About to remove the currently displayed item (ticker, or just a reminder) */
782 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
783 MoveToNextTickerItem();
786 delete ni;
788 SetWindowDirty(WC_MESSAGE_HISTORY, 0);
792 * Create a new newsitem to be shown.
793 * @param string_id String to display.
794 * @param type The type of news.
795 * @param flags Flags related to how to display the news.
796 * @param reftype1 Type of ref1.
797 * @param ref1 Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted.
798 * @param reftype2 Type of ref2.
799 * @param ref2 Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted.
800 * @param data Pointer to data that must be released once the news message is cleared.
802 * @see NewsSubtype
804 NewsItem::NewsItem(StringID string_id, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, const NewsAllocatedData *data) :
805 string_id(string_id), date(_date), type(type), flags(flags), reftype1(reftype1), reftype2(reftype2), ref1(ref1), ref2(ref2), data(data)
807 /* show this news message in colour? */
808 if (_cur_year >= _settings_client.gui.coloured_news_year) this->flags |= NF_INCOLOUR;
809 CopyOutDParam(this->params, 0, lengthof(this->params));
813 * Add a new newsitem to be shown.
814 * @param string String to display
815 * @param type news category
816 * @param flags display flags for the news
817 * @param reftype1 Type of ref1
818 * @param ref1 Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted.
819 * @param reftype2 Type of ref2
820 * @param ref2 Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted.
821 * @param data Pointer to data that must be released once the news message is cleared.
823 * @see NewsSubtype
825 void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, const NewsAllocatedData *data)
827 if (_game_mode == GM_MENU) return;
829 /* Create new news item node */
830 NewsItem *ni = new NewsItem(string, type, flags, reftype1, ref1, reftype2, ref2, data);
832 if (_total_news++ == 0) {
833 assert(_oldest_news == nullptr);
834 _oldest_news = ni;
835 ni->prev = nullptr;
836 } else {
837 assert(_latest_news->next == nullptr);
838 _latest_news->next = ni;
839 ni->prev = _latest_news;
842 ni->next = nullptr;
843 _latest_news = ni;
845 /* Keep the number of stored news items to a managable number */
846 if (_total_news > MAX_NEWS_AMOUNT) {
847 DeleteNewsItem(_oldest_news);
850 SetWindowDirty(WC_MESSAGE_HISTORY, 0);
854 * Create a new custom news item.
855 * @param flags type of operation
856 * @aram type NewsType of the message.
857 * @param reftype1 NewsReferenceType of first reference.
858 * @param company Company this news message is for.
859 * @param reference First reference of the news message.
860 * @param text The text of the news message.
861 * @return the cost of this operation or an error
863 CommandCost CmdCustomNewsItem(DoCommandFlag flags, NewsType type, NewsReferenceType reftype1, CompanyID company, uint32 reference, const std::string &text)
865 if (_current_company != OWNER_DEITY) return CMD_ERROR;
867 if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR;
868 if (type >= NT_END) return CMD_ERROR;
869 if (text.empty()) return CMD_ERROR;
871 switch (reftype1) {
872 case NR_NONE: break;
873 case NR_TILE:
874 if (!IsValidTile(reference)) return CMD_ERROR;
875 break;
877 case NR_VEHICLE:
878 if (!Vehicle::IsValidID(reference)) return CMD_ERROR;
879 break;
881 case NR_STATION:
882 if (!Station::IsValidID(reference)) return CMD_ERROR;
883 break;
885 case NR_INDUSTRY:
886 if (!Industry::IsValidID(reference)) return CMD_ERROR;
887 break;
889 case NR_TOWN:
890 if (!Town::IsValidID(reference)) return CMD_ERROR;
891 break;
893 case NR_ENGINE:
894 if (!Engine::IsValidID(reference)) return CMD_ERROR;
895 break;
897 default: return CMD_ERROR;
900 if (company != INVALID_OWNER && company != _local_company) return CommandCost();
902 if (flags & DC_EXEC) {
903 NewsStringData *news = new NewsStringData(text);
904 SetDParamStr(0, news->string);
905 AddNewsItem(STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype1, reference, NR_NONE, UINT32_MAX, news);
908 return CommandCost();
912 * Delete a news item type about a vehicle.
913 * When the news item type is INVALID_STRING_ID all news about the vehicle gets deleted.
914 * @param vid The vehicle to remove the news for.
915 * @param news The news type to remove.
917 void DeleteVehicleNews(VehicleID vid, StringID news)
919 NewsItem *ni = _oldest_news;
921 while (ni != nullptr) {
922 NewsItem *next = ni->next;
923 if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
924 (news == INVALID_STRING_ID || ni->string_id == news)) {
925 DeleteNewsItem(ni);
927 ni = next;
932 * Remove news regarding given station so there are no 'unknown station now accepts Mail'
933 * or 'First train arrived at unknown station' news items.
934 * @param sid station to remove news about
936 void DeleteStationNews(StationID sid)
938 NewsItem *ni = _oldest_news;
940 while (ni != nullptr) {
941 NewsItem *next = ni->next;
942 if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
943 DeleteNewsItem(ni);
945 ni = next;
950 * Remove news regarding given industry
951 * @param iid industry to remove news about
953 void DeleteIndustryNews(IndustryID iid)
955 NewsItem *ni = _oldest_news;
957 while (ni != nullptr) {
958 NewsItem *next = ni->next;
959 if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
960 DeleteNewsItem(ni);
962 ni = next;
967 * Remove engine announcements for invalid engines.
969 void DeleteInvalidEngineNews()
971 NewsItem *ni = _oldest_news;
973 while (ni != nullptr) {
974 NewsItem *next = ni->next;
975 if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
976 (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
977 DeleteNewsItem(ni);
979 ni = next;
983 static void RemoveOldNewsItems()
985 NewsItem *next;
986 for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != nullptr; cur = next) {
987 next = cur->next;
988 if (_date - _news_type_data[cur->type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
993 * Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.
994 * @note Viewports of currently displayed news is changed via #ChangeVehicleViewports
995 * @param from_index the old vehicle ID
996 * @param to_index the new vehicle ID
998 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
1000 for (NewsItem *ni = _oldest_news; ni != nullptr; ni = ni->next) {
1001 if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
1002 if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
1003 if (ni->flags & NF_VEHICLE_PARAM0 && ni->params[0] == from_index) ni->params[0] = to_index;
1007 void NewsLoop()
1009 /* no news item yet */
1010 if (_total_news == 0) return;
1012 static byte _last_clean_month = 0;
1014 if (_last_clean_month != _cur_month) {
1015 RemoveOldNewsItems();
1016 _last_clean_month = _cur_month;
1019 if (ReadyForNextTickerItem()) MoveToNextTickerItem();
1020 if (ReadyForNextNewsItem()) MoveToNextNewsItem();
1023 /** Do a forced show of a specific message */
1024 static void ShowNewsMessage(const NewsItem *ni)
1026 assert(_total_news != 0);
1028 /* Delete the news window */
1029 CloseWindowById(WC_NEWS_WINDOW, 0);
1031 /* setup forced news item */
1032 _forced_news = ni;
1034 if (_forced_news != nullptr) {
1035 CloseWindowById(WC_NEWS_WINDOW, 0);
1036 ShowNewspaper(ni);
1041 * Close active news message window
1042 * @return true if a window was closed.
1044 bool HideActiveNewsMessage() {
1045 NewsWindow *w = (NewsWindow*)FindWindowById(WC_NEWS_WINDOW, 0);
1046 if (w == nullptr) return false;
1047 w->Close();
1048 return true;
1051 /** Show previous news item */
1052 void ShowLastNewsMessage()
1054 const NewsItem *ni = nullptr;
1055 if (_total_news == 0) {
1056 return;
1057 } else if (_forced_news == nullptr) {
1058 /* Not forced any news yet, show the current one, unless a news window is
1059 * open (which can only be the current one), then show the previous item */
1060 if (_current_news == nullptr) {
1061 /* No news were shown yet resp. the last shown one was already deleted.
1062 * Threat this as if _forced_news reached _oldest_news; so, wrap around and start anew with the latest. */
1063 ni = _latest_news;
1064 } else {
1065 const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
1066 ni = (w == nullptr || (_current_news == _oldest_news)) ? _current_news : _current_news->prev;
1068 } else if (_forced_news == _oldest_news) {
1069 /* We have reached the oldest news, start anew with the latest */
1070 ni = _latest_news;
1071 } else {
1072 /* 'Scrolling' through news history show each one in turn */
1073 ni = _forced_news->prev;
1075 bool wrap = false;
1076 for (;;) {
1077 if (_news_type_data[ni->type].GetDisplay() != ND_OFF) {
1078 ShowNewsMessage(ni);
1079 break;
1082 ni = ni->prev;
1083 if (ni == nullptr) {
1084 if (wrap) break;
1085 /* We have reached the oldest news, start anew with the latest */
1086 ni = _latest_news;
1087 wrap = true;
1094 * Draw an unformatted news message truncated to a maximum length. If
1095 * length exceeds maximum length it will be postfixed by '...'
1096 * @param left the left most location for the string
1097 * @param right the right most location for the string
1098 * @param y position of the string
1099 * @param colour the colour the string will be shown in
1100 * @param *ni NewsItem being printed
1102 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
1104 char buffer[512], buffer2[512];
1105 StringID str;
1107 CopyInDParam(0, ni->params, lengthof(ni->params));
1108 str = ni->string_id;
1110 GetString(buffer, str, lastof(buffer));
1111 /* Copy the just gotten string to another buffer to remove any formatting
1112 * from it such as big fonts, etc. */
1113 const char *ptr = buffer;
1114 char *dest = buffer2;
1115 WChar c_last = '\0';
1116 for (;;) {
1117 WChar c = Utf8Consume(&ptr);
1118 if (c == 0) break;
1119 /* Make a space from a newline, but ignore multiple newlines */
1120 if (c == '\n' && c_last != '\n') {
1121 dest[0] = ' ';
1122 dest++;
1123 } else if (c == '\r') {
1124 dest[0] = dest[1] = dest[2] = dest[3] = ' ';
1125 dest += 4;
1126 } else if (IsPrintable(c)) {
1127 dest += Utf8Encode(dest, c);
1129 c_last = c;
1132 *dest = '\0';
1133 /* Truncate and show string; postfixed by '...' if necessary */
1134 DrawString(left, right, y, buffer2, colour);
1137 struct MessageHistoryWindow : Window {
1138 static const int top_spacing; ///< Additional spacing at the top of the #WID_MH_BACKGROUND widget.
1139 static const int bottom_spacing; ///< Additional spacing at the bottom of the #WID_MH_BACKGROUND widget.
1141 int line_height; /// < Height of a single line in the news history window including spacing.
1142 int date_width; /// < Width needed for the date part.
1144 Scrollbar *vscroll;
1146 MessageHistoryWindow(WindowDesc *desc) : Window(desc)
1148 this->CreateNestedTree();
1149 this->vscroll = this->GetScrollbar(WID_MH_SCROLLBAR);
1150 this->FinishInitNested(); // Initializes 'this->line_height' and 'this->date_width'.
1151 this->OnInvalidateData(0);
1154 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1156 if (widget == WID_MH_BACKGROUND) {
1157 this->line_height = FONT_HEIGHT_NORMAL + 2;
1158 resize->height = this->line_height;
1160 /* Months are off-by-one, so it's actually 8. Not using
1161 * month 12 because the 1 is usually less wide. */
1162 SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 7, 30));
1163 this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
1165 size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
1166 size->width = std::max(200u, size->width); // At least 200 pixels wide.
1170 void OnPaint() override
1172 this->OnInvalidateData(0);
1173 this->DrawWidgets();
1176 void DrawWidget(const Rect &r, int widget) const override
1178 if (widget != WID_MH_BACKGROUND || _total_news == 0) return;
1180 /* Find the first news item to display. */
1181 NewsItem *ni = _latest_news;
1182 for (int n = this->vscroll->GetPosition(); n > 0; n--) {
1183 ni = ni->prev;
1184 if (ni == nullptr) return;
1187 /* Fill the widget with news items. */
1188 int y = r.top + this->top_spacing;
1189 bool rtl = _current_text_dir == TD_RTL;
1190 uint date_left = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
1191 uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
1192 uint news_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT + ScaleFontTrad(5);
1193 uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT - ScaleFontTrad(5) : r.right - WD_FRAMERECT_RIGHT;
1194 for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
1195 SetDParam(0, ni->date);
1196 DrawString(date_left, date_right, y, STR_SHORT_DATE);
1198 DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
1199 y += this->line_height;
1201 ni = ni->prev;
1202 if (ni == nullptr) return;
1207 * Some data on this window has become invalid.
1208 * @param data Information about the changed data.
1209 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1211 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1213 if (!gui_scope) return;
1214 this->vscroll->SetCount(_total_news);
1217 void OnClick(Point pt, int widget, int click_count) override
1219 if (widget == WID_MH_BACKGROUND) {
1220 NewsItem *ni = _latest_news;
1221 if (ni == nullptr) return;
1223 for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_MH_BACKGROUND, WD_FRAMERECT_TOP); n > 0; n--) {
1224 ni = ni->prev;
1225 if (ni == nullptr) return;
1228 ShowNewsMessage(ni);
1232 void OnResize() override
1234 this->vscroll->SetCapacityFromWidget(this, WID_MH_BACKGROUND);
1238 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
1239 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
1241 static const NWidgetPart _nested_message_history[] = {
1242 NWidget(NWID_HORIZONTAL),
1243 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1244 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1245 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1246 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1247 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1248 EndContainer(),
1250 NWidget(NWID_HORIZONTAL),
1251 NWidget(WWT_PANEL, COLOUR_BROWN, WID_MH_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR),
1252 EndContainer(),
1253 NWidget(NWID_VERTICAL),
1254 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_MH_SCROLLBAR),
1255 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1256 EndContainer(),
1257 EndContainer(),
1260 static WindowDesc _message_history_desc(
1261 WDP_AUTO, "list_news", 400, 140,
1262 WC_MESSAGE_HISTORY, WC_NONE,
1264 _nested_message_history, lengthof(_nested_message_history)
1267 /** Display window with news messages history */
1268 void ShowMessageHistory()
1270 CloseWindowById(WC_MESSAGE_HISTORY, 0);
1271 new MessageHistoryWindow(&_message_history_desc);