error_gui.cpp

Go to the documentation of this file.
00001 /* $Id: error_gui.cpp 23583 2011-12-17 22:35:22Z rubidium $ */
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 
00070 class ErrorMessageData {
00071 protected:
00072   uint duration;                  
00073   uint64 decode_params[20];       
00074   const char *strings[20];        
00075   uint textref_stack_size;        
00076   uint32 textref_stack[16];       
00077   StringID summary_msg;           
00078   StringID detailed_msg;          
00079   Point position;                 
00080   CompanyID face;                 
00081 
00082 public:
00087   ErrorMessageData(const ErrorMessageData &data)
00088   {
00089     *this = data;
00090     for (size_t i = 0; i < lengthof(this->strings); i++) {
00091       if (this->strings[i] != NULL) {
00092         this->strings[i] = strdup(this->strings[i]);
00093         this->decode_params[i] = (size_t)this->strings[i];
00094       }
00095     }
00096   }
00097 
00099   ~ErrorMessageData()
00100   {
00101     for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
00102   }
00103 
00114   ErrorMessageData(StringID summary_msg, StringID detailed_msg, uint duration, int x, int y, uint textref_stack_size, const uint32 *textref_stack) :
00115     duration(duration),
00116     textref_stack_size(textref_stack_size),
00117     summary_msg(summary_msg),
00118     detailed_msg(detailed_msg)
00119   {
00120     this->position.x = x;
00121     this->position.y = y;
00122     CopyOutDParam(this->decode_params, this->strings, detailed_msg == INVALID_STRING_ID ? summary_msg : detailed_msg, lengthof(this->decode_params));
00123     if (textref_stack_size > 0) {
00124       MemCpyT(this->textref_stack, textref_stack, textref_stack_size);
00125     }
00126 
00127     CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
00128     this->face = (this->detailed_msg == STR_ERROR_OWNED_BY && company < MAX_COMPANIES) ? company : INVALID_COMPANY;
00129 
00130     assert(summary_msg != INVALID_STRING_ID);
00131   }
00132 };
00133 
00135 typedef std::list<ErrorMessageData> ErrorList;
00137 ErrorList _error_list;
00139 bool _window_system_initialized = false;
00140 
00142 struct ErrmsgWindow : public Window, ErrorMessageData {
00143 private:
00144   uint height_summary;            
00145   uint height_detailed;           
00146 
00147 public:
00148   ErrmsgWindow(const ErrorMessageData &data) : Window(), ErrorMessageData(data)
00149   {
00150     this->InitNested((this->face == INVALID_COMPANY) ? &_errmsg_desc : &_errmsg_face_desc);
00151   }
00152 
00153   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00154   {
00155     if (widget != WID_EM_MESSAGE) return;
00156 
00157     CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00158     if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
00159 
00160     int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
00161     this->height_summary  = GetStringHeight(this->summary_msg, text_width);
00162     this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
00163 
00164     if (this->textref_stack_size > 0) StopTextRefStackUsage();
00165 
00166     uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
00167     if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
00168 
00169     size->height = max(size->height, panel_height);
00170   }
00171 
00172   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00173   {
00174     /* Position (0, 0) given, center the window. */
00175     if (this->position.x == 0 && this->position.y == 0) {
00176       Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
00177       return pt;
00178     }
00179 
00180     /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
00181      * Add a fixed distance 20 to make it less cluttered.
00182      */
00183     int scr_top = GetMainViewTop() + 20;
00184     int scr_bot = GetMainViewBottom() - 20;
00185 
00186     Point pt = RemapCoords2(this->position.x, this->position.y);
00187     const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
00188     if (this->face == INVALID_COMPANY) {
00189       /* move x pos to opposite corner */
00190       pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
00191       pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20; // Stay 20 pixels away from the edge of the screen.
00192 
00193       /* move y pos to opposite corner */
00194       pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
00195       pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
00196     } else {
00197       pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2),  0, _screen.width  - sm_width);
00198       pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top,  vp->zoom) + vp->top  - (sm_height / 2), scr_top, scr_bot - sm_height);
00199     }
00200     return pt;
00201   }
00202 
00208   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00209   {
00210     /* If company gets shut down, while displaying an error about it, remove the error message. */
00211     if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
00212   }
00213 
00214   virtual void SetStringParameters(int widget) const
00215   {
00216     if (widget == WID_EM_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00217   }
00218 
00219   virtual void DrawWidget(const Rect &r, int widget) const
00220   {
00221     switch (widget) {
00222       case WID_EM_FACE: {
00223         const Company *c = Company::Get(this->face);
00224         DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
00225         break;
00226       }
00227 
00228       case WID_EM_MESSAGE:
00229         CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00230         if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
00231 
00232         if (this->detailed_msg == INVALID_STRING_ID) {
00233           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00234               this->summary_msg, TC_FROMSTRING, SA_CENTER);
00235         } else {
00236           int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
00237 
00238           /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
00239           int top = r.top + WD_FRAMERECT_TOP;
00240           int bottom = top + this->height_summary + extra;
00241           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
00242 
00243           bottom = r.bottom - WD_FRAMERECT_BOTTOM;
00244           top = bottom - this->height_detailed - extra;
00245           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
00246         }
00247 
00248         if (this->textref_stack_size > 0) StopTextRefStackUsage();
00249         break;
00250 
00251       default:
00252         break;
00253     }
00254   }
00255 
00256   virtual void OnMouseLoop()
00257   {
00258     /* Disallow closing the window too easily, if timeout is disabled */
00259     if (_right_button_down && this->duration != 0) delete this;
00260   }
00261 
00262   virtual void OnHundredthTick()
00263   {
00264     /* Timeout enabled? */
00265     if (this->duration != 0) {
00266       this->duration--;
00267       if (this->duration == 0) delete this;
00268     }
00269   }
00270 
00271   ~ErrmsgWindow()
00272   {
00273     SetRedErrorSquare(INVALID_TILE);
00274     if (_window_system_initialized) ShowFirstError();
00275   }
00276 
00277   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00278   {
00279     if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
00280     delete this;
00281     return ES_HANDLED;
00282   }
00283 
00288   bool IsCritical()
00289   {
00290     return this->duration == 0;
00291   }
00292 };
00293 
00297 void ClearErrorMessages()
00298 {
00299   UnshowCriticalError();
00300   _error_list.clear();
00301 }
00302 
00304 void ShowFirstError()
00305 {
00306   _window_system_initialized = true;
00307   if (!_error_list.empty()) {
00308     new ErrmsgWindow(_error_list.front());
00309     _error_list.pop_front();
00310   }
00311 }
00312 
00318 void UnshowCriticalError()
00319 {
00320   ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00321   if (_window_system_initialized && w != NULL) {
00322     if (w->IsCritical()) _error_list.push_front(*w);
00323     _window_system_initialized = false;
00324     delete w;
00325   }
00326 }
00327 
00338 void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y, uint textref_stack_size, const uint32 *textref_stack)
00339 {
00340   assert(textref_stack_size == 0 || textref_stack != NULL);
00341   if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
00342 
00343   if (wl != WL_INFO) {
00344     /* Print message to console */
00345     char buf[DRAW_STRING_BUFFER];
00346 
00347     if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_size, textref_stack);
00348 
00349     char *b = GetString(buf, summary_msg, lastof(buf));
00350     if (detailed_msg != INVALID_STRING_ID) {
00351       b += seprintf(b, lastof(buf), " ");
00352       GetString(b, detailed_msg, lastof(buf));
00353     }
00354 
00355     if (textref_stack_size > 0) StopTextRefStackUsage();
00356 
00357     switch (wl) {
00358       case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
00359       default:         IConsoleError(buf); break;
00360     }
00361   }
00362 
00363   bool no_timeout = wl == WL_CRITICAL;
00364 
00365   if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
00366 
00367   ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_size, textref_stack);
00368 
00369   ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00370   if (w != NULL && w->IsCritical()) {
00371     /* A critical error is currently shown. */
00372     if (wl == WL_CRITICAL) {
00373       /* Push another critical error in the queue of errors,
00374        * but do not put other errors in the queue. */
00375       _error_list.push_back(data);
00376     }
00377   } else {
00378     /* Nothing or a non-critical error was shown. */
00379     delete w;
00380     new ErrmsgWindow(data);
00381   }
00382 }