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 (!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 = (!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 OnClick(Point pt, int widget, int click_count)
00348 {
00349 switch (widget) {
00350 case ANGRFW_GRF_LIST: {
00351
00352 uint i = (pt.y - this->GetWidget<NWidgetBase>(ANGRFW_GRF_LIST)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height + this->vscroll.GetPosition();
00353
00354 if (i < this->grfs.Length()) {
00355 this->sel = this->grfs[i];
00356 this->sel_pos = i;
00357 } else {
00358 this->sel = NULL;
00359 this->sel_pos = -1;
00360 }
00361 this->InvalidateData(1);
00362 if (click_count == 1) break;
00363 }
00364
00365 case ANGRFW_ADD:
00366 if (this->sel != NULL) {
00367 const GRFConfig *src = this->sel;
00368 GRFConfig **list;
00369
00370
00371 for (list = this->list; *list != NULL; list = &(*list)->next) {
00372 if ((*list)->grfid == src->grfid) {
00373 ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, 0, 0);
00374 return;
00375 }
00376 }
00377
00378
00379 GRFConfig *c = DuplicateGRFConfig(src);
00380 c->next = NULL;
00381
00382
00383 *list = c;
00384
00385 DeleteWindowByClass(WC_SAVELOAD);
00386 InvalidateWindowData(WC_GAME_OPTIONS, 0, 2);
00387 }
00388 break;
00389
00390 case ANGRFW_RESCAN:
00391 ScanNewGRFFiles();
00392 this->InvalidateData();
00393 break;
00394 }
00395 }
00396
00397 virtual void OnMouseLoop()
00398 {
00399 this->HandleEditBox(ANGRFW_FILTER);
00400 }
00401
00402 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00403 {
00404 switch (keycode) {
00405 case WKC_UP:
00406
00407 if (this->sel_pos > 0) this->sel_pos--;
00408 break;
00409
00410 case WKC_DOWN:
00411
00412 if (this->sel_pos < (int)this->grfs.Length() - 1) this->sel_pos++;
00413 break;
00414
00415 case WKC_PAGEUP:
00416
00417 this->sel_pos = (this->sel_pos < this->vscroll.GetCapacity()) ? 0 : this->sel_pos - this->vscroll.GetCapacity();
00418 break;
00419
00420 case WKC_PAGEDOWN:
00421
00422 this->sel_pos = min(this->sel_pos + this->vscroll.GetCapacity(), (int)this->grfs.Length() - 1);
00423 break;
00424
00425 case WKC_HOME:
00426
00427 this->sel_pos = 0;
00428 break;
00429
00430 case WKC_END:
00431
00432 this->sel_pos = this->grfs.Length() - 1;
00433 break;
00434
00435 default: {
00436
00437 EventState state = ES_NOT_HANDLED;
00438 if (this->HandleEditBoxKey(ANGRFW_FILTER, key, keycode, state) == HEBR_EDITING) {
00439 this->OnOSKInput(ANGRFW_FILTER);
00440 }
00441 return state;
00442 }
00443 }
00444
00445 if (this->grfs.Length() == 0) this->sel_pos = -1;
00446 if (this->sel_pos >= 0) {
00447 this->sel = this->grfs[this->sel_pos];
00448
00449 this->ScrollToSelected();
00450 this->InvalidateData(1);
00451 }
00452
00453 return ES_HANDLED;
00454 }
00455
00456 virtual void OnOSKInput(int wid)
00457 {
00458 this->grfs.SetFilterState(!StrEmpty(this->edit_str_buf));
00459 this->grfs.ForceRebuild();
00460 this->InvalidateData(1);
00461 }
00462 };
00463
00464 Listing NewGRFAddWindow::last_sorting = {false, 0};
00465 Filtering NewGRFAddWindow::last_filtering = {false, 0};
00466
00467 NewGRFAddWindow::GUIGRFConfigList::SortFunction * const NewGRFAddWindow::sorter_funcs[] = {
00468 &NameSorter,
00469 };
00470
00471 NewGRFAddWindow::GUIGRFConfigList::FilterFunction * const NewGRFAddWindow::filter_funcs[] = {
00472 &TagNameFilter,
00473 };
00474
00475 static const NWidgetPart _nested_newgrf_add_dlg_widgets[] = {
00476 NWidget(NWID_HORIZONTAL),
00477 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00478 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_ADD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00479 EndContainer(),
00480 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0),
00481 NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
00482 NWidget(WWT_TEXT, COLOUR_GREY), SetFill(0, 1), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
00483 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),
00484 EndContainer(),
00485 EndContainer(),
00486 NWidget(NWID_HORIZONTAL),
00487 NWidget(WWT_PANEL, COLOUR_GREY),
00488 NWidget(WWT_INSET, COLOUR_GREY, ANGRFW_GRF_LIST), SetMinimalSize(290, 1), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
00489 EndContainer(),
00490 NWidget(WWT_SCROLLBAR, COLOUR_GREY, ANGRFW_SCROLLBAR),
00491 EndContainer(),
00492 NWidget(WWT_PANEL, COLOUR_GREY, ANGRFW_GRF_INFO), SetMinimalSize(306, 1), SetResize(1, 0), EndContainer(),
00493 NWidget(NWID_HORIZONTAL),
00494 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00495 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ANGRFW_ADD), SetMinimalSize(147, 12), SetResize(1, 0), SetDataTip(STR_NEWGRF_ADD_FILE, STR_NEWGRF_ADD_FILE_TOOLTIP),
00496 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),
00497 EndContainer(),
00498 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00499 EndContainer(),
00500 };
00501
00502
00503 static const WindowDesc _newgrf_add_dlg_desc(
00504 WDP_CENTER, 306, 347,
00505 WC_SAVELOAD, WC_NONE,
00506 WDF_UNCLICK_BUTTONS,
00507 _nested_newgrf_add_dlg_widgets, lengthof(_nested_newgrf_add_dlg_widgets)
00508 );
00509
00510 static GRFPresetList _grf_preset_list;
00511
00512 class DropDownListPresetItem : public DropDownListItem {
00513 public:
00514 DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
00515
00516 virtual ~DropDownListPresetItem() {}
00517
00518 bool Selectable() const
00519 {
00520 return true;
00521 }
00522
00523 void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
00524 {
00525 DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
00526 }
00527 };
00528
00529 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
00530
00532 enum ShowNewGRFStateWidgets {
00533 SNGRFS_PRESET_LIST,
00534 SNGRFS_PRESET_SAVE,
00535 SNGRFS_PRESET_DELETE,
00536 SNGRFS_ADD,
00537 SNGRFS_REMOVE,
00538 SNGRFS_MOVE_UP,
00539 SNGRFS_MOVE_DOWN,
00540 SNGRFS_FILE_LIST,
00541 SNGRFS_SCROLLBAR,
00542 SNGRFS_NEWGRF_INFO,
00543 SNGRFS_SET_PARAMETERS,
00544 SNGRFS_TOGGLE_PALETTE,
00545 SNGRFS_APPLY_CHANGES,
00546 SNGRFS_CONTENT_DOWNLOAD,
00547 };
00548
00552 struct NewGRFWindow : public Window {
00553 GRFConfig **orig_list;
00554 GRFConfig *list;
00555 GRFConfig *sel;
00556 bool editable;
00557 bool show_params;
00558 bool execute;
00559 int query_widget;
00560 int preset;
00561
00562 NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool exec_changes, GRFConfig **config) : Window()
00563 {
00564 this->sel = NULL;
00565 this->list = NULL;
00566 this->orig_list = config;
00567 this->editable = editable;
00568 this->execute = exec_changes;
00569 this->show_params = show_params;
00570 this->preset = -1;
00571
00572 CopyGRFConfigList(&this->list, *config, false);
00573 GetGRFPresetList(&_grf_preset_list);
00574
00575 this->InitNested(desc);
00576 this->OnInvalidateData(2);
00577 }
00578
00579 ~NewGRFWindow()
00580 {
00581 if (this->editable && !this->execute) {
00582 CopyGRFConfigList(this->orig_list, this->list, true);
00583 ResetGRFConfig(false);
00584 ReloadNewGRFData();
00585 }
00586
00587
00588 ClearGRFConfigList(&this->list);
00589 _grf_preset_list.Clear();
00590 }
00591
00592 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00593 {
00594 switch (widget) {
00595 case SNGRFS_FILE_LIST:
00596 resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00597 size->height = max(size->height, 7 * resize->height);
00598 break;
00599
00600 case SNGRFS_NEWGRF_INFO:
00601 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
00602 break;
00603
00604 case SNGRFS_PRESET_LIST: {
00605 Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
00606 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00607 if (_grf_preset_list[i] != NULL) {
00608 SetDParamStr(0, _grf_preset_list[i]);
00609 d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
00610 }
00611 }
00612 d.width += padding.width;
00613 *size = maxdim(d, *size);
00614 break;
00615 }
00616
00617 case SNGRFS_CONTENT_DOWNLOAD: {
00618 Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
00619 *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
00620 size->width += padding.width;
00621 size->height += padding.height;
00622 break;
00623 }
00624 }
00625 }
00626
00627 virtual void OnResize()
00628 {
00629 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00630 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00631 }
00632
00633 virtual void SetStringParameters(int widget) const
00634 {
00635 switch (widget) {
00636 case SNGRFS_PRESET_LIST:
00637 if (this->preset == -1) {
00638 SetDParam(0, STR_NUM_CUSTOM);
00639 } else {
00640 SetDParam(0, STR_JUST_RAW_STRING);
00641 SetDParamStr(1, _grf_preset_list[this->preset]);
00642 }
00643 break;
00644 }
00645 }
00646
00647 virtual void OnPaint()
00648 {
00649 this->DrawWidgets();
00650 }
00651
00652 virtual void DrawWidget(const Rect &r, int widget) const
00653 {
00654 switch (widget) {
00655 case SNGRFS_FILE_LIST: {
00656 uint y = r.top + WD_MATRIX_TOP;
00657 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 - 1;
00658
00659 bool rtl = _dynlang.text_dir == TD_RTL;
00660 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + 25;
00661 uint text_right = rtl ? r.right - 25 : r.right - WD_FRAMERECT_RIGHT;
00662 uint square_left = rtl ? r.right - 15 : r.left + 5;
00663 uint warning_left = rtl ? r.right - 30 : r.left + 20;
00664
00665 int i = 0;
00666 for (const GRFConfig *c = this->list; c != NULL; c = c->next, i++) {
00667 if (this->vscroll.IsVisible(i)) {
00668 const char *text = (!StrEmpty(c->name)) ? c->name : c->filename;
00669 PaletteID pal;
00670
00671
00672 switch (c->status) {
00673 case GCS_NOT_FOUND:
00674 case GCS_DISABLED:
00675 pal = PALETTE_TO_RED;
00676 break;
00677 case GCS_ACTIVATED:
00678 pal = PALETTE_TO_GREEN;
00679 break;
00680 default:
00681 pal = PALETTE_TO_BLUE;
00682 break;
00683 }
00684
00685
00686 if (pal != PALETTE_TO_RED) {
00687 if (HasBit(c->flags, GCF_STATIC)) {
00688 pal = PALETTE_TO_GREY;
00689 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
00690 pal = PALETTE_TO_ORANGE;
00691 }
00692 }
00693
00694 DrawSprite(SPR_SQUARE, pal, square_left, y + sprite_offset_y);
00695 if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + sprite_offset_y);
00696 uint txtoffset = c->error == NULL ? 0 : 10;
00697 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y, text, this->sel == c ? TC_WHITE : TC_BLACK);
00698 y += this->resize.step_height;
00699 }
00700 }
00701 } break;
00702
00703 case SNGRFS_NEWGRF_INFO:
00704 if (this->sel != NULL) {
00705 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);
00706 }
00707 break;
00708 }
00709 }
00710
00711 virtual void OnClick(Point pt, int widget, int click_count)
00712 {
00713 switch (widget) {
00714 case SNGRFS_PRESET_LIST: {
00715 DropDownList *list = new DropDownList();
00716
00717
00718 list->push_back(new DropDownListStringItem(STR_NONE, -1, false));
00719
00720 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00721 if (_grf_preset_list[i] != NULL) {
00722 list->push_back(new DropDownListPresetItem(i));
00723 }
00724 }
00725
00726 ShowDropDownList(this, list, this->preset, SNGRFS_PRESET_LIST);
00727 break;
00728 }
00729
00730 case SNGRFS_PRESET_SAVE:
00731 this->query_widget = widget;
00732 ShowQueryString(STR_EMPTY, STR_NEWGRF_SETTINGS_PRESET_SAVE_QUERY, 32, 100, this, CS_ALPHANUMERAL, QSF_NONE);
00733 break;
00734
00735 case SNGRFS_PRESET_DELETE:
00736 if (this->preset == -1) return;
00737
00738 DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
00739 GetGRFPresetList(&_grf_preset_list);
00740 this->preset = -1;
00741 this->InvalidateData();
00742 break;
00743
00744 case SNGRFS_ADD:
00745 DeleteWindowByClass(WC_SAVELOAD);
00746 new NewGRFAddWindow(&_newgrf_add_dlg_desc, this, &this->list);
00747 break;
00748
00749 case SNGRFS_REMOVE: {
00750 GRFConfig **pc, *c, *newsel;
00751
00752
00753 newsel = this->sel->next;
00754
00755 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) {
00756
00757
00758 if (newsel == NULL && c->next == this->sel) newsel = c;
00759
00760 if (c == this->sel) {
00761 *pc = c->next;
00762 free(c);
00763 break;
00764 }
00765 }
00766
00767 this->sel = newsel;
00768 this->preset = -1;
00769 this->InvalidateData(3);
00770 this->DeleteChildWindows(WC_QUERY_STRING);
00771 break;
00772 }
00773
00774 case SNGRFS_MOVE_UP: {
00775 GRFConfig **pc, *c;
00776 if (this->sel == NULL) break;
00777
00778 int pos = 0;
00779 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next, pos++) {
00780 if (c->next == this->sel) {
00781 c->next = this->sel->next;
00782 this->sel->next = c;
00783 *pc = this->sel;
00784 break;
00785 }
00786 }
00787 this->vscroll.ScrollTowards(pos);
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 int pos = 1;
00798 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next, pos++) {
00799 if (c == this->sel) {
00800 *pc = c->next;
00801 c->next = c->next->next;
00802 (*pc)->next = c;
00803 break;
00804 }
00805 }
00806 this->vscroll.ScrollTowards(pos);
00807 this->preset = -1;
00808 this->InvalidateData();
00809 break;
00810 }
00811
00812 case SNGRFS_FILE_LIST: {
00813 GRFConfig *c;
00814 uint i = (pt.y - this->GetWidget<NWidgetBase>(SNGRFS_FILE_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
00815
00816 for (c = this->list; c != NULL && i > 0; c = c->next, i--) {}
00817
00818 if (this->sel != c) this->DeleteChildWindows(WC_QUERY_STRING);
00819 this->sel = c;
00820
00821 this->InvalidateData();
00822 if (click_count > 1) this->OnClick(pt, SNGRFS_SET_PARAMETERS, 1);
00823 break;
00824 }
00825
00826 case SNGRFS_APPLY_CHANGES:
00827 if (this->execute) {
00828 ShowQuery(
00829 STR_NEWGRF_POPUP_CAUTION_CAPTION,
00830 STR_NEWGRF_CONFIRMATION_TEXT,
00831 this,
00832 NewGRFConfirmationCallback
00833 );
00834 } else {
00835 CopyGRFConfigList(this->orig_list, this->list, true);
00836 ResetGRFConfig(false);
00837 ReloadNewGRFData();
00838 }
00839 break;
00840
00841 case SNGRFS_SET_PARAMETERS: {
00842 if (this->sel == NULL) break;
00843
00844 this->query_widget = widget;
00845 static char buff[512];
00846 GRFBuildParamList(buff, this->sel, lastof(buff));
00847 SetDParamStr(0, buff);
00848 ShowQueryString(STR_JUST_RAW_STRING, STR_NEWGRF_SETTINGS_PARAMETER_QUERY, 63, 250, this, CS_NUMERAL_SPACE, QSF_NONE);
00849 break;
00850 }
00851
00852 case SNGRFS_TOGGLE_PALETTE:
00853 if (this->sel != NULL) {
00854 this->sel->windows_paletted ^= true;
00855 this->SetDirty();
00856 }
00857 break;
00858
00859 case SNGRFS_CONTENT_DOWNLOAD:
00860 if (!_network_available) {
00861 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, 0, 0);
00862 } else {
00863 #if defined(ENABLE_NETWORK)
00864
00865 ContentVector cv;
00866 for (const GRFConfig *c = this->list; c != NULL; c = c->next) {
00867 if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
00868
00869 ContentInfo *ci = new ContentInfo();
00870 ci->type = CONTENT_TYPE_NEWGRF;
00871 ci->state = ContentInfo::DOES_NOT_EXIST;
00872 ttd_strlcpy(ci->name, c->name != NULL ? c->name : c->filename, lengthof(ci->name));
00873 ci->unique_id = BSWAP32(c->grfid);
00874 memcpy(ci->md5sum, c->md5sum, sizeof(ci->md5sum));
00875 if (HasBit(c->flags, GCF_COMPATIBLE)) GamelogGetOriginalGRFMD5Checksum(c->grfid, ci->md5sum);
00876 *cv.Append() = ci;
00877 }
00878 ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
00879 #endif
00880 }
00881 break;
00882 }
00883 }
00884
00885 virtual void OnDropdownSelect(int widget, int index)
00886 {
00887 if (index == -1) {
00888 ClearGRFConfigList(&this->list);
00889 this->preset = -1;
00890 } else {
00891 GRFConfig *c = LoadGRFPresetFromConfig(_grf_preset_list[index]);
00892
00893 if (c != NULL) {
00894 this->sel = NULL;
00895 ClearGRFConfigList(&this->list);
00896 this->list = c;
00897 this->preset = index;
00898 }
00899 }
00900
00901 this->sel = NULL;
00902 this->InvalidateData(3);
00903 }
00904
00905 virtual void OnQueryTextFinished(char *str)
00906 {
00907 if (str == NULL) return;
00908
00909 switch (this->query_widget) {
00910 default: NOT_REACHED();
00911
00912 case SNGRFS_PRESET_SAVE:
00913 SaveGRFPresetToConfig(str, this->list);
00914 GetGRFPresetList(&_grf_preset_list);
00915
00916
00917 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00918 if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
00919 this->preset = i;
00920 break;
00921 }
00922 }
00923 break;
00924
00925 case SNGRFS_SET_PARAMETERS: {
00926
00927 GRFConfig *c = this->sel;
00928 c->num_params = parse_intlist(str, (int*)c->param, lengthof(c->param));
00929
00930
00931 if (c->num_params == (byte)-1) c->num_params = 0;
00932
00933 this->preset = -1;
00934 break;
00935 }
00936 }
00937
00938 this->InvalidateData();
00939 }
00940
00941 virtual void OnInvalidateData(int data = 0)
00942 {
00943 switch (data) {
00944 default: NOT_REACHED();
00945 case 0:
00946
00947 break;
00948
00949 case 1:
00950
00951 for (GRFConfig *c = this->list; c != NULL; c = c->next) {
00952 if (c->status != GCS_NOT_FOUND) continue;
00953
00954 const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00955 if (f == NULL) continue;
00956
00957 free(c->filename);
00958 free(c->name);
00959 free(c->info);
00960
00961 c->filename = f->filename == NULL ? NULL : strdup(f->filename);
00962 c->name = f->name == NULL ? NULL : strdup(f->name);
00963 c->info = f->info == NULL ? NULL : strdup(f->info);
00964 c->status = GCS_UNKNOWN;
00965 }
00966 break;
00967
00968 case 2:
00969 this->preset = -1;
00970
00971 case 3:
00972 const GRFConfig *c;
00973 int i;
00974
00975 for (c = this->list, i = 0; c != NULL; c = c->next, i++) {}
00976
00977 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00978 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00979 this->vscroll.SetCount(i);
00980 break;
00981 }
00982
00983 this->SetWidgetsDisabledState(!this->editable,
00984 SNGRFS_PRESET_LIST,
00985 SNGRFS_ADD,
00986 SNGRFS_APPLY_CHANGES,
00987 SNGRFS_TOGGLE_PALETTE,
00988 WIDGET_LIST_END
00989 );
00990
00991 bool disable_all = this->sel == NULL || !this->editable;
00992 this->SetWidgetsDisabledState(disable_all,
00993 SNGRFS_REMOVE,
00994 SNGRFS_MOVE_UP,
00995 SNGRFS_MOVE_DOWN,
00996 WIDGET_LIST_END
00997 );
00998 this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all);
00999 this->SetWidgetDisabledState(SNGRFS_TOGGLE_PALETTE, disable_all);
01000
01001 if (!disable_all) {
01002
01003 if (this->sel == this->list) this->DisableWidget(SNGRFS_MOVE_UP);
01004 if (this->sel->next == NULL) this->DisableWidget(SNGRFS_MOVE_DOWN);
01005 if (this->sel->IsOpenTTDBaseGRF()) this->DisableWidget(SNGRFS_REMOVE);
01006 }
01007
01008 this->SetWidgetDisabledState(SNGRFS_PRESET_DELETE, this->preset == -1);
01009
01010 bool has_missing = false;
01011 bool has_compatible = false;
01012 for (const GRFConfig *c = this->list; !has_missing && c != NULL; c = c->next) {
01013 has_missing |= c->status == GCS_NOT_FOUND;
01014 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
01015 }
01016 if (has_missing || has_compatible) {
01017 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
01018 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
01019 } else {
01020 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_INTRO_ONLINE_CONTENT;
01021 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
01022 }
01023 this->SetWidgetDisabledState(SNGRFS_PRESET_SAVE, has_missing);
01024 }
01025 };
01026
01027
01028 static const NWidgetPart _nested_newgrf_widgets[] = {
01029 NWidget(NWID_HORIZONTAL),
01030 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01031 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01032 EndContainer(),
01033 NWidget(WWT_PANEL, COLOUR_MAUVE),
01034 NWidget(NWID_HORIZONTAL), SetPadding(2, 10, 2, 10),
01035 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, SNGRFS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
01036 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01037 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_SAVE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
01038 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_DELETE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
01039 EndContainer(),
01040 EndContainer(),
01041 EndContainer(),
01042 NWidget(WWT_PANEL, COLOUR_MAUVE),
01043 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 10, 2, 10),
01044 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_ADD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_TOOLTIP),
01045 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_REMOVE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
01046 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_UP), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
01047 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
01048 EndContainer(),
01049 EndContainer(),
01050 NWidget(NWID_HORIZONTAL),
01051 NWidget(WWT_MATRIX, COLOUR_MAUVE, SNGRFS_FILE_LIST), SetFill(1, 0), SetDataTip(0x501, STR_NEWGRF_SETTINGS_FILE_TOOLTIP), SetResize(1, 0),
01052 NWidget(WWT_SCROLLBAR, COLOUR_MAUVE, SNGRFS_SCROLLBAR),
01053 EndContainer(),
01054 NWidget(WWT_PANEL, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO), SetFill(1, 0), SetResize(1, 0), EndContainer(),
01055 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01056 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
01057 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),
01058 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
01059 EndContainer(),
01060 NWidget(NWID_HORIZONTAL),
01061 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01062 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01063 EndContainer(),
01064 };
01065
01066
01067 static const WindowDesc _newgrf_desc(
01068 WDP_CENTER, 300, 263,
01069 WC_GAME_OPTIONS, WC_NONE,
01070 WDF_UNCLICK_BUTTONS,
01071 _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
01072 );
01073
01078 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
01079 {
01080 if (confirmed) {
01081 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
01082 GRFConfig *c;
01083 int i = 0;
01084
01085 GamelogStartAction(GLAT_GRF);
01086 GamelogGRFUpdate(_grfconfig, nw->list);
01087 CopyGRFConfigList(nw->orig_list, nw->list, false);
01088 ReloadNewGRFData();
01089 GamelogStopAction();
01090
01091
01092 for (c = nw->list; c != NULL && c != nw->sel; c = c->next, i++) {}
01093 CopyGRFConfigList(&nw->list, *nw->orig_list, false);
01094 for (c = nw->list; c != NULL && i > 0; c = c->next, i--) {}
01095 nw->sel = c;
01096
01097 w->SetDirty();
01098 }
01099 }
01100
01101
01102
01109 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
01110 {
01111 DeleteWindowByClass(WC_GAME_OPTIONS);
01112 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
01113 }