error_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "table/strings.h"
00028 #include <list>
00029 
00031 enum ErrorMessageWidgets {
00032   EMW_CAPTION,
00033   EMW_FACE,
00034   EMW_MESSAGE,
00035 };
00036 
00037 static const NWidgetPart _nested_errmsg_widgets[] = {
00038   NWidget(NWID_HORIZONTAL),
00039     NWidget(WWT_CLOSEBOX, COLOUR_RED),
00040     NWidget(WWT_CAPTION, COLOUR_RED, EMW_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION, STR_NULL),
00041   EndContainer(),
00042   NWidget(WWT_PANEL, COLOUR_RED),
00043     NWidget(WWT_EMPTY, COLOUR_RED, EMW_MESSAGE), SetPadding(0, 2, 0, 2), SetMinimalSize(236, 32),
00044   EndContainer(),
00045 };
00046 
00047 static const WindowDesc _errmsg_desc(
00048   WDP_MANUAL, 0, 0,
00049   WC_ERRMSG, WC_NONE,
00050   0,
00051   _nested_errmsg_widgets, lengthof(_nested_errmsg_widgets)
00052 );
00053 
00054 static const NWidgetPart _nested_errmsg_face_widgets[] = {
00055   NWidget(NWID_HORIZONTAL),
00056     NWidget(WWT_CLOSEBOX, COLOUR_RED),
00057     NWidget(WWT_CAPTION, COLOUR_RED, EMW_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL),
00058   EndContainer(),
00059   NWidget(WWT_PANEL, COLOUR_RED),
00060     NWidget(NWID_HORIZONTAL), SetPIP(2, 1, 2),
00061       NWidget(WWT_EMPTY, COLOUR_RED, EMW_FACE), SetMinimalSize(92, 119), SetFill(0, 1), SetPadding(2, 0, 1, 0),
00062       NWidget(WWT_EMPTY, COLOUR_RED, EMW_MESSAGE), SetFill(0, 1), SetMinimalSize(238, 123),
00063     EndContainer(),
00064   EndContainer(),
00065 };
00066 
00067 static const WindowDesc _errmsg_face_desc(
00068   WDP_MANUAL, 0, 0,
00069   WC_ERRMSG, WC_NONE,
00070   0,
00071   _nested_errmsg_face_widgets, lengthof(_nested_errmsg_face_widgets)
00072 );
00073 
00075 class ErrorMessageData {
00076 protected:
00077   uint duration;                  
00078   uint64 decode_params[20];       
00079   const char *strings[20];        
00080   uint textref_stack_size;        
00081   uint32 textref_stack[16];       
00082   StringID summary_msg;           
00083   StringID detailed_msg;          
00084   Point position;                 
00085   CompanyID face;                 
00086 
00087 public:
00092   ErrorMessageData(const ErrorMessageData &data)
00093   {
00094     *this = data;
00095     for (size_t i = 0; i < lengthof(this->strings); i++) {
00096       if (this->strings[i] != NULL) {
00097         this->strings[i] = strdup(this->strings[i]);
00098         this->decode_params[i] = (size_t)this->strings[i];
00099       }
00100     }
00101   }
00102 
00104   ~ErrorMessageData()
00105   {
00106     for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
00107   }
00108 
00119   ErrorMessageData(StringID summary_msg, StringID detailed_msg, uint duration, int x, int y, uint textref_stack_size, const uint32 *textref_stack) :
00120     duration(duration),
00121     textref_stack_size(textref_stack_size),
00122     summary_msg(summary_msg),
00123     detailed_msg(detailed_msg)
00124   {
00125     this->position.x = x;
00126     this->position.y = y;
00127     CopyOutDParam(this->decode_params, this->strings, detailed_msg == INVALID_STRING_ID ? summary_msg : detailed_msg, lengthof(this->decode_params));
00128     if (textref_stack_size > 0) {
00129       MemCpyT(this->textref_stack, textref_stack, textref_stack_size);
00130     }
00131 
00132     CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
00133     this->face = (this->detailed_msg == STR_ERROR_OWNED_BY && company < MAX_COMPANIES) ? company : INVALID_COMPANY;
00134 
00135     assert(summary_msg != INVALID_STRING_ID);
00136   }
00137 };
00138 
00140 typedef std::list<ErrorMessageData> ErrorList;
00142 ErrorList _errors;
00144 bool _window_system_initialized = false;
00145 
00147 struct ErrmsgWindow : public Window, ErrorMessageData {
00148 private:
00149   uint height_summary;            
00150   uint height_detailed;           
00151 
00152 public:
00153   ErrmsgWindow(const ErrorMessageData &data) : Window(), ErrorMessageData(data)
00154   {
00155     this->InitNested((this->face == INVALID_COMPANY) ? &_errmsg_desc : &_errmsg_face_desc);
00156   }
00157 
00158   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00159   {
00160     if (widget != EMW_MESSAGE) return;
00161 
00162     CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00163     if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
00164 
00165     int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
00166     this->height_summary  = GetStringHeight(this->summary_msg, text_width);
00167     this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
00168 
00169     if (this->textref_stack_size > 0) StopTextRefStackUsage();
00170 
00171     uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
00172     if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
00173 
00174     size->height = max(size->height, panel_height);
00175   }
00176 
00177   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00178   {
00179     /* Position (0, 0) given, center the window. */
00180     if (this->position.x == 0 && this->position.y == 0) {
00181       Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
00182       return pt;
00183     }
00184 
00185     /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
00186      * Add a fixed distance 20 to make it less cluttered.
00187      */
00188     int scr_top = GetMainViewTop() + 20;
00189     int scr_bot = GetMainViewBottom() - 20;
00190 
00191     Point pt = RemapCoords2(this->position.x, this->position.y);
00192     const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
00193     if (this->face == INVALID_COMPANY) {
00194       /* move x pos to opposite corner */
00195       pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
00196       pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20; // Stay 20 pixels away from the edge of the screen.
00197 
00198       /* move y pos to opposite corner */
00199       pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
00200       pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
00201     } else {
00202       pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2),  0, _screen.width  - sm_width);
00203       pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top,  vp->zoom) + vp->top  - (sm_height / 2), scr_top, scr_bot - sm_height);
00204     }
00205     return pt;
00206   }
00207 
00213   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00214   {
00215     /* If company gets shut down, while displaying an error about it, remove the error message. */
00216     if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
00217   }
00218 
00219   virtual void SetStringParameters(int widget) const
00220   {
00221     if (widget == EMW_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00222   }
00223 
00224   virtual void DrawWidget(const Rect &r, int widget) const
00225   {
00226     switch (widget) {
00227       case EMW_FACE: {
00228         const Company *c = Company::Get(this->face);
00229         DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
00230         break;
00231       }
00232 
00233       case EMW_MESSAGE:
00234         CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00235         if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
00236 
00237         if (this->detailed_msg == INVALID_STRING_ID) {
00238           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00239               this->summary_msg, TC_FROMSTRING, SA_CENTER);
00240         } else {
00241           int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
00242 
00243           /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
00244           int top = r.top + WD_FRAMERECT_TOP;
00245           int bottom = top + this->height_summary + extra;
00246           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
00247 
00248           bottom = r.bottom - WD_FRAMERECT_BOTTOM;
00249           top = bottom - this->height_detailed - extra;
00250           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
00251         }
00252 
00253         if (this->textref_stack_size > 0) StopTextRefStackUsage();
00254         break;
00255 
00256       default:
00257         break;
00258     }
00259   }
00260 
00261   virtual void OnMouseLoop()
00262   {
00263     /* Disallow closing the window too easily, if timeout is disabled */
00264     if (_right_button_down && this->duration != 0) delete this;
00265   }
00266 
00267   virtual void OnHundredthTick()
00268   {
00269     /* Timeout enabled? */
00270     if (this->duration != 0) {
00271       this->duration--;
00272       if (this->duration == 0) delete this;
00273     }
00274   }
00275 
00276   ~ErrmsgWindow()
00277   {
00278     SetRedErrorSquare(INVALID_TILE);
00279     if (_window_system_initialized) ShowFirstError();
00280   }
00281 
00282   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00283   {
00284     if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
00285     delete this;
00286     return ES_HANDLED;
00287   }
00288 
00293   bool IsCritical()
00294   {
00295     return this->duration == 0;
00296   }
00297 };
00298 
00302 void ClearErrorMessages()
00303 {
00304   _errors.clear();
00305 }
00306 
00308 void ShowFirstError()
00309 {
00310   _window_system_initialized = true;
00311   if (!_errors.empty()) {
00312     new ErrmsgWindow(_errors.front());
00313     _errors.pop_front();
00314   }
00315 }
00316 
00322 void UnshowCriticalError()
00323 {
00324   ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00325   if (w != NULL) {
00326     if (w->IsCritical()) _errors.push_front(*w);
00327     _window_system_initialized = false;
00328     delete w;
00329   }
00330 }
00331 
00342 void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y, uint textref_stack_size, const uint32 *textref_stack)
00343 {
00344   assert(textref_stack_size == 0 || textref_stack != NULL);
00345   if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
00346 
00347   if (wl != WL_INFO) {
00348     /* Print message to console */
00349     char buf[DRAW_STRING_BUFFER];
00350 
00351     if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_size, textref_stack);
00352 
00353     char *b = GetString(buf, summary_msg, lastof(buf));
00354     if (detailed_msg != INVALID_STRING_ID) {
00355       b += seprintf(b, lastof(buf), " ");
00356       GetString(b, detailed_msg, lastof(buf));
00357     }
00358 
00359     if (textref_stack_size > 0) StopTextRefStackUsage();
00360 
00361     switch (wl) {
00362       case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
00363       default:         IConsoleError(buf); break;
00364     }
00365   }
00366 
00367   bool no_timeout = wl == WL_CRITICAL;
00368 
00369   if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
00370 
00371   ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_size, textref_stack);
00372 
00373   ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00374   if (w != NULL && w->IsCritical()) {
00375     /* A critical error is currently shown. */
00376     if (wl == WL_CRITICAL) {
00377       /* Push another critical error in the queue of errors,
00378        * but do not put other errors in the queue. */
00379       _errors.push_back(data);
00380     }
00381   } else {
00382     /* Nothing or a non-critical error was shown. */
00383     delete w;
00384     new ErrmsgWindow(data);
00385   }
00386 }