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