2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "Window_win.h"
15 #include "VulkanTestContext_win.h"
17 Window* Window::CreateNativeWindow(void* platformData) {
18 HINSTANCE hInstance = (HINSTANCE)platformData;
20 Window_win* window = new Window_win();
21 if (!window->init(hInstance)) {
29 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
31 bool Window_win::init(HINSTANCE hInstance) {
32 fHInstance = hInstance ? hInstance : GetModuleHandle(nullptr);
35 // The main window class name
36 static const TCHAR gSZWindowClass[] = _T("SkiaApp");
38 wcex.cbSize = sizeof(WNDCLASSEX);
40 wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
41 wcex.lpfnWndProc = WndProc;
44 wcex.hInstance = fHInstance;
45 wcex.hIcon = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO);
46 wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);;
47 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
48 wcex.lpszMenuName = nullptr;
49 wcex.lpszClassName = gSZWindowClass;
50 wcex.hIconSm = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO);;
52 if (!RegisterClassEx(&wcex)) {
59 DEVMODE dmScreenSettings;
60 // If full screen set the screen to maximum size of the users desktop and 32bit.
61 memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
62 dmScreenSettings.dmSize = sizeof(dmScreenSettings);
63 dmScreenSettings.dmPelsWidth = (unsigned long)width;
64 dmScreenSettings.dmPelsHeight = (unsigned long)height;
65 dmScreenSettings.dmBitsPerPel = 32;
66 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
68 // Change the display settings to full screen.
69 ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
71 // Set the position of the window to the top left corner.
75 // gIsFullscreen = fullscreen;
77 fHWnd = CreateWindow(gSZWindowClass, nullptr, WS_OVERLAPPEDWINDOW,
78 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, fHInstance, nullptr);
84 SetWindowLongPtr(fHWnd, GWLP_USERDATA, (LONG_PTR)this);
89 static Window::Key get_key(WPARAM vk) {
94 { VK_BACK, Window::kBack_Key },
95 { VK_CLEAR, Window::kBack_Key },
96 { VK_RETURN, Window::kOK_Key },
97 { VK_UP, Window::kUp_Key },
98 { VK_DOWN, Window::kDown_Key },
99 { VK_LEFT, Window::kLeft_Key },
100 { VK_RIGHT, Window::kRight_Key }
102 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) {
103 if (gPair[i].fVK == vk) {
104 return gPair[i].fKey;
107 return Window::kNONE_Key;
110 static uint32_t get_modifiers(UINT message, WPARAM wParam, LPARAM lParam) {
111 uint32_t modifiers = 0;
116 if (0 == (lParam & (1 << 30))) {
117 modifiers |= Window::kFirstPress_ModifierKey;
119 if (lParam & (1 << 29)) {
120 modifiers |= Window::kOption_ModifierKey;
126 if (0 == (lParam & (1 << 30))) {
127 modifiers |= Window::kFirstPress_ModifierKey;
129 if (lParam & (1 << 29)) {
130 modifiers |= Window::kOption_ModifierKey;
136 if (lParam & (1 << 29)) {
137 modifiers |= Window::kOption_ModifierKey;
144 if (wParam & MK_CONTROL) {
145 modifiers |= Window::kControl_ModifierKey;
147 if (wParam & MK_SHIFT) {
148 modifiers |= Window::kShift_ModifierKey;
155 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
160 Window_win* window = (Window_win*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
162 bool eventHandled = false;
166 hdc = BeginPaint(hWnd, &ps);
179 // disable/enable rendering here, depending on wParam != WA_INACTIVE
183 window->onResize(LOWORD(lParam), HIWORD(lParam));
188 eventHandled = window->onChar((SkUnichar)wParam,
189 get_modifiers(message, wParam, lParam));
193 const uint16_t* c = reinterpret_cast<uint16_t*>(&wParam);
194 eventHandled = window->onChar(SkUTF16_NextUnichar(&c),
195 get_modifiers(message, wParam, lParam));
200 eventHandled = window->onKey(get_key(wParam), Window::kDown_InputState,
201 get_modifiers(message, wParam, lParam));
206 eventHandled = window->onKey(get_key(wParam), Window::kUp_InputState,
207 get_modifiers(message, wParam, lParam));
212 int xPos = GET_X_LPARAM(lParam);
213 int yPos = GET_Y_LPARAM(lParam);
215 //if (!gIsFullscreen)
217 // RECT rc = { 0, 0, 640, 480 };
218 // AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
223 Window::InputState istate = ((wParam & MK_LBUTTON) != 0) ? Window::kDown_InputState
224 : Window::kUp_InputState;
226 eventHandled = window->onMouse(xPos, yPos, istate,
227 get_modifiers(message, wParam, lParam));
231 // only track if left button is down
232 if ((wParam & MK_LBUTTON) != 0) {
233 int xPos = GET_X_LPARAM(lParam);
234 int yPos = GET_Y_LPARAM(lParam);
236 //if (!gIsFullscreen)
238 // RECT rc = { 0, 0, 640, 480 };
239 // AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
244 eventHandled = window->onMouse(xPos, yPos, Window::kMove_InputState,
245 get_modifiers(message, wParam, lParam));
250 return DefWindowProc(hWnd, message, wParam, lParam);
253 return eventHandled ? 0 : 1;
256 void Window_win::setTitle(const char* title) {
257 SetWindowTextA(fHWnd, title);
260 void Window_win::show() {
261 ShowWindow(fHWnd, SW_SHOW);
265 bool Window_win::attach(BackEndType attachType, int msaaSampleCount) {
266 if (kVulkan_BackendType != attachType) {
270 ContextPlatformData_win platformData;
271 platformData.fHInstance = fHInstance;
272 platformData.fHWnd = fHWnd;
274 fTestContext = VulkanTestContext::Create((void*)&platformData, msaaSampleCount);
276 return (SkToBool(fTestContext));
279 void Window_win::inval() {
280 InvalidateRect(fHWnd, nullptr, false);