Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / mojo / services / view_manager / node.cc
1 // Copyright 2014 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 "mojo/services/view_manager/node.h"
6
7 #include "mojo/services/view_manager/node_delegate.h"
8 #include "ui/aura/window_property.h"
9 #include "ui/base/cursor/cursor.h"
10 #include "ui/base/hit_test.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/image/image_skia.h"
13 #include "ui/gfx/native_widget_types.h"
14
15 DECLARE_WINDOW_PROPERTY_TYPE(mojo::service::Node*);
16
17 namespace mojo {
18 namespace service {
19
20 DEFINE_WINDOW_PROPERTY_KEY(Node*, kNodeKey, NULL);
21
22 Node::Node(NodeDelegate* delegate, const NodeId& id)
23     : delegate_(delegate),
24       id_(id),
25       window_(this) {
26   DCHECK(delegate);  // Must provide a delegate.
27   window_.set_owned_by_parent(false);
28   window_.AddObserver(this);
29   window_.SetProperty(kNodeKey, this);
30   window_.Init(aura::WINDOW_LAYER_TEXTURED);
31
32   // TODO(sky): this likely needs to be false and add a visibility API.
33   window_.Show();
34 }
35
36 Node::~Node() {
37   // This is implicitly done during deletion of the window, but we do it here so
38   // that we're in a known state.
39   if (window_.parent())
40     window_.parent()->RemoveChild(&window_);
41
42   delegate_->OnNodeDestroyed(this);
43 }
44
45 // static
46 Node* Node::NodeForWindow(aura::Window* window) {
47   return window->GetProperty(kNodeKey);
48 }
49
50 const Node* Node::GetParent() const {
51   if (!window_.parent())
52     return NULL;
53   return window_.parent()->GetProperty(kNodeKey);
54 }
55
56 void Node::Add(Node* child) {
57   window_.AddChild(&child->window_);
58 }
59
60 void Node::Remove(Node* child) {
61   window_.RemoveChild(&child->window_);
62 }
63
64 void Node::Reorder(Node* child, Node* relative, OrderDirection direction) {
65   if (direction == ORDER_DIRECTION_ABOVE)
66     window_.StackChildAbove(child->window(), relative->window());
67   else if (direction == ORDER_DIRECTION_BELOW)
68     window_.StackChildBelow(child->window(), relative->window());
69 }
70
71 const Node* Node::GetRoot() const {
72   const aura::Window* window = &window_;
73   while (window && window->parent())
74     window = window->parent();
75   return window->GetProperty(kNodeKey);
76 }
77
78 std::vector<const Node*> Node::GetChildren() const {
79   std::vector<const Node*> children;
80   children.reserve(window_.children().size());
81   for (size_t i = 0; i < window_.children().size(); ++i)
82     children.push_back(window_.children()[i]->GetProperty(kNodeKey));
83   return children;
84 }
85
86 std::vector<Node*> Node::GetChildren() {
87   std::vector<Node*> children;
88   children.reserve(window_.children().size());
89   for (size_t i = 0; i < window_.children().size(); ++i)
90     children.push_back(window_.children()[i]->GetProperty(kNodeKey));
91   return children;
92 }
93
94 bool Node::Contains(const Node* node) const {
95   return node && window_.Contains(&(node->window_));
96 }
97
98 bool Node::IsVisible() const {
99   return window_.TargetVisibility();
100 }
101
102 void Node::SetVisible(bool value) {
103   if (value)
104     window_.Show();
105   else
106     window_.Hide();
107 }
108
109 void Node::SetBitmap(const SkBitmap& bitmap) {
110   bitmap_ = bitmap;
111   window_.SchedulePaintInRect(gfx::Rect(window_.bounds().size()));
112 }
113
114 void Node::OnWindowHierarchyChanged(
115     const aura::WindowObserver::HierarchyChangeParams& params) {
116   if (params.target != &window_ || params.receiver != &window_)
117     return;
118   const Node* new_parent = params.new_parent ?
119       params.new_parent->GetProperty(kNodeKey) : NULL;
120   const Node* old_parent = params.old_parent ?
121       params.old_parent->GetProperty(kNodeKey) : NULL;
122   // This check is needed because even the root Node's aura::Window has a
123   // parent, but the Node itself has no parent (so it's possible for us to
124   // receive this notification from aura when no logical Node hierarchy change
125   // has actually ocurred).
126   if (new_parent != old_parent)
127     delegate_->OnNodeHierarchyChanged(this, new_parent, old_parent);
128 }
129
130 gfx::Size Node::GetMinimumSize() const {
131   return gfx::Size();
132 }
133
134 gfx::Size Node::GetMaximumSize() const {
135   return gfx::Size();
136 }
137
138 void Node::OnBoundsChanged(const gfx::Rect& old_bounds,
139                            const gfx::Rect& new_bounds) {
140   delegate_->OnNodeBoundsChanged(this, old_bounds, new_bounds);
141 }
142
143 gfx::NativeCursor Node::GetCursor(const gfx::Point& point) {
144   return gfx::kNullCursor;
145 }
146
147 int Node::GetNonClientComponent(const gfx::Point& point) const {
148   return HTCAPTION;
149 }
150
151 bool Node::ShouldDescendIntoChildForEventHandling(
152     aura::Window* child,
153     const gfx::Point& location) {
154   return true;
155 }
156
157 bool Node::CanFocus() {
158   return true;
159 }
160
161 void Node::OnCaptureLost() {
162 }
163
164 void Node::OnPaint(gfx::Canvas* canvas) {
165   canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bitmap_), 0, 0);
166 }
167
168 void Node::OnDeviceScaleFactorChanged(float device_scale_factor) {
169 }
170
171 void Node::OnWindowDestroying(aura::Window* window) {
172 }
173
174 void Node::OnWindowDestroyed(aura::Window* window) {
175 }
176
177 void Node::OnWindowTargetVisibilityChanged(bool visible) {
178 }
179
180 bool Node::HasHitTestMask() const {
181   return false;
182 }
183
184 void Node::GetHitTestMask(gfx::Path* mask) const {
185 }
186
187 }  // namespace service
188 }  // namespace mojo