Util_SystemGUI.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /************************************************************************************
  2. Filename : Util_SystemGUI.cpp
  3. Content : OS GUI access, usually for diagnostics.
  4. Created : October 20, 2014
  5. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  6. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  7. you may not use the Oculus VR Rift SDK except in compliance with the License,
  8. which is provided at the time of installation or download, or which
  9. otherwise accompanies this software in either electronic or hard copy form.
  10. You may obtain a copy of the License at
  11. http://www.oculusvr.com/licenses/LICENSE-3.2
  12. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. *************************************************************************************/
  18. #include "Util_SystemGUI.h"
  19. #include "Kernel/OVR_UTF8Util.h"
  20. #if defined(OVR_OS_WIN32)
  21. #include "Kernel/OVR_Win32_IncludeWindows.h"
  22. #endif // OVR_OS_WIN32
  23. #include "Kernel/OVR_Std.h"
  24. #include "Kernel/OVR_Allocator.h"
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. namespace OVR { namespace Util {
  28. bool DisplayMessageBoxVaList(const char* pTitle, const char* pFormat, va_list argList)
  29. {
  30. char buffer[512];
  31. char* pBuffer = buffer;
  32. char* pAllocated = nullptr;
  33. va_list argListSaved;
  34. OVR_VA_COPY(argListSaved, argList);
  35. int result = OVR_vsnprintf(buffer, OVR_ARRAY_COUNT(buffer), pFormat, argList); // Returns the required strlen of buffer.
  36. if(result >= (int)OVR_ARRAY_COUNT(buffer)) // If there was insufficient capacity...
  37. {
  38. pAllocated = new char[result + 1];
  39. pBuffer = pAllocated;
  40. va_end(argList); // The caller owns argList and will call va_end on it.
  41. OVR_VA_COPY(argList, argListSaved);
  42. OVR_vsnprintf(pBuffer, (size_t)result + 1, pFormat, argList);
  43. }
  44. bool returnValue = DisplayMessageBox(pTitle, pBuffer);
  45. delete[] pAllocated; // OK if it's nullptr.
  46. return returnValue;
  47. }
  48. bool DisplayMessageBoxF(const char* pTitle, const char* pFormat, ...)
  49. {
  50. va_list argList;
  51. va_start(argList, pFormat);
  52. bool returnValue = DisplayMessageBoxVaList(pTitle, pFormat, argList);
  53. va_end(argList);
  54. return returnValue;
  55. }
  56. #if defined(OVR_OS_MS)
  57. // On Windows we implement a manual dialog message box. The reason for this is that there's no way to
  58. // have a message box like this without either using MFC or WinForms or relying on Windows Vista+.
  59. bool DisplayMessageBox(const char* pTitle, const char* pText)
  60. {
  61. #define ID_EDIT 100
  62. struct Dialog
  63. {
  64. static size_t LineCount(const char* pText)
  65. {
  66. size_t count = 0;
  67. while(*pText)
  68. {
  69. if(*pText++ == '\n')
  70. count++;
  71. }
  72. return count;
  73. }
  74. static WORD* WordUp(WORD* pIn){ return (WORD*)((((uintptr_t)pIn + 3) >> 2) << 2); }
  75. static BOOL CALLBACK Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  76. {
  77. switch (iMsg)
  78. {
  79. case WM_INITDIALOG:
  80. {
  81. HWND hWndEdit = GetDlgItem(hDlg, ID_EDIT);
  82. const wchar_t* pText = (const wchar_t*)lParam;
  83. SetWindowTextW(hWndEdit, pText);
  84. HFONT hFont = CreateFontW(-11, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Courier New");
  85. if(hFont)
  86. SendMessage(hWndEdit, WM_SETFONT, WPARAM(hFont), TRUE);
  87. SendMessage(hWndEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0);
  88. return TRUE;
  89. }
  90. case WM_COMMAND:
  91. switch (LOWORD(wParam))
  92. {
  93. case ID_EDIT:
  94. {
  95. // Handle messages from the edit control here.
  96. HWND hWndEdit = GetDlgItem(hDlg, ID_EDIT);
  97. SendMessage(hWndEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0);
  98. return TRUE;
  99. }
  100. case IDOK:
  101. EndDialog(hDlg, 0);
  102. return TRUE;
  103. case IDABORT:
  104. _exit(0); // We don't call abort() because the visual studio runtime
  105. // will capture the signal and throw up another dialog
  106. }
  107. break;
  108. case WM_CLOSE:
  109. EndDialog(hDlg, 0);
  110. return TRUE;
  111. }
  112. return FALSE;
  113. }
  114. };
  115. char dialogTemplateMemory[1024];
  116. memset(dialogTemplateMemory, 0, sizeof(dialogTemplateMemory));
  117. LPDLGTEMPLATEW pDlg = (LPDLGTEMPLATEW)dialogTemplateMemory;
  118. const size_t textLength = strlen(pText);
  119. const size_t textLineCount = Dialog::LineCount(pText);
  120. // Sizes are in Windows dialog units, which are relative to a character size. Depends on the font and environment settings. Often the pixel size will be ~3x the dialog unit x size. Often the pixel size will be ~3x the dialog unit y size.
  121. const int kGutterSize = 6; // Empty border space around controls within the dialog
  122. const int kButtonWidth = 30;
  123. const int kButtonHeight = 10;
  124. const int kDialogWidth = ((textLength > 1000) ? 600 : ((textLength > 400) ? 300 : 200)); // To do: Clip this against screen bounds.
  125. const int kDialogHeight = ((textLineCount > 100) ? 400 : ((textLineCount > 25) ? 300 : ((textLineCount > 10) ? 200 : 100)));
  126. // Define a dialog box.
  127. pDlg->style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;
  128. pDlg->cdit = 3; // Control count
  129. pDlg->x = 10; // X position To do: Center the dialog.
  130. pDlg->y = 10;
  131. pDlg->cx = (short)kDialogWidth;
  132. pDlg->cy = (short)kDialogHeight;
  133. WORD* pWord = (WORD*)(pDlg + 1);
  134. *pWord++ = 0; // No menu
  135. *pWord++ = 0; // Default dialog box class
  136. WCHAR* pWchar = (WCHAR*)pWord;
  137. const size_t titleLength = strlen(pTitle);
  138. size_t wcharCount = OVR::UTF8Util::Strlcpy(pWchar, 128, pTitle, titleLength);
  139. pWord += wcharCount +1;
  140. // Define an OK button.
  141. pWord = Dialog::WordUp(pWord);
  142. PDLGITEMTEMPLATEW pDlgItem = (PDLGITEMTEMPLATEW)pWord;
  143. pDlgItem->x = pDlg->cx - (kGutterSize + kButtonWidth);
  144. pDlgItem->y = pDlg->cy - (kGutterSize + kButtonHeight);
  145. pDlgItem->cx = kButtonWidth;
  146. pDlgItem->cy = kButtonHeight;
  147. pDlgItem->id = IDOK;
  148. pDlgItem->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;
  149. pWord = (WORD*)(pDlgItem + 1);
  150. *pWord++ = 0xFFFF;
  151. *pWord++ = 0x0080; // button class
  152. pWchar = (WCHAR*)pWord;
  153. pWchar[0] = 'O'; pWchar[1] = 'K'; pWchar[2] = '\0'; // Not currently localized.
  154. pWord += 3; // OK\0
  155. *pWord++ = 0; // no creation data
  156. // Define a QUIT button
  157. pWord = Dialog::WordUp(pWord);
  158. pDlgItem = (PDLGITEMTEMPLATEW)pWord;
  159. pDlgItem->x = pDlg->cx - ((kGutterSize + kButtonWidth) * 2);
  160. pDlgItem->y = pDlg->cy - (kGutterSize + kButtonHeight);
  161. pDlgItem->cx = kButtonWidth;
  162. pDlgItem->cy = kButtonHeight;
  163. pDlgItem->id = IDABORT;
  164. pDlgItem->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;
  165. pWord = (WORD*)(pDlgItem + 1);
  166. *pWord++ = 0xFFFF;
  167. *pWord++ = 0x0080; // button class
  168. pWchar = (WCHAR*)pWord;
  169. pWchar[0] = 'Q'; pWchar[1] = 'U'; pWchar[2] = 'I'; pWchar[3] = 'T'; pWchar[4] = '\0'; // Not currently localized.
  170. pWord += 5; // QUIT\0
  171. *pWord++ = 0; // no creation data
  172. // Define an EDIT contol.
  173. pWord = Dialog::WordUp(pWord);
  174. pDlgItem = (PDLGITEMTEMPLATEW)pWord;
  175. pDlgItem->x = kGutterSize;
  176. pDlgItem->y = kGutterSize;
  177. pDlgItem->cx = pDlg->cx - (kGutterSize + kGutterSize);
  178. pDlgItem->cy = pDlg->cy - (kGutterSize + kButtonHeight + kGutterSize + (kGutterSize / 2));
  179. pDlgItem->id = ID_EDIT;
  180. pDlgItem->style = ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN | ES_READONLY | WS_VSCROLL | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
  181. pWord = (WORD*)(pDlgItem + 1);
  182. *pWord++ = 0xFFFF;
  183. *pWord++ = 0x0081; // edit class atom
  184. *pWord++ = 0; // no creation data
  185. // We use SafeMMapAlloc instead of malloc/new because we may be in a situation with a broken heap, which triggered this message box.
  186. LRESULT ret = 0;
  187. size_t textBuffWCapacity = textLength + 1;
  188. wchar_t* textBuffW = (wchar_t*)OVR::SafeMMapAlloc(textBuffWCapacity * sizeof(wchar_t));
  189. if(textBuffW)
  190. {
  191. OVR::UTF8Util::Strlcpy(textBuffW, textBuffWCapacity, pText, textLength);
  192. ret = DialogBoxIndirectParamW(NULL, (LPDLGTEMPLATEW)pDlg, NULL, (DLGPROC)Dialog::Proc, (LPARAM)textBuffW);
  193. OVR::SafeMMapFree(textBuffW, textBuffWCapacity * sizeof(wchar_t));
  194. }
  195. return (ret != 0);
  196. }
  197. #elif defined(OVR_OS_MAC)
  198. // For Apple we use the Objective C implementation in Util_GUI.mm
  199. #else
  200. // To do.
  201. bool DisplayMessageBox(const char* pTitle, const char* pText)
  202. {
  203. printf("\n\nMessageBox\n%s\n", pTitle);
  204. printf("%s\n\n", pText);
  205. return false;
  206. }
  207. #endif
  208. }} // namespace OVR::Util