MsgHandler.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cstdarg>
  5. #include <cstdio>
  6. #include <string>
  7. #include "Common/CommonTypes.h"
  8. #include "Common/StringUtil.h"
  9. bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, int Style);
  10. static MsgAlertHandler msg_handler = DefaultMsgHandler;
  11. static bool AlertEnabled = true;
  12. std::string DefaultStringTranslator(const char* text);
  13. static StringTranslator str_translator = DefaultStringTranslator;
  14. // Select which of these functions that are used for message boxes. If
  15. // wxWidgets is enabled we will use wxMsgAlert() that is defined in Main.cpp
  16. void RegisterMsgAlertHandler(MsgAlertHandler handler)
  17. {
  18. msg_handler = handler;
  19. }
  20. // Select translation function. For wxWidgets use wxStringTranslator in Main.cpp
  21. void RegisterStringTranslator(StringTranslator translator)
  22. {
  23. str_translator = translator;
  24. }
  25. // enable/disable the alert handler
  26. void SetEnableAlert(bool enable)
  27. {
  28. AlertEnabled = enable;
  29. }
  30. // This is the first stop for gui alerts where the log is updated and the
  31. // correct window is shown
  32. bool MsgAlert(bool yes_no, int Style, const char* format, ...)
  33. {
  34. // Read message and write it to the log
  35. std::string caption;
  36. char buffer[2048];
  37. static std::string info_caption;
  38. static std::string warn_caption;
  39. static std::string ques_caption;
  40. static std::string crit_caption;
  41. if (!info_caption.length())
  42. {
  43. info_caption = str_translator(_trans("Information"));
  44. ques_caption = str_translator(_trans("Question"));
  45. warn_caption = str_translator(_trans("Warning"));
  46. crit_caption = str_translator(_trans("Critical"));
  47. }
  48. switch (Style)
  49. {
  50. case INFORMATION:
  51. caption = info_caption;
  52. break;
  53. case QUESTION:
  54. caption = ques_caption;
  55. break;
  56. case WARNING:
  57. caption = warn_caption;
  58. break;
  59. case CRITICAL:
  60. caption = crit_caption;
  61. break;
  62. }
  63. va_list args;
  64. va_start(args, format);
  65. CharArrayFromFormatV(buffer, sizeof(buffer) - 1, str_translator(format).c_str(), args);
  66. va_end(args);
  67. ERROR_LOG(MASTER_LOG, "%s: %s", caption.c_str(), buffer);
  68. // Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored
  69. if (msg_handler && (AlertEnabled || Style == QUESTION || Style == CRITICAL))
  70. return msg_handler(caption.c_str(), buffer, yes_no, Style);
  71. return true;
  72. }
  73. // Default non library dependent panic alert
  74. bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, int Style)
  75. {
  76. #ifdef _WIN32
  77. int STYLE = MB_ICONINFORMATION;
  78. if (Style == QUESTION) STYLE = MB_ICONQUESTION;
  79. if (Style == WARNING) STYLE = MB_ICONWARNING;
  80. return IDYES == MessageBox(0, UTF8ToTStr(text).c_str(), UTF8ToTStr(caption).c_str(), STYLE | (yes_no ? MB_YESNO : MB_OK));
  81. #else
  82. printf("%s\n", text);
  83. return true;
  84. #endif
  85. }
  86. // Default (non) translator
  87. std::string DefaultStringTranslator(const char* text)
  88. {
  89. return text;
  90. }