OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "stdafx.h" | 5 #include "stdafx.h" |
6 #include <corewindow.h> | 6 #include <corewindow.h> |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 | 9 |
10 EXTERN_C IMAGE_DOS_HEADER __ImageBase; | 10 EXTERN_C IMAGE_DOS_HEADER __ImageBase; |
| 11 int g_window_count = 0; |
11 | 12 |
12 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, | 13 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
13 WPARAM wparam, LPARAM lparam) { | 14 WPARAM wparam, LPARAM lparam) { |
14 PAINTSTRUCT ps; | 15 PAINTSTRUCT ps; |
15 HDC hdc; | 16 HDC hdc; |
16 switch (message) { | 17 switch (message) { |
| 18 case WM_CREATE: |
| 19 ++g_window_count; |
| 20 break; |
17 case WM_PAINT: | 21 case WM_PAINT: |
18 hdc = ::BeginPaint(hwnd, &ps); | 22 hdc = ::BeginPaint(hwnd, &ps); |
19 EndPaint(hwnd, &ps); | 23 ::EndPaint(hwnd, &ps); |
20 break; | 24 break; |
21 case WM_LBUTTONUP: | 25 case WM_LBUTTONUP: |
22 // TODO(cpu): Remove this test code. | 26 // TODO(cpu): Remove this test code. |
23 ::InvalidateRect(hwnd, NULL, TRUE); | 27 ::InvalidateRect(hwnd, NULL, TRUE); |
24 break; | 28 break; |
| 29 case WM_CLOSE: |
| 30 ::DestroyWindow(hwnd); |
| 31 break; |
25 case WM_DESTROY: | 32 case WM_DESTROY: |
26 PostQuitMessage(0); | 33 --g_window_count; |
| 34 if (!g_window_count) |
| 35 ::PostQuitMessage(0); |
27 break; | 36 break; |
28 default: | 37 default: |
29 return ::DefWindowProc(hwnd, message, wparam, lparam); | 38 return ::DefWindowProc(hwnd, message, wparam, lparam); |
30 } | 39 } |
31 return 0; | 40 return 0; |
32 } | 41 } |
33 | 42 |
34 HWND CreateMetroTopLevelWindow() { | 43 HWND CreateMetroTopLevelWindow() { |
35 HINSTANCE hInst = reinterpret_cast<HINSTANCE>(&__ImageBase); | 44 HINSTANCE hInst = reinterpret_cast<HINSTANCE>(&__ImageBase); |
36 WNDCLASSEXW wcex; | 45 WNDCLASSEXW wcex; |
37 wcex.cbSize = sizeof(wcex); | 46 wcex.cbSize = sizeof(wcex); |
38 wcex.style = CS_HREDRAW | CS_VREDRAW; | 47 wcex.style = CS_HREDRAW | CS_VREDRAW; |
39 wcex.lpfnWndProc = WndProc; | 48 wcex.lpfnWndProc = WndProc; |
40 wcex.cbClsExtra = 0; | 49 wcex.cbClsExtra = 0; |
41 wcex.cbWndExtra = 0; | 50 wcex.cbWndExtra = 0; |
42 wcex.hInstance = hInst; | 51 wcex.hInstance = hInst; |
43 wcex.hIcon = 0; | 52 wcex.hIcon = 0; |
44 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); | 53 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); |
45 wcex.hbrBackground = (HBRUSH)(COLOR_INACTIVECAPTION+1); | 54 wcex.hbrBackground = (HBRUSH)(COLOR_INACTIVECAPTION+1); |
46 wcex.lpszMenuName = 0; | 55 wcex.lpszMenuName = 0; |
47 wcex.lpszClassName = L"Windows.UI.Core.CoreWindow"; | 56 wcex.lpszClassName = L"Windows.UI.Core.CoreWindow"; |
48 wcex.hIconSm = 0; | 57 wcex.hIconSm = 0; |
49 | 58 |
50 HWND hwnd = ::CreateWindowExW(0, | 59 HWND hwnd = ::CreateWindowExW(0, |
51 MAKEINTATOM(::RegisterClassExW(&wcex)), | 60 MAKEINTATOM(::RegisterClassExW(&wcex)), |
52 L"metro_win7", | 61 L"metro_win7", |
53 WS_POPUP | WS_VISIBLE, | 62 WS_POPUP | WS_VISIBLE, |
54 0, 0, 1024, 1024, | 63 0, 0, 1600, 900, |
55 NULL, NULL, hInst, NULL); | 64 NULL, NULL, hInst, NULL); |
56 return hwnd; | 65 return hwnd; |
57 } | 66 } |
58 | 67 |
59 typedef winfoundtn::ITypedEventHandler< | 68 typedef winfoundtn::ITypedEventHandler< |
60 winapp::Core::CoreApplicationView*, | 69 winapp::Core::CoreApplicationView*, |
61 winapp::Activation::IActivatedEventArgs*> ActivatedHandler; | 70 winapp::Activation::IActivatedEventArgs*> ActivatedHandler; |
62 | 71 |
63 typedef winfoundtn::ITypedEventHandler< | 72 typedef winfoundtn::ITypedEventHandler< |
64 winui::Core::CoreWindow*, | 73 winui::Core::CoreWindow*, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } | 148 } |
140 | 149 |
141 virtual HRESULT STDMETHODCALLTYPE ProcessEvents( | 150 virtual HRESULT STDMETHODCALLTYPE ProcessEvents( |
142 winui::Core::CoreProcessEventsOption options) { | 151 winui::Core::CoreProcessEventsOption options) { |
143 // We don't support the other message pump modes. So we basically enter a | 152 // We don't support the other message pump modes. So we basically enter a |
144 // traditional message loop that we only exit a teardown. | 153 // traditional message loop that we only exit a teardown. |
145 if (options != winui::Core::CoreProcessEventsOption_ProcessUntilQuit) | 154 if (options != winui::Core::CoreProcessEventsOption_ProcessUntilQuit) |
146 return E_FAIL; | 155 return E_FAIL; |
147 | 156 |
148 MSG msg = {0}; | 157 MSG msg = {0}; |
149 while(::GetMessage(&msg, NULL, 0, 0) != 0) { | 158 while((::GetMessage(&msg, NULL, 0, 0) != 0) && g_window_count > 0) { |
150 ::TranslateMessage(&msg); | 159 ::TranslateMessage(&msg); |
151 ::DispatchMessage(&msg); | 160 ::DispatchMessage(&msg); |
152 } | 161 } |
153 // TODO(cpu): figure what to do with msg.WParam which we would normally | 162 // TODO(cpu): figure what to do with msg.WParam which we would normally |
154 // return here. | 163 // return here. |
155 return S_OK; | 164 return S_OK; |
156 } | 165 } |
157 | 166 |
158 virtual HRESULT STDMETHODCALLTYPE RunAsync( | 167 virtual HRESULT STDMETHODCALLTYPE RunAsync( |
159 winui::Core::CoreDispatcherPriority priority, | 168 winui::Core::CoreDispatcherPriority priority, |
(...skipping 27 matching lines...) Expand all Loading... |
187 : public mswr::RuntimeClass< | 196 : public mswr::RuntimeClass< |
188 mswr::RuntimeClassFlags<mswr::WinRtClassicComMix>, | 197 mswr::RuntimeClassFlags<mswr::WinRtClassicComMix>, |
189 winui::Core::ICoreWindow, ICoreWindowInterop> { | 198 winui::Core::ICoreWindow, ICoreWindowInterop> { |
190 public: | 199 public: |
191 CoreWindowEmulation() : core_hwnd_(NULL) { | 200 CoreWindowEmulation() : core_hwnd_(NULL) { |
192 dispatcher_ = mswr::Make<CoreDispacherEmulation>(); | 201 dispatcher_ = mswr::Make<CoreDispacherEmulation>(); |
193 core_hwnd_ = CreateMetroTopLevelWindow(); | 202 core_hwnd_ = CreateMetroTopLevelWindow(); |
194 } | 203 } |
195 | 204 |
196 ~CoreWindowEmulation() { | 205 ~CoreWindowEmulation() { |
197 ::DestroyWindow(core_hwnd_); | 206 if (core_hwnd_) |
| 207 ::DestroyWindow(core_hwnd_); |
198 } | 208 } |
199 | 209 |
200 // ICoreWindow implementation: | 210 // ICoreWindow implementation: |
201 virtual HRESULT STDMETHODCALLTYPE get_AutomationHostProvider( | 211 virtual HRESULT STDMETHODCALLTYPE get_AutomationHostProvider( |
202 IInspectable** value) { | 212 IInspectable** value) { |
203 return S_OK; | 213 return S_OK; |
204 } | 214 } |
205 | 215 |
206 virtual HRESULT STDMETHODCALLTYPE get_Bounds( | 216 virtual HRESULT STDMETHODCALLTYPE get_Bounds( |
207 winfoundtn::Rect* value) { | 217 winfoundtn::Rect* value) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 boolean* value) { | 272 boolean* value) { |
263 return S_OK; | 273 return S_OK; |
264 } | 274 } |
265 | 275 |
266 virtual HRESULT STDMETHODCALLTYPE Activate(void) { | 276 virtual HRESULT STDMETHODCALLTYPE Activate(void) { |
267 // After we fire OnActivate on the View, Chrome calls us back here. | 277 // After we fire OnActivate on the View, Chrome calls us back here. |
268 return S_OK; | 278 return S_OK; |
269 } | 279 } |
270 | 280 |
271 virtual HRESULT STDMETHODCALLTYPE Close(void) { | 281 virtual HRESULT STDMETHODCALLTYPE Close(void) { |
| 282 ::PostMessage(core_hwnd_, WM_CLOSE, 0, 0); |
| 283 core_hwnd_ = NULL; |
272 return S_OK; | 284 return S_OK; |
273 } | 285 } |
274 | 286 |
275 virtual HRESULT STDMETHODCALLTYPE GetAsyncKeyState( | 287 virtual HRESULT STDMETHODCALLTYPE GetAsyncKeyState( |
276 ABI::Windows::System::VirtualKey virtualKey, | 288 ABI::Windows::System::VirtualKey virtualKey, |
277 winui::Core::CoreVirtualKeyStates* KeyState) { | 289 winui::Core::CoreVirtualKeyStates* KeyState) { |
278 return S_OK; | 290 return S_OK; |
279 } | 291 } |
280 | 292 |
281 virtual HRESULT STDMETHODCALLTYPE GetKeyState( | 293 virtual HRESULT STDMETHODCALLTYPE GetKeyState( |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 HRESULT Activate() { | 555 HRESULT Activate() { |
544 if (activated_handler_) { | 556 if (activated_handler_) { |
545 auto ae = mswr::Make<ActivatedEvent>( | 557 auto ae = mswr::Make<ActivatedEvent>( |
546 winapp::Activation::ActivationKind_File); | 558 winapp::Activation::ActivationKind_File); |
547 return activated_handler_->Invoke(this, ae.Get()); | 559 return activated_handler_->Invoke(this, ae.Get()); |
548 } else { | 560 } else { |
549 return S_OK; | 561 return S_OK; |
550 } | 562 } |
551 } | 563 } |
552 | 564 |
| 565 HRESULT Close() { |
| 566 return core_window_->Close(); |
| 567 } |
| 568 |
553 // ICoreApplicationView implementation: | 569 // ICoreApplicationView implementation: |
554 virtual HRESULT STDMETHODCALLTYPE get_CoreWindow( | 570 virtual HRESULT STDMETHODCALLTYPE get_CoreWindow( |
555 winui::Core::ICoreWindow** value) { | 571 winui::Core::ICoreWindow** value) { |
556 if (!core_window_) | 572 if (!core_window_) |
557 return E_FAIL; | 573 return E_FAIL; |
558 return core_window_.CopyTo(value); | 574 return core_window_.CopyTo(value); |
559 } | 575 } |
560 | 576 |
561 virtual HRESULT STDMETHODCALLTYPE add_Activated( | 577 virtual HRESULT STDMETHODCALLTYPE add_Activated( |
562 ActivatedHandler* handler, | 578 ActivatedHandler* handler, |
(...skipping 15 matching lines...) Expand all Loading... |
578 boolean* value) { | 594 boolean* value) { |
579 return S_OK; | 595 return S_OK; |
580 } | 596 } |
581 | 597 |
582 virtual HRESULT STDMETHODCALLTYPE get_IsHosted( | 598 virtual HRESULT STDMETHODCALLTYPE get_IsHosted( |
583 boolean* value) { | 599 boolean* value) { |
584 return S_OK; | 600 return S_OK; |
585 } | 601 } |
586 | 602 |
587 private: | 603 private: |
588 mswr::ComPtr<winui::Core::ICoreWindow> core_window_; | 604 mswr::ComPtr<CoreWindowEmulation> core_window_; |
589 mswr::ComPtr<ActivatedHandler> activated_handler_; | 605 mswr::ComPtr<ActivatedHandler> activated_handler_; |
590 }; | 606 }; |
591 | 607 |
592 class CoreApplicationWin7Emulation | 608 class CoreApplicationWin7Emulation |
593 : public mswr::RuntimeClass<winapp::Core::ICoreApplication, | 609 : public mswr::RuntimeClass<winapp::Core::ICoreApplication, |
594 winapp::Core::ICoreApplicationExit> { | 610 winapp::Core::ICoreApplicationExit> { |
595 public: | 611 public: |
596 // ICoreApplication implementation: | 612 // ICoreApplication implementation: |
597 | 613 |
598 virtual HRESULT STDMETHODCALLTYPE get_Id( | 614 virtual HRESULT STDMETHODCALLTYPE get_Id( |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 } | 674 } |
659 | 675 |
660 virtual HRESULT STDMETHODCALLTYPE RunWithActivationFactories( | 676 virtual HRESULT STDMETHODCALLTYPE RunWithActivationFactories( |
661 winfoundtn::IGetActivationFactory* activationFactoryCallback) { | 677 winfoundtn::IGetActivationFactory* activationFactoryCallback) { |
662 return S_OK; | 678 return S_OK; |
663 } | 679 } |
664 | 680 |
665 // ICoreApplicationExit implementation: | 681 // ICoreApplicationExit implementation: |
666 | 682 |
667 virtual HRESULT STDMETHODCALLTYPE Exit(void) { | 683 virtual HRESULT STDMETHODCALLTYPE Exit(void) { |
668 return S_OK; | 684 return view_emulation_->Close(); |
669 } | 685 } |
670 | 686 |
671 virtual HRESULT STDMETHODCALLTYPE add_Exiting( | 687 virtual HRESULT STDMETHODCALLTYPE add_Exiting( |
672 winfoundtn::IEventHandler<IInspectable*>* handler, | 688 winfoundtn::IEventHandler<IInspectable*>* handler, |
673 EventRegistrationToken* token) { | 689 EventRegistrationToken* token) { |
674 return S_OK; | 690 return S_OK; |
675 } | 691 } |
676 | 692 |
677 virtual HRESULT STDMETHODCALLTYPE remove_Exiting( | 693 virtual HRESULT STDMETHODCALLTYPE remove_Exiting( |
678 EventRegistrationToken token) { | 694 EventRegistrationToken token) { |
679 return S_OK; | 695 return S_OK; |
680 } | 696 } |
681 | 697 |
682 private: | 698 private: |
683 mswr::ComPtr<winapp::Core::IFrameworkView> app_view_; | 699 mswr::ComPtr<winapp::Core::IFrameworkView> app_view_; |
684 mswr::ComPtr<CoreApplicationViewEmulation> view_emulation_; | 700 mswr::ComPtr<CoreApplicationViewEmulation> view_emulation_; |
685 }; | 701 }; |
686 | 702 |
687 | 703 |
688 mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows7() { | 704 mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows7() { |
689 HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED); | 705 HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED); |
690 if (FAILED(hr)) | 706 if (FAILED(hr)) |
691 CHECK(false); | 707 CHECK(false); |
692 return mswr::Make<CoreApplicationWin7Emulation>(); | 708 return mswr::Make<CoreApplicationWin7Emulation>(); |
693 } | 709 } |
694 | 710 |
OLD | NEW |