blob: b49fab6895b3dae54c30ed29590670f780ea0a29 [file] [log] [blame]
[email protected]731433e2011-01-25 17:34:251// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2362e4f2009-05-08 00:34:055#include "views/view.h"
initial.commit09911bf2008-07-26 23:55:296
7#include <algorithm>
initial.commit09911bf2008-07-26 23:55:298
[email protected]a8f21152011-09-08 15:30:029#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/message_loop.h"
[email protected]36de1cb2011-09-20 20:47:0713#include "base/stringprintf.h"
[email protected]d55194ca2010-03-11 18:25:4514#include "base/utf_string_conversions.h"
[email protected]eb93b619b2011-02-15 22:53:1915#include "third_party/skia/include/core/SkRect.h"
[email protected]79e549f2011-03-14 06:56:3316#include "ui/base/accessibility/accessibility_types.h"
[email protected]02bae0f2011-01-19 20:22:0017#include "ui/base/dragdrop/drag_drop_types.h"
[email protected]08397d52011-02-05 01:53:3818#include "ui/gfx/canvas_skia.h"
[email protected]7c060932011-03-23 15:49:1019#include "ui/gfx/compositor/compositor.h"
[email protected]adc93fa72011-06-21 19:47:3920#include "ui/gfx/compositor/layer.h"
[email protected]36de1cb2011-09-20 20:47:0721#include "ui/gfx/interpolated_transform.h"
[email protected]08397d52011-02-05 01:53:3822#include "ui/gfx/path.h"
[email protected]80248e32011-07-08 15:31:1123#include "ui/gfx/point3.h"
[email protected]36df22b2011-02-24 21:47:5624#include "ui/gfx/transform.h"
[email protected]2362e4f2009-05-08 00:34:0525#include "views/background.h"
[email protected]1a2490c32011-06-23 17:01:5526#include "views/context_menu_controller.h"
[email protected]3eaa3a82011-06-21 23:10:1527#include "views/drag_controller.h"
[email protected]710a98d2011-06-23 20:13:2928#include "views/layer_property_setter.h"
[email protected]731433e2011-01-25 17:34:2529#include "views/layout/layout_manager.h"
[email protected]3ee83f2c2009-05-10 05:58:4030#include "views/views_delegate.h"
[email protected]2f2b57b2011-06-16 17:21:2331#include "views/widget/native_widget_private.h"
[email protected]86477142011-08-19 19:06:1532#include "views/widget/native_widget_views.h"
[email protected]2362e4f2009-05-08 00:34:0533#include "views/widget/root_view.h"
[email protected]319d4ae2009-05-28 19:09:4534#include "views/widget/tooltip_manager.h"
[email protected]2362e4f2009-05-08 00:34:0535#include "views/widget/widget.h"
[email protected]031e4d32009-12-29 01:13:2336
[email protected]6ff244f2009-01-20 20:38:0837#if defined(OS_WIN)
[email protected]a684f122010-12-29 23:48:4638#include "base/win/scoped_gdi_object.h"
[email protected]79e549f2011-03-14 06:56:3339#include "views/accessibility/native_view_accessibility_win.h"
[email protected]6ff244f2009-01-20 20:38:0840#endif
[email protected]9d43e372011-09-22 19:39:5241#if defined(TOOLKIT_USES_GTK)
[email protected]9dd7e3d72011-01-20 18:27:0642#include "ui/base/gtk/scoped_handle_gtk.h"
[email protected]031e4d32009-12-29 01:13:2343#endif
initial.commit09911bf2008-07-26 23:55:2944
[email protected]c797cd42011-03-15 02:18:3645namespace {
[email protected]3534e7a2011-05-26 17:44:0346
[email protected]c797cd42011-03-15 02:18:3647// Whether to use accelerated compositing when necessary (e.g. when a view has a
48// transformation).
[email protected]4916d622011-09-28 23:45:3849#if defined(VIEWS_COMPOSITOR)
[email protected]c797cd42011-03-15 02:18:3650bool use_acceleration_when_possible = true;
[email protected]f7080812011-06-13 17:09:5451#else
52bool use_acceleration_when_possible = false;
53#endif
[email protected]f1785de2011-03-15 19:27:4954
55// Saves the drawing state, and restores the state when going out of scope.
56class ScopedCanvas {
57 public:
58 explicit ScopedCanvas(gfx::Canvas* canvas) : canvas_(canvas) {
59 if (canvas_)
60 canvas_->Save();
61 }
62 ~ScopedCanvas() {
63 if (canvas_)
64 canvas_->Restore();
65 }
66 void SetCanvas(gfx::Canvas* canvas) {
67 if (canvas_)
68 canvas_->Restore();
69 canvas_ = canvas;
70 canvas_->Save();
71 }
72
73 private:
74 gfx::Canvas* canvas_;
75
76 DISALLOW_COPY_AND_ASSIGN(ScopedCanvas);
77};
78
[email protected]3534e7a2011-05-26 17:44:0379} // namespace
[email protected]c797cd42011-03-15 02:18:3680
[email protected]c2dacc92008-10-16 23:51:3881namespace views {
initial.commit09911bf2008-07-26 23:55:2982
83// static
[email protected]3ee83f2c2009-05-10 05:58:4084ViewsDelegate* ViewsDelegate::views_delegate = NULL;
85
86// static
[email protected]2362e4f2009-05-08 00:34:0587char View::kViewClassName[] = "views/View";
initial.commit09911bf2008-07-26 23:55:2988
[email protected]2df3cc92011-02-05 00:38:5989////////////////////////////////////////////////////////////////////////////////
90// View, public:
91
92// TO BE MOVED -----------------------------------------------------------------
93
94void View::SetHotTracked(bool flag) {
95}
96
[email protected]16460642011-03-04 23:15:5397bool View::IsHotTracked() const {
98 return false;
99}
100
[email protected]2df3cc92011-02-05 00:38:59101// Creation and lifetime -------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29102
103View::View()
[email protected]23948b92011-06-02 00:22:09104 : parent_owned_(true),
[email protected]fb880be2011-05-26 20:06:15105 id_(0),
106 group_(-1),
initial.commit09911bf2008-07-26 23:55:29107 parent_(NULL),
[email protected]5fe3f3a2011-06-11 00:31:32108 visible_(true),
[email protected]6ed82bb82011-05-27 20:24:07109 enabled_(true),
[email protected]a8f21152011-09-08 15:30:02110 painting_enabled_(true),
initial.commit09911bf2008-07-26 23:55:29111 registered_for_visible_bounds_notification_(false),
[email protected]36df22b2011-02-24 21:47:56112 clip_x_(0.0),
113 clip_y_(0.0),
[email protected]2df3cc92011-02-05 00:38:59114 needs_layout_(true),
115 flip_canvas_on_paint_for_rtl_ui_(false),
[email protected]18dab372011-10-03 21:21:44116 paint_to_layer_(false),
[email protected]bda9556c2010-01-07 00:55:16117 accelerator_registration_delayed_(false),
[email protected]bda9556c2010-01-07 00:55:16118 accelerator_focus_manager_(NULL),
[email protected]71421c3f2009-06-06 00:41:44119 registered_accelerator_count_(0),
[email protected]2df3cc92011-02-05 00:38:59120 next_focusable_view_(NULL),
121 previous_focusable_view_(NULL),
[email protected]23948b92011-06-02 00:22:09122 focusable_(false),
[email protected]5867c242011-05-27 02:42:50123 accessibility_focusable_(false),
initial.commit09911bf2008-07-26 23:55:29124 context_menu_controller_(NULL),
[email protected]2df3cc92011-02-05 00:38:59125 drag_controller_(NULL) {
initial.commit09911bf2008-07-26 23:55:29126}
127
128View::~View() {
[email protected]8c57de22010-03-12 21:06:04129 if (parent_)
130 parent_->RemoveChildView(this);
131
[email protected]24e3c96b2011-06-06 22:31:42132 for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i) {
133 (*i)->parent_ = NULL;
134 if ((*i)->parent_owned())
135 delete *i;
initial.commit09911bf2008-07-26 23:55:29136 }
[email protected]a64f33902009-10-10 05:41:23137
138#if defined(OS_WIN)
[email protected]79e549f2011-03-14 06:56:33139 if (native_view_accessibility_win_.get())
140 native_view_accessibility_win_->set_view(NULL);
[email protected]a64f33902009-10-10 05:41:23141#endif
initial.commit09911bf2008-07-26 23:55:29142}
143
[email protected]2df3cc92011-02-05 00:38:59144// Tree operations -------------------------------------------------------------
145
[email protected]20838602011-02-09 04:50:21146const Widget* View::GetWidget() const {
[email protected]2df3cc92011-02-05 00:38:59147 // The root view holds a reference to this view hierarchy's Widget.
148 return parent_ ? parent_->GetWidget() : NULL;
149}
150
[email protected]20838602011-02-09 04:50:21151Widget* View::GetWidget() {
152 return const_cast<Widget*>(const_cast<const View*>(this)->GetWidget());
153}
154
155void View::AddChildView(View* view) {
156 AddChildViewAt(view, child_count());
157}
158
159void View::AddChildViewAt(View* view, int index) {
[email protected]cacf0af2011-06-16 23:31:11160 CHECK_NE(view, this) << "You cannot add a view as its own child";
[email protected]20838602011-02-09 04:50:21161
[email protected]2fa942b2011-06-14 15:59:55162 // If |view| has a parent, remove it from its parent.
163 View* parent = view->parent_;
164 if (parent)
165 parent->RemoveChildView(view);
[email protected]20838602011-02-09 04:50:21166
167 // Sets the prev/next focus views.
168 InitFocusSiblings(view, index);
169
170 // Let's insert the view.
[email protected]b5fe7442011-05-26 02:42:43171 view->parent_ = this;
[email protected]2fa942b2011-06-14 15:59:55172 children_.insert(children_.begin() + index, view);
[email protected]20838602011-02-09 04:50:21173
[email protected]2fa942b2011-06-14 15:59:55174 for (View* v = this; v; v = v->parent_)
175 v->ViewHierarchyChangedImpl(false, true, this, view);
[email protected]20838602011-02-09 04:50:21176
177 view->PropagateAddNotifications(this, view);
178 UpdateTooltip();
[email protected]18dab372011-10-03 21:21:44179 if (GetWidget())
[email protected]6260f9e2011-02-16 21:28:01180 RegisterChildrenForVisibleBoundsNotification(view);
[email protected]20838602011-02-09 04:50:21181
182 if (layout_manager_.get())
183 layout_manager_->ViewAdded(this, view);
[email protected]18da0fd2011-10-07 16:25:31184
185 if (use_acceleration_when_possible)
186 ReorderLayers();
[email protected]20838602011-02-09 04:50:21187}
188
[email protected]ea1fc4b12011-06-16 17:49:04189void View::ReorderChildView(View* view, int index) {
190 DCHECK_EQ(view->parent_, this);
191 if (index < 0)
192 index = child_count() - 1;
193 else if (index >= child_count())
194 return;
195 if (children_[index] == view)
196 return;
197
198 const Views::iterator i(std::find(children_.begin(), children_.end(), view));
199 DCHECK(i != children_.end());
200 children_.erase(i);
201
202 // Unlink the view first
203 View* next_focusable = view->next_focusable_view_;
204 View* prev_focusable = view->previous_focusable_view_;
205 if (prev_focusable)
206 prev_focusable->next_focusable_view_ = next_focusable;
207 if (next_focusable)
208 next_focusable->previous_focusable_view_ = prev_focusable;
209
210 // Add it in the specified index now.
211 InitFocusSiblings(view, index);
212 children_.insert(children_.begin() + index, view);
[email protected]18da0fd2011-10-07 16:25:31213
214 if (use_acceleration_when_possible)
215 ReorderLayers();
[email protected]ea1fc4b12011-06-16 17:49:04216}
217
[email protected]20838602011-02-09 04:50:21218void View::RemoveChildView(View* view) {
219 DoRemoveChildView(view, true, true, false);
220}
221
[email protected]77a08752011-06-03 00:00:08222void View::RemoveAllChildViews(bool delete_children) {
[email protected]f27ddd72011-06-09 23:13:26223 while (!children_.empty())
224 DoRemoveChildView(children_.front(), false, false, delete_children);
[email protected]20838602011-02-09 04:50:21225 UpdateTooltip();
226}
227
[email protected]20838602011-02-09 04:50:21228bool View::Contains(const View* view) const {
[email protected]280cddd2011-05-17 01:00:46229 for (const View* v = view; v; v = v->parent_) {
230 if (v == this)
[email protected]20838602011-02-09 04:50:21231 return true;
[email protected]20838602011-02-09 04:50:21232 }
233 return false;
234}
235
236int View::GetIndexOf(const View* view) const {
[email protected]695e942e2011-06-06 15:15:51237 Views::const_iterator i(std::find(children_.begin(), children_.end(), view));
[email protected]26b2cb722011-06-07 16:35:09238 return i != children_.end() ? static_cast<int>(i - children_.begin()) : -1;
[email protected]20838602011-02-09 04:50:21239}
240
[email protected]2df3cc92011-02-05 00:38:59241// Size and disposition --------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29242
[email protected]97a31142011-02-08 00:09:16243void View::SetBounds(int x, int y, int width, int height) {
244 SetBoundsRect(gfx::Rect(x, y, std::max(0, width), std::max(0, height)));
initial.commit09911bf2008-07-26 23:55:29245}
246
[email protected]97a31142011-02-08 00:09:16247void View::SetBoundsRect(const gfx::Rect& bounds) {
[email protected]de8d613d2010-10-25 22:30:10248 if (bounds == bounds_) {
249 if (needs_layout_) {
250 needs_layout_ = false;
251 Layout();
[email protected]8eb52a9a2011-03-09 16:52:00252 SchedulePaint();
[email protected]de8d613d2010-10-25 22:30:10253 }
initial.commit09911bf2008-07-26 23:55:29254 return;
[email protected]de8d613d2010-10-25 22:30:10255 }
initial.commit09911bf2008-07-26 23:55:29256
[email protected]adc93fa72011-06-21 19:47:39257 if (IsVisible()) {
258 // Paint where the view is currently.
259 SchedulePaintBoundsChanged(
260 bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
261 SCHEDULE_PAINT_SIZE_CHANGED);
262 }
263
[email protected]80f8b9f2008-10-16 18:17:47264 gfx::Rect prev = bounds_;
initial.commit09911bf2008-07-26 23:55:29265 bounds_ = bounds;
[email protected]8eb52a9a2011-03-09 16:52:00266 BoundsChanged(prev);
initial.commit09911bf2008-07-26 23:55:29267}
268
[email protected]97a31142011-02-08 00:09:16269void View::SetSize(const gfx::Size& size) {
270 SetBounds(x(), y(), size.width(), size.height());
[email protected]2df3cc92011-02-05 00:38:59271}
272
[email protected]97a31142011-02-08 00:09:16273void View::SetPosition(const gfx::Point& position) {
274 SetBounds(position.x(), position.y(), width(), height());
275}
initial.commit09911bf2008-07-26 23:55:29276
[email protected]97a31142011-02-08 00:09:16277void View::SetX(int x) {
278 SetBounds(x, y(), width(), height());
279}
280
281void View::SetY(int y) {
282 SetBounds(x(), y, width(), height());
283}
284
[email protected]97a31142011-02-08 00:09:16285gfx::Rect View::GetContentsBounds() const {
286 gfx::Rect contents_bounds(GetLocalBounds());
287 if (border_.get()) {
288 gfx::Insets insets;
289 border_->GetInsets(&insets);
290 contents_bounds.Inset(insets);
291 }
292 return contents_bounds;
293}
294
295gfx::Rect View::GetLocalBounds() const {
296 return gfx::Rect(0, 0, width(), height());
initial.commit09911bf2008-07-26 23:55:29297}
298
[email protected]2df3cc92011-02-05 00:38:59299gfx::Insets View::GetInsets() const {
300 gfx::Insets insets;
301 if (border_.get())
302 border_->GetInsets(&insets);
303 return insets;
304}
305
[email protected]4d352522011-05-31 16:24:27306gfx::Rect View::GetVisibleBounds() const {
[email protected]2df3cc92011-02-05 00:38:59307 if (!IsVisibleInRootView())
308 return gfx::Rect();
309 gfx::Rect vis_bounds(0, 0, width(), height());
310 gfx::Rect ancestor_bounds;
[email protected]4d352522011-05-31 16:24:27311 const View* view = this;
[email protected]277c7b72011-06-06 15:23:09312 ui::Transform transform;
313
[email protected]2df3cc92011-02-05 00:38:59314 while (view != NULL && !vis_bounds.IsEmpty()) {
[email protected]18dab372011-10-03 21:21:44315 transform.ConcatTransform(view->GetTransform());
[email protected]277c7b72011-06-06 15:23:09316 transform.ConcatTranslate(static_cast<float>(view->GetMirroredX()),
317 static_cast<float>(view->y()));
318
319 vis_bounds = view->ConvertRectToParent(vis_bounds);
[email protected]2fa942b2011-06-14 15:59:55320 const View* ancestor = view->parent_;
[email protected]2df3cc92011-02-05 00:38:59321 if (ancestor != NULL) {
[email protected]4d352522011-05-31 16:24:27322 ancestor_bounds.SetRect(0, 0, ancestor->width(), ancestor->height());
[email protected]2df3cc92011-02-05 00:38:59323 vis_bounds = vis_bounds.Intersect(ancestor_bounds);
324 } else if (!view->GetWidget()) {
325 // If the view has no Widget, we're not visible. Return an empty rect.
326 return gfx::Rect();
327 }
328 view = ancestor;
329 }
330 if (vis_bounds.IsEmpty())
331 return vis_bounds;
332 // Convert back to this views coordinate system.
[email protected]277c7b72011-06-06 15:23:09333 transform.TransformRectReverse(&vis_bounds);
[email protected]2df3cc92011-02-05 00:38:59334 return vis_bounds;
335}
336
337gfx::Rect View::GetScreenBounds() const {
338 gfx::Point origin;
339 View::ConvertPointToScreen(this, &origin);
340 return gfx::Rect(origin, size());
341}
342
[email protected]154f8bc2008-10-15 18:02:30343gfx::Size View::GetPreferredSize() {
344 if (layout_manager_.get())
345 return layout_manager_->GetPreferredSize(this);
346 return gfx::Size();
initial.commit09911bf2008-07-26 23:55:29347}
348
[email protected]754b0082011-05-27 22:53:11349int View::GetBaseline() const {
[email protected]a1360162009-11-30 21:19:07350 return -1;
351}
352
initial.commit09911bf2008-07-26 23:55:29353void View::SizeToPreferredSize() {
[email protected]154f8bc2008-10-15 18:02:30354 gfx::Size prefsize = GetPreferredSize();
355 if ((prefsize.width() != width()) || (prefsize.height() != height()))
356 SetBounds(x(), y(), prefsize.width(), prefsize.height());
initial.commit09911bf2008-07-26 23:55:29357}
358
[email protected]154f8bc2008-10-15 18:02:30359gfx::Size View::GetMinimumSize() {
360 return GetPreferredSize();
initial.commit09911bf2008-07-26 23:55:29361}
362
363int View::GetHeightForWidth(int w) {
364 if (layout_manager_.get())
365 return layout_manager_->GetPreferredHeightForWidth(this, w);
[email protected]154f8bc2008-10-15 18:02:30366 return GetPreferredSize().height();
initial.commit09911bf2008-07-26 23:55:29367}
368
[email protected]6ed82bb82011-05-27 20:24:07369void View::SetVisible(bool visible) {
[email protected]5fe3f3a2011-06-11 00:31:32370 if (visible != visible_) {
[email protected]3aa43942011-09-13 20:59:53371 // If the View is currently visible, schedule paint to refresh parent.
[email protected]18dab372011-10-03 21:21:44372 // TODO(beng): not sure we should be doing this if we have a layer.
[email protected]5fe3f3a2011-06-11 00:31:32373 if (visible_)
[email protected]2df3cc92011-02-05 00:38:59374 SchedulePaint();
initial.commit09911bf2008-07-26 23:55:29375
[email protected]5fe3f3a2011-06-11 00:31:32376 visible_ = visible;
[email protected]18dab372011-10-03 21:21:44377 if (layer())
378 layer()->SetVisible(visible_);
[email protected]1d27aa532011-07-06 15:38:59379
[email protected]20838602011-02-09 04:50:21380 // This notifies all sub-views recursively.
[email protected]5fe3f3a2011-06-11 00:31:32381 PropagateVisibilityNotifications(this, visible_);
[email protected]2df3cc92011-02-05 00:38:59382
383 // If we are newly visible, schedule paint.
[email protected]5fe3f3a2011-06-11 00:31:32384 if (visible_)
[email protected]2df3cc92011-02-05 00:38:59385 SchedulePaint();
[email protected]e9adf0702010-03-08 23:34:07386 }
initial.commit09911bf2008-07-26 23:55:29387}
388
[email protected]16460642011-03-04 23:15:53389bool View::IsVisible() const {
[email protected]5fe3f3a2011-06-11 00:31:32390 return visible_;
[email protected]16460642011-03-04 23:15:53391}
392
[email protected]2df3cc92011-02-05 00:38:59393bool View::IsVisibleInRootView() const {
[email protected]2fa942b2011-06-14 15:59:55394 return IsVisible() && parent_ ? parent_->IsVisibleInRootView() : false;
[email protected]2df3cc92011-02-05 00:38:59395}
396
[email protected]6ed82bb82011-05-27 20:24:07397void View::SetEnabled(bool enabled) {
398 if (enabled != enabled_) {
399 enabled_ = enabled;
400 OnEnabledChanged();
[email protected]2df3cc92011-02-05 00:38:59401 }
402}
403
404bool View::IsEnabled() const {
405 return enabled_;
406}
407
[email protected]6ed82bb82011-05-27 20:24:07408void View::OnEnabledChanged() {
409 SchedulePaint();
410}
411
[email protected]eb93b619b2011-02-15 22:53:19412// Transformations -------------------------------------------------------------
413
[email protected]36df22b2011-02-24 21:47:56414const ui::Transform& View::GetTransform() const {
[email protected]b9b1e7a42011-05-17 15:29:51415 static const ui::Transform* no_op = new ui::Transform;
[email protected]18dab372011-10-03 21:21:44416 return layer() ? layer()->transform() : *no_op;
[email protected]eb93b619b2011-02-15 22:53:19417}
418
[email protected]b9b1e7a42011-05-17 15:29:51419void View::SetTransform(const ui::Transform& transform) {
420 if (!transform.HasChange()) {
[email protected]18dab372011-10-03 21:21:44421 if (layer()) {
422 layer_property_setter_->SetTransform(layer(), transform);
423 if (!paint_to_layer_)
424 DestroyLayer();
[email protected]40500ba2011-09-30 23:39:51425 } else {
[email protected]18dab372011-10-03 21:21:44426 // Nothing.
[email protected]40500ba2011-09-30 23:39:51427 }
[email protected]18dab372011-10-03 21:21:44428 } else {
429 if (!layer())
430 CreateLayer();
431 layer_property_setter_->SetTransform(layer(), transform);
432 layer()->ScheduleDraw();
[email protected]b9b1e7a42011-05-17 15:29:51433 }
[email protected]eb93b619b2011-02-15 22:53:19434}
435
[email protected]18dab372011-10-03 21:21:44436void View::SetPaintToLayer(bool paint_to_layer) {
437 paint_to_layer_ = paint_to_layer;
438 if (paint_to_layer_ && !layer()) {
[email protected]adc93fa72011-06-21 19:47:39439 CreateLayer();
[email protected]18dab372011-10-03 21:21:44440 } else if (!paint_to_layer_ && layer()) {
441 DestroyLayer();
[email protected]7c4b1b92011-06-27 16:37:18442 }
[email protected]6b8b0f02011-06-02 22:04:02443}
444
[email protected]710a98d2011-06-23 20:13:29445void View::SetLayerPropertySetter(LayerPropertySetter* setter) {
[email protected]18dab372011-10-03 21:21:44446 DCHECK(layer());
447 LayerPropertySetter* old_setter = layer_property_setter_.get();
448 if (!layer() || (old_setter && old_setter == setter))
[email protected]710a98d2011-06-23 20:13:29449 return;
[email protected]18dab372011-10-03 21:21:44450 if (!setter)
451 setter = LayerPropertySetter::CreateDefaultSetter();
[email protected]710a98d2011-06-23 20:13:29452
[email protected]18dab372011-10-03 21:21:44453 if (old_setter)
454 old_setter->Uninstalled(layer());
455 layer_property_setter_.reset(setter);
456 layer_property_setter_->Installed(layer());
[email protected]710a98d2011-06-23 20:13:29457}
458
[email protected]2df3cc92011-02-05 00:38:59459// RTL positioning -------------------------------------------------------------
460
[email protected]9defbad2011-02-08 04:23:18461gfx::Rect View::GetMirroredBounds() const {
[email protected]97a31142011-02-08 00:09:16462 gfx::Rect bounds(bounds_);
[email protected]9defbad2011-02-08 04:23:18463 bounds.set_x(GetMirroredX());
[email protected]97a31142011-02-08 00:09:16464 return bounds;
465}
466
[email protected]9defbad2011-02-08 04:23:18467gfx::Point View::GetMirroredPosition() const {
468 return gfx::Point(GetMirroredX(), y());
[email protected]97a31142011-02-08 00:09:16469}
470
[email protected]9defbad2011-02-08 04:23:18471int View::GetMirroredX() const {
[email protected]2fa942b2011-06-14 15:59:55472 return parent_ ? parent_->GetMirroredXForRect(bounds_) : x();
[email protected]2df3cc92011-02-05 00:38:59473}
474
[email protected]9defbad2011-02-08 04:23:18475int View::GetMirroredXForRect(const gfx::Rect& bounds) const {
[email protected]2df3cc92011-02-05 00:38:59476 return base::i18n::IsRTL() ?
477 (width() - bounds.x() - bounds.width()) : bounds.x();
478}
479
[email protected]9defbad2011-02-08 04:23:18480int View::GetMirroredXInView(int x) const {
481 return base::i18n::IsRTL() ? width() - x : x;
482}
483
484int View::GetMirroredXWithWidthInView(int x, int w) const {
485 return base::i18n::IsRTL() ? width() - x - w : x;
486}
487
[email protected]2df3cc92011-02-05 00:38:59488// Layout ----------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29489
490void View::Layout() {
[email protected]9ea053e2010-07-15 08:19:05491 needs_layout_ = false;
492
[email protected]f8617192010-07-05 07:57:10493 // If we have a layout manager, let it handle the layout for us.
[email protected]8eb52a9a2011-03-09 16:52:00494 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29495 layout_manager_->Layout(this);
initial.commit09911bf2008-07-26 23:55:29496
[email protected]9ea053e2010-07-15 08:19:05497 // Make sure to propagate the Layout() call to any children that haven't
498 // received it yet through the layout manager and need to be laid out. This
499 // is needed for the case when the child requires a layout but its bounds
500 // weren't changed by the layout manager. If there is no layout manager, we
501 // just propagate the Layout() call down the hierarchy, so whoever receives
502 // the call can take appropriate action.
[email protected]20838602011-02-09 04:50:21503 for (int i = 0, count = child_count(); i < count; ++i) {
[email protected]fe84b912011-07-15 17:13:33504 View* child = child_at(i);
[email protected]9ea053e2010-07-15 08:19:05505 if (child->needs_layout_ || !layout_manager_.get()) {
506 child->needs_layout_ = false;
507 child->Layout();
508 }
initial.commit09911bf2008-07-26 23:55:29509 }
510}
511
[email protected]9ea053e2010-07-15 08:19:05512void View::InvalidateLayout() {
[email protected]de8d613d2010-10-25 22:30:10513 // Always invalidate up. This is needed to handle the case of us already being
514 // valid, but not our parent.
[email protected]9ea053e2010-07-15 08:19:05515 needs_layout_ = true;
516 if (parent_)
517 parent_->InvalidateLayout();
518}
519
initial.commit09911bf2008-07-26 23:55:29520LayoutManager* View::GetLayoutManager() const {
521 return layout_manager_.get();
522}
523
524void View::SetLayoutManager(LayoutManager* layout_manager) {
[email protected]09fe9492009-11-07 02:23:06525 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29526 layout_manager_->Uninstalled(this);
[email protected]09fe9492009-11-07 02:23:06527
initial.commit09911bf2008-07-26 23:55:29528 layout_manager_.reset(layout_manager);
[email protected]09fe9492009-11-07 02:23:06529 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29530 layout_manager_->Installed(this);
initial.commit09911bf2008-07-26 23:55:29531}
532
[email protected]2df3cc92011-02-05 00:38:59533// Attributes ------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29534
[email protected]2df3cc92011-02-05 00:38:59535std::string View::GetClassName() const {
536 return kViewClassName;
initial.commit09911bf2008-07-26 23:55:29537}
538
[email protected]2df3cc92011-02-05 00:38:59539View* View::GetAncestorWithClassName(const std::string& name) {
[email protected]2fa942b2011-06-14 15:59:55540 for (View* view = this; view; view = view->parent_) {
[email protected]2df3cc92011-02-05 00:38:59541 if (view->GetClassName() == name)
542 return view;
543 }
544 return NULL;
initial.commit09911bf2008-07-26 23:55:29545}
546
[email protected]20838602011-02-09 04:50:21547const View* View::GetViewByID(int id) const {
[email protected]2df3cc92011-02-05 00:38:59548 if (id == id_)
549 return const_cast<View*>(this);
initial.commit09911bf2008-07-26 23:55:29550
[email protected]20838602011-02-09 04:50:21551 for (int i = 0, count = child_count(); i < count; ++i) {
[email protected]fe84b912011-07-15 17:13:33552 const View* view = child_at(i)->GetViewByID(id);
[email protected]2df3cc92011-02-05 00:38:59553 if (view)
554 return view;
555 }
556 return NULL;
initial.commit09911bf2008-07-26 23:55:29557}
558
[email protected]20838602011-02-09 04:50:21559View* View::GetViewByID(int id) {
560 return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id));
561}
562
[email protected]2df3cc92011-02-05 00:38:59563void View::SetGroup(int gid) {
564 // Don't change the group id once it's set.
565 DCHECK(group_ == -1 || group_ == gid);
566 group_ = gid;
567}
568
569int View::GetGroup() const {
570 return group_;
571}
572
[email protected]16460642011-03-04 23:15:53573bool View::IsGroupFocusTraversable() const {
574 return true;
575}
576
[email protected]48250ad2011-07-13 22:06:28577void View::GetViewsInGroup(int group, Views* views) {
578 if (group_ == group)
579 views->push_back(this);
[email protected]2df3cc92011-02-05 00:38:59580
[email protected]20838602011-02-09 04:50:21581 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:33582 child_at(i)->GetViewsInGroup(group, views);
[email protected]2df3cc92011-02-05 00:38:59583}
584
[email protected]48250ad2011-07-13 22:06:28585View* View::GetSelectedViewForGroup(int group) {
[email protected]695e942e2011-06-06 15:15:51586 Views views;
[email protected]48250ad2011-07-13 22:06:28587 GetWidget()->GetRootView()->GetViewsInGroup(group, &views);
[email protected]6775e40a2011-03-04 21:03:47588 return views.empty() ? NULL : views[0];
[email protected]2df3cc92011-02-05 00:38:59589}
590
591// Coordinate conversion -------------------------------------------------------
592
593// static
594void View::ConvertPointToView(const View* src,
595 const View* dst,
596 gfx::Point* point) {
597 ConvertPointToView(src, dst, point, true);
598}
599
600// static
601void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
602 DCHECK(src);
603 DCHECK(p);
604
[email protected]36df22b2011-02-24 21:47:56605 src->ConvertPointForAncestor(NULL, p);
[email protected]2df3cc92011-02-05 00:38:59606}
607
608// static
609void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) {
[email protected]36df22b2011-02-24 21:47:56610 DCHECK(dest);
611 DCHECK(p);
612
613 dest->ConvertPointFromAncestor(NULL, p);
[email protected]2df3cc92011-02-05 00:38:59614}
615
616// static
617void View::ConvertPointToScreen(const View* src, gfx::Point* p) {
618 DCHECK(src);
619 DCHECK(p);
620
621 // If the view is not connected to a tree, there's nothing we can do.
[email protected]20838602011-02-09 04:50:21622 const Widget* widget = src->GetWidget();
[email protected]2df3cc92011-02-05 00:38:59623 if (widget) {
624 ConvertPointToWidget(src, p);
[email protected]642c224f62011-03-03 17:30:05625 gfx::Rect r = widget->GetClientAreaScreenBounds();
[email protected]2df3cc92011-02-05 00:38:59626 p->SetPoint(p->x() + r.x(), p->y() + r.y());
initial.commit09911bf2008-07-26 23:55:29627 }
628}
629
[email protected]eb93b619b2011-02-15 22:53:19630gfx::Rect View::ConvertRectToParent(const gfx::Rect& rect) const {
[email protected]36df22b2011-02-24 21:47:56631 gfx::Rect x_rect = rect;
[email protected]18dab372011-10-03 21:21:44632 GetTransform().TransformRect(&x_rect);
[email protected]a1f4eb52011-06-29 15:06:04633 x_rect.Offset(GetMirroredPosition());
634 return x_rect;
635}
636
637gfx::Rect View::ConvertRectToWidget(const gfx::Rect& rect) const {
638 gfx::Rect x_rect = rect;
639 for (const View* v = this; v; v = v->parent_)
640 x_rect = v->ConvertRectToParent(x_rect);
[email protected]36df22b2011-02-24 21:47:56641 return x_rect;
642}
643
[email protected]2df3cc92011-02-05 00:38:59644// Painting --------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29645
[email protected]7907e592011-02-15 19:05:30646void View::SchedulePaint() {
[email protected]0a119c92011-02-23 17:50:56647 SchedulePaintInRect(GetLocalBounds());
[email protected]7907e592011-02-15 19:05:30648}
649
[email protected]0a119c92011-02-23 17:50:56650void View::SchedulePaintInRect(const gfx::Rect& rect) {
[email protected]a8f21152011-09-08 15:30:02651 if (!IsVisible() || !painting_enabled_)
initial.commit09911bf2008-07-26 23:55:29652 return;
initial.commit09911bf2008-07-26 23:55:29653
[email protected]3aa43942011-09-13 20:59:53654 if (layer()) {
655 layer()->SchedulePaint(rect);
656 } else if (parent_) {
657 // Translate the requested paint rect to the parent's coordinate system
658 // then pass this notification up to the parent.
659 parent_->SchedulePaintInRect(ConvertRectToParent(rect));
660 }
initial.commit09911bf2008-07-26 23:55:29661}
662
[email protected]0bcebb02011-02-16 03:37:04663void View::Paint(gfx::Canvas* canvas) {
[email protected]a8f21152011-09-08 15:30:02664 TRACE_EVENT0("View", "Paint");
[email protected]3aa43942011-09-13 20:59:53665
666 ScopedCanvas scoped_canvas(canvas);
667
668 // Paint this View and its children, setting the clip rect to the bounds
669 // of this View and translating the origin to the local bounds' top left
670 // point.
671 //
672 // Note that the X (or left) position we pass to ClipRectInt takes into
673 // consideration whether or not the view uses a right-to-left layout so that
674 // we paint our view in its mirrored position if need be.
675 if (!canvas->ClipRectInt(GetMirroredX(), y(),
676 width() - static_cast<int>(clip_x_),
677 height() - static_cast<int>(clip_y_))) {
initial.commit09911bf2008-07-26 23:55:29678 return;
initial.commit09911bf2008-07-26 23:55:29679 }
[email protected]3aa43942011-09-13 20:59:53680 // Non-empty clip, translate the graphics such that 0,0 corresponds to
681 // where this view is located (related to its parent).
682 canvas->TranslateInt(GetMirroredX(), y());
initial.commit09911bf2008-07-26 23:55:29683
[email protected]18dab372011-10-03 21:21:44684 canvas->Transform(GetTransform());
[email protected]f1785de2011-03-15 19:27:49685
[email protected]3aa43942011-09-13 20:59:53686 PaintCommon(canvas);
initial.commit09911bf2008-07-26 23:55:29687}
688
[email protected]2df3cc92011-02-05 00:38:59689ThemeProvider* View::GetThemeProvider() const {
[email protected]20838602011-02-09 04:50:21690 const Widget* widget = GetWidget();
[email protected]2df3cc92011-02-05 00:38:59691 return widget ? widget->GetThemeProvider() : NULL;
692}
693
[email protected]c797cd42011-03-15 02:18:36694// Accelerated Painting --------------------------------------------------------
695
696// static
697void View::set_use_acceleration_when_possible(bool use) {
698 use_acceleration_when_possible = use;
699}
700
[email protected]d7e2ee9d2011-06-09 13:07:11701// static
702bool View::get_use_acceleration_when_possible() {
703 return use_acceleration_when_possible;
704}
705
[email protected]2df3cc92011-02-05 00:38:59706// Input -----------------------------------------------------------------------
707
[email protected]e4607f02011-02-25 21:51:51708View* View::GetEventHandlerForPoint(const gfx::Point& point) {
[email protected]2df3cc92011-02-05 00:38:59709 // Walk the child Views recursively looking for the View that most
710 // tightly encloses the specified point.
[email protected]20838602011-02-09 04:50:21711 for (int i = child_count() - 1; i >= 0; --i) {
[email protected]fe84b912011-07-15 17:13:33712 View* child = child_at(i);
[email protected]2df3cc92011-02-05 00:38:59713 if (!child->IsVisible())
714 continue;
715
716 gfx::Point point_in_child_coords(point);
717 View::ConvertPointToView(this, child, &point_in_child_coords);
718 if (child->HitTest(point_in_child_coords))
[email protected]e4607f02011-02-25 21:51:51719 return child->GetEventHandlerForPoint(point_in_child_coords);
[email protected]2df3cc92011-02-05 00:38:59720 }
721 return this;
initial.commit09911bf2008-07-26 23:55:29722}
723
[email protected]3446c062011-05-03 21:48:00724gfx::NativeCursor View::GetCursor(const MouseEvent& event) {
[email protected]5c303962011-04-27 03:48:14725#if defined(OS_WIN)
726 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW);
727 return arrow;
728#else
[email protected]9abf8dd62009-06-04 06:40:42729 return NULL;
[email protected]5c303962011-04-27 03:48:14730#endif
[email protected]9abf8dd62009-06-04 06:40:42731}
732
[email protected]a52ca4672009-06-05 05:41:09733bool View::HitTest(const gfx::Point& l) const {
[email protected]5c303962011-04-27 03:48:14734 if (GetLocalBounds().Contains(l)) {
[email protected]a52ca4672009-06-05 05:41:09735 if (HasHitTestMask()) {
736 gfx::Path mask;
737 GetHitTestMask(&mask);
[email protected]b7bf6b682011-09-30 18:05:23738#if defined(USE_AURA)
[email protected]af9c1fa32011-10-04 15:29:02739 // TODO: should we use this every where?
740 SkRegion clip_region;
741 clip_region.setRect(0, 0, width(), height());
742 SkRegion mask_region;
743 return mask_region.setPath(mask, clip_region) &&
744 mask_region.contains(l.x(), l.y());
[email protected]b7bf6b682011-09-30 18:05:23745#elif defined(OS_WIN)
[email protected]a684f122010-12-29 23:48:46746 base::win::ScopedRegion rgn(mask.CreateNativeRegion());
[email protected]a52ca4672009-06-05 05:41:09747 return !!PtInRegion(rgn, l.x(), l.y());
[email protected]10a6e77b2009-12-31 01:03:52748#elif defined(TOOLKIT_USES_GTK)
[email protected]9dd7e3d72011-01-20 18:27:06749 ui::ScopedRegion rgn(mask.CreateNativeRegion());
[email protected]a3406972009-11-04 05:05:48750 return gdk_region_point_in(rgn.Get(), l.x(), l.y());
[email protected]a52ca4672009-06-05 05:41:09751#endif
752 }
753 // No mask, but inside our bounds.
754 return true;
755 }
756 // Outside our bounds.
757 return false;
758}
759
[email protected]2c831b22011-03-14 23:17:20760bool View::OnMousePressed(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59761 return false;
762}
763
[email protected]2c831b22011-03-14 23:17:20764bool View::OnMouseDragged(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59765 return false;
766}
767
[email protected]34338252011-03-29 00:39:05768void View::OnMouseReleased(const MouseEvent& event) {
769}
770
771void View::OnMouseCaptureLost() {
[email protected]2df3cc92011-02-05 00:38:59772}
773
[email protected]2c831b22011-03-14 23:17:20774void View::OnMouseMoved(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59775}
776
[email protected]2c831b22011-03-14 23:17:20777void View::OnMouseEntered(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59778}
779
[email protected]2c831b22011-03-14 23:17:20780void View::OnMouseExited(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59781}
782
[email protected]6a4056b2011-06-14 22:09:17783ui::TouchStatus View::OnTouchEvent(const TouchEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59784 DVLOG(1) << "visited the OnTouchEvent";
[email protected]6a4056b2011-06-14 22:09:17785 return ui::TOUCH_STATUS_UNKNOWN;
[email protected]2df3cc92011-02-05 00:38:59786}
[email protected]2df3cc92011-02-05 00:38:59787
788void View::SetMouseHandler(View *new_mouse_handler) {
789 // It is valid for new_mouse_handler to be NULL
790 if (parent_)
791 parent_->SetMouseHandler(new_mouse_handler);
792}
793
[email protected]2c831b22011-03-14 23:17:20794bool View::OnKeyPressed(const KeyEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59795 return false;
796}
797
[email protected]2c831b22011-03-14 23:17:20798bool View::OnKeyReleased(const KeyEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59799 return false;
800}
801
[email protected]2c831b22011-03-14 23:17:20802bool View::OnMouseWheel(const MouseWheelEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59803 return false;
804}
805
[email protected]9fa3de1a2011-03-31 21:12:08806TextInputClient* View::GetTextInputClient() {
807 return NULL;
808}
809
810InputMethod* View::GetInputMethod() {
[email protected]6c8a4312011-04-01 22:05:14811 Widget* widget = GetWidget();
812 return widget ? widget->GetInputMethod() : NULL;
[email protected]9fa3de1a2011-03-31 21:12:08813}
814
[email protected]2df3cc92011-02-05 00:38:59815// Accelerators ----------------------------------------------------------------
816
817void View::AddAccelerator(const Accelerator& accelerator) {
818 if (!accelerators_.get())
819 accelerators_.reset(new std::vector<Accelerator>());
820
[email protected]afed4572011-06-09 18:00:11821 DCHECK(std::find(accelerators_->begin(), accelerators_->end(), accelerator) ==
822 accelerators_->end())
[email protected]2df3cc92011-02-05 00:38:59823 << "Registering the same accelerator multiple times";
824
825 accelerators_->push_back(accelerator);
826 RegisterPendingAccelerators();
827}
828
829void View::RemoveAccelerator(const Accelerator& accelerator) {
[email protected]afed4572011-06-09 18:00:11830 if (!accelerators_.get()) {
[email protected]2df3cc92011-02-05 00:38:59831 NOTREACHED() << "Removing non-existing accelerator";
832 return;
833 }
834
[email protected]afed4572011-06-09 18:00:11835 std::vector<Accelerator>::iterator i(
836 std::find(accelerators_->begin(), accelerators_->end(), accelerator));
837 if (i == accelerators_->end()) {
838 NOTREACHED() << "Removing non-existing accelerator";
839 return;
840 }
841
842 size_t index = i - accelerators_->begin();
843 accelerators_->erase(i);
[email protected]2df3cc92011-02-05 00:38:59844 if (index >= registered_accelerator_count_) {
845 // The accelerator is not registered to FocusManager.
846 return;
847 }
848 --registered_accelerator_count_;
849
[email protected]72f17242011-02-18 21:05:55850 // Providing we are attached to a Widget and registered with a focus manager,
851 // we should de-register from that focus manager now.
852 if (GetWidget() && accelerator_focus_manager_)
[email protected]2df3cc92011-02-05 00:38:59853 accelerator_focus_manager_->UnregisterAccelerator(accelerator, this);
[email protected]2df3cc92011-02-05 00:38:59854}
855
856void View::ResetAccelerators() {
857 if (accelerators_.get())
858 UnregisterAccelerators(false);
859}
860
[email protected]16460642011-03-04 23:15:53861bool View::AcceleratorPressed(const Accelerator& accelerator) {
862 return false;
863}
864
[email protected]2df3cc92011-02-05 00:38:59865// Focus -----------------------------------------------------------------------
866
867bool View::HasFocus() {
868 FocusManager* focus_manager = GetFocusManager();
869 if (focus_manager)
870 return focus_manager->GetFocusedView() == this;
871 return false;
872}
873
874View* View::GetNextFocusableView() {
875 return next_focusable_view_;
876}
877
[email protected]20838602011-02-09 04:50:21878const View* View::GetNextFocusableView() const {
879 return next_focusable_view_;
880}
881
[email protected]2df3cc92011-02-05 00:38:59882View* View::GetPreviousFocusableView() {
883 return previous_focusable_view_;
884}
885
886void View::SetNextFocusableView(View* view) {
887 view->previous_focusable_view_ = this;
888 next_focusable_view_ = view;
889}
890
[email protected]2df3cc92011-02-05 00:38:59891bool View::IsFocusableInRootView() const {
892 return IsFocusable() && IsVisibleInRootView();
893}
894
895bool View::IsAccessibilityFocusableInRootView() const {
896 return (focusable_ || accessibility_focusable_) && IsEnabled() &&
897 IsVisibleInRootView();
898}
899
900FocusManager* View::GetFocusManager() {
901 Widget* widget = GetWidget();
902 return widget ? widget->GetFocusManager() : NULL;
903}
904
905void View::RequestFocus() {
[email protected]72f17242011-02-18 21:05:55906 FocusManager* focus_manager = GetFocusManager();
[email protected]6b78e39f2011-08-22 23:57:32907 if (focus_manager && IsFocusableInRootView())
[email protected]72f17242011-02-18 21:05:55908 focus_manager->SetFocusedView(this);
[email protected]2df3cc92011-02-05 00:38:59909}
910
[email protected]2c831b22011-03-14 23:17:20911bool View::SkipDefaultKeyEventProcessing(const KeyEvent& event) {
[email protected]16460642011-03-04 23:15:53912 return false;
913}
914
915FocusTraversable* View::GetFocusTraversable() {
916 return NULL;
917}
918
919FocusTraversable* View::GetPaneFocusTraversable() {
920 return NULL;
921}
922
[email protected]2df3cc92011-02-05 00:38:59923// Tooltips --------------------------------------------------------------------
924
[email protected]8d37b112011-09-15 18:01:01925bool View::GetTooltipText(const gfx::Point& p, string16* tooltip) {
[email protected]2df3cc92011-02-05 00:38:59926 return false;
927}
928
929bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) {
930 return false;
931}
932
933// Context menus ---------------------------------------------------------------
934
[email protected]e9adf0702010-03-08 23:34:07935void View::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) {
[email protected]042811c2008-10-31 21:31:34936 if (!context_menu_controller_)
937 return;
938
[email protected]0933ab82011-03-04 04:57:58939 context_menu_controller_->ShowContextMenuForView(this, p, is_mouse_gesture);
[email protected]042811c2008-10-31 21:31:34940}
941
[email protected]2df3cc92011-02-05 00:38:59942// Drag and drop ---------------------------------------------------------------
943
[email protected]2df3cc92011-02-05 00:38:59944bool View::GetDropFormats(
945 int* formats,
946 std::set<OSExchangeData::CustomFormat>* custom_formats) {
947 return false;
948}
949
950bool View::AreDropTypesRequired() {
951 return false;
952}
953
954bool View::CanDrop(const OSExchangeData& data) {
955 // TODO(sky): when I finish up migration, this should default to true.
956 return false;
957}
958
959void View::OnDragEntered(const DropTargetEvent& event) {
960}
961
962int View::OnDragUpdated(const DropTargetEvent& event) {
963 return ui::DragDropTypes::DRAG_NONE;
964}
965
966void View::OnDragExited() {
967}
968
969int View::OnPerformDrop(const DropTargetEvent& event) {
970 return ui::DragDropTypes::DRAG_NONE;
971}
972
[email protected]72f17242011-02-18 21:05:55973void View::OnDragDone() {
974}
975
[email protected]2df3cc92011-02-05 00:38:59976// static
977bool View::ExceededDragThreshold(int delta_x, int delta_y) {
978 return (abs(delta_x) > GetHorizontalDragThreshold() ||
979 abs(delta_y) > GetVerticalDragThreshold());
980}
981
[email protected]2df3cc92011-02-05 00:38:59982// Scrolling -------------------------------------------------------------------
983
984void View::ScrollRectToVisible(const gfx::Rect& rect) {
[email protected]2df3cc92011-02-05 00:38:59985 // We must take RTL UI mirroring into account when adjusting the position of
986 // the region.
[email protected]2fa942b2011-06-14 15:59:55987 if (parent_) {
[email protected]2df3cc92011-02-05 00:38:59988 gfx::Rect scroll_rect(rect);
[email protected]9defbad2011-02-08 04:23:18989 scroll_rect.Offset(GetMirroredX(), y());
[email protected]2fa942b2011-06-14 15:59:55990 parent_->ScrollRectToVisible(scroll_rect);
[email protected]2df3cc92011-02-05 00:38:59991 }
992}
993
994int View::GetPageScrollIncrement(ScrollView* scroll_view,
995 bool is_horizontal, bool is_positive) {
996 return 0;
997}
998
999int View::GetLineScrollIncrement(ScrollView* scroll_view,
1000 bool is_horizontal, bool is_positive) {
1001 return 0;
1002}
1003
1004////////////////////////////////////////////////////////////////////////////////
1005// View, protected:
1006
1007// Size and disposition --------------------------------------------------------
1008
[email protected]8eb52a9a2011-03-09 16:52:001009void View::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1010}
1011
[email protected]2df3cc92011-02-05 00:38:591012void View::PreferredSizeChanged() {
1013 InvalidateLayout();
1014 if (parent_)
1015 parent_->ChildPreferredSizeChanged(this);
1016}
1017
[email protected]6260f9e2011-02-16 21:28:011018bool View::NeedsNotificationWhenVisibleBoundsChange() const {
1019 return false;
[email protected]2df3cc92011-02-05 00:38:591020}
1021
[email protected]6260f9e2011-02-16 21:28:011022void View::OnVisibleBoundsChanged() {
[email protected]2df3cc92011-02-05 00:38:591023}
1024
1025// Tree operations -------------------------------------------------------------
1026
1027void View::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
1028}
1029
1030void View::VisibilityChanged(View* starting_from, bool is_visible) {
1031}
1032
1033void View::NativeViewHierarchyChanged(bool attached,
1034 gfx::NativeView native_view,
[email protected]10946072011-05-20 15:40:451035 internal::RootView* root_view) {
[email protected]2df3cc92011-02-05 00:38:591036 FocusManager* focus_manager = GetFocusManager();
1037 if (!accelerator_registration_delayed_ &&
1038 accelerator_focus_manager_ &&
1039 accelerator_focus_manager_ != focus_manager) {
1040 UnregisterAccelerators(true);
1041 accelerator_registration_delayed_ = true;
1042 }
1043 if (accelerator_registration_delayed_ && attached) {
1044 if (focus_manager) {
1045 RegisterPendingAccelerators();
1046 accelerator_registration_delayed_ = false;
1047 }
1048 }
1049}
1050
1051// Painting --------------------------------------------------------------------
1052
[email protected]0bcebb02011-02-16 03:37:041053void View::PaintChildren(gfx::Canvas* canvas) {
[email protected]fe84b912011-07-15 17:13:331054 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]23601582011-09-28 13:32:181055 if (!child_at(i)->layer())
1056 child_at(i)->Paint(canvas);
[email protected]0bcebb02011-02-16 03:37:041057}
1058
1059void View::OnPaint(gfx::Canvas* canvas) {
1060 OnPaintBackground(canvas);
1061 OnPaintFocusBorder(canvas);
1062 OnPaintBorder(canvas);
1063}
1064
1065void View::OnPaintBackground(gfx::Canvas* canvas) {
1066 if (background_.get())
1067 background_->Paint(canvas, this);
1068}
1069
1070void View::OnPaintBorder(gfx::Canvas* canvas) {
1071 if (border_.get())
1072 border_->Paint(*this, canvas);
1073}
1074
1075void View::OnPaintFocusBorder(gfx::Canvas* canvas) {
[email protected]a1a9e7c82011-04-15 21:58:241076 if ((IsFocusable() || IsAccessibilityFocusableInRootView()) && HasFocus())
[email protected]0bcebb02011-02-16 03:37:041077 canvas->DrawFocusRect(0, 0, width(), height());
1078}
1079
[email protected]c797cd42011-03-15 02:18:361080// Accelerated Painting --------------------------------------------------------
1081
[email protected]c155c252011-07-29 16:17:551082void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) {
[email protected]18dab372011-10-03 21:21:441083 // This method should not have the side-effect of creating the layer.
[email protected]c155c252011-07-29 16:17:551084 if (layer())
1085 layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely);
1086}
1087
[email protected]1cbbee3c2011-06-24 12:32:191088bool View::SetExternalTexture(ui::Texture* texture) {
[email protected]28cd2bb2011-09-19 21:04:191089 DCHECK(texture);
[email protected]18dab372011-10-03 21:21:441090 SetPaintToLayer(true);
[email protected]7c4b1b92011-06-27 16:37:181091
[email protected]18dab372011-10-03 21:21:441092 layer()->SetExternalTexture(texture);
[email protected]1cbbee3c2011-06-24 12:32:191093
[email protected]3a1a0cc2011-08-10 14:36:421094 // Child views must not paint into the external texture. So make sure each
1095 // child view has its own layer to paint into.
[email protected]28cd2bb2011-09-19 21:04:191096 for (Views::iterator i = children_.begin(); i != children_.end(); ++i)
1097 (*i)->SetPaintToLayer(true);
[email protected]3a1a0cc2011-08-10 14:36:421098
[email protected]3aa43942011-09-13 20:59:531099 SchedulePaintInRect(GetLocalBounds());
[email protected]1cbbee3c2011-06-24 12:32:191100 return true;
1101}
1102
[email protected]e865b362011-07-01 17:09:461103void View::CalculateOffsetToAncestorWithLayer(gfx::Point* offset,
[email protected]3aa43942011-09-13 20:59:531104 ui::Layer** layer_parent) {
[email protected]e865b362011-07-01 17:09:461105 if (layer()) {
[email protected]3aa43942011-09-13 20:59:531106 if (layer_parent)
1107 *layer_parent = layer();
[email protected]e865b362011-07-01 17:09:461108 return;
1109 }
1110 if (!parent_)
1111 return;
1112
1113 offset->Offset(x(), y());
[email protected]3aa43942011-09-13 20:59:531114 parent_->CalculateOffsetToAncestorWithLayer(offset, layer_parent);
[email protected]e865b362011-07-01 17:09:461115}
1116
[email protected]e865b362011-07-01 17:09:461117void View::MoveLayerToParent(ui::Layer* parent_layer,
1118 const gfx::Point& point) {
1119 gfx::Point local_point(point);
1120 if (parent_layer != layer())
1121 local_point.Offset(x(), y());
1122 if (layer() && parent_layer != layer()) {
1123 parent_layer->Add(layer());
[email protected]18dab372011-10-03 21:21:441124 layer()->SetBounds(gfx::Rect(local_point.x(), local_point.y(),
1125 width(), height()));
[email protected]e865b362011-07-01 17:09:461126 } else {
1127 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331128 child_at(i)->MoveLayerToParent(parent_layer, local_point);
[email protected]e865b362011-07-01 17:09:461129 }
1130}
1131
[email protected]18dab372011-10-03 21:21:441132void View::UpdateChildLayerBounds(const gfx::Point& offset) {
[email protected]e865b362011-07-01 17:09:461133 if (layer()) {
[email protected]18dab372011-10-03 21:21:441134 layer_property_setter_->SetBounds(layer(), gfx::Rect(offset.x(), offset.y(),
1135 width(), height()));
[email protected]e865b362011-07-01 17:09:461136 } else {
[email protected]18dab372011-10-03 21:21:441137 for (int i = 0, count = child_count(); i < count; ++i) {
1138 gfx::Point new_offset(offset.x() + child_at(i)->x(),
1139 offset.y() + child_at(i)->y());
1140 child_at(i)->UpdateChildLayerBounds(new_offset);
1141 }
[email protected]e865b362011-07-01 17:09:461142 }
1143}
1144
[email protected]3aa43942011-09-13 20:59:531145void View::OnPaintLayer(gfx::Canvas* canvas) {
[email protected]be172ba82011-10-05 19:05:071146 canvas->GetSkCanvas()->drawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
[email protected]3aa43942011-09-13 20:59:531147 PaintCommon(canvas);
1148}
1149
[email protected]9ef00a52011-10-08 11:53:351150void View::ReorderLayers() {
1151 View* v = this;
1152 while (v && !v->layer())
1153 v = v->parent();
1154
1155 // Forward to widget in case we're in a NativeWidgetView.
1156 if (!v) {
1157 if (GetWidget())
1158 GetWidget()->ReorderLayers();
1159 } else {
1160 for (Views::const_iterator i(v->children_.begin());
1161 i != v->children_.end();
1162 ++i)
1163 (*i)->ReorderChildLayers(v->layer());
1164 }
1165}
1166
1167void View::ReorderChildLayers(ui::Layer* parent_layer) {
1168 if (layer()) {
1169 DCHECK_EQ(parent_layer, layer()->parent());
1170 parent_layer->MoveToFront(layer());
1171 } else {
1172 for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i)
1173 (*i)->ReorderChildLayers(parent_layer);
1174 }
1175}
1176
[email protected]2df3cc92011-02-05 00:38:591177// Input -----------------------------------------------------------------------
1178
1179bool View::HasHitTestMask() const {
1180 return false;
1181}
1182
1183void View::GetHitTestMask(gfx::Path* mask) const {
1184 DCHECK(mask);
1185}
1186
1187// Focus -----------------------------------------------------------------------
1188
1189bool View::IsFocusable() const {
1190 return focusable_ && IsEnabled() && IsVisible();
1191}
1192
[email protected]420138332011-02-24 00:59:521193void View::OnFocus() {
1194 // TODO(beng): Investigate whether it's possible for us to move this to
1195 // Focus().
[email protected]2df3cc92011-02-05 00:38:591196 // By default, we clear the native focus. This ensures that no visible native
1197 // view as the focus and that we still receive keyboard inputs.
1198 FocusManager* focus_manager = GetFocusManager();
1199 if (focus_manager)
1200 focus_manager->ClearNativeFocus();
1201
[email protected]420138332011-02-24 00:59:521202 // TODO(beng): Investigate whether it's possible for us to move this to
1203 // Focus().
[email protected]2df3cc92011-02-05 00:38:591204 // Notify assistive technologies of the focus change.
[email protected]79e549f2011-03-14 06:56:331205 GetWidget()->NotifyAccessibilityEvent(
1206 this, ui::AccessibilityTypes::EVENT_FOCUS, true);
[email protected]2df3cc92011-02-05 00:38:591207}
1208
[email protected]420138332011-02-24 00:59:521209void View::OnBlur() {
1210}
1211
1212void View::Focus() {
1213 SchedulePaint();
1214 OnFocus();
1215}
1216
1217void View::Blur() {
1218 SchedulePaint();
1219 OnBlur();
1220}
1221
[email protected]2df3cc92011-02-05 00:38:591222// Tooltips --------------------------------------------------------------------
1223
1224void View::TooltipTextChanged() {
1225 Widget* widget = GetWidget();
[email protected]2463b91b2011-06-13 21:11:371226 // TooltipManager may be null if there is a problem creating it.
[email protected]2f2b57b2011-06-16 17:21:231227 if (widget && widget->native_widget_private()->GetTooltipManager()) {
1228 widget->native_widget_private()->GetTooltipManager()->
1229 TooltipTextChanged(this);
1230 }
[email protected]2df3cc92011-02-05 00:38:591231}
1232
1233// Context menus ---------------------------------------------------------------
1234
1235gfx::Point View::GetKeyboardContextMenuLocation() {
1236 gfx::Rect vis_bounds = GetVisibleBounds();
1237 gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2,
1238 vis_bounds.y() + vis_bounds.height() / 2);
1239 ConvertPointToScreen(this, &screen_point);
1240 return screen_point;
1241}
1242
1243// Drag and drop ---------------------------------------------------------------
1244
1245int View::GetDragOperations(const gfx::Point& press_pt) {
1246 return drag_controller_ ?
[email protected]c3fac082011-03-04 01:04:181247 drag_controller_->GetDragOperationsForView(this, press_pt) :
[email protected]2df3cc92011-02-05 00:38:591248 ui::DragDropTypes::DRAG_NONE;
1249}
1250
1251void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) {
1252 DCHECK(drag_controller_);
[email protected]c3fac082011-03-04 01:04:181253 drag_controller_->WriteDragDataForView(this, press_pt, data);
[email protected]2df3cc92011-02-05 00:38:591254}
1255
[email protected]2df3cc92011-02-05 00:38:591256bool View::InDrag() {
[email protected]72f17242011-02-18 21:05:551257 Widget* widget = GetWidget();
[email protected]9861ae92011-03-03 20:15:031258 return widget ? widget->dragged_view() == this : false;
[email protected]2df3cc92011-02-05 00:38:591259}
1260
[email protected]36de1cb2011-09-20 20:47:071261// Debugging -------------------------------------------------------------------
1262
1263#if !defined(NDEBUG)
1264
1265std::string View::PrintViewGraph(bool first) {
1266 return DoPrintViewGraph(first, this);
1267}
1268
1269std::string View::DoPrintViewGraph(bool first, View* view_with_children) {
1270 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
1271 const size_t kMaxPointerStringLength = 19;
1272
1273 std::string result;
1274
1275 if (first)
1276 result.append("digraph {\n");
1277
1278 // Node characteristics.
1279 char p[kMaxPointerStringLength];
1280
1281 size_t baseNameIndex = GetClassName().find_last_of('/');
1282 if (baseNameIndex == std::string::npos)
1283 baseNameIndex = 0;
1284 else
1285 baseNameIndex++;
1286
1287 char bounds_buffer[512];
1288
1289 // Information about current node.
1290 base::snprintf(p, arraysize(bounds_buffer), "%p", view_with_children);
1291 result.append(" N");
1292 result.append(p+2);
1293 result.append(" [label=\"");
1294
1295 result.append(GetClassName().substr(baseNameIndex).c_str());
1296
1297 base::snprintf(bounds_buffer,
1298 arraysize(bounds_buffer),
1299 "\\n bounds: (%d, %d), (%dx%d)",
1300 this->bounds().x(),
1301 this->bounds().y(),
1302 this->bounds().width(),
1303 this->bounds().height());
1304 result.append(bounds_buffer);
1305
1306 if (layer() && !layer()->hole_rect().IsEmpty()) {
1307 base::snprintf(bounds_buffer,
1308 arraysize(bounds_buffer),
1309 "\\n hole bounds: (%d, %d), (%dx%d)",
1310 layer()->hole_rect().x(),
1311 layer()->hole_rect().y(),
1312 layer()->hole_rect().width(),
1313 layer()->hole_rect().height());
1314 result.append(bounds_buffer);
1315 }
1316
1317 if (GetTransform().HasChange()) {
1318 gfx::Point translation;
1319 float rotation;
1320 gfx::Point3f scale;
1321 if (ui::InterpolatedTransform::FactorTRS(GetTransform(),
1322 &translation,
1323 &rotation,
1324 &scale)) {
1325 if (translation != gfx::Point(0, 0)) {
1326 base::snprintf(bounds_buffer,
1327 arraysize(bounds_buffer),
1328 "\\n translation: (%d, %d)",
1329 translation.x(),
1330 translation.y());
1331 result.append(bounds_buffer);
1332 }
1333
1334 if (fabs(rotation) > 1e-5) {
1335 base::snprintf(bounds_buffer,
1336 arraysize(bounds_buffer),
1337 "\\n rotation: %3.2f", rotation);
1338 result.append(bounds_buffer);
1339 }
1340
1341 if (scale.AsPoint() != gfx::Point(0, 0)) {
1342 base::snprintf(bounds_buffer,
1343 arraysize(bounds_buffer),
1344 "\\n scale: (%2.4f, %2.4f)",
1345 scale.x(),
1346 scale.y());
1347 result.append(bounds_buffer);
1348 }
1349 }
1350 }
1351
1352 result.append("\"");
1353 if (!parent_)
1354 result.append(", shape=box");
1355 if (layer()) {
1356 if (layer()->texture())
1357 result.append(", color=green");
1358 else
1359 result.append(", color=red");
1360
1361 if (layer()->fills_bounds_opaquely())
1362 result.append(", style=filled");
1363 }
1364 result.append("]\n");
1365
1366 // Link to parent.
1367 if (parent_) {
1368 char pp[kMaxPointerStringLength];
1369
1370 base::snprintf(pp, kMaxPointerStringLength, "%p", parent_);
1371 result.append(" N");
1372 result.append(pp+2);
1373 result.append(" -> N");
1374 result.append(p+2);
1375 result.append("\n");
1376 }
1377
1378 // Children.
1379 for (int i = 0, count = view_with_children->child_count(); i < count; ++i)
1380 result.append(view_with_children->child_at(i)->PrintViewGraph(false));
1381
1382 if (first)
1383 result.append("}\n");
1384
1385 return result;
1386}
1387
1388#endif
1389
[email protected]2df3cc92011-02-05 00:38:591390////////////////////////////////////////////////////////////////////////////////
1391// View, private:
1392
1393// DropInfo --------------------------------------------------------------------
1394
1395void View::DragInfo::Reset() {
1396 possible_drag = false;
1397 start_pt = gfx::Point();
1398}
1399
1400void View::DragInfo::PossibleDrag(const gfx::Point& p) {
1401 possible_drag = true;
1402 start_pt = p;
1403}
1404
[email protected]adc93fa72011-06-21 19:47:391405// Painting --------------------------------------------------------------------
1406
1407void View::SchedulePaintBoundsChanged(SchedulePaintType type) {
[email protected]3aa43942011-09-13 20:59:531408 // If we have a layer and the View's size did not change, we do not need to
1409 // schedule any paints since the layer will be redrawn at its new location
1410 // during the next Draw() cycle in the compositor.
1411 if (!layer() || type == SCHEDULE_PAINT_SIZE_CHANGED) {
1412 // Otherwise, if the size changes or we don't have a layer then we need to
1413 // use SchedulePaint to invalidate the area occupied by the View.
[email protected]adc93fa72011-06-21 19:47:391414 SchedulePaint();
1415 }
1416}
1417
[email protected]3aa43942011-09-13 20:59:531418void View::PaintCommon(gfx::Canvas* canvas) {
1419 if (!IsVisible() || !painting_enabled_)
1420 return;
1421
1422 {
1423 // If the View we are about to paint requested the canvas to be flipped, we
1424 // should change the transform appropriately.
1425 // The canvas mirroring is undone once the View is done painting so that we
1426 // don't pass the canvas with the mirrored transform to Views that didn't
1427 // request the canvas to be flipped.
1428 ScopedCanvas scoped(canvas);
1429 if (FlipCanvasOnPaintForRTLUI()) {
1430 canvas->TranslateInt(width(), 0);
1431 canvas->ScaleInt(-1, 1);
1432 }
1433
1434 OnPaint(canvas);
1435 }
1436
1437 PaintChildren(canvas);
1438}
1439
[email protected]2df3cc92011-02-05 00:38:591440// Tree operations -------------------------------------------------------------
1441
[email protected]20838602011-02-09 04:50:211442void View::DoRemoveChildView(View* view,
[email protected]2df3cc92011-02-05 00:38:591443 bool update_focus_cycle,
1444 bool update_tool_tip,
1445 bool delete_removed_view) {
[email protected]20838602011-02-09 04:50:211446 DCHECK(view);
[email protected]afed4572011-06-09 18:00:111447 const Views::iterator i(std::find(children_.begin(), children_.end(), view));
[email protected]2df3cc92011-02-05 00:38:591448 scoped_ptr<View> view_to_be_deleted;
[email protected]20838602011-02-09 04:50:211449 if (i != children_.end()) {
[email protected]2df3cc92011-02-05 00:38:591450 if (update_focus_cycle) {
1451 // Let's remove the view from the focus traversal.
[email protected]20838602011-02-09 04:50:211452 View* next_focusable = view->next_focusable_view_;
1453 View* prev_focusable = view->previous_focusable_view_;
[email protected]2df3cc92011-02-05 00:38:591454 if (prev_focusable)
1455 prev_focusable->next_focusable_view_ = next_focusable;
1456 if (next_focusable)
1457 next_focusable->previous_focusable_view_ = prev_focusable;
1458 }
1459
[email protected]6260f9e2011-02-16 21:28:011460 if (GetWidget())
1461 UnregisterChildrenForVisibleBoundsNotification(view);
[email protected]20838602011-02-09 04:50:211462 view->PropagateRemoveNotifications(this);
[email protected]b5fe7442011-05-26 02:42:431463 view->parent_ = NULL;
[email protected]2df3cc92011-02-05 00:38:591464
[email protected]3534e7a2011-05-26 17:44:031465 if (delete_removed_view && view->parent_owned())
[email protected]20838602011-02-09 04:50:211466 view_to_be_deleted.reset(view);
[email protected]2df3cc92011-02-05 00:38:591467
[email protected]20838602011-02-09 04:50:211468 children_.erase(i);
[email protected]2df3cc92011-02-05 00:38:591469 }
1470
1471 if (update_tool_tip)
1472 UpdateTooltip();
1473
1474 if (layout_manager_.get())
[email protected]20838602011-02-09 04:50:211475 layout_manager_->ViewRemoved(this, view);
[email protected]2df3cc92011-02-05 00:38:591476}
1477
[email protected]2df3cc92011-02-05 00:38:591478void View::PropagateRemoveNotifications(View* parent) {
[email protected]20838602011-02-09 04:50:211479 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331480 child_at(i)->PropagateRemoveNotifications(parent);
[email protected]2df3cc92011-02-05 00:38:591481
[email protected]2fa942b2011-06-14 15:59:551482 for (View* v = this; v; v = v->parent_)
[email protected]2df3cc92011-02-05 00:38:591483 v->ViewHierarchyChangedImpl(true, false, parent, this);
1484}
1485
1486void View::PropagateAddNotifications(View* parent, View* child) {
[email protected]20838602011-02-09 04:50:211487 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331488 child_at(i)->PropagateAddNotifications(parent, child);
[email protected]2df3cc92011-02-05 00:38:591489 ViewHierarchyChangedImpl(true, true, parent, child);
1490}
1491
1492void View::PropagateNativeViewHierarchyChanged(bool attached,
1493 gfx::NativeView native_view,
[email protected]10946072011-05-20 15:40:451494 internal::RootView* root_view) {
[email protected]20838602011-02-09 04:50:211495 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331496 child_at(i)->PropagateNativeViewHierarchyChanged(attached,
[email protected]2df3cc92011-02-05 00:38:591497 native_view,
1498 root_view);
1499 NativeViewHierarchyChanged(attached, native_view, root_view);
1500}
1501
1502void View::ViewHierarchyChangedImpl(bool register_accelerators,
1503 bool is_add,
1504 View* parent,
1505 View* child) {
1506 if (register_accelerators) {
1507 if (is_add) {
1508 // If you get this registration, you are part of a subtree that has been
1509 // added to the view hierarchy.
1510 if (GetFocusManager()) {
1511 RegisterPendingAccelerators();
1512 } else {
1513 // Delay accelerator registration until visible as we do not have
1514 // focus manager until then.
1515 accelerator_registration_delayed_ = true;
1516 }
1517 } else {
1518 if (child == this)
1519 UnregisterAccelerators(true);
1520 }
1521 }
1522
[email protected]06061b492011-10-03 21:48:491523 if (is_add && layer() && !layer()->parent()) {
[email protected]18dab372011-10-03 21:21:441524 UpdateParentLayer();
[email protected]06061b492011-10-03 21:48:491525 } else if (!is_add && child == this) {
1526 // Make sure the layers beloning to the subtree rooted at |child| get
1527 // removed from layers that do not belong in the same subtree.
1528 OrphanLayers();
1529 }
[email protected]18dab372011-10-03 21:21:441530
[email protected]2df3cc92011-02-05 00:38:591531 ViewHierarchyChanged(is_add, parent, child);
1532 parent->needs_layout_ = true;
1533}
1534
[email protected]2df3cc92011-02-05 00:38:591535// Size and disposition --------------------------------------------------------
1536
1537void View::PropagateVisibilityNotifications(View* start, bool is_visible) {
[email protected]20838602011-02-09 04:50:211538 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331539 child_at(i)->PropagateVisibilityNotifications(start, is_visible);
[email protected]2df3cc92011-02-05 00:38:591540 VisibilityChangedImpl(start, is_visible);
1541}
1542
1543void View::VisibilityChangedImpl(View* starting_from, bool is_visible) {
1544 if (is_visible)
1545 RegisterPendingAccelerators();
1546 else
1547 UnregisterAccelerators(true);
1548 VisibilityChanged(starting_from, is_visible);
1549}
1550
[email protected]8eb52a9a2011-03-09 16:52:001551void View::BoundsChanged(const gfx::Rect& previous_bounds) {
[email protected]19bebd02011-04-06 18:27:341552 if (IsVisible()) {
[email protected]adc93fa72011-06-21 19:47:391553 // Paint the new bounds.
1554 SchedulePaintBoundsChanged(
1555 bounds_.size() == previous_bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
1556 SCHEDULE_PAINT_SIZE_CHANGED);
[email protected]06061b492011-10-03 21:48:491557 }
[email protected]adc93fa72011-06-21 19:47:391558
[email protected]06061b492011-10-03 21:48:491559 if (use_acceleration_when_possible) {
1560 if (layer()) {
1561 if (parent_) {
[email protected]e865b362011-07-01 17:09:461562 gfx::Point offset;
[email protected]06061b492011-10-03 21:48:491563 parent_->CalculateOffsetToAncestorWithLayer(&offset, NULL);
1564 offset.Offset(x(), y());
1565 layer_property_setter_->SetBounds(layer(), gfx::Rect(offset, size()));
1566 } else {
1567 layer_property_setter_->SetBounds(layer(), bounds_);
[email protected]ee012852011-06-24 02:46:201568 }
[email protected]06061b492011-10-03 21:48:491569 // TODO(beng): this seems redundant with the SchedulePaint at the top of
1570 // this function. explore collapsing.
1571 if (previous_bounds.size() != bounds_.size() &&
1572 !layer()->layer_updated_externally()) {
1573 // If our bounds have changed then we need to update the complete
1574 // texture.
1575 layer()->SchedulePaint(GetLocalBounds());
1576 }
1577 } else {
1578 // If our bounds have changed, then any descendant layer bounds may
1579 // have changed. Update them accordingly.
1580 gfx::Point offset;
1581 CalculateOffsetToAncestorWithLayer(&offset, NULL);
1582 UpdateChildLayerBounds(offset);
[email protected]19bebd02011-04-06 18:27:341583 }
[email protected]8eb52a9a2011-03-09 16:52:001584 }
1585
1586 OnBoundsChanged(previous_bounds);
1587
1588 if (previous_bounds.size() != size()) {
1589 needs_layout_ = false;
1590 Layout();
1591 }
[email protected]6260f9e2011-02-16 21:28:011592
[email protected]18dab372011-10-03 21:21:441593 if (NeedsNotificationWhenVisibleBoundsChange())
[email protected]44e9d9c2011-04-14 21:04:061594 OnVisibleBoundsChanged();
[email protected]44e9d9c2011-04-14 21:04:061595
[email protected]6260f9e2011-02-16 21:28:011596 // Notify interested Views that visible bounds within the root view may have
1597 // changed.
1598 if (descendants_to_notify_.get()) {
[email protected]afed4572011-06-09 18:00:111599 for (Views::iterator i(descendants_to_notify_->begin());
[email protected]6260f9e2011-02-16 21:28:011600 i != descendants_to_notify_->end(); ++i) {
1601 (*i)->OnVisibleBoundsChanged();
1602 }
1603 }
[email protected]2df3cc92011-02-05 00:38:591604}
1605
1606// static
[email protected]6260f9e2011-02-16 21:28:011607void View::RegisterChildrenForVisibleBoundsNotification(View* view) {
1608 if (view->NeedsNotificationWhenVisibleBoundsChange())
1609 view->RegisterForVisibleBoundsNotification();
[email protected]20838602011-02-09 04:50:211610 for (int i = 0; i < view->child_count(); ++i)
[email protected]fe84b912011-07-15 17:13:331611 RegisterChildrenForVisibleBoundsNotification(view->child_at(i));
[email protected]6260f9e2011-02-16 21:28:011612}
1613
1614// static
1615void View::UnregisterChildrenForVisibleBoundsNotification(View* view) {
1616 if (view->NeedsNotificationWhenVisibleBoundsChange())
1617 view->UnregisterForVisibleBoundsNotification();
1618 for (int i = 0; i < view->child_count(); ++i)
[email protected]fe84b912011-07-15 17:13:331619 UnregisterChildrenForVisibleBoundsNotification(view->child_at(i));
[email protected]6260f9e2011-02-16 21:28:011620}
1621
1622void View::RegisterForVisibleBoundsNotification() {
1623 if (registered_for_visible_bounds_notification_)
1624 return;
[email protected]44e9d9c2011-04-14 21:04:061625
[email protected]6260f9e2011-02-16 21:28:011626 registered_for_visible_bounds_notification_ = true;
[email protected]2fa942b2011-06-14 15:59:551627 for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
[email protected]6260f9e2011-02-16 21:28:011628 ancestor->AddDescendantToNotify(this);
[email protected]6260f9e2011-02-16 21:28:011629}
1630
1631void View::UnregisterForVisibleBoundsNotification() {
1632 if (!registered_for_visible_bounds_notification_)
1633 return;
[email protected]2fa942b2011-06-14 15:59:551634
[email protected]6260f9e2011-02-16 21:28:011635 registered_for_visible_bounds_notification_ = false;
[email protected]2fa942b2011-06-14 15:59:551636 for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
[email protected]6260f9e2011-02-16 21:28:011637 ancestor->RemoveDescendantToNotify(this);
[email protected]2df3cc92011-02-05 00:38:591638}
1639
1640void View::AddDescendantToNotify(View* view) {
1641 DCHECK(view);
1642 if (!descendants_to_notify_.get())
[email protected]695e942e2011-06-06 15:15:511643 descendants_to_notify_.reset(new Views);
[email protected]2df3cc92011-02-05 00:38:591644 descendants_to_notify_->push_back(view);
1645}
1646
1647void View::RemoveDescendantToNotify(View* view) {
1648 DCHECK(view && descendants_to_notify_.get());
[email protected]afed4572011-06-09 18:00:111649 Views::iterator i(std::find(
1650 descendants_to_notify_->begin(), descendants_to_notify_->end(), view));
[email protected]2df3cc92011-02-05 00:38:591651 DCHECK(i != descendants_to_notify_->end());
1652 descendants_to_notify_->erase(i);
1653 if (descendants_to_notify_->empty())
1654 descendants_to_notify_.reset();
1655}
1656
[email protected]eb93b619b2011-02-15 22:53:191657// Transformations -------------------------------------------------------------
1658
[email protected]b9b1e7a42011-05-17 15:29:511659bool View::GetTransformRelativeTo(const View* ancestor,
1660 ui::Transform* transform) const {
[email protected]6bb8b6c2011-05-19 22:09:051661 const View* p = this;
1662
1663 while (p && p != ancestor) {
[email protected]18dab372011-10-03 21:21:441664 transform->ConcatTransform(p->GetTransform());
[email protected]6bb8b6c2011-05-19 22:09:051665 transform->ConcatTranslate(static_cast<float>(p->GetMirroredX()),
1666 static_cast<float>(p->y()));
1667
1668 p = p->parent_;
[email protected]c36b0bf2011-05-10 18:26:251669 }
[email protected]6bb8b6c2011-05-19 22:09:051670
1671 return p == ancestor;
[email protected]c36b0bf2011-05-10 18:26:251672}
1673
[email protected]2df3cc92011-02-05 00:38:591674// Coordinate conversion -------------------------------------------------------
1675
1676// static
1677void View::ConvertPointToView(const View* src,
1678 const View* dst,
1679 gfx::Point* point,
1680 bool try_other_direction) {
1681 // src can be NULL
1682 DCHECK(dst);
1683 DCHECK(point);
1684
[email protected]86477142011-08-19 19:06:151685 const Widget* src_widget = src ? src->GetWidget() : NULL ;
1686 const Widget* dst_widget = dst->GetWidget();
1687 // If dest and src aren't in the same widget, try to convert the
1688 // point to the destination widget's coordinates first.
1689 // TODO(oshima|sadrul): Cleanup and consolidate conversion methods.
1690 if (Widget::IsPureViews() && src_widget && src_widget != dst_widget) {
1691 // convert to src_widget first.
1692 gfx::Point p = *point;
1693 src->ConvertPointForAncestor(src_widget->GetRootView(), &p);
1694 if (dst_widget->ConvertPointFromAncestor(src_widget, &p)) {
1695 // Convertion to destination widget's coordinates was successful.
1696 // Use destination's root as a source to convert the point further.
1697 src = dst_widget->GetRootView();
1698 *point = p;
1699 }
1700 }
1701
[email protected]36df22b2011-02-24 21:47:561702 if (src == NULL || src->Contains(dst)) {
1703 dst->ConvertPointFromAncestor(src, point);
1704 if (!src) {
[email protected]86477142011-08-19 19:06:151705 if (dst_widget) {
1706 gfx::Rect b = dst_widget->GetClientAreaScreenBounds();
[email protected]2df3cc92011-02-05 00:38:591707 point->SetPoint(point->x() - b.x(), point->y() - b.y());
1708 }
1709 }
[email protected]36df22b2011-02-24 21:47:561710 } else if (src && try_other_direction) {
1711 if (!src->ConvertPointForAncestor(dst, point)) {
1712 // |src| is not an ancestor of |dst|, and |dst| is not an ancestor of
1713 // |src| either. At this stage, |point| is in the widget's coordinate
[email protected]c069fb032011-03-09 02:26:541714 // system. So convert from the widget's to |dst|'s coordinate system now.
[email protected]36df22b2011-02-24 21:47:561715 ConvertPointFromWidget(dst, point);
1716 }
[email protected]2df3cc92011-02-05 00:38:591717 }
1718}
1719
[email protected]2c831b22011-03-14 23:17:201720bool View::ConvertPointForAncestor(const View* ancestor,
1721 gfx::Point* point) const {
[email protected]b9b1e7a42011-05-17 15:29:511722 ui::Transform trans;
[email protected]2c831b22011-03-14 23:17:201723 // TODO(sad): Have some way of caching the transformation results.
[email protected]b9b1e7a42011-05-17 15:29:511724 bool result = GetTransformRelativeTo(ancestor, &trans);
[email protected]80248e32011-07-08 15:31:111725 gfx::Point3f p(*point);
1726 trans.TransformPoint(p);
1727 *point = p.AsPoint();
[email protected]b9b1e7a42011-05-17 15:29:511728 return result;
[email protected]2c831b22011-03-14 23:17:201729}
1730
1731bool View::ConvertPointFromAncestor(const View* ancestor,
1732 gfx::Point* point) const {
[email protected]b9b1e7a42011-05-17 15:29:511733 ui::Transform trans;
1734 bool result = GetTransformRelativeTo(ancestor, &trans);
[email protected]80248e32011-07-08 15:31:111735 gfx::Point3f p(*point);
1736 trans.TransformPointReverse(p);
1737 *point = p.AsPoint();
[email protected]b9b1e7a42011-05-17 15:29:511738 return result;
[email protected]2c831b22011-03-14 23:17:201739}
1740
[email protected]c36b0bf2011-05-10 18:26:251741// Accelerated painting --------------------------------------------------------
1742
[email protected]adc93fa72011-06-21 19:47:391743void View::CreateLayer() {
[email protected]18dab372011-10-03 21:21:441744 layer_.reset(new ui::Layer(NULL));
1745 layer_->set_delegate(this);
1746 if (layer_property_setter_.get())
1747 layer_property_setter_->Installed(layer());
1748 else
1749 SetLayerPropertySetter(NULL);
[email protected]06061b492011-10-03 21:48:491750 layer_->SetVisible(IsVisible());
[email protected]adc93fa72011-06-21 19:47:391751
[email protected]18dab372011-10-03 21:21:441752 UpdateParentLayers();
[email protected]18da0fd2011-10-07 16:25:311753
1754 // The new layer needs to be ordered in the layer tree according
1755 // to the view tree. Children of this layer were added in order
1756 // in UpdateParentLayers().
1757 if (parent())
1758 parent()->ReorderLayers();
[email protected]adc93fa72011-06-21 19:47:391759}
1760
[email protected]18dab372011-10-03 21:21:441761void View::UpdateParentLayers() {
1762 // Attach all top-level un-parented layers.
1763 if (layer() && !layer()->parent()) {
1764 UpdateParentLayer();
1765 } else {
1766 for (int i = 0, count = child_count(); i < count; ++i)
1767 child_at(i)->UpdateParentLayers();
1768 }
1769}
1770
1771void View::UpdateParentLayer() {
[email protected]40500ba2011-09-30 23:39:511772 if (!layer())
1773 return;
1774
[email protected]18dab372011-10-03 21:21:441775 ui::Layer* parent_layer = NULL;
1776 gfx::Point offset(x(), y());
[email protected]06061b492011-10-03 21:48:491777
1778 // TODO(sad): The NULL check here for parent_ essentially is to check if this
1779 // is the RootView. Instead of doing this, this function should be made
1780 // virtual and overridden from the RootView.
[email protected]18dab372011-10-03 21:21:441781 if (parent_)
1782 parent_->CalculateOffsetToAncestorWithLayer(&offset, &parent_layer);
[email protected]06061b492011-10-03 21:48:491783 else if (!parent_ && GetWidget())
1784 GetWidget()->CalculateOffsetToAncestorWithLayer(&offset, &parent_layer);
[email protected]7c4b1b92011-06-27 16:37:181785
[email protected]18dab372011-10-03 21:21:441786 ReparentLayer(offset, parent_layer);
1787}
[email protected]6fb3c272011-09-29 20:36:291788
[email protected]06061b492011-10-03 21:48:491789void View::OrphanLayers() {
1790 if (layer()) {
1791 if (layer()->parent())
1792 layer()->parent()->Remove(layer());
1793
1794 // The layer belonging to this View has already been orphaned. It is not
1795 // necessary to orphan the child layers.
1796 return;
1797 }
1798 for (int i = 0, count = child_count(); i < count; ++i)
1799 child_at(i)->OrphanLayers();
1800}
1801
[email protected]18dab372011-10-03 21:21:441802void View::ReparentLayer(const gfx::Point& offset, ui::Layer* parent_layer) {
1803 layer_->SetBounds(gfx::Rect(offset.x(), offset.y(), width(), height()));
1804 DCHECK_NE(layer(), parent_layer);
1805 if (parent_layer)
1806 parent_layer->Add(layer());
1807 layer_->SchedulePaint(GetLocalBounds());
1808 MoveLayerToParent(layer(), gfx::Point());
[email protected]40500ba2011-09-30 23:39:511809}
[email protected]6fb3c272011-09-29 20:36:291810
[email protected]40500ba2011-09-30 23:39:511811void View::DestroyLayer() {
[email protected]18dab372011-10-03 21:21:441812 ui::Layer* new_parent = layer()->parent();
1813 std::vector<ui::Layer*> children = layer()->children();
1814 for (size_t i = 0; i < children.size(); ++i) {
1815 layer()->Remove(children[i]);
1816 if (new_parent)
1817 new_parent->Add(children[i]);
1818 }
[email protected]40500ba2011-09-30 23:39:511819
[email protected]18dab372011-10-03 21:21:441820 if (layer_property_setter_.get())
1821 layer_property_setter_->Uninstalled(layer());
1822
1823 layer_.reset();
1824
[email protected]18da0fd2011-10-07 16:25:311825 if (new_parent)
1826 ReorderLayers();
1827
[email protected]18dab372011-10-03 21:21:441828 gfx::Point offset;
1829 CalculateOffsetToAncestorWithLayer(&offset, NULL);
1830 UpdateChildLayerBounds(offset);
1831
1832 SchedulePaint();
[email protected]adc93fa72011-06-21 19:47:391833}
1834
[email protected]2df3cc92011-02-05 00:38:591835// Input -----------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:291836
[email protected]2c831b22011-03-14 23:17:201837bool View::ProcessMousePressed(const MouseEvent& event, DragInfo* drag_info) {
[email protected]83548a42010-06-18 13:53:371838 const bool enabled = IsEnabled();
[email protected]e9adf0702010-03-08 23:34:071839 int drag_operations =
[email protected]2c831b22011-03-14 23:17:201840 (enabled && event.IsOnlyLeftMouseButton() && HitTest(event.location())) ?
1841 GetDragOperations(event.location()) : 0;
1842 ContextMenuController* context_menu_controller = event.IsRightMouseButton() ?
[email protected]e9adf0702010-03-08 23:34:071843 context_menu_controller_ : 0;
initial.commit09911bf2008-07-26 23:55:291844
[email protected]2c831b22011-03-14 23:17:201845 const bool result = OnMousePressed(event);
[email protected]c069fb032011-03-09 02:26:541846 // WARNING: we may have been deleted, don't use any View variables.
initial.commit09911bf2008-07-26 23:55:291847
1848 if (!enabled)
1849 return result;
1850
[email protected]02bae0f2011-01-19 20:22:001851 if (drag_operations != ui::DragDropTypes::DRAG_NONE) {
[email protected]2c831b22011-03-14 23:17:201852 drag_info->PossibleDrag(event.location());
initial.commit09911bf2008-07-26 23:55:291853 return true;
1854 }
1855 return !!context_menu_controller || result;
1856}
1857
[email protected]2c831b22011-03-14 23:17:201858bool View::ProcessMouseDragged(const MouseEvent& event, DragInfo* drag_info) {
initial.commit09911bf2008-07-26 23:55:291859 // Copy the field, that way if we're deleted after drag and drop no harm is
1860 // done.
1861 ContextMenuController* context_menu_controller = context_menu_controller_;
1862 const bool possible_drag = drag_info->possible_drag;
[email protected]2c831b22011-03-14 23:17:201863 if (possible_drag && ExceededDragThreshold(
1864 drag_info->start_pt.x() - event.x(),
1865 drag_info->start_pt.y() - event.y())) {
[email protected]b5f94de2009-12-04 07:59:001866 if (!drag_controller_ ||
[email protected]c3fac082011-03-04 01:04:181867 drag_controller_->CanStartDragForView(
[email protected]2c831b22011-03-14 23:17:201868 this, drag_info->start_pt, event.location()))
1869 DoDrag(event, drag_info->start_pt);
initial.commit09911bf2008-07-26 23:55:291870 } else {
[email protected]2c831b22011-03-14 23:17:201871 if (OnMouseDragged(event))
initial.commit09911bf2008-07-26 23:55:291872 return true;
1873 // Fall through to return value based on context menu controller.
1874 }
1875 // WARNING: we may have been deleted.
1876 return (context_menu_controller != NULL) || possible_drag;
1877}
1878
[email protected]34338252011-03-29 00:39:051879void View::ProcessMouseReleased(const MouseEvent& event) {
1880 if (context_menu_controller_ && event.IsOnlyRightMouseButton()) {
initial.commit09911bf2008-07-26 23:55:291881 // Assume that if there is a context menu controller we won't be deleted
1882 // from mouse released.
[email protected]2c831b22011-03-14 23:17:201883 gfx::Point location(event.location());
[email protected]34338252011-03-29 00:39:051884 OnMouseReleased(event);
[email protected]464fdb32009-03-19 20:25:441885 if (HitTest(location)) {
1886 ConvertPointToScreen(this, &location);
[email protected]e9adf0702010-03-08 23:34:071887 ShowContextMenu(location, true);
[email protected]464fdb32009-03-19 20:25:441888 }
initial.commit09911bf2008-07-26 23:55:291889 } else {
[email protected]34338252011-03-29 00:39:051890 OnMouseReleased(event);
initial.commit09911bf2008-07-26 23:55:291891 }
1892 // WARNING: we may have been deleted.
1893}
1894
[email protected]6a4056b2011-06-14 22:09:171895ui::TouchStatus View::ProcessTouchEvent(const TouchEvent& event) {
[email protected]18dab372011-10-03 21:21:441896 // TODO(rjkroege): Implement a grab scheme similar to as as is found in
1897 // MousePressed.
[email protected]2c831b22011-03-14 23:17:201898 return OnTouchEvent(event);
[email protected]ad84c8f2010-09-08 14:06:101899}
[email protected]ad84c8f2010-09-08 14:06:101900
[email protected]2df3cc92011-02-05 00:38:591901// Accelerators ----------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:291902
[email protected]71421c3f2009-06-06 00:41:441903void View::RegisterPendingAccelerators() {
1904 if (!accelerators_.get() ||
1905 registered_accelerator_count_ == accelerators_->size()) {
1906 // No accelerators are waiting for registration.
initial.commit09911bf2008-07-26 23:55:291907 return;
[email protected]71421c3f2009-06-06 00:41:441908 }
initial.commit09911bf2008-07-26 23:55:291909
[email protected]72f17242011-02-18 21:05:551910 if (!GetWidget()) {
1911 // The view is not yet attached to a widget, defer registration until then.
initial.commit09911bf2008-07-26 23:55:291912 return;
1913 }
[email protected]f3735c5d2009-03-19 17:26:231914
[email protected]bda9556c2010-01-07 00:55:161915 accelerator_focus_manager_ = GetFocusManager();
1916 if (!accelerator_focus_manager_) {
initial.commit09911bf2008-07-26 23:55:291917 // Some crash reports seem to show that we may get cases where we have no
1918 // focus manager (see bug #1291225). This should never be the case, just
1919 // making sure we don't crash.
[email protected]f8dce002009-09-10 21:07:041920
[email protected]2fc66722011-05-19 14:43:121921 // TODO(jcampan): This fails for a view under NativeWidgetGtk with
1922 // TYPE_CHILD. (see http://crbug.com/21335) reenable
1923 // NOTREACHED assertion and verify accelerators works as
1924 // expected.
[email protected]f8dce002009-09-10 21:07:041925#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291926 NOTREACHED();
[email protected]f8dce002009-09-10 21:07:041927#endif
initial.commit09911bf2008-07-26 23:55:291928 return;
1929 }
[email protected]37e6e612011-02-02 18:13:071930 // Only register accelerators if we are visible.
1931 if (!IsVisibleInRootView())
1932 return;
[email protected]afed4572011-06-09 18:00:111933 for (std::vector<Accelerator>::const_iterator i(
1934 accelerators_->begin() + registered_accelerator_count_);
1935 i != accelerators_->end(); ++i) {
1936 accelerator_focus_manager_->RegisterAccelerator(*i, this);
initial.commit09911bf2008-07-26 23:55:291937 }
[email protected]71421c3f2009-06-06 00:41:441938 registered_accelerator_count_ = accelerators_->size();
initial.commit09911bf2008-07-26 23:55:291939}
1940
[email protected]bda9556c2010-01-07 00:55:161941void View::UnregisterAccelerators(bool leave_data_intact) {
initial.commit09911bf2008-07-26 23:55:291942 if (!accelerators_.get())
1943 return;
1944
[email protected]72f17242011-02-18 21:05:551945 if (GetWidget()) {
[email protected]bda9556c2010-01-07 00:55:161946 if (accelerator_focus_manager_) {
initial.commit09911bf2008-07-26 23:55:291947 // We may not have a FocusManager if the window containing us is being
1948 // closed, in which case the FocusManager is being deleted so there is
1949 // nothing to unregister.
[email protected]bda9556c2010-01-07 00:55:161950 accelerator_focus_manager_->UnregisterAccelerators(this);
1951 accelerator_focus_manager_ = NULL;
initial.commit09911bf2008-07-26 23:55:291952 }
[email protected]bda9556c2010-01-07 00:55:161953 if (!leave_data_intact) {
1954 accelerators_->clear();
1955 accelerators_.reset();
1956 }
[email protected]71421c3f2009-06-06 00:41:441957 registered_accelerator_count_ = 0;
initial.commit09911bf2008-07-26 23:55:291958 }
1959}
1960
[email protected]2df3cc92011-02-05 00:38:591961// Focus -----------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:291962
[email protected]2df3cc92011-02-05 00:38:591963void View::InitFocusSiblings(View* v, int index) {
[email protected]39a0f0c62011-06-09 16:27:181964 int count = child_count();
initial.commit09911bf2008-07-26 23:55:291965
[email protected]39a0f0c62011-06-09 16:27:181966 if (count == 0) {
[email protected]2df3cc92011-02-05 00:38:591967 v->next_focusable_view_ = NULL;
1968 v->previous_focusable_view_ = NULL;
initial.commit09911bf2008-07-26 23:55:291969 } else {
[email protected]39a0f0c62011-06-09 16:27:181970 if (index == count) {
[email protected]2df3cc92011-02-05 00:38:591971 // We are inserting at the end, but the end of the child list may not be
1972 // the last focusable element. Let's try to find an element with no next
1973 // focusable element to link to.
1974 View* last_focusable_view = NULL;
[email protected]695e942e2011-06-06 15:15:511975 for (Views::iterator i(children_.begin()); i != children_.end(); ++i) {
1976 if (!(*i)->next_focusable_view_) {
1977 last_focusable_view = *i;
[email protected]2df3cc92011-02-05 00:38:591978 break;
1979 }
initial.commit09911bf2008-07-26 23:55:291980 }
[email protected]2df3cc92011-02-05 00:38:591981 if (last_focusable_view == NULL) {
1982 // Hum... there is a cycle in the focus list. Let's just insert ourself
1983 // after the last child.
[email protected]20838602011-02-09 04:50:211984 View* prev = children_[index - 1];
[email protected]2df3cc92011-02-05 00:38:591985 v->previous_focusable_view_ = prev;
1986 v->next_focusable_view_ = prev->next_focusable_view_;
1987 prev->next_focusable_view_->previous_focusable_view_ = v;
1988 prev->next_focusable_view_ = v;
1989 } else {
1990 last_focusable_view->next_focusable_view_ = v;
1991 v->next_focusable_view_ = NULL;
1992 v->previous_focusable_view_ = last_focusable_view;
1993 }
1994 } else {
[email protected]20838602011-02-09 04:50:211995 View* prev = children_[index]->GetPreviousFocusableView();
[email protected]2df3cc92011-02-05 00:38:591996 v->previous_focusable_view_ = prev;
[email protected]20838602011-02-09 04:50:211997 v->next_focusable_view_ = children_[index];
[email protected]2df3cc92011-02-05 00:38:591998 if (prev)
1999 prev->next_focusable_view_ = v;
[email protected]20838602011-02-09 04:50:212000 children_[index]->previous_focusable_view_ = v;
initial.commit09911bf2008-07-26 23:55:292001 }
2002 }
2003}
2004
[email protected]2df3cc92011-02-05 00:38:592005// System events ---------------------------------------------------------------
[email protected]96b667d2008-10-14 20:58:442006
[email protected]2df3cc92011-02-05 00:38:592007void View::PropagateThemeChanged() {
[email protected]20838602011-02-09 04:50:212008 for (int i = child_count() - 1; i >= 0; --i)
[email protected]fe84b912011-07-15 17:13:332009 child_at(i)->PropagateThemeChanged();
[email protected]2df3cc92011-02-05 00:38:592010 OnThemeChanged();
initial.commit09911bf2008-07-26 23:55:292011}
2012
[email protected]2df3cc92011-02-05 00:38:592013void View::PropagateLocaleChanged() {
[email protected]20838602011-02-09 04:50:212014 for (int i = child_count() - 1; i >= 0; --i)
[email protected]fe84b912011-07-15 17:13:332015 child_at(i)->PropagateLocaleChanged();
[email protected]2df3cc92011-02-05 00:38:592016 OnLocaleChanged();
initial.commit09911bf2008-07-26 23:55:292017}
2018
[email protected]2df3cc92011-02-05 00:38:592019// Tooltips --------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:292020
2021void View::UpdateTooltip() {
[email protected]a0dde122008-11-21 20:51:202022 Widget* widget = GetWidget();
[email protected]9861ae92011-03-03 20:15:032023 // TODO(beng): The TooltipManager NULL check can be removed when we
2024 // consolidate Init() methods and make views_unittests Init() all
2025 // Widgets that it uses.
[email protected]2f2b57b2011-06-16 17:21:232026 if (widget && widget->native_widget_private()->GetTooltipManager())
2027 widget->native_widget_private()->GetTooltipManager()->UpdateTooltip();
initial.commit09911bf2008-07-26 23:55:292028}
2029
[email protected]2df3cc92011-02-05 00:38:592030// Drag and drop ---------------------------------------------------------------
2031
[email protected]2c831b22011-03-14 23:17:202032void View::DoDrag(const MouseEvent& event, const gfx::Point& press_pt) {
[email protected]2df3cc92011-02-05 00:38:592033 int drag_operations = GetDragOperations(press_pt);
2034 if (drag_operations == ui::DragDropTypes::DRAG_NONE)
2035 return;
2036
2037 OSExchangeData data;
2038 WriteDragData(press_pt, &data);
2039
2040 // Message the RootView to do the drag and drop. That way if we're removed
2041 // the RootView can detect it and avoid calling us back.
[email protected]9861ae92011-03-03 20:15:032042 GetWidget()->RunShellDrag(this, data, drag_operations);
initial.commit09911bf2008-07-26 23:55:292043}
2044
[email protected]2df3cc92011-02-05 00:38:592045} // namespace views