ovr_sdk

view 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 source
1 /************************************************************************************
3 Filename : Util_SystemGUI.cpp
4 Content : OS GUI access, usually for diagnostics.
5 Created : October 20, 2014
6 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
8 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
9 you may not use the Oculus VR Rift SDK except in compliance with the License,
10 which is provided at the time of installation or download, or which
11 otherwise accompanies this software in either electronic or hard copy form.
13 You may obtain a copy of the License at
15 http://www.oculusvr.com/licenses/LICENSE-3.2
17 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
23 *************************************************************************************/
25 #include "Util_SystemGUI.h"
26 #include "../Kernel/OVR_UTF8Util.h"
27 #include <stdio.h>
29 #if defined(OVR_OS_MS)
30 #include <Windows.h>
31 #endif
34 namespace OVR { namespace Util {
37 #if defined(OVR_OS_MS)
39 // On Windows we implement a manual dialog message box. The reason for this is that there's no way to
40 // have a message box like this without either using MFC or WinForms or relying on Windows Vista+.
42 bool DisplayMessageBox(const char* pTitle, const char* pText)
43 {
44 #define ID_EDIT 100
46 struct Dialog
47 {
48 static size_t LineCount(const char* pText)
49 {
50 size_t count = 0;
51 while(*pText)
52 {
53 if(*pText++ == '\n')
54 count++;
55 }
56 return count;
57 }
59 static WORD* WordUp(WORD* pIn){ return (WORD*)((((uintptr_t)pIn + 3) >> 2) << 2); }
61 static BOOL CALLBACK Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
62 {
63 switch (iMsg)
64 {
65 case WM_INITDIALOG:
66 {
67 HWND hWndEdit = GetDlgItem(hDlg, ID_EDIT);
69 const char* pText = (const char*)lParam;
70 SetWindowTextA(hWndEdit, pText);
72 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");
73 if(hFont)
74 SendMessage(hWndEdit, WM_SETFONT, WPARAM(hFont), TRUE);
76 SendMessage(hWndEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0);
78 return TRUE;
79 }
81 case WM_COMMAND:
82 switch (LOWORD(wParam))
83 {
84 case ID_EDIT:
85 {
86 // Handle messages from the edit control here.
87 HWND hWndEdit = GetDlgItem(hDlg, ID_EDIT);
88 SendMessage(hWndEdit, EM_SETSEL, (WPARAM)0, (LPARAM)0);
89 return TRUE;
90 }
92 case IDOK:
93 EndDialog(hDlg, 0);
94 return TRUE;
95 }
96 break;
97 }
99 return FALSE;
100 }
101 };
104 char dialogTemplateMemory[1024];
105 memset(dialogTemplateMemory, 0, sizeof(dialogTemplateMemory));
106 DLGTEMPLATE* pDlg = (LPDLGTEMPLATE)dialogTemplateMemory;
108 const size_t textLineCount = Dialog::LineCount(pText);
110 // 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.
111 const int kGutterSize = 6; // Empty border space around controls within the dialog
112 const int kButtonWidth = 24;
113 const int kButtonHeight = 10;
114 const int kDialogWidth = 600; // To do: Clip this against screen bounds.
115 const int kDialogHeight = ((textLineCount > 100) ? 400 : ((textLineCount > 25) ? 300 : 200));
117 // Define a dialog box.
118 pDlg->style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;
119 pDlg->cdit = 2; // Control count
120 pDlg->x = 10; // X position To do: Center the dialog.
121 pDlg->y = 10;
122 pDlg->cx = (short)kDialogWidth;
123 pDlg->cy = (short)kDialogHeight;
124 WORD* pWord = (WORD*)(pDlg + 1);
125 *pWord++ = 0; // No menu
126 *pWord++ = 0; // Default dialog box class
128 WCHAR* pWchar = (WCHAR*)pWord;
129 const size_t titleLength = strlen(pTitle);
130 size_t wcharCount = OVR::UTF8Util::DecodeString(pWchar, pTitle, (titleLength > 128) ? 128 : titleLength);
131 pWord += wcharCount + 1;
133 // Define an OK button.
134 pWord = Dialog::WordUp(pWord);
136 DLGITEMTEMPLATE* pDlgItem = (DLGITEMTEMPLATE*)pWord;
137 pDlgItem->x = pDlg->cx - (kGutterSize + kButtonWidth);
138 pDlgItem->y = pDlg->cy - (kGutterSize + kButtonHeight);
139 pDlgItem->cx = kButtonWidth;
140 pDlgItem->cy = kButtonHeight;
141 pDlgItem->id = IDOK;
142 pDlgItem->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;
144 pWord = (WORD*)(pDlgItem + 1);
145 *pWord++ = 0xFFFF;
146 *pWord++ = 0x0080; // button class
148 pWchar = (WCHAR*)pWord;
149 pWchar[0] = 'O'; pWchar[1] = 'K'; pWchar[2] = '\0'; // Not currently localized.
150 pWord += 3; // OK\0
151 *pWord++ = 0; // no creation data
153 // Define an EDIT contol.
154 pWord = Dialog::WordUp(pWord);
156 pDlgItem = (DLGITEMTEMPLATE*)pWord;
157 pDlgItem->x = kGutterSize;
158 pDlgItem->y = kGutterSize;
159 pDlgItem->cx = pDlg->cx - (kGutterSize + kGutterSize);
160 pDlgItem->cy = pDlg->cy - (kGutterSize + kButtonHeight + kGutterSize + (kGutterSize / 2));
161 pDlgItem->id = ID_EDIT;
162 pDlgItem->style = ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN | ES_READONLY | WS_VSCROLL | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
164 pWord = (WORD*)(pDlgItem + 1);
165 *pWord++ = 0xFFFF;
166 *pWord++ = 0x0081; // edit class atom
167 *pWord++ = 0; // no creation data
169 LRESULT ret = DialogBoxIndirectParam(NULL, (LPDLGTEMPLATE)pDlg, NULL, (DLGPROC)Dialog::Proc, (LPARAM)pText);
171 return (ret != 0);
172 }
173 #elif defined(OVR_OS_MAC)
174 // For Apple we use the Objective C implementation in Util_GUI.mm
175 #else
176 // To do.
177 bool DisplayMessageBox(const char* pTitle, const char* pText)
178 {
179 printf("\n\nMessageBox\n%s\n", pTitle);
180 printf("%s\n\n", pText);
181 return false;
182 }
183 #endif
186 } } // namespace OVR::Util