peter1138@5237: /* $Id$ */ peter1138@5237: belugas@6674: /** @file newgrf_gui.cpp */ belugas@6674: peter1138@5237: #include "stdafx.h" peter1138@5237: #include "openttd.h" peter1138@5237: #include "functions.h" peter1138@5237: #include "variables.h" peter1138@5237: #include "gfx.h" peter1138@5237: #include "gui.h" peter1138@5237: #include "window.h" peter1138@5237: #include "table/strings.h" peter1138@5237: #include "table/sprites.h" Darkvater@5352: #include "newgrf.h" peter1138@5237: #include "newgrf_config.h" maedhros@6465: #include "strings.h" rubidium@5838: #include "helpers.hpp" peter1138@5237: 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@6703: static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint w, uint bottom, bool show_params) peter1138@5237: { Darkvater@5885: char buff[256]; peter1138@5237: maedhros@6429: if (c->error != NULL) { maedhros@6465: SetDParamStr(0, c->filename); maedhros@7369: SetDParamStr(1, c->error->data); maedhros@6465: for (uint i = 0; i < c->error->num_params; i++) { maedhros@6465: uint32 param = 0; maedhros@6465: byte param_number = c->error->param_number[i]; maedhros@6465: maedhros@6465: if (param_number < c->num_params) param = c->param[param_number]; maedhros@6465: maedhros@6465: SetDParam(2 + i, param); maedhros@6465: } maedhros@6465: maedhros@6465: char message[512]; maedhros@7369: GetString(message, c->error->custom_message != NULL ? BindCString(c->error->custom_message) : c->error->message, lastof(message)); maedhros@6465: maedhros@6465: SetDParamStr(0, message); maedhros@6703: y += DrawStringMultiLine(x, y, c->error->severity, w, bottom - y); maedhros@6429: } maedhros@6429: 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@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_FILENAME, w, bottom - y); rubidium@5339: } peter1138@5237: peter1138@5237: /* Prepare and draw GRF ID */ Darkvater@5894: snprintf(buff, lengthof(buff), "%08X", BSWAP32(c->grfid)); peter1138@5237: SetDParamStr(0, buff); maedhros@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_GRF_ID, w, bottom - y); peter1138@5237: peter1138@5237: /* Prepare and draw MD5 sum */ Darkvater@5885: md5sumToString(buff, lastof(buff), c->md5sum); peter1138@5237: SetDParamStr(0, buff); maedhros@6703: 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@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_PARAMETER, w, bottom - y); peter1138@5237: } peter1138@5237: peter1138@5237: /* Show flags */ maedhros@6703: if (c->status == GCS_NOT_FOUND) y += DrawStringMultiLine(x, y, STR_NEWGRF_NOT_FOUND, w, bottom - y); maedhros@6703: if (c->status == GCS_DISABLED) y += DrawStringMultiLine(x, y, STR_NEWGRF_DISABLED, w, bottom - y); skidd13@8424: 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@5889: if (c->info != NULL && !StrEmpty(c->info)) { peter1138@5237: SetDParamStr(0, c->info); maedhros@6703: y += DrawStringMultiLine(x, y, STR_02BD, w, bottom - y); peter1138@5237: } else { maedhros@6703: 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@6574: struct newgrf_add_d { peter1138@5237: GRFConfig **list; peter1138@5237: const GRFConfig *sel; rubidium@6574: }; peter1138@5237: 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; peter1138@5237: int y; peter1138@5237: 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: peter1138@5237: w->vscroll.cap = (w->widget[3].bottom - w->widget[3].top) / 10; peter1138@5237: SetVScrollCount(w, n); peter1138@5237: rubidium@8493: w->SetWidgetDisabledState(6, WP(w, newgrf_add_d).sel == NULL || WP(w, newgrf_add_d).sel->IsOpenTTDBaseGRF()); peter1138@5237: DrawWindowWidgets(w); peter1138@5237: peter1138@5237: GfxFillRect(w->widget[3].left + 1, w->widget[3].top + 1, w->widget[3].right, w->widget[3].bottom, 0xD7); peter1138@5237: peter1138@5237: n = 0; peter1138@5237: y = w->widget[3].top + 1; peter1138@5237: peter1138@5237: for (c = _all_grfs; c != NULL; c = c->next) { peter1138@5237: if (n >= w->vscroll.pos && n < w->vscroll.pos + w->vscroll.cap) { peter1138@5237: bool h = c == WP(w, newgrf_add_d).sel; tron@5889: 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@8320: DoDrawStringTruncated(text, 4, y, h ? TC_WHITE : TC_ORANGE, w->width - 18); peter1138@5237: y += 10; peter1138@5237: } peter1138@5237: n++; peter1138@5237: } peter1138@5237: peter1138@5237: if (WP(w, newgrf_add_d).sel != NULL) { peter1138@5237: const Widget *wi = &w->widget[5]; maedhros@6703: 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@7507: case WE_DOUBLE_CLICK: glx@7507: if (e->we.click.widget != 3) break; glx@7507: e->we.click.widget = 6; glx@7507: /* Fall through */ glx@7507: peter1138@5237: case WE_CLICK: peter1138@5237: switch (e->we.click.widget) { peter1138@5237: case 3: { belugas@6674: /* Get row... */ peter1138@5237: const GRFConfig *c; peter1138@5237: uint i = (e->we.click.pt.y - w->widget[3].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: belugas@6674: case 6: // 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@5860: 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@5860: GRFConfig *c = CallocT(1); peter1138@5237: *c = *src; peter1138@5237: c->filename = strdup(src->filename); rubidium@7279: if (src->name != NULL) c->name = strdup(src->name); rubidium@7279: 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: belugas@6674: case 7: // 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: peter1138@5237: peter1138@5237: static const Widget _newgrf_add_dlg_widgets[] = { peter1138@5237: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, peter1138@5237: { WWT_CAPTION, RESIZE_RIGHT, 14, 11, 306, 0, 13, STR_NEWGRF_ADD_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, peter1138@5237: peter1138@5237: /* List of files */ rubidium@7837: { WWT_PANEL, RESIZE_RB, 14, 0, 294, 14, 121, 0x0, STR_NULL }, rubidium@7837: { WWT_INSET, RESIZE_RB, 14, 2, 292, 16, 119, 0x0, STR_NULL }, rubidium@7837: { WWT_SCROLLBAR, RESIZE_LRB, 14, 295, 306, 14, 121, 0x0, STR_NULL }, peter1138@5237: peter1138@5237: /* NewGRF file info */ rubidium@7837: { WWT_PANEL, RESIZE_RTB, 14, 0, 306, 122, 224, 0x0, STR_NULL }, peter1138@5237: rubidium@7837: { WWT_PUSHTXTBTN, RESIZE_RTB, 14, 0, 146, 225, 236, STR_NEWGRF_ADD_FILE, STR_NEWGRF_ADD_FILE_TIP }, rubidium@7837: { WWT_PUSHTXTBTN, RESIZE_LRTB, 14, 147, 294, 225, 236, STR_NEWGRF_RESCAN_FILES, STR_NEWGRF_RESCAN_FILES_TIP }, rubidium@7837: { WWT_RESIZEBOX, RESIZE_LRTB, 14, 295, 306, 225, 236, 0x0, STR_RESIZE_BUTTON }, peter1138@5237: { WIDGETS_END }, peter1138@5237: }; peter1138@5237: peter1138@5237: peter1138@5237: static const WindowDesc _newgrf_add_dlg_desc = { rubidium@7837: WDP_CENTER, WDP_CENTER, 307, 237, 307, 337, rubidium@6144: WC_SAVELOAD, WC_NONE, belugas@8515: 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@6574: 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@6574: }; peter1138@5237: assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(newgrf_d)); peter1138@5237: Darkvater@5352: Darkvater@5345: enum ShowNewGRFStateWidgets { Darkvater@5345: SNGRFS_ADD = 3, Darkvater@5345: SNGRFS_REMOVE, Darkvater@5345: SNGRFS_MOVE_UP, Darkvater@5345: SNGRFS_MOVE_DOWN, Darkvater@5345: SNGRFS_FILE_LIST = 7, Darkvater@5345: SNGRFS_NEWGRF_INFO = 9, Darkvater@5345: SNGRFS_SET_PARAMETERS, Darkvater@5352: SNGRFS_APPLY_CHANGES, Darkvater@5345: }; peter1138@5237: Darkvater@5352: 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@8493: w->SetWidgetDisabledState(3, !WP(w, newgrf_d).editable); rubidium@8493: 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@8493: 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@8493: if (WP(w, newgrf_d).sel == *WP(w, newgrf_d).list) w->DisableWidget(SNGRFS_MOVE_UP); rubidium@8493: if (WP(w, newgrf_d).sel->next == NULL) w->DisableWidget(SNGRFS_MOVE_DOWN); rubidium@8493: 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@8493: 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@6984: * @param w Window which is calling this callback belugas@6984: * @param confirmed boolean value, true when yes was clicked, false otherwise belugas@6984: */ Darkvater@5669: static void NewGRFConfirmationCallback(Window *w, bool confirmed) Darkvater@5352: { Darkvater@5669: if (confirmed) { Darkvater@5352: newgrf_d *nd = &WP(w, newgrf_d); Darkvater@5886: GRFConfig *c; Darkvater@5886: int i = 0; Darkvater@5352: glx@7452: CopyGRFConfigList(nd->orig_list, *nd->list, false); Darkvater@5352: ReloadNewGRFData(); Darkvater@5886: Darkvater@5886: /* Show new, updated list */ Darkvater@5886: for (c = *nd->list; c != NULL && c != nd->sel; c = c->next, i++); glx@7452: CopyGRFConfigList(nd->list, *nd->orig_list, false); Darkvater@5886: for (c = *nd->list; c != NULL && i > 0; c = c->next, i--); Darkvater@5886: nd->sel = c; Darkvater@5886: Darkvater@5886: 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@5889: const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; peter1138@5919: SpriteID pal; belugas@8233: byte txtoffset; peter1138@5237: peter1138@5237: /* Pick a colour */ maedhros@6555: switch (c->status) { maedhros@6555: case GCS_NOT_FOUND: maedhros@6555: case GCS_DISABLED: maedhros@6555: pal = PALETTE_TO_RED; maedhros@6555: break; maedhros@6555: case GCS_ACTIVATED: maedhros@6555: pal = PALETTE_TO_GREEN; maedhros@6555: break; maedhros@6555: default: maedhros@6555: pal = PALETTE_TO_BLUE; maedhros@6555: break; maedhros@6555: } maedhros@6555: rubidium@8275: /* Do not show a "not-failure" colour when it actually failed to load */ rubidium@8275: if (pal != PALETTE_TO_RED) { skidd13@8424: if (HasBit(c->flags, GCF_STATIC)) { rubidium@8275: pal = PALETTE_TO_GREY; skidd13@8424: } else if (HasBit(c->flags, GCF_COMPATIBLE)) { rubidium@8275: pal = PALETTE_TO_ORANGE; rubidium@8275: } peter1138@5237: } peter1138@5237: peter1138@5919: DrawSprite(SPR_SQUARE, pal, 5, y + 2); maedhros@6429: if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, 20, y + 2); belugas@8233: txtoffset = c->error != NULL ? 35 : 25; belugas@8320: 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@6703: 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@6674: 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@6674: 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@6674: 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@6674: 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@6674: 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@6674: 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@5669: w, Darkvater@5669: NewGRFConfirmationCallback Darkvater@5352: ); Darkvater@5352: } else { glx@7452: CopyGRFConfigList(WP(w, newgrf_d).orig_list, *WP(w, newgrf_d).list, true); glx@7452: ResetGRFConfig(false); glx@7452: ReloadNewGRFData(); Darkvater@5352: } Darkvater@5352: break; Darkvater@5352: belugas@6674: 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@5682: 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@6492: if (!WP(w, newgrf_d).execute) { glx@7452: CopyGRFConfigList(WP(w, newgrf_d).orig_list, *WP(w, newgrf_d).list, true); glx@7452: ResetGRFConfig(false); glx@7452: ReloadNewGRFData(); Darkvater@6492: } 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@8176: SetupNewGRFWindow(w); peter1138@5237: break; peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@5237: peter1138@5237: static const Widget _newgrf_widgets[] = { peter1138@5237: { WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, peter1138@5237: { WWT_CAPTION, RESIZE_RIGHT, 10, 11, 299, 0, 13, STR_NEWGRF_SETTINGS_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, peter1138@5237: peter1138@5237: /* NewGRF file Add, Remove, Move up, Move down */ Darkvater@5345: { WWT_PANEL, RESIZE_RIGHT, 10, 0, 299, 14, 29, STR_NULL, STR_NULL }, peter1138@5237: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 10, 79, 16, 27, STR_NEWGRF_ADD, STR_NEWGRF_ADD_TIP }, peter1138@5237: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 80, 149, 16, 27, STR_NEWGRF_REMOVE, STR_NEWGRF_REMOVE_TIP }, peter1138@5237: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 150, 219, 16, 27, STR_NEWGRF_MOVEUP, STR_NEWGRF_MOVEUP_TIP }, peter1138@5237: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 220, 289, 16, 27, STR_NEWGRF_MOVEDOWN, STR_NEWGRF_MOVEDOWN_TIP }, peter1138@5237: peter1138@5237: /* NewGRF file list */ peter1138@5237: { WWT_MATRIX, RESIZE_RB, 10, 0, 287, 30, 99, 0x501, STR_NEWGRF_FILE_TIP }, peter1138@5237: { WWT_SCROLLBAR, RESIZE_LRB, 10, 288, 299, 30, 99, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST }, peter1138@5237: peter1138@5237: /* NewGRF file info */ rubidium@5552: { WWT_PANEL, RESIZE_RTB, 10, 0, 299, 100, 212, STR_NULL, STR_NULL }, peter1138@5237: Darkvater@5352: /* Edit parameter and apply changes button... */ rubidium@5552: { WWT_PUSHTXTBTN, RESIZE_TB, 10, 0, 143, 213, 224, STR_NEWGRF_SET_PARAMETERS, STR_NULL }, rubidium@5552: { WWT_PUSHTXTBTN, RESIZE_RTB, 10, 144, 287, 213, 224, STR_NEWGRF_APPLY_CHANGES, STR_NULL }, Darkvater@5352: rubidium@5552: { WWT_RESIZEBOX, RESIZE_LRTB, 10, 288, 299, 213, 224, 0x0, STR_RESIZE_BUTTON }, peter1138@5237: peter1138@5237: { WIDGETS_END }, peter1138@5237: }; peter1138@5237: peter1138@5237: peter1138@5237: static const WindowDesc _newgrf_desc = { rubidium@7837: WDP_CENTER, WDP_CENTER, 300, 225, 300, 225, rubidium@6144: 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@7452: 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: }