src/news_gui.cpp
branchNewGRF_ports
changeset 10724 68a692eacf22
parent 10274 b3c58f3df92b
child 10731 67db0d431d5e
--- a/src/news_gui.cpp	Fri Apr 25 02:15:34 2008 +0000
+++ b/src/news_gui.cpp	Mon May 26 20:45:25 2008 +0000
@@ -1,5 +1,6 @@
 /* $Id$ */
 
+/** @file news_gui.cpp GUI functions related to news messages. */
 
 #include "stdafx.h"
 #include "openttd.h"
@@ -16,6 +17,8 @@
 #include "sound_func.h"
 #include "string_func.h"
 #include "widgets/dropdown_func.h"
+#include "map_func.h"
+#include "statusbar_gui.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -45,8 +48,6 @@
  * \endverbatim
  */
 
-/** Number of news items in the FIFO queue */
-#define MAX_NEWS 30
 #define NB_WIDG_PER_SETTING 4
 
 typedef byte NewsID;
@@ -54,51 +55,72 @@
 
 NewsItem _statusbar_news_item;
 bool _news_ticker_sound;
-static NewsItem _news_items[MAX_NEWS];      ///< The news FIFO queue
+static NewsItem *_news_items = NULL;        ///< The news FIFO queue
+static uint _max_news_items = 0;            ///< size of news FIFO queue
 static NewsID _current_news = INVALID_NEWS; ///< points to news item that should be shown next
 static NewsID _oldest_news = 0;             ///< points to first item in fifo queue
 static NewsID _latest_news = INVALID_NEWS;  ///< points to last item in fifo queue
 
-struct news_d : vp_d {
-	uint16 follow_vehicle;
-	int32 scrollpos_x;
-	int32 scrollpos_y;
-	int32 dest_scrollpos_x;
-	int32 dest_scrollpos_y;
-	NewsItem *ni;
-};
-assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(news_d));
-
 /** Forced news item.
  * Users can force an item by accessing the history or "last message".
  * If the message being shown was forced by the user, its index is stored in
  * _forced_news. Otherwise, \a _forced_news variable is INVALID_NEWS. */
 static NewsID _forced_news = INVALID_NEWS;
 
-static byte _total_news = 0; ///< Number of news items in FIFO queue @see _news_items
+static uint _total_news = 0; ///< Number of news items in FIFO queue @see _news_items
+static void MoveToNextItem();
 
+
+typedef void DrawNewsCallbackProc(struct Window *w, const NewsItem *ni);
 void DrawNewsNewVehicleAvail(Window *w, const NewsItem *ni);
 void DrawNewsBankrupcy(Window *w, const NewsItem *ni);
-static void MoveToNextItem();
-
-StringID GetNewsStringNewVehicleAvail(const NewsItem *ni);
-StringID GetNewsStringBankrupcy(const NewsItem *ni);
 
 static DrawNewsCallbackProc * const _draw_news_callback[] = {
 	DrawNewsNewVehicleAvail,  ///< DNC_VEHICLEAVAIL
 	DrawNewsBankrupcy,        ///< DNC_BANKRUPCY
 };
 
-extern GetNewsStringCallbackProc * const _get_news_string_callback[];
-GetNewsStringCallbackProc * const _get_news_string_callback[] = {
-	GetNewsStringNewVehicleAvail,  ///< DNC_VEHICLEAVAIL
-	GetNewsStringBankrupcy,        ///< DNC_BANKRUPCY
+/**
+ * Data common to all news items of a given subtype (structure)
+ */
+struct NewsSubtypeData {
+	NewsType type;         ///< News category @see NewsType
+	NewsMode display_mode; ///< Display mode value @see NewsMode
+	NewsFlag flags;        ///< Initial NewsFlags bits @see NewsFlag
+	NewsCallback callback; ///< Call-back function
+};
+
+/**
+ * Data common to all news items of a given subtype (actual data)
+ */
+static const struct NewsSubtypeData _news_subtype_data[NS_END] = {
+	/* type,             display_mode, flags,                  callback */
+	{ NT_ARRIVAL_PLAYER,  NM_THIN,     NF_VIEWPORT|NF_VEHICLE, DNC_NONE         }, ///< NS_ARRIVAL_PLAYER
+	{ NT_ARRIVAL_OTHER,   NM_THIN,     NF_VIEWPORT|NF_VEHICLE, DNC_NONE         }, ///< NS_ARRIVAL_OTHER
+	{ NT_ACCIDENT,        NM_THIN,     NF_VIEWPORT|NF_TILE,    DNC_NONE         }, ///< NS_ACCIDENT_TILE
+	{ NT_ACCIDENT,        NM_THIN,     NF_VIEWPORT|NF_VEHICLE, DNC_NONE         }, ///< NS_ACCIDENT_VEHICLE
+	{ NT_COMPANY_INFO,    NM_CALLBACK, NF_NONE,                DNC_BANKRUPCY    }, ///< NS_COMPANY_TROUBLE
+	{ NT_COMPANY_INFO,    NM_CALLBACK, NF_NONE,                DNC_BANKRUPCY    }, ///< NS_COMPANY_MERGER
+	{ NT_COMPANY_INFO,    NM_CALLBACK, NF_NONE,                DNC_BANKRUPCY    }, ///< NS_COMPANY_BANKRUPT
+	{ NT_COMPANY_INFO,    NM_CALLBACK, NF_TILE,                DNC_BANKRUPCY    }, ///< NS_COMPANY_NEW
+	{ NT_OPENCLOSE,       NM_THIN,     NF_VIEWPORT|NF_TILE,    DNC_NONE         }, ///< NS_OPENCLOSE
+	{ NT_ECONOMY,         NM_NORMAL,   NF_NONE,                DNC_NONE         }, ///< NS_ECONOMY
+	{ NT_INDUSTRY_PLAYER, NM_THIN,     NF_VIEWPORT|NF_TILE,    DNC_NONE         }, ///< NS_INDUSTRY_PLAYER
+	{ NT_INDUSTRY_OTHER,  NM_THIN,     NF_VIEWPORT|NF_TILE,    DNC_NONE         }, ///< NS_INDUSTRY_OTHER
+	{ NT_INDUSTRY_NOBODY, NM_THIN,     NF_VIEWPORT|NF_TILE,    DNC_NONE         }, ///< NS_INDUSTRY_NOBODY
+	{ NT_ADVICE,          NM_SMALL,    NF_VIEWPORT|NF_VEHICLE, DNC_NONE         }, ///< NS_ADVICE
+	{ NT_NEW_VEHICLES,    NM_CALLBACK, NF_NONE,                DNC_VEHICLEAVAIL }, ///< NS_NEW_VEHICLES
+	{ NT_ACCEPTANCE,      NM_SMALL,    NF_VIEWPORT|NF_TILE,    DNC_NONE         }, ///< NS_ACCEPTANCE
+	{ NT_SUBSIDIES,       NM_NORMAL,   NF_TILE|NF_TILE2,       DNC_NONE         }, ///< NS_SUBSIDIES
+	{ NT_GENERAL,         NM_NORMAL,   NF_TILE,                DNC_NONE         }, ///< NS_GENERAL
 };
 
 /** Initialize the news-items data structures */
 void InitNewsItemStructs()
 {
-	memset(_news_items, 0, sizeof(_news_items));
+	free(_news_items);
+	_max_news_items = max(ScaleByMapSize(30), 30U);
+	_news_items = CallocT<NewsItem>(_max_news_items);
 	_current_news = INVALID_NEWS;
 	_oldest_news = 0;
 	_latest_news = INVALID_NEWS;
@@ -106,137 +128,154 @@
 	_total_news = 0;
 }
 
-void DrawNewsBorder(const Window *w)
-{
-	int left = 0;
-	int right = w->width - 1;
-	int top = 0;
-	int bottom = w->height - 1;
-
-	GfxFillRect(left, top, right, bottom, 0xF);
-
-	GfxFillRect(left, top, left, bottom, 0xD7);
-	GfxFillRect(right, top, right, bottom, 0xD7);
-	GfxFillRect(left, top, right, top, 0xD7);
-	GfxFillRect(left, bottom, right, bottom, 0xD7);
-
-	DrawString(left + 2, top + 1, STR_00C6, TC_FROMSTRING);
-}
-
-static void NewsWindowProc(Window *w, WindowEvent *e)
-{
-	switch (e->event) {
-		case WE_CREATE: { // If chatbar is open at creation time, we need to go above it
-			const Window *w1 = FindWindowById(WC_SEND_NETWORK_MSG, 0);
-			w->message.msg = (w1 != NULL) ? w1->height : 0;
-		} break;
-
-		case WE_PAINT: {
-			const NewsItem *ni = WP(w, news_d).ni;
-
-			switch (ni->display_mode) {
-				case NM_NORMAL:
-				case NM_THIN: {
-					DrawNewsBorder(w);
-
-					DrawString(2, 1, STR_00C6, TC_FROMSTRING);
-
-					SetDParam(0, ni->date);
-					DrawStringRightAligned(428, 1, STR_01FF, TC_FROMSTRING);
-
-					if (!(ni->flags & NF_VIEWPORT)) {
-						CopyInDParam(0, ni->params, lengthof(ni->params));
-						DrawStringMultiCenter(215, ni->display_mode == NM_NORMAL ? 76 : 56,
-							ni->string_id, w->width - 4);
-					} else {
-						/* Back up transparency options to draw news view */
-						TransparencyOptionBits to_backup = _transparency_opt;
-						_transparency_opt = 0;
-						DrawWindowViewport(w);
-						_transparency_opt = to_backup;
-
-						/* Shade the viewport into gray, or color*/
-						ViewPort *vp = w->viewport;
-						GfxFillRect(vp->left - w->left, vp->top - w->top,
-							vp->left - w->left + vp->width - 1, vp->top - w->top + vp->height - 1,
-							(ni->flags & NF_INCOLOR ? PALETTE_TO_TRANSPARENT : PALETTE_TO_STRUCT_GREY) | (1 << USE_COLORTABLE)
-						);
-
-						CopyInDParam(0, ni->params, lengthof(ni->params));
-						DrawStringMultiCenter(w->width / 2, 20, ni->string_id, w->width - 4);
-					}
-					break;
-				}
+struct NewsWindow : Window {
+	uint16 chat_height;
+	NewsItem *ni;
 
-				case NM_CALLBACK:
-					_draw_news_callback[ni->callback](w, ni);
-					break;
+	NewsWindow(const WindowDesc *desc, NewsItem *ni) : Window(desc), ni(ni)
+	{
+		const Window *w = FindWindowById(WC_SEND_NETWORK_MSG, 0);
+		this->chat_height = (w != NULL) ? w->height : 0;
 
-				default:
-					DrawWindowWidgets(w);
-					if (!(ni->flags & NF_VIEWPORT)) {
-						CopyInDParam(0, ni->params, lengthof(ni->params));
-						DrawStringMultiCenter(140, 38, ni->string_id, 276);
-					} else {
-						DrawWindowViewport(w);
-						CopyInDParam(0, ni->params, lengthof(ni->params));
-						DrawStringMultiCenter(w->width / 2, w->height - 16, ni->string_id, w->width - 4);
-					}
-					break;
+		this->ni = &_news_items[_forced_news == INVALID_NEWS ? _current_news : _forced_news];
+		this->flags4 |= WF_DISABLE_VP_SCROLL;
+
+		this->FindWindowPlacementAndResize(desc);
+	}
+
+	void DrawNewsBorder()
+	{
+		int left = 0;
+		int right = this->width - 1;
+		int top = 0;
+		int bottom = this->height - 1;
+
+		GfxFillRect(left, top, right, bottom, 0xF);
+
+		GfxFillRect(left, top, left, bottom, 0xD7);
+		GfxFillRect(right, top, right, bottom, 0xD7);
+		GfxFillRect(left, top, right, top, 0xD7);
+		GfxFillRect(left, bottom, right, bottom, 0xD7);
+
+		DrawString(left + 2, top + 1, STR_00C6, TC_FROMSTRING);
+	}
+
+	virtual void OnPaint()
+	{
+		const NewsMode display_mode = _news_subtype_data[this->ni->subtype].display_mode;
+
+		switch (display_mode) {
+			case NM_NORMAL:
+			case NM_THIN: {
+				this->DrawNewsBorder();
+
+				DrawString(2, 1, STR_00C6, TC_FROMSTRING);
+
+				SetDParam(0, this->ni->date);
+				DrawStringRightAligned(428, 1, STR_01FF, TC_FROMSTRING);
+
+				if (!(this->ni->flags & NF_VIEWPORT)) {
+					CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
+					DrawStringMultiCenter(215, display_mode == NM_NORMAL ? 76 : 56,
+						this->ni->string_id, this->width - 4);
+				} else {
+					/* Back up transparency options to draw news view */
+					TransparencyOptionBits to_backup = _transparency_opt;
+					_transparency_opt = 0;
+					this->DrawViewport();
+					_transparency_opt = to_backup;
+
+					/* Shade the viewport into gray, or color*/
+					ViewPort *vp = this->viewport;
+					GfxFillRect(vp->left - this->left, vp->top - this->top,
+						vp->left - this->left + vp->width - 1, vp->top - this->top + vp->height - 1,
+						(this->ni->flags & NF_INCOLOR ? PALETTE_TO_TRANSPARENT : PALETTE_TO_STRUCT_GREY) | (1 << USE_COLORTABLE)
+					);
+
+					CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
+					DrawStringMultiCenter(this->width / 2, 20, this->ni->string_id, this->width - 4);
+				}
+				break;
 			}
-		} break;
 
-		case WE_CLICK: {
-			switch (e->we.click.widget) {
-			case 1: {
-				NewsItem *ni = WP(w, news_d).ni;
-				DeleteWindow(w);
-				ni->duration = 0;
+			case NM_CALLBACK:
+				this->DrawNewsBorder();
+				_draw_news_callback[_news_subtype_data[this->ni->subtype].callback](this, ni);
+				break;
+
+			default:
+				this->DrawWidgets();
+				if (!(this->ni->flags & NF_VIEWPORT)) {
+					CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
+					DrawStringMultiCenter(140, 38, this->ni->string_id, 276);
+				} else {
+					this->DrawViewport();
+					CopyInDParam(0, this->ni->params, lengthof(this->ni->params));
+					DrawStringMultiCenter(this->width / 2, this->height - 16, this->ni->string_id, this->width - 4);
+				}
+				break;
+		}
+	}
+
+	virtual void OnClick(Point pt, int widget)
+	{
+		switch (widget) {
+			case 1:
+				this->ni->duration = 0;
+				delete this;
 				_forced_news = INVALID_NEWS;
-			} break;
-			case 0: {
-				NewsItem *ni = WP(w, news_d).ni;
-				if (ni->flags & NF_VEHICLE) {
-					Vehicle *v = GetVehicle(ni->data_a);
+				break;
+
+			case 0:
+				if (this->ni->flags & NF_VEHICLE) {
+					Vehicle *v = GetVehicle(this->ni->data_a);
 					ScrollMainWindowTo(v->x_pos, v->y_pos);
-				} else if (ni->flags & NF_TILE) {
-					if (!ScrollMainWindowToTile(ni->data_a) && ni->data_b != 0)
-						ScrollMainWindowToTile(ni->data_b);
+				} else if (this->ni->flags & NF_TILE) {
+					if (_ctrl_pressed) {
+						ShowExtraViewPortWindow(this->ni->data_a);
+						if (this->ni->flags & NF_TILE2) {
+							ShowExtraViewPortWindow(this->ni->data_b);
+						}
+					} else {
+						if (!ScrollMainWindowToTile(this->ni->data_a) && this->ni->flags & NF_TILE2) {
+							ScrollMainWindowToTile(this->ni->data_b);
+						}
+					}
 				}
-			} break;
-			}
-		} break;
-
-		case WE_KEYPRESS:
-			if (e->we.keypress.keycode == WKC_SPACE) {
-				/* Don't continue. */
-				e->we.keypress.cont = false;
-				DeleteWindow(w);
-			}
-			break;
+				break;
+		}
+	}
 
-		case WE_MESSAGE: // The chatbar has notified us that is was either created or closed
-			switch (e->we.message.msg) {
-				case WE_CREATE: w->message.msg = e->we.message.wparam; break;
-				case WE_DESTROY: w->message.msg = 0; break;
-			}
-			break;
+	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	{
+		if (keycode == WKC_SPACE) {
+			/* Don't continue. */
+			delete this;
+			return false;
+		}
+		return true;
+	}
 
-		case WE_TICK: { // Scroll up newsmessages from the bottom in steps of 4 pixels
-			int diff;
-			int y = max(w->top - 4, _screen.height - w->height - 12 - w->message.msg);
-			if (y == w->top) return;
+	virtual void OnInvalidateData(int data)
+	{
+		/* The chatbar has notified us that is was either created or closed */
+		this->chat_height = data;
+	}
 
-			if (w->viewport != NULL)
-				w->viewport->top += y - w->top;
+	virtual void OnTick()
+	{
+		/* Scroll up newsmessages from the bottom in steps of 4 pixels */
+		int y = max(this->top - 4, _screen.height - this->height - 12 - this->chat_height);
+		if (y == this->top) return;
 
-			diff = Delta(w->top, y);
-			w->top = y;
+		if (this->viewport != NULL) this->viewport->top += y - this->top;
 
-			SetDirtyBlocks(w->left, w->top - diff, w->left + w->width, w->top + w->height);
-		} break;
+		int diff = Delta(this->top, y);
+		this->top = y;
+
+		SetDirtyBlocks(this->left, this->top - diff, this->left + this->width, this->top + this->height);
 	}
-}
+};
 
 /**
  * Return the correct index in the pseudo-fifo
@@ -245,7 +284,7 @@
 static inline NewsID IncreaseIndex(NewsID i)
 {
 	assert(i != INVALID_NEWS);
-	return (i + 1) % MAX_NEWS;
+	return (i + 1) % _max_news_items;
 }
 
 /**
@@ -255,43 +294,28 @@
 static inline NewsID DecreaseIndex(NewsID i)
 {
 	assert(i != INVALID_NEWS);
-	return (i + MAX_NEWS - 1) % MAX_NEWS;
+	return (i + _max_news_items - 1) % _max_news_items;
 }
 
 /**
  * Add a new newsitem to be shown.
- * @param string String to display, can have special values based on parameter \a display_mode
- * @param display_mode, any of the NewsMode enums (NM_)
- * @param flags any of the NewsFlag enums (NF_)
- * @param type news category, any of the NewsType enums (NT_)
- * @param callback news callback function, any of the NewsCallback enums (DNC_)
+ * @param string String to display
+ * @param subtype news category, any of the NewsSubtype enums (NS_)
  * @param data_a news-specific value based on news type
  * @param data_b news-specific value based on news type
  *
- * @note If the display mode is NM_CALLBACK, special news is shown and parameter
- * \a string has a special meaning.
- *  - For DNC_TRAINAVAIL, DNC_ROADAVAIL, DNC_SHIPAVAIL, DNC_AIRCRAFTAVAIL messages: StringID is
- *    the index of the engine that is shown
- *
- *  - For DNC_BANKRUPCY: bytes 0-3 of StringID contains the player that is in trouble,
- *    and 4-7 contains what kind of bankrupcy message is shown.
- *    @see NewsBankrupcy
- *
- * @see NewsMode
- * @see NewsFlag
- * @see NewsType
- * @see NewsCallback
+ * @see NewsSubype
  */
-void AddNewsItem(StringID string, NewsMode display_mode, NewsFlag flags, NewsType type, NewsCallback callback, uint data_a, uint data_b)
+void AddNewsItem(StringID string, NewsSubtype subtype, uint data_a, uint data_b)
 {
 	if (_game_mode == GM_MENU) return;
 
 	/* check the rare case that the oldest (to be overwritten) news item is open */
-	if (_total_news == MAX_NEWS && (_oldest_news == _current_news || _oldest_news == _forced_news)) {
+	if (_total_news == _max_news_items && (_oldest_news == _current_news || _oldest_news == _forced_news)) {
 		MoveToNextItem();
 	}
 
-	if (_total_news < MAX_NEWS) _total_news++;
+	if (_total_news < _max_news_items) _total_news++;
 
 	/* Increase _latest_news. If we have no news yet, use _oldest news as an
 	 * index. We cannot use 0 as _oldest_news can jump around due to
@@ -301,7 +325,7 @@
 
 	/* If the fifo-buffer is full, overwrite the oldest entry */
 	if (l_news != INVALID_NEWS && _latest_news == _oldest_news) {
-		assert(_total_news == MAX_NEWS);
+		assert(_total_news == _max_news_items);
 		_oldest_news = IncreaseIndex(_oldest_news);
 	}
 
@@ -313,14 +337,12 @@
 	memset(ni, 0, sizeof(*ni));
 
 	ni->string_id = string;
-	ni->display_mode = display_mode;
-	ni->flags = flags;
+	ni->subtype = subtype;
+	ni->flags = _news_subtype_data[subtype].flags;
 
 	/* show this news message in color? */
 	if (_cur_year >= _patches.colored_news_year) ni->flags |= NF_INCOLOR;
 
-	ni->type = type;
-	ni->callback = callback;
 	ni->data_a = data_a;
 	ni->data_b = data_b;
 	ni->date = _date;
@@ -328,7 +350,7 @@
 
 	Window *w = FindWindowById(WC_MESSAGE_HISTORY, 0);
 	if (w == NULL) return;
-	SetWindowDirty(w);
+	w->SetDirty();
 	w->vscroll.count = _total_news;
 }
 
@@ -366,7 +388,7 @@
 	WC_NEWS_WINDOW, WC_NONE,
 	WDF_DEF_WIDGET,
 	_news_type13_widgets,
-	NewsWindowProc
+	NULL
 };
 
 static const Widget _news_type2_widgets[] = {
@@ -380,7 +402,7 @@
 	WC_NEWS_WINDOW, WC_NONE,
 	WDF_DEF_WIDGET,
 	_news_type2_widgets,
-	NewsWindowProc
+	NULL
 };
 
 static const Widget _news_type0_widgets[] = {
@@ -396,7 +418,7 @@
 	WC_NEWS_WINDOW, WC_NONE,
 	WDF_DEF_WIDGET,
 	_news_type0_widgets,
-	NewsWindowProc
+	NULL
 };
 
 
@@ -406,16 +428,16 @@
 	ni->flags &= ~NF_FORCE_BIG;
 	ni->duration = 555;
 
-	SoundFx sound = _news_type_data[ni->type].sound;
+	SoundFx sound = _news_type_data[_news_subtype_data[ni->subtype].type].sound;
 	if (sound != 0) SndPlayFx(sound);
 
 	int top = _screen.height;
 	Window *w;
-	switch (ni->display_mode) {
+	switch (_news_subtype_data[ni->subtype].display_mode) {
 		case NM_NORMAL:
 		case NM_CALLBACK:
 			_news_type13_desc.top = top;
-			w = AllocateWindowDesc(&_news_type13_desc);
+			w = new NewsWindow(&_news_type13_desc, ni);
 			if (ni->flags & NF_VIEWPORT) {
 				InitializeWindowViewport(w, 2, 58, 426, 110,
 					ni->data_a | (ni->flags & NF_VEHICLE ? 0x80000000 : 0), ZOOM_LVL_NEWS);
@@ -424,7 +446,7 @@
 
 		case NM_THIN:
 			_news_type2_desc.top = top;
-			w = AllocateWindowDesc(&_news_type2_desc);
+			w = new NewsWindow(&_news_type2_desc, ni);
 			if (ni->flags & NF_VIEWPORT) {
 				InitializeWindowViewport(w, 2, 58, 426, 70,
 					ni->data_a | (ni->flags & NF_VEHICLE ? 0x80000000 : 0), ZOOM_LVL_NEWS);
@@ -433,7 +455,7 @@
 
 		default:
 			_news_type0_desc.top = top;
-			w = AllocateWindowDesc(&_news_type0_desc);
+			w = new NewsWindow(&_news_type0_desc, ni);
 			if (ni->flags & NF_VIEWPORT) {
 				InitializeWindowViewport(w, 3, 17, 274, 47,
 					ni->data_a | (ni->flags & NF_VEHICLE ? 0x80000000 : 0), ZOOM_LVL_NEWS);
@@ -443,9 +465,6 @@
 
 	/*DEBUG(misc, 0, " cur %3d, old %2d, lat %3d, for %3d, tot %2d",
 	  _current_news, _oldest_news, _latest_news, _forced_news, _total_news);*/
-
-	WP(w, news_d).ni = &_news_items[_forced_news == INVALID_NEWS ? _current_news : _forced_news];
-	w->flags4 |= WF_DISABLE_VP_SCROLL;
 }
 
 /** Show news item in the ticker */
@@ -454,8 +473,7 @@
 	if (_news_ticker_sound) SndPlayFx(SND_16_MORSE);
 
 	_statusbar_news_item = *ni;
-	Window *w = FindWindowById(WC_STATUS_BAR, 0);
-	if (w != NULL) WP(w, def_d).data_1 = 360;
+	InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
 }
 
 
@@ -467,13 +485,12 @@
 {
 	NewsID item = (_forced_news == INVALID_NEWS) ? _current_news : _forced_news;
 
-	if (item >= MAX_NEWS) return true;
+	if (item >= _max_news_items) return true;
 	NewsItem *ni = &_news_items[item];
 
 	/* Ticker message
 	 * Check if the status bar message is still being displayed? */
-	const Window *w = FindWindowById(WC_STATUS_BAR, 0);
-	if (w != NULL && WP(w, const def_d).data_1 > -1280) return false;
+	if (IsNewsTickerShown()) return false;
 
 	/* Newspaper message, decrement duration counter */
 	if (ni->duration != 0) ni->duration--;
@@ -492,21 +509,16 @@
 	if (_current_news != _latest_news) {
 		_current_news = (_current_news == INVALID_NEWS) ? _oldest_news : IncreaseIndex(_current_news);
 		NewsItem *ni = &_news_items[_current_news];
+		const NewsType type = _news_subtype_data[ni->subtype].type;
 
 		/* check the date, don't show too old items */
-		if (_date - _news_type_data[ni->type].age > ni->date) return;
+		if (_date - _news_type_data[type].age > ni->date) return;
 
-		switch (_news_type_data[ni->type].display) {
+		switch (_news_type_data[type].display) {
 			default: NOT_REACHED();
-			case ND_OFF: { // Off - show nothing only a small reminder in the status bar
-				Window *w = FindWindowById(WC_STATUS_BAR, 0);
-
-				if (w != NULL) {
-					WP(w, def_d).data_2 = 91;
-					SetWindowDirty(w);
-				}
+			case ND_OFF: // Off - show nothing only a small reminder in the status bar
+				InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
 				break;
-			}
 
 			case ND_SUMMARY: // Summary - show ticker, but if forced big, cascade to full
 				if (!(ni->flags & NF_FORCE_BIG)) {
@@ -575,12 +587,12 @@
 	if (i >= _total_news) return INVALID_NEWS;
 
 	if (_latest_news < i) {
-		i = _latest_news + MAX_NEWS - i;
+		i = _latest_news + _max_news_items - i;
 	} else {
 		i = _latest_news - i;
 	}
 
-	i %= MAX_NEWS;
+	i %= _max_news_items;
 	return i;
 }
 
@@ -597,12 +609,8 @@
 	char buffer[512], buffer2[512];
 	StringID str;
 
-	if (ni->display_mode == NM_CALLBACK) {
-		str = _get_news_string_callback[ni->callback](ni);
-	} else {
-		CopyInDParam(0, ni->params, lengthof(ni->params));
-		str = ni->string_id;
-	}
+	CopyInDParam(0, ni->params, lengthof(ni->params));
+	str = ni->string_id;
 
 	GetString(buffer, str, lastof(buffer));
 	/* Copy the just gotten string to another buffer to remove any formatting
@@ -639,7 +647,7 @@
 			int y = 19;
 
 			SetVScrollCount(w, _total_news);
-			DrawWindowWidgets(w);
+			w->DrawWidgets();
 
 			if (_total_news == 0) break;
 			NewsID show = min(_total_news, w->vscroll.cap);
@@ -694,17 +702,17 @@
 void ShowMessageHistory()
 {
 	DeleteWindowById(WC_MESSAGE_HISTORY, 0);
-	Window *w = AllocateWindowDesc(&_message_history_desc);
+	Window *w = new Window(&_message_history_desc);
 
-	if (w != NULL) {
-		w->vscroll.cap = 10;
-		w->vscroll.count = _total_news;
-		w->resize.step_height = 12;
-		w->resize.height = w->height - 12 * 6; // minimum of 4 items in the list, each item 12 high
-		w->resize.step_width = 1;
-		w->resize.width = 200; // can't make window any smaller than 200 pixel
-		SetWindowDirty(w);
-	}
+	if (w == NULL) return;
+
+	w->vscroll.cap = 10;
+	w->vscroll.count = _total_news;
+	w->resize.step_height = 12;
+	w->resize.height = w->height - 12 * 6; // minimum of 4 items in the list, each item 12 high
+	w->resize.step_width = 1;
+	w->resize.width = 200; // can't make window any smaller than 200 pixel
+	w->SetDirty();
 }
 
 
@@ -715,99 +723,96 @@
 	WIDGET_NEWSOPT_START_OPTION = 8,  ///< First widget that is part of a group [<] .. [.]
 };
 
-/**
- * Setup the disabled/enabled buttons in the message window
- * If the value is 'off' disable the [<] widget, and enable the [>] one
- * Same-wise for all the others. Starting value of 4 is the first widget
- * group. These are grouped as [<][>] .. [<][>], etc.
- * @param w Window been used
- * @param value to set in the widget
- * @param element index of the group of widget to set
- */
-static void SetMessageButtonStates(Window *w, byte value, int element)
-{
-	element *= NB_WIDG_PER_SETTING;
-
-	w->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION, value == 0);
-	w->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION + 2, value == 2);
-}
-
-/**
- * Event handler of the Message Options window
- * @param w window pointer
- * @param e event been triggered
- */
-static void MessageOptionsWndProc(Window *w, WindowEvent *e)
-{
-	static const StringID message_opt[] = {STR_OFF, STR_SUMMARY, STR_FULL, INVALID_STRING_ID};
-
-	/* WP(w, def_d).data_1 stores state of the ALL on/off/summary button */
-	switch (e->event) {
-		case WE_CREATE: {
-			NewsDisplay all_val;
-
-			/* Set up the initial disabled buttons in the case of 'off' or 'full' */
-			all_val = _news_type_data[0].display;
-			for (int i = 0; i < NT_END; i++) {
-				SetMessageButtonStates(w, _news_type_data[i].display, i);
-				/* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
-				if (_news_type_data[i].display != all_val) all_val = ND_OFF;
-			}
-			/* If all values are the same value, the ALL-button will take over this value */
-			WP(w, def_d).data_1 = all_val;
-		} break;
-
-		case WE_PAINT: {
-			if (_news_ticker_sound) w->LowerWidget(WIDGET_NEWSOPT_SOUNDTICKER);
+static const StringID _message_opt[] = {STR_OFF, STR_SUMMARY, STR_FULL, INVALID_STRING_ID};
 
-			w->widget[WIDGET_NEWSOPT_DROP_SUMMARY].data = message_opt[WP(w, def_d).data_1];
-			DrawWindowWidgets(w);
-
-			/* Draw the string of each setting on each button. */
-			for (int i = 0, y = 26; i < NT_END; i++, y += 12) {
-				/* 51 comes from 13 + 89 (left and right of the button)+1, shiefted by one as to get division,
-				 * which will give centered position */
-				DrawStringCentered(51, y + 1, message_opt[_news_type_data[i].display], TC_BLACK);
-			}
-		} break;
-
-		case WE_CLICK:
-			switch (e->we.click.widget) {
-				case WIDGET_NEWSOPT_DROP_SUMMARY: // Dropdown menu for all settings
-					ShowDropDownMenu(w, message_opt, WP(w, def_d).data_1, WIDGET_NEWSOPT_DROP_SUMMARY, 0, 0);
-					break;
-
-				case WIDGET_NEWSOPT_SOUNDTICKER: // Change ticker sound on/off
-					_news_ticker_sound ^= 1;
-					w->ToggleWidgetLoweredState(e->we.click.widget);
-					w->InvalidateWidget(e->we.click.widget);
-					break;
+struct MessageOptionsWindow : Window {
+	int state;
 
-				default: { // Clicked on the [<] .. [>] widgets
-					int wid = e->we.click.widget - WIDGET_NEWSOPT_START_OPTION;
-					if (wid >= 0 && wid < (NB_WIDG_PER_SETTING * NT_END)) {
-						int element = wid / NB_WIDG_PER_SETTING;
-						byte val = (_news_type_data[element].display + ((wid % NB_WIDG_PER_SETTING) ? 1 : -1)) % 3;
+	MessageOptionsWindow(const WindowDesc *desc) : Window(desc)
+	{
+		NewsDisplay all_val;
 
-						SetMessageButtonStates(w, val, element);
-						_news_type_data[element].display = (NewsDisplay)val;
-						SetWindowDirty(w);
-					}
-				} break;
+		/* Set up the initial disabled buttons in the case of 'off' or 'full' */
+		all_val = _news_type_data[0].display;
+		for (int i = 0; i < NT_END; i++) {
+			this->SetMessageButtonStates(_news_type_data[i].display, i);
+			/* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
+			if (_news_type_data[i].display != all_val) all_val = ND_OFF;
+		}
+		/* If all values are the same value, the ALL-button will take over this value */
+		this->state = all_val;
+	}
+
+	/**
+	 * Setup the disabled/enabled buttons in the message window
+	 * If the value is 'off' disable the [<] widget, and enable the [>] one
+	 * Same-wise for all the others. Starting value of 4 is the first widget
+	 * group. These are grouped as [<][>] .. [<][>], etc.
+	 * @param value to set in the widget
+	 * @param element index of the group of widget to set
+	 */
+	void SetMessageButtonStates(byte value, int element)
+	{
+		element *= NB_WIDG_PER_SETTING;
+
+		this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION, value == 0);
+		this->SetWidgetDisabledState(element + WIDGET_NEWSOPT_START_OPTION + 2, value == 2);
+	}
+
+	virtual void OnPaint()
+	{
+		if (_news_ticker_sound) this->LowerWidget(WIDGET_NEWSOPT_SOUNDTICKER);
+
+		this->widget[WIDGET_NEWSOPT_DROP_SUMMARY].data = _message_opt[this->state];
+		this->DrawWidgets();
+
+		/* Draw the string of each setting on each button. */
+		for (int i = 0, y = 26; i < NT_END; i++, y += 12) {
+			/* 51 comes from 13 + 89 (left and right of the button)+1, shiefted by one as to get division,
+				* which will give centered position */
+			DrawStringCentered(51, y + 1, _message_opt[_news_type_data[i].display], TC_BLACK);
+		}
+	}
+
+	virtual void OnClick(Point pt, int widget)
+	{
+		switch (widget) {
+			case WIDGET_NEWSOPT_DROP_SUMMARY: // Dropdown menu for all settings
+				ShowDropDownMenu(this, _message_opt, this->state, WIDGET_NEWSOPT_DROP_SUMMARY, 0, 0);
+				break;
+
+			case WIDGET_NEWSOPT_SOUNDTICKER: // Change ticker sound on/off
+				_news_ticker_sound ^= 1;
+				this->ToggleWidgetLoweredState(widget);
+				this->InvalidateWidget(widget);
+				break;
+
+			default: { // Clicked on the [<] .. [>] widgets
+				int wid = widget - WIDGET_NEWSOPT_START_OPTION;
+				if (wid >= 0 && wid < (NB_WIDG_PER_SETTING * NT_END)) {
+					int element = wid / NB_WIDG_PER_SETTING;
+					byte val = (_news_type_data[element].display + ((wid % NB_WIDG_PER_SETTING) ? 1 : -1)) % 3;
+
+					this->SetMessageButtonStates(val, element);
+					_news_type_data[element].display = (NewsDisplay)val;
+					this->SetDirty();
+				}
+				break;
 			}
-			break;
-
-		case WE_DROPDOWN_SELECT: // Select all settings for newsmessages
-			WP(w, def_d).data_1 = e->we.dropdown.index;
+		}
+	}
 
-			for (int i = 0; i < NT_END; i++) {
-				SetMessageButtonStates(w, e->we.dropdown.index, i);
-				_news_type_data[i].display = (NewsDisplay)e->we.dropdown.index;
-			}
-			SetWindowDirty(w);
-			break;
+	virtual void OnDropdownSelect(int widget, int index)
+	{
+		this->state = index;
+
+		for (int i = 0; i < NT_END; i++) {
+			this->SetMessageButtonStates(index, i);
+			_news_type_data[i].display = (NewsDisplay)index;
+		}
+		this->SetDirty();
 	}
-}
+};
 
 
 /*
@@ -908,13 +913,13 @@
 	WC_GAME_OPTIONS, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
 	_message_options_widgets,
-	MessageOptionsWndProc
+	NULL
 };
 
 void ShowMessageOptions()
 {
 	DeleteWindowById(WC_GAME_OPTIONS, 0);
-	AllocateWindowDesc(&_message_options_desc);
+	new MessageOptionsWindow(&_message_options_desc);
 }
 
 
@@ -949,8 +954,8 @@
 			 * We also need an update of the current, forced and visible (open window)
 			 * news's as this shifting could change the items they were pointing to */
 			if (_total_news != 0) {
-				Window *w = FindWindowById(WC_NEWS_WINDOW, 0);
-				NewsID visible_news = (w != NULL) ? (NewsID)(WP(w, news_d).ni - _news_items) : INVALID_NEWS;
+				NewsWindow *w = dynamic_cast<NewsWindow*>(FindWindowById(WC_NEWS_WINDOW, 0));
+				NewsID visible_news = (w != NULL) ? (NewsID)(w->ni - _news_items) : INVALID_NEWS;
 
 				for (NewsID i = n;; i = DecreaseIndex(i)) {
 					_news_items[i] = _news_items[DecreaseIndex(i)];
@@ -958,7 +963,7 @@
 					if (i != _latest_news) {
 						if (i == _current_news) _current_news = IncreaseIndex(_current_news);
 						if (i == _forced_news) _forced_news = IncreaseIndex(_forced_news);
-						if (i == visible_news) WP(w, news_d).ni = &_news_items[IncreaseIndex(visible_news)];
+						if (i == visible_news) w->ni = &_news_items[IncreaseIndex(visible_news)];
 					}
 
 					if (i == _oldest_news) break;
@@ -971,7 +976,7 @@
 
 			Window *w = FindWindowById(WC_MESSAGE_HISTORY, 0);
 			if (w != NULL) {
-				SetWindowDirty(w);
+				w->SetDirty();
 				w->vscroll.count = _total_news;
 			}
 		}