win32MsgBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/console.h"
  25. #include "string/unicode.h"
  26. #include "platform/nativeDialogs/msgBox.h"
  27. struct _FlagMap
  28. {
  29. S32 num;
  30. U32 flag;
  31. };
  32. static _FlagMap sgButtonMap[] =
  33. {
  34. { MBOk, MB_OK },
  35. { MBOkCancel, MB_OKCANCEL },
  36. { MBRetryCancel, MB_RETRYCANCEL },
  37. { MBSaveDontSave, MB_YESNO },
  38. { MBSaveDontSaveCancel, MB_YESNOCANCEL },
  39. { 0xffffffff, 0xffffffff }
  40. };
  41. static _FlagMap sgIconMap[] =
  42. {
  43. { MIWarning, MB_ICONWARNING },
  44. { MIInformation, MB_ICONINFORMATION },
  45. { MIQuestion, MB_ICONQUESTION },
  46. { MIStop, MB_ICONSTOP },
  47. { 0xffffffff, 0xffffffff }
  48. };
  49. static _FlagMap sgMsgBoxRetMap[] =
  50. {
  51. { IDCANCEL, MRCancel },
  52. { IDNO, MRDontSave },
  53. { IDOK, MROk},
  54. { IDRETRY, MRRetry },
  55. { IDYES, MROk },
  56. { 0xffffffff, 0xffffffff }
  57. };
  58. //////////////////////////////////////////////////////////////////////////
  59. static U32 getMaskFromID(_FlagMap *map, S32 id)
  60. {
  61. for(S32 i = 0;map[i].num != 0xffffffff && map[i].flag != 0xffffffff;++i)
  62. {
  63. if(map[i].num == id)
  64. return map[i].flag;
  65. }
  66. return 0;
  67. }
  68. //////////////////////////////////////////////////////////////////////////
  69. S32 Platform::messageBox(const UTF8 *title, const UTF8 *message, MBButtons buttons, MBIcons icon)
  70. {
  71. // Get us rendering while we're blocking.
  72. winState.renderThreadBlocked = true;
  73. #ifdef UNICODE
  74. const UTF16 *msg = convertUTF8toUTF16(message);
  75. const UTF16 *t = convertUTF8toUTF16(title);
  76. #else
  77. const UTF8 *msg = message;
  78. const UTF8 *t = title;
  79. #endif
  80. S32 ret = ::MessageBox(winState.appWindow, (LPCWSTR)msg, (LPCWSTR)t, getMaskFromID(sgButtonMap, buttons) | getMaskFromID(sgIconMap, icon));
  81. #ifdef UNICODE
  82. delete [] msg;
  83. delete [] t;
  84. #endif
  85. // Dialog is gone.
  86. winState.renderThreadBlocked = false;
  87. return getMaskFromID(sgMsgBoxRetMap, ret);
  88. }