win32MsgBox.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platformWin32/platformWin32.h"
  24. #include "platform/platformInput.h"
  25. #include "platform/nativeDialogs/msgBox.h"
  26. #include "console/console.h"
  27. #include "core/strings/unicode.h"
  28. #include "windowManager/platformWindowMgr.h"
  29. #include "windowManager/win32/win32Window.h"
  30. struct _FlagMap
  31. {
  32. S32 num;
  33. U32 flag;
  34. };
  35. static _FlagMap sgButtonMap[] =
  36. {
  37. { MBOk, MB_OK },
  38. { MBOkCancel, MB_OKCANCEL },
  39. { MBRetryCancel, MB_RETRYCANCEL },
  40. { MBSaveDontSave, MB_YESNO },
  41. { MBSaveDontSaveCancel, MB_YESNOCANCEL },
  42. { 0xffffffff, 0xffffffff }
  43. };
  44. static _FlagMap sgIconMap[] =
  45. {
  46. { MIWarning, MB_ICONWARNING },
  47. { MIInformation, MB_ICONINFORMATION },
  48. { MIQuestion, MB_ICONQUESTION },
  49. { MIStop, MB_ICONSTOP },
  50. { 0xffffffff, 0xffffffff }
  51. };
  52. static _FlagMap sgMsgBoxRetMap[] =
  53. {
  54. { IDCANCEL, MRCancel },
  55. { IDNO, MRDontSave },
  56. { IDOK, MROk},
  57. { IDRETRY, MRRetry },
  58. { IDYES, MROk },
  59. { 0xffffffff, 0xffffffff }
  60. };
  61. //-----------------------------------------------------------------------------
  62. static U32 getMaskFromID(_FlagMap *map, S32 id)
  63. {
  64. for(S32 i = 0;map[i].num != 0xffffffff && map[i].flag != 0xffffffff;++i)
  65. {
  66. if(map[i].num == id)
  67. return map[i].flag;
  68. }
  69. return 0;
  70. }
  71. //-----------------------------------------------------------------------------
  72. #ifndef TORQUE_SDL
  73. S32 Platform::messageBox(const UTF8 *title, const UTF8 *message, MBButtons buttons, MBIcons icon)
  74. {
  75. PlatformWindow *pWindow = WindowManager->getFirstWindow();
  76. // Get us rendering while we're blocking.
  77. winState.renderThreadBlocked = true;
  78. // We don't keep a locked mouse or else we're going
  79. // to end up possibly locking our mouse out of the
  80. // message box area
  81. bool cursorLocked = pWindow && pWindow->isMouseLocked();
  82. if( cursorLocked )
  83. pWindow->setMouseLocked( false );
  84. // Need a visible cursor to click stuff accurately
  85. bool cursorVisible = !pWindow || pWindow->isCursorVisible();
  86. if( !cursorVisible )
  87. pWindow->setCursorVisible(true);
  88. #ifdef UNICODE
  89. const UTF16 *msg = createUTF16string(message);
  90. const UTF16 *t = createUTF16string(title);
  91. #else
  92. const UTF8 *msg = message;
  93. const UTF8 *t = title;
  94. #endif
  95. HWND parent = pWindow ? static_cast<Win32Window*>(pWindow)->getHWND() : NULL;
  96. S32 ret = ::MessageBox( parent, msg, t, getMaskFromID(sgButtonMap, buttons) | getMaskFromID(sgIconMap, icon));
  97. #ifdef UNICODE
  98. delete [] msg;
  99. delete [] t;
  100. #endif
  101. // Dialog is gone.
  102. winState.renderThreadBlocked = false;
  103. if( cursorVisible == false )
  104. pWindow->setCursorVisible( false );
  105. if( cursorLocked == true )
  106. pWindow->setMouseLocked( true );
  107. return getMaskFromID(sgMsgBoxRetMap, ret);
  108. }
  109. #endif