[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] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 29 | #include "views/accessibility/view_accessibility_wrapper.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), |
[email protected] | 6ff244f | 2009-01-20 20:38:08 | [diff] [blame] | 71 | #if defined(OS_WIN) |
| 72 | accessibility_(NULL), |
| 73 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | drag_controller_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | flip_canvas_on_paint_for_rtl_ui_(false) { |
| 76 | } |
| 77 | |
| 78 | View::~View() { |
[email protected] | 8c57de2 | 2010-03-12 21:06:04 | [diff] [blame] | 79 | if (parent_) |
| 80 | parent_->RemoveChildView(this); |
| 81 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | int c = static_cast<int>(child_views_.size()); |
| 83 | while (--c >= 0) { |
[email protected] | 8c57de2 | 2010-03-12 21:06:04 | [diff] [blame] | 84 | child_views_[c]->SetParent(NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | if (child_views_[c]->IsParentOwned()) |
| 86 | delete child_views_[c]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | } |
[email protected] | a64f3390 | 2009-10-10 05:41:23 | [diff] [blame] | 88 | |
| 89 | #if defined(OS_WIN) |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 90 | if (accessibility_.get()) |
[email protected] | a64f3390 | 2009-10-10 05:41:23 | [diff] [blame] | 91 | accessibility_->Uninitialize(); |
[email protected] | a64f3390 | 2009-10-10 05:41:23 | [diff] [blame] | 92 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ///////////////////////////////////////////////////////////////////////////// |
| 96 | // |
| 97 | // View - sizing |
| 98 | // |
| 99 | ///////////////////////////////////////////////////////////////////////////// |
| 100 | |
[email protected] | 0d8ea70 | 2008-10-14 17:03:07 | [diff] [blame] | 101 | gfx::Rect View::GetBounds(PositionMirroringSettings settings) const { |
| 102 | gfx::Rect bounds(bounds_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | |
| 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] | 0d8ea70 | 2008-10-14 17:03:07 | [diff] [blame] | 107 | if (settings == APPLY_MIRRORING_TRANSFORMATION) |
| 108 | bounds.set_x(MirroredX()); |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 109 | |
[email protected] | 0d8ea70 | 2008-10-14 17:03:07 | [diff] [blame] | 110 | return bounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 113 | // y(), width() and height() are agnostic to the RTL UI layout of the |
| 114 | // parent view. x(), on the other hand, is not. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | int View::GetX(PositionMirroringSettings settings) const { |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 116 | return settings == IGNORE_MIRRORING_TRANSFORMATION ? x() : MirroredX(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 119 | void View::SetBounds(const gfx::Rect& bounds) { |
| 120 | if (bounds == bounds_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 123 | gfx::Rect prev = bounds_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | bounds_ = bounds; |
[email protected] | b97c8a2 | 2010-02-05 19:36:43 | [diff] [blame] | 125 | bool size_changed = prev.size() != bounds_.size(); |
| 126 | bool position_changed = prev.origin() != bounds_.origin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | |
[email protected] | b97c8a2 | 2010-02-05 19:36:43 | [diff] [blame] | 128 | if (size_changed || position_changed) { |
| 129 | DidChangeBounds(prev, bounds_); |
| 130 | |
| 131 | RootView* root = GetRootView(); |
| 132 | if (root) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | root->ViewBoundsChanged(this, size_changed, position_changed); |
| 134 | } |
| 135 | } |
| 136 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 137 | gfx::Rect View::GetLocalBounds(bool include_border) const { |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 138 | if (include_border || !border_.get()) |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 139 | return gfx::Rect(0, 0, width(), height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 141 | gfx::Insets insets; |
| 142 | border_->GetInsets(&insets); |
| 143 | return gfx::Rect(insets.left(), insets.top(), |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 144 | std::max(0, width() - insets.width()), |
| 145 | std::max(0, height() - insets.height())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 148 | gfx::Point View::GetPosition() const { |
| 149 | return gfx::Point(GetX(APPLY_MIRRORING_TRANSFORMATION), y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 152 | gfx::Size View::GetPreferredSize() { |
| 153 | if (layout_manager_.get()) |
| 154 | return layout_manager_->GetPreferredSize(this); |
| 155 | return gfx::Size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | a136016 | 2009-11-30 21:19:07 | [diff] [blame] | 158 | int View::GetBaseline() { |
| 159 | return -1; |
| 160 | } |
| 161 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | void View::SizeToPreferredSize() { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 163 | gfx::Size prefsize = GetPreferredSize(); |
| 164 | if ((prefsize.width() != width()) || (prefsize.height() != height())) |
| 165 | SetBounds(x(), y(), prefsize.width(), prefsize.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 7ccc52b7 | 2009-05-08 21:09:11 | [diff] [blame] | 168 | void View::PreferredSizeChanged() { |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 169 | InvalidateLayout(); |
[email protected] | 7ccc52b7 | 2009-05-08 21:09:11 | [diff] [blame] | 170 | if (parent_) |
| 171 | parent_->ChildPreferredSizeChanged(this); |
| 172 | } |
| 173 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 174 | gfx::Size View::GetMinimumSize() { |
| 175 | return GetPreferredSize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int View::GetHeightForWidth(int w) { |
| 179 | if (layout_manager_.get()) |
| 180 | return layout_manager_->GetPreferredHeightForWidth(this, w); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 181 | return GetPreferredSize().height(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 184 | void View::DidChangeBounds(const gfx::Rect& previous, |
| 185 | const gfx::Rect& current) { |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 186 | needs_layout_ = false; |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 187 | Layout(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 190 | void View::ScrollRectToVisible(const gfx::Rect& rect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 191 | View* parent = GetParent(); |
| 192 | |
| 193 | // We must take RTL UI mirroring into account when adjusting the position of |
| 194 | // the region. |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 195 | if (parent) { |
| 196 | gfx::Rect scroll_rect(rect); |
| 197 | scroll_rect.Offset(GetX(APPLY_MIRRORING_TRANSFORMATION), y()); |
| 198 | parent->ScrollRectToVisible(scroll_rect); |
| 199 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | ///////////////////////////////////////////////////////////////////////////// |
| 203 | // |
| 204 | // View - layout |
| 205 | // |
| 206 | ///////////////////////////////////////////////////////////////////////////// |
| 207 | |
| 208 | void View::Layout() { |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 209 | needs_layout_ = false; |
| 210 | |
[email protected] | f861719 | 2010-07-05 07:57:10 | [diff] [blame] | 211 | // If we have a layout manager, let it handle the layout for us. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | if (layout_manager_.get()) { |
| 213 | layout_manager_->Layout(this); |
| 214 | SchedulePaint(); |
| 215 | } |
| 216 | |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 217 | // 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] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 223 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | View* child = GetChildViewAt(i); |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 225 | if (child->needs_layout_ || !layout_manager_.get()) { |
| 226 | child->needs_layout_ = false; |
| 227 | child->Layout(); |
| 228 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 232 | void View::InvalidateLayout() { |
| 233 | if (needs_layout_) |
| 234 | return; |
| 235 | needs_layout_ = true; |
| 236 | if (parent_) |
| 237 | parent_->InvalidateLayout(); |
| 238 | } |
| 239 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 240 | LayoutManager* View::GetLayoutManager() const { |
| 241 | return layout_manager_.get(); |
| 242 | } |
| 243 | |
| 244 | void View::SetLayoutManager(LayoutManager* layout_manager) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 245 | if (layout_manager_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | layout_manager_->Uninstalled(this); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 247 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | layout_manager_.reset(layout_manager); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 249 | if (layout_manager_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | layout_manager_->Installed(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | //////////////////////////////////////////////////////////////////////////////// |
| 254 | // |
| 255 | // View - Right-to-left UI layout |
| 256 | // |
| 257 | //////////////////////////////////////////////////////////////////////////////// |
| 258 | |
[email protected] | c3bca42 | 2010-02-19 23:29:02 | [diff] [blame] | 259 | int View::MirroredX() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | View* parent = GetParent(); |
[email protected] | aaa71da5 | 2010-02-19 21:39:24 | [diff] [blame] | 261 | return parent ? parent->MirroredLeftPointForRect(bounds_) : x(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | int View::MirroredLeftPointForRect(const gfx::Rect& bounds) const { |
[email protected] | c2f4bdb7 | 2010-05-10 23:15:21 | [diff] [blame] | 265 | return base::i18n::IsRTL() ? |
[email protected] | aaa71da5 | 2010-02-19 21:39:24 | [diff] [blame] | 266 | (width() - bounds.x() - bounds.width()) : bounds.x(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | //////////////////////////////////////////////////////////////////////////////// |
| 270 | // |
| 271 | // View - states |
| 272 | // |
| 273 | //////////////////////////////////////////////////////////////////////////////// |
| 274 | |
| 275 | bool View::IsEnabled() const { |
| 276 | return enabled_; |
| 277 | } |
| 278 | |
| 279 | void View::SetEnabled(bool state) { |
| 280 | if (enabled_ != state) { |
| 281 | enabled_ = state; |
| 282 | SchedulePaint(); |
| 283 | } |
| 284 | } |
| 285 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | void View::SetFocusable(bool focusable) { |
| 287 | focusable_ = focusable; |
| 288 | } |
| 289 | |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 290 | bool View::IsFocusableInRootView() const { |
| 291 | return IsFocusable() && IsVisibleInRootView(); |
| 292 | } |
| 293 | |
| 294 | bool View::IsAccessibilityFocusableInRootView() const { |
[email protected] | 1aa3655 | 2010-07-14 08:21:25 | [diff] [blame] | 295 | return (focusable_ || accessibility_focusable_) && IsEnabled() && |
| 296 | IsVisibleInRootView(); |
[email protected] | 83548a4 | 2010-06-18 13:53:37 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | 82166b6 | 2009-06-30 18:48:00 | [diff] [blame] | 299 | FocusManager* View::GetFocusManager() { |
| 300 | Widget* widget = GetWidget(); |
| 301 | return widget ? widget->GetFocusManager() : NULL; |
| 302 | } |
| 303 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | bool View::HasFocus() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 305 | FocusManager* focus_manager = GetFocusManager(); |
| 306 | if (focus_manager) |
| 307 | return focus_manager->GetFocusedView() == this; |
| 308 | return false; |
| 309 | } |
| 310 | |
[email protected] | 5c9e97a | 2009-09-09 23:48:30 | [diff] [blame] | 311 | void View::Focus() { |
[email protected] | 7f14e1f | 2009-10-01 15:57:26 | [diff] [blame] | 312 | // 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] | 5c9e97a | 2009-09-09 23:48:30 | [diff] [blame] | 314 | FocusManager* focus_manager = GetFocusManager(); |
| 315 | if (focus_manager) |
[email protected] | 7f14e1f | 2009-10-01 15:57:26 | [diff] [blame] | 316 | focus_manager->ClearNativeFocus(); |
[email protected] | 6a054ffa | 2010-08-06 07:07:18 | [diff] [blame^] | 317 | |
| 318 | // Notify assistive technologies of the focus change. |
| 319 | NotifyAccessibilityEvent(AccessibilityTypes::EVENT_FOCUS); |
| 320 | } |
| 321 | |
| 322 | void View::NotifyAccessibilityEvent(AccessibilityTypes::Event event_type) { |
| 323 | NotifyAccessibilityEvent(event_type, true); |
[email protected] | 5c9e97a | 2009-09-09 23:48:30 | [diff] [blame] | 324 | } |
| 325 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | void View::SetHotTracked(bool flag) { |
| 327 | } |
| 328 | |
| 329 | ///////////////////////////////////////////////////////////////////////////// |
| 330 | // |
| 331 | // View - painting |
| 332 | // |
| 333 | ///////////////////////////////////////////////////////////////////////////// |
| 334 | |
[email protected] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 335 | void View::SchedulePaint(const gfx::Rect& r, bool urgent) { |
| 336 | if (!IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | |
| 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] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 342 | gfx::Rect paint_rect = r; |
| 343 | paint_rect.Offset(GetPosition()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | parent_->SchedulePaint(paint_rect, urgent); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void View::SchedulePaint() { |
[email protected] | 0a1d36b2 | 2008-10-17 19:33:09 | [diff] [blame] | 349 | SchedulePaint(GetLocalBounds(true), false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | } |
| 351 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 352 | void View::Paint(gfx::Canvas* canvas) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | PaintBackground(canvas); |
| 354 | PaintFocusBorder(canvas); |
| 355 | PaintBorder(canvas); |
| 356 | } |
| 357 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 358 | void View::PaintBackground(gfx::Canvas* canvas) { |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 359 | if (background_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 360 | background_->Paint(canvas, this); |
| 361 | } |
| 362 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 363 | void View::PaintBorder(gfx::Canvas* canvas) { |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 364 | if (border_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 365 | border_->Paint(*this, canvas); |
| 366 | } |
| 367 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 368 | void View::PaintFocusBorder(gfx::Canvas* canvas) { |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 369 | if (HasFocus() && (IsFocusable() || IsAccessibilityFocusableInRootView())) |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 370 | canvas->DrawFocusRect(0, 0, width(), height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 373 | void View::PaintChildren(gfx::Canvas* canvas) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 374 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | 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] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 384 | void View::ProcessPaint(gfx::Canvas* canvas) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 385 | if (!IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 387 | |
| 388 | // We're going to modify the canvas, save it's state first. |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 389 | canvas->Save(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | |
| 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] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 398 | if (canvas->ClipRectInt(MirroredX(), y(), width(), height())) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | // Non-empty clip, translate the graphics such that 0,0 corresponds to |
| 400 | // where this view is located (related to its parent). |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 401 | canvas->TranslateInt(MirroredX(), y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | |
| 403 | // Save the state again, so that any changes don't effect PaintChildren. |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 404 | canvas->Save(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | |
| 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] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 410 | canvas->TranslateInt(width(), 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 411 | canvas->ScaleInt(-1, 1); |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 412 | canvas->Save(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | } |
| 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] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 420 | if (flip_canvas) |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 421 | canvas->Restore(); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 422 | |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 423 | canvas->Restore(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | PaintChildren(canvas); |
| 425 | } |
| 426 | |
| 427 | // Restore the canvas's original transform. |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 428 | canvas->Restore(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void View::PaintNow() { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 432 | if (!IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 434 | |
| 435 | View* view = GetParent(); |
| 436 | if (view) |
| 437 | view->PaintNow(); |
| 438 | } |
| 439 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 440 | gfx::Insets View::GetInsets() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | gfx::Insets insets; |
[email protected] | 9a3f0ac2 | 2008-11-14 03:24:02 | [diff] [blame] | 442 | if (border_.get()) |
| 443 | border_->GetInsets(&insets); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | return insets; |
| 445 | } |
| 446 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 447 | gfx::NativeCursor View::GetCursorForPoint(Event::EventType event_type, |
| 448 | const gfx::Point& p) { |
[email protected] | 9abf8dd6 | 2009-06-04 06:40:42 | [diff] [blame] | 449 | return NULL; |
| 450 | } |
| 451 | |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 452 | bool View::HitTest(const gfx::Point& l) const { |
[email protected] | d7fc9794 | 2009-11-20 22:07:11 | [diff] [blame] | 453 | if (l.x() >= 0 && l.x() < width() && l.y() >= 0 && l.y() < height()) { |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 454 | if (HasHitTestMask()) { |
| 455 | gfx::Path mask; |
| 456 | GetHitTestMask(&mask); |
[email protected] | a340697 | 2009-11-04 05:05:48 | [diff] [blame] | 457 | ScopedRegion rgn(mask.CreateNativeRegion()); |
| 458 | // TODO: can this use SkRegion's contains instead? |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 459 | #if defined(OS_WIN) |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 460 | return !!PtInRegion(rgn, l.x(), l.y()); |
[email protected] | 10a6e77b | 2009-12-31 01:03:52 | [diff] [blame] | 461 | #elif defined(TOOLKIT_USES_GTK) |
[email protected] | a340697 | 2009-11-04 05:05:48 | [diff] [blame] | 462 | return gdk_region_point_in(rgn.Get(), l.x(), l.y()); |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 463 | #endif |
| 464 | } |
| 465 | // No mask, but inside our bounds. |
| 466 | return true; |
| 467 | } |
| 468 | // Outside our bounds. |
| 469 | return false; |
| 470 | } |
| 471 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 472 | void View::SetContextMenuController(ContextMenuController* menu_controller) { |
| 473 | context_menu_controller_ = menu_controller; |
| 474 | } |
| 475 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 476 | void View::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) { |
[email protected] | 042811c | 2008-10-31 21:31:34 | [diff] [blame] | 477 | if (!context_menu_controller_) |
| 478 | return; |
| 479 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 480 | context_menu_controller_->ShowContextMenu(this, p, is_mouse_gesture); |
[email protected] | 042811c | 2008-10-31 21:31:34 | [diff] [blame] | 481 | } |
| 482 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | ///////////////////////////////////////////////////////////////////////////// |
| 484 | // |
| 485 | // View - tree |
| 486 | // |
| 487 | ///////////////////////////////////////////////////////////////////////////// |
| 488 | |
| 489 | bool View::ProcessMousePressed(const MouseEvent& e, DragInfo* drag_info) { |
[email protected] | 83548a4 | 2010-06-18 13:53:37 | [diff] [blame] | 490 | const bool enabled = IsEnabled(); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 491 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 496 | |
| 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] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 504 | drag_info->PossibleDrag(e.location()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 505 | return true; |
| 506 | } |
| 507 | return !!context_menu_controller || result; |
| 508 | } |
| 509 | |
| 510 | bool 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] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 515 | if (possible_drag && ExceededDragThreshold(drag_info->start_pt.x() - e.x(), |
| 516 | drag_info->start_pt.y() - e.y())) { |
[email protected] | b5f94de | 2009-12-04 07:59:00 | [diff] [blame] | 517 | if (!drag_controller_ || |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 518 | drag_controller_->CanStartDrag(this, drag_info->start_pt, e.location())) |
| 519 | DoDrag(e, drag_info->start_pt); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 520 | } 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 | |
| 529 | void 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] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 533 | gfx::Point location(e.location()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | OnMouseReleased(e, canceled); |
[email protected] | 464fdb3 | 2009-03-19 20:25:44 | [diff] [blame] | 535 | if (HitTest(location)) { |
| 536 | ConvertPointToScreen(this, &location); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 537 | ShowContextMenu(location, true); |
[email protected] | 464fdb3 | 2009-03-19 20:25:44 | [diff] [blame] | 538 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 539 | } else { |
| 540 | OnMouseReleased(e, canceled); |
| 541 | } |
| 542 | // WARNING: we may have been deleted. |
| 543 | } |
| 544 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 545 | void View::AddChildView(View* v) { |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 546 | AddChildView(static_cast<int>(child_views_.size()), v); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | void View::AddChildView(int index, View* v) { |
[email protected] | 0b2fa2b | 2009-12-01 01:06:12 | [diff] [blame] | 550 | CHECK(v != this) << "You cannot add a view as its own child"; |
| 551 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | // Remove the view from its current parent if any. |
| 553 | if (v->GetParent()) |
| 554 | v->GetParent()->RemoveChildView(v); |
| 555 | |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 556 | // Sets the prev/next focus views. |
| 557 | InitFocusSiblings(v, index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 558 | |
| 559 | // Let's insert the view. |
| 560 | child_views_.insert(child_views_.begin() + index, v); |
| 561 | v->SetParent(this); |
| 562 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 563 | for (View* p = this; p; p = p->GetParent()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 564 | p->ViewHierarchyChangedImpl(false, true, this, v); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 565 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 566 | v->PropagateAddNotifications(this, v); |
| 567 | UpdateTooltip(); |
| 568 | RootView* root = GetRootView(); |
| 569 | if (root) |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 570 | RegisterChildrenForVisibleBoundsNotification(root, v); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | |
| 572 | if (layout_manager_.get()) |
| 573 | layout_manager_->ViewAdded(this, v); |
| 574 | } |
| 575 | |
| 576 | View* View::GetChildViewAt(int index) const { |
| 577 | return index < GetChildViewCount() ? child_views_[index] : NULL; |
| 578 | } |
| 579 | |
| 580 | int View::GetChildViewCount() const { |
| 581 | return static_cast<int>(child_views_.size()); |
| 582 | } |
| 583 | |
[email protected] | 3a9c26f | 2010-03-12 21:04:39 | [diff] [blame] | 584 | bool View::HasChildView(View* a_view) { |
| 585 | return find(child_views_.begin(), |
| 586 | child_views_.end(), |
| 587 | a_view) != child_views_.end(); |
| 588 | } |
| 589 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 590 | void View::RemoveChildView(View* a_view) { |
| 591 | DoRemoveChildView(a_view, true, true, false); |
| 592 | } |
| 593 | |
| 594 | void 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] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 602 | void View::DoDrag(const MouseEvent& e, const gfx::Point& press_pt) { |
| 603 | int drag_operations = GetDragOperations(press_pt); |
[email protected] | 4768c65b | 2009-08-28 21:42:59 | [diff] [blame] | 604 | if (drag_operations == DragDropTypes::DRAG_NONE) |
| 605 | return; |
| 606 | |
| 607 | OSExchangeData data; |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 608 | WriteDragData(press_pt, &data); |
[email protected] | 4768c65b | 2009-08-28 21:42:59 | [diff] [blame] | 609 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 616 | void 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] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 630 | if (update_focus_cycle) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 631 | // 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] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 642 | UnregisterChildrenForVisibleBoundsNotification(root, a_view); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 643 | 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 | |
| 659 | void View::PropagateRemoveNotifications(View* parent) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 660 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | GetChildViewAt(i)->PropagateRemoveNotifications(parent); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 662 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 663 | for (View* v = this; v; v = v->GetParent()) |
| 664 | v->ViewHierarchyChangedImpl(true, false, parent, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void View::PropagateAddNotifications(View* parent, View* child) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 668 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 669 | GetChildViewAt(i)->PropagateAddNotifications(parent, child); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 670 | ViewHierarchyChangedImpl(true, true, parent, child); |
| 671 | } |
| 672 | |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 673 | bool View::IsFocusable() const { |
| 674 | return focusable_ && IsEnabled() && IsVisible(); |
| 675 | } |
| 676 | |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 677 | void View::PropagateThemeChanged() { |
[email protected] | b2b71801 | 2010-03-25 15:09:06 | [diff] [blame] | 678 | for (int i = GetChildViewCount() - 1; i >= 0; --i) |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 679 | GetChildViewAt(i)->PropagateThemeChanged(); |
| 680 | OnThemeChanged(); |
[email protected] | 32670b0 | 2009-03-03 00:28:00 | [diff] [blame] | 681 | } |
| 682 | |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 683 | void View::PropagateLocaleChanged() { |
[email protected] | b2b71801 | 2010-03-25 15:09:06 | [diff] [blame] | 684 | for (int i = GetChildViewCount() - 1; i >= 0; --i) |
[email protected] | 8b9a8f1 | 2010-07-27 01:39:13 | [diff] [blame] | 685 | GetChildViewAt(i)->PropagateLocaleChanged(); |
| 686 | OnLocaleChanged(); |
[email protected] | b2b71801 | 2010-03-25 15:09:06 | [diff] [blame] | 687 | } |
| 688 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 689 | #ifndef NDEBUG |
| 690 | bool View::IsProcessingPaint() const { |
| 691 | return GetParent() && GetParent()->IsProcessingPaint(); |
| 692 | } |
| 693 | #endif |
| 694 | |
[email protected] | f704ee7 | 2008-11-10 21:31:59 | [diff] [blame] | 695 | gfx::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] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 703 | bool View::HasHitTestMask() const { |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | void View::GetHitTestMask(gfx::Path* mask) const { |
| 708 | DCHECK(mask); |
| 709 | } |
| 710 | |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 711 | void View::ViewHierarchyChanged(bool is_add, |
| 712 | View* parent, |
| 713 | View* child) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | void View::ViewHierarchyChangedImpl(bool register_accelerators, |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 717 | bool is_add, |
| 718 | View* parent, |
| 719 | View* child) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 720 | 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] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 724 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 731 | } else { |
| 732 | if (child == this) |
[email protected] | fa1cf0b8 | 2010-01-15 21:49:44 | [diff] [blame] | 733 | UnregisterAccelerators(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | ViewHierarchyChanged(is_add, parent, child); |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 738 | parent->needs_layout_ = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | void View::PropagateVisibilityNotifications(View* start, bool is_visible) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 742 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 743 | GetChildViewAt(i)->PropagateVisibilityNotifications(start, is_visible); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 744 | VisibilityChanged(start, is_visible); |
| 745 | } |
| 746 | |
| 747 | void View::VisibilityChanged(View* starting_from, bool is_visible) { |
| 748 | } |
| 749 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 750 | void 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 | |
| 760 | void 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 778 | void 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 | |
| 791 | bool View::GetNotifyWhenVisibleBoundsInRootChanges() { |
| 792 | return notify_when_visible_bounds_in_root_changes_; |
| 793 | } |
| 794 | |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 795 | View* View::GetViewForPoint(const gfx::Point& point) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 796 | // Walk the child Views recursively looking for the View that most |
| 797 | // tightly encloses the specified point. |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 798 | for (int i = GetChildViewCount() - 1; i >= 0; --i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 799 | View* child = GetChildViewAt(i); |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 800 | if (!child->IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 801 | continue; |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 802 | |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 803 | gfx::Point point_in_child_coords(point); |
[email protected] | 82739cf | 2008-09-16 00:37:56 | [diff] [blame] | 804 | View::ConvertPointToView(this, child, &point_in_child_coords); |
[email protected] | 613b806 | 2008-10-14 23:45:09 | [diff] [blame] | 805 | if (child->HitTest(point_in_child_coords)) |
[email protected] | 0d52b230 | 2009-05-11 23:50:55 | [diff] [blame] | 806 | return child->GetViewForPoint(point_in_child_coords); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 807 | } |
| 808 | return this; |
| 809 | } |
| 810 | |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 811 | Widget* View::GetWidget() const { |
| 812 | // The root view holds a reference to this view hierarchy's Widget. |
| 813 | return parent_ ? parent_->GetWidget() : NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 814 | } |
| 815 | |
[email protected] | cd8c4790 | 2009-04-30 20:55:35 | [diff] [blame] | 816 | Window* View::GetWindow() const { |
| 817 | Widget* widget = GetWidget(); |
| 818 | return widget ? widget->GetWindow() : NULL; |
| 819 | } |
| 820 | |
[email protected] | 2db27be | 2010-02-10 22:46:47 | [diff] [blame] | 821 | bool 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 829 | // Get the containing RootView |
| 830 | RootView* View::GetRootView() { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 831 | Widget* widget = GetWidget(); |
| 832 | return widget ? widget->GetRootView() : NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | View* View::GetViewByID(int id) const { |
| 836 | if (id == id_) |
| 837 | return const_cast<View*>(this); |
| 838 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 839 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 840 | View* child = GetChildViewAt(i); |
| 841 | View* view = child->GetViewByID(id); |
| 842 | if (view) |
| 843 | return view; |
| 844 | } |
| 845 | return NULL; |
| 846 | } |
| 847 | |
| 848 | void View::GetViewsWithGroup(int group_id, std::vector<View*>* out) { |
| 849 | if (group_ == group_id) |
| 850 | out->push_back(this); |
| 851 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 852 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 853 | GetChildViewAt(i)->GetViewsWithGroup(group_id, out); |
| 854 | } |
| 855 | |
| 856 | View* 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 | |
| 865 | void View::SetID(int id) { |
| 866 | id_ = id; |
| 867 | } |
| 868 | |
| 869 | int View::GetID() const { |
| 870 | return id_; |
| 871 | } |
| 872 | |
| 873 | void View::SetGroup(int gid) { |
[email protected] | 96f960d | 2009-09-14 18:45:30 | [diff] [blame] | 874 | // Don't change the group id once it's set. |
| 875 | DCHECK(group_ == -1 || group_ == gid); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 876 | group_ = gid; |
| 877 | } |
| 878 | |
| 879 | int View::GetGroup() const { |
| 880 | return group_; |
| 881 | } |
| 882 | |
| 883 | void View::SetParent(View* parent) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 884 | if (parent != parent_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 885 | parent_ = parent; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | bool 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] | 5e26d9d4 | 2010-05-13 22:11:03 | [diff] [blame] | 899 | int View::GetChildIndex(const View* v) const { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 900 | for (int i = 0, count = GetChildViewCount(); i < count; i++) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 901 | if (v == GetChildViewAt(i)) |
| 902 | return i; |
| 903 | } |
| 904 | return -1; |
| 905 | } |
| 906 | |
| 907 | /////////////////////////////////////////////////////////////////////////////// |
| 908 | // |
| 909 | // View - focus |
| 910 | // |
| 911 | /////////////////////////////////////////////////////////////////////////////// |
| 912 | |
| 913 | View* View::GetNextFocusableView() { |
| 914 | return next_focusable_view_; |
| 915 | } |
| 916 | |
| 917 | View* View::GetPreviousFocusableView() { |
| 918 | return previous_focusable_view_; |
| 919 | } |
| 920 | |
| 921 | void View::SetNextFocusableView(View* view) { |
| 922 | view->previous_focusable_view_ = this; |
| 923 | next_focusable_view_ = view; |
| 924 | } |
| 925 | |
| 926 | void 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 |
| 970 | void View::PrintViewHierarchy() { |
| 971 | PrintViewHierarchyImp(0); |
| 972 | } |
| 973 | |
| 974 | void 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] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 983 | buf << bounds_.x() << L"," << bounds_.y() << L","; |
| 984 | buf << bounds_.right() << L"," << bounds_.bottom(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 985 | buf << L' '; |
| 986 | buf << this; |
| 987 | |
| 988 | LOG(INFO) << buf.str(); |
| 989 | std::cout << buf.str() << std::endl; |
| 990 | |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 991 | for (int i = 0, count = GetChildViewCount(); i < count; ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 992 | GetChildViewAt(i)->PrintViewHierarchyImp(indent + 2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | |
| 996 | void View::PrintFocusHierarchy() { |
| 997 | PrintFocusHierarchyImp(0); |
| 998 | } |
| 999 | |
| 1000 | void 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 | |
| 1031 | void View::AddAccelerator(const Accelerator& accelerator) { |
| 1032 | if (!accelerators_.get()) |
| 1033 | accelerators_.reset(new std::vector<Accelerator>()); |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1034 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1040 | accelerators_->push_back(accelerator); |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1041 | RegisterPendingAccelerators(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1042 | } |
| 1043 | |
[email protected] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1044 | void 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] | 4bd23f3 | 2009-06-08 20:59:19 | [diff] [blame] | 1053 | size_t index = iter - accelerators_->begin(); |
[email protected] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1054 | accelerators_->erase(iter); |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1055 | if (index >= registered_accelerator_count_) { |
| 1056 | // The accelerator is not registered to FocusManager. |
| 1057 | return; |
| 1058 | } |
| 1059 | --registered_accelerator_count_; |
| 1060 | |
[email protected] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1061 | 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] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1069 | // 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] | e8e0f36 | 2008-11-08 01:13:25 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1076 | void View::ResetAccelerators() { |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1077 | if (accelerators_.get()) |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1078 | UnregisterAccelerators(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1079 | } |
| 1080 | |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1081 | void View::RegisterPendingAccelerators() { |
| 1082 | if (!accelerators_.get() || |
| 1083 | registered_accelerator_count_ == accelerators_->size()) { |
| 1084 | // No accelerators are waiting for registration. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1085 | return; |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1086 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1087 | |
| 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] | f3735c5d | 2009-03-19 17:26:23 | [diff] [blame] | 1094 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1095 | accelerator_focus_manager_ = GetFocusManager(); |
| 1096 | if (!accelerator_focus_manager_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1097 | // 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] | f8dce00 | 2009-09-10 21:07:04 | [diff] [blame] | 1100 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1105 | NOTREACHED(); |
[email protected] | f8dce00 | 2009-09-10 21:07:04 | [diff] [blame] | 1106 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1107 | return; |
| 1108 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1109 | std::vector<Accelerator>::const_iterator iter; |
| 1110 | for (iter = accelerators_->begin() + registered_accelerator_count_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1111 | iter != accelerators_->end(); ++iter) { |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1112 | accelerator_focus_manager_->RegisterAccelerator(*iter, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1113 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1114 | registered_accelerator_count_ = accelerators_->size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1115 | } |
| 1116 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1117 | void View::UnregisterAccelerators(bool leave_data_intact) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1118 | if (!accelerators_.get()) |
| 1119 | return; |
| 1120 | |
| 1121 | RootView* root_view = GetRootView(); |
| 1122 | if (root_view) { |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1123 | if (accelerator_focus_manager_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1124 | // 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] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1127 | accelerator_focus_manager_->UnregisterAccelerators(this); |
| 1128 | accelerator_focus_manager_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1129 | } |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1130 | if (!leave_data_intact) { |
| 1131 | accelerators_->clear(); |
| 1132 | accelerators_.reset(); |
| 1133 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1134 | registered_accelerator_count_ = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1138 | int View::GetDragOperations(const gfx::Point& press_pt) { |
| 1139 | return drag_controller_ ? |
| 1140 | drag_controller_->GetDragOperations(this, press_pt) : |
| 1141 | DragDropTypes::DRAG_NONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1142 | } |
| 1143 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1144 | void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1145 | DCHECK(drag_controller_); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1146 | drag_controller_->WriteDragData(this, press_pt, data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | void View::OnDragDone() { |
| 1150 | } |
| 1151 | |
| 1152 | bool View::InDrag() { |
| 1153 | RootView* root_view = GetRootView(); |
| 1154 | return root_view ? (root_view->GetDragView() == this) : false; |
| 1155 | } |
| 1156 | |
[email protected] | d5c8101 | 2010-04-03 00:56:12 | [diff] [blame] | 1157 | bool 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] | b82a049 | 2010-05-27 23:00:03 | [diff] [blame] | 1166 | bool View::GetAccessibleRole(AccessibilityTypes::Role* role) { |
| 1167 | if (accessible_role_) { |
| 1168 | *role = accessible_role_; |
| 1169 | return true; |
| 1170 | } |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
[email protected] | d5c8101 | 2010-04-03 00:56:12 | [diff] [blame] | 1174 | void View::SetAccessibleName(const std::wstring& name) { |
| 1175 | accessible_name_ = name; |
| 1176 | } |
| 1177 | |
[email protected] | b82a049 | 2010-05-27 23:00:03 | [diff] [blame] | 1178 | void View::SetAccessibleRole(const AccessibilityTypes::Role role) { |
| 1179 | accessible_role_ = role; |
| 1180 | } |
| 1181 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1182 | // static |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 1183 | void View::ConvertPointToView(const View* src, |
| 1184 | const View* dst, |
| 1185 | gfx::Point* point) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1186 | ConvertPointToView(src, dst, point, true); |
| 1187 | } |
| 1188 | |
| 1189 | // static |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 1190 | void View::ConvertPointToView(const View* src, |
| 1191 | const View* dst, |
| 1192 | gfx::Point* point, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1193 | bool try_other_direction) { |
| 1194 | // src can be NULL |
| 1195 | DCHECK(dst); |
| 1196 | DCHECK(point); |
| 1197 | |
[email protected] | bb515ed | 2009-01-15 00:53:43 | [diff] [blame] | 1198 | const View* v; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1199 | 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] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1203 | offset.y() + v->y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1204 | } |
| 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] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1221 | Widget* widget = dst->GetWidget(); |
| 1222 | if (widget) { |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1223 | gfx::Rect b; |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1224 | widget->GetBounds(&b, false); |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1225 | point->SetPoint(point->x() - b.x(), point->y() - b.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | // static |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1232 | void View::ConvertPointToWidget(const View* src, gfx::Point* p) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1233 | DCHECK(src); |
| 1234 | DCHECK(p); |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1235 | |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1236 | gfx::Point offset; |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1237 | for (const View* v = src; v; v = v->GetParent()) { |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1238 | offset.set_x(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION)); |
| 1239 | offset.set_y(offset.y() + v->y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1240 | } |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1241 | p->SetPoint(p->x() + offset.x(), p->y() + offset.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | // static |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1245 | void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) { |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1246 | gfx::Point t; |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1247 | ConvertPointToWidget(dest, &t); |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1248 | p->SetPoint(p->x() - t.x(), p->y() - t.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | // static |
[email protected] | 2fb6d46 | 2009-02-13 18:40:10 | [diff] [blame] | 1252 | void View::ConvertPointToScreen(const View* src, gfx::Point* p) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1253 | DCHECK(src); |
| 1254 | DCHECK(p); |
| 1255 | |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 1256 | // 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] | 1257 | Widget* widget = src->GetWidget(); |
| 1258 | if (widget) { |
| 1259 | ConvertPointToWidget(src, p); |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1260 | gfx::Rect r; |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1261 | widget->GetBounds(&r, false); |
[email protected] | 3b680a8 | 2008-12-17 02:03:23 | [diff] [blame] | 1262 | p->SetPoint(p->x() + r.x(), p->y() + r.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | ///////////////////////////////////////////////////////////////////////////// |
| 1267 | // |
| 1268 | // View - event handlers |
| 1269 | // |
| 1270 | ///////////////////////////////////////////////////////////////////////////// |
| 1271 | |
| 1272 | bool View::OnMousePressed(const MouseEvent& e) { |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | bool View::OnMouseDragged(const MouseEvent& e) { |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
| 1280 | void View::OnMouseReleased(const MouseEvent& e, bool canceled) { |
| 1281 | } |
| 1282 | |
| 1283 | void View::OnMouseMoved(const MouseEvent& e) { |
| 1284 | } |
| 1285 | |
| 1286 | void View::OnMouseEntered(const MouseEvent& e) { |
| 1287 | } |
| 1288 | |
| 1289 | void View::OnMouseExited(const MouseEvent& e) { |
| 1290 | } |
| 1291 | |
| 1292 | void View::SetMouseHandler(View *new_mouse_handler) { |
| 1293 | // It is valid for new_mouse_handler to be NULL |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1294 | if (parent_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1295 | parent_->SetMouseHandler(new_mouse_handler); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | void View::SetVisible(bool flag) { |
| 1299 | if (flag != is_visible_) { |
| 1300 | // If the tab is currently visible, schedule paint to |
| 1301 | // refresh parent |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1302 | if (IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1303 | SchedulePaint(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1304 | |
| 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] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1311 | if (IsVisible()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1312 | SchedulePaint(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | bool View::IsVisibleInRootView() const { |
| 1317 | View* parent = GetParent(); |
| 1318 | if (IsVisible() && parent) |
| 1319 | return parent->IsVisibleInRootView(); |
| 1320 | else |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1324 | ///////////////////////////////////////////////////////////////////////////// |
| 1325 | // |
| 1326 | // View - keyboard and focus |
| 1327 | // |
| 1328 | ///////////////////////////////////////////////////////////////////////////// |
| 1329 | |
| 1330 | void View::RequestFocus() { |
| 1331 | RootView* rv = GetRootView(); |
[email protected] | 59461d2 | 2010-07-21 15:41:09 | [diff] [blame] | 1332 | if (rv && IsFocusableInRootView()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1333 | rv->FocusView(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | void View::WillGainFocus() { |
| 1337 | } |
| 1338 | |
| 1339 | void View::DidGainFocus() { |
| 1340 | } |
| 1341 | |
| 1342 | void View::WillLoseFocus() { |
| 1343 | } |
| 1344 | |
| 1345 | bool View::OnKeyPressed(const KeyEvent& e) { |
| 1346 | return false; |
| 1347 | } |
| 1348 | |
| 1349 | bool View::OnKeyReleased(const KeyEvent& e) { |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
| 1353 | bool View::OnMouseWheel(const MouseWheelEvent& e) { |
| 1354 | return false; |
| 1355 | } |
| 1356 | |
| 1357 | void View::SetDragController(DragController* drag_controller) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 1358 | drag_controller_ = drag_controller; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | DragController* View::GetDragController() { |
| 1362 | return drag_controller_; |
| 1363 | } |
| 1364 | |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 1365 | bool View::GetDropFormats( |
| 1366 | int* formats, |
| 1367 | std::set<OSExchangeData::CustomFormat>* custom_formats) { |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | bool View::AreDropTypesRequired() { |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1375 | bool View::CanDrop(const OSExchangeData& data) { |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 1376 | // TODO(sky): when I finish up migration, this should default to true. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1377 | return false; |
| 1378 | } |
| 1379 | |
| 1380 | void View::OnDragEntered(const DropTargetEvent& event) { |
| 1381 | } |
| 1382 | |
| 1383 | int View::OnDragUpdated(const DropTargetEvent& event) { |
| 1384 | return DragDropTypes::DRAG_NONE; |
| 1385 | } |
| 1386 | |
| 1387 | void View::OnDragExited() { |
| 1388 | } |
| 1389 | |
| 1390 | int View::OnPerformDrop(const DropTargetEvent& event) { |
| 1391 | return DragDropTypes::DRAG_NONE; |
| 1392 | } |
| 1393 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1394 | // static |
| 1395 | bool View::ExceededDragThreshold(int delta_x, int delta_y) { |
| 1396 | return (abs(delta_x) > GetHorizontalDragThreshold() || |
| 1397 | abs(delta_y) > GetVerticalDragThreshold()); |
| 1398 | } |
| 1399 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1400 | // Tooltips ----------------------------------------------------------------- |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1401 | bool View::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1402 | return false; |
| 1403 | } |
| 1404 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1405 | bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1406 | return false; |
| 1407 | } |
| 1408 | |
| 1409 | void View::TooltipTextChanged() { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1410 | Widget* widget = GetWidget(); |
| 1411 | if (widget && widget->GetTooltipManager()) |
| 1412 | widget->GetTooltipManager()->TooltipTextChanged(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | void View::UpdateTooltip() { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1416 | Widget* widget = GetWidget(); |
| 1417 | if (widget && widget->GetTooltipManager()) |
| 1418 | widget->GetTooltipManager()->UpdateTooltip(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1419 | } |
| 1420 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1421 | std::string View::GetClassName() const { |
| 1422 | return kViewClassName; |
| 1423 | } |
| 1424 | |
[email protected] | 5c2b98b | 2009-03-09 20:55:54 | [diff] [blame] | 1425 | View* 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1433 | gfx::Rect View::GetVisibleBounds() { |
[email protected] | b6296fd | 2009-02-13 17:34:16 | [diff] [blame] | 1434 | if (!IsVisibleInRootView()) |
| 1435 | return gfx::Rect(); |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1436 | gfx::Rect vis_bounds(0, 0, width(), height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1437 | gfx::Rect ancestor_bounds; |
| 1438 | View* view = this; |
| 1439 | int root_x = 0; |
| 1440 | int root_y = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1441 | while (view != NULL && !vis_bounds.IsEmpty()) { |
| 1442 | root_x += view->GetX(APPLY_MIRRORING_TRANSFORMATION); |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1443 | root_y += view->y(); |
| 1444 | vis_bounds.Offset(view->GetX(APPLY_MIRRORING_TRANSFORMATION), view->y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1445 | View* ancestor = view->GetParent(); |
| 1446 | if (ancestor != NULL) { |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 1447 | ancestor_bounds.SetRect(0, 0, ancestor->width(), |
| 1448 | ancestor->height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1449 | vis_bounds = vis_bounds.Intersect(ancestor_bounds); |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 1450 | } else if (!view->GetWidget()) { |
| 1451 | // 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] | 1452 | 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 | |
| 1463 | int View::GetPageScrollIncrement(ScrollView* scroll_view, |
| 1464 | bool is_horizontal, bool is_positive) { |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | int View::GetLineScrollIncrement(ScrollView* scroll_view, |
| 1469 | bool is_horizontal, bool is_positive) { |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
[email protected] | 45da6c7 | 2009-10-28 20:45:42 | [diff] [blame] | 1473 | ThemeProvider* View::GetThemeProvider() const { |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1474 | Widget* widget = GetWidget(); |
| 1475 | return widget ? widget->GetThemeProvider() : NULL; |
| 1476 | } |
| 1477 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1478 | // static |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1479 | void View::RegisterChildrenForVisibleBoundsNotification( |
| 1480 | RootView* root, View* view) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1481 | DCHECK(root && view); |
| 1482 | if (view->GetNotifyWhenVisibleBoundsInRootChanges()) |
| 1483 | root->RegisterViewForVisibleBoundsNotification(view); |
| 1484 | for (int i = 0; i < view->GetChildViewCount(); ++i) |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1485 | RegisterChildrenForVisibleBoundsNotification(root, view->GetChildViewAt(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | // static |
[email protected] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1489 | void View::UnregisterChildrenForVisibleBoundsNotification( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1490 | 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] | 9fc4f20 | 2010-07-07 15:38:19 | [diff] [blame] | 1495 | UnregisterChildrenForVisibleBoundsNotification(root, |
| 1496 | view->GetChildViewAt(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | void 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 | |
| 1506 | void 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1517 | // DropInfo -------------------------------------------------------------------- |
| 1518 | |
| 1519 | void View::DragInfo::Reset() { |
| 1520 | possible_drag = false; |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1521 | start_pt = gfx::Point(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1522 | } |
| 1523 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1524 | void View::DragInfo::PossibleDrag(const gfx::Point& p) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1525 | possible_drag = true; |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1526 | start_pt = p; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | } // namespace |