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 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 if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_size, textref_stack);
00123 CopyOutDParam(this->decode_params, this->strings, detailed_msg == INVALID_STRING_ID ? summary_msg : detailed_msg, lengthof(this->decode_params));
00124 if (textref_stack_size > 0) {
00125 StopTextRefStackUsage();
00126 MemCpyT(this->textref_stack, textref_stack, textref_stack_size);
00127 }
00128
00129 CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
00130 this->face = (this->detailed_msg == STR_ERROR_OWNED_BY && company < MAX_COMPANIES) ? company : INVALID_COMPANY;
00131
00132 assert(summary_msg != INVALID_STRING_ID);
00133 }
00134 };
00135
00137 typedef std::list<ErrorMessageData> ErrorList;
00139 ErrorList _error_list;
00141 bool _window_system_initialized = false;
00142
00144 struct ErrmsgWindow : public Window, ErrorMessageData {
00145 private:
00146 uint height_summary;
00147 uint height_detailed;
00148
00149 public:
00150 ErrmsgWindow(const ErrorMessageData &data) : Window(), ErrorMessageData(data)
00151 {
00152 this->InitNested((this->face == INVALID_COMPANY) ? &_errmsg_desc : &_errmsg_face_desc);
00153 }
00154
00155 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00156 {
00157 if (widget != WID_EM_MESSAGE) return;
00158
00159 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00160 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
00161
00162 int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
00163 this->height_summary = GetStringHeight(this->summary_msg, text_width);
00164 this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
00165
00166 if (this->textref_stack_size > 0) StopTextRefStackUsage();
00167
00168 uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
00169 if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
00170
00171 size->height = max(size->height, panel_height);
00172 }
00173
00174 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00175 {
00176
00177 if (this->position.x == 0 && this->position.y == 0) {
00178 Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
00179 return pt;
00180 }
00181
00182
00183
00184
00185 int scr_top = GetMainViewTop() + 20;
00186 int scr_bot = GetMainViewBottom() - 20;
00187
00188 Point pt = RemapCoords2(this->position.x, this->position.y);
00189 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
00190 if (this->face == INVALID_COMPANY) {
00191
00192 pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
00193 pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20;
00194
00195
00196 pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
00197 pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
00198 } else {
00199 pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2), 0, _screen.width - sm_width);
00200 pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top - (sm_height / 2), scr_top, scr_bot - sm_height);
00201 }
00202 return pt;
00203 }
00204
00210 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00211 {
00212
00213 if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
00214 }
00215
00216 virtual void SetStringParameters(int widget) const
00217 {
00218 if (widget == WID_EM_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00219 }
00220
00221 virtual void DrawWidget(const Rect &r, int widget) const
00222 {
00223 switch (widget) {
00224 case WID_EM_FACE: {
00225 const Company *c = Company::Get(this->face);
00226 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
00227 break;
00228 }
00229
00230 case WID_EM_MESSAGE:
00231 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00232 if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_size, this->textref_stack);
00233
00234 if (this->detailed_msg == INVALID_STRING_ID) {
00235 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00236 this->summary_msg, TC_FROMSTRING, SA_CENTER);
00237 } else {
00238 int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
00239
00240
00241 int top = r.top + WD_FRAMERECT_TOP;
00242 int bottom = top + this->height_summary + extra;
00243 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
00244
00245 bottom = r.bottom - WD_FRAMERECT_BOTTOM;
00246 top = bottom - this->height_detailed - extra;
00247 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
00248 }
00249
00250 if (this->textref_stack_size > 0) StopTextRefStackUsage();
00251 break;
00252
00253 default:
00254 break;
00255 }
00256 }
00257
00258 virtual void OnMouseLoop()
00259 {
00260
00261 if (_right_button_down && this->duration != 0) delete this;
00262 }
00263
00264 virtual void OnHundredthTick()
00265 {
00266
00267 if (this->duration != 0) {
00268 this->duration--;
00269 if (this->duration == 0) delete this;
00270 }
00271 }
00272
00273 ~ErrmsgWindow()
00274 {
00275 SetRedErrorSquare(INVALID_TILE);
00276 if (_window_system_initialized) ShowFirstError();
00277 }
00278
00279 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00280 {
00281 if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
00282 delete this;
00283 return ES_HANDLED;
00284 }
00285
00290 bool IsCritical()
00291 {
00292 return this->duration == 0;
00293 }
00294 };
00295
00299 void ClearErrorMessages()
00300 {
00301 UnshowCriticalError();
00302 _error_list.clear();
00303 }
00304
00306 void ShowFirstError()
00307 {
00308 _window_system_initialized = true;
00309 if (!_error_list.empty()) {
00310 new ErrmsgWindow(_error_list.front());
00311 _error_list.pop_front();
00312 }
00313 }
00314
00320 void UnshowCriticalError()
00321 {
00322 ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00323 if (_window_system_initialized && w != NULL) {
00324 if (w->IsCritical()) _error_list.push_front(*w);
00325 _window_system_initialized = false;
00326 delete w;
00327 }
00328 }
00329
00340 void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y, uint textref_stack_size, const uint32 *textref_stack)
00341 {
00342 assert(textref_stack_size == 0 || textref_stack != NULL);
00343 if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
00344
00345 if (wl != WL_INFO) {
00346
00347 char buf[DRAW_STRING_BUFFER];
00348
00349 if (textref_stack_size > 0) StartTextRefStackUsage(textref_stack_size, textref_stack);
00350
00351 char *b = GetString(buf, summary_msg, lastof(buf));
00352 if (detailed_msg != INVALID_STRING_ID) {
00353 b += seprintf(b, lastof(buf), " ");
00354 GetString(b, detailed_msg, lastof(buf));
00355 }
00356
00357 if (textref_stack_size > 0) StopTextRefStackUsage();
00358
00359 switch (wl) {
00360 case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
00361 default: IConsoleError(buf); break;
00362 }
00363 }
00364
00365 bool no_timeout = wl == WL_CRITICAL;
00366
00367 if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
00368
00369 ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_size, textref_stack);
00370
00371 ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);
00372 if (w != NULL && w->IsCritical()) {
00373
00374 if (wl == WL_CRITICAL) {
00375
00376
00377 _error_list.push_back(data);
00378 }
00379 } else {
00380
00381 delete w;
00382 new ErrmsgWindow(data);
00383 }
00384 }