| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "aura/window.h" | 5 #include "aura/window.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "aura/desktop.h" | 9 #include "aura/desktop.h" |
| 10 #include "aura/window_delegate.h" | 10 #include "aura/window_delegate.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "ui/gfx/canvas_skia.h" | 12 #include "ui/gfx/canvas_skia.h" |
| 13 #include "ui/gfx/compositor/compositor.h" | 13 #include "ui/gfx/compositor/compositor.h" |
| 14 #include "ui/gfx/compositor/layer.h" | 14 #include "ui/gfx/compositor/layer.h" |
| 15 | 15 |
| 16 namespace aura { | 16 namespace aura { |
| 17 | 17 |
| 18 Window::Window(WindowDelegate* delegate) | 18 Window::Window(WindowDelegate* delegate) |
| 19 : delegate_(delegate), | 19 : delegate_(delegate), |
| 20 visibility_(VISIBILITY_HIDDEN), | 20 visibility_(VISIBILITY_HIDDEN), |
| 21 needs_paint_all_(true), | 21 needs_paint_all_(true), |
| 22 parent_(NULL), | 22 parent_(NULL), |
| 23 id_(-1) { | 23 id_(-1) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 Window::~Window() { | 26 Window::~Window() { |
| 27 if (delegate_) | |
| 28 delegate_->OnWindowDestroyed(); | |
| 29 if (parent_) | |
| 30 parent_->RemoveChild(this); | |
| 31 } | 27 } |
| 32 | 28 |
| 33 void Window::Init() { | 29 void Window::Init() { |
| 34 layer_.reset(new ui::Layer(Desktop::GetInstance()->compositor())); | 30 layer_.reset(new ui::Layer(Desktop::GetInstance()->compositor())); |
| 35 } | 31 } |
| 36 | 32 |
| 37 void Window::SetVisibility(Visibility visibility) { | 33 void Window::SetVisibility(Visibility visibility) { |
| 38 if (visibility_ == visibility) | 34 if (visibility_ == visibility) |
| 39 return; | 35 return; |
| 40 | 36 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 67 if (parent) | 63 if (parent) |
| 68 parent->AddChild(this); | 64 parent->AddChild(this); |
| 69 else | 65 else |
| 70 Desktop::GetInstance()->window()->AddChild(this); | 66 Desktop::GetInstance()->window()->AddChild(this); |
| 71 } | 67 } |
| 72 | 68 |
| 73 void Window::DrawTree() { | 69 void Window::DrawTree() { |
| 74 UpdateLayerCanvas(); | 70 UpdateLayerCanvas(); |
| 75 Draw(); | 71 Draw(); |
| 76 | 72 |
| 73 // First pass updates the layer bitmaps. |
| 77 for (Windows::iterator i = children_.begin(); i != children_.end(); ++i) | 74 for (Windows::iterator i = children_.begin(); i != children_.end(); ++i) |
| 78 (*i)->DrawTree(); | 75 (*i)->DrawTree(); |
| 79 } | 76 } |
| 80 | 77 |
| 81 void Window::AddChild(Window* child) { | 78 void Window::AddChild(Window* child) { |
| 82 DCHECK(std::find(children_.begin(), children_.end(), child) == | 79 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 83 children_.end()); | 80 children_.end()); |
| 84 child->parent_ = this; | 81 child->parent_ = this; |
| 85 layer_->Add(child->layer_.get()); | 82 layer_->Add(child->layer_.get()); |
| 86 children_.push_back(child); | 83 children_.push_back(child); |
| 87 } | 84 } |
| 88 | 85 |
| 89 void Window::RemoveChild(Window* child) { | 86 void Window::RemoveChild(Window* child) { |
| 90 Windows::iterator i = std::find(children_.begin(), children_.end(), child); | 87 Windows::iterator i = std::find(children_.begin(), children_.end(), child); |
| 91 DCHECK(i != children_.end()); | 88 DCHECK(i != children_.end()); |
| 92 child->parent_ = NULL; | 89 child->parent_ = NULL; |
| 93 layer_->Remove(child->layer_.get()); | 90 layer_->Remove(child->layer_.get()); |
| 94 children_.erase(i); | 91 children_.erase(i); |
| 95 } | 92 } |
| 96 | 93 |
| 97 // static | |
| 98 void Window::ConvertPointToWindow(Window* source, | |
| 99 Window* target, | |
| 100 gfx::Point* point) { | |
| 101 ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point); | |
| 102 } | |
| 103 | |
| 104 bool Window::OnMouseEvent(const MouseEvent& event) { | 94 bool Window::OnMouseEvent(const MouseEvent& event) { |
| 105 return true; | 95 return true; |
| 106 } | 96 } |
| 107 | 97 |
| 108 bool Window::HitTest(const gfx::Point& point) { | |
| 109 gfx::Rect local_bounds(gfx::Point(), bounds().size()); | |
| 110 // TODO(beng): hittest masks. | |
| 111 return local_bounds.Contains(point); | |
| 112 } | |
| 113 | |
| 114 Window* Window::GetEventHandlerForPoint(const gfx::Point& point) { | |
| 115 Windows::const_reverse_iterator i = children_.rbegin(); | |
| 116 for (; i != children_.rend(); ++i) { | |
| 117 Window* child = *i; | |
| 118 if (child->visibility() == Window::VISIBILITY_HIDDEN) | |
| 119 continue; | |
| 120 gfx::Point point_in_child_coords(point); | |
| 121 Window::ConvertPointToWindow(this, child, &point_in_child_coords); | |
| 122 if (child->HitTest(point_in_child_coords)) | |
| 123 return child->GetEventHandlerForPoint(point_in_child_coords); | |
| 124 } | |
| 125 return this; | |
| 126 } | |
| 127 | |
| 128 void Window::UpdateLayerCanvas() { | 98 void Window::UpdateLayerCanvas() { |
| 129 if (needs_paint_all_) { | 99 if (needs_paint_all_) { |
| 130 needs_paint_all_ = false; | 100 needs_paint_all_ = false; |
| 131 dirty_rect_ = gfx::Rect(0, 0, bounds().width(), bounds().height()); | 101 dirty_rect_ = gfx::Rect(0, 0, bounds().width(), bounds().height()); |
| 132 } | 102 } |
| 133 gfx::Rect dirty_rect = dirty_rect_.Intersect( | 103 gfx::Rect dirty_rect = dirty_rect_.Intersect( |
| 134 gfx::Rect(0, 0, bounds().width(), bounds().height())); | 104 gfx::Rect(0, 0, bounds().width(), bounds().height())); |
| 135 dirty_rect_.SetRect(0, 0, 0, 0); | 105 dirty_rect_.SetRect(0, 0, 0, 0); |
| 136 if (dirty_rect.IsEmpty()) | 106 if (dirty_rect.IsEmpty()) |
| 137 return; | 107 return; |
| 138 if (delegate_) { | 108 if (delegate_) { |
| 139 scoped_ptr<gfx::Canvas> canvas(gfx::Canvas::CreateCanvas( | 109 scoped_ptr<gfx::Canvas> canvas(gfx::Canvas::CreateCanvas( |
| 140 dirty_rect.width(), dirty_rect.height(), false)); | 110 dirty_rect.width(), dirty_rect.height(), false)); |
| 141 canvas->TranslateInt(dirty_rect.x(), dirty_rect.y()); | 111 canvas->TranslateInt(dirty_rect.x(), dirty_rect.y()); |
| 142 delegate_->OnPaint(canvas.get()); | 112 delegate_->OnPaint(canvas.get()); |
| 143 SetCanvas(*canvas->AsCanvasSkia(), bounds().origin()); | 113 SetCanvas(*canvas->AsCanvasSkia(), bounds().origin()); |
| 144 } | 114 } |
| 145 } | 115 } |
| 146 | 116 |
| 147 void Window::Draw() { | 117 void Window::Draw() { |
| 148 if (visibility_ != VISIBILITY_HIDDEN) | 118 if (visibility_ != VISIBILITY_HIDDEN) |
| 149 layer_->Draw(); | 119 layer_->Draw(); |
| 150 } | 120 } |
| 151 | 121 |
| 152 } // namespace aura | 122 } // namespace aura |
| OLD | NEW |