| OLD | NEW | 
 | (Empty) | 
|    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 |  | 
|    3 // found in the LICENSE file. |  | 
|    4  |  | 
|    5 #include "aura/window.h" |  | 
|    6  |  | 
|    7 #include <algorithm> |  | 
|    8  |  | 
|    9 #include "aura/desktop.h" |  | 
|   10 #include "aura/event.h" |  | 
|   11 #include "aura/window_delegate.h" |  | 
|   12 #include "aura/window_manager.h" |  | 
|   13 #include "base/logging.h" |  | 
|   14 #include "ui/gfx/canvas_skia.h" |  | 
|   15 #include "ui/gfx/compositor/compositor.h" |  | 
|   16 #include "ui/gfx/compositor/layer.h" |  | 
|   17  |  | 
|   18 namespace aura { |  | 
|   19  |  | 
|   20 Window::Window(WindowDelegate* delegate) |  | 
|   21     : delegate_(delegate), |  | 
|   22       visibility_(VISIBILITY_HIDDEN), |  | 
|   23       parent_(NULL), |  | 
|   24       id_(-1), |  | 
|   25       user_data_(NULL) { |  | 
|   26 } |  | 
|   27  |  | 
|   28 Window::~Window() { |  | 
|   29   // Let the delegate know we're in the processing of destroying. |  | 
|   30   if (delegate_) |  | 
|   31     delegate_->OnWindowDestroying(); |  | 
|   32   // Then destroy the children. |  | 
|   33   while (!children_.empty()) { |  | 
|   34     Window* child = children_[0]; |  | 
|   35     delete child; |  | 
|   36     // Deleting the child so remove it from out children_ list. |  | 
|   37     DCHECK(std::find(children_.begin(), children_.end(), child) == |  | 
|   38            children_.end()); |  | 
|   39   } |  | 
|   40   // And let the delegate do any post cleanup. |  | 
|   41   if (delegate_) |  | 
|   42     delegate_->OnWindowDestroyed(); |  | 
|   43   if (parent_) |  | 
|   44     parent_->RemoveChild(this); |  | 
|   45 } |  | 
|   46  |  | 
|   47 void Window::Init() { |  | 
|   48   layer_.reset(new ui::Layer(Desktop::GetInstance()->compositor())); |  | 
|   49   layer_->set_delegate(this); |  | 
|   50 } |  | 
|   51  |  | 
|   52 void Window::SetVisibility(Visibility visibility) { |  | 
|   53   if (visibility_ == visibility) |  | 
|   54     return; |  | 
|   55  |  | 
|   56   visibility_ = visibility; |  | 
|   57   layer_->set_visible(visibility_ != VISIBILITY_HIDDEN); |  | 
|   58   if (layer_->visible()) |  | 
|   59     SchedulePaint(); |  | 
|   60 } |  | 
|   61  |  | 
|   62 void Window::SetBounds(const gfx::Rect& bounds, int anim_ms) { |  | 
|   63   // TODO: support anim_ms |  | 
|   64   // TODO: funnel this through the Desktop. |  | 
|   65   bool was_move = bounds_.size() == bounds.size(); |  | 
|   66   bounds_ = bounds; |  | 
|   67   layer_->SetBounds(bounds); |  | 
|   68   if (was_move) |  | 
|   69     SchedulePaintInRect(gfx::Rect()); |  | 
|   70   else |  | 
|   71     SchedulePaint(); |  | 
|   72 } |  | 
|   73  |  | 
|   74 void Window::SchedulePaintInRect(const gfx::Rect& rect) { |  | 
|   75   layer_->SchedulePaint(rect); |  | 
|   76 } |  | 
|   77  |  | 
|   78 void Window::SetCanvas(const SkCanvas& canvas, const gfx::Point& origin) { |  | 
|   79   // TODO: figure out how this is going to work when animating the layer. In |  | 
|   80   // particular if we're animating the size then the underlying Texture is going |  | 
|   81   // to be unhappy if we try to set a texture on a size bigger than the size of |  | 
|   82   // the texture. |  | 
|   83   layer_->SetCanvas(canvas, origin); |  | 
|   84 } |  | 
|   85  |  | 
|   86 void Window::SetParent(Window* parent) { |  | 
|   87   if (parent) |  | 
|   88     parent->AddChild(this); |  | 
|   89   else |  | 
|   90     Desktop::GetInstance()->window()->AddChild(this); |  | 
|   91 } |  | 
|   92  |  | 
|   93 bool Window::IsTopLevelWindowContainer() const { |  | 
|   94   return false; |  | 
|   95 } |  | 
|   96  |  | 
|   97 void Window::MoveChildToFront(Window* child) { |  | 
|   98   DCHECK_EQ(child->parent(), this); |  | 
|   99   const Windows::iterator i(std::find(children_.begin(), children_.end(), |  | 
|  100                                       child)); |  | 
|  101   DCHECK(i != children_.end()); |  | 
|  102   children_.erase(i); |  | 
|  103  |  | 
|  104   // TODO(beng): this obviously has to handle different window types. |  | 
|  105   children_.insert(children_.begin() + children_.size(), child); |  | 
|  106   SchedulePaintInRect(gfx::Rect()); |  | 
|  107 } |  | 
|  108  |  | 
|  109 void Window::AddChild(Window* child) { |  | 
|  110   DCHECK(std::find(children_.begin(), children_.end(), child) == |  | 
|  111       children_.end()); |  | 
|  112   child->parent_ = this; |  | 
|  113   layer_->Add(child->layer_.get()); |  | 
|  114   children_.push_back(child); |  | 
|  115 } |  | 
|  116  |  | 
|  117 void Window::RemoveChild(Window* child) { |  | 
|  118   Windows::iterator i = std::find(children_.begin(), children_.end(), child); |  | 
|  119   DCHECK(i != children_.end()); |  | 
|  120   child->parent_ = NULL; |  | 
|  121   layer_->Remove(child->layer_.get()); |  | 
|  122   children_.erase(i); |  | 
|  123 } |  | 
|  124  |  | 
|  125 // static |  | 
|  126 void Window::ConvertPointToWindow(Window* source, |  | 
|  127                                   Window* target, |  | 
|  128                                   gfx::Point* point) { |  | 
|  129   ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point); |  | 
|  130 } |  | 
|  131  |  | 
|  132 bool Window::OnMouseEvent(MouseEvent* event) { |  | 
|  133   if (!window_manager_.get()) |  | 
|  134     window_manager_.reset(new WindowManager(this)); |  | 
|  135   return window_manager_->OnMouseEvent(event) || delegate_->OnMouseEvent(event); |  | 
|  136 } |  | 
|  137  |  | 
|  138 bool Window::OnKeyEvent(KeyEvent* event) { |  | 
|  139   return delegate_->OnKeyEvent(event); |  | 
|  140 } |  | 
|  141  |  | 
|  142 bool Window::HitTest(const gfx::Point& point) { |  | 
|  143   gfx::Rect local_bounds(gfx::Point(), bounds().size()); |  | 
|  144   // TODO(beng): hittest masks. |  | 
|  145   return local_bounds.Contains(point); |  | 
|  146 } |  | 
|  147  |  | 
|  148 Window* Window::GetEventHandlerForPoint(const gfx::Point& point) { |  | 
|  149   Windows::const_reverse_iterator i = children_.rbegin(); |  | 
|  150   for (; i != children_.rend(); ++i) { |  | 
|  151     Window* child = *i; |  | 
|  152     if (child->visibility() == Window::VISIBILITY_HIDDEN) |  | 
|  153       continue; |  | 
|  154     gfx::Point point_in_child_coords(point); |  | 
|  155     Window::ConvertPointToWindow(this, child, &point_in_child_coords); |  | 
|  156     if (child->HitTest(point_in_child_coords)) |  | 
|  157       return child->GetEventHandlerForPoint(point_in_child_coords); |  | 
|  158   } |  | 
|  159   return this; |  | 
|  160 } |  | 
|  161  |  | 
|  162 internal::FocusManager* Window::GetFocusManager() { |  | 
|  163   return parent_ ? parent_->GetFocusManager() : NULL; |  | 
|  164 } |  | 
|  165  |  | 
|  166 void Window::SchedulePaint() { |  | 
|  167   SchedulePaintInRect(gfx::Rect(0, 0, bounds_.width(), bounds_.height())); |  | 
|  168 } |  | 
|  169  |  | 
|  170 void Window::OnPaintLayer(gfx::Canvas* canvas) { |  | 
|  171   if (delegate_) |  | 
|  172     delegate_->OnPaint(canvas); |  | 
|  173 } |  | 
|  174  |  | 
|  175 }  // namespace aura |  | 
| OLD | NEW |