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"
9 #include "ui/gfx/geometry/safe_integer_conversions.h"
10 #include "ui/gfx/win/msg_util.h"
12 EXTERN_C IMAGE_DOS_HEADER __ImageBase
;
13 int g_window_count
= 0;
15 extern float GetModernUIScale();
17 LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
18 WPARAM wparam
, LPARAM lparam
) {
26 hdc
= ::BeginPaint(hwnd
, &ps
);
27 ::EndPaint(hwnd
, &ps
);
30 ::DestroyWindow(hwnd
);
37 // Always allow Chrome to set the cursor.
41 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
46 HWND
CreateMetroTopLevelWindow() {
47 HINSTANCE hInst
= reinterpret_cast<HINSTANCE
>(&__ImageBase
);
49 wcex
.cbSize
= sizeof(wcex
);
50 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
51 wcex
.lpfnWndProc
= WndProc
;
54 wcex
.hInstance
= hInst
;
56 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
57 wcex
.hbrBackground
= (HBRUSH
)(COLOR_INACTIVECAPTION
+1);
58 wcex
.lpszMenuName
= 0;
59 wcex
.lpszClassName
= L
"Windows.UI.Core.CoreWindow";
62 HWND hwnd
= ::CreateWindowExW(0,
63 MAKEINTATOM(::RegisterClassExW(&wcex
)),
65 WS_POPUP
| WS_VISIBLE
,
67 NULL
, NULL
, hInst
, NULL
);
71 typedef winfoundtn::ITypedEventHandler
<
72 winapp::Core::CoreApplicationView
*,
73 winapp::Activation::IActivatedEventArgs
*> ActivatedHandler
;
75 typedef winfoundtn::ITypedEventHandler
<
76 winui::Core::CoreWindow
*,
77 winui::Core::WindowActivatedEventArgs
*> WindowActivatedHandler
;
79 typedef winfoundtn::ITypedEventHandler
<
80 winui::Core::CoreWindow
*,
81 winui::Core::AutomationProviderRequestedEventArgs
*>
82 AutomationProviderHandler
;
84 typedef winfoundtn::ITypedEventHandler
<
85 winui::Core::CoreWindow
*,
86 winui::Core::CharacterReceivedEventArgs
*> CharEventHandler
;
88 typedef winfoundtn::ITypedEventHandler
<
89 winui::Core::CoreWindow
*,
90 winui::Core::CoreWindowEventArgs
*> CoreWindowEventHandler
;
92 typedef winfoundtn::ITypedEventHandler
<
93 winui::Core::CoreWindow
*,
94 winui::Core::InputEnabledEventArgs
*> InputEnabledEventHandler
;
96 typedef winfoundtn::ITypedEventHandler
<
97 winui::Core::CoreWindow
*,
98 winui::Core::KeyEventArgs
*> KeyEventHandler
;
100 typedef winfoundtn::ITypedEventHandler
<
101 winui::Core::CoreWindow
*,
102 winui::Core::PointerEventArgs
*> PointerEventHandler
;
104 typedef winfoundtn::ITypedEventHandler
<
105 winui::Core::CoreWindow
*,
106 winui::Core::WindowSizeChangedEventArgs
*> SizeChangedHandler
;
108 typedef winfoundtn::ITypedEventHandler
<
109 winui::Core::CoreWindow
*,
110 winui::Core::TouchHitTestingEventArgs
*> TouchHitTestHandler
;
112 typedef winfoundtn::ITypedEventHandler
<
113 winui::Core::CoreWindow
*,
114 winui::Core::VisibilityChangedEventArgs
*> VisibilityChangedHandler
;
116 typedef winfoundtn::ITypedEventHandler
<
117 winui::Core::CoreDispatcher
*,
118 winui::Core::AcceleratorKeyEventArgs
*> AcceleratorKeyEventHandler
;
120 // This interface is implemented by classes which handle mouse and keyboard
125 virtual ~InputHandler() {}
127 virtual bool HandleKeyboardMessage(const MSG
& msg
) = 0;
128 virtual bool HandleMouseMessage(const MSG
& msg
) = 0;
131 DISALLOW_COPY_AND_ASSIGN(InputHandler
);
134 // This class implements the winrt interfaces corresponding to mouse input.
135 class MouseEvent
: public mswr::RuntimeClass
<
136 winui::Core::IPointerEventArgs
,
137 winui::Input::IPointerPoint
,
138 winui::Input::IPointerPointProperties
,
139 windevs::Input::IPointerDevice
> {
141 MouseEvent(const MSG
& msg
)
145 // IPointerEventArgs implementation.
146 virtual HRESULT STDMETHODCALLTYPE
get_CurrentPoint(
147 winui::Input::IPointerPoint
** point
) {
148 return QueryInterface(winui::Input::IID_IPointerPoint
,
149 reinterpret_cast<void**>(point
));
152 virtual HRESULT STDMETHODCALLTYPE
get_KeyModifiers(
153 winsys::VirtualKeyModifiers
* modifiers
) {
157 virtual HRESULT STDMETHODCALLTYPE
GetIntermediatePoints(
158 winfoundtn::Collections::IVector
<winui::Input::PointerPoint
*>** points
) {
162 // IPointerPoint implementation.
163 virtual HRESULT STDMETHODCALLTYPE
get_PointerDevice(
164 windevs::Input::IPointerDevice
** pointer_device
) {
165 return QueryInterface(windevs::Input::IID_IPointerDevice
,
166 reinterpret_cast<void**>(pointer_device
));
169 virtual HRESULT STDMETHODCALLTYPE
get_Position(winfoundtn::Point
* position
) {
170 static float scale
= GetModernUIScale();
171 // Scale down the points here as they are scaled up on the other side.
172 position
->X
= gfx::ToRoundedInt(CR_GET_X_LPARAM(msg_
.lParam
) / scale
);
173 position
->Y
= gfx::ToRoundedInt(CR_GET_Y_LPARAM(msg_
.lParam
) / scale
);
177 virtual HRESULT STDMETHODCALLTYPE
get_PointerId(uint32
* pointer_id
) {
179 // Implement this properly.
184 virtual HRESULT STDMETHODCALLTYPE
get_Timestamp(uint64
* timestamp
) {
185 *timestamp
= msg_
.time
;
189 virtual HRESULT STDMETHODCALLTYPE
get_Properties(
190 winui::Input::IPointerPointProperties
** properties
) {
191 return QueryInterface(winui::Input::IID_IPointerPointProperties
,
192 reinterpret_cast<void**>(properties
));
195 virtual HRESULT STDMETHODCALLTYPE
get_RawPosition(
196 winfoundtn::Point
* position
) {
200 virtual HRESULT STDMETHODCALLTYPE
get_FrameId(uint32
* frame_id
) {
204 virtual HRESULT STDMETHODCALLTYPE
get_IsInContact(boolean
* in_contact
) {
208 // IPointerPointProperties implementation.
209 virtual HRESULT STDMETHODCALLTYPE
get_PointerUpdateKind(
210 winui::Input::PointerUpdateKind
* update_kind
) {
212 // There is no WM_POINTERUPDATE equivalent on Windows 7. Look into
214 if (msg_
.message
== WM_LBUTTONDOWN
) {
215 *update_kind
= winui::Input::PointerUpdateKind_LeftButtonPressed
;
216 } else if (msg_
.message
== WM_RBUTTONDOWN
) {
217 *update_kind
= winui::Input::PointerUpdateKind_RightButtonPressed
;
218 } else if (msg_
.message
== WM_MBUTTONDOWN
) {
219 *update_kind
= winui::Input::PointerUpdateKind_MiddleButtonPressed
;
220 } else if (msg_
.message
== WM_LBUTTONUP
) {
221 *update_kind
= winui::Input::PointerUpdateKind_LeftButtonReleased
;
222 } else if (msg_
.message
== WM_RBUTTONUP
) {
223 *update_kind
= winui::Input::PointerUpdateKind_RightButtonReleased
;
224 } else if (msg_
.message
== WM_MBUTTONUP
) {
225 *update_kind
= winui::Input::PointerUpdateKind_MiddleButtonReleased
;
230 virtual HRESULT STDMETHODCALLTYPE
get_IsLeftButtonPressed(
231 boolean
* left_button_pressed
) {
232 *left_button_pressed
= msg_
.wParam
& MK_LBUTTON
? true : false;
236 virtual HRESULT STDMETHODCALLTYPE
get_IsRightButtonPressed(
237 boolean
* right_button_pressed
) {
238 *right_button_pressed
= msg_
.wParam
& MK_RBUTTON
? true : false;
242 virtual HRESULT STDMETHODCALLTYPE
get_IsMiddleButtonPressed(
243 boolean
* middle_button_pressed
) {
244 *middle_button_pressed
= msg_
.wParam
& MK_MBUTTON
? true : false;
248 virtual HRESULT STDMETHODCALLTYPE
get_IsHorizontalMouseWheel(
249 boolean
* is_horizontal_mouse_wheel
) {
250 *is_horizontal_mouse_wheel
=
251 (msg_
.message
== WM_MOUSEHWHEEL
) ? true : false;
255 virtual HRESULT STDMETHODCALLTYPE
get_MouseWheelDelta(int* delta
) {
256 if (msg_
.message
== WM_MOUSEWHEEL
|| msg_
.message
== WM_MOUSEHWHEEL
) {
257 *delta
= GET_WHEEL_DELTA_WPARAM(msg_
.wParam
);
264 virtual HRESULT STDMETHODCALLTYPE
get_Pressure(float* pressure
) {
268 virtual HRESULT STDMETHODCALLTYPE
get_IsInverted(boolean
* inverted
) {
272 virtual HRESULT STDMETHODCALLTYPE
get_IsEraser(boolean
* is_eraser
) {
276 virtual HRESULT STDMETHODCALLTYPE
get_Orientation(float* orientation
) {
280 virtual HRESULT STDMETHODCALLTYPE
get_XTilt(float* x_tilt
) {
284 virtual HRESULT STDMETHODCALLTYPE
get_YTilt(float* y_tilt
) {
288 virtual HRESULT STDMETHODCALLTYPE
get_Twist(float* twist
) {
292 virtual HRESULT STDMETHODCALLTYPE
get_ContactRect(winfoundtn::Rect
* rect
) {
296 virtual HRESULT STDMETHODCALLTYPE
get_ContactRectRaw(winfoundtn::Rect
* rect
) {
300 virtual HRESULT STDMETHODCALLTYPE
get_TouchConfidence(boolean
* confidence
) {
304 virtual HRESULT STDMETHODCALLTYPE
get_IsPrimary(boolean
* is_primary
) {
308 virtual HRESULT STDMETHODCALLTYPE
get_IsInRange(boolean
* is_in_range
) {
312 virtual HRESULT STDMETHODCALLTYPE
get_IsCanceled(boolean
* is_canceled
) {
316 virtual HRESULT STDMETHODCALLTYPE
get_IsBarrelButtonPressed(
317 boolean
* is_barrel_button_pressed
) {
321 virtual HRESULT STDMETHODCALLTYPE
get_IsXButton1Pressed(
322 boolean
* is_xbutton1_pressed
) {
326 virtual HRESULT STDMETHODCALLTYPE
get_IsXButton2Pressed(
327 boolean
* is_xbutton2_pressed
) {
331 virtual HRESULT STDMETHODCALLTYPE
HasUsage(uint32 usage_page
,
333 boolean
* has_usage
) {
337 virtual HRESULT STDMETHODCALLTYPE
GetUsageValue(uint32 usage_page
,
339 int32
* usage_value
) {
343 // IPointerDevice implementation.
344 virtual HRESULT STDMETHODCALLTYPE
get_PointerDeviceType(
345 windevs::Input::PointerDeviceType
* device_type
) {
346 if (msg_
.message
== WM_TOUCH
) {
347 *device_type
= windevs::Input::PointerDeviceType_Touch
;
349 *device_type
= windevs::Input::PointerDeviceType_Mouse
;
354 virtual HRESULT STDMETHODCALLTYPE
get_IsIntegrated(boolean
* is_integrated
) {
358 virtual HRESULT STDMETHODCALLTYPE
get_MaxContacts(uint32
* contacts
) {
362 virtual HRESULT STDMETHODCALLTYPE
get_PhysicalDeviceRect(
363 winfoundtn::Rect
* rect
) {
367 virtual HRESULT STDMETHODCALLTYPE
get_ScreenRect(winfoundtn::Rect
* rect
) {
371 virtual HRESULT STDMETHODCALLTYPE
get_SupportedUsages(
372 winfoundtn::Collections::IVectorView
<
373 windevs::Input::PointerDeviceUsage
>** usages
) {
380 DISALLOW_COPY_AND_ASSIGN(MouseEvent
);
383 // This class implements the winrt interfaces needed to support keyboard
384 // character and system character messages.
385 class KeyEvent
: public mswr::RuntimeClass
<
386 winui::Core::IKeyEventArgs
,
387 winui::Core::ICharacterReceivedEventArgs
,
388 winui::Core::IAcceleratorKeyEventArgs
> {
390 KeyEvent(const MSG
& msg
)
393 // IKeyEventArgs implementation.
394 virtual HRESULT STDMETHODCALLTYPE
get_VirtualKey(
395 winsys::VirtualKey
* virtual_key
) {
396 *virtual_key
= static_cast<winsys::VirtualKey
>(msg_
.wParam
);
400 virtual HRESULT STDMETHODCALLTYPE
get_KeyStatus(
401 winui::Core::CorePhysicalKeyStatus
* key_status
) {
402 // As per msdn documentation for the keyboard messages.
403 key_status
->RepeatCount
= msg_
.lParam
& 0x0000FFFF;
404 key_status
->ScanCode
= (msg_
.lParam
>> 16) & 0x00FF;
405 key_status
->IsExtendedKey
= (msg_
.lParam
& (1 << 24));
406 key_status
->IsMenuKeyDown
= (msg_
.lParam
& (1 << 29));
407 key_status
->WasKeyDown
= (msg_
.lParam
& (1 << 30));
408 key_status
->IsKeyReleased
= (msg_
.lParam
& (1 << 31));
412 // ICharacterReceivedEventArgs implementation.
413 virtual HRESULT STDMETHODCALLTYPE
get_KeyCode(uint32
* key_code
) {
414 *key_code
= msg_
.wParam
;
418 // IAcceleratorKeyEventArgs implementation.
419 virtual HRESULT STDMETHODCALLTYPE
get_EventType(
420 winui::Core::CoreAcceleratorKeyEventType
* event_type
) {
421 if (msg_
.message
== WM_SYSKEYDOWN
) {
422 *event_type
= winui::Core::CoreAcceleratorKeyEventType_SystemKeyDown
;
423 } else if (msg_
.message
== WM_SYSKEYUP
) {
424 *event_type
= winui::Core::CoreAcceleratorKeyEventType_SystemKeyUp
;
425 } else if (msg_
.message
== WM_SYSCHAR
) {
426 *event_type
= winui::Core::CoreAcceleratorKeyEventType_SystemCharacter
;
435 // The following classes are the emulation of the WinRT system as exposed
436 // to metro applications. There is one application (ICoreApplication) which
437 // contains a series of Views (ICoreApplicationView) each one of them
438 // containing a CoreWindow which represents a surface that can drawn to
439 // and that receives events.
441 // Here is the general dependency hierachy in terms of interfaces:
443 // IFrameworkViewSource --> IFrameworkView
446 // ---------------------------------------------------------------------
449 // ICoreApplication ICoreApplicationView
452 // ICoreWindow -----> ICoreWindowInterop
456 // ICoreDispatcher <==> real HWND
458 class CoreDispatcherEmulation
:
459 public mswr::RuntimeClass
<
460 winui::Core::ICoreDispatcher
,
461 winui::Core::ICoreAcceleratorKeys
> {
463 CoreDispatcherEmulation(InputHandler
* input_handler
)
464 : input_handler_(input_handler
),
465 accelerator_key_event_handler_(NULL
) {}
467 // ICoreDispatcher implementation:
468 virtual HRESULT STDMETHODCALLTYPE
get_HasThreadAccess(boolean
* value
) {
472 virtual HRESULT STDMETHODCALLTYPE
ProcessEvents(
473 winui::Core::CoreProcessEventsOption options
) {
474 // We don't support the other message pump modes. So we basically enter a
475 // traditional message loop that we only exit a teardown.
476 if (options
!= winui::Core::CoreProcessEventsOption_ProcessUntilQuit
)
480 while((::GetMessage(&msg
, NULL
, 0, 0) != 0) && g_window_count
> 0) {
481 ProcessInputMessage(msg
);
482 ::TranslateMessage(&msg
);
483 ::DispatchMessage(&msg
);
485 // TODO(cpu): figure what to do with msg.WParam which we would normally
490 virtual HRESULT STDMETHODCALLTYPE
RunAsync(
491 winui::Core::CoreDispatcherPriority priority
,
492 winui::Core::IDispatchedHandler
*agileCallback
,
493 ABI::Windows::Foundation::IAsyncAction
** asyncAction
) {
497 virtual HRESULT STDMETHODCALLTYPE
RunIdleAsync(
498 winui::Core::IIdleDispatchedHandler
*agileCallback
,
499 winfoundtn::IAsyncAction
** asyncAction
) {
503 // ICoreAcceleratorKeys implementation:
504 virtual HRESULT STDMETHODCALLTYPE
add_AcceleratorKeyActivated(
505 AcceleratorKeyEventHandler
* handler
,
506 EventRegistrationToken
*pCookie
) {
507 accelerator_key_event_handler_
= handler
;
508 accelerator_key_event_handler_
->AddRef();
512 virtual HRESULT STDMETHODCALLTYPE
remove_AcceleratorKeyActivated(
513 EventRegistrationToken cookie
) {
514 accelerator_key_event_handler_
->Release();
515 accelerator_key_event_handler_
= NULL
;
520 bool ProcessInputMessage(const MSG
& msg
) {
521 // Poor man's way of dispatching input events.
523 if (input_handler_
) {
524 if ((msg
.message
>= WM_KEYFIRST
) && (msg
.message
<= WM_KEYLAST
)) {
525 if ((msg
.message
== WM_SYSKEYDOWN
) || (msg
.message
== WM_SYSKEYUP
) ||
526 msg
.message
== WM_SYSCHAR
) {
527 ret
= HandleSystemKeys(msg
);
529 ret
= input_handler_
->HandleKeyboardMessage(msg
);
531 } else if ((msg
.message
>= WM_MOUSEFIRST
) &&
532 (msg
.message
<= WM_MOUSELAST
)) {
533 ret
= input_handler_
->HandleMouseMessage(msg
);
539 bool HandleSystemKeys(const MSG
& msg
) {
540 mswr::ComPtr
<winui::Core::IAcceleratorKeyEventArgs
> event_args
;
541 event_args
= mswr::Make
<KeyEvent
>(msg
);
542 accelerator_key_event_handler_
->Invoke(this, event_args
.Get());
546 InputHandler
* input_handler_
;
547 AcceleratorKeyEventHandler
* accelerator_key_event_handler_
;
550 class CoreWindowEmulation
551 : public mswr::RuntimeClass
<
552 mswr::RuntimeClassFlags
<mswr::WinRtClassicComMix
>,
553 winui::Core::ICoreWindow
, ICoreWindowInterop
>,
554 public InputHandler
{
556 CoreWindowEmulation()
558 mouse_moved_handler_(NULL
),
559 mouse_capture_lost_handler_(NULL
),
560 mouse_pressed_handler_(NULL
),
561 mouse_released_handler_(NULL
),
562 mouse_entered_handler_(NULL
),
563 mouse_exited_handler_(NULL
),
564 mouse_wheel_changed_handler_(NULL
),
565 key_down_handler_(NULL
),
566 key_up_handler_(NULL
),
567 character_received_handler_(NULL
) {
568 dispatcher_
= mswr::Make
<CoreDispatcherEmulation
>(this);
569 core_hwnd_
= CreateMetroTopLevelWindow();
572 ~CoreWindowEmulation() {
574 ::DestroyWindow(core_hwnd_
);
577 // ICoreWindow implementation:
578 virtual HRESULT STDMETHODCALLTYPE
get_AutomationHostProvider(
579 IInspectable
** value
) {
583 virtual HRESULT STDMETHODCALLTYPE
get_Bounds(
584 winfoundtn::Rect
* value
) {
586 if (!::GetClientRect(core_hwnd_
, &rect
))
588 value
->Width
= rect
.right
;
589 value
->Height
= rect
.bottom
;
593 virtual HRESULT STDMETHODCALLTYPE
get_CustomProperties(
594 winfoundtn::Collections::IPropertySet
** value
) {
598 virtual HRESULT STDMETHODCALLTYPE
get_Dispatcher(
599 winui::Core::ICoreDispatcher
** value
) {
600 return dispatcher_
.CopyTo(value
);
603 virtual HRESULT STDMETHODCALLTYPE
get_FlowDirection(
604 winui::Core::CoreWindowFlowDirection
* value
) {
608 virtual HRESULT STDMETHODCALLTYPE
put_FlowDirection(
609 winui::Core::CoreWindowFlowDirection value
) {
613 virtual HRESULT STDMETHODCALLTYPE
get_IsInputEnabled(
618 virtual HRESULT STDMETHODCALLTYPE
put_IsInputEnabled(
623 virtual HRESULT STDMETHODCALLTYPE
get_PointerCursor(
624 winui::Core::ICoreCursor
** value
) {
628 virtual HRESULT STDMETHODCALLTYPE
put_PointerCursor(
629 winui::Core::ICoreCursor
* value
) {
633 virtual HRESULT STDMETHODCALLTYPE
get_PointerPosition(
634 winfoundtn::Point
* value
) {
638 virtual HRESULT STDMETHODCALLTYPE
get_Visible(
643 virtual HRESULT STDMETHODCALLTYPE
Activate(void) {
644 // After we fire OnActivate on the View, Chrome calls us back here.
648 virtual HRESULT STDMETHODCALLTYPE
Close(void) {
649 ::PostMessage(core_hwnd_
, WM_CLOSE
, 0, 0);
654 virtual HRESULT STDMETHODCALLTYPE
GetAsyncKeyState(
655 ABI::Windows::System::VirtualKey virtualKey
,
656 winui::Core::CoreVirtualKeyStates
* KeyState
) {
660 virtual HRESULT STDMETHODCALLTYPE
GetKeyState(
661 ABI::Windows::System::VirtualKey virtualKey
,
662 winui::Core::CoreVirtualKeyStates
* KeyState
) {
666 virtual HRESULT STDMETHODCALLTYPE
ReleasePointerCapture(void) {
670 virtual HRESULT STDMETHODCALLTYPE
SetPointerCapture(void) {
674 virtual HRESULT STDMETHODCALLTYPE
add_Activated(
675 WindowActivatedHandler
* handler
,
676 EventRegistrationToken
* pCookie
) {
677 // TODO(cpu) implement this.
681 virtual HRESULT STDMETHODCALLTYPE
remove_Activated(
682 EventRegistrationToken cookie
) {
686 virtual HRESULT STDMETHODCALLTYPE
add_AutomationProviderRequested(
687 AutomationProviderHandler
* handler
,
688 EventRegistrationToken
* cookie
) {
692 virtual HRESULT STDMETHODCALLTYPE
remove_AutomationProviderRequested(
693 EventRegistrationToken cookie
) {
697 virtual HRESULT STDMETHODCALLTYPE
add_CharacterReceived(
698 CharEventHandler
* handler
,
699 EventRegistrationToken
* pCookie
) {
700 character_received_handler_
= handler
;
701 character_received_handler_
->AddRef();
705 virtual HRESULT STDMETHODCALLTYPE
remove_CharacterReceived(
706 EventRegistrationToken cookie
) {
707 character_received_handler_
->Release();
708 character_received_handler_
= NULL
;
712 virtual HRESULT STDMETHODCALLTYPE
add_Closed(
713 CoreWindowEventHandler
* handler
,
714 EventRegistrationToken
* pCookie
) {
718 virtual HRESULT STDMETHODCALLTYPE
remove_Closed(
719 EventRegistrationToken cookie
) {
723 virtual HRESULT STDMETHODCALLTYPE
add_InputEnabled(
724 InputEnabledEventHandler
* handler
,
725 EventRegistrationToken
* pCookie
) {
729 virtual HRESULT STDMETHODCALLTYPE
remove_InputEnabled(
730 EventRegistrationToken cookie
) {
734 virtual HRESULT STDMETHODCALLTYPE
add_KeyDown(
735 KeyEventHandler
* handler
,
736 EventRegistrationToken
* pCookie
) {
737 key_down_handler_
= handler
;
738 key_down_handler_
->AddRef();
742 virtual HRESULT STDMETHODCALLTYPE
remove_KeyDown(
743 EventRegistrationToken cookie
) {
744 key_down_handler_
->Release();
745 key_down_handler_
= NULL
;
749 virtual HRESULT STDMETHODCALLTYPE
add_KeyUp(
750 KeyEventHandler
* handler
,
751 EventRegistrationToken
* pCookie
) {
752 key_up_handler_
= handler
;
753 key_up_handler_
->AddRef();
757 virtual HRESULT STDMETHODCALLTYPE
remove_KeyUp(
758 EventRegistrationToken cookie
) {
759 key_up_handler_
->Release();
760 key_up_handler_
= NULL
;
764 virtual HRESULT STDMETHODCALLTYPE
add_PointerCaptureLost(
765 PointerEventHandler
* handler
,
766 EventRegistrationToken
* cookie
) {
767 mouse_capture_lost_handler_
= handler
;
771 virtual HRESULT STDMETHODCALLTYPE
remove_PointerCaptureLost(
772 EventRegistrationToken cookie
) {
773 mouse_capture_lost_handler_
= NULL
;
777 virtual HRESULT STDMETHODCALLTYPE
add_PointerEntered(
778 PointerEventHandler
* handler
,
779 EventRegistrationToken
* cookie
) {
780 mouse_entered_handler_
= handler
;
784 virtual HRESULT STDMETHODCALLTYPE
remove_PointerEntered(
785 EventRegistrationToken cookie
) {
786 mouse_entered_handler_
= NULL
;
790 virtual HRESULT STDMETHODCALLTYPE
add_PointerExited(
791 PointerEventHandler
* handler
,
792 EventRegistrationToken
* cookie
) {
793 mouse_exited_handler_
= handler
;
797 virtual HRESULT STDMETHODCALLTYPE
remove_PointerExited(
798 EventRegistrationToken cookie
) {
799 mouse_exited_handler_
= NULL
;
803 virtual HRESULT STDMETHODCALLTYPE
add_PointerMoved(
804 PointerEventHandler
* handler
,
805 EventRegistrationToken
* cookie
) {
806 mouse_moved_handler_
= handler
;
807 mouse_moved_handler_
->AddRef();
811 virtual HRESULT STDMETHODCALLTYPE
remove_PointerMoved(
812 EventRegistrationToken cookie
) {
813 mouse_moved_handler_
->Release();
814 mouse_moved_handler_
= NULL
;
818 virtual HRESULT STDMETHODCALLTYPE
add_PointerPressed(
819 PointerEventHandler
* handler
,
820 EventRegistrationToken
* cookie
) {
821 mouse_pressed_handler_
= handler
;
822 mouse_pressed_handler_
->AddRef();
826 virtual HRESULT STDMETHODCALLTYPE
remove_PointerPressed(
827 EventRegistrationToken cookie
) {
828 mouse_pressed_handler_
->Release();
829 mouse_pressed_handler_
= NULL
;
833 virtual HRESULT STDMETHODCALLTYPE
add_PointerReleased(
834 PointerEventHandler
* handler
,
835 EventRegistrationToken
* cookie
) {
836 mouse_released_handler_
= handler
;
837 mouse_released_handler_
->AddRef();
841 virtual HRESULT STDMETHODCALLTYPE
remove_PointerReleased(
842 EventRegistrationToken cookie
) {
843 mouse_released_handler_
->Release();
844 mouse_released_handler_
= NULL
;
848 virtual HRESULT STDMETHODCALLTYPE
add_TouchHitTesting(
849 TouchHitTestHandler
* handler
,
850 EventRegistrationToken
* pCookie
) {
854 virtual HRESULT STDMETHODCALLTYPE
remove_TouchHitTesting(
855 EventRegistrationToken cookie
) {
859 virtual HRESULT STDMETHODCALLTYPE
add_PointerWheelChanged(
860 PointerEventHandler
* handler
,
861 EventRegistrationToken
* cookie
) {
862 mouse_wheel_changed_handler_
= handler
;
863 mouse_wheel_changed_handler_
->AddRef();
867 virtual HRESULT STDMETHODCALLTYPE
remove_PointerWheelChanged(
868 EventRegistrationToken cookie
) {
869 mouse_wheel_changed_handler_
->Release();
870 mouse_wheel_changed_handler_
= NULL
;
874 virtual HRESULT STDMETHODCALLTYPE
add_SizeChanged(
875 SizeChangedHandler
* handler
,
876 EventRegistrationToken
* pCookie
) {
877 // TODO(cpu): implement this.
881 virtual HRESULT STDMETHODCALLTYPE
remove_SizeChanged(
882 EventRegistrationToken cookie
) {
886 virtual HRESULT STDMETHODCALLTYPE
add_VisibilityChanged(
887 VisibilityChangedHandler
* handler
,
888 EventRegistrationToken
* pCookie
) {
892 virtual HRESULT STDMETHODCALLTYPE
remove_VisibilityChanged(
893 EventRegistrationToken cookie
) {
897 // ICoreWindowInterop implementation:
898 virtual HRESULT STDMETHODCALLTYPE
get_WindowHandle(HWND
* hwnd
) {
905 virtual HRESULT STDMETHODCALLTYPE
put_MessageHandled(
911 virtual bool HandleKeyboardMessage(const MSG
& msg
) OVERRIDE
{
912 switch (msg
.message
) {
915 mswr::ComPtr
<winui::Core::IKeyEventArgs
> event_args
;
916 event_args
= mswr::Make
<KeyEvent
>(msg
);
917 KeyEventHandler
* handler
= NULL
;
918 if (msg
.message
== WM_KEYDOWN
) {
919 handler
= key_down_handler_
;
921 handler
= key_up_handler_
;
923 handler
->Invoke(this, event_args
.Get());
930 mswr::ComPtr
<winui::Core::ICharacterReceivedEventArgs
> event_args
;
931 event_args
= mswr::Make
<KeyEvent
>(msg
);
932 character_received_handler_
->Invoke(this, event_args
.Get());
942 virtual bool HandleMouseMessage(const MSG
& msg
) OVERRIDE
{
943 PointerEventHandler
* handler
= NULL
;
944 mswr::ComPtr
<winui::Core::IPointerEventArgs
> event_args
;
945 event_args
= mswr::Make
<MouseEvent
>(msg
);
946 switch (msg
.message
) {
948 handler
= mouse_moved_handler_
;
951 case WM_LBUTTONDOWN
: {
954 handler
= mouse_pressed_handler_
;
961 handler
= mouse_released_handler_
;
965 case WM_MOUSEWHEEL
: {
967 handler
= mouse_wheel_changed_handler_
;
975 handler
->Invoke(this, event_args
.Get());
980 PointerEventHandler
* mouse_moved_handler_
;
981 PointerEventHandler
* mouse_capture_lost_handler_
;
982 PointerEventHandler
* mouse_pressed_handler_
;
983 PointerEventHandler
* mouse_released_handler_
;
984 PointerEventHandler
* mouse_entered_handler_
;
985 PointerEventHandler
* mouse_exited_handler_
;
986 PointerEventHandler
* mouse_wheel_changed_handler_
;
987 KeyEventHandler
* key_down_handler_
;
988 KeyEventHandler
* key_up_handler_
;
989 CharEventHandler
* character_received_handler_
;
991 mswr::ComPtr
<winui::Core::ICoreDispatcher
> dispatcher_
;
995 : public mswr::RuntimeClass
<winapp::Activation::IActivatedEventArgs
> {
997 ActivatedEvent(winapp::Activation::ActivationKind activation_kind
)
998 : activation_kind_(activation_kind
) {
1001 virtual HRESULT STDMETHODCALLTYPE
get_Kind(
1002 winapp::Activation::ActivationKind
*value
) {
1003 *value
= activation_kind_
;
1007 virtual HRESULT STDMETHODCALLTYPE
get_PreviousExecutionState(
1008 winapp::Activation::ApplicationExecutionState
*value
) {
1009 *value
= winapp::Activation::ApplicationExecutionState_ClosedByUser
;
1013 virtual HRESULT STDMETHODCALLTYPE
get_SplashScreen(
1014 winapp::Activation::ISplashScreen
**value
) {
1019 winapp::Activation::ActivationKind activation_kind_
;
1022 class CoreApplicationViewEmulation
1023 : public mswr::RuntimeClass
<winapp::Core::ICoreApplicationView
> {
1025 CoreApplicationViewEmulation() {
1026 core_window_
= mswr::Make
<CoreWindowEmulation
>();
1029 HRESULT
Activate() {
1030 if (activated_handler_
) {
1031 auto ae
= mswr::Make
<ActivatedEvent
>(
1032 winapp::Activation::ActivationKind_File
);
1033 return activated_handler_
->Invoke(this, ae
.Get());
1040 return core_window_
->Close();
1043 // ICoreApplicationView implementation:
1044 virtual HRESULT STDMETHODCALLTYPE
get_CoreWindow(
1045 winui::Core::ICoreWindow
** value
) {
1048 return core_window_
.CopyTo(value
);
1051 virtual HRESULT STDMETHODCALLTYPE
add_Activated(
1052 ActivatedHandler
* handler
,
1053 EventRegistrationToken
* token
) {
1054 // The real component supports multiple handles but we don't yet.
1055 if (activated_handler_
)
1057 activated_handler_
= handler
;
1061 virtual HRESULT STDMETHODCALLTYPE
remove_Activated(
1062 EventRegistrationToken token
) {
1063 // Chrome never unregisters handlers, so we don't care about it.
1067 virtual HRESULT STDMETHODCALLTYPE
get_IsMain(
1072 virtual HRESULT STDMETHODCALLTYPE
get_IsHosted(
1078 mswr::ComPtr
<CoreWindowEmulation
> core_window_
;
1079 mswr::ComPtr
<ActivatedHandler
> activated_handler_
;
1082 class CoreApplicationWin7Emulation
1083 : public mswr::RuntimeClass
<winapp::Core::ICoreApplication
,
1084 winapp::Core::ICoreApplicationExit
> {
1086 // ICoreApplication implementation:
1088 virtual HRESULT STDMETHODCALLTYPE
get_Id(
1093 virtual HRESULT STDMETHODCALLTYPE
add_Suspending(
1094 winfoundtn::IEventHandler
<winapp::SuspendingEventArgs
*>* handler
,
1095 EventRegistrationToken
* token
) {
1099 virtual HRESULT STDMETHODCALLTYPE
remove_Suspending(
1100 EventRegistrationToken token
) {
1104 virtual HRESULT STDMETHODCALLTYPE
add_Resuming(
1105 winfoundtn::IEventHandler
<IInspectable
*>* handler
,
1106 EventRegistrationToken
* token
) {
1110 virtual HRESULT STDMETHODCALLTYPE
remove_Resuming(
1111 EventRegistrationToken token
) {
1115 virtual HRESULT STDMETHODCALLTYPE
get_Properties(
1116 winfoundtn::Collections::IPropertySet
** value
) {
1120 virtual HRESULT STDMETHODCALLTYPE
GetCurrentView(
1121 winapp::Core::ICoreApplicationView
** value
) {
1125 virtual HRESULT STDMETHODCALLTYPE
Run(
1126 winapp::Core::IFrameworkViewSource
* viewSource
) {
1127 HRESULT hr
= viewSource
->CreateView(app_view_
.GetAddressOf());
1130 view_emulation_
= mswr::Make
<CoreApplicationViewEmulation
>();
1131 hr
= app_view_
->Initialize(view_emulation_
.Get());
1134 mswr::ComPtr
<winui::Core::ICoreWindow
> core_window
;
1135 hr
= view_emulation_
->get_CoreWindow(core_window
.GetAddressOf());
1138 hr
= app_view_
->SetWindow(core_window
.Get());
1141 hr
= app_view_
->Load(NULL
);
1144 hr
= view_emulation_
->Activate();
1147 return app_view_
->Run();
1150 virtual HRESULT STDMETHODCALLTYPE
RunWithActivationFactories(
1151 winfoundtn::IGetActivationFactory
* activationFactoryCallback
) {
1155 // ICoreApplicationExit implementation:
1157 virtual HRESULT STDMETHODCALLTYPE
Exit(void) {
1158 return view_emulation_
->Close();
1161 virtual HRESULT STDMETHODCALLTYPE
add_Exiting(
1162 winfoundtn::IEventHandler
<IInspectable
*>* handler
,
1163 EventRegistrationToken
* token
) {
1167 virtual HRESULT STDMETHODCALLTYPE
remove_Exiting(
1168 EventRegistrationToken token
) {
1173 mswr::ComPtr
<winapp::Core::IFrameworkView
> app_view_
;
1174 mswr::ComPtr
<CoreApplicationViewEmulation
> view_emulation_
;
1178 mswr::ComPtr
<winapp::Core::ICoreApplication
> InitWindows7() {
1179 HRESULT hr
= ::CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1182 return mswr::Make
<CoreApplicationWin7Emulation
>();