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. 4 5 #include "ui/views/widget/native_widget_aura.h" 6 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" 45 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 52 53 #if defined(USE_X11) && !defined(OS_CHROMEOS) 54 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h" 55 #endif 56 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 61 62 namespace views { 63 64 namespace { 65 66 void SetRestoreBounds(aura::Window* window, const gfx::Rect& bounds) { 67 window->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds)); 68 } 69 70 } // namespace 71 72 //////////////////////////////////////////////////////////////////////////////// 73 // NativeWidgetAura, public: 74 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); 85 } 86 87 // static 88 void NativeWidgetAura::RegisterNativeWidgetForWindow( 89 internal::NativeWidgetPrivate* native_widget, 90 aura::Window* window) { 91 window->set_user_data(native_widget); 92 } 93 94 //////////////////////////////////////////////////////////////////////////////// 95 // NativeWidgetAura, internal::NativeWidgetPrivate implementation: 96 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); 101 102 ownership_ = params.ownership; 103 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(); 118 119 delegate_->OnNativeWidgetCreated(false); 120 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; 132 } 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()); 143 } 144 } 145 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()); 152 153 if (parent) { 154 parent->AddChild(window_); 155 } else { 156 aura::client::ParentWindowWithContext( 157 window_, context->GetRootWindow(), window_bounds); 158 } 159 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())); 171 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); 176 } 177 178 aura::client::SetActivationDelegate(window_, this); 179 180 window_reorderer_.reset(new WindowReorderer(window_, 181 GetWidget()->GetRootView())); 182 } 183 184 NonClientFrameView* NativeWidgetAura::CreateNonClientFrameView() { 185 return NULL; 186 } 187 188 bool NativeWidgetAura::ShouldUseNativeFrame() const { 189 // There is only one frame type for aura. 190 return false; 191 } 192 193 bool NativeWidgetAura::ShouldWindowContentsBeTransparent() const { 194 return false; 195 } 196 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(); 202 } 203 204 Widget* NativeWidgetAura::GetWidget() { 205 return delegate_->AsWidget(); 206 } 207 208 const Widget* NativeWidgetAura::GetWidget() const { 209 return delegate_->AsWidget(); 210 } 211 212 gfx::NativeView NativeWidgetAura::GetNativeView() const { 213 return window_; 214 } 215 216 gfx::NativeWindow NativeWidgetAura::GetNativeWindow() const { 217 return window_; 218 } 219 220 Widget* NativeWidgetAura::GetTopLevelWidget() { 221 NativeWidgetPrivate* native_widget = GetTopLevelNativeWidget(GetNativeView()); 222 return native_widget ? native_widget->GetWidget() : NULL; 223 } 224 225 const ui::Compositor* NativeWidgetAura::GetCompositor() const { 226 return window_ ? window_->layer()->GetCompositor() : NULL; 227 } 228 229 ui::Compositor* NativeWidgetAura::GetCompositor() { 230 return window_ ? window_->layer()->GetCompositor() : NULL; 231 } 232 233 ui::Layer* NativeWidgetAura::GetLayer() { 234 return window_ ? window_->layer() : NULL; 235 } 236 237 void NativeWidgetAura::ReorderNativeViews() { 238 window_reorderer_->ReorderChildWindows(); 239 } 240 241 void NativeWidgetAura::ViewRemoved(View* view) { 242 DCHECK(drop_helper_.get() != NULL); 243 drop_helper_->ResetTargetViewIfEquals(view); 244 } 245 246 void NativeWidgetAura::SetNativeWindowProperty(const char* name, void* value) { 247 if (window_) 248 window_->SetNativeWindowProperty(name, value); 249 } 250 251 void* NativeWidgetAura::GetNativeWindowProperty(const char* name) const { 252 return window_ ? window_->GetNativeWindowProperty(name) : NULL; 253 } 254 255 TooltipManager* NativeWidgetAura::GetTooltipManager() const { 256 return tooltip_manager_.get(); 257 } 258 259 void NativeWidgetAura::SetCapture() { 260 if (window_) 261 window_->SetCapture(); 262 } 263 264 void NativeWidgetAura::ReleaseCapture() { 265 if (window_) 266 window_->ReleaseCapture(); 267 } 268 269 bool NativeWidgetAura::HasCapture() const { 270 return window_ && window_->HasCapture(); 271 } 272 273 InputMethod* NativeWidgetAura::CreateInputMethod() { 274 if (!window_) 275 return NULL; 276 277 if (switches::IsTextInputFocusManagerEnabled()) 278 return new NullInputMethod(); 279 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); 284 } 285 286 internal::InputMethodDelegate* NativeWidgetAura::GetInputMethodDelegate() { 287 return this; 288 } 289 290 ui::InputMethod* NativeWidgetAura::GetHostInputMethod() { 291 aura::Window* root_window = window_->GetRootWindow(); 292 return root_window->GetProperty(aura::client::kRootWindowInputMethodKey); 293 } 294 295 void NativeWidgetAura::CenterWindow(const gfx::Size& size) { 296 if (!window_) 297 return; 298 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(); 305 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); 313 } 314 315 parent_bounds.Intersect(work_area); 316 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; 326 } 327 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); 336 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); 343 } 344 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; 352 } 353 354 bool NativeWidgetAura::SetWindowTitle(const base::string16& title) { 355 if (!window_) 356 return false; 357 if (window_->title() == title) 358 return false; 359 window_->set_title(title); 360 return true; 361 } 362 363 void NativeWidgetAura::SetWindowIcons(const gfx::ImageSkia& window_icon, 364 const gfx::ImageSkia& app_icon) { 365 // Aura doesn't have window icons. 366 } 367 368 void NativeWidgetAura::InitModalType(ui::ModalType modal_type) { 369 if (modal_type != ui::MODAL_TYPE_NONE) 370 window_->SetProperty(aura::client::kModalKey, modal_type); 371 } 372 373 gfx::Rect NativeWidgetAura::GetWindowBoundsInScreen() const { 374 return window_ ? window_->GetBoundsInScreen() : gfx::Rect(); 375 } 376 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(); 381 } 382 383 gfx::Rect NativeWidgetAura::GetRestoredBounds() const { 384 if (!window_) 385 return gfx::Rect(); 386 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; 396 } 397 return window_->GetBoundsInScreen(); 398 } 399 400 void NativeWidgetAura::SetBounds(const gfx::Rect& bounds) { 401 if (!window_) 402 return; 403 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; 413 } 414 } 415 window_->SetBounds(bounds); 416 } 417 418 void NativeWidgetAura::SetSize(const gfx::Size& size) { 419 if (window_) 420 window_->SetBounds(gfx::Rect(window_->bounds().origin(), size)); 421 } 422 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); 427 } 428 429 void NativeWidgetAura::StackAtTop() { 430 if (window_) 431 window_->parent()->StackChildAtTop(window_); 432 } 433 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); 438 } 439 440 void NativeWidgetAura::SetShape(gfx::NativeRegion region) { 441 // No need for this. Just delete and ignore. 442 delete region; 443 } 444 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); 455 } 456 457 if (!close_widget_factory_.HasWeakPtrs()) { 458 base::MessageLoop::current()->PostTask( 459 FROM_HERE, 460 base::Bind(&NativeWidgetAura::CloseNow, 461 close_widget_factory_.GetWeakPtr())); 462 } 463 } 464 465 void NativeWidgetAura::CloseNow() { 466 delete window_; 467 } 468 469 void NativeWidgetAura::Show() { 470 ShowWithWindowState(ui::SHOW_STATE_NORMAL); 471 } 472 473 void NativeWidgetAura::Hide() { 474 if (window_) 475 window_->Hide(); 476 } 477 478 void NativeWidgetAura::ShowMaximizedWithBounds( 479 const gfx::Rect& restored_bounds) { 480 SetRestoreBounds(window_, restored_bounds); 481 ShowWithWindowState(ui::SHOW_STATE_MAXIMIZED); 482 } 483 484 void NativeWidgetAura::ShowWithWindowState(ui::WindowShowState state) { 485 if (!window_) 486 return; 487 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); 498 } 499 } 500 501 bool NativeWidgetAura::IsVisible() const { 502 return window_ && window_->IsVisible(); 503 } 504 505 void NativeWidgetAura::Activate() { 506 if (!window_) 507 return; 508 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_); 514 } 515 if (window_->GetProperty(aura::client::kDrawAttentionKey)) 516 window_->SetProperty(aura::client::kDrawAttentionKey, false); 517 } 518 519 void NativeWidgetAura::Deactivate() { 520 if (!window_) 521 return; 522 aura::client::GetActivationClient(window_->GetRootWindow())->DeactivateWindow( 523 window_); 524 } 525 526 bool NativeWidgetAura::IsActive() const { 527 return window_ && wm::IsActiveWindow(window_); 528 } 529 530 void NativeWidgetAura::SetAlwaysOnTop(bool on_top) { 531 if (window_) 532 window_->SetProperty(aura::client::kAlwaysOnTopKey, on_top); 533 } 534 535 bool NativeWidgetAura::IsAlwaysOnTop() const { 536 return window_ && window_->GetProperty(aura::client::kAlwaysOnTopKey); 537 } 538 539 void NativeWidgetAura::SetVisibleOnAllWorkspaces(bool always_visible) { 540 // Not implemented on chromeos or for child widgets. 541 } 542 543 void NativeWidgetAura::Maximize() { 544 if (window_) 545 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED); 546 } 547 548 void NativeWidgetAura::Minimize() { 549 if (window_) 550 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED); 551 } 552 553 bool NativeWidgetAura::IsMaximized() const { 554 return window_ && window_->GetProperty(aura::client::kShowStateKey) == 555 ui::SHOW_STATE_MAXIMIZED; 556 } 557 558 bool NativeWidgetAura::IsMinimized() const { 559 return window_ && window_->GetProperty(aura::client::kShowStateKey) == 560 ui::SHOW_STATE_MINIMIZED; 561 } 562 563 void NativeWidgetAura::Restore() { 564 if (window_) 565 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL); 566 } 567 568 void NativeWidgetAura::SetFullscreen(bool fullscreen) { 569 if (!window_ || IsFullscreen() == fullscreen) 570 return; // Nothing to do. 571 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); 576 577 window_->SetProperty( 578 aura::client::kShowStateKey, 579 fullscreen ? ui::SHOW_STATE_FULLSCREEN : saved_window_state_); 580 } 581 582 bool NativeWidgetAura::IsFullscreen() const { 583 return window_ && window_->GetProperty(aura::client::kShowStateKey) == 584 ui::SHOW_STATE_FULLSCREEN; 585 } 586 587 void NativeWidgetAura::SetOpacity(unsigned char opacity) { 588 if (window_) 589 window_->layer()->SetOpacity(opacity / 255.0); 590 } 591 592 void NativeWidgetAura::SetUseDragFrame(bool use_drag_frame) { 593 NOTIMPLEMENTED(); 594 } 595 596 void NativeWidgetAura::FlashFrame(bool flash) { 597 if (window_) 598 window_->SetProperty(aura::client::kDrawAttentionKey, flash); 599 } 600 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); 608 } 609 610 void NativeWidgetAura::SchedulePaintInRect(const gfx::Rect& rect) { 611 if (window_) 612 window_->SchedulePaintInRect(rect); 613 } 614 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); 621 } 622 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; 629 } 630 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_); 635 } 636 637 gfx::Rect NativeWidgetAura::GetWorkAreaBoundsInScreen() const { 638 if (!window_) 639 return gfx::Rect(); 640 return gfx::Screen::GetScreenFor(window_)-> 641 GetDisplayNearestWindow(window_).work_area(); 642 } 643 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; 656 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; 665 } 666 return Widget::MOVE_LOOP_CANCELED; 667 } 668 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(); 676 } 677 678 void NativeWidgetAura::SetVisibilityChangedAnimationsEnabled(bool value) { 679 if (window_) 680 window_->SetProperty(aura::client::kAnimationsDisabledKey, !value); 681 } 682 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 689 } 690 691 void NativeWidgetAura::OnRootViewLayout() const { 692 } 693 694 bool NativeWidgetAura::IsTranslucentWindowOpacitySupported() const { 695 return true; 696 } 697 698 void NativeWidgetAura::RepostNativeEvent(gfx::NativeEvent native_event) { 699 OnEvent(native_event); 700 } 701 702 //////////////////////////////////////////////////////////////////////////////// 703 // NativeWidgetAura, views::InputMethodDelegate implementation: 704 705 void NativeWidgetAura::DispatchKeyEventPostIME(const ui::KeyEvent& key) { 706 FocusManager* focus_manager = GetWidget()->GetFocusManager(); 707 delegate_->OnKeyEvent(const_cast<ui::KeyEvent*>(&key)); 708 if (key.handled() || !focus_manager) 709 return; 710 focus_manager->OnKeyEvent(key); 711 } 712 713 //////////////////////////////////////////////////////////////////////////////// 714 // NativeWidgetAura, aura::WindowDelegate implementation: 715 716 gfx::Size NativeWidgetAura::GetMinimumSize() const { 717 return delegate_->GetMinimumSize(); 718 } 719 720 gfx::Size NativeWidgetAura::GetMaximumSize() const { 721 // If a window have a maximum size, the window should not be 722 // maximizable. 723 DCHECK(delegate_->GetMaximumSize().IsEmpty() || 724 !window_->GetProperty(aura::client::kCanMaximizeKey)); 725 return delegate_->GetMaximumSize(); 726 } 727 728 void NativeWidgetAura::OnBoundsChanged(const gfx::Rect& old_bounds, 729 const gfx::Rect& new_bounds) { 730 // Assume that if the old bounds was completely empty a move happened. This 731 // handles the case of a maximize animation acquiring the layer (acquiring a 732 // layer results in clearing the bounds). 733 if (old_bounds.origin() != new_bounds.origin() || 734 (old_bounds == gfx::Rect(0, 0, 0, 0) && !new_bounds.IsEmpty())) { 735 delegate_->OnNativeWidgetMove(); 736 } 737 if (old_bounds.size() != new_bounds.size()) 738 delegate_->OnNativeWidgetSizeChanged(new_bounds.size()); 739 } 740 741 gfx::NativeCursor NativeWidgetAura::GetCursor(const gfx::Point& point) { 742 return cursor_; 743 } 744 745 int NativeWidgetAura::GetNonClientComponent(const gfx::Point& point) const { 746 return delegate_->GetNonClientComponent(point); 747 } 748 749 bool NativeWidgetAura::ShouldDescendIntoChildForEventHandling( 750 aura::Window* child, 751 const gfx::Point& location) { 752 views::WidgetDelegate* widget_delegate = GetWidget()->widget_delegate(); 753 if (widget_delegate && 754 !widget_delegate->ShouldDescendIntoChildForEventHandling(child, location)) 755 return false; 756 757 // Don't descend into |child| if there is a view with a Layer that contains 758 // the point and is stacked above |child|s layer. 759 typedef std::vector<ui::Layer*> Layers; 760 const Layers& root_layers(delegate_->GetRootLayers()); 761 if (root_layers.empty()) 762 return true; 763 764 Layers::const_iterator child_layer_iter( 765 std::find(window_->layer()->children().begin(), 766 window_->layer()->children().end(), child->layer())); 767 if (child_layer_iter == window_->layer()->children().end()) 768 return true; 769 770 for (std::vector<ui::Layer*>::const_reverse_iterator i = root_layers.rbegin(); 771 i != root_layers.rend(); ++i) { 772 ui::Layer* layer = *i; 773 if (layer->visible() && layer->bounds().Contains(location)) { 774 Layers::const_iterator root_layer_iter( 775 std::find(window_->layer()->children().begin(), 776 window_->layer()->children().end(), layer)); 777 if (root_layer_iter > child_layer_iter) 778 return false; 779 } 780 } 781 return true; 782 } 783 784 bool NativeWidgetAura::CanFocus() { 785 return ShouldActivate(); 786 } 787 788 void NativeWidgetAura::OnCaptureLost() { 789 delegate_->OnMouseCaptureLost(); 790 } 791 792 void NativeWidgetAura::OnPaint(gfx::Canvas* canvas) { 793 delegate_->OnNativeWidgetPaint(canvas); 794 } 795 796 void NativeWidgetAura::OnDeviceScaleFactorChanged(float device_scale_factor) { 797 // Repainting with new scale factor will paint the content at the right scale. 798 } 799 800 void NativeWidgetAura::OnWindowDestroying(aura::Window* window) { 801 delegate_->OnNativeWidgetDestroying(); 802 803 // If the aura::Window is destroyed, we can no longer show tooltips. 804 tooltip_manager_.reset(); 805 } 806 807 void NativeWidgetAura::OnWindowDestroyed(aura::Window* window) { 808 window_ = NULL; 809 delegate_->OnNativeWidgetDestroyed(); 810 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) 811 delete this; 812 } 813 814 void NativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible) { 815 delegate_->OnNativeWidgetVisibilityChanged(visible); 816 } 817 818 bool NativeWidgetAura::HasHitTestMask() const { 819 return delegate_->HasHitTestMask(); 820 } 821 822 void NativeWidgetAura::GetHitTestMask(gfx::Path* mask) const { 823 DCHECK(mask); 824 delegate_->GetHitTestMask(mask); 825 } 826 827 //////////////////////////////////////////////////////////////////////////////// 828 // NativeWidgetAura, ui::EventHandler implementation: 829 830 void NativeWidgetAura::OnKeyEvent(ui::KeyEvent* event) { 831 DCHECK(window_); 832 if (event->is_char()) { 833 // If a ui::InputMethod object is attached to the root window, character 834 // events are handled inside the object and are not passed to this function. 835 // If such object is not attached, character events might be sent (e.g. on 836 // Windows). In this case, we just skip these. 837 return; 838 } 839 // Renderer may send a key event back to us if the key event wasn't handled, 840 // and the window may be invisible by that time. 841 if (!window_->IsVisible()) 842 return; 843 GetWidget()->GetInputMethod()->DispatchKeyEvent(*event); 844 if (switches::IsTextInputFocusManagerEnabled()) { 845 FocusManager* focus_manager = GetWidget()->GetFocusManager(); 846 delegate_->OnKeyEvent(event); 847 if (!event->handled() && focus_manager) 848 focus_manager->OnKeyEvent(*event); 849 } 850 event->SetHandled(); 851 } 852 853 void NativeWidgetAura::OnMouseEvent(ui::MouseEvent* event) { 854 DCHECK(window_); 855 DCHECK(window_->IsVisible()); 856 if (event->type() == ui::ET_MOUSEWHEEL) { 857 delegate_->OnMouseEvent(event); 858 if (event->handled()) 859 return; 860 } 861 862 if (tooltip_manager_.get()) 863 tooltip_manager_->UpdateTooltip(); 864 TooltipManagerAura::UpdateTooltipManagerForCapture(GetWidget()); 865 delegate_->OnMouseEvent(event); 866 } 867 868 void NativeWidgetAura::OnScrollEvent(ui::ScrollEvent* event) { 869 delegate_->OnScrollEvent(event); 870 } 871 872 void NativeWidgetAura::OnGestureEvent(ui::GestureEvent* event) { 873 DCHECK(window_); 874 DCHECK(window_->IsVisible() || event->IsEndingEvent()); 875 delegate_->OnGestureEvent(event); 876 } 877 878 //////////////////////////////////////////////////////////////////////////////// 879 // NativeWidgetAura, aura::client::ActivationDelegate implementation: 880 881 bool NativeWidgetAura::ShouldActivate() const { 882 return delegate_->CanActivate(); 883 } 884 885 //////////////////////////////////////////////////////////////////////////////// 886 // NativeWidgetAura, aura::client::ActivationChangeObserver implementation: 887 888 void NativeWidgetAura::OnWindowActivated(aura::Window* gained_active, 889 aura::Window* lost_active) { 890 DCHECK(window_ == gained_active || window_ == lost_active); 891 if (GetWidget()->GetFocusManager()) { 892 if (window_ == gained_active) 893 GetWidget()->GetFocusManager()->RestoreFocusedView(); 894 else if (window_ == lost_active) 895 GetWidget()->GetFocusManager()->StoreFocusedView(true); 896 } 897 delegate_->OnNativeWidgetActivationChanged(window_ == gained_active); 898 } 899 900 //////////////////////////////////////////////////////////////////////////////// 901 // NativeWidgetAura, aura::client::FocusChangeObserver: 902 903 void NativeWidgetAura::OnWindowFocused(aura::Window* gained_focus, 904 aura::Window* lost_focus) { 905 if (window_ == gained_focus) { 906 // In aura, it is possible for child native widgets to take input and focus, 907 // this differs from the behavior on windows. 908 if (GetWidget()->GetInputMethod()) // Null in tests. 909 GetWidget()->GetInputMethod()->OnFocus(); 910 delegate_->OnNativeFocus(lost_focus); 911 } else if (window_ == lost_focus) { 912 // GetInputMethod() recreates the input method if it's previously been 913 // destroyed. If we get called during destruction, the input method will be 914 // gone, and creating a new one and telling it that we lost the focus will 915 // trigger a DCHECK (the new input method doesn't think that we have the 916 // focus and doesn't expect a blur). OnBlur() shouldn't be called during 917 // destruction unless WIDGET_OWNS_NATIVE_WIDGET is set (which is just the 918 // case in tests). 919 if (!destroying_) { 920 if (GetWidget()->GetInputMethod()) 921 GetWidget()->GetInputMethod()->OnBlur(); 922 } else { 923 DCHECK_EQ(ownership_, Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET); 924 } 925 926 delegate_->OnNativeBlur(gained_focus); 927 } 928 } 929 930 //////////////////////////////////////////////////////////////////////////////// 931 // NativeWidgetAura, aura::WindowDragDropDelegate implementation: 932 933 void NativeWidgetAura::OnDragEntered(const ui::DropTargetEvent& event) { 934 DCHECK(drop_helper_.get() != NULL); 935 last_drop_operation_ = drop_helper_->OnDragOver(event.data(), 936 event.location(), event.source_operations()); 937 } 938 939 int NativeWidgetAura::OnDragUpdated(const ui::DropTargetEvent& event) { 940 DCHECK(drop_helper_.get() != NULL); 941 last_drop_operation_ = drop_helper_->OnDragOver(event.data(), 942 event.location(), event.source_operations()); 943 return last_drop_operation_; 944 } 945 946 void NativeWidgetAura::OnDragExited() { 947 DCHECK(drop_helper_.get() != NULL); 948 drop_helper_->OnDragExit(); 949 } 950 951 int NativeWidgetAura::OnPerformDrop(const ui::DropTargetEvent& event) { 952 DCHECK(drop_helper_.get() != NULL); 953 return drop_helper_->OnDrop(event.data(), event.location(), 954 last_drop_operation_); 955 } 956 957 //////////////////////////////////////////////////////////////////////////////// 958 // NativeWidgetAura, protected: 959 960 NativeWidgetAura::~NativeWidgetAura() { 961 destroying_ = true; 962 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) 963 delete delegate_; 964 else 965 CloseNow(); 966 } 967 968 //////////////////////////////////////////////////////////////////////////////// 969 // NativeWidgetAura, private: 970 971 void NativeWidgetAura::SetInitialFocus(ui::WindowShowState show_state) { 972 // The window does not get keyboard messages unless we focus it. 973 if (!GetWidget()->SetInitialFocus(show_state)) 974 window_->Focus(); 975 } 976 977 //////////////////////////////////////////////////////////////////////////////// 978 // Widget, public: 979 980 namespace { 981 #if defined(OS_WIN) || (defined(USE_X11) && !defined(OS_CHROMEOS)) 982 void CloseWindow(aura::Window* window) { 983 if (window) { 984 Widget* widget = Widget::GetWidgetForNativeView(window); 985 if (widget && widget->is_secondary_widget()) 986 // To avoid the delay in shutdown caused by using Close which may wait 987 // for animations, use CloseNow. Because this is only used on secondary 988 // widgets it seems relatively safe to skip the extra processing of 989 // Close. 990 widget->CloseNow(); 991 } 992 } 993 #endif 994 995 #if defined(OS_WIN) 996 BOOL CALLBACK WindowCallbackProc(HWND hwnd, LPARAM lParam) { 997 aura::Window* root_window = 998 DesktopWindowTreeHostWin::GetContentWindowForHWND(hwnd); 999 CloseWindow(root_window); 1000 return TRUE; 1001 } 1002 #endif 1003 } // namespace 1004 1005 // static 1006 void Widget::CloseAllSecondaryWidgets() { 1007 #if defined(OS_WIN) 1008 EnumThreadWindows(GetCurrentThreadId(), WindowCallbackProc, 0); 1009 #endif 1010 1011 #if defined(USE_X11) && !defined(OS_CHROMEOS) 1012 std::vector<aura::Window*> open_windows = 1013 DesktopWindowTreeHostX11::GetAllOpenWindows(); 1014 std::for_each(open_windows.begin(), open_windows.end(), CloseWindow); 1015 DesktopWindowTreeHostX11::CleanUpWindowList(); 1016 #endif 1017 } 1018 1019 bool Widget::ConvertRect(const Widget* source, 1020 const Widget* target, 1021 gfx::Rect* rect) { 1022 return false; 1023 } 1024 1025 namespace internal { 1026 1027 //////////////////////////////////////////////////////////////////////////////// 1028 // internal::NativeWidgetPrivate, public: 1029 1030 // static 1031 NativeWidgetPrivate* NativeWidgetPrivate::CreateNativeWidget( 1032 internal::NativeWidgetDelegate* delegate) { 1033 return new NativeWidgetAura(delegate); 1034 } 1035 1036 // static 1037 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeView( 1038 gfx::NativeView native_view) { 1039 // Cast must match type supplied to RegisterNativeWidgetForWindow(). 1040 return reinterpret_cast<NativeWidgetPrivate*>(native_view->user_data()); 1041 } 1042 1043 // static 1044 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeWindow( 1045 gfx::NativeWindow native_window) { 1046 // Cast must match type supplied to RegisterNativeWidgetForWindow(). 1047 return reinterpret_cast<NativeWidgetPrivate*>(native_window->user_data()); 1048 } 1049 1050 // static 1051 NativeWidgetPrivate* NativeWidgetPrivate::GetTopLevelNativeWidget( 1052 gfx::NativeView native_view) { 1053 aura::Window* window = native_view; 1054 NativeWidgetPrivate* top_level_native_widget = NULL; 1055 while (window) { 1056 NativeWidgetPrivate* native_widget = GetNativeWidgetForNativeView(window); 1057 if (native_widget) 1058 top_level_native_widget = native_widget; 1059 window = window->parent(); 1060 } 1061 return top_level_native_widget; 1062 } 1063 1064 // static 1065 void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view, 1066 Widget::Widgets* children) { 1067 { 1068 // Code expects widget for |native_view| to be added to |children|. 1069 NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>( 1070 GetNativeWidgetForNativeView(native_view)); 1071 if (native_widget && native_widget->GetWidget()) 1072 children->insert(native_widget->GetWidget()); 1073 } 1074 1075 const aura::Window::Windows& child_windows = native_view->children(); 1076 for (aura::Window::Windows::const_iterator i = child_windows.begin(); 1077 i != child_windows.end(); ++i) { 1078 GetAllChildWidgets((*i), children); 1079 } 1080 } 1081 1082 // static 1083 void NativeWidgetPrivate::GetAllOwnedWidgets(gfx::NativeView native_view, 1084 Widget::Widgets* owned) { 1085 const aura::Window::Windows& transient_children = 1086 wm::GetTransientChildren(native_view); 1087 for (aura::Window::Windows::const_iterator i = transient_children.begin(); 1088 i != transient_children.end(); ++i) { 1089 NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>( 1090 GetNativeWidgetForNativeView(*i)); 1091 if (native_widget && native_widget->GetWidget()) 1092 owned->insert(native_widget->GetWidget()); 1093 GetAllOwnedWidgets((*i), owned); 1094 } 1095 } 1096 1097 // static 1098 void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view, 1099 gfx::NativeView new_parent) { 1100 DCHECK(native_view != new_parent); 1101 1102 gfx::NativeView previous_parent = native_view->parent(); 1103 if (previous_parent == new_parent) 1104 return; 1105 1106 Widget::Widgets widgets; 1107 GetAllChildWidgets(native_view, &widgets); 1108 1109 // First notify all the widgets that they are being disassociated 1110 // from their previous parent. 1111 for (Widget::Widgets::iterator it = widgets.begin(); 1112 it != widgets.end(); ++it) { 1113 (*it)->NotifyNativeViewHierarchyWillChange(); 1114 } 1115 1116 if (new_parent) { 1117 new_parent->AddChild(native_view); 1118 } else { 1119 // The following looks weird, but it's the equivalent of what aura has 1120 // always done. (The previous behaviour of aura::Window::SetParent() used 1121 // NULL as a special value that meant ask the WindowTreeClient where things 1122 // should go.) 1123 // 1124 // This probably isn't strictly correct, but its an invariant that a Window 1125 // in use will be attached to a RootWindow, so we can't just call 1126 // RemoveChild here. The only possible thing that could assign a RootWindow 1127 // in this case is the stacking client of the current RootWindow. This 1128 // matches our previous behaviour; the global stacking client would almost 1129 // always reattach the window to the same RootWindow. 1130 aura::Window* root_window = native_view->GetRootWindow(); 1131 aura::client::ParentWindowWithContext( 1132 native_view, root_window, root_window->GetBoundsInScreen()); 1133 } 1134 1135 // And now, notify them that they have a brand new parent. 1136 for (Widget::Widgets::iterator it = widgets.begin(); 1137 it != widgets.end(); ++it) { 1138 (*it)->NotifyNativeViewHierarchyChanged(); 1139 } 1140 } 1141 1142 // static 1143 bool NativeWidgetPrivate::IsMouseButtonDown() { 1144 return aura::Env::GetInstance()->IsMouseButtonDown(); 1145 } 1146 1147 // static 1148 bool NativeWidgetPrivate::IsTouchDown() { 1149 return aura::Env::GetInstance()->is_touch_down(); 1150 } 1151 1152 // static 1153 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() { 1154 #if defined(OS_WIN) 1155 NONCLIENTMETRICS ncm; 1156 base::win::GetNonClientMetrics(&ncm); 1157 l10n_util::AdjustUIFont(&(ncm.lfCaptionFont)); 1158 base::win::ScopedHFONT caption_font(CreateFontIndirect(&(ncm.lfCaptionFont))); 1159 return gfx::FontList(gfx::Font(caption_font)); 1160 #else 1161 return gfx::FontList(); 1162 #endif 1163 } 1164 1165 } // namespace internal 1166 } // namespace views 1167