Add support for deleted functions
[openttd/fttd.git] / src / error_gui.cpp
blob5e8f52432d0dd338dcc7371e7b7142d005d1d697
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file error_gui.cpp GUI related to errors. */
12 #include "stdafx.h"
13 #include "landscape.h"
14 #include "newgrf_text.h"
15 #include "error.h"
16 #include "viewport_func.h"
17 #include "gfx_func.h"
18 #include "string_func.h"
19 #include "company_base.h"
20 #include "company_manager_face.h"
21 #include "strings_func.h"
22 #include "zoom_func.h"
23 #include "window_func.h"
24 #include "console_func.h"
25 #include "window_gui.h"
27 #include "widgets/error_widget.h"
29 #include "table/strings.h"
30 #include <list>
32 static const NWidgetPart _nested_errmsg_widgets[] = {
33 NWidget(NWID_HORIZONTAL),
34 NWidget(WWT_CLOSEBOX, COLOUR_RED),
35 NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION, STR_NULL),
36 EndContainer(),
37 NWidget(WWT_PANEL, COLOUR_RED),
38 NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetPadding(0, 2, 0, 2), SetMinimalSize(236, 32),
39 EndContainer(),
42 static WindowDesc _errmsg_desc(
43 WDP_MANUAL, "error", 0, 0,
44 WC_ERRMSG, WC_NONE,
46 _nested_errmsg_widgets, lengthof(_nested_errmsg_widgets)
49 static const NWidgetPart _nested_errmsg_face_widgets[] = {
50 NWidget(NWID_HORIZONTAL),
51 NWidget(WWT_CLOSEBOX, COLOUR_RED),
52 NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL),
53 EndContainer(),
54 NWidget(WWT_PANEL, COLOUR_RED),
55 NWidget(NWID_HORIZONTAL), SetPIP(2, 1, 2),
56 NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_FACE), SetMinimalSize(92, 119), SetFill(0, 1), SetPadding(2, 0, 1, 0),
57 NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetFill(0, 1), SetMinimalSize(238, 123),
58 EndContainer(),
59 EndContainer(),
62 static WindowDesc _errmsg_face_desc(
63 WDP_MANUAL, "error_face", 0, 0,
64 WC_ERRMSG, WC_NONE,
66 _nested_errmsg_face_widgets, lengthof(_nested_errmsg_face_widgets)
69 /**
70 * Copy the given data into our instance.
71 * @param data The data to copy.
73 ErrorMessageData::ErrorMessageData(const ErrorMessageData &data)
75 *this = data;
76 for (size_t i = 0; i < lengthof(this->strings); i++) {
77 if (this->strings[i] != NULL) {
78 this->strings[i] = strdup(this->strings[i]);
79 this->decode_params[i] = (size_t)this->strings[i];
84 /** Free all the strings. */
85 ErrorMessageData::~ErrorMessageData()
87 for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
90 /**
91 * Display an error message in a window.
92 * @param summary_msg General error message showed in first line. Must be valid.
93 * @param detailed_msg Detailed error message showed in second line. Can be INVALID_STRING_ID.
94 * @param duration The amount of time to show this error message.
95 * @param x World X position (TileVirtX) of the error location. Set both x and y to 0 to just center the message when there is no related error tile.
96 * @param y World Y position (TileVirtY) of the error location. Set both x and y to 0 to just center the message when there is no related error tile.
97 * @param textref_stack_grffile NewGRF that provides the #TextRefStack for the error message.
98 * @param textref_stack_size Number of uint32 values to put on the #TextRefStack for the error message; 0 if the #TextRefStack shall not be used.
99 * @param textref_stack Values to put on the #TextRefStack.
101 ErrorMessageData::ErrorMessageData(StringID summary_msg, StringID detailed_msg, uint duration, int x, int y, const GRFFile *textref_stack_grffile, uint textref_stack_size, const uint32 *textref_stack) :
102 duration(duration),
103 textref_stack_grffile(textref_stack_grffile),
104 textref_stack_size(textref_stack_size),
105 summary_msg(summary_msg),
106 detailed_msg(detailed_msg),
107 face(INVALID_COMPANY)
109 this->position.x = x;
110 this->position.y = y;
112 memset(this->decode_params, 0, sizeof(this->decode_params));
113 memset(this->strings, 0, sizeof(this->strings));
115 if (textref_stack_size > 0) MemCpyT(this->textref_stack, textref_stack, textref_stack_size);
117 assert(summary_msg != INVALID_STRING_ID);
121 * Copy error parameters from current DParams.
123 void ErrorMessageData::CopyOutDParams()
125 /* Reset parameters */
126 for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
127 memset(this->decode_params, 0, sizeof(this->decode_params));
128 memset(this->strings, 0, sizeof(this->strings));
130 /* Get parameters using type information */
131 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack);
132 CopyOutDParam(this->decode_params, this->strings, this->detailed_msg == INVALID_STRING_ID ? this->summary_msg : this->detailed_msg, lengthof(this->decode_params));
133 if (this->textref_stack_size > 0) StopTextRefStackUsage();
135 if (this->detailed_msg == STR_ERROR_OWNED_BY) {
136 CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
137 if (company < MAX_COMPANIES) face = company;
142 * Set a error string parameter.
143 * @param n Parameter index
144 * @param v Parameter value
146 void ErrorMessageData::SetDParam(uint n, uint64 v)
148 this->decode_params[n] = v;
152 * Set a rawstring parameter.
153 * @param n Parameter index
154 * @param str Raw string
156 void ErrorMessageData::SetDParamStr(uint n, const char *str)
158 free(this->strings[n]);
159 this->strings[n] = strdup(str);
162 /** Define a queue with errors. */
163 typedef std::list<ErrorMessageData> ErrorList;
164 /** The actual queue with errors. */
165 ErrorList _error_list;
166 /** Whether the window system is initialized or not. */
167 bool _window_system_initialized = false;
169 /** Window class for displaying an error message window. */
170 struct ErrmsgWindow : public Window, ErrorMessageData {
171 private:
172 uint height_summary; ///< Height of the #summary_msg string in pixels in the #WID_EM_MESSAGE widget.
173 uint height_detailed; ///< Height of the #detailed_msg string in pixels in the #WID_EM_MESSAGE widget.
175 public:
176 ErrmsgWindow(const ErrorMessageData &data) : Window(data.HasFace() ? &_errmsg_face_desc : &_errmsg_desc), ErrorMessageData(data)
178 this->InitNested();
181 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
183 if (widget != WID_EM_MESSAGE) return;
185 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
186 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack);
188 int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
189 this->height_summary = GetStringHeight(this->summary_msg, text_width);
190 this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
192 if (this->textref_stack_size > 0) StopTextRefStackUsage();
194 uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
195 if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
197 size->height = max(size->height, panel_height);
200 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
202 /* Position (0, 0) given, center the window. */
203 if (this->position.x == 0 && this->position.y == 0) {
204 Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
205 return pt;
208 /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
209 * Add a fixed distance 20 to make it less cluttered.
211 int scr_top = GetMainViewTop() + 20;
212 int scr_bot = GetMainViewBottom() - 20;
214 Point pt = RemapCoords2(this->position.x, this->position.y);
215 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
216 if (this->face == INVALID_COMPANY) {
217 /* move x pos to opposite corner */
218 pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
219 pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20; // Stay 20 pixels away from the edge of the screen.
221 /* move y pos to opposite corner */
222 pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
223 pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
224 } else {
225 pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2), 0, _screen.width - sm_width);
226 pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top - (sm_height / 2), scr_top, scr_bot - sm_height);
228 return pt;
232 * Some data on this window has become invalid.
233 * @param data Information about the changed data.
234 * @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.
236 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
238 /* If company gets shut down, while displaying an error about it, remove the error message. */
239 if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
242 virtual void SetStringParameters(int widget) const
244 if (widget == WID_EM_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
247 virtual void DrawWidget(const Rect &r, int widget) const
249 switch (widget) {
250 case WID_EM_FACE: {
251 const Company *c = Company::Get(this->face);
252 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
253 break;
256 case WID_EM_MESSAGE:
257 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
258 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack);
260 if (this->detailed_msg == INVALID_STRING_ID) {
261 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
262 this->summary_msg, TC_FROMSTRING, SA_CENTER);
263 } else {
264 int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
266 /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
267 int top = r.top + WD_FRAMERECT_TOP;
268 int bottom = top + this->height_summary + extra;
269 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
271 bottom = r.bottom - WD_FRAMERECT_BOTTOM;
272 top = bottom - this->height_detailed - extra;
273 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
276 if (this->textref_stack_size > 0) StopTextRefStackUsage();
277 break;
279 default:
280 break;
284 virtual void OnMouseLoop()
286 /* Disallow closing the window too easily, if timeout is disabled */
287 if (_right_button_down && this->duration != 0) delete this;
290 virtual void OnHundredthTick()
292 /* Timeout enabled? */
293 if (this->duration != 0) {
294 this->duration--;
295 if (this->duration == 0) delete this;
299 ~ErrmsgWindow()
301 SetRedErrorSquare(INVALID_TILE);
302 if (_window_system_initialized) ShowFirstError();
305 virtual EventState OnKeyPress(WChar key, uint16 keycode)
307 if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
308 delete this;
309 return ES_HANDLED;
313 * Check whether the currently shown error message was critical or not.
314 * @return True iff the message was critical.
316 bool IsCritical()
318 return this->duration == 0;
323 * Clear all errors from the queue.
325 void ClearErrorMessages()
327 UnshowCriticalError();
328 _error_list.clear();
331 /** Show the first error of the queue. */
332 void ShowFirstError()
334 _window_system_initialized = true;
335 if (!_error_list.empty()) {
336 new ErrmsgWindow(_error_list.front());
337 _error_list.pop_front();
342 * Unshow the critical error. This has to happen when a critical
343 * error is shown and we uninitialise the window system, i.e.
344 * remove all the windows.
346 void UnshowCriticalError()
348 ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
349 if (_window_system_initialized && w != NULL) {
350 if (w->IsCritical()) _error_list.push_front(*w);
351 _window_system_initialized = false;
352 delete w;
357 * Display an error message in a window.
358 * @param summary_msg General error message showed in first line. Must be valid.
359 * @param detailed_msg Detailed error message showed in second line. Can be INVALID_STRING_ID.
360 * @param wl Message severity.
361 * @param x World X position (TileVirtX) of the error location. Set both x and y to 0 to just center the message when there is no related error tile.
362 * @param y World Y position (TileVirtY) of the error location. Set both x and y to 0 to just center the message when there is no related error tile.
363 * @param textref_stack_grffile NewGRF providing the #TextRefStack for the error message.
364 * @param textref_stack_size Number of uint32 values to put on the #TextRefStack for the error message; 0 if the #TextRefStack shall not be used.
365 * @param textref_stack Values to put on the #TextRefStack.
367 void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y, const GRFFile *textref_stack_grffile, uint textref_stack_size, const uint32 *textref_stack)
369 assert(textref_stack_size == 0 || (textref_stack_grffile != NULL && textref_stack != NULL));
370 if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
372 if (wl != WL_INFO) {
373 /* Print message to console */
374 char buf[DRAW_STRING_BUFFER];
376 if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_grffile, textref_stack_size, textref_stack);
378 char *b = GetString(buf, summary_msg, lastof(buf));
379 if (detailed_msg != INVALID_STRING_ID) {
380 b += seprintf(b, lastof(buf), " ");
381 GetString(b, detailed_msg, lastof(buf));
384 if (textref_stack_size > 0) StopTextRefStackUsage();
386 switch (wl) {
387 case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
388 default: IConsoleError(buf); break;
392 bool no_timeout = wl == WL_CRITICAL;
394 if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
396 ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_grffile, textref_stack_size, textref_stack);
397 data.CopyOutDParams();
399 ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
400 if (w != NULL && w->IsCritical()) {
401 /* A critical error is currently shown. */
402 if (wl == WL_CRITICAL) {
403 /* Push another critical error in the queue of errors,
404 * but do not put other errors in the queue. */
405 _error_list.push_back(data);
407 } else {
408 /* Nothing or a non-critical error was shown. */
409 delete w;
410 new ErrmsgWindow(data);
415 * Schedule a list of errors.
416 * Note: This does not try to display the error now. This is useful if the window system is not yet running.
417 * @param data Error message datas; cleared afterwards
419 void ScheduleErrorMessage(ErrorList &datas)
421 _error_list.splice(_error_list.end(), datas);
425 * Schedule an error.
426 * Note: This does not try to display the error now. This is useful if the window system is not yet running.
427 * @param data Error message data; cleared afterwards
429 void ScheduleErrorMessage(const ErrorMessageData &data)
431 _error_list.push_back(data);