1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32:48 -05:00

Add UTF-8 flag to the Windows message_box() wrapper.

message_box() previously differed from the real MessageBox API
function in that it permitted the user to provide a help context to be
used for a Help button in the dialog box.

Now it adds a second unusual ability: you can specify that the text
and caption strings are in UTF-8 rather than the system code page.
This commit is contained in:
Simon Tatham
2023-05-29 13:28:11 +01:00
parent d22ccbac6f
commit 5f43d11f83
6 changed files with 44 additions and 22 deletions

View File

@ -1,5 +1,9 @@
/*
* Message box with optional context help.
* Enhanced version of the MessageBox API function. Permits enabling a
* Help button by setting helpctxid to a context id in the help file
* relevant to this dialog box. Also permits setting the 'utf8' flag
* to indicate that the char strings given as 'text' and 'caption' are
* encoded in UTF-8 rather than the system code page.
*/
#include "putty.h"
@ -25,10 +29,10 @@ static VOID CALLBACK message_box_help_callback(LPHELPINFO lpHelpInfo)
launch_help(message_box_owner, context);
}
int message_box(HWND owner, LPCTSTR text, LPCTSTR caption,
DWORD style, DWORD helpctxid)
int message_box(HWND owner, LPCTSTR text, LPCTSTR caption, DWORD style,
bool utf8, DWORD helpctxid)
{
MSGBOXPARAMS mbox;
MSGBOXPARAMSW mbox;
/*
* We use MessageBoxIndirect() because it allows us to specify a
@ -37,13 +41,31 @@ int message_box(HWND owner, LPCTSTR text, LPCTSTR caption,
mbox.cbSize = sizeof(mbox);
/* Assumes the globals `hinst' and `hwnd' have sensible values. */
mbox.hInstance = hinst;
mbox.hwndOwner = message_box_owner = owner;
mbox.lpfnMsgBoxCallback = &message_box_help_callback;
mbox.dwLanguageId = LANG_NEUTRAL;
mbox.lpszText = text;
mbox.lpszCaption = caption;
mbox.dwContextHelpId = helpctxid;
mbox.hwndOwner = message_box_owner = owner;
wchar_t *wtext, *wcaption;
if (utf8) {
wtext = decode_utf8_to_wide_string(text);
wcaption = decode_utf8_to_wide_string(caption);
} else {
wtext = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, text);
wcaption = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, caption);
}
mbox.lpszText = wtext;
mbox.lpszCaption = wcaption;
mbox.dwStyle = style;
mbox.dwContextHelpId = helpctxid;
if (helpctxid != 0 && has_help()) mbox.dwStyle |= MB_HELP;
return MessageBoxIndirect(&mbox);
mbox.lpfnMsgBoxCallback = &message_box_help_callback;
int toret = MessageBoxIndirectW(&mbox);
sfree(wtext);
sfree(wcaption);
return toret;
}

View File

@ -21,5 +21,5 @@ void pgp_fingerprints_msgbox(HWND owner)
", " PGP_PREV_MASTER_KEY_DETAILS "):\n"
" " PGP_PREV_MASTER_KEY_FP,
"PGP fingerprints", MB_ICONINFORMATION | MB_OK,
HELPCTXID(pgp_fingerprints));
false, HELPCTXID(pgp_fingerprints));
}