peter1138@5237: /* $Id$ */ peter1138@5237: rubidium@9111: /** @file newgrf_gui.cpp GUI to change NewGRF settings. */ belugas@6348: peter1138@5237: #include "stdafx.h" peter1138@5237: #include "openttd.h" peter1138@5237: #include "variables.h" peter1138@5237: #include "gui.h" rubidium@8107: #include "window_gui.h" rubidium@8107: #include "textbuf_gui.h" Darkvater@5352: #include "newgrf.h" peter1138@5237: #include "newgrf_config.h" rubidium@8114: #include "strings_func.h" rubidium@8131: #include "window_func.h" rubidium@8213: #include "core/alloc_func.hpp" rubidium@8214: #include "string_func.h" rubidium@8224: #include "gfx_func.h" smatz@9457: #include "gamelog.h" peter1138@9677: #include "settings_func.h" peter1138@9677: #include "widgets/dropdown_type.h" peter1138@5237: rubidium@8264: #include "table/strings.h" rubidium@8264: #include "table/sprites.h" rubidium@8264: peter1138@5237: /** Parse an integerlist string and set each found value peter1138@5237: * @param p the string to be parsed. Each element in the list is seperated by a peter1138@5237: * comma or a space character peter1138@5237: * @param items pointer to the integerlist-array that will be filled with values peter1138@5237: * @param maxitems the maximum number of elements the integerlist-array has peter1138@5237: * @return returns the number of items found, or -1 on an error */ peter1138@5237: static int parse_intlist(const char *p, int *items, int maxitems) peter1138@5237: { peter1138@5237: int n = 0, v; peter1138@5237: char *end; peter1138@5237: peter1138@5237: for (;;) { peter1138@5237: v = strtol(p, &end, 0); peter1138@5237: if (p == end || n == maxitems) return -1; peter1138@5237: p = end; peter1138@5237: items[n++] = v; peter1138@5237: if (*p == '\0') break; peter1138@5237: if (*p != ',' && *p != ' ') return -1; peter1138@5237: p++; peter1138@5237: } peter1138@5237: peter1138@5237: return n; peter1138@5237: } peter1138@5237: peter1138@5237: maedhros@6377: static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint w, uint bottom, bool show_params) peter1138@5237: { Darkvater@5634: char buff[256]; peter1138@5237: maedhros@6103: if (c->error != NULL) { rubidium@9648: char message[512]; frosch@9957: SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages frosch@9957: SetDParam (1, STR_JUST_RAW_STRING); frosch@9957: SetDParamStr(2, c->filename); frosch@9957: SetDParam (3, STR_JUST_RAW_STRING); frosch@9957: SetDParamStr(4, c->error->data); frosch@9957: for (uint i = 0; i < c->error->num_params; i++) { frosch@9957: uint32 param = 0; frosch@9957: byte param_number = c->error->param_number[i]; maedhros@6139: frosch@9957: if (param_number < c->num_params) param = c->param[param_number]; rubidium@9648: frosch@9957: SetDParam(5 + i, param); maedhros@6139: } frosch@9957: GetString(message, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message)); maedhros@6139: maedhros@6139: SetDParamStr(0, message); maedhros@6377: y += DrawStringMultiLine(x, y, c->error->severity, w, bottom - y); maedhros@6103: } maedhros@6103: rubidium@5339: /* Draw filename or not if it is not known (GRF sent over internet) */ rubidium@5339: if (c->filename != NULL) { rubidium@5339: SetDParamStr(0, c->filename); maedhros@6377: y += DrawStringMultiLine(x, y, STR_NEWGRF_FILENAME, w, bottom - y); rubidium@5339: } peter1138@5237: peter1138@5237: /* Prepare and draw GRF ID */ Darkvater@5643: snprintf(buff, lengthof(buff), "%08X", BSWAP32(c->grfid)); peter1138@5237: SetDParamStr(0, buff); maedhros@6377: y += DrawStringMultiLine(x, y, STR_NEWGRF_GRF_ID, w, bottom - y); peter1138@5237: peter1138@5237: /* Prepare and draw MD5 sum */ Darkvater@5634: md5sumToString(buff, lastof(buff), c->md5sum); peter1138@5237: SetDParamStr(0, buff); maedhros@6377: y += DrawStringMultiLine(x, y, STR_NEWGRF_MD5SUM, w, bottom - y); peter1138@5237: peter1138@5237: /* Show GRF parameter list */ peter1138@5237: if (show_params) { peter1138@5237: if (c->num_params > 0) { peter1138@5308: GRFBuildParamList(buff, c, lastof(buff)); rubidium@9648: SetDParam(0, STR_JUST_RAW_STRING); rubidium@9648: SetDParamStr(1, buff); peter1138@5237: } else { peter1138@5237: SetDParam(0, STR_01A9_NONE); peter1138@5237: } maedhros@6377: y += DrawStringMultiLine(x, y, STR_NEWGRF_PARAMETER, w, bottom - y); peter1138@5237: rubidium@10126: /* Draw the palette of the NewGRF */ rubidium@10126: SetDParamStr(0, c->windows_paletted ? "Windows" : "DOS"); rubidium@10126: y += DrawStringMultiLine(x, y, STR_NEWGRF_PALETTE, w, bottom - y); rubidium@10126: } rubidium@10066: peter1138@5237: /* Show flags */ maedhros@6377: if (c->status == GCS_NOT_FOUND) y += DrawStringMultiLine(x, y, STR_NEWGRF_NOT_FOUND, w, bottom - y); maedhros@6377: if (c->status == GCS_DISABLED) y += DrawStringMultiLine(x, y, STR_NEWGRF_DISABLED, w, bottom - y); skidd13@7928: if (HasBit(c->flags, GCF_COMPATIBLE)) y += DrawStringMultiLine(x, y, STR_NEWGRF_COMPATIBLE_LOADED, w, bottom - y); peter1138@5237: peter1138@5237: /* Draw GRF info if it exists */ tron@5638: if (c->info != NULL && !StrEmpty(c->info)) { rubidium@9648: SetDParam(0, STR_JUST_RAW_STRING); rubidium@9648: SetDParamStr(1, c->info); maedhros@6377: y += DrawStringMultiLine(x, y, STR_02BD, w, bottom - y); peter1138@5237: } else { maedhros@6377: y += DrawStringMultiLine(x, y, STR_NEWGRF_NO_INFO, w, bottom - y); peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@5237: peter1138@9207: /** peter1138@9207: * Window for adding NewGRF files peter1138@9207: */ peter1138@9207: struct NewGRFAddWindow : public Window { peter1138@9207: /* Names of the add a newgrf window widgets */ peter1138@9207: enum AddNewGRFWindowWidgets { peter1138@9207: ANGRFW_CLOSEBOX = 0, peter1138@9207: ANGRFW_CAPTION, peter1138@9207: ANGRFW_BACKGROUND, peter1138@9207: ANGRFW_GRF_LIST, peter1138@9207: ANGRFW_SCROLLBAR, peter1138@9207: ANGRFW_GRF_INFO, peter1138@9207: ANGRFW_ADD, peter1138@9207: ANGRFW_RESCAN, peter1138@9207: ANGRFW_RESIZE, peter1138@9207: }; peter1138@9207: peter1138@5237: GRFConfig **list; peter1138@5237: const GRFConfig *sel; peter1138@5237: peter1138@9207: NewGRFAddWindow(const WindowDesc *desc, GRFConfig **list) : Window(desc, 0) peter1138@9207: { peter1138@9207: this->list = list; peter1138@9207: this->resize.step_height = 10; peter1138@5237: peter1138@9207: this->FindWindowPlacementAndResize(desc); peter1138@9207: } peter1138@5237: peter1138@9207: virtual void OnPaint() peter1138@9207: { peter1138@9207: const GRFConfig *c; peter1138@9207: const Widget *wl = &this->widget[ANGRFW_GRF_LIST]; peter1138@9207: int n = 0; peter1138@5237: peter1138@9207: /* Count the number of GRFs */ peter1138@9207: for (c = _all_grfs; c != NULL; c = c->next) n++; peter1138@9207: peter1138@9207: this->vscroll.cap = (wl->bottom - wl->top) / 10; peter1138@9207: SetVScrollCount(this, n); peter1138@9207: peter1138@9207: this->SetWidgetDisabledState(ANGRFW_ADD, this->sel == NULL || this->sel->IsOpenTTDBaseGRF()); rubidium@9273: this->DrawWidgets(); peter1138@9207: peter1138@9207: GfxFillRect(wl->left + 1, wl->top + 1, wl->right, wl->bottom, 0xD7); peter1138@9207: peter1138@9207: uint y = wl->top + 1; peter1138@9207: for (c = _all_grfs, n = 0; c != NULL && n < (this->vscroll.pos + this->vscroll.cap); c = c->next, n++) { peter1138@9207: if (n >= this->vscroll.pos) { peter1138@9207: bool h = c == this->sel; peter1138@9207: const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; peter1138@9207: peter1138@9207: /* Draw selection background */ peter1138@9207: if (h) GfxFillRect(3, y, this->width - 15, y + 9, 156); peter1138@9207: DoDrawStringTruncated(text, 4, y, h ? TC_WHITE : TC_ORANGE, this->width - 18); peter1138@9207: y += 10; peter1138@9207: } peter1138@9207: } peter1138@9207: peter1138@9207: if (this->sel != NULL) { peter1138@9207: const Widget *wi = &this->widget[ANGRFW_GRF_INFO]; peter1138@9207: ShowNewGRFInfo(this->sel, wi->left + 2, wi->top + 2, wi->right - wi->left - 2, wi->bottom, false); peter1138@9207: } peter1138@9207: } peter1138@9207: peter1138@9207: virtual void OnDoubleClick(Point pt, int widget) peter1138@9207: { peter1138@9207: if (widget == ANGRFW_GRF_LIST) this->OnClick(pt, ANGRFW_ADD); peter1138@9207: } peter1138@9207: peter1138@9207: virtual void OnClick(Point pt, int widget) peter1138@9207: { peter1138@9207: switch (widget) { peter1138@9207: case ANGRFW_GRF_LIST: { peter1138@9207: /* Get row... */ peter1138@9207: const GRFConfig *c; peter1138@9207: uint i = (pt.y - this->widget[ANGRFW_GRF_LIST].top) / 10 + this->vscroll.pos; peter1138@9207: peter1138@9207: for (c = _all_grfs; c != NULL && i > 0; c = c->next, i--) {} peter1138@9207: this->sel = c; peter1138@9207: this->SetDirty(); peter1138@9207: break; peter1138@5237: } peter1138@5237: peter1138@9207: case ANGRFW_ADD: // Add selection to list peter1138@9207: if (this->sel != NULL) { peter1138@9207: const GRFConfig *src = this->sel; peter1138@9207: GRFConfig **list; peter1138@5243: peter1138@9207: /* Find last entry in the list, checking for duplicate grfid on the way */ peter1138@9207: for (list = this->list; *list != NULL; list = &(*list)->next) { peter1138@9207: if ((*list)->grfid == src->grfid) { peter1138@9207: ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DUPLICATE_GRFID, 0, 0); peter1138@9207: return; peter1138@5243: } peter1138@9207: } peter1138@5243: peter1138@9207: /* Copy GRF details from scanned list */ peter1138@9207: GRFConfig *c = CallocT(1); peter1138@9207: *c = *src; peter1138@9207: c->filename = strdup(src->filename); peter1138@9207: if (src->name != NULL) c->name = strdup(src->name); peter1138@9207: if (src->info != NULL) c->info = strdup(src->info); peter1138@9207: c->next = NULL; peter1138@5237: peter1138@9207: /* Append GRF config to configuration list */ peter1138@9207: *list = c; peter1138@9207: peter1138@9207: DeleteWindowByClass(WC_SAVELOAD); peter1138@9207: InvalidateWindowData(WC_GAME_OPTIONS, 0); peter1138@9207: } peter1138@9207: break; peter1138@9207: peter1138@9207: case ANGRFW_RESCAN: // Rescan list peter1138@9207: this->sel = NULL; peter1138@9207: ScanNewGRFFiles(); peter1138@9207: this->SetDirty(); peter1138@9207: break; peter1138@9207: } peter1138@5237: } peter1138@9207: }; peter1138@5237: skidd13@8025: /* Widget definition for the add a newgrf window */ peter1138@5237: static const Widget _newgrf_add_dlg_widgets[] = { belugas@9760: { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, // ANGRFW_CLOSEBOX belugas@9760: { WWT_CAPTION, RESIZE_RIGHT, COLOUR_GREY, 11, 306, 0, 13, STR_NEWGRF_ADD_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, // ANGRFW_CAPTION belugas@9760: { WWT_PANEL, RESIZE_RB, COLOUR_GREY, 0, 294, 14, 121, 0x0, STR_NULL }, // ANGRFW_BACKGROUND belugas@9760: { WWT_INSET, RESIZE_RB, COLOUR_GREY, 2, 292, 16, 119, 0x0, STR_NULL }, // ANGRFW_GRF_LIST belugas@9760: { WWT_SCROLLBAR, RESIZE_LRB, COLOUR_GREY, 295, 306, 14, 121, 0x0, STR_NULL }, // ANGRFW_SCROLLBAR belugas@9760: { WWT_PANEL, RESIZE_RTB, COLOUR_GREY, 0, 306, 122, 224, 0x0, STR_NULL }, // ANGRFW_GRF_INFO belugas@9760: { WWT_PUSHTXTBTN, RESIZE_RTB, COLOUR_GREY, 0, 146, 225, 236, STR_NEWGRF_ADD_FILE, STR_NEWGRF_ADD_FILE_TIP }, // ANGRFW_ADD belugas@9760: { WWT_PUSHTXTBTN, RESIZE_LRTB, COLOUR_GREY, 147, 294, 225, 236, STR_NEWGRF_RESCAN_FILES, STR_NEWGRF_RESCAN_FILES_TIP }, // ANGRFW_RESCAN belugas@9760: { WWT_RESIZEBOX, RESIZE_LRTB, COLOUR_GREY, 295, 306, 225, 236, 0x0, STR_RESIZE_BUTTON }, // ANGRFW_RESIZE peter1138@5237: { WIDGETS_END }, peter1138@5237: }; peter1138@5237: skidd13@8025: /* Window definition for the add a newgrf window */ peter1138@5237: static const WindowDesc _newgrf_add_dlg_desc = { rubidium@7341: WDP_CENTER, WDP_CENTER, 307, 237, 307, 337, rubidium@5893: WC_SAVELOAD, WC_NONE, belugas@8019: WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, peter1138@5237: _newgrf_add_dlg_widgets, peter1138@5237: }; peter1138@5237: peter1138@9677: static GRFPresetList _grf_preset_list; peter1138@9677: peter1138@9677: class DropDownListPresetItem : public DropDownListItem { peter1138@9677: public: peter1138@9677: DropDownListPresetItem(int result) : DropDownListItem(result, false) {} peter1138@9677: peter1138@9677: virtual ~DropDownListPresetItem() {} peter1138@9864: peter1138@9863: bool Selectable() const peter1138@9677: { peter1138@9863: return true; peter1138@9677: } peter1138@9677: peter1138@9863: void Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const peter1138@9677: { peter1138@9677: DoDrawStringTruncated(_grf_preset_list[this->result], x + 2, y, sel ? TC_WHITE : TC_BLACK, x + width); peter1138@9677: } peter1138@9677: }; peter1138@5237: peter1138@9207: static void NewGRFConfirmationCallback(Window *w, bool confirmed); peter1138@9207: peter1138@9207: /** peter1138@9207: * Window for showing NewGRF files peter1138@9207: */ peter1138@9207: struct NewGRFWindow : public Window { peter1138@9207: /* Names of the manage newgrfs window widgets */ peter1138@9207: enum ShowNewGRFStateWidgets { peter1138@9207: SNGRFS_CLOSEBOX = 0, peter1138@9207: SNGRFS_CAPTION, peter1138@9677: SNGRFS_BACKGROUND1, peter1138@9677: SNGRFS_PRESET_LIST, peter1138@9677: SNGRFS_PRESET_SAVE, peter1138@9677: SNGRFS_PRESET_DELETE, peter1138@9677: SNGRFS_BACKGROUND2, peter1138@9207: SNGRFS_ADD, peter1138@9207: SNGRFS_REMOVE, peter1138@9207: SNGRFS_MOVE_UP, peter1138@9207: SNGRFS_MOVE_DOWN, peter1138@9207: SNGRFS_FILE_LIST, peter1138@9207: SNGRFS_SCROLLBAR, peter1138@9207: SNGRFS_NEWGRF_INFO, peter1138@9207: SNGRFS_SET_PARAMETERS, rubidium@10066: SNGRFS_TOGGLE_PALETTE, peter1138@9207: SNGRFS_APPLY_CHANGES, peter1138@9207: SNGRFS_RESIZE, peter1138@9207: }; peter1138@9207: Darkvater@5352: GRFConfig **orig_list; ///< grf list the window is shown with peter1138@9207: GRFConfig *list; ///< temporary grf list to which changes are made Darkvater@5352: GRFConfig *sel; ///< selected grf item Darkvater@5352: bool editable; ///< is the window editable Darkvater@5352: bool show_params; ///< are the grf-parameters shown in the info-panel Darkvater@5352: bool execute; ///< on pressing 'apply changes' are grf changes applied immediately, or only list is updated peter1138@9677: int query_widget; ///< widget that opened a query peter1138@9677: int preset; ///< selected preset Darkvater@5352: peter1138@9207: NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool exec_changes, GRFConfig **config) : Window(desc, 0) peter1138@9207: { peter1138@9207: this->resize.step_height = 14; peter1138@9207: this->sel = NULL; peter1138@9207: this->list = NULL; peter1138@9207: this->orig_list = config; peter1138@9207: this->editable = editable; peter1138@9207: this->execute = exec_changes; peter1138@9207: this->show_params = show_params; peter1138@9677: this->preset = -1; peter1138@5237: peter1138@9207: CopyGRFConfigList(&this->list, *config, false); peter1138@9677: GetGRFPresetList(&_grf_preset_list); maedhros@6229: peter1138@9207: this->FindWindowPlacementAndResize(desc); peter1138@9207: this->SetupNewGRFWindow(); peter1138@9207: } peter1138@5237: peter1138@9207: ~NewGRFWindow() peter1138@9207: { peter1138@9207: if (!this->execute) { peter1138@9207: CopyGRFConfigList(this->orig_list, this->list, true); peter1138@9207: ResetGRFConfig(false); peter1138@9207: ReloadNewGRFData(); peter1138@5237: } peter1138@5237: peter1138@9207: /* Remove the temporary copy of grf-list used in window */ peter1138@9207: ClearGRFConfigList(&this->list); peter1138@9677: _grf_preset_list.Clear(); peter1138@9207: } peter1138@5248: peter1138@9207: void SetupNewGRFWindow() peter1138@9207: { peter1138@9207: const GRFConfig *c; peter1138@9207: int i; peter1138@5248: peter1138@9207: for (c = this->list, i = 0; c != NULL; c = c->next, i++) {} peter1138@5237: peter1138@9207: this->vscroll.cap = (this->widget[SNGRFS_FILE_LIST].bottom - this->widget[SNGRFS_FILE_LIST].top) / 14 + 1; peter1138@9207: SetVScrollCount(this, i); peter1138@9207: peter1138@9677: this->SetWidgetsDisabledState(!this->editable, peter1138@9677: SNGRFS_PRESET_LIST, peter1138@9677: SNGRFS_ADD, peter1138@9677: SNGRFS_APPLY_CHANGES, rubidium@10066: SNGRFS_TOGGLE_PALETTE, peter1138@9677: WIDGET_LIST_END peter1138@9677: ); peter1138@9207: } peter1138@9207: peter1138@9207: virtual void OnPaint() peter1138@9207: { peter1138@9207: bool disable_all = this->sel == NULL || !this->editable; peter1138@9207: peter1138@9207: this->SetWidgetsDisabledState(disable_all, peter1138@9207: SNGRFS_REMOVE, peter1138@9207: SNGRFS_MOVE_UP, peter1138@9207: SNGRFS_MOVE_DOWN, peter1138@9207: WIDGET_LIST_END peter1138@9207: ); peter1138@9207: this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all); rubidium@10115: this->SetWidgetDisabledState(SNGRFS_TOGGLE_PALETTE, disable_all); peter1138@9207: peter1138@9207: if (!disable_all) { peter1138@9207: /* All widgets are now enabled, so disable widgets we can't use */ peter1138@9207: if (this->sel == this->list) this->DisableWidget(SNGRFS_MOVE_UP); peter1138@9207: if (this->sel->next == NULL) this->DisableWidget(SNGRFS_MOVE_DOWN); peter1138@9207: if (this->sel->IsOpenTTDBaseGRF()) this->DisableWidget(SNGRFS_REMOVE); peter1138@9207: } peter1138@9207: peter1138@9677: if (this->preset == -1) { peter1138@9677: this->widget[SNGRFS_PRESET_LIST].data = STR_02BF_CUSTOM; peter1138@9677: } else { peter1138@9677: SetDParamStr(0, _grf_preset_list[this->preset]); peter1138@9677: this->widget[SNGRFS_PRESET_LIST].data = STR_JUST_RAW_STRING; peter1138@9677: } peter1138@9677: rubidium@9273: this->DrawWidgets(); peter1138@9207: peter1138@9207: /* Draw NewGRF list */ peter1138@9207: int y = this->widget[SNGRFS_FILE_LIST].top; peter1138@9207: int i = 0; peter1138@9207: for (const GRFConfig *c = this->list; c != NULL; c = c->next, i++) { peter1138@9207: if (i >= this->vscroll.pos && i < this->vscroll.pos + this->vscroll.cap) { peter1138@9207: const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; peter1138@9207: SpriteID pal; peter1138@9207: byte txtoffset; peter1138@9207: peter1138@9207: /* Pick a colour */ peter1138@9207: switch (c->status) { peter1138@9207: case GCS_NOT_FOUND: peter1138@9207: case GCS_DISABLED: peter1138@9207: pal = PALETTE_TO_RED; peter1138@9207: break; peter1138@9207: case GCS_ACTIVATED: peter1138@9207: pal = PALETTE_TO_GREEN; peter1138@9207: break; peter1138@9207: default: peter1138@9207: pal = PALETTE_TO_BLUE; peter1138@9207: break; peter1138@5237: } peter1138@5237: peter1138@9207: /* Do not show a "not-failure" colour when it actually failed to load */ peter1138@9207: if (pal != PALETTE_TO_RED) { peter1138@9207: if (HasBit(c->flags, GCF_STATIC)) { peter1138@9207: pal = PALETTE_TO_GREY; peter1138@9207: } else if (HasBit(c->flags, GCF_COMPATIBLE)) { peter1138@9207: pal = PALETTE_TO_ORANGE; peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@9207: DrawSprite(SPR_SQUARE, pal, 5, y + 2); peter1138@9207: if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, 20, y + 2); peter1138@9207: txtoffset = c->error != NULL ? 35 : 25; peter1138@9207: DoDrawStringTruncated(text, txtoffset, y + 3, this->sel == c ? TC_WHITE : TC_BLACK, this->width - txtoffset - 10); peter1138@9207: y += 14; peter1138@5237: } peter1138@9207: } peter1138@5237: peter1138@9207: if (this->sel != NULL) { peter1138@9207: /* Draw NewGRF file info */ peter1138@9207: const Widget *wi = &this->widget[SNGRFS_NEWGRF_INFO]; peter1138@9207: ShowNewGRFInfo(this->sel, wi->left + 2, wi->top + 2, wi->right - wi->left - 2, wi->bottom, this->show_params); peter1138@9207: } peter1138@9207: } peter1138@5237: peter1138@9207: virtual void OnClick(Point pt, int widget) peter1138@9207: { peter1138@9207: switch (widget) { peter1138@9677: case SNGRFS_PRESET_LIST: { peter1138@9677: DropDownList *list = new DropDownList(); peter1138@9677: peter1138@9677: /* Add 'None' option for clearing list */ peter1138@9677: list->push_back(new DropDownListStringItem(STR_NONE, -1, false)); peter1138@9677: peter1138@9677: for (uint i = 0; i < _grf_preset_list.Length(); i++) { peter1138@9677: if (_grf_preset_list[i] != NULL) { peter1138@9677: list->push_back(new DropDownListPresetItem(i)); peter1138@9677: } peter1138@9677: } peter1138@9677: peter1138@9677: ShowDropDownList(this, list, this->preset, SNGRFS_PRESET_LIST); peter1138@9677: break; peter1138@9677: } peter1138@9677: peter1138@9677: case SNGRFS_PRESET_SAVE: peter1138@9677: this->query_widget = widget; smatz@10145: ShowQueryString(STR_EMPTY, STR_NEWGRF_PRESET_SAVE_QUERY, 32, 100, this, CS_ALPHANUMERAL, QSF_NONE); peter1138@9677: break; peter1138@9677: peter1138@9677: case SNGRFS_PRESET_DELETE: peter1138@9677: if (this->preset == -1) return; peter1138@9677: peter1138@9677: DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]); peter1138@9677: GetGRFPresetList(&_grf_preset_list); peter1138@9677: this->preset = -1; peter1138@9677: this->SetDirty(); peter1138@9677: break; peter1138@9677: peter1138@9207: case SNGRFS_ADD: // Add GRF peter1138@9207: DeleteWindowByClass(WC_SAVELOAD); peter1138@9207: new NewGRFAddWindow(&_newgrf_add_dlg_desc, &this->list); peter1138@9207: break; Darkvater@5352: peter1138@9207: case SNGRFS_REMOVE: { // Remove GRF peter1138@9207: GRFConfig **pc, *c, *newsel; peter1138@9207: peter1138@9207: /* Choose the next GRF file to be the selected file */ peter1138@9207: newsel = this->sel->next; peter1138@9207: peter1138@9207: for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) { peter1138@9207: /* If the new selection is empty (i.e. we're deleting the last item peter1138@9207: * in the list, pick the file just before the selected file */ peter1138@9207: if (newsel == NULL && c->next == this->sel) newsel = c; peter1138@9207: peter1138@9207: if (c == this->sel) { peter1138@9207: *pc = c->next; peter1138@9207: free(c); peter1138@9207: break; peter1138@9207: } peter1138@9207: } peter1138@9207: peter1138@9207: this->sel = newsel; peter1138@9677: this->preset = -1; peter1138@9207: this->SetupNewGRFWindow(); peter1138@9207: this->SetDirty(); peter1138@9207: break; peter1138@8591: } peter1138@9207: peter1138@9207: case SNGRFS_MOVE_UP: { // Move GRF up peter1138@9207: GRFConfig **pc, *c; peter1138@9207: if (this->sel == NULL) break; peter1138@9207: peter1138@9207: for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) { peter1138@9207: if (c->next == this->sel) { peter1138@9207: c->next = this->sel->next; peter1138@9207: this->sel->next = c; peter1138@9207: *pc = this->sel; peter1138@9207: break; peter1138@9207: } peter1138@9207: } peter1138@9677: this->preset = -1; peter1138@9207: this->SetDirty(); peter1138@9207: break; peter1138@9207: } peter1138@9207: peter1138@9207: case SNGRFS_MOVE_DOWN: { // Move GRF down peter1138@9207: GRFConfig **pc, *c; peter1138@9207: if (this->sel == NULL) break; peter1138@9207: peter1138@9207: for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) { peter1138@9207: if (c == this->sel) { peter1138@9207: *pc = c->next; peter1138@9207: c->next = c->next->next; peter1138@9207: (*pc)->next = c; peter1138@9207: break; peter1138@9207: } peter1138@9207: } peter1138@9677: this->preset = -1; peter1138@9207: this->SetDirty(); peter1138@9207: break; peter1138@9207: } peter1138@9207: peter1138@9207: case SNGRFS_FILE_LIST: { // Select a GRF peter1138@9207: GRFConfig *c; peter1138@9207: uint i = (pt.y - this->widget[SNGRFS_FILE_LIST].top) / 14 + this->vscroll.pos; peter1138@9207: peter1138@9207: for (c = this->list; c != NULL && i > 0; c = c->next, i--) {} peter1138@9207: this->sel = c; peter1138@9207: peter1138@9207: this->SetDirty(); peter1138@9207: break; peter1138@9207: } peter1138@9207: peter1138@9207: case SNGRFS_APPLY_CHANGES: // Apply changes made to GRF list peter1138@9207: if (this->execute) { peter1138@9207: ShowQuery( peter1138@9207: STR_POPUP_CAUTION_CAPTION, peter1138@9207: STR_NEWGRF_CONFIRMATION_TEXT, peter1138@9207: this, peter1138@9207: NewGRFConfirmationCallback peter1138@9207: ); peter1138@9207: } else { peter1138@9207: CopyGRFConfigList(this->orig_list, this->list, true); peter1138@9207: ResetGRFConfig(false); peter1138@9207: ReloadNewGRFData(); peter1138@9207: } peter1138@9207: break; peter1138@9207: peter1138@9207: case SNGRFS_SET_PARAMETERS: { // Edit parameters peter1138@9207: if (this->sel == NULL) break; peter1138@9207: peter1138@9677: this->query_widget = widget; rubidium@9648: static char buff[512]; peter1138@9207: GRFBuildParamList(buff, this->sel, lastof(buff)); rubidium@9648: SetDParamStr(0, buff); smatz@10145: ShowQueryString(STR_JUST_RAW_STRING, STR_NEWGRF_PARAMETER_QUERY, 63, 250, this, CS_ALPHANUMERAL, QSF_NONE); peter1138@9207: break; peter1138@9207: } rubidium@10066: rubidium@10066: case SNGRFS_TOGGLE_PALETTE: { rubidium@10066: if (this->sel != NULL) { rubidium@10066: this->sel->windows_paletted ^= true; rubidium@10066: this->SetDirty(); rubidium@10066: } rubidium@10066: break; rubidium@10066: } peter1138@9207: } peter1138@5237: } peter1138@9207: peter1138@9677: virtual void OnDropdownSelect(int widget, int index) peter1138@9677: { peter1138@9677: if (index == -1) { peter1138@9677: ClearGRFConfigList(&this->list); peter1138@9677: this->preset = -1; peter1138@9677: } else { peter1138@9677: GRFConfig *c = LoadGRFPresetFromConfig(_grf_preset_list[index]); peter1138@9677: peter1138@9677: if (c != NULL) { peter1138@9677: this->sel = NULL; peter1138@9677: ClearGRFConfigList(&this->list); peter1138@9677: this->list = c; peter1138@9677: this->preset = index; peter1138@9677: } peter1138@9677: } peter1138@9677: peter1138@9706: this->sel = NULL; peter1138@9677: this->SetupNewGRFWindow(); peter1138@9677: this->SetDirty(); peter1138@9677: } peter1138@9677: peter1138@9207: virtual void OnQueryTextFinished(char *str) peter1138@9207: { peter1138@9207: if (str == NULL) return; peter1138@9207: peter1138@9677: switch (this->query_widget) { peter1138@9677: case SNGRFS_PRESET_SAVE: peter1138@9677: SaveGRFPresetToConfig(str, this->list); peter1138@9677: GetGRFPresetList(&_grf_preset_list); peter1138@9207: peter1138@9677: /* Switch to this preset */ peter1138@9683: for (uint i = 0; i < _grf_preset_list.Length(); i++) { peter1138@9677: if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) { peter1138@9677: this->preset = i; peter1138@9677: break; peter1138@9677: } peter1138@9677: } peter1138@9683: peter1138@9683: this->SetDirty(); peter1138@9677: break; peter1138@9207: peter1138@9677: case SNGRFS_SET_PARAMETERS: { peter1138@9677: /* Parse our new "int list" */ peter1138@9677: GRFConfig *c = this->sel; peter1138@9677: c->num_params = parse_intlist(str, (int*)c->param, lengthof(c->param)); peter1138@9677: peter1138@9677: /* parse_intlist returns -1 on error */ peter1138@9677: if (c->num_params == (byte)-1) c->num_params = 0; peter1138@9677: peter1138@9677: this->preset = -1; peter1138@9677: this->SetDirty(); peter1138@9677: break; peter1138@9677: } peter1138@9677: } peter1138@9207: } peter1138@9207: peter1138@9207: virtual void OnResize(Point new_size, Point delta) peter1138@9207: { peter1138@9207: if (delta.x != 0) { peter1138@9207: ResizeButtons(this, SNGRFS_ADD, SNGRFS_MOVE_DOWN); peter1138@9207: ResizeButtons(this, SNGRFS_SET_PARAMETERS, SNGRFS_APPLY_CHANGES); peter1138@9207: } peter1138@9207: peter1138@9207: this->vscroll.cap += delta.y / 14; peter1138@9207: this->widget[SNGRFS_FILE_LIST].data = (this->vscroll.cap << 8) + 1; peter1138@9207: peter1138@9207: this->SetupNewGRFWindow(); peter1138@9207: } peter1138@9207: peter1138@9207: virtual void OnInvalidateData(int data = 0) peter1138@9207: { peter1138@9677: this->preset = -1; peter1138@9207: this->SetupNewGRFWindow(); peter1138@9207: } peter1138@9207: }; peter1138@5237: skidd13@8025: /* Widget definition of the manage newgrfs window */ peter1138@5237: static const Widget _newgrf_widgets[] = { belugas@9760: { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_MAUVE, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, // SNGRFS_CLOSEBOX belugas@9760: { WWT_CAPTION, RESIZE_RIGHT, COLOUR_MAUVE, 11, 299, 0, 13, STR_NEWGRF_SETTINGS_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, // SNGRFS_CAPTION belugas@9760: { WWT_PANEL, RESIZE_RIGHT, COLOUR_MAUVE, 0, 299, 14, 41, STR_NULL, STR_NULL }, // SNGRFS_BACKGROUND1 belugas@9760: { WWT_DROPDOWN, RESIZE_RIGHT, COLOUR_YELLOW, 10, 103, 16, 27, STR_EMPTY, STR_NEWGRF_PRESET_LIST_TIP }, // SNGRFS_PRESET_LIST belugas@9760: { WWT_PUSHTXTBTN, RESIZE_LR, COLOUR_YELLOW, 104, 196, 16, 27, STR_NEWGRF_PRESET_SAVE, STR_NEWGRF_PRESET_SAVE_TIP }, // SNGRFS_PRESET_SAVE belugas@9760: { WWT_PUSHTXTBTN, RESIZE_LR, COLOUR_YELLOW, 197, 289, 16, 27, STR_NEWGRF_PRESET_DELETE, STR_NEWGRF_PRESET_DELETE_TIP }, // SNGRFS_PRESET_DELETE belugas@9760: { WWT_PANEL, RESIZE_RIGHT, COLOUR_MAUVE, 0, 299, 30, 45, STR_NULL, STR_NULL }, // SNGRFS_BACKGROUND belugas@9760: { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_YELLOW, 10, 79, 32, 43, STR_NEWGRF_ADD, STR_NEWGRF_ADD_TIP }, // SNGRFS_ADD belugas@9760: { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_YELLOW, 80, 149, 32, 43, STR_NEWGRF_REMOVE, STR_NEWGRF_REMOVE_TIP }, // SNGRFS_REMOVE belugas@9760: { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_YELLOW, 150, 219, 32, 43, STR_NEWGRF_MOVEUP, STR_NEWGRF_MOVEUP_TIP }, // SNGRFS_MOVE_UP belugas@9760: { WWT_PUSHTXTBTN, RESIZE_RIGHT, COLOUR_YELLOW, 220, 289, 32, 43, STR_NEWGRF_MOVEDOWN, STR_NEWGRF_MOVEDOWN_TIP }, // SNGRFS_MOVE_DOWN belugas@9760: { WWT_MATRIX, RESIZE_RB, COLOUR_MAUVE, 0, 287, 46, 115, 0x501, STR_NEWGRF_FILE_TIP }, // SNGRFS_FILE_LIST belugas@9760: { WWT_SCROLLBAR, RESIZE_LRB, COLOUR_MAUVE, 288, 299, 46, 115, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST }, // SNGRFS_SCROLLBAR rubidium@10066: { WWT_PANEL, RESIZE_RTB, COLOUR_MAUVE, 0, 299, 116, 238, STR_NULL, STR_NULL }, // SNGRFS_NEWGRF_INFO rubidium@10066: { WWT_PUSHTXTBTN, RESIZE_TB, COLOUR_MAUVE, 0, 95, 239, 250, STR_NEWGRF_SET_PARAMETERS, STR_NULL }, // SNGRFS_SET_PARAMETERS rubidium@10066: { WWT_PUSHTXTBTN, RESIZE_RTB, COLOUR_MAUVE, 96, 191, 239, 250, STR_NEWGRF_TOGGLE_PALETTE, STR_NEWGRF_TOGGLE_PALETTE_TIP }, // SNGRFS_TOGGLE_PALETTE rubidium@10066: { WWT_PUSHTXTBTN, RESIZE_RTB, COLOUR_MAUVE, 192, 287, 239, 250, STR_NEWGRF_APPLY_CHANGES, STR_NULL }, // SNGRFS_APPLY_CHANGES rubidium@10066: { WWT_RESIZEBOX, RESIZE_LRTB, COLOUR_MAUVE, 288, 299, 239, 250, 0x0, STR_RESIZE_BUTTON }, // SNGRFS_RESIZE peter1138@5237: { WIDGETS_END }, peter1138@5237: }; peter1138@5237: skidd13@8025: /* Window definition of the manage newgrfs window */ peter1138@5237: static const WindowDesc _newgrf_desc = { rubidium@10066: WDP_CENTER, WDP_CENTER, 300, 251, 300, 251, rubidium@5893: WC_GAME_OPTIONS, WC_NONE, peter1138@5237: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, peter1138@5237: _newgrf_widgets, peter1138@5237: }; peter1138@5237: peter1138@9207: /** Callback function for the newgrf 'apply changes' confirmation window peter1138@9207: * @param w Window which is calling this callback peter1138@9207: * @param confirmed boolean value, true when yes was clicked, false otherwise peter1138@9207: */ peter1138@9207: static void NewGRFConfirmationCallback(Window *w, bool confirmed) peter1138@9207: { peter1138@9207: if (confirmed) { peter1138@9207: NewGRFWindow *nw = dynamic_cast(w); peter1138@9207: GRFConfig *c; peter1138@9207: int i = 0; peter1138@9207: smatz@9457: GamelogStartAction(GLAT_GRF); smatz@9457: GamelogGRFUpdate(_grfconfig, nw->list); // log GRF changes peter1138@9207: CopyGRFConfigList(nw->orig_list, nw->list, false); peter1138@9207: ReloadNewGRFData(); smatz@9457: GamelogStopAction(); peter1138@9207: peter1138@9207: /* Show new, updated list */ peter1138@9207: for (c = nw->list; c != NULL && c != nw->sel; c = c->next, i++) {} peter1138@9207: CopyGRFConfigList(&nw->list, *nw->orig_list, false); peter1138@9207: for (c = nw->list; c != NULL && i > 0; c = c->next, i--) {} peter1138@9207: nw->sel = c; peter1138@9207: peter1138@9207: w->SetDirty(); peter1138@9207: } peter1138@9207: } peter1138@9207: peter1138@9207: peter1138@5237: Darkvater@5352: /** Setup the NewGRF gui Darkvater@5352: * @param editable allow the user to make changes to the grfconfig in the window Darkvater@5352: * @param show_params show information about what parameters are set for the grf files Darkvater@5352: * @param exec_changes if changes are made to the list (editable is true), apply these Darkvater@5352: * changes immediately or only update the list Darkvater@5352: * @param config pointer to a linked-list of grfconfig's that will be shown */ Darkvater@5352: void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config) peter1138@5237: { peter1138@5237: DeleteWindowByClass(WC_GAME_OPTIONS); peter1138@9207: new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config); peter1138@5237: }