From 984da2455b75663100a42b1db5bbcedd351abe67 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 11 Feb 2025 17:30:16 +0000 Subject: [PATCH] Codechange: Use EncodedString for Query window. (#13528) This removes the need to separately capture and store global parameters. --- src/depot_gui.cpp | 6 ++---- src/fios_gui.cpp | 10 ++++++++-- src/genworld_gui.cpp | 8 ++++---- src/group_gui.cpp | 5 ++++- src/industry_gui.cpp | 10 ++++++++-- src/intro_gui.cpp | 8 ++++---- src/misc_cmd.cpp | 4 ++-- src/misc_gui.cpp | 28 +++++++++++----------------- src/network/network_content_gui.cpp | 5 ++++- src/network/network_gui.cpp | 17 ++++++++--------- src/newgrf_gui.cpp | 4 ++-- src/screenshot.cpp | 6 +++--- src/settings_gui.cpp | 4 ++-- src/terraform_gui.cpp | 5 ++++- src/textbuf_gui.h | 2 +- 15 files changed, 67 insertions(+), 55 deletions(-) diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 748ceb04da..5a1c8ab645 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -822,11 +822,9 @@ struct DepotWindow : Window { case WID_D_SELL_ALL: /* Only open the confirmation window if there are anything to sell */ if (!this->vehicle_list.empty() || !this->wagon_list.empty()) { - SetDParam(0, this->type); - SetDParam(1, this->GetDestinationIndex()); ShowQuery( - STR_DEPOT_CAPTION, - STR_DEPOT_SELL_CONFIRMATION_TEXT, + GetEncodedString(STR_DEPOT_CAPTION, this->type, this->GetDestinationIndex()), + GetEncodedString(STR_DEPOT_SELL_CONFIRMATION_TEXT), this, DepotSellAllConfirmationCallback ); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index d3909cc9e6..899b000046 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -825,14 +825,20 @@ public: if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) { _file_to_saveload.name = FiosMakeSavegameName(this->filename_editbox.text.GetText()); if (FioCheckFileExists(_file_to_saveload.name, Subdirectory::SAVE_DIR)) { - ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE, STR_SAVELOAD_OVERWRITE_WARNING, this, SaveLoadWindow::SaveGameConfirmationCallback); + ShowQuery( + GetEncodedString(STR_SAVELOAD_OVERWRITE_TITLE), + GetEncodedString(STR_SAVELOAD_OVERWRITE_WARNING), + this, SaveLoadWindow::SaveGameConfirmationCallback); } else { _switch_mode = SM_SAVE_GAME; } } else { _file_to_saveload.name = FiosMakeHeightmapName(this->filename_editbox.text.GetText()); if (FioCheckFileExists(_file_to_saveload.name, Subdirectory::SAVE_DIR)) { - ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE, STR_SAVELOAD_OVERWRITE_WARNING, this, SaveLoadWindow::SaveHeightmapConfirmationCallback); + ShowQuery( + GetEncodedString(STR_SAVELOAD_OVERWRITE_TITLE), + GetEncodedString(STR_SAVELOAD_OVERWRITE_WARNING), + this, SaveLoadWindow::SaveHeightmapConfirmationCallback); } else { _switch_mode = SM_SAVE_HEIGHTMAP; } diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index 8416a8e2a7..ec082d5a08 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -713,8 +713,8 @@ struct GenerateLandscapeWindow : public Window { map_y * 2 < (1U << _settings_newgame.game_creation.map_y) || map_y / 2 > (1U << _settings_newgame.game_creation.map_y))) { ShowQuery( - STR_WARNING_HEIGHTMAP_SCALE_CAPTION, - STR_WARNING_HEIGHTMAP_SCALE_MESSAGE, + GetEncodedString(STR_WARNING_HEIGHTMAP_SCALE_CAPTION), + GetEncodedString(STR_WARNING_HEIGHTMAP_SCALE_MESSAGE), this, LandscapeGenerationCallback); } else { @@ -1381,8 +1381,8 @@ struct GenerateProgressWindow : public Window { case WID_GP_ABORT: SetMouseCursorBusy(false); ShowQuery( - STR_GENERATION_ABORT_CAPTION, - STR_GENERATION_ABORT_MESSAGE, + GetEncodedString(STR_GENERATION_ABORT_CAPTION), + GetEncodedString(STR_GENERATION_ABORT_MESSAGE), this, AbortGeneratingWorldCallback ); diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 3d2fc6b651..f504515d02 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -841,7 +841,10 @@ public: case WID_GL_DELETE_GROUP: { // Delete the selected group this->group_confirm = this->vli.ToGroupID(); - ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback); + ShowQuery( + GetEncodedString(STR_QUERY_GROUP_DELETE_CAPTION), + GetEncodedString(STR_GROUP_DELETE_QUERY_TEXT), + this, DeleteGroupCallback); break; } diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 565a44ce73..6eccb0cc1a 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -641,14 +641,20 @@ public: case WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET: { assert(_game_mode == GM_EDITOR); this->HandleButtonClick(WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET); - ShowQuery(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_CAPTION, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_QUERY, nullptr, AskManyRandomIndustriesCallback); + ShowQuery( + GetEncodedString(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_CAPTION), + GetEncodedString(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_QUERY), + nullptr, AskManyRandomIndustriesCallback); break; } case WID_DPI_REMOVE_ALL_INDUSTRIES_WIDGET: { assert(_game_mode == GM_EDITOR); this->HandleButtonClick(WID_DPI_REMOVE_ALL_INDUSTRIES_WIDGET); - ShowQuery(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_CAPTION, STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_QUERY, nullptr, AskRemoveAllIndustriesCallback); + ShowQuery( + GetEncodedString(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_CAPTION), + GetEncodedString(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_QUERY), + nullptr, AskRemoveAllIndustriesCallback); break; } diff --git a/src/intro_gui.cpp b/src/intro_gui.cpp index 659f81885f..9e6e7393db 100644 --- a/src/intro_gui.cpp +++ b/src/intro_gui.cpp @@ -479,8 +479,8 @@ static void AskExitGameCallback(Window *, bool confirmed) void AskExitGame() { ShowQuery( - STR_QUIT_CAPTION, - STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD, + GetEncodedString(STR_QUIT_CAPTION), + GetEncodedString(STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD), nullptr, AskExitGameCallback, true @@ -499,8 +499,8 @@ static void AskExitToGameMenuCallback(Window *, bool confirmed) void AskExitToGameMenu() { ShowQuery( - STR_ABANDON_GAME_CAPTION, - (_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDON_SCENARIO_QUERY, + GetEncodedString(STR_ABANDON_GAME_CAPTION), + GetEncodedString((_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDON_SCENARIO_QUERY), nullptr, AskExitToGameMenuCallback, true diff --git a/src/misc_cmd.cpp b/src/misc_cmd.cpp index 4a12fad2a5..27d6c9178b 100644 --- a/src/misc_cmd.cpp +++ b/src/misc_cmd.cpp @@ -186,8 +186,8 @@ CommandCost CmdPause(DoCommandFlag flags, PauseMode mode, bool pause) if (flags & DC_EXEC) { if (mode == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) { ShowQuery( - STR_NEWGRF_UNPAUSE_WARNING_TITLE, - STR_NEWGRF_UNPAUSE_WARNING, + GetEncodedString(STR_NEWGRF_UNPAUSE_WARNING_TITLE), + GetEncodedString(STR_NEWGRF_UNPAUSE_WARNING), nullptr, AskUnsafeUnpauseCallback ); diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index b8a092de35..b9b89703c0 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -1065,20 +1065,15 @@ void ShowQueryString(std::string_view str, StringID caption, uint maxsize, Windo */ struct QueryWindow : public Window { QueryCallbackProc *proc; ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise - std::vector params; ///< local copy of #_global_string_params - StringID message; ///< message shown for query window + EncodedString caption; ///< caption for query window. + EncodedString message; ///< message for query window. - QueryWindow(WindowDesc &desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc) + QueryWindow(WindowDesc &desc, EncodedString &&caption, EncodedString &&message, Window *parent, QueryCallbackProc *callback) + : Window(desc), proc(callback), caption(std::move(caption)), message(std::move(message)) { - /* Create a backup of the variadic arguments to strings because it will be - * overridden pretty often. We will copy these back for drawing */ - CopyOutDParam(this->params, 10); - this->message = message; - this->proc = callback; - this->parent = parent; + this->parent = parent; this->CreateNestedTree(); - this->GetWidget(WID_Q_CAPTION)->SetString(caption); this->FinishInitNested(WN_CONFIRM_POPUP_QUERY); } @@ -1100,8 +1095,7 @@ struct QueryWindow : public Window { { switch (widget) { case WID_Q_CAPTION: - case WID_Q_TEXT: - CopyInDParam(this->params); + SetDParamStr(0, this->caption.GetDecodedString()); break; } } @@ -1110,14 +1104,14 @@ struct QueryWindow : public Window { { if (widget != WID_Q_TEXT) return; - size = GetStringMultiLineBoundingBox(this->message, size); + size = GetStringMultiLineBoundingBox(this->message.GetDecodedString(), size); } void DrawWidget(const Rect &r, WidgetID widget) const override { if (widget != WID_Q_TEXT) return; - DrawStringMultiLine(r, this->message, TC_FROMSTRING, SA_CENTER); + DrawStringMultiLine(r, this->message.GetDecodedString(), TC_FROMSTRING, SA_CENTER); } void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override @@ -1166,7 +1160,7 @@ struct QueryWindow : public Window { static constexpr NWidgetPart _nested_query_widgets[] = { NWidget(NWID_HORIZONTAL), NWidget(WWT_CLOSEBOX, COLOUR_RED), - NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), // The caption's string is set in the constructor + NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetToolTip(STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), EndContainer(), NWidget(WWT_PANEL, COLOUR_RED), NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_wide, 0), SetPadding(WidgetDimensions::unscaled.modalpopup), @@ -1196,7 +1190,7 @@ static WindowDesc _query_desc( * @param callback callback function pointer to set in the window descriptor * @param focus whether the window should be focussed (by default false) */ -void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback, bool focus) +void ShowQuery(EncodedString &&caption, EncodedString &&message, Window *parent, QueryCallbackProc *callback, bool focus) { if (parent == nullptr) parent = GetMainWindow(); @@ -1210,6 +1204,6 @@ void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallback break; } - QueryWindow *q = new QueryWindow(_query_desc, caption, message, parent, callback); + QueryWindow *q = new QueryWindow(_query_desc, std::move(caption), std::move(message), parent, callback); if (focus) SetFocusedWindow(q); } diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 7b840975c7..5cdc45ba5c 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -869,7 +869,10 @@ public: if (_accepted_external_search) { this->OpenExternalSearch(); } else { - ShowQuery(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER_CAPTION, STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER, this, ExternalSearchDisclaimerCallback); + ShowQuery( + GetEncodedString(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER_CAPTION), + GetEncodedString(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER), + this, ExternalSearchDisclaimerCallback); } break; } diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 058fc77241..9d8cc258bb 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1823,40 +1823,39 @@ public: break; case WID_CL_MATRIX: { - StringID text = STR_NULL; QueryCallbackProc *callback = nullptr; + EncodedString text; switch (index) { case DD_CLIENT_ADMIN_KICK: _admin_client_id = this->dd_client_id; - text = STR_NETWORK_CLIENT_LIST_ASK_CLIENT_KICK; callback = AdminClientKickCallback; - SetDParamStr(0, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name); + text = GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_CLIENT_KICK, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name); break; case DD_CLIENT_ADMIN_BAN: _admin_client_id = this->dd_client_id; - text = STR_NETWORK_CLIENT_LIST_ASK_CLIENT_BAN; callback = AdminClientBanCallback; - SetDParamStr(0, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name); + text = GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_CLIENT_BAN, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name); break; case DD_COMPANY_ADMIN_RESET: _admin_company_id = this->dd_company_id; - text = STR_NETWORK_CLIENT_LIST_ASK_COMPANY_RESET; callback = AdminCompanyResetCallback; - SetDParam(0, _admin_company_id); + text = GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_COMPANY_RESET, _admin_company_id); break; default: NOT_REACHED(); } - assert(text != STR_NULL); assert(callback != nullptr); /* Always ask confirmation for all admin actions. */ - ShowQuery(STR_NETWORK_CLIENT_LIST_ASK_CAPTION, text, this, callback); + ShowQuery( + GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_CAPTION), + std::move(text), + this, callback); break; } diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 32b613f865..3d90b6eb0e 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1097,8 +1097,8 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { if (!this->editable) break; if (this->execute) { ShowQuery( - STR_NEWGRF_POPUP_CAUTION_CAPTION, - STR_NEWGRF_CONFIRMATION_TEXT, + GetEncodedString(STR_NEWGRF_POPUP_CAUTION_CAPTION), + GetEncodedString(STR_NEWGRF_CONFIRMATION_TEXT), this, NewGRFConfirmationCallback ); diff --git a/src/screenshot.cpp b/src/screenshot.cpp index 01dec7a1f1..89295f8bfd 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -858,9 +858,9 @@ void MakeScreenshotWithConfirm(ScreenshotType t) if (width * height > 8192 * 8192) { /* Ask for confirmation */ _confirmed_screenshot_type = t; - SetDParam(0, width); - SetDParam(1, height); - ShowQuery(STR_WARNING_SCREENSHOT_SIZE_CAPTION, STR_WARNING_SCREENSHOT_SIZE_MESSAGE, nullptr, ScreenshotConfirmationCallback); + ShowQuery( + GetEncodedString(STR_WARNING_SCREENSHOT_SIZE_CAPTION), + GetEncodedString(STR_WARNING_SCREENSHOT_SIZE_MESSAGE, width, height), nullptr, ScreenshotConfirmationCallback); } else { /* Less than 64M pixels, just do it */ MakeScreenshot(t, {}); diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 5081ae4fee..84d83138cf 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -2547,8 +2547,8 @@ struct GameSettingsWindow : Window { case WID_GS_RESET_ALL: ShowQuery( - STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_CAPTION, - STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_TEXT, + GetEncodedString(STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_CAPTION), + GetEncodedString(STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_TEXT), this, ResetAllSettingsConfirmationCallback ); diff --git a/src/terraform_gui.cpp b/src/terraform_gui.cpp index ce8eebc8e8..5d1e5a4649 100644 --- a/src/terraform_gui.cpp +++ b/src/terraform_gui.cpp @@ -638,7 +638,10 @@ struct ScenarioEditorLandscapeGenerationWindow : Window { break; case WID_ETT_RESET_LANDSCAPE: // Reset landscape - ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback); + ShowQuery( + GetEncodedString(STR_QUERY_RESET_LANDSCAPE_CAPTION), + GetEncodedString(STR_RESET_LANDSCAPE_CONFIRMATION_TEXT), + nullptr, ResetLandscapeConfirmationCallback); break; default: NOT_REACHED(); diff --git a/src/textbuf_gui.h b/src/textbuf_gui.h index c52210d3ba..c28216f1b8 100644 --- a/src/textbuf_gui.h +++ b/src/textbuf_gui.h @@ -28,7 +28,7 @@ DECLARE_ENUM_AS_BIT_SET(QueryStringFlags) typedef void QueryCallbackProc(Window*, bool); void ShowQueryString(std::string_view str, StringID caption, uint max_len, Window *parent, CharSetFilter afilter, QueryStringFlags flags); -void ShowQuery(StringID caption, StringID message, Window *w, QueryCallbackProc *callback, bool focus = false); +void ShowQuery(EncodedString &&caption, EncodedString &&message, Window *w, QueryCallbackProc *callback, bool focus = false); /** The number of 'characters' on the on-screen keyboard. */ static const uint OSK_KEYBOARD_ENTRIES = 50;