ovr_sdk
diff LibOVR/Src/Util/Util_SystemGUI.cpp @ 0:1b39a1b46319
initial 0.4.4
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 14 Jan 2015 06:51:16 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/LibOVR/Src/Util/Util_SystemGUI.cpp Wed Jan 14 06:51:16 2015 +0200 1.3 @@ -0,0 +1,190 @@ 1.4 +/************************************************************************************ 1.5 + 1.6 +Filename : Util_SystemGUI.cpp 1.7 +Content : OS GUI access, usually for diagnostics. 1.8 +Created : October 20, 2014 1.9 +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. 1.10 + 1.11 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 1.12 +you may not use the Oculus VR Rift SDK except in compliance with the License, 1.13 +which is provided at the time of installation or download, or which 1.14 +otherwise accompanies this software in either electronic or hard copy form. 1.15 + 1.16 +You may obtain a copy of the License at 1.17 + 1.18 +http://www.oculusvr.com/licenses/LICENSE-3.2 1.19 + 1.20 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 1.21 +distributed under the License is distributed on an "AS IS" BASIS, 1.22 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.23 +See the License for the specific language governing permissions and 1.24 +limitations under the License. 1.25 + 1.26 +*************************************************************************************/ 1.27 + 1.28 +#include "Util_SystemGUI.h" 1.29 +#include "../Kernel/OVR_UTF8Util.h" 1.30 +#include <stdio.h> 1.31 + 1.32 +#if defined(OVR_OS_MS) 1.33 + #include <Windows.h> 1.34 +#endif 1.35 + 1.36 + 1.37 +namespace OVR { namespace Util { 1.38 + 1.39 + 1.40 +#if defined(OVR_OS_MS) 1.41 + 1.42 + // On Windows we implement a manual dialog message box. The reason for this is that there's no way to 1.43 + // have a message box like this without either using MFC or WinForms or relying on Windows Vista+. 1.44 + 1.45 + bool DisplayMessageBox(const char* pTitle, const char* pText) 1.46 + { 1.47 + #define ID_EDIT 100 1.48 + 1.49 + struct Dialog 1.50 + { 1.51 + static size_t LineCount(const char* pText) 1.52 + { 1.53 + size_t count = 0; 1.54 + while(*pText) 1.55 + { 1.56 + if(*pText++ == '\n') 1.57 + count++; 1.58 + } 1.59 + return count; 1.60 + } 1.61 + 1.62 + static WORD* WordUp(WORD* pIn){ return (WORD*)((((uintptr_t)pIn + 3) >> 2) << 2); } 1.63 + 1.64 + static BOOL CALLBACK Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) 1.65 + { 1.66 + switch (iMsg) 1.67 + { 1.68 + case WM_INITDIALOG: 1.69 + { 1.70 + HWND hWndEdit = GetDlgItem(hDlg, ID_EDIT); 1.71 + 1.72 + const char* pText = (const char*)lParam; 1.73 + SetWindowTextA(hWndEdit, pText); 1.74 + 1.75 + 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"); 1.76 + if(hFont) 1.77 + SendMessage(hWndEdit, WM_SETFONT, WPARAM(hFont), TRUE); 1.78 + 1.79 + SendMessage(hWndEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0); 1.80 + 1.81 + return TRUE; 1.82 + } 1.83 + 1.84 + case WM_COMMAND: 1.85 + switch (LOWORD(wParam)) 1.86 + { 1.87 + case ID_EDIT: 1.88 + { 1.89 + // Handle messages from the edit control here. 1.90 + HWND hWndEdit = GetDlgItem(hDlg, ID_EDIT); 1.91 + SendMessage(hWndEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0); 1.92 + return TRUE; 1.93 + } 1.94 + 1.95 + case IDOK: 1.96 + EndDialog(hDlg, 0); 1.97 + return TRUE; 1.98 + } 1.99 + break; 1.100 + } 1.101 + 1.102 + return FALSE; 1.103 + } 1.104 + }; 1.105 + 1.106 + 1.107 + char dialogTemplateMemory[1024]; 1.108 + memset(dialogTemplateMemory, 0, sizeof(dialogTemplateMemory)); 1.109 + DLGTEMPLATE* pDlg = (LPDLGTEMPLATE)dialogTemplateMemory; 1.110 + 1.111 + const size_t textLineCount = Dialog::LineCount(pText); 1.112 + 1.113 + // 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. 1.114 + const int kGutterSize = 6; // Empty border space around controls within the dialog 1.115 + const int kButtonWidth = 24; 1.116 + const int kButtonHeight = 10; 1.117 + const int kDialogWidth = 600; // To do: Clip this against screen bounds. 1.118 + const int kDialogHeight = ((textLineCount > 100) ? 400 : ((textLineCount > 25) ? 300 : 200)); 1.119 + 1.120 + // Define a dialog box. 1.121 + pDlg->style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION; 1.122 + pDlg->cdit = 2; // Control count 1.123 + pDlg->x = 10; // X position To do: Center the dialog. 1.124 + pDlg->y = 10; 1.125 + pDlg->cx = (short)kDialogWidth; 1.126 + pDlg->cy = (short)kDialogHeight; 1.127 + WORD* pWord = (WORD*)(pDlg + 1); 1.128 + *pWord++ = 0; // No menu 1.129 + *pWord++ = 0; // Default dialog box class 1.130 + 1.131 + WCHAR* pWchar = (WCHAR*)pWord; 1.132 + const size_t titleLength = strlen(pTitle); 1.133 + size_t wcharCount = OVR::UTF8Util::DecodeString(pWchar, pTitle, (titleLength > 128) ? 128 : titleLength); 1.134 + pWord += wcharCount + 1; 1.135 + 1.136 + // Define an OK button. 1.137 + pWord = Dialog::WordUp(pWord); 1.138 + 1.139 + DLGITEMTEMPLATE* pDlgItem = (DLGITEMTEMPLATE*)pWord; 1.140 + pDlgItem->x = pDlg->cx - (kGutterSize + kButtonWidth); 1.141 + pDlgItem->y = pDlg->cy - (kGutterSize + kButtonHeight); 1.142 + pDlgItem->cx = kButtonWidth; 1.143 + pDlgItem->cy = kButtonHeight; 1.144 + pDlgItem->id = IDOK; 1.145 + pDlgItem->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON; 1.146 + 1.147 + pWord = (WORD*)(pDlgItem + 1); 1.148 + *pWord++ = 0xFFFF; 1.149 + *pWord++ = 0x0080; // button class 1.150 + 1.151 + pWchar = (WCHAR*)pWord; 1.152 + pWchar[0] = 'O'; pWchar[1] = 'K'; pWchar[2] = '\0'; // Not currently localized. 1.153 + pWord += 3; // OK\0 1.154 + *pWord++ = 0; // no creation data 1.155 + 1.156 + // Define an EDIT contol. 1.157 + pWord = Dialog::WordUp(pWord); 1.158 + 1.159 + pDlgItem = (DLGITEMTEMPLATE*)pWord; 1.160 + pDlgItem->x = kGutterSize; 1.161 + pDlgItem->y = kGutterSize; 1.162 + pDlgItem->cx = pDlg->cx - (kGutterSize + kGutterSize); 1.163 + pDlgItem->cy = pDlg->cy - (kGutterSize + kButtonHeight + kGutterSize + (kGutterSize / 2)); 1.164 + pDlgItem->id = ID_EDIT; 1.165 + pDlgItem->style = ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN | ES_READONLY | WS_VSCROLL | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE; 1.166 + 1.167 + pWord = (WORD*)(pDlgItem + 1); 1.168 + *pWord++ = 0xFFFF; 1.169 + *pWord++ = 0x0081; // edit class atom 1.170 + *pWord++ = 0; // no creation data 1.171 + 1.172 + LRESULT ret = DialogBoxIndirectParam(NULL, (LPDLGTEMPLATE)pDlg, NULL, (DLGPROC)Dialog::Proc, (LPARAM)pText); 1.173 + 1.174 + return (ret != 0); 1.175 + } 1.176 +#elif defined(OVR_OS_MAC) 1.177 + // For Apple we use the Objective C implementation in Util_GUI.mm 1.178 +#else 1.179 + // To do. 1.180 + bool DisplayMessageBox(const char* pTitle, const char* pText) 1.181 + { 1.182 + printf("\n\nMessageBox\n%s\n", pTitle); 1.183 + printf("%s\n\n", pText); 1.184 + return false; 1.185 + } 1.186 +#endif 1.187 + 1.188 + 1.189 +} } // namespace OVR::Util 1.190 + 1.191 + 1.192 + 1.193 +