OpenTTD
news_gui.cpp
Go to the documentation of this file.
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  */
7 
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 
38 #include "widgets/news_widget.h"
39 
40 #include "table/strings.h"
41 
42 #include "safeguards.h"
43 
44 const NewsItem *_statusbar_news_item = nullptr;
45 
46 static uint MIN_NEWS_AMOUNT = 30;
47 static uint _total_news = 0;
48 static NewsItem *_oldest_news = nullptr;
49 NewsItem *_latest_news = nullptr;
50 
57 static const NewsItem *_forced_news = nullptr;
58 
60 static const NewsItem *_current_news = nullptr;
61 
62 
69 static TileIndex GetReferenceTile(NewsReferenceType reftype, uint32 ref)
70 {
71  switch (reftype) {
72  case NR_TILE: return (TileIndex)ref;
73  case NR_STATION: return Station::Get((StationID)ref)->xy;
74  case NR_INDUSTRY: return Industry::Get((IndustryID)ref)->location.tile + TileDiffXY(1, 1);
75  case NR_TOWN: return Town::Get((TownID)ref)->xy;
76  default: return INVALID_TILE;
77  }
78 }
79 
80 /* Normal news items. */
81 static const NWidgetPart _nested_normal_news_widgets[] = {
82  NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
83  NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
84  NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
85  NWidget(NWID_SPACER), SetFill(1, 0),
87  NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
88  NWidget(NWID_SPACER), SetFill(0, 1),
89  EndContainer(),
90  EndContainer(),
91  NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 154), SetPadding(0, 5, 1, 5),
92  EndContainer(),
93 };
94 
95 static WindowDesc _normal_news_desc(
96  WDP_MANUAL, nullptr, 0, 0,
98  0,
99  _nested_normal_news_widgets, lengthof(_nested_normal_news_widgets)
100 );
101 
102 /* New vehicles news items. */
103 static const NWidgetPart _nested_vehicle_news_widgets[] = {
104  NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
105  NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
107  NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
108  NWidget(NWID_SPACER), SetFill(0, 1),
109  EndContainer(),
110  NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_VEH_TITLE), SetFill(1, 1), SetMinimalSize(419, 55), SetDataTip(STR_EMPTY, STR_NULL),
111  EndContainer(),
112  NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_VEH_BKGND), SetPadding(0, 25, 1, 25),
114  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_NAME), SetMinimalSize(369, 33), SetFill(1, 0),
115  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_SPR), SetMinimalSize(369, 32), SetFill(1, 0),
116  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_N_VEH_INFO), SetMinimalSize(369, 46), SetFill(1, 0),
117  EndContainer(),
118  EndContainer(),
119  EndContainer(),
120 };
121 
122 static WindowDesc _vehicle_news_desc(
123  WDP_MANUAL, nullptr, 0, 0,
125  0,
126  _nested_vehicle_news_widgets, lengthof(_nested_vehicle_news_widgets)
127 );
128 
129 /* Company news items. */
130 static const NWidgetPart _nested_company_news_widgets[] = {
131  NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
132  NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
134  NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
135  NWidget(NWID_SPACER), SetFill(0, 1),
136  EndContainer(),
137  NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_TITLE), SetFill(1, 1), SetMinimalSize(410, 20), SetDataTip(STR_EMPTY, STR_NULL),
138  EndContainer(),
139  NWidget(NWID_HORIZONTAL), SetPadding(0, 1, 1, 1),
141  NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_FACE), SetMinimalSize(93, 119), SetPadding(2, 6, 2, 1),
142  NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MGR_NAME), SetMinimalSize(93, 24), SetPadding(0, 0, 0, 1),
143  NWidget(NWID_SPACER), SetFill(0, 1),
144  EndContainer(),
145  NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_COMPANY_MSG), SetFill(1, 1), SetMinimalSize(328, 150),
146  EndContainer(),
147  EndContainer(),
148 };
149 
150 static WindowDesc _company_news_desc(
151  WDP_MANUAL, nullptr, 0, 0,
153  0,
154  _nested_company_news_widgets, lengthof(_nested_company_news_widgets)
155 );
156 
157 /* Thin news items. */
158 static const NWidgetPart _nested_thin_news_widgets[] = {
159  NWidget(WWT_PANEL, COLOUR_WHITE, WID_N_PANEL),
160  NWidget(NWID_HORIZONTAL), SetPadding(1, 1, 0, 1),
161  NWidget(WWT_CLOSEBOX, COLOUR_WHITE, WID_N_CLOSEBOX), SetPadding(0, 0, 0, 1),
162  NWidget(NWID_SPACER), SetFill(1, 0),
164  NWidget(WWT_LABEL, COLOUR_WHITE, WID_N_DATE), SetDataTip(STR_DATE_LONG_SMALL, STR_NULL),
165  NWidget(NWID_SPACER), SetFill(0, 1),
166  EndContainer(),
167  EndContainer(),
168  NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(428, 48), SetFill(1, 0), SetPadding(0, 5, 0, 5),
169  NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetMinimalSize(426, 70), SetPadding(1, 2, 2, 2),
170  EndContainer(),
171 };
172 
173 static WindowDesc _thin_news_desc(
174  WDP_MANUAL, nullptr, 0, 0,
176  0,
177  _nested_thin_news_widgets, lengthof(_nested_thin_news_widgets)
178 );
179 
180 /* Small news items. */
181 static const NWidgetPart _nested_small_news_widgets[] = {
182  /* Caption + close box. The caption is no WWT_CAPTION as the window shall not be moveable and so on. */
184  NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE, WID_N_CLOSEBOX),
185  NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_N_CAPTION), SetFill(1, 0),
186  NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_N_SHOW_GROUP), SetMinimalSize(14, 11), SetResize(1, 0),
187  SetDataTip(STR_NULL /* filled in later */, STR_NEWS_SHOW_VEHICLE_GROUP_TOOLTIP),
188  EndContainer(),
189 
190  /* Main part */
191  NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_N_HEADLINE),
192  NWidget(WWT_INSET, COLOUR_LIGHT_BLUE, WID_N_INSET), SetPadding(2, 2, 2, 2),
193  NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_N_VIEWPORT), SetPadding(1, 1, 1, 1), SetMinimalSize(274, 47), SetFill(1, 0),
194  EndContainer(),
195  NWidget(WWT_EMPTY, COLOUR_WHITE, WID_N_MESSAGE), SetMinimalSize(275, 20), SetFill(1, 0), SetPadding(0, 5, 0, 5),
196  EndContainer(),
197 };
198 
199 static WindowDesc _small_news_desc(
200  WDP_MANUAL, nullptr, 0, 0,
202  0,
203  _nested_small_news_widgets, lengthof(_nested_small_news_widgets)
204 );
205 
210  &_thin_news_desc,
211  &_small_news_desc,
212  &_normal_news_desc,
213  &_vehicle_news_desc,
214  &_company_news_desc,
215 };
216 
217 WindowDesc* GetNewsWindowLayout(NewsFlag flags)
218 {
219  uint layout = GB(flags, NFB_WINDOW_LAYOUT, NFB_WINDOW_LAYOUT_COUNT);
220  assert(layout < lengthof(_news_window_layout));
221  return _news_window_layout[layout];
222 }
223 
228  /* name, age, sound, */
229  NewsTypeData("news_display.arrival_player", 60, SND_1D_APPLAUSE ),
230  NewsTypeData("news_display.arrival_other", 60, SND_1D_APPLAUSE ),
231  NewsTypeData("news_display.accident", 90, SND_BEGIN ),
232  NewsTypeData("news_display.company_info", 60, SND_BEGIN ),
233  NewsTypeData("news_display.open", 90, SND_BEGIN ),
234  NewsTypeData("news_display.close", 90, SND_BEGIN ),
235  NewsTypeData("news_display.economy", 30, SND_BEGIN ),
236  NewsTypeData("news_display.production_player", 30, SND_BEGIN ),
237  NewsTypeData("news_display.production_other", 30, SND_BEGIN ),
238  NewsTypeData("news_display.production_nobody", 30, SND_BEGIN ),
239  NewsTypeData("news_display.advice", 150, SND_BEGIN ),
240  NewsTypeData("news_display.new_vehicles", 30, SND_1E_OOOOH ),
241  NewsTypeData("news_display.acceptance", 90, SND_BEGIN ),
242  NewsTypeData("news_display.subsidies", 180, SND_BEGIN ),
243  NewsTypeData("news_display.general", 60, SND_BEGIN ),
244 };
245 
246 assert_compile(lengthof(_news_type_data) == NT_END);
247 
253 {
254  uint index;
255  const SettingDesc *sd = GetSettingFromName(this->name, &index);
256  assert(sd != nullptr);
257  void *ptr = GetVariableAddress(nullptr, &sd->save);
258  return (NewsDisplay)ReadValue(ptr, sd->save.conv);
259 }
260 
262 struct NewsWindow : Window {
263  uint16 chat_height;
264  uint16 status_height;
265  const NewsItem *ni;
266  static int duration;
267 
268  GUITimer timer;
269 
270  NewsWindow(WindowDesc *desc, const NewsItem *ni) : Window(desc), ni(ni)
271  {
272  NewsWindow::duration = 16650;
274  this->chat_height = (w != nullptr) ? w->height : 0;
275  this->status_height = FindWindowById(WC_STATUS_BAR, 0)->height;
276 
277  this->flags |= WF_DISABLE_VP_SCROLL;
278 
279  this->timer.SetInterval(15);
280 
281  this->CreateNestedTree();
282 
283  /* For company news with a face we have a separate headline in param[0] */
284  if (desc == &_company_news_desc) this->GetWidget<NWidgetCore>(WID_N_TITLE)->widget_data = this->ni->params[0];
285 
286  NWidgetCore *nwid = this->GetWidget<NWidgetCore>(WID_N_SHOW_GROUP);
287  if (ni->reftype1 == NR_VEHICLE && nwid != nullptr) {
288  const Vehicle *v = Vehicle::Get(ni->ref1);
289  switch (v->type) {
290  case VEH_TRAIN:
291  nwid->widget_data = STR_TRAIN;
292  break;
293  case VEH_ROAD:
294  nwid->widget_data = RoadVehicle::From(v)->IsBus() ? STR_BUS : STR_LORRY;
295  break;
296  case VEH_SHIP:
297  nwid->widget_data = STR_SHIP;
298  break;
299  case VEH_AIRCRAFT:
300  nwid->widget_data = STR_PLANE;
301  break;
302  default:
303  break; // Do nothing
304  }
305  }
306 
307  this->FinishInitNested(0);
308 
309  /* Initialize viewport if it exists. */
310  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_N_VIEWPORT);
311  if (nvp != nullptr) {
312  nvp->InitializeViewport(this, ni->reftype1 == NR_VEHICLE ? 0x80000000 | ni->ref1 : GetReferenceTile(ni->reftype1, ni->ref1), ZOOM_LVL_NEWS);
313  if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags |= ND_NO_TRANSPARENCY;
314  if ((this->ni->flags & NF_INCOLOUR) == 0) {
315  nvp->disp_flags |= ND_SHADE_GREY;
316  } else if (this->ni->flags & NF_SHADE) {
317  nvp->disp_flags |= ND_SHADE_DIMMED;
318  }
319  }
320 
321  PositionNewsMessage(this);
322  }
323 
324  void DrawNewsBorder(const Rect &r) const
325  {
326  GfxFillRect(r.left, r.top, r.right, r.bottom, PC_WHITE);
327 
328  GfxFillRect(r.left, r.top, r.left, r.bottom, PC_BLACK);
329  GfxFillRect(r.right, r.top, r.right, r.bottom, PC_BLACK);
330  GfxFillRect(r.left, r.top, r.right, r.top, PC_BLACK);
331  GfxFillRect(r.left, r.bottom, r.right, r.bottom, PC_BLACK);
332  }
333 
334  Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
335  {
336  Point pt = { 0, _screen.height };
337  return pt;
338  }
339 
340  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
341  {
342  StringID str = STR_NULL;
343  switch (widget) {
344  case WID_N_CAPTION: {
345  /* Caption is not a real caption (so that the window cannot be moved)
346  * thus it doesn't get the default sizing of a caption. */
347  Dimension d2 = GetStringBoundingBox(STR_NEWS_MESSAGE_CAPTION);
349  *size = maxdim(*size, d2);
350  return;
351  }
352 
353  case WID_N_MGR_FACE:
354  *size = maxdim(*size, GetSpriteSize(SPR_GRADIENT));
355  break;
356 
357  case WID_N_MGR_NAME:
358  SetDParamStr(0, static_cast<const CompanyNewsInformation *>(this->ni->free_data)->president_name);
359  str = STR_JUST_RAW_STRING;
360  break;
361 
362  case WID_N_MESSAGE:
363  CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
364  str = this->ni->string_id;
365  break;
366 
367  case WID_N_COMPANY_MSG:
368  str = this->GetCompanyMessageString();
369  break;
370 
371  case WID_N_VEH_NAME:
372  case WID_N_VEH_TITLE:
373  str = this->GetNewVehicleMessageString(widget);
374  break;
375 
376  case WID_N_VEH_INFO: {
377  assert(this->ni->reftype1 == NR_ENGINE);
378  EngineID engine = this->ni->ref1;
379  str = GetEngineInfoString(engine);
380  break;
381  }
382 
383  case WID_N_SHOW_GROUP:
384  if (this->ni->reftype1 == NR_VEHICLE) {
385  Dimension d2 = GetStringBoundingBox(this->GetWidget<NWidgetCore>(WID_N_SHOW_GROUP)->widget_data);
388  *size = d2;
389  } else {
390  /* Hide 'Show group window' button if this news is not about a vehicle. */
391  size->width = 0;
392  size->height = 0;
393  resize->width = 0;
394  resize->height = 0;
395  fill->width = 0;
396  fill->height = 0;
397  }
398  return;
399 
400  default:
401  return; // Do nothing
402  }
403 
404  /* Update minimal size with length of the multi-line string. */
405  Dimension d = *size;
406  d.width = (d.width >= padding.width) ? d.width - padding.width : 0;
407  d.height = (d.height >= padding.height) ? d.height - padding.height : 0;
408  d = GetStringMultiLineBoundingBox(str, d);
409  d.width += padding.width;
410  d.height += padding.height;
411  *size = maxdim(*size, d);
412  }
413 
414  void SetStringParameters(int widget) const override
415  {
416  if (widget == WID_N_DATE) SetDParam(0, this->ni->date);
417  }
418 
419  void DrawWidget(const Rect &r, int widget) const override
420  {
421  switch (widget) {
422  case WID_N_CAPTION:
423  DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION);
424  break;
425 
426  case WID_N_PANEL:
427  this->DrawNewsBorder(r);
428  break;
429 
430  case WID_N_MESSAGE:
431  CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
432  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->ni->string_id, TC_FROMSTRING, SA_CENTER);
433  break;
434 
435  case WID_N_MGR_FACE: {
436  const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
437  DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top);
438  GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
439  break;
440  }
441  case WID_N_MGR_NAME: {
442  const CompanyNewsInformation *cni = (const CompanyNewsInformation*)this->ni->free_data;
443  SetDParamStr(0, cni->president_name);
444  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
445  break;
446  }
447  case WID_N_COMPANY_MSG:
448  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetCompanyMessageString(), TC_FROMSTRING, SA_CENTER);
449  break;
450 
451  case WID_N_VEH_BKGND:
452  GfxFillRect(r.left, r.top, r.right, r.bottom, PC_GREY);
453  break;
454 
455  case WID_N_VEH_NAME:
456  case WID_N_VEH_TITLE:
457  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->GetNewVehicleMessageString(widget), TC_FROMSTRING, SA_CENTER);
458  break;
459 
460  case WID_N_VEH_SPR: {
461  assert(this->ni->reftype1 == NR_ENGINE);
462  EngineID engine = this->ni->ref1;
463  DrawVehicleEngine(r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW);
464  GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
465  break;
466  }
467  case WID_N_VEH_INFO: {
468  assert(this->ni->reftype1 == NR_ENGINE);
469  EngineID engine = this->ni->ref1;
470  DrawStringMultiLine(r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
471  break;
472  }
473  }
474  }
475 
476  void OnClick(Point pt, int widget, int click_count) override
477  {
478  switch (widget) {
479  case WID_N_CLOSEBOX:
481  delete this;
482  _forced_news = nullptr;
483  break;
484 
485  case WID_N_CAPTION:
486  if (this->ni->reftype1 == NR_VEHICLE) {
487  const Vehicle *v = Vehicle::Get(this->ni->ref1);
489  }
490  break;
491 
492  case WID_N_VIEWPORT:
493  break; // Ignore clicks
494 
495  case WID_N_SHOW_GROUP:
496  if (this->ni->reftype1 == NR_VEHICLE) {
497  const Vehicle *v = Vehicle::Get(this->ni->ref1);
499  }
500  break;
501  default:
502  if (this->ni->reftype1 == NR_VEHICLE) {
503  const Vehicle *v = Vehicle::Get(this->ni->ref1);
504  ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
505  } else {
506  TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
507  TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
508  if (_ctrl_pressed) {
509  if (tile1 != INVALID_TILE) ShowExtraViewPortWindow(tile1);
510  if (tile2 != INVALID_TILE) ShowExtraViewPortWindow(tile2);
511  } else {
512  if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
513  ScrollMainWindowToTile(tile2);
514  }
515  }
516  }
517  break;
518  }
519  }
520 
521  EventState OnKeyPress(WChar key, uint16 keycode) override
522  {
523  if (keycode == WKC_SPACE) {
524  /* Don't continue. */
525  delete this;
526  return ES_HANDLED;
527  }
528  return ES_NOT_HANDLED;
529  }
530 
536  void OnInvalidateData(int data = 0, bool gui_scope = true) override
537  {
538  if (!gui_scope) return;
539  /* The chatbar has notified us that is was either created or closed */
540  int newtop = this->top + this->chat_height - data;
541  this->chat_height = data;
542  this->SetWindowTop(newtop);
543  }
544 
545  void OnRealtimeTick(uint delta_ms) override
546  {
547  int count = this->timer.CountElapsed(delta_ms);
548  if (count > 0) {
549  /* Scroll up newsmessages from the bottom */
550  int newtop = max(this->top - 2 * count, _screen.height - this->height - this->status_height - this->chat_height);
551  this->SetWindowTop(newtop);
552  }
553 
554  /* Decrement the news timer. We don't need to action an elapsed event here,
555  * so no need to use TimerElapsed(). */
556  if (NewsWindow::duration > 0) NewsWindow::duration -= delta_ms;
557  }
558 
559 private:
564  void SetWindowTop(int newtop)
565  {
566  if (this->top == newtop) return;
567 
568  int mintop = min(newtop, this->top);
569  int maxtop = max(newtop, this->top);
570  if (this->viewport != nullptr) this->viewport->top += newtop - this->top;
571  this->top = newtop;
572 
573  SetDirtyBlocks(this->left, mintop, this->left + this->width, maxtop + this->height);
574  }
575 
576  StringID GetCompanyMessageString() const
577  {
578  /* Company news with a face have a separate headline, so the normal message is shifted by two params */
579  CopyInDParam(0, this->ni->params + 2, lengthof(this->ni->params) - 2);
580  return this->ni->params[1];
581  }
582 
583  StringID GetNewVehicleMessageString(int widget) const
584  {
585  assert(this->ni->reftype1 == NR_ENGINE);
586  EngineID engine = this->ni->ref1;
587 
588  switch (widget) {
589  case WID_N_VEH_TITLE:
590  SetDParam(0, GetEngineCategoryName(engine));
591  return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
592 
593  case WID_N_VEH_NAME:
594  SetDParam(0, engine);
595  return STR_NEWS_NEW_VEHICLE_TYPE;
596 
597  default:
598  NOT_REACHED();
599  }
600  }
601 };
602 
603 /* static */ int NewsWindow::duration = 0; // Instance creation.
604 
605 
607 static void ShowNewspaper(const NewsItem *ni)
608 {
609  SoundFx sound = _news_type_data[ni->type].sound;
610  if (sound != 0 && _settings_client.sound.news_full) SndPlayFx(sound);
611 
612  new NewsWindow(GetNewsWindowLayout(ni->flags), ni);
613 }
614 
616 static void ShowTicker(const NewsItem *ni)
617 {
618  if (_settings_client.sound.news_ticker) SndPlayFx(SND_16_MORSE);
619 
620  _statusbar_news_item = ni;
622 }
623 
626 {
627  for (NewsItem *ni = _oldest_news; ni != nullptr; ) {
628  NewsItem *next = ni->next;
629  delete ni;
630  ni = next;
631  }
632 
633  _total_news = 0;
634  _oldest_news = nullptr;
635  _latest_news = nullptr;
636  _forced_news = nullptr;
637  _current_news = nullptr;
638  _statusbar_news_item = nullptr;
640 }
641 
647 {
648  const NewsItem *ni = _statusbar_news_item;
649  if (ni == nullptr) return true;
650 
651  /* Ticker message
652  * Check if the status bar message is still being displayed? */
653  if (IsNewsTickerShown()) return false;
654  return true;
655 }
656 
661 static bool ReadyForNextNewsItem()
662 {
663  const NewsItem *ni = _forced_news == nullptr ? _current_news : _forced_news;
664  if (ni == nullptr) return true;
665 
666  /* neither newsticker nor newspaper are running */
667  return (NewsWindow::duration <= 0 || FindWindowById(WC_NEWS_WINDOW, 0) == nullptr);
668 }
669 
671 static void MoveToNextTickerItem()
672 {
673  InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
674 
675  /* if we're not at the last item, then move on */
676  while (_statusbar_news_item != _latest_news) {
677  _statusbar_news_item = (_statusbar_news_item == nullptr) ? _oldest_news : _statusbar_news_item->next;
678  const NewsItem *ni = _statusbar_news_item;
679  const NewsType type = ni->type;
680 
681  /* check the date, don't show too old items */
682  if (_date - _news_type_data[type].age > ni->date) continue;
683 
684  switch (_news_type_data[type].GetDisplay()) {
685  default: NOT_REACHED();
686  case ND_OFF: // Off - show nothing only a small reminder in the status bar
688  break;
689 
690  case ND_SUMMARY: // Summary - show ticker
691  ShowTicker(ni);
692  break;
693 
694  case ND_FULL: // Full - show newspaper, skipped here
695  continue;
696  }
697  return;
698  }
699 }
700 
702 static void MoveToNextNewsItem()
703 {
704  DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
705  _forced_news = nullptr;
706 
707  /* if we're not at the last item, then move on */
708  while (_current_news != _latest_news) {
709  _current_news = (_current_news == nullptr) ? _oldest_news : _current_news->next;
710  const NewsItem *ni = _current_news;
711  const NewsType type = ni->type;
712 
713  /* check the date, don't show too old items */
714  if (_date - _news_type_data[type].age > ni->date) continue;
715 
716  switch (_news_type_data[type].GetDisplay()) {
717  default: NOT_REACHED();
718  case ND_OFF: // Off - show nothing only a small reminder in the status bar, skipped here
719  continue;
720 
721  case ND_SUMMARY: // Summary - show ticker, skipped here
722  continue;
723 
724  case ND_FULL: // Full - show newspaper
725  ShowNewspaper(ni);
726  break;
727  }
728  return;
729  }
730 }
731 
745 void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
746 {
747  if (_game_mode == GM_MENU) return;
748 
749  /* Create new news item node */
750  NewsItem *ni = new NewsItem;
751 
752  ni->string_id = string;
753  ni->type = type;
754  ni->flags = flags;
755 
756  /* show this news message in colour? */
758 
759  ni->reftype1 = reftype1;
760  ni->reftype2 = reftype2;
761  ni->ref1 = ref1;
762  ni->ref2 = ref2;
763  ni->free_data = free_data;
764  ni->date = _date;
765  CopyOutDParam(ni->params, 0, lengthof(ni->params));
766 
767  if (_total_news++ == 0) {
768  assert(_oldest_news == nullptr);
769  _oldest_news = ni;
770  ni->prev = nullptr;
771  } else {
772  assert(_latest_news->next == nullptr);
773  _latest_news->next = ni;
774  ni->prev = _latest_news;
775  }
776 
777  ni->next = nullptr;
778  _latest_news = ni;
779 
781 }
782 
795 CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
796 {
797  if (_current_company != OWNER_DEITY) return CMD_ERROR;
798 
799  NewsType type = (NewsType)GB(p1, 0, 8);
800  NewsReferenceType reftype1 = (NewsReferenceType)GB(p1, 8, 8);
801  CompanyID company = (CompanyID)GB(p1, 16, 8);
802 
803  if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR;
804  if (type >= NT_END) return CMD_ERROR;
805  if (StrEmpty(text)) return CMD_ERROR;
806 
807  switch (reftype1) {
808  case NR_NONE: break;
809  case NR_TILE:
810  if (!IsValidTile(p2)) return CMD_ERROR;
811  break;
812 
813  case NR_VEHICLE:
814  if (!Vehicle::IsValidID(p2)) return CMD_ERROR;
815  break;
816 
817  case NR_STATION:
818  if (!Station::IsValidID(p2)) return CMD_ERROR;
819  break;
820 
821  case NR_INDUSTRY:
822  if (!Industry::IsValidID(p2)) return CMD_ERROR;
823  break;
824 
825  case NR_TOWN:
826  if (!Town::IsValidID(p2)) return CMD_ERROR;
827  break;
828 
829  case NR_ENGINE:
830  if (!Engine::IsValidID(p2)) return CMD_ERROR;
831  break;
832 
833  default: return CMD_ERROR;
834  }
835 
836  if (company != INVALID_OWNER && company != _local_company) return CommandCost();
837 
838  if (flags & DC_EXEC) {
839  char *news = stredup(text);
840  SetDParamStr(0, news);
841  AddNewsItem(STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype1, p2, NR_NONE, UINT32_MAX, news);
842  }
843 
844  return CommandCost();
845 }
846 
848 static void DeleteNewsItem(NewsItem *ni)
849 {
850  /* Delete the news from the news queue. */
851  if (ni->prev != nullptr) {
852  ni->prev->next = ni->next;
853  } else {
854  assert(_oldest_news == ni);
855  _oldest_news = ni->next;
856  }
857 
858  if (ni->next != nullptr) {
859  ni->next->prev = ni->prev;
860  } else {
861  assert(_latest_news == ni);
862  _latest_news = ni->prev;
863  }
864 
865  _total_news--;
866 
867  if (_forced_news == ni || _current_news == ni) {
868  /* When we're the current news, go to the previous item first;
869  * we just possibly made that the last news item. */
870  if (_current_news == ni) _current_news = ni->prev;
871 
872  /* About to remove the currently forced item (shown as newspapers) ||
873  * about to remove the currently displayed item (newspapers) */
875  }
876 
877  if (_statusbar_news_item == ni) {
878  /* When we're the current news, go to the previous item first;
879  * we just possibly made that the last news item. */
880  _statusbar_news_item = ni->prev;
881 
882  /* About to remove the currently displayed item (ticker, or just a reminder) */
884  }
885 
886  delete ni;
887 
889 }
890 
898 {
899  NewsItem *ni = _oldest_news;
900 
901  while (ni != nullptr) {
902  NewsItem *next = ni->next;
903  if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
904  (news == INVALID_STRING_ID || ni->string_id == news)) {
905  DeleteNewsItem(ni);
906  }
907  ni = next;
908  }
909 }
910 
916 void DeleteStationNews(StationID sid)
917 {
918  NewsItem *ni = _oldest_news;
919 
920  while (ni != nullptr) {
921  NewsItem *next = ni->next;
922  if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
923  DeleteNewsItem(ni);
924  }
925  ni = next;
926  }
927 }
928 
933 void DeleteIndustryNews(IndustryID iid)
934 {
935  NewsItem *ni = _oldest_news;
936 
937  while (ni != nullptr) {
938  NewsItem *next = ni->next;
939  if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
940  DeleteNewsItem(ni);
941  }
942  ni = next;
943  }
944 }
945 
950 {
951  NewsItem *ni = _oldest_news;
952 
953  while (ni != nullptr) {
954  NewsItem *next = ni->next;
955  if ((ni->reftype1 == NR_ENGINE && (!Engine::IsValidID(ni->ref1) || !Engine::Get(ni->ref1)->IsEnabled())) ||
956  (ni->reftype2 == NR_ENGINE && (!Engine::IsValidID(ni->ref2) || !Engine::Get(ni->ref2)->IsEnabled()))) {
957  DeleteNewsItem(ni);
958  }
959  ni = next;
960  }
961 }
962 
963 static void RemoveOldNewsItems()
964 {
965  NewsItem *next;
966  for (NewsItem *cur = _oldest_news; _total_news > MIN_NEWS_AMOUNT && cur != nullptr; cur = next) {
967  next = cur->next;
968  if (_date - _news_type_data[cur->type].age * _settings_client.gui.news_message_timeout > cur->date) DeleteNewsItem(cur);
969  }
970 }
971 
978 void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
979 {
980  for (NewsItem *ni = _oldest_news; ni != nullptr; ni = ni->next) {
981  if (ni->reftype1 == NR_VEHICLE && ni->ref1 == from_index) ni->ref1 = to_index;
982  if (ni->reftype2 == NR_VEHICLE && ni->ref2 == from_index) ni->ref2 = to_index;
983  if (ni->flags & NF_VEHICLE_PARAM0 && ni->params[0] == from_index) ni->params[0] = to_index;
984  }
985 }
986 
987 void NewsLoop()
988 {
989  /* no news item yet */
990  if (_total_news == 0) return;
991 
992  /* There is no status bar, so no reason to show news;
993  * especially important with the end game screen when
994  * there is no status bar but possible news. */
995  if (FindWindowById(WC_STATUS_BAR, 0) == nullptr) return;
996 
997  static byte _last_clean_month = 0;
998 
999  if (_last_clean_month != _cur_month) {
1000  RemoveOldNewsItems();
1001  _last_clean_month = _cur_month;
1002  }
1003 
1006 }
1007 
1009 static void ShowNewsMessage(const NewsItem *ni)
1010 {
1011  assert(_total_news != 0);
1012 
1013  /* Delete the news window */
1015 
1016  /* setup forced news item */
1017  _forced_news = ni;
1018 
1019  if (_forced_news != nullptr) {
1021  ShowNewspaper(ni);
1022  }
1023 }
1024 
1027 {
1028  const NewsItem *ni = nullptr;
1029  if (_total_news == 0) {
1030  return;
1031  } else if (_forced_news == nullptr) {
1032  /* Not forced any news yet, show the current one, unless a news window is
1033  * open (which can only be the current one), then show the previous item */
1034  if (_current_news == nullptr) {
1035  /* No news were shown yet resp. the last shown one was already deleted.
1036  * Threat this as if _forced_news reached _oldest_news; so, wrap around and start anew with the latest. */
1037  ni = _latest_news;
1038  } else {
1039  const Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
1040  ni = (w == nullptr || (_current_news == _oldest_news)) ? _current_news : _current_news->prev;
1041  }
1042  } else if (_forced_news == _oldest_news) {
1043  /* We have reached the oldest news, start anew with the latest */
1044  ni = _latest_news;
1045  } else {
1046  /* 'Scrolling' through news history show each one in turn */
1047  ni = _forced_news->prev;
1048  }
1049  bool wrap = false;
1050  for (;;) {
1051  if (_news_type_data[ni->type].GetDisplay() != ND_OFF) {
1052  ShowNewsMessage(ni);
1053  break;
1054  }
1055 
1056  ni = ni->prev;
1057  if (ni == nullptr) {
1058  if (wrap) break;
1059  /* We have reached the oldest news, start anew with the latest */
1060  ni = _latest_news;
1061  wrap = true;
1062  }
1063  }
1064 }
1065 
1066 
1076 static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
1077 {
1078  char buffer[512], buffer2[512];
1079  StringID str;
1080 
1081  CopyInDParam(0, ni->params, lengthof(ni->params));
1082  str = ni->string_id;
1083 
1084  GetString(buffer, str, lastof(buffer));
1085  /* Copy the just gotten string to another buffer to remove any formatting
1086  * from it such as big fonts, etc. */
1087  const char *ptr = buffer;
1088  char *dest = buffer2;
1089  WChar c_last = '\0';
1090  for (;;) {
1091  WChar c = Utf8Consume(&ptr);
1092  if (c == 0) break;
1093  /* Make a space from a newline, but ignore multiple newlines */
1094  if (c == '\n' && c_last != '\n') {
1095  dest[0] = ' ';
1096  dest++;
1097  } else if (c == '\r') {
1098  dest[0] = dest[1] = dest[2] = dest[3] = ' ';
1099  dest += 4;
1100  } else if (IsPrintable(c)) {
1101  dest += Utf8Encode(dest, c);
1102  }
1103  c_last = c;
1104  }
1105 
1106  *dest = '\0';
1107  /* Truncate and show string; postfixed by '...' if necessary */
1108  DrawString(left, right, y, buffer2, colour);
1109 }
1110 
1112  static const int top_spacing;
1113  static const int bottom_spacing;
1114 
1115  int line_height;
1117 
1119 
1120  MessageHistoryWindow(WindowDesc *desc) : Window(desc)
1121  {
1122  this->CreateNestedTree();
1123  this->vscroll = this->GetScrollbar(WID_MH_SCROLLBAR);
1124  this->FinishInitNested(); // Initializes 'this->line_height' and 'this->date_width'.
1125  this->OnInvalidateData(0);
1126  }
1127 
1128  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1129  {
1130  if (widget == WID_MH_BACKGROUND) {
1131  this->line_height = FONT_HEIGHT_NORMAL + 2;
1132  resize->height = this->line_height;
1133 
1134  /* Months are off-by-one, so it's actually 8. Not using
1135  * month 12 because the 1 is usually less wide. */
1137  this->date_width = GetStringBoundingBox(STR_SHORT_DATE).width;
1138 
1139  size->height = 4 * resize->height + this->top_spacing + this->bottom_spacing; // At least 4 lines are visible.
1140  size->width = max(200u, size->width); // At least 200 pixels wide.
1141  }
1142  }
1143 
1144  void OnPaint() override
1145  {
1146  this->OnInvalidateData(0);
1147  this->DrawWidgets();
1148  }
1149 
1150  void DrawWidget(const Rect &r, int widget) const override
1151  {
1152  if (widget != WID_MH_BACKGROUND || _total_news == 0) return;
1153 
1154  /* Find the first news item to display. */
1155  NewsItem *ni = _latest_news;
1156  for (int n = this->vscroll->GetPosition(); n > 0; n--) {
1157  ni = ni->prev;
1158  if (ni == nullptr) return;
1159  }
1160 
1161  /* Fill the widget with news items. */
1162  int y = r.top + this->top_spacing;
1163  bool rtl = _current_text_dir == TD_RTL;
1164  uint date_left = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width : r.left + WD_FRAMERECT_LEFT;
1165  uint date_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->date_width;
1166  uint news_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->date_width + WD_FRAMERECT_RIGHT;
1167  uint news_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->date_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
1168  for (int n = this->vscroll->GetCapacity(); n > 0; n--) {
1169  SetDParam(0, ni->date);
1170  DrawString(date_left, date_right, y, STR_SHORT_DATE);
1171 
1172  DrawNewsString(news_left, news_right, y, TC_WHITE, ni);
1173  y += this->line_height;
1174 
1175  ni = ni->prev;
1176  if (ni == nullptr) return;
1177  }
1178  }
1179 
1185  void OnInvalidateData(int data = 0, bool gui_scope = true) override
1186  {
1187  if (!gui_scope) return;
1188  this->vscroll->SetCount(_total_news);
1189  }
1190 
1191  void OnClick(Point pt, int widget, int click_count) override
1192  {
1193  if (widget == WID_MH_BACKGROUND) {
1194  NewsItem *ni = _latest_news;
1195  if (ni == nullptr) return;
1196 
1197  for (int n = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_MH_BACKGROUND, WD_FRAMERECT_TOP, this->line_height); n > 0; n--) {
1198  ni = ni->prev;
1199  if (ni == nullptr) return;
1200  }
1201 
1202  ShowNewsMessage(ni);
1203  }
1204  }
1205 
1206  void OnResize() override
1207  {
1208  this->vscroll->SetCapacityFromWidget(this, WID_MH_BACKGROUND);
1209  }
1210 };
1211 
1214 
1215 static const NWidgetPart _nested_message_history[] = {
1217  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1218  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MESSAGE_HISTORY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1219  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1220  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1221  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1222  EndContainer(),
1223 
1225  NWidget(WWT_PANEL, COLOUR_BROWN, WID_MH_BACKGROUND), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR),
1226  EndContainer(),
1228  NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_MH_SCROLLBAR),
1229  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1230  EndContainer(),
1231  EndContainer(),
1232 };
1233 
1234 static WindowDesc _message_history_desc(
1235  WDP_AUTO, "list_news", 400, 140,
1237  0,
1238  _nested_message_history, lengthof(_nested_message_history)
1239 );
1240 
1243 {
1245  new MessageHistoryWindow(&_message_history_desc);
1246 }
EventState
State of handling an event.
Definition: window_type.h:711
Nested widget containing a viewport.
Definition: widget_type.h:79
void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
Draw an engine.
Definition: engine_gui.cpp:296
NewsItem * _latest_news
tail of news items queue
Definition: news_gui.cpp:49
void OnResize() override
Called after the window got resized.
Definition: news_gui.cpp:1206
Functions related to OTTD&#39;s strings.
void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.
Definition: news_gui.cpp:978
Background of the window.
Definition: news_widget.h:40
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Road vehicle states.
Face of the manager.
Definition: news_widget.h:27
void DeleteStationNews(StationID sid)
Remove news regarding given station so there are no &#39;unknown station now accepts Mail&#39; or &#39;First trai...
Definition: news_gui.cpp:916
Empty widget, place holder to reserve space in widget array.
Definition: widget_type.h:46
start scrolling news
Definition: statusbar_gui.h:17
Definition of stuff that is very close to a company, like the company struct itself.
static const int top_spacing
Additional spacing at the top of the WID_MH_BACKGROUND widget.
Definition: news_gui.cpp:1112
static const uint8 PC_WHITE
White palette colour.
Definition: gfx_func.h:206
StringID GetEngineInfoString(EngineID engine)
Get a multi-line string with some technical data, describing the engine.
Definition: engine_gui.cpp:266
StringID GetEngineCategoryName(EngineID engine)
Return the category of an engine.
Definition: engine_gui.cpp:38
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:928
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen...
Definition: gfx.cpp:110
Show newspaper.
Definition: news_type.h:91
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3215
Reference station. Scroll to station when clicking on the news. Delete news when station is deleted...
Definition: news_type.h:53
Year coloured_news_year
when does newspaper become coloured?
High level window description.
Definition: window_gui.h:166
end-of-array marker
Definition: news_type.h:37
An invalid owner.
Definition: company_type.h:29
uint CountElapsed(uint delta)
Count how many times the interval has elapsed.
Definition: guitimer_func.h:40
void SetWindowTop(int newtop)
Moves the window to a new top coordinate.
Definition: news_gui.cpp:564
Scrollbar for the list.
Definition: news_widget.h:41
static uint MIN_NEWS_AMOUNT
preferred minimum amount of news messages
Definition: news_gui.cpp:46
Types related to the news widgets.
void DeleteInvalidEngineNews()
Remove engine announcements for invalid engines.
Definition: news_gui.cpp:949
Train vehicle type.
Definition: vehicle_type.h:24
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
NewsReferenceType reftype2
Type of ref2.
Definition: news_type.h:128
Functions related to dates.
const char *const name
Name.
Definition: news_type.h:98
Centered label.
Definition: widget_type.h:55
static const Year ORIGINAL_MAX_YEAR
The maximum year of the original TTD.
Definition: date_type.h:53
Scrollbar data structure.
Definition: widget_type.h:587
void OnPaint() override
The window must be repainted.
Definition: news_gui.cpp:1144
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:62
Inset around the viewport in the window. Only used in small news items.
Definition: news_widget.h:23
Horizontal container.
Definition: widget_type.h:73
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1130
The passed event is not handled.
Definition: window_type.h:713
static int duration
Remaining time for showing the current news message (may only be access while a news item is displaye...
Definition: news_gui.cpp:266
Bit value of the &#39;no transparency&#39; flag.
Definition: widget_type.h:268
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: news_gui.cpp:419
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
Ship vehicle type.
Definition: vehicle_type.h:26
First bit for window layout.
Definition: news_type.h:67
Functions related to vehicles.
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:66
Pressed (inset) panel, most commonly used as combo box text area.
Definition: widget_type.h:49
Vehicle data structure.
Definition: vehicle_base.h:210
static NewsTypeData _news_type_data[]
Per-NewsType data.
Definition: news_gui.cpp:227
void CopyInDParam(int offs, const uint64 *src, int num)
Copy num string parameters from array src into the global string parameter array. ...
Definition: strings.cpp:138
void DrawCompanyManagerFace(CompanyManagerFace cmf, int colour, int x, int y)
Draws the face of a company manager&#39;s face.
int64 ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:755
Close box (at top-left of a window)
Definition: widget_type.h:67
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: news_gui.cpp:340
uint64 params[10]
Parameters for string resolving.
Definition: news_type.h:139
Bit value of the &#39;shade to grey&#39; flag.
Definition: widget_type.h:269
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
Data that needs to be stored for company news messages.
Definition: news_type.h:148
void InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:1925
static void ShowNewsMessage(const NewsItem *ni)
Do a forced show of a specific message.
Definition: news_gui.cpp:1009
static void MoveToNextNewsItem()
Move to the next news item.
Definition: news_gui.cpp:702
Common return value for all commands.
Definition: command_type.h:23
NewsReferenceType reftype1
Type of ref1.
Definition: news_type.h:127
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:24
byte news_message_timeout
how much longer than the news message "age" should we keep the message in the history ...
Nested widget to display a viewport in a window.
Definition: widget_type.h:573
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Per-NewsType data.
Definition: news_type.h:97
Functions related to the vehicle&#39;s GUIs.
Viewport in the window.
Definition: news_widget.h:24
Bit value for specifying that string param 0 contains a vehicle ID. (special autoreplace behaviour) ...
Definition: news_type.h:74
static NewsItem * _oldest_news
head of news items queue
Definition: news_gui.cpp:48
void * free_data
Data to be freed when the news item has reached its end.
Definition: news_type.h:132
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:668
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: news_gui.cpp:1128
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:908
Functions related to (drawing on) viewports.
bool news_full
Play sound effects associated to certain news types.
void ShowCompanyGroupForVehicle(const Vehicle *v)
Show the group window for the given vehicle.
Definition: group_gui.cpp:1071
Data structure for an opened window.
Definition: window_gui.h:276
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
Panel of the window.
Definition: news_widget.h:17
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: news_gui.cpp:536
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1044
SaveLoad save
Internal structure (going to savegame, parts to config)
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
Date of the news item.
Definition: news_widget.h:21
static bool ReadyForNextNewsItem()
Are we ready to show another news item? Only if no newspaper is displayed.
Definition: news_gui.cpp:661
Date date
Date of the news.
Definition: news_type.h:123
Functions related to low-level strings.
Only show a reminder in the status bar.
Definition: news_type.h:89
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: news_gui.cpp:1150
Invisible widget that takes some space.
Definition: widget_type.h:77
StringID string_id
Message text.
Definition: news_type.h:122
VarType conv
type of the variable to be saved, int
Definition: saveload.h:499
GUI Timers.
uint32 ref1
Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news...
Definition: news_type.h:129
Window class displaying a news item.
Definition: news_gui.cpp:262
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:63
int GetScrolledRowFromWidget(int clickpos, const Window *const w, int widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition: widget.cpp:1957
int date_width
< Height of a single line in the news history window including spacing.
Definition: news_gui.cpp:1116
int PositionNewsMessage(Window *w)
(Re)position news message window at the screen.
Definition: window.cpp:3524
SoundSettings sound
sound effect settings
Chatbox; Window numbers:
Definition: window_type.h:491
Show vehicle&#39;s group.
Definition: news_widget.h:34
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
void ShowMessageHistory()
Display window with news messages history.
Definition: news_gui.cpp:1242
DoCommandFlag
List of flags for a command.
Definition: command_type.h:342
static TileIndex GetReferenceTile(NewsReferenceType reftype, uint32 ref)
Get the position a news-reference is referencing.
Definition: news_gui.cpp:69
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:176
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1012
abort current news display (active news were deleted)
Definition: statusbar_gui.h:19
NewsDisplay
News display options.
Definition: news_type.h:88
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
Apply a recolour sprite to the screen content.
Definition: gfx_type.h:284
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:945
static bool ReadyForNextTickerItem()
Are we ready to show another ticker item? Only if nothing is in the newsticker is displayed...
Definition: news_gui.cpp:646
Definition of base types and functions in a cross-platform compatible way.
Center both horizontally and vertically.
Definition: gfx_func.h:104
PaletteID GetEnginePalette(EngineID engine_type, CompanyID company)
Get the colour map for an engine.
Definition: vehicle.cpp:1972
Bit value for disabling transparency.
Definition: news_type.h:72
char president_name[64]
The name of the president.
Definition: news_type.h:150
NewsFlag
Various OR-able news-item flags.
Definition: news_type.h:63
Reference tile. Scroll to tile when clicking on the news.
Definition: news_type.h:51
A number of safeguards to prevent using unsafe methods.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:245
Name of the new vehicle.
Definition: news_widget.h:31
Title of the company news.
Definition: news_widget.h:18
void CopyOutDParam(uint64 *dst, int offs, int num)
Copy num string parameters from the global string parameter array to the dst array.
Definition: strings.cpp:149
Vehicle drawn in preview window, news, ...
Definition: vehicle_type.h:91
Geometry functions.
Simple depressed panel.
Definition: widget_type.h:48
show a reminder (dot on the right side of the statusbar)
Definition: statusbar_gui.h:18
static void ShowNewspaper(const NewsItem *ni)
Open up an own newspaper window for the news item.
Definition: news_gui.cpp:607
Engine GUI functions, used by build_vehicle_gui and autoreplace_gui
const SettingDesc * GetSettingFromName(const char *name, uint *i)
Given a name of setting, return a setting description of it.
Definition: settings.cpp:2057
Empty reference.
Definition: news_type.h:50
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
static const PaletteID PALETTE_NEWSPAPER
Recolour sprite for newspaper-greying.
Definition: sprites.h:1586
Normal news item. (Newspaper with text only)
Definition: news_type.h:78
NewsItem * prev
Previous news item.
Definition: news_type.h:120
void ShowLastNewsMessage()
Show previous news item.
Definition: news_gui.cpp:1026
Title bar of the window. Only used in small news items.
Definition: news_widget.h:22
void DeleteIndustryNews(IndustryID iid)
Remove news regarding given industry.
Definition: news_gui.cpp:933
uint32 ref2
Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news ...
Definition: news_type.h:130
uint16 status_height
Height of the status bar window.
Definition: news_gui.cpp:264
Bit value of the &#39;dimmed colours&#39; flag.
Definition: widget_type.h:270
const NewsItem * ni
News item to display.
Definition: news_gui.cpp:265
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1112
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:63
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: news_gui.cpp:1185
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:498
void ShowVehicleViewWindow(const Vehicle *v)
Shows the vehicle view window of the given vehicle.
Bit value for coloured news.
Definition: news_type.h:71
Offset of the caption text at the left.
Definition: window_gui.h:125
static const int bottom_spacing
Additional spacing at the bottom of the WID_MH_BACKGROUND widget.
Definition: news_gui.cpp:1113
void SetDirtyBlocks(int left, int top, int right, int bottom)
This function extends the internal _invalid_rect rectangle as it now contains the rectangle defined b...
Definition: gfx.cpp:1419
Close the window.
Definition: news_widget.h:20
void InitNewsItemStructs()
Initialize the news-items data structures.
Definition: news_gui.cpp:625
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
Graphical display of the new vehicle.
Definition: news_widget.h:32
Message in company news items.
Definition: news_widget.h:25
Functions related to sound.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
EventState OnKeyPress(WChar key, uint16 keycode) override
A key has been pressed.
Definition: news_gui.cpp:521
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: news_gui.cpp:476
static const uint8 PC_BLACK
Black palette colour.
Definition: gfx_func.h:203
void DeleteVehicleNews(VehicleID vid, StringID news)
Delete a news item type about a vehicle.
Definition: news_gui.cpp:897
NewsDisplay GetDisplay() const
Return the news display option.
Definition: news_gui.cpp:252
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
Add a new newsitem to be shown.
Definition: news_gui.cpp:745
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
Compute the initial position of the window.
Definition: news_gui.cpp:334
Name of the manager.
Definition: news_widget.h:28
void ShowExtraViewPortWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Offset of the caption text at the top.
Definition: window_gui.h:127
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:700
News history list; Window numbers:
Definition: window_type.h:265
void OnRealtimeTick(uint delta_ms) override
Called periodically.
Definition: news_gui.cpp:545
Month _cur_month
Current month (0..11)
Definition: date.cpp:25
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition: gfx.cpp:587
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1162
Number of bits for window layout.
Definition: news_type.h:68
static const uint8 PC_GREY
Grey palette colour.
Definition: gfx_func.h:205
execute the given command
Definition: command_type.h:344
The news headline.
Definition: news_widget.h:19
static void * GetVariableAddress(const void *object, const SaveLoad *sld)
Get the address of the variable.
Definition: saveload.h:823
uint32 face
The face of the president.
Definition: news_type.h:153
Functions related to companies.
static void DeleteNewsItem(NewsItem *ni)
Delete a news item from the queue.
Definition: news_gui.cpp:848
Show ticker.
Definition: news_type.h:90
bool ScrollMainWindowTo(int x, int y, int z, bool instant)
Scrolls the main window to given coordinates.
bool IsBus() const
Check whether a roadvehicle is a bus.
Definition: roadveh_cmd.cpp:79
Space for displaying the message. Only used in small news items.
Definition: news_widget.h:26
Base class for engines.
Functionality related to the company manager&#39;s face.
GUISettings gui
settings related to the GUI
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
NewsItem * next
Next news item.
Definition: news_type.h:121
Reference town. Scroll to town when clicking on the news.
Definition: news_type.h:55
Base class for all vehicles.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
Offset of the caption text at the bottom.
Definition: window_gui.h:128
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: news_gui.cpp:1191
CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new custom news item.
Definition: news_gui.cpp:795
Vertical container.
Definition: widget_type.h:75
static WindowDesc * _news_window_layout[]
Window layouts for news items.
Definition: news_gui.cpp:209
TileIndex xy
Base tile of the station.
const SoundFx sound
Sound.
Definition: news_type.h:100
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:997
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static const NewsItem * _forced_news
Forced news item.
Definition: news_gui.cpp:57
Functions and types used internally for the settings configurations.
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
Dark background of new vehicle news.
Definition: news_widget.h:30
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
Definition: window.cpp:1146
NewsFlag flags
NewsFlags bits.
Definition: news_type.h:125
int32 z_pos
z coordinate.
Definition: vehicle_base.h:268
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2396
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
Functions/definitions that have something to do with groups.
Functions related to commands.
static void MoveToNextTickerItem()
Move to the next ticker item.
Definition: news_gui.cpp:671
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
Coordinates of a point in 2D.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:770
Vehicle new title.
Definition: news_widget.h:29
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:620
bool IsNewsTickerShown()
Checks whether the news ticker is currently being used.
Window does not do autoscroll,.
Definition: window_gui.h:239
Reference engine.
Definition: news_type.h:56
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
NewsReferenceType
References to objects in news.
Definition: news_type.h:49
size_t Utf8Encode(char *buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:486
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
Draw a caption bar.
Definition: widget.cpp:538
Base of all industries.
Aircraft vehicle type.
Definition: vehicle_type.h:27
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:61
static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
Draw an unformatted news message truncated to a maximum length.
Definition: news_gui.cpp:1076
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
uint16 chat_height
Height of the chat window.
Definition: news_gui.cpp:263
int32 x_pos
x coordinate.
Definition: vehicle_base.h:266
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:981
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
Base of the town class.
Reference industry. Scroll to industry when clicking on the news. Delete news when industry is delete...
Definition: news_type.h:54
static uint _total_news
current number of news items
Definition: news_gui.cpp:47
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: news_gui.cpp:414
NewsType type
Type of the news.
Definition: news_type.h:124
void SetCapacityFromWidget(Window *w, int widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget. ...
Definition: widget.cpp:1971
Specification of a rectangle with absolute coordinates of all edges.
Vertical scrollbar.
Definition: widget_type.h:82
The passed event is handled.
Definition: window_type.h:712
int32 y_pos
y coordinate.
Definition: vehicle_base.h:267
Text is written right-to-left by default.
Definition: strings_type.h:24
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:147
Window functions not directly related to making/drawing windows.
Find a place automatically.
Definition: window_gui.h:154
Manually align the window (so no automatic location finding)
Definition: window_gui.h:153
NWidgetDisplay disp_flags
Flags that affect display and interaction with the widget.
Definition: widget_type.h:300
GUI functions that shouldn&#39;t be here.
Some technical data of the new vehicle.
Definition: news_widget.h:33
NewsType
Type of news.
Definition: news_type.h:21
Base classes/functions for stations.
Functions, definitions and such used only by the GUI.
static Station * Get(size_t index)
Gets station with given index.
Date _date
Current date in days (day counter)
Definition: date.cpp:26
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
bool news_ticker
Play a ticker sound when a news item is published.
const byte age
Maximum age of news items (in days)
Definition: news_type.h:99
static void ShowTicker(const NewsItem *ni)
Show news item in the ticker.
Definition: news_gui.cpp:616
static NWidgetPart SetScrollbar(int index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1093
Reference vehicle. Scroll to vehicle when clicking on the news. Delete news when vehicle is deleted...
Definition: news_type.h:52
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:44
The object is owned by a superuser / goal script.
Definition: company_type.h:27
Dimensions (a width and height) of a rectangle in 2D.
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:60
Bit value for enabling shading.
Definition: news_type.h:73
SoundFx
Sound effects from baseset.
Definition: sound_type.h:37
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62
Road vehicle type.
Definition: vehicle_type.h:25
Default zoom level for the news messages.
Definition: zoom_type.h:34
Scrollbar * vscroll
< Width needed for the date part.
Definition: news_gui.cpp:1118
Offset of the caption text at the right.
Definition: window_gui.h:126
Information about a single item of news.
Definition: news_type.h:119
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3316
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:320
static const NewsItem * _current_news
Current news item (last item shown regularly).
Definition: news_gui.cpp:60
News window; Window numbers:
Definition: window_type.h:241
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:621
(Toggle) Button with text
Definition: widget_type.h:53
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:629
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
Base class for a &#39;real&#39; widget.
Definition: widget_type.h:282
byte colour
The colour related to the company.
Definition: news_type.h:154