Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / aura / window.cc
blob5c05d4440acd1f96f612415f583f5fb75170b244
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/aura/window.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "ui/aura/client/capture_client.h"
17 #include "ui/aura/client/cursor_client.h"
18 #include "ui/aura/client/event_client.h"
19 #include "ui/aura/client/focus_client.h"
20 #include "ui/aura/client/screen_position_client.h"
21 #include "ui/aura/client/visibility_client.h"
22 #include "ui/aura/client/window_stacking_client.h"
23 #include "ui/aura/env.h"
24 #include "ui/aura/layout_manager.h"
25 #include "ui/aura/window_delegate.h"
26 #include "ui/aura/window_event_dispatcher.h"
27 #include "ui/aura/window_observer.h"
28 #include "ui/aura/window_tracker.h"
29 #include "ui/aura/window_tree_host.h"
30 #include "ui/compositor/compositor.h"
31 #include "ui/compositor/layer.h"
32 #include "ui/events/event_target_iterator.h"
33 #include "ui/gfx/canvas.h"
34 #include "ui/gfx/path.h"
35 #include "ui/gfx/scoped_canvas.h"
36 #include "ui/gfx/screen.h"
38 namespace aura {
40 namespace {
42 ui::LayerType WindowLayerTypeToUILayerType(WindowLayerType window_layer_type) {
43 switch (window_layer_type) {
44 case WINDOW_LAYER_NONE:
45 break;
46 case WINDOW_LAYER_NOT_DRAWN:
47 return ui::LAYER_NOT_DRAWN;
48 case WINDOW_LAYER_TEXTURED:
49 return ui::LAYER_TEXTURED;
50 case WINDOW_LAYER_SOLID_COLOR:
51 return ui::LAYER_SOLID_COLOR;
53 NOTREACHED();
54 return ui::LAYER_NOT_DRAWN;
57 // Used when searching for a Window to stack relative to.
58 template <class T>
59 T IteratorForDirectionBegin(aura::Window* window);
61 template <>
62 Window::Windows::const_iterator IteratorForDirectionBegin(
63 aura::Window* window) {
64 return window->children().begin();
67 template <>
68 Window::Windows::const_reverse_iterator IteratorForDirectionBegin(
69 aura::Window* window) {
70 return window->children().rbegin();
73 template <class T>
74 T IteratorForDirectionEnd(aura::Window* window);
76 template <>
77 Window::Windows::const_iterator IteratorForDirectionEnd(aura::Window* window) {
78 return window->children().end();
81 template <>
82 Window::Windows::const_reverse_iterator IteratorForDirectionEnd(
83 aura::Window* window) {
84 return window->children().rend();
87 // Depth first search for the first Window with a layer to stack relative
88 // to. Starts at target. Does not descend into |ignore|.
89 template <class T>
90 ui::Layer* FindStackingTargetLayerDown(aura::Window* target,
91 aura::Window* ignore) {
92 if (target == ignore)
93 return NULL;
95 if (target->layer())
96 return target->layer();
98 for (T i = IteratorForDirectionBegin<T>(target);
99 i != IteratorForDirectionEnd<T>(target); ++i) {
100 ui::Layer* layer = FindStackingTargetLayerDown<T>(*i, ignore);
101 if (layer)
102 return layer;
104 return NULL;
107 // Depth first search through the siblings of |target||. This does not search
108 // all the siblings, only those before/after |target| (depening upon the
109 // template type) and ignoring |ignore|. Returns the Layer of the first Window
110 // encountered with a Layer.
111 template <class T>
112 ui::Layer* FindStackingLayerInSiblings(aura::Window* target,
113 aura::Window* ignore) {
114 aura::Window* parent = target->parent();
115 for (T i = std::find(IteratorForDirectionBegin<T>(parent),
116 IteratorForDirectionEnd<T>(parent), target);
117 i != IteratorForDirectionEnd<T>(parent); ++i) {
118 ui::Layer* layer = FindStackingTargetLayerDown<T>(*i, ignore);
119 if (layer)
120 return layer;
122 return NULL;
125 // Returns the first Window that has a Layer. This does a depth first search
126 // through the descendants of |target| first, then ascends up doing a depth
127 // first search through siblings of all ancestors until a Layer is found or an
128 // ancestor with a layer is found. This is intended to locate a layer to stack
129 // other layers relative to.
130 template <class T>
131 ui::Layer* FindStackingTargetLayer(aura::Window* target, aura::Window* ignore) {
132 ui::Layer* result = FindStackingTargetLayerDown<T>(target, ignore);
133 if (result)
134 return result;
135 while (target->parent()) {
136 ui::Layer* result = FindStackingLayerInSiblings<T>(target, ignore);
137 if (result)
138 return result;
139 target = target->parent();
140 if (target->layer())
141 return NULL;
143 return NULL;
146 // Does a depth first search for all descendants of |child| that have layers.
147 // This stops at any descendants that have layers (and adds them to |layers|).
148 void GetLayersToStack(aura::Window* child, std::vector<ui::Layer*>* layers) {
149 if (child->layer()) {
150 layers->push_back(child->layer());
151 return;
153 for (size_t i = 0; i < child->children().size(); ++i)
154 GetLayersToStack(child->children()[i], layers);
157 } // namespace
159 class ScopedCursorHider {
160 public:
161 explicit ScopedCursorHider(Window* window)
162 : window_(window),
163 hid_cursor_(false) {
164 if (!window_->IsRootWindow())
165 return;
166 const bool cursor_is_in_bounds = window_->GetBoundsInScreen().Contains(
167 Env::GetInstance()->last_mouse_location());
168 client::CursorClient* cursor_client = client::GetCursorClient(window_);
169 if (cursor_is_in_bounds && cursor_client &&
170 cursor_client->IsCursorVisible()) {
171 cursor_client->HideCursor();
172 hid_cursor_ = true;
175 ~ScopedCursorHider() {
176 if (!window_->IsRootWindow())
177 return;
179 // Update the device scale factor of the cursor client only when the last
180 // mouse location is on this root window.
181 if (hid_cursor_) {
182 client::CursorClient* cursor_client = client::GetCursorClient(window_);
183 if (cursor_client) {
184 const gfx::Display& display =
185 gfx::Screen::GetScreenFor(window_)->GetDisplayNearestWindow(
186 window_);
187 cursor_client->SetDisplay(display);
188 cursor_client->ShowCursor();
193 private:
194 Window* window_;
195 bool hid_cursor_;
197 DISALLOW_COPY_AND_ASSIGN(ScopedCursorHider);
200 Window::Window(WindowDelegate* delegate)
201 : host_(NULL),
202 type_(ui::wm::WINDOW_TYPE_UNKNOWN),
203 owned_by_parent_(true),
204 delegate_(delegate),
205 parent_(NULL),
206 visible_(false),
207 id_(-1),
208 transparent_(false),
209 user_data_(NULL),
210 ignore_events_(false),
211 // Don't notify newly added observers during notification. This causes
212 // problems for code that adds an observer as part of an observer
213 // notification (such as the workspace code).
214 observers_(ObserverList<WindowObserver>::NOTIFY_EXISTING_ONLY) {
215 set_target_handler(delegate_);
218 Window::~Window() {
219 // |layer()| can be NULL during tests, or if this Window is layerless.
220 if (layer()) {
221 if (layer()->owner() == this)
222 layer()->CompleteAllAnimations();
223 layer()->SuppressPaint();
226 // Let the delegate know we're in the processing of destroying.
227 if (delegate_)
228 delegate_->OnWindowDestroying(this);
229 FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowDestroying(this));
231 // TODO(beng): See comment in window_event_dispatcher.h. This shouldn't be
232 // necessary but unfortunately is right now due to ordering
233 // peculiarities. WED must be notified _after_ other observers
234 // are notified of pending teardown but before the hierarchy
235 // is actually torn down.
236 WindowTreeHost* host = GetHost();
237 if (host)
238 host->dispatcher()->OnPostNotifiedWindowDestroying(this);
240 // The window should have already had its state cleaned up in
241 // WindowEventDispatcher::OnWindowHidden(), but there have been some crashes
242 // involving windows being destroyed without being hidden first. See
243 // crbug.com/342040. This should help us debug the issue. TODO(tdresser):
244 // remove this once we determine why we have windows that are destroyed
245 // without being hidden.
246 bool window_incorrectly_cleaned_up = CleanupGestureState();
247 CHECK(!window_incorrectly_cleaned_up);
249 // Then destroy the children.
250 RemoveOrDestroyChildren();
252 // The window needs to be removed from the parent before calling the
253 // WindowDestroyed callbacks of delegate and the observers.
254 if (parent_)
255 parent_->RemoveChild(this);
257 if (delegate_)
258 delegate_->OnWindowDestroyed(this);
259 ObserverListBase<WindowObserver>::Iterator iter(observers_);
260 for (WindowObserver* observer = iter.GetNext(); observer;
261 observer = iter.GetNext()) {
262 RemoveObserver(observer);
263 observer->OnWindowDestroyed(this);
266 // Clear properties.
267 for (std::map<const void*, Value>::const_iterator iter = prop_map_.begin();
268 iter != prop_map_.end();
269 ++iter) {
270 if (iter->second.deallocator)
271 (*iter->second.deallocator)(iter->second.value);
273 prop_map_.clear();
275 // If we have layer it will either be destroyed by |layer_owner_|'s dtor, or
276 // by whoever acquired it. We don't have a layer if Init() wasn't invoked or
277 // we are layerless.
278 if (layer())
279 layer()->set_delegate(NULL);
280 DestroyLayer();
283 void Window::Init(WindowLayerType window_layer_type) {
284 if (window_layer_type != WINDOW_LAYER_NONE) {
285 SetLayer(new ui::Layer(WindowLayerTypeToUILayerType(window_layer_type)));
286 layer()->SetVisible(false);
287 layer()->set_delegate(this);
288 UpdateLayerName();
289 layer()->SetFillsBoundsOpaquely(!transparent_);
292 Env::GetInstance()->NotifyWindowInitialized(this);
295 void Window::SetType(ui::wm::WindowType type) {
296 // Cannot change type after the window is initialized.
297 DCHECK(!layer());
298 type_ = type;
301 void Window::SetName(const std::string& name) {
302 name_ = name;
304 if (layer())
305 UpdateLayerName();
308 void Window::SetTitle(const base::string16& title) {
309 title_ = title;
310 FOR_EACH_OBSERVER(WindowObserver,
311 observers_,
312 OnWindowTitleChanged(this));
315 void Window::SetTransparent(bool transparent) {
316 transparent_ = transparent;
317 if (layer())
318 layer()->SetFillsBoundsOpaquely(!transparent_);
321 void Window::SetFillsBoundsCompletely(bool fills_bounds) {
322 if (layer())
323 layer()->SetFillsBoundsCompletely(fills_bounds);
326 Window* Window::GetRootWindow() {
327 return const_cast<Window*>(
328 static_cast<const Window*>(this)->GetRootWindow());
331 const Window* Window::GetRootWindow() const {
332 return IsRootWindow() ? this : parent_ ? parent_->GetRootWindow() : NULL;
335 WindowTreeHost* Window::GetHost() {
336 return const_cast<WindowTreeHost*>(const_cast<const Window*>(this)->
337 GetHost());
340 const WindowTreeHost* Window::GetHost() const {
341 const Window* root_window = GetRootWindow();
342 return root_window ? root_window->host_ : NULL;
345 void Window::Show() {
346 if (layer()) {
347 DCHECK_EQ(visible_, layer()->GetTargetVisibility());
348 // It is not allowed that a window is visible but the layers alpha is fully
349 // transparent since the window would still be considered to be active but
350 // could not be seen.
351 DCHECK(!(visible_ && layer()->GetTargetOpacity() == 0.0f));
353 SetVisible(true);
356 void Window::Hide() {
357 // RootWindow::OnVisibilityChanged will call ReleaseCapture.
358 SetVisible(false);
361 bool Window::IsVisible() const {
362 // Layer visibility can be inconsistent with window visibility, for example
363 // when a Window is hidden, we want this function to return false immediately
364 // after, even though the client may decide to animate the hide effect (and
365 // so the layer will be visible for some time after Hide() is called).
366 for (const Window* window = this; window; window = window->parent()) {
367 if (!window->visible_)
368 return false;
369 if (window->layer())
370 return window->layer()->IsDrawn();
372 return false;
375 gfx::Rect Window::GetBoundsInRootWindow() const {
376 // TODO(beng): There may be a better way to handle this, and the existing code
377 // is likely wrong anyway in a multi-display world, but this will
378 // do for now.
379 if (!GetRootWindow())
380 return bounds();
381 gfx::Point origin = bounds().origin();
382 ConvertPointToTarget(parent_, GetRootWindow(), &origin);
383 return gfx::Rect(origin, bounds().size());
386 gfx::Rect Window::GetBoundsInScreen() const {
387 gfx::Rect bounds(GetBoundsInRootWindow());
388 const Window* root = GetRootWindow();
389 if (root) {
390 aura::client::ScreenPositionClient* screen_position_client =
391 aura::client::GetScreenPositionClient(root);
392 if (screen_position_client) {
393 gfx::Point origin = bounds.origin();
394 screen_position_client->ConvertPointToScreen(root, &origin);
395 bounds.set_origin(origin);
398 return bounds;
401 void Window::SetTransform(const gfx::Transform& transform) {
402 if (!layer()) {
403 // Transforms aren't supported on layerless windows.
404 NOTREACHED();
405 return;
407 FOR_EACH_OBSERVER(WindowObserver, observers_,
408 OnWindowTransforming(this));
409 layer()->SetTransform(transform);
410 FOR_EACH_OBSERVER(WindowObserver, observers_,
411 OnWindowTransformed(this));
414 void Window::SetLayoutManager(LayoutManager* layout_manager) {
415 if (layout_manager == layout_manager_)
416 return;
417 layout_manager_.reset(layout_manager);
418 if (!layout_manager)
419 return;
420 // If we're changing to a new layout manager, ensure it is aware of all the
421 // existing child windows.
422 for (Windows::const_iterator it = children_.begin();
423 it != children_.end();
424 ++it)
425 layout_manager_->OnWindowAddedToLayout(*it);
428 scoped_ptr<ui::EventTargeter>
429 Window::SetEventTargeter(scoped_ptr<ui::EventTargeter> targeter) {
430 scoped_ptr<ui::EventTargeter> old_targeter = targeter_.Pass();
431 targeter_ = targeter.Pass();
432 return old_targeter.Pass();
435 void Window::SetBounds(const gfx::Rect& new_bounds) {
436 if (parent_ && parent_->layout_manager())
437 parent_->layout_manager()->SetChildBounds(this, new_bounds);
438 else {
439 // Ensure we don't go smaller than our minimum bounds.
440 gfx::Rect final_bounds(new_bounds);
441 if (delegate_) {
442 const gfx::Size& min_size = delegate_->GetMinimumSize();
443 final_bounds.set_width(std::max(min_size.width(), final_bounds.width()));
444 final_bounds.set_height(std::max(min_size.height(),
445 final_bounds.height()));
447 SetBoundsInternal(final_bounds);
451 void Window::SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen,
452 const gfx::Display& dst_display) {
453 Window* root = GetRootWindow();
454 if (root) {
455 gfx::Point origin = new_bounds_in_screen.origin();
456 aura::client::ScreenPositionClient* screen_position_client =
457 aura::client::GetScreenPositionClient(root);
458 screen_position_client->SetBounds(this, new_bounds_in_screen, dst_display);
459 return;
461 SetBounds(new_bounds_in_screen);
464 gfx::Rect Window::GetTargetBounds() const {
465 if (!layer())
466 return bounds();
468 if (!parent_ || parent_->layer())
469 return layer()->GetTargetBounds();
471 // We have a layer but our parent (who is valid) doesn't. This means the
472 // coordinates of the layer are relative to the first ancestor with a layer;
473 // convert to be relative to parent.
474 gfx::Vector2d offset;
475 const aura::Window* ancestor_with_layer =
476 parent_->GetAncestorWithLayer(&offset);
477 if (!ancestor_with_layer)
478 return layer()->GetTargetBounds();
480 gfx::Rect layer_target_bounds = layer()->GetTargetBounds();
481 layer_target_bounds -= offset;
482 return layer_target_bounds;
485 void Window::SchedulePaintInRect(const gfx::Rect& rect) {
486 if (!layer() && parent_) {
487 // Notification of paint scheduled happens for the window with a layer.
488 gfx::Rect parent_rect(bounds().size());
489 parent_rect.Intersect(rect);
490 if (!parent_rect.IsEmpty()) {
491 parent_rect.Offset(bounds().origin().OffsetFromOrigin());
492 parent_->SchedulePaintInRect(parent_rect);
494 } else if (layer()) {
495 layer()->SchedulePaint(rect);
499 void Window::StackChildAtTop(Window* child) {
500 if (children_.size() <= 1 || child == children_.back())
501 return; // In the front already.
502 StackChildAbove(child, children_.back());
505 void Window::StackChildAbove(Window* child, Window* target) {
506 StackChildRelativeTo(child, target, STACK_ABOVE);
509 void Window::StackChildAtBottom(Window* child) {
510 if (children_.size() <= 1 || child == children_.front())
511 return; // At the bottom already.
512 StackChildBelow(child, children_.front());
515 void Window::StackChildBelow(Window* child, Window* target) {
516 StackChildRelativeTo(child, target, STACK_BELOW);
519 void Window::AddChild(Window* child) {
520 WindowObserver::HierarchyChangeParams params;
521 params.target = child;
522 params.new_parent = this;
523 params.old_parent = child->parent();
524 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
525 NotifyWindowHierarchyChange(params);
527 Window* old_root = child->GetRootWindow();
529 DCHECK(std::find(children_.begin(), children_.end(), child) ==
530 children_.end());
531 if (child->parent())
532 child->parent()->RemoveChildImpl(child, this);
534 gfx::Vector2d offset;
535 aura::Window* ancestor_with_layer = GetAncestorWithLayer(&offset);
537 child->parent_ = this;
539 if (ancestor_with_layer) {
540 offset += child->bounds().OffsetFromOrigin();
541 child->ReparentLayers(ancestor_with_layer->layer(), offset);
544 children_.push_back(child);
545 if (layout_manager_)
546 layout_manager_->OnWindowAddedToLayout(child);
547 FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowAdded(child));
548 child->OnParentChanged();
550 Window* root_window = GetRootWindow();
551 if (root_window && old_root != root_window) {
552 root_window->GetHost()->dispatcher()->OnWindowAddedToRootWindow(child);
553 child->NotifyAddedToRootWindow();
556 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
557 NotifyWindowHierarchyChange(params);
560 void Window::RemoveChild(Window* child) {
561 WindowObserver::HierarchyChangeParams params;
562 params.target = child;
563 params.new_parent = NULL;
564 params.old_parent = this;
565 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
566 NotifyWindowHierarchyChange(params);
568 RemoveChildImpl(child, NULL);
570 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
571 NotifyWindowHierarchyChange(params);
574 bool Window::Contains(const Window* other) const {
575 for (const Window* parent = other; parent; parent = parent->parent_) {
576 if (parent == this)
577 return true;
579 return false;
582 Window* Window::GetChildById(int id) {
583 return const_cast<Window*>(const_cast<const Window*>(this)->GetChildById(id));
586 const Window* Window::GetChildById(int id) const {
587 Windows::const_iterator i;
588 for (i = children_.begin(); i != children_.end(); ++i) {
589 if ((*i)->id() == id)
590 return *i;
591 const Window* result = (*i)->GetChildById(id);
592 if (result)
593 return result;
595 return NULL;
598 // static
599 void Window::ConvertPointToTarget(const Window* source,
600 const Window* target,
601 gfx::Point* point) {
602 if (!source)
603 return;
604 if (source->GetRootWindow() != target->GetRootWindow()) {
605 client::ScreenPositionClient* source_client =
606 client::GetScreenPositionClient(source->GetRootWindow());
607 // |source_client| can be NULL in tests.
608 if (source_client)
609 source_client->ConvertPointToScreen(source, point);
611 client::ScreenPositionClient* target_client =
612 client::GetScreenPositionClient(target->GetRootWindow());
613 // |target_client| can be NULL in tests.
614 if (target_client)
615 target_client->ConvertPointFromScreen(target, point);
616 } else if ((source != target) && (!source->layer() || !target->layer())) {
617 if (!source->layer()) {
618 gfx::Vector2d offset_to_layer;
619 source = source->GetAncestorWithLayer(&offset_to_layer);
620 *point += offset_to_layer;
622 if (!target->layer()) {
623 gfx::Vector2d offset_to_layer;
624 target = target->GetAncestorWithLayer(&offset_to_layer);
625 *point -= offset_to_layer;
627 ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point);
628 } else {
629 ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point);
633 // static
634 void Window::ConvertRectToTarget(const Window* source,
635 const Window* target,
636 gfx::Rect* rect) {
637 DCHECK(rect);
638 gfx::Point origin = rect->origin();
639 ConvertPointToTarget(source, target, &origin);
640 rect->set_origin(origin);
643 void Window::MoveCursorTo(const gfx::Point& point_in_window) {
644 Window* root_window = GetRootWindow();
645 DCHECK(root_window);
646 gfx::Point point_in_root(point_in_window);
647 ConvertPointToTarget(this, root_window, &point_in_root);
648 root_window->GetHost()->MoveCursorTo(point_in_root);
651 gfx::NativeCursor Window::GetCursor(const gfx::Point& point) const {
652 return delegate_ ? delegate_->GetCursor(point) : gfx::kNullCursor;
655 void Window::AddObserver(WindowObserver* observer) {
656 observer->OnObservingWindow(this);
657 observers_.AddObserver(observer);
660 void Window::RemoveObserver(WindowObserver* observer) {
661 observer->OnUnobservingWindow(this);
662 observers_.RemoveObserver(observer);
665 bool Window::HasObserver(WindowObserver* observer) {
666 return observers_.HasObserver(observer);
669 bool Window::ContainsPointInRoot(const gfx::Point& point_in_root) const {
670 const Window* root_window = GetRootWindow();
671 if (!root_window)
672 return false;
673 gfx::Point local_point(point_in_root);
674 ConvertPointToTarget(root_window, this, &local_point);
675 return gfx::Rect(GetTargetBounds().size()).Contains(local_point);
678 bool Window::ContainsPoint(const gfx::Point& local_point) const {
679 return gfx::Rect(bounds().size()).Contains(local_point);
682 Window* Window::GetEventHandlerForPoint(const gfx::Point& local_point) {
683 return GetWindowForPoint(local_point, true, true);
686 Window* Window::GetTopWindowContainingPoint(const gfx::Point& local_point) {
687 return GetWindowForPoint(local_point, false, false);
690 Window* Window::GetToplevelWindow() {
691 Window* topmost_window_with_delegate = NULL;
692 for (aura::Window* window = this; window != NULL; window = window->parent()) {
693 if (window->delegate())
694 topmost_window_with_delegate = window;
696 return topmost_window_with_delegate;
699 void Window::Focus() {
700 client::FocusClient* client = client::GetFocusClient(this);
701 DCHECK(client);
702 client->FocusWindow(this);
705 void Window::Blur() {
706 client::FocusClient* client = client::GetFocusClient(this);
707 DCHECK(client);
708 client->FocusWindow(NULL);
711 bool Window::HasFocus() const {
712 client::FocusClient* client = client::GetFocusClient(this);
713 return client && client->GetFocusedWindow() == this;
716 bool Window::CanFocus() const {
717 if (IsRootWindow())
718 return IsVisible();
720 // NOTE: as part of focusing the window the ActivationClient may make the
721 // window visible (by way of making a hidden ancestor visible). For this
722 // reason we can't check visibility here and assume the client is doing it.
723 if (!parent_ || (delegate_ && !delegate_->CanFocus()))
724 return false;
726 // The client may forbid certain windows from receiving focus at a given point
727 // in time.
728 client::EventClient* client = client::GetEventClient(GetRootWindow());
729 if (client && !client->CanProcessEventsWithinSubtree(this))
730 return false;
732 return parent_->CanFocus();
735 bool Window::CanReceiveEvents() const {
736 if (IsRootWindow())
737 return IsVisible();
739 // The client may forbid certain windows from receiving events at a given
740 // point in time.
741 client::EventClient* client = client::GetEventClient(GetRootWindow());
742 if (client && !client->CanProcessEventsWithinSubtree(this))
743 return false;
745 return parent_ && IsVisible() && parent_->CanReceiveEvents();
748 void Window::SetCapture() {
749 if (!IsVisible())
750 return;
752 Window* root_window = GetRootWindow();
753 if (!root_window)
754 return;
755 client::CaptureClient* capture_client = client::GetCaptureClient(root_window);
756 if (!capture_client)
757 return;
758 client::GetCaptureClient(root_window)->SetCapture(this);
761 void Window::ReleaseCapture() {
762 Window* root_window = GetRootWindow();
763 if (!root_window)
764 return;
765 client::CaptureClient* capture_client = client::GetCaptureClient(root_window);
766 if (!capture_client)
767 return;
768 client::GetCaptureClient(root_window)->ReleaseCapture(this);
771 bool Window::HasCapture() {
772 Window* root_window = GetRootWindow();
773 if (!root_window)
774 return false;
775 client::CaptureClient* capture_client = client::GetCaptureClient(root_window);
776 return capture_client && capture_client->GetCaptureWindow() == this;
779 void Window::SuppressPaint() {
780 if (layer())
781 layer()->SuppressPaint();
784 // {Set,Get,Clear}Property are implemented in window_property.h.
786 void Window::SetNativeWindowProperty(const char* key, void* value) {
787 SetPropertyInternal(
788 key, key, NULL, reinterpret_cast<int64>(value), 0);
791 void* Window::GetNativeWindowProperty(const char* key) const {
792 return reinterpret_cast<void*>(GetPropertyInternal(key, 0));
795 void Window::OnDeviceScaleFactorChanged(float device_scale_factor) {
796 ScopedCursorHider hider(this);
797 if (delegate_)
798 delegate_->OnDeviceScaleFactorChanged(device_scale_factor);
801 #if !defined(NDEBUG)
802 std::string Window::GetDebugInfo() const {
803 return base::StringPrintf(
804 "%s<%d> bounds(%d, %d, %d, %d) %s %s opacity=%.1f",
805 name().empty() ? "Unknown" : name().c_str(), id(),
806 bounds().x(), bounds().y(), bounds().width(), bounds().height(),
807 visible_ ? "WindowVisible" : "WindowHidden",
808 layer() ?
809 (layer()->GetTargetVisibility() ? "LayerVisible" : "LayerHidden") :
810 "NoLayer",
811 layer() ? layer()->opacity() : 1.0f);
814 void Window::PrintWindowHierarchy(int depth) const {
815 VLOG(0) << base::StringPrintf(
816 "%*s%s", depth * 2, "", GetDebugInfo().c_str());
817 for (Windows::const_iterator it = children_.begin();
818 it != children_.end(); ++it) {
819 Window* child = *it;
820 child->PrintWindowHierarchy(depth + 1);
823 #endif
825 void Window::RemoveOrDestroyChildren() {
826 while (!children_.empty()) {
827 Window* child = children_[0];
828 if (child->owned_by_parent_) {
829 delete child;
830 // Deleting the child so remove it from out children_ list.
831 DCHECK(std::find(children_.begin(), children_.end(), child) ==
832 children_.end());
833 } else {
834 // Even if we can't delete the child, we still need to remove it from the
835 // parent so that relevant bookkeeping (parent_ back-pointers etc) are
836 // updated.
837 RemoveChild(child);
842 ///////////////////////////////////////////////////////////////////////////////
843 // Window, private:
845 int64 Window::SetPropertyInternal(const void* key,
846 const char* name,
847 PropertyDeallocator deallocator,
848 int64 value,
849 int64 default_value) {
850 int64 old = GetPropertyInternal(key, default_value);
851 if (value == default_value) {
852 prop_map_.erase(key);
853 } else {
854 Value prop_value;
855 prop_value.name = name;
856 prop_value.value = value;
857 prop_value.deallocator = deallocator;
858 prop_map_[key] = prop_value;
860 FOR_EACH_OBSERVER(WindowObserver, observers_,
861 OnWindowPropertyChanged(this, key, old));
862 return old;
865 int64 Window::GetPropertyInternal(const void* key,
866 int64 default_value) const {
867 std::map<const void*, Value>::const_iterator iter = prop_map_.find(key);
868 if (iter == prop_map_.end())
869 return default_value;
870 return iter->second.value;
873 bool Window::HitTest(const gfx::Point& local_point) {
874 gfx::Rect local_bounds(bounds().size());
875 if (!delegate_ || !delegate_->HasHitTestMask())
876 return local_bounds.Contains(local_point);
878 gfx::Path mask;
879 delegate_->GetHitTestMask(&mask);
881 SkRegion clip_region;
882 clip_region.setRect(local_bounds.x(), local_bounds.y(),
883 local_bounds.width(), local_bounds.height());
884 SkRegion mask_region;
885 return mask_region.setPath(mask, clip_region) &&
886 mask_region.contains(local_point.x(), local_point.y());
889 void Window::SetBoundsInternal(const gfx::Rect& new_bounds) {
890 gfx::Rect actual_new_bounds(new_bounds);
891 gfx::Rect old_bounds = GetTargetBounds();
893 // Always need to set the layer's bounds -- even if it is to the same thing.
894 // This may cause important side effects such as stopping animation.
895 if (!layer()) {
896 const gfx::Vector2d origin_delta = new_bounds.OffsetFromOrigin() -
897 bounds_.OffsetFromOrigin();
898 bounds_ = new_bounds;
899 OffsetLayerBounds(origin_delta);
900 } else {
901 if (parent_ && !parent_->layer()) {
902 gfx::Vector2d offset;
903 const aura::Window* ancestor_with_layer =
904 parent_->GetAncestorWithLayer(&offset);
905 if (ancestor_with_layer)
906 actual_new_bounds.Offset(offset);
908 layer()->SetBounds(actual_new_bounds);
911 // If we are currently not the layer's delegate, we will not get bounds
912 // changed notification from the layer (this typically happens after animating
913 // hidden). We must notify ourselves.
914 if (!layer() || layer()->delegate() != this)
915 OnWindowBoundsChanged(old_bounds);
918 void Window::SetVisible(bool visible) {
919 if ((layer() && visible == layer()->GetTargetVisibility()) ||
920 (!layer() && visible == visible_))
921 return; // No change.
923 FOR_EACH_OBSERVER(WindowObserver, observers_,
924 OnWindowVisibilityChanging(this, visible));
926 client::VisibilityClient* visibility_client =
927 client::GetVisibilityClient(this);
928 if (visibility_client)
929 visibility_client->UpdateLayerVisibility(this, visible);
930 else if (layer())
931 layer()->SetVisible(visible);
932 visible_ = visible;
933 SchedulePaint();
934 if (parent_ && parent_->layout_manager_)
935 parent_->layout_manager_->OnChildWindowVisibilityChanged(this, visible);
937 if (delegate_)
938 delegate_->OnWindowTargetVisibilityChanged(visible);
940 NotifyWindowVisibilityChanged(this, visible);
943 void Window::SchedulePaint() {
944 SchedulePaintInRect(gfx::Rect(0, 0, bounds().width(), bounds().height()));
947 void Window::Paint(gfx::Canvas* canvas) {
948 if (delegate_)
949 delegate_->OnPaint(canvas);
950 PaintLayerlessChildren(canvas);
953 void Window::PaintLayerlessChildren(gfx::Canvas* canvas) {
954 for (size_t i = 0, count = children_.size(); i < count; ++i) {
955 Window* child = children_[i];
956 if (!child->layer() && child->visible_) {
957 gfx::ScopedCanvas scoped_canvas(canvas);
958 canvas->ClipRect(child->bounds());
959 if (!canvas->IsClipEmpty()) {
960 canvas->Translate(child->bounds().OffsetFromOrigin());
961 child->Paint(canvas);
967 Window* Window::GetWindowForPoint(const gfx::Point& local_point,
968 bool return_tightest,
969 bool for_event_handling) {
970 if (!IsVisible())
971 return NULL;
973 if ((for_event_handling && !HitTest(local_point)) ||
974 (!for_event_handling && !ContainsPoint(local_point)))
975 return NULL;
977 // Check if I should claim this event and not pass it to my children because
978 // the location is inside my hit test override area. For details, see
979 // set_hit_test_bounds_override_inner().
980 if (for_event_handling && !hit_test_bounds_override_inner_.empty()) {
981 gfx::Rect inset_local_bounds(gfx::Point(), bounds().size());
982 inset_local_bounds.Inset(hit_test_bounds_override_inner_);
983 // We know we're inside the normal local bounds, so if we're outside the
984 // inset bounds we must be in the special hit test override area.
985 DCHECK(HitTest(local_point));
986 if (!inset_local_bounds.Contains(local_point))
987 return delegate_ ? this : NULL;
990 if (!return_tightest && delegate_)
991 return this;
993 for (Windows::const_reverse_iterator it = children_.rbegin(),
994 rend = children_.rend();
995 it != rend; ++it) {
996 Window* child = *it;
998 if (for_event_handling) {
999 if (child->ignore_events_)
1000 continue;
1001 // The client may not allow events to be processed by certain subtrees.
1002 client::EventClient* client = client::GetEventClient(GetRootWindow());
1003 if (client && !client->CanProcessEventsWithinSubtree(child))
1004 continue;
1005 if (delegate_ && !delegate_->ShouldDescendIntoChildForEventHandling(
1006 child, local_point))
1007 continue;
1010 gfx::Point point_in_child_coords(local_point);
1011 ConvertPointToTarget(this, child, &point_in_child_coords);
1012 Window* match = child->GetWindowForPoint(point_in_child_coords,
1013 return_tightest,
1014 for_event_handling);
1015 if (match)
1016 return match;
1019 return delegate_ ? this : NULL;
1022 void Window::RemoveChildImpl(Window* child, Window* new_parent) {
1023 if (layout_manager_)
1024 layout_manager_->OnWillRemoveWindowFromLayout(child);
1025 FOR_EACH_OBSERVER(WindowObserver, observers_, OnWillRemoveWindow(child));
1026 Window* root_window = child->GetRootWindow();
1027 Window* new_root_window = new_parent ? new_parent->GetRootWindow() : NULL;
1028 if (root_window && root_window != new_root_window)
1029 child->NotifyRemovingFromRootWindow(new_root_window);
1031 gfx::Vector2d offset;
1032 GetAncestorWithLayer(&offset);
1033 child->UnparentLayers(!layer(), offset);
1034 child->parent_ = NULL;
1035 Windows::iterator i = std::find(children_.begin(), children_.end(), child);
1036 DCHECK(i != children_.end());
1037 children_.erase(i);
1038 child->OnParentChanged();
1039 if (layout_manager_)
1040 layout_manager_->OnWindowRemovedFromLayout(child);
1043 void Window::UnparentLayers(bool has_layerless_ancestor,
1044 const gfx::Vector2d& offset) {
1045 if (!layer()) {
1046 const gfx::Vector2d new_offset = offset + bounds().OffsetFromOrigin();
1047 for (size_t i = 0; i < children_.size(); ++i) {
1048 children_[i]->UnparentLayers(true, new_offset);
1050 } else {
1051 // Only remove the layer if we still own it. Someone else may have acquired
1052 // ownership of it via AcquireLayer() and may expect the hierarchy to go
1053 // unchanged as the Window is destroyed.
1054 if (OwnsLayer()) {
1055 if (layer()->parent())
1056 layer()->parent()->Remove(layer());
1057 if (has_layerless_ancestor) {
1058 const gfx::Rect real_bounds(bounds_);
1059 gfx::Rect layer_bounds(layer()->bounds());
1060 layer_bounds.Offset(-offset);
1061 layer()->SetBounds(layer_bounds);
1062 bounds_ = real_bounds;
1068 void Window::ReparentLayers(ui::Layer* parent_layer,
1069 const gfx::Vector2d& offset) {
1070 if (!layer()) {
1071 for (size_t i = 0; i < children_.size(); ++i) {
1072 children_[i]->ReparentLayers(
1073 parent_layer,
1074 offset + children_[i]->bounds().OffsetFromOrigin());
1076 } else {
1077 const gfx::Rect real_bounds(bounds());
1078 parent_layer->Add(layer());
1079 gfx::Rect layer_bounds(layer()->bounds().size());
1080 layer_bounds += offset;
1081 layer()->SetBounds(layer_bounds);
1082 bounds_ = real_bounds;
1086 void Window::OffsetLayerBounds(const gfx::Vector2d& offset) {
1087 if (!layer()) {
1088 for (size_t i = 0; i < children_.size(); ++i)
1089 children_[i]->OffsetLayerBounds(offset);
1090 } else {
1091 gfx::Rect layer_bounds(layer()->bounds());
1092 layer_bounds += offset;
1093 layer()->SetBounds(layer_bounds);
1097 void Window::OnParentChanged() {
1098 FOR_EACH_OBSERVER(
1099 WindowObserver, observers_, OnWindowParentChanged(this, parent_));
1102 void Window::StackChildRelativeTo(Window* child,
1103 Window* target,
1104 StackDirection direction) {
1105 DCHECK_NE(child, target);
1106 DCHECK(child);
1107 DCHECK(target);
1108 DCHECK_EQ(this, child->parent());
1109 DCHECK_EQ(this, target->parent());
1111 client::WindowStackingClient* stacking_client =
1112 client::GetWindowStackingClient();
1113 if (stacking_client &&
1114 !stacking_client->AdjustStacking(&child, &target, &direction))
1115 return;
1117 const size_t child_i =
1118 std::find(children_.begin(), children_.end(), child) - children_.begin();
1119 const size_t target_i =
1120 std::find(children_.begin(), children_.end(), target) - children_.begin();
1122 // Don't move the child if it is already in the right place.
1123 if ((direction == STACK_ABOVE && child_i == target_i + 1) ||
1124 (direction == STACK_BELOW && child_i + 1 == target_i))
1125 return;
1127 const size_t dest_i =
1128 direction == STACK_ABOVE ?
1129 (child_i < target_i ? target_i : target_i + 1) :
1130 (child_i < target_i ? target_i - 1 : target_i);
1131 children_.erase(children_.begin() + child_i);
1132 children_.insert(children_.begin() + dest_i, child);
1134 StackChildLayerRelativeTo(child, target, direction);
1136 child->OnStackingChanged();
1139 void Window::StackChildLayerRelativeTo(Window* child,
1140 Window* target,
1141 StackDirection direction) {
1142 Window* ancestor_with_layer = GetAncestorWithLayer(NULL);
1143 ui::Layer* ancestor_layer =
1144 ancestor_with_layer ? ancestor_with_layer->layer() : NULL;
1145 if (!ancestor_layer)
1146 return;
1148 if (child->layer() && target->layer()) {
1149 if (direction == STACK_ABOVE)
1150 ancestor_layer->StackAbove(child->layer(), target->layer());
1151 else
1152 ancestor_layer->StackBelow(child->layer(), target->layer());
1153 return;
1155 typedef std::vector<ui::Layer*> Layers;
1156 Layers layers;
1157 GetLayersToStack(child, &layers);
1158 if (layers.empty())
1159 return;
1161 ui::Layer* target_layer;
1162 if (direction == STACK_ABOVE) {
1163 target_layer =
1164 FindStackingTargetLayer<Windows::const_reverse_iterator>(target, child);
1165 } else {
1166 target_layer =
1167 FindStackingTargetLayer<Windows::const_iterator>(target, child);
1170 if (!target_layer) {
1171 if (direction == STACK_ABOVE) {
1172 for (Layers::const_reverse_iterator i = layers.rbegin(),
1173 rend = layers.rend(); i != rend; ++i) {
1174 ancestor_layer->StackAtBottom(*i);
1176 } else {
1177 for (Layers::const_iterator i = layers.begin(); i != layers.end(); ++i)
1178 ancestor_layer->StackAtTop(*i);
1180 return;
1183 if (direction == STACK_ABOVE) {
1184 for (Layers::const_reverse_iterator i = layers.rbegin(),
1185 rend = layers.rend(); i != rend; ++i) {
1186 ancestor_layer->StackAbove(*i, target_layer);
1188 } else {
1189 for (Layers::const_iterator i = layers.begin(); i != layers.end(); ++i)
1190 ancestor_layer->StackBelow(*i, target_layer);
1194 void Window::OnStackingChanged() {
1195 FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowStackingChanged(this));
1198 void Window::NotifyRemovingFromRootWindow(Window* new_root) {
1199 FOR_EACH_OBSERVER(WindowObserver, observers_,
1200 OnWindowRemovingFromRootWindow(this, new_root));
1201 for (Window::Windows::const_iterator it = children_.begin();
1202 it != children_.end(); ++it) {
1203 (*it)->NotifyRemovingFromRootWindow(new_root);
1207 void Window::NotifyAddedToRootWindow() {
1208 FOR_EACH_OBSERVER(WindowObserver, observers_,
1209 OnWindowAddedToRootWindow(this));
1210 for (Window::Windows::const_iterator it = children_.begin();
1211 it != children_.end(); ++it) {
1212 (*it)->NotifyAddedToRootWindow();
1216 void Window::NotifyWindowHierarchyChange(
1217 const WindowObserver::HierarchyChangeParams& params) {
1218 params.target->NotifyWindowHierarchyChangeDown(params);
1219 switch (params.phase) {
1220 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING:
1221 if (params.old_parent)
1222 params.old_parent->NotifyWindowHierarchyChangeUp(params);
1223 break;
1224 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED:
1225 if (params.new_parent)
1226 params.new_parent->NotifyWindowHierarchyChangeUp(params);
1227 break;
1228 default:
1229 NOTREACHED();
1230 break;
1234 void Window::NotifyWindowHierarchyChangeDown(
1235 const WindowObserver::HierarchyChangeParams& params) {
1236 NotifyWindowHierarchyChangeAtReceiver(params);
1237 for (Window::Windows::const_iterator it = children_.begin();
1238 it != children_.end(); ++it) {
1239 (*it)->NotifyWindowHierarchyChangeDown(params);
1243 void Window::NotifyWindowHierarchyChangeUp(
1244 const WindowObserver::HierarchyChangeParams& params) {
1245 for (Window* window = this; window; window = window->parent())
1246 window->NotifyWindowHierarchyChangeAtReceiver(params);
1249 void Window::NotifyWindowHierarchyChangeAtReceiver(
1250 const WindowObserver::HierarchyChangeParams& params) {
1251 WindowObserver::HierarchyChangeParams local_params = params;
1252 local_params.receiver = this;
1254 switch (params.phase) {
1255 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING:
1256 FOR_EACH_OBSERVER(WindowObserver, observers_,
1257 OnWindowHierarchyChanging(local_params));
1258 break;
1259 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED:
1260 FOR_EACH_OBSERVER(WindowObserver, observers_,
1261 OnWindowHierarchyChanged(local_params));
1262 break;
1263 default:
1264 NOTREACHED();
1265 break;
1269 void Window::NotifyWindowVisibilityChanged(aura::Window* target,
1270 bool visible) {
1271 if (!NotifyWindowVisibilityChangedDown(target, visible)) {
1272 return; // |this| has been deleted.
1274 NotifyWindowVisibilityChangedUp(target, visible);
1277 bool Window::NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
1278 bool visible) {
1279 // |this| may be deleted during a call to OnWindowVisibilityChanged() on one
1280 // of the observers. We create an local observer for that. In that case we
1281 // exit without further access to any members.
1282 WindowTracker tracker;
1283 tracker.Add(this);
1284 FOR_EACH_OBSERVER(WindowObserver, observers_,
1285 OnWindowVisibilityChanged(target, visible));
1286 return tracker.Contains(this);
1289 bool Window::NotifyWindowVisibilityChangedDown(aura::Window* target,
1290 bool visible) {
1291 if (!NotifyWindowVisibilityChangedAtReceiver(target, visible))
1292 return false; // |this| was deleted.
1293 std::set<const Window*> child_already_processed;
1294 bool child_destroyed = false;
1295 do {
1296 child_destroyed = false;
1297 for (Window::Windows::const_iterator it = children_.begin();
1298 it != children_.end(); ++it) {
1299 if (!child_already_processed.insert(*it).second)
1300 continue;
1301 if (!(*it)->NotifyWindowVisibilityChangedDown(target, visible)) {
1302 // |*it| was deleted, |it| is invalid and |children_| has changed.
1303 // We exit the current for-loop and enter a new one.
1304 child_destroyed = true;
1305 break;
1308 } while (child_destroyed);
1309 return true;
1312 void Window::NotifyWindowVisibilityChangedUp(aura::Window* target,
1313 bool visible) {
1314 for (Window* window = this; window; window = window->parent()) {
1315 bool ret = window->NotifyWindowVisibilityChangedAtReceiver(target, visible);
1316 DCHECK(ret);
1320 void Window::OnWindowBoundsChanged(const gfx::Rect& old_bounds) {
1321 if (layer()) {
1322 bounds_ = layer()->bounds();
1323 if (parent_ && !parent_->layer()) {
1324 gfx::Vector2d offset;
1325 aura::Window* ancestor_with_layer =
1326 parent_->GetAncestorWithLayer(&offset);
1327 if (ancestor_with_layer)
1328 bounds_.Offset(-offset);
1332 if (layout_manager_)
1333 layout_manager_->OnWindowResized();
1334 if (delegate_)
1335 delegate_->OnBoundsChanged(old_bounds, bounds());
1336 FOR_EACH_OBSERVER(WindowObserver,
1337 observers_,
1338 OnWindowBoundsChanged(this, old_bounds, bounds()));
1341 bool Window::CleanupGestureState() {
1342 bool state_modified = false;
1343 state_modified |= ui::GestureRecognizer::Get()->CancelActiveTouches(this);
1344 state_modified |=
1345 ui::GestureRecognizer::Get()->CleanupStateForConsumer(this);
1346 for (Window::Windows::iterator iter = children_.begin();
1347 iter != children_.end();
1348 ++iter) {
1349 state_modified |= (*iter)->CleanupGestureState();
1351 return state_modified;
1354 void Window::OnPaintLayer(gfx::Canvas* canvas) {
1355 Paint(canvas);
1358 void Window::OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) {
1359 DCHECK(layer());
1360 FOR_EACH_OBSERVER(WindowObserver,
1361 observers_,
1362 OnDelegatedFrameDamage(this, damage_rect_in_dip));
1365 base::Closure Window::PrepareForLayerBoundsChange() {
1366 return base::Bind(&Window::OnWindowBoundsChanged, base::Unretained(this),
1367 bounds());
1370 bool Window::CanAcceptEvent(const ui::Event& event) {
1371 // The client may forbid certain windows from receiving events at a given
1372 // point in time.
1373 client::EventClient* client = client::GetEventClient(GetRootWindow());
1374 if (client && !client->CanProcessEventsWithinSubtree(this))
1375 return false;
1377 // We need to make sure that a touch cancel event and any gesture events it
1378 // creates can always reach the window. This ensures that we receive a valid
1379 // touch / gesture stream.
1380 if (event.IsEndingEvent())
1381 return true;
1383 if (!IsVisible())
1384 return false;
1386 // The top-most window can always process an event.
1387 if (!parent_)
1388 return true;
1390 // For located events (i.e. mouse, touch etc.), an assumption is made that
1391 // windows that don't have a default event-handler cannot process the event
1392 // (see more in GetWindowForPoint()). This assumption is not made for key
1393 // events.
1394 return event.IsKeyEvent() || target_handler();
1397 ui::EventTarget* Window::GetParentTarget() {
1398 if (IsRootWindow()) {
1399 return client::GetEventClient(this) ?
1400 client::GetEventClient(this)->GetToplevelEventTarget() :
1401 Env::GetInstance();
1403 return parent_;
1406 scoped_ptr<ui::EventTargetIterator> Window::GetChildIterator() const {
1407 return scoped_ptr<ui::EventTargetIterator>(
1408 new ui::EventTargetIteratorImpl<Window>(children()));
1411 ui::EventTargeter* Window::GetEventTargeter() {
1412 return targeter_.get();
1415 void Window::ConvertEventToTarget(ui::EventTarget* target,
1416 ui::LocatedEvent* event) {
1417 event->ConvertLocationToTarget(this,
1418 static_cast<Window*>(target));
1421 void Window::UpdateLayerName() {
1422 #if !defined(NDEBUG)
1423 DCHECK(layer());
1425 std::string layer_name(name_);
1426 if (layer_name.empty())
1427 layer_name = "Unnamed Window";
1429 if (id_ != -1)
1430 layer_name += " " + base::IntToString(id_);
1432 layer()->set_name(layer_name);
1433 #endif
1436 bool Window::ContainsMouse() {
1437 bool contains_mouse = false;
1438 if (IsVisible()) {
1439 WindowTreeHost* host = GetHost();
1440 contains_mouse = host &&
1441 ContainsPointInRoot(host->dispatcher()->GetLastMouseLocationInRoot());
1443 return contains_mouse;
1446 const Window* Window::GetAncestorWithLayer(gfx::Vector2d* offset) const {
1447 for (const aura::Window* window = this; window; window = window->parent()) {
1448 if (window->layer())
1449 return window;
1450 if (offset)
1451 *offset += window->bounds().OffsetFromOrigin();
1453 if (offset)
1454 *offset = gfx::Vector2d();
1455 return NULL;
1458 } // namespace aura