msg_handler.cpp 3.1 KB

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