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/>.
10 /** @file news_gui.cpp GUI functions related to news messages. */
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"
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"
37 #include "widgets/news_widget.h"
39 #include "table/strings.h"
41 const NewsItem
*_statusbar_news_item
= NULL
;
43 static uint MIN_NEWS_AMOUNT
= 30; ///< preferred minimum amount of news messages
44 static uint _total_news
= 0; ///< current number of news items
45 static NewsItem
*_oldest_news
= NULL
; ///< head of news items queue
46 static NewsItem
*_latest_news
= NULL
; ///< tail of news items queue
50 * Users can force an item by accessing the history or "last message".
51 * If the message being shown was forced by the user, a pointer is stored
52 * in _forced_news. Otherwise, \a _forced_news variable is NULL.
54 static const NewsItem
*_forced_news
= NULL
; ///< item the user has asked for
56 /** Current news item (last item shown regularly). */
57 static const NewsItem
*_current_news
= NULL
;
61 * Get the position a news-reference is referencing.
62 * @param reftype The type of reference.
63 * @param ref The reference.
64 * @return A tile for the referenced object, or INVALID_TILE if none.
66 static TileIndex
GetReferenceTile(NewsReferenceType reftype
, uint32 ref
)
69 case NR_TILE
: return (TileIndex
)ref
;
70 case NR_STATION
: return Station::Get((StationID
)ref
)->xy
;
71 case NR_INDUSTRY
: return Industry::Get((IndustryID
)ref
)->location
.tile
+ TileDiffXY(1, 1);
72 case NR_TOWN
: return Town::Get((TownID
)ref
)->xy
;
73 default: return INVALID_TILE
;
77 /* Normal news items. */
78 static const NWidgetPart _nested_normal_news_widgets
[] = {
79 NWidget(WWT_PANEL
, COLOUR_WHITE
, WID_N_PANEL
),
80 NWidget(NWID_HORIZONTAL
), SetPadding(1, 1, 0, 1),
81 NWidget(WWT_TEXT
, COLOUR_WHITE
, WID_N_CLOSEBOX
), SetDataTip(STR_SILVER_CROSS
, STR_NULL
), SetPadding(0, 0, 0, 1),
82 NWidget(NWID_SPACER
), SetFill(1, 0),
83 NWidget(NWID_VERTICAL
),
84 NWidget(WWT_LABEL
, COLOUR_WHITE
, WID_N_DATE
), SetDataTip(STR_DATE_LONG_SMALL
, STR_NULL
),
85 NWidget(NWID_SPACER
), SetFill(0, 1),
88 NWidget(WWT_EMPTY
, COLOUR_WHITE
, WID_N_MESSAGE
), SetMinimalSize(428, 154), SetPadding(0, 5, 1, 5),
92 static WindowDesc
_normal_news_desc(
93 WDP_MANUAL
, NULL
, 0, 0,
94 WC_NEWS_WINDOW
, WC_NONE
,
96 _nested_normal_news_widgets
, lengthof(_nested_normal_news_widgets
)
99 /* New vehicles news items. */
100 static const NWidgetPart _nested_vehicle_news_widgets
[] = {
101 NWidget(WWT_PANEL
, COLOUR_WHITE
, WID_N_PANEL
),
102 NWidget(NWID_HORIZONTAL
), SetPadding(1, 1, 0, 1),
103 NWidget(NWID_VERTICAL
),
104 NWidget(WWT_TEXT
, COLOUR_WHITE
, WID_N_CLOSEBOX
), SetDataTip(STR_SILVER_CROSS
, STR_NULL
), SetPadding(0, 0, 0, 1),
105 NWidget(NWID_SPACER
), SetFill(0, 1),
107 NWidget(WWT_LABEL
, COLOUR_WHITE
, WID_N_VEH_TITLE
), SetFill(1, 1), SetMinimalSize(419, 55), SetDataTip(STR_EMPTY
, STR_NULL
),
109 NWidget(WWT_PANEL
, COLOUR_WHITE
, WID_N_VEH_BKGND
), SetPadding(0, 25, 1, 25),
110 NWidget(NWID_VERTICAL
),
111 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_N_VEH_NAME
), SetMinimalSize(369, 33), SetFill(1, 0),
112 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_N_VEH_SPR
), SetMinimalSize(369, 32), SetFill(1, 0),
113 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_N_VEH_INFO
), SetMinimalSize(369, 46), SetFill(1, 0),
119 static WindowDesc
_vehicle_news_desc(
120 WDP_MANUAL
, NULL
, 0, 0,
121 WC_NEWS_WINDOW
, WC_NONE
,
123 _nested_vehicle_news_widgets
, lengthof(_nested_vehicle_news_widgets
)
126 /* Company news items. */
127 static const NWidgetPart _nested_company_news_widgets
[] = {
128 NWidget(WWT_PANEL
, COLOUR_WHITE
, WID_N_PANEL
),
129 NWidget(NWID_HORIZONTAL
), SetPadding(1, 1, 0, 1),
130 NWidget(NWID_VERTICAL
),
131 NWidget(WWT_TEXT
, COLOUR_WHITE
, WID_N_CLOSEBOX
), SetDataTip(STR_SILVER_CROSS
, STR_NULL
), SetPadding(0, 0, 0, 1),
132 NWidget(NWID_SPACER
), SetFill(0, 1),
134 NWidget(WWT_LABEL
, COLOUR_WHITE
, WID_N_TITLE
), SetFill(1, 1), SetMinimalSize(410, 20), SetDataTip(STR_EMPTY
, STR_NULL
),
136 NWidget(NWID_HORIZONTAL
), SetPadding(0, 1, 1, 1),
137 NWidget(NWID_VERTICAL
),
138 NWidget(WWT_EMPTY
, COLOUR_WHITE
, WID_N_MGR_FACE
), SetMinimalSize(93, 119), SetPadding(2, 6, 2, 1),
139 NWidget(NWID_HORIZONTAL
),
140 NWidget(WWT_EMPTY
, COLOUR_WHITE
, WID_N_MGR_NAME
), SetMinimalSize(93, 24), SetPadding(0, 0, 0, 1),
141 NWidget(NWID_SPACER
), SetFill(1, 0),
143 NWidget(NWID_SPACER
), SetFill(0, 1),
145 NWidget(WWT_EMPTY
, COLOUR_WHITE
, WID_N_COMPANY_MSG
), SetFill(1, 1), SetMinimalSize(328, 150),
150 static WindowDesc
_company_news_desc(
151 WDP_MANUAL
, NULL
, 0, 0,
152 WC_NEWS_WINDOW
, WC_NONE
,
154 _nested_company_news_widgets
, lengthof(_nested_company_news_widgets
)
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_TEXT
, COLOUR_WHITE
, WID_N_CLOSEBOX
), SetDataTip(STR_SILVER_CROSS
, STR_NULL
), SetPadding(0, 0, 0, 1),
162 NWidget(NWID_SPACER
), SetFill(1, 0),
163 NWidget(NWID_VERTICAL
),
164 NWidget(WWT_LABEL
, COLOUR_WHITE
, WID_N_DATE
), SetDataTip(STR_DATE_LONG_SMALL
, STR_NULL
),
165 NWidget(NWID_SPACER
), SetFill(0, 1),
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),
173 static WindowDesc
_thin_news_desc(
174 WDP_MANUAL
, NULL
, 0, 0,
175 WC_NEWS_WINDOW
, WC_NONE
,
177 _nested_thin_news_widgets
, lengthof(_nested_thin_news_widgets
)
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. */
183 NWidget(NWID_HORIZONTAL
),
184 NWidget(WWT_CLOSEBOX
, COLOUR_LIGHT_BLUE
, WID_N_CLOSEBOX
),
185 NWidget(WWT_EMPTY
, COLOUR_LIGHT_BLUE
, WID_N_CAPTION
), SetFill(1, 0),
189 NWidget(WWT_PANEL
, COLOUR_LIGHT_BLUE
, WID_N_HEADLINE
),
190 NWidget(WWT_INSET
, COLOUR_LIGHT_BLUE
, WID_N_INSET
), SetPadding(2, 2, 2, 2),
191 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_N_VIEWPORT
), SetPadding(1, 1, 1, 1), SetMinimalSize(274, 47), SetFill(1, 0),
193 NWidget(WWT_EMPTY
, COLOUR_WHITE
, WID_N_MESSAGE
), SetMinimalSize(275, 20), SetFill(1, 0), SetPadding(0, 5, 0, 5),
197 static WindowDesc
_small_news_desc(
198 WDP_MANUAL
, NULL
, 0, 0,
199 WC_NEWS_WINDOW
, WC_NONE
,
201 _nested_small_news_widgets
, lengthof(_nested_small_news_widgets
)
205 * Window layouts for news items.
207 static WindowDesc
* _news_window_layout
[] = {
208 &_thin_news_desc
, ///< NF_THIN
209 &_small_news_desc
, ///< NF_SMALL
210 &_normal_news_desc
, ///< NF_NORMAL
211 &_vehicle_news_desc
, ///< NF_VEHICLE
212 &_company_news_desc
, ///< NF_COMPANY
215 WindowDesc
* GetNewsWindowLayout(NewsFlag flags
)
217 uint layout
= GB(flags
, NFB_WINDOW_LAYOUT
, NFB_WINDOW_LAYOUT_COUNT
);
218 assert(layout
< lengthof(_news_window_layout
));
219 return _news_window_layout
[layout
];
225 static NewsTypeData _news_type_data
[] = {
226 /* name, age, sound, */
227 NewsTypeData("news_display.arrival_player", 60, SND_1D_APPLAUSE
), ///< NT_ARRIVAL_COMPANY
228 NewsTypeData("news_display.arrival_other", 60, SND_1D_APPLAUSE
), ///< NT_ARRIVAL_OTHER
229 NewsTypeData("news_display.accident", 90, SND_BEGIN
), ///< NT_ACCIDENT
230 NewsTypeData("news_display.company_info", 60, SND_BEGIN
), ///< NT_COMPANY_INFO
231 NewsTypeData("news_display.open", 90, SND_BEGIN
), ///< NT_INDUSTRY_OPEN
232 NewsTypeData("news_display.close", 90, SND_BEGIN
), ///< NT_INDUSTRY_CLOSE
233 NewsTypeData("news_display.economy", 30, SND_BEGIN
), ///< NT_ECONOMY
234 NewsTypeData("news_display.production_player", 30, SND_BEGIN
), ///< NT_INDUSTRY_COMPANY
235 NewsTypeData("news_display.production_other", 30, SND_BEGIN
), ///< NT_INDUSTRY_OTHER
236 NewsTypeData("news_display.production_nobody", 30, SND_BEGIN
), ///< NT_INDUSTRY_NOBODY
237 NewsTypeData("news_display.advice", 150, SND_BEGIN
), ///< NT_ADVICE
238 NewsTypeData("news_display.new_vehicles", 30, SND_1E_OOOOH
), ///< NT_NEW_VEHICLES
239 NewsTypeData("news_display.acceptance", 90, SND_BEGIN
), ///< NT_ACCEPTANCE
240 NewsTypeData("news_display.subsidies", 180, SND_BEGIN
), ///< NT_SUBSIDIES
241 NewsTypeData("news_display.general", 60, SND_BEGIN
), ///< NT_GENERAL
244 assert_compile(lengthof(_news_type_data
) == NT_END
);
247 * Return the news display option.
248 * @return display options
250 NewsDisplay
NewsTypeData::GetDisplay() const
253 const SettingDesc
*sd
= GetSettingFromName(this->name
, &index
);
255 void *ptr
= GetVariableAddress(&sd
->save
);
256 return (NewsDisplay
)ReadValue(ptr
, sd
->save
.conv
);
259 /** Window class displaying a news item. */
260 struct NewsWindow
: Window
{
261 uint16 chat_height
; ///< Height of the chat window.
262 uint16 status_height
; ///< Height of the status bar window
263 const NewsItem
*ni
; ///< News item to display.
264 static uint duration
; ///< Remaining time for showing current news message (may only be accessed while a news item is displayed).
266 NewsWindow(WindowDesc
*desc
, const NewsItem
*ni
) : Window(desc
), ni(ni
)
268 NewsWindow::duration
= 555;
269 const Window
*w
= FindWindowByClass(WC_SEND_NETWORK_MSG
);
270 this->chat_height
= (w
!= NULL
) ? w
->height
: 0;
271 this->status_height
= FindWindowById(WC_STATUS_BAR
, 0)->height
;
273 this->flags
|= WF_DISABLE_VP_SCROLL
;
275 this->CreateNestedTree();
277 /* For company news with a face we have a separate headline in param[0] */
278 if (desc
== &_company_news_desc
) this->GetWidget
<NWidgetCore
>(WID_N_TITLE
)->widget_data
= this->ni
->params
[0];
280 this->FinishInitNested(0);
282 /* Initialize viewport if it exists. */
283 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_N_VIEWPORT
);
285 nvp
->InitializeViewport(this, ni
->reftype1
== NR_VEHICLE
? 0x80000000 | ni
->ref1
: GetReferenceTile(ni
->reftype1
, ni
->ref1
), ZOOM_LVL_NEWS
);
286 if (this->ni
->flags
& NF_NO_TRANSPARENT
) nvp
->disp_flags
|= ND_NO_TRANSPARENCY
;
287 if ((this->ni
->flags
& NF_INCOLOUR
) == 0) {
288 nvp
->disp_flags
|= ND_SHADE_GREY
;
289 } else if (this->ni
->flags
& NF_SHADE
) {
290 nvp
->disp_flags
|= ND_SHADE_DIMMED
;
294 PositionNewsMessage(this);
297 void DrawNewsBorder(const Rect
&r
) const
299 GfxFillRect(r
.left
, r
.top
, r
.right
, r
.bottom
, PC_WHITE
);
301 GfxFillRect(r
.left
, r
.top
, r
.left
, r
.bottom
, PC_BLACK
);
302 GfxFillRect(r
.right
, r
.top
, r
.right
, r
.bottom
, PC_BLACK
);
303 GfxFillRect(r
.left
, r
.top
, r
.right
, r
.top
, PC_BLACK
);
304 GfxFillRect(r
.left
, r
.bottom
, r
.right
, r
.bottom
, PC_BLACK
);
307 virtual Point
OnInitialPosition(int16 sm_width
, int16 sm_height
, int window_number
)
309 Point pt
= { 0, _screen
.height
};
313 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
315 StringID str
= STR_NULL
;
318 CopyInDParam(0, this->ni
->params
, lengthof(this->ni
->params
));
319 str
= this->ni
->string_id
;
322 case WID_N_COMPANY_MSG
:
323 str
= this->GetCompanyMessageString();
327 case WID_N_VEH_TITLE
:
328 str
= this->GetNewVehicleMessageString(widget
);
331 case WID_N_VEH_INFO
: {
332 assert(this->ni
->reftype1
== NR_ENGINE
);
333 EngineID engine
= this->ni
->ref1
;
334 str
= GetEngineInfoString(engine
);
338 return; // Do nothing
341 /* Update minimal size with length of the multi-line string. */
343 d
.width
= (d
.width
>= padding
.width
) ? d
.width
- padding
.width
: 0;
344 d
.height
= (d
.height
>= padding
.height
) ? d
.height
- padding
.height
: 0;
345 d
= GetStringMultiLineBoundingBox(str
, d
);
346 d
.width
+= padding
.width
;
347 d
.height
+= padding
.height
;
348 *size
= maxdim(*size
, d
);
351 virtual void SetStringParameters(int widget
) const
353 if (widget
== WID_N_DATE
) SetDParam(0, this->ni
->date
);
356 virtual void DrawWidget(const Rect
&r
, int widget
) const
360 DrawCaption(r
, COLOUR_LIGHT_BLUE
, this->owner
, STR_NEWS_MESSAGE_CAPTION
);
364 this->DrawNewsBorder(r
);
368 CopyInDParam(0, this->ni
->params
, lengthof(this->ni
->params
));
369 DrawStringMultiLine(r
.left
, r
.right
, r
.top
, r
.bottom
, this->ni
->string_id
, TC_FROMSTRING
, SA_CENTER
);
372 case WID_N_MGR_FACE
: {
373 const CompanyNewsInformation
*cni
= (const CompanyNewsInformation
*)this->ni
->free_data
;
374 DrawCompanyManagerFace(cni
->face
, cni
->colour
, r
.left
, r
.top
);
375 GfxFillRect(r
.left
+ 1, r
.top
, r
.left
+ 1 + 91, r
.top
+ 118, PALETTE_NEWSPAPER
, FILLRECT_RECOLOUR
);
378 case WID_N_MGR_NAME
: {
379 const CompanyNewsInformation
*cni
= (const CompanyNewsInformation
*)this->ni
->free_data
;
380 SetDParamStr(0, cni
->president_name
);
381 DrawStringMultiLine(r
.left
, r
.right
, r
.top
, r
.bottom
, STR_JUST_RAW_STRING
, TC_FROMSTRING
, SA_CENTER
);
384 case WID_N_COMPANY_MSG
:
385 DrawStringMultiLine(r
.left
, r
.right
, r
.top
, r
.bottom
, this->GetCompanyMessageString(), TC_FROMSTRING
, SA_CENTER
);
388 case WID_N_VEH_BKGND
:
389 GfxFillRect(r
.left
, r
.top
, r
.right
, r
.bottom
, PC_GREY
);
393 case WID_N_VEH_TITLE
:
394 DrawStringMultiLine(r
.left
, r
.right
, r
.top
, r
.bottom
, this->GetNewVehicleMessageString(widget
), TC_FROMSTRING
, SA_CENTER
);
397 case WID_N_VEH_SPR
: {
398 assert(this->ni
->reftype1
== NR_ENGINE
);
399 EngineID engine
= this->ni
->ref1
;
400 DrawVehicleEngine(r
.left
, r
.right
, (r
.left
+ r
.right
) / 2, (r
.top
+ r
.bottom
) / 2, engine
, GetEnginePalette(engine
, _local_company
), EIT_PREVIEW
);
401 GfxFillRect(r
.left
, r
.top
, r
.right
, r
.bottom
, PALETTE_NEWSPAPER
, FILLRECT_RECOLOUR
);
404 case WID_N_VEH_INFO
: {
405 assert(this->ni
->reftype1
== NR_ENGINE
);
406 EngineID engine
= this->ni
->ref1
;
407 DrawStringMultiLine(r
.left
, r
.right
, r
.top
, r
.bottom
, GetEngineInfoString(engine
), TC_FROMSTRING
, SA_CENTER
);
413 virtual void OnClick(Point pt
, int widget
, int click_count
)
417 NewsWindow::duration
= 0;
423 if (this->ni
->reftype1
== NR_VEHICLE
) {
424 const Vehicle
*v
= Vehicle::Get(this->ni
->ref1
);
425 ShowVehicleViewWindow(v
);
430 break; // Ignore clicks
433 if (this->ni
->reftype1
== NR_VEHICLE
) {
434 const Vehicle
*v
= Vehicle::Get(this->ni
->ref1
);
435 ScrollMainWindowTo(v
->x_pos
, v
->y_pos
, v
->z_pos
);
437 TileIndex tile1
= GetReferenceTile(this->ni
->reftype1
, this->ni
->ref1
);
438 TileIndex tile2
= GetReferenceTile(this->ni
->reftype2
, this->ni
->ref2
);
440 if (tile1
!= INVALID_TILE
) ShowExtraViewPortWindow(tile1
);
441 if (tile2
!= INVALID_TILE
) ShowExtraViewPortWindow(tile2
);
443 if ((tile1
== INVALID_TILE
|| !ScrollMainWindowToTile(tile1
)) && tile2
!= INVALID_TILE
) {
444 ScrollMainWindowToTile(tile2
);
452 virtual EventState
OnKeyPress(WChar key
, uint16 keycode
)
454 if (keycode
== WKC_SPACE
) {
455 /* Don't continue. */
459 return ES_NOT_HANDLED
;
463 * Some data on this window has become invalid.
464 * @param data Information about the changed data.
465 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
467 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
469 if (!gui_scope
) return;
470 /* The chatbar has notified us that is was either created or closed */
471 int newtop
= this->top
+ this->chat_height
- data
;
472 this->chat_height
= data
;
473 this->SetWindowTop(newtop
);
476 virtual void OnTick()
478 /* Scroll up newsmessages from the bottom in steps of 4 pixels */
479 int newtop
= max(this->top
- 4, _screen
.height
- this->height
- this->status_height
- this->chat_height
);
480 this->SetWindowTop(newtop
);
485 * Moves the window so #newtop is new 'top' coordinate. Makes screen dirty where needed.
486 * @param newtop new top coordinate
488 void SetWindowTop(int newtop
)
490 if (this->top
== newtop
) return;
492 int mintop
= min(newtop
, this->top
);
493 int maxtop
= max(newtop
, this->top
);
494 if (this->viewport
!= NULL
) this->viewport
->top
+= newtop
- this->top
;
497 SetDirtyBlocks(this->left
, mintop
, this->left
+ this->width
, maxtop
+ this->height
);
500 StringID
GetCompanyMessageString() const
502 /* Company news with a face have a separate headline, so the normal message is shifted by two params */
503 CopyInDParam(0, this->ni
->params
+ 2, lengthof(this->ni
->params
) - 2);
504 return this->ni
->params
[1];
507 StringID
GetNewVehicleMessageString(int widget
) const
509 assert(this->ni
->reftype1
== NR_ENGINE
);
510 EngineID engine
= this->ni
->ref1
;
513 case WID_N_VEH_TITLE
:
514 SetDParam(0, GetEngineCategoryName(engine
));
515 return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE
;
518 SetDParam(0, engine
);
519 return STR_NEWS_NEW_VEHICLE_TYPE
;
527 /* static */ uint
NewsWindow::duration
= 0; // Instance creation.
530 /** Open up an own newspaper window for the news item */
531 static void ShowNewspaper(const NewsItem
*ni
)
533 SoundFx sound
= _news_type_data
[ni
->type
].sound
;
534 if (sound
!= 0 && _settings_client
.sound
.news_full
) SndPlayFx(sound
);
536 new NewsWindow(GetNewsWindowLayout(ni
->flags
), ni
);
539 /** Show news item in the ticker */
540 static void ShowTicker(const NewsItem
*ni
)
542 if (_settings_client
.sound
.news_ticker
) SndPlayFx(SND_16_MORSE
);
544 _statusbar_news_item
= ni
;
545 InvalidateWindowData(WC_STATUS_BAR
, 0, SBI_SHOW_TICKER
);
548 /** Initialize the news-items data structures */
549 void InitNewsItemStructs()
551 for (NewsItem
*ni
= _oldest_news
; ni
!= NULL
; ) {
552 NewsItem
*next
= ni
->next
;
561 _current_news
= NULL
;
562 _statusbar_news_item
= NULL
;
563 NewsWindow::duration
= 0;
567 * Are we ready to show another news item?
568 * Only if nothing is in the newsticker and no newspaper is displayed
570 static bool ReadyForNextItem()
572 const NewsItem
*ni
= _forced_news
== NULL
? _current_news
: _forced_news
;
573 if (ni
== NULL
) return true;
576 * Check if the status bar message is still being displayed? */
577 if (IsNewsTickerShown()) return false;
579 /* Newspaper message, decrement duration counter */
580 if (NewsWindow::duration
!= 0) NewsWindow::duration
--;
582 /* neither newsticker nor newspaper are running */
583 return (NewsWindow::duration
== 0 || FindWindowById(WC_NEWS_WINDOW
, 0) == NULL
);
586 /** Move to the next news item */
587 static void MoveToNextItem()
589 InvalidateWindowData(WC_STATUS_BAR
, 0, SBI_NEWS_DELETED
); // invalidate the statusbar
590 DeleteWindowById(WC_NEWS_WINDOW
, 0); // close the newspapers window if shown
592 _statusbar_news_item
= NULL
;
594 /* if we're not at the last item, then move on */
595 if (_current_news
!= _latest_news
) {
596 _current_news
= (_current_news
== NULL
) ? _oldest_news
: _current_news
->next
;
597 const NewsItem
*ni
= _current_news
;
598 const NewsType type
= ni
->type
;
600 /* check the date, don't show too old items */
601 if (_date
- _news_type_data
[type
].age
> ni
->date
) return;
603 switch (_news_type_data
[type
].GetDisplay()) {
604 default: NOT_REACHED();
605 case ND_OFF
: // Off - show nothing only a small reminder in the status bar
606 InvalidateWindowData(WC_STATUS_BAR
, 0, SBI_SHOW_REMINDER
);
609 case ND_SUMMARY
: // Summary - show ticker
613 case ND_FULL
: // Full - show newspaper
621 * Add a new newsitem to be shown.
622 * @param string String to display
623 * @param type news category
624 * @param flags display flags for the news
625 * @param reftype1 Type of ref1
626 * @param ref1 Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleteing the news when the object is deleted.
627 * @param reftype2 Type of ref2
628 * @param ref2 Reference 2 to some object: Used for scrolling after clicking on the news, and for deleteing the news when the object is deleted.
629 * @param free_data Pointer to data that must be freed once the news message is cleared
633 void AddNewsItem(StringID string
, NewsType type
, NewsFlag flags
, NewsReferenceType reftype1
, uint32 ref1
, NewsReferenceType reftype2
, uint32 ref2
, void *free_data
)
635 if (_game_mode
== GM_MENU
) return;
637 /* Create new news item node */
638 NewsItem
*ni
= new NewsItem
;
640 ni
->string_id
= string
;
644 /* show this news message in colour? */
645 if (_cur_year
>= _settings_client
.gui
.coloured_news_year
) ni
->flags
|= NF_INCOLOUR
;
647 ni
->reftype1
= reftype1
;
648 ni
->reftype2
= reftype2
;
651 ni
->free_data
= free_data
;
653 CopyOutDParam(ni
->params
, 0, lengthof(ni
->params
));
655 if (_total_news
++ == 0) {
656 assert(_oldest_news
== NULL
);
660 assert(_latest_news
->next
== NULL
);
661 _latest_news
->next
= ni
;
662 ni
->prev
= _latest_news
;
668 SetWindowDirty(WC_MESSAGE_HISTORY
, 0);
672 * Create a new custom news item.
674 * @param flags type of operation
675 * @param p1 various bitstuffed elements
676 * - p1 = (bit 0 - 7) - NewsType of the message.
677 * - p1 = (bit 8 - 15) - NewsReferenceType of first reference.
678 * - p1 = (bit 16 - 23) - Company this news message is for.
679 * @param p2 First reference of the news message.
680 * @param text The text of the news message.
681 * @return the cost of this operation or an error
683 CommandCost
CmdCustomNewsItem(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
685 if (_current_company
!= OWNER_DEITY
) return CMD_ERROR
;
687 NewsType type
= (NewsType
)GB(p1
, 0, 8);
688 NewsReferenceType reftype1
= (NewsReferenceType
)GB(p1
, 8, 8);
689 CompanyID company
= (CompanyID
)GB(p1
, 16, 8);
691 if (company
!= INVALID_OWNER
&& !Company::IsValidID(company
)) return CMD_ERROR
;
692 if (type
>= NT_END
) return CMD_ERROR
;
693 if (StrEmpty(text
)) return CMD_ERROR
;
698 if (!IsValidTile(p2
)) return CMD_ERROR
;
702 if (!Vehicle::IsValidID(p2
)) return CMD_ERROR
;
706 if (!Station::IsValidID(p2
)) return CMD_ERROR
;
710 if (!Industry::IsValidID(p2
)) return CMD_ERROR
;
714 if (!Town::IsValidID(p2
)) return CMD_ERROR
;
718 if (!Engine::IsValidID(p2
)) return CMD_ERROR
;
721 default: return CMD_ERROR
;
724 if (company
!= INVALID_OWNER
&& company
!= _local_company
) return CommandCost();
726 if (flags
& DC_EXEC
) {
727 char *news
= strdup(text
);
728 SetDParamStr(0, news
);
729 AddNewsItem(STR_NEWS_CUSTOM_ITEM
, type
, NF_NORMAL
, reftype1
, p2
, NR_NONE
, UINT32_MAX
, news
);
732 return CommandCost();
735 /** Delete a news item from the queue */
736 static void DeleteNewsItem(NewsItem
*ni
)
738 /* Delete the news from the news queue. */
739 if (ni
->prev
!= NULL
) {
740 ni
->prev
->next
= ni
->next
;
742 assert(_oldest_news
== ni
);
743 _oldest_news
= ni
->next
;
746 if (ni
->next
!= NULL
) {
747 ni
->next
->prev
= ni
->prev
;
749 assert(_latest_news
== ni
);
750 _latest_news
= ni
->prev
;
755 if (_forced_news
== ni
|| _current_news
== ni
|| _statusbar_news_item
== ni
) {
756 /* When we're the current news, go to the previous item first;
757 * we just possibly made that the last news item. */
758 if (_current_news
== ni
) _current_news
= ni
->prev
;
760 /* About to remove the currently forced item (shown as newspapers) ||
761 * about to remove the currently displayed item (newspapers, ticker, or just a reminder) */
767 SetWindowDirty(WC_MESSAGE_HISTORY
, 0);
771 * Delete a news item type about a vehicle.
772 * When the news item type is INVALID_STRING_ID all news about the vehicle gets deleted.
773 * @param vid The vehicle to remove the news for.
774 * @param news The news type to remove.
776 void DeleteVehicleNews(VehicleID vid
, StringID news
)
778 NewsItem
*ni
= _oldest_news
;
781 NewsItem
*next
= ni
->next
;
782 if (((ni
->reftype1
== NR_VEHICLE
&& ni
->ref1
== vid
) || (ni
->reftype2
== NR_VEHICLE
&& ni
->ref2
== vid
)) &&
783 (news
== INVALID_STRING_ID
|| ni
->string_id
== news
)) {
791 * Remove news regarding given station so there are no 'unknown station now accepts Mail'
792 * or 'First train arrived at unknown station' news items.
793 * @param sid station to remove news about
795 void DeleteStationNews(StationID sid
)
797 NewsItem
*ni
= _oldest_news
;
800 NewsItem
*next
= ni
->next
;
801 if ((ni
->reftype1
== NR_STATION
&& ni
->ref1
== sid
) || (ni
->reftype2
== NR_STATION
&& ni
->ref2
== sid
)) {
809 * Remove news regarding given industry
810 * @param iid industry to remove news about
812 void DeleteIndustryNews(IndustryID iid
)
814 NewsItem
*ni
= _oldest_news
;
817 NewsItem
*next
= ni
->next
;
818 if ((ni
->reftype1
== NR_INDUSTRY
&& ni
->ref1
== iid
) || (ni
->reftype2
== NR_INDUSTRY
&& ni
->ref2
== iid
)) {
826 * Remove engine announcements for invalid engines.
828 void DeleteInvalidEngineNews()
830 NewsItem
*ni
= _oldest_news
;
833 NewsItem
*next
= ni
->next
;
834 if ((ni
->reftype1
== NR_ENGINE
&& (!Engine::IsValidID(ni
->ref1
) || !Engine::Get(ni
->ref1
)->IsEnabled())) ||
835 (ni
->reftype2
== NR_ENGINE
&& (!Engine::IsValidID(ni
->ref2
) || !Engine::Get(ni
->ref2
)->IsEnabled()))) {
842 static void RemoveOldNewsItems()
845 for (NewsItem
*cur
= _oldest_news
; _total_news
> MIN_NEWS_AMOUNT
&& cur
!= NULL
; cur
= next
) {
847 if (_date
- _news_type_data
[cur
->type
].age
* _settings_client
.gui
.news_message_timeout
> cur
->date
) DeleteNewsItem(cur
);
852 * Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.
853 * @note Viewports of currently displayed news is changed via #ChangeVehicleViewports
854 * @param from_index the old vehicle ID
855 * @param to_index the new vehicle ID
857 void ChangeVehicleNews(VehicleID from_index
, VehicleID to_index
)
859 for (NewsItem
*ni
= _oldest_news
; ni
!= NULL
; ni
= ni
->next
) {
860 if (ni
->reftype1
== NR_VEHICLE
&& ni
->ref1
== from_index
) ni
->ref1
= to_index
;
861 if (ni
->reftype2
== NR_VEHICLE
&& ni
->ref2
== from_index
) ni
->ref2
= to_index
;
862 if (ni
->flags
& NF_VEHICLE_PARAM0
&& ni
->params
[0] == from_index
) ni
->params
[0] = to_index
;
868 /* no news item yet */
869 if (_total_news
== 0) return;
871 /* There is no status bar, so no reason to show news;
872 * especially important with the end game screen when
873 * there is no status bar but possible news. */
874 if (FindWindowById(WC_STATUS_BAR
, 0) == NULL
) return;
876 static byte _last_clean_month
= 0;
878 if (_last_clean_month
!= _cur_month
) {
879 RemoveOldNewsItems();
880 _last_clean_month
= _cur_month
;
883 if (ReadyForNextItem()) MoveToNextItem();
886 /** Do a forced show of a specific message */
887 static void ShowNewsMessage(const NewsItem
*ni
)
889 assert(_total_news
!= 0);
891 /* Delete the news window */
892 DeleteWindowById(WC_NEWS_WINDOW
, 0);
894 /* setup forced news item */
897 if (_forced_news
!= NULL
) {
898 DeleteWindowById(WC_NEWS_WINDOW
, 0);
903 /** Show previous news item */
904 void ShowLastNewsMessage()
906 const NewsItem
*ni
= NULL
;
907 if (_total_news
== 0) {
909 } else if (_forced_news
== NULL
) {
910 /* Not forced any news yet, show the current one, unless a news window is
911 * open (which can only be the current one), then show the previous item */
912 if (_current_news
== NULL
) {
913 /* No news were shown yet resp. the last shown one was already deleted.
914 * Threat this as if _forced_news reached _oldest_news; so, wrap around and start anew with the latest. */
917 const Window
*w
= FindWindowById(WC_NEWS_WINDOW
, 0);
918 ni
= (w
== NULL
|| (_current_news
== _oldest_news
)) ? _current_news
: _current_news
->prev
;
920 } else if (_forced_news
== _oldest_news
) {
921 /* We have reached the oldest news, start anew with the latest */
924 /* 'Scrolling' through news history show each one in turn */
925 ni
= _forced_news
->prev
;
929 if (_news_type_data
[ni
->type
].GetDisplay() != ND_OFF
) {
937 /* We have reached the oldest news, start anew with the latest */
946 * Draw an unformatted news message truncated to a maximum length. If
947 * length exceeds maximum length it will be postfixed by '...'
948 * @param left the left most location for the string
949 * @param right the right most location for the string
950 * @param y position of the string
951 * @param colour the colour the string will be shown in
952 * @param *ni NewsItem being printed
953 * @param maxw maximum width of string in pixels
955 static void DrawNewsString(uint left
, uint right
, int y
, TextColour colour
, const NewsItem
*ni
)
957 char buffer
[512], buffer2
[512];
960 CopyInDParam(0, ni
->params
, lengthof(ni
->params
));
963 GetString(buffer
, str
, lastof(buffer
));
964 /* Copy the just gotten string to another buffer to remove any formatting
965 * from it such as big fonts, etc. */
966 const char *ptr
= buffer
;
967 char *dest
= buffer2
;
970 WChar c
= Utf8Consume(&ptr
);
972 /* Make a space from a newline, but ignore multiple newlines */
973 if (c
== '\n' && c_last
!= '\n') {
976 } else if (c
== '\r') {
977 dest
[0] = dest
[1] = dest
[2] = dest
[3] = ' ';
979 } else if (IsPrintable(c
)) {
980 dest
+= Utf8Encode(dest
, c
);
986 /* Truncate and show string; postfixed by '...' if necessary */
987 DrawString(left
, right
, y
, buffer2
, colour
);
990 struct MessageHistoryWindow
: Window
{
991 static const int top_spacing
; ///< Additional spacing at the top of the #WID_MH_BACKGROUND widget.
992 static const int bottom_spacing
; ///< Additional spacing at the bottom of the #WID_MH_BACKGROUND widget.
994 int line_height
; /// < Height of a single line in the news history window including spacing.
995 int date_width
; /// < Width needed for the date part.
999 MessageHistoryWindow(WindowDesc
*desc
) : Window(desc
)
1001 this->CreateNestedTree();
1002 this->vscroll
= this->GetScrollbar(WID_MH_SCROLLBAR
);
1003 this->FinishInitNested(); // Initializes 'this->line_height' and 'this->date_width'.
1004 this->OnInvalidateData(0);
1007 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
1009 if (widget
== WID_MH_BACKGROUND
) {
1010 this->line_height
= FONT_HEIGHT_NORMAL
+ 2;
1011 resize
->height
= this->line_height
;
1013 /* Months are off-by-one, so it's actually 8. Not using
1014 * month 12 because the 1 is usually less wide. */
1015 SetDParam(0, ConvertYMDToDate(ORIGINAL_MAX_YEAR
, 7, 30));
1016 this->date_width
= GetStringBoundingBox(STR_SHORT_DATE
).width
;
1018 size
->height
= 4 * resize
->height
+ this->top_spacing
+ this->bottom_spacing
; // At least 4 lines are visible.
1019 size
->width
= max(200u, size
->width
); // At least 200 pixels wide.
1023 virtual void OnPaint()
1025 this->OnInvalidateData(0);
1026 this->DrawWidgets();
1029 virtual void DrawWidget(const Rect
&r
, int widget
) const
1031 if (widget
!= WID_MH_BACKGROUND
|| _total_news
== 0) return;
1033 /* Find the first news item to display. */
1034 NewsItem
*ni
= _latest_news
;
1035 for (int n
= this->vscroll
->GetPosition(); n
> 0; n
--) {
1037 if (ni
== NULL
) return;
1040 /* Fill the widget with news items. */
1041 int y
= r
.top
+ this->top_spacing
;
1042 bool rtl
= _current_text_dir
== TD_RTL
;
1043 uint date_left
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
- this->date_width
: r
.left
+ WD_FRAMERECT_LEFT
;
1044 uint date_right
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
: r
.left
+ WD_FRAMERECT_LEFT
+ this->date_width
;
1045 uint news_left
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
: r
.left
+ WD_FRAMERECT_LEFT
+ this->date_width
+ WD_FRAMERECT_RIGHT
;
1046 uint news_right
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
- this->date_width
- WD_FRAMERECT_RIGHT
: r
.right
- WD_FRAMERECT_RIGHT
;
1047 for (int n
= this->vscroll
->GetCapacity(); n
> 0; n
--) {
1048 SetDParam(0, ni
->date
);
1049 DrawString(date_left
, date_right
, y
, STR_SHORT_DATE
);
1051 DrawNewsString(news_left
, news_right
, y
, TC_WHITE
, ni
);
1052 y
+= this->line_height
;
1055 if (ni
== NULL
) return;
1060 * Some data on this window has become invalid.
1061 * @param data Information about the changed data.
1062 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1064 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
1066 if (!gui_scope
) return;
1067 this->vscroll
->SetCount(_total_news
);
1070 virtual void OnClick(Point pt
, int widget
, int click_count
)
1072 if (widget
== WID_MH_BACKGROUND
) {
1073 NewsItem
*ni
= _latest_news
;
1074 if (ni
== NULL
) return;
1076 for (int n
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_MH_BACKGROUND
, WD_FRAMERECT_TOP
, this->line_height
); n
> 0; n
--) {
1078 if (ni
== NULL
) return;
1081 ShowNewsMessage(ni
);
1085 virtual void OnResize()
1087 this->vscroll
->SetCapacityFromWidget(this, WID_MH_BACKGROUND
);
1091 const int MessageHistoryWindow::top_spacing
= WD_FRAMERECT_TOP
+ 4;
1092 const int MessageHistoryWindow::bottom_spacing
= WD_FRAMERECT_BOTTOM
;
1094 static const NWidgetPart _nested_message_history
[] = {
1095 NWidget(NWID_HORIZONTAL
),
1096 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1097 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_MESSAGE_HISTORY
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1098 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1099 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1100 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1103 NWidget(NWID_HORIZONTAL
),
1104 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_MH_BACKGROUND
), SetMinimalSize(200, 125), SetDataTip(0x0, STR_MESSAGE_HISTORY_TOOLTIP
), SetResize(1, 12), SetScrollbar(WID_MH_SCROLLBAR
),
1106 NWidget(NWID_VERTICAL
),
1107 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_MH_SCROLLBAR
),
1108 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1113 static WindowDesc
_message_history_desc(
1114 WDP_AUTO
, "list_news", 400, 140,
1115 WC_MESSAGE_HISTORY
, WC_NONE
,
1117 _nested_message_history
, lengthof(_nested_message_history
)
1120 /** Display window with news messages history */
1121 void ShowMessageHistory()
1123 DeleteWindowById(WC_MESSAGE_HISTORY
, 0);
1124 new MessageHistoryWindow(&_message_history_desc
);