[email protected] | 8af4c199 | 2010-02-04 21:38:07 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 5 | #include "views/view.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 8 | #ifndef NDEBUG |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include <iostream> |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 10 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
[email protected] | 3712621 | 2009-05-06 02:23:31 | [diff] [blame] | 12 | #include "app/drag_drop_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | #include "base/message_loop.h" |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 15 | #include "base/scoped_handle.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 16 | #include "base/utf_string_conversions.h" |
[email protected] | 74db48eb | 2010-06-25 16:33:23 | [diff] [blame] | 17 | #include "gfx/canvas_skia.h" |
[email protected] | 5c7293a | 2010-03-17 06:40:57 | [diff] [blame] | 18 | #include "gfx/path.h" |
[email protected] | d5282e7 | 2009-05-13 13:16:52 | [diff] [blame] | 19 | #include "third_party/skia/include/core/SkShader.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 20 | #include "views/background.h" |
| 21 | #include "views/layout_manager.h" |
[email protected] | 3ee83f2c | 2009-05-10 05:58:40 | [diff] [blame] | 22 | #include "views/views_delegate.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 23 | #include "views/widget/root_view.h" |
[email protected] | 319d4ae | 2009-05-28 19:09:45 | [diff] [blame] | 24 | #include "views/widget/tooltip_manager.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 25 | #include "views/widget/widget.h" |
| 26 | #include "views/window/window.h" |
[email protected] | 031e4d3 | 2009-12-29 01:13:23 | [diff] [blame] | 27 | |
[email protected] | 6ff244f | 2009-01-20 20:38:08 | [diff] [blame] | 28 | #if defined(OS_WIN) |
[email protected] | 5fe1572 | 2010-10-22 00:03:22 | [diff] [blame] | 29 | #include "views/accessibility/view_accessibility.h" |
[email protected] | 6ff244f | 2009-01-20 20:38:08 | [diff] [blame] | 30 | #endif |
[email protected] | 031e4d3 | 2009-12-29 01:13:23 | [diff] [blame] | 31 | #if defined(OS_LINUX) |
| 32 | #include "app/scoped_handle_gtk.h" |
| 33 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 35 | namespace views { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
| 37 | // static |
[email protected] | 3ee83f2c | 2009-05-10 05:58:40 | [diff] [blame] | 38 | ViewsDelegate* ViewsDelegate::views_delegate = NULL; |
| 39 | |
| 40 | // static |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 41 | char View::kViewClassName[] = "views/View"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
[email protected] | 8af4c199 | 2010-02-04 21:38:07 | [diff] [blame] | 43 | // static |
| 44 | const int View::kShowFolderDropMenuDelay = 400; |
| 45 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | ///////////////////////////////////////////////////////////////////////////// |
| 47 | // |
| 48 | // View - constructors, destructors, initialization |
| 49 | // |
| 50 | ///////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | View::View() |
| 53 | : id_(0), |
| 54 | group_(-1), |
[email protected] | 6ff244f | 2009-01-20 20:38:08 | [diff] [blame] | 55 | enabled_(true), |
| 56 | focusable_(false), |
[email protected] | 83548a4 | 2010-06-18 13:53:37 | [diff] [blame] | 57 | accessibility_focusable_(false), |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 58 | bounds_(0, 0, 0, 0), |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 59 | needs_layout_(true), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | parent_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | is_visible_(true), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | is_parent_owned_(true), |
| 63 | notify_when_visible_bounds_in_root_changes_(false), |
| 64 | registered_for_visible_bounds_notification_(false), |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 65 | accelerator_registration_delayed_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | next_focusable_view_(NULL), |
| 67 | previous_focusable_view_(NULL), |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 68 | accelerator_focus_manager_(NULL), |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 69 | registered_accelerator_count_(0), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | context_menu_controller_(NULL), |
| 71 | drag_controller_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | flip_canvas_on_paint_for_rtl_ui_(false) { |
| 73 | } |
| 74 | |
| 75 | View::~View() { |
[email protected] | 8c57de2 | 2010-03-12 21:06:04 | [diff] [blame] | 76 | if (parent_) |
| 77 | parent_->RemoveChildView(this); |
| 78 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | int c = static_cast<int>(child_views_.size()); |
| 80 | while (--c >= 0) { |
[email protected] | 8c57de2 | 2010-03-12 21:06:04 | [diff] [blame] | 81 | child_views_[c]->SetParent(NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | if (child_views_[c]->IsParentOwned()) |
| 83 | delete child_views_[c]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | } |
[email protected] | a64f3390 | 2009-10-10 05:41:23 | [diff] [blame] | 85 | |
| 86 | #if defined(OS_WIN) |
[email protected] | 5fe1572 | 2010-10-22 00:03:22 | [diff] [blame] | 87 | if (view_accessibility_.get()) |
| 88 | view_accessibility_->set_view(NULL); |
[email protected] | a64f3390 | 2009-10-10 05:41:23 | [diff] [blame] | 89 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | ///////////////////////////////////////////////////////////////////////////// |
| 93 | // |
| 94 | // View - sizing |
| 95 | // |
| 96 | ///////////////////////////////////////////////////////////////////////////// |
| 97 | |
[email protected] | 0d8ea70 | 2008-10-14 17:03:07 | [diff] [blame] | 98 | gfx::Rect View::GetBounds(PositionMirroringSettings settings) const { |
| 99 | gfx::Rect bounds(bounds_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | |
| 101 | // If the parent uses an RTL UI layout and if we are asked to transform the |
| 102 | // bounds to their mirrored position if necessary, then we should shift the |
| 103 | // rectangle appropriately. |
[email protected] | 0d8ea70 | 2008-10-14 17:03:07 | [diff] [blame] | 104 | if (settings == APPLY_MIRRORING_TRANSFORMATION) |
| 105 | bounds.set_x(MirroredX()); |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 106 | |
[email protected] | 0d8ea70 | 2008-10-14 17:03:07 | [diff] [blame] | 107 | return bounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 110 | // y(), width() and height() are agnostic to the RTL UI layout of the |
| 111 | // parent view. x(), on the other hand, is not. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | int View::GetX(PositionMirroringSettings settings) const { |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 113 | return settings == IGNORE_MIRRORING_TRANSFORMATION ? x() : MirroredX(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 116 | void View::SetBounds(const gfx::Rect& bounds) { |
[email protected] | de8d613d | 2010-10-25 22:30:10 | [diff] [blame] | 117 | if (bounds == bounds_) { |
| 118 | if (needs_layout_) { |
| 119 | needs_layout_ = false; |
| 120 | Layout(); |
| 121 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | return; |
[email protected] | de8d613d | 2010-10-25 22:30:10 | [diff] [blame] | 123 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 125 | gfx::Rect prev = bounds_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | bounds_ = bounds; |
[email protected] | b97c8a2 | 2010-02-05 19:36:43 | [diff] [blame] | 127 | bool size_changed = prev.size() != bounds_.size(); |
| 128 | bool position_changed = prev.origin() != bounds_.origin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | |
[email protected] | b97c8a2 | 2010-02-05 19:36:43 | [diff] [blame] | 130 | if (size_changed || position_changed) { |
| 131 | DidChangeBounds(prev, bounds_); |
| 132 | |
| 133 | RootView* root = GetRootView(); |
| 134 | if (root) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | root->ViewBoundsChanged(this, size_changed, position_changed); |
| 136 | } |
| 137 | } |
| 138 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 139 | gfx::Rect View::GetLocalBounds(bool include_border) const { |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 140 | if (include_border || !border_.get()) |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 141 | return gfx::Rect(0, 0, width(), height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 143 | gfx::Insets insets; |
| 144 | border_->GetInsets(&insets); |
| 145 | return gfx::Rect(insets.left(), insets.top(), |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 146 | std::max(0, width() - insets.width()), |
| 147 | std::max(0, height() - insets.height())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 150 | gfx::Point View::GetPosition() const { |
| 151 | return gfx::Point(GetX(APPLY_MIRRORING_TRANSFORMATION), y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 154 | gfx::Size View::GetPreferredSize() { |
| 155 | if (layout_manager_.get()) |
| 156 | return layout_manager_->GetPreferredSize(this); |
| 157 | return gfx::Size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | a136016 | 2009-11-30 21:19:07 | [diff] [blame] | 160 | int View::GetBaseline() { |
| 161 | return -1; |
| 162 | } |
| 163 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | void View::SizeToPreferredSize() { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 165 | gfx::Size prefsize = GetPreferredSize(); |
| 166 | if ((prefsize.width() != width()) || (prefsize.height() != height())) |
| 167 | SetBounds(x(), y(), prefsize.width(), prefsize.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 7ccc52b7 | 2009-05-08 21:09:11 | [diff] [blame] | 170 | void View::PreferredSizeChanged() { |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 171 | InvalidateLayout(); |
[email protected] | 7ccc52b7 | 2009-05-08 21:09:11 | [diff] [blame] | 172 | if (parent_) |
| 173 | parent_->ChildPreferredSizeChanged(this); |
| 174 | } |
| 175 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 176 | gfx::Size View::GetMinimumSize() { |
| 177 | return GetPreferredSize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int View::GetHeightForWidth(int w) { |
| 181 | if (layout_manager_.get()) |
| 182 | return layout_manager_->GetPreferredHeightForWidth(this, w); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 183 | return GetPreferredSize().height(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 186 | void View::DidChangeBounds(const gfx::Rect& previous, |
| 187 | const gfx::Rect& current) { |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 188 | needs_layout_ = false; |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 189 | Layout(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 192 | void View::ScrollRectToVisible(const gfx::Rect& rect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | View* parent = GetParent(); |
| 194 | |
| 195 | // We must take RTL UI mirroring into account when adjusting the position of |
| 196 | // the region. |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 197 | if (parent) { |
| 198 | gfx::Rect scroll_rect(rect); |
| 199 | scroll_rect.Offset(GetX(APPLY_MIRRORING_TRANSFORMATION), y()); |
| 200 | parent->ScrollRectToVisible(scroll_rect); |
| 201 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | ///////////////////////////////////////////////////////////////////////////// |
| 205 | // |
| 206 | // View - layout |
| 207 | // |
| 208 | ///////////////////////////////////////////////////////////////////////////// |
| 209 | |
| 210 | void View::Layout() { |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 211 | needs_layout_ = false; |
| 212 | |
[email protected] | f861719 | 2010-07-05 07:57:10 | [diff] [blame] | 213 | // If we have a layout manager, let it handle the layout for us. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | if (layout_manager_.get()) { |
| 215 | layout_manager_->Layout(this); |
| 216 | SchedulePaint(); |
| 217 | } |
| 218 | |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 219 | // Make sure to propagate the Layout() call to any children that haven't |
| 220 | // received it yet through the layout manager and need to be laid out. This |
| 221 | // is needed for the case when the child requires a layout but its bounds |
| 222 | // weren't changed by the layout manager. If there is no layout manager, we |
| 223 | // just propagate the Layout() call down the hierarchy, so whoever receives |
| 224 | // the call can take appropriate action. |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 225 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | View* child = GetChildViewAt(i); |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 227 | if (child->needs_layout_ || !layout_manager_.get()) { |
| 228 | child->needs_layout_ = false; |
| 229 | child->Layout(); |
| 230 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 234 | void View::InvalidateLayout() { |
[email protected] | de8d613d | 2010-10-25 22:30:10 | [diff] [blame] | 235 | // Always invalidate up. This is needed to handle the case of us already being |
| 236 | // valid, but not our parent. |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 237 | needs_layout_ = true; |
| 238 | if (parent_) |
| 239 | parent_->InvalidateLayout(); |
| 240 | } |
| 241 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | LayoutManager* View::GetLayoutManager() const { |
| 243 | return layout_manager_.get(); |
| 244 | } |
| 245 | |
| 246 | void View::SetLayoutManager(LayoutManager* layout_manager) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 247 | if (layout_manager_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | layout_manager_->Uninstalled(this); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 249 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | layout_manager_.reset(layout_manager); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 251 | if (layout_manager_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | layout_manager_->Installed(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | //////////////////////////////////////////////////////////////////////////////// |
| 256 | // |
| 257 | // View - Right-to-left UI layout |
| 258 | // |
| 259 | //////////////////////////////////////////////////////////////////////////////// |
| 260 | |
[email protected] | c3bca42 | 2010-02-19 23:29:02 | [diff] [blame] | 261 | int View::MirroredX() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | View* parent = GetParent(); |
[email protected] | aaa71da5 | 2010-02-19 21:39:24 | [diff] [blame] | 263 | return parent ? parent->MirroredLeftPointForRect(bounds_) : x(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int View::MirroredLeftPointForRect(const gfx::Rect& bounds) const { |
[email protected] | c2f4bdb7 | 2010-05-10 23:15:21 | [diff] [blame] | 267 | return base::i18n::IsRTL() ? |
[email protected] | aaa71da5 | 2010-02-19 21:39:24 | [diff] [blame] | 268 | (width() - bounds.x() - bounds.width()) : bounds.x(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | //////////////////////////////////////////////////////////////////////////////// |
| 272 | // |
| 273 | // View - states |
| 274 | // |
| 275 | //////////////////////////////////////////////////////////////////////////////// |
| 276 | |
| 277 | bool View::IsEnabled() const { |
| 278 | return enabled_; |
| 279 | } |
| 280 | |
| 281 | void View::SetEnabled(bool state) { |
| 282 | if (enabled_ != state) { |
| 283 | enabled_ = state; |
| 284 | SchedulePaint(); |
| 285 | } |
| 286 | } |
| 287 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | void View::SetFocusable(bool focusable) { |
| 289 | focusable_ = focusable; |
| 290 | } |
| 291 | |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 292 | bool View::IsFocusableInRootView() const { |
| 293 | return IsFocusable() && IsVisibleInRootView(); |
| 294 | } |
| 295 | |
| 296 | bool View::IsAccessibilityFocusableInRootView() const { |
[email protected] | 1aa3655 | 2010-07-14 08:21:25 | [diff] [blame] | 297 | return (focusable_ || accessibility_focusable_) && IsEnabled() && |
| 298 | IsVisibleInRootView(); |
[email protected] | 83548a4 | 2010-06-18 13:53:37 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | 82166b6 | 2009-06-30 18:48:00 | [diff] [blame] | 301 | FocusManager* View::GetFocusManager() { |
| 302 | Widget* widget = GetWidget(); |
| 303 | return widget ? widget->GetFocusManager() : NULL; |
| 304 | } |
| 305 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | bool View::HasFocus() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 307 | FocusManager* focus_manager = GetFocusManager(); |
| 308 | if (focus_manager) |
| 309 | return focus_manager->GetFocusedView() == this; |
| 310 | return false; |
| 311 | } |
| 312 | |
[email protected] | 5c9e97a | 2009-09-09 23:48:30 | [diff] [blame] | 313 | void View::Focus() { |
[email protected] | 7f14e1f | 2009-10-01 15:57:26 | [diff] [blame] | 314 | // By default, we clear the native focus. This ensures that no visible native |
| 315 | // view as the focus and that we still receive keyboard inputs. |
[email protected] | 5c9e97a | 2009-09-09 23:48:30 | [diff] [blame] | 316 | FocusManager* focus_manager = GetFocusManager(); |
| 317 | if (focus_manager) |
[email protected] | 7f14e1f | 2009-10-01 15:57:26 | [diff] [blame] | 318 | focus_manager->ClearNativeFocus(); |
[email protected] | 6a054ffa | 2010-08-06 07:07:18 | [diff] [blame] | 319 | |
| 320 | // Notify assistive technologies of the focus change. |
| 321 | NotifyAccessibilityEvent(AccessibilityTypes::EVENT_FOCUS); |
| 322 | } |
| 323 | |
| 324 | void View::NotifyAccessibilityEvent(AccessibilityTypes::Event event_type) { |
| 325 | NotifyAccessibilityEvent(event_type, true); |
[email protected] | 5c9e97a | 2009-09-09 23:48:30 | [diff] [blame] | 326 | } |
| 327 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | void View::SetHotTracked(bool flag) { |
| 329 | } |
| 330 | |
| 331 | ///////////////////////////////////////////////////////////////////////////// |
| 332 | // |
| 333 | // View - painting |
| 334 | // |
| 335 | ///////////////////////////////////////////////////////////////////////////// |
| 336 | |
[email protected] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 337 | void View::SchedulePaint(const gfx::Rect& r, bool urgent) { |
| 338 | if (!IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 339 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 340 | |
| 341 | if (parent_) { |
| 342 | // Translate the requested paint rect to the parent's coordinate system |
| 343 | // then pass this notification up to the parent. |
[email protected] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 344 | gfx::Rect paint_rect = r; |
| 345 | paint_rect.Offset(GetPosition()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | parent_->SchedulePaint(paint_rect, urgent); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | void View::SchedulePaint() { |
[email protected] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 351 | SchedulePaint(GetLocalBounds(true), false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 354 | void View::Paint(gfx::Canvas* canvas) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 355 | PaintBackground(canvas); |
| 356 | PaintFocusBorder(canvas); |
| 357 | PaintBorder(canvas); |
| 358 | } |
| 359 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 360 | void View::PaintBackground(gfx::Canvas* canvas) { |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 361 | if (background_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | background_->Paint(canvas, this); |
| 363 | } |
| 364 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 365 | void View::PaintBorder(gfx::Canvas* canvas) { |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 366 | if (border_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | border_->Paint(*this, canvas); |
| 368 | } |
| 369 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 370 | void View::PaintFocusBorder(gfx::Canvas* canvas) { |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 371 | if (HasFocus() && (IsFocusable() || IsAccessibilityFocusableInRootView())) |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 372 | canvas->DrawFocusRect(0, 0, width(), height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 375 | void View::PaintChildren(gfx::Canvas* canvas) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 376 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | View* child = GetChildViewAt(i); |
| 378 | if (!child) { |
| 379 | NOTREACHED() << "Should not have a NULL child View for index in bounds"; |
| 380 | continue; |
| 381 | } |
| 382 | child->ProcessPaint(canvas); |
| 383 | } |
| 384 | } |
| 385 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 386 | void View::ProcessPaint(gfx::Canvas* canvas) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 387 | if (!IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | |
| 390 | // We're going to modify the canvas, save it's state first. |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 391 | canvas->Save(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | |
| 393 | // Paint this View and its children, setting the clip rect to the bounds |
| 394 | // of this View and translating the origin to the local bounds' top left |
| 395 | // point. |
| 396 | // |
| 397 | // Note that the X (or left) position we pass to ClipRectInt takes into |
| 398 | // consideration whether or not the view uses a right-to-left layout so that |
| 399 | // we paint our view in its mirrored position if need be. |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 400 | if (canvas->ClipRectInt(MirroredX(), y(), width(), height())) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | // Non-empty clip, translate the graphics such that 0,0 corresponds to |
| 402 | // where this view is located (related to its parent). |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 403 | canvas->TranslateInt(MirroredX(), y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | // If the View we are about to paint requested the canvas to be flipped, we |
| 406 | // should change the transform appropriately. |
[email protected] | abffeda | 2010-08-13 22:35:50 | [diff] [blame] | 407 | canvas->Save(); |
| 408 | if (FlipCanvasOnPaintForRTLUI()) { |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 409 | canvas->TranslateInt(width(), 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | canvas->ScaleInt(-1, 1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | Paint(canvas); |
| 414 | |
| 415 | // We must undo the canvas mirroring once the View is done painting so that |
| 416 | // we don't pass the canvas with the mirrored transform to Views that |
| 417 | // didn't request the canvas to be flipped. |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 418 | canvas->Restore(); |
[email protected] | abffeda | 2010-08-13 22:35:50 | [diff] [blame] | 419 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | PaintChildren(canvas); |
| 421 | } |
| 422 | |
| 423 | // Restore the canvas's original transform. |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 424 | canvas->Restore(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void View::PaintNow() { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 428 | if (!IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 430 | |
| 431 | View* view = GetParent(); |
| 432 | if (view) |
| 433 | view->PaintNow(); |
| 434 | } |
| 435 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | gfx::Insets View::GetInsets() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | gfx::Insets insets; |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 438 | if (border_.get()) |
| 439 | border_->GetInsets(&insets); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 440 | return insets; |
| 441 | } |
| 442 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 443 | gfx::NativeCursor View::GetCursorForPoint(Event::EventType event_type, |
| 444 | const gfx::Point& p) { |
[email protected] | 9abf8dd6 | 2009-06-04 06:40:42 | [diff] [blame] | 445 | return NULL; |
| 446 | } |
| 447 | |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 448 | bool View::HitTest(const gfx::Point& l) const { |
[email protected] | d7fc9794 | 2009-11-20 22:07:11 | [diff] [blame] | 449 | if (l.x() >= 0 && l.x() < width() && l.y() >= 0 && l.y() < height()) { |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 450 | if (HasHitTestMask()) { |
| 451 | gfx::Path mask; |
| 452 | GetHitTestMask(&mask); |
[email protected] | a340697 | 2009-11-04 05:05:48 | [diff] [blame] | 453 | ScopedRegion rgn(mask.CreateNativeRegion()); |
| 454 | // TODO: can this use SkRegion's contains instead? |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 455 | #if defined(OS_WIN) |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 456 | return !!PtInRegion(rgn, l.x(), l.y()); |
[email protected] | 10a6e77b | 2009-12-31 01:03:52 | [diff] [blame] | 457 | #elif defined(TOOLKIT_USES_GTK) |
[email protected] | a340697 | 2009-11-04 05:05:48 | [diff] [blame] | 458 | return gdk_region_point_in(rgn.Get(), l.x(), l.y()); |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 459 | #endif |
| 460 | } |
| 461 | // No mask, but inside our bounds. |
| 462 | return true; |
| 463 | } |
| 464 | // Outside our bounds. |
| 465 | return false; |
| 466 | } |
| 467 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 468 | void View::SetContextMenuController(ContextMenuController* menu_controller) { |
| 469 | context_menu_controller_ = menu_controller; |
| 470 | } |
| 471 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 472 | void View::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) { |
[email protected] | 042811c | 2008-10-31 21:31:34 | [diff] [blame] | 473 | if (!context_menu_controller_) |
| 474 | return; |
| 475 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 476 | context_menu_controller_->ShowContextMenu(this, p, is_mouse_gesture); |
[email protected] | 042811c | 2008-10-31 21:31:34 | [diff] [blame] | 477 | } |
| 478 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | ///////////////////////////////////////////////////////////////////////////// |
| 480 | // |
| 481 | // View - tree |
| 482 | // |
| 483 | ///////////////////////////////////////////////////////////////////////////// |
| 484 | |
| 485 | bool View::ProcessMousePressed(const MouseEvent& e, DragInfo* drag_info) { |
[email protected] | 83548a4 | 2010-06-18 13:53:37 | [diff] [blame] | 486 | const bool enabled = IsEnabled(); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 487 | int drag_operations = |
| 488 | (enabled && e.IsOnlyLeftMouseButton() && HitTest(e.location())) ? |
| 489 | GetDragOperations(e.location()) : 0; |
| 490 | ContextMenuController* context_menu_controller = e.IsRightMouseButton() ? |
| 491 | context_menu_controller_ : 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 492 | |
| 493 | const bool result = OnMousePressed(e); |
| 494 | // WARNING: we may have been deleted, don't use any View variables; |
| 495 | |
| 496 | if (!enabled) |
| 497 | return result; |
| 498 | |
| 499 | if (drag_operations != DragDropTypes::DRAG_NONE) { |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 500 | drag_info->PossibleDrag(e.location()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 501 | return true; |
| 502 | } |
| 503 | return !!context_menu_controller || result; |
| 504 | } |
| 505 | |
| 506 | bool View::ProcessMouseDragged(const MouseEvent& e, DragInfo* drag_info) { |
| 507 | // Copy the field, that way if we're deleted after drag and drop no harm is |
| 508 | // done. |
| 509 | ContextMenuController* context_menu_controller = context_menu_controller_; |
| 510 | const bool possible_drag = drag_info->possible_drag; |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 511 | if (possible_drag && ExceededDragThreshold(drag_info->start_pt.x() - e.x(), |
| 512 | drag_info->start_pt.y() - e.y())) { |
[email protected] | b5f94de | 2009-12-04 07:59:00 | [diff] [blame] | 513 | if (!drag_controller_ || |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 514 | drag_controller_->CanStartDrag(this, drag_info->start_pt, e.location())) |
| 515 | DoDrag(e, drag_info->start_pt); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | } else { |
| 517 | if (OnMouseDragged(e)) |
| 518 | return true; |
| 519 | // Fall through to return value based on context menu controller. |
| 520 | } |
| 521 | // WARNING: we may have been deleted. |
| 522 | return (context_menu_controller != NULL) || possible_drag; |
| 523 | } |
| 524 | |
| 525 | void View::ProcessMouseReleased(const MouseEvent& e, bool canceled) { |
| 526 | if (!canceled && context_menu_controller_ && e.IsOnlyRightMouseButton()) { |
| 527 | // Assume that if there is a context menu controller we won't be deleted |
| 528 | // from mouse released. |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 529 | gfx::Point location(e.location()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 530 | OnMouseReleased(e, canceled); |
[email protected] | 464fdb3 | 2009-03-19 20:25:44 | [diff] [blame] | 531 | if (HitTest(location)) { |
| 532 | ConvertPointToScreen(this, &location); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 533 | ShowContextMenu(location, true); |
[email protected] | 464fdb3 | 2009-03-19 20:25:44 | [diff] [blame] | 534 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 535 | } else { |
| 536 | OnMouseReleased(e, canceled); |
| 537 | } |
| 538 | // WARNING: we may have been deleted. |
| 539 | } |
| 540 | |
[email protected] | ad84c8f | 2010-09-08 14:06:10 | [diff] [blame] | 541 | #if defined(TOUCH_UI) |
| 542 | bool View::ProcessTouchEvent(const TouchEvent& e) { |
| 543 | // TODO(rjkroege): Implement a grab scheme similar to as |
| 544 | // as is found in MousePressed. |
| 545 | const bool result = OnTouchEvent(e); |
| 546 | return result; |
| 547 | } |
| 548 | #endif |
| 549 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 550 | void View::AddChildView(View* v) { |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 551 | AddChildView(static_cast<int>(child_views_.size()), v); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void View::AddChildView(int index, View* v) { |
[email protected] | 0b2fa2b | 2009-12-01 01:06:12 | [diff] [blame] | 555 | CHECK(v != this) << "You cannot add a view as its own child"; |
| 556 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 557 | // Remove the view from its current parent if any. |
| 558 | if (v->GetParent()) |
| 559 | v->GetParent()->RemoveChildView(v); |
| 560 | |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 561 | // Sets the prev/next focus views. |
| 562 | InitFocusSiblings(v, index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 563 | |
| 564 | // Let's insert the view. |
| 565 | child_views_.insert(child_views_.begin() + index, v); |
| 566 | v->SetParent(this); |
| 567 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 568 | for (View* p = this; p; p = p->GetParent()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 569 | p->ViewHierarchyChangedImpl(false, true, this, v); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 570 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | v->PropagateAddNotifications(this, v); |
| 572 | UpdateTooltip(); |
| 573 | RootView* root = GetRootView(); |
| 574 | if (root) |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 575 | RegisterChildrenForVisibleBoundsNotification(root, v); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | |
| 577 | if (layout_manager_.get()) |
| 578 | layout_manager_->ViewAdded(this, v); |
| 579 | } |
| 580 | |
| 581 | View* View::GetChildViewAt(int index) const { |
| 582 | return index < GetChildViewCount() ? child_views_[index] : NULL; |
| 583 | } |
| 584 | |
| 585 | int View::GetChildViewCount() const { |
| 586 | return static_cast<int>(child_views_.size()); |
| 587 | } |
| 588 | |
[email protected] | 3a9c26f | 2010-03-12 21:04:39 | [diff] [blame] | 589 | bool View::HasChildView(View* a_view) { |
| 590 | return find(child_views_.begin(), |
| 591 | child_views_.end(), |
| 592 | a_view) != child_views_.end(); |
| 593 | } |
| 594 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 595 | void View::RemoveChildView(View* a_view) { |
| 596 | DoRemoveChildView(a_view, true, true, false); |
| 597 | } |
| 598 | |
| 599 | void View::RemoveAllChildViews(bool delete_views) { |
| 600 | ViewList::iterator iter; |
| 601 | while ((iter = child_views_.begin()) != child_views_.end()) { |
| 602 | DoRemoveChildView(*iter, false, false, delete_views); |
| 603 | } |
| 604 | UpdateTooltip(); |
| 605 | } |
| 606 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 607 | void View::DoDrag(const MouseEvent& e, const gfx::Point& press_pt) { |
| 608 | int drag_operations = GetDragOperations(press_pt); |
[email protected] | 4768c65b | 2009-08-28 21:42:59 | [diff] [blame] | 609 | if (drag_operations == DragDropTypes::DRAG_NONE) |
| 610 | return; |
| 611 | |
| 612 | OSExchangeData data; |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 613 | WriteDragData(press_pt, &data); |
[email protected] | 4768c65b | 2009-08-28 21:42:59 | [diff] [blame] | 614 | |
| 615 | // Message the RootView to do the drag and drop. That way if we're removed |
| 616 | // the RootView can detect it and avoid calling us back. |
| 617 | RootView* root_view = GetRootView(); |
| 618 | root_view->StartDragForViewFromMouseEvent(this, data, drag_operations); |
| 619 | } |
| 620 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 621 | void View::DoRemoveChildView(View* a_view, |
| 622 | bool update_focus_cycle, |
| 623 | bool update_tool_tip, |
| 624 | bool delete_removed_view) { |
| 625 | #ifndef NDEBUG |
| 626 | DCHECK(!IsProcessingPaint()) << "Should not be removing a child view " << |
| 627 | "during a paint, this will seriously " << |
| 628 | "mess things up!"; |
| 629 | #endif |
| 630 | DCHECK(a_view); |
| 631 | const ViewList::iterator i = find(child_views_.begin(), |
| 632 | child_views_.end(), |
| 633 | a_view); |
| 634 | if (i != child_views_.end()) { |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 635 | if (update_focus_cycle) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 636 | // Let's remove the view from the focus traversal. |
| 637 | View* next_focusable = a_view->next_focusable_view_; |
| 638 | View* prev_focusable = a_view->previous_focusable_view_; |
| 639 | if (prev_focusable) |
| 640 | prev_focusable->next_focusable_view_ = next_focusable; |
| 641 | if (next_focusable) |
| 642 | next_focusable->previous_focusable_view_ = prev_focusable; |
| 643 | } |
| 644 | |
| 645 | RootView* root = GetRootView(); |
| 646 | if (root) |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 647 | UnregisterChildrenForVisibleBoundsNotification(root, a_view); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 648 | a_view->PropagateRemoveNotifications(this); |
| 649 | a_view->SetParent(NULL); |
| 650 | |
| 651 | if (delete_removed_view && a_view->IsParentOwned()) |
| 652 | delete a_view; |
| 653 | |
| 654 | child_views_.erase(i); |
| 655 | } |
| 656 | |
| 657 | if (update_tool_tip) |
| 658 | UpdateTooltip(); |
| 659 | |
| 660 | if (layout_manager_.get()) |
| 661 | layout_manager_->ViewRemoved(this, a_view); |
| 662 | } |
| 663 | |
| 664 | void View::PropagateRemoveNotifications(View* parent) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 665 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 666 | GetChildViewAt(i)->PropagateRemoveNotifications(parent); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 667 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 668 | for (View* v = this; v; v = v->GetParent()) |
| 669 | v->ViewHierarchyChangedImpl(true, false, parent, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void View::PropagateAddNotifications(View* parent, View* child) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 673 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 674 | GetChildViewAt(i)->PropagateAddNotifications(parent, child); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 675 | ViewHierarchyChangedImpl(true, true, parent, child); |
| 676 | } |
| 677 | |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 678 | bool View::IsFocusable() const { |
| 679 | return focusable_ && IsEnabled() && IsVisible(); |
| 680 | } |
| 681 | |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 682 | void View::PropagateThemeChanged() { |
[email protected] | b2b71801 | 2010-03-25 15:09:06 | [diff] [blame] | 683 | for (int i = GetChildViewCount() - 1; i >= 0; --i) |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 684 | GetChildViewAt(i)->PropagateThemeChanged(); |
| 685 | OnThemeChanged(); |
[email protected] | 32670b0 | 2009-03-03 00:28:00 | [diff] [blame] | 686 | } |
| 687 | |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 688 | void View::PropagateLocaleChanged() { |
[email protected] | b2b71801 | 2010-03-25 15:09:06 | [diff] [blame] | 689 | for (int i = GetChildViewCount() - 1; i >= 0; --i) |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 690 | GetChildViewAt(i)->PropagateLocaleChanged(); |
| 691 | OnLocaleChanged(); |
[email protected] | b2b71801 | 2010-03-25 15:09:06 | [diff] [blame] | 692 | } |
| 693 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 694 | #ifndef NDEBUG |
| 695 | bool View::IsProcessingPaint() const { |
| 696 | return GetParent() && GetParent()->IsProcessingPaint(); |
| 697 | } |
| 698 | #endif |
| 699 | |
[email protected] | f704ee7 | 2008-11-10 21:31:59 | [diff] [blame] | 700 | gfx::Point View::GetKeyboardContextMenuLocation() { |
| 701 | gfx::Rect vis_bounds = GetVisibleBounds(); |
| 702 | gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2, |
| 703 | vis_bounds.y() + vis_bounds.height() / 2); |
| 704 | ConvertPointToScreen(this, &screen_point); |
| 705 | return screen_point; |
| 706 | } |
| 707 | |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 708 | bool View::HasHitTestMask() const { |
| 709 | return false; |
| 710 | } |
| 711 | |
| 712 | void View::GetHitTestMask(gfx::Path* mask) const { |
| 713 | DCHECK(mask); |
| 714 | } |
| 715 | |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 716 | void View::ViewHierarchyChanged(bool is_add, |
| 717 | View* parent, |
| 718 | View* child) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void View::ViewHierarchyChangedImpl(bool register_accelerators, |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 722 | bool is_add, |
| 723 | View* parent, |
| 724 | View* child) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 725 | if (register_accelerators) { |
| 726 | if (is_add) { |
| 727 | // If you get this registration, you are part of a subtree that has been |
| 728 | // added to the view hierarchy. |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 729 | if (GetFocusManager()) { |
| 730 | RegisterPendingAccelerators(); |
| 731 | } else { |
| 732 | // Delay accelerator registration until visible as we do not have |
| 733 | // focus manager until then. |
| 734 | accelerator_registration_delayed_ = true; |
| 735 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 736 | } else { |
| 737 | if (child == this) |
[email protected] | fa1cf0b8 | 2010-01-15 21:49:44 | [diff] [blame] | 738 | UnregisterAccelerators(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | ViewHierarchyChanged(is_add, parent, child); |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 743 | parent->needs_layout_ = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | void View::PropagateVisibilityNotifications(View* start, bool is_visible) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 747 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 748 | GetChildViewAt(i)->PropagateVisibilityNotifications(start, is_visible); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 749 | VisibilityChanged(start, is_visible); |
| 750 | } |
| 751 | |
| 752 | void View::VisibilityChanged(View* starting_from, bool is_visible) { |
| 753 | } |
| 754 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 755 | void View::PropagateNativeViewHierarchyChanged(bool attached, |
| 756 | gfx::NativeView native_view, |
| 757 | RootView* root_view) { |
| 758 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
| 759 | GetChildViewAt(i)->PropagateNativeViewHierarchyChanged(attached, |
| 760 | native_view, |
| 761 | root_view); |
| 762 | NativeViewHierarchyChanged(attached, native_view, root_view); |
| 763 | } |
| 764 | |
| 765 | void View::NativeViewHierarchyChanged(bool attached, |
| 766 | gfx::NativeView native_view, |
| 767 | RootView* root_view) { |
| 768 | FocusManager* focus_manager = GetFocusManager(); |
| 769 | if (!accelerator_registration_delayed_ && |
| 770 | accelerator_focus_manager_ && |
| 771 | accelerator_focus_manager_ != focus_manager) { |
| 772 | UnregisterAccelerators(true); |
| 773 | accelerator_registration_delayed_ = true; |
| 774 | } |
| 775 | if (accelerator_registration_delayed_ && attached) { |
| 776 | if (focus_manager) { |
| 777 | RegisterPendingAccelerators(); |
| 778 | accelerator_registration_delayed_ = false; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 783 | void View::SetNotifyWhenVisibleBoundsInRootChanges(bool value) { |
| 784 | if (notify_when_visible_bounds_in_root_changes_ == value) |
| 785 | return; |
| 786 | notify_when_visible_bounds_in_root_changes_ = value; |
| 787 | RootView* root = GetRootView(); |
| 788 | if (root) { |
| 789 | if (value) |
| 790 | root->RegisterViewForVisibleBoundsNotification(this); |
| 791 | else |
| 792 | root->UnregisterViewForVisibleBoundsNotification(this); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | bool View::GetNotifyWhenVisibleBoundsInRootChanges() { |
| 797 | return notify_when_visible_bounds_in_root_changes_; |
| 798 | } |
| 799 | |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 800 | View* View::GetViewForPoint(const gfx::Point& point) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 801 | // Walk the child Views recursively looking for the View that most |
| 802 | // tightly encloses the specified point. |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 803 | for (int i = GetChildViewCount() - 1; i >= 0; --i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 804 | View* child = GetChildViewAt(i); |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 805 | if (!child->IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 806 | continue; |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 807 | |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 808 | gfx::Point point_in_child_coords(point); |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 809 | View::ConvertPointToView(this, child, &point_in_child_coords); |
[email protected] | 613b806 | 2008-10-14 23:45:09 | [diff] [blame] | 810 | if (child->HitTest(point_in_child_coords)) |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 811 | return child->GetViewForPoint(point_in_child_coords); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 812 | } |
| 813 | return this; |
| 814 | } |
| 815 | |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 816 | Widget* View::GetWidget() const { |
| 817 | // The root view holds a reference to this view hierarchy's Widget. |
| 818 | return parent_ ? parent_->GetWidget() : NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 819 | } |
| 820 | |
[email protected] | cd8c4790 | 2009-04-30 20:55:35 | [diff] [blame] | 821 | Window* View::GetWindow() const { |
| 822 | Widget* widget = GetWidget(); |
| 823 | return widget ? widget->GetWindow() : NULL; |
| 824 | } |
| 825 | |
[email protected] | 2db27be | 2010-02-10 22:46:47 | [diff] [blame] | 826 | bool View::ContainsNativeView(gfx::NativeView native_view) const { |
| 827 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) { |
| 828 | if (GetChildViewAt(i)->ContainsNativeView(native_view)) |
| 829 | return true; |
| 830 | } |
| 831 | return false; |
| 832 | } |
| 833 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 834 | // Get the containing RootView |
| 835 | RootView* View::GetRootView() { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 836 | Widget* widget = GetWidget(); |
| 837 | return widget ? widget->GetRootView() : NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | View* View::GetViewByID(int id) const { |
| 841 | if (id == id_) |
| 842 | return const_cast<View*>(this); |
| 843 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 844 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 845 | View* child = GetChildViewAt(i); |
| 846 | View* view = child->GetViewByID(id); |
| 847 | if (view) |
| 848 | return view; |
| 849 | } |
| 850 | return NULL; |
| 851 | } |
| 852 | |
| 853 | void View::GetViewsWithGroup(int group_id, std::vector<View*>* out) { |
| 854 | if (group_ == group_id) |
| 855 | out->push_back(this); |
| 856 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 857 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 858 | GetChildViewAt(i)->GetViewsWithGroup(group_id, out); |
| 859 | } |
| 860 | |
| 861 | View* View::GetSelectedViewForGroup(int group_id) { |
| 862 | std::vector<View*> views; |
| 863 | GetRootView()->GetViewsWithGroup(group_id, &views); |
| 864 | if (views.size() > 0) |
| 865 | return views[0]; |
| 866 | else |
| 867 | return NULL; |
| 868 | } |
| 869 | |
| 870 | void View::SetID(int id) { |
| 871 | id_ = id; |
| 872 | } |
| 873 | |
| 874 | int View::GetID() const { |
| 875 | return id_; |
| 876 | } |
| 877 | |
| 878 | void View::SetGroup(int gid) { |
[email protected] | 96f960d | 2009-09-14 18:45:30 | [diff] [blame] | 879 | // Don't change the group id once it's set. |
| 880 | DCHECK(group_ == -1 || group_ == gid); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 881 | group_ = gid; |
| 882 | } |
| 883 | |
| 884 | int View::GetGroup() const { |
| 885 | return group_; |
| 886 | } |
| 887 | |
| 888 | void View::SetParent(View* parent) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 889 | if (parent != parent_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 890 | parent_ = parent; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | bool View::IsParentOf(View* v) const { |
| 894 | DCHECK(v); |
| 895 | View* parent = v->GetParent(); |
| 896 | while (parent) { |
| 897 | if (this == parent) |
| 898 | return true; |
| 899 | parent = parent->GetParent(); |
| 900 | } |
| 901 | return false; |
| 902 | } |
| 903 | |
[email protected] | 5e26d9d4 | 2010-05-13 22:11:03 | [diff] [blame] | 904 | int View::GetChildIndex(const View* v) const { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 905 | for (int i = 0, count = GetChildViewCount(); i < count; i++) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 906 | if (v == GetChildViewAt(i)) |
| 907 | return i; |
| 908 | } |
| 909 | return -1; |
| 910 | } |
| 911 | |
| 912 | /////////////////////////////////////////////////////////////////////////////// |
| 913 | // |
| 914 | // View - focus |
| 915 | // |
| 916 | /////////////////////////////////////////////////////////////////////////////// |
| 917 | |
| 918 | View* View::GetNextFocusableView() { |
| 919 | return next_focusable_view_; |
| 920 | } |
| 921 | |
| 922 | View* View::GetPreviousFocusableView() { |
| 923 | return previous_focusable_view_; |
| 924 | } |
| 925 | |
| 926 | void View::SetNextFocusableView(View* view) { |
| 927 | view->previous_focusable_view_ = this; |
| 928 | next_focusable_view_ = view; |
| 929 | } |
| 930 | |
| 931 | void View::InitFocusSiblings(View* v, int index) { |
| 932 | int child_count = static_cast<int>(child_views_.size()); |
| 933 | |
| 934 | if (child_count == 0) { |
| 935 | v->next_focusable_view_ = NULL; |
| 936 | v->previous_focusable_view_ = NULL; |
| 937 | } else { |
| 938 | if (index == child_count) { |
| 939 | // We are inserting at the end, but the end of the child list may not be |
| 940 | // the last focusable element. Let's try to find an element with no next |
| 941 | // focusable element to link to. |
| 942 | View* last_focusable_view = NULL; |
| 943 | for (std::vector<View*>::iterator iter = child_views_.begin(); |
| 944 | iter != child_views_.end(); ++iter) { |
| 945 | if (!(*iter)->next_focusable_view_) { |
| 946 | last_focusable_view = *iter; |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | if (last_focusable_view == NULL) { |
| 951 | // Hum... there is a cycle in the focus list. Let's just insert ourself |
| 952 | // after the last child. |
| 953 | View* prev = child_views_[index - 1]; |
| 954 | v->previous_focusable_view_ = prev; |
| 955 | v->next_focusable_view_ = prev->next_focusable_view_; |
| 956 | prev->next_focusable_view_->previous_focusable_view_ = v; |
| 957 | prev->next_focusable_view_ = v; |
| 958 | } else { |
| 959 | last_focusable_view->next_focusable_view_ = v; |
| 960 | v->next_focusable_view_ = NULL; |
| 961 | v->previous_focusable_view_ = last_focusable_view; |
| 962 | } |
| 963 | } else { |
| 964 | View* prev = child_views_[index]->GetPreviousFocusableView(); |
| 965 | v->previous_focusable_view_ = prev; |
| 966 | v->next_focusable_view_ = child_views_[index]; |
| 967 | if (prev) |
| 968 | prev->next_focusable_view_ = v; |
| 969 | child_views_[index]->previous_focusable_view_ = v; |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | #ifndef NDEBUG |
| 975 | void View::PrintViewHierarchy() { |
| 976 | PrintViewHierarchyImp(0); |
| 977 | } |
| 978 | |
| 979 | void View::PrintViewHierarchyImp(int indent) { |
| 980 | std::wostringstream buf; |
| 981 | int ind = indent; |
| 982 | while (ind-- > 0) |
| 983 | buf << L' '; |
| 984 | buf << UTF8ToWide(GetClassName()); |
| 985 | buf << L' '; |
| 986 | buf << GetID(); |
| 987 | buf << L' '; |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 988 | buf << bounds_.x() << L"," << bounds_.y() << L","; |
| 989 | buf << bounds_.right() << L"," << bounds_.bottom(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 990 | buf << L' '; |
| 991 | buf << this; |
| 992 | |
[email protected] | b4d13c6 | 2010-10-20 00:11:40 | [diff] [blame] | 993 | VLOG(1) << buf.str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 994 | std::cout << buf.str() << std::endl; |
| 995 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 996 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 997 | GetChildViewAt(i)->PrintViewHierarchyImp(indent + 2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
| 1001 | void View::PrintFocusHierarchy() { |
| 1002 | PrintFocusHierarchyImp(0); |
| 1003 | } |
| 1004 | |
| 1005 | void View::PrintFocusHierarchyImp(int indent) { |
| 1006 | std::wostringstream buf; |
| 1007 | int ind = indent; |
| 1008 | while (ind-- > 0) |
| 1009 | buf << L' '; |
| 1010 | buf << UTF8ToWide(GetClassName()); |
| 1011 | buf << L' '; |
| 1012 | buf << GetID(); |
| 1013 | buf << L' '; |
| 1014 | buf << GetClassName().c_str(); |
| 1015 | buf << L' '; |
| 1016 | buf << this; |
| 1017 | |
[email protected] | b4d13c6 | 2010-10-20 00:11:40 | [diff] [blame] | 1018 | VLOG(1) << buf.str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1019 | std::cout << buf.str() << std::endl; |
| 1020 | |
| 1021 | if (GetChildViewCount() > 0) |
| 1022 | GetChildViewAt(0)->PrintFocusHierarchyImp(indent + 2); |
| 1023 | |
| 1024 | View* v = GetNextFocusableView(); |
| 1025 | if (v) |
| 1026 | v->PrintFocusHierarchyImp(indent); |
| 1027 | } |
| 1028 | #endif |
| 1029 | |
| 1030 | //////////////////////////////////////////////////////////////////////////////// |
| 1031 | // |
| 1032 | // View - accelerators |
| 1033 | // |
| 1034 | //////////////////////////////////////////////////////////////////////////////// |
| 1035 | |
| 1036 | void View::AddAccelerator(const Accelerator& accelerator) { |
| 1037 | if (!accelerators_.get()) |
| 1038 | accelerators_.reset(new std::vector<Accelerator>()); |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1039 | |
| 1040 | std::vector<Accelerator>::iterator iter = |
| 1041 | std::find(accelerators_->begin(), accelerators_->end(), accelerator); |
| 1042 | DCHECK(iter == accelerators_->end()) |
| 1043 | << "Registering the same accelerator multiple times"; |
| 1044 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1045 | accelerators_->push_back(accelerator); |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1046 | RegisterPendingAccelerators(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1047 | } |
| 1048 | |
[email protected] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1049 | void View::RemoveAccelerator(const Accelerator& accelerator) { |
| 1050 | std::vector<Accelerator>::iterator iter; |
| 1051 | if (!accelerators_.get() || |
| 1052 | ((iter = std::find(accelerators_->begin(), accelerators_->end(), |
| 1053 | accelerator)) == accelerators_->end())) { |
| 1054 | NOTREACHED() << "Removing non-existing accelerator"; |
| 1055 | return; |
| 1056 | } |
| 1057 | |
[email protected] | 4bd23f3 | 2009-06-08 20:59:19 | [diff] [blame] | 1058 | size_t index = iter - accelerators_->begin(); |
[email protected] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1059 | accelerators_->erase(iter); |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1060 | if (index >= registered_accelerator_count_) { |
| 1061 | // The accelerator is not registered to FocusManager. |
| 1062 | return; |
| 1063 | } |
| 1064 | --registered_accelerator_count_; |
| 1065 | |
[email protected] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1066 | RootView* root_view = GetRootView(); |
| 1067 | if (!root_view) { |
| 1068 | // We are not part of a view hierarchy, so there is nothing to do as we |
| 1069 | // removed ourselves from accelerators_, we won't be registered when added |
| 1070 | // to one. |
| 1071 | return; |
| 1072 | } |
| 1073 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1074 | // If accelerator_focus_manager_ is NULL then we did not registered |
| 1075 | // accelerators so there is nothing to unregister. |
| 1076 | if (accelerator_focus_manager_) { |
| 1077 | accelerator_focus_manager_->UnregisterAccelerator(accelerator, this); |
[email protected] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1081 | void View::ResetAccelerators() { |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1082 | if (accelerators_.get()) |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1083 | UnregisterAccelerators(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1084 | } |
| 1085 | |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1086 | void View::RegisterPendingAccelerators() { |
| 1087 | if (!accelerators_.get() || |
| 1088 | registered_accelerator_count_ == accelerators_->size()) { |
| 1089 | // No accelerators are waiting for registration. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1090 | return; |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1091 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1092 | |
| 1093 | RootView* root_view = GetRootView(); |
| 1094 | if (!root_view) { |
| 1095 | // We are not yet part of a view hierarchy, we'll register ourselves once |
| 1096 | // added to one. |
| 1097 | return; |
| 1098 | } |
[email protected] | f3735c5d | 2009-03-19 17:26:23 | [diff] [blame] | 1099 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1100 | accelerator_focus_manager_ = GetFocusManager(); |
| 1101 | if (!accelerator_focus_manager_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1102 | // Some crash reports seem to show that we may get cases where we have no |
| 1103 | // focus manager (see bug #1291225). This should never be the case, just |
| 1104 | // making sure we don't crash. |
[email protected] | f8dce00 | 2009-09-10 21:07:04 | [diff] [blame] | 1105 | |
| 1106 | // TODO(jcampan): This fails for a view under WidgetGtk with TYPE_CHILD. |
| 1107 | // (see http://crbug.com/21335) reenable NOTREACHED assertion and |
| 1108 | // verify accelerators works as expected. |
| 1109 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1110 | NOTREACHED(); |
[email protected] | f8dce00 | 2009-09-10 21:07:04 | [diff] [blame] | 1111 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1112 | return; |
| 1113 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1114 | std::vector<Accelerator>::const_iterator iter; |
| 1115 | for (iter = accelerators_->begin() + registered_accelerator_count_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1116 | iter != accelerators_->end(); ++iter) { |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1117 | accelerator_focus_manager_->RegisterAccelerator(*iter, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1118 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1119 | registered_accelerator_count_ = accelerators_->size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1120 | } |
| 1121 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1122 | void View::UnregisterAccelerators(bool leave_data_intact) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1123 | if (!accelerators_.get()) |
| 1124 | return; |
| 1125 | |
| 1126 | RootView* root_view = GetRootView(); |
| 1127 | if (root_view) { |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1128 | if (accelerator_focus_manager_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1129 | // We may not have a FocusManager if the window containing us is being |
| 1130 | // closed, in which case the FocusManager is being deleted so there is |
| 1131 | // nothing to unregister. |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1132 | accelerator_focus_manager_->UnregisterAccelerators(this); |
| 1133 | accelerator_focus_manager_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1134 | } |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1135 | if (!leave_data_intact) { |
| 1136 | accelerators_->clear(); |
| 1137 | accelerators_.reset(); |
| 1138 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1139 | registered_accelerator_count_ = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1143 | int View::GetDragOperations(const gfx::Point& press_pt) { |
| 1144 | return drag_controller_ ? |
| 1145 | drag_controller_->GetDragOperations(this, press_pt) : |
| 1146 | DragDropTypes::DRAG_NONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1147 | } |
| 1148 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1149 | void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1150 | DCHECK(drag_controller_); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1151 | drag_controller_->WriteDragData(this, press_pt, data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | void View::OnDragDone() { |
| 1155 | } |
| 1156 | |
| 1157 | bool View::InDrag() { |
| 1158 | RootView* root_view = GetRootView(); |
| 1159 | return root_view ? (root_view->GetDragView() == this) : false; |
| 1160 | } |
| 1161 | |
[email protected] | d5c8101 | 2010-04-03 00:56:12 | [diff] [blame] | 1162 | bool View::GetAccessibleName(std::wstring* name) { |
| 1163 | DCHECK(name); |
| 1164 | |
| 1165 | if (accessible_name_.empty()) |
| 1166 | return false; |
| 1167 | *name = accessible_name_; |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
[email protected] | 4c671e1 | 2010-09-28 19:30:23 | [diff] [blame] | 1171 | AccessibilityTypes::Role View::GetAccessibleRole() { |
| 1172 | return AccessibilityTypes::ROLE_CLIENT; |
[email protected] | b82a049 | 2010-05-27 23:00:03 | [diff] [blame] | 1173 | } |
| 1174 | |
[email protected] | d5c8101 | 2010-04-03 00:56:12 | [diff] [blame] | 1175 | void View::SetAccessibleName(const std::wstring& name) { |
| 1176 | accessible_name_ = name; |
| 1177 | } |
| 1178 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1179 | // static |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 1180 | void View::ConvertPointToView(const View* src, |
| 1181 | const View* dst, |
| 1182 | gfx::Point* point) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1183 | ConvertPointToView(src, dst, point, true); |
| 1184 | } |
| 1185 | |
| 1186 | // static |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 1187 | void View::ConvertPointToView(const View* src, |
| 1188 | const View* dst, |
| 1189 | gfx::Point* point, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1190 | bool try_other_direction) { |
| 1191 | // src can be NULL |
| 1192 | DCHECK(dst); |
| 1193 | DCHECK(point); |
| 1194 | |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 1195 | const View* v; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1196 | gfx::Point offset; |
| 1197 | |
| 1198 | for (v = dst; v && v != src; v = v->GetParent()) { |
| 1199 | offset.SetPoint(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION), |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1200 | offset.y() + v->y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | // The source was not found. The caller wants a conversion |
| 1204 | // from a view to a transitive parent. |
| 1205 | if (src && v == NULL && try_other_direction) { |
| 1206 | gfx::Point p; |
| 1207 | // note: try_other_direction is force to FALSE so we don't |
| 1208 | // end up in an infinite recursion should both src and dst |
| 1209 | // are not parented. |
| 1210 | ConvertPointToView(dst, src, &p, false); |
| 1211 | // since the src and dst are inverted, p should also be negated |
| 1212 | point->SetPoint(point->x() - p.x(), point->y() - p.y()); |
| 1213 | } else { |
| 1214 | point->SetPoint(point->x() - offset.x(), point->y() - offset.y()); |
| 1215 | |
| 1216 | // If src is NULL, sp is in the screen coordinate system |
| 1217 | if (src == NULL) { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1218 | Widget* widget = dst->GetWidget(); |
| 1219 | if (widget) { |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1220 | gfx::Rect b; |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1221 | widget->GetBounds(&b, false); |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1222 | point->SetPoint(point->x() - b.x(), point->y() - b.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | // static |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1229 | void View::ConvertPointToWidget(const View* src, gfx::Point* p) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1230 | DCHECK(src); |
| 1231 | DCHECK(p); |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1232 | |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1233 | gfx::Point offset; |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1234 | for (const View* v = src; v; v = v->GetParent()) { |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1235 | offset.set_x(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION)); |
| 1236 | offset.set_y(offset.y() + v->y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1237 | } |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1238 | p->SetPoint(p->x() + offset.x(), p->y() + offset.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | // static |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1242 | void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) { |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1243 | gfx::Point t; |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1244 | ConvertPointToWidget(dest, &t); |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1245 | p->SetPoint(p->x() - t.x(), p->y() - t.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | // static |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1249 | void View::ConvertPointToScreen(const View* src, gfx::Point* p) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1250 | DCHECK(src); |
| 1251 | DCHECK(p); |
| 1252 | |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1253 | // If the view is not connected to a tree, there's nothing we can do. |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1254 | Widget* widget = src->GetWidget(); |
| 1255 | if (widget) { |
| 1256 | ConvertPointToWidget(src, p); |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1257 | gfx::Rect r; |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1258 | widget->GetBounds(&r, false); |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1259 | p->SetPoint(p->x() + r.x(), p->y() + r.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
[email protected] | a0949a4 | 2010-08-23 16:03:05 | [diff] [blame] | 1263 | gfx::Rect View::GetScreenBounds() const { |
| 1264 | gfx::Point origin; |
| 1265 | View::ConvertPointToScreen(this, &origin); |
| 1266 | return gfx::Rect(origin, size()); |
| 1267 | } |
| 1268 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1269 | ///////////////////////////////////////////////////////////////////////////// |
| 1270 | // |
| 1271 | // View - event handlers |
| 1272 | // |
| 1273 | ///////////////////////////////////////////////////////////////////////////// |
| 1274 | |
| 1275 | bool View::OnMousePressed(const MouseEvent& e) { |
| 1276 | return false; |
| 1277 | } |
| 1278 | |
| 1279 | bool View::OnMouseDragged(const MouseEvent& e) { |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
| 1283 | void View::OnMouseReleased(const MouseEvent& e, bool canceled) { |
| 1284 | } |
| 1285 | |
| 1286 | void View::OnMouseMoved(const MouseEvent& e) { |
| 1287 | } |
| 1288 | |
| 1289 | void View::OnMouseEntered(const MouseEvent& e) { |
| 1290 | } |
| 1291 | |
| 1292 | void View::OnMouseExited(const MouseEvent& e) { |
| 1293 | } |
| 1294 | |
[email protected] | ad84c8f | 2010-09-08 14:06:10 | [diff] [blame] | 1295 | #if defined(TOUCH_UI) |
| 1296 | bool View::OnTouchEvent(const TouchEvent& event) { |
[email protected] | b4d13c6 | 2010-10-20 00:11:40 | [diff] [blame] | 1297 | DVLOG(1) << "visited the OnTouchEvent"; |
[email protected] | ad84c8f | 2010-09-08 14:06:10 | [diff] [blame] | 1298 | return false; |
| 1299 | } |
| 1300 | #endif |
| 1301 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1302 | void View::SetMouseHandler(View *new_mouse_handler) { |
| 1303 | // It is valid for new_mouse_handler to be NULL |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1304 | if (parent_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1305 | parent_->SetMouseHandler(new_mouse_handler); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | void View::SetVisible(bool flag) { |
| 1309 | if (flag != is_visible_) { |
| 1310 | // If the tab is currently visible, schedule paint to |
| 1311 | // refresh parent |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1312 | if (IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1313 | SchedulePaint(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1314 | |
| 1315 | is_visible_ = flag; |
| 1316 | |
| 1317 | // This notifies all subviews recursively. |
| 1318 | PropagateVisibilityNotifications(this, flag); |
| 1319 | |
| 1320 | // If we are newly visible, schedule paint. |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1321 | if (IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1322 | SchedulePaint(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | bool View::IsVisibleInRootView() const { |
| 1327 | View* parent = GetParent(); |
| 1328 | if (IsVisible() && parent) |
| 1329 | return parent->IsVisibleInRootView(); |
| 1330 | else |
| 1331 | return false; |
| 1332 | } |
| 1333 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1334 | ///////////////////////////////////////////////////////////////////////////// |
| 1335 | // |
| 1336 | // View - keyboard and focus |
| 1337 | // |
| 1338 | ///////////////////////////////////////////////////////////////////////////// |
| 1339 | |
| 1340 | void View::RequestFocus() { |
| 1341 | RootView* rv = GetRootView(); |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 1342 | if (rv && IsFocusableInRootView()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1343 | rv->FocusView(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | void View::WillGainFocus() { |
| 1347 | } |
| 1348 | |
| 1349 | void View::DidGainFocus() { |
| 1350 | } |
| 1351 | |
| 1352 | void View::WillLoseFocus() { |
| 1353 | } |
| 1354 | |
| 1355 | bool View::OnKeyPressed(const KeyEvent& e) { |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | bool View::OnKeyReleased(const KeyEvent& e) { |
| 1360 | return false; |
| 1361 | } |
| 1362 | |
| 1363 | bool View::OnMouseWheel(const MouseWheelEvent& e) { |
| 1364 | return false; |
| 1365 | } |
| 1366 | |
| 1367 | void View::SetDragController(DragController* drag_controller) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1368 | drag_controller_ = drag_controller; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | DragController* View::GetDragController() { |
| 1372 | return drag_controller_; |
| 1373 | } |
| 1374 | |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 1375 | bool View::GetDropFormats( |
| 1376 | int* formats, |
| 1377 | std::set<OSExchangeData::CustomFormat>* custom_formats) { |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
| 1381 | bool View::AreDropTypesRequired() { |
| 1382 | return false; |
| 1383 | } |
| 1384 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1385 | bool View::CanDrop(const OSExchangeData& data) { |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 1386 | // TODO(sky): when I finish up migration, this should default to true. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
| 1389 | |
| 1390 | void View::OnDragEntered(const DropTargetEvent& event) { |
| 1391 | } |
| 1392 | |
| 1393 | int View::OnDragUpdated(const DropTargetEvent& event) { |
| 1394 | return DragDropTypes::DRAG_NONE; |
| 1395 | } |
| 1396 | |
| 1397 | void View::OnDragExited() { |
| 1398 | } |
| 1399 | |
| 1400 | int View::OnPerformDrop(const DropTargetEvent& event) { |
| 1401 | return DragDropTypes::DRAG_NONE; |
| 1402 | } |
| 1403 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1404 | // static |
| 1405 | bool View::ExceededDragThreshold(int delta_x, int delta_y) { |
| 1406 | return (abs(delta_x) > GetHorizontalDragThreshold() || |
| 1407 | abs(delta_y) > GetVerticalDragThreshold()); |
| 1408 | } |
| 1409 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1410 | // Tooltips ----------------------------------------------------------------- |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1411 | bool View::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1412 | return false; |
| 1413 | } |
| 1414 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1415 | bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1416 | return false; |
| 1417 | } |
| 1418 | |
| 1419 | void View::TooltipTextChanged() { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1420 | Widget* widget = GetWidget(); |
| 1421 | if (widget && widget->GetTooltipManager()) |
| 1422 | widget->GetTooltipManager()->TooltipTextChanged(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | void View::UpdateTooltip() { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1426 | Widget* widget = GetWidget(); |
| 1427 | if (widget && widget->GetTooltipManager()) |
| 1428 | widget->GetTooltipManager()->UpdateTooltip(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1429 | } |
| 1430 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1431 | std::string View::GetClassName() const { |
| 1432 | return kViewClassName; |
| 1433 | } |
| 1434 | |
[email protected] | 5c2b98b | 2009-03-09 20:55:54 | [diff] [blame] | 1435 | View* View::GetAncestorWithClassName(const std::string& name) { |
| 1436 | for (View* view = this; view; view = view->GetParent()) { |
| 1437 | if (view->GetClassName() == name) |
| 1438 | return view; |
| 1439 | } |
| 1440 | return NULL; |
| 1441 | } |
| 1442 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1443 | gfx::Rect View::GetVisibleBounds() { |
[email protected] | b6296fd | 2009-02-13 17:34:16 | [diff] [blame] | 1444 | if (!IsVisibleInRootView()) |
| 1445 | return gfx::Rect(); |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1446 | gfx::Rect vis_bounds(0, 0, width(), height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1447 | gfx::Rect ancestor_bounds; |
| 1448 | View* view = this; |
| 1449 | int root_x = 0; |
| 1450 | int root_y = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1451 | while (view != NULL && !vis_bounds.IsEmpty()) { |
| 1452 | root_x += view->GetX(APPLY_MIRRORING_TRANSFORMATION); |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1453 | root_y += view->y(); |
| 1454 | vis_bounds.Offset(view->GetX(APPLY_MIRRORING_TRANSFORMATION), view->y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1455 | View* ancestor = view->GetParent(); |
| 1456 | if (ancestor != NULL) { |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1457 | ancestor_bounds.SetRect(0, 0, ancestor->width(), |
| 1458 | ancestor->height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1459 | vis_bounds = vis_bounds.Intersect(ancestor_bounds); |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1460 | } else if (!view->GetWidget()) { |
| 1461 | // If the view has no Widget, we're not visible. Return an empty rect. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1462 | return gfx::Rect(); |
| 1463 | } |
| 1464 | view = ancestor; |
| 1465 | } |
| 1466 | if (vis_bounds.IsEmpty()) |
| 1467 | return vis_bounds; |
| 1468 | // Convert back to this views coordinate system. |
| 1469 | vis_bounds.Offset(-root_x, -root_y); |
| 1470 | return vis_bounds; |
| 1471 | } |
| 1472 | |
| 1473 | int View::GetPageScrollIncrement(ScrollView* scroll_view, |
| 1474 | bool is_horizontal, bool is_positive) { |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | int View::GetLineScrollIncrement(ScrollView* scroll_view, |
| 1479 | bool is_horizontal, bool is_positive) { |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
[email protected] | 45da6c7 | 2009-10-28 20:45:42 | [diff] [blame] | 1483 | ThemeProvider* View::GetThemeProvider() const { |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1484 | Widget* widget = GetWidget(); |
| 1485 | return widget ? widget->GetThemeProvider() : NULL; |
| 1486 | } |
| 1487 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1488 | // static |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1489 | void View::RegisterChildrenForVisibleBoundsNotification( |
| 1490 | RootView* root, View* view) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1491 | DCHECK(root && view); |
| 1492 | if (view->GetNotifyWhenVisibleBoundsInRootChanges()) |
| 1493 | root->RegisterViewForVisibleBoundsNotification(view); |
| 1494 | for (int i = 0; i < view->GetChildViewCount(); ++i) |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1495 | RegisterChildrenForVisibleBoundsNotification(root, view->GetChildViewAt(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | // static |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1499 | void View::UnregisterChildrenForVisibleBoundsNotification( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1500 | RootView* root, View* view) { |
| 1501 | DCHECK(root && view); |
| 1502 | if (view->GetNotifyWhenVisibleBoundsInRootChanges()) |
| 1503 | root->UnregisterViewForVisibleBoundsNotification(view); |
| 1504 | for (int i = 0; i < view->GetChildViewCount(); ++i) |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1505 | UnregisterChildrenForVisibleBoundsNotification(root, |
| 1506 | view->GetChildViewAt(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | void View::AddDescendantToNotify(View* view) { |
| 1510 | DCHECK(view); |
| 1511 | if (!descendants_to_notify_.get()) |
| 1512 | descendants_to_notify_.reset(new ViewList()); |
| 1513 | descendants_to_notify_->push_back(view); |
| 1514 | } |
| 1515 | |
| 1516 | void View::RemoveDescendantToNotify(View* view) { |
| 1517 | DCHECK(view && descendants_to_notify_.get()); |
| 1518 | ViewList::iterator i = find(descendants_to_notify_->begin(), |
| 1519 | descendants_to_notify_->end(), |
| 1520 | view); |
| 1521 | DCHECK(i != descendants_to_notify_->end()); |
| 1522 | descendants_to_notify_->erase(i); |
| 1523 | if (descendants_to_notify_->empty()) |
| 1524 | descendants_to_notify_.reset(); |
| 1525 | } |
| 1526 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1527 | // DropInfo -------------------------------------------------------------------- |
| 1528 | |
| 1529 | void View::DragInfo::Reset() { |
| 1530 | possible_drag = false; |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1531 | start_pt = gfx::Point(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1532 | } |
| 1533 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1534 | void View::DragInfo::PossibleDrag(const gfx::Point& p) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1535 | possible_drag = true; |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1536 | start_pt = p; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | } // namespace |