OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

news_gui.cpp
Go to the documentation of this file.
1 /* $Id: news_gui.cpp 27838 2017-03-31 23:09:50Z peter1138 $ */
2
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 */
9
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_func.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
37 #include "widgets/news_widget.h"
38
39 #include "table/strings.h"
40
41 #include "safeguards.h"
42
43 const NewsItem *_statusbar_news_item = NULL;
44
45 static uint MIN_NEWS_AMOUNT = 30;
46 static uint _total_news = 0;
47 static NewsItem *_oldest_news = NULL;
48 static NewsItem *_latest_news = NULL;
49
56 static const NewsItem *_forced_news = NULL;
57
59 static const NewsItem *_current_news = NULL;
60
61
68 static TileIndex GetReferenceTile(NewsReferenceType reftype, uint32 ref)
69 {
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;
76 }
77 }
78
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(),
92 };
93
94 static WindowDesc _normal_news_desc(
95 WDP_MANUAL, NULL, 0, 0,
96 WC_NEWS_WINDOW, WC_NONE,
97 0,
98 _nested_normal_news_widgets, lengthof(_nested_normal_news_widgets)
99 );
100
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(),
119 };
120
121 static WindowDesc _vehicle_news_desc(
122 WDP_MANUAL, NULL, 0, 0,
123 WC_NEWS_WINDOW, WC_NONE,
124 0,
125 _nested_vehicle_news_widgets, lengthof(_nested_vehicle_news_widgets)
126 );
127
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(),
147 };
148
149 static WindowDesc _company_news_desc(
150 WDP_MANUAL, NULL, 0, 0,
151 WC_NEWS_WINDOW, WC_NONE,
152 0,
153 _nested_company_news_widgets, lengthof(_nested_company_news_widgets)
154 );
155
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(),
170 };
171
172 static WindowDesc _thin_news_desc(
173 WDP_MANUAL, NULL, 0, 0,
174 WC_NEWS_WINDOW, WC_NONE,
175 0,
176 _nested_thin_news_widgets, lengthof(_nested_thin_news_widgets)
177 );
178
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(),
186
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(),
194 };
195
196 static WindowDesc _small_news_desc(
197 WDP_MANUAL, NULL, 0, 0,
198 WC_NEWS_WINDOW, WC_NONE,
199 0,
200 _nested_small_news_widgets, lengthof(_nested_small_news_widgets)
201 );
202
206 static WindowDesc* _news_window_layout[] = {
207 &_thin_news_desc,
208 &_small_news_desc,
209 &_normal_news_desc,
210 &_vehicle_news_desc,
211 &_company_news_desc,
212 };
213
214 WindowDesc* GetNewsWindowLayout(NewsFlag flags)
215 {
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];
219 }
220
224 static NewsTypeData _news_type_data[] = {
225 /* name, age, sound, */
226 NewsTypeData("news_display.arrival_player", 60, SND_1D_APPLAUSE ),
227 NewsTypeData("news_display.arrival_other", 60, SND_1D_APPLAUSE ),
228 NewsTypeData("news_display.accident", 90, SND_BEGIN ),
229 NewsTypeData("news_display.company_info", 60, SND_BEGIN ),
230 NewsTypeData("news_display.open", 90, SND_BEGIN ),
231 NewsTypeData("news_display.close", 90, SND_BEGIN ),
232 NewsTypeData("news_display.economy", 30, SND_BEGIN ),
233 NewsTypeData("news_display.production_player", 30, SND_BEGIN ),
234 NewsTypeData("news_display.production_other", 30, SND_BEGIN ),
235 NewsTypeData("news_display.production_nobody", 30, SND_BEGIN ),
236 NewsTypeData("news_display.advice", 150, SND_BEGIN ),
237 NewsTypeData("news_display.new_vehicles", 30, SND_1E_OOOOH ),
238 NewsTypeData("news_display.acceptance", 90, SND_BEGIN ),
239 NewsTypeData("news_display.subsidies", 180, SND_BEGIN ),
240 NewsTypeData("news_display.general", 60, SND_BEGIN ),
241 };
242
243 assert_compile(lengthof(_news_type_data) == NT_END);
244
249 NewsDisplay NewsTypeData::GetDisplay() const
250 {
251 uint index;
252 const SettingDesc *sd = GetSettingFromName(this->name, &index);
253 assert(sd != NULL);
254 void *ptr = GetVariableAddress(NULL, &sd->save);
255 return (NewsDisplay)ReadValue(ptr, sd->save.conv);
256 }
257
259 struct NewsWindow : Window {
260 uint16 chat_height;
261 uint16 status_height;
262 const NewsItem *ni;
263 static uint duration;
264
265 NewsWindow(WindowDesc *desc, const NewsItem *ni) : Window(desc), ni(ni)
266 {
267 NewsWindow::duration = 555;
268 const Window *w = FindWindowByClass(WC_SEND_NETWORK_MSG);
269 this->chat_height = (w != NULL) ? w->height : 0;
270 this->status_height = FindWindowById(WC_STATUS_BAR, 0)->height;
271
272 this->flags |= WF_DISABLE_VP_SCROLL;
273
274 this->CreateNestedTree();
275
276 /* For company news with a face we have a separate headline in param[0] */
277 if (desc == &_company_news_desc) this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = this->ni->params[0];
278
279 this->FinishInitNested(0);
280
281 /* Initialize viewport if it exists. */
282 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_N_VIEWPORT);
283 if (nvp != NULL) {
284 nvp->InitializeViewport(this, ni->reftype1 == NR_VEHICLE ? 0x80000000 | ni->ref1 : GetReferenceTile(ni->reftype1, ni->ref1), ZOOM_LVL_NEWS);
285 if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags |= ND_NO_TRANSPARENCY;
286 if ((this->ni->flags & NF_INCOLOUR) == 0) {
287 nvp->disp_flags |= ND_SHADE_GREY;
288 } else if (this->ni->flags & NF_SHADE) {
289 nvp->disp_flags |= ND_SHADE_DIMMED;
290 }
291 }
292
293 PositionNewsMessage(this);
294 }
295
296 void DrawNewsBorder(const Rect &r) const
297 {
298 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_WHITE);
299
300 GfxFillRect(r.left, r.top, r.left, r.bottom, PC_BLACK);
301 GfxFillRect(r.right, r.top, r.right, r.bottom, PC_BLACK);
302 GfxFillRect(r.left, r.top, r.right, r.top, PC_BLACK);
303 GfxFillRect(r.left, r.bottom, r.right, r.bottom, PC_BLACK);
304 }
305
306 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
307 {
308 Point pt = { 0, _screen.height };
309 return pt;
310 }
311
312 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
313 {
314 StringID str = STR_NULL;
315 switch (widget) {
316 case WID_N_CAPTION: {
317 /* Caption is not a real caption (so that the window cannot be moved)
318 * thus it doesn't get the default sizing of a caption. */
319 Dimension d2 = GetStringBoundingBox(STR_NEWS_MESSAGE_CAPTION);
320 d2.height += WD_CAPTIONTEXT_TOP + WD_CAPTIONTEXT_BOTTOM;
321 *size = maxdim(*size, d2);
322 return;
323 }
324
325 case WID_N_MGR_FACE:
326 *size = maxdim(*size, GetSpriteSize(SPR_GRADIENT));
327 break;
328
329 case WID_N_MESSAGE:
330 CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
331 str = this->ni->string_id;
332 break;
333
334 case WID_N_COMPANY_MSG:
335 str = this->GetCompanyMessageString();
336 break;
337
338 case WID_N_VEH_NAME:
339 case WID_N_VEH_TITLE:
340 str = this->GetNewVehicleMessageString(widget);
341 break;
342
343 case WID_N_VEH_INFO: {
344 assert(this->ni->reftype1 == NR_ENGINE);
345 EngineID engine = this->ni->ref1;
346 str = GetEngineInfoString(engine);
347 break;
348 }
349 default:
350 return; // Do nothing
351 }
352
353 /* Update minimal size with length of the multi-line string. */
354 Dimension d = *size;
355 d.width = (d.width >= padding.width) ? d.width - padding.width : 0;
356 d.height = (d.height >= padding.height) ? d.height - padding.height : 0;
357 d = GetStringMultiLineBoundingBox(str, d);
358 d.width += padding.width;
359 d.height += padding.height;
360 *size = maxdim(*size, d);
361 }
362
363 virtual void SetStringParameters(int widget) const
364 {
365 if (widget == WID_N_DATE) SetDParam(0, this->ni->date);
366 }
367
368 virtual void DrawWidget(const Rect &r, int widget) const
369 {
370 switch (widget) {
371 case WID_N_CAPTION:
372 DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION);
373 break;
374
375 case WID_N_PANEL:
376 this->DrawNewsBorder(r);
377 break;
378
379 case WID_N_MESSAGE:
380 CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
381 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->ni->string_id, TC_FROMSTRING, SA_CENTER);
382 break;
383
384 case WID_N_MGR_FACE: {
385 const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
386 DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top);
387 GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
388 break;
389 }
390 case WID_N_MGR_NAME: {
391 const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
392 SetDParamStr(0, cni->president_name);
393 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
394 break;
395 }
396 case WID_N_COMPANY_MSG:
397 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetCompanyMessageString(), TC_FROMSTRING, SA_CENTER);
398 break;
399
400 case WID_N_VEH_BKGND:
401 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_GREY);
402 break;
403
404 case WID_N_VEH_NAME:
405 case WID_N_VEH_TITLE:
406 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetNewVehicleMessageString(widget), TC_FROMSTRING, SA_CENTER);
407 break;
408
409 case WID_N_VEH_SPR: {
410 assert(this->ni->reftype1 == NR_ENGINE);
411 EngineID engine = this->ni->ref1;
412 DrawVehicleEngine(r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW);
413 GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
414 break;
415 }
416 case WID_N_VEH_INFO: {
417 assert(this->ni->reftype1 == NR_ENGINE);
418 EngineID engine = this->ni->ref1;
419 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
420 break;
421 }
422 }
423 }
424
425 virtual void OnClick(Point pt, int widget, int click_count)
426 {
427 switch (widget) {
428 case WID_N_CLOSEBOX:
429 NewsWindow::duration = 0;
430 delete this;
431 _forced_news = NULL;
432 break;
433
434 case WID_N_CAPTION:
435 if (this->ni->reftype1 == NR_VEHICLE) {
436 const Vehicle *v = Vehicle::Get(this->ni->ref1);
437 ShowVehicleViewWindow(v);
438 }
439 break;
440
441 case WID_N_VIEWPORT:
442 break; // Ignore clicks
443
444 default:
445 if (this->ni->reftype1 == NR_VEHICLE) {
446 const Vehicle *v = Vehicle::Get(this->ni->ref1);
447 ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
448 } else {
449 TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
450 TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
451 if (_ctrl_pressed) {
452 if (tile1 != INVALID_TILE) ShowExtraViewPortWindow(tile1);
453 if (tile2 != INVALID_TILE) ShowExtraViewPortWindow(tile2);
454 } else {
455 if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
456 ScrollMainWindowToTile(tile2);
457 }
458 }
459 }
460 break;
461 }
462 }
463
464 virtual EventState OnKeyPress(WChar key, uint16 keycode)
465 {
466 if (keycode == WKC_SPACE) {
467 /* Don't continue. */
468 delete this;
469 return ES_HANDLED;
470 }
471 return ES_NOT_HANDLED;
472 }
473
479 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
480 {
481 if (!gui_scope) return;
482 /* The chatbar has notified us that is was either created or closed */
483 int newtop = this->top + this->chat_height - data;
484 this->chat_height = data;
485 this->SetWindowTop(newtop);
486 }
487
488 virtual void OnTick()
489 {
490 /* Scroll up newsmessages from the bottom in steps of 4 pixels */
491 int newtop = max(this->top - 4, _screen.height - this->height - this->status_height - this->chat_height);
492 this->SetWindowTop(newtop);
493 }
494
495 private:
500 void SetWindowTop(int newtop)
501 {
502 if (this->top == newtop) return;
503
504 int mintop = min(newtop, this->top);
505 int maxtop = max(newtop, this->top);
506 if (this->viewport != NULL) this->viewport->top += newtop - this->top;
507 this->top = newtop;
508
509 SetDirtyBlocks(this->left, mintop, this->left + this->width, maxtop + this->height);
510 }
511
512 StringID GetCompanyMessageString() const
513 {
514 /* Company news with a face have a separate headline, so the normal message is shifted by two params */
515 CopyInDParam(0, this->ni->params + 2, lengthof(this->ni->params) - 2);
516 return this->ni->params[1];
517 }
518
519 StringID GetNewVehicleMessageString(int widget) const
520 {
521 assert(this->ni->reftype1 == NR_ENGINE);
522 EngineID engine = this->ni->ref1;
523
524 switch (widget) {
525 case WID_N_VEH_TITLE:
526 SetDParam(0, GetEngineCategoryName(engine));
527 return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
528
529 case WID_N_VEH_NAME:
530 SetDParam(0, engine);
531 return STR_NEWS_NEW_VEHICLE_TYPE;
532
533 default:
534 NOT_REACHED();
535 }
536 }
537 };
538
539 /* static */ uint NewsWindow::duration = 0; // Instance creation.
540
541
543 static void ShowNewspaper(const NewsItem *ni)
544 {
545 SoundFx sound = _news_type_data[ni->type].sound;
546 if (sound != 0 && _settings_client.sound.news_full) SndPlayFx(sound);
547
548 new NewsWindow(GetNewsWindowLayout(ni->flags), ni);
549 }
550
552 static void ShowTicker(const NewsItem *ni)
553 {
554 if (_settings_client.sound.news_ticker) SndPlayFx(SND_16_MORSE);
555
556 _statusbar_news_item = ni;
557 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
558 }
559
561 void InitNewsItemStructs()
562 {
563 for (NewsItem *ni = _oldest_news; ni != NULL; ) {
564 NewsItem *next = ni->next;
565 delete ni;
566 ni = next;
567 }
568
569 _total_news = 0;
570 _oldest_news = NULL;
571 _latest_news = NULL;
572 _forced_news = NULL;
573 _current_news = NULL;
574 _statusbar_news_item = NULL;
575 NewsWindow::duration = 0;
576 }
577
582 static bool ReadyForNextItem()
583 {
584 const NewsItem *ni = _forced_news == NULL ? _current_news : _forced_news;
585 if (ni == NULL) return true;
586
587 /* Ticker message
588 * Check if the status bar message is still being displayed? */
589 if (IsNewsTickerShown()) return false;
590
591 /* Newspaper message, decrement duration counter */
592 if (NewsWindow::duration != 0) NewsWindow::duration--;
593
594 /* neither newsticker nor newspaper are running */
595 return (NewsWindow::duration == 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
596 }
597
599 static void MoveToNextItem()
600 {
601 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
602 DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
603 _forced_news = NULL;
604 _statusbar_news_item = NULL;
605
606 /* if we're not at the last item, then move on */
607 if (_current_news != _latest_news) {
608 _current_news = (_current_news == NULL) ? _oldest_news : _current_news->next;
609 const NewsItem *ni = _current_news;
610 const NewsType type = ni->type;
611
612 /* check the date, don't show too old items */
613 if (_date - _news_type_data[type].age > ni->date) return;
614
615 switch (_news_type_data[type].GetDisplay()) {
616 default: NOT_REACHED();
617 case ND_OFF: // Off - show nothing only a small reminder in the status bar
618 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
619 break;
620
621 case ND_SUMMARY: // Summary - show ticker
622 ShowTicker(ni);
623 break;
624
625 case ND_FULL: // Full - show newspaper
626 ShowNewspaper(ni);
627 break;
628 }
629 }
630 }
631
645 void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
646 {
647 if (_game_mode == GM_MENU) return;
648
649 /* Create new news item node */
650 NewsItem *ni = new NewsItem;
651
652 ni->string_id = string;
653 ni->type = type;
654 ni->flags = flags;
655
656 /* show this news message in colour? */
657 if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR;
658
659 ni->reftype1 = reftype1;
660 ni->reftype2 = reftype2;
661 ni->ref1 = ref1;
662 ni->ref2 = ref2;
663 ni->free_data = free_data;
664 ni->date = _date;
665 CopyOutDParam(ni->params, 0, lengthof(ni->params));
666
667 if (_total_news++ == 0) {
668 assert(_oldest_news == NULL);
669 _oldest_news = ni;
670 ni->prev = NULL;
671 } else {
672 assert(_latest_news->next == NULL);
673 _latest_news->next = ni;
674 ni->prev = _latest_news;
675 }
676
677 ni->next = NULL;
678 _latest_news = ni;
679
680 SetWindowDirty(WC_MESSAGE_HISTORY, 0);
681 }
682
695 CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
696 {
697 if (_current_company != OWNER_DEITY) return CMD_ERROR;
698
699 NewsType type = (NewsType)GB(p1, 0, 8);
700 NewsReferenceType reftype1 = (NewsReferenceType)GB(p1, 8, 8);
701 CompanyID company = (CompanyID)GB(p1, 16, 8);
702
703 if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR;
704 if (type >= NT_END) return CMD_ERROR;
705 if (StrEmpty(text)) return CMD_ERROR;
706
707 switch (reftype1) {
708 case NR_NONE: break;
709 case NR_TILE:
710 if (!IsValidTile(p2)) return CMD_ERROR;
711 break;
712
713 case NR_VEHICLE:
714 if (!Vehicle::IsValidID(p2)) return CMD_ERROR;
715 break;
716
717 case NR_STATION:
718 if (!Station::IsValidID(p2)) return CMD_ERROR;
719 break;
720
721 case NR_INDUSTRY:
722 if (!Industry::IsValidID(p2)) return CMD_ERROR;
723 break;
724
725 case NR_TOWN:
726 if (!Town::IsValidID(p2)) return CMD_ERROR;
727 break;
728
729 case NR_ENGINE:
730 if (!Engine::IsValidID(p2)) return CMD_ERROR;
731 break;
732
733 default: return CMD_ERROR;
734 }
735
736 if (company != INVALID_OWNER && company != _local_company) return CommandCost();
737
738 if (flags & DC_EXEC) {
739 char *news = stredup(text);
740 SetDParamStr(0, news);
741 AddNewsItem(STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype1, p2, NR_NONE, UINT32_MAX, news);
742 }
743
744 return CommandCost();
745 }
746
748 static void DeleteNewsItem(NewsItem *ni)
749 {
750 /* Delete the news from the news queue. */
751 if (ni->prev != NULL) {
752 ni->prev->next = ni->next;
753 } else {
754 assert(_oldest_news == ni);
755 _oldest_news = ni->next;
756 }
757
758 if (ni->next != NULL) {
759 ni->next->prev = ni->prev;
760 } else {
761 assert(_latest_news == ni);
762 _latest_news = ni->prev;
763 }
764
765 _total_news--;
766
767 if (_forced_news == ni || _current_news == ni || _statusbar_news_item == ni) {
768 /* When we're the current news, go to the previous item first;
769 * we just possibly made that the last news item. */
770 if (_current_news == ni) _current_news = ni->prev;
771
772 /* About to remove the currently forced item (shown as newspapers) ||
773 * about to remove the currently displayed item (newspapers, ticker, or just a reminder) */
774 MoveToNextItem();
775 }
776
777 delete ni;
778
779 SetWindowDirty(WC_MESSAGE_HISTORY, 0);
780 }
781
788 void DeleteVehicleNews(VehicleID vid, StringID news)
789 {
790 NewsItem *ni = _oldest_news;
791
792 while (ni != NULL) {
793 NewsItem *next = ni->next;
794 if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
795 (news == INVALID_STRING_ID || ni->string_id == news)) {
796 DeleteNewsItem(ni);
797 }
798 ni = next;
799 }
800 }
801
807 void DeleteStationNews(StationID sid)
808 {
809 NewsItem *ni = _oldest_news;
810
811 while (ni != NULL) {
812 NewsItem *next = ni->next;
813 if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
814 DeleteNewsItem(ni);
815 }
816 ni = next;
817 }
818 }
819
824 void DeleteIndustryNews(IndustryID iid)
825 {
826 NewsItem *ni = _oldest_news;
827
828 while (ni != NULL) {
829 NewsItem *next = ni->next;
830 if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
831 DeleteNewsItem(ni);
832 }
833 ni = next;
834 }
835 }
836
840 void DeleteInvalidEngineNews()
841 {
842 NewsItem *ni = _oldest_news;
843
844 while (ni != NULL) {
845 NewsItem *next = ni->next;
846 if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
847 (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
848 DeleteNewsItem(ni);
849 }
850 ni = next;
851 }
852 }
853
854 static void RemoveOldNewsItems()
855 {
856 NewsItem *next;
857 for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != NULL; cur = next) {
858 next = cur->next;
859 if (_date - _news_type_data[cur->type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
860 }
861 }
862
869 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
870 {
871 for (NewsItem *ni = _oldest_news; ni != NULL; ni = ni->next) {
872 if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
873 if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
874 if (ni->flags & NF_VEHICLE_PARAM0 && ni->params[0] == from_index) ni->params[0] = to_index;
875 }
876 }
877
878 void NewsLoop()
879 {
880 /* no news item yet */
881 if (_total_news == 0) return;
882
883 /* There is no status bar, so no reason to show news;
884 * especially important with the end game screen when
885 * there is no status bar but possible news. */
886 if (FindWindowById(WC_STATUS_BAR, 0) == NULL) return;
887
888 static byte _last_clean_month = 0;
889
890 if (_last_clean_month != _cur_month) {
891 RemoveOldNewsItems();
892 _last_clean_month = _cur_month;
893 }
894
895 if (ReadyForNextItem()) MoveToNextItem();
896 }
897
899 static void ShowNewsMessage(const NewsItem *ni)
900 {
901 assert(_total_news != 0);
902
903 /* Delete the news window */
904 DeleteWindowById(WC_NEWS_WINDOW, 0);
905
906 /* setup forced news item */
907 _forced_news = ni;
908
909 if (_forced_news != NULL) {
910 DeleteWindowById(WC_NEWS_WINDOW, 0);
911 ShowNewspaper(ni);
912 }
913 }
914
916 void ShowLastNewsMessage()
917 {
918 const NewsItem *ni = NULL;
919 if (_total_news == 0) {
920 return;
921 } else if (_forced_news == NULL) {
922 /* Not forced any news yet, show the current one, unless a news window is
923 * open (which can only be the current one), then show the previous item */
924 if (_current_news == NULL) {
925 /* No news were shown yet resp. the last shown one was already deleted.
926 * Threat this as if _forced_news reached _oldest_news; so, wrap around and start anew with the latest. */
927 ni = _latest_news;
928 } else {
929 const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
930 ni = (w == NULL || (_current_news == _oldest_news)) ? _current_news : _current_news->prev;
931 }
932 } else if (_forced_news == _oldest_news) {
933 /* We have reached the oldest news, start anew with the latest */
934 ni = _latest_news;
935 } else {
936 /* 'Scrolling' through news history show each one in turn */
937 ni = _forced_news->prev;
938 }
939 bool wrap = false;
940 for (;;) {
941 if (_news_type_data[ni->type].GetDisplay() != ND_OFF) {
942 ShowNewsMessage(ni);
943 break;
944 }
945
946 ni = ni->prev;
947 if (ni == NULL) {
948 if (wrap) break;
949 /* We have reached the oldest news, start anew with the latest */
950 ni = _latest_news;
951 wrap = true;
952 }
953 }
954 }
955
956
967 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
968 {
969 char buffer[512], buffer2[512];
970 StringID str;
971
972 CopyInDParam(0, ni->params, lengthof(ni->params));
973 str = ni->string_id;
974
975 GetString(buffer, str, lastof(buffer));
976 /* Copy the just gotten string to another buffer to remove any formatting
977 * from it such as big fonts, etc. */
978 const char *ptr = buffer;
979 char *dest = buffer2;
980 WChar c_last = '\0';
981 for (;;) {
982 WChar c = Utf8Consume(&ptr);
983 if (c == 0) break;
984 /* Make a space from a newline, but ignore multiple newlines */
985 if (c == '\n' && c_last != '\n') {
986 dest[0] = ' ';
987 dest++;
988 } else if (c == '\r') {
989 dest[0] = dest[1] = dest[2] = dest[3] = ' ';
990 dest += 4;
991 } else if (IsPrintable(c)) {
992 dest += Utf8Encode(dest, c);
993 }
994 c_last = c;
995 }
996
997 *dest = '\0';
998 /* Truncate and show string; postfixed by '...' if necessary */
999 DrawString(left, right, y, buffer2, colour);
1000 }
1001
1002 struct MessageHistoryWindow : Window {
1003 static const int top_spacing;
1004 static const int bottom_spacing;
1005
1006 int line_height;
1007 int date_width;
1008
1009 Scrollbar *vscroll;
1010
1011 MessageHistoryWindow(WindowDesc *desc) : Window(desc)
1012 {
1013 this->CreateNestedTree();
1014 this->vscroll = this->GetScrollbar(WID_MH_SCROLLBAR);
1015 this->FinishInitNested(); // Initializes 'this->line_height' and 'this->date_width'.
1016 this->OnInvalidateData(0);
1017 }
1018
1019 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1020 {
1021 if (widget == WID_MH_BACKGROUND) {
1022 this->line_height = FONT_HEIGHT_NORMAL + 2;
1023 resize->height = this->line_height;
1024
1025 /* Months are off-by-one, so it's actually 8. Not using
1026 * month 12 because the 1 is usually less wide. */
1027 SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR, 7, 30));
1028 this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
1029
1030 size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
1031 size->width = max(200u, size->width); // At least 200 pixels wide.
1032 }
1033 }
1034
1035 virtual void OnPaint()
1036 {
1037 this->OnInvalidateData(0);
1038 this->DrawWidgets();
1039 }
1040
1041 virtual void DrawWidget(const Rect &r, int widget) const
1042 {
1043 if (widget != WID_MH_BACKGROUND || _total_news == 0) return;
1044
1045 /* Find the first news item to display. */
1046 NewsItem *ni = _latest_news;
1047 for (int n = this->vscroll->GetPosition(); n > 0; n--) {
1048 ni = ni->prev;
1049 if (ni == NULL) return;
1050 }
1051
1052 /* Fill the widget with news items. */
1053 int y = r.top + this->top_spacing;
1054 bool rtl = _current_text_dir == TD_RTL;
1055 uint date_left = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
1056 uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
1057 uint news_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
1058 uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
1059 for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
1060 SetDParam(0, ni->date);
1061 DrawString(date_left, date_right, y, STR_SHORT_DATE);
1062
1063 DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
1064 y += this->line_height;
1065
1066 ni = ni->prev;
1067 if (ni == NULL) return;
1068 }
1069 }
1070
1076 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1077 {
1078 if (!gui_scope) return;
1079 this->vscroll->SetCount(_total_news);
1080 }
1081
1082 virtual void OnClick(Point pt, int widget, int click_count)
1083 {
1084 if (widget == WID_MH_BACKGROUND) {
1085 NewsItem *ni = _latest_news;
1086 if (ni == NULL) return;
1087
1088 for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_MH_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
1089 ni = ni->prev;
1090 if (ni == NULL) return;
1091 }
1092
1093 ShowNewsMessage(ni);
1094 }
1095 }
1096
1097 virtual void OnResize()
1098 {
1099 this->vscroll->SetCapacityFromWidget(this, WID_MH_BACKGROUND);
1100 }
1101 };
1102
1103 const int MessageHistoryWindow::top_spacing = WD_FRAMERECT_TOP + 4;
1104 const int MessageHistoryWindow::bottom_spacing = WD_FRAMERECT_BOTTOM;
1105
1106 static const NWidgetPart _nested_message_history[] = {
1107 NWidget(NWID_HORIZONTAL),
1108 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1109 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1110 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1111 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1112 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1113 EndContainer(),
1114
1115 NWidget(NWID_HORIZONTAL),
1116 NWidget(WWT_PANEL, COLOUR_BROWN, WID_MH_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR),
1117 EndContainer(),
1118 NWidget(NWID_VERTICAL),
1119 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_MH_SCROLLBAR),
1120 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1121 EndContainer(),
1122 EndContainer(),
1123 };
1124
1125 static WindowDesc _message_history_desc(
1126 WDP_AUTO, "list_news", 400, 140,
1127 WC_MESSAGE_HISTORY, WC_NONE,
1128 0,
1129 _nested_message_history, lengthof(_nested_message_history)
1130 );
1131
1133 void ShowMessageHistory()
1134 {
1135 DeleteWindowById(WC_MESSAGE_HISTORY, 0);
1136 new MessageHistoryWindow(&_message_history_desc);
1137 }