blob: 0a6fdd1d3c1c165d715fc3890b6097dd96b3b908 [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]25ae9a12011-10-12 14:55:22187
188 // Make sure the visibility of the child layers are correct.
189 // If any of the parent View is hidden, then the layers of the subtree
190 // rooted at |this| should be hidden. Otherwise, all the child layers should
191 // inherit the visibility of the owner View.
192 UpdateLayerVisibility();
[email protected]20838602011-02-09 04:50:21193}
194
[email protected]ea1fc4b12011-06-16 17:49:04195void View::ReorderChildView(View* view, int index) {
196 DCHECK_EQ(view->parent_, this);
197 if (index < 0)
198 index = child_count() - 1;
199 else if (index >= child_count())
200 return;
201 if (children_[index] == view)
202 return;
203
204 const Views::iterator i(std::find(children_.begin(), children_.end(), view));
205 DCHECK(i != children_.end());
206 children_.erase(i);
207
208 // Unlink the view first
209 View* next_focusable = view->next_focusable_view_;
210 View* prev_focusable = view->previous_focusable_view_;
211 if (prev_focusable)
212 prev_focusable->next_focusable_view_ = next_focusable;
213 if (next_focusable)
214 next_focusable->previous_focusable_view_ = prev_focusable;
215
216 // Add it in the specified index now.
217 InitFocusSiblings(view, index);
218 children_.insert(children_.begin() + index, view);
[email protected]18da0fd2011-10-07 16:25:31219
220 if (use_acceleration_when_possible)
221 ReorderLayers();
[email protected]ea1fc4b12011-06-16 17:49:04222}
223
[email protected]20838602011-02-09 04:50:21224void View::RemoveChildView(View* view) {
225 DoRemoveChildView(view, true, true, false);
226}
227
[email protected]77a08752011-06-03 00:00:08228void View::RemoveAllChildViews(bool delete_children) {
[email protected]f27ddd72011-06-09 23:13:26229 while (!children_.empty())
230 DoRemoveChildView(children_.front(), false, false, delete_children);
[email protected]20838602011-02-09 04:50:21231 UpdateTooltip();
232}
233
[email protected]20838602011-02-09 04:50:21234bool View::Contains(const View* view) const {
[email protected]280cddd2011-05-17 01:00:46235 for (const View* v = view; v; v = v->parent_) {
236 if (v == this)
[email protected]20838602011-02-09 04:50:21237 return true;
[email protected]20838602011-02-09 04:50:21238 }
239 return false;
240}
241
242int View::GetIndexOf(const View* view) const {
[email protected]695e942e2011-06-06 15:15:51243 Views::const_iterator i(std::find(children_.begin(), children_.end(), view));
[email protected]26b2cb722011-06-07 16:35:09244 return i != children_.end() ? static_cast<int>(i - children_.begin()) : -1;
[email protected]20838602011-02-09 04:50:21245}
246
[email protected]2df3cc92011-02-05 00:38:59247// Size and disposition --------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29248
[email protected]97a31142011-02-08 00:09:16249void View::SetBounds(int x, int y, int width, int height) {
250 SetBoundsRect(gfx::Rect(x, y, std::max(0, width), std::max(0, height)));
initial.commit09911bf2008-07-26 23:55:29251}
252
[email protected]97a31142011-02-08 00:09:16253void View::SetBoundsRect(const gfx::Rect& bounds) {
[email protected]de8d613d2010-10-25 22:30:10254 if (bounds == bounds_) {
255 if (needs_layout_) {
256 needs_layout_ = false;
257 Layout();
[email protected]8eb52a9a2011-03-09 16:52:00258 SchedulePaint();
[email protected]de8d613d2010-10-25 22:30:10259 }
initial.commit09911bf2008-07-26 23:55:29260 return;
[email protected]de8d613d2010-10-25 22:30:10261 }
initial.commit09911bf2008-07-26 23:55:29262
[email protected]adc93fa72011-06-21 19:47:39263 if (IsVisible()) {
264 // Paint where the view is currently.
265 SchedulePaintBoundsChanged(
266 bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
267 SCHEDULE_PAINT_SIZE_CHANGED);
268 }
269
[email protected]80f8b9f2008-10-16 18:17:47270 gfx::Rect prev = bounds_;
initial.commit09911bf2008-07-26 23:55:29271 bounds_ = bounds;
[email protected]8eb52a9a2011-03-09 16:52:00272 BoundsChanged(prev);
initial.commit09911bf2008-07-26 23:55:29273}
274
[email protected]97a31142011-02-08 00:09:16275void View::SetSize(const gfx::Size& size) {
276 SetBounds(x(), y(), size.width(), size.height());
[email protected]2df3cc92011-02-05 00:38:59277}
278
[email protected]97a31142011-02-08 00:09:16279void View::SetPosition(const gfx::Point& position) {
280 SetBounds(position.x(), position.y(), width(), height());
281}
initial.commit09911bf2008-07-26 23:55:29282
[email protected]97a31142011-02-08 00:09:16283void View::SetX(int x) {
284 SetBounds(x, y(), width(), height());
285}
286
287void View::SetY(int y) {
288 SetBounds(x(), y, width(), height());
289}
290
[email protected]97a31142011-02-08 00:09:16291gfx::Rect View::GetContentsBounds() const {
292 gfx::Rect contents_bounds(GetLocalBounds());
293 if (border_.get()) {
294 gfx::Insets insets;
295 border_->GetInsets(&insets);
296 contents_bounds.Inset(insets);
297 }
298 return contents_bounds;
299}
300
301gfx::Rect View::GetLocalBounds() const {
[email protected]27488c22011-10-25 02:45:21302 return gfx::Rect(gfx::Point(), size());
initial.commit09911bf2008-07-26 23:55:29303}
304
[email protected]2df3cc92011-02-05 00:38:59305gfx::Insets View::GetInsets() const {
306 gfx::Insets insets;
307 if (border_.get())
308 border_->GetInsets(&insets);
309 return insets;
310}
311
[email protected]4d352522011-05-31 16:24:27312gfx::Rect View::GetVisibleBounds() const {
[email protected]2df3cc92011-02-05 00:38:59313 if (!IsVisibleInRootView())
314 return gfx::Rect();
315 gfx::Rect vis_bounds(0, 0, width(), height());
316 gfx::Rect ancestor_bounds;
[email protected]4d352522011-05-31 16:24:27317 const View* view = this;
[email protected]277c7b72011-06-06 15:23:09318 ui::Transform transform;
319
[email protected]2df3cc92011-02-05 00:38:59320 while (view != NULL && !vis_bounds.IsEmpty()) {
[email protected]18dab372011-10-03 21:21:44321 transform.ConcatTransform(view->GetTransform());
[email protected]277c7b72011-06-06 15:23:09322 transform.ConcatTranslate(static_cast<float>(view->GetMirroredX()),
323 static_cast<float>(view->y()));
324
325 vis_bounds = view->ConvertRectToParent(vis_bounds);
[email protected]2fa942b2011-06-14 15:59:55326 const View* ancestor = view->parent_;
[email protected]2df3cc92011-02-05 00:38:59327 if (ancestor != NULL) {
[email protected]4d352522011-05-31 16:24:27328 ancestor_bounds.SetRect(0, 0, ancestor->width(), ancestor->height());
[email protected]2df3cc92011-02-05 00:38:59329 vis_bounds = vis_bounds.Intersect(ancestor_bounds);
330 } else if (!view->GetWidget()) {
331 // If the view has no Widget, we're not visible. Return an empty rect.
332 return gfx::Rect();
333 }
334 view = ancestor;
335 }
336 if (vis_bounds.IsEmpty())
337 return vis_bounds;
338 // Convert back to this views coordinate system.
[email protected]277c7b72011-06-06 15:23:09339 transform.TransformRectReverse(&vis_bounds);
[email protected]2df3cc92011-02-05 00:38:59340 return vis_bounds;
341}
342
343gfx::Rect View::GetScreenBounds() const {
344 gfx::Point origin;
345 View::ConvertPointToScreen(this, &origin);
346 return gfx::Rect(origin, size());
347}
348
[email protected]154f8bc2008-10-15 18:02:30349gfx::Size View::GetPreferredSize() {
350 if (layout_manager_.get())
351 return layout_manager_->GetPreferredSize(this);
352 return gfx::Size();
initial.commit09911bf2008-07-26 23:55:29353}
354
[email protected]754b0082011-05-27 22:53:11355int View::GetBaseline() const {
[email protected]a1360162009-11-30 21:19:07356 return -1;
357}
358
initial.commit09911bf2008-07-26 23:55:29359void View::SizeToPreferredSize() {
[email protected]154f8bc2008-10-15 18:02:30360 gfx::Size prefsize = GetPreferredSize();
361 if ((prefsize.width() != width()) || (prefsize.height() != height()))
362 SetBounds(x(), y(), prefsize.width(), prefsize.height());
initial.commit09911bf2008-07-26 23:55:29363}
364
[email protected]154f8bc2008-10-15 18:02:30365gfx::Size View::GetMinimumSize() {
366 return GetPreferredSize();
initial.commit09911bf2008-07-26 23:55:29367}
368
369int View::GetHeightForWidth(int w) {
370 if (layout_manager_.get())
371 return layout_manager_->GetPreferredHeightForWidth(this, w);
[email protected]154f8bc2008-10-15 18:02:30372 return GetPreferredSize().height();
initial.commit09911bf2008-07-26 23:55:29373}
374
[email protected]6ed82bb82011-05-27 20:24:07375void View::SetVisible(bool visible) {
[email protected]5fe3f3a2011-06-11 00:31:32376 if (visible != visible_) {
[email protected]3aa43942011-09-13 20:59:53377 // If the View is currently visible, schedule paint to refresh parent.
[email protected]18dab372011-10-03 21:21:44378 // TODO(beng): not sure we should be doing this if we have a layer.
[email protected]5fe3f3a2011-06-11 00:31:32379 if (visible_)
[email protected]2df3cc92011-02-05 00:38:59380 SchedulePaint();
initial.commit09911bf2008-07-26 23:55:29381
[email protected]5fe3f3a2011-06-11 00:31:32382 visible_ = visible;
[email protected]1d27aa532011-07-06 15:38:59383
[email protected]20838602011-02-09 04:50:21384 // This notifies all sub-views recursively.
[email protected]5fe3f3a2011-06-11 00:31:32385 PropagateVisibilityNotifications(this, visible_);
[email protected]25ae9a12011-10-12 14:55:22386 UpdateLayerVisibility();
[email protected]2df3cc92011-02-05 00:38:59387
388 // If we are newly visible, schedule paint.
[email protected]5fe3f3a2011-06-11 00:31:32389 if (visible_)
[email protected]2df3cc92011-02-05 00:38:59390 SchedulePaint();
[email protected]e9adf0702010-03-08 23:34:07391 }
initial.commit09911bf2008-07-26 23:55:29392}
393
[email protected]16460642011-03-04 23:15:53394bool View::IsVisible() const {
[email protected]5fe3f3a2011-06-11 00:31:32395 return visible_;
[email protected]16460642011-03-04 23:15:53396}
397
[email protected]2df3cc92011-02-05 00:38:59398bool View::IsVisibleInRootView() const {
[email protected]2fa942b2011-06-14 15:59:55399 return IsVisible() && parent_ ? parent_->IsVisibleInRootView() : false;
[email protected]2df3cc92011-02-05 00:38:59400}
401
[email protected]6ed82bb82011-05-27 20:24:07402void View::SetEnabled(bool enabled) {
403 if (enabled != enabled_) {
404 enabled_ = enabled;
405 OnEnabledChanged();
[email protected]2df3cc92011-02-05 00:38:59406 }
407}
408
409bool View::IsEnabled() const {
410 return enabled_;
411}
412
[email protected]6ed82bb82011-05-27 20:24:07413void View::OnEnabledChanged() {
414 SchedulePaint();
415}
416
[email protected]eb93b619b2011-02-15 22:53:19417// Transformations -------------------------------------------------------------
418
[email protected]36df22b2011-02-24 21:47:56419const ui::Transform& View::GetTransform() const {
[email protected]b9b1e7a42011-05-17 15:29:51420 static const ui::Transform* no_op = new ui::Transform;
[email protected]18dab372011-10-03 21:21:44421 return layer() ? layer()->transform() : *no_op;
[email protected]eb93b619b2011-02-15 22:53:19422}
423
[email protected]b9b1e7a42011-05-17 15:29:51424void View::SetTransform(const ui::Transform& transform) {
425 if (!transform.HasChange()) {
[email protected]18dab372011-10-03 21:21:44426 if (layer()) {
427 layer_property_setter_->SetTransform(layer(), transform);
428 if (!paint_to_layer_)
429 DestroyLayer();
[email protected]40500ba2011-09-30 23:39:51430 } else {
[email protected]18dab372011-10-03 21:21:44431 // Nothing.
[email protected]40500ba2011-09-30 23:39:51432 }
[email protected]18dab372011-10-03 21:21:44433 } else {
434 if (!layer())
435 CreateLayer();
436 layer_property_setter_->SetTransform(layer(), transform);
437 layer()->ScheduleDraw();
[email protected]b9b1e7a42011-05-17 15:29:51438 }
[email protected]eb93b619b2011-02-15 22:53:19439}
440
[email protected]18dab372011-10-03 21:21:44441void View::SetPaintToLayer(bool paint_to_layer) {
442 paint_to_layer_ = paint_to_layer;
443 if (paint_to_layer_ && !layer()) {
[email protected]adc93fa72011-06-21 19:47:39444 CreateLayer();
[email protected]18dab372011-10-03 21:21:44445 } else if (!paint_to_layer_ && layer()) {
446 DestroyLayer();
[email protected]7c4b1b92011-06-27 16:37:18447 }
[email protected]6b8b0f02011-06-02 22:04:02448}
449
[email protected]710a98d2011-06-23 20:13:29450void View::SetLayerPropertySetter(LayerPropertySetter* setter) {
[email protected]18dab372011-10-03 21:21:44451 DCHECK(layer());
452 LayerPropertySetter* old_setter = layer_property_setter_.get();
453 if (!layer() || (old_setter && old_setter == setter))
[email protected]710a98d2011-06-23 20:13:29454 return;
[email protected]18dab372011-10-03 21:21:44455 if (!setter)
456 setter = LayerPropertySetter::CreateDefaultSetter();
[email protected]710a98d2011-06-23 20:13:29457
[email protected]18dab372011-10-03 21:21:44458 if (old_setter)
459 old_setter->Uninstalled(layer());
460 layer_property_setter_.reset(setter);
461 layer_property_setter_->Installed(layer());
[email protected]710a98d2011-06-23 20:13:29462}
463
[email protected]2df3cc92011-02-05 00:38:59464// RTL positioning -------------------------------------------------------------
465
[email protected]9defbad2011-02-08 04:23:18466gfx::Rect View::GetMirroredBounds() const {
[email protected]97a31142011-02-08 00:09:16467 gfx::Rect bounds(bounds_);
[email protected]9defbad2011-02-08 04:23:18468 bounds.set_x(GetMirroredX());
[email protected]97a31142011-02-08 00:09:16469 return bounds;
470}
471
[email protected]9defbad2011-02-08 04:23:18472gfx::Point View::GetMirroredPosition() const {
473 return gfx::Point(GetMirroredX(), y());
[email protected]97a31142011-02-08 00:09:16474}
475
[email protected]9defbad2011-02-08 04:23:18476int View::GetMirroredX() const {
[email protected]2fa942b2011-06-14 15:59:55477 return parent_ ? parent_->GetMirroredXForRect(bounds_) : x();
[email protected]2df3cc92011-02-05 00:38:59478}
479
[email protected]9defbad2011-02-08 04:23:18480int View::GetMirroredXForRect(const gfx::Rect& bounds) const {
[email protected]2df3cc92011-02-05 00:38:59481 return base::i18n::IsRTL() ?
482 (width() - bounds.x() - bounds.width()) : bounds.x();
483}
484
[email protected]9defbad2011-02-08 04:23:18485int View::GetMirroredXInView(int x) const {
486 return base::i18n::IsRTL() ? width() - x : x;
487}
488
489int View::GetMirroredXWithWidthInView(int x, int w) const {
490 return base::i18n::IsRTL() ? width() - x - w : x;
491}
492
[email protected]2df3cc92011-02-05 00:38:59493// Layout ----------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29494
495void View::Layout() {
[email protected]9ea053e2010-07-15 08:19:05496 needs_layout_ = false;
497
[email protected]f8617192010-07-05 07:57:10498 // If we have a layout manager, let it handle the layout for us.
[email protected]8eb52a9a2011-03-09 16:52:00499 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29500 layout_manager_->Layout(this);
initial.commit09911bf2008-07-26 23:55:29501
[email protected]9ea053e2010-07-15 08:19:05502 // Make sure to propagate the Layout() call to any children that haven't
503 // received it yet through the layout manager and need to be laid out. This
504 // is needed for the case when the child requires a layout but its bounds
505 // weren't changed by the layout manager. If there is no layout manager, we
506 // just propagate the Layout() call down the hierarchy, so whoever receives
507 // the call can take appropriate action.
[email protected]20838602011-02-09 04:50:21508 for (int i = 0, count = child_count(); i < count; ++i) {
[email protected]fe84b912011-07-15 17:13:33509 View* child = child_at(i);
[email protected]9ea053e2010-07-15 08:19:05510 if (child->needs_layout_ || !layout_manager_.get()) {
511 child->needs_layout_ = false;
512 child->Layout();
513 }
initial.commit09911bf2008-07-26 23:55:29514 }
515}
516
[email protected]9ea053e2010-07-15 08:19:05517void View::InvalidateLayout() {
[email protected]de8d613d2010-10-25 22:30:10518 // Always invalidate up. This is needed to handle the case of us already being
519 // valid, but not our parent.
[email protected]9ea053e2010-07-15 08:19:05520 needs_layout_ = true;
521 if (parent_)
522 parent_->InvalidateLayout();
523}
524
initial.commit09911bf2008-07-26 23:55:29525LayoutManager* View::GetLayoutManager() const {
526 return layout_manager_.get();
527}
528
529void View::SetLayoutManager(LayoutManager* layout_manager) {
[email protected]09fe9492009-11-07 02:23:06530 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29531 layout_manager_->Uninstalled(this);
[email protected]09fe9492009-11-07 02:23:06532
initial.commit09911bf2008-07-26 23:55:29533 layout_manager_.reset(layout_manager);
[email protected]09fe9492009-11-07 02:23:06534 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29535 layout_manager_->Installed(this);
initial.commit09911bf2008-07-26 23:55:29536}
537
[email protected]2df3cc92011-02-05 00:38:59538// Attributes ------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29539
[email protected]2df3cc92011-02-05 00:38:59540std::string View::GetClassName() const {
541 return kViewClassName;
initial.commit09911bf2008-07-26 23:55:29542}
543
[email protected]2df3cc92011-02-05 00:38:59544View* View::GetAncestorWithClassName(const std::string& name) {
[email protected]2fa942b2011-06-14 15:59:55545 for (View* view = this; view; view = view->parent_) {
[email protected]2df3cc92011-02-05 00:38:59546 if (view->GetClassName() == name)
547 return view;
548 }
549 return NULL;
initial.commit09911bf2008-07-26 23:55:29550}
551
[email protected]20838602011-02-09 04:50:21552const View* View::GetViewByID(int id) const {
[email protected]2df3cc92011-02-05 00:38:59553 if (id == id_)
554 return const_cast<View*>(this);
initial.commit09911bf2008-07-26 23:55:29555
[email protected]20838602011-02-09 04:50:21556 for (int i = 0, count = child_count(); i < count; ++i) {
[email protected]fe84b912011-07-15 17:13:33557 const View* view = child_at(i)->GetViewByID(id);
[email protected]2df3cc92011-02-05 00:38:59558 if (view)
559 return view;
560 }
561 return NULL;
initial.commit09911bf2008-07-26 23:55:29562}
563
[email protected]20838602011-02-09 04:50:21564View* View::GetViewByID(int id) {
565 return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id));
566}
567
[email protected]2df3cc92011-02-05 00:38:59568void View::SetGroup(int gid) {
569 // Don't change the group id once it's set.
570 DCHECK(group_ == -1 || group_ == gid);
571 group_ = gid;
572}
573
574int View::GetGroup() const {
575 return group_;
576}
577
[email protected]16460642011-03-04 23:15:53578bool View::IsGroupFocusTraversable() const {
579 return true;
580}
581
[email protected]48250ad2011-07-13 22:06:28582void View::GetViewsInGroup(int group, Views* views) {
583 if (group_ == group)
584 views->push_back(this);
[email protected]2df3cc92011-02-05 00:38:59585
[email protected]20838602011-02-09 04:50:21586 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:33587 child_at(i)->GetViewsInGroup(group, views);
[email protected]2df3cc92011-02-05 00:38:59588}
589
[email protected]48250ad2011-07-13 22:06:28590View* View::GetSelectedViewForGroup(int group) {
[email protected]695e942e2011-06-06 15:15:51591 Views views;
[email protected]48250ad2011-07-13 22:06:28592 GetWidget()->GetRootView()->GetViewsInGroup(group, &views);
[email protected]6775e40a2011-03-04 21:03:47593 return views.empty() ? NULL : views[0];
[email protected]2df3cc92011-02-05 00:38:59594}
595
596// Coordinate conversion -------------------------------------------------------
597
598// static
599void View::ConvertPointToView(const View* src,
600 const View* dst,
601 gfx::Point* point) {
602 ConvertPointToView(src, dst, point, true);
603}
604
605// static
606void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
607 DCHECK(src);
608 DCHECK(p);
609
[email protected]36df22b2011-02-24 21:47:56610 src->ConvertPointForAncestor(NULL, p);
[email protected]2df3cc92011-02-05 00:38:59611}
612
613// static
614void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) {
[email protected]36df22b2011-02-24 21:47:56615 DCHECK(dest);
616 DCHECK(p);
617
618 dest->ConvertPointFromAncestor(NULL, p);
[email protected]2df3cc92011-02-05 00:38:59619}
620
621// static
622void View::ConvertPointToScreen(const View* src, gfx::Point* p) {
623 DCHECK(src);
624 DCHECK(p);
625
626 // If the view is not connected to a tree, there's nothing we can do.
[email protected]20838602011-02-09 04:50:21627 const Widget* widget = src->GetWidget();
[email protected]2df3cc92011-02-05 00:38:59628 if (widget) {
629 ConvertPointToWidget(src, p);
[email protected]642c224f62011-03-03 17:30:05630 gfx::Rect r = widget->GetClientAreaScreenBounds();
[email protected]2df3cc92011-02-05 00:38:59631 p->SetPoint(p->x() + r.x(), p->y() + r.y());
initial.commit09911bf2008-07-26 23:55:29632 }
633}
634
[email protected]eb93b619b2011-02-15 22:53:19635gfx::Rect View::ConvertRectToParent(const gfx::Rect& rect) const {
[email protected]36df22b2011-02-24 21:47:56636 gfx::Rect x_rect = rect;
[email protected]18dab372011-10-03 21:21:44637 GetTransform().TransformRect(&x_rect);
[email protected]a1f4eb52011-06-29 15:06:04638 x_rect.Offset(GetMirroredPosition());
639 return x_rect;
640}
641
642gfx::Rect View::ConvertRectToWidget(const gfx::Rect& rect) const {
643 gfx::Rect x_rect = rect;
644 for (const View* v = this; v; v = v->parent_)
645 x_rect = v->ConvertRectToParent(x_rect);
[email protected]36df22b2011-02-24 21:47:56646 return x_rect;
647}
648
[email protected]2df3cc92011-02-05 00:38:59649// Painting --------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29650
[email protected]7907e592011-02-15 19:05:30651void View::SchedulePaint() {
[email protected]0a119c92011-02-23 17:50:56652 SchedulePaintInRect(GetLocalBounds());
[email protected]7907e592011-02-15 19:05:30653}
654
[email protected]0a119c92011-02-23 17:50:56655void View::SchedulePaintInRect(const gfx::Rect& rect) {
[email protected]a8f21152011-09-08 15:30:02656 if (!IsVisible() || !painting_enabled_)
initial.commit09911bf2008-07-26 23:55:29657 return;
initial.commit09911bf2008-07-26 23:55:29658
[email protected]3aa43942011-09-13 20:59:53659 if (layer()) {
660 layer()->SchedulePaint(rect);
661 } else if (parent_) {
662 // Translate the requested paint rect to the parent's coordinate system
663 // then pass this notification up to the parent.
664 parent_->SchedulePaintInRect(ConvertRectToParent(rect));
665 }
initial.commit09911bf2008-07-26 23:55:29666}
667
[email protected]0bcebb02011-02-16 03:37:04668void View::Paint(gfx::Canvas* canvas) {
[email protected]a8f21152011-09-08 15:30:02669 TRACE_EVENT0("View", "Paint");
[email protected]3aa43942011-09-13 20:59:53670
671 ScopedCanvas scoped_canvas(canvas);
672
673 // Paint this View and its children, setting the clip rect to the bounds
674 // of this View and translating the origin to the local bounds' top left
675 // point.
676 //
677 // Note that the X (or left) position we pass to ClipRectInt takes into
678 // consideration whether or not the view uses a right-to-left layout so that
679 // we paint our view in its mirrored position if need be.
[email protected]7fe28392011-10-27 00:16:05680 if (!canvas->ClipRect(gfx::Rect(GetMirroredX(), y(),
681 width() - static_cast<int>(clip_x_),
682 height() - static_cast<int>(clip_y_)))) {
initial.commit09911bf2008-07-26 23:55:29683 return;
initial.commit09911bf2008-07-26 23:55:29684 }
[email protected]3aa43942011-09-13 20:59:53685 // Non-empty clip, translate the graphics such that 0,0 corresponds to
686 // where this view is located (related to its parent).
[email protected]7fe28392011-10-27 00:16:05687 canvas->Translate(GetMirroredPosition());
[email protected]18dab372011-10-03 21:21:44688 canvas->Transform(GetTransform());
[email protected]f1785de2011-03-15 19:27:49689
[email protected]3aa43942011-09-13 20:59:53690 PaintCommon(canvas);
initial.commit09911bf2008-07-26 23:55:29691}
692
[email protected]2df3cc92011-02-05 00:38:59693ThemeProvider* View::GetThemeProvider() const {
[email protected]20838602011-02-09 04:50:21694 const Widget* widget = GetWidget();
[email protected]2df3cc92011-02-05 00:38:59695 return widget ? widget->GetThemeProvider() : NULL;
696}
697
[email protected]c797cd42011-03-15 02:18:36698// Accelerated Painting --------------------------------------------------------
699
700// static
701void View::set_use_acceleration_when_possible(bool use) {
702 use_acceleration_when_possible = use;
703}
704
[email protected]d7e2ee9d2011-06-09 13:07:11705// static
706bool View::get_use_acceleration_when_possible() {
707 return use_acceleration_when_possible;
708}
709
[email protected]2df3cc92011-02-05 00:38:59710// Input -----------------------------------------------------------------------
711
[email protected]e4607f02011-02-25 21:51:51712View* View::GetEventHandlerForPoint(const gfx::Point& point) {
[email protected]2df3cc92011-02-05 00:38:59713 // Walk the child Views recursively looking for the View that most
714 // tightly encloses the specified point.
[email protected]20838602011-02-09 04:50:21715 for (int i = child_count() - 1; i >= 0; --i) {
[email protected]fe84b912011-07-15 17:13:33716 View* child = child_at(i);
[email protected]2df3cc92011-02-05 00:38:59717 if (!child->IsVisible())
718 continue;
719
720 gfx::Point point_in_child_coords(point);
721 View::ConvertPointToView(this, child, &point_in_child_coords);
722 if (child->HitTest(point_in_child_coords))
[email protected]e4607f02011-02-25 21:51:51723 return child->GetEventHandlerForPoint(point_in_child_coords);
[email protected]2df3cc92011-02-05 00:38:59724 }
725 return this;
initial.commit09911bf2008-07-26 23:55:29726}
727
[email protected]3446c062011-05-03 21:48:00728gfx::NativeCursor View::GetCursor(const MouseEvent& event) {
[email protected]9e591cb2011-10-17 18:14:32729#if defined(OS_WIN) && !defined(USE_AURA)
[email protected]5c303962011-04-27 03:48:14730 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW);
731 return arrow;
732#else
[email protected]6a7362342011-10-11 04:20:33733 return gfx::kNullCursor;
[email protected]5c303962011-04-27 03:48:14734#endif
[email protected]9abf8dd62009-06-04 06:40:42735}
736
[email protected]a52ca4672009-06-05 05:41:09737bool View::HitTest(const gfx::Point& l) const {
[email protected]5c303962011-04-27 03:48:14738 if (GetLocalBounds().Contains(l)) {
[email protected]a52ca4672009-06-05 05:41:09739 if (HasHitTestMask()) {
740 gfx::Path mask;
741 GetHitTestMask(&mask);
[email protected]b7bf6b682011-09-30 18:05:23742#if defined(USE_AURA)
[email protected]af9c1fa32011-10-04 15:29:02743 // TODO: should we use this every where?
744 SkRegion clip_region;
745 clip_region.setRect(0, 0, width(), height());
746 SkRegion mask_region;
747 return mask_region.setPath(mask, clip_region) &&
748 mask_region.contains(l.x(), l.y());
[email protected]b7bf6b682011-09-30 18:05:23749#elif defined(OS_WIN)
[email protected]a684f122010-12-29 23:48:46750 base::win::ScopedRegion rgn(mask.CreateNativeRegion());
[email protected]a52ca4672009-06-05 05:41:09751 return !!PtInRegion(rgn, l.x(), l.y());
[email protected]10a6e77b2009-12-31 01:03:52752#elif defined(TOOLKIT_USES_GTK)
[email protected]9dd7e3d72011-01-20 18:27:06753 ui::ScopedRegion rgn(mask.CreateNativeRegion());
[email protected]a3406972009-11-04 05:05:48754 return gdk_region_point_in(rgn.Get(), l.x(), l.y());
[email protected]a52ca4672009-06-05 05:41:09755#endif
756 }
757 // No mask, but inside our bounds.
758 return true;
759 }
760 // Outside our bounds.
761 return false;
762}
763
[email protected]2c831b22011-03-14 23:17:20764bool View::OnMousePressed(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59765 return false;
766}
767
[email protected]2c831b22011-03-14 23:17:20768bool View::OnMouseDragged(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59769 return false;
770}
771
[email protected]34338252011-03-29 00:39:05772void View::OnMouseReleased(const MouseEvent& event) {
773}
774
775void View::OnMouseCaptureLost() {
[email protected]2df3cc92011-02-05 00:38:59776}
777
[email protected]2c831b22011-03-14 23:17:20778void View::OnMouseMoved(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59779}
780
[email protected]2c831b22011-03-14 23:17:20781void View::OnMouseEntered(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59782}
783
[email protected]2c831b22011-03-14 23:17:20784void View::OnMouseExited(const MouseEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59785}
786
[email protected]6a4056b2011-06-14 22:09:17787ui::TouchStatus View::OnTouchEvent(const TouchEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59788 DVLOG(1) << "visited the OnTouchEvent";
[email protected]6a4056b2011-06-14 22:09:17789 return ui::TOUCH_STATUS_UNKNOWN;
[email protected]2df3cc92011-02-05 00:38:59790}
[email protected]2df3cc92011-02-05 00:38:59791
792void View::SetMouseHandler(View *new_mouse_handler) {
793 // It is valid for new_mouse_handler to be NULL
794 if (parent_)
795 parent_->SetMouseHandler(new_mouse_handler);
796}
797
[email protected]2c831b22011-03-14 23:17:20798bool View::OnKeyPressed(const KeyEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59799 return false;
800}
801
[email protected]2c831b22011-03-14 23:17:20802bool View::OnKeyReleased(const KeyEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59803 return false;
804}
805
[email protected]2c831b22011-03-14 23:17:20806bool View::OnMouseWheel(const MouseWheelEvent& event) {
[email protected]2df3cc92011-02-05 00:38:59807 return false;
808}
809
[email protected]9fa3de1a2011-03-31 21:12:08810TextInputClient* View::GetTextInputClient() {
811 return NULL;
812}
813
814InputMethod* View::GetInputMethod() {
[email protected]6c8a4312011-04-01 22:05:14815 Widget* widget = GetWidget();
816 return widget ? widget->GetInputMethod() : NULL;
[email protected]9fa3de1a2011-03-31 21:12:08817}
818
[email protected]2df3cc92011-02-05 00:38:59819// Accelerators ----------------------------------------------------------------
820
821void View::AddAccelerator(const Accelerator& accelerator) {
822 if (!accelerators_.get())
823 accelerators_.reset(new std::vector<Accelerator>());
824
[email protected]afed4572011-06-09 18:00:11825 DCHECK(std::find(accelerators_->begin(), accelerators_->end(), accelerator) ==
826 accelerators_->end())
[email protected]2df3cc92011-02-05 00:38:59827 << "Registering the same accelerator multiple times";
828
829 accelerators_->push_back(accelerator);
830 RegisterPendingAccelerators();
831}
832
833void View::RemoveAccelerator(const Accelerator& accelerator) {
[email protected]afed4572011-06-09 18:00:11834 if (!accelerators_.get()) {
[email protected]2df3cc92011-02-05 00:38:59835 NOTREACHED() << "Removing non-existing accelerator";
836 return;
837 }
838
[email protected]afed4572011-06-09 18:00:11839 std::vector<Accelerator>::iterator i(
840 std::find(accelerators_->begin(), accelerators_->end(), accelerator));
841 if (i == accelerators_->end()) {
842 NOTREACHED() << "Removing non-existing accelerator";
843 return;
844 }
845
846 size_t index = i - accelerators_->begin();
847 accelerators_->erase(i);
[email protected]2df3cc92011-02-05 00:38:59848 if (index >= registered_accelerator_count_) {
849 // The accelerator is not registered to FocusManager.
850 return;
851 }
852 --registered_accelerator_count_;
853
[email protected]72f17242011-02-18 21:05:55854 // Providing we are attached to a Widget and registered with a focus manager,
855 // we should de-register from that focus manager now.
856 if (GetWidget() && accelerator_focus_manager_)
[email protected]2df3cc92011-02-05 00:38:59857 accelerator_focus_manager_->UnregisterAccelerator(accelerator, this);
[email protected]2df3cc92011-02-05 00:38:59858}
859
860void View::ResetAccelerators() {
861 if (accelerators_.get())
862 UnregisterAccelerators(false);
863}
864
[email protected]16460642011-03-04 23:15:53865bool View::AcceleratorPressed(const Accelerator& accelerator) {
866 return false;
867}
868
[email protected]2df3cc92011-02-05 00:38:59869// Focus -----------------------------------------------------------------------
870
[email protected]0aaa5282011-10-26 18:54:50871bool View::HasFocus() const {
872 const FocusManager* focus_manager = GetFocusManager();
873 return focus_manager && (focus_manager->GetFocusedView() == this);
[email protected]2df3cc92011-02-05 00:38:59874}
875
876View* View::GetNextFocusableView() {
877 return next_focusable_view_;
878}
879
[email protected]20838602011-02-09 04:50:21880const View* View::GetNextFocusableView() const {
881 return next_focusable_view_;
882}
883
[email protected]2df3cc92011-02-05 00:38:59884View* View::GetPreviousFocusableView() {
885 return previous_focusable_view_;
886}
887
888void View::SetNextFocusableView(View* view) {
889 view->previous_focusable_view_ = this;
890 next_focusable_view_ = view;
891}
892
[email protected]2df3cc92011-02-05 00:38:59893bool View::IsFocusableInRootView() const {
894 return IsFocusable() && IsVisibleInRootView();
895}
896
897bool View::IsAccessibilityFocusableInRootView() const {
898 return (focusable_ || accessibility_focusable_) && IsEnabled() &&
899 IsVisibleInRootView();
900}
901
902FocusManager* View::GetFocusManager() {
903 Widget* widget = GetWidget();
904 return widget ? widget->GetFocusManager() : NULL;
905}
906
[email protected]0aaa5282011-10-26 18:54:50907const FocusManager* View::GetFocusManager() const {
908 const Widget* widget = GetWidget();
909 return widget ? widget->GetFocusManager() : NULL;
910}
911
[email protected]2df3cc92011-02-05 00:38:59912void View::RequestFocus() {
[email protected]72f17242011-02-18 21:05:55913 FocusManager* focus_manager = GetFocusManager();
[email protected]6b78e39f2011-08-22 23:57:32914 if (focus_manager && IsFocusableInRootView())
[email protected]72f17242011-02-18 21:05:55915 focus_manager->SetFocusedView(this);
[email protected]2df3cc92011-02-05 00:38:59916}
917
[email protected]2c831b22011-03-14 23:17:20918bool View::SkipDefaultKeyEventProcessing(const KeyEvent& event) {
[email protected]16460642011-03-04 23:15:53919 return false;
920}
921
922FocusTraversable* View::GetFocusTraversable() {
923 return NULL;
924}
925
926FocusTraversable* View::GetPaneFocusTraversable() {
927 return NULL;
928}
929
[email protected]2df3cc92011-02-05 00:38:59930// Tooltips --------------------------------------------------------------------
931
[email protected]0aaa5282011-10-26 18:54:50932bool View::GetTooltipText(const gfx::Point& p, string16* tooltip) const {
[email protected]2df3cc92011-02-05 00:38:59933 return false;
934}
935
[email protected]0aaa5282011-10-26 18:54:50936bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) const {
[email protected]2df3cc92011-02-05 00:38:59937 return false;
938}
939
940// Context menus ---------------------------------------------------------------
941
[email protected]e9adf0702010-03-08 23:34:07942void View::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) {
[email protected]042811c2008-10-31 21:31:34943 if (!context_menu_controller_)
944 return;
945
[email protected]0933ab82011-03-04 04:57:58946 context_menu_controller_->ShowContextMenuForView(this, p, is_mouse_gesture);
[email protected]042811c2008-10-31 21:31:34947}
948
[email protected]2df3cc92011-02-05 00:38:59949// Drag and drop ---------------------------------------------------------------
950
[email protected]2df3cc92011-02-05 00:38:59951bool View::GetDropFormats(
952 int* formats,
953 std::set<OSExchangeData::CustomFormat>* custom_formats) {
954 return false;
955}
956
957bool View::AreDropTypesRequired() {
958 return false;
959}
960
961bool View::CanDrop(const OSExchangeData& data) {
962 // TODO(sky): when I finish up migration, this should default to true.
963 return false;
964}
965
966void View::OnDragEntered(const DropTargetEvent& event) {
967}
968
969int View::OnDragUpdated(const DropTargetEvent& event) {
970 return ui::DragDropTypes::DRAG_NONE;
971}
972
973void View::OnDragExited() {
974}
975
976int View::OnPerformDrop(const DropTargetEvent& event) {
977 return ui::DragDropTypes::DRAG_NONE;
978}
979
[email protected]72f17242011-02-18 21:05:55980void View::OnDragDone() {
981}
982
[email protected]2df3cc92011-02-05 00:38:59983// static
984bool View::ExceededDragThreshold(int delta_x, int delta_y) {
985 return (abs(delta_x) > GetHorizontalDragThreshold() ||
986 abs(delta_y) > GetVerticalDragThreshold());
987}
988
[email protected]2df3cc92011-02-05 00:38:59989// Scrolling -------------------------------------------------------------------
990
991void View::ScrollRectToVisible(const gfx::Rect& rect) {
[email protected]2df3cc92011-02-05 00:38:59992 // We must take RTL UI mirroring into account when adjusting the position of
993 // the region.
[email protected]2fa942b2011-06-14 15:59:55994 if (parent_) {
[email protected]2df3cc92011-02-05 00:38:59995 gfx::Rect scroll_rect(rect);
[email protected]9defbad2011-02-08 04:23:18996 scroll_rect.Offset(GetMirroredX(), y());
[email protected]2fa942b2011-06-14 15:59:55997 parent_->ScrollRectToVisible(scroll_rect);
[email protected]2df3cc92011-02-05 00:38:59998 }
999}
1000
1001int View::GetPageScrollIncrement(ScrollView* scroll_view,
1002 bool is_horizontal, bool is_positive) {
1003 return 0;
1004}
1005
1006int View::GetLineScrollIncrement(ScrollView* scroll_view,
1007 bool is_horizontal, bool is_positive) {
1008 return 0;
1009}
1010
1011////////////////////////////////////////////////////////////////////////////////
1012// View, protected:
1013
1014// Size and disposition --------------------------------------------------------
1015
[email protected]8eb52a9a2011-03-09 16:52:001016void View::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1017}
1018
[email protected]2df3cc92011-02-05 00:38:591019void View::PreferredSizeChanged() {
1020 InvalidateLayout();
1021 if (parent_)
1022 parent_->ChildPreferredSizeChanged(this);
1023}
1024
[email protected]6260f9e2011-02-16 21:28:011025bool View::NeedsNotificationWhenVisibleBoundsChange() const {
1026 return false;
[email protected]2df3cc92011-02-05 00:38:591027}
1028
[email protected]6260f9e2011-02-16 21:28:011029void View::OnVisibleBoundsChanged() {
[email protected]2df3cc92011-02-05 00:38:591030}
1031
1032// Tree operations -------------------------------------------------------------
1033
1034void View::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
1035}
1036
1037void View::VisibilityChanged(View* starting_from, bool is_visible) {
1038}
1039
1040void View::NativeViewHierarchyChanged(bool attached,
1041 gfx::NativeView native_view,
[email protected]10946072011-05-20 15:40:451042 internal::RootView* root_view) {
[email protected]2df3cc92011-02-05 00:38:591043 FocusManager* focus_manager = GetFocusManager();
1044 if (!accelerator_registration_delayed_ &&
1045 accelerator_focus_manager_ &&
1046 accelerator_focus_manager_ != focus_manager) {
1047 UnregisterAccelerators(true);
1048 accelerator_registration_delayed_ = true;
1049 }
1050 if (accelerator_registration_delayed_ && attached) {
1051 if (focus_manager) {
1052 RegisterPendingAccelerators();
1053 accelerator_registration_delayed_ = false;
1054 }
1055 }
1056}
1057
1058// Painting --------------------------------------------------------------------
1059
[email protected]0bcebb02011-02-16 03:37:041060void View::PaintChildren(gfx::Canvas* canvas) {
[email protected]fe84b912011-07-15 17:13:331061 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]23601582011-09-28 13:32:181062 if (!child_at(i)->layer())
1063 child_at(i)->Paint(canvas);
[email protected]0bcebb02011-02-16 03:37:041064}
1065
1066void View::OnPaint(gfx::Canvas* canvas) {
1067 OnPaintBackground(canvas);
1068 OnPaintFocusBorder(canvas);
1069 OnPaintBorder(canvas);
1070}
1071
1072void View::OnPaintBackground(gfx::Canvas* canvas) {
1073 if (background_.get())
1074 background_->Paint(canvas, this);
1075}
1076
1077void View::OnPaintBorder(gfx::Canvas* canvas) {
1078 if (border_.get())
1079 border_->Paint(*this, canvas);
1080}
1081
1082void View::OnPaintFocusBorder(gfx::Canvas* canvas) {
[email protected]a1a9e7c82011-04-15 21:58:241083 if ((IsFocusable() || IsAccessibilityFocusableInRootView()) && HasFocus())
[email protected]27488c22011-10-25 02:45:211084 canvas->DrawFocusRect(GetLocalBounds());
[email protected]0bcebb02011-02-16 03:37:041085}
1086
[email protected]c797cd42011-03-15 02:18:361087// Accelerated Painting --------------------------------------------------------
1088
[email protected]c155c252011-07-29 16:17:551089void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) {
[email protected]18dab372011-10-03 21:21:441090 // This method should not have the side-effect of creating the layer.
[email protected]c155c252011-07-29 16:17:551091 if (layer())
1092 layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely);
1093}
1094
[email protected]1cbbee3c2011-06-24 12:32:191095bool View::SetExternalTexture(ui::Texture* texture) {
[email protected]28cd2bb2011-09-19 21:04:191096 DCHECK(texture);
[email protected]18dab372011-10-03 21:21:441097 SetPaintToLayer(true);
[email protected]7c4b1b92011-06-27 16:37:181098
[email protected]18dab372011-10-03 21:21:441099 layer()->SetExternalTexture(texture);
[email protected]1cbbee3c2011-06-24 12:32:191100
[email protected]3a1a0cc2011-08-10 14:36:421101 // Child views must not paint into the external texture. So make sure each
1102 // child view has its own layer to paint into.
[email protected]28cd2bb2011-09-19 21:04:191103 for (Views::iterator i = children_.begin(); i != children_.end(); ++i)
1104 (*i)->SetPaintToLayer(true);
[email protected]3a1a0cc2011-08-10 14:36:421105
[email protected]3aa43942011-09-13 20:59:531106 SchedulePaintInRect(GetLocalBounds());
[email protected]1cbbee3c2011-06-24 12:32:191107 return true;
1108}
1109
[email protected]e865b362011-07-01 17:09:461110void View::CalculateOffsetToAncestorWithLayer(gfx::Point* offset,
[email protected]3aa43942011-09-13 20:59:531111 ui::Layer** layer_parent) {
[email protected]e865b362011-07-01 17:09:461112 if (layer()) {
[email protected]3aa43942011-09-13 20:59:531113 if (layer_parent)
1114 *layer_parent = layer();
[email protected]e865b362011-07-01 17:09:461115 return;
1116 }
1117 if (!parent_)
1118 return;
1119
1120 offset->Offset(x(), y());
[email protected]3aa43942011-09-13 20:59:531121 parent_->CalculateOffsetToAncestorWithLayer(offset, layer_parent);
[email protected]e865b362011-07-01 17:09:461122}
1123
[email protected]e865b362011-07-01 17:09:461124void View::MoveLayerToParent(ui::Layer* parent_layer,
1125 const gfx::Point& point) {
1126 gfx::Point local_point(point);
1127 if (parent_layer != layer())
1128 local_point.Offset(x(), y());
1129 if (layer() && parent_layer != layer()) {
1130 parent_layer->Add(layer());
[email protected]18dab372011-10-03 21:21:441131 layer()->SetBounds(gfx::Rect(local_point.x(), local_point.y(),
1132 width(), height()));
[email protected]e865b362011-07-01 17:09:461133 } else {
1134 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331135 child_at(i)->MoveLayerToParent(parent_layer, local_point);
[email protected]e865b362011-07-01 17:09:461136 }
1137}
1138
[email protected]25ae9a12011-10-12 14:55:221139void View::UpdateLayerVisibility() {
1140 if (!use_acceleration_when_possible)
1141 return;
1142 bool visible = IsVisible();
1143 for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_)
1144 visible = v->IsVisible();
1145
1146 UpdateChildLayerVisibility(visible);
1147}
1148
1149void View::UpdateChildLayerVisibility(bool ancestor_visible) {
1150 if (layer()) {
1151 layer()->SetVisible(ancestor_visible && IsVisible());
1152 } else {
1153 for (int i = 0, count = child_count(); i < count; ++i)
1154 child_at(i)->UpdateChildLayerVisibility(ancestor_visible && IsVisible());
1155 }
1156}
1157
[email protected]18dab372011-10-03 21:21:441158void View::UpdateChildLayerBounds(const gfx::Point& offset) {
[email protected]e865b362011-07-01 17:09:461159 if (layer()) {
[email protected]18dab372011-10-03 21:21:441160 layer_property_setter_->SetBounds(layer(), gfx::Rect(offset.x(), offset.y(),
1161 width(), height()));
[email protected]e865b362011-07-01 17:09:461162 } else {
[email protected]18dab372011-10-03 21:21:441163 for (int i = 0, count = child_count(); i < count; ++i) {
1164 gfx::Point new_offset(offset.x() + child_at(i)->x(),
1165 offset.y() + child_at(i)->y());
1166 child_at(i)->UpdateChildLayerBounds(new_offset);
1167 }
[email protected]e865b362011-07-01 17:09:461168 }
1169}
1170
[email protected]3aa43942011-09-13 20:59:531171void View::OnPaintLayer(gfx::Canvas* canvas) {
[email protected]1d833532011-10-26 15:00:171172 if (!layer() || !layer()->fills_bounds_opaquely())
1173 canvas->GetSkCanvas()->drawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
[email protected]3aa43942011-09-13 20:59:531174 PaintCommon(canvas);
1175}
1176
[email protected]326828b2011-10-21 02:01:101177void View::OnLayerAnimationEnded(const ui::Animation* animation) {
1178}
1179
[email protected]9ef00a52011-10-08 11:53:351180void View::ReorderLayers() {
1181 View* v = this;
1182 while (v && !v->layer())
1183 v = v->parent();
1184
1185 // Forward to widget in case we're in a NativeWidgetView.
1186 if (!v) {
1187 if (GetWidget())
1188 GetWidget()->ReorderLayers();
1189 } else {
1190 for (Views::const_iterator i(v->children_.begin());
1191 i != v->children_.end();
1192 ++i)
1193 (*i)->ReorderChildLayers(v->layer());
1194 }
1195}
1196
1197void View::ReorderChildLayers(ui::Layer* parent_layer) {
1198 if (layer()) {
1199 DCHECK_EQ(parent_layer, layer()->parent());
1200 parent_layer->MoveToFront(layer());
1201 } else {
1202 for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i)
1203 (*i)->ReorderChildLayers(parent_layer);
1204 }
1205}
1206
[email protected]2df3cc92011-02-05 00:38:591207// Input -----------------------------------------------------------------------
1208
1209bool View::HasHitTestMask() const {
1210 return false;
1211}
1212
1213void View::GetHitTestMask(gfx::Path* mask) const {
1214 DCHECK(mask);
1215}
1216
1217// Focus -----------------------------------------------------------------------
1218
1219bool View::IsFocusable() const {
1220 return focusable_ && IsEnabled() && IsVisible();
1221}
1222
[email protected]420138332011-02-24 00:59:521223void View::OnFocus() {
1224 // TODO(beng): Investigate whether it's possible for us to move this to
1225 // Focus().
[email protected]2df3cc92011-02-05 00:38:591226 // By default, we clear the native focus. This ensures that no visible native
1227 // view as the focus and that we still receive keyboard inputs.
1228 FocusManager* focus_manager = GetFocusManager();
1229 if (focus_manager)
1230 focus_manager->ClearNativeFocus();
1231
[email protected]420138332011-02-24 00:59:521232 // TODO(beng): Investigate whether it's possible for us to move this to
1233 // Focus().
[email protected]2df3cc92011-02-05 00:38:591234 // Notify assistive technologies of the focus change.
[email protected]79e549f2011-03-14 06:56:331235 GetWidget()->NotifyAccessibilityEvent(
1236 this, ui::AccessibilityTypes::EVENT_FOCUS, true);
[email protected]2df3cc92011-02-05 00:38:591237}
1238
[email protected]420138332011-02-24 00:59:521239void View::OnBlur() {
1240}
1241
1242void View::Focus() {
1243 SchedulePaint();
1244 OnFocus();
1245}
1246
1247void View::Blur() {
1248 SchedulePaint();
1249 OnBlur();
1250}
1251
[email protected]2df3cc92011-02-05 00:38:591252// Tooltips --------------------------------------------------------------------
1253
1254void View::TooltipTextChanged() {
1255 Widget* widget = GetWidget();
[email protected]2463b91b2011-06-13 21:11:371256 // TooltipManager may be null if there is a problem creating it.
[email protected]2f2b57b2011-06-16 17:21:231257 if (widget && widget->native_widget_private()->GetTooltipManager()) {
1258 widget->native_widget_private()->GetTooltipManager()->
1259 TooltipTextChanged(this);
1260 }
[email protected]2df3cc92011-02-05 00:38:591261}
1262
1263// Context menus ---------------------------------------------------------------
1264
1265gfx::Point View::GetKeyboardContextMenuLocation() {
1266 gfx::Rect vis_bounds = GetVisibleBounds();
1267 gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2,
1268 vis_bounds.y() + vis_bounds.height() / 2);
1269 ConvertPointToScreen(this, &screen_point);
1270 return screen_point;
1271}
1272
1273// Drag and drop ---------------------------------------------------------------
1274
1275int View::GetDragOperations(const gfx::Point& press_pt) {
1276 return drag_controller_ ?
[email protected]c3fac082011-03-04 01:04:181277 drag_controller_->GetDragOperationsForView(this, press_pt) :
[email protected]2df3cc92011-02-05 00:38:591278 ui::DragDropTypes::DRAG_NONE;
1279}
1280
1281void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) {
1282 DCHECK(drag_controller_);
[email protected]c3fac082011-03-04 01:04:181283 drag_controller_->WriteDragDataForView(this, press_pt, data);
[email protected]2df3cc92011-02-05 00:38:591284}
1285
[email protected]2df3cc92011-02-05 00:38:591286bool View::InDrag() {
[email protected]72f17242011-02-18 21:05:551287 Widget* widget = GetWidget();
[email protected]9861ae92011-03-03 20:15:031288 return widget ? widget->dragged_view() == this : false;
[email protected]2df3cc92011-02-05 00:38:591289}
1290
[email protected]36de1cb2011-09-20 20:47:071291// Debugging -------------------------------------------------------------------
1292
1293#if !defined(NDEBUG)
1294
1295std::string View::PrintViewGraph(bool first) {
1296 return DoPrintViewGraph(first, this);
1297}
1298
1299std::string View::DoPrintViewGraph(bool first, View* view_with_children) {
1300 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
1301 const size_t kMaxPointerStringLength = 19;
1302
1303 std::string result;
1304
1305 if (first)
1306 result.append("digraph {\n");
1307
1308 // Node characteristics.
1309 char p[kMaxPointerStringLength];
1310
1311 size_t baseNameIndex = GetClassName().find_last_of('/');
1312 if (baseNameIndex == std::string::npos)
1313 baseNameIndex = 0;
1314 else
1315 baseNameIndex++;
1316
1317 char bounds_buffer[512];
1318
1319 // Information about current node.
1320 base::snprintf(p, arraysize(bounds_buffer), "%p", view_with_children);
1321 result.append(" N");
1322 result.append(p+2);
1323 result.append(" [label=\"");
1324
1325 result.append(GetClassName().substr(baseNameIndex).c_str());
1326
1327 base::snprintf(bounds_buffer,
1328 arraysize(bounds_buffer),
1329 "\\n bounds: (%d, %d), (%dx%d)",
1330 this->bounds().x(),
1331 this->bounds().y(),
1332 this->bounds().width(),
1333 this->bounds().height());
1334 result.append(bounds_buffer);
1335
1336 if (layer() && !layer()->hole_rect().IsEmpty()) {
1337 base::snprintf(bounds_buffer,
1338 arraysize(bounds_buffer),
1339 "\\n hole bounds: (%d, %d), (%dx%d)",
1340 layer()->hole_rect().x(),
1341 layer()->hole_rect().y(),
1342 layer()->hole_rect().width(),
1343 layer()->hole_rect().height());
1344 result.append(bounds_buffer);
1345 }
1346
1347 if (GetTransform().HasChange()) {
1348 gfx::Point translation;
1349 float rotation;
1350 gfx::Point3f scale;
1351 if (ui::InterpolatedTransform::FactorTRS(GetTransform(),
1352 &translation,
1353 &rotation,
1354 &scale)) {
1355 if (translation != gfx::Point(0, 0)) {
1356 base::snprintf(bounds_buffer,
1357 arraysize(bounds_buffer),
1358 "\\n translation: (%d, %d)",
1359 translation.x(),
1360 translation.y());
1361 result.append(bounds_buffer);
1362 }
1363
1364 if (fabs(rotation) > 1e-5) {
1365 base::snprintf(bounds_buffer,
1366 arraysize(bounds_buffer),
1367 "\\n rotation: %3.2f", rotation);
1368 result.append(bounds_buffer);
1369 }
1370
1371 if (scale.AsPoint() != gfx::Point(0, 0)) {
1372 base::snprintf(bounds_buffer,
1373 arraysize(bounds_buffer),
1374 "\\n scale: (%2.4f, %2.4f)",
1375 scale.x(),
1376 scale.y());
1377 result.append(bounds_buffer);
1378 }
1379 }
1380 }
1381
1382 result.append("\"");
1383 if (!parent_)
1384 result.append(", shape=box");
1385 if (layer()) {
1386 if (layer()->texture())
1387 result.append(", color=green");
1388 else
1389 result.append(", color=red");
1390
1391 if (layer()->fills_bounds_opaquely())
1392 result.append(", style=filled");
1393 }
1394 result.append("]\n");
1395
1396 // Link to parent.
1397 if (parent_) {
1398 char pp[kMaxPointerStringLength];
1399
1400 base::snprintf(pp, kMaxPointerStringLength, "%p", parent_);
1401 result.append(" N");
1402 result.append(pp+2);
1403 result.append(" -> N");
1404 result.append(p+2);
1405 result.append("\n");
1406 }
1407
1408 // Children.
1409 for (int i = 0, count = view_with_children->child_count(); i < count; ++i)
1410 result.append(view_with_children->child_at(i)->PrintViewGraph(false));
1411
1412 if (first)
1413 result.append("}\n");
1414
1415 return result;
1416}
1417
1418#endif
1419
[email protected]2df3cc92011-02-05 00:38:591420////////////////////////////////////////////////////////////////////////////////
1421// View, private:
1422
1423// DropInfo --------------------------------------------------------------------
1424
1425void View::DragInfo::Reset() {
1426 possible_drag = false;
1427 start_pt = gfx::Point();
1428}
1429
1430void View::DragInfo::PossibleDrag(const gfx::Point& p) {
1431 possible_drag = true;
1432 start_pt = p;
1433}
1434
[email protected]adc93fa72011-06-21 19:47:391435// Painting --------------------------------------------------------------------
1436
1437void View::SchedulePaintBoundsChanged(SchedulePaintType type) {
[email protected]3aa43942011-09-13 20:59:531438 // If we have a layer and the View's size did not change, we do not need to
1439 // schedule any paints since the layer will be redrawn at its new location
1440 // during the next Draw() cycle in the compositor.
1441 if (!layer() || type == SCHEDULE_PAINT_SIZE_CHANGED) {
1442 // Otherwise, if the size changes or we don't have a layer then we need to
1443 // use SchedulePaint to invalidate the area occupied by the View.
[email protected]adc93fa72011-06-21 19:47:391444 SchedulePaint();
[email protected]33097102011-10-12 13:19:021445 } else if (parent_ && type == SCHEDULE_PAINT_SIZE_SAME) {
1446 // The compositor doesn't Draw() until something on screen changes, so
1447 // if our position changes but nothing is being animated on screen, then
1448 // tell the compositor to redraw the scene. We know layer() exists due to
1449 // the above if clause.
1450 layer()->ScheduleDraw();
[email protected]adc93fa72011-06-21 19:47:391451 }
1452}
1453
[email protected]3aa43942011-09-13 20:59:531454void View::PaintCommon(gfx::Canvas* canvas) {
1455 if (!IsVisible() || !painting_enabled_)
1456 return;
1457
1458 {
1459 // If the View we are about to paint requested the canvas to be flipped, we
1460 // should change the transform appropriately.
1461 // The canvas mirroring is undone once the View is done painting so that we
1462 // don't pass the canvas with the mirrored transform to Views that didn't
1463 // request the canvas to be flipped.
1464 ScopedCanvas scoped(canvas);
1465 if (FlipCanvasOnPaintForRTLUI()) {
[email protected]7fe28392011-10-27 00:16:051466 canvas->Translate(gfx::Point(width(), 0));
[email protected]3aa43942011-09-13 20:59:531467 canvas->ScaleInt(-1, 1);
1468 }
1469
1470 OnPaint(canvas);
1471 }
1472
1473 PaintChildren(canvas);
1474}
1475
[email protected]2df3cc92011-02-05 00:38:591476// Tree operations -------------------------------------------------------------
1477
[email protected]20838602011-02-09 04:50:211478void View::DoRemoveChildView(View* view,
[email protected]2df3cc92011-02-05 00:38:591479 bool update_focus_cycle,
1480 bool update_tool_tip,
1481 bool delete_removed_view) {
[email protected]20838602011-02-09 04:50:211482 DCHECK(view);
[email protected]afed4572011-06-09 18:00:111483 const Views::iterator i(std::find(children_.begin(), children_.end(), view));
[email protected]2df3cc92011-02-05 00:38:591484 scoped_ptr<View> view_to_be_deleted;
[email protected]20838602011-02-09 04:50:211485 if (i != children_.end()) {
[email protected]2df3cc92011-02-05 00:38:591486 if (update_focus_cycle) {
1487 // Let's remove the view from the focus traversal.
[email protected]20838602011-02-09 04:50:211488 View* next_focusable = view->next_focusable_view_;
1489 View* prev_focusable = view->previous_focusable_view_;
[email protected]2df3cc92011-02-05 00:38:591490 if (prev_focusable)
1491 prev_focusable->next_focusable_view_ = next_focusable;
1492 if (next_focusable)
1493 next_focusable->previous_focusable_view_ = prev_focusable;
1494 }
1495
[email protected]6260f9e2011-02-16 21:28:011496 if (GetWidget())
1497 UnregisterChildrenForVisibleBoundsNotification(view);
[email protected]20838602011-02-09 04:50:211498 view->PropagateRemoveNotifications(this);
[email protected]b5fe7442011-05-26 02:42:431499 view->parent_ = NULL;
[email protected]25ae9a12011-10-12 14:55:221500 view->UpdateLayerVisibility();
[email protected]2df3cc92011-02-05 00:38:591501
[email protected]3534e7a2011-05-26 17:44:031502 if (delete_removed_view && view->parent_owned())
[email protected]20838602011-02-09 04:50:211503 view_to_be_deleted.reset(view);
[email protected]2df3cc92011-02-05 00:38:591504
[email protected]20838602011-02-09 04:50:211505 children_.erase(i);
[email protected]2df3cc92011-02-05 00:38:591506 }
1507
1508 if (update_tool_tip)
1509 UpdateTooltip();
1510
1511 if (layout_manager_.get())
[email protected]20838602011-02-09 04:50:211512 layout_manager_->ViewRemoved(this, view);
[email protected]2df3cc92011-02-05 00:38:591513}
1514
[email protected]2df3cc92011-02-05 00:38:591515void View::PropagateRemoveNotifications(View* parent) {
[email protected]20838602011-02-09 04:50:211516 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331517 child_at(i)->PropagateRemoveNotifications(parent);
[email protected]2df3cc92011-02-05 00:38:591518
[email protected]2fa942b2011-06-14 15:59:551519 for (View* v = this; v; v = v->parent_)
[email protected]2df3cc92011-02-05 00:38:591520 v->ViewHierarchyChangedImpl(true, false, parent, this);
1521}
1522
1523void View::PropagateAddNotifications(View* parent, View* child) {
[email protected]20838602011-02-09 04:50:211524 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331525 child_at(i)->PropagateAddNotifications(parent, child);
[email protected]2df3cc92011-02-05 00:38:591526 ViewHierarchyChangedImpl(true, true, parent, child);
1527}
1528
1529void View::PropagateNativeViewHierarchyChanged(bool attached,
1530 gfx::NativeView native_view,
[email protected]10946072011-05-20 15:40:451531 internal::RootView* root_view) {
[email protected]20838602011-02-09 04:50:211532 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331533 child_at(i)->PropagateNativeViewHierarchyChanged(attached,
[email protected]2df3cc92011-02-05 00:38:591534 native_view,
1535 root_view);
1536 NativeViewHierarchyChanged(attached, native_view, root_view);
1537}
1538
1539void View::ViewHierarchyChangedImpl(bool register_accelerators,
1540 bool is_add,
1541 View* parent,
1542 View* child) {
1543 if (register_accelerators) {
1544 if (is_add) {
1545 // If you get this registration, you are part of a subtree that has been
1546 // added to the view hierarchy.
1547 if (GetFocusManager()) {
1548 RegisterPendingAccelerators();
1549 } else {
1550 // Delay accelerator registration until visible as we do not have
1551 // focus manager until then.
1552 accelerator_registration_delayed_ = true;
1553 }
1554 } else {
1555 if (child == this)
1556 UnregisterAccelerators(true);
1557 }
1558 }
1559
[email protected]06061b492011-10-03 21:48:491560 if (is_add && layer() && !layer()->parent()) {
[email protected]18dab372011-10-03 21:21:441561 UpdateParentLayer();
[email protected]06061b492011-10-03 21:48:491562 } else if (!is_add && child == this) {
1563 // Make sure the layers beloning to the subtree rooted at |child| get
1564 // removed from layers that do not belong in the same subtree.
1565 OrphanLayers();
1566 }
[email protected]18dab372011-10-03 21:21:441567
[email protected]2df3cc92011-02-05 00:38:591568 ViewHierarchyChanged(is_add, parent, child);
1569 parent->needs_layout_ = true;
1570}
1571
[email protected]2df3cc92011-02-05 00:38:591572// Size and disposition --------------------------------------------------------
1573
1574void View::PropagateVisibilityNotifications(View* start, bool is_visible) {
[email protected]20838602011-02-09 04:50:211575 for (int i = 0, count = child_count(); i < count; ++i)
[email protected]fe84b912011-07-15 17:13:331576 child_at(i)->PropagateVisibilityNotifications(start, is_visible);
[email protected]2df3cc92011-02-05 00:38:591577 VisibilityChangedImpl(start, is_visible);
1578}
1579
1580void View::VisibilityChangedImpl(View* starting_from, bool is_visible) {
1581 if (is_visible)
1582 RegisterPendingAccelerators();
1583 else
1584 UnregisterAccelerators(true);
1585 VisibilityChanged(starting_from, is_visible);
1586}
1587
[email protected]8eb52a9a2011-03-09 16:52:001588void View::BoundsChanged(const gfx::Rect& previous_bounds) {
[email protected]19bebd02011-04-06 18:27:341589 if (IsVisible()) {
[email protected]adc93fa72011-06-21 19:47:391590 // Paint the new bounds.
1591 SchedulePaintBoundsChanged(
1592 bounds_.size() == previous_bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
1593 SCHEDULE_PAINT_SIZE_CHANGED);
[email protected]06061b492011-10-03 21:48:491594 }
[email protected]adc93fa72011-06-21 19:47:391595
[email protected]06061b492011-10-03 21:48:491596 if (use_acceleration_when_possible) {
1597 if (layer()) {
1598 if (parent_) {
[email protected]e865b362011-07-01 17:09:461599 gfx::Point offset;
[email protected]06061b492011-10-03 21:48:491600 parent_->CalculateOffsetToAncestorWithLayer(&offset, NULL);
1601 offset.Offset(x(), y());
1602 layer_property_setter_->SetBounds(layer(), gfx::Rect(offset, size()));
1603 } else {
1604 layer_property_setter_->SetBounds(layer(), bounds_);
[email protected]ee012852011-06-24 02:46:201605 }
[email protected]06061b492011-10-03 21:48:491606 // TODO(beng): this seems redundant with the SchedulePaint at the top of
1607 // this function. explore collapsing.
1608 if (previous_bounds.size() != bounds_.size() &&
1609 !layer()->layer_updated_externally()) {
1610 // If our bounds have changed then we need to update the complete
1611 // texture.
1612 layer()->SchedulePaint(GetLocalBounds());
1613 }
1614 } else {
1615 // If our bounds have changed, then any descendant layer bounds may
1616 // have changed. Update them accordingly.
1617 gfx::Point offset;
1618 CalculateOffsetToAncestorWithLayer(&offset, NULL);
1619 UpdateChildLayerBounds(offset);
[email protected]19bebd02011-04-06 18:27:341620 }
[email protected]8eb52a9a2011-03-09 16:52:001621 }
1622
1623 OnBoundsChanged(previous_bounds);
1624
1625 if (previous_bounds.size() != size()) {
1626 needs_layout_ = false;
1627 Layout();
1628 }
[email protected]6260f9e2011-02-16 21:28:011629
[email protected]18dab372011-10-03 21:21:441630 if (NeedsNotificationWhenVisibleBoundsChange())
[email protected]44e9d9c2011-04-14 21:04:061631 OnVisibleBoundsChanged();
[email protected]44e9d9c2011-04-14 21:04:061632
[email protected]6260f9e2011-02-16 21:28:011633 // Notify interested Views that visible bounds within the root view may have
1634 // changed.
1635 if (descendants_to_notify_.get()) {
[email protected]afed4572011-06-09 18:00:111636 for (Views::iterator i(descendants_to_notify_->begin());
[email protected]6260f9e2011-02-16 21:28:011637 i != descendants_to_notify_->end(); ++i) {
1638 (*i)->OnVisibleBoundsChanged();
1639 }
1640 }
[email protected]2df3cc92011-02-05 00:38:591641}
1642
1643// static
[email protected]6260f9e2011-02-16 21:28:011644void View::RegisterChildrenForVisibleBoundsNotification(View* view) {
1645 if (view->NeedsNotificationWhenVisibleBoundsChange())
1646 view->RegisterForVisibleBoundsNotification();
[email protected]20838602011-02-09 04:50:211647 for (int i = 0; i < view->child_count(); ++i)
[email protected]fe84b912011-07-15 17:13:331648 RegisterChildrenForVisibleBoundsNotification(view->child_at(i));
[email protected]6260f9e2011-02-16 21:28:011649}
1650
1651// static
1652void View::UnregisterChildrenForVisibleBoundsNotification(View* view) {
1653 if (view->NeedsNotificationWhenVisibleBoundsChange())
1654 view->UnregisterForVisibleBoundsNotification();
1655 for (int i = 0; i < view->child_count(); ++i)
[email protected]fe84b912011-07-15 17:13:331656 UnregisterChildrenForVisibleBoundsNotification(view->child_at(i));
[email protected]6260f9e2011-02-16 21:28:011657}
1658
1659void View::RegisterForVisibleBoundsNotification() {
1660 if (registered_for_visible_bounds_notification_)
1661 return;
[email protected]44e9d9c2011-04-14 21:04:061662
[email protected]6260f9e2011-02-16 21:28:011663 registered_for_visible_bounds_notification_ = true;
[email protected]2fa942b2011-06-14 15:59:551664 for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
[email protected]6260f9e2011-02-16 21:28:011665 ancestor->AddDescendantToNotify(this);
[email protected]6260f9e2011-02-16 21:28:011666}
1667
1668void View::UnregisterForVisibleBoundsNotification() {
1669 if (!registered_for_visible_bounds_notification_)
1670 return;
[email protected]2fa942b2011-06-14 15:59:551671
[email protected]6260f9e2011-02-16 21:28:011672 registered_for_visible_bounds_notification_ = false;
[email protected]2fa942b2011-06-14 15:59:551673 for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
[email protected]6260f9e2011-02-16 21:28:011674 ancestor->RemoveDescendantToNotify(this);
[email protected]2df3cc92011-02-05 00:38:591675}
1676
1677void View::AddDescendantToNotify(View* view) {
1678 DCHECK(view);
1679 if (!descendants_to_notify_.get())
[email protected]695e942e2011-06-06 15:15:511680 descendants_to_notify_.reset(new Views);
[email protected]2df3cc92011-02-05 00:38:591681 descendants_to_notify_->push_back(view);
1682}
1683
1684void View::RemoveDescendantToNotify(View* view) {
1685 DCHECK(view && descendants_to_notify_.get());
[email protected]afed4572011-06-09 18:00:111686 Views::iterator i(std::find(
1687 descendants_to_notify_->begin(), descendants_to_notify_->end(), view));
[email protected]2df3cc92011-02-05 00:38:591688 DCHECK(i != descendants_to_notify_->end());
1689 descendants_to_notify_->erase(i);
1690 if (descendants_to_notify_->empty())
1691 descendants_to_notify_.reset();
1692}
1693
[email protected]eb93b619b2011-02-15 22:53:191694// Transformations -------------------------------------------------------------
1695
[email protected]b9b1e7a42011-05-17 15:29:511696bool View::GetTransformRelativeTo(const View* ancestor,
1697 ui::Transform* transform) const {
[email protected]6bb8b6c2011-05-19 22:09:051698 const View* p = this;
1699
1700 while (p && p != ancestor) {
[email protected]18dab372011-10-03 21:21:441701 transform->ConcatTransform(p->GetTransform());
[email protected]6bb8b6c2011-05-19 22:09:051702 transform->ConcatTranslate(static_cast<float>(p->GetMirroredX()),
1703 static_cast<float>(p->y()));
1704
1705 p = p->parent_;
[email protected]c36b0bf2011-05-10 18:26:251706 }
[email protected]6bb8b6c2011-05-19 22:09:051707
1708 return p == ancestor;
[email protected]c36b0bf2011-05-10 18:26:251709}
1710
[email protected]2df3cc92011-02-05 00:38:591711// Coordinate conversion -------------------------------------------------------
1712
1713// static
1714void View::ConvertPointToView(const View* src,
1715 const View* dst,
1716 gfx::Point* point,
1717 bool try_other_direction) {
1718 // src can be NULL
1719 DCHECK(dst);
1720 DCHECK(point);
1721
[email protected]86477142011-08-19 19:06:151722 const Widget* src_widget = src ? src->GetWidget() : NULL ;
1723 const Widget* dst_widget = dst->GetWidget();
1724 // If dest and src aren't in the same widget, try to convert the
1725 // point to the destination widget's coordinates first.
1726 // TODO(oshima|sadrul): Cleanup and consolidate conversion methods.
1727 if (Widget::IsPureViews() && src_widget && src_widget != dst_widget) {
1728 // convert to src_widget first.
1729 gfx::Point p = *point;
1730 src->ConvertPointForAncestor(src_widget->GetRootView(), &p);
1731 if (dst_widget->ConvertPointFromAncestor(src_widget, &p)) {
1732 // Convertion to destination widget's coordinates was successful.
1733 // Use destination's root as a source to convert the point further.
1734 src = dst_widget->GetRootView();
1735 *point = p;
1736 }
1737 }
1738
[email protected]36df22b2011-02-24 21:47:561739 if (src == NULL || src->Contains(dst)) {
1740 dst->ConvertPointFromAncestor(src, point);
1741 if (!src) {
[email protected]86477142011-08-19 19:06:151742 if (dst_widget) {
1743 gfx::Rect b = dst_widget->GetClientAreaScreenBounds();
[email protected]2df3cc92011-02-05 00:38:591744 point->SetPoint(point->x() - b.x(), point->y() - b.y());
1745 }
1746 }
[email protected]36df22b2011-02-24 21:47:561747 } else if (src && try_other_direction) {
1748 if (!src->ConvertPointForAncestor(dst, point)) {
1749 // |src| is not an ancestor of |dst|, and |dst| is not an ancestor of
1750 // |src| either. At this stage, |point| is in the widget's coordinate
[email protected]c069fb032011-03-09 02:26:541751 // system. So convert from the widget's to |dst|'s coordinate system now.
[email protected]36df22b2011-02-24 21:47:561752 ConvertPointFromWidget(dst, point);
1753 }
[email protected]2df3cc92011-02-05 00:38:591754 }
1755}
1756
[email protected]2c831b22011-03-14 23:17:201757bool View::ConvertPointForAncestor(const View* ancestor,
1758 gfx::Point* point) const {
[email protected]b9b1e7a42011-05-17 15:29:511759 ui::Transform trans;
[email protected]2c831b22011-03-14 23:17:201760 // TODO(sad): Have some way of caching the transformation results.
[email protected]b9b1e7a42011-05-17 15:29:511761 bool result = GetTransformRelativeTo(ancestor, &trans);
[email protected]80248e32011-07-08 15:31:111762 gfx::Point3f p(*point);
1763 trans.TransformPoint(p);
1764 *point = p.AsPoint();
[email protected]b9b1e7a42011-05-17 15:29:511765 return result;
[email protected]2c831b22011-03-14 23:17:201766}
1767
1768bool View::ConvertPointFromAncestor(const View* ancestor,
1769 gfx::Point* point) const {
[email protected]b9b1e7a42011-05-17 15:29:511770 ui::Transform trans;
1771 bool result = GetTransformRelativeTo(ancestor, &trans);
[email protected]80248e32011-07-08 15:31:111772 gfx::Point3f p(*point);
1773 trans.TransformPointReverse(p);
1774 *point = p.AsPoint();
[email protected]b9b1e7a42011-05-17 15:29:511775 return result;
[email protected]2c831b22011-03-14 23:17:201776}
1777
[email protected]c36b0bf2011-05-10 18:26:251778// Accelerated painting --------------------------------------------------------
1779
[email protected]adc93fa72011-06-21 19:47:391780void View::CreateLayer() {
[email protected]25ae9a12011-10-12 14:55:221781 // A new layer is being created for the view. So all the layers of the
1782 // sub-tree can inherit the visibility of the corresponding view.
1783 for (int i = 0, count = child_count(); i < count; ++i)
1784 child_at(i)->UpdateChildLayerVisibility(true);
1785
[email protected]18dab372011-10-03 21:21:441786 layer_.reset(new ui::Layer(NULL));
1787 layer_->set_delegate(this);
1788 if (layer_property_setter_.get())
1789 layer_property_setter_->Installed(layer());
1790 else
1791 SetLayerPropertySetter(NULL);
[email protected]adc93fa72011-06-21 19:47:391792
[email protected]18dab372011-10-03 21:21:441793 UpdateParentLayers();
[email protected]25ae9a12011-10-12 14:55:221794 UpdateLayerVisibility();
[email protected]18da0fd2011-10-07 16:25:311795
1796 // The new layer needs to be ordered in the layer tree according
1797 // to the view tree. Children of this layer were added in order
1798 // in UpdateParentLayers().
1799 if (parent())
1800 parent()->ReorderLayers();
[email protected]adc93fa72011-06-21 19:47:391801}
1802
[email protected]18dab372011-10-03 21:21:441803void View::UpdateParentLayers() {
1804 // Attach all top-level un-parented layers.
1805 if (layer() && !layer()->parent()) {
1806 UpdateParentLayer();
1807 } else {
1808 for (int i = 0, count = child_count(); i < count; ++i)
1809 child_at(i)->UpdateParentLayers();
1810 }
1811}
1812
1813void View::UpdateParentLayer() {
[email protected]40500ba2011-09-30 23:39:511814 if (!layer())
1815 return;
1816
[email protected]18dab372011-10-03 21:21:441817 ui::Layer* parent_layer = NULL;
1818 gfx::Point offset(x(), y());
[email protected]06061b492011-10-03 21:48:491819
1820 // TODO(sad): The NULL check here for parent_ essentially is to check if this
1821 // is the RootView. Instead of doing this, this function should be made
1822 // virtual and overridden from the RootView.
[email protected]18dab372011-10-03 21:21:441823 if (parent_)
1824 parent_->CalculateOffsetToAncestorWithLayer(&offset, &parent_layer);
[email protected]06061b492011-10-03 21:48:491825 else if (!parent_ && GetWidget())
1826 GetWidget()->CalculateOffsetToAncestorWithLayer(&offset, &parent_layer);
[email protected]7c4b1b92011-06-27 16:37:181827
[email protected]18dab372011-10-03 21:21:441828 ReparentLayer(offset, parent_layer);
1829}
[email protected]6fb3c272011-09-29 20:36:291830
[email protected]06061b492011-10-03 21:48:491831void View::OrphanLayers() {
1832 if (layer()) {
1833 if (layer()->parent())
1834 layer()->parent()->Remove(layer());
1835
1836 // The layer belonging to this View has already been orphaned. It is not
1837 // necessary to orphan the child layers.
1838 return;
1839 }
1840 for (int i = 0, count = child_count(); i < count; ++i)
1841 child_at(i)->OrphanLayers();
1842}
1843
[email protected]18dab372011-10-03 21:21:441844void View::ReparentLayer(const gfx::Point& offset, ui::Layer* parent_layer) {
1845 layer_->SetBounds(gfx::Rect(offset.x(), offset.y(), width(), height()));
1846 DCHECK_NE(layer(), parent_layer);
1847 if (parent_layer)
1848 parent_layer->Add(layer());
1849 layer_->SchedulePaint(GetLocalBounds());
1850 MoveLayerToParent(layer(), gfx::Point());
[email protected]40500ba2011-09-30 23:39:511851}
[email protected]6fb3c272011-09-29 20:36:291852
[email protected]40500ba2011-09-30 23:39:511853void View::DestroyLayer() {
[email protected]18dab372011-10-03 21:21:441854 ui::Layer* new_parent = layer()->parent();
1855 std::vector<ui::Layer*> children = layer()->children();
1856 for (size_t i = 0; i < children.size(); ++i) {
1857 layer()->Remove(children[i]);
1858 if (new_parent)
1859 new_parent->Add(children[i]);
1860 }
[email protected]40500ba2011-09-30 23:39:511861
[email protected]18dab372011-10-03 21:21:441862 if (layer_property_setter_.get())
1863 layer_property_setter_->Uninstalled(layer());
1864
1865 layer_.reset();
1866
[email protected]18da0fd2011-10-07 16:25:311867 if (new_parent)
1868 ReorderLayers();
1869
[email protected]18dab372011-10-03 21:21:441870 gfx::Point offset;
1871 CalculateOffsetToAncestorWithLayer(&offset, NULL);
1872 UpdateChildLayerBounds(offset);
1873
1874 SchedulePaint();
[email protected]adc93fa72011-06-21 19:47:391875}
1876
[email protected]2df3cc92011-02-05 00:38:591877// Input -----------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:291878
[email protected]2c831b22011-03-14 23:17:201879bool View::ProcessMousePressed(const MouseEvent& event, DragInfo* drag_info) {
[email protected]83548a42010-06-18 13:53:371880 const bool enabled = IsEnabled();
[email protected]e9adf0702010-03-08 23:34:071881 int drag_operations =
[email protected]2c831b22011-03-14 23:17:201882 (enabled && event.IsOnlyLeftMouseButton() && HitTest(event.location())) ?
1883 GetDragOperations(event.location()) : 0;
1884 ContextMenuController* context_menu_controller = event.IsRightMouseButton() ?
[email protected]e9adf0702010-03-08 23:34:071885 context_menu_controller_ : 0;
initial.commit09911bf2008-07-26 23:55:291886
[email protected]2c831b22011-03-14 23:17:201887 const bool result = OnMousePressed(event);
[email protected]c069fb032011-03-09 02:26:541888 // WARNING: we may have been deleted, don't use any View variables.
initial.commit09911bf2008-07-26 23:55:291889
1890 if (!enabled)
1891 return result;
1892
[email protected]02bae0f2011-01-19 20:22:001893 if (drag_operations != ui::DragDropTypes::DRAG_NONE) {
[email protected]2c831b22011-03-14 23:17:201894 drag_info->PossibleDrag(event.location());
initial.commit09911bf2008-07-26 23:55:291895 return true;
1896 }
1897 return !!context_menu_controller || result;
1898}
1899
[email protected]2c831b22011-03-14 23:17:201900bool View::ProcessMouseDragged(const MouseEvent& event, DragInfo* drag_info) {
initial.commit09911bf2008-07-26 23:55:291901 // Copy the field, that way if we're deleted after drag and drop no harm is
1902 // done.
1903 ContextMenuController* context_menu_controller = context_menu_controller_;
1904 const bool possible_drag = drag_info->possible_drag;
[email protected]2c831b22011-03-14 23:17:201905 if (possible_drag && ExceededDragThreshold(
1906 drag_info->start_pt.x() - event.x(),
1907 drag_info->start_pt.y() - event.y())) {
[email protected]b5f94de2009-12-04 07:59:001908 if (!drag_controller_ ||
[email protected]c3fac082011-03-04 01:04:181909 drag_controller_->CanStartDragForView(
[email protected]2c831b22011-03-14 23:17:201910 this, drag_info->start_pt, event.location()))
1911 DoDrag(event, drag_info->start_pt);
initial.commit09911bf2008-07-26 23:55:291912 } else {
[email protected]2c831b22011-03-14 23:17:201913 if (OnMouseDragged(event))
initial.commit09911bf2008-07-26 23:55:291914 return true;
1915 // Fall through to return value based on context menu controller.
1916 }
1917 // WARNING: we may have been deleted.
1918 return (context_menu_controller != NULL) || possible_drag;
1919}
1920
[email protected]34338252011-03-29 00:39:051921void View::ProcessMouseReleased(const MouseEvent& event) {
1922 if (context_menu_controller_ && event.IsOnlyRightMouseButton()) {
initial.commit09911bf2008-07-26 23:55:291923 // Assume that if there is a context menu controller we won't be deleted
1924 // from mouse released.
[email protected]2c831b22011-03-14 23:17:201925 gfx::Point location(event.location());
[email protected]34338252011-03-29 00:39:051926 OnMouseReleased(event);
[email protected]464fdb32009-03-19 20:25:441927 if (HitTest(location)) {
1928 ConvertPointToScreen(this, &location);
[email protected]e9adf0702010-03-08 23:34:071929 ShowContextMenu(location, true);
[email protected]464fdb32009-03-19 20:25:441930 }
initial.commit09911bf2008-07-26 23:55:291931 } else {
[email protected]34338252011-03-29 00:39:051932 OnMouseReleased(event);
initial.commit09911bf2008-07-26 23:55:291933 }
1934 // WARNING: we may have been deleted.
1935}
1936
[email protected]6a4056b2011-06-14 22:09:171937ui::TouchStatus View::ProcessTouchEvent(const TouchEvent& event) {
[email protected]18dab372011-10-03 21:21:441938 // TODO(rjkroege): Implement a grab scheme similar to as as is found in
1939 // MousePressed.
[email protected]2c831b22011-03-14 23:17:201940 return OnTouchEvent(event);
[email protected]ad84c8f2010-09-08 14:06:101941}
[email protected]ad84c8f2010-09-08 14:06:101942
[email protected]2df3cc92011-02-05 00:38:591943// Accelerators ----------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:291944
[email protected]71421c3f2009-06-06 00:41:441945void View::RegisterPendingAccelerators() {
1946 if (!accelerators_.get() ||
1947 registered_accelerator_count_ == accelerators_->size()) {
1948 // No accelerators are waiting for registration.
initial.commit09911bf2008-07-26 23:55:291949 return;
[email protected]71421c3f2009-06-06 00:41:441950 }
initial.commit09911bf2008-07-26 23:55:291951
[email protected]72f17242011-02-18 21:05:551952 if (!GetWidget()) {
1953 // The view is not yet attached to a widget, defer registration until then.
initial.commit09911bf2008-07-26 23:55:291954 return;
1955 }
[email protected]f3735c5d2009-03-19 17:26:231956
[email protected]bda9556c2010-01-07 00:55:161957 accelerator_focus_manager_ = GetFocusManager();
1958 if (!accelerator_focus_manager_) {
initial.commit09911bf2008-07-26 23:55:291959 // Some crash reports seem to show that we may get cases where we have no
1960 // focus manager (see bug #1291225). This should never be the case, just
1961 // making sure we don't crash.
[email protected]f8dce002009-09-10 21:07:041962
[email protected]2fc66722011-05-19 14:43:121963 // TODO(jcampan): This fails for a view under NativeWidgetGtk with
1964 // TYPE_CHILD. (see http://crbug.com/21335) reenable
1965 // NOTREACHED assertion and verify accelerators works as
1966 // expected.
[email protected]f8dce002009-09-10 21:07:041967#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291968 NOTREACHED();
[email protected]f8dce002009-09-10 21:07:041969#endif
initial.commit09911bf2008-07-26 23:55:291970 return;
1971 }
[email protected]37e6e612011-02-02 18:13:071972 // Only register accelerators if we are visible.
1973 if (!IsVisibleInRootView())
1974 return;
[email protected]afed4572011-06-09 18:00:111975 for (std::vector<Accelerator>::const_iterator i(
1976 accelerators_->begin() + registered_accelerator_count_);
1977 i != accelerators_->end(); ++i) {
1978 accelerator_focus_manager_->RegisterAccelerator(*i, this);
initial.commit09911bf2008-07-26 23:55:291979 }
[email protected]71421c3f2009-06-06 00:41:441980 registered_accelerator_count_ = accelerators_->size();
initial.commit09911bf2008-07-26 23:55:291981}
1982
[email protected]bda9556c2010-01-07 00:55:161983void View::UnregisterAccelerators(bool leave_data_intact) {
initial.commit09911bf2008-07-26 23:55:291984 if (!accelerators_.get())
1985 return;
1986
[email protected]72f17242011-02-18 21:05:551987 if (GetWidget()) {
[email protected]bda9556c2010-01-07 00:55:161988 if (accelerator_focus_manager_) {
initial.commit09911bf2008-07-26 23:55:291989 // We may not have a FocusManager if the window containing us is being
1990 // closed, in which case the FocusManager is being deleted so there is
1991 // nothing to unregister.
[email protected]bda9556c2010-01-07 00:55:161992 accelerator_focus_manager_->UnregisterAccelerators(this);
1993 accelerator_focus_manager_ = NULL;
initial.commit09911bf2008-07-26 23:55:291994 }
[email protected]bda9556c2010-01-07 00:55:161995 if (!leave_data_intact) {
1996 accelerators_->clear();
1997 accelerators_.reset();
1998 }
[email protected]71421c3f2009-06-06 00:41:441999 registered_accelerator_count_ = 0;
initial.commit09911bf2008-07-26 23:55:292000 }
2001}
2002
[email protected]2df3cc92011-02-05 00:38:592003// Focus -----------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:292004
[email protected]2df3cc92011-02-05 00:38:592005void View::InitFocusSiblings(View* v, int index) {
[email protected]39a0f0c62011-06-09 16:27:182006 int count = child_count();
initial.commit09911bf2008-07-26 23:55:292007
[email protected]39a0f0c62011-06-09 16:27:182008 if (count == 0) {
[email protected]2df3cc92011-02-05 00:38:592009 v->next_focusable_view_ = NULL;
2010 v->previous_focusable_view_ = NULL;
initial.commit09911bf2008-07-26 23:55:292011 } else {
[email protected]39a0f0c62011-06-09 16:27:182012 if (index == count) {
[email protected]2df3cc92011-02-05 00:38:592013 // We are inserting at the end, but the end of the child list may not be
2014 // the last focusable element. Let's try to find an element with no next
2015 // focusable element to link to.
2016 View* last_focusable_view = NULL;
[email protected]695e942e2011-06-06 15:15:512017 for (Views::iterator i(children_.begin()); i != children_.end(); ++i) {
2018 if (!(*i)->next_focusable_view_) {
2019 last_focusable_view = *i;
[email protected]2df3cc92011-02-05 00:38:592020 break;
2021 }
initial.commit09911bf2008-07-26 23:55:292022 }
[email protected]2df3cc92011-02-05 00:38:592023 if (last_focusable_view == NULL) {
2024 // Hum... there is a cycle in the focus list. Let's just insert ourself
2025 // after the last child.
[email protected]20838602011-02-09 04:50:212026 View* prev = children_[index - 1];
[email protected]2df3cc92011-02-05 00:38:592027 v->previous_focusable_view_ = prev;
2028 v->next_focusable_view_ = prev->next_focusable_view_;
2029 prev->next_focusable_view_->previous_focusable_view_ = v;
2030 prev->next_focusable_view_ = v;
2031 } else {
2032 last_focusable_view->next_focusable_view_ = v;
2033 v->next_focusable_view_ = NULL;
2034 v->previous_focusable_view_ = last_focusable_view;
2035 }
2036 } else {
[email protected]20838602011-02-09 04:50:212037 View* prev = children_[index]->GetPreviousFocusableView();
[email protected]2df3cc92011-02-05 00:38:592038 v->previous_focusable_view_ = prev;
[email protected]20838602011-02-09 04:50:212039 v->next_focusable_view_ = children_[index];
[email protected]2df3cc92011-02-05 00:38:592040 if (prev)
2041 prev->next_focusable_view_ = v;
[email protected]20838602011-02-09 04:50:212042 children_[index]->previous_focusable_view_ = v;
initial.commit09911bf2008-07-26 23:55:292043 }
2044 }
2045}
2046
[email protected]2df3cc92011-02-05 00:38:592047// System events ---------------------------------------------------------------
[email protected]96b667d2008-10-14 20:58:442048
[email protected]2df3cc92011-02-05 00:38:592049void View::PropagateThemeChanged() {
[email protected]20838602011-02-09 04:50:212050 for (int i = child_count() - 1; i >= 0; --i)
[email protected]fe84b912011-07-15 17:13:332051 child_at(i)->PropagateThemeChanged();
[email protected]2df3cc92011-02-05 00:38:592052 OnThemeChanged();
initial.commit09911bf2008-07-26 23:55:292053}
2054
[email protected]2df3cc92011-02-05 00:38:592055void View::PropagateLocaleChanged() {
[email protected]20838602011-02-09 04:50:212056 for (int i = child_count() - 1; i >= 0; --i)
[email protected]fe84b912011-07-15 17:13:332057 child_at(i)->PropagateLocaleChanged();
[email protected]2df3cc92011-02-05 00:38:592058 OnLocaleChanged();
initial.commit09911bf2008-07-26 23:55:292059}
2060
[email protected]2df3cc92011-02-05 00:38:592061// Tooltips --------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:292062
2063void View::UpdateTooltip() {
[email protected]a0dde122008-11-21 20:51:202064 Widget* widget = GetWidget();
[email protected]9861ae92011-03-03 20:15:032065 // TODO(beng): The TooltipManager NULL check can be removed when we
2066 // consolidate Init() methods and make views_unittests Init() all
2067 // Widgets that it uses.
[email protected]2f2b57b2011-06-16 17:21:232068 if (widget && widget->native_widget_private()->GetTooltipManager())
2069 widget->native_widget_private()->GetTooltipManager()->UpdateTooltip();
initial.commit09911bf2008-07-26 23:55:292070}
2071
[email protected]2df3cc92011-02-05 00:38:592072// Drag and drop ---------------------------------------------------------------
2073
[email protected]2c831b22011-03-14 23:17:202074void View::DoDrag(const MouseEvent& event, const gfx::Point& press_pt) {
[email protected]2df3cc92011-02-05 00:38:592075 int drag_operations = GetDragOperations(press_pt);
2076 if (drag_operations == ui::DragDropTypes::DRAG_NONE)
2077 return;
2078
2079 OSExchangeData data;
2080 WriteDragData(press_pt, &data);
2081
2082 // Message the RootView to do the drag and drop. That way if we're removed
2083 // the RootView can detect it and avoid calling us back.
[email protected]9861ae92011-03-03 20:15:032084 GetWidget()->RunShellDrag(this, data, drag_operations);
initial.commit09911bf2008-07-26 23:55:292085}
2086
[email protected]2df3cc92011-02-05 00:38:592087} // namespace views