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.
5 #include "ui/views/widget/desktop_aura/desktop_root_window_host_win.h"
7 #include "base/win/metro.h"
8 #include "third_party/skia/include/core/SkPath.h"
9 #include "third_party/skia/include/core/SkRegion.h"
10 #include "ui/aura/client/aura_constants.h"
11 #include "ui/aura/client/cursor_client.h"
12 #include "ui/aura/client/focus_client.h"
13 #include "ui/aura/client/scoped_tooltip_disabler.h"
14 #include "ui/aura/root_window.h"
15 #include "ui/aura/window_property.h"
16 #include "ui/base/cursor/cursor_loader_win.h"
17 #include "ui/base/ime/input_method.h"
18 #include "ui/base/win/shell.h"
19 #include "ui/compositor/compositor_constants.h"
20 #include "ui/gfx/insets.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gfx/path.h"
23 #include "ui/gfx/path_win.h"
24 #include "ui/gfx/vector2d.h"
25 #include "ui/gfx/win/dpi.h"
26 #include "ui/native_theme/native_theme_aura.h"
27 #include "ui/native_theme/native_theme_win.h"
28 #include "ui/views/corewm/compound_event_filter.h"
29 #include "ui/views/corewm/corewm_switches.h"
30 #include "ui/views/corewm/input_method_event_filter.h"
31 #include "ui/views/corewm/tooltip_win.h"
32 #include "ui/views/corewm/window_animations.h"
33 #include "ui/views/ime/input_method_bridge.h"
34 #include "ui/views/widget/desktop_aura/desktop_cursor_loader_updater.h"
35 #include "ui/views/widget/desktop_aura/desktop_drag_drop_client_win.h"
36 #include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h"
37 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
38 #include "ui/views/widget/root_view.h"
39 #include "ui/views/widget/widget_delegate.h"
40 #include "ui/views/widget/widget_hwnd_utils.h"
41 #include "ui/views/win/fullscreen_handler.h"
42 #include "ui/views/win/hwnd_message_handler.h"
43 #include "ui/views/window/native_frame_view.h"
49 gfx::Size
GetExpandedWindowSize(DWORD window_style
, gfx::Size size
) {
50 if (!(window_style
& WS_EX_COMPOSITED
) || !ui::win::IsAeroGlassEnabled())
53 // Some AMD drivers can't display windows that are less than 64x64 pixels,
54 // so expand them to be at least that size. http://crbug.com/286609
55 gfx::Size
expanded(std::max(size
.width(), 64), std::max(size
.height(), 64));
59 void InsetBottomRight(gfx::Rect
* rect
, gfx::Vector2d vector
) {
60 rect
->Inset(0, 0, vector
.x(), vector
.y());
65 DEFINE_WINDOW_PROPERTY_KEY(aura::Window
*, kContentWindowForRootWindow
, NULL
);
67 // Identifies the DesktopWindowTreeHostWin associated with the RootWindow.
68 DEFINE_WINDOW_PROPERTY_KEY(DesktopWindowTreeHostWin
*, kDesktopWindowTreeHostKey
,
71 ////////////////////////////////////////////////////////////////////////////////
72 // DesktopWindowTreeHostWin, public:
74 bool DesktopWindowTreeHostWin::is_cursor_visible_
= true;
76 DesktopWindowTreeHostWin::DesktopWindowTreeHostWin(
77 internal::NativeWidgetDelegate
* native_widget_delegate
,
78 DesktopNativeWidgetAura
* desktop_native_widget_aura
)
80 message_handler_(new HWNDMessageHandler(this)),
81 native_widget_delegate_(native_widget_delegate
),
82 desktop_native_widget_aura_(desktop_native_widget_aura
),
83 content_window_(NULL
),
84 drag_drop_client_(NULL
),
85 should_animate_window_close_(false),
86 pending_close_(false),
87 has_non_client_view_(false),
91 DesktopWindowTreeHostWin::~DesktopWindowTreeHostWin() {
92 // WARNING: |content_window_| has been destroyed by the time we get here.
93 desktop_native_widget_aura_
->OnDesktopWindowTreeHostDestroyed(
98 aura::Window
* DesktopWindowTreeHostWin::GetContentWindowForHWND(HWND hwnd
) {
99 aura::RootWindow
* root
= aura::RootWindow::GetForAcceleratedWidget(hwnd
);
100 return root
? root
->window()->GetProperty(kContentWindowForRootWindow
) : NULL
;
104 ui::NativeTheme
* DesktopWindowTreeHost::GetNativeTheme(aura::Window
* window
) {
105 // Use NativeThemeWin for windows shown on the desktop, those not on the
106 // desktop come from Ash and get NativeThemeAura.
107 aura::WindowEventDispatcher
* dispatcher
=
108 window
? window
->GetDispatcher() : NULL
;
110 HWND host_hwnd
= dispatcher
->host()->GetAcceleratedWidget();
112 DesktopWindowTreeHostWin::GetContentWindowForHWND(host_hwnd
)) {
113 return ui::NativeThemeWin::instance();
116 return ui::NativeThemeAura::instance();
119 ////////////////////////////////////////////////////////////////////////////////
120 // DesktopWindowTreeHostWin, DesktopWindowTreeHost implementation:
122 void DesktopWindowTreeHostWin::Init(
123 aura::Window
* content_window
,
124 const Widget::InitParams
& params
,
125 aura::RootWindow::CreateParams
* rw_create_params
) {
126 // TODO(beng): SetInitParams().
127 content_window_
= content_window
;
129 aura::client::SetAnimationHost(content_window_
, this);
131 ConfigureWindowStyles(message_handler_
.get(), params
,
132 GetWidget()->widget_delegate(),
133 native_widget_delegate_
);
135 HWND parent_hwnd
= NULL
;
136 if (params
.parent
&& params
.parent
->GetDispatcher()) {
138 params
.parent
->GetDispatcher()->host()->GetAcceleratedWidget();
141 message_handler_
->set_remove_standard_frame(params
.remove_standard_frame
);
143 has_non_client_view_
= Widget::RequiresNonClientView(params
.type
);
145 gfx::Rect pixel_bounds
= gfx::win::DIPToScreenRect(params
.bounds
);
146 message_handler_
->Init(parent_hwnd
, pixel_bounds
);
147 if (params
.type
== Widget::InitParams::TYPE_MENU
) {
148 ::SetProp(GetAcceleratedWidget(),
149 kForceSoftwareCompositor
,
150 reinterpret_cast<HANDLE
>(true));
152 CreateCompositor(GetAcceleratedWidget());
154 rw_create_params
->host
= this;
157 void DesktopWindowTreeHostWin::OnRootWindowCreated(
158 aura::RootWindow
* root
,
159 const Widget::InitParams
& params
) {
162 // The cursor is not necessarily visible when the root window is created.
163 aura::client::CursorClient
* cursor_client
=
164 aura::client::GetCursorClient(root_window_
->window());
166 is_cursor_visible_
= cursor_client
->IsCursorVisible();
168 root_window_
->window()->SetProperty(kContentWindowForRootWindow
,
170 root_window_
->window()->SetProperty(kDesktopWindowTreeHostKey
, this);
172 should_animate_window_close_
=
173 content_window_
->type() != ui::wm::WINDOW_TYPE_NORMAL
&&
174 !views::corewm::WindowAnimationsDisabled(content_window_
);
176 // TODO this is not invoked *after* Init(), but should be ok.
177 SetWindowTransparency();
180 scoped_ptr
<corewm::Tooltip
> DesktopWindowTreeHostWin::CreateTooltip() {
182 tooltip_
= new corewm::TooltipWin(GetAcceleratedWidget());
183 return scoped_ptr
<corewm::Tooltip
>(tooltip_
);
186 scoped_ptr
<aura::client::DragDropClient
>
187 DesktopWindowTreeHostWin::CreateDragDropClient(
188 DesktopNativeCursorManager
* cursor_manager
) {
189 drag_drop_client_
= new DesktopDragDropClientWin(root_window_
->window(),
191 return scoped_ptr
<aura::client::DragDropClient
>(drag_drop_client_
).Pass();
194 void DesktopWindowTreeHostWin::Close() {
195 // TODO(beng): Move this entire branch to DNWA so it can be shared with X11.
196 if (should_animate_window_close_
) {
197 pending_close_
= true;
198 const bool is_animating
=
199 content_window_
->layer()->GetAnimator()->IsAnimatingProperty(
200 ui::LayerAnimationElement::VISIBILITY
);
201 // Animation may not start for a number of reasons.
203 message_handler_
->Close();
204 // else case, OnWindowHidingAnimationCompleted does the actual Close.
206 message_handler_
->Close();
210 void DesktopWindowTreeHostWin::CloseNow() {
211 message_handler_
->CloseNow();
214 aura::WindowTreeHost
* DesktopWindowTreeHostWin::AsWindowTreeHost() {
218 void DesktopWindowTreeHostWin::ShowWindowWithState(
219 ui::WindowShowState show_state
) {
220 message_handler_
->ShowWindowWithState(show_state
);
223 void DesktopWindowTreeHostWin::ShowMaximizedWithBounds(
224 const gfx::Rect
& restored_bounds
) {
225 gfx::Rect pixel_bounds
= gfx::win::DIPToScreenRect(restored_bounds
);
226 message_handler_
->ShowMaximizedWithBounds(pixel_bounds
);
229 bool DesktopWindowTreeHostWin::IsVisible() const {
230 return message_handler_
->IsVisible();
233 void DesktopWindowTreeHostWin::SetSize(const gfx::Size
& size
) {
234 gfx::Size size_in_pixels
= gfx::win::DIPToScreenSize(size
);
235 gfx::Size expanded
= GetExpandedWindowSize(
236 message_handler_
->window_ex_style(), size_in_pixels
);
237 window_enlargement_
=
238 gfx::Vector2d(expanded
.width() - size_in_pixels
.width(),
239 expanded
.height() - size_in_pixels
.height());
240 message_handler_
->SetSize(expanded
);
243 void DesktopWindowTreeHostWin::StackAtTop() {
244 message_handler_
->StackAtTop();
247 void DesktopWindowTreeHostWin::CenterWindow(const gfx::Size
& size
) {
248 gfx::Size size_in_pixels
= gfx::win::DIPToScreenSize(size
);
249 gfx::Size expanded_size
;
250 expanded_size
= GetExpandedWindowSize(message_handler_
->window_ex_style(),
252 window_enlargement_
=
253 gfx::Vector2d(expanded_size
.width() - size_in_pixels
.width(),
254 expanded_size
.height() - size_in_pixels
.height());
255 message_handler_
->CenterWindow(expanded_size
);
258 void DesktopWindowTreeHostWin::GetWindowPlacement(
260 ui::WindowShowState
* show_state
) const {
261 message_handler_
->GetWindowPlacement(bounds
, show_state
);
262 InsetBottomRight(bounds
, window_enlargement_
);
263 *bounds
= gfx::win::ScreenToDIPRect(*bounds
);
266 gfx::Rect
DesktopWindowTreeHostWin::GetWindowBoundsInScreen() const {
267 gfx::Rect pixel_bounds
= message_handler_
->GetWindowBoundsInScreen();
268 InsetBottomRight(&pixel_bounds
, window_enlargement_
);
269 return gfx::win::ScreenToDIPRect(pixel_bounds
);
272 gfx::Rect
DesktopWindowTreeHostWin::GetClientAreaBoundsInScreen() const {
273 gfx::Rect pixel_bounds
= message_handler_
->GetClientAreaBoundsInScreen();
274 InsetBottomRight(&pixel_bounds
, window_enlargement_
);
275 return gfx::win::ScreenToDIPRect(pixel_bounds
);
278 gfx::Rect
DesktopWindowTreeHostWin::GetRestoredBounds() const {
279 gfx::Rect pixel_bounds
= message_handler_
->GetRestoredBounds();
280 InsetBottomRight(&pixel_bounds
, window_enlargement_
);
281 return gfx::win::ScreenToDIPRect(pixel_bounds
);
284 gfx::Rect
DesktopWindowTreeHostWin::GetWorkAreaBoundsInScreen() const {
285 MONITORINFO monitor_info
;
286 monitor_info
.cbSize
= sizeof(monitor_info
);
287 GetMonitorInfo(MonitorFromWindow(message_handler_
->hwnd(),
288 MONITOR_DEFAULTTONEAREST
),
290 gfx::Rect pixel_bounds
= gfx::Rect(monitor_info
.rcWork
);
291 return gfx::win::ScreenToDIPRect(pixel_bounds
);
294 void DesktopWindowTreeHostWin::SetShape(gfx::NativeRegion native_region
) {
296 message_handler_
->SetRegion(gfx::CreateHRGNFromSkRegion(*native_region
));
298 message_handler_
->SetRegion(NULL
);
301 delete native_region
;
304 void DesktopWindowTreeHostWin::Activate() {
305 message_handler_
->Activate();
308 void DesktopWindowTreeHostWin::Deactivate() {
309 message_handler_
->Deactivate();
312 bool DesktopWindowTreeHostWin::IsActive() const {
313 return message_handler_
->IsActive();
316 void DesktopWindowTreeHostWin::Maximize() {
317 message_handler_
->Maximize();
320 void DesktopWindowTreeHostWin::Minimize() {
321 message_handler_
->Minimize();
324 void DesktopWindowTreeHostWin::Restore() {
325 message_handler_
->Restore();
328 bool DesktopWindowTreeHostWin::IsMaximized() const {
329 return message_handler_
->IsMaximized();
332 bool DesktopWindowTreeHostWin::IsMinimized() const {
333 return message_handler_
->IsMinimized();
336 bool DesktopWindowTreeHostWin::HasCapture() const {
337 return message_handler_
->HasCapture();
340 void DesktopWindowTreeHostWin::SetAlwaysOnTop(bool always_on_top
) {
341 message_handler_
->SetAlwaysOnTop(always_on_top
);
344 bool DesktopWindowTreeHostWin::IsAlwaysOnTop() const {
345 return message_handler_
->IsAlwaysOnTop();
348 bool DesktopWindowTreeHostWin::SetWindowTitle(const base::string16
& title
) {
349 return message_handler_
->SetTitle(title
);
352 void DesktopWindowTreeHostWin::ClearNativeFocus() {
353 message_handler_
->ClearNativeFocus();
356 Widget::MoveLoopResult
DesktopWindowTreeHostWin::RunMoveLoop(
357 const gfx::Vector2d
& drag_offset
,
358 Widget::MoveLoopSource source
,
359 Widget::MoveLoopEscapeBehavior escape_behavior
) {
360 const bool hide_on_escape
=
361 escape_behavior
== Widget::MOVE_LOOP_ESCAPE_BEHAVIOR_HIDE
;
362 return message_handler_
->RunMoveLoop(drag_offset
, hide_on_escape
) ?
363 Widget::MOVE_LOOP_SUCCESSFUL
: Widget::MOVE_LOOP_CANCELED
;
366 void DesktopWindowTreeHostWin::EndMoveLoop() {
367 message_handler_
->EndMoveLoop();
370 void DesktopWindowTreeHostWin::SetVisibilityChangedAnimationsEnabled(
372 message_handler_
->SetVisibilityChangedAnimationsEnabled(value
);
373 content_window_
->SetProperty(aura::client::kAnimationsDisabledKey
, !value
);
376 bool DesktopWindowTreeHostWin::ShouldUseNativeFrame() {
377 return ui::win::IsAeroGlassEnabled();
380 void DesktopWindowTreeHostWin::FrameTypeChanged() {
381 message_handler_
->FrameTypeChanged();
382 SetWindowTransparency();
385 NonClientFrameView
* DesktopWindowTreeHostWin::CreateNonClientFrameView() {
386 return GetWidget()->ShouldUseNativeFrame() ?
387 new NativeFrameView(GetWidget()) : NULL
;
390 void DesktopWindowTreeHostWin::SetFullscreen(bool fullscreen
) {
391 message_handler_
->fullscreen_handler()->SetFullscreen(fullscreen
);
392 // TODO(sky): workaround for ScopedFullscreenVisibility showing window
393 // directly. Instead of this should listen for visibility changes and then
395 if (message_handler_
->IsVisible() && !content_window_
->TargetVisibility())
396 content_window_
->Show();
397 SetWindowTransparency();
400 bool DesktopWindowTreeHostWin::IsFullscreen() const {
401 return message_handler_
->fullscreen_handler()->fullscreen();
404 void DesktopWindowTreeHostWin::SetOpacity(unsigned char opacity
) {
405 message_handler_
->SetOpacity(static_cast<BYTE
>(opacity
));
406 content_window_
->layer()->SetOpacity(opacity
/ 255.0);
409 void DesktopWindowTreeHostWin::SetWindowIcons(
410 const gfx::ImageSkia
& window_icon
, const gfx::ImageSkia
& app_icon
) {
411 message_handler_
->SetWindowIcons(window_icon
, app_icon
);
414 void DesktopWindowTreeHostWin::InitModalType(ui::ModalType modal_type
) {
415 message_handler_
->InitModalType(modal_type
);
418 void DesktopWindowTreeHostWin::FlashFrame(bool flash_frame
) {
419 message_handler_
->FlashFrame(flash_frame
);
422 void DesktopWindowTreeHostWin::OnRootViewLayout() const {
425 void DesktopWindowTreeHostWin::OnNativeWidgetFocus() {
426 // HWNDMessageHandler will perform the proper updating on its own.
429 void DesktopWindowTreeHostWin::OnNativeWidgetBlur() {
432 bool DesktopWindowTreeHostWin::IsAnimatingClosed() const {
433 return pending_close_
;
436 ////////////////////////////////////////////////////////////////////////////////
437 // DesktopWindowTreeHostWin, WindowTreeHost implementation:
439 aura::RootWindow
* DesktopWindowTreeHostWin::GetRootWindow() {
443 gfx::AcceleratedWidget
DesktopWindowTreeHostWin::GetAcceleratedWidget() {
444 return message_handler_
->hwnd();
447 void DesktopWindowTreeHostWin::Show() {
448 message_handler_
->Show();
451 void DesktopWindowTreeHostWin::Hide() {
453 message_handler_
->Hide();
456 void DesktopWindowTreeHostWin::ToggleFullScreen() {
457 SetWindowTransparency();
460 // GetBounds and SetBounds work in pixel coordinates, whereas other get/set
461 // methods work in DIP.
463 gfx::Rect
DesktopWindowTreeHostWin::GetBounds() const {
464 gfx::Rect
bounds(message_handler_
->GetClientAreaBounds());
465 // If the window bounds were expanded we need to return the original bounds
466 // To achieve this we do the reverse of the expansion, i.e. add the
467 // window_expansion_top_left_delta_ to the origin and subtract the
468 // window_expansion_bottom_right_delta_ from the width and height.
469 gfx::Rect
without_expansion(
470 bounds
.x() + window_expansion_top_left_delta_
.x(),
471 bounds
.y() + window_expansion_top_left_delta_
.y(),
472 bounds
.width() - window_expansion_bottom_right_delta_
.x() -
473 window_enlargement_
.x(),
474 bounds
.height() - window_expansion_bottom_right_delta_
.y() -
475 window_enlargement_
.y());
476 return without_expansion
;
479 void DesktopWindowTreeHostWin::SetBounds(const gfx::Rect
& bounds
) {
480 // If the window bounds have to be expanded we need to subtract the
481 // window_expansion_top_left_delta_ from the origin and add the
482 // window_expansion_bottom_right_delta_ to the width and height
483 gfx::Size
old_hwnd_size(message_handler_
->GetClientAreaBounds().size());
484 gfx::Size old_content_size
= GetBounds().size();
487 bounds
.x() - window_expansion_top_left_delta_
.x(),
488 bounds
.y() - window_expansion_top_left_delta_
.y(),
489 bounds
.width() + window_expansion_bottom_right_delta_
.x(),
490 bounds
.height() + window_expansion_bottom_right_delta_
.y());
492 gfx::Rect
new_expanded(
494 GetExpandedWindowSize(message_handler_
->window_ex_style(),
496 window_enlargement_
=
497 gfx::Vector2d(new_expanded
.width() - expanded
.width(),
498 new_expanded
.height() - expanded
.height());
499 message_handler_
->SetBounds(new_expanded
);
501 // The client area size may have changed even though the window bounds have
502 // not, if the window bounds were expanded to 64 pixels both times.
503 if (old_hwnd_size
== new_expanded
.size() && old_content_size
!= bounds
.size())
504 HandleClientSizeChanged(new_expanded
.size());
507 gfx::Insets
DesktopWindowTreeHostWin::GetInsets() const {
508 return gfx::Insets();
511 void DesktopWindowTreeHostWin::SetInsets(const gfx::Insets
& insets
) {
514 gfx::Point
DesktopWindowTreeHostWin::GetLocationOnNativeScreen() const {
515 return GetBounds().origin();
518 void DesktopWindowTreeHostWin::SetCapture() {
519 message_handler_
->SetCapture();
522 void DesktopWindowTreeHostWin::ReleaseCapture() {
523 message_handler_
->ReleaseCapture();
526 void DesktopWindowTreeHostWin::SetCursor(gfx::NativeCursor cursor
) {
527 ui::CursorLoaderWin cursor_loader
;
528 cursor_loader
.SetPlatformCursor(&cursor
);
530 message_handler_
->SetCursor(cursor
.platform());
533 bool DesktopWindowTreeHostWin::QueryMouseLocation(gfx::Point
* location_return
) {
534 aura::client::CursorClient
* cursor_client
=
535 aura::client::GetCursorClient(root_window_
->window());
536 if (cursor_client
&& !cursor_client
->IsMouseEventsEnabled()) {
537 *location_return
= gfx::Point(0, 0);
543 gfx::Point(static_cast<int>(pt
.x
), static_cast<int>(pt
.y
));
547 bool DesktopWindowTreeHostWin::ConfineCursorToRootWindow() {
548 RECT window_rect
= root_window_
->window()->GetBoundsInScreen().ToRECT();
549 ::ClipCursor(&window_rect
);
553 void DesktopWindowTreeHostWin::UnConfineCursor() {
557 void DesktopWindowTreeHostWin::OnCursorVisibilityChanged(bool show
) {
558 if (is_cursor_visible_
== show
)
560 is_cursor_visible_
= show
;
561 ::ShowCursor(!!show
);
564 void DesktopWindowTreeHostWin::MoveCursorTo(const gfx::Point
& location
) {
565 POINT cursor_location
= location
.ToPOINT();
566 ::ClientToScreen(GetHWND(), &cursor_location
);
567 ::SetCursorPos(cursor_location
.x
, cursor_location
.y
);
570 void DesktopWindowTreeHostWin::PostNativeEvent(
571 const base::NativeEvent
& native_event
) {
574 void DesktopWindowTreeHostWin::OnDeviceScaleFactorChanged(
575 float device_scale_factor
) {
578 void DesktopWindowTreeHostWin::PrepareForShutdown() {
582 ////////////////////////////////////////////////////////////////////////////////
583 // DesktopWindowTreeHostWin, ui::EventSource implementation:
585 ui::EventProcessor
* DesktopWindowTreeHostWin::GetEventProcessor() {
586 return delegate_
->GetEventProcessor();
589 ////////////////////////////////////////////////////////////////////////////////
590 // DesktopWindowTreeHostWin, aura::AnimationHost implementation:
592 void DesktopWindowTreeHostWin::SetHostTransitionOffsets(
593 const gfx::Vector2d
& top_left_delta
,
594 const gfx::Vector2d
& bottom_right_delta
) {
595 gfx::Rect bounds_without_expansion
= GetBounds();
596 window_expansion_top_left_delta_
= top_left_delta
;
597 window_expansion_bottom_right_delta_
= bottom_right_delta
;
598 SetBounds(bounds_without_expansion
);
601 void DesktopWindowTreeHostWin::OnWindowHidingAnimationCompleted() {
603 message_handler_
->Close();
606 ////////////////////////////////////////////////////////////////////////////////
607 // DesktopWindowTreeHostWin, HWNDMessageHandlerDelegate implementation:
609 bool DesktopWindowTreeHostWin::IsWidgetWindow() const {
610 return has_non_client_view_
;
613 bool DesktopWindowTreeHostWin::IsUsingCustomFrame() const {
614 return !GetWidget()->ShouldUseNativeFrame();
617 void DesktopWindowTreeHostWin::SchedulePaint() {
618 GetWidget()->GetRootView()->SchedulePaint();
621 void DesktopWindowTreeHostWin::EnableInactiveRendering() {
622 native_widget_delegate_
->EnableInactiveRendering();
625 bool DesktopWindowTreeHostWin::IsInactiveRenderingDisabled() {
626 return native_widget_delegate_
->IsInactiveRenderingDisabled();
629 bool DesktopWindowTreeHostWin::CanResize() const {
630 return GetWidget()->widget_delegate()->CanResize();
633 bool DesktopWindowTreeHostWin::CanMaximize() const {
634 return GetWidget()->widget_delegate()->CanMaximize();
637 bool DesktopWindowTreeHostWin::CanActivate() const {
638 if (IsModalWindowActive())
640 return native_widget_delegate_
->CanActivate();
643 bool DesktopWindowTreeHostWin::WidgetSizeIsClientSize() const {
644 const Widget
* widget
= GetWidget()->GetTopLevelWidget();
645 return IsMaximized() || (widget
&& widget
->ShouldUseNativeFrame());
648 bool DesktopWindowTreeHostWin::CanSaveFocus() const {
649 return GetWidget()->is_top_level();
652 void DesktopWindowTreeHostWin::SaveFocusOnDeactivate() {
653 GetWidget()->GetFocusManager()->StoreFocusedView(true);
656 void DesktopWindowTreeHostWin::RestoreFocusOnActivate() {
657 RestoreFocusOnEnable();
660 void DesktopWindowTreeHostWin::RestoreFocusOnEnable() {
661 GetWidget()->GetFocusManager()->RestoreFocusedView();
664 bool DesktopWindowTreeHostWin::IsModal() const {
665 return native_widget_delegate_
->IsModal();
668 int DesktopWindowTreeHostWin::GetInitialShowState() const {
669 return SW_SHOWNORMAL
;
672 bool DesktopWindowTreeHostWin::WillProcessWorkAreaChange() const {
673 return GetWidget()->widget_delegate()->WillProcessWorkAreaChange();
676 int DesktopWindowTreeHostWin::GetNonClientComponent(
677 const gfx::Point
& point
) const {
678 gfx::Point dip_position
= gfx::win::ScreenToDIPPoint(point
);
679 return native_widget_delegate_
->GetNonClientComponent(dip_position
);
682 void DesktopWindowTreeHostWin::GetWindowMask(const gfx::Size
& size
,
684 if (GetWidget()->non_client_view()) {
685 GetWidget()->non_client_view()->GetWindowMask(size
, path
);
686 } else if (!window_enlargement_
.IsZero()) {
687 gfx::Rect
bounds(WidgetSizeIsClientSize()
688 ? message_handler_
->GetClientAreaBoundsInScreen()
689 : message_handler_
->GetWindowBoundsInScreen());
690 InsetBottomRight(&bounds
, window_enlargement_
);
691 path
->addRect(SkRect::MakeXYWH(0, 0, bounds
.width(), bounds
.height()));
695 bool DesktopWindowTreeHostWin::GetClientAreaInsets(gfx::Insets
* insets
) const {
699 void DesktopWindowTreeHostWin::GetMinMaxSize(gfx::Size
* min_size
,
700 gfx::Size
* max_size
) const {
701 *min_size
= native_widget_delegate_
->GetMinimumSize();
702 *max_size
= native_widget_delegate_
->GetMaximumSize();
705 gfx::Size
DesktopWindowTreeHostWin::GetRootViewSize() const {
706 return GetWidget()->GetRootView()->size();
709 void DesktopWindowTreeHostWin::ResetWindowControls() {
710 GetWidget()->non_client_view()->ResetWindowControls();
713 void DesktopWindowTreeHostWin::PaintLayeredWindow(gfx::Canvas
* canvas
) {
714 GetWidget()->GetRootView()->Paint(canvas
);
717 gfx::NativeViewAccessible
DesktopWindowTreeHostWin::GetNativeViewAccessible() {
718 return GetWidget()->GetRootView()->GetNativeViewAccessible();
721 InputMethod
* DesktopWindowTreeHostWin::GetInputMethod() {
722 return GetWidget()->GetInputMethodDirect();
725 bool DesktopWindowTreeHostWin::ShouldHandleSystemCommands() const {
726 return GetWidget()->widget_delegate()->ShouldHandleSystemCommands();
729 void DesktopWindowTreeHostWin::HandleAppDeactivated() {
730 native_widget_delegate_
->EnableInactiveRendering();
733 void DesktopWindowTreeHostWin::HandleActivationChanged(bool active
) {
734 // This can be invoked from HWNDMessageHandler::Init(), at which point we're
735 // not in a good state and need to ignore it.
740 delegate_
->OnHostActivated();
741 desktop_native_widget_aura_
->HandleActivationChanged(active
);
744 bool DesktopWindowTreeHostWin::HandleAppCommand(short command
) {
745 // We treat APPCOMMAND ids as an extension of our command namespace, and just
746 // let the delegate figure out what to do...
747 return GetWidget()->widget_delegate() &&
748 GetWidget()->widget_delegate()->ExecuteWindowsCommand(command
);
751 void DesktopWindowTreeHostWin::HandleCancelMode() {
752 delegate_
->OnHostCancelMode();
755 void DesktopWindowTreeHostWin::HandleCaptureLost() {
756 delegate_
->OnHostLostWindowCapture();
757 native_widget_delegate_
->OnMouseCaptureLost();
760 void DesktopWindowTreeHostWin::HandleClose() {
761 GetWidget()->Close();
764 bool DesktopWindowTreeHostWin::HandleCommand(int command
) {
765 return GetWidget()->widget_delegate()->ExecuteWindowsCommand(command
);
768 void DesktopWindowTreeHostWin::HandleAccelerator(
769 const ui::Accelerator
& accelerator
) {
770 GetWidget()->GetFocusManager()->ProcessAccelerator(accelerator
);
773 void DesktopWindowTreeHostWin::HandleCreate() {
777 native_widget_delegate_
->OnNativeWidgetCreated(true);
779 // 1. Window property association
783 void DesktopWindowTreeHostWin::HandleDestroying() {
784 drag_drop_client_
->OnNativeWidgetDestroying(GetHWND());
785 native_widget_delegate_
->OnNativeWidgetDestroying();
787 // Destroy the compositor before destroying the HWND since shutdown
788 // may try to swap to the window.
792 void DesktopWindowTreeHostWin::HandleDestroyed() {
793 desktop_native_widget_aura_
->OnHostClosed();
796 bool DesktopWindowTreeHostWin::HandleInitialFocus(
797 ui::WindowShowState show_state
) {
798 return GetWidget()->SetInitialFocus(show_state
);
801 void DesktopWindowTreeHostWin::HandleDisplayChange() {
802 GetWidget()->widget_delegate()->OnDisplayChanged();
805 void DesktopWindowTreeHostWin::HandleBeginWMSizeMove() {
806 native_widget_delegate_
->OnNativeWidgetBeginUserBoundsChange();
809 void DesktopWindowTreeHostWin::HandleEndWMSizeMove() {
810 native_widget_delegate_
->OnNativeWidgetEndUserBoundsChange();
813 void DesktopWindowTreeHostWin::HandleMove() {
814 native_widget_delegate_
->OnNativeWidgetMove();
816 delegate_
->OnHostMoved(GetBounds().origin());
819 void DesktopWindowTreeHostWin::HandleWorkAreaChanged() {
820 GetWidget()->widget_delegate()->OnWorkAreaChanged();
823 void DesktopWindowTreeHostWin::HandleVisibilityChanging(bool visible
) {
824 native_widget_delegate_
->OnNativeWidgetVisibilityChanging(visible
);
827 void DesktopWindowTreeHostWin::HandleVisibilityChanged(bool visible
) {
828 native_widget_delegate_
->OnNativeWidgetVisibilityChanged(visible
);
831 void DesktopWindowTreeHostWin::HandleClientSizeChanged(
832 const gfx::Size
& new_size
) {
834 NotifyHostResized(new_size
);
837 void DesktopWindowTreeHostWin::HandleFrameChanged() {
838 SetWindowTransparency();
839 // Replace the frame and layout the contents.
840 GetWidget()->non_client_view()->UpdateFrame();
843 void DesktopWindowTreeHostWin::HandleNativeFocus(HWND last_focused_window
) {
844 // TODO(beng): inform the native_widget_delegate_.
845 InputMethod
* input_method
= GetInputMethod();
847 input_method
->OnFocus();
850 void DesktopWindowTreeHostWin::HandleNativeBlur(HWND focused_window
) {
851 // TODO(beng): inform the native_widget_delegate_.
852 InputMethod
* input_method
= GetInputMethod();
854 input_method
->OnBlur();
857 bool DesktopWindowTreeHostWin::HandleMouseEvent(const ui::MouseEvent
& event
) {
858 SendEventToProcessor(const_cast<ui::MouseEvent
*>(&event
));
859 return event
.handled();
862 bool DesktopWindowTreeHostWin::HandleKeyEvent(const ui::KeyEvent
& event
) {
866 bool DesktopWindowTreeHostWin::HandleUntranslatedKeyEvent(
867 const ui::KeyEvent
& event
) {
868 ui::KeyEvent
duplicate_event(event
);
869 SendEventToProcessor(&duplicate_event
);
870 return duplicate_event
.handled();
873 void DesktopWindowTreeHostWin::HandleTouchEvent(
874 const ui::TouchEvent
& event
) {
875 // HWNDMessageHandler asynchronously processes touch events. Because of this
876 // it's possible for the aura::RootWindow to have been destroyed by the time
877 // we attempt to process them.
878 if (!GetWidget()->GetNativeView())
881 // Currently we assume the window that has capture gets touch events too.
882 aura::RootWindow
* root
=
883 aura::RootWindow::GetForAcceleratedWidget(GetCapture());
885 DesktopWindowTreeHostWin
* target
=
886 root
->window()->GetProperty(kDesktopWindowTreeHostKey
);
887 if (target
&& target
->HasCapture() && target
!= this) {
888 POINT
target_location(event
.location().ToPOINT());
889 ClientToScreen(GetHWND(), &target_location
);
890 ScreenToClient(target
->GetHWND(), &target_location
);
891 ui::TouchEvent
target_event(event
, static_cast<View
*>(NULL
),
892 static_cast<View
*>(NULL
));
893 target_event
.set_location(gfx::Point(target_location
));
894 target_event
.set_root_location(target_event
.location());
895 target
->SendEventToProcessor(&target_event
);
899 SendEventToProcessor(const_cast<ui::TouchEvent
*>(&event
));
902 bool DesktopWindowTreeHostWin::HandleIMEMessage(UINT message
,
907 msg
.hwnd
= GetHWND();
908 msg
.message
= message
;
909 msg
.wParam
= w_param
;
910 msg
.lParam
= l_param
;
911 return desktop_native_widget_aura_
->input_method_event_filter()->
912 input_method()->OnUntranslatedIMEMessage(msg
, result
);
915 void DesktopWindowTreeHostWin::HandleInputLanguageChange(
917 HKL input_language_id
) {
918 desktop_native_widget_aura_
->input_method_event_filter()->
919 input_method()->OnInputLocaleChanged();
922 bool DesktopWindowTreeHostWin::HandlePaintAccelerated(
923 const gfx::Rect
& invalid_rect
) {
924 return native_widget_delegate_
->OnNativeWidgetPaintAccelerated(invalid_rect
);
927 void DesktopWindowTreeHostWin::HandlePaint(gfx::Canvas
* canvas
) {
928 compositor()->ScheduleRedrawRect(gfx::Rect());
931 bool DesktopWindowTreeHostWin::HandleTooltipNotify(int w_param
,
934 return tooltip_
&& tooltip_
->HandleNotify(w_param
, l_param
, l_result
);
937 void DesktopWindowTreeHostWin::HandleTooltipMouseMove(UINT message
,
940 // TooltipWin implementation doesn't need this.
941 // TODO(sky): remove from HWNDMessageHandler once non-aura path nuked.
944 void DesktopWindowTreeHostWin::HandleMenuLoop(bool in_menu_loop
) {
946 tooltip_disabler_
.reset(
947 new aura::client::ScopedTooltipDisabler(root_window_
->window()));
949 tooltip_disabler_
.reset();
953 bool DesktopWindowTreeHostWin::PreHandleMSG(UINT message
,
960 void DesktopWindowTreeHostWin::PostHandleMSG(UINT message
,
965 bool DesktopWindowTreeHostWin::HandleScrollEvent(
966 const ui::ScrollEvent
& event
) {
967 SendEventToProcessor(const_cast<ui::ScrollEvent
*>(&event
));
968 return event
.handled();
971 ////////////////////////////////////////////////////////////////////////////////
972 // DesktopWindowTreeHostWin, private:
974 Widget
* DesktopWindowTreeHostWin::GetWidget() {
975 return native_widget_delegate_
->AsWidget();
978 const Widget
* DesktopWindowTreeHostWin::GetWidget() const {
979 return native_widget_delegate_
->AsWidget();
982 HWND
DesktopWindowTreeHostWin::GetHWND() const {
983 return message_handler_
->hwnd();
986 void DesktopWindowTreeHostWin::SetWindowTransparency() {
987 bool transparent
= ShouldUseNativeFrame() && !IsFullscreen();
988 root_window_
->host()->compositor()->SetHostHasTransparentBackground(
990 root_window_
->window()->SetTransparent(transparent
);
991 content_window_
->SetTransparent(transparent
);
994 bool DesktopWindowTreeHostWin::IsModalWindowActive() const {
995 // This function can get called during window creation which occurs before
996 // root_window_ has been created.
1000 aura::Window::Windows::const_iterator index
;
1001 for (index
= root_window_
->window()->children().begin();
1002 index
!= root_window_
->window()->children().end();
1004 if ((*index
)->GetProperty(aura::client::kModalKey
) !=
1005 ui:: MODAL_TYPE_NONE
&& (*index
)->TargetVisibility())
1011 ////////////////////////////////////////////////////////////////////////////////
1012 // DesktopWindowTreeHost, public:
1015 DesktopWindowTreeHost
* DesktopWindowTreeHost::Create(
1016 internal::NativeWidgetDelegate
* native_widget_delegate
,
1017 DesktopNativeWidgetAura
* desktop_native_widget_aura
) {
1018 return new DesktopWindowTreeHostWin(native_widget_delegate
,
1019 desktop_native_widget_aura
);
1022 } // namespace views