Translations update
[openttd/fttd.git] / src / news_gui.cpp
blob175960f86cdba9ea0b4d05217bde1904416b281a
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file news_gui.cpp GUI functions related to news messages. */
12 #include "stdafx.h"
13 #include "gui.h"
14 #include "viewport_func.h"
15 #include "strings_func.h"
16 #include "window_func.h"
17 #include "date_func.h"
18 #include "vehicle_base.h"
19 #include "vehicle_func.h"
20 #include "vehicle_gui.h"
21 #include "station_base.h"
22 #include "industry.h"
23 #include "town.h"
24 #include "sound_func.h"
25 #include "string.h"
26 #include "widgets/dropdown_func.h"
27 #include "statusbar_gui.h"
28 #include "company_manager_face.h"
29 #include "company_func.h"
30 #include "engine_base.h"
31 #include "engine_gui.h"
32 #include "core/geometry_func.hpp"
33 #include "command_func.h"
34 #include "company_base.h"
35 #include "settings_internal.h"
36 #include "subsidy_base.h"
37 #include "news_func.h"
39 #include "widgets/news_widget.h"
41 #include "table/strings.h"
43 const NewsItem *_statusbar_news_item = NULL;
45 static const uint MIN_NEWS_AMOUNT = 30; ///< preferred minimum amount of news messages
46 static uint _total_news = 0; ///< current number of news items
47 static NewsItem *_oldest_news = NULL; ///< head of news items queue
48 static NewsItem *_latest_news = NULL; ///< tail of news items queue
50 /**
51 * Forced news item.
52 * Users can force an item by accessing the history or "last message".
53 * If the message being shown was forced by the user, a pointer is stored
54 * in _forced_news. Otherwise, \a _forced_news variable is NULL.
56 static const NewsItem *_forced_news = NULL; ///< item the user has asked for
58 /** Current news item (last item shown regularly). */
59 static const NewsItem *_current_news = NULL;
62 /**
63 * Get the position a news-reference is referencing.
64 * @param reftype The type of reference.
65 * @param ref The reference.
66 * @return A tile for the referenced object, or INVALID_TILE if none.
68 static TileIndex GetReferenceTile(NewsReferenceType reftype, uint32 ref)
70 switch (reftype) {
71 case NR_TILE: return (TileIndex)ref;
72 case NR_STATION: return Station::Get((StationID)ref)->xy;
73 case NR_INDUSTRY: return Industry::Get((IndustryID)ref)->location.tile + TileDiffXY(1, 1);
74 case NR_TOWN: return Town::Get((TownID)ref)->xy;
75 default: return INVALID_TILE;
79 /* Normal news items. */
80 static const NWidgetPart _nested_normal_news_widgets[] = {
81 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
82 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
83 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
84 NWidget(NWID_SPACER), SetFill(1, 0),
85 NWidget(NWID_VERTICAL),
86 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
87 NWidget(NWID_SPACER), SetFill(0, 1),
88 EndContainer(),
89 EndContainer(),
90 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 154), SetPadding(0, 5, 1, 5),
91 EndContainer(),
94 static const WindowDesc _normal_news_desc(
95 WDP_MANUAL, 0, 0,
96 WC_NEWS_WINDOW, WC_NONE,
98 _nested_normal_news_widgets, lengthof(_nested_normal_news_widgets)
101 /* New vehicles news items. */
102 static const NWidgetPart _nested_vehicle_news_widgets[] = {
103 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
104 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
105 NWidget(NWID_VERTICAL),
106 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
107 NWidget(NWID_SPACER), SetFill(0, 1),
108 EndContainer(),
109 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_VEH_TITLE), SetFill(1, 1), SetMinimalSize(419, 55), SetDataTip(STR_EMPTY, STR_NULL),
110 EndContainer(),
111 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_VEH_BKGND), SetPadding(0, 25, 1, 25),
112 NWidget(NWID_VERTICAL),
113 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_NAME), SetMinimalSize(369, 33), SetFill(1, 0),
114 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_SPR), SetMinimalSize(369, 32), SetFill(1, 0),
115 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_INFO), SetMinimalSize(369, 46), SetFill(1, 0),
116 EndContainer(),
117 EndContainer(),
118 EndContainer(),
121 static const WindowDesc _vehicle_news_desc(
122 WDP_MANUAL, 0, 0,
123 WC_NEWS_WINDOW, WC_NONE,
125 _nested_vehicle_news_widgets, lengthof(_nested_vehicle_news_widgets)
128 /* Company news items. */
129 static const NWidgetPart _nested_company_news_widgets[] = {
130 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
131 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
132 NWidget(NWID_VERTICAL),
133 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
134 NWidget(NWID_SPACER), SetFill(0, 1),
135 EndContainer(),
136 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_TITLE), SetFill(1, 1), SetMinimalSize(410, 20), SetDataTip(STR_EMPTY, STR_NULL),
137 EndContainer(),
138 NWidget(NWID_HORIZONTAL), SetPadding(0, 1, 1, 1),
139 NWidget(NWID_VERTICAL),
140 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_FACE), SetMinimalSize(93, 119), SetPadding(2, 6, 2, 1),
141 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_NAME), SetMinimalSize(93, 24), SetPadding(0, 0, 0, 1),
142 NWidget(NWID_SPACER), SetFill(0, 1),
143 EndContainer(),
144 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_COMPANY_MSG), SetFill(1, 1), SetMinimalSize(328, 150),
145 EndContainer(),
146 EndContainer(),
149 static const WindowDesc _company_news_desc(
150 WDP_MANUAL, 0, 0,
151 WC_NEWS_WINDOW, WC_NONE,
153 _nested_company_news_widgets, lengthof(_nested_company_news_widgets)
156 /* Thin news items. */
157 static const NWidgetPart _nested_thin_news_widgets[] = {
158 NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
159 NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
160 NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
161 NWidget(NWID_SPACER), SetFill(1, 0),
162 NWidget(NWID_VERTICAL),
163 NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
164 NWidget(NWID_SPACER), SetFill(0, 1),
165 EndContainer(),
166 EndContainer(),
167 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 48), SetFill(1, 0), SetPadding(0, 5, 0, 5),
168 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetMinimalSize(426, 70), SetPadding(1, 2, 2, 2),
169 EndContainer(),
172 static const WindowDesc _thin_news_desc(
173 WDP_MANUAL, 0, 0,
174 WC_NEWS_WINDOW, WC_NONE,
176 _nested_thin_news_widgets, lengthof(_nested_thin_news_widgets)
179 /* Small news items. */
180 static const NWidgetPart _nested_small_news_widgets[] = {
181 /* Caption + close box. The caption is no WWT_CAPTION as the window shall not be moveable and so on. */
182 NWidget(NWID_HORIZONTAL),
183 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE, WID_N_CLOSEBOX),
184 NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_N_CAPTION), SetFill(1, 0),
185 EndContainer(),
187 /* Main part */
188 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_N_HEADLINE),
189 NWidget(WWT_INSET, COLOUR_LIGHT_BLUE, WID_N_INSET), SetPadding(2, 2, 2, 2),
190 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetPadding(1, 1, 1, 1), SetMinimalSize(274, 47), SetFill(1, 0),
191 EndContainer(),
192 NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(275, 20), SetFill(1, 0), SetPadding(0, 5, 0, 5),
193 EndContainer(),
196 static const WindowDesc _small_news_desc(
197 WDP_MANUAL, 0, 0,
198 WC_NEWS_WINDOW, WC_NONE,
200 _nested_small_news_widgets, lengthof(_nested_small_news_widgets)
204 * Window layouts for news items.
206 static const WindowDesc* const _news_window_layout[] = {
207 &_thin_news_desc, ///< NF_THIN
208 &_small_news_desc, ///< NF_SMALL
209 &_normal_news_desc, ///< NF_NORMAL
210 &_vehicle_news_desc, ///< NF_VEHICLE
211 &_company_news_desc, ///< NF_COMPANY
214 static const WindowDesc* GetNewsWindowLayout (NewsFlag flags)
216 uint layout = GB(flags, NFB_WINDOW_LAYOUT, NFB_WINDOW_LAYOUT_COUNT);
217 assert(layout < lengthof(_news_window_layout));
218 return _news_window_layout[layout];
222 * Per-NewsType data
224 static const NewsTypeData _news_type_data[] = {
225 /* name, age, sound, */
226 NewsTypeData("news_display.arrival_player", 60, SND_1D_APPLAUSE ), ///< NT_ARRIVAL_COMPANY
227 NewsTypeData("news_display.arrival_other", 60, SND_1D_APPLAUSE ), ///< NT_ARRIVAL_OTHER
228 NewsTypeData("news_display.accident", 90, SND_BEGIN ), ///< NT_ACCIDENT
229 NewsTypeData("news_display.company_info", 60, SND_BEGIN ), ///< NT_COMPANY_INFO
230 NewsTypeData("news_display.open", 90, SND_BEGIN ), ///< NT_INDUSTRY_OPEN
231 NewsTypeData("news_display.close", 90, SND_BEGIN ), ///< NT_INDUSTRY_CLOSE
232 NewsTypeData("news_display.economy", 30, SND_BEGIN ), ///< NT_ECONOMY
233 NewsTypeData("news_display.production_player", 30, SND_BEGIN ), ///< NT_INDUSTRY_COMPANY
234 NewsTypeData("news_display.production_other", 30, SND_BEGIN ), ///< NT_INDUSTRY_OTHER
235 NewsTypeData("news_display.production_nobody", 30, SND_BEGIN ), ///< NT_INDUSTRY_NOBODY
236 NewsTypeData("news_display.advice", 150, SND_BEGIN ), ///< NT_ADVICE
237 NewsTypeData("news_display.new_vehicles", 30, SND_1E_OOOOH ), ///< NT_NEW_VEHICLES
238 NewsTypeData("news_display.acceptance", 90, SND_BEGIN ), ///< NT_ACCEPTANCE
239 NewsTypeData("news_display.subsidies", 180, SND_BEGIN ), ///< NT_SUBSIDIES
240 NewsTypeData("news_display.general", 60, SND_BEGIN ), ///< NT_GENERAL
243 assert_compile(lengthof(_news_type_data) == NT_END);
246 * Return the news display option.
247 * @return display options
249 NewsDisplay NewsTypeData::GetDisplay() const
251 uint index;
252 const SettingDesc *sd = GetSettingFromName(this->name, &index);
253 assert(sd != NULL);
254 void *ptr = sd->save.get_variable_address();
255 return (NewsDisplay)ReadValue(ptr, sd->save.conv);
258 /** Window class displaying a news item. */
259 struct NewsWindow : Window {
260 uint16 chat_height; ///< Height of the chat window.
261 uint16 status_height; ///< Height of the status bar window
262 const NewsItem *ni; ///< News item to display.
263 static uint duration; ///< Remaining time for showing current news message (may only be accessed while a news item is displayed).
265 NewsWindow (const WindowDesc *desc, const NewsItem *ni) :
266 Window (desc), chat_height (0), status_height (0), ni(ni)
268 NewsWindow::duration = 555;
269 const Window *w = FindWindowByClass(WC_SEND_NETWORK_MSG);
270 this->chat_height = (w != NULL) ? w->height : 0;
271 this->status_height = FindWindowById(WC_STATUS_BAR, 0)->height;
273 this->flags |= WF_DISABLE_VP_SCROLL;
275 this->CreateNestedTree();
277 /* For company news with a face we have a separate headline in param[0] */
278 if (desc == &_company_news_desc) this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = this->ni->params[0];
280 this->InitNested(0);
282 /* Initialize viewport if it exists. */
283 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_N_VIEWPORT);
284 if (nvp != NULL) {
285 nvp->InitializeViewport(this, ni->reftype1 == NR_VEHICLE ? 0x80000000 | ni->ref1 : GetReferenceTile(ni->reftype1, ni->ref1), ZOOM_LVL_NEWS);
286 if (this->ni->flags & NF_SHADE) nvp->disp_flags |= ND_NO_TRANSPARENCY;
287 if ((this->ni->flags & NF_INCOLOUR) == 0) {
288 nvp->disp_flags |= ND_SHADE_GREY;
289 } else if (this->ni->flags & NF_SHADE) {
290 nvp->disp_flags |= ND_SHADE_DIMMED;
294 PositionNewsMessage(this);
297 void DrawNewsBorder (BlitArea *dpi, const Rect &r) const
299 GfxFillRect (dpi, r.left, r.top, r.right, r.bottom, PC_WHITE);
301 GfxFillRect (dpi, r.left, r.top, r.left, r.bottom, PC_BLACK);
302 GfxFillRect (dpi, r.right, r.top, r.right, r.bottom, PC_BLACK);
303 GfxFillRect (dpi, r.left, r.top, r.right, r.top, PC_BLACK);
304 GfxFillRect (dpi, r.left, r.bottom, r.right, r.bottom, PC_BLACK);
307 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
309 Point pt = { 0, _screen_height };
310 return pt;
313 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
315 StringID str = STR_NULL;
316 switch (widget) {
317 case WID_N_MGR_FACE:
318 *size = maxdim(*size, GetSpriteSize(SPR_GRADIENT));
319 break;
321 case WID_N_MESSAGE:
322 CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
323 str = this->ni->string_id;
324 break;
326 case WID_N_COMPANY_MSG:
327 str = this->GetCompanyMessageString();
328 break;
330 case WID_N_VEH_NAME:
331 case WID_N_VEH_TITLE:
332 str = this->GetNewVehicleMessageString(widget);
333 break;
335 case WID_N_VEH_INFO: {
336 assert(this->ni->reftype1 == NR_ENGINE);
337 EngineID engine = this->ni->ref1;
338 str = GetEngineInfoString(engine);
339 break;
341 default:
342 return; // Do nothing
345 /* Update minimal size with length of the multi-line string. */
346 Dimension d = *size;
347 d.width = (d.width >= padding.width) ? d.width - padding.width : 0;
348 d.height = GetStringHeight (str, d.width);
349 d.width += padding.width;
350 d.height += padding.height;
351 *size = maxdim(*size, d);
354 virtual void SetStringParameters(int widget) const
356 if (widget == WID_N_DATE) SetDParam(0, this->ni->date);
359 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
361 switch (widget) {
362 case WID_N_CAPTION:
363 DrawCaption (dpi, r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION);
364 break;
366 case WID_N_PANEL:
367 this->DrawNewsBorder (dpi, r);
368 break;
370 case WID_N_MESSAGE:
371 CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
372 DrawStringMultiLine (dpi, r.left, r.right, r.top, r.bottom, this->ni->string_id, TC_FROMSTRING, SA_CENTER);
373 break;
375 case WID_N_MGR_FACE: {
376 const BaseCompanyNewsItem *cni = static_cast<const BaseCompanyNewsItem *>(this->ni);
377 DrawCompanyManagerFace (cni->face, cni->colour, dpi, r.left, r.top);
378 GfxFillRect (dpi, r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
379 break;
381 case WID_N_MGR_NAME: {
382 const BaseCompanyNewsItem *cni = static_cast<const BaseCompanyNewsItem *>(this->ni);
383 SetDParamStr (0, cni->pname.get());
384 DrawStringMultiLine (dpi, r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
385 break;
387 case WID_N_COMPANY_MSG:
388 DrawStringMultiLine (dpi, r.left, r.right, r.top, r.bottom, this->GetCompanyMessageString(), TC_FROMSTRING, SA_CENTER);
389 break;
391 case WID_N_VEH_BKGND:
392 GfxFillRect (dpi, r.left, r.top, r.right, r.bottom, PC_GREY);
393 break;
395 case WID_N_VEH_NAME:
396 case WID_N_VEH_TITLE:
397 DrawStringMultiLine (dpi, r.left, r.right, r.top, r.bottom, this->GetNewVehicleMessageString(widget), TC_FROMSTRING, SA_CENTER);
398 break;
400 case WID_N_VEH_SPR: {
401 assert(this->ni->reftype1 == NR_ENGINE);
402 EngineID engine = this->ni->ref1;
403 DrawVehicleEngine (dpi, r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette (engine, _local_company), EIT_PREVIEW);
404 GfxFillRect (dpi, r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
405 break;
407 case WID_N_VEH_INFO: {
408 assert(this->ni->reftype1 == NR_ENGINE);
409 EngineID engine = this->ni->ref1;
410 DrawStringMultiLine (dpi, r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
411 break;
416 virtual void OnClick(Point pt, int widget, int click_count)
418 switch (widget) {
419 case WID_N_CLOSEBOX:
420 NewsWindow::duration = 0;
421 this->Delete();
422 _forced_news = NULL;
423 break;
425 case WID_N_CAPTION:
426 if (this->ni->reftype1 == NR_VEHICLE) {
427 const Vehicle *v = Vehicle::Get(this->ni->ref1);
428 ShowVehicleViewWindow(v);
430 break;
432 case WID_N_VIEWPORT:
433 break; // Ignore clicks
435 default:
436 if (this->ni->reftype1 == NR_VEHICLE) {
437 const Vehicle *v = Vehicle::Get(this->ni->ref1);
438 ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
439 } else {
440 TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
441 TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
442 if (_ctrl_pressed) {
443 if (tile1 != INVALID_TILE) ShowExtraViewPortWindow(tile1);
444 if (tile2 != INVALID_TILE) ShowExtraViewPortWindow(tile2);
445 } else {
446 if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
447 ScrollMainWindowToTile(tile2);
451 break;
455 virtual EventState OnKeyPress(WChar key, uint16 keycode)
457 if (keycode == WKC_SPACE) {
458 /* Don't continue. */
459 this->Delete();
460 return ES_HANDLED;
462 return ES_NOT_HANDLED;
466 * Some data on this window has become invalid.
467 * @param data Information about the changed data.
468 * @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.
470 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
472 if (!gui_scope) return;
473 /* The chatbar has notified us that is was either created or closed */
474 int newtop = this->top + this->chat_height - data;
475 this->chat_height = data;
476 this->SetWindowTop(newtop);
479 virtual void OnTick()
481 /* Scroll up newsmessages from the bottom in steps of 4 pixels */
482 int newtop = max (this->top - 4, _screen_height - this->height - this->status_height - this->chat_height);
483 this->SetWindowTop(newtop);
486 private:
488 * Moves the window so #newtop is new 'top' coordinate. Makes screen dirty where needed.
489 * @param newtop new top coordinate
491 void SetWindowTop(int newtop)
493 if (this->top == newtop) return;
495 int mintop = min(newtop, this->top);
496 int maxtop = max(newtop, this->top);
497 if (this->viewport != NULL) this->viewport->top += newtop - this->top;
498 this->top = newtop;
500 SetDirtyBlocks(this->left, mintop, this->left + this->width, maxtop + this->height);
503 StringID GetCompanyMessageString() const
505 /* Company news with a face have a separate headline, so the normal message is shifted by two params */
506 CopyInDParam(0, this->ni->params + 2, lengthof(this->ni->params) - 2);
507 return this->ni->params[1];
510 StringID GetNewVehicleMessageString(int widget) const
512 assert(this->ni->reftype1 == NR_ENGINE);
513 EngineID engine = this->ni->ref1;
515 switch (widget) {
516 case WID_N_VEH_TITLE:
517 SetDParam(0, GetEngineCategoryName(engine));
518 return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
520 case WID_N_VEH_NAME:
521 SetDParam(0, engine);
522 return STR_NEWS_NEW_VEHICLE_TYPE;
524 default:
525 NOT_REACHED();
530 /* static */ uint NewsWindow::duration = 0; // Instance creation.
533 /** Open up an own newspaper window for the news item */
534 static void ShowNewspaper(const NewsItem *ni)
536 SoundFx sound = _news_type_data[ni->type].sound;
537 if (sound != 0 && _settings_client.sound.news_full) SndPlayFx(sound);
539 new NewsWindow(GetNewsWindowLayout(ni->flags), ni);
542 /** Show news item in the ticker */
543 static void ShowTicker(const NewsItem *ni)
545 if (_settings_client.sound.news_ticker) SndPlayFx(SND_16_MORSE);
547 _statusbar_news_item = ni;
548 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
551 /** Initialize the news-items data structures */
552 void InitNewsItemStructs()
554 for (NewsItem *ni = _oldest_news; ni != NULL; ) {
555 NewsItem *next = ni->next;
556 delete ni;
557 ni = next;
560 _total_news = 0;
561 _oldest_news = NULL;
562 _latest_news = NULL;
563 _forced_news = NULL;
564 _current_news = NULL;
565 _statusbar_news_item = NULL;
566 NewsWindow::duration = 0;
570 * Are we ready to show another news item?
571 * Only if nothing is in the newsticker and no newspaper is displayed
573 static bool ReadyForNextItem()
575 const NewsItem *ni = _forced_news == NULL ? _current_news : _forced_news;
576 if (ni == NULL) return true;
578 /* Ticker message
579 * Check if the status bar message is still being displayed? */
580 if (IsNewsTickerShown()) return false;
582 /* Newspaper message, decrement duration counter */
583 if (NewsWindow::duration != 0) NewsWindow::duration--;
585 /* neither newsticker nor newspaper are running */
586 return (NewsWindow::duration == 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
589 /** Move to the next news item */
590 static void MoveToNextItem()
592 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
593 DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
594 _forced_news = NULL;
595 _statusbar_news_item = NULL;
597 /* if we're not at the last item, then move on */
598 if (_current_news != _latest_news) {
599 _current_news = (_current_news == NULL) ? _oldest_news : _current_news->next;
600 const NewsItem *ni = _current_news;
601 const NewsType type = ni->type;
603 /* check the date, don't show too old items */
604 if (_date - _news_type_data[type].age > ni->date) return;
606 switch (_news_type_data[type].GetDisplay()) {
607 default: NOT_REACHED();
608 case ND_OFF: // Off - show nothing only a small reminder in the status bar
609 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
610 break;
612 case ND_SUMMARY: // Summary - show ticker
613 ShowTicker(ni);
614 break;
616 case ND_FULL: // Full - show newspaper
617 ShowNewspaper(ni);
618 break;
624 * Insert a news item into the list.
625 * @param ni The news item to insert.
627 void InsertNewsItem (NewsItem *ni)
629 assert (ni->prev == NULL);
630 assert (ni->next == NULL);
632 if (_total_news++ == 0) {
633 assert(_oldest_news == NULL);
634 _oldest_news = ni;
635 ni->prev = NULL;
636 } else {
637 assert(_latest_news->next == NULL);
638 _latest_news->next = ni;
639 ni->prev = _latest_news;
642 ni->next = NULL;
643 _latest_news = ni;
645 SetWindowDirty(WC_MESSAGE_HISTORY, 0);
648 /** Construct a NewsItem::CompanyName. */
649 NewsItem::CompanyName::CompanyName (CompanyID cid)
651 SetDParam (0, cid);
652 GetString (this->data, STR_COMPANY_NAME);
655 /** Construct a BaseVehicleNewsItem. */
656 BaseVehicleNewsItem::BaseVehicleNewsItem (StringID string, NewsType type,
657 VehicleID vid, const struct Station *st)
658 : NewsItem (string, type, NF_SHADE_THIN, NR_VEHICLE, vid,
659 st != NULL ? NR_STATION : NR_NONE,
660 st != NULL ? st->index : UINT32_MAX)
664 /** Construct an ArrivalNewsItem. */
665 ArrivalNewsItem::ArrivalNewsItem (StringID string, const Vehicle *v,
666 const Station *st)
667 : BaseVehicleNewsItem (string, (v->owner == _local_company) ?
668 NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
669 v->index, st)
671 this->params[0] = st->index;
674 /** Construct a PlaneCrashNewsItem. */
675 PlaneCrashNewsItem::PlaneCrashNewsItem (VehicleID vid, const Station *st,
676 uint pass)
677 : BaseVehicleNewsItem (st != NULL ? STR_NEWS_AIRCRAFT_CRASH :
678 STR_NEWS_PLANE_CRASH_OUT_OF_FUEL, NT_ACCIDENT, vid, st)
680 this->params[0] = pass;
681 if (st != NULL) this->params[1] = st->index;
684 /** Construct an AcceptanceNewsItem. */
685 AcceptanceNewsItem::AcceptanceNewsItem (const Station *st,
686 uint num_items, CargoID *cargo, StringID msg)
687 : NewsItem (msg, NT_ACCEPTANCE, (NewsFlag)(NF_INCOLOUR | NF_SMALL),
688 NR_STATION, st->index)
690 this->params[0] = st->index;
692 static const uint max_items = lengthof(this->params) - 1;
693 if (num_items > max_items) num_items = max_items;
695 for (uint i = 0; i < num_items; i++) {
696 this->params[i + 1] = CargoSpec::Get(cargo[i])->name;
700 /** Construct a FoundTownNewsItem. */
701 FoundTownNewsItem::FoundTownNewsItem (TownID tid, TileIndex tile,
702 CompanyID cid)
703 : TileNewsItem (STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile),
704 cname (cid)
706 this->params[0] = this->cname.get_as_param();
707 this->params[1] = tid;
710 /** Construct a RoadRebuildNewsItem. */
711 RoadRebuildNewsItem::RoadRebuildNewsItem (TownID tid, CompanyID cid)
712 : NewsItem (STR_NEWS_ROAD_REBUILDING, NT_GENERAL, NF_NORMAL, NR_TOWN, tid),
713 cname (cid)
715 this->params[0] = tid;
716 this->params[1] = this->cname.get_as_param();
719 /** Construct an EngineNewsItem. */
720 EngineNewsItem::EngineNewsItem (EngineID eid)
721 : NewsItem (STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE,
722 NT_NEW_VEHICLES, NF_VEHICLE, NR_ENGINE, eid)
724 this->params[0] = GetEngineCategoryName (eid);
725 this->params[1] = eid;
728 /** Construct a SubsidyNewsItem. */
729 SubsidyNewsItem::SubsidyNewsItem (StringID string, const Subsidy *s,
730 bool plural, uint offset)
731 : NewsItem (string, NT_SUBSIDIES, NF_NORMAL)
733 const CargoSpec *cs = CargoSpec::Get (s->cargo_type);
734 this->params[offset] = plural ? cs->name : cs->name_single;
736 if (s->src.type == ST_INDUSTRY) {
737 this->reftype1 = NR_INDUSTRY;
738 this->params[offset + 1] = STR_INDUSTRY_NAME;
739 } else {
740 this->reftype1 = NR_TOWN;
741 this->params[offset + 1] = STR_TOWN_NAME;
743 this->params[offset + 2] = this->ref1 = s->src.id;
745 if (s->dst.type == ST_INDUSTRY) {
746 this->reftype2 = NR_INDUSTRY;
747 this->params[offset + 4] = STR_INDUSTRY_NAME;
748 } else {
749 this->reftype2 = NR_TOWN;
750 this->params[offset + 4] = STR_TOWN_NAME;
752 this->params[offset + 5] = this->ref2 = s->dst.id;
755 /** Construct a SubsidyAwardNewsItem. */
756 SubsidyAwardNewsItem::SubsidyAwardNewsItem (const Subsidy *s, CompanyID cid)
757 : SubsidyNewsItem (STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
758 s, false, 1),
759 cname (cid)
761 this->params[0] = this->cname.get_as_param();
764 /** Construct a BaseCompanyNewsItem. */
765 BaseCompanyNewsItem::BaseCompanyNewsItem (NewsType type, StringID str,
766 const Company *c, NewsReferenceType reftype, uint32 ref)
767 : NewsItem (STR_MESSAGE_NEWS_FORMAT, type, NF_COMPANY, reftype, ref),
768 cname (c->index)
770 SetDParam (0, c->index);
771 GetString (this->pname.data, STR_PRESIDENT_NAME_MANAGER);
773 this->colour = c->colour;
774 this->face = c->face;
776 this->params[0] = str;
779 /** Construct a CompanyNewsItem. */
780 CompanyNewsItem::CompanyNewsItem (StringID str1, StringID str2,
781 const Company *c)
782 : BaseCompanyNewsItem (NT_COMPANY_INFO, str1, c)
784 this->params[1] = str2;
785 this->params[2] = this->cname.get_as_param();
788 /** Construct a LaunchNewsItem. */
789 LaunchNewsItem::LaunchNewsItem (const Company *c, TownID tid)
790 : BaseCompanyNewsItem (NT_COMPANY_INFO, STR_NEWS_COMPANY_LAUNCH_TITLE,
791 c, NR_TILE, c->last_build_coordinate)
793 this->params[1] = STR_NEWS_COMPANY_LAUNCH_DESCRIPTION;
794 this->params[2] = this->cname.get_as_param();
795 this->params[3] = tid;
798 /** Construct a MergerNewsItem. */
799 MergerNewsItem::MergerNewsItem (const Company *c, const Company *merger)
800 : BaseCompanyNewsItem (NT_COMPANY_INFO, STR_NEWS_COMPANY_MERGER_TITLE,
801 merger),
802 cname2 (c->index)
804 this->params[1] = c->bankrupt_value == 0 ?
805 STR_NEWS_MERGER_TAKEOVER_TITLE :
806 STR_NEWS_COMPANY_MERGER_DESCRIPTION;
807 this->params[2] = this->cname2.get_as_param();
808 this->params[3] = this->cname.get_as_param();
809 this->params[4] = c->bankrupt_value;
812 /** Construct an ExclusiveRightsNewsItem. */
813 ExclusiveRightsNewsItem::ExclusiveRightsNewsItem (TownID tid, const Company *c)
814 : BaseCompanyNewsItem (NT_GENERAL, STR_NEWS_EXCLUSIVE_RIGHTS_TITLE,
815 c, NR_TOWN, tid)
817 this->params[1] = STR_NEWS_EXCLUSIVE_RIGHTS_DESCRIPTION;
818 this->params[2] = tid;
819 this->params[3] = this->cname.get_as_param();
822 /** Custom news item. */
823 struct CustomNewsItem : NewsItem {
824 ttd_unique_free_ptr <char> text;
826 CustomNewsItem (NewsType type, NewsReferenceType reftype, uint32 ref,
827 const char *s)
828 : NewsItem (STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype, ref),
829 text (xstrdup (s))
831 this->params[0] = (uint64)(size_t)this->text.get();
836 * Create a new custom news item.
837 * @param tile unused
838 * @param flags type of operation
839 * @param p1 various bitstuffed elements
840 * - p1 = (bit 0 - 7) - NewsType of the message.
841 * - p1 = (bit 8 - 15) - NewsReferenceType of first reference.
842 * - p1 = (bit 16 - 23) - Company this news message is for.
843 * @param p2 First reference of the news message.
844 * @param text The text of the news message.
845 * @return the cost of this operation or an error
847 CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
849 if (_current_company != OWNER_DEITY) return CMD_ERROR;
851 NewsType type = (NewsType)GB(p1, 0, 8);
852 NewsReferenceType reftype1 = (NewsReferenceType)GB(p1, 8, 8);
853 CompanyID company = (CompanyID)GB(p1, 16, 8);
855 if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR;
856 if (type >= NT_END) return CMD_ERROR;
857 if (StrEmpty(text)) return CMD_ERROR;
859 switch (reftype1) {
860 case NR_NONE: break;
861 case NR_TILE:
862 if (!IsValidTile(p2)) return CMD_ERROR;
863 break;
865 case NR_VEHICLE:
866 if (!Vehicle::IsValidID(p2)) return CMD_ERROR;
867 break;
869 case NR_STATION:
870 if (!Station::IsValidID(p2)) return CMD_ERROR;
871 break;
873 case NR_INDUSTRY:
874 if (!Industry::IsValidID(p2)) return CMD_ERROR;
875 break;
877 case NR_TOWN:
878 if (!Town::IsValidID(p2)) return CMD_ERROR;
879 break;
881 case NR_ENGINE:
882 if (!Engine::IsValidID(p2)) return CMD_ERROR;
883 break;
885 default: return CMD_ERROR;
888 if (company != INVALID_OWNER && company != _local_company) return CommandCost();
890 if (flags & DC_EXEC) {
891 AddNewsItem<CustomNewsItem> (type, reftype1, p2, text);
894 return CommandCost();
897 /** Delete a news item from the queue */
898 static void DeleteNewsItem(NewsItem *ni)
900 /* Delete the news from the news queue. */
901 if (ni->prev != NULL) {
902 ni->prev->next = ni->next;
903 } else {
904 assert(_oldest_news == ni);
905 _oldest_news = ni->next;
908 if (ni->next != NULL) {
909 ni->next->prev = ni->prev;
910 } else {
911 assert(_latest_news == ni);
912 _latest_news = ni->prev;
915 _total_news--;
917 if (_forced_news == ni || _current_news == ni || _statusbar_news_item == ni) {
918 /* When we're the current news, go to the previous item first;
919 * we just possibly made that the last news item. */
920 if (_current_news == ni) _current_news = ni->prev;
922 /* About to remove the currently forced item (shown as newspapers) ||
923 * about to remove the currently displayed item (newspapers, ticker, or just a reminder) */
924 MoveToNextItem();
927 delete ni;
929 SetWindowDirty(WC_MESSAGE_HISTORY, 0);
933 * Delete a news item type about a vehicle.
934 * When the news item type is INVALID_STRING_ID all news about the vehicle gets deleted.
935 * @param vid The vehicle to remove the news for.
936 * @param news The news type to remove.
938 void DeleteVehicleNews(VehicleID vid, StringID news)
940 NewsItem *ni = _oldest_news;
942 while (ni != NULL) {
943 NewsItem *next = ni->next;
944 if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
945 (news == INVALID_STRING_ID || ni->string_id == news)) {
946 DeleteNewsItem(ni);
948 ni = next;
953 * Remove news regarding given station so there are no 'unknown station now accepts Mail'
954 * or 'First train arrived at unknown station' news items.
955 * @param sid station to remove news about
957 void DeleteStationNews(StationID sid)
959 NewsItem *ni = _oldest_news;
961 while (ni != NULL) {
962 NewsItem *next = ni->next;
963 if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
964 DeleteNewsItem(ni);
966 ni = next;
971 * Remove news regarding given industry
972 * @param iid industry to remove news about
974 void DeleteIndustryNews(IndustryID iid)
976 NewsItem *ni = _oldest_news;
978 while (ni != NULL) {
979 NewsItem *next = ni->next;
980 if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
981 DeleteNewsItem(ni);
983 ni = next;
988 * Remove engine announcements for invalid engines.
990 void DeleteInvalidEngineNews()
992 NewsItem *ni = _oldest_news;
994 while (ni != NULL) {
995 NewsItem *next = ni->next;
996 if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
997 (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
998 DeleteNewsItem(ni);
1000 ni = next;
1004 static void RemoveOldNewsItems()
1006 NewsItem *next;
1007 for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
1008 next = cur->next;
1009 if (_date - _news_type_data[cur->type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
1014 * Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.
1015 * @note Viewports of currently displayed news is changed via #ChangeVehicleViewports
1016 * @param from_index the old vehicle ID
1017 * @param to_index the new vehicle ID
1019 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
1021 for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
1022 if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
1023 if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
1024 if (ni->flags & NF_VEHICLE_PARAM0 && ni->params[0] == from_index) ni->params[0] = to_index;
1028 void NewsLoop()
1030 /* no news item yet */
1031 if (_total_news == 0) return;
1033 /* There is no status bar, so no reason to show news;
1034 * especially important with the end game screen when
1035 * there is no status bar but possible news. */
1036 if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
1038 static byte _last_clean_month = 0;
1040 if (_last_clean_month != _cur_month) {
1041 RemoveOldNewsItems();
1042 _last_clean_month = _cur_month;
1045 if (ReadyForNextItem()) MoveToNextItem();
1048 /** Do a forced show of a specific message */
1049 static void ShowNewsMessage(const NewsItem *ni)
1051 assert(_total_news != 0);
1053 /* Delete the news window */
1054 DeleteWindowById(WC_NEWS_WINDOW, 0);
1056 /* setup forced news item */
1057 _forced_news = ni;
1059 if (_forced_news != NULL) {
1060 DeleteWindowById(WC_NEWS_WINDOW, 0);
1061 ShowNewspaper(ni);
1065 /** Show previous news item */
1066 void ShowLastNewsMessage()
1068 const NewsItem *ni = NULL;
1069 if (_total_news == 0) {
1070 return;
1071 } else if (_forced_news == NULL) {
1072 /* Not forced any news yet, show the current one, unless a news window is
1073 * open (which can only be the current one), then show the previous item */
1074 if (_current_news == NULL) {
1075 /* No news were shown yet resp. the last shown one was already deleted.
1076 * Threat this as if _forced_news reached _oldest_news; so, wrap around and start anew with the latest. */
1077 ni = _latest_news;
1078 } else {
1079 const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
1080 ni = (w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev;
1082 } else if (_forced_news == _oldest_news) {
1083 /* We have reached the oldest news, start anew with the latest */
1084 ni = _latest_news;
1085 } else {
1086 /* 'Scrolling' through news history show each one in turn */
1087 ni = _forced_news->prev;
1089 bool wrap = false;
1090 for (;;) {
1091 if (_news_type_data[ni->type].GetDisplay() != ND_OFF) {
1092 ShowNewsMessage(ni);
1093 break;
1096 ni = ni->prev;
1097 if (ni == NULL) {
1098 if (wrap) break;
1099 /* We have reached the oldest news, start anew with the latest */
1100 ni = _latest_news;
1101 wrap = true;
1108 * Draw an unformatted news message truncated to a maximum length. If
1109 * length exceeds maximum length it will be postfixed by '...'
1110 * @param dpi the area to draw on
1111 * @param left the left most location for the string
1112 * @param right the right most location for the string
1113 * @param y position of the string
1114 * @param colour the colour the string will be shown in
1115 * @param *ni NewsItem being printed
1116 * @param maxw maximum width of string in pixels
1118 static void DrawNewsString (BlitArea *dpi, uint left, uint right, int y,
1119 TextColour colour, const NewsItem *ni)
1121 char buffer[512], buffer2[512];
1122 StringID str;
1124 CopyInDParam(0, ni->params, lengthof(ni->params));
1125 str = ni->string_id;
1127 GetString (buffer, str);
1128 /* Copy the just gotten string to another buffer to remove any formatting
1129 * from it such as big fonts, etc. */
1130 const char *ptr = buffer;
1131 char *dest = buffer2;
1132 for (;;) {
1133 WChar c = Utf8Consume(&ptr);
1134 if (c == 0) break;
1135 /* Make a space from a newline, but ignore multiple newlines */
1136 if (c == '\n') {
1137 dest[0] = ' ';
1138 dest++;
1139 while (*ptr == '\n') ptr++;
1140 } else if (c == '\r') {
1141 dest[0] = dest[1] = dest[2] = dest[3] = ' ';
1142 dest += 4;
1143 } else if (IsPrintable(c)) {
1144 dest += Utf8Encode(dest, c);
1148 *dest = '\0';
1149 /* Truncate and show string; postfixed by '...' if necessary */
1150 DrawString (dpi, left, right, y, buffer2, colour);
1153 struct MessageHistoryWindow : Window {
1154 static const int top_spacing; ///< Additional spacing at the top of the #WID_MH_BACKGROUND widget.
1155 static const int bottom_spacing; ///< Additional spacing at the bottom of the #WID_MH_BACKGROUND widget.
1157 int line_height; /// < Height of a single line in the news history window including spacing.
1158 int date_width; /// < Width needed for the date part.
1160 Scrollbar *vscroll;
1162 MessageHistoryWindow (const WindowDesc *desc) :
1163 Window (desc), line_height (0), date_width (0)
1165 this->CreateNestedTree();
1166 this->vscroll = this->GetScrollbar(WID_MH_SCROLLBAR);
1167 this->InitNested(); // Initializes 'this->line_height' and 'this->date_width'.
1168 this->OnInvalidateData(0);
1171 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1173 if (widget == WID_MH_BACKGROUND) {
1174 this->line_height = FONT_HEIGHT_NORMAL + 2;
1175 resize->height = this->line_height;
1177 /* Months are off-by-one, so it's actually 8. Not using
1178 * month 12 because the 1 is usually less wide. */
1179 SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 7, 30));
1180 this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
1182 size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
1183 size->width = max(200u, size->width); // At least 200 pixels wide.
1187 void OnPaint (BlitArea *dpi) OVERRIDE
1189 this->OnInvalidateData(0);
1190 this->DrawWidgets (dpi);
1193 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
1195 if (widget != WID_MH_BACKGROUND || _total_news == 0) return;
1197 /* Find the first news item to display. */
1198 NewsItem *ni = _latest_news;
1199 for (int n = this->vscroll->GetPosition(); n > 0; n--) {
1200 ni = ni->prev;
1201 if (ni == NULL) return;
1204 /* Fill the widget with news items. */
1205 int y = r.top + this->top_spacing;
1206 bool rtl = _current_text_dir == TD_RTL;
1207 uint date_left = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
1208 uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
1209 uint news_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
1210 uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
1211 for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
1212 SetDParam(0, ni->date);
1213 DrawString (dpi, date_left, date_right, y, STR_SHORT_DATE);
1215 DrawNewsString (dpi, news_left, news_right, y, TC_WHITE, ni);
1216 y += this->line_height;
1218 ni = ni->prev;
1219 if (ni == NULL) return;
1224 * Some data on this window has become invalid.
1225 * @param data Information about the changed data.
1226 * @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.
1228 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1230 if (!gui_scope) return;
1231 this->vscroll->SetCount(_total_news);
1234 virtual void OnClick(Point pt, int widget, int click_count)
1236 if (widget == WID_MH_BACKGROUND) {
1237 NewsItem *ni = _latest_news;
1238 if (ni == NULL) return;
1240 for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_MH_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
1241 ni = ni->prev;
1242 if (ni == NULL) return;
1245 ShowNewsMessage(ni);
1249 virtual void OnResize()
1251 this->vscroll->SetCapacityFromWidget(this, WID_MH_BACKGROUND);
1255 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
1256 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
1258 static const NWidgetPart _nested_message_history[] = {
1259 NWidget(NWID_HORIZONTAL),
1260 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1261 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1262 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1263 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1264 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1265 EndContainer(),
1267 NWidget(NWID_HORIZONTAL),
1268 NWidget(WWT_PANEL, COLOUR_BROWN, WID_MH_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR),
1269 EndContainer(),
1270 NWidget(NWID_VERTICAL),
1271 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_MH_SCROLLBAR),
1272 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1273 EndContainer(),
1274 EndContainer(),
1277 static WindowDesc::Prefs _message_history_prefs ("list_news");
1279 static const WindowDesc _message_history_desc(
1280 WDP_AUTO, 400, 140,
1281 WC_MESSAGE_HISTORY, WC_NONE,
1283 _nested_message_history, lengthof(_nested_message_history),
1284 &_message_history_prefs
1287 /** Display window with news messages history */
1288 void ShowMessageHistory()
1290 DeleteWindowById(WC_MESSAGE_HISTORY, 0);
1291 new MessageHistoryWindow(&_message_history_desc);