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