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