00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "gui.h"
00014 #include "newgrf.h"
00015 #include "strings_func.h"
00016 #include "window_func.h"
00017 #include "gamelog.h"
00018 #include "settings_func.h"
00019 #include "widgets/dropdown_type.h"
00020 #include "network/network.h"
00021 #include "network/network_content.h"
00022 #include "sortlist_type.h"
00023 #include "querystring_gui.h"
00024 #include "core/geometry_func.hpp"
00025
00026 #include "table/strings.h"
00027 #include "table/sprites.h"
00028
00032 void ShowNewGRFError()
00033 {
00034 for (const GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
00035
00036 if (c->error == NULL || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
00037
00038 SetDParam (0, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING);
00039 SetDParamStr(1, c->error->custom_message);
00040 SetDParam (2, STR_JUST_RAW_STRING);
00041 SetDParamStr(3, c->filename);
00042 SetDParam (4, STR_JUST_RAW_STRING);
00043 SetDParamStr(5, c->error->data);
00044 for (uint i = 0; i < c->error->num_params; i++) {
00045 SetDParam(6 + i, c->error->param_value[i]);
00046 }
00047 ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, 0, 0, true);
00048 break;
00049 }
00050 }
00051
00058 static int parse_intlist(const char *p, int *items, int maxitems)
00059 {
00060 int n = 0, v;
00061 char *end;
00062
00063 for (;;) {
00064 while (*p == ' ' || *p == ',') p++;
00065 if (*p == '\0') break;
00066 v = strtol(p, &end, 0);
00067 if (p == end || n == maxitems) return -1;
00068 p = end;
00069 items[n++] = v;
00070 }
00071
00072 return n;
00073 }
00074
00075
00076 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
00077 {
00078 char buff[256];
00079
00080 if (c->error != NULL) {
00081 char message[512];
00082 SetDParamStr(0, c->error->custom_message);
00083 SetDParam (1, STR_JUST_RAW_STRING);
00084 SetDParamStr(2, c->filename);
00085 SetDParam (3, STR_JUST_RAW_STRING);
00086 SetDParamStr(4, c->error->data);
00087 for (uint i = 0; i < c->error->num_params; i++) {
00088 SetDParam(5 + i, c->error->param_value[i]);
00089 }
00090 GetString(message, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
00091
00092 SetDParamStr(0, message);
00093 y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
00094 }
00095
00096
00097 if (c->filename != NULL) {
00098 SetDParamStr(0, c->filename);
00099 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
00100 }
00101
00102
00103 snprintf(buff, lengthof(buff), "%08X", BSWAP32(c->grfid));
00104 SetDParamStr(0, buff);
00105 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
00106
00107
00108 md5sumToString(buff, lastof(buff), c->md5sum);
00109 SetDParamStr(0, buff);
00110 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
00111
00112
00113 if (show_params) {
00114 if (c->num_params > 0) {
00115 GRFBuildParamList(buff, c, lastof(buff));
00116 SetDParam(0, STR_JUST_RAW_STRING);
00117 SetDParamStr(1, buff);
00118 } else {
00119 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
00120 }
00121 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
00122
00123
00124 SetDParamStr(0, c->windows_paletted ? "Windows" : "DOS");
00125 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
00126 }
00127
00128
00129 if (c->status == GCS_NOT_FOUND) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
00130 if (c->status == GCS_DISABLED) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
00131 if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
00132
00133
00134 if (c->info != NULL && !StrEmpty(c->info)) {
00135 SetDParam(0, STR_JUST_RAW_STRING);
00136 SetDParamStr(1, c->info);
00137 y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_STRING);
00138 } else {
00139 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
00140 }
00141 }
00142
00143
00145 enum AddNewGRFWindowWidgets {
00146 ANGRFW_FILTER,
00147 ANGRFW_GRF_LIST,
00148 ANGRFW_SCROLLBAR,
00149 ANGRFW_GRF_INFO,
00150 ANGRFW_ADD,
00151 ANGRFW_RESCAN,
00152 };
00153
00157 struct NewGRFAddWindow : public QueryStringBaseWindow {
00158 private:
00159 typedef GUIList<const GRFConfig *> GUIGRFConfigList;
00160
00161 GRFConfig **list;
00162
00164 static Listing last_sorting;
00165 static Filtering last_filtering;
00166
00167 static GUIGRFConfigList::SortFunction * const sorter_funcs[];
00168 static GUIGRFConfigList::FilterFunction * const filter_funcs[];
00169 GUIGRFConfigList grfs;
00170
00171 const GRFConfig *sel;
00172 int sel_pos;
00173
00174 enum {
00175 EDITBOX_MAX_SIZE = 50,
00176 EDITBOX_MAX_LENGTH = 300,
00177 };
00178
00183 void BuildGrfList()
00184 {
00185 if (!this->grfs.NeedRebuild()) return;
00186
00187
00188 this->grfs.Clear();
00189
00190 for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
00191 *this->grfs.Append() = c;
00192 }
00193
00194 this->FilterGrfList();
00195 this->grfs.Compact();
00196 this->grfs.RebuildDone();
00197 this->SortGrfList();
00198
00199 this->vscroll.SetCount(this->grfs.Length());
00200 this->ScrollToSelected();
00201 }
00202
00204 static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
00205 {
00206 const char *name_a = ((*a)->name != NULL) ? (*a)->name : "";
00207 const char *name_b = ((*b)->name != NULL) ? (*b)->name : "";
00208 int result = strcasecmp(name_a, name_b);
00209 if (result == 0) {
00210 result = strcasecmp((*a)->filename, (*b)->filename);
00211 }
00212 return result;
00213 }
00214
00216 void SortGrfList()
00217 {
00218 if (!this->grfs.Sort()) return;
00219 this->UpdateListPosition();
00220 }
00221
00223 void UpdateListPosition()
00224 {
00225
00226 if (this->sel != NULL) {
00227 this->sel_pos = this->grfs.FindIndex(this->sel);
00228 if (this->sel_pos < 0) {
00229 this->sel = NULL;
00230 }
00231 }
00232 }
00233
00235 static bool CDECL TagNameFilter(const GRFConfig * const *a, const char *filter_string)
00236 {
00237 if ((*a)->name != NULL && strcasestr((*a)->name, filter_string) != NULL) return true;
00238 if ((*a)->filename != NULL && strcasestr((*a)->filename, filter_string) != NULL) return true;
00239 if ((*a)->info != NULL && strcasestr((*a)->info, filter_string) != NULL) return true;
00240 return false;
00241 }
00242
00244 void FilterGrfList()
00245 {
00246 if (!this->grfs.Filter(this->edit_str_buf)) return;
00247 this->UpdateListPosition();
00248 }
00249
00251 void ScrollToSelected()
00252 {
00253 if (this->sel_pos >= 0) {
00254 this->vscroll.ScrollTowards(this->sel_pos);
00255 }
00256 }
00257
00258 public:
00259 NewGRFAddWindow(const WindowDesc *desc, Window *parent, GRFConfig **list) : QueryStringBaseWindow(EDITBOX_MAX_SIZE)
00260 {
00261 this->parent = parent;
00262 this->InitNested(desc, 0);
00263
00264 InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, EDITBOX_MAX_LENGTH);
00265 this->SetFocusedWidget(ANGRFW_FILTER);
00266
00267 this->list = list;
00268 this->grfs.SetListing(this->last_sorting);
00269 this->grfs.SetFiltering(this->last_filtering);
00270 this->grfs.SetSortFuncs(this->sorter_funcs);
00271 this->grfs.SetFilterFuncs(this->filter_funcs);
00272
00273 this->OnInvalidateData(0);
00274 }
00275
00276 virtual void OnInvalidateData(int data)
00277 {
00278
00279
00280 if (data == 0) {
00281 this->sel = NULL;
00282 this->sel_pos = -1;
00283 this->grfs.ForceRebuild();
00284 }
00285
00286 this->BuildGrfList();
00287 this->SetWidgetDisabledState(ANGRFW_ADD, this->sel == NULL || this->sel->IsOpenTTDBaseGRF());
00288 }
00289
00290 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00291 {
00292 switch (widget) {
00293 case ANGRFW_GRF_LIST:
00294 resize->height = FONT_HEIGHT_NORMAL;
00295 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * resize->height + WD_FRAMERECT_BOTTOM + padding.height + 2);
00296 break;
00297
00298 case ANGRFW_GRF_INFO:
00299 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
00300 break;
00301 }
00302 }
00303
00304 virtual void OnResize()
00305 {
00306 this->vscroll.SetCapacityFromWidget(this, ANGRFW_GRF_LIST);
00307 }
00308
00309 virtual void OnPaint()
00310 {
00311 this->DrawWidgets();
00312 this->DrawEditBox(ANGRFW_FILTER);
00313 }
00314
00315 virtual void DrawWidget(const Rect &r, int widget) const
00316 {
00317 switch (widget) {
00318 case ANGRFW_GRF_LIST: {
00319 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0xD7);
00320
00321 uint y = r.top + WD_FRAMERECT_TOP;
00322 uint min_index = this->vscroll.GetPosition();
00323 uint max_index = min(min_index + this->vscroll.GetCapacity(), this->grfs.Length());
00324
00325 for (uint i = min_index; i < max_index; i++)
00326 {
00327 const GRFConfig *c = this->grfs[i];
00328 bool h = c == this->sel;
00329 const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename;
00330
00331
00332 if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + this->resize.step_height - 1, 156);
00333 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, text, h ? TC_WHITE : TC_ORANGE);
00334 y += this->resize.step_height;
00335 }
00336 break;
00337 }
00338
00339 case ANGRFW_GRF_INFO:
00340 if (this->sel != NULL) {
00341 ShowNewGRFInfo(this->sel, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, r.right - WD_FRAMERECT_RIGHT, r.bottom - WD_FRAMERECT_BOTTOM, false);
00342 }
00343 break;
00344 }
00345 }
00346
00347 virtual void OnDoubleClick(Point pt, int widget)
00348 {
00349 if (widget == ANGRFW_GRF_LIST) this->OnClick(pt, ANGRFW_ADD);
00350 }
00351
00352 virtual void OnClick(Point pt, int widget)
00353 {
00354 switch (widget) {
00355 case ANGRFW_GRF_LIST: {
00356
00357 uint i = (pt.y - this->GetWidget<NWidgetBase>(ANGRFW_GRF_LIST)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height + this->vscroll.GetPosition();
00358
00359 if (i < this->grfs.Length()) {
00360 this->sel = this->grfs[i];
00361 this->sel_pos = i;
00362 } else {
00363 this->sel = NULL;
00364 this->sel_pos = -1;
00365 }
00366 this->InvalidateData(1);
00367 break;
00368 }
00369
00370 case ANGRFW_ADD:
00371 if (this->sel != NULL) {
00372 const GRFConfig *src = this->sel;
00373 GRFConfig **list;
00374
00375
00376 for (list = this->list; *list != NULL; list = &(*list)->next) {
00377 if ((*list)->grfid == src->grfid) {
00378 ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, 0, 0);
00379 return;
00380 }
00381 }
00382
00383
00384 GRFConfig *c = DuplicateGRFConfig(src);
00385 c->next = NULL;
00386
00387
00388 *list = c;
00389
00390 DeleteWindowByClass(WC_SAVELOAD);
00391 InvalidateWindowData(WC_GAME_OPTIONS, 0, 2);
00392 }
00393 break;
00394
00395 case ANGRFW_RESCAN:
00396 ScanNewGRFFiles();
00397 this->InvalidateData();
00398 break;
00399 }
00400 }
00401
00402 virtual void OnMouseLoop()
00403 {
00404 this->HandleEditBox(ANGRFW_FILTER);
00405 }
00406
00407 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00408 {
00409 switch (keycode) {
00410 case WKC_UP:
00411
00412 if (this->sel_pos > 0) this->sel_pos--;
00413 break;
00414
00415 case WKC_DOWN:
00416
00417 if (this->sel_pos < (int)this->grfs.Length() - 1) this->sel_pos++;
00418 break;
00419
00420 case WKC_PAGEUP:
00421
00422 this->sel_pos = (this->sel_pos < this->vscroll.GetCapacity()) ? 0 : this->sel_pos - this->vscroll.GetCapacity();
00423 break;
00424
00425 case WKC_PAGEDOWN:
00426
00427 this->sel_pos = min(this->sel_pos + this->vscroll.GetCapacity(), (int)this->grfs.Length() - 1);
00428 break;
00429
00430 case WKC_HOME:
00431
00432 this->sel_pos = 0;
00433 break;
00434
00435 case WKC_END:
00436
00437 this->sel_pos = this->grfs.Length() - 1;
00438 break;
00439
00440 default: {
00441
00442 EventState state = ES_NOT_HANDLED;
00443 if (this->HandleEditBoxKey(ANGRFW_FILTER, key, keycode, state) == HEBR_EDITING) {
00444 this->OnOSKInput(ANGRFW_FILTER);
00445 }
00446 return state;
00447 }
00448 }
00449
00450 if (this->grfs.Length() == 0) this->sel_pos = -1;
00451 if (this->sel_pos >= 0) {
00452 this->sel = this->grfs[this->sel_pos];
00453
00454 this->ScrollToSelected();
00455 this->InvalidateData(1);
00456 }
00457
00458 return ES_HANDLED;
00459 }
00460
00461 virtual void OnOSKInput(int wid)
00462 {
00463 this->grfs.SetFilterState(!StrEmpty(this->edit_str_buf));
00464 this->grfs.ForceRebuild();
00465 this->InvalidateData(1);
00466 }
00467 };
00468
00469 Listing NewGRFAddWindow::last_sorting = {false, 0};
00470 Filtering NewGRFAddWindow::last_filtering = {false, 0};
00471
00472 NewGRFAddWindow::GUIGRFConfigList::SortFunction * const NewGRFAddWindow::sorter_funcs[] = {
00473 &NameSorter,
00474 };
00475
00476 NewGRFAddWindow::GUIGRFConfigList::FilterFunction * const NewGRFAddWindow::filter_funcs[] = {
00477 &TagNameFilter,
00478 };
00479
00480 static const NWidgetPart _nested_newgrf_add_dlg_widgets[] = {
00481 NWidget(NWID_HORIZONTAL),
00482 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00483 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_ADD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00484 EndContainer(),
00485 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0),
00486 NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
00487 NWidget(WWT_TEXT, COLOUR_GREY), SetFill(0, 1), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
00488 NWidget(WWT_EDITBOX, COLOUR_GREY, ANGRFW_FILTER), SetFill(1, 0), SetMinimalSize(100, 12), SetResize(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
00489 EndContainer(),
00490 EndContainer(),
00491 NWidget(NWID_HORIZONTAL),
00492 NWidget(WWT_PANEL, COLOUR_GREY),
00493 NWidget(WWT_INSET, COLOUR_GREY, ANGRFW_GRF_LIST), SetMinimalSize(290, 1), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
00494 EndContainer(),
00495 NWidget(WWT_SCROLLBAR, COLOUR_GREY, ANGRFW_SCROLLBAR),
00496 EndContainer(),
00497 NWidget(WWT_PANEL, COLOUR_GREY, ANGRFW_GRF_INFO), SetMinimalSize(306, 1), SetResize(1, 0), EndContainer(),
00498 NWidget(NWID_HORIZONTAL),
00499 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00500 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ANGRFW_ADD), SetMinimalSize(147, 12), SetResize(1, 0), SetDataTip(STR_NEWGRF_ADD_FILE, STR_NEWGRF_ADD_FILE_TOOLTIP),
00501 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ANGRFW_RESCAN), SetMinimalSize(147, 12), SetResize(1, 0), SetDataTip(STR_NEWGRF_ADD_RESCAN_FILES, STR_NEWGRF_ADD_RESCAN_FILES_TOOLTIP),
00502 EndContainer(),
00503 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00504 EndContainer(),
00505 };
00506
00507
00508 static const WindowDesc _newgrf_add_dlg_desc(
00509 WDP_CENTER, 306, 347,
00510 WC_SAVELOAD, WC_NONE,
00511 WDF_UNCLICK_BUTTONS,
00512 _nested_newgrf_add_dlg_widgets, lengthof(_nested_newgrf_add_dlg_widgets)
00513 );
00514
00515 static GRFPresetList _grf_preset_list;
00516
00517 class DropDownListPresetItem : public DropDownListItem {
00518 public:
00519 DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
00520
00521 virtual ~DropDownListPresetItem() {}
00522
00523 bool Selectable() const
00524 {
00525 return true;
00526 }
00527
00528 void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
00529 {
00530 DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
00531 }
00532 };
00533
00534 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
00535
00537 enum ShowNewGRFStateWidgets {
00538 SNGRFS_PRESET_LIST,
00539 SNGRFS_PRESET_SAVE,
00540 SNGRFS_PRESET_DELETE,
00541 SNGRFS_ADD,
00542 SNGRFS_REMOVE,
00543 SNGRFS_MOVE_UP,
00544 SNGRFS_MOVE_DOWN,
00545 SNGRFS_FILE_LIST,
00546 SNGRFS_SCROLLBAR,
00547 SNGRFS_NEWGRF_INFO,
00548 SNGRFS_SET_PARAMETERS,
00549 SNGRFS_TOGGLE_PALETTE,
00550 SNGRFS_APPLY_CHANGES,
00551 SNGRFS_CONTENT_DOWNLOAD,
00552 };
00553
00557 struct NewGRFWindow : public Window {
00558 GRFConfig **orig_list;
00559 GRFConfig *list;
00560 GRFConfig *sel;
00561 bool editable;
00562 bool show_params;
00563 bool execute;
00564 int query_widget;
00565 int preset;
00566
00567 NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool exec_changes, GRFConfig **config) : Window()
00568 {
00569 this->sel = NULL;
00570 this->list = NULL;
00571 this->orig_list = config;
00572 this->editable = editable;
00573 this->execute = exec_changes;
00574 this->show_params = show_params;
00575 this->preset = -1;
00576
00577 CopyGRFConfigList(&this->list, *config, false);
00578 GetGRFPresetList(&_grf_preset_list);
00579
00580 this->InitNested(desc);
00581 this->OnInvalidateData(2);
00582 }
00583
00584 ~NewGRFWindow()
00585 {
00586 if (this->editable && !this->execute) {
00587 CopyGRFConfigList(this->orig_list, this->list, true);
00588 ResetGRFConfig(false);
00589 ReloadNewGRFData();
00590 }
00591
00592
00593 ClearGRFConfigList(&this->list);
00594 _grf_preset_list.Clear();
00595 }
00596
00597 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00598 {
00599 switch (widget) {
00600 case SNGRFS_FILE_LIST:
00601 resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00602 size->height = max(size->height, 7 * resize->height);
00603 break;
00604
00605 case SNGRFS_NEWGRF_INFO:
00606 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
00607 break;
00608
00609 case SNGRFS_PRESET_LIST: {
00610 Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
00611 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00612 if (_grf_preset_list[i] != NULL) {
00613 SetDParamStr(0, _grf_preset_list[i]);
00614 d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
00615 }
00616 }
00617 d.width += padding.width;
00618 *size = maxdim(d, *size);
00619 break;
00620 }
00621 }
00622 }
00623
00624 virtual void OnResize()
00625 {
00626 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00627 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00628 }
00629
00630 virtual void SetStringParameters(int widget) const
00631 {
00632 switch (widget) {
00633 case SNGRFS_PRESET_LIST:
00634 if (this->preset == -1) {
00635 SetDParam(0, STR_NUM_CUSTOM);
00636 } else {
00637 SetDParam(0, STR_JUST_RAW_STRING);
00638 SetDParamStr(1, _grf_preset_list[this->preset]);
00639 }
00640 break;
00641 }
00642 }
00643
00644 virtual void OnPaint()
00645 {
00646 this->DrawWidgets();
00647 }
00648
00649 virtual void DrawWidget(const Rect &r, int widget) const
00650 {
00651 switch (widget) {
00652 case SNGRFS_FILE_LIST: {
00653 uint y = r.top + WD_MATRIX_TOP;
00654 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 - 1;
00655
00656 bool rtl = _dynlang.text_dir == TD_RTL;
00657 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + 25;
00658 uint text_right = rtl ? r.right - 25 : r.right - WD_FRAMERECT_RIGHT;
00659 uint square_left = rtl ? r.right - 15 : r.left + 5;
00660 uint warning_left = rtl ? r.right - 30 : r.left + 20;
00661
00662 int i = 0;
00663 for (const GRFConfig *c = this->list; c != NULL; c = c->next, i++) {
00664 if (this->vscroll.IsVisible(i)) {
00665 const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename;
00666 SpriteID pal;
00667
00668
00669 switch (c->status) {
00670 case GCS_NOT_FOUND:
00671 case GCS_DISABLED:
00672 pal = PALETTE_TO_RED;
00673 break;
00674 case GCS_ACTIVATED:
00675 pal = PALETTE_TO_GREEN;
00676 break;
00677 default:
00678 pal = PALETTE_TO_BLUE;
00679 break;
00680 }
00681
00682
00683 if (pal != PALETTE_TO_RED) {
00684 if (HasBit(c->flags, GCF_STATIC)) {
00685 pal = PALETTE_TO_GREY;
00686 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
00687 pal = PALETTE_TO_ORANGE;
00688 }
00689 }
00690
00691 DrawSprite(SPR_SQUARE, pal, square_left, y + sprite_offset_y);
00692 if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + sprite_offset_y);
00693 uint txtoffset = c->error == NULL ? 0 : 10;
00694 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y, text, this->sel == c ? TC_WHITE : TC_BLACK);
00695 y += this->resize.step_height;
00696 }
00697 }
00698 } break;
00699
00700 case SNGRFS_NEWGRF_INFO:
00701 if (this->sel != NULL) {
00702 ShowNewGRFInfo(this->sel, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, r.right - WD_FRAMERECT_RIGHT, r.bottom - WD_FRAMERECT_BOTTOM, this->show_params);
00703 }
00704 break;
00705 }
00706 }
00707
00708 virtual void OnDoubleClick(Point pt, int widget)
00709 {
00710 if (widget == SNGRFS_FILE_LIST) this->OnClick(pt, SNGRFS_SET_PARAMETERS);
00711 }
00712
00713 virtual void OnClick(Point pt, int widget)
00714 {
00715 switch (widget) {
00716 case SNGRFS_PRESET_LIST: {
00717 DropDownList *list = new DropDownList();
00718
00719
00720 list->push_back(new DropDownListStringItem(STR_NONE, -1, false));
00721
00722 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00723 if (_grf_preset_list[i] != NULL) {
00724 list->push_back(new DropDownListPresetItem(i));
00725 }
00726 }
00727
00728 ShowDropDownList(this, list, this->preset, SNGRFS_PRESET_LIST);
00729 break;
00730 }
00731
00732 case SNGRFS_PRESET_SAVE:
00733 this->query_widget = widget;
00734 ShowQueryString(STR_EMPTY, STR_NEWGRF_SETTINGS_PRESET_SAVE_QUERY, 32, 100, this, CS_ALPHANUMERAL, QSF_NONE);
00735 break;
00736
00737 case SNGRFS_PRESET_DELETE:
00738 if (this->preset == -1) return;
00739
00740 DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
00741 GetGRFPresetList(&_grf_preset_list);
00742 this->preset = -1;
00743 this->InvalidateData();
00744 break;
00745
00746 case SNGRFS_ADD:
00747 DeleteWindowByClass(WC_SAVELOAD);
00748 new NewGRFAddWindow(&_newgrf_add_dlg_desc, this, &this->list);
00749 break;
00750
00751 case SNGRFS_REMOVE: {
00752 GRFConfig **pc, *c, *newsel;
00753
00754
00755 newsel = this->sel->next;
00756
00757 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) {
00758
00759
00760 if (newsel == NULL && c->next == this->sel) newsel = c;
00761
00762 if (c == this->sel) {
00763 *pc = c->next;
00764 free(c);
00765 break;
00766 }
00767 }
00768
00769 this->sel = newsel;
00770 this->preset = -1;
00771 this->InvalidateData();
00772 this->DeleteChildWindows(WC_QUERY_STRING);
00773 break;
00774 }
00775
00776 case SNGRFS_MOVE_UP: {
00777 GRFConfig **pc, *c;
00778 if (this->sel == NULL) break;
00779
00780 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) {
00781 if (c->next == this->sel) {
00782 c->next = this->sel->next;
00783 this->sel->next = c;
00784 *pc = this->sel;
00785 break;
00786 }
00787 }
00788 this->preset = -1;
00789 this->InvalidateData();
00790 break;
00791 }
00792
00793 case SNGRFS_MOVE_DOWN: {
00794 GRFConfig **pc, *c;
00795 if (this->sel == NULL) break;
00796
00797 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) {
00798 if (c == this->sel) {
00799 *pc = c->next;
00800 c->next = c->next->next;
00801 (*pc)->next = c;
00802 break;
00803 }
00804 }
00805 this->preset = -1;
00806 this->InvalidateData();
00807 break;
00808 }
00809
00810 case SNGRFS_FILE_LIST: {
00811 GRFConfig *c;
00812 uint i = (pt.y - this->GetWidget<NWidgetBase>(SNGRFS_FILE_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
00813
00814 for (c = this->list; c != NULL && i > 0; c = c->next, i--) {}
00815
00816 if (this->sel != c) this->DeleteChildWindows(WC_QUERY_STRING);
00817 this->sel = c;
00818
00819 this->InvalidateData();
00820 break;
00821 }
00822
00823 case SNGRFS_APPLY_CHANGES:
00824 if (this->execute) {
00825 ShowQuery(
00826 STR_NEWGRF_POPUP_CAUTION_CAPTION,
00827 STR_NEWGRF_CONFIRMATION_TEXT,
00828 this,
00829 NewGRFConfirmationCallback
00830 );
00831 } else {
00832 CopyGRFConfigList(this->orig_list, this->list, true);
00833 ResetGRFConfig(false);
00834 ReloadNewGRFData();
00835 }
00836 break;
00837
00838 case SNGRFS_SET_PARAMETERS: {
00839 if (this->sel == NULL) break;
00840
00841 this->query_widget = widget;
00842 static char buff[512];
00843 GRFBuildParamList(buff, this->sel, lastof(buff));
00844 SetDParamStr(0, buff);
00845 ShowQueryString(STR_JUST_RAW_STRING, STR_NEWGRF_SETTINGS_PARAMETER_QUERY, 63, 250, this, CS_NUMERAL_SPACE, QSF_NONE);
00846 break;
00847 }
00848
00849 case SNGRFS_TOGGLE_PALETTE:
00850 if (this->sel != NULL) {
00851 this->sel->windows_paletted ^= true;
00852 this->SetDirty();
00853 }
00854 break;
00855
00856 case SNGRFS_CONTENT_DOWNLOAD:
00857 if (!_network_available) {
00858 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, 0, 0);
00859 } else {
00860 #if defined(ENABLE_NETWORK)
00861
00862 ContentVector cv;
00863 for (const GRFConfig *c = this->list; c != NULL; c = c->next) {
00864 if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
00865
00866 ContentInfo *ci = new ContentInfo();
00867 ci->type = CONTENT_TYPE_NEWGRF;
00868 ci->state = ContentInfo::DOES_NOT_EXIST;
00869 ttd_strlcpy(ci->name, c->name != NULL ? c->name : c->filename, lengthof(ci->name));
00870 ci->unique_id = BSWAP32(c->grfid);
00871 memcpy(ci->md5sum, c->md5sum, sizeof(ci->md5sum));
00872 if (HasBit(c->flags, GCF_COMPATIBLE)) GamelogGetOriginalGRFMD5Checksum(c->grfid, ci->md5sum);
00873 *cv.Append() = ci;
00874 }
00875 ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
00876 #endif
00877 }
00878 break;
00879 }
00880 }
00881
00882 virtual void OnDropdownSelect(int widget, int index)
00883 {
00884 if (index == -1) {
00885 ClearGRFConfigList(&this->list);
00886 this->preset = -1;
00887 } else {
00888 GRFConfig *c = LoadGRFPresetFromConfig(_grf_preset_list[index]);
00889
00890 if (c != NULL) {
00891 this->sel = NULL;
00892 ClearGRFConfigList(&this->list);
00893 this->list = c;
00894 this->preset = index;
00895 }
00896 }
00897
00898 this->sel = NULL;
00899 this->InvalidateData(3);
00900 }
00901
00902 virtual void OnQueryTextFinished(char *str)
00903 {
00904 if (str == NULL) return;
00905
00906 switch (this->query_widget) {
00907 default: NOT_REACHED();
00908
00909 case SNGRFS_PRESET_SAVE:
00910 SaveGRFPresetToConfig(str, this->list);
00911 GetGRFPresetList(&_grf_preset_list);
00912
00913
00914 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00915 if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
00916 this->preset = i;
00917 break;
00918 }
00919 }
00920 break;
00921
00922 case SNGRFS_SET_PARAMETERS: {
00923
00924 GRFConfig *c = this->sel;
00925 c->num_params = parse_intlist(str, (int*)c->param, lengthof(c->param));
00926
00927
00928 if (c->num_params == (byte)-1) c->num_params = 0;
00929
00930 this->preset = -1;
00931 break;
00932 }
00933 }
00934
00935 this->InvalidateData();
00936 }
00937
00938 virtual void OnInvalidateData(int data = 0)
00939 {
00940 switch (data) {
00941 default: NOT_REACHED();
00942 case 0:
00943
00944 break;
00945
00946 case 1:
00947
00948 for (GRFConfig *c = this->list; c != NULL; c = c->next) {
00949 if (c->status != GCS_NOT_FOUND) continue;
00950
00951 const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00952 if (f == NULL) continue;
00953
00954 free(c->filename);
00955 free(c->name);
00956 free(c->info);
00957
00958 c->filename = f->filename == NULL ? NULL : strdup(f->filename);
00959 c->name = f->name == NULL ? NULL : strdup(f->name);;
00960 c->info = f->info == NULL ? NULL : strdup(f->info);;
00961 c->status = GCS_UNKNOWN;
00962 }
00963 break;
00964
00965 case 2:
00966 this->preset = -1;
00967
00968 case 3:
00969 const GRFConfig *c;
00970 int i;
00971
00972 for (c = this->list, i = 0; c != NULL; c = c->next, i++) {}
00973
00974 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00975 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00976 this->vscroll.SetCount(i);
00977 break;
00978 }
00979
00980 this->SetWidgetsDisabledState(!this->editable,
00981 SNGRFS_PRESET_LIST,
00982 SNGRFS_ADD,
00983 SNGRFS_APPLY_CHANGES,
00984 SNGRFS_TOGGLE_PALETTE,
00985 WIDGET_LIST_END
00986 );
00987
00988 bool disable_all = this->sel == NULL || !this->editable;
00989 this->SetWidgetsDisabledState(disable_all,
00990 SNGRFS_REMOVE,
00991 SNGRFS_MOVE_UP,
00992 SNGRFS_MOVE_DOWN,
00993 WIDGET_LIST_END
00994 );
00995 this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all);
00996 this->SetWidgetDisabledState(SNGRFS_TOGGLE_PALETTE, disable_all);
00997
00998 if (!disable_all) {
00999
01000 if (this->sel == this->list) this->DisableWidget(SNGRFS_MOVE_UP);
01001 if (this->sel->next == NULL) this->DisableWidget(SNGRFS_MOVE_DOWN);
01002 if (this->sel->IsOpenTTDBaseGRF()) this->DisableWidget(SNGRFS_REMOVE);
01003 }
01004
01005 this->SetWidgetDisabledState(SNGRFS_PRESET_DELETE, this->preset == -1);
01006
01007 bool has_missing = false;
01008 bool has_compatible = false;
01009 for (const GRFConfig *c = this->list; !has_missing && c != NULL; c = c->next) {
01010 has_missing |= c->status == GCS_NOT_FOUND;
01011 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
01012 }
01013 if (has_missing || has_compatible) {
01014 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
01015 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
01016 } else {
01017 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_INTRO_ONLINE_CONTENT;
01018 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
01019 }
01020 this->SetWidgetDisabledState(SNGRFS_PRESET_SAVE, has_missing);
01021 }
01022 };
01023
01024
01025 static const NWidgetPart _nested_newgrf_widgets[] = {
01026 NWidget(NWID_HORIZONTAL),
01027 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01028 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01029 EndContainer(),
01030 NWidget(WWT_PANEL, COLOUR_MAUVE),
01031 NWidget(NWID_HORIZONTAL), SetPadding(2, 10, 2, 10),
01032 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, SNGRFS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
01033 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01034 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_SAVE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
01035 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_DELETE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
01036 EndContainer(),
01037 EndContainer(),
01038 EndContainer(),
01039 NWidget(WWT_PANEL, COLOUR_MAUVE),
01040 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 10, 2, 10),
01041 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_ADD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_TOOLTIP),
01042 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_REMOVE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
01043 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_UP), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
01044 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
01045 EndContainer(),
01046 EndContainer(),
01047 NWidget(NWID_HORIZONTAL),
01048 NWidget(WWT_MATRIX, COLOUR_MAUVE, SNGRFS_FILE_LIST), SetFill(1, 0), SetDataTip(0x501, STR_NEWGRF_SETTINGS_FILE_TOOLTIP), SetResize(1, 0),
01049 NWidget(WWT_SCROLLBAR, COLOUR_MAUVE, SNGRFS_SCROLLBAR),
01050 EndContainer(),
01051 NWidget(WWT_PANEL, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO), SetFill(1, 0), SetResize(1, 0), EndContainer(),
01052 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01053 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
01054 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
01055 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
01056 EndContainer(),
01057 NWidget(NWID_HORIZONTAL),
01058 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01059 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01060 EndContainer(),
01061 };
01062
01063
01064 static const WindowDesc _newgrf_desc(
01065 WDP_CENTER, 300, 263,
01066 WC_GAME_OPTIONS, WC_NONE,
01067 WDF_UNCLICK_BUTTONS,
01068 _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
01069 );
01070
01075 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
01076 {
01077 if (confirmed) {
01078 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
01079 GRFConfig *c;
01080 int i = 0;
01081
01082 GamelogStartAction(GLAT_GRF);
01083 GamelogGRFUpdate(_grfconfig, nw->list);
01084 CopyGRFConfigList(nw->orig_list, nw->list, false);
01085 ReloadNewGRFData();
01086 GamelogStopAction();
01087
01088
01089 for (c = nw->list; c != NULL && c != nw->sel; c = c->next, i++) {}
01090 CopyGRFConfigList(&nw->list, *nw->orig_list, false);
01091 for (c = nw->list; c != NULL && i > 0; c = c->next, i--) {}
01092 nw->sel = c;
01093
01094 w->SetDirty();
01095 }
01096 }
01097
01098
01099
01106 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
01107 {
01108 DeleteWindowByClass(WC_GAME_OPTIONS);
01109 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
01110 }