message_box.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Enhanced version of the MessageBox API function. Permits enabling a
  3. * Help button by setting helpctxid to a context id in the help file
  4. * relevant to this dialog box. Also permits setting the 'utf8' flag
  5. * to indicate that the char strings given as 'text' and 'caption' are
  6. * encoded in UTF-8 rather than the system code page.
  7. */
  8. #include "putty.h"
  9. static HWND message_box_owner;
  10. /* Callback function to launch context help. */
  11. static VOID CALLBACK message_box_help_callback(LPHELPINFO lpHelpInfo)
  12. {
  13. const char *context = NULL;
  14. #define CHECK_CTX(name) \
  15. do { \
  16. if (lpHelpInfo->dwContextId == WINHELP_CTXID_ ## name) \
  17. context = WINHELP_CTX_ ## name; \
  18. } while (0)
  19. CHECK_CTX(errors_hostkey_absent);
  20. CHECK_CTX(errors_hostkey_changed);
  21. CHECK_CTX(errors_cantloadkey);
  22. CHECK_CTX(option_cleanup);
  23. CHECK_CTX(pgp_fingerprints);
  24. #undef CHECK_CTX
  25. if (context)
  26. launch_help(message_box_owner, context);
  27. }
  28. int message_box(HWND owner, LPCTSTR text, LPCTSTR caption, DWORD style,
  29. bool utf8, DWORD helpctxid)
  30. {
  31. MSGBOXPARAMSW mbox;
  32. /*
  33. * We use MessageBoxIndirect() because it allows us to specify a
  34. * callback function for the Help button.
  35. */
  36. mbox.cbSize = sizeof(mbox);
  37. /* Assumes the globals `hinst' and `hwnd' have sensible values. */
  38. mbox.hInstance = hinst;
  39. mbox.dwLanguageId = LANG_NEUTRAL;
  40. mbox.hwndOwner = message_box_owner = owner;
  41. wchar_t *wtext, *wcaption;
  42. if (utf8) {
  43. wtext = decode_utf8_to_wide_string(text);
  44. wcaption = decode_utf8_to_wide_string(caption);
  45. } else {
  46. wtext = dup_mb_to_wc(DEFAULT_CODEPAGE, text);
  47. wcaption = dup_mb_to_wc(DEFAULT_CODEPAGE, caption);
  48. }
  49. mbox.lpszText = wtext;
  50. mbox.lpszCaption = wcaption;
  51. mbox.dwStyle = style;
  52. mbox.dwContextHelpId = helpctxid;
  53. if (helpctxid != 0 && has_help()) mbox.dwStyle |= MB_HELP;
  54. mbox.lpfnMsgBoxCallback = &message_box_help_callback;
  55. int toret = MessageBoxIndirectW(&mbox);
  56. sfree(wtext);
  57. sfree(wcaption);
  58. return toret;
  59. }