peter1138@5237: /* $Id$ */ peter1138@5237: belugas@6348: /** @file newgrf_gui.cpp */ 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" 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) { maedhros@6139: SetDParamStr(0, c->filename); maedhros@6873: SetDParamStr(1, c->error->data); maedhros@6139: for (uint i = 0; i < c->error->num_params; i++) { maedhros@6139: uint32 param = 0; maedhros@6139: byte param_number = c->error->param_number[i]; maedhros@6139: maedhros@6139: if (param_number < c->num_params) param = c->param[param_number]; maedhros@6139: maedhros@6139: SetDParam(2 + i, param); maedhros@6139: } maedhros@6139: maedhros@6139: char message[512]; maedhros@6873: GetString(message, c->error->custom_message != NULL ? BindCString(c->error->custom_message) : c->error->message, 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)); peter1138@5237: SetDParamStr(0, 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: } peter1138@5237: 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)) { peter1138@5237: SetDParamStr(0, 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@5237: /* Dialogue for adding NewGRF files to the selection */ rubidium@6248: struct newgrf_add_d { peter1138@5237: GRFConfig **list; peter1138@5237: const GRFConfig *sel; rubidium@6248: }; peter1138@5237: skidd13@8025: /* Names of the add a newgrf window widgets */ skidd13@8025: enum AddNewGRFWindowWidgets { skidd13@8025: ANGRFW_CLOSEBOX = 0, skidd13@8025: ANGRFW_CAPTION, skidd13@8025: ANGRFW_BACKGROUND, skidd13@8025: ANGRFW_GRF_LIST, skidd13@8025: ANGRFW_SCROLLBAR, skidd13@8025: ANGRFW_GRF_INFO, skidd13@8025: ANGRFW_ADD, skidd13@8025: ANGRFW_RESCAN, skidd13@8025: ANGRFW_RESIZE, skidd13@8025: }; peter1138@5237: peter1138@5237: static void NewGRFAddDlgWndProc(Window *w, WindowEvent *e) peter1138@5237: { peter1138@5237: switch (e->event) { peter1138@5237: case WE_PAINT: { peter1138@5237: const GRFConfig *c; skidd13@8025: const Widget *wl = &w->widget[ANGRFW_GRF_LIST]; glx@8070: int n = 0; peter1138@5237: peter1138@5237: /* Count the number of GRFs */ peter1138@5237: for (c = _all_grfs; c != NULL; c = c->next) n++; peter1138@5237: skidd13@8025: w->vscroll.cap = (wl->bottom - wl->top) / 10; peter1138@5237: SetVScrollCount(w, n); peter1138@5237: skidd13@8025: w->SetWidgetDisabledState(ANGRFW_ADD, WP(w, newgrf_add_d).sel == NULL || WP(w, newgrf_add_d).sel->IsOpenTTDBaseGRF()); peter1138@5237: DrawWindowWidgets(w); peter1138@5237: skidd13@8025: GfxFillRect(wl->left + 1, wl->top + 1, wl->right, wl->bottom, 0xD7); peter1138@5237: skidd13@8025: uint y = wl->top + 1; skidd13@8025: for (c = _all_grfs, n = 0; c != NULL && n < (w->vscroll.pos + w->vscroll.cap); c = c->next, n++) { skidd13@8025: if (n >= w->vscroll.pos) { peter1138@5237: bool h = c == WP(w, newgrf_add_d).sel; tron@5638: const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; peter1138@5237: peter1138@5237: /* Draw selection background */ peter1138@5237: if (h) GfxFillRect(3, y, w->width - 15, y + 9, 156); belugas@7824: DoDrawStringTruncated(text, 4, y, h ? TC_WHITE : TC_ORANGE, w->width - 18); peter1138@5237: y += 10; peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@5237: if (WP(w, newgrf_add_d).sel != NULL) { skidd13@8025: const Widget *wi = &w->widget[ANGRFW_GRF_INFO]; maedhros@6377: ShowNewGRFInfo(WP(w, newgrf_add_d).sel, wi->left + 2, wi->top + 2, wi->right - wi->left - 2, wi->bottom, false); peter1138@5237: } peter1138@5237: break; peter1138@5237: } peter1138@5237: glx@7011: case WE_DOUBLE_CLICK: skidd13@8025: if (e->we.click.widget != ANGRFW_GRF_LIST) break; skidd13@8025: e->we.click.widget = ANGRFW_ADD; glx@7011: /* Fall through */ glx@7011: peter1138@5237: case WE_CLICK: peter1138@5237: switch (e->we.click.widget) { skidd13@8025: case ANGRFW_GRF_LIST: { belugas@6348: /* Get row... */ peter1138@5237: const GRFConfig *c; skidd13@8025: uint i = (e->we.click.pt.y - w->widget[ANGRFW_GRF_LIST].top) / 10 + w->vscroll.pos; peter1138@5237: peter1138@5237: for (c = _all_grfs; c != NULL && i > 0; c = c->next, i--); peter1138@5237: WP(w, newgrf_add_d).sel = c; peter1138@5237: SetWindowDirty(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: skidd13@8025: case ANGRFW_ADD: // Add selection to list peter1138@5237: if (WP(w, newgrf_add_d).sel != NULL) { peter1138@5237: const GRFConfig *src = WP(w, newgrf_add_d).sel; KUDr@5609: GRFConfig **list; peter1138@5243: peter1138@5243: /* Find last entry in the list, checking for duplicate grfid on the way */ peter1138@5243: for (list = WP(w, newgrf_add_d).list; *list != NULL; list = &(*list)->next) { peter1138@5243: if ((*list)->grfid == src->grfid) { peter1138@5243: ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DUPLICATE_GRFID, 0, 0); peter1138@5243: return; peter1138@5243: } peter1138@5243: } peter1138@5243: peter1138@5243: /* Copy GRF details from scanned list */ KUDr@5609: GRFConfig *c = CallocT(1); peter1138@5237: *c = *src; peter1138@5237: c->filename = strdup(src->filename); rubidium@6783: if (src->name != NULL) c->name = strdup(src->name); rubidium@6783: if (src->info != NULL) c->info = strdup(src->info); peter1138@5237: c->next = NULL; peter1138@5243: peter1138@5243: /* Append GRF config to configuration list */ peter1138@5243: *list = c; peter1138@5237: peter1138@5237: DeleteWindowByClass(WC_SAVELOAD); peter1138@5237: InvalidateWindowData(WC_GAME_OPTIONS, 0); peter1138@5237: } peter1138@5237: break; peter1138@5237: skidd13@8025: case ANGRFW_RESCAN: // Rescan list peter1138@5237: WP(w, newgrf_add_d).sel = NULL; peter1138@5237: ScanNewGRFFiles(); peter1138@5237: SetWindowDirty(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: break; peter1138@5237: } peter1138@5237: } peter1138@5237: skidd13@8025: /* Widget definition for the add a newgrf window */ peter1138@5237: static const Widget _newgrf_add_dlg_widgets[] = { skidd13@8025: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, // ANGRFW_CLOSEBOX skidd13@8025: { WWT_CAPTION, RESIZE_RIGHT, 14, 11, 306, 0, 13, STR_NEWGRF_ADD_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, // ANGRFW_CAPTION skidd13@8025: { WWT_PANEL, RESIZE_RB, 14, 0, 294, 14, 121, 0x0, STR_NULL }, // ANGRFW_BACKGROUND skidd13@8025: { WWT_INSET, RESIZE_RB, 14, 2, 292, 16, 119, 0x0, STR_NULL }, // ANGRFW_GRF_LIST skidd13@8025: { WWT_SCROLLBAR, RESIZE_LRB, 14, 295, 306, 14, 121, 0x0, STR_NULL }, // ANGRFW_SCROLLBAR skidd13@8025: { WWT_PANEL, RESIZE_RTB, 14, 0, 306, 122, 224, 0x0, STR_NULL }, // ANGRFW_GRF_INFO skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_RTB, 14, 0, 146, 225, 236, STR_NEWGRF_ADD_FILE, STR_NEWGRF_ADD_FILE_TIP }, // ANGRFW_ADD skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_LRTB, 14, 147, 294, 225, 236, STR_NEWGRF_RESCAN_FILES, STR_NEWGRF_RESCAN_FILES_TIP }, // ANGRFW_RESCAN skidd13@8025: { WWT_RESIZEBOX, RESIZE_LRTB, 14, 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: NewGRFAddDlgWndProc, peter1138@5237: }; peter1138@5237: peter1138@5237: peter1138@5237: /* 'NewGRF Settings' dialogue */ rubidium@6248: struct newgrf_d { Darkvater@5352: GRFConfig **orig_list; ///< grf list the window is shown with Darkvater@5352: 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 rubidium@6248: }; peter1138@5237: assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(newgrf_d)); peter1138@5237: Darkvater@5352: skidd13@8025: /* Names of the manage newgrfs window widgets */ Darkvater@5345: enum ShowNewGRFStateWidgets { skidd13@8025: SNGRFS_CLOSEBOX = 0, skidd13@8025: SNGRFS_CAPTION, skidd13@8025: SNGRFS_BACKGROUND, skidd13@8025: SNGRFS_ADD, Darkvater@5345: SNGRFS_REMOVE, Darkvater@5345: SNGRFS_MOVE_UP, Darkvater@5345: SNGRFS_MOVE_DOWN, skidd13@8025: SNGRFS_FILE_LIST, skidd13@8025: SNGRFS_SCROLLBAR, skidd13@8025: SNGRFS_NEWGRF_INFO, Darkvater@5345: SNGRFS_SET_PARAMETERS, Darkvater@5352: SNGRFS_APPLY_CHANGES, skidd13@8025: SNGRFS_RESIZE, Darkvater@5345: }; peter1138@5237: peter1138@5237: static void SetupNewGRFState(Window *w) peter1138@5237: { peter1138@5237: bool disable_all = WP(w, newgrf_d).sel == NULL || !WP(w, newgrf_d).editable; peter1138@5237: rubidium@7997: w->SetWidgetDisabledState(3, !WP(w, newgrf_d).editable); rubidium@7997: w->SetWidgetsDisabledState(disable_all, Darkvater@5345: SNGRFS_REMOVE, Darkvater@5345: SNGRFS_MOVE_UP, Darkvater@5345: SNGRFS_MOVE_DOWN, Darkvater@5345: WIDGET_LIST_END Darkvater@5345: ); rubidium@7997: w->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !WP(w, newgrf_d).show_params || disable_all); peter1138@5237: peter1138@5237: if (!disable_all) { peter1138@5237: /* All widgets are now enabled, so disable widgets we can't use */ rubidium@7997: if (WP(w, newgrf_d).sel == *WP(w, newgrf_d).list) w->DisableWidget(SNGRFS_MOVE_UP); rubidium@7997: if (WP(w, newgrf_d).sel->next == NULL) w->DisableWidget(SNGRFS_MOVE_DOWN); rubidium@7997: if (WP(w, newgrf_d).sel->IsOpenTTDBaseGRF()) w->DisableWidget(SNGRFS_REMOVE); peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@5237: peter1138@5237: static void SetupNewGRFWindow(Window *w) peter1138@5237: { Darkvater@5345: const GRFConfig *c; peter1138@5237: int i; peter1138@5237: peter1138@5237: for (c = *WP(w, newgrf_d).list, i = 0; c != NULL; c = c->next, i++); peter1138@5237: Darkvater@5345: w->vscroll.cap = (w->widget[SNGRFS_FILE_LIST].bottom - w->widget[SNGRFS_FILE_LIST].top) / 14 + 1; peter1138@5237: SetVScrollCount(w, i); rubidium@7997: w->SetWidgetDisabledState(SNGRFS_APPLY_CHANGES, !WP(w, newgrf_d).editable); Darkvater@5352: } Darkvater@5352: Darkvater@5352: Darkvater@5352: /** Callback function for the newgrf 'apply changes' confirmation window belugas@6488: * @param w Window which is calling this callback belugas@6488: * @param confirmed boolean value, true when yes was clicked, false otherwise belugas@6488: */ Darkvater@5418: static void NewGRFConfirmationCallback(Window *w, bool confirmed) Darkvater@5352: { Darkvater@5418: if (confirmed) { Darkvater@5352: newgrf_d *nd = &WP(w, newgrf_d); Darkvater@5635: GRFConfig *c; Darkvater@5635: int i = 0; Darkvater@5352: glx@6956: CopyGRFConfigList(nd->orig_list, *nd->list, false); Darkvater@5352: ReloadNewGRFData(); Darkvater@5635: Darkvater@5635: /* Show new, updated list */ Darkvater@5635: for (c = *nd->list; c != NULL && c != nd->sel; c = c->next, i++); glx@6956: CopyGRFConfigList(nd->list, *nd->orig_list, false); Darkvater@5635: for (c = *nd->list; c != NULL && i > 0; c = c->next, i--); Darkvater@5635: nd->sel = c; Darkvater@5635: Darkvater@5635: SetWindowDirty(w); Darkvater@5352: } peter1138@5237: } peter1138@5237: peter1138@5237: peter1138@5237: static void NewGRFWndProc(Window *w, WindowEvent *e) peter1138@5237: { peter1138@5237: switch (e->event) { peter1138@5237: case WE_PAINT: { Darkvater@5345: const GRFConfig *c; peter1138@5237: int i, y; peter1138@5237: peter1138@5237: SetupNewGRFState(w); peter1138@5237: peter1138@5237: DrawWindowWidgets(w); peter1138@5237: peter1138@5237: /* Draw NewGRF list */ Darkvater@5345: y = w->widget[SNGRFS_FILE_LIST].top; peter1138@5237: for (c = *WP(w, newgrf_d).list, i = 0; c != NULL; c = c->next, i++) { peter1138@5237: if (i >= w->vscroll.pos && i < w->vscroll.pos + w->vscroll.cap) { tron@5638: const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; peter1138@5668: SpriteID pal; belugas@7737: byte txtoffset; peter1138@5237: peter1138@5237: /* Pick a colour */ maedhros@6229: switch (c->status) { maedhros@6229: case GCS_NOT_FOUND: maedhros@6229: case GCS_DISABLED: maedhros@6229: pal = PALETTE_TO_RED; maedhros@6229: break; maedhros@6229: case GCS_ACTIVATED: maedhros@6229: pal = PALETTE_TO_GREEN; maedhros@6229: break; maedhros@6229: default: maedhros@6229: pal = PALETTE_TO_BLUE; maedhros@6229: break; maedhros@6229: } maedhros@6229: rubidium@7779: /* Do not show a "not-failure" colour when it actually failed to load */ rubidium@7779: if (pal != PALETTE_TO_RED) { skidd13@7928: if (HasBit(c->flags, GCF_STATIC)) { rubidium@7779: pal = PALETTE_TO_GREY; skidd13@7928: } else if (HasBit(c->flags, GCF_COMPATIBLE)) { rubidium@7779: pal = PALETTE_TO_ORANGE; rubidium@7779: } peter1138@5237: } peter1138@5237: peter1138@5668: DrawSprite(SPR_SQUARE, pal, 5, y + 2); maedhros@6103: if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, 20, y + 2); belugas@7737: txtoffset = c->error != NULL ? 35 : 25; belugas@7824: DoDrawStringTruncated(text, txtoffset, y + 3, WP(w, newgrf_d).sel == c ? TC_WHITE : TC_BLACK, w->width - txtoffset - 10); peter1138@5237: y += 14; peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@5237: if (WP(w, newgrf_d).sel != NULL) { peter1138@5237: /* Draw NewGRF file info */ Darkvater@5345: const Widget *wi = &w->widget[SNGRFS_NEWGRF_INFO]; maedhros@6377: ShowNewGRFInfo(WP(w, newgrf_d).sel, wi->left + 2, wi->top + 2, wi->right - wi->left - 2, wi->bottom, WP(w, newgrf_d).show_params); peter1138@5237: } peter1138@5237: peter1138@5237: break; peter1138@5237: } peter1138@5237: peter1138@5241: case WE_INVALIDATE_DATA: peter1138@5241: SetupNewGRFWindow(w); peter1138@5241: break; peter1138@5241: peter1138@5237: case WE_CLICK: peter1138@5237: switch (e->we.click.widget) { belugas@6348: case SNGRFS_ADD: { // Add GRF peter1138@5237: GRFConfig **list = WP(w, newgrf_d).list; peter1138@5237: Window *w; peter1138@5237: peter1138@5237: DeleteWindowByClass(WC_SAVELOAD); peter1138@5237: w = AllocateWindowDesc(&_newgrf_add_dlg_desc); peter1138@5237: w->resize.step_height = 10; peter1138@5237: peter1138@5237: WP(w, newgrf_add_d).list = list; peter1138@5237: break; peter1138@5237: } peter1138@5237: belugas@6348: case SNGRFS_REMOVE: { // Remove GRF peter1138@5248: GRFConfig **pc, *c, *newsel; peter1138@5248: peter1138@5248: /* Choose the next GRF file to be the selected file */ peter1138@5248: newsel = WP(w, newgrf_d).sel->next; peter1138@5248: peter1138@5237: for (pc = WP(w, newgrf_d).list; (c = *pc) != NULL; pc = &c->next) { peter1138@5248: /* If the new selection is empty (i.e. we're deleting the last item peter1138@5248: * in the list, pick the file just before the selected file */ peter1138@5248: if (newsel == NULL && c->next == WP(w, newgrf_d).sel) newsel = c; peter1138@5248: peter1138@5237: if (c == WP(w, newgrf_d).sel) { peter1138@5237: *pc = c->next; peter1138@5237: free(c); peter1138@5237: break; peter1138@5237: } peter1138@5237: } peter1138@5248: peter1138@5248: WP(w, newgrf_d).sel = newsel; peter1138@5237: SetupNewGRFWindow(w); peter1138@5237: SetWindowDirty(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: belugas@6348: case SNGRFS_MOVE_UP: { // Move GRF up peter1138@5237: GRFConfig **pc, *c; peter1138@5237: if (WP(w, newgrf_d).sel == NULL) break; peter1138@5237: peter1138@5237: for (pc = WP(w, newgrf_d).list; (c = *pc) != NULL; pc = &c->next) { peter1138@5237: if (c->next == WP(w, newgrf_d).sel) { peter1138@5237: c->next = WP(w, newgrf_d).sel->next; peter1138@5237: WP(w, newgrf_d).sel->next = c; peter1138@5237: *pc = WP(w, newgrf_d).sel; peter1138@5237: break; peter1138@5237: } peter1138@5237: } peter1138@5237: SetWindowDirty(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: belugas@6348: case SNGRFS_MOVE_DOWN: { // Move GRF down peter1138@5237: GRFConfig **pc, *c; peter1138@5237: if (WP(w, newgrf_d).sel == NULL) break; peter1138@5237: peter1138@5237: for (pc = WP(w, newgrf_d).list; (c = *pc) != NULL; pc = &c->next) { peter1138@5237: if (c == WP(w, newgrf_d).sel) { peter1138@5237: *pc = c->next; peter1138@5237: c->next = c->next->next; peter1138@5237: (*pc)->next = c; peter1138@5237: break; peter1138@5237: } peter1138@5237: } peter1138@5237: SetWindowDirty(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: belugas@6348: case SNGRFS_FILE_LIST: { // Select a GRF peter1138@5237: GRFConfig *c; Darkvater@5345: uint i = (e->we.click.pt.y - w->widget[SNGRFS_FILE_LIST].top) / 14 + w->vscroll.pos; peter1138@5237: peter1138@5237: for (c = *WP(w, newgrf_d).list; c != NULL && i > 0; c = c->next, i--); peter1138@5237: WP(w, newgrf_d).sel = c; peter1138@5237: peter1138@5237: SetWindowDirty(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: belugas@6348: case SNGRFS_APPLY_CHANGES: // Apply changes made to GRF list Darkvater@5352: if (WP(w, newgrf_d).execute) { Darkvater@5352: ShowQuery( Darkvater@5352: STR_POPUP_CAUTION_CAPTION, Darkvater@5352: STR_NEWGRF_CONFIRMATION_TEXT, Darkvater@5418: w, Darkvater@5418: NewGRFConfirmationCallback Darkvater@5352: ); Darkvater@5352: } else { glx@6956: CopyGRFConfigList(WP(w, newgrf_d).orig_list, *WP(w, newgrf_d).list, true); glx@6956: ResetGRFConfig(false); glx@6956: ReloadNewGRFData(); Darkvater@5352: } Darkvater@5352: break; Darkvater@5352: belugas@6348: case SNGRFS_SET_PARAMETERS: { // Edit parameters peter1138@5237: char buff[512]; peter1138@5237: if (WP(w, newgrf_d).sel == NULL) break; peter1138@5237: peter1138@5308: GRFBuildParamList(buff, WP(w, newgrf_d).sel, lastof(buff)); Darkvater@5431: ShowQueryString(BindCString(buff), STR_NEWGRF_PARAMETER_QUERY, 63, 250, w, CS_ALPHANUMERAL); peter1138@5237: break; peter1138@5237: } peter1138@5237: } peter1138@5237: break; peter1138@5237: peter1138@5237: case WE_ON_EDIT_TEXT: peter1138@5237: if (e->we.edittext.str != NULL) { peter1138@5237: /* Parse our new "int list" */ peter1138@5237: GRFConfig *c = WP(w, newgrf_d).sel; peter1138@5237: c->num_params = parse_intlist(e->we.edittext.str, (int*)c->param, lengthof(c->param)); peter1138@5237: peter1138@5237: /* parse_intlist returns -1 on error */ peter1138@5237: if (c->num_params == (byte)-1) c->num_params = 0; peter1138@5237: } peter1138@5237: SetWindowDirty(w); peter1138@5237: break; peter1138@5237: Darkvater@5352: case WE_DESTROY: Darkvater@6166: if (!WP(w, newgrf_d).execute) { glx@6956: CopyGRFConfigList(WP(w, newgrf_d).orig_list, *WP(w, newgrf_d).list, true); glx@6956: ResetGRFConfig(false); glx@6956: ReloadNewGRFData(); Darkvater@6166: } Darkvater@5352: /* Remove the temporary copy of grf-list used in window */ Darkvater@5352: ClearGRFConfigList(WP(w, newgrf_d).list); Darkvater@5352: break; Darkvater@5352: peter1138@5237: case WE_RESIZE: peter1138@5237: w->vscroll.cap += e->we.sizing.diff.y / 14; Darkvater@5345: w->widget[SNGRFS_FILE_LIST].data = (w->vscroll.cap << 8) + 1; rubidium@7680: SetupNewGRFWindow(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: } peter1138@5237: skidd13@8025: /* Widget definition of the manage newgrfs window */ peter1138@5237: static const Widget _newgrf_widgets[] = { skidd13@8025: { WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, // SNGRFS_CLOSEBOX skidd13@8025: { WWT_CAPTION, RESIZE_RIGHT, 10, 11, 299, 0, 13, STR_NEWGRF_SETTINGS_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, // SNGRFS_CAPTION skidd13@8025: { WWT_PANEL, RESIZE_RIGHT, 10, 0, 299, 14, 29, STR_NULL, STR_NULL }, // SNGRFS_BACKGROUND skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 10, 79, 16, 27, STR_NEWGRF_ADD, STR_NEWGRF_ADD_TIP }, // SNGRFS_ADD skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 80, 149, 16, 27, STR_NEWGRF_REMOVE, STR_NEWGRF_REMOVE_TIP }, // SNGRFS_REMOVE skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 150, 219, 16, 27, STR_NEWGRF_MOVEUP, STR_NEWGRF_MOVEUP_TIP }, // SNGRFS_MOVE_UP skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 220, 289, 16, 27, STR_NEWGRF_MOVEDOWN, STR_NEWGRF_MOVEDOWN_TIP }, // SNGRFS_MOVE_DOWN skidd13@8025: { WWT_MATRIX, RESIZE_RB, 10, 0, 287, 30, 99, 0x501, STR_NEWGRF_FILE_TIP }, // SNGRFS_FILE_LIST skidd13@8025: { WWT_SCROLLBAR, RESIZE_LRB, 10, 288, 299, 30, 99, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST }, // SNGRFS_SCROLLBAR skidd13@8025: { WWT_PANEL, RESIZE_RTB, 10, 0, 299, 100, 212, STR_NULL, STR_NULL }, // SNGRFS_NEWGRF_INFO skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_TB, 10, 0, 143, 213, 224, STR_NEWGRF_SET_PARAMETERS, STR_NULL }, // SNGRFS_SET_PARAMETERS skidd13@8025: { WWT_PUSHTXTBTN, RESIZE_RTB, 10, 144, 287, 213, 224, STR_NEWGRF_APPLY_CHANGES, STR_NULL }, // SNGRFS_APPLY_CHANGES skidd13@8025: { WWT_RESIZEBOX, RESIZE_LRTB, 10, 288, 299, 213, 224, 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@7341: WDP_CENTER, WDP_CENTER, 300, 225, 300, 225, 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: NewGRFWndProc, peter1138@5237: }; peter1138@5237: 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: { Darkvater@5352: static GRFConfig *local = NULL; peter1138@5237: Window *w; peter1138@5237: peter1138@5237: DeleteWindowByClass(WC_GAME_OPTIONS); peter1138@5237: w = AllocateWindowDesc(&_newgrf_desc); peter1138@5237: if (w == NULL) return; peter1138@5237: peter1138@5237: w->resize.step_height = 14; glx@6956: CopyGRFConfigList(&local, *config, false); peter1138@5237: peter1138@5237: /* Clear selections */ peter1138@5237: WP(w, newgrf_d).sel = NULL; Darkvater@5352: WP(w, newgrf_d).list = &local; Darkvater@5352: WP(w, newgrf_d).orig_list = config; peter1138@5237: WP(w, newgrf_d).editable = editable; Darkvater@5352: WP(w, newgrf_d).execute = exec_changes; peter1138@5237: WP(w, newgrf_d).show_params = show_params; peter1138@5237: peter1138@5237: SetupNewGRFWindow(w); peter1138@5237: }