[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / views / widget / native_widget_aura.cc
blobf55168705125ba8c82e66a9f0a7a3740faefc3c1
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/native_widget_aura.h"
7 #include "base/bind.h"
8 #include "base/strings/string_util.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/screen_position_client.h"
14 #include "ui/aura/client/window_tree_client.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/aura/window_observer.h"
19 #include "ui/base/dragdrop/os_exchange_data.h"
20 #include "ui/base/ui_base_switches_util.h"
21 #include "ui/base/ui_base_types.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/events/event.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/font_list.h"
26 #include "ui/gfx/screen.h"
27 #include "ui/native_theme/native_theme_aura.h"
28 #include "ui/views/drag_utils.h"
29 #include "ui/views/ime/input_method_bridge.h"
30 #include "ui/views/ime/null_input_method.h"
31 #include "ui/views/views_delegate.h"
32 #include "ui/views/widget/drop_helper.h"
33 #include "ui/views/widget/native_widget_delegate.h"
34 #include "ui/views/widget/root_view.h"
35 #include "ui/views/widget/tooltip_manager_aura.h"
36 #include "ui/views/widget/widget_aura_utils.h"
37 #include "ui/views/widget/widget_delegate.h"
38 #include "ui/views/widget/window_reorderer.h"
39 #include "ui/wm/core/shadow_types.h"
40 #include "ui/wm/core/window_util.h"
41 #include "ui/wm/public/activation_client.h"
42 #include "ui/wm/public/drag_drop_client.h"
43 #include "ui/wm/public/window_move_client.h"
44 #include "ui/wm/public/window_types.h"
46 #if defined(OS_WIN)
47 #include "base/win/scoped_gdi_object.h"
48 #include "base/win/win_util.h"
49 #include "ui/base/l10n/l10n_util_win.h"
50 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_win.h"
51 #endif
53 #if defined(USE_X11) && !defined(OS_CHROMEOS)
54 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
55 #endif
57 #if !defined(OS_CHROMEOS)
58 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
59 #include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
60 #endif
62 namespace views {
64 namespace {
66 void SetRestoreBounds(aura::Window* window, const gfx::Rect& bounds) {
67 window->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds));
70 } // namespace
72 ////////////////////////////////////////////////////////////////////////////////
73 // NativeWidgetAura, public:
75 NativeWidgetAura::NativeWidgetAura(internal::NativeWidgetDelegate* delegate)
76 : delegate_(delegate),
77 window_(new aura::Window(this)),
78 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET),
79 close_widget_factory_(this),
80 destroying_(false),
81 cursor_(gfx::kNullCursor),
82 saved_window_state_(ui::SHOW_STATE_DEFAULT) {
83 aura::client::SetFocusChangeObserver(window_, this);
84 aura::client::SetActivationChangeObserver(window_, this);
87 // static
88 void NativeWidgetAura::RegisterNativeWidgetForWindow(
89 internal::NativeWidgetPrivate* native_widget,
90 aura::Window* window) {
91 window->set_user_data(native_widget);
94 ////////////////////////////////////////////////////////////////////////////////
95 // NativeWidgetAura, internal::NativeWidgetPrivate implementation:
97 void NativeWidgetAura::InitNativeWidget(const Widget::InitParams& params) {
98 // Aura needs to know which desktop (Ash or regular) will manage this widget.
99 // See Widget::InitParams::context for details.
100 DCHECK(params.parent || params.context);
102 ownership_ = params.ownership;
104 RegisterNativeWidgetForWindow(this, window_);
105 window_->SetType(GetAuraWindowTypeForWidgetType(params.type));
106 window_->SetProperty(aura::client::kShowStateKey, params.show_state);
107 if (params.type == Widget::InitParams::TYPE_BUBBLE)
108 aura::client::SetHideOnDeactivate(window_, true);
109 window_->SetTransparent(
110 params.opacity == Widget::InitParams::TRANSLUCENT_WINDOW);
111 window_->Init(params.layer_type);
112 if (params.shadow_type == Widget::InitParams::SHADOW_TYPE_NONE)
113 SetShadowType(window_, wm::SHADOW_TYPE_NONE);
114 else if (params.shadow_type == Widget::InitParams::SHADOW_TYPE_DROP)
115 SetShadowType(window_, wm::SHADOW_TYPE_RECTANGULAR);
116 if (params.type == Widget::InitParams::TYPE_CONTROL)
117 window_->Show();
119 delegate_->OnNativeWidgetCreated(false);
121 gfx::Rect window_bounds = params.bounds;
122 gfx::NativeView parent = params.parent;
123 gfx::NativeView context = params.context;
124 if (!params.child) {
125 // Set up the transient child before the window is added. This way the
126 // LayoutManager knows the window has a transient parent.
127 if (parent && parent->type() != ui::wm::WINDOW_TYPE_UNKNOWN) {
128 wm::AddTransientChild(parent, window_);
129 if (!context)
130 context = parent;
131 parent = NULL;
133 // SetAlwaysOnTop before SetParent so that always-on-top container is used.
134 SetAlwaysOnTop(params.keep_on_top);
135 // Make sure we have a real |window_bounds|.
136 if (parent && window_bounds == gfx::Rect()) {
137 // If a parent is specified but no bounds are given,
138 // use the origin of the parent's display so that the widget
139 // will be added to the same display as the parent.
140 gfx::Rect bounds = gfx::Screen::GetScreenFor(parent)->
141 GetDisplayNearestWindow(parent).bounds();
142 window_bounds.set_origin(bounds.origin());
146 // Set properties before addeing to the parent so that its layout manager
147 // sees the correct values.
148 window_->SetProperty(aura::client::kCanMaximizeKey,
149 GetWidget()->widget_delegate()->CanMaximize());
150 window_->SetProperty(aura::client::kCanResizeKey,
151 GetWidget()->widget_delegate()->CanResize());
153 if (parent) {
154 parent->AddChild(window_);
155 } else {
156 aura::client::ParentWindowWithContext(
157 window_, context->GetRootWindow(), window_bounds);
160 // Wait to set the bounds until we have a parent. That way we can know our
161 // true state/bounds (the LayoutManager may enforce a particular
162 // state/bounds).
163 if (IsMaximized())
164 SetRestoreBounds(window_, window_bounds);
165 else
166 SetBounds(window_bounds);
167 window_->set_ignore_events(!params.accept_events);
168 DCHECK(GetWidget()->GetRootView());
169 if (params.type != Widget::InitParams::TYPE_TOOLTIP)
170 tooltip_manager_.reset(new views::TooltipManagerAura(GetWidget()));
172 drop_helper_.reset(new DropHelper(GetWidget()->GetRootView()));
173 if (params.type != Widget::InitParams::TYPE_TOOLTIP &&
174 params.type != Widget::InitParams::TYPE_POPUP) {
175 aura::client::SetDragDropDelegate(window_, this);
178 aura::client::SetActivationDelegate(window_, this);
180 window_reorderer_.reset(new WindowReorderer(window_,
181 GetWidget()->GetRootView()));
184 NonClientFrameView* NativeWidgetAura::CreateNonClientFrameView() {
185 return NULL;
188 bool NativeWidgetAura::ShouldUseNativeFrame() const {
189 // There is only one frame type for aura.
190 return false;
193 bool NativeWidgetAura::ShouldWindowContentsBeTransparent() const {
194 return false;
197 void NativeWidgetAura::FrameTypeChanged() {
198 // This is called when the Theme has changed; forward the event to the root
199 // widget.
200 GetWidget()->ThemeChanged();
201 GetWidget()->GetRootView()->SchedulePaint();
204 Widget* NativeWidgetAura::GetWidget() {
205 return delegate_->AsWidget();
208 const Widget* NativeWidgetAura::GetWidget() const {
209 return delegate_->AsWidget();
212 gfx::NativeView NativeWidgetAura::GetNativeView() const {
213 return window_;
216 gfx::NativeWindow NativeWidgetAura::GetNativeWindow() const {
217 return window_;
220 Widget* NativeWidgetAura::GetTopLevelWidget() {
221 NativeWidgetPrivate* native_widget = GetTopLevelNativeWidget(GetNativeView());
222 return native_widget ? native_widget->GetWidget() : NULL;
225 const ui::Compositor* NativeWidgetAura::GetCompositor() const {
226 return window_ ? window_->layer()->GetCompositor() : NULL;
229 ui::Compositor* NativeWidgetAura::GetCompositor() {
230 return window_ ? window_->layer()->GetCompositor() : NULL;
233 ui::Layer* NativeWidgetAura::GetLayer() {
234 return window_ ? window_->layer() : NULL;
237 void NativeWidgetAura::ReorderNativeViews() {
238 window_reorderer_->ReorderChildWindows();
241 void NativeWidgetAura::ViewRemoved(View* view) {
242 DCHECK(drop_helper_.get() != NULL);
243 drop_helper_->ResetTargetViewIfEquals(view);
246 void NativeWidgetAura::SetNativeWindowProperty(const char* name, void* value) {
247 if (window_)
248 window_->SetNativeWindowProperty(name, value);
251 void* NativeWidgetAura::GetNativeWindowProperty(const char* name) const {
252 return window_ ? window_->GetNativeWindowProperty(name) : NULL;
255 TooltipManager* NativeWidgetAura::GetTooltipManager() const {
256 return tooltip_manager_.get();
259 void NativeWidgetAura::SetCapture() {
260 if (window_)
261 window_->SetCapture();
264 void NativeWidgetAura::ReleaseCapture() {
265 if (window_)
266 window_->ReleaseCapture();
269 bool NativeWidgetAura::HasCapture() const {
270 return window_ && window_->HasCapture();
273 InputMethod* NativeWidgetAura::CreateInputMethod() {
274 if (!window_)
275 return NULL;
277 if (switches::IsTextInputFocusManagerEnabled())
278 return new NullInputMethod();
280 aura::Window* root_window = window_->GetRootWindow();
281 ui::InputMethod* host =
282 root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
283 return new InputMethodBridge(this, host, true);
286 internal::InputMethodDelegate* NativeWidgetAura::GetInputMethodDelegate() {
287 return this;
290 ui::InputMethod* NativeWidgetAura::GetHostInputMethod() {
291 aura::Window* root_window = window_->GetRootWindow();
292 return root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
295 void NativeWidgetAura::CenterWindow(const gfx::Size& size) {
296 if (!window_)
297 return;
299 gfx::Rect parent_bounds(window_->parent()->GetBoundsInRootWindow());
300 // When centering window, we take the intersection of the host and
301 // the parent. We assume the root window represents the visible
302 // rect of a single screen.
303 gfx::Rect work_area = gfx::Screen::GetScreenFor(window_)->
304 GetDisplayNearestWindow(window_).work_area();
306 aura::client::ScreenPositionClient* screen_position_client =
307 aura::client::GetScreenPositionClient(window_->GetRootWindow());
308 if (screen_position_client) {
309 gfx::Point origin = work_area.origin();
310 screen_position_client->ConvertPointFromScreen(window_->GetRootWindow(),
311 &origin);
312 work_area.set_origin(origin);
315 parent_bounds.Intersect(work_area);
317 // If |window_|'s transient parent's bounds are big enough to fit it, then we
318 // center it with respect to the transient parent.
319 if (wm::GetTransientParent(window_)) {
320 gfx::Rect transient_parent_rect =
321 wm::GetTransientParent(window_)->GetBoundsInRootWindow();
322 transient_parent_rect.Intersect(work_area);
323 if (transient_parent_rect.height() >= size.height() &&
324 transient_parent_rect.width() >= size.width())
325 parent_bounds = transient_parent_rect;
328 gfx::Rect window_bounds(
329 parent_bounds.x() + (parent_bounds.width() - size.width()) / 2,
330 parent_bounds.y() + (parent_bounds.height() - size.height()) / 2,
331 size.width(),
332 size.height());
333 // Don't size the window bigger than the parent, otherwise the user may not be
334 // able to close or move it.
335 window_bounds.AdjustToFit(parent_bounds);
337 // Convert the bounds back relative to the parent.
338 gfx::Point origin = window_bounds.origin();
339 aura::Window::ConvertPointToTarget(window_->GetRootWindow(),
340 window_->parent(), &origin);
341 window_bounds.set_origin(origin);
342 window_->SetBounds(window_bounds);
345 void NativeWidgetAura::GetWindowPlacement(
346 gfx::Rect* bounds,
347 ui::WindowShowState* show_state) const {
348 // The interface specifies returning restored bounds, not current bounds.
349 *bounds = GetRestoredBounds();
350 *show_state = window_ ? window_->GetProperty(aura::client::kShowStateKey) :
351 ui::SHOW_STATE_DEFAULT;
354 bool NativeWidgetAura::SetWindowTitle(const base::string16& title) {
355 if (!window_)
356 return false;
357 if (window_->title() == title)
358 return false;
359 window_->SetTitle(title);
360 return true;
363 void NativeWidgetAura::SetWindowIcons(const gfx::ImageSkia& window_icon,
364 const gfx::ImageSkia& app_icon) {
365 // Aura doesn't have window icons.
368 void NativeWidgetAura::InitModalType(ui::ModalType modal_type) {
369 if (modal_type != ui::MODAL_TYPE_NONE)
370 window_->SetProperty(aura::client::kModalKey, modal_type);
373 gfx::Rect NativeWidgetAura::GetWindowBoundsInScreen() const {
374 return window_ ? window_->GetBoundsInScreen() : gfx::Rect();
377 gfx::Rect NativeWidgetAura::GetClientAreaBoundsInScreen() const {
378 // View-to-screen coordinate system transformations depend on this returning
379 // the full window bounds, for example View::ConvertPointToScreen().
380 return window_ ? window_->GetBoundsInScreen() : gfx::Rect();
383 gfx::Rect NativeWidgetAura::GetRestoredBounds() const {
384 if (!window_)
385 return gfx::Rect();
387 // Restored bounds should only be relevant if the window is minimized or
388 // maximized. However, in some places the code expects GetRestoredBounds()
389 // to return the current window bounds if the window is not in either state.
390 if (IsMinimized() || IsMaximized() || IsFullscreen()) {
391 // Restore bounds are in screen coordinates, no need to convert.
392 gfx::Rect* restore_bounds =
393 window_->GetProperty(aura::client::kRestoreBoundsKey);
394 if (restore_bounds)
395 return *restore_bounds;
397 return window_->GetBoundsInScreen();
400 void NativeWidgetAura::SetBounds(const gfx::Rect& bounds) {
401 if (!window_)
402 return;
404 aura::Window* root = window_->GetRootWindow();
405 if (root) {
406 aura::client::ScreenPositionClient* screen_position_client =
407 aura::client::GetScreenPositionClient(root);
408 if (screen_position_client) {
409 gfx::Display dst_display =
410 gfx::Screen::GetScreenFor(window_)->GetDisplayMatching(bounds);
411 screen_position_client->SetBounds(window_, bounds, dst_display);
412 return;
415 window_->SetBounds(bounds);
418 void NativeWidgetAura::SetSize(const gfx::Size& size) {
419 if (window_)
420 window_->SetBounds(gfx::Rect(window_->bounds().origin(), size));
423 void NativeWidgetAura::StackAbove(gfx::NativeView native_view) {
424 if (window_ && window_->parent() &&
425 window_->parent() == native_view->parent())
426 window_->parent()->StackChildAbove(window_, native_view);
429 void NativeWidgetAura::StackAtTop() {
430 if (window_)
431 window_->parent()->StackChildAtTop(window_);
434 void NativeWidgetAura::StackBelow(gfx::NativeView native_view) {
435 if (window_ && window_->parent() &&
436 window_->parent() == native_view->parent())
437 window_->parent()->StackChildBelow(window_, native_view);
440 void NativeWidgetAura::SetShape(gfx::NativeRegion region) {
441 // No need for this. Just delete and ignore.
442 delete region;
445 void NativeWidgetAura::Close() {
446 // |window_| may already be deleted by parent window. This can happen
447 // when this widget is child widget or has transient parent
448 // and ownership is WIDGET_OWNS_NATIVE_WIDGET.
449 DCHECK(window_ ||
450 ownership_ == Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
451 if (window_) {
452 window_->SuppressPaint();
453 Hide();
454 window_->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_NONE);
457 if (!close_widget_factory_.HasWeakPtrs()) {
458 base::MessageLoop::current()->PostTask(
459 FROM_HERE,
460 base::Bind(&NativeWidgetAura::CloseNow,
461 close_widget_factory_.GetWeakPtr()));
465 void NativeWidgetAura::CloseNow() {
466 delete window_;
469 void NativeWidgetAura::Show() {
470 ShowWithWindowState(ui::SHOW_STATE_NORMAL);
473 void NativeWidgetAura::Hide() {
474 if (window_)
475 window_->Hide();
478 void NativeWidgetAura::ShowMaximizedWithBounds(
479 const gfx::Rect& restored_bounds) {
480 SetRestoreBounds(window_, restored_bounds);
481 ShowWithWindowState(ui::SHOW_STATE_MAXIMIZED);
484 void NativeWidgetAura::ShowWithWindowState(ui::WindowShowState state) {
485 if (!window_)
486 return;
488 if (state == ui::SHOW_STATE_MAXIMIZED || state == ui::SHOW_STATE_FULLSCREEN)
489 window_->SetProperty(aura::client::kShowStateKey, state);
490 window_->Show();
491 if (delegate_->CanActivate()) {
492 if (state != ui::SHOW_STATE_INACTIVE)
493 Activate();
494 // SetInitialFocus() should be always be called, even for
495 // SHOW_STATE_INACTIVE. If the window has to stay inactive, the method will
496 // do the right thing.
497 SetInitialFocus(state);
501 bool NativeWidgetAura::IsVisible() const {
502 return window_ && window_->IsVisible();
505 void NativeWidgetAura::Activate() {
506 if (!window_)
507 return;
509 // We don't necessarily have a root window yet. This can happen with
510 // constrained windows.
511 if (window_->GetRootWindow()) {
512 aura::client::GetActivationClient(window_->GetRootWindow())->ActivateWindow(
513 window_);
515 if (window_->GetProperty(aura::client::kDrawAttentionKey))
516 window_->SetProperty(aura::client::kDrawAttentionKey, false);
519 void NativeWidgetAura::Deactivate() {
520 if (!window_)
521 return;
522 aura::client::GetActivationClient(window_->GetRootWindow())->DeactivateWindow(
523 window_);
526 bool NativeWidgetAura::IsActive() const {
527 return window_ && wm::IsActiveWindow(window_);
530 void NativeWidgetAura::SetAlwaysOnTop(bool on_top) {
531 if (window_)
532 window_->SetProperty(aura::client::kAlwaysOnTopKey, on_top);
535 bool NativeWidgetAura::IsAlwaysOnTop() const {
536 return window_ && window_->GetProperty(aura::client::kAlwaysOnTopKey);
539 void NativeWidgetAura::SetVisibleOnAllWorkspaces(bool always_visible) {
540 // Not implemented on chromeos or for child widgets.
543 void NativeWidgetAura::Maximize() {
544 if (window_)
545 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
548 void NativeWidgetAura::Minimize() {
549 if (window_)
550 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
553 bool NativeWidgetAura::IsMaximized() const {
554 return window_ && window_->GetProperty(aura::client::kShowStateKey) ==
555 ui::SHOW_STATE_MAXIMIZED;
558 bool NativeWidgetAura::IsMinimized() const {
559 return window_ && window_->GetProperty(aura::client::kShowStateKey) ==
560 ui::SHOW_STATE_MINIMIZED;
563 void NativeWidgetAura::Restore() {
564 if (window_)
565 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
568 void NativeWidgetAura::SetFullscreen(bool fullscreen) {
569 if (!window_ || IsFullscreen() == fullscreen)
570 return; // Nothing to do.
572 // Save window state before entering full screen so that it could restored
573 // when exiting full screen.
574 if (fullscreen)
575 saved_window_state_ = window_->GetProperty(aura::client::kShowStateKey);
577 window_->SetProperty(
578 aura::client::kShowStateKey,
579 fullscreen ? ui::SHOW_STATE_FULLSCREEN : saved_window_state_);
582 bool NativeWidgetAura::IsFullscreen() const {
583 return window_ && window_->GetProperty(aura::client::kShowStateKey) ==
584 ui::SHOW_STATE_FULLSCREEN;
587 void NativeWidgetAura::SetOpacity(unsigned char opacity) {
588 if (window_)
589 window_->layer()->SetOpacity(opacity / 255.0);
592 void NativeWidgetAura::SetUseDragFrame(bool use_drag_frame) {
593 NOTIMPLEMENTED();
596 void NativeWidgetAura::FlashFrame(bool flash) {
597 if (window_)
598 window_->SetProperty(aura::client::kDrawAttentionKey, flash);
601 void NativeWidgetAura::RunShellDrag(View* view,
602 const ui::OSExchangeData& data,
603 const gfx::Point& location,
604 int operation,
605 ui::DragDropTypes::DragEventSource source) {
606 if (window_)
607 views::RunShellDrag(window_, data, location, operation, source);
610 void NativeWidgetAura::SchedulePaintInRect(const gfx::Rect& rect) {
611 if (window_)
612 window_->SchedulePaintInRect(rect);
615 void NativeWidgetAura::SetCursor(gfx::NativeCursor cursor) {
616 cursor_ = cursor;
617 aura::client::CursorClient* cursor_client =
618 aura::client::GetCursorClient(window_->GetRootWindow());
619 if (cursor_client)
620 cursor_client->SetCursor(cursor);
623 bool NativeWidgetAura::IsMouseEventsEnabled() const {
624 if (!window_)
625 return false;
626 aura::client::CursorClient* cursor_client =
627 aura::client::GetCursorClient(window_->GetRootWindow());
628 return cursor_client ? cursor_client->IsMouseEventsEnabled() : true;
631 void NativeWidgetAura::ClearNativeFocus() {
632 aura::client::FocusClient* client = aura::client::GetFocusClient(window_);
633 if (window_ && client && window_->Contains(client->GetFocusedWindow()))
634 client->ResetFocusWithinActiveWindow(window_);
637 gfx::Rect NativeWidgetAura::GetWorkAreaBoundsInScreen() const {
638 if (!window_)
639 return gfx::Rect();
640 return gfx::Screen::GetScreenFor(window_)->
641 GetDisplayNearestWindow(window_).work_area();
644 Widget::MoveLoopResult NativeWidgetAura::RunMoveLoop(
645 const gfx::Vector2d& drag_offset,
646 Widget::MoveLoopSource source,
647 Widget::MoveLoopEscapeBehavior escape_behavior) {
648 // |escape_behavior| is only needed on windows when running the native message
649 // loop.
650 if (!window_ || !window_->GetRootWindow())
651 return Widget::MOVE_LOOP_CANCELED;
652 aura::client::WindowMoveClient* move_client =
653 aura::client::GetWindowMoveClient(window_->GetRootWindow());
654 if (!move_client)
655 return Widget::MOVE_LOOP_CANCELED;
657 SetCapture();
658 aura::client::WindowMoveSource window_move_source =
659 source == Widget::MOVE_LOOP_SOURCE_MOUSE ?
660 aura::client::WINDOW_MOVE_SOURCE_MOUSE :
661 aura::client::WINDOW_MOVE_SOURCE_TOUCH;
662 if (move_client->RunMoveLoop(window_, drag_offset, window_move_source) ==
663 aura::client::MOVE_SUCCESSFUL) {
664 return Widget::MOVE_LOOP_SUCCESSFUL;
666 return Widget::MOVE_LOOP_CANCELED;
669 void NativeWidgetAura::EndMoveLoop() {
670 if (!window_ || !window_->GetRootWindow())
671 return;
672 aura::client::WindowMoveClient* move_client =
673 aura::client::GetWindowMoveClient(window_->GetRootWindow());
674 if (move_client)
675 move_client->EndMoveLoop();
678 void NativeWidgetAura::SetVisibilityChangedAnimationsEnabled(bool value) {
679 if (window_)
680 window_->SetProperty(aura::client::kAnimationsDisabledKey, !value);
683 ui::NativeTheme* NativeWidgetAura::GetNativeTheme() const {
684 #if !defined(OS_CHROMEOS)
685 return DesktopWindowTreeHost::GetNativeTheme(window_);
686 #else
687 return ui::NativeThemeAura::instance();
688 #endif
691 void NativeWidgetAura::OnRootViewLayout() const {
694 void NativeWidgetAura::RepostNativeEvent(gfx::NativeEvent native_event) {
695 OnEvent(native_event);
698 ////////////////////////////////////////////////////////////////////////////////
699 // NativeWidgetAura, views::InputMethodDelegate implementation:
701 void NativeWidgetAura::DispatchKeyEventPostIME(const ui::KeyEvent& key) {
702 FocusManager* focus_manager = GetWidget()->GetFocusManager();
703 delegate_->OnKeyEvent(const_cast<ui::KeyEvent*>(&key));
704 if (key.handled() || !focus_manager)
705 return;
706 focus_manager->OnKeyEvent(key);
709 ////////////////////////////////////////////////////////////////////////////////
710 // NativeWidgetAura, aura::WindowDelegate implementation:
712 gfx::Size NativeWidgetAura::GetMinimumSize() const {
713 return delegate_->GetMinimumSize();
716 gfx::Size NativeWidgetAura::GetMaximumSize() const {
717 // If a window have a maximum size, the window should not be
718 // maximizable.
719 DCHECK(delegate_->GetMaximumSize().IsEmpty() ||
720 !window_->GetProperty(aura::client::kCanMaximizeKey));
721 return delegate_->GetMaximumSize();
724 void NativeWidgetAura::OnBoundsChanged(const gfx::Rect& old_bounds,
725 const gfx::Rect& new_bounds) {
726 // Assume that if the old bounds was completely empty a move happened. This
727 // handles the case of a maximize animation acquiring the layer (acquiring a
728 // layer results in clearing the bounds).
729 if (old_bounds.origin() != new_bounds.origin() ||
730 (old_bounds == gfx::Rect(0, 0, 0, 0) && !new_bounds.IsEmpty())) {
731 delegate_->OnNativeWidgetMove();
733 if (old_bounds.size() != new_bounds.size())
734 delegate_->OnNativeWidgetSizeChanged(new_bounds.size());
737 gfx::NativeCursor NativeWidgetAura::GetCursor(const gfx::Point& point) {
738 return cursor_;
741 int NativeWidgetAura::GetNonClientComponent(const gfx::Point& point) const {
742 return delegate_->GetNonClientComponent(point);
745 bool NativeWidgetAura::ShouldDescendIntoChildForEventHandling(
746 aura::Window* child,
747 const gfx::Point& location) {
748 views::WidgetDelegate* widget_delegate = GetWidget()->widget_delegate();
749 if (widget_delegate &&
750 !widget_delegate->ShouldDescendIntoChildForEventHandling(child, location))
751 return false;
753 // Don't descend into |child| if there is a view with a Layer that contains
754 // the point and is stacked above |child|s layer.
755 typedef std::vector<ui::Layer*> Layers;
756 const Layers& root_layers(delegate_->GetRootLayers());
757 if (root_layers.empty())
758 return true;
760 Layers::const_iterator child_layer_iter(
761 std::find(window_->layer()->children().begin(),
762 window_->layer()->children().end(), child->layer()));
763 if (child_layer_iter == window_->layer()->children().end())
764 return true;
766 for (std::vector<ui::Layer*>::const_reverse_iterator i = root_layers.rbegin();
767 i != root_layers.rend(); ++i) {
768 ui::Layer* layer = *i;
769 if (layer->visible() && layer->bounds().Contains(location)) {
770 Layers::const_iterator root_layer_iter(
771 std::find(window_->layer()->children().begin(),
772 window_->layer()->children().end(), layer));
773 if (root_layer_iter > child_layer_iter)
774 return false;
777 return true;
780 bool NativeWidgetAura::CanFocus() {
781 return ShouldActivate();
784 void NativeWidgetAura::OnCaptureLost() {
785 delegate_->OnMouseCaptureLost();
788 void NativeWidgetAura::OnPaint(gfx::Canvas* canvas) {
789 delegate_->OnNativeWidgetPaint(canvas);
792 void NativeWidgetAura::OnDeviceScaleFactorChanged(float device_scale_factor) {
793 // Repainting with new scale factor will paint the content at the right scale.
796 void NativeWidgetAura::OnWindowDestroying(aura::Window* window) {
797 delegate_->OnNativeWidgetDestroying();
799 // If the aura::Window is destroyed, we can no longer show tooltips.
800 tooltip_manager_.reset();
803 void NativeWidgetAura::OnWindowDestroyed(aura::Window* window) {
804 window_ = NULL;
805 delegate_->OnNativeWidgetDestroyed();
806 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
807 delete this;
810 void NativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible) {
811 delegate_->OnNativeWidgetVisibilityChanged(visible);
814 bool NativeWidgetAura::HasHitTestMask() const {
815 return delegate_->HasHitTestMask();
818 void NativeWidgetAura::GetHitTestMask(gfx::Path* mask) const {
819 DCHECK(mask);
820 delegate_->GetHitTestMask(mask);
823 ////////////////////////////////////////////////////////////////////////////////
824 // NativeWidgetAura, ui::EventHandler implementation:
826 void NativeWidgetAura::OnKeyEvent(ui::KeyEvent* event) {
827 DCHECK(window_);
828 if (event->is_char()) {
829 // If a ui::InputMethod object is attached to the root window, character
830 // events are handled inside the object and are not passed to this function.
831 // If such object is not attached, character events might be sent (e.g. on
832 // Windows). In this case, we just skip these.
833 return;
835 // Renderer may send a key event back to us if the key event wasn't handled,
836 // and the window may be invisible by that time.
837 if (!window_->IsVisible())
838 return;
839 GetWidget()->GetInputMethod()->DispatchKeyEvent(*event);
840 if (switches::IsTextInputFocusManagerEnabled()) {
841 FocusManager* focus_manager = GetWidget()->GetFocusManager();
842 delegate_->OnKeyEvent(event);
843 if (!event->handled() && focus_manager)
844 focus_manager->OnKeyEvent(*event);
846 event->SetHandled();
849 void NativeWidgetAura::OnMouseEvent(ui::MouseEvent* event) {
850 DCHECK(window_);
851 DCHECK(window_->IsVisible());
852 if (event->type() == ui::ET_MOUSEWHEEL) {
853 delegate_->OnMouseEvent(event);
854 if (event->handled())
855 return;
858 if (tooltip_manager_.get())
859 tooltip_manager_->UpdateTooltip();
860 TooltipManagerAura::UpdateTooltipManagerForCapture(GetWidget());
861 delegate_->OnMouseEvent(event);
864 void NativeWidgetAura::OnScrollEvent(ui::ScrollEvent* event) {
865 delegate_->OnScrollEvent(event);
868 void NativeWidgetAura::OnGestureEvent(ui::GestureEvent* event) {
869 DCHECK(window_);
870 DCHECK(window_->IsVisible() || event->IsEndingEvent());
871 delegate_->OnGestureEvent(event);
874 ////////////////////////////////////////////////////////////////////////////////
875 // NativeWidgetAura, aura::client::ActivationDelegate implementation:
877 bool NativeWidgetAura::ShouldActivate() const {
878 return delegate_->CanActivate();
881 ////////////////////////////////////////////////////////////////////////////////
882 // NativeWidgetAura, aura::client::ActivationChangeObserver implementation:
884 void NativeWidgetAura::OnWindowActivated(aura::Window* gained_active,
885 aura::Window* lost_active) {
886 DCHECK(window_ == gained_active || window_ == lost_active);
887 if (GetWidget()->GetFocusManager()) {
888 if (window_ == gained_active)
889 GetWidget()->GetFocusManager()->RestoreFocusedView();
890 else if (window_ == lost_active)
891 GetWidget()->GetFocusManager()->StoreFocusedView(true);
893 delegate_->OnNativeWidgetActivationChanged(window_ == gained_active);
896 ////////////////////////////////////////////////////////////////////////////////
897 // NativeWidgetAura, aura::client::FocusChangeObserver:
899 void NativeWidgetAura::OnWindowFocused(aura::Window* gained_focus,
900 aura::Window* lost_focus) {
901 if (window_ == gained_focus) {
902 // In aura, it is possible for child native widgets to take input and focus,
903 // this differs from the behavior on windows.
904 if (GetWidget()->GetInputMethod()) // Null in tests.
905 GetWidget()->GetInputMethod()->OnFocus();
906 delegate_->OnNativeFocus(lost_focus);
907 GetWidget()->GetFocusManager()->RestoreFocusedView();
908 } else if (window_ == lost_focus) {
909 // GetInputMethod() recreates the input method if it's previously been
910 // destroyed. If we get called during destruction, the input method will be
911 // gone, and creating a new one and telling it that we lost the focus will
912 // trigger a DCHECK (the new input method doesn't think that we have the
913 // focus and doesn't expect a blur). OnBlur() shouldn't be called during
914 // destruction unless WIDGET_OWNS_NATIVE_WIDGET is set (which is just the
915 // case in tests).
916 if (!destroying_) {
917 if (GetWidget()->GetInputMethod())
918 GetWidget()->GetInputMethod()->OnBlur();
919 } else {
920 DCHECK_EQ(ownership_, Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
923 delegate_->OnNativeBlur(gained_focus);
924 if (GetWidget()->GetFocusManager())
925 GetWidget()->GetFocusManager()->StoreFocusedView(true);
929 ////////////////////////////////////////////////////////////////////////////////
930 // NativeWidgetAura, aura::WindowDragDropDelegate implementation:
932 void NativeWidgetAura::OnDragEntered(const ui::DropTargetEvent& event) {
933 DCHECK(drop_helper_.get() != NULL);
934 last_drop_operation_ = drop_helper_->OnDragOver(event.data(),
935 event.location(), event.source_operations());
938 int NativeWidgetAura::OnDragUpdated(const ui::DropTargetEvent& event) {
939 DCHECK(drop_helper_.get() != NULL);
940 last_drop_operation_ = drop_helper_->OnDragOver(event.data(),
941 event.location(), event.source_operations());
942 return last_drop_operation_;
945 void NativeWidgetAura::OnDragExited() {
946 DCHECK(drop_helper_.get() != NULL);
947 drop_helper_->OnDragExit();
950 int NativeWidgetAura::OnPerformDrop(const ui::DropTargetEvent& event) {
951 DCHECK(drop_helper_.get() != NULL);
952 return drop_helper_->OnDrop(event.data(), event.location(),
953 last_drop_operation_);
956 ////////////////////////////////////////////////////////////////////////////////
957 // NativeWidgetAura, protected:
959 NativeWidgetAura::~NativeWidgetAura() {
960 destroying_ = true;
961 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
962 delete delegate_;
963 else
964 CloseNow();
967 ////////////////////////////////////////////////////////////////////////////////
968 // NativeWidgetAura, private:
970 void NativeWidgetAura::SetInitialFocus(ui::WindowShowState show_state) {
971 // The window does not get keyboard messages unless we focus it.
972 if (!GetWidget()->SetInitialFocus(show_state))
973 window_->Focus();
976 ////////////////////////////////////////////////////////////////////////////////
977 // Widget, public:
979 namespace {
980 #if defined(OS_WIN) || (defined(USE_X11) && !defined(OS_CHROMEOS))
981 void CloseWindow(aura::Window* window) {
982 if (window) {
983 Widget* widget = Widget::GetWidgetForNativeView(window);
984 if (widget && widget->is_secondary_widget())
985 // To avoid the delay in shutdown caused by using Close which may wait
986 // for animations, use CloseNow. Because this is only used on secondary
987 // widgets it seems relatively safe to skip the extra processing of
988 // Close.
989 widget->CloseNow();
992 #endif
994 #if defined(OS_WIN)
995 BOOL CALLBACK WindowCallbackProc(HWND hwnd, LPARAM lParam) {
996 aura::Window* root_window =
997 DesktopWindowTreeHostWin::GetContentWindowForHWND(hwnd);
998 CloseWindow(root_window);
999 return TRUE;
1001 #endif
1002 } // namespace
1004 // static
1005 void Widget::CloseAllSecondaryWidgets() {
1006 #if defined(OS_WIN)
1007 EnumThreadWindows(GetCurrentThreadId(), WindowCallbackProc, 0);
1008 #endif
1010 #if defined(USE_X11) && !defined(OS_CHROMEOS)
1011 std::vector<aura::Window*> open_windows =
1012 DesktopWindowTreeHostX11::GetAllOpenWindows();
1013 std::for_each(open_windows.begin(), open_windows.end(), CloseWindow);
1014 DesktopWindowTreeHostX11::CleanUpWindowList();
1015 #endif
1018 bool Widget::ConvertRect(const Widget* source,
1019 const Widget* target,
1020 gfx::Rect* rect) {
1021 return false;
1024 namespace internal {
1026 ////////////////////////////////////////////////////////////////////////////////
1027 // internal::NativeWidgetPrivate, public:
1029 // static
1030 NativeWidgetPrivate* NativeWidgetPrivate::CreateNativeWidget(
1031 internal::NativeWidgetDelegate* delegate) {
1032 return new NativeWidgetAura(delegate);
1035 // static
1036 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeView(
1037 gfx::NativeView native_view) {
1038 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1039 return reinterpret_cast<NativeWidgetPrivate*>(native_view->user_data());
1042 // static
1043 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
1044 gfx::NativeWindow native_window) {
1045 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1046 return reinterpret_cast<NativeWidgetPrivate*>(native_window->user_data());
1049 // static
1050 NativeWidgetPrivate* NativeWidgetPrivate::GetTopLevelNativeWidget(
1051 gfx::NativeView native_view) {
1052 aura::Window* window = native_view;
1053 NativeWidgetPrivate* top_level_native_widget = NULL;
1054 while (window) {
1055 NativeWidgetPrivate* native_widget = GetNativeWidgetForNativeView(window);
1056 if (native_widget)
1057 top_level_native_widget = native_widget;
1058 window = window->parent();
1060 return top_level_native_widget;
1063 // static
1064 void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view,
1065 Widget::Widgets* children) {
1067 // Code expects widget for |native_view| to be added to |children|.
1068 NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>(
1069 GetNativeWidgetForNativeView(native_view));
1070 if (native_widget && native_widget->GetWidget())
1071 children->insert(native_widget->GetWidget());
1074 const aura::Window::Windows& child_windows = native_view->children();
1075 for (aura::Window::Windows::const_iterator i = child_windows.begin();
1076 i != child_windows.end(); ++i) {
1077 GetAllChildWidgets((*i), children);
1081 // static
1082 void NativeWidgetPrivate::GetAllOwnedWidgets(gfx::NativeView native_view,
1083 Widget::Widgets* owned) {
1084 const aura::Window::Windows& transient_children =
1085 wm::GetTransientChildren(native_view);
1086 for (aura::Window::Windows::const_iterator i = transient_children.begin();
1087 i != transient_children.end(); ++i) {
1088 NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>(
1089 GetNativeWidgetForNativeView(*i));
1090 if (native_widget && native_widget->GetWidget())
1091 owned->insert(native_widget->GetWidget());
1092 GetAllOwnedWidgets((*i), owned);
1096 // static
1097 void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view,
1098 gfx::NativeView new_parent) {
1099 DCHECK(native_view != new_parent);
1101 gfx::NativeView previous_parent = native_view->parent();
1102 if (previous_parent == new_parent)
1103 return;
1105 Widget::Widgets widgets;
1106 GetAllChildWidgets(native_view, &widgets);
1108 // First notify all the widgets that they are being disassociated
1109 // from their previous parent.
1110 for (Widget::Widgets::iterator it = widgets.begin();
1111 it != widgets.end(); ++it) {
1112 (*it)->NotifyNativeViewHierarchyWillChange();
1115 if (new_parent) {
1116 new_parent->AddChild(native_view);
1117 } else {
1118 // The following looks weird, but it's the equivalent of what aura has
1119 // always done. (The previous behaviour of aura::Window::SetParent() used
1120 // NULL as a special value that meant ask the WindowTreeClient where things
1121 // should go.)
1123 // This probably isn't strictly correct, but its an invariant that a Window
1124 // in use will be attached to a RootWindow, so we can't just call
1125 // RemoveChild here. The only possible thing that could assign a RootWindow
1126 // in this case is the stacking client of the current RootWindow. This
1127 // matches our previous behaviour; the global stacking client would almost
1128 // always reattach the window to the same RootWindow.
1129 aura::Window* root_window = native_view->GetRootWindow();
1130 aura::client::ParentWindowWithContext(
1131 native_view, root_window, root_window->GetBoundsInScreen());
1134 // And now, notify them that they have a brand new parent.
1135 for (Widget::Widgets::iterator it = widgets.begin();
1136 it != widgets.end(); ++it) {
1137 (*it)->NotifyNativeViewHierarchyChanged();
1141 // static
1142 bool NativeWidgetPrivate::IsMouseButtonDown() {
1143 return aura::Env::GetInstance()->IsMouseButtonDown();
1146 // static
1147 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() {
1148 #if defined(OS_WIN)
1149 NONCLIENTMETRICS ncm;
1150 base::win::GetNonClientMetrics(&ncm);
1151 l10n_util::AdjustUIFont(&(ncm.lfCaptionFont));
1152 base::win::ScopedHFONT caption_font(CreateFontIndirect(&(ncm.lfCaptionFont)));
1153 return gfx::FontList(gfx::Font(caption_font));
1154 #else
1155 return gfx::FontList();
1156 #endif
1159 } // namespace internal
1160 } // namespace views