OpenTTD
error_gui.cpp
Go to the documentation of this file.
1 /* $Id: error_gui.cpp 26509 2014-04-25 15:40:32Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include "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"
26 
27 #include "widgets/error_widget.h"
28 
29 #include "table/strings.h"
30 #include <list>
31 
32 #include "safeguards.h"
33 
34 static const NWidgetPart _nested_errmsg_widgets[] = {
36  NWidget(WWT_CLOSEBOX, COLOUR_RED),
37  NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION, STR_NULL),
38  EndContainer(),
39  NWidget(WWT_PANEL, COLOUR_RED),
40  NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetPadding(0, 2, 0, 2), SetMinimalSize(236, 32),
41  EndContainer(),
42 };
43 
44 static WindowDesc _errmsg_desc(
45  WDP_MANUAL, "error", 0, 0,
47  0,
48  _nested_errmsg_widgets, lengthof(_nested_errmsg_widgets)
49 );
50 
51 static const NWidgetPart _nested_errmsg_face_widgets[] = {
53  NWidget(WWT_CLOSEBOX, COLOUR_RED),
54  NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL),
55  EndContainer(),
56  NWidget(WWT_PANEL, COLOUR_RED),
57  NWidget(NWID_HORIZONTAL), SetPIP(2, 1, 2),
58  NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_FACE), SetMinimalSize(92, 119), SetFill(0, 1), SetPadding(2, 0, 1, 0),
59  NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetFill(0, 1), SetMinimalSize(238, 123),
60  EndContainer(),
61  EndContainer(),
62 };
63 
64 static WindowDesc _errmsg_face_desc(
65  WDP_MANUAL, "error_face", 0, 0,
67  0,
68  _nested_errmsg_face_widgets, lengthof(_nested_errmsg_face_widgets)
69 );
70 
76 {
77  *this = data;
78  for (size_t i = 0; i < lengthof(this->strings); i++) {
79  if (this->strings[i] != NULL) {
80  this->strings[i] = stredup(this->strings[i]);
81  this->decode_params[i] = (size_t)this->strings[i];
82  }
83  }
84 }
85 
88 {
89  for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
90 }
91 
103 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) :
104  duration(duration),
105  textref_stack_grffile(textref_stack_grffile),
106  textref_stack_size(textref_stack_size),
107  summary_msg(summary_msg),
108  detailed_msg(detailed_msg),
109  face(INVALID_COMPANY)
110 {
111  this->position.x = x;
112  this->position.y = y;
113 
114  memset(this->decode_params, 0, sizeof(this->decode_params));
115  memset(this->strings, 0, sizeof(this->strings));
116 
117  if (textref_stack_size > 0) MemCpyT(this->textref_stack, textref_stack, textref_stack_size);
118 
119  assert(summary_msg != INVALID_STRING_ID);
120 }
121 
126 {
127  /* Reset parameters */
128  for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
129  memset(this->decode_params, 0, sizeof(this->decode_params));
130  memset(this->strings, 0, sizeof(this->strings));
131 
132  /* Get parameters using type information */
135  if (this->textref_stack_size > 0) StopTextRefStackUsage();
136 
137  if (this->detailed_msg == STR_ERROR_OWNED_BY) {
138  CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
139  if (company < MAX_COMPANIES) face = company;
140  }
141 }
142 
148 void ErrorMessageData::SetDParam(uint n, uint64 v)
149 {
150  this->decode_params[n] = v;
151 }
152 
158 void ErrorMessageData::SetDParamStr(uint n, const char *str)
159 {
160  free(this->strings[n]);
161  this->strings[n] = stredup(str);
162 }
163 
165 typedef std::list<ErrorMessageData> ErrorList;
167 ErrorList _error_list;
170 
173 private:
176 
177 public:
178  ErrmsgWindow(const ErrorMessageData &data) : Window(data.HasFace() ? &_errmsg_face_desc : &_errmsg_desc), ErrorMessageData(data)
179  {
180  this->InitNested();
181  }
182 
183  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
184  {
185  if (widget != WID_EM_MESSAGE) return;
186 
189 
190  int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
191  this->height_summary = GetStringHeight(this->summary_msg, text_width);
192  this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
193 
194  if (this->textref_stack_size > 0) StopTextRefStackUsage();
195 
196  uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
197  if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
198 
199  size->height = max(size->height, panel_height);
200  }
201 
202  virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
203  {
204  /* Position (0, 0) given, center the window. */
205  if (this->position.x == 0 && this->position.y == 0) {
206  Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
207  return pt;
208  }
209 
210  /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
211  * Add a fixed distance 20 to make it less cluttered.
212  */
213  int scr_top = GetMainViewTop() + 20;
214  int scr_bot = GetMainViewBottom() - 20;
215 
216  Point pt = RemapCoords2(this->position.x, this->position.y);
218  if (this->face == INVALID_COMPANY) {
219  /* move x pos to opposite corner */
220  pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
221  pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20; // Stay 20 pixels away from the edge of the screen.
222 
223  /* move y pos to opposite corner */
224  pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
225  pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
226  } else {
227  pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2), 0, _screen.width - sm_width);
228  pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top - (sm_height / 2), scr_top, scr_bot - sm_height);
229  }
230  return pt;
231  }
232 
238  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
239  {
240  /* If company gets shut down, while displaying an error about it, remove the error message. */
241  if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
242  }
243 
244  virtual void SetStringParameters(int widget) const
245  {
246  if (widget == WID_EM_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
247  }
248 
249  virtual void DrawWidget(const Rect &r, int widget) const
250  {
251  switch (widget) {
252  case WID_EM_FACE: {
253  const Company *c = Company::Get(this->face);
254  DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
255  break;
256  }
257 
258  case WID_EM_MESSAGE:
261 
262  if (this->detailed_msg == INVALID_STRING_ID) {
264  this->summary_msg, TC_FROMSTRING, SA_CENTER);
265  } else {
266  int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
267 
268  /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
269  int top = r.top + WD_FRAMERECT_TOP;
270  int bottom = top + this->height_summary + extra;
271  DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
272 
273  bottom = r.bottom - WD_FRAMERECT_BOTTOM;
274  top = bottom - this->height_detailed - extra;
275  DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
276  }
277 
278  if (this->textref_stack_size > 0) StopTextRefStackUsage();
279  break;
280 
281  default:
282  break;
283  }
284  }
285 
286  virtual void OnMouseLoop()
287  {
288  /* Disallow closing the window too easily, if timeout is disabled */
289  if (_right_button_down && this->duration != 0) delete this;
290  }
291 
292  virtual void OnHundredthTick()
293  {
294  /* Timeout enabled? */
295  if (this->duration != 0) {
296  this->duration--;
297  if (this->duration == 0) delete this;
298  }
299  }
300 
301  ~ErrmsgWindow()
302  {
304  if (_window_system_initialized) ShowFirstError();
305  }
306 
307  virtual EventState OnKeyPress(WChar key, uint16 keycode)
308  {
309  if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
310  delete this;
311  return ES_HANDLED;
312  }
313 
318  bool IsCritical()
319  {
320  return this->duration == 0;
321  }
322 };
323 
328 {
330  _error_list.clear();
331 }
332 
335 {
336  _window_system_initialized = true;
337  if (!_error_list.empty()) {
338  new ErrmsgWindow(_error_list.front());
339  _error_list.pop_front();
340  }
341 }
342 
349 {
351  if (_window_system_initialized && w != NULL) {
352  if (w->IsCritical()) _error_list.push_front(*w);
353  _window_system_initialized = false;
354  delete w;
355  }
356 }
357 
369 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)
370 {
371  assert(textref_stack_size == 0 || (textref_stack_grffile != NULL && textref_stack != NULL));
372  if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
373 
374  if (wl != WL_INFO) {
375  /* Print message to console */
376  char buf[DRAW_STRING_BUFFER];
377 
378  if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_grffile, textref_stack_size, textref_stack);
379 
380  char *b = GetString(buf, summary_msg, lastof(buf));
381  if (detailed_msg != INVALID_STRING_ID) {
382  b += seprintf(b, lastof(buf), " ");
383  GetString(b, detailed_msg, lastof(buf));
384  }
385 
386  if (textref_stack_size > 0) StopTextRefStackUsage();
387 
388  switch (wl) {
389  case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
390  default: IConsoleError(buf); break;
391  }
392  }
393 
394  bool no_timeout = wl == WL_CRITICAL;
395 
396  if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
397 
398  ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_grffile, textref_stack_size, textref_stack);
399  data.CopyOutDParams();
400 
402  if (w != NULL && w->IsCritical()) {
403  /* A critical error is currently shown. */
404  if (wl == WL_CRITICAL) {
405  /* Push another critical error in the queue of errors,
406  * but do not put other errors in the queue. */
407  _error_list.push_back(data);
408  }
409  } else {
410  /* Nothing or a non-critical error was shown. */
411  delete w;
412  new ErrmsgWindow(data);
413  }
414 }
415 
421 void ScheduleErrorMessage(ErrorList &datas)
422 {
423  _error_list.splice(_error_list.end(), datas);
424 }
425 
432 {
433  _error_list.push_back(data);
434 }