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
00220
00221 if (this->sel != NULL) {
00222 this->sel_pos = this->grfs.FindIndex(this->sel);
00223 if (this->sel_pos < 0) {
00224 this->sel = NULL;
00225 }
00226 }
00227 }
00228
00230 static bool CDECL TagNameFilter(const GRFConfig * const *a, const char *filter_string)
00231 {
00232 if ((*a)->name != NULL && strcasestr((*a)->name, filter_string) != NULL) return true;
00233 if ((*a)->filename != NULL && strcasestr((*a)->filename, filter_string) != NULL) return true;
00234 if ((*a)->info != NULL && strcasestr((*a)->info, filter_string) != NULL) return true;
00235 return false;
00236 }
00237
00239 void FilterGrfList()
00240 {
00241 if (!this->grfs.Filter(this->edit_str_buf)) return;
00242 }
00243
00245 void ScrollToSelected()
00246 {
00247 if (this->sel_pos >= 0) {
00248 this->vscroll.ScrollTowards(this->sel_pos);
00249 }
00250 }
00251
00252 public:
00253 NewGRFAddWindow(const WindowDesc *desc, Window *parent, GRFConfig **list) : QueryStringBaseWindow(EDITBOX_MAX_SIZE)
00254 {
00255 this->parent = parent;
00256 this->InitNested(desc, 0);
00257
00258 InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, EDITBOX_MAX_LENGTH);
00259 this->SetFocusedWidget(ANGRFW_FILTER);
00260
00261 this->list = list;
00262 this->grfs.SetListing(this->last_sorting);
00263 this->grfs.SetFiltering(this->last_filtering);
00264 this->grfs.SetSortFuncs(this->sorter_funcs);
00265 this->grfs.SetFilterFuncs(this->filter_funcs);
00266
00267 this->OnInvalidateData(0);
00268 }
00269
00270 virtual void OnInvalidateData(int data)
00271 {
00272
00273
00274 if (data == 0) {
00275 this->sel = NULL;
00276 this->sel_pos = -1;
00277 this->grfs.ForceRebuild();
00278 }
00279
00280 this->BuildGrfList();
00281 this->SetWidgetDisabledState(ANGRFW_ADD, this->sel == NULL || this->sel->IsOpenTTDBaseGRF());
00282 }
00283
00284 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00285 {
00286 switch (widget) {
00287 case ANGRFW_GRF_LIST:
00288 resize->height = FONT_HEIGHT_NORMAL;
00289 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * resize->height + WD_FRAMERECT_BOTTOM + padding.height + 2);
00290 break;
00291
00292 case ANGRFW_GRF_INFO:
00293 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
00294 break;
00295 }
00296 }
00297
00298 virtual void OnResize()
00299 {
00300 this->vscroll.SetCapacityFromWidget(this, ANGRFW_GRF_LIST);
00301 }
00302
00303 virtual void OnPaint()
00304 {
00305 this->DrawWidgets();
00306 this->DrawEditBox(ANGRFW_FILTER);
00307 }
00308
00309 virtual void DrawWidget(const Rect &r, int widget) const
00310 {
00311 switch (widget) {
00312 case ANGRFW_GRF_LIST: {
00313 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0xD7);
00314
00315 uint y = r.top + WD_FRAMERECT_TOP;
00316 uint min_index = this->vscroll.GetPosition();
00317 uint max_index = min(min_index + this->vscroll.GetCapacity(), this->grfs.Length());
00318
00319 for (uint i = min_index; i < max_index; i++)
00320 {
00321 const GRFConfig *c = this->grfs[i];
00322 bool h = c == this->sel;
00323 const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename;
00324
00325
00326 if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + this->resize.step_height - 1, 156);
00327 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, text, h ? TC_WHITE : TC_ORANGE);
00328 y += this->resize.step_height;
00329 }
00330 break;
00331 }
00332
00333 case ANGRFW_GRF_INFO:
00334 if (this->sel != NULL) {
00335 ShowNewGRFInfo(this->sel, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, r.right - WD_FRAMERECT_RIGHT, r.bottom - WD_FRAMERECT_BOTTOM, false);
00336 }
00337 break;
00338 }
00339 }
00340
00341 virtual void OnDoubleClick(Point pt, int widget)
00342 {
00343 if (widget == ANGRFW_GRF_LIST) this->OnClick(pt, ANGRFW_ADD);
00344 }
00345
00346 virtual void OnClick(Point pt, int widget)
00347 {
00348 switch (widget) {
00349 case ANGRFW_GRF_LIST: {
00350
00351 uint i = (pt.y - this->GetWidget<NWidgetBase>(ANGRFW_GRF_LIST)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height + this->vscroll.GetPosition();
00352
00353 if (i < this->grfs.Length()) {
00354 this->sel = this->grfs[i];
00355 this->sel_pos = i;
00356 } else {
00357 this->sel = NULL;
00358 this->sel_pos = -1;
00359 }
00360 this->InvalidateData(1);
00361 break;
00362 }
00363
00364 case ANGRFW_ADD:
00365 if (this->sel != NULL) {
00366 const GRFConfig *src = this->sel;
00367 GRFConfig **list;
00368
00369
00370 for (list = this->list; *list != NULL; list = &(*list)->next) {
00371 if ((*list)->grfid == src->grfid) {
00372 ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, 0, 0);
00373 return;
00374 }
00375 }
00376
00377
00378 GRFConfig *c = CallocT<GRFConfig>(1);
00379 *c = *src;
00380 c->filename = strdup(src->filename);
00381 if (src->name != NULL) c->name = strdup(src->name);
00382 if (src->info != NULL) c->info = strdup(src->info);
00383 c->next = NULL;
00384
00385
00386 *list = c;
00387
00388 DeleteWindowByClass(WC_SAVELOAD);
00389 InvalidateWindowData(WC_GAME_OPTIONS, 0, 2);
00390 }
00391 break;
00392
00393 case ANGRFW_RESCAN:
00394 ScanNewGRFFiles();
00395 this->InvalidateData();
00396 break;
00397 }
00398 }
00399
00400 virtual void OnMouseLoop()
00401 {
00402 this->HandleEditBox(ANGRFW_FILTER);
00403 }
00404
00405 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00406 {
00407 switch (keycode) {
00408 case WKC_UP:
00409
00410 if (this->sel_pos > 0) this->sel_pos--;
00411 break;
00412
00413 case WKC_DOWN:
00414
00415 if (this->sel_pos < (int)this->grfs.Length() - 1) this->sel_pos++;
00416 break;
00417
00418 case WKC_PAGEUP:
00419
00420 this->sel_pos = (this->sel_pos < this->vscroll.GetCapacity()) ? 0 : this->sel_pos - this->vscroll.GetCapacity();
00421 break;
00422
00423 case WKC_PAGEDOWN:
00424
00425 this->sel_pos = min(this->sel_pos + this->vscroll.GetCapacity(), (int)this->grfs.Length() - 1);
00426 break;
00427
00428 case WKC_HOME:
00429
00430 this->sel_pos = 0;
00431 break;
00432
00433 case WKC_END:
00434
00435 this->sel_pos = this->grfs.Length() - 1;
00436 break;
00437
00438 default: {
00439
00440 EventState state = ES_NOT_HANDLED;
00441 if (this->HandleEditBoxKey(ANGRFW_FILTER, key, keycode, state) == HEBR_EDITING) {
00442 this->OnOSKInput(ANGRFW_FILTER);
00443 }
00444 return state;
00445 }
00446 }
00447
00448 if (this->grfs.Length() == 0) this->sel_pos = -1;
00449 if (this->sel_pos >= 0) {
00450 this->sel = this->grfs[this->sel_pos];
00451
00452 this->ScrollToSelected();
00453 this->InvalidateData(1);
00454 }
00455
00456 return ES_HANDLED;
00457 }
00458
00459 virtual void OnOSKInput(int wid)
00460 {
00461 this->grfs.SetFilterState(!StrEmpty(this->edit_str_buf));
00462 this->grfs.ForceRebuild();
00463 this->InvalidateData(1);
00464 }
00465 };
00466
00467 Listing NewGRFAddWindow::last_sorting = {false, 0};
00468 Filtering NewGRFAddWindow::last_filtering = {false, 0};
00469
00470 NewGRFAddWindow::GUIGRFConfigList::SortFunction * const NewGRFAddWindow::sorter_funcs[] = {
00471 &NameSorter,
00472 };
00473
00474 NewGRFAddWindow::GUIGRFConfigList::FilterFunction * const NewGRFAddWindow::filter_funcs[] = {
00475 &TagNameFilter,
00476 };
00477
00478 static const NWidgetPart _nested_newgrf_add_dlg_widgets[] = {
00479 NWidget(NWID_HORIZONTAL),
00480 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00481 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_ADD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00482 EndContainer(),
00483 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0),
00484 NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
00485 NWidget(WWT_TEXT, COLOUR_GREY), SetFill(0, 1), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
00486 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),
00487 EndContainer(),
00488 EndContainer(),
00489 NWidget(NWID_HORIZONTAL),
00490 NWidget(WWT_PANEL, COLOUR_GREY),
00491 NWidget(WWT_INSET, COLOUR_GREY, ANGRFW_GRF_LIST), SetMinimalSize(290, 1), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
00492 EndContainer(),
00493 NWidget(WWT_SCROLLBAR, COLOUR_GREY, ANGRFW_SCROLLBAR),
00494 EndContainer(),
00495 NWidget(WWT_PANEL, COLOUR_GREY, ANGRFW_GRF_INFO), SetMinimalSize(306, 1), SetResize(1, 0), EndContainer(),
00496 NWidget(NWID_HORIZONTAL),
00497 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00498 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ANGRFW_ADD), SetMinimalSize(147, 12), SetResize(1, 0), SetDataTip(STR_NEWGRF_ADD_FILE, STR_NEWGRF_ADD_FILE_TOOLTIP),
00499 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),
00500 EndContainer(),
00501 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00502 EndContainer(),
00503 };
00504
00505
00506 static const WindowDesc _newgrf_add_dlg_desc(
00507 WDP_CENTER, 306, 347,
00508 WC_SAVELOAD, WC_NONE,
00509 WDF_UNCLICK_BUTTONS,
00510 _nested_newgrf_add_dlg_widgets, lengthof(_nested_newgrf_add_dlg_widgets)
00511 );
00512
00513 static GRFPresetList _grf_preset_list;
00514
00515 class DropDownListPresetItem : public DropDownListItem {
00516 public:
00517 DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
00518
00519 virtual ~DropDownListPresetItem() {}
00520
00521 bool Selectable() const
00522 {
00523 return true;
00524 }
00525
00526 void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
00527 {
00528 DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
00529 }
00530 };
00531
00532 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
00533
00535 enum ShowNewGRFStateWidgets {
00536 SNGRFS_PRESET_LIST,
00537 SNGRFS_PRESET_SAVE,
00538 SNGRFS_PRESET_DELETE,
00539 SNGRFS_ADD,
00540 SNGRFS_REMOVE,
00541 SNGRFS_MOVE_UP,
00542 SNGRFS_MOVE_DOWN,
00543 SNGRFS_FILE_LIST,
00544 SNGRFS_SCROLLBAR,
00545 SNGRFS_NEWGRF_INFO,
00546 SNGRFS_SET_PARAMETERS,
00547 SNGRFS_TOGGLE_PALETTE,
00548 SNGRFS_APPLY_CHANGES,
00549 SNGRFS_CONTENT_DOWNLOAD,
00550 };
00551
00555 struct NewGRFWindow : public Window {
00556 GRFConfig **orig_list;
00557 GRFConfig *list;
00558 GRFConfig *sel;
00559 bool editable;
00560 bool show_params;
00561 bool execute;
00562 int query_widget;
00563 int preset;
00564
00565 NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool exec_changes, GRFConfig **config) : Window()
00566 {
00567 this->sel = NULL;
00568 this->list = NULL;
00569 this->orig_list = config;
00570 this->editable = editable;
00571 this->execute = exec_changes;
00572 this->show_params = show_params;
00573 this->preset = -1;
00574
00575 CopyGRFConfigList(&this->list, *config, false);
00576 GetGRFPresetList(&_grf_preset_list);
00577
00578 this->InitNested(desc);
00579 this->OnInvalidateData(2);
00580 }
00581
00582 ~NewGRFWindow()
00583 {
00584 if (this->editable && !this->execute) {
00585 CopyGRFConfigList(this->orig_list, this->list, true);
00586 ResetGRFConfig(false);
00587 ReloadNewGRFData();
00588 }
00589
00590
00591 ClearGRFConfigList(&this->list);
00592 _grf_preset_list.Clear();
00593 }
00594
00595 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00596 {
00597 switch (widget) {
00598 case SNGRFS_FILE_LIST:
00599 resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00600 size->height = max(size->height, 7 * resize->height);
00601 break;
00602
00603 case SNGRFS_NEWGRF_INFO:
00604 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
00605 break;
00606
00607 case SNGRFS_PRESET_LIST: {
00608 Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
00609 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00610 if (_grf_preset_list[i] != NULL) {
00611 SetDParamStr(0, _grf_preset_list[i]);
00612 d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
00613 }
00614 }
00615 d.width += padding.width;
00616 *size = maxdim(d, *size);
00617 break;
00618 }
00619 }
00620 }
00621
00622 virtual void OnResize()
00623 {
00624 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00625 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00626 }
00627
00628 virtual void SetStringParameters(int widget) const
00629 {
00630 switch (widget) {
00631 case SNGRFS_PRESET_LIST:
00632 if (this->preset == -1) {
00633 SetDParam(0, STR_NUM_CUSTOM);
00634 } else {
00635 SetDParam(0, STR_JUST_RAW_STRING);
00636 SetDParamStr(1, _grf_preset_list[this->preset]);
00637 }
00638 break;
00639 }
00640 }
00641
00642 virtual void OnPaint()
00643 {
00644 this->DrawWidgets();
00645 }
00646
00647 virtual void DrawWidget(const Rect &r, int widget) const
00648 {
00649 switch (widget) {
00650 case SNGRFS_FILE_LIST: {
00651 uint y = r.top + WD_MATRIX_TOP;
00652 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 - 1;
00653
00654 bool rtl = _dynlang.text_dir == TD_RTL;
00655 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + 25;
00656 uint text_right = rtl ? r.right - 25 : r.right - WD_FRAMERECT_RIGHT;
00657 uint square_left = rtl ? r.right - 15 : r.left + 5;
00658 uint warning_left = rtl ? r.right - 30 : r.left + 20;
00659
00660 int i = 0;
00661 for (const GRFConfig *c = this->list; c != NULL; c = c->next, i++) {
00662 if (this->vscroll.IsVisible(i)) {
00663 const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename;
00664 SpriteID pal;
00665
00666
00667 switch (c->status) {
00668 case GCS_NOT_FOUND:
00669 case GCS_DISABLED:
00670 pal = PALETTE_TO_RED;
00671 break;
00672 case GCS_ACTIVATED:
00673 pal = PALETTE_TO_GREEN;
00674 break;
00675 default:
00676 pal = PALETTE_TO_BLUE;
00677 break;
00678 }
00679
00680
00681 if (pal != PALETTE_TO_RED) {
00682 if (HasBit(c->flags, GCF_STATIC)) {
00683 pal = PALETTE_TO_GREY;
00684 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
00685 pal = PALETTE_TO_ORANGE;
00686 }
00687 }
00688
00689 DrawSprite(SPR_SQUARE, pal, square_left, y + sprite_offset_y);
00690 if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + sprite_offset_y);
00691 uint txtoffset = c->error == NULL ? 0 : 10;
00692 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y, text, this->sel == c ? TC_WHITE : TC_BLACK);
00693 y += this->resize.step_height;
00694 }
00695 }
00696 } break;
00697
00698 case SNGRFS_NEWGRF_INFO:
00699 if (this->sel != NULL) {
00700 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);
00701 }
00702 break;
00703 }
00704 }
00705
00706 virtual void OnDoubleClick(Point pt, int widget)
00707 {
00708 if (widget == SNGRFS_FILE_LIST) this->OnClick(pt, SNGRFS_SET_PARAMETERS);
00709 }
00710
00711 virtual void OnClick(Point pt, int widget)
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();
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 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) {
00779 if (c->next == this->sel) {
00780 c->next = this->sel->next;
00781 this->sel->next = c;
00782 *pc = this->sel;
00783 break;
00784 }
00785 }
00786 this->preset = -1;
00787 this->InvalidateData();
00788 break;
00789 }
00790
00791 case SNGRFS_MOVE_DOWN: {
00792 GRFConfig **pc, *c;
00793 if (this->sel == NULL) break;
00794
00795 for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) {
00796 if (c == this->sel) {
00797 *pc = c->next;
00798 c->next = c->next->next;
00799 (*pc)->next = c;
00800 break;
00801 }
00802 }
00803 this->preset = -1;
00804 this->InvalidateData();
00805 break;
00806 }
00807
00808 case SNGRFS_FILE_LIST: {
00809 GRFConfig *c;
00810 uint i = (pt.y - this->GetWidget<NWidgetBase>(SNGRFS_FILE_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
00811
00812 for (c = this->list; c != NULL && i > 0; c = c->next, i--) {}
00813
00814 if (this->sel != c) this->DeleteChildWindows(WC_QUERY_STRING);
00815 this->sel = c;
00816
00817 this->InvalidateData();
00818 break;
00819 }
00820
00821 case SNGRFS_APPLY_CHANGES:
00822 if (this->execute) {
00823 ShowQuery(
00824 STR_NEWGRF_POPUP_CAUTION_CAPTION,
00825 STR_NEWGRF_CONFIRMATION_TEXT,
00826 this,
00827 NewGRFConfirmationCallback
00828 );
00829 } else {
00830 CopyGRFConfigList(this->orig_list, this->list, true);
00831 ResetGRFConfig(false);
00832 ReloadNewGRFData();
00833 }
00834 break;
00835
00836 case SNGRFS_SET_PARAMETERS: {
00837 if (this->sel == NULL) break;
00838
00839 this->query_widget = widget;
00840 static char buff[512];
00841 GRFBuildParamList(buff, this->sel, lastof(buff));
00842 SetDParamStr(0, buff);
00843 ShowQueryString(STR_JUST_RAW_STRING, STR_NEWGRF_SETTINGS_PARAMETER_QUERY, 63, 250, this, CS_NUMERAL_SPACE, QSF_NONE);
00844 break;
00845 }
00846
00847 case SNGRFS_TOGGLE_PALETTE:
00848 if (this->sel != NULL) {
00849 this->sel->windows_paletted ^= true;
00850 this->SetDirty();
00851 }
00852 break;
00853
00854 case SNGRFS_CONTENT_DOWNLOAD:
00855 if (!_network_available) {
00856 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, 0, 0);
00857 } else {
00858 #if defined(ENABLE_NETWORK)
00859
00860 ContentVector cv;
00861 for (const GRFConfig *c = this->list; c != NULL; c = c->next) {
00862 if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
00863
00864 ContentInfo *ci = new ContentInfo();
00865 ci->type = CONTENT_TYPE_NEWGRF;
00866 ci->state = ContentInfo::DOES_NOT_EXIST;
00867 ttd_strlcpy(ci->name, c->name != NULL ? c->name : c->filename, lengthof(ci->name));
00868 ci->unique_id = BSWAP32(c->grfid);
00869 memcpy(ci->md5sum, c->md5sum, sizeof(ci->md5sum));
00870 if (HasBit(c->flags, GCF_COMPATIBLE)) GamelogGetOriginalGRFMD5Checksum(c->grfid, ci->md5sum);
00871 *cv.Append() = ci;
00872 }
00873 ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
00874 #endif
00875 }
00876 break;
00877 }
00878 }
00879
00880 virtual void OnDropdownSelect(int widget, int index)
00881 {
00882 if (index == -1) {
00883 ClearGRFConfigList(&this->list);
00884 this->preset = -1;
00885 } else {
00886 GRFConfig *c = LoadGRFPresetFromConfig(_grf_preset_list[index]);
00887
00888 if (c != NULL) {
00889 this->sel = NULL;
00890 ClearGRFConfigList(&this->list);
00891 this->list = c;
00892 this->preset = index;
00893 }
00894 }
00895
00896 this->sel = NULL;
00897 this->InvalidateData(3);
00898 }
00899
00900 virtual void OnQueryTextFinished(char *str)
00901 {
00902 if (str == NULL) return;
00903
00904 switch (this->query_widget) {
00905 default: NOT_REACHED();
00906
00907 case SNGRFS_PRESET_SAVE:
00908 SaveGRFPresetToConfig(str, this->list);
00909 GetGRFPresetList(&_grf_preset_list);
00910
00911
00912 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00913 if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
00914 this->preset = i;
00915 break;
00916 }
00917 }
00918 break;
00919
00920 case SNGRFS_SET_PARAMETERS: {
00921
00922 GRFConfig *c = this->sel;
00923 c->num_params = parse_intlist(str, (int*)c->param, lengthof(c->param));
00924
00925
00926 if (c->num_params == (byte)-1) c->num_params = 0;
00927
00928 this->preset = -1;
00929 break;
00930 }
00931 }
00932
00933 this->InvalidateData();
00934 }
00935
00936 virtual void OnInvalidateData(int data = 0)
00937 {
00938 switch (data) {
00939 default: NOT_REACHED();
00940 case 0:
00941
00942 break;
00943
00944 case 1:
00945
00946 for (GRFConfig *c = this->list; c != NULL; c = c->next) {
00947 if (c->status != GCS_NOT_FOUND) continue;
00948
00949 const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
00950 if (f == NULL) continue;
00951
00952 free(c->filename);
00953 free(c->name);
00954 free(c->info);
00955
00956 c->filename = f->filename == NULL ? NULL : strdup(f->filename);
00957 c->name = f->name == NULL ? NULL : strdup(f->name);;
00958 c->info = f->info == NULL ? NULL : strdup(f->info);;
00959 c->status = GCS_UNKNOWN;
00960 }
00961 break;
00962
00963 case 2:
00964 this->preset = -1;
00965
00966 case 3:
00967 const GRFConfig *c;
00968 int i;
00969
00970 for (c = this->list, i = 0; c != NULL; c = c->next, i++) {}
00971
00972 this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00973 this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00974 this->vscroll.SetCount(i);
00975 break;
00976 }
00977
00978 this->SetWidgetsDisabledState(!this->editable,
00979 SNGRFS_PRESET_LIST,
00980 SNGRFS_ADD,
00981 SNGRFS_APPLY_CHANGES,
00982 SNGRFS_TOGGLE_PALETTE,
00983 WIDGET_LIST_END
00984 );
00985
00986 bool disable_all = this->sel == NULL || !this->editable;
00987 this->SetWidgetsDisabledState(disable_all,
00988 SNGRFS_REMOVE,
00989 SNGRFS_MOVE_UP,
00990 SNGRFS_MOVE_DOWN,
00991 WIDGET_LIST_END
00992 );
00993 this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all);
00994 this->SetWidgetDisabledState(SNGRFS_TOGGLE_PALETTE, disable_all);
00995
00996 if (!disable_all) {
00997
00998 if (this->sel == this->list) this->DisableWidget(SNGRFS_MOVE_UP);
00999 if (this->sel->next == NULL) this->DisableWidget(SNGRFS_MOVE_DOWN);
01000 if (this->sel->IsOpenTTDBaseGRF()) this->DisableWidget(SNGRFS_REMOVE);
01001 }
01002
01003 this->SetWidgetDisabledState(SNGRFS_PRESET_DELETE, this->preset == -1);
01004
01005 bool has_missing = false;
01006 bool has_compatible = false;
01007 for (const GRFConfig *c = this->list; !has_missing && c != NULL; c = c->next) {
01008 has_missing |= c->status == GCS_NOT_FOUND;
01009 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
01010 }
01011 if (has_missing || has_compatible) {
01012 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
01013 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
01014 } else {
01015 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data = STR_INTRO_ONLINE_CONTENT;
01016 this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
01017 }
01018 this->SetWidgetDisabledState(SNGRFS_PRESET_SAVE, has_missing);
01019 }
01020 };
01021
01022
01023 static const NWidgetPart _nested_newgrf_widgets[] = {
01024 NWidget(NWID_HORIZONTAL),
01025 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01026 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01027 EndContainer(),
01028 NWidget(WWT_PANEL, COLOUR_MAUVE),
01029 NWidget(NWID_HORIZONTAL), SetPadding(2, 10, 2, 10),
01030 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, SNGRFS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
01031 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01032 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_SAVE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
01033 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_DELETE), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
01034 EndContainer(),
01035 EndContainer(),
01036 EndContainer(),
01037 NWidget(WWT_PANEL, COLOUR_MAUVE),
01038 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 10, 2, 10),
01039 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_ADD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_TOOLTIP),
01040 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_REMOVE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
01041 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_UP), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
01042 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
01043 EndContainer(),
01044 EndContainer(),
01045 NWidget(NWID_HORIZONTAL),
01046 NWidget(WWT_MATRIX, COLOUR_MAUVE, SNGRFS_FILE_LIST), SetFill(1, 0), SetDataTip(0x501, STR_NEWGRF_SETTINGS_FILE_TOOLTIP), SetResize(1, 0),
01047 NWidget(WWT_SCROLLBAR, COLOUR_MAUVE, SNGRFS_SCROLLBAR),
01048 EndContainer(),
01049 NWidget(WWT_PANEL, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO), SetFill(1, 0), SetResize(1, 0), EndContainer(),
01050 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01051 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
01052 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),
01053 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
01054 EndContainer(),
01055 NWidget(NWID_HORIZONTAL),
01056 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, SNGRFS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01057 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01058 EndContainer(),
01059 };
01060
01061
01062 static const WindowDesc _newgrf_desc(
01063 WDP_CENTER, 300, 263,
01064 WC_GAME_OPTIONS, WC_NONE,
01065 WDF_UNCLICK_BUTTONS,
01066 _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
01067 );
01068
01073 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
01074 {
01075 if (confirmed) {
01076 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
01077 GRFConfig *c;
01078 int i = 0;
01079
01080 GamelogStartAction(GLAT_GRF);
01081 GamelogGRFUpdate(_grfconfig, nw->list);
01082 CopyGRFConfigList(nw->orig_list, nw->list, false);
01083 ReloadNewGRFData();
01084 GamelogStopAction();
01085
01086
01087 for (c = nw->list; c != NULL && c != nw->sel; c = c->next, i++) {}
01088 CopyGRFConfigList(&nw->list, *nw->orig_list, false);
01089 for (c = nw->list; c != NULL && i > 0; c = c->next, i--) {}
01090 nw->sel = c;
01091
01092 w->SetDirty();
01093 }
01094 }
01095
01096
01097
01104 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
01105 {
01106 DeleteWindowByClass(WC_GAME_OPTIONS);
01107 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
01108 }