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_window_tree_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/window_event_dispatcher.h"
14 #include "ui/aura/window_property.h"
15 #include "ui/base/cursor/cursor_loader_win.h"
16 #include "ui/base/ime/input_method.h"
17 #include "ui/base/win/shell.h"
18 #include "ui/compositor/compositor_constants.h"
19 #include "ui/gfx/insets.h"
20 #include "ui/gfx/native_widget_types.h"
21 #include "ui/gfx/path.h"
22 #include "ui/gfx/path_win.h"
23 #include "ui/gfx/vector2d.h"
24 #include "ui/gfx/win/dpi.h"
25 #include "ui/native_theme/native_theme_aura.h"
26 #include "ui/native_theme/native_theme_win.h"
27 #include "ui/views/corewm/tooltip_win.h"
28 #include "ui/views/ime/input_method_bridge.h"
29 #include "ui/views/widget/desktop_aura/desktop_cursor_loader_updater.h"
30 #include "ui/views/widget/desktop_aura/desktop_drag_drop_client_win.h"
31 #include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h"
32 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
33 #include "ui/views/widget/root_view.h"
34 #include "ui/views/widget/widget_delegate.h"
35 #include "ui/views/widget/widget_hwnd_utils.h"
36 #include "ui/views/win/fullscreen_handler.h"
37 #include "ui/views/win/hwnd_message_handler.h"
38 #include "ui/wm/core/compound_event_filter.h"
39 #include "ui/wm/core/input_method_event_filter.h"
40 #include "ui/wm/core/window_animations.h"
41 #include "ui/wm/public/scoped_tooltip_disabler.h"
47 gfx::Size GetExpandedWindowSize(DWORD window_style, gfx::Size size) {
48 if (!(window_style & WS_EX_COMPOSITED) || !ui::win::IsAeroGlassEnabled())
51 // Some AMD drivers can't display windows that are less than 64x64 pixels,
52 // so expand them to be at least that size. http://crbug.com/286609
53 gfx::Size expanded(std::max(size.width(), 64), std::max(size.height(), 64));
57 void InsetBottomRight(gfx::Rect* rect, gfx::Vector2d vector) {
58 rect->Inset(0, 0, vector.x(), vector.y());
63 DEFINE_WINDOW_PROPERTY_KEY(aura::Window*, kContentWindowForRootWindow, NULL);
65 // Identifies the DesktopWindowTreeHostWin associated with the
66 // WindowEventDispatcher.
67 DEFINE_WINDOW_PROPERTY_KEY(DesktopWindowTreeHostWin*, kDesktopWindowTreeHostKey,
70 ////////////////////////////////////////////////////////////////////////////////
71 // DesktopWindowTreeHostWin, public:
73 bool DesktopWindowTreeHostWin::is_cursor_visible_ = true;
75 DesktopWindowTreeHostWin::DesktopWindowTreeHostWin(
76 internal::NativeWidgetDelegate* native_widget_delegate,
77 DesktopNativeWidgetAura* desktop_native_widget_aura)
78 : message_handler_(new HWNDMessageHandler(this)),
79 native_widget_delegate_(native_widget_delegate),
80 desktop_native_widget_aura_(desktop_native_widget_aura),
81 content_window_(NULL),
82 drag_drop_client_(NULL),
83 should_animate_window_close_(false),
84 pending_close_(false),
85 has_non_client_view_(false),
87 need_synchronous_paint_(false),
88 in_sizing_loop_(false) {
91 DesktopWindowTreeHostWin::~DesktopWindowTreeHostWin() {
92 // WARNING: |content_window_| has been destroyed by the time we get here.
93 desktop_native_widget_aura_->OnDesktopWindowTreeHostDestroyed(this);
98 aura::Window* DesktopWindowTreeHostWin::GetContentWindowForHWND(HWND hwnd) {
99 aura::WindowTreeHost* host =
100 aura::WindowTreeHost::GetForAcceleratedWidget(hwnd);
101 return host ? host->window()->GetProperty(kContentWindowForRootWindow) : NULL;
105 ui::NativeTheme* DesktopWindowTreeHost::GetNativeTheme(aura::Window* window) {
106 // Use NativeThemeWin for windows shown on the desktop, those not on the
107 // desktop come from Ash and get NativeThemeAura.
108 aura::WindowTreeHost* host = window ? window->GetHost() : NULL;
110 HWND host_hwnd = 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(aura::Window* content_window,
123 const Widget::InitParams& params) {
124 // TODO(beng): SetInitParams().
125 content_window_ = content_window;
127 aura::client::SetAnimationHost(content_window_, this);
129 ConfigureWindowStyles(message_handler_.get(), params,
130 GetWidget()->widget_delegate(),
131 native_widget_delegate_);
133 HWND parent_hwnd = NULL;
134 if (params.parent && params.parent->GetHost())
135 parent_hwnd = params.parent->GetHost()->GetAcceleratedWidget();
137 message_handler_->set_remove_standard_frame(params.remove_standard_frame);
139 has_non_client_view_ = Widget::RequiresNonClientView(params.type);
141 gfx::Rect pixel_bounds = gfx::win::DIPToScreenRect(params.bounds);
142 message_handler_->Init(parent_hwnd, pixel_bounds);
143 if (params.type == Widget::InitParams::TYPE_MENU) {
144 ::SetProp(GetAcceleratedWidget(),
145 kForceSoftwareCompositor,
146 reinterpret_cast<HANDLE>(true));
148 CreateCompositor(GetAcceleratedWidget());
151 void DesktopWindowTreeHostWin::OnNativeWidgetCreated(
152 const Widget::InitParams& params) {
153 // The cursor is not necessarily visible when the root window is created.
154 aura::client::CursorClient* cursor_client =
155 aura::client::GetCursorClient(window());
157 is_cursor_visible_ = cursor_client->IsCursorVisible();
159 window()->SetProperty(kContentWindowForRootWindow, content_window_);
160 window()->SetProperty(kDesktopWindowTreeHostKey, this);
162 should_animate_window_close_ =
163 content_window_->type() != ui::wm::WINDOW_TYPE_NORMAL &&
164 !wm::WindowAnimationsDisabled(content_window_);
166 // TODO this is not invoked *after* Init(), but should be ok.
167 SetWindowTransparency();
170 scoped_ptr<corewm::Tooltip> DesktopWindowTreeHostWin::CreateTooltip() {
172 tooltip_ = new corewm::TooltipWin(GetAcceleratedWidget());
173 return scoped_ptr<corewm::Tooltip>(tooltip_);
176 scoped_ptr<aura::client::DragDropClient>
177 DesktopWindowTreeHostWin::CreateDragDropClient(
178 DesktopNativeCursorManager* cursor_manager) {
179 drag_drop_client_ = new DesktopDragDropClientWin(window(), GetHWND());
180 return scoped_ptr<aura::client::DragDropClient>(drag_drop_client_).Pass();
183 void DesktopWindowTreeHostWin::Close() {
184 // TODO(beng): Move this entire branch to DNWA so it can be shared with X11.
185 if (should_animate_window_close_) {
186 pending_close_ = true;
187 const bool is_animating =
188 content_window_->layer()->GetAnimator()->IsAnimatingProperty(
189 ui::LayerAnimationElement::VISIBILITY);
190 // Animation may not start for a number of reasons.
192 message_handler_->Close();
193 // else case, OnWindowHidingAnimationCompleted does the actual Close.
195 message_handler_->Close();
199 void DesktopWindowTreeHostWin::CloseNow() {
200 message_handler_->CloseNow();
203 aura::WindowTreeHost* DesktopWindowTreeHostWin::AsWindowTreeHost() {
207 void DesktopWindowTreeHostWin::ShowWindowWithState(
208 ui::WindowShowState show_state) {
209 message_handler_->ShowWindowWithState(show_state);
212 void DesktopWindowTreeHostWin::ShowMaximizedWithBounds(
213 const gfx::Rect& restored_bounds) {
214 gfx::Rect pixel_bounds = gfx::win::DIPToScreenRect(restored_bounds);
215 message_handler_->ShowMaximizedWithBounds(pixel_bounds);
218 bool DesktopWindowTreeHostWin::IsVisible() const {
219 return message_handler_->IsVisible();
222 void DesktopWindowTreeHostWin::SetSize(const gfx::Size& size) {
223 gfx::Size size_in_pixels = gfx::win::DIPToScreenSize(size);
224 gfx::Size expanded = GetExpandedWindowSize(
225 message_handler_->window_ex_style(), size_in_pixels);
226 window_enlargement_ =
227 gfx::Vector2d(expanded.width() - size_in_pixels.width(),
228 expanded.height() - size_in_pixels.height());
229 message_handler_->SetSize(expanded);
232 void DesktopWindowTreeHostWin::StackAtTop() {
233 message_handler_->StackAtTop();
236 void DesktopWindowTreeHostWin::CenterWindow(const gfx::Size& size) {
237 gfx::Size size_in_pixels = gfx::win::DIPToScreenSize(size);
238 gfx::Size expanded_size;
239 expanded_size = GetExpandedWindowSize(message_handler_->window_ex_style(),
241 window_enlargement_ =
242 gfx::Vector2d(expanded_size.width() - size_in_pixels.width(),
243 expanded_size.height() - size_in_pixels.height());
244 message_handler_->CenterWindow(expanded_size);
247 void DesktopWindowTreeHostWin::GetWindowPlacement(
249 ui::WindowShowState* show_state) const {
250 message_handler_->GetWindowPlacement(bounds, show_state);
251 InsetBottomRight(bounds, window_enlargement_);
252 *bounds = gfx::win::ScreenToDIPRect(*bounds);
255 gfx::Rect DesktopWindowTreeHostWin::GetWindowBoundsInScreen() const {
256 gfx::Rect pixel_bounds = message_handler_->GetWindowBoundsInScreen();
257 InsetBottomRight(&pixel_bounds, window_enlargement_);
258 return gfx::win::ScreenToDIPRect(pixel_bounds);
261 gfx::Rect DesktopWindowTreeHostWin::GetClientAreaBoundsInScreen() const {
262 gfx::Rect pixel_bounds = message_handler_->GetClientAreaBoundsInScreen();
263 InsetBottomRight(&pixel_bounds, window_enlargement_);
264 return gfx::win::ScreenToDIPRect(pixel_bounds);
267 gfx::Rect DesktopWindowTreeHostWin::GetRestoredBounds() const {
268 gfx::Rect pixel_bounds = message_handler_->GetRestoredBounds();
269 InsetBottomRight(&pixel_bounds, window_enlargement_);
270 return gfx::win::ScreenToDIPRect(pixel_bounds);
273 gfx::Rect DesktopWindowTreeHostWin::GetWorkAreaBoundsInScreen() const {
274 MONITORINFO monitor_info;
275 monitor_info.cbSize = sizeof(monitor_info);
276 GetMonitorInfo(MonitorFromWindow(message_handler_->hwnd(),
277 MONITOR_DEFAULTTONEAREST),
279 gfx::Rect pixel_bounds = gfx::Rect(monitor_info.rcWork);
280 return gfx::win::ScreenToDIPRect(pixel_bounds);
283 void DesktopWindowTreeHostWin::SetShape(gfx::NativeRegion native_region) {
285 // TODO(wez): This would be a lot simpler if we were passed an SkPath.
286 // See crbug.com/410593.
287 gfx::NativeRegion shape = native_region;
288 SkRegion device_region;
289 if (gfx::GetDPIScale() > 1.0) {
290 shape = &device_region;
291 const float& scale = gfx::GetDPIScale();
292 std::vector<SkIRect> rects;
293 for (SkRegion::Iterator it(*native_region); !it.done(); it.next()) {
294 const SkIRect& rect = it.rect();
296 SkRect::MakeLTRB(rect.left() * scale, rect.top() * scale,
297 rect.right() * scale, rect.bottom() * scale);
298 SkIRect rounded_scaled_rect;
299 scaled_rect.roundOut(&rounded_scaled_rect);
300 rects.push_back(rounded_scaled_rect);
303 device_region.setRects(&rects[0], rects.size());
306 message_handler_->SetRegion(gfx::CreateHRGNFromSkRegion(*shape));
308 message_handler_->SetRegion(NULL);
311 delete native_region;
314 void DesktopWindowTreeHostWin::Activate() {
315 message_handler_->Activate();
318 void DesktopWindowTreeHostWin::Deactivate() {
319 message_handler_->Deactivate();
322 bool DesktopWindowTreeHostWin::IsActive() const {
323 return message_handler_->IsActive();
326 void DesktopWindowTreeHostWin::Maximize() {
327 message_handler_->Maximize();
330 void DesktopWindowTreeHostWin::Minimize() {
331 message_handler_->Minimize();
334 void DesktopWindowTreeHostWin::Restore() {
335 message_handler_->Restore();
338 bool DesktopWindowTreeHostWin::IsMaximized() const {
339 return message_handler_->IsMaximized();
342 bool DesktopWindowTreeHostWin::IsMinimized() const {
343 return message_handler_->IsMinimized();
346 bool DesktopWindowTreeHostWin::HasCapture() const {
347 return message_handler_->HasCapture();
350 void DesktopWindowTreeHostWin::SetAlwaysOnTop(bool always_on_top) {
351 message_handler_->SetAlwaysOnTop(always_on_top);
354 bool DesktopWindowTreeHostWin::IsAlwaysOnTop() const {
355 return message_handler_->IsAlwaysOnTop();
358 void DesktopWindowTreeHostWin::SetVisibleOnAllWorkspaces(bool always_visible) {
359 // Windows does not have the concept of workspaces.
362 bool DesktopWindowTreeHostWin::SetWindowTitle(const base::string16& title) {
363 return message_handler_->SetTitle(title);
366 void DesktopWindowTreeHostWin::ClearNativeFocus() {
367 message_handler_->ClearNativeFocus();
370 Widget::MoveLoopResult DesktopWindowTreeHostWin::RunMoveLoop(
371 const gfx::Vector2d& drag_offset,
372 Widget::MoveLoopSource source,
373 Widget::MoveLoopEscapeBehavior escape_behavior) {
374 const bool hide_on_escape =
375 escape_behavior == Widget::MOVE_LOOP_ESCAPE_BEHAVIOR_HIDE;
376 return message_handler_->RunMoveLoop(drag_offset, hide_on_escape) ?
377 Widget::MOVE_LOOP_SUCCESSFUL : Widget::MOVE_LOOP_CANCELED;
380 void DesktopWindowTreeHostWin::EndMoveLoop() {
381 message_handler_->EndMoveLoop();
384 void DesktopWindowTreeHostWin::SetVisibilityChangedAnimationsEnabled(
386 message_handler_->SetVisibilityChangedAnimationsEnabled(value);
387 content_window_->SetProperty(aura::client::kAnimationsDisabledKey, !value);
390 bool DesktopWindowTreeHostWin::ShouldUseNativeFrame() const {
391 return IsTranslucentWindowOpacitySupported();
394 bool DesktopWindowTreeHostWin::ShouldWindowContentsBeTransparent() const {
395 // If the window has a native frame, we assume it is an Aero Glass window, and
396 // is therefore transparent. Note: This is not equivalent to calling
397 // IsAeroGlassEnabled, because ShouldUseNativeFrame is overridden in a
399 return ShouldUseNativeFrame();
402 void DesktopWindowTreeHostWin::FrameTypeChanged() {
403 message_handler_->FrameTypeChanged();
404 SetWindowTransparency();
407 void DesktopWindowTreeHostWin::SetFullscreen(bool fullscreen) {
408 message_handler_->SetFullscreen(fullscreen);
409 // TODO(sky): workaround for ScopedFullscreenVisibility showing window
410 // directly. Instead of this should listen for visibility changes and then
412 if (message_handler_->IsVisible() && !content_window_->TargetVisibility())
413 content_window_->Show();
414 SetWindowTransparency();
417 bool DesktopWindowTreeHostWin::IsFullscreen() const {
418 return message_handler_->fullscreen_handler()->fullscreen();
421 void DesktopWindowTreeHostWin::SetOpacity(unsigned char opacity) {
422 message_handler_->SetOpacity(static_cast<BYTE>(opacity));
423 content_window_->layer()->SetOpacity(opacity / 255.0);
426 void DesktopWindowTreeHostWin::SetWindowIcons(
427 const gfx::ImageSkia& window_icon, const gfx::ImageSkia& app_icon) {
428 message_handler_->SetWindowIcons(window_icon, app_icon);
431 void DesktopWindowTreeHostWin::InitModalType(ui::ModalType modal_type) {
432 message_handler_->InitModalType(modal_type);
435 void DesktopWindowTreeHostWin::FlashFrame(bool flash_frame) {
436 message_handler_->FlashFrame(flash_frame);
439 void DesktopWindowTreeHostWin::OnRootViewLayout() {
442 void DesktopWindowTreeHostWin::OnNativeWidgetFocus() {
443 // HWNDMessageHandler will perform the proper updating on its own.
446 void DesktopWindowTreeHostWin::OnNativeWidgetBlur() {
449 bool DesktopWindowTreeHostWin::IsAnimatingClosed() const {
450 return pending_close_;
453 bool DesktopWindowTreeHostWin::IsTranslucentWindowOpacitySupported() const {
454 return ui::win::IsAeroGlassEnabled();
457 void DesktopWindowTreeHostWin::SizeConstraintsChanged() {
458 message_handler_->SizeConstraintsChanged();
461 ////////////////////////////////////////////////////////////////////////////////
462 // DesktopWindowTreeHostWin, WindowTreeHost implementation:
464 ui::EventSource* DesktopWindowTreeHostWin::GetEventSource() {
468 gfx::AcceleratedWidget DesktopWindowTreeHostWin::GetAcceleratedWidget() {
469 return message_handler_->hwnd();
472 void DesktopWindowTreeHostWin::Show() {
473 message_handler_->Show();
476 void DesktopWindowTreeHostWin::Hide() {
478 message_handler_->Hide();
481 // GetBounds and SetBounds work in pixel coordinates, whereas other get/set
482 // methods work in DIP.
484 gfx::Rect DesktopWindowTreeHostWin::GetBounds() const {
485 gfx::Rect bounds(message_handler_->GetClientAreaBounds());
486 // If the window bounds were expanded we need to return the original bounds
487 // To achieve this we do the reverse of the expansion, i.e. add the
488 // window_expansion_top_left_delta_ to the origin and subtract the
489 // window_expansion_bottom_right_delta_ from the width and height.
490 gfx::Rect without_expansion(
491 bounds.x() + window_expansion_top_left_delta_.x(),
492 bounds.y() + window_expansion_top_left_delta_.y(),
493 bounds.width() - window_expansion_bottom_right_delta_.x() -
494 window_enlargement_.x(),
495 bounds.height() - window_expansion_bottom_right_delta_.y() -
496 window_enlargement_.y());
497 return without_expansion;
500 void DesktopWindowTreeHostWin::SetBounds(const gfx::Rect& bounds) {
501 // If the window bounds have to be expanded we need to subtract the
502 // window_expansion_top_left_delta_ from the origin and add the
503 // window_expansion_bottom_right_delta_ to the width and height
504 gfx::Size old_hwnd_size(message_handler_->GetClientAreaBounds().size());
505 gfx::Size old_content_size = GetBounds().size();
508 bounds.x() - window_expansion_top_left_delta_.x(),
509 bounds.y() - window_expansion_top_left_delta_.y(),
510 bounds.width() + window_expansion_bottom_right_delta_.x(),
511 bounds.height() + window_expansion_bottom_right_delta_.y());
513 gfx::Rect new_expanded(
515 GetExpandedWindowSize(message_handler_->window_ex_style(),
517 window_enlargement_ =
518 gfx::Vector2d(new_expanded.width() - expanded.width(),
519 new_expanded.height() - expanded.height());
520 message_handler_->SetBounds(new_expanded, old_content_size != bounds.size());
523 gfx::Point DesktopWindowTreeHostWin::GetLocationOnNativeScreen() const {
524 return GetBounds().origin();
527 void DesktopWindowTreeHostWin::SetCapture() {
528 message_handler_->SetCapture();
531 void DesktopWindowTreeHostWin::ReleaseCapture() {
532 message_handler_->ReleaseCapture();
535 void DesktopWindowTreeHostWin::SetCursorNative(gfx::NativeCursor cursor) {
536 ui::CursorLoaderWin cursor_loader;
537 cursor_loader.SetPlatformCursor(&cursor);
539 message_handler_->SetCursor(cursor.platform());
542 void DesktopWindowTreeHostWin::OnCursorVisibilityChangedNative(bool show) {
543 if (is_cursor_visible_ == show)
545 is_cursor_visible_ = show;
546 ::ShowCursor(!!show);
549 void DesktopWindowTreeHostWin::MoveCursorToNative(const gfx::Point& location) {
550 POINT cursor_location = location.ToPOINT();
551 ::ClientToScreen(GetHWND(), &cursor_location);
552 ::SetCursorPos(cursor_location.x, cursor_location.y);
555 ////////////////////////////////////////////////////////////////////////////////
556 // DesktopWindowTreeHostWin, ui::EventSource implementation:
558 ui::EventProcessor* DesktopWindowTreeHostWin::GetEventProcessor() {
562 ////////////////////////////////////////////////////////////////////////////////
563 // DesktopWindowTreeHostWin, aura::AnimationHost implementation:
565 void DesktopWindowTreeHostWin::SetHostTransitionOffsets(
566 const gfx::Vector2d& top_left_delta,
567 const gfx::Vector2d& bottom_right_delta) {
568 gfx::Rect bounds_without_expansion = GetBounds();
569 window_expansion_top_left_delta_ = top_left_delta;
570 window_expansion_bottom_right_delta_ = bottom_right_delta;
571 SetBounds(bounds_without_expansion);
574 void DesktopWindowTreeHostWin::OnWindowHidingAnimationCompleted() {
576 message_handler_->Close();
579 ////////////////////////////////////////////////////////////////////////////////
580 // DesktopWindowTreeHostWin, HWNDMessageHandlerDelegate implementation:
582 bool DesktopWindowTreeHostWin::IsWidgetWindow() const {
583 return has_non_client_view_;
586 bool DesktopWindowTreeHostWin::IsUsingCustomFrame() const {
587 return !GetWidget()->ShouldUseNativeFrame();
590 void DesktopWindowTreeHostWin::SchedulePaint() {
591 GetWidget()->GetRootView()->SchedulePaint();
594 void DesktopWindowTreeHostWin::EnableInactiveRendering() {
595 native_widget_delegate_->EnableInactiveRendering();
598 bool DesktopWindowTreeHostWin::IsInactiveRenderingDisabled() {
599 return native_widget_delegate_->IsInactiveRenderingDisabled();
602 bool DesktopWindowTreeHostWin::CanResize() const {
603 return GetWidget()->widget_delegate()->CanResize();
606 bool DesktopWindowTreeHostWin::CanMaximize() const {
607 return GetWidget()->widget_delegate()->CanMaximize();
610 bool DesktopWindowTreeHostWin::CanMinimize() const {
611 return GetWidget()->widget_delegate()->CanMinimize();
614 bool DesktopWindowTreeHostWin::CanActivate() const {
615 if (IsModalWindowActive())
617 return native_widget_delegate_->CanActivate();
620 bool DesktopWindowTreeHostWin::WidgetSizeIsClientSize() const {
621 const Widget* widget = GetWidget()->GetTopLevelWidget();
622 return IsMaximized() || (widget && widget->ShouldUseNativeFrame());
625 bool DesktopWindowTreeHostWin::IsModal() const {
626 return native_widget_delegate_->IsModal();
629 int DesktopWindowTreeHostWin::GetInitialShowState() const {
630 return CanActivate() ? SW_SHOWNORMAL : SW_SHOWNOACTIVATE;
633 bool DesktopWindowTreeHostWin::WillProcessWorkAreaChange() const {
634 return GetWidget()->widget_delegate()->WillProcessWorkAreaChange();
637 int DesktopWindowTreeHostWin::GetNonClientComponent(
638 const gfx::Point& point) const {
639 gfx::Point dip_position = gfx::win::ScreenToDIPPoint(point);
640 return native_widget_delegate_->GetNonClientComponent(dip_position);
643 void DesktopWindowTreeHostWin::GetWindowMask(const gfx::Size& size,
645 if (GetWidget()->non_client_view()) {
646 GetWidget()->non_client_view()->GetWindowMask(size, path);
647 } else if (!window_enlargement_.IsZero()) {
648 gfx::Rect bounds(WidgetSizeIsClientSize()
649 ? message_handler_->GetClientAreaBoundsInScreen()
650 : message_handler_->GetWindowBoundsInScreen());
651 InsetBottomRight(&bounds, window_enlargement_);
652 path->addRect(SkRect::MakeXYWH(0, 0, bounds.width(), bounds.height()));
656 bool DesktopWindowTreeHostWin::GetClientAreaInsets(gfx::Insets* insets) const {
660 void DesktopWindowTreeHostWin::GetMinMaxSize(gfx::Size* min_size,
661 gfx::Size* max_size) const {
662 *min_size = native_widget_delegate_->GetMinimumSize();
663 *max_size = native_widget_delegate_->GetMaximumSize();
666 gfx::Size DesktopWindowTreeHostWin::GetRootViewSize() const {
667 return GetWidget()->GetRootView()->size();
670 void DesktopWindowTreeHostWin::ResetWindowControls() {
671 GetWidget()->non_client_view()->ResetWindowControls();
674 void DesktopWindowTreeHostWin::PaintLayeredWindow(gfx::Canvas* canvas) {
675 GetWidget()->GetRootView()->Paint(canvas, views::CullSet());
678 gfx::NativeViewAccessible DesktopWindowTreeHostWin::GetNativeViewAccessible() {
679 return GetWidget()->GetRootView()->GetNativeViewAccessible();
682 InputMethod* DesktopWindowTreeHostWin::GetInputMethod() {
683 return GetWidget()->GetInputMethodDirect();
686 bool DesktopWindowTreeHostWin::ShouldHandleSystemCommands() const {
687 return GetWidget()->widget_delegate()->ShouldHandleSystemCommands();
690 void DesktopWindowTreeHostWin::HandleAppDeactivated() {
691 native_widget_delegate_->EnableInactiveRendering();
694 void DesktopWindowTreeHostWin::HandleActivationChanged(bool active) {
695 // This can be invoked from HWNDMessageHandler::Init(), at which point we're
696 // not in a good state and need to ignore it.
697 // TODO(beng): Do we need this still now the host owns the dispatcher?
703 desktop_native_widget_aura_->HandleActivationChanged(active);
706 bool DesktopWindowTreeHostWin::HandleAppCommand(short command) {
707 // We treat APPCOMMAND ids as an extension of our command namespace, and just
708 // let the delegate figure out what to do...
709 return GetWidget()->widget_delegate() &&
710 GetWidget()->widget_delegate()->ExecuteWindowsCommand(command);
713 void DesktopWindowTreeHostWin::HandleCancelMode() {
714 dispatcher()->DispatchCancelModeEvent();
717 void DesktopWindowTreeHostWin::HandleCaptureLost() {
718 OnHostLostWindowCapture();
721 void DesktopWindowTreeHostWin::HandleClose() {
722 GetWidget()->Close();
725 bool DesktopWindowTreeHostWin::HandleCommand(int command) {
726 // Windows uses the 4 lower order bits of |notification_code| for type-
727 // specific information so we must exclude this when comparing.
728 static const int sc_mask = 0xFFF0;
729 switch (command & sc_mask) {
732 need_synchronous_paint_ = true;
736 in_sizing_loop_ = true;
742 return GetWidget()->widget_delegate()->ExecuteWindowsCommand(command);
745 void DesktopWindowTreeHostWin::HandleAccelerator(
746 const ui::Accelerator& accelerator) {
747 GetWidget()->GetFocusManager()->ProcessAccelerator(accelerator);
750 void DesktopWindowTreeHostWin::HandleCreate() {
751 native_widget_delegate_->OnNativeWidgetCreated(true);
754 void DesktopWindowTreeHostWin::HandleDestroying() {
755 drag_drop_client_->OnNativeWidgetDestroying(GetHWND());
756 native_widget_delegate_->OnNativeWidgetDestroying();
758 // Destroy the compositor before destroying the HWND since shutdown
759 // may try to swap to the window.
763 void DesktopWindowTreeHostWin::HandleDestroyed() {
764 desktop_native_widget_aura_->OnHostClosed();
767 bool DesktopWindowTreeHostWin::HandleInitialFocus(
768 ui::WindowShowState show_state) {
769 return GetWidget()->SetInitialFocus(show_state);
772 void DesktopWindowTreeHostWin::HandleDisplayChange() {
773 GetWidget()->widget_delegate()->OnDisplayChanged();
776 void DesktopWindowTreeHostWin::HandleBeginWMSizeMove() {
778 need_synchronous_paint_ = true;
779 native_widget_delegate_->OnNativeWidgetBeginUserBoundsChange();
782 void DesktopWindowTreeHostWin::HandleEndWMSizeMove() {
783 if (in_sizing_loop_) {
784 need_synchronous_paint_ = false;
785 in_sizing_loop_ = false;
787 native_widget_delegate_->OnNativeWidgetEndUserBoundsChange();
790 void DesktopWindowTreeHostWin::HandleMove() {
791 native_widget_delegate_->OnNativeWidgetMove();
792 OnHostMoved(GetBounds().origin());
795 void DesktopWindowTreeHostWin::HandleWorkAreaChanged() {
796 GetWidget()->widget_delegate()->OnWorkAreaChanged();
799 void DesktopWindowTreeHostWin::HandleVisibilityChanging(bool visible) {
800 native_widget_delegate_->OnNativeWidgetVisibilityChanging(visible);
803 void DesktopWindowTreeHostWin::HandleVisibilityChanged(bool visible) {
804 native_widget_delegate_->OnNativeWidgetVisibilityChanged(visible);
807 void DesktopWindowTreeHostWin::HandleClientSizeChanged(
808 const gfx::Size& new_size) {
810 OnHostResized(new_size);
813 void DesktopWindowTreeHostWin::HandleFrameChanged() {
814 SetWindowTransparency();
815 // Replace the frame and layout the contents.
816 GetWidget()->non_client_view()->UpdateFrame();
819 void DesktopWindowTreeHostWin::HandleNativeFocus(HWND last_focused_window) {
820 // TODO(beng): inform the native_widget_delegate_.
821 InputMethod* input_method = GetInputMethod();
823 input_method->OnFocus();
826 void DesktopWindowTreeHostWin::HandleNativeBlur(HWND focused_window) {
827 // TODO(beng): inform the native_widget_delegate_.
828 InputMethod* input_method = GetInputMethod();
830 input_method->OnBlur();
833 bool DesktopWindowTreeHostWin::HandleMouseEvent(const ui::MouseEvent& event) {
834 SendEventToProcessor(const_cast<ui::MouseEvent*>(&event));
835 return event.handled();
838 bool DesktopWindowTreeHostWin::HandleKeyEvent(const ui::KeyEvent& event) {
842 bool DesktopWindowTreeHostWin::HandleUntranslatedKeyEvent(
843 const ui::KeyEvent& event) {
844 ui::KeyEvent duplicate_event(event);
845 SendEventToProcessor(&duplicate_event);
846 return duplicate_event.handled();
849 void DesktopWindowTreeHostWin::HandleTouchEvent(
850 const ui::TouchEvent& event) {
851 // HWNDMessageHandler asynchronously processes touch events. Because of this
852 // it's possible for the aura::WindowEventDispatcher to have been destroyed
853 // by the time we attempt to process them.
854 if (!GetWidget()->GetNativeView())
857 // Currently we assume the window that has capture gets touch events too.
858 aura::WindowTreeHost* host =
859 aura::WindowTreeHost::GetForAcceleratedWidget(GetCapture());
861 DesktopWindowTreeHostWin* target =
862 host->window()->GetProperty(kDesktopWindowTreeHostKey);
863 if (target && target->HasCapture() && target != this) {
864 POINT target_location(event.location().ToPOINT());
865 ClientToScreen(GetHWND(), &target_location);
866 ScreenToClient(target->GetHWND(), &target_location);
867 ui::TouchEvent target_event(event, static_cast<View*>(NULL),
868 static_cast<View*>(NULL));
869 target_event.set_location(gfx::Point(target_location));
870 target_event.set_root_location(target_event.location());
871 target->SendEventToProcessor(&target_event);
875 SendEventToProcessor(const_cast<ui::TouchEvent*>(&event));
878 bool DesktopWindowTreeHostWin::HandleIMEMessage(UINT message,
883 msg.hwnd = GetHWND();
884 msg.message = message;
885 msg.wParam = w_param;
886 msg.lParam = l_param;
887 return desktop_native_widget_aura_->input_method_event_filter()->
888 input_method()->OnUntranslatedIMEMessage(msg, result);
891 void DesktopWindowTreeHostWin::HandleInputLanguageChange(
893 HKL input_language_id) {
894 desktop_native_widget_aura_->input_method_event_filter()->
895 input_method()->OnInputLocaleChanged();
898 bool DesktopWindowTreeHostWin::HandlePaintAccelerated(
899 const gfx::Rect& invalid_rect) {
900 return native_widget_delegate_->OnNativeWidgetPaintAccelerated(invalid_rect);
903 void DesktopWindowTreeHostWin::HandlePaint(gfx::Canvas* canvas) {
904 // It appears possible to get WM_PAINT after WM_DESTROY.
906 compositor()->ScheduleRedrawRect(gfx::Rect());
909 bool DesktopWindowTreeHostWin::HandleTooltipNotify(int w_param,
912 return tooltip_ && tooltip_->HandleNotify(w_param, l_param, l_result);
915 void DesktopWindowTreeHostWin::HandleMenuLoop(bool in_menu_loop) {
917 tooltip_disabler_.reset(
918 new aura::client::ScopedTooltipDisabler(window()));
920 tooltip_disabler_.reset();
924 bool DesktopWindowTreeHostWin::PreHandleMSG(UINT message,
931 void DesktopWindowTreeHostWin::PostHandleMSG(UINT message,
936 bool DesktopWindowTreeHostWin::HandleScrollEvent(
937 const ui::ScrollEvent& event) {
938 SendEventToProcessor(const_cast<ui::ScrollEvent*>(&event));
939 return event.handled();
942 void DesktopWindowTreeHostWin::HandleWindowSizeChanging() {
943 if (compositor() && need_synchronous_paint_) {
944 compositor()->FinishAllRendering();
945 // If we received the window size changing notification due to a restore or
946 // maximize operation, then we can reset the need_synchronous_paint_ flag
947 // here. For a sizing operation, the flag will be reset at the end of the
949 if (!in_sizing_loop_)
950 need_synchronous_paint_ = false;
954 ////////////////////////////////////////////////////////////////////////////////
955 // DesktopWindowTreeHostWin, private:
957 Widget* DesktopWindowTreeHostWin::GetWidget() {
958 return native_widget_delegate_->AsWidget();
961 const Widget* DesktopWindowTreeHostWin::GetWidget() const {
962 return native_widget_delegate_->AsWidget();
965 HWND DesktopWindowTreeHostWin::GetHWND() const {
966 return message_handler_->hwnd();
969 void DesktopWindowTreeHostWin::SetWindowTransparency() {
970 bool transparent = ShouldUseNativeFrame() && !IsFullscreen();
971 compositor()->SetHostHasTransparentBackground(transparent);
972 window()->SetTransparent(transparent);
973 content_window_->SetTransparent(transparent);
976 bool DesktopWindowTreeHostWin::IsModalWindowActive() const {
977 // This function can get called during window creation which occurs before
978 // dispatcher() has been created.
982 aura::Window::Windows::const_iterator index;
983 for (index = window()->children().begin();
984 index != window()->children().end();
986 if ((*index)->GetProperty(aura::client::kModalKey) !=
987 ui:: MODAL_TYPE_NONE && (*index)->TargetVisibility())
993 ////////////////////////////////////////////////////////////////////////////////
994 // DesktopWindowTreeHost, public:
997 DesktopWindowTreeHost* DesktopWindowTreeHost::Create(
998 internal::NativeWidgetDelegate* native_widget_delegate,
999 DesktopNativeWidgetAura* desktop_native_widget_aura) {
1000 return new DesktopWindowTreeHostWin(native_widget_delegate,
1001 desktop_native_widget_aura);
1004 } // namespace views