00001
00002
00003
00004
00005
00006
00007
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 WindowDesc _errmsg_desc(
00043 WDP_MANUAL, "error", 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 WindowDesc _errmsg_face_desc(
00063 WDP_MANUAL, "error_face", 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
00101 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) :
00102 duration(duration),
00103 textref_stack_grffile(textref_stack_grffile),
00104 textref_stack_size(textref_stack_size),
00105 summary_msg(summary_msg),
00106 detailed_msg(detailed_msg),
00107 face(INVALID_COMPANY)
00108 {
00109 this->position.x = x;
00110 this->position.y = y;
00111
00112 memset(this->decode_params, 0, sizeof(this->decode_params));
00113 memset(this->strings, 0, sizeof(this->strings));
00114
00115 if (textref_stack_size > 0) MemCpyT(this->textref_stack, textref_stack, textref_stack_size);
00116
00117 assert(summary_msg != INVALID_STRING_ID);
00118 }
00119
00123 void ErrorMessageData::CopyOutDParams()
00124 {
00125
00126 for (size_t i = 0; i < lengthof(this->strings); i++) free(this->strings[i]);
00127 memset(this->decode_params, 0, sizeof(this->decode_params));
00128 memset(this->strings, 0, sizeof(this->strings));
00129
00130
00131 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack);
00132 CopyOutDParam(this->decode_params, this->strings, this->detailed_msg == INVALID_STRING_ID ? this->summary_msg : this->detailed_msg, lengthof(this->decode_params));
00133 if (this->textref_stack_size > 0) StopTextRefStackUsage();
00134
00135 if (this->detailed_msg == STR_ERROR_OWNED_BY) {
00136 CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
00137 if (company < MAX_COMPANIES) face = company;
00138 }
00139 }
00140
00146 void ErrorMessageData::SetDParam(uint n, uint64 v)
00147 {
00148 this->decode_params[n] = v;
00149 }
00150
00156 void ErrorMessageData::SetDParamStr(uint n, const char *str)
00157 {
00158 free(this->strings[n]);
00159 this->strings[n] = strdup(str);
00160 }
00161
00163 typedef std::list<ErrorMessageData> ErrorList;
00165 ErrorList _error_list;
00167 bool _window_system_initialized = false;
00168
00170 struct ErrmsgWindow : public Window, ErrorMessageData {
00171 private:
00172 uint height_summary;
00173 uint height_detailed;
00174
00175 public:
00176 ErrmsgWindow(const ErrorMessageData &data) : Window(data.HasFace() ? &_errmsg_face_desc : &_errmsg_desc), ErrorMessageData(data)
00177 {
00178 this->InitNested();
00179 }
00180
00181 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00182 {
00183 if (widget != WID_EM_MESSAGE) return;
00184
00185 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00186 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack);
00187
00188 int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
00189 this->height_summary = GetStringHeight(this->summary_msg, text_width);
00190 this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
00191
00192 if (this->textref_stack_size > 0) StopTextRefStackUsage();
00193
00194 uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
00195 if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
00196
00197 size->height = max(size->height, panel_height);
00198 }
00199
00200 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
00201 {
00202
00203 if (this->position.x == 0 && this->position.y == 0) {
00204 Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
00205 return pt;
00206 }
00207
00208
00209
00210
00211 int scr_top = GetMainViewTop() + 20;
00212 int scr_bot = GetMainViewBottom() - 20;
00213
00214 Point pt = RemapCoords2(this->position.x, this->position.y);
00215 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
00216 if (this->face == INVALID_COMPANY) {
00217
00218 pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
00219 pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20;
00220
00221
00222 pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
00223 pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
00224 } else {
00225 pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2), 0, _screen.width - sm_width);
00226 pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top - (sm_height / 2), scr_top, scr_bot - sm_height);
00227 }
00228 return pt;
00229 }
00230
00236 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00237 {
00238
00239 if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
00240 }
00241
00242 virtual void SetStringParameters(int widget) const
00243 {
00244 if (widget == WID_EM_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00245 }
00246
00247 virtual void DrawWidget(const Rect &r, int widget) const
00248 {
00249 switch (widget) {
00250 case WID_EM_FACE: {
00251 const Company *c = Company::Get(this->face);
00252 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
00253 break;
00254 }
00255
00256 case WID_EM_MESSAGE:
00257 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00258 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack);
00259
00260 if (this->detailed_msg == INVALID_STRING_ID) {
00261 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00262 this->summary_msg, TC_FROMSTRING, SA_CENTER);
00263 } else {
00264 int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
00265
00266
00267 int top = r.top + WD_FRAMERECT_TOP;
00268 int bottom = top + this->height_summary + extra;
00269 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
00270
00271 bottom = r.bottom - WD_FRAMERECT_BOTTOM;
00272 top = bottom - this->height_detailed - extra;
00273 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
00274 }
00275
00276 if (this->textref_stack_size > 0) StopTextRefStackUsage();
00277 break;
00278
00279 default:
00280 break;
00281 }
00282 }
00283
00284 virtual void OnMouseLoop()
00285 {
00286
00287 if (_right_button_down && this->duration != 0) delete this;
00288 }
00289
00290 virtual void OnHundredthTick()
00291 {
00292
00293 if (this->duration != 0) {
00294 this->duration--;
00295 if (this->duration == 0) delete this;
00296 }
00297 }
00298
00299 ~ErrmsgWindow()
00300 {
00301 SetRedErrorSquare(INVALID_TILE);
00302 if (_window_system_initialized) ShowFirstError();
00303 }
00304
00305 virtual EventState OnKeyPress(WChar key, uint16 keycode)
00306 {
00307 if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
00308 delete this;
00309 return ES_HANDLED;
00310 }
00311
00316 bool IsCritical()
00317 {
00318 return this->duration == 0;
00319 }
00320 };
00321
00325 void ClearErrorMessages()
00326 {
00327 UnshowCriticalError();
00328 _error_list.clear();
00329 }
00330
00332 void ShowFirstError()
00333 {
00334 _window_system_initialized = true;
00335 if (!_error_list.empty()) {
00336 new ErrmsgWindow(_error_list.front());
00337 _error_list.pop_front();
00338 }
00339 }
00340
00346 void UnshowCriticalError()
00347 {
00348 ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00349 if (_window_system_initialized && w != NULL) {
00350 if (w->IsCritical()) _error_list.push_front(*w);
00351 _window_system_initialized = false;
00352 delete w;
00353 }
00354 }
00355
00367 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)
00368 {
00369 assert(textref_stack_size == 0 || (textref_stack_grffile != NULL && textref_stack != NULL));
00370 if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
00371
00372 if (wl != WL_INFO) {
00373
00374 char buf[DRAW_STRING_BUFFER];
00375
00376 if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_grffile, textref_stack_size, textref_stack);
00377
00378 char *b = GetString(buf, summary_msg, lastof(buf));
00379 if (detailed_msg != INVALID_STRING_ID) {
00380 b += seprintf(b, lastof(buf), " ");
00381 GetString(b, detailed_msg, lastof(buf));
00382 }
00383
00384 if (textref_stack_size > 0) StopTextRefStackUsage();
00385
00386 switch (wl) {
00387 case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
00388 default: IConsoleError(buf); break;
00389 }
00390 }
00391
00392 bool no_timeout = wl == WL_CRITICAL;
00393
00394 if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
00395
00396 ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_grffile, textref_stack_size, textref_stack);
00397 data.CopyOutDParams();
00398
00399 ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00400 if (w != NULL && w->IsCritical()) {
00401
00402 if (wl == WL_CRITICAL) {
00403
00404
00405 _error_list.push_back(data);
00406 }
00407 } else {
00408
00409 delete w;
00410 new ErrmsgWindow(data);
00411 }
00412 }
00413
00419 void ScheduleErrorMessage(ErrorList &datas)
00420 {
00421 _error_list.splice(_error_list.end(), datas);
00422 }
00423
00429 void ScheduleErrorMessage(const ErrorMessageData &data)
00430 {
00431 _error_list.push_back(data);
00432 }