blob: 02d2766dee5e41195f6eab732c69fa7f05d49b68 [file] [log] [blame]
[email protected]8af4c1992010-02-04 21:38:071// Copyright (c) 2010 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>
[email protected]1eb89e82008-08-15 12:27:038#ifndef NDEBUG
initial.commit09911bf2008-07-26 23:55:299#include <iostream>
[email protected]1eb89e82008-08-15 12:27:0310#endif
initial.commit09911bf2008-07-26 23:55:2911
[email protected]37126212009-05-06 02:23:3112#include "app/drag_drop_types.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/logging.h"
14#include "base/message_loop.h"
[email protected]82739cf2008-09-16 00:37:5615#include "base/scoped_handle.h"
[email protected]d55194ca2010-03-11 18:25:4516#include "base/utf_string_conversions.h"
[email protected]74db48eb2010-06-25 16:33:2317#include "gfx/canvas_skia.h"
[email protected]5c7293a2010-03-17 06:40:5718#include "gfx/path.h"
[email protected]d5282e72009-05-13 13:16:5219#include "third_party/skia/include/core/SkShader.h"
[email protected]2362e4f2009-05-08 00:34:0520#include "views/background.h"
21#include "views/layout_manager.h"
[email protected]3ee83f2c2009-05-10 05:58:4022#include "views/views_delegate.h"
[email protected]2362e4f2009-05-08 00:34:0523#include "views/widget/root_view.h"
[email protected]319d4ae2009-05-28 19:09:4524#include "views/widget/tooltip_manager.h"
[email protected]2362e4f2009-05-08 00:34:0525#include "views/widget/widget.h"
26#include "views/window/window.h"
[email protected]031e4d32009-12-29 01:13:2327
[email protected]6ff244f2009-01-20 20:38:0828#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0529#include "views/accessibility/view_accessibility_wrapper.h"
[email protected]6ff244f2009-01-20 20:38:0830#endif
[email protected]031e4d32009-12-29 01:13:2331#if defined(OS_LINUX)
32#include "app/scoped_handle_gtk.h"
33#endif
initial.commit09911bf2008-07-26 23:55:2934
[email protected]c2dacc92008-10-16 23:51:3835namespace views {
initial.commit09911bf2008-07-26 23:55:2936
37// static
[email protected]3ee83f2c2009-05-10 05:58:4038ViewsDelegate* ViewsDelegate::views_delegate = NULL;
39
40// static
[email protected]2362e4f2009-05-08 00:34:0541char View::kViewClassName[] = "views/View";
initial.commit09911bf2008-07-26 23:55:2942
[email protected]8af4c1992010-02-04 21:38:0743// static
44const int View::kShowFolderDropMenuDelay = 400;
45
initial.commit09911bf2008-07-26 23:55:2946/////////////////////////////////////////////////////////////////////////////
47//
48// View - constructors, destructors, initialization
49//
50/////////////////////////////////////////////////////////////////////////////
51
52View::View()
53 : id_(0),
54 group_(-1),
[email protected]6ff244f2009-01-20 20:38:0855 enabled_(true),
56 focusable_(false),
[email protected]83548a42010-06-18 13:53:3757 accessibility_focusable_(false),
[email protected]4f3dc372009-02-24 00:10:2958 bounds_(0, 0, 0, 0),
[email protected]9ea053e2010-07-15 08:19:0559 needs_layout_(true),
initial.commit09911bf2008-07-26 23:55:2960 parent_(NULL),
initial.commit09911bf2008-07-26 23:55:2961 is_visible_(true),
initial.commit09911bf2008-07-26 23:55:2962 is_parent_owned_(true),
63 notify_when_visible_bounds_in_root_changes_(false),
64 registered_for_visible_bounds_notification_(false),
[email protected]bda9556c2010-01-07 00:55:1665 accelerator_registration_delayed_(false),
initial.commit09911bf2008-07-26 23:55:2966 next_focusable_view_(NULL),
67 previous_focusable_view_(NULL),
[email protected]bda9556c2010-01-07 00:55:1668 accelerator_focus_manager_(NULL),
[email protected]71421c3f2009-06-06 00:41:4469 registered_accelerator_count_(0),
initial.commit09911bf2008-07-26 23:55:2970 context_menu_controller_(NULL),
[email protected]6ff244f2009-01-20 20:38:0871#if defined(OS_WIN)
72 accessibility_(NULL),
73#endif
initial.commit09911bf2008-07-26 23:55:2974 drag_controller_(NULL),
initial.commit09911bf2008-07-26 23:55:2975 flip_canvas_on_paint_for_rtl_ui_(false) {
76}
77
78View::~View() {
[email protected]8c57de22010-03-12 21:06:0479 if (parent_)
80 parent_->RemoveChildView(this);
81
initial.commit09911bf2008-07-26 23:55:2982 int c = static_cast<int>(child_views_.size());
83 while (--c >= 0) {
[email protected]8c57de22010-03-12 21:06:0484 child_views_[c]->SetParent(NULL);
initial.commit09911bf2008-07-26 23:55:2985 if (child_views_[c]->IsParentOwned())
86 delete child_views_[c];
initial.commit09911bf2008-07-26 23:55:2987 }
[email protected]a64f33902009-10-10 05:41:2388
89#if defined(OS_WIN)
[email protected]09fe9492009-11-07 02:23:0690 if (accessibility_.get())
[email protected]a64f33902009-10-10 05:41:2391 accessibility_->Uninitialize();
[email protected]a64f33902009-10-10 05:41:2392#endif
initial.commit09911bf2008-07-26 23:55:2993}
94
95/////////////////////////////////////////////////////////////////////////////
96//
97// View - sizing
98//
99/////////////////////////////////////////////////////////////////////////////
100
[email protected]0d8ea702008-10-14 17:03:07101gfx::Rect View::GetBounds(PositionMirroringSettings settings) const {
102 gfx::Rect bounds(bounds_);
initial.commit09911bf2008-07-26 23:55:29103
104 // If the parent uses an RTL UI layout and if we are asked to transform the
105 // bounds to their mirrored position if necessary, then we should shift the
106 // rectangle appropriately.
[email protected]0d8ea702008-10-14 17:03:07107 if (settings == APPLY_MIRRORING_TRANSFORMATION)
108 bounds.set_x(MirroredX());
[email protected]4f3dc372009-02-24 00:10:29109
[email protected]0d8ea702008-10-14 17:03:07110 return bounds;
initial.commit09911bf2008-07-26 23:55:29111}
112
[email protected]6f3bb6c2008-09-17 22:25:33113// y(), width() and height() are agnostic to the RTL UI layout of the
114// parent view. x(), on the other hand, is not.
initial.commit09911bf2008-07-26 23:55:29115int View::GetX(PositionMirroringSettings settings) const {
[email protected]80f8b9f2008-10-16 18:17:47116 return settings == IGNORE_MIRRORING_TRANSFORMATION ? x() : MirroredX();
initial.commit09911bf2008-07-26 23:55:29117}
118
[email protected]80f8b9f2008-10-16 18:17:47119void View::SetBounds(const gfx::Rect& bounds) {
120 if (bounds == bounds_)
initial.commit09911bf2008-07-26 23:55:29121 return;
initial.commit09911bf2008-07-26 23:55:29122
[email protected]80f8b9f2008-10-16 18:17:47123 gfx::Rect prev = bounds_;
initial.commit09911bf2008-07-26 23:55:29124 bounds_ = bounds;
[email protected]b97c8a22010-02-05 19:36:43125 bool size_changed = prev.size() != bounds_.size();
126 bool position_changed = prev.origin() != bounds_.origin();
initial.commit09911bf2008-07-26 23:55:29127
[email protected]b97c8a22010-02-05 19:36:43128 if (size_changed || position_changed) {
129 DidChangeBounds(prev, bounds_);
130
131 RootView* root = GetRootView();
132 if (root)
initial.commit09911bf2008-07-26 23:55:29133 root->ViewBoundsChanged(this, size_changed, position_changed);
134 }
135}
136
[email protected]80f8b9f2008-10-16 18:17:47137gfx::Rect View::GetLocalBounds(bool include_border) const {
[email protected]9a3f0ac22008-11-14 03:24:02138 if (include_border || !border_.get())
[email protected]80f8b9f2008-10-16 18:17:47139 return gfx::Rect(0, 0, width(), height());
initial.commit09911bf2008-07-26 23:55:29140
[email protected]80f8b9f2008-10-16 18:17:47141 gfx::Insets insets;
142 border_->GetInsets(&insets);
143 return gfx::Rect(insets.left(), insets.top(),
[email protected]4a8d3272009-03-10 19:15:08144 std::max(0, width() - insets.width()),
145 std::max(0, height() - insets.height()));
initial.commit09911bf2008-07-26 23:55:29146}
147
[email protected]0a1d36b22008-10-17 19:33:09148gfx::Point View::GetPosition() const {
149 return gfx::Point(GetX(APPLY_MIRRORING_TRANSFORMATION), y());
initial.commit09911bf2008-07-26 23:55:29150}
151
[email protected]154f8bc2008-10-15 18:02:30152gfx::Size View::GetPreferredSize() {
153 if (layout_manager_.get())
154 return layout_manager_->GetPreferredSize(this);
155 return gfx::Size();
initial.commit09911bf2008-07-26 23:55:29156}
157
[email protected]a1360162009-11-30 21:19:07158int View::GetBaseline() {
159 return -1;
160}
161
initial.commit09911bf2008-07-26 23:55:29162void View::SizeToPreferredSize() {
[email protected]154f8bc2008-10-15 18:02:30163 gfx::Size prefsize = GetPreferredSize();
164 if ((prefsize.width() != width()) || (prefsize.height() != height()))
165 SetBounds(x(), y(), prefsize.width(), prefsize.height());
initial.commit09911bf2008-07-26 23:55:29166}
167
[email protected]7ccc52b72009-05-08 21:09:11168void View::PreferredSizeChanged() {
[email protected]9ea053e2010-07-15 08:19:05169 InvalidateLayout();
[email protected]7ccc52b72009-05-08 21:09:11170 if (parent_)
171 parent_->ChildPreferredSizeChanged(this);
172}
173
[email protected]154f8bc2008-10-15 18:02:30174gfx::Size View::GetMinimumSize() {
175 return GetPreferredSize();
initial.commit09911bf2008-07-26 23:55:29176}
177
178int View::GetHeightForWidth(int w) {
179 if (layout_manager_.get())
180 return layout_manager_->GetPreferredHeightForWidth(this, w);
[email protected]154f8bc2008-10-15 18:02:30181 return GetPreferredSize().height();
initial.commit09911bf2008-07-26 23:55:29182}
183
[email protected]80f8b9f2008-10-16 18:17:47184void View::DidChangeBounds(const gfx::Rect& previous,
185 const gfx::Rect& current) {
[email protected]9ea053e2010-07-15 08:19:05186 needs_layout_ = false;
[email protected]80f8b9f2008-10-16 18:17:47187 Layout();
initial.commit09911bf2008-07-26 23:55:29188}
189
[email protected]e9adf0702010-03-08 23:34:07190void View::ScrollRectToVisible(const gfx::Rect& rect) {
initial.commit09911bf2008-07-26 23:55:29191 View* parent = GetParent();
192
193 // We must take RTL UI mirroring into account when adjusting the position of
194 // the region.
[email protected]e9adf0702010-03-08 23:34:07195 if (parent) {
196 gfx::Rect scroll_rect(rect);
197 scroll_rect.Offset(GetX(APPLY_MIRRORING_TRANSFORMATION), y());
198 parent->ScrollRectToVisible(scroll_rect);
199 }
initial.commit09911bf2008-07-26 23:55:29200}
201
202/////////////////////////////////////////////////////////////////////////////
203//
204// View - layout
205//
206/////////////////////////////////////////////////////////////////////////////
207
208void View::Layout() {
[email protected]9ea053e2010-07-15 08:19:05209 needs_layout_ = false;
210
[email protected]f8617192010-07-05 07:57:10211 // If we have a layout manager, let it handle the layout for us.
initial.commit09911bf2008-07-26 23:55:29212 if (layout_manager_.get()) {
213 layout_manager_->Layout(this);
214 SchedulePaint();
215 }
216
[email protected]9ea053e2010-07-15 08:19:05217 // Make sure to propagate the Layout() call to any children that haven't
218 // received it yet through the layout manager and need to be laid out. This
219 // is needed for the case when the child requires a layout but its bounds
220 // weren't changed by the layout manager. If there is no layout manager, we
221 // just propagate the Layout() call down the hierarchy, so whoever receives
222 // the call can take appropriate action.
[email protected]09fe9492009-11-07 02:23:06223 for (int i = 0, count = GetChildViewCount(); i < count; ++i) {
initial.commit09911bf2008-07-26 23:55:29224 View* child = GetChildViewAt(i);
[email protected]9ea053e2010-07-15 08:19:05225 if (child->needs_layout_ || !layout_manager_.get()) {
226 child->needs_layout_ = false;
227 child->Layout();
228 }
initial.commit09911bf2008-07-26 23:55:29229 }
230}
231
[email protected]9ea053e2010-07-15 08:19:05232void View::InvalidateLayout() {
233 if (needs_layout_)
234 return;
235 needs_layout_ = true;
236 if (parent_)
237 parent_->InvalidateLayout();
238}
239
initial.commit09911bf2008-07-26 23:55:29240LayoutManager* View::GetLayoutManager() const {
241 return layout_manager_.get();
242}
243
244void View::SetLayoutManager(LayoutManager* layout_manager) {
[email protected]09fe9492009-11-07 02:23:06245 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29246 layout_manager_->Uninstalled(this);
[email protected]09fe9492009-11-07 02:23:06247
initial.commit09911bf2008-07-26 23:55:29248 layout_manager_.reset(layout_manager);
[email protected]09fe9492009-11-07 02:23:06249 if (layout_manager_.get())
initial.commit09911bf2008-07-26 23:55:29250 layout_manager_->Installed(this);
initial.commit09911bf2008-07-26 23:55:29251}
252
253////////////////////////////////////////////////////////////////////////////////
254//
255// View - Right-to-left UI layout
256//
257////////////////////////////////////////////////////////////////////////////////
258
[email protected]c3bca422010-02-19 23:29:02259int View::MirroredX() const {
initial.commit09911bf2008-07-26 23:55:29260 View* parent = GetParent();
[email protected]aaa71da52010-02-19 21:39:24261 return parent ? parent->MirroredLeftPointForRect(bounds_) : x();
initial.commit09911bf2008-07-26 23:55:29262}
263
264int View::MirroredLeftPointForRect(const gfx::Rect& bounds) const {
[email protected]c2f4bdb72010-05-10 23:15:21265 return base::i18n::IsRTL() ?
[email protected]aaa71da52010-02-19 21:39:24266 (width() - bounds.x() - bounds.width()) : bounds.x();
initial.commit09911bf2008-07-26 23:55:29267}
268
269////////////////////////////////////////////////////////////////////////////////
270//
271// View - states
272//
273////////////////////////////////////////////////////////////////////////////////
274
275bool View::IsEnabled() const {
276 return enabled_;
277}
278
279void View::SetEnabled(bool state) {
280 if (enabled_ != state) {
281 enabled_ = state;
282 SchedulePaint();
283 }
284}
285
initial.commit09911bf2008-07-26 23:55:29286void View::SetFocusable(bool focusable) {
287 focusable_ = focusable;
288}
289
[email protected]59461d22010-07-21 15:41:09290bool View::IsFocusableInRootView() const {
291 return IsFocusable() && IsVisibleInRootView();
292}
293
294bool View::IsAccessibilityFocusableInRootView() const {
[email protected]1aa36552010-07-14 08:21:25295 return (focusable_ || accessibility_focusable_) && IsEnabled() &&
296 IsVisibleInRootView();
[email protected]83548a42010-06-18 13:53:37297}
298
[email protected]82166b62009-06-30 18:48:00299FocusManager* View::GetFocusManager() {
300 Widget* widget = GetWidget();
301 return widget ? widget->GetFocusManager() : NULL;
302}
303
initial.commit09911bf2008-07-26 23:55:29304bool View::HasFocus() {
initial.commit09911bf2008-07-26 23:55:29305 FocusManager* focus_manager = GetFocusManager();
306 if (focus_manager)
307 return focus_manager->GetFocusedView() == this;
308 return false;
309}
310
[email protected]5c9e97a2009-09-09 23:48:30311void View::Focus() {
[email protected]7f14e1f2009-10-01 15:57:26312 // By default, we clear the native focus. This ensures that no visible native
313 // view as the focus and that we still receive keyboard inputs.
[email protected]5c9e97a2009-09-09 23:48:30314 FocusManager* focus_manager = GetFocusManager();
315 if (focus_manager)
[email protected]7f14e1f2009-10-01 15:57:26316 focus_manager->ClearNativeFocus();
[email protected]6a054ffa2010-08-06 07:07:18317
318 // Notify assistive technologies of the focus change.
319 NotifyAccessibilityEvent(AccessibilityTypes::EVENT_FOCUS);
320}
321
322void View::NotifyAccessibilityEvent(AccessibilityTypes::Event event_type) {
323 NotifyAccessibilityEvent(event_type, true);
[email protected]5c9e97a2009-09-09 23:48:30324}
325
initial.commit09911bf2008-07-26 23:55:29326void View::SetHotTracked(bool flag) {
327}
328
329/////////////////////////////////////////////////////////////////////////////
330//
331// View - painting
332//
333/////////////////////////////////////////////////////////////////////////////
334
[email protected]0a1d36b22008-10-17 19:33:09335void View::SchedulePaint(const gfx::Rect& r, bool urgent) {
336 if (!IsVisible())
initial.commit09911bf2008-07-26 23:55:29337 return;
initial.commit09911bf2008-07-26 23:55:29338
339 if (parent_) {
340 // Translate the requested paint rect to the parent's coordinate system
341 // then pass this notification up to the parent.
[email protected]0a1d36b22008-10-17 19:33:09342 gfx::Rect paint_rect = r;
343 paint_rect.Offset(GetPosition());
initial.commit09911bf2008-07-26 23:55:29344 parent_->SchedulePaint(paint_rect, urgent);
345 }
346}
347
348void View::SchedulePaint() {
[email protected]0a1d36b22008-10-17 19:33:09349 SchedulePaint(GetLocalBounds(true), false);
initial.commit09911bf2008-07-26 23:55:29350}
351
[email protected]82522512009-05-15 07:37:29352void View::Paint(gfx::Canvas* canvas) {
initial.commit09911bf2008-07-26 23:55:29353 PaintBackground(canvas);
354 PaintFocusBorder(canvas);
355 PaintBorder(canvas);
356}
357
[email protected]82522512009-05-15 07:37:29358void View::PaintBackground(gfx::Canvas* canvas) {
[email protected]9a3f0ac22008-11-14 03:24:02359 if (background_.get())
initial.commit09911bf2008-07-26 23:55:29360 background_->Paint(canvas, this);
361}
362
[email protected]82522512009-05-15 07:37:29363void View::PaintBorder(gfx::Canvas* canvas) {
[email protected]9a3f0ac22008-11-14 03:24:02364 if (border_.get())
initial.commit09911bf2008-07-26 23:55:29365 border_->Paint(*this, canvas);
366}
367
[email protected]82522512009-05-15 07:37:29368void View::PaintFocusBorder(gfx::Canvas* canvas) {
[email protected]59461d22010-07-21 15:41:09369 if (HasFocus() && (IsFocusable() || IsAccessibilityFocusableInRootView()))
[email protected]6f3bb6c2008-09-17 22:25:33370 canvas->DrawFocusRect(0, 0, width(), height());
initial.commit09911bf2008-07-26 23:55:29371}
372
[email protected]82522512009-05-15 07:37:29373void View::PaintChildren(gfx::Canvas* canvas) {
[email protected]09fe9492009-11-07 02:23:06374 for (int i = 0, count = GetChildViewCount(); i < count; ++i) {
initial.commit09911bf2008-07-26 23:55:29375 View* child = GetChildViewAt(i);
376 if (!child) {
377 NOTREACHED() << "Should not have a NULL child View for index in bounds";
378 continue;
379 }
380 child->ProcessPaint(canvas);
381 }
382}
383
[email protected]82522512009-05-15 07:37:29384void View::ProcessPaint(gfx::Canvas* canvas) {
[email protected]09fe9492009-11-07 02:23:06385 if (!IsVisible())
initial.commit09911bf2008-07-26 23:55:29386 return;
initial.commit09911bf2008-07-26 23:55:29387
388 // We're going to modify the canvas, save it's state first.
[email protected]bd026802010-06-29 22:08:44389 canvas->Save();
initial.commit09911bf2008-07-26 23:55:29390
391 // Paint this View and its children, setting the clip rect to the bounds
392 // of this View and translating the origin to the local bounds' top left
393 // point.
394 //
395 // Note that the X (or left) position we pass to ClipRectInt takes into
396 // consideration whether or not the view uses a right-to-left layout so that
397 // we paint our view in its mirrored position if need be.
[email protected]80f8b9f2008-10-16 18:17:47398 if (canvas->ClipRectInt(MirroredX(), y(), width(), height())) {
initial.commit09911bf2008-07-26 23:55:29399 // Non-empty clip, translate the graphics such that 0,0 corresponds to
400 // where this view is located (related to its parent).
[email protected]80f8b9f2008-10-16 18:17:47401 canvas->TranslateInt(MirroredX(), y());
initial.commit09911bf2008-07-26 23:55:29402
403 // Save the state again, so that any changes don't effect PaintChildren.
[email protected]bd026802010-06-29 22:08:44404 canvas->Save();
initial.commit09911bf2008-07-26 23:55:29405
406 // If the View we are about to paint requested the canvas to be flipped, we
407 // should change the transform appropriately.
408 bool flip_canvas = FlipCanvasOnPaintForRTLUI();
409 if (flip_canvas) {
[email protected]6f3bb6c2008-09-17 22:25:33410 canvas->TranslateInt(width(), 0);
initial.commit09911bf2008-07-26 23:55:29411 canvas->ScaleInt(-1, 1);
[email protected]bd026802010-06-29 22:08:44412 canvas->Save();
initial.commit09911bf2008-07-26 23:55:29413 }
414
415 Paint(canvas);
416
417 // We must undo the canvas mirroring once the View is done painting so that
418 // we don't pass the canvas with the mirrored transform to Views that
419 // didn't request the canvas to be flipped.
[email protected]09fe9492009-11-07 02:23:06420 if (flip_canvas)
[email protected]bd026802010-06-29 22:08:44421 canvas->Restore();
[email protected]09fe9492009-11-07 02:23:06422
[email protected]bd026802010-06-29 22:08:44423 canvas->Restore();
initial.commit09911bf2008-07-26 23:55:29424 PaintChildren(canvas);
425 }
426
427 // Restore the canvas's original transform.
[email protected]bd026802010-06-29 22:08:44428 canvas->Restore();
initial.commit09911bf2008-07-26 23:55:29429}
430
431void View::PaintNow() {
[email protected]09fe9492009-11-07 02:23:06432 if (!IsVisible())
initial.commit09911bf2008-07-26 23:55:29433 return;
initial.commit09911bf2008-07-26 23:55:29434
435 View* view = GetParent();
436 if (view)
437 view->PaintNow();
438}
439
initial.commit09911bf2008-07-26 23:55:29440gfx::Insets View::GetInsets() const {
initial.commit09911bf2008-07-26 23:55:29441 gfx::Insets insets;
[email protected]9a3f0ac22008-11-14 03:24:02442 if (border_.get())
443 border_->GetInsets(&insets);
initial.commit09911bf2008-07-26 23:55:29444 return insets;
445}
446
[email protected]e9adf0702010-03-08 23:34:07447gfx::NativeCursor View::GetCursorForPoint(Event::EventType event_type,
448 const gfx::Point& p) {
[email protected]9abf8dd62009-06-04 06:40:42449 return NULL;
450}
451
[email protected]a52ca4672009-06-05 05:41:09452bool View::HitTest(const gfx::Point& l) const {
[email protected]d7fc97942009-11-20 22:07:11453 if (l.x() >= 0 && l.x() < width() && l.y() >= 0 && l.y() < height()) {
[email protected]a52ca4672009-06-05 05:41:09454 if (HasHitTestMask()) {
455 gfx::Path mask;
456 GetHitTestMask(&mask);
[email protected]a3406972009-11-04 05:05:48457 ScopedRegion rgn(mask.CreateNativeRegion());
458 // TODO: can this use SkRegion's contains instead?
[email protected]a52ca4672009-06-05 05:41:09459#if defined(OS_WIN)
[email protected]a52ca4672009-06-05 05:41:09460 return !!PtInRegion(rgn, l.x(), l.y());
[email protected]10a6e77b2009-12-31 01:03:52461#elif defined(TOOLKIT_USES_GTK)
[email protected]a3406972009-11-04 05:05:48462 return gdk_region_point_in(rgn.Get(), l.x(), l.y());
[email protected]a52ca4672009-06-05 05:41:09463#endif
464 }
465 // No mask, but inside our bounds.
466 return true;
467 }
468 // Outside our bounds.
469 return false;
470}
471
initial.commit09911bf2008-07-26 23:55:29472void View::SetContextMenuController(ContextMenuController* menu_controller) {
473 context_menu_controller_ = menu_controller;
474}
475
[email protected]e9adf0702010-03-08 23:34:07476void View::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) {
[email protected]042811c2008-10-31 21:31:34477 if (!context_menu_controller_)
478 return;
479
[email protected]e9adf0702010-03-08 23:34:07480 context_menu_controller_->ShowContextMenu(this, p, is_mouse_gesture);
[email protected]042811c2008-10-31 21:31:34481}
482
initial.commit09911bf2008-07-26 23:55:29483/////////////////////////////////////////////////////////////////////////////
484//
485// View - tree
486//
487/////////////////////////////////////////////////////////////////////////////
488
489bool View::ProcessMousePressed(const MouseEvent& e, DragInfo* drag_info) {
[email protected]83548a42010-06-18 13:53:37490 const bool enabled = IsEnabled();
[email protected]e9adf0702010-03-08 23:34:07491 int drag_operations =
492 (enabled && e.IsOnlyLeftMouseButton() && HitTest(e.location())) ?
493 GetDragOperations(e.location()) : 0;
494 ContextMenuController* context_menu_controller = e.IsRightMouseButton() ?
495 context_menu_controller_ : 0;
initial.commit09911bf2008-07-26 23:55:29496
497 const bool result = OnMousePressed(e);
498 // WARNING: we may have been deleted, don't use any View variables;
499
500 if (!enabled)
501 return result;
502
503 if (drag_operations != DragDropTypes::DRAG_NONE) {
[email protected]e9adf0702010-03-08 23:34:07504 drag_info->PossibleDrag(e.location());
initial.commit09911bf2008-07-26 23:55:29505 return true;
506 }
507 return !!context_menu_controller || result;
508}
509
510bool View::ProcessMouseDragged(const MouseEvent& e, DragInfo* drag_info) {
511 // Copy the field, that way if we're deleted after drag and drop no harm is
512 // done.
513 ContextMenuController* context_menu_controller = context_menu_controller_;
514 const bool possible_drag = drag_info->possible_drag;
[email protected]e9adf0702010-03-08 23:34:07515 if (possible_drag && ExceededDragThreshold(drag_info->start_pt.x() - e.x(),
516 drag_info->start_pt.y() - e.y())) {
[email protected]b5f94de2009-12-04 07:59:00517 if (!drag_controller_ ||
[email protected]e9adf0702010-03-08 23:34:07518 drag_controller_->CanStartDrag(this, drag_info->start_pt, e.location()))
519 DoDrag(e, drag_info->start_pt);
initial.commit09911bf2008-07-26 23:55:29520 } else {
521 if (OnMouseDragged(e))
522 return true;
523 // Fall through to return value based on context menu controller.
524 }
525 // WARNING: we may have been deleted.
526 return (context_menu_controller != NULL) || possible_drag;
527}
528
529void View::ProcessMouseReleased(const MouseEvent& e, bool canceled) {
530 if (!canceled && context_menu_controller_ && e.IsOnlyRightMouseButton()) {
531 // Assume that if there is a context menu controller we won't be deleted
532 // from mouse released.
[email protected]96b667d2008-10-14 20:58:44533 gfx::Point location(e.location());
initial.commit09911bf2008-07-26 23:55:29534 OnMouseReleased(e, canceled);
[email protected]464fdb32009-03-19 20:25:44535 if (HitTest(location)) {
536 ConvertPointToScreen(this, &location);
[email protected]e9adf0702010-03-08 23:34:07537 ShowContextMenu(location, true);
[email protected]464fdb32009-03-19 20:25:44538 }
initial.commit09911bf2008-07-26 23:55:29539 } else {
540 OnMouseReleased(e, canceled);
541 }
542 // WARNING: we may have been deleted.
543}
544
initial.commit09911bf2008-07-26 23:55:29545void View::AddChildView(View* v) {
[email protected]0d52b2302009-05-11 23:50:55546 AddChildView(static_cast<int>(child_views_.size()), v);
initial.commit09911bf2008-07-26 23:55:29547}
548
549void View::AddChildView(int index, View* v) {
[email protected]0b2fa2b2009-12-01 01:06:12550 CHECK(v != this) << "You cannot add a view as its own child";
551
initial.commit09911bf2008-07-26 23:55:29552 // Remove the view from its current parent if any.
553 if (v->GetParent())
554 v->GetParent()->RemoveChildView(v);
555
[email protected]0d52b2302009-05-11 23:50:55556 // Sets the prev/next focus views.
557 InitFocusSiblings(v, index);
initial.commit09911bf2008-07-26 23:55:29558
559 // Let's insert the view.
560 child_views_.insert(child_views_.begin() + index, v);
561 v->SetParent(this);
562
[email protected]09fe9492009-11-07 02:23:06563 for (View* p = this; p; p = p->GetParent())
initial.commit09911bf2008-07-26 23:55:29564 p->ViewHierarchyChangedImpl(false, true, this, v);
[email protected]09fe9492009-11-07 02:23:06565
initial.commit09911bf2008-07-26 23:55:29566 v->PropagateAddNotifications(this, v);
567 UpdateTooltip();
568 RootView* root = GetRootView();
569 if (root)
[email protected]9fc4f202010-07-07 15:38:19570 RegisterChildrenForVisibleBoundsNotification(root, v);
initial.commit09911bf2008-07-26 23:55:29571
572 if (layout_manager_.get())
573 layout_manager_->ViewAdded(this, v);
574}
575
576View* View::GetChildViewAt(int index) const {
577 return index < GetChildViewCount() ? child_views_[index] : NULL;
578}
579
580int View::GetChildViewCount() const {
581 return static_cast<int>(child_views_.size());
582}
583
[email protected]3a9c26f2010-03-12 21:04:39584bool View::HasChildView(View* a_view) {
585 return find(child_views_.begin(),
586 child_views_.end(),
587 a_view) != child_views_.end();
588}
589
initial.commit09911bf2008-07-26 23:55:29590void View::RemoveChildView(View* a_view) {
591 DoRemoveChildView(a_view, true, true, false);
592}
593
594void View::RemoveAllChildViews(bool delete_views) {
595 ViewList::iterator iter;
596 while ((iter = child_views_.begin()) != child_views_.end()) {
597 DoRemoveChildView(*iter, false, false, delete_views);
598 }
599 UpdateTooltip();
600}
601
[email protected]e9adf0702010-03-08 23:34:07602void View::DoDrag(const MouseEvent& e, const gfx::Point& press_pt) {
603 int drag_operations = GetDragOperations(press_pt);
[email protected]4768c65b2009-08-28 21:42:59604 if (drag_operations == DragDropTypes::DRAG_NONE)
605 return;
606
607 OSExchangeData data;
[email protected]e9adf0702010-03-08 23:34:07608 WriteDragData(press_pt, &data);
[email protected]4768c65b2009-08-28 21:42:59609
610 // Message the RootView to do the drag and drop. That way if we're removed
611 // the RootView can detect it and avoid calling us back.
612 RootView* root_view = GetRootView();
613 root_view->StartDragForViewFromMouseEvent(this, data, drag_operations);
614}
615
initial.commit09911bf2008-07-26 23:55:29616void View::DoRemoveChildView(View* a_view,
617 bool update_focus_cycle,
618 bool update_tool_tip,
619 bool delete_removed_view) {
620#ifndef NDEBUG
621 DCHECK(!IsProcessingPaint()) << "Should not be removing a child view " <<
622 "during a paint, this will seriously " <<
623 "mess things up!";
624#endif
625 DCHECK(a_view);
626 const ViewList::iterator i = find(child_views_.begin(),
627 child_views_.end(),
628 a_view);
629 if (i != child_views_.end()) {
[email protected]0d52b2302009-05-11 23:50:55630 if (update_focus_cycle) {
initial.commit09911bf2008-07-26 23:55:29631 // Let's remove the view from the focus traversal.
632 View* next_focusable = a_view->next_focusable_view_;
633 View* prev_focusable = a_view->previous_focusable_view_;
634 if (prev_focusable)
635 prev_focusable->next_focusable_view_ = next_focusable;
636 if (next_focusable)
637 next_focusable->previous_focusable_view_ = prev_focusable;
638 }
639
640 RootView* root = GetRootView();
641 if (root)
[email protected]9fc4f202010-07-07 15:38:19642 UnregisterChildrenForVisibleBoundsNotification(root, a_view);
initial.commit09911bf2008-07-26 23:55:29643 a_view->PropagateRemoveNotifications(this);
644 a_view->SetParent(NULL);
645
646 if (delete_removed_view && a_view->IsParentOwned())
647 delete a_view;
648
649 child_views_.erase(i);
650 }
651
652 if (update_tool_tip)
653 UpdateTooltip();
654
655 if (layout_manager_.get())
656 layout_manager_->ViewRemoved(this, a_view);
657}
658
659void View::PropagateRemoveNotifications(View* parent) {
[email protected]09fe9492009-11-07 02:23:06660 for (int i = 0, count = GetChildViewCount(); i < count; ++i)
initial.commit09911bf2008-07-26 23:55:29661 GetChildViewAt(i)->PropagateRemoveNotifications(parent);
initial.commit09911bf2008-07-26 23:55:29662
[email protected]09fe9492009-11-07 02:23:06663 for (View* v = this; v; v = v->GetParent())
664 v->ViewHierarchyChangedImpl(true, false, parent, this);
initial.commit09911bf2008-07-26 23:55:29665}
666
667void View::PropagateAddNotifications(View* parent, View* child) {
[email protected]09fe9492009-11-07 02:23:06668 for (int i = 0, count = GetChildViewCount(); i < count; ++i)
initial.commit09911bf2008-07-26 23:55:29669 GetChildViewAt(i)->PropagateAddNotifications(parent, child);
initial.commit09911bf2008-07-26 23:55:29670 ViewHierarchyChangedImpl(true, true, parent, child);
671}
672
[email protected]59461d22010-07-21 15:41:09673bool View::IsFocusable() const {
674 return focusable_ && IsEnabled() && IsVisible();
675}
676
[email protected]8b9a8f12010-07-27 01:39:13677void View::PropagateThemeChanged() {
[email protected]b2b718012010-03-25 15:09:06678 for (int i = GetChildViewCount() - 1; i >= 0; --i)
[email protected]8b9a8f12010-07-27 01:39:13679 GetChildViewAt(i)->PropagateThemeChanged();
680 OnThemeChanged();
[email protected]32670b02009-03-03 00:28:00681}
682
[email protected]8b9a8f12010-07-27 01:39:13683void View::PropagateLocaleChanged() {
[email protected]b2b718012010-03-25 15:09:06684 for (int i = GetChildViewCount() - 1; i >= 0; --i)
[email protected]8b9a8f12010-07-27 01:39:13685 GetChildViewAt(i)->PropagateLocaleChanged();
686 OnLocaleChanged();
[email protected]b2b718012010-03-25 15:09:06687}
688
initial.commit09911bf2008-07-26 23:55:29689#ifndef NDEBUG
690bool View::IsProcessingPaint() const {
691 return GetParent() && GetParent()->IsProcessingPaint();
692}
693#endif
694
[email protected]f704ee72008-11-10 21:31:59695gfx::Point View::GetKeyboardContextMenuLocation() {
696 gfx::Rect vis_bounds = GetVisibleBounds();
697 gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2,
698 vis_bounds.y() + vis_bounds.height() / 2);
699 ConvertPointToScreen(this, &screen_point);
700 return screen_point;
701}
702
[email protected]82739cf2008-09-16 00:37:56703bool View::HasHitTestMask() const {
704 return false;
705}
706
707void View::GetHitTestMask(gfx::Path* mask) const {
708 DCHECK(mask);
709}
710
[email protected]bb515ed2009-01-15 00:53:43711void View::ViewHierarchyChanged(bool is_add,
712 View* parent,
713 View* child) {
initial.commit09911bf2008-07-26 23:55:29714}
715
716void View::ViewHierarchyChangedImpl(bool register_accelerators,
[email protected]bb515ed2009-01-15 00:53:43717 bool is_add,
718 View* parent,
719 View* child) {
initial.commit09911bf2008-07-26 23:55:29720 if (register_accelerators) {
721 if (is_add) {
722 // If you get this registration, you are part of a subtree that has been
723 // added to the view hierarchy.
[email protected]bda9556c2010-01-07 00:55:16724 if (GetFocusManager()) {
725 RegisterPendingAccelerators();
726 } else {
727 // Delay accelerator registration until visible as we do not have
728 // focus manager until then.
729 accelerator_registration_delayed_ = true;
730 }
initial.commit09911bf2008-07-26 23:55:29731 } else {
732 if (child == this)
[email protected]fa1cf0b82010-01-15 21:49:44733 UnregisterAccelerators(true);
initial.commit09911bf2008-07-26 23:55:29734 }
735 }
736
737 ViewHierarchyChanged(is_add, parent, child);
[email protected]9ea053e2010-07-15 08:19:05738 parent->needs_layout_ = true;
initial.commit09911bf2008-07-26 23:55:29739}
740
741void View::PropagateVisibilityNotifications(View* start, bool is_visible) {
[email protected]09fe9492009-11-07 02:23:06742 for (int i = 0, count = GetChildViewCount(); i < count; ++i)
initial.commit09911bf2008-07-26 23:55:29743 GetChildViewAt(i)->PropagateVisibilityNotifications(start, is_visible);
initial.commit09911bf2008-07-26 23:55:29744 VisibilityChanged(start, is_visible);
745}
746
747void View::VisibilityChanged(View* starting_from, bool is_visible) {
748}
749
[email protected]bda9556c2010-01-07 00:55:16750void View::PropagateNativeViewHierarchyChanged(bool attached,
751 gfx::NativeView native_view,
752 RootView* root_view) {
753 for (int i = 0, count = GetChildViewCount(); i < count; ++i)
754 GetChildViewAt(i)->PropagateNativeViewHierarchyChanged(attached,
755 native_view,
756 root_view);
757 NativeViewHierarchyChanged(attached, native_view, root_view);
758}
759
760void View::NativeViewHierarchyChanged(bool attached,
761 gfx::NativeView native_view,
762 RootView* root_view) {
763 FocusManager* focus_manager = GetFocusManager();
764 if (!accelerator_registration_delayed_ &&
765 accelerator_focus_manager_ &&
766 accelerator_focus_manager_ != focus_manager) {
767 UnregisterAccelerators(true);
768 accelerator_registration_delayed_ = true;
769 }
770 if (accelerator_registration_delayed_ && attached) {
771 if (focus_manager) {
772 RegisterPendingAccelerators();
773 accelerator_registration_delayed_ = false;
774 }
775 }
776}
777
initial.commit09911bf2008-07-26 23:55:29778void View::SetNotifyWhenVisibleBoundsInRootChanges(bool value) {
779 if (notify_when_visible_bounds_in_root_changes_ == value)
780 return;
781 notify_when_visible_bounds_in_root_changes_ = value;
782 RootView* root = GetRootView();
783 if (root) {
784 if (value)
785 root->RegisterViewForVisibleBoundsNotification(this);
786 else
787 root->UnregisterViewForVisibleBoundsNotification(this);
788 }
789}
790
791bool View::GetNotifyWhenVisibleBoundsInRootChanges() {
792 return notify_when_visible_bounds_in_root_changes_;
793}
794
[email protected]0d52b2302009-05-11 23:50:55795View* View::GetViewForPoint(const gfx::Point& point) {
initial.commit09911bf2008-07-26 23:55:29796 // Walk the child Views recursively looking for the View that most
797 // tightly encloses the specified point.
[email protected]09fe9492009-11-07 02:23:06798 for (int i = GetChildViewCount() - 1; i >= 0; --i) {
initial.commit09911bf2008-07-26 23:55:29799 View* child = GetChildViewAt(i);
[email protected]82739cf2008-09-16 00:37:56800 if (!child->IsVisible())
initial.commit09911bf2008-07-26 23:55:29801 continue;
[email protected]82739cf2008-09-16 00:37:56802
[email protected]96b667d2008-10-14 20:58:44803 gfx::Point point_in_child_coords(point);
[email protected]82739cf2008-09-16 00:37:56804 View::ConvertPointToView(this, child, &point_in_child_coords);
[email protected]613b8062008-10-14 23:45:09805 if (child->HitTest(point_in_child_coords))
[email protected]0d52b2302009-05-11 23:50:55806 return child->GetViewForPoint(point_in_child_coords);
initial.commit09911bf2008-07-26 23:55:29807 }
808 return this;
809}
810
[email protected]a0dde122008-11-21 20:51:20811Widget* View::GetWidget() const {
812 // The root view holds a reference to this view hierarchy's Widget.
813 return parent_ ? parent_->GetWidget() : NULL;
initial.commit09911bf2008-07-26 23:55:29814}
815
[email protected]cd8c47902009-04-30 20:55:35816Window* View::GetWindow() const {
817 Widget* widget = GetWidget();
818 return widget ? widget->GetWindow() : NULL;
819}
820
[email protected]2db27be2010-02-10 22:46:47821bool View::ContainsNativeView(gfx::NativeView native_view) const {
822 for (int i = 0, count = GetChildViewCount(); i < count; ++i) {
823 if (GetChildViewAt(i)->ContainsNativeView(native_view))
824 return true;
825 }
826 return false;
827}
828
initial.commit09911bf2008-07-26 23:55:29829// Get the containing RootView
830RootView* View::GetRootView() {
[email protected]a0dde122008-11-21 20:51:20831 Widget* widget = GetWidget();
832 return widget ? widget->GetRootView() : NULL;
initial.commit09911bf2008-07-26 23:55:29833}
834
835View* View::GetViewByID(int id) const {
836 if (id == id_)
837 return const_cast<View*>(this);
838
[email protected]09fe9492009-11-07 02:23:06839 for (int i = 0, count = GetChildViewCount(); i < count; ++i) {
initial.commit09911bf2008-07-26 23:55:29840 View* child = GetChildViewAt(i);
841 View* view = child->GetViewByID(id);
842 if (view)
843 return view;
844 }
845 return NULL;
846}
847
848void View::GetViewsWithGroup(int group_id, std::vector<View*>* out) {
849 if (group_ == group_id)
850 out->push_back(this);
851
[email protected]09fe9492009-11-07 02:23:06852 for (int i = 0, count = GetChildViewCount(); i < count; ++i)
initial.commit09911bf2008-07-26 23:55:29853 GetChildViewAt(i)->GetViewsWithGroup(group_id, out);
854}
855
856View* View::GetSelectedViewForGroup(int group_id) {
857 std::vector<View*> views;
858 GetRootView()->GetViewsWithGroup(group_id, &views);
859 if (views.size() > 0)
860 return views[0];
861 else
862 return NULL;
863}
864
865void View::SetID(int id) {
866 id_ = id;
867}
868
869int View::GetID() const {
870 return id_;
871}
872
873void View::SetGroup(int gid) {
[email protected]96f960d2009-09-14 18:45:30874 // Don't change the group id once it's set.
875 DCHECK(group_ == -1 || group_ == gid);
initial.commit09911bf2008-07-26 23:55:29876 group_ = gid;
877}
878
879int View::GetGroup() const {
880 return group_;
881}
882
883void View::SetParent(View* parent) {
[email protected]09fe9492009-11-07 02:23:06884 if (parent != parent_)
initial.commit09911bf2008-07-26 23:55:29885 parent_ = parent;
initial.commit09911bf2008-07-26 23:55:29886}
887
888bool View::IsParentOf(View* v) const {
889 DCHECK(v);
890 View* parent = v->GetParent();
891 while (parent) {
892 if (this == parent)
893 return true;
894 parent = parent->GetParent();
895 }
896 return false;
897}
898
[email protected]5e26d9d42010-05-13 22:11:03899int View::GetChildIndex(const View* v) const {
[email protected]09fe9492009-11-07 02:23:06900 for (int i = 0, count = GetChildViewCount(); i < count; i++) {
initial.commit09911bf2008-07-26 23:55:29901 if (v == GetChildViewAt(i))
902 return i;
903 }
904 return -1;
905}
906
907///////////////////////////////////////////////////////////////////////////////
908//
909// View - focus
910//
911///////////////////////////////////////////////////////////////////////////////
912
913View* View::GetNextFocusableView() {
914 return next_focusable_view_;
915}
916
917View* View::GetPreviousFocusableView() {
918 return previous_focusable_view_;
919}
920
921void View::SetNextFocusableView(View* view) {
922 view->previous_focusable_view_ = this;
923 next_focusable_view_ = view;
924}
925
926void View::InitFocusSiblings(View* v, int index) {
927 int child_count = static_cast<int>(child_views_.size());
928
929 if (child_count == 0) {
930 v->next_focusable_view_ = NULL;
931 v->previous_focusable_view_ = NULL;
932 } else {
933 if (index == child_count) {
934 // We are inserting at the end, but the end of the child list may not be
935 // the last focusable element. Let's try to find an element with no next
936 // focusable element to link to.
937 View* last_focusable_view = NULL;
938 for (std::vector<View*>::iterator iter = child_views_.begin();
939 iter != child_views_.end(); ++iter) {
940 if (!(*iter)->next_focusable_view_) {
941 last_focusable_view = *iter;
942 break;
943 }
944 }
945 if (last_focusable_view == NULL) {
946 // Hum... there is a cycle in the focus list. Let's just insert ourself
947 // after the last child.
948 View* prev = child_views_[index - 1];
949 v->previous_focusable_view_ = prev;
950 v->next_focusable_view_ = prev->next_focusable_view_;
951 prev->next_focusable_view_->previous_focusable_view_ = v;
952 prev->next_focusable_view_ = v;
953 } else {
954 last_focusable_view->next_focusable_view_ = v;
955 v->next_focusable_view_ = NULL;
956 v->previous_focusable_view_ = last_focusable_view;
957 }
958 } else {
959 View* prev = child_views_[index]->GetPreviousFocusableView();
960 v->previous_focusable_view_ = prev;
961 v->next_focusable_view_ = child_views_[index];
962 if (prev)
963 prev->next_focusable_view_ = v;
964 child_views_[index]->previous_focusable_view_ = v;
965 }
966 }
967}
968
969#ifndef NDEBUG
970void View::PrintViewHierarchy() {
971 PrintViewHierarchyImp(0);
972}
973
974void View::PrintViewHierarchyImp(int indent) {
975 std::wostringstream buf;
976 int ind = indent;
977 while (ind-- > 0)
978 buf << L' ';
979 buf << UTF8ToWide(GetClassName());
980 buf << L' ';
981 buf << GetID();
982 buf << L' ';
[email protected]80f8b9f2008-10-16 18:17:47983 buf << bounds_.x() << L"," << bounds_.y() << L",";
984 buf << bounds_.right() << L"," << bounds_.bottom();
initial.commit09911bf2008-07-26 23:55:29985 buf << L' ';
986 buf << this;
987
988 LOG(INFO) << buf.str();
989 std::cout << buf.str() << std::endl;
990
[email protected]09fe9492009-11-07 02:23:06991 for (int i = 0, count = GetChildViewCount(); i < count; ++i)
initial.commit09911bf2008-07-26 23:55:29992 GetChildViewAt(i)->PrintViewHierarchyImp(indent + 2);
initial.commit09911bf2008-07-26 23:55:29993}
994
995
996void View::PrintFocusHierarchy() {
997 PrintFocusHierarchyImp(0);
998}
999
1000void View::PrintFocusHierarchyImp(int indent) {
1001 std::wostringstream buf;
1002 int ind = indent;
1003 while (ind-- > 0)
1004 buf << L' ';
1005 buf << UTF8ToWide(GetClassName());
1006 buf << L' ';
1007 buf << GetID();
1008 buf << L' ';
1009 buf << GetClassName().c_str();
1010 buf << L' ';
1011 buf << this;
1012
1013 LOG(INFO) << buf.str();
1014 std::cout << buf.str() << std::endl;
1015
1016 if (GetChildViewCount() > 0)
1017 GetChildViewAt(0)->PrintFocusHierarchyImp(indent + 2);
1018
1019 View* v = GetNextFocusableView();
1020 if (v)
1021 v->PrintFocusHierarchyImp(indent);
1022}
1023#endif
1024
1025////////////////////////////////////////////////////////////////////////////////
1026//
1027// View - accelerators
1028//
1029////////////////////////////////////////////////////////////////////////////////
1030
1031void View::AddAccelerator(const Accelerator& accelerator) {
1032 if (!accelerators_.get())
1033 accelerators_.reset(new std::vector<Accelerator>());
[email protected]71421c3f2009-06-06 00:41:441034
1035 std::vector<Accelerator>::iterator iter =
1036 std::find(accelerators_->begin(), accelerators_->end(), accelerator);
1037 DCHECK(iter == accelerators_->end())
1038 << "Registering the same accelerator multiple times";
1039
initial.commit09911bf2008-07-26 23:55:291040 accelerators_->push_back(accelerator);
[email protected]71421c3f2009-06-06 00:41:441041 RegisterPendingAccelerators();
initial.commit09911bf2008-07-26 23:55:291042}
1043
[email protected]e8e0f362008-11-08 01:13:251044void View::RemoveAccelerator(const Accelerator& accelerator) {
1045 std::vector<Accelerator>::iterator iter;
1046 if (!accelerators_.get() ||
1047 ((iter = std::find(accelerators_->begin(), accelerators_->end(),
1048 accelerator)) == accelerators_->end())) {
1049 NOTREACHED() << "Removing non-existing accelerator";
1050 return;
1051 }
1052
[email protected]4bd23f32009-06-08 20:59:191053 size_t index = iter - accelerators_->begin();
[email protected]e8e0f362008-11-08 01:13:251054 accelerators_->erase(iter);
[email protected]71421c3f2009-06-06 00:41:441055 if (index >= registered_accelerator_count_) {
1056 // The accelerator is not registered to FocusManager.
1057 return;
1058 }
1059 --registered_accelerator_count_;
1060
[email protected]e8e0f362008-11-08 01:13:251061 RootView* root_view = GetRootView();
1062 if (!root_view) {
1063 // We are not part of a view hierarchy, so there is nothing to do as we
1064 // removed ourselves from accelerators_, we won't be registered when added
1065 // to one.
1066 return;
1067 }
1068
[email protected]bda9556c2010-01-07 00:55:161069 // If accelerator_focus_manager_ is NULL then we did not registered
1070 // accelerators so there is nothing to unregister.
1071 if (accelerator_focus_manager_) {
1072 accelerator_focus_manager_->UnregisterAccelerator(accelerator, this);
[email protected]e8e0f362008-11-08 01:13:251073 }
1074}
1075
initial.commit09911bf2008-07-26 23:55:291076void View::ResetAccelerators() {
[email protected]71421c3f2009-06-06 00:41:441077 if (accelerators_.get())
[email protected]bda9556c2010-01-07 00:55:161078 UnregisterAccelerators(false);
initial.commit09911bf2008-07-26 23:55:291079}
1080
[email protected]71421c3f2009-06-06 00:41:441081void View::RegisterPendingAccelerators() {
1082 if (!accelerators_.get() ||
1083 registered_accelerator_count_ == accelerators_->size()) {
1084 // No accelerators are waiting for registration.
initial.commit09911bf2008-07-26 23:55:291085 return;
[email protected]71421c3f2009-06-06 00:41:441086 }
initial.commit09911bf2008-07-26 23:55:291087
1088 RootView* root_view = GetRootView();
1089 if (!root_view) {
1090 // We are not yet part of a view hierarchy, we'll register ourselves once
1091 // added to one.
1092 return;
1093 }
[email protected]f3735c5d2009-03-19 17:26:231094
[email protected]bda9556c2010-01-07 00:55:161095 accelerator_focus_manager_ = GetFocusManager();
1096 if (!accelerator_focus_manager_) {
initial.commit09911bf2008-07-26 23:55:291097 // Some crash reports seem to show that we may get cases where we have no
1098 // focus manager (see bug #1291225). This should never be the case, just
1099 // making sure we don't crash.
[email protected]f8dce002009-09-10 21:07:041100
1101 // TODO(jcampan): This fails for a view under WidgetGtk with TYPE_CHILD.
1102 // (see http://crbug.com/21335) reenable NOTREACHED assertion and
1103 // verify accelerators works as expected.
1104#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291105 NOTREACHED();
[email protected]f8dce002009-09-10 21:07:041106#endif
initial.commit09911bf2008-07-26 23:55:291107 return;
1108 }
[email protected]71421c3f2009-06-06 00:41:441109 std::vector<Accelerator>::const_iterator iter;
1110 for (iter = accelerators_->begin() + registered_accelerator_count_;
initial.commit09911bf2008-07-26 23:55:291111 iter != accelerators_->end(); ++iter) {
[email protected]bda9556c2010-01-07 00:55:161112 accelerator_focus_manager_->RegisterAccelerator(*iter, this);
initial.commit09911bf2008-07-26 23:55:291113 }
[email protected]71421c3f2009-06-06 00:41:441114 registered_accelerator_count_ = accelerators_->size();
initial.commit09911bf2008-07-26 23:55:291115}
1116
[email protected]bda9556c2010-01-07 00:55:161117void View::UnregisterAccelerators(bool leave_data_intact) {
initial.commit09911bf2008-07-26 23:55:291118 if (!accelerators_.get())
1119 return;
1120
1121 RootView* root_view = GetRootView();
1122 if (root_view) {
[email protected]bda9556c2010-01-07 00:55:161123 if (accelerator_focus_manager_) {
initial.commit09911bf2008-07-26 23:55:291124 // We may not have a FocusManager if the window containing us is being
1125 // closed, in which case the FocusManager is being deleted so there is
1126 // nothing to unregister.
[email protected]bda9556c2010-01-07 00:55:161127 accelerator_focus_manager_->UnregisterAccelerators(this);
1128 accelerator_focus_manager_ = NULL;
initial.commit09911bf2008-07-26 23:55:291129 }
[email protected]bda9556c2010-01-07 00:55:161130 if (!leave_data_intact) {
1131 accelerators_->clear();
1132 accelerators_.reset();
1133 }
[email protected]71421c3f2009-06-06 00:41:441134 registered_accelerator_count_ = 0;
initial.commit09911bf2008-07-26 23:55:291135 }
1136}
1137
[email protected]e9adf0702010-03-08 23:34:071138int View::GetDragOperations(const gfx::Point& press_pt) {
1139 return drag_controller_ ?
1140 drag_controller_->GetDragOperations(this, press_pt) :
1141 DragDropTypes::DRAG_NONE;
initial.commit09911bf2008-07-26 23:55:291142}
1143
[email protected]e9adf0702010-03-08 23:34:071144void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) {
initial.commit09911bf2008-07-26 23:55:291145 DCHECK(drag_controller_);
[email protected]e9adf0702010-03-08 23:34:071146 drag_controller_->WriteDragData(this, press_pt, data);
initial.commit09911bf2008-07-26 23:55:291147}
1148
1149void View::OnDragDone() {
1150}
1151
1152bool View::InDrag() {
1153 RootView* root_view = GetRootView();
1154 return root_view ? (root_view->GetDragView() == this) : false;
1155}
1156
[email protected]d5c81012010-04-03 00:56:121157bool View::GetAccessibleName(std::wstring* name) {
1158 DCHECK(name);
1159
1160 if (accessible_name_.empty())
1161 return false;
1162 *name = accessible_name_;
1163 return true;
1164}
1165
[email protected]b82a0492010-05-27 23:00:031166bool View::GetAccessibleRole(AccessibilityTypes::Role* role) {
1167 if (accessible_role_) {
1168 *role = accessible_role_;
1169 return true;
1170 }
1171 return false;
1172}
1173
[email protected]d5c81012010-04-03 00:56:121174void View::SetAccessibleName(const std::wstring& name) {
1175 accessible_name_ = name;
1176}
1177
[email protected]b82a0492010-05-27 23:00:031178void View::SetAccessibleRole(const AccessibilityTypes::Role role) {
1179 accessible_role_ = role;
1180}
1181
initial.commit09911bf2008-07-26 23:55:291182// static
[email protected]bb515ed2009-01-15 00:53:431183void View::ConvertPointToView(const View* src,
1184 const View* dst,
1185 gfx::Point* point) {
initial.commit09911bf2008-07-26 23:55:291186 ConvertPointToView(src, dst, point, true);
1187}
1188
1189// static
[email protected]bb515ed2009-01-15 00:53:431190void View::ConvertPointToView(const View* src,
1191 const View* dst,
1192 gfx::Point* point,
initial.commit09911bf2008-07-26 23:55:291193 bool try_other_direction) {
1194 // src can be NULL
1195 DCHECK(dst);
1196 DCHECK(point);
1197
[email protected]bb515ed2009-01-15 00:53:431198 const View* v;
initial.commit09911bf2008-07-26 23:55:291199 gfx::Point offset;
1200
1201 for (v = dst; v && v != src; v = v->GetParent()) {
1202 offset.SetPoint(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION),
[email protected]6f3bb6c2008-09-17 22:25:331203 offset.y() + v->y());
initial.commit09911bf2008-07-26 23:55:291204 }
1205
1206 // The source was not found. The caller wants a conversion
1207 // from a view to a transitive parent.
1208 if (src && v == NULL && try_other_direction) {
1209 gfx::Point p;
1210 // note: try_other_direction is force to FALSE so we don't
1211 // end up in an infinite recursion should both src and dst
1212 // are not parented.
1213 ConvertPointToView(dst, src, &p, false);
1214 // since the src and dst are inverted, p should also be negated
1215 point->SetPoint(point->x() - p.x(), point->y() - p.y());
1216 } else {
1217 point->SetPoint(point->x() - offset.x(), point->y() - offset.y());
1218
1219 // If src is NULL, sp is in the screen coordinate system
1220 if (src == NULL) {
[email protected]a0dde122008-11-21 20:51:201221 Widget* widget = dst->GetWidget();
1222 if (widget) {
[email protected]3b680a82008-12-17 02:03:231223 gfx::Rect b;
[email protected]a0dde122008-11-21 20:51:201224 widget->GetBounds(&b, false);
[email protected]3b680a82008-12-17 02:03:231225 point->SetPoint(point->x() - b.x(), point->y() - b.y());
initial.commit09911bf2008-07-26 23:55:291226 }
1227 }
1228 }
1229}
1230
1231// static
[email protected]2fb6d462009-02-13 18:40:101232void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
initial.commit09911bf2008-07-26 23:55:291233 DCHECK(src);
1234 DCHECK(p);
[email protected]96b667d2008-10-14 20:58:441235
[email protected]96b667d2008-10-14 20:58:441236 gfx::Point offset;
[email protected]2fb6d462009-02-13 18:40:101237 for (const View* v = src; v; v = v->GetParent()) {
[email protected]96b667d2008-10-14 20:58:441238 offset.set_x(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION));
1239 offset.set_y(offset.y() + v->y());
initial.commit09911bf2008-07-26 23:55:291240 }
[email protected]96b667d2008-10-14 20:58:441241 p->SetPoint(p->x() + offset.x(), p->y() + offset.y());
initial.commit09911bf2008-07-26 23:55:291242}
1243
1244// static
[email protected]2fb6d462009-02-13 18:40:101245void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) {
[email protected]96b667d2008-10-14 20:58:441246 gfx::Point t;
[email protected]2fb6d462009-02-13 18:40:101247 ConvertPointToWidget(dest, &t);
[email protected]96b667d2008-10-14 20:58:441248 p->SetPoint(p->x() - t.x(), p->y() - t.y());
initial.commit09911bf2008-07-26 23:55:291249}
1250
1251// static
[email protected]2fb6d462009-02-13 18:40:101252void View::ConvertPointToScreen(const View* src, gfx::Point* p) {
initial.commit09911bf2008-07-26 23:55:291253 DCHECK(src);
1254 DCHECK(p);
1255
[email protected]96b667d2008-10-14 20:58:441256 // If the view is not connected to a tree, there's nothing we can do.
[email protected]a0dde122008-11-21 20:51:201257 Widget* widget = src->GetWidget();
1258 if (widget) {
1259 ConvertPointToWidget(src, p);
[email protected]3b680a82008-12-17 02:03:231260 gfx::Rect r;
[email protected]a0dde122008-11-21 20:51:201261 widget->GetBounds(&r, false);
[email protected]3b680a82008-12-17 02:03:231262 p->SetPoint(p->x() + r.x(), p->y() + r.y());
initial.commit09911bf2008-07-26 23:55:291263 }
1264}
1265
1266/////////////////////////////////////////////////////////////////////////////
1267//
1268// View - event handlers
1269//
1270/////////////////////////////////////////////////////////////////////////////
1271
1272bool View::OnMousePressed(const MouseEvent& e) {
1273 return false;
1274}
1275
1276bool View::OnMouseDragged(const MouseEvent& e) {
1277 return false;
1278}
1279
1280void View::OnMouseReleased(const MouseEvent& e, bool canceled) {
1281}
1282
1283void View::OnMouseMoved(const MouseEvent& e) {
1284}
1285
1286void View::OnMouseEntered(const MouseEvent& e) {
1287}
1288
1289void View::OnMouseExited(const MouseEvent& e) {
1290}
1291
1292void View::SetMouseHandler(View *new_mouse_handler) {
1293 // It is valid for new_mouse_handler to be NULL
[email protected]09fe9492009-11-07 02:23:061294 if (parent_)
initial.commit09911bf2008-07-26 23:55:291295 parent_->SetMouseHandler(new_mouse_handler);
initial.commit09911bf2008-07-26 23:55:291296}
1297
1298void View::SetVisible(bool flag) {
1299 if (flag != is_visible_) {
1300 // If the tab is currently visible, schedule paint to
1301 // refresh parent
[email protected]09fe9492009-11-07 02:23:061302 if (IsVisible())
initial.commit09911bf2008-07-26 23:55:291303 SchedulePaint();
initial.commit09911bf2008-07-26 23:55:291304
1305 is_visible_ = flag;
1306
1307 // This notifies all subviews recursively.
1308 PropagateVisibilityNotifications(this, flag);
1309
1310 // If we are newly visible, schedule paint.
[email protected]09fe9492009-11-07 02:23:061311 if (IsVisible())
initial.commit09911bf2008-07-26 23:55:291312 SchedulePaint();
initial.commit09911bf2008-07-26 23:55:291313 }
1314}
1315
1316bool View::IsVisibleInRootView() const {
1317 View* parent = GetParent();
1318 if (IsVisible() && parent)
1319 return parent->IsVisibleInRootView();
1320 else
1321 return false;
1322}
1323
initial.commit09911bf2008-07-26 23:55:291324/////////////////////////////////////////////////////////////////////////////
1325//
1326// View - keyboard and focus
1327//
1328/////////////////////////////////////////////////////////////////////////////
1329
1330void View::RequestFocus() {
1331 RootView* rv = GetRootView();
[email protected]59461d22010-07-21 15:41:091332 if (rv && IsFocusableInRootView())
initial.commit09911bf2008-07-26 23:55:291333 rv->FocusView(this);
initial.commit09911bf2008-07-26 23:55:291334}
1335
1336void View::WillGainFocus() {
1337}
1338
1339void View::DidGainFocus() {
1340}
1341
1342void View::WillLoseFocus() {
1343}
1344
1345bool View::OnKeyPressed(const KeyEvent& e) {
1346 return false;
1347}
1348
1349bool View::OnKeyReleased(const KeyEvent& e) {
1350 return false;
1351}
1352
1353bool View::OnMouseWheel(const MouseWheelEvent& e) {
1354 return false;
1355}
1356
1357void View::SetDragController(DragController* drag_controller) {
[email protected]09fe9492009-11-07 02:23:061358 drag_controller_ = drag_controller;
initial.commit09911bf2008-07-26 23:55:291359}
1360
1361DragController* View::GetDragController() {
1362 return drag_controller_;
1363}
1364
[email protected]134c47b92009-08-19 03:33:441365bool View::GetDropFormats(
1366 int* formats,
1367 std::set<OSExchangeData::CustomFormat>* custom_formats) {
1368 return false;
1369}
1370
1371bool View::AreDropTypesRequired() {
1372 return false;
1373}
1374
initial.commit09911bf2008-07-26 23:55:291375bool View::CanDrop(const OSExchangeData& data) {
[email protected]134c47b92009-08-19 03:33:441376 // TODO(sky): when I finish up migration, this should default to true.
initial.commit09911bf2008-07-26 23:55:291377 return false;
1378}
1379
1380void View::OnDragEntered(const DropTargetEvent& event) {
1381}
1382
1383int View::OnDragUpdated(const DropTargetEvent& event) {
1384 return DragDropTypes::DRAG_NONE;
1385}
1386
1387void View::OnDragExited() {
1388}
1389
1390int View::OnPerformDrop(const DropTargetEvent& event) {
1391 return DragDropTypes::DRAG_NONE;
1392}
1393
initial.commit09911bf2008-07-26 23:55:291394// static
1395bool View::ExceededDragThreshold(int delta_x, int delta_y) {
1396 return (abs(delta_x) > GetHorizontalDragThreshold() ||
1397 abs(delta_y) > GetVerticalDragThreshold());
1398}
1399
initial.commit09911bf2008-07-26 23:55:291400// Tooltips -----------------------------------------------------------------
[email protected]e9adf0702010-03-08 23:34:071401bool View::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) {
initial.commit09911bf2008-07-26 23:55:291402 return false;
1403}
1404
[email protected]e9adf0702010-03-08 23:34:071405bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) {
initial.commit09911bf2008-07-26 23:55:291406 return false;
1407}
1408
1409void View::TooltipTextChanged() {
[email protected]a0dde122008-11-21 20:51:201410 Widget* widget = GetWidget();
1411 if (widget && widget->GetTooltipManager())
1412 widget->GetTooltipManager()->TooltipTextChanged(this);
initial.commit09911bf2008-07-26 23:55:291413}
1414
1415void View::UpdateTooltip() {
[email protected]a0dde122008-11-21 20:51:201416 Widget* widget = GetWidget();
1417 if (widget && widget->GetTooltipManager())
1418 widget->GetTooltipManager()->UpdateTooltip();
initial.commit09911bf2008-07-26 23:55:291419}
1420
initial.commit09911bf2008-07-26 23:55:291421std::string View::GetClassName() const {
1422 return kViewClassName;
1423}
1424
[email protected]5c2b98b2009-03-09 20:55:541425View* View::GetAncestorWithClassName(const std::string& name) {
1426 for (View* view = this; view; view = view->GetParent()) {
1427 if (view->GetClassName() == name)
1428 return view;
1429 }
1430 return NULL;
1431}
1432
initial.commit09911bf2008-07-26 23:55:291433gfx::Rect View::GetVisibleBounds() {
[email protected]b6296fd2009-02-13 17:34:161434 if (!IsVisibleInRootView())
1435 return gfx::Rect();
[email protected]6f3bb6c2008-09-17 22:25:331436 gfx::Rect vis_bounds(0, 0, width(), height());
initial.commit09911bf2008-07-26 23:55:291437 gfx::Rect ancestor_bounds;
1438 View* view = this;
1439 int root_x = 0;
1440 int root_y = 0;
initial.commit09911bf2008-07-26 23:55:291441 while (view != NULL && !vis_bounds.IsEmpty()) {
1442 root_x += view->GetX(APPLY_MIRRORING_TRANSFORMATION);
[email protected]6f3bb6c2008-09-17 22:25:331443 root_y += view->y();
1444 vis_bounds.Offset(view->GetX(APPLY_MIRRORING_TRANSFORMATION), view->y());
initial.commit09911bf2008-07-26 23:55:291445 View* ancestor = view->GetParent();
1446 if (ancestor != NULL) {
[email protected]6f3bb6c2008-09-17 22:25:331447 ancestor_bounds.SetRect(0, 0, ancestor->width(),
1448 ancestor->height());
initial.commit09911bf2008-07-26 23:55:291449 vis_bounds = vis_bounds.Intersect(ancestor_bounds);
[email protected]a0dde122008-11-21 20:51:201450 } else if (!view->GetWidget()) {
1451 // If the view has no Widget, we're not visible. Return an empty rect.
initial.commit09911bf2008-07-26 23:55:291452 return gfx::Rect();
1453 }
1454 view = ancestor;
1455 }
1456 if (vis_bounds.IsEmpty())
1457 return vis_bounds;
1458 // Convert back to this views coordinate system.
1459 vis_bounds.Offset(-root_x, -root_y);
1460 return vis_bounds;
1461}
1462
1463int View::GetPageScrollIncrement(ScrollView* scroll_view,
1464 bool is_horizontal, bool is_positive) {
1465 return 0;
1466}
1467
1468int View::GetLineScrollIncrement(ScrollView* scroll_view,
1469 bool is_horizontal, bool is_positive) {
1470 return 0;
1471}
1472
[email protected]45da6c72009-10-28 20:45:421473ThemeProvider* View::GetThemeProvider() const {
[email protected]4a190632009-05-09 01:07:421474 Widget* widget = GetWidget();
1475 return widget ? widget->GetThemeProvider() : NULL;
1476}
1477
initial.commit09911bf2008-07-26 23:55:291478// static
[email protected]9fc4f202010-07-07 15:38:191479void View::RegisterChildrenForVisibleBoundsNotification(
1480 RootView* root, View* view) {
initial.commit09911bf2008-07-26 23:55:291481 DCHECK(root && view);
1482 if (view->GetNotifyWhenVisibleBoundsInRootChanges())
1483 root->RegisterViewForVisibleBoundsNotification(view);
1484 for (int i = 0; i < view->GetChildViewCount(); ++i)
[email protected]9fc4f202010-07-07 15:38:191485 RegisterChildrenForVisibleBoundsNotification(root, view->GetChildViewAt(i));
initial.commit09911bf2008-07-26 23:55:291486}
1487
1488// static
[email protected]9fc4f202010-07-07 15:38:191489void View::UnregisterChildrenForVisibleBoundsNotification(
initial.commit09911bf2008-07-26 23:55:291490 RootView* root, View* view) {
1491 DCHECK(root && view);
1492 if (view->GetNotifyWhenVisibleBoundsInRootChanges())
1493 root->UnregisterViewForVisibleBoundsNotification(view);
1494 for (int i = 0; i < view->GetChildViewCount(); ++i)
[email protected]9fc4f202010-07-07 15:38:191495 UnregisterChildrenForVisibleBoundsNotification(root,
1496 view->GetChildViewAt(i));
initial.commit09911bf2008-07-26 23:55:291497}
1498
1499void View::AddDescendantToNotify(View* view) {
1500 DCHECK(view);
1501 if (!descendants_to_notify_.get())
1502 descendants_to_notify_.reset(new ViewList());
1503 descendants_to_notify_->push_back(view);
1504}
1505
1506void View::RemoveDescendantToNotify(View* view) {
1507 DCHECK(view && descendants_to_notify_.get());
1508 ViewList::iterator i = find(descendants_to_notify_->begin(),
1509 descendants_to_notify_->end(),
1510 view);
1511 DCHECK(i != descendants_to_notify_->end());
1512 descendants_to_notify_->erase(i);
1513 if (descendants_to_notify_->empty())
1514 descendants_to_notify_.reset();
1515}
1516
initial.commit09911bf2008-07-26 23:55:291517// DropInfo --------------------------------------------------------------------
1518
1519void View::DragInfo::Reset() {
1520 possible_drag = false;
[email protected]e9adf0702010-03-08 23:34:071521 start_pt = gfx::Point();
initial.commit09911bf2008-07-26 23:55:291522}
1523
[email protected]e9adf0702010-03-08 23:34:071524void View::DragInfo::PossibleDrag(const gfx::Point& p) {
initial.commit09911bf2008-07-26 23:55:291525 possible_drag = true;
[email protected]e9adf0702010-03-08 23:34:071526 start_pt = p;
initial.commit09911bf2008-07-26 23:55:291527}
1528
1529} // namespace