# error_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: error_gui.cpp 24900 2013-01-08 22:46:42Z planetmaker $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "landscape.h"
    00014 #include "newgrf_text.h"
    00015 #include "error.h"
    00016 #include "viewport_func.h"
    00017 #include "gfx_func.h"
    00018 #include "string_func.h"
    00019 #include "company_base.h"
    00020 #include "company_manager_face.h"
    00021 #include "strings_func.h"
    00022 #include "zoom_func.h"
    00023 #include "window_func.h"
    00024 #include "console_func.h"
    00025 #include "window_gui.h"
    00026
    00027 #include "widgets/error_widget.h"
    00028
    00029 #include "table/strings.h"
    00030 #include <list>
    00031
    00032 static const NWidgetPart _nested_errmsg_widgets[] = {
    00033   NWidget(NWID_HORIZONTAL),
    00034     NWidget(WWT_CLOSEBOX, COLOUR_RED),
    00035     NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION, STR_NULL),
    00036   EndContainer(),
    00037   NWidget(WWT_PANEL, COLOUR_RED),
    00038     NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetPadding(0, 2, 0, 2), SetMinimalSize(236, 32),
    00039   EndContainer(),
    00040 };
    00041
    00042 static const WindowDesc _errmsg_desc(
    00043   WDP_MANUAL, 0, 0,
    00044   WC_ERRMSG, WC_NONE,
    00045   0,
    00046   _nested_errmsg_widgets, lengthof(_nested_errmsg_widgets)
    00047 );
    00048
    00049 static const NWidgetPart _nested_errmsg_face_widgets[] = {
    00050   NWidget(NWID_HORIZONTAL),
    00051     NWidget(WWT_CLOSEBOX, COLOUR_RED),
    00052     NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL),
    00053   EndContainer(),
    00054   NWidget(WWT_PANEL, COLOUR_RED),
    00055     NWidget(NWID_HORIZONTAL), SetPIP(2, 1, 2),
    00056       NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_FACE), SetMinimalSize(92, 119), SetFill(0, 1), SetPadding(2, 0, 1, 0),
    00057       NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetFill(0, 1), SetMinimalSize(238, 123),
    00058     EndContainer(),
    00059   EndContainer(),
    00060 };
    00061
    00062 static const WindowDesc _errmsg_face_desc(
    00063   WDP_MANUAL, 0, 0,
    00064   WC_ERRMSG, WC_NONE,
    00065   0,
    00066   _nested_errmsg_face_widgets, lengthof(_nested_errmsg_face_widgets)
    00067 );
    00068
    00073 ErrorMessageData::ErrorMessageData(const ErrorMessageData &data)
    00074 {
    00075   *this = data;
    00076   for (size_t i = 0; i < lengthof(this->strings); i++) {
    00077     if (this->strings[i] != NULL) {
    00078       this->strings[i] = strdup(this->strings[i]);
    00079       this->decode_params[i] = (size_t)this->strings[i];
    00080     }
    00081   }
    00082 }
    00083
    00085 ErrorMessageData::~ErrorMessageData()
    00086 {
    00087   for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
    00088 }
    00089
    00100 ErrorMessageData::ErrorMessageData(StringID summary_msg, StringID detailed_msg, uint duration, int x, int y, uint textref_stack_size, const uint32 *textref_stack) :
    00101   duration(duration),
    00102   textref_stack_size(textref_stack_size),
    00103   summary_msg(summary_msg),
    00104   detailed_msg(detailed_msg),
    00105   face(INVALID_COMPANY)
    00106 {
    00107   this->position.x = x;
    00108   this->position.y = y;
    00109
    00110   memset(this->decode_params, 0, sizeof(this->decode_params));
    00111   memset(this->strings, 0, sizeof(this->strings));
    00112
    00113   if (textref_stack_size > 0) MemCpyT(this->textref_stack, textref_stack, textref_stack_size);
    00114
    00115   assert(summary_msg != INVALID_STRING_ID);
    00116 }
    00117
    00121 void ErrorMessageData::CopyOutDParams()
    00122 {
    00123   /* Reset parameters */
    00124   for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
    00125   memset(this->decode_params, 0, sizeof(this->decode_params));
    00126   memset(this->strings, 0, sizeof(this->strings));
    00127
    00128   /* Get parameters using type information */
    00129   if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
    00130   CopyOutDParam(this->decode_params, this->strings, this->detailed_msg == INVALID_STRING_ID ? this->summary_msg : this->detailed_msg, lengthof(this->decode_params));
    00131   if (this->textref_stack_size > 0) StopTextRefStackUsage();
    00132
    00133   if (this->detailed_msg == STR_ERROR_OWNED_BY) {
    00134     CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
    00135     if (company < MAX_COMPANIES) face = company;
    00136   }
    00137 }
    00138
    00144 void ErrorMessageData::SetDParam(uint n, uint64 v)
    00145 {
    00146   this->decode_params[n] = v;
    00147 }
    00148
    00154 void ErrorMessageData::SetDParamStr(uint n, const char *str)
    00155 {
    00156   free(this->strings[n]);
    00157   this->strings[n] = strdup(str);
    00158 }
    00159
    00161 typedef std::list<ErrorMessageData> ErrorList;
    00163 ErrorList _error_list;
    00165 bool _window_system_initialized = false;
    00166
    00168 struct ErrmsgWindow : public Window, ErrorMessageData {
    00169 private:
    00170   uint height_summary;
    00171   uint height_detailed;
    00172
    00173 public:
    00174   ErrmsgWindow(const ErrorMessageData &data) : Window(), ErrorMessageData(data)
    00175   {
    00176     this->InitNested((this->face == INVALID_COMPANY) ? &_errmsg_desc : &_errmsg_face_desc);
    00177   }
    00178
    00179   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00180   {
    00181     if (widget != WID_EM_MESSAGE) return;
    00182
    00183     CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
    00184     if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
    00185
    00186     int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
    00187     this->height_summary  = GetStringHeight(this->summary_msg, text_width);
    00188     this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
    00189
    00190     if (this->textref_stack_size > 0) StopTextRefStackUsage();
    00191
    00192     uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
    00193     if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
    00194
    00195     size->height = max(size->height, panel_height);
    00196   }
    00197
    00198   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00199   {
    00200     /* Position (0, 0) given, center the window. */
    00201     if (this->position.x == 0 && this->position.y == 0) {
    00202       Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
    00203       return pt;
    00204     }
    00205
    00206     /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
    00207      * Add a fixed distance 20 to make it less cluttered.
    00208      */
    00209     int scr_top = GetMainViewTop() + 20;
    00210     int scr_bot = GetMainViewBottom() - 20;
    00211
    00212     Point pt = RemapCoords2(this->position.x, this->position.y);
    00213     const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
    00214     if (this->face == INVALID_COMPANY) {
    00215       /* move x pos to opposite corner */
    00216       pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
    00217       pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20; // Stay 20 pixels away from the edge of the screen.
    00218
    00219       /* move y pos to opposite corner */
    00220       pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
    00221       pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
    00222     } else {
    00223       pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2),  0, _screen.width  - sm_width);
    00224       pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top,  vp->zoom) + vp->top  - (sm_height / 2), scr_top, scr_bot - sm_height);
    00225     }
    00226     return pt;
    00227   }
    00228
    00234   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00235   {
    00236     /* If company gets shut down, while displaying an error about it, remove the error message. */
    00237     if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
    00238   }
    00239
    00240   virtual void SetStringParameters(int widget) const
    00241   {
    00242     if (widget == WID_EM_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
    00243   }
    00244
    00245   virtual void DrawWidget(const Rect &r, int widget) const
    00246   {
    00247     switch (widget) {
    00248       case WID_EM_FACE: {
    00249         const Company *c = Company::Get(this->face);
    00250         DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
    00251         break;
    00252       }
    00253
    00254       case WID_EM_MESSAGE:
    00255         CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
    00256         if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
    00257
    00258         if (this->detailed_msg == INVALID_STRING_ID) {
    00259           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
    00260               this->summary_msg, TC_FROMSTRING, SA_CENTER);
    00261         } else {
    00262           int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
    00263
    00264           /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
    00265           int top = r.top + WD_FRAMERECT_TOP;
    00266           int bottom = top + this->height_summary + extra;
    00267           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
    00268
    00269           bottom = r.bottom - WD_FRAMERECT_BOTTOM;
    00270           top = bottom - this->height_detailed - extra;
    00271           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
    00272         }
    00273
    00274         if (this->textref_stack_size > 0) StopTextRefStackUsage();
    00275         break;
    00276
    00277       default:
    00278         break;
    00279     }
    00280   }
    00281
    00282   virtual void OnMouseLoop()
    00283   {
    00284     /* Disallow closing the window too easily, if timeout is disabled */
    00285     if (_right_button_down && this->duration != 0) delete this;
    00286   }
    00287
    00288   virtual void OnHundredthTick()
    00289   {
    00290     /* Timeout enabled? */
    00291     if (this->duration != 0) {
    00292       this->duration--;
    00293       if (this->duration == 0) delete this;
    00294     }
    00295   }
    00296
    00297   ~ErrmsgWindow()
    00298   {
    00299     SetRedErrorSquare(INVALID_TILE);
    00300     if (_window_system_initialized) ShowFirstError();
    00301   }
    00302
    00303   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00304   {
    00305     if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
    00306     delete this;
    00307     return ES_HANDLED;
    00308   }
    00309
    00314   bool IsCritical()
    00315   {
    00316     return this->duration == 0;
    00317   }
    00318 };
    00319
    00323 void ClearErrorMessages()
    00324 {
    00325   UnshowCriticalError();
    00326   _error_list.clear();
    00327 }
    00328
    00330 void ShowFirstError()
    00331 {
    00332   _window_system_initialized = true;
    00333   if (!_error_list.empty()) {
    00334     new ErrmsgWindow(_error_list.front());
    00335     _error_list.pop_front();
    00336   }
    00337 }
    00338
    00344 void UnshowCriticalError()
    00345 {
    00346   ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
    00347   if (_window_system_initialized && w != NULL) {
    00348     if (w->IsCritical()) _error_list.push_front(*w);
    00349     _window_system_initialized = false;
    00350     delete w;
    00351   }
    00352 }
    00353
    00364 void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y, uint textref_stack_size, const uint32 *textref_stack)
    00365 {
    00366   assert(textref_stack_size == 0 || textref_stack != NULL);
    00367   if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
    00368
    00369   if (wl != WL_INFO) {
    00370     /* Print message to console */
    00371     char buf[DRAW_STRING_BUFFER];
    00372
    00373     if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_size, textref_stack);
    00374
    00375     char *b = GetString(buf, summary_msg, lastof(buf));
    00376     if (detailed_msg != INVALID_STRING_ID) {
    00377       b += seprintf(b, lastof(buf), " ");
    00378       GetString(b, detailed_msg, lastof(buf));
    00379     }
    00380
    00381     if (textref_stack_size > 0) StopTextRefStackUsage();
    00382
    00383     switch (wl) {
    00384       case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
    00385       default:         IConsoleError(buf); break;
    00386     }
    00387   }
    00388
    00389   bool no_timeout = wl == WL_CRITICAL;
    00390
    00391   if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
    00392
    00393   ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_size, textref_stack);
    00394   data.CopyOutDParams();
    00395
    00396   ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
    00397   if (w != NULL && w->IsCritical()) {
    00398     /* A critical error is currently shown. */
    00399     if (wl == WL_CRITICAL) {
    00400       /* Push another critical error in the queue of errors,
    00401        * but do not put other errors in the queue. */
    00402       _error_list.push_back(data);
    00403     }
    00404   } else {
    00405     /* Nothing or a non-critical error was shown. */
    00406     delete w;
    00407     new ErrmsgWindow(data);
    00408   }
    00409 }
    00410
    00416 void ScheduleErrorMessage(ErrorList &datas)
    00417 {
    00418   _error_list.splice(_error_list.end(), datas);
    00419 }
    00420
    00426 void ScheduleErrorMessage(const ErrorMessageData &data)
    00427 {
    00428   _error_list.push_back(data);
    00429 }