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
3 // found in the LICENSE file.
6 #include <corewindow.h>
8 #include "base/logging.h"
10 EXTERN_C IMAGE_DOS_HEADER __ImageBase
;
11 int g_window_count
= 0;
13 LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
14 WPARAM wparam
, LPARAM lparam
) {
22 hdc
= ::BeginPaint(hwnd
, &ps
);
23 ::EndPaint(hwnd
, &ps
);
26 // TODO(cpu): Remove this test code.
27 ::InvalidateRect(hwnd
, NULL
, TRUE
);
30 ::DestroyWindow(hwnd
);
38 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
43 HWND
CreateMetroTopLevelWindow() {
44 HINSTANCE hInst
= reinterpret_cast<HINSTANCE
>(&__ImageBase
);
46 wcex
.cbSize
= sizeof(wcex
);
47 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
48 wcex
.lpfnWndProc
= WndProc
;
51 wcex
.hInstance
= hInst
;
53 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
54 wcex
.hbrBackground
= (HBRUSH
)(COLOR_INACTIVECAPTION
+1);
55 wcex
.lpszMenuName
= 0;
56 wcex
.lpszClassName
= L
"Windows.UI.Core.CoreWindow";
59 HWND hwnd
= ::CreateWindowExW(0,
60 MAKEINTATOM(::RegisterClassExW(&wcex
)),
62 WS_POPUP
| WS_VISIBLE
,
64 NULL
, NULL
, hInst
, NULL
);
68 typedef winfoundtn::ITypedEventHandler
<
69 winapp::Core::CoreApplicationView
*,
70 winapp::Activation::IActivatedEventArgs
*> ActivatedHandler
;
72 typedef winfoundtn::ITypedEventHandler
<
73 winui::Core::CoreWindow
*,
74 winui::Core::WindowActivatedEventArgs
*> WindowActivatedHandler
;
76 typedef winfoundtn::ITypedEventHandler
<
77 winui::Core::CoreWindow
*,
78 winui::Core::AutomationProviderRequestedEventArgs
*>
79 AutomationProviderHandler
;
81 typedef winfoundtn::ITypedEventHandler
<
82 winui::Core::CoreWindow
*,
83 winui::Core::CharacterReceivedEventArgs
*> CharEventHandler
;
85 typedef winfoundtn::ITypedEventHandler
<
86 winui::Core::CoreWindow
*,
87 winui::Core::CoreWindowEventArgs
*> CoreWindowEventHandler
;
89 typedef winfoundtn::ITypedEventHandler
<
90 winui::Core::CoreWindow
*,
91 winui::Core::InputEnabledEventArgs
*> InputEnabledEventHandler
;
93 typedef winfoundtn::ITypedEventHandler
<
94 winui::Core::CoreWindow
*,
95 winui::Core::KeyEventArgs
*> KeyEventHandler
;
97 typedef winfoundtn::ITypedEventHandler
<
98 winui::Core::CoreWindow
*,
99 winui::Core::PointerEventArgs
*> PointerEventHandler
;
101 typedef winfoundtn::ITypedEventHandler
<
102 winui::Core::CoreWindow
*,
103 winui::Core::WindowSizeChangedEventArgs
*> SizeChangedHandler
;
105 typedef winfoundtn::ITypedEventHandler
<
106 winui::Core::CoreWindow
*,
107 winui::Core::TouchHitTestingEventArgs
*> TouchHitTestHandler
;
109 typedef winfoundtn::ITypedEventHandler
<
110 winui::Core::CoreWindow
*,
111 winui::Core::VisibilityChangedEventArgs
*> VisibilityChangedHandler
;
113 typedef winfoundtn::ITypedEventHandler
<
114 winui::Core::CoreDispatcher
*,
115 winui::Core::AcceleratorKeyEventArgs
*> AcceleratorKeyEventHandler
;
117 // The following classes are the emulation of the WinRT system as exposed
118 // to metro applications. There is one application (ICoreApplication) which
119 // contains a series of Views (ICoreApplicationView) each one of them
120 // containing a CoreWindow which represents a surface that can drawn to
121 // and that receives events.
123 // Here is the general dependency hierachy in terms of interfaces:
125 // IFrameworkViewSource --> IFrameworkView
128 // ---------------------------------------------------------------------
131 // ICoreApplication ICoreApplicationView
134 // ICoreWindow -----> ICoreWindowInterop
138 // ICoreDispatcher <==> real HWND
140 class CoreDispacherEmulation
:
141 public mswr::RuntimeClass
<
142 winui::Core::ICoreDispatcher
,
143 winui::Core::ICoreAcceleratorKeys
> {
145 // ICoreDispatcher implementation:
146 virtual HRESULT STDMETHODCALLTYPE
get_HasThreadAccess(boolean
* value
) {
150 virtual HRESULT STDMETHODCALLTYPE
ProcessEvents(
151 winui::Core::CoreProcessEventsOption options
) {
152 // We don't support the other message pump modes. So we basically enter a
153 // traditional message loop that we only exit a teardown.
154 if (options
!= winui::Core::CoreProcessEventsOption_ProcessUntilQuit
)
158 while((::GetMessage(&msg
, NULL
, 0, 0) != 0) && g_window_count
> 0) {
159 ::TranslateMessage(&msg
);
160 ::DispatchMessage(&msg
);
162 // TODO(cpu): figure what to do with msg.WParam which we would normally
167 virtual HRESULT STDMETHODCALLTYPE
RunAsync(
168 winui::Core::CoreDispatcherPriority priority
,
169 winui::Core::IDispatchedHandler
*agileCallback
,
170 ABI::Windows::Foundation::IAsyncAction
** asyncAction
) {
174 virtual HRESULT STDMETHODCALLTYPE
RunIdleAsync(
175 winui::Core::IIdleDispatchedHandler
*agileCallback
,
176 winfoundtn::IAsyncAction
** asyncAction
) {
180 // ICoreAcceleratorKeys implementation:
181 virtual HRESULT STDMETHODCALLTYPE
add_AcceleratorKeyActivated(
182 AcceleratorKeyEventHandler
* handler
,
183 EventRegistrationToken
*pCookie
) {
184 // TODO(cpu): implement this.
188 virtual HRESULT STDMETHODCALLTYPE
remove_AcceleratorKeyActivated(
189 EventRegistrationToken cookie
) {
195 class CoreWindowEmulation
196 : public mswr::RuntimeClass
<
197 mswr::RuntimeClassFlags
<mswr::WinRtClassicComMix
>,
198 winui::Core::ICoreWindow
, ICoreWindowInterop
> {
200 CoreWindowEmulation() : core_hwnd_(NULL
) {
201 dispatcher_
= mswr::Make
<CoreDispacherEmulation
>();
202 core_hwnd_
= CreateMetroTopLevelWindow();
205 ~CoreWindowEmulation() {
207 ::DestroyWindow(core_hwnd_
);
210 // ICoreWindow implementation:
211 virtual HRESULT STDMETHODCALLTYPE
get_AutomationHostProvider(
212 IInspectable
** value
) {
216 virtual HRESULT STDMETHODCALLTYPE
get_Bounds(
217 winfoundtn::Rect
* value
) {
219 if (!::GetClientRect(core_hwnd_
, &rect
))
221 value
->Width
= rect
.right
;
222 value
->Height
= rect
.bottom
;
226 virtual HRESULT STDMETHODCALLTYPE
get_CustomProperties(
227 winfoundtn::Collections::IPropertySet
** value
) {
231 virtual HRESULT STDMETHODCALLTYPE
get_Dispatcher(
232 winui::Core::ICoreDispatcher
** value
) {
233 return dispatcher_
.CopyTo(value
);
236 virtual HRESULT STDMETHODCALLTYPE
get_FlowDirection(
237 winui::Core::CoreWindowFlowDirection
* value
) {
241 virtual HRESULT STDMETHODCALLTYPE
put_FlowDirection(
242 winui::Core::CoreWindowFlowDirection value
) {
246 virtual HRESULT STDMETHODCALLTYPE
get_IsInputEnabled(
251 virtual HRESULT STDMETHODCALLTYPE
put_IsInputEnabled(
256 virtual HRESULT STDMETHODCALLTYPE
get_PointerCursor(
257 winui::Core::ICoreCursor
** value
) {
261 virtual HRESULT STDMETHODCALLTYPE
put_PointerCursor(
262 winui::Core::ICoreCursor
* value
) {
266 virtual HRESULT STDMETHODCALLTYPE
get_PointerPosition(
267 winfoundtn::Point
* value
) {
271 virtual HRESULT STDMETHODCALLTYPE
get_Visible(
276 virtual HRESULT STDMETHODCALLTYPE
Activate(void) {
277 // After we fire OnActivate on the View, Chrome calls us back here.
281 virtual HRESULT STDMETHODCALLTYPE
Close(void) {
282 ::PostMessage(core_hwnd_
, WM_CLOSE
, 0, 0);
287 virtual HRESULT STDMETHODCALLTYPE
GetAsyncKeyState(
288 ABI::Windows::System::VirtualKey virtualKey
,
289 winui::Core::CoreVirtualKeyStates
* KeyState
) {
293 virtual HRESULT STDMETHODCALLTYPE
GetKeyState(
294 ABI::Windows::System::VirtualKey virtualKey
,
295 winui::Core::CoreVirtualKeyStates
* KeyState
) {
299 virtual HRESULT STDMETHODCALLTYPE
ReleasePointerCapture(void) {
303 virtual HRESULT STDMETHODCALLTYPE
SetPointerCapture(void) {
307 virtual HRESULT STDMETHODCALLTYPE
add_Activated(
308 WindowActivatedHandler
* handler
,
309 EventRegistrationToken
* pCookie
) {
310 // TODO(cpu) implement this.
314 virtual HRESULT STDMETHODCALLTYPE
remove_Activated(
315 EventRegistrationToken cookie
) {
319 virtual HRESULT STDMETHODCALLTYPE
add_AutomationProviderRequested(
320 AutomationProviderHandler
* handler
,
321 EventRegistrationToken
* cookie
) {
325 virtual HRESULT STDMETHODCALLTYPE
remove_AutomationProviderRequested(
326 EventRegistrationToken cookie
) {
330 virtual HRESULT STDMETHODCALLTYPE
add_CharacterReceived(
331 CharEventHandler
* handler
,
332 EventRegistrationToken
* pCookie
) {
333 // TODO(cpu) : implement this.
337 virtual HRESULT STDMETHODCALLTYPE
remove_CharacterReceived(
338 EventRegistrationToken cookie
) {
342 virtual HRESULT STDMETHODCALLTYPE
add_Closed(
343 CoreWindowEventHandler
* handler
,
344 EventRegistrationToken
* pCookie
) {
348 virtual HRESULT STDMETHODCALLTYPE
remove_Closed(
349 EventRegistrationToken cookie
) {
353 virtual HRESULT STDMETHODCALLTYPE
add_InputEnabled(
354 InputEnabledEventHandler
* handler
,
355 EventRegistrationToken
* pCookie
) {
359 virtual HRESULT STDMETHODCALLTYPE
remove_InputEnabled(
360 EventRegistrationToken cookie
) {
364 virtual HRESULT STDMETHODCALLTYPE
add_KeyDown(
365 KeyEventHandler
* handler
,
366 EventRegistrationToken
* pCookie
) {
367 // TODO(cpu): implement this.
371 virtual HRESULT STDMETHODCALLTYPE
remove_KeyDown(
372 EventRegistrationToken cookie
) {
376 virtual HRESULT STDMETHODCALLTYPE
add_KeyUp(
377 KeyEventHandler
* handler
,
378 EventRegistrationToken
* pCookie
) {
379 // TODO(cpu): implement this.
383 virtual HRESULT STDMETHODCALLTYPE
remove_KeyUp(
384 EventRegistrationToken cookie
) {
388 virtual HRESULT STDMETHODCALLTYPE
add_PointerCaptureLost(
389 PointerEventHandler
* handler
,
390 EventRegistrationToken
* cookie
) {
394 virtual HRESULT STDMETHODCALLTYPE
remove_PointerCaptureLost(
395 EventRegistrationToken cookie
) {
399 virtual HRESULT STDMETHODCALLTYPE
add_PointerEntered(
400 PointerEventHandler
* handler
,
401 EventRegistrationToken
* cookie
) {
405 virtual HRESULT STDMETHODCALLTYPE
remove_PointerEntered(
406 EventRegistrationToken cookie
) {
410 virtual HRESULT STDMETHODCALLTYPE
add_PointerExited(
411 PointerEventHandler
* handler
,
412 EventRegistrationToken
* cookie
) {
416 virtual HRESULT STDMETHODCALLTYPE
remove_PointerExited(
417 EventRegistrationToken cookie
) {
421 virtual HRESULT STDMETHODCALLTYPE
add_PointerMoved(
422 PointerEventHandler
* handler
,
423 EventRegistrationToken
* cookie
) {
424 // TODO(cpu) : implement this.
428 virtual HRESULT STDMETHODCALLTYPE
remove_PointerMoved(
429 EventRegistrationToken cookie
) {
433 virtual HRESULT STDMETHODCALLTYPE
add_PointerPressed(
434 PointerEventHandler
* handler
,
435 EventRegistrationToken
* cookie
) {
436 // TODO(cpu): implement this.
440 virtual HRESULT STDMETHODCALLTYPE
remove_PointerPressed(
441 EventRegistrationToken cookie
) {
445 virtual HRESULT STDMETHODCALLTYPE
add_PointerReleased(
446 PointerEventHandler
* handler
,
447 EventRegistrationToken
* cookie
) {
448 // TODO(cpu): implement this.
452 virtual HRESULT STDMETHODCALLTYPE
remove_PointerReleased(
453 EventRegistrationToken cookie
) {
457 virtual HRESULT STDMETHODCALLTYPE
add_TouchHitTesting(
458 TouchHitTestHandler
* handler
,
459 EventRegistrationToken
* pCookie
) {
463 virtual HRESULT STDMETHODCALLTYPE
remove_TouchHitTesting(
464 EventRegistrationToken cookie
) {
468 virtual HRESULT STDMETHODCALLTYPE
add_PointerWheelChanged(
469 PointerEventHandler
* handler
,
470 EventRegistrationToken
* cookie
) {
474 virtual HRESULT STDMETHODCALLTYPE
remove_PointerWheelChanged(
475 EventRegistrationToken cookie
) {
479 virtual HRESULT STDMETHODCALLTYPE
add_SizeChanged(
480 SizeChangedHandler
* handler
,
481 EventRegistrationToken
* pCookie
) {
482 // TODO(cpu): implement this.
486 virtual HRESULT STDMETHODCALLTYPE
remove_SizeChanged(
487 EventRegistrationToken cookie
) {
491 virtual HRESULT STDMETHODCALLTYPE
add_VisibilityChanged(
492 VisibilityChangedHandler
* handler
,
493 EventRegistrationToken
* pCookie
) {
497 virtual HRESULT STDMETHODCALLTYPE
remove_VisibilityChanged(
498 EventRegistrationToken cookie
) {
502 // ICoreWindowInterop implementation:
503 virtual HRESULT STDMETHODCALLTYPE
get_WindowHandle(HWND
* hwnd
) {
510 virtual HRESULT STDMETHODCALLTYPE
put_MessageHandled(
517 mswr::ComPtr
<winui::Core::ICoreDispatcher
> dispatcher_
;
521 : public mswr::RuntimeClass
<winapp::Activation::IActivatedEventArgs
> {
523 ActivatedEvent(winapp::Activation::ActivationKind activation_kind
)
524 : activation_kind_(activation_kind
) {
527 virtual HRESULT STDMETHODCALLTYPE
get_Kind(
528 winapp::Activation::ActivationKind
*value
) {
529 *value
= activation_kind_
;
533 virtual HRESULT STDMETHODCALLTYPE
get_PreviousExecutionState(
534 winapp::Activation::ApplicationExecutionState
*value
) {
535 *value
= winapp::Activation::ApplicationExecutionState_ClosedByUser
;
539 virtual HRESULT STDMETHODCALLTYPE
get_SplashScreen(
540 winapp::Activation::ISplashScreen
**value
) {
545 winapp::Activation::ActivationKind activation_kind_
;
548 class CoreApplicationViewEmulation
549 : public mswr::RuntimeClass
<winapp::Core::ICoreApplicationView
> {
551 CoreApplicationViewEmulation() {
552 core_window_
= mswr::Make
<CoreWindowEmulation
>();
556 if (activated_handler_
) {
557 auto ae
= mswr::Make
<ActivatedEvent
>(
558 winapp::Activation::ActivationKind_File
);
559 return activated_handler_
->Invoke(this, ae
.Get());
566 return core_window_
->Close();
569 // ICoreApplicationView implementation:
570 virtual HRESULT STDMETHODCALLTYPE
get_CoreWindow(
571 winui::Core::ICoreWindow
** value
) {
574 return core_window_
.CopyTo(value
);
577 virtual HRESULT STDMETHODCALLTYPE
add_Activated(
578 ActivatedHandler
* handler
,
579 EventRegistrationToken
* token
) {
580 // The real component supports multiple handles but we don't yet.
581 if (activated_handler_
)
583 activated_handler_
= handler
;
587 virtual HRESULT STDMETHODCALLTYPE
remove_Activated(
588 EventRegistrationToken token
) {
589 // Chrome never unregisters handlers, so we don't care about it.
593 virtual HRESULT STDMETHODCALLTYPE
get_IsMain(
598 virtual HRESULT STDMETHODCALLTYPE
get_IsHosted(
604 mswr::ComPtr
<CoreWindowEmulation
> core_window_
;
605 mswr::ComPtr
<ActivatedHandler
> activated_handler_
;
608 class CoreApplicationWin7Emulation
609 : public mswr::RuntimeClass
<winapp::Core::ICoreApplication
,
610 winapp::Core::ICoreApplicationExit
> {
612 // ICoreApplication implementation:
614 virtual HRESULT STDMETHODCALLTYPE
get_Id(
619 virtual HRESULT STDMETHODCALLTYPE
add_Suspending(
620 winfoundtn::IEventHandler
<winapp::SuspendingEventArgs
*>* handler
,
621 EventRegistrationToken
* token
) {
625 virtual HRESULT STDMETHODCALLTYPE
remove_Suspending(
626 EventRegistrationToken token
) {
630 virtual HRESULT STDMETHODCALLTYPE
add_Resuming(
631 winfoundtn::IEventHandler
<IInspectable
*>* handler
,
632 EventRegistrationToken
* token
) {
636 virtual HRESULT STDMETHODCALLTYPE
remove_Resuming(
637 EventRegistrationToken token
) {
641 virtual HRESULT STDMETHODCALLTYPE
get_Properties(
642 winfoundtn::Collections::IPropertySet
** value
) {
646 virtual HRESULT STDMETHODCALLTYPE
GetCurrentView(
647 winapp::Core::ICoreApplicationView
** value
) {
651 virtual HRESULT STDMETHODCALLTYPE
Run(
652 winapp::Core::IFrameworkViewSource
* viewSource
) {
653 HRESULT hr
= viewSource
->CreateView(app_view_
.GetAddressOf());
656 view_emulation_
= mswr::Make
<CoreApplicationViewEmulation
>();
657 hr
= app_view_
->Initialize(view_emulation_
.Get());
660 mswr::ComPtr
<winui::Core::ICoreWindow
> core_window
;
661 hr
= view_emulation_
->get_CoreWindow(core_window
.GetAddressOf());
664 hr
= app_view_
->SetWindow(core_window
.Get());
667 hr
= app_view_
->Load(NULL
);
670 hr
= view_emulation_
->Activate();
673 return app_view_
->Run();
676 virtual HRESULT STDMETHODCALLTYPE
RunWithActivationFactories(
677 winfoundtn::IGetActivationFactory
* activationFactoryCallback
) {
681 // ICoreApplicationExit implementation:
683 virtual HRESULT STDMETHODCALLTYPE
Exit(void) {
684 return view_emulation_
->Close();
687 virtual HRESULT STDMETHODCALLTYPE
add_Exiting(
688 winfoundtn::IEventHandler
<IInspectable
*>* handler
,
689 EventRegistrationToken
* token
) {
693 virtual HRESULT STDMETHODCALLTYPE
remove_Exiting(
694 EventRegistrationToken token
) {
699 mswr::ComPtr
<winapp::Core::IFrameworkView
> app_view_
;
700 mswr::ComPtr
<CoreApplicationViewEmulation
> view_emulation_
;
704 mswr::ComPtr
<winapp::Core::ICoreApplication
> InitWindows7() {
705 HRESULT hr
= ::CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
708 return mswr::Make
<CoreApplicationWin7Emulation
>();