Reporting.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Reporting.cpp
  3. * -------------
  4. * Purpose: A class for showing notifications, prompts, etc...
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "Reporting.h"
  11. #include "../mptrack/Mainfrm.h"
  12. #include "../mptrack/InputHandler.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. static inline UINT LogLevelToFlags(LogLevel level)
  15. {
  16. switch(level)
  17. {
  18. case LogDebug: return MB_OK; break;
  19. case LogNotification: return MB_OK; break;
  20. case LogInformation: return MB_OK | MB_ICONINFORMATION; break;
  21. case LogWarning: return MB_OK | MB_ICONWARNING; break;
  22. case LogError: return MB_OK | MB_ICONERROR; break;
  23. }
  24. return MB_OK;
  25. }
  26. static CString GetTitle()
  27. {
  28. return MAINFRAME_TITLE;
  29. }
  30. static CString FillEmptyCaption(const CString &caption, LogLevel level)
  31. {
  32. CString result = caption;
  33. if(result.IsEmpty())
  34. {
  35. result = GetTitle() + _T(" - ");
  36. switch(level)
  37. {
  38. case LogDebug: result += _T("Debug"); break;
  39. case LogNotification: result += _T("Notification"); break;
  40. case LogInformation: result += _T("Information"); break;
  41. case LogWarning: result += _T("Warning"); break;
  42. case LogError: result += _T("Error"); break;
  43. }
  44. }
  45. return result;
  46. }
  47. static CString FillEmptyCaption(const CString &caption)
  48. {
  49. CString result = caption;
  50. if(result.IsEmpty())
  51. {
  52. result = GetTitle();
  53. }
  54. return result;
  55. }
  56. static UINT ShowNotificationImpl(const CString &text, const CString &caption, UINT flags, const CWnd *parent)
  57. {
  58. if(parent == nullptr)
  59. {
  60. parent = CMainFrame::GetActiveWindow();
  61. }
  62. BypassInputHandler bih;
  63. UINT result = ::MessageBox(parent->GetSafeHwnd(), text, caption.IsEmpty() ? CString(MAINFRAME_TITLE) : caption, flags);
  64. return result;
  65. }
  66. UINT Reporting::CustomNotification(const AnyStringLocale &text, const AnyStringLocale &caption, UINT flags, const CWnd *parent)
  67. {
  68. return ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption)), flags, parent);
  69. }
  70. void Reporting::Notification(const AnyStringLocale &text, const CWnd *parent)
  71. {
  72. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(CString(), LogNotification), LogLevelToFlags(LogNotification), parent);
  73. }
  74. void Reporting::Notification(const AnyStringLocale &text, const AnyStringLocale &caption, const CWnd *parent)
  75. {
  76. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption), LogNotification), LogLevelToFlags(LogNotification), parent);
  77. }
  78. void Reporting::Information(const AnyStringLocale &text, const CWnd *parent)
  79. {
  80. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(CString(), LogInformation), LogLevelToFlags(LogInformation), parent);
  81. }
  82. void Reporting::Information(const AnyStringLocale &text, const AnyStringLocale &caption, const CWnd *parent)
  83. {
  84. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption), LogInformation), LogLevelToFlags(LogInformation), parent);
  85. }
  86. void Reporting::Warning(const AnyStringLocale &text, const CWnd *parent)
  87. {
  88. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(CString(), LogWarning), LogLevelToFlags(LogWarning), parent);
  89. }
  90. void Reporting::Warning(const AnyStringLocale &text, const AnyStringLocale &caption, const CWnd *parent)
  91. {
  92. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption), LogWarning), LogLevelToFlags(LogWarning), parent);
  93. }
  94. void Reporting::Error(const AnyStringLocale &text, const CWnd *parent)
  95. {
  96. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(CString(), LogError), LogLevelToFlags(LogError), parent);
  97. }
  98. void Reporting::Error(const AnyStringLocale &text, const AnyStringLocale &caption, const CWnd *parent)
  99. {
  100. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption), LogError), LogLevelToFlags(LogError), parent);
  101. }
  102. void Reporting::Message(LogLevel level, const AnyStringLocale &text, const CWnd *parent)
  103. {
  104. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(CString(), level), LogLevelToFlags(level), parent);
  105. }
  106. void Reporting::Message(LogLevel level, const AnyStringLocale &text, const AnyStringLocale &caption, const CWnd *parent)
  107. {
  108. ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption), level), LogLevelToFlags(level), parent);
  109. }
  110. ConfirmAnswer Reporting::Confirm(const AnyStringLocale &text, bool showCancel, bool defaultNo, const CWnd *parent)
  111. {
  112. return Confirm(mpt::ToCString(text), GetTitle() + _T(" - Confirmation"), showCancel, defaultNo, parent);
  113. }
  114. ConfirmAnswer Reporting::Confirm(const AnyStringLocale &text, const AnyStringLocale &caption, bool showCancel, bool defaultNo, const CWnd *parent)
  115. {
  116. UINT result = ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption)), (showCancel ? MB_YESNOCANCEL : MB_YESNO) | MB_ICONQUESTION | (defaultNo ? MB_DEFBUTTON2 : 0), parent);
  117. switch(result)
  118. {
  119. case IDYES:
  120. return cnfYes;
  121. case IDNO:
  122. return cnfNo;
  123. default:
  124. case IDCANCEL:
  125. return cnfCancel;
  126. }
  127. }
  128. RetryAnswer Reporting::RetryCancel(const AnyStringLocale &text, const CWnd *parent)
  129. {
  130. return RetryCancel(mpt::ToCString(text), GetTitle(), parent);
  131. }
  132. RetryAnswer Reporting::RetryCancel(const AnyStringLocale &text, const AnyStringLocale &caption, const CWnd *parent)
  133. {
  134. UINT result = ShowNotificationImpl(mpt::ToCString(text), FillEmptyCaption(mpt::ToCString(caption)), MB_RETRYCANCEL, parent);
  135. switch(result)
  136. {
  137. case IDRETRY:
  138. return rtyRetry;
  139. default:
  140. case IDCANCEL:
  141. return rtyCancel;
  142. }
  143. }
  144. OPENMPT_NAMESPACE_END