Allow using ShowQuery with std::string texts

This commit is contained in:
Jonathan G Rennison
2023-05-14 12:46:07 +01:00
parent b8f2d96250
commit b26d91770f
2 changed files with 64 additions and 13 deletions

View File

@@ -1200,11 +1200,15 @@ struct QueryWindow : public Window {
uint64 params[10]; ///< local copy of #_global_string_params uint64 params[10]; ///< local copy of #_global_string_params
StringID message; ///< message shown for query window StringID message; ///< message shown for query window
StringID caption; ///< title of window StringID caption; ///< title of window
bool precomposed;
std::string caption_str;
mutable std::string message_str;
QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc) QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
{ {
/* Create a backup of the variadic arguments to strings because it will be /* Create a backup of the variadic arguments to strings because it will be
* overridden pretty often. We will copy these back for drawing */ * overridden pretty often. We will copy these back for drawing */
this->precomposed = false;
CopyOutDParam(this->params, 0, lengthof(this->params)); CopyOutDParam(this->params, 0, lengthof(this->params));
this->caption = caption; this->caption = caption;
this->message = message; this->message = message;
@@ -1214,6 +1218,19 @@ struct QueryWindow : public Window {
this->InitNested(WN_CONFIRM_POPUP_QUERY); this->InitNested(WN_CONFIRM_POPUP_QUERY);
} }
QueryWindow(WindowDesc *desc, std::string caption, std::string message, Window *parent, QueryCallbackProc *callback) : Window(desc)
{
this->precomposed = true;
this->caption = SPECSTR_TEMP_START;
this->message = STR_EMPTY;
this->caption_str = std::move(caption);
this->message_str = std::move(message);
this->proc = callback;
this->parent = parent;
this->InitNested(WN_CONFIRM_POPUP_QUERY);
}
~QueryWindow() ~QueryWindow()
{ {
if (this->proc != nullptr) this->proc(this->parent, false); if (this->proc != nullptr) this->proc(this->parent, false);
@@ -1231,12 +1248,18 @@ struct QueryWindow : public Window {
{ {
switch (widget) { switch (widget) {
case WID_Q_CAPTION: case WID_Q_CAPTION:
if (this->precomposed) {
_temp_special_strings[0] = this->caption_str;
} else {
CopyInDParam(1, this->params, lengthof(this->params)); CopyInDParam(1, this->params, lengthof(this->params));
}
SetDParam(0, this->caption); SetDParam(0, this->caption);
break; break;
case WID_Q_TEXT: case WID_Q_TEXT:
if (!this->precomposed) {
CopyInDParam(0, this->params, lengthof(this->params)); CopyInDParam(0, this->params, lengthof(this->params));
}
break; break;
} }
} }
@@ -1245,7 +1268,9 @@ struct QueryWindow : public Window {
{ {
if (widget != WID_Q_TEXT) return; if (widget != WID_Q_TEXT) return;
Dimension d = GetStringMultiLineBoundingBox(this->message, *size); if (!this->precomposed) this->message_str = GetString(this->message);
Dimension d = GetStringMultiLineBoundingBox(this->message_str.c_str(), *size);
d.width += WidgetDimensions::scaled.frametext.Horizontal(); d.width += WidgetDimensions::scaled.frametext.Horizontal();
d.height += WidgetDimensions::scaled.framerect.Vertical(); d.height += WidgetDimensions::scaled.framerect.Vertical();
*size = d; *size = d;
@@ -1255,8 +1280,10 @@ struct QueryWindow : public Window {
{ {
if (widget != WID_Q_TEXT) return; if (widget != WID_Q_TEXT) return;
if (!this->precomposed) this->message_str = GetString(this->message);
DrawStringMultiLine(r.Shrink(WidgetDimensions::scaled.frametext, WidgetDimensions::scaled.framerect), DrawStringMultiLine(r.Shrink(WidgetDimensions::scaled.frametext, WidgetDimensions::scaled.framerect),
this->message, TC_FROMSTRING, SA_CENTER); this->message_str, TC_FROMSTRING, SA_CENTER);
} }
void OnClick(Point pt, int widget, int click_count) override void OnClick(Point pt, int widget, int click_count) override
@@ -1325,6 +1352,19 @@ static WindowDesc _query_desc(
_nested_query_widgets, lengthof(_nested_query_widgets) _nested_query_widgets, lengthof(_nested_query_widgets)
); );
static void RemoveExistingQueryWindow(Window *parent, QueryCallbackProc *callback)
{
for (const Window *w : Window::IterateFromBack()) {
if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
const QueryWindow *qw = (const QueryWindow *)w;
if (qw->parent != parent || qw->proc != callback) continue;
delete qw;
break;
}
}
/** /**
* Show a modal confirmation window with standard 'yes' and 'no' buttons * Show a modal confirmation window with standard 'yes' and 'no' buttons
* The window is aligned to the centre of its parent. * The window is aligned to the centre of its parent.
@@ -1338,19 +1378,29 @@ void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallback
{ {
if (parent == nullptr) parent = GetMainWindow(); if (parent == nullptr) parent = GetMainWindow();
for (const Window *w : Window::IterateFromBack()) { RemoveExistingQueryWindow(parent, callback);
if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
const QueryWindow *qw = (const QueryWindow *)w;
if (qw->parent != parent || qw->proc != callback) continue;
delete qw;
break;
}
new QueryWindow(&_query_desc, caption, message, parent, callback); new QueryWindow(&_query_desc, caption, message, parent, callback);
} }
/**
* Show a modal confirmation window with standard 'yes' and 'no' buttons
* The window is aligned to the centre of its parent.
* @param caption string shown as window caption
* @param message string that will be shown for the window
* @param parent pointer to parent window, if this pointer is nullptr the parent becomes
* the main window WC_MAIN_WINDOW
* @param callback callback function pointer to set in the window descriptor
*/
void ShowQuery(std::string caption, std::string message, Window *parent, QueryCallbackProc *callback)
{
if (parent == nullptr) parent = GetMainWindow();
RemoveExistingQueryWindow(parent, callback);
new QueryWindow(&_query_desc, std::move(caption), std::move(message), parent, callback);
}
static const NWidgetPart _modifier_key_toggle_widgets[] = { static const NWidgetPart _modifier_key_toggle_widgets[] = {
NWidget(NWID_HORIZONTAL), NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_GREY), NWidget(WWT_CLOSEBOX, COLOUR_GREY),

View File

@@ -30,6 +30,7 @@ typedef void QueryCallbackProc(Window*, bool);
void ShowQueryString(StringID str, StringID caption, uint max_len, Window *parent, CharSetFilter afilter, QueryStringFlags flags); void ShowQueryString(StringID str, StringID caption, uint max_len, Window *parent, CharSetFilter afilter, QueryStringFlags flags);
void ShowQuery(StringID caption, StringID message, Window *w, QueryCallbackProc *callback); void ShowQuery(StringID caption, StringID message, Window *w, QueryCallbackProc *callback);
void ShowQuery(std::string caption, std::string message, Window *parent, QueryCallbackProc *callback);
/** The number of 'characters' on the on-screen keyboard. */ /** The number of 'characters' on the on-screen keyboard. */
static const uint OSK_KEYBOARD_ENTRIES = 50; static const uint OSK_KEYBOARD_ENTRIES = 50;