1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00: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

@ -531,7 +531,7 @@ static void win_add_keyfile(Filename *filename, bool encrypted)
}
error:
message_box(traywindow, err, APPNAME, MB_OK | MB_ICONERROR,
message_box(traywindow, err, APPNAME, MB_OK | MB_ICONERROR, false,
HELPCTXID(errors_cantloadkey));
done:
sfree(err);

View File

@ -398,8 +398,8 @@ bool request_file(filereq *state, OPENFILENAME *of, bool preserve, bool save);
filereq *filereq_new(void);
void filereq_free(filereq *state);
void pgp_fingerprints_msgbox(HWND owner);
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);
void MakeDlgItemBorderless(HWND parent, int id);
char *GetDlgItemText_alloc(HWND hwnd, int id);
void split_into_argv(char *, bool includes_program_name,

View File

@ -83,7 +83,7 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
s2 = dupprintf("%s Warning", appname);
if (message_box(NULL, s1, s2,
MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2,
HELPCTXID(option_cleanup)) == IDYES) {
false, HELPCTXID(option_cleanup)) == IDYES) {
cleanup_all();
}
sfree(s1);

View File

@ -1139,7 +1139,7 @@ void load_key_file(HWND hwnd, struct MainDlgState *state,
char *msg = dupprintf("Couldn't load private key (%s)",
key_type_to_str(type));
message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
HELPCTXID(errors_cantloadkey));
false, HELPCTXID(errors_cantloadkey));
sfree(msg);
return;
}
@ -1202,7 +1202,7 @@ void load_key_file(HWND hwnd, struct MainDlgState *state,
if (ret == 0) {
char *msg = dupprintf("Couldn't load private key (%s)", errmsg);
message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
HELPCTXID(errors_cantloadkey));
false, HELPCTXID(errors_cantloadkey));
sfree(msg);
} else if (ret == 1) {
/*
@ -1241,7 +1241,7 @@ void add_certificate(HWND hwnd, struct MainDlgState *state,
char *msg = dupprintf("Couldn't load certificate (%s)",
key_type_to_str(type));
message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
HELPCTXID(errors_cantloadkey));
false, HELPCTXID(errors_cantloadkey));
sfree(msg);
return;
}
@ -1254,7 +1254,7 @@ void add_certificate(HWND hwnd, struct MainDlgState *state,
&error)) {
char *msg = dupprintf("Couldn't load certificate (%s)", error);
message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
HELPCTXID(errors_cantloadkey));
false, HELPCTXID(errors_cantloadkey));
sfree(msg);
strbuf_free(pub);
return;
@ -1267,7 +1267,7 @@ void add_certificate(HWND hwnd, struct MainDlgState *state,
char *msg = dupprintf("Couldn't load certificate (unsupported "
"algorithm name '%s')", algname);
message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
HELPCTXID(errors_cantloadkey));
false, HELPCTXID(errors_cantloadkey));
sfree(msg);
sfree(algname);
strbuf_free(pub);
@ -1295,7 +1295,7 @@ void add_certificate(HWND hwnd, struct MainDlgState *state,
if (!match) {
char *msg = dupprintf("Certificate is for a different public key");
message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
HELPCTXID(errors_cantloadkey));
false, HELPCTXID(errors_cantloadkey));
sfree(msg);
strbuf_free(pub);
return;
@ -1311,7 +1311,7 @@ void add_certificate(HWND hwnd, struct MainDlgState *state,
if (!newkey) {
char *msg = dupprintf("Couldn't combine certificate with key");
message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
HELPCTXID(errors_cantloadkey));
false, HELPCTXID(errors_cantloadkey));
sfree(msg);
return;
}

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));
}