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>
9 #include "base/logging.h"
10 #include "ui/gfx/geometry/safe_integer_conversions.h"
11 #include "ui/gfx/win/msg_util.h"
13 #pragma comment(lib, "shell32.lib")
15 EXTERN_C IMAGE_DOS_HEADER __ImageBase
;
16 // Even though we only create a single window, we need to keep this
17 // count because of the hidden window used by the UI message loop of
19 int g_window_count
= 0;
21 const wchar_t kAshWin7AppId
[] = L
"Google.Chrome.AshWin7.1";
23 extern float GetModernUIScale();
25 LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
26 WPARAM wparam
, LPARAM lparam
) {
34 hdc
= ::BeginPaint(hwnd
, &ps
);
35 ::EndPaint(hwnd
, &ps
);
38 ::DestroyWindow(hwnd
);
45 // Always allow Chrome to set the cursor.
49 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
54 HWND
CreateMetroTopLevelWindow(const RECT
& work_area
) {
55 HINSTANCE hInst
= reinterpret_cast<HINSTANCE
>(&__ImageBase
);
57 wcex
.cbSize
= sizeof(wcex
);
58 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
59 wcex
.lpfnWndProc
= WndProc
;
62 wcex
.hInstance
= hInst
;
63 wcex
.hIcon
= LoadIcon(::GetModuleHandle(NULL
), L
"IDR_MAINFRAME");
64 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
65 wcex
.hbrBackground
= (HBRUSH
)(COLOR_INACTIVECAPTION
+1);
66 wcex
.lpszMenuName
= 0;
67 wcex
.lpszClassName
= L
"Windows.UI.Core.CoreWindow";
68 wcex
.hIconSm
= LoadIcon(::GetModuleHandle(NULL
), L
"IDR_MAINFRAME");
72 HWND hwnd
= ::CreateWindowExW(0,
73 MAKEINTATOM(::RegisterClassExW(&wcex
)),
75 WS_POPUP
| WS_VISIBLE
| WS_MINIMIZEBOX
,
76 work_area
.top
, work_area
.left
,
77 work_area
.right
, work_area
.bottom
,
78 NULL
, NULL
, hInst
, NULL
);
82 typedef winfoundtn::ITypedEventHandler
<
83 winapp::Core::CoreApplicationView
*,
84 winapp::Activation::IActivatedEventArgs
*> ActivatedHandler
;
86 typedef winfoundtn::ITypedEventHandler
<
87 winui::Core::CoreWindow
*,
88 winui::Core::WindowActivatedEventArgs
*> WindowActivatedHandler
;
90 typedef winfoundtn::ITypedEventHandler
<
91 winui::Core::CoreWindow
*,
92 winui::Core::AutomationProviderRequestedEventArgs
*>
93 AutomationProviderHandler
;
95 typedef winfoundtn::ITypedEventHandler
<
96 winui::Core::CoreWindow
*,
97 winui::Core::CharacterReceivedEventArgs
*> CharEventHandler
;
99 typedef winfoundtn::ITypedEventHandler
<
100 winui::Core::CoreWindow
*,
101 winui::Core::CoreWindowEventArgs
*> CoreWindowEventHandler
;
103 typedef winfoundtn::ITypedEventHandler
<
104 winui::Core::CoreWindow
*,
105 winui::Core::InputEnabledEventArgs
*> InputEnabledEventHandler
;
107 typedef winfoundtn::ITypedEventHandler
<
108 winui::Core::CoreWindow
*,
109 winui::Core::KeyEventArgs
*> KeyEventHandler
;
111 typedef winfoundtn::ITypedEventHandler
<
112 winui::Core::CoreWindow
*,
113 winui::Core::PointerEventArgs
*> PointerEventHandler
;
115 typedef winfoundtn::ITypedEventHandler
<
116 winui::Core::CoreWindow
*,
117 winui::Core::WindowSizeChangedEventArgs
*> SizeChangedHandler
;
119 typedef winfoundtn::ITypedEventHandler
<
120 winui::Core::CoreWindow
*,
121 winui::Core::TouchHitTestingEventArgs
*> TouchHitTestHandler
;
123 typedef winfoundtn::ITypedEventHandler
<
124 winui::Core::CoreWindow
*,
125 winui::Core::VisibilityChangedEventArgs
*> VisibilityChangedHandler
;
127 typedef winfoundtn::ITypedEventHandler
<
128 winui::Core::CoreDispatcher
*,
129 winui::Core::AcceleratorKeyEventArgs
*> AcceleratorKeyEventHandler
;
131 // This interface is implemented by classes which handle mouse and keyboard
136 virtual ~InputHandler() {}
138 virtual bool HandleKeyboardMessage(const MSG
& msg
) = 0;
139 virtual bool HandleMouseMessage(const MSG
& msg
) = 0;
142 DISALLOW_COPY_AND_ASSIGN(InputHandler
);
145 // This class implements the winrt interfaces corresponding to mouse input.
146 class MouseEvent
: public mswr::RuntimeClass
<
147 winui::Core::IPointerEventArgs
,
148 winui::Input::IPointerPoint
,
149 winui::Input::IPointerPointProperties
,
150 windevs::Input::IPointerDevice
> {
152 MouseEvent(const MSG
& msg
)
156 // IPointerEventArgs implementation.
157 virtual HRESULT STDMETHODCALLTYPE
get_CurrentPoint(
158 winui::Input::IPointerPoint
** point
) {
159 return QueryInterface(winui::Input::IID_IPointerPoint
,
160 reinterpret_cast<void**>(point
));
163 virtual HRESULT STDMETHODCALLTYPE
get_KeyModifiers(
164 winsys::VirtualKeyModifiers
* modifiers
) {
168 virtual HRESULT STDMETHODCALLTYPE
GetIntermediatePoints(
169 winfoundtn::Collections::IVector
<winui::Input::PointerPoint
*>** points
) {
173 // IPointerPoint implementation.
174 virtual HRESULT STDMETHODCALLTYPE
get_PointerDevice(
175 windevs::Input::IPointerDevice
** pointer_device
) {
176 return QueryInterface(windevs::Input::IID_IPointerDevice
,
177 reinterpret_cast<void**>(pointer_device
));
180 virtual HRESULT STDMETHODCALLTYPE
get_Position(winfoundtn::Point
* position
) {
181 static float scale
= GetModernUIScale();
182 // Scale down the points here as they are scaled up on the other side.
183 position
->X
= gfx::ToRoundedInt(CR_GET_X_LPARAM(msg_
.lParam
) / scale
);
184 position
->Y
= gfx::ToRoundedInt(CR_GET_Y_LPARAM(msg_
.lParam
) / scale
);
188 virtual HRESULT STDMETHODCALLTYPE
get_PointerId(uint32
* pointer_id
) {
190 // Implement this properly.
195 virtual HRESULT STDMETHODCALLTYPE
get_Timestamp(uint64
* timestamp
) {
196 *timestamp
= msg_
.time
;
200 virtual HRESULT STDMETHODCALLTYPE
get_Properties(
201 winui::Input::IPointerPointProperties
** properties
) {
202 return QueryInterface(winui::Input::IID_IPointerPointProperties
,
203 reinterpret_cast<void**>(properties
));
206 virtual HRESULT STDMETHODCALLTYPE
get_RawPosition(
207 winfoundtn::Point
* position
) {
211 virtual HRESULT STDMETHODCALLTYPE
get_FrameId(uint32
* frame_id
) {
215 virtual HRESULT STDMETHODCALLTYPE
get_IsInContact(boolean
* in_contact
) {
219 // IPointerPointProperties implementation.
220 virtual HRESULT STDMETHODCALLTYPE
get_PointerUpdateKind(
221 winui::Input::PointerUpdateKind
* update_kind
) {
223 // There is no WM_POINTERUPDATE equivalent on Windows 7. Look into
225 if (msg_
.message
== WM_LBUTTONDOWN
) {
226 *update_kind
= winui::Input::PointerUpdateKind_LeftButtonPressed
;
227 } else if (msg_
.message
== WM_RBUTTONDOWN
) {
228 *update_kind
= winui::Input::PointerUpdateKind_RightButtonPressed
;
229 } else if (msg_
.message
== WM_MBUTTONDOWN
) {
230 *update_kind
= winui::Input::PointerUpdateKind_MiddleButtonPressed
;
231 } else if (msg_
.message
== WM_LBUTTONUP
) {
232 *update_kind
= winui::Input::PointerUpdateKind_LeftButtonReleased
;
233 } else if (msg_
.message
== WM_RBUTTONUP
) {
234 *update_kind
= winui::Input::PointerUpdateKind_RightButtonReleased
;
235 } else if (msg_
.message
== WM_MBUTTONUP
) {
236 *update_kind
= winui::Input::PointerUpdateKind_MiddleButtonReleased
;
241 virtual HRESULT STDMETHODCALLTYPE
get_IsLeftButtonPressed(
242 boolean
* left_button_pressed
) {
243 *left_button_pressed
= msg_
.wParam
& MK_LBUTTON
? true : false;
247 virtual HRESULT STDMETHODCALLTYPE
get_IsRightButtonPressed(
248 boolean
* right_button_pressed
) {
249 *right_button_pressed
= msg_
.wParam
& MK_RBUTTON
? true : false;
253 virtual HRESULT STDMETHODCALLTYPE
get_IsMiddleButtonPressed(
254 boolean
* middle_button_pressed
) {
255 *middle_button_pressed
= msg_
.wParam
& MK_MBUTTON
? true : false;
259 virtual HRESULT STDMETHODCALLTYPE
get_IsHorizontalMouseWheel(
260 boolean
* is_horizontal_mouse_wheel
) {
261 *is_horizontal_mouse_wheel
=
262 (msg_
.message
== WM_MOUSEHWHEEL
) ? true : false;
266 virtual HRESULT STDMETHODCALLTYPE
get_MouseWheelDelta(int* delta
) {
267 if (msg_
.message
== WM_MOUSEWHEEL
|| msg_
.message
== WM_MOUSEHWHEEL
) {
268 *delta
= GET_WHEEL_DELTA_WPARAM(msg_
.wParam
);
275 virtual HRESULT STDMETHODCALLTYPE
get_Pressure(float* pressure
) {
279 virtual HRESULT STDMETHODCALLTYPE
get_IsInverted(boolean
* inverted
) {
283 virtual HRESULT STDMETHODCALLTYPE
get_IsEraser(boolean
* is_eraser
) {
287 virtual HRESULT STDMETHODCALLTYPE
get_Orientation(float* orientation
) {
291 virtual HRESULT STDMETHODCALLTYPE
get_XTilt(float* x_tilt
) {
295 virtual HRESULT STDMETHODCALLTYPE
get_YTilt(float* y_tilt
) {
299 virtual HRESULT STDMETHODCALLTYPE
get_Twist(float* twist
) {
303 virtual HRESULT STDMETHODCALLTYPE
get_ContactRect(winfoundtn::Rect
* rect
) {
307 virtual HRESULT STDMETHODCALLTYPE
get_ContactRectRaw(winfoundtn::Rect
* rect
) {
311 virtual HRESULT STDMETHODCALLTYPE
get_TouchConfidence(boolean
* confidence
) {
315 virtual HRESULT STDMETHODCALLTYPE
get_IsPrimary(boolean
* is_primary
) {
319 virtual HRESULT STDMETHODCALLTYPE
get_IsInRange(boolean
* is_in_range
) {
323 virtual HRESULT STDMETHODCALLTYPE
get_IsCanceled(boolean
* is_canceled
) {
327 virtual HRESULT STDMETHODCALLTYPE
get_IsBarrelButtonPressed(
328 boolean
* is_barrel_button_pressed
) {
332 virtual HRESULT STDMETHODCALLTYPE
get_IsXButton1Pressed(
333 boolean
* is_xbutton1_pressed
) {
337 virtual HRESULT STDMETHODCALLTYPE
get_IsXButton2Pressed(
338 boolean
* is_xbutton2_pressed
) {
342 virtual HRESULT STDMETHODCALLTYPE
HasUsage(uint32 usage_page
,
344 boolean
* has_usage
) {
348 virtual HRESULT STDMETHODCALLTYPE
GetUsageValue(uint32 usage_page
,
350 int32
* usage_value
) {
354 // IPointerDevice implementation.
355 virtual HRESULT STDMETHODCALLTYPE
get_PointerDeviceType(
356 windevs::Input::PointerDeviceType
* device_type
) {
357 if (msg_
.message
== WM_TOUCH
) {
358 *device_type
= windevs::Input::PointerDeviceType_Touch
;
360 *device_type
= windevs::Input::PointerDeviceType_Mouse
;
365 virtual HRESULT STDMETHODCALLTYPE
get_IsIntegrated(boolean
* is_integrated
) {
369 virtual HRESULT STDMETHODCALLTYPE
get_MaxContacts(uint32
* contacts
) {
373 virtual HRESULT STDMETHODCALLTYPE
get_PhysicalDeviceRect(
374 winfoundtn::Rect
* rect
) {
378 virtual HRESULT STDMETHODCALLTYPE
get_ScreenRect(winfoundtn::Rect
* rect
) {
382 virtual HRESULT STDMETHODCALLTYPE
get_SupportedUsages(
383 winfoundtn::Collections::IVectorView
<
384 windevs::Input::PointerDeviceUsage
>** usages
) {
391 DISALLOW_COPY_AND_ASSIGN(MouseEvent
);
394 // This class implements the winrt interfaces needed to support keyboard
395 // character and system character messages.
396 class KeyEvent
: public mswr::RuntimeClass
<
397 winui::Core::IKeyEventArgs
,
398 winui::Core::ICharacterReceivedEventArgs
,
399 winui::Core::IAcceleratorKeyEventArgs
> {
401 KeyEvent(const MSG
& msg
)
404 // IKeyEventArgs implementation.
405 virtual HRESULT STDMETHODCALLTYPE
get_VirtualKey(
406 winsys::VirtualKey
* virtual_key
) {
407 *virtual_key
= static_cast<winsys::VirtualKey
>(msg_
.wParam
);
411 virtual HRESULT STDMETHODCALLTYPE
get_KeyStatus(
412 winui::Core::CorePhysicalKeyStatus
* key_status
) {
413 // As per msdn documentation for the keyboard messages.
414 key_status
->RepeatCount
= msg_
.lParam
& 0x0000FFFF;
415 key_status
->ScanCode
= (msg_
.lParam
>> 16) & 0x00FF;
416 key_status
->IsExtendedKey
= (msg_
.lParam
& (1 << 24));
417 key_status
->IsMenuKeyDown
= (msg_
.lParam
& (1 << 29));
418 key_status
->WasKeyDown
= (msg_
.lParam
& (1 << 30));
419 key_status
->IsKeyReleased
= (msg_
.lParam
& (1 << 31));
423 // ICharacterReceivedEventArgs implementation.
424 virtual HRESULT STDMETHODCALLTYPE
get_KeyCode(uint32
* key_code
) {
425 *key_code
= msg_
.wParam
;
429 // IAcceleratorKeyEventArgs implementation.
430 virtual HRESULT STDMETHODCALLTYPE
get_EventType(
431 winui::Core::CoreAcceleratorKeyEventType
* event_type
) {
432 if (msg_
.message
== WM_SYSKEYDOWN
) {
433 *event_type
= winui::Core::CoreAcceleratorKeyEventType_SystemKeyDown
;
434 } else if (msg_
.message
== WM_SYSKEYUP
) {
435 *event_type
= winui::Core::CoreAcceleratorKeyEventType_SystemKeyUp
;
436 } else if (msg_
.message
== WM_SYSCHAR
) {
437 *event_type
= winui::Core::CoreAcceleratorKeyEventType_SystemCharacter
;
446 // The following classes are the emulation of the WinRT system as exposed
447 // to metro applications. There is one application (ICoreApplication) which
448 // contains a series of Views (ICoreApplicationView) each one of them
449 // containing a CoreWindow which represents a surface that can drawn to
450 // and that receives events.
452 // Here is the general dependency hierachy in terms of interfaces:
454 // IFrameworkViewSource --> IFrameworkView
457 // ---------------------------------------------------------------------
460 // ICoreApplication ICoreApplicationView
463 // ICoreWindow -----> ICoreWindowInterop
467 // ICoreDispatcher <==> real HWND
469 class CoreDispatcherEmulation
:
470 public mswr::RuntimeClass
<
471 winui::Core::ICoreDispatcher
,
472 winui::Core::ICoreAcceleratorKeys
> {
474 CoreDispatcherEmulation(InputHandler
* input_handler
)
475 : input_handler_(input_handler
),
476 accelerator_key_event_handler_(NULL
) {}
478 // ICoreDispatcher implementation:
479 virtual HRESULT STDMETHODCALLTYPE
get_HasThreadAccess(boolean
* value
) {
483 virtual HRESULT STDMETHODCALLTYPE
ProcessEvents(
484 winui::Core::CoreProcessEventsOption options
) {
485 // We don't support the other message pump modes. So we basically enter a
486 // traditional message loop that we only exit a teardown.
487 if (options
!= winui::Core::CoreProcessEventsOption_ProcessUntilQuit
)
491 while((::GetMessage(&msg
, NULL
, 0, 0) != 0) && g_window_count
> 0) {
492 ProcessInputMessage(msg
);
493 ::TranslateMessage(&msg
);
494 ::DispatchMessage(&msg
);
496 // TODO(cpu): figure what to do with msg.WParam which we would normally
501 virtual HRESULT STDMETHODCALLTYPE
RunAsync(
502 winui::Core::CoreDispatcherPriority priority
,
503 winui::Core::IDispatchedHandler
*agileCallback
,
504 ABI::Windows::Foundation::IAsyncAction
** asyncAction
) {
508 virtual HRESULT STDMETHODCALLTYPE
RunIdleAsync(
509 winui::Core::IIdleDispatchedHandler
*agileCallback
,
510 winfoundtn::IAsyncAction
** asyncAction
) {
514 // ICoreAcceleratorKeys implementation:
515 virtual HRESULT STDMETHODCALLTYPE
add_AcceleratorKeyActivated(
516 AcceleratorKeyEventHandler
* handler
,
517 EventRegistrationToken
*pCookie
) {
518 accelerator_key_event_handler_
= handler
;
519 accelerator_key_event_handler_
->AddRef();
523 virtual HRESULT STDMETHODCALLTYPE
remove_AcceleratorKeyActivated(
524 EventRegistrationToken cookie
) {
525 accelerator_key_event_handler_
->Release();
526 accelerator_key_event_handler_
= NULL
;
531 bool ProcessInputMessage(const MSG
& msg
) {
532 // Poor man's way of dispatching input events.
534 if (input_handler_
) {
535 if ((msg
.message
>= WM_KEYFIRST
) && (msg
.message
<= WM_KEYLAST
)) {
536 if ((msg
.message
== WM_SYSKEYDOWN
) || (msg
.message
== WM_SYSKEYUP
) ||
537 msg
.message
== WM_SYSCHAR
) {
538 ret
= HandleSystemKeys(msg
);
540 ret
= input_handler_
->HandleKeyboardMessage(msg
);
542 } else if ((msg
.message
>= WM_MOUSEFIRST
) &&
543 (msg
.message
<= WM_MOUSELAST
)) {
544 ret
= input_handler_
->HandleMouseMessage(msg
);
550 bool HandleSystemKeys(const MSG
& msg
) {
551 mswr::ComPtr
<winui::Core::IAcceleratorKeyEventArgs
> event_args
;
552 event_args
= mswr::Make
<KeyEvent
>(msg
);
553 accelerator_key_event_handler_
->Invoke(this, event_args
.Get());
557 InputHandler
* input_handler_
;
558 AcceleratorKeyEventHandler
* accelerator_key_event_handler_
;
561 class CoreWindowEmulation
562 : public mswr::RuntimeClass
<
563 mswr::RuntimeClassFlags
<mswr::WinRtClassicComMix
>,
564 winui::Core::ICoreWindow
, ICoreWindowInterop
>,
565 public InputHandler
{
567 CoreWindowEmulation()
569 mouse_moved_handler_(NULL
),
570 mouse_capture_lost_handler_(NULL
),
571 mouse_pressed_handler_(NULL
),
572 mouse_released_handler_(NULL
),
573 mouse_entered_handler_(NULL
),
574 mouse_exited_handler_(NULL
),
575 mouse_wheel_changed_handler_(NULL
),
576 key_down_handler_(NULL
),
577 key_up_handler_(NULL
),
578 character_received_handler_(NULL
) {
579 dispatcher_
= mswr::Make
<CoreDispatcherEmulation
>(this);
581 // Unless we select our own AppUserModelID the shell might confuse us
582 // with the app launcher one and we get the wrong taskbar button and icon.
583 ::SetCurrentProcessExplicitAppUserModelID(kAshWin7AppId
);
585 RECT work_area
= {0};
586 ::SystemParametersInfo(SPI_GETWORKAREA
, 0, &work_area
, 0);
587 if (::IsDebuggerPresent()) {
590 work_area
.right
= 1600;
591 work_area
.bottom
= 900;
594 core_hwnd_
= CreateMetroTopLevelWindow(work_area
);
597 ~CoreWindowEmulation() {
599 ::DestroyWindow(core_hwnd_
);
602 // ICoreWindow implementation:
603 virtual HRESULT STDMETHODCALLTYPE
get_AutomationHostProvider(
604 IInspectable
** value
) {
608 virtual HRESULT STDMETHODCALLTYPE
get_Bounds(
609 winfoundtn::Rect
* value
) {
611 if (!::GetClientRect(core_hwnd_
, &rect
))
613 value
->Width
= rect
.right
;
614 value
->Height
= rect
.bottom
;
618 virtual HRESULT STDMETHODCALLTYPE
get_CustomProperties(
619 winfoundtn::Collections::IPropertySet
** value
) {
623 virtual HRESULT STDMETHODCALLTYPE
get_Dispatcher(
624 winui::Core::ICoreDispatcher
** value
) {
625 return dispatcher_
.CopyTo(value
);
628 virtual HRESULT STDMETHODCALLTYPE
get_FlowDirection(
629 winui::Core::CoreWindowFlowDirection
* value
) {
633 virtual HRESULT STDMETHODCALLTYPE
put_FlowDirection(
634 winui::Core::CoreWindowFlowDirection value
) {
638 virtual HRESULT STDMETHODCALLTYPE
get_IsInputEnabled(
643 virtual HRESULT STDMETHODCALLTYPE
put_IsInputEnabled(
648 virtual HRESULT STDMETHODCALLTYPE
get_PointerCursor(
649 winui::Core::ICoreCursor
** value
) {
653 virtual HRESULT STDMETHODCALLTYPE
put_PointerCursor(
654 winui::Core::ICoreCursor
* value
) {
658 virtual HRESULT STDMETHODCALLTYPE
get_PointerPosition(
659 winfoundtn::Point
* value
) {
663 virtual HRESULT STDMETHODCALLTYPE
get_Visible(
668 virtual HRESULT STDMETHODCALLTYPE
Activate(void) {
669 // After we fire OnActivate on the View, Chrome calls us back here.
673 virtual HRESULT STDMETHODCALLTYPE
Close(void) {
674 ::PostMessage(core_hwnd_
, WM_CLOSE
, 0, 0);
679 virtual HRESULT STDMETHODCALLTYPE
GetAsyncKeyState(
680 ABI::Windows::System::VirtualKey virtualKey
,
681 winui::Core::CoreVirtualKeyStates
* KeyState
) {
685 virtual HRESULT STDMETHODCALLTYPE
GetKeyState(
686 ABI::Windows::System::VirtualKey virtualKey
,
687 winui::Core::CoreVirtualKeyStates
* KeyState
) {
691 virtual HRESULT STDMETHODCALLTYPE
ReleasePointerCapture(void) {
695 virtual HRESULT STDMETHODCALLTYPE
SetPointerCapture(void) {
699 virtual HRESULT STDMETHODCALLTYPE
add_Activated(
700 WindowActivatedHandler
* handler
,
701 EventRegistrationToken
* pCookie
) {
702 // TODO(cpu) implement this.
706 virtual HRESULT STDMETHODCALLTYPE
remove_Activated(
707 EventRegistrationToken cookie
) {
711 virtual HRESULT STDMETHODCALLTYPE
add_AutomationProviderRequested(
712 AutomationProviderHandler
* handler
,
713 EventRegistrationToken
* cookie
) {
717 virtual HRESULT STDMETHODCALLTYPE
remove_AutomationProviderRequested(
718 EventRegistrationToken cookie
) {
722 virtual HRESULT STDMETHODCALLTYPE
add_CharacterReceived(
723 CharEventHandler
* handler
,
724 EventRegistrationToken
* pCookie
) {
725 character_received_handler_
= handler
;
726 character_received_handler_
->AddRef();
730 virtual HRESULT STDMETHODCALLTYPE
remove_CharacterReceived(
731 EventRegistrationToken cookie
) {
732 character_received_handler_
->Release();
733 character_received_handler_
= NULL
;
737 virtual HRESULT STDMETHODCALLTYPE
add_Closed(
738 CoreWindowEventHandler
* handler
,
739 EventRegistrationToken
* pCookie
) {
743 virtual HRESULT STDMETHODCALLTYPE
remove_Closed(
744 EventRegistrationToken cookie
) {
748 virtual HRESULT STDMETHODCALLTYPE
add_InputEnabled(
749 InputEnabledEventHandler
* handler
,
750 EventRegistrationToken
* pCookie
) {
754 virtual HRESULT STDMETHODCALLTYPE
remove_InputEnabled(
755 EventRegistrationToken cookie
) {
759 virtual HRESULT STDMETHODCALLTYPE
add_KeyDown(
760 KeyEventHandler
* handler
,
761 EventRegistrationToken
* pCookie
) {
762 key_down_handler_
= handler
;
763 key_down_handler_
->AddRef();
767 virtual HRESULT STDMETHODCALLTYPE
remove_KeyDown(
768 EventRegistrationToken cookie
) {
769 key_down_handler_
->Release();
770 key_down_handler_
= NULL
;
774 virtual HRESULT STDMETHODCALLTYPE
add_KeyUp(
775 KeyEventHandler
* handler
,
776 EventRegistrationToken
* pCookie
) {
777 key_up_handler_
= handler
;
778 key_up_handler_
->AddRef();
782 virtual HRESULT STDMETHODCALLTYPE
remove_KeyUp(
783 EventRegistrationToken cookie
) {
784 key_up_handler_
->Release();
785 key_up_handler_
= NULL
;
789 virtual HRESULT STDMETHODCALLTYPE
add_PointerCaptureLost(
790 PointerEventHandler
* handler
,
791 EventRegistrationToken
* cookie
) {
792 mouse_capture_lost_handler_
= handler
;
796 virtual HRESULT STDMETHODCALLTYPE
remove_PointerCaptureLost(
797 EventRegistrationToken cookie
) {
798 mouse_capture_lost_handler_
= NULL
;
802 virtual HRESULT STDMETHODCALLTYPE
add_PointerEntered(
803 PointerEventHandler
* handler
,
804 EventRegistrationToken
* cookie
) {
805 mouse_entered_handler_
= handler
;
809 virtual HRESULT STDMETHODCALLTYPE
remove_PointerEntered(
810 EventRegistrationToken cookie
) {
811 mouse_entered_handler_
= NULL
;
815 virtual HRESULT STDMETHODCALLTYPE
add_PointerExited(
816 PointerEventHandler
* handler
,
817 EventRegistrationToken
* cookie
) {
818 mouse_exited_handler_
= handler
;
822 virtual HRESULT STDMETHODCALLTYPE
remove_PointerExited(
823 EventRegistrationToken cookie
) {
824 mouse_exited_handler_
= NULL
;
828 virtual HRESULT STDMETHODCALLTYPE
add_PointerMoved(
829 PointerEventHandler
* handler
,
830 EventRegistrationToken
* cookie
) {
831 mouse_moved_handler_
= handler
;
832 mouse_moved_handler_
->AddRef();
836 virtual HRESULT STDMETHODCALLTYPE
remove_PointerMoved(
837 EventRegistrationToken cookie
) {
838 mouse_moved_handler_
->Release();
839 mouse_moved_handler_
= NULL
;
843 virtual HRESULT STDMETHODCALLTYPE
add_PointerPressed(
844 PointerEventHandler
* handler
,
845 EventRegistrationToken
* cookie
) {
846 mouse_pressed_handler_
= handler
;
847 mouse_pressed_handler_
->AddRef();
851 virtual HRESULT STDMETHODCALLTYPE
remove_PointerPressed(
852 EventRegistrationToken cookie
) {
853 mouse_pressed_handler_
->Release();
854 mouse_pressed_handler_
= NULL
;
858 virtual HRESULT STDMETHODCALLTYPE
add_PointerReleased(
859 PointerEventHandler
* handler
,
860 EventRegistrationToken
* cookie
) {
861 mouse_released_handler_
= handler
;
862 mouse_released_handler_
->AddRef();
866 virtual HRESULT STDMETHODCALLTYPE
remove_PointerReleased(
867 EventRegistrationToken cookie
) {
868 mouse_released_handler_
->Release();
869 mouse_released_handler_
= NULL
;
873 virtual HRESULT STDMETHODCALLTYPE
add_TouchHitTesting(
874 TouchHitTestHandler
* handler
,
875 EventRegistrationToken
* pCookie
) {
879 virtual HRESULT STDMETHODCALLTYPE
remove_TouchHitTesting(
880 EventRegistrationToken cookie
) {
884 virtual HRESULT STDMETHODCALLTYPE
add_PointerWheelChanged(
885 PointerEventHandler
* handler
,
886 EventRegistrationToken
* cookie
) {
887 mouse_wheel_changed_handler_
= handler
;
888 mouse_wheel_changed_handler_
->AddRef();
892 virtual HRESULT STDMETHODCALLTYPE
remove_PointerWheelChanged(
893 EventRegistrationToken cookie
) {
894 mouse_wheel_changed_handler_
->Release();
895 mouse_wheel_changed_handler_
= NULL
;
899 virtual HRESULT STDMETHODCALLTYPE
add_SizeChanged(
900 SizeChangedHandler
* handler
,
901 EventRegistrationToken
* pCookie
) {
902 // TODO(cpu): implement this.
906 virtual HRESULT STDMETHODCALLTYPE
remove_SizeChanged(
907 EventRegistrationToken cookie
) {
911 virtual HRESULT STDMETHODCALLTYPE
add_VisibilityChanged(
912 VisibilityChangedHandler
* handler
,
913 EventRegistrationToken
* pCookie
) {
917 virtual HRESULT STDMETHODCALLTYPE
remove_VisibilityChanged(
918 EventRegistrationToken cookie
) {
922 // ICoreWindowInterop implementation:
923 virtual HRESULT STDMETHODCALLTYPE
get_WindowHandle(HWND
* hwnd
) {
930 virtual HRESULT STDMETHODCALLTYPE
put_MessageHandled(
936 virtual bool HandleKeyboardMessage(const MSG
& msg
) OVERRIDE
{
937 switch (msg
.message
) {
940 mswr::ComPtr
<winui::Core::IKeyEventArgs
> event_args
;
941 event_args
= mswr::Make
<KeyEvent
>(msg
);
942 KeyEventHandler
* handler
= NULL
;
943 if (msg
.message
== WM_KEYDOWN
) {
944 handler
= key_down_handler_
;
946 handler
= key_up_handler_
;
948 handler
->Invoke(this, event_args
.Get());
955 mswr::ComPtr
<winui::Core::ICharacterReceivedEventArgs
> event_args
;
956 event_args
= mswr::Make
<KeyEvent
>(msg
);
957 character_received_handler_
->Invoke(this, event_args
.Get());
967 virtual bool HandleMouseMessage(const MSG
& msg
) OVERRIDE
{
968 PointerEventHandler
* handler
= NULL
;
969 mswr::ComPtr
<winui::Core::IPointerEventArgs
> event_args
;
970 event_args
= mswr::Make
<MouseEvent
>(msg
);
971 switch (msg
.message
) {
973 handler
= mouse_moved_handler_
;
976 case WM_LBUTTONDOWN
: {
979 handler
= mouse_pressed_handler_
;
986 handler
= mouse_released_handler_
;
990 case WM_MOUSEWHEEL
: {
992 handler
= mouse_wheel_changed_handler_
;
1000 handler
->Invoke(this, event_args
.Get());
1005 PointerEventHandler
* mouse_moved_handler_
;
1006 PointerEventHandler
* mouse_capture_lost_handler_
;
1007 PointerEventHandler
* mouse_pressed_handler_
;
1008 PointerEventHandler
* mouse_released_handler_
;
1009 PointerEventHandler
* mouse_entered_handler_
;
1010 PointerEventHandler
* mouse_exited_handler_
;
1011 PointerEventHandler
* mouse_wheel_changed_handler_
;
1012 KeyEventHandler
* key_down_handler_
;
1013 KeyEventHandler
* key_up_handler_
;
1014 CharEventHandler
* character_received_handler_
;
1016 mswr::ComPtr
<winui::Core::ICoreDispatcher
> dispatcher_
;
1019 class ActivatedEvent
1020 : public mswr::RuntimeClass
<winapp::Activation::IActivatedEventArgs
> {
1022 ActivatedEvent(winapp::Activation::ActivationKind activation_kind
)
1023 : activation_kind_(activation_kind
) {
1026 virtual HRESULT STDMETHODCALLTYPE
get_Kind(
1027 winapp::Activation::ActivationKind
*value
) {
1028 *value
= activation_kind_
;
1032 virtual HRESULT STDMETHODCALLTYPE
get_PreviousExecutionState(
1033 winapp::Activation::ApplicationExecutionState
*value
) {
1034 *value
= winapp::Activation::ApplicationExecutionState_ClosedByUser
;
1038 virtual HRESULT STDMETHODCALLTYPE
get_SplashScreen(
1039 winapp::Activation::ISplashScreen
**value
) {
1044 winapp::Activation::ActivationKind activation_kind_
;
1047 class CoreApplicationViewEmulation
1048 : public mswr::RuntimeClass
<winapp::Core::ICoreApplicationView
> {
1050 CoreApplicationViewEmulation() {
1051 core_window_
= mswr::Make
<CoreWindowEmulation
>();
1054 HRESULT
Activate() {
1055 if (activated_handler_
) {
1056 auto ae
= mswr::Make
<ActivatedEvent
>(
1057 winapp::Activation::ActivationKind_File
);
1058 return activated_handler_
->Invoke(this, ae
.Get());
1065 return core_window_
->Close();
1068 // ICoreApplicationView implementation:
1069 virtual HRESULT STDMETHODCALLTYPE
get_CoreWindow(
1070 winui::Core::ICoreWindow
** value
) {
1073 return core_window_
.CopyTo(value
);
1076 virtual HRESULT STDMETHODCALLTYPE
add_Activated(
1077 ActivatedHandler
* handler
,
1078 EventRegistrationToken
* token
) {
1079 // The real component supports multiple handles but we don't yet.
1080 if (activated_handler_
)
1082 activated_handler_
= handler
;
1086 virtual HRESULT STDMETHODCALLTYPE
remove_Activated(
1087 EventRegistrationToken token
) {
1088 // Chrome never unregisters handlers, so we don't care about it.
1092 virtual HRESULT STDMETHODCALLTYPE
get_IsMain(
1097 virtual HRESULT STDMETHODCALLTYPE
get_IsHosted(
1103 mswr::ComPtr
<CoreWindowEmulation
> core_window_
;
1104 mswr::ComPtr
<ActivatedHandler
> activated_handler_
;
1107 class CoreApplicationWin7Emulation
1108 : public mswr::RuntimeClass
<winapp::Core::ICoreApplication
,
1109 winapp::Core::ICoreApplicationExit
> {
1111 // ICoreApplication implementation:
1113 virtual HRESULT STDMETHODCALLTYPE
get_Id(
1118 virtual HRESULT STDMETHODCALLTYPE
add_Suspending(
1119 winfoundtn::IEventHandler
<winapp::SuspendingEventArgs
*>* handler
,
1120 EventRegistrationToken
* token
) {
1124 virtual HRESULT STDMETHODCALLTYPE
remove_Suspending(
1125 EventRegistrationToken token
) {
1129 virtual HRESULT STDMETHODCALLTYPE
add_Resuming(
1130 winfoundtn::IEventHandler
<IInspectable
*>* handler
,
1131 EventRegistrationToken
* token
) {
1135 virtual HRESULT STDMETHODCALLTYPE
remove_Resuming(
1136 EventRegistrationToken token
) {
1140 virtual HRESULT STDMETHODCALLTYPE
get_Properties(
1141 winfoundtn::Collections::IPropertySet
** value
) {
1145 virtual HRESULT STDMETHODCALLTYPE
GetCurrentView(
1146 winapp::Core::ICoreApplicationView
** value
) {
1150 virtual HRESULT STDMETHODCALLTYPE
Run(
1151 winapp::Core::IFrameworkViewSource
* viewSource
) {
1152 HRESULT hr
= viewSource
->CreateView(app_view_
.GetAddressOf());
1155 view_emulation_
= mswr::Make
<CoreApplicationViewEmulation
>();
1156 hr
= app_view_
->Initialize(view_emulation_
.Get());
1159 mswr::ComPtr
<winui::Core::ICoreWindow
> core_window
;
1160 hr
= view_emulation_
->get_CoreWindow(core_window
.GetAddressOf());
1163 hr
= app_view_
->SetWindow(core_window
.Get());
1166 hr
= app_view_
->Load(NULL
);
1169 hr
= view_emulation_
->Activate();
1172 return app_view_
->Run();
1175 virtual HRESULT STDMETHODCALLTYPE
RunWithActivationFactories(
1176 winfoundtn::IGetActivationFactory
* activationFactoryCallback
) {
1180 // ICoreApplicationExit implementation:
1182 virtual HRESULT STDMETHODCALLTYPE
Exit(void) {
1183 return view_emulation_
->Close();
1186 virtual HRESULT STDMETHODCALLTYPE
add_Exiting(
1187 winfoundtn::IEventHandler
<IInspectable
*>* handler
,
1188 EventRegistrationToken
* token
) {
1192 virtual HRESULT STDMETHODCALLTYPE
remove_Exiting(
1193 EventRegistrationToken token
) {
1198 mswr::ComPtr
<winapp::Core::IFrameworkView
> app_view_
;
1199 mswr::ComPtr
<CoreApplicationViewEmulation
> view_emulation_
;
1203 mswr::ComPtr
<winapp::Core::ICoreApplication
> InitWindows7() {
1204 HRESULT hr
= ::CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1207 return mswr::Make
<CoreApplicationWin7Emulation
>();