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 }
00618
00619 virtual void OnResize()
00620 {
00621 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00622 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00623 }
00624
00625 virtual void SetStringParameters(int widget) const
00626 {
00627 switch (widget) {
00628 case SNGRFS_PRESET_LIST:
00629 if (this->preset == -1) {
00630 SetDParam(0, STR_NUM_CUSTOM);
00631 } else {
00632 SetDParam(0, STR_JUST_RAW_STRING);
00633 SetDParamStr(1, _grf_preset_list[this->preset]);
00634 }
00635 break;
00636 }
00637 }
00638
00639 virtual void OnPaint()
00640 {
00641 this->DrawWidgets();
00642 }
00643
00644 virtual void DrawWidget(const Rect &r, int widget) const
00645 {
00646 switch (widget) {
00647 case SNGRFS_FILE_LIST: {
00648 uint y = r.top + WD_MATRIX_TOP;
00649 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 - 1;
00650
00651 bool rtl = _dynlang.text_dir == TD_RTL;
00652 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + 25;
00653 uint text_right = rtl ? r.right - 25 : r.right - WD_FRAMERECT_RIGHT;
00654 uint square_left = rtl ? r.right - 15 : r.left + 5;
00655 uint warning_left = rtl ? r.right - 30 : r.left + 20;
00656
00657 int i = 0;
00658 for (const GRFConfig *c = this->list; c != NULL; c = c->next, i++) {
00659 if (this->vscroll.IsVisible(i)) {
00660 const char *text = (!StrEmpty(c->name)) ? c->name : c->filename;
00661 PaletteID pal;
00662
00663
00664 switch (c->status) {
00665 case GCS_NOT_FOUND:
00666 case GCS_DISABLED:
00667 pal = PALETTE_TO_RED;
00668 break;
00669 case GCS_ACTIVATED:
00670 pal = PALETTE_TO_GREEN;
00671 break;
00672 default:
00673 pal = PALETTE_TO_BLUE;
00674 break;
00675 }
00676
00677
00678 if (pal != PALETTE_TO_RED) {
00679 if (HasBit(c->flags, GCF_STATIC)) {
00680 pal = PALETTE_TO_GREY;
00681 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
00682 pal = PALETTE_TO_ORANGE;
00683 }
00684 }
00685
00686 DrawSprite(SPR_SQUARE, pal, square_left, y + sprite_offset_y);
00687 if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + sprite_offset_y);
00688 uint txtoffset = c->error == NULL ? 0 : 10;
00689 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y, text, this->sel == c ? TC_WHITE : TC_BLACK);
00690 y += this->resize.step_height;
00691 }
00692 }
00693 } break;
00694
00695 case SNGRFS_NEWGRF_INFO:
00696 if (this->sel != NULL) {
00697 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);
00698 }
00699 break;
00700 }
00701 }
00702
00703 virtual void OnClick(Point pt, int widget, int click_count)
00704 {
00705 switch (widget) {
00706 case SNGRFS_PRESET_LIST: {
00707 DropDownList *list = new DropDownList();
00708
00709
00710 list->push_back(new DropDownListStringItem(STR_NONE, -1, false));
00711
00712 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00713 if (_grf_preset_list[i] != NULL) {
00714 list->push_back(new DropDownListPresetItem(i));
00715 }
00716 }
00717
00718 ShowDropDownList(this, list, this->preset, SNGRFS_PRESET_LIST);
00719 break;
00720 }
00721
00722 case SNGRFS_PRESET_SAVE:
00723 this->query_widget = widget;
00724 ShowQueryString(STR_EMPTY, STR_NEWGRF_SETTINGS_PRESET_SAVE_QUERY, 32, 100, this, CS_ALPHANUMERAL, QSF_NONE);
00725 break;
00726
00727 case SNGRFS_PRESET_DELETE:
00728 if (this->preset == -1) return;
00729
00730 DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
00731 GetGRFPresetList(&_grf_preset_list);
00732 this->preset = -1;
00733 this->InvalidateData();
00734 break;
00735
00736 case SNGRFS_ADD:
00737 DeleteWindowByClass(WC_SAVELOAD);
00738 new NewGRFAddWindow(&_newgrf_add_dlg_desc, this, &this->list);
00739 break;
00740
00741 case SNGRFS_REMOVE: {
00742 GRFConfig **pc, *c, *newsel;
00743
00744
00745 newsel = this->sel->next;
00746
00747 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) {
00748
00749
00750 if (newsel == NULL && c->next == this->sel) newsel = c;
00751
00752 if (c == this->sel) {
00753 *pc = c->next;
00754 free(c);
00755 break;
00756 }
00757 }
00758
00759 this->sel = newsel;
00760 this->preset = -1;
00761 this->InvalidateData(3);
00762 this->DeleteChildWindows(WC_QUERY_STRING);
00763 break;
00764 }
00765
00766 case SNGRFS_MOVE_UP: {
00767 GRFConfig **pc, *c;
00768 if (this->sel == NULL) break;
00769
00770 int pos = 0;
00771 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next, pos++) {
00772 if (c->next == this->sel) {
00773 c->next = this->sel->next;
00774 this->sel->next = c;
00775 *pc = this->sel;
00776 break;
00777 }
00778 }
00779 this->vscroll.ScrollTowards(pos);
00780 this->preset = -1;
00781 this->InvalidateData();
00782 break;
00783 }
00784
00785 case SNGRFS_MOVE_DOWN: {
00786 GRFConfig **pc, *c;
00787 if (this->sel == NULL) break;
00788
00789 int pos = 1;
00790 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next, pos++) {
00791 if (c == this->sel) {
00792 *pc = c->next;
00793 c->next = c->next->next;
00794 (*pc)->next = c;
00795 break;
00796 }
00797 }
00798 this->vscroll.ScrollTowards(pos);
00799 this->preset = -1;
00800 this->InvalidateData();
00801 break;
00802 }
00803
00804 case SNGRFS_FILE_LIST: {
00805 GRFConfig *c;
00806 uint i = (pt.y - this->GetWidget<NWidgetBase>(SNGRFS_FILE_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
00807
00808 for (c = this->list; c != NULL && i > 0; c = c->next, i--) {}
00809
00810 if (this->sel != c) this->DeleteChildWindows(WC_QUERY_STRING);
00811 this->sel = c;
00812
00813 this->InvalidateData();
00814 if (click_count > 1) this->OnClick(pt, SNGRFS_SET_PARAMETERS, 1);
00815 break;
00816 }
00817
00818 case SNGRFS_APPLY_CHANGES:
00819 if (this->execute) {
00820 ShowQuery(
00821 STR_NEWGRF_POPUP_CAUTION_CAPTION,
00822 STR_NEWGRF_CONFIRMATION_TEXT,
00823 this,
00824 NewGRFConfirmationCallback
00825 );
00826 } else {
00827 CopyGRFConfigList(this->orig_list, this->list, true);
00828 ResetGRFConfig(false);
00829 ReloadNewGRFData();
00830 }
00831 break;
00832
00833 case SNGRFS_SET_PARAMETERS: {
00834 if (this->sel == NULL) break;
00835
00836 this->query_widget = widget;
00837 static char buff[512];
00838 GRFBuildParamList(buff, this->sel, lastof(buff));
00839 SetDParamStr(0, buff);
00840 ShowQueryString(STR_JUST_RAW_STRING, STR_NEWGRF_SETTINGS_PARAMETER_QUERY, 63, 250, this, CS_NUMERAL_SPACE, QSF_NONE);
00841 break;
00842 }
00843
00844 case SNGRFS_TOGGLE_PALETTE:
00845 if (this->sel != NULL) {
00846 this->sel->windows_paletted ^= true;
00847 this->SetDirty();
00848 }
00849 break;
00850
00851 case SNGRFS_CONTENT_DOWNLOAD:
00852 if (!_network_available) {
00853 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, 0, 0);
00854 } else {
00855 #if defined(ENABLE_NETWORK)
00856
00857 ContentVector cv;
00858 for (const GRFConfig *c = this->list; c != NULL; c = c->next) {
00859 if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
00860
00861 ContentInfo *ci = new ContentInfo();
00862 ci->type = CONTENT_TYPE_NEWGRF;
00863 ci->state = ContentInfo::DOES_NOT_EXIST;
00864 ttd_strlcpy(ci->name, c->name != NULL ? c->name : c->filename, lengthof(ci->name));
00865 ci->unique_id = BSWAP32(c->grfid);
00866 memcpy(ci->md5sum, c->md5sum, sizeof(ci->md5sum));
00867 if (HasBit(c->flags, GCF_COMPATIBLE)) GamelogGetOriginalGRFMD5Checksum(c->grfid, ci->md5sum);
00868 *cv.Append() = ci;
00869 }
00870 ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
00871 #endif
00872 }
00873 break;
00874 }
00875 }
00876
00877 virtual void OnDropdownSelect(int widget, int index)
00878 {
00879 if (index == -1) {
00880 ClearGRFConfigList(&this->list);
00881 this->preset = -1;
00882 } else {
00883 GRFConfig *c = LoadGRFPresetFromConfig(_grf_preset_list[index]);
00884
00885 if (c != NULL) {
00886 this->sel = NULL;
00887 ClearGRFConfigList(&this->list);
00888 this->list = c;
00889 this->preset = index;
00890 }
00891 }
00892
00893 this->sel = NULL;
00894 this->InvalidateData(3);
00895 }
00896
00897 virtual void OnQueryTextFinished(char *str)
00898 {
00899 if (str == NULL) return;
00900
00901 switch (this->query_widget) {
00902 default: NOT_REACHED();
00903
00904 case SNGRFS_PRESET_SAVE:
00905 SaveGRFPresetToConfig(str, this->list);
00906 GetGRFPresetList(&_grf_preset_list);
00907
00908
00909 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00910 if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
00911 this->preset = i;
00912 break;
00913 }
00914 }
00915 break;
00916
00917 case SNGRFS_SET_PARAMETERS: {
00918
00919 GRFConfig *c = this->sel;
00920 c->num_params = parse_intlist(str, (int*)c->param, lengthof(c->param));
00921
00922
00923 if (c->num_params == (byte)-1) c->num_params = 0;
00924
00925 this->preset = -1;
00926 break;
00927 }
00928 }
00929
00930 this->InvalidateData();
00931 }
00932
00933 virtual void OnInvalidateData(int data = 0)
00934 {
00935 switch (data) {
00936 default: NOT_REACHED();
00937 case 0:
00938
00939 break;
00940
00941 case 1:
00942
00943 for (GRFConfig *c = this->list; c != NULL; c = c->next) {
00944 if (c->status != GCS_NOT_FOUND) continue;
00945
00946 const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00947 if (f == NULL) continue;
00948
00949 free(c->filename);
00950 free(c->name);
00951 free(c->info);
00952
00953 c->filename = f->filename == NULL ? NULL : strdup(f->filename);
00954 c->name = f->name == NULL ? NULL : strdup(f->name);
00955 c->info = f->info == NULL ? NULL : strdup(f->info);
00956 c->status = GCS_UNKNOWN;
00957 }
00958 break;
00959
00960 case 2:
00961 this->preset = -1;
00962
00963 case 3:
00964 const GRFConfig *c;
00965 int i;
00966
00967 for (c = this->list, i = 0; c != NULL; c = c->next, i++) {}
00968
00969 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00970 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00971 this->vscroll.SetCount(i);
00972 break;
00973 }
00974
00975 this->SetWidgetsDisabledState(!this->editable,
00976 SNGRFS_PRESET_LIST,
00977 SNGRFS_ADD,
00978 SNGRFS_APPLY_CHANGES,
00979 SNGRFS_TOGGLE_PALETTE,
00980 WIDGET_LIST_END
00981 );
00982
00983 bool disable_all = this->sel == NULL || !this->editable;
00984 this->SetWidgetsDisabledState(disable_all,
00985 SNGRFS_REMOVE,
00986 SNGRFS_MOVE_UP,
00987 SNGRFS_MOVE_DOWN,
00988 WIDGET_LIST_END
00989 );
00990 this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all);
00991 this->SetWidgetDisabledState(SNGRFS_TOGGLE_PALETTE, disable_all);
00992
00993 if (!disable_all) {
00994
00995 if (this->sel == this->list) this->DisableWidget(SNGRFS_MOVE_UP);
00996 if (this->sel->next == NULL) this->DisableWidget(SNGRFS_MOVE_DOWN);
00997 if (this->sel->IsOpenTTDBaseGRF()) this->DisableWidget(SNGRFS_REMOVE);
00998 }
00999
01000 this->SetWidgetDisabledState(SNGRFS_PRESET_DELETE, this->preset == -1);
01001
01002 bool has_missing = false;
01003 bool has_compatible = false;
01004 for (const GRFConfig *c = this->list; !has_missing && c != NULL; c = c->next) {
01005 has_missing |= c->status == GCS_NOT_FOUND;
01006 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
01007 }
01008 if (has_missing || has_compatible) {
01009 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
01010 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
01011 } else {
01012 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_INTRO_ONLINE_CONTENT;
01013 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
01014 }
01015 this->SetWidgetDisabledState(SNGRFS_PRESET_SAVE, has_missing);
01016 }
01017 };
01018
01019
01020 static const NWidgetPart _nested_newgrf_widgets[] = {
01021 NWidget(NWID_HORIZONTAL),
01022 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01023 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01024 EndContainer(),
01025 NWidget(WWT_PANEL, COLOUR_MAUVE),
01026 NWidget(NWID_HORIZONTAL), SetPadding(2, 10, 2, 10),
01027 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, SNGRFS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
01028 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01029 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_SAVE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
01030 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_DELETE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
01031 EndContainer(),
01032 EndContainer(),
01033 EndContainer(),
01034 NWidget(WWT_PANEL, COLOUR_MAUVE),
01035 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 10, 2, 10),
01036 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_ADD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_TOOLTIP),
01037 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_REMOVE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
01038 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_UP), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
01039 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
01040 EndContainer(),
01041 EndContainer(),
01042 NWidget(NWID_HORIZONTAL),
01043 NWidget(WWT_MATRIX, COLOUR_MAUVE, SNGRFS_FILE_LIST), SetFill(1, 0), SetDataTip(0x501, STR_NEWGRF_SETTINGS_FILE_TOOLTIP), SetResize(1, 0),
01044 NWidget(WWT_SCROLLBAR, COLOUR_MAUVE, SNGRFS_SCROLLBAR),
01045 EndContainer(),
01046 NWidget(WWT_PANEL, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO), SetFill(1, 0), SetResize(1, 0), EndContainer(),
01047 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01048 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
01049 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),
01050 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
01051 EndContainer(),
01052 NWidget(NWID_HORIZONTAL),
01053 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01054 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01055 EndContainer(),
01056 };
01057
01058
01059 static const WindowDesc _newgrf_desc(
01060 WDP_CENTER, 300, 263,
01061 WC_GAME_OPTIONS, WC_NONE,
01062 WDF_UNCLICK_BUTTONS,
01063 _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
01064 );
01065
01070 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
01071 {
01072 if (confirmed) {
01073 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
01074 GRFConfig *c;
01075 int i = 0;
01076
01077 GamelogStartAction(GLAT_GRF);
01078 GamelogGRFUpdate(_grfconfig, nw->list);
01079 CopyGRFConfigList(nw->orig_list, nw->list, false);
01080 ReloadNewGRFData();
01081 GamelogStopAction();
01082
01083
01084 for (c = nw->list; c != NULL && c != nw->sel; c = c->next, i++) {}
01085 CopyGRFConfigList(&nw->list, *nw->orig_list, false);
01086 for (c = nw->list; c != NULL && i > 0; c = c->next, i--) {}
01087 nw->sel = c;
01088
01089 w->SetDirty();
01090 }
01091 }
01092
01093
01094
01101 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
01102 {
01103 DeleteWindowByClass(WC_GAME_OPTIONS);
01104 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
01105 }