[email protected] | 731433e | 2011-01-25 17:34:25 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 5 | #include "views/view.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 9 | #include "base/debug/trace_event.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/message_loop.h" |
[email protected] | 36de1cb | 2011-09-20 20:47:07 | [diff] [blame] | 13 | #include "base/stringprintf.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
[email protected] | eb93b619b | 2011-02-15 22:53:19 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkRect.h" |
[email protected] | 79e549f | 2011-03-14 06:56:33 | [diff] [blame] | 16 | #include "ui/base/accessibility/accessibility_types.h" |
[email protected] | 02bae0f | 2011-01-19 20:22:00 | [diff] [blame] | 17 | #include "ui/base/dragdrop/drag_drop_types.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 18 | #include "ui/gfx/canvas_skia.h" |
[email protected] | 7c06093 | 2011-03-23 15:49:10 | [diff] [blame] | 19 | #include "ui/gfx/compositor/compositor.h" |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 20 | #include "ui/gfx/compositor/layer.h" |
[email protected] | 36de1cb | 2011-09-20 20:47:07 | [diff] [blame] | 21 | #include "ui/gfx/interpolated_transform.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 22 | #include "ui/gfx/path.h" |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 23 | #include "ui/gfx/point3.h" |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 24 | #include "ui/gfx/transform.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 25 | #include "views/background.h" |
[email protected] | 1a2490c3 | 2011-06-23 17:01:55 | [diff] [blame] | 26 | #include "views/context_menu_controller.h" |
[email protected] | 3eaa3a8 | 2011-06-21 23:10:15 | [diff] [blame] | 27 | #include "views/drag_controller.h" |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 28 | #include "views/layer_property_setter.h" |
[email protected] | 731433e | 2011-01-25 17:34:25 | [diff] [blame] | 29 | #include "views/layout/layout_manager.h" |
[email protected] | 3ee83f2c | 2009-05-10 05:58:40 | [diff] [blame] | 30 | #include "views/views_delegate.h" |
[email protected] | 2f2b57b | 2011-06-16 17:21:23 | [diff] [blame] | 31 | #include "views/widget/native_widget_private.h" |
[email protected] | 8647714 | 2011-08-19 19:06:15 | [diff] [blame] | 32 | #include "views/widget/native_widget_views.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 33 | #include "views/widget/root_view.h" |
[email protected] | 319d4ae | 2009-05-28 19:09:45 | [diff] [blame] | 34 | #include "views/widget/tooltip_manager.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 35 | #include "views/widget/widget.h" |
[email protected] | 031e4d3 | 2009-12-29 01:13:23 | [diff] [blame] | 36 | |
[email protected] | 6ff244f | 2009-01-20 20:38:08 | [diff] [blame] | 37 | #if defined(OS_WIN) |
[email protected] | a684f12 | 2010-12-29 23:48:46 | [diff] [blame] | 38 | #include "base/win/scoped_gdi_object.h" |
[email protected] | 79e549f | 2011-03-14 06:56:33 | [diff] [blame] | 39 | #include "views/accessibility/native_view_accessibility_win.h" |
[email protected] | 6ff244f | 2009-01-20 20:38:08 | [diff] [blame] | 40 | #endif |
[email protected] | 9d43e37 | 2011-09-22 19:39:52 | [diff] [blame] | 41 | #if defined(TOOLKIT_USES_GTK) |
[email protected] | 9dd7e3d7 | 2011-01-20 18:27:06 | [diff] [blame] | 42 | #include "ui/base/gtk/scoped_handle_gtk.h" |
[email protected] | 031e4d3 | 2009-12-29 01:13:23 | [diff] [blame] | 43 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 45 | namespace { |
[email protected] | 3534e7a | 2011-05-26 17:44:03 | [diff] [blame] | 46 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 47 | // Whether to use accelerated compositing when necessary (e.g. when a view has a |
| 48 | // transformation). |
[email protected] | 4916d62 | 2011-09-28 23:45:38 | [diff] [blame] | 49 | #if defined(VIEWS_COMPOSITOR) |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 50 | bool use_acceleration_when_possible = true; |
[email protected] | f708081 | 2011-06-13 17:09:54 | [diff] [blame] | 51 | #else |
| 52 | bool use_acceleration_when_possible = false; |
| 53 | #endif |
[email protected] | f1785de | 2011-03-15 19:27:49 | [diff] [blame] | 54 | |
| 55 | // Saves the drawing state, and restores the state when going out of scope. |
| 56 | class ScopedCanvas { |
| 57 | public: |
| 58 | explicit ScopedCanvas(gfx::Canvas* canvas) : canvas_(canvas) { |
| 59 | if (canvas_) |
| 60 | canvas_->Save(); |
| 61 | } |
| 62 | ~ScopedCanvas() { |
| 63 | if (canvas_) |
| 64 | canvas_->Restore(); |
| 65 | } |
| 66 | void SetCanvas(gfx::Canvas* canvas) { |
| 67 | if (canvas_) |
| 68 | canvas_->Restore(); |
| 69 | canvas_ = canvas; |
| 70 | canvas_->Save(); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | gfx::Canvas* canvas_; |
| 75 | |
| 76 | DISALLOW_COPY_AND_ASSIGN(ScopedCanvas); |
| 77 | }; |
| 78 | |
[email protected] | 3534e7a | 2011-05-26 17:44:03 | [diff] [blame] | 79 | } // namespace |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 80 | |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 81 | namespace views { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | |
| 83 | // static |
[email protected] | 3ee83f2c | 2009-05-10 05:58:40 | [diff] [blame] | 84 | ViewsDelegate* ViewsDelegate::views_delegate = NULL; |
| 85 | |
| 86 | // static |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 87 | char View::kViewClassName[] = "views/View"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 89 | //////////////////////////////////////////////////////////////////////////////// |
| 90 | // View, public: |
| 91 | |
| 92 | // TO BE MOVED ----------------------------------------------------------------- |
| 93 | |
| 94 | void View::SetHotTracked(bool flag) { |
| 95 | } |
| 96 | |
[email protected] | 1646064 | 2011-03-04 23:15:53 | [diff] [blame] | 97 | bool View::IsHotTracked() const { |
| 98 | return false; |
| 99 | } |
| 100 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 101 | // Creation and lifetime ------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | |
| 103 | View::View() |
[email protected] | 23948b9 | 2011-06-02 00:22:09 | [diff] [blame] | 104 | : parent_owned_(true), |
[email protected] | fb880be | 2011-05-26 20:06:15 | [diff] [blame] | 105 | id_(0), |
| 106 | group_(-1), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | parent_(NULL), |
[email protected] | 5fe3f3a | 2011-06-11 00:31:32 | [diff] [blame] | 108 | visible_(true), |
[email protected] | 6ed82bb8 | 2011-05-27 20:24:07 | [diff] [blame] | 109 | enabled_(true), |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 110 | painting_enabled_(true), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | registered_for_visible_bounds_notification_(false), |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 112 | clip_x_(0.0), |
| 113 | clip_y_(0.0), |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 114 | needs_layout_(true), |
| 115 | flip_canvas_on_paint_for_rtl_ui_(false), |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 116 | paint_to_layer_(false), |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 117 | accelerator_registration_delayed_(false), |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 118 | accelerator_focus_manager_(NULL), |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 119 | registered_accelerator_count_(0), |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 120 | next_focusable_view_(NULL), |
| 121 | previous_focusable_view_(NULL), |
[email protected] | 23948b9 | 2011-06-02 00:22:09 | [diff] [blame] | 122 | focusable_(false), |
[email protected] | 5867c24 | 2011-05-27 02:42:50 | [diff] [blame] | 123 | accessibility_focusable_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | context_menu_controller_(NULL), |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 125 | drag_controller_(NULL) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | View::~View() { |
[email protected] | 8c57de2 | 2010-03-12 21:06:04 | [diff] [blame] | 129 | if (parent_) |
| 130 | parent_->RemoveChildView(this); |
| 131 | |
[email protected] | 24e3c96b | 2011-06-06 22:31:42 | [diff] [blame] | 132 | for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i) { |
| 133 | (*i)->parent_ = NULL; |
| 134 | if ((*i)->parent_owned()) |
| 135 | delete *i; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | } |
[email protected] | a64f3390 | 2009-10-10 05:41:23 | [diff] [blame] | 137 | |
| 138 | #if defined(OS_WIN) |
[email protected] | 79e549f | 2011-03-14 06:56:33 | [diff] [blame] | 139 | if (native_view_accessibility_win_.get()) |
| 140 | native_view_accessibility_win_->set_view(NULL); |
[email protected] | a64f3390 | 2009-10-10 05:41:23 | [diff] [blame] | 141 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 144 | // Tree operations ------------------------------------------------------------- |
| 145 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 146 | const Widget* View::GetWidget() const { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 147 | // The root view holds a reference to this view hierarchy's Widget. |
| 148 | return parent_ ? parent_->GetWidget() : NULL; |
| 149 | } |
| 150 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 151 | Widget* View::GetWidget() { |
| 152 | return const_cast<Widget*>(const_cast<const View*>(this)->GetWidget()); |
| 153 | } |
| 154 | |
| 155 | void View::AddChildView(View* view) { |
| 156 | AddChildViewAt(view, child_count()); |
| 157 | } |
| 158 | |
| 159 | void View::AddChildViewAt(View* view, int index) { |
[email protected] | cacf0af | 2011-06-16 23:31:11 | [diff] [blame] | 160 | CHECK_NE(view, this) << "You cannot add a view as its own child"; |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 161 | |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 162 | // If |view| has a parent, remove it from its parent. |
| 163 | View* parent = view->parent_; |
| 164 | if (parent) |
| 165 | parent->RemoveChildView(view); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 166 | |
| 167 | // Sets the prev/next focus views. |
| 168 | InitFocusSiblings(view, index); |
| 169 | |
| 170 | // Let's insert the view. |
[email protected] | b5fe744 | 2011-05-26 02:42:43 | [diff] [blame] | 171 | view->parent_ = this; |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 172 | children_.insert(children_.begin() + index, view); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 173 | |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 174 | for (View* v = this; v; v = v->parent_) |
| 175 | v->ViewHierarchyChangedImpl(false, true, this, view); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 176 | |
| 177 | view->PropagateAddNotifications(this, view); |
| 178 | UpdateTooltip(); |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 179 | if (GetWidget()) |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 180 | RegisterChildrenForVisibleBoundsNotification(view); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 181 | |
| 182 | if (layout_manager_.get()) |
| 183 | layout_manager_->ViewAdded(this, view); |
[email protected] | 18da0fd | 2011-10-07 16:25:31 | [diff] [blame] | 184 | |
| 185 | if (use_acceleration_when_possible) |
| 186 | ReorderLayers(); |
[email protected] | 25ae9a1 | 2011-10-12 14:55:22 | [diff] [blame] | 187 | |
| 188 | // Make sure the visibility of the child layers are correct. |
| 189 | // If any of the parent View is hidden, then the layers of the subtree |
| 190 | // rooted at |this| should be hidden. Otherwise, all the child layers should |
| 191 | // inherit the visibility of the owner View. |
| 192 | UpdateLayerVisibility(); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | ea1fc4b1 | 2011-06-16 17:49:04 | [diff] [blame] | 195 | void View::ReorderChildView(View* view, int index) { |
| 196 | DCHECK_EQ(view->parent_, this); |
| 197 | if (index < 0) |
| 198 | index = child_count() - 1; |
| 199 | else if (index >= child_count()) |
| 200 | return; |
| 201 | if (children_[index] == view) |
| 202 | return; |
| 203 | |
| 204 | const Views::iterator i(std::find(children_.begin(), children_.end(), view)); |
| 205 | DCHECK(i != children_.end()); |
| 206 | children_.erase(i); |
| 207 | |
| 208 | // Unlink the view first |
| 209 | View* next_focusable = view->next_focusable_view_; |
| 210 | View* prev_focusable = view->previous_focusable_view_; |
| 211 | if (prev_focusable) |
| 212 | prev_focusable->next_focusable_view_ = next_focusable; |
| 213 | if (next_focusable) |
| 214 | next_focusable->previous_focusable_view_ = prev_focusable; |
| 215 | |
| 216 | // Add it in the specified index now. |
| 217 | InitFocusSiblings(view, index); |
| 218 | children_.insert(children_.begin() + index, view); |
[email protected] | 18da0fd | 2011-10-07 16:25:31 | [diff] [blame] | 219 | |
| 220 | if (use_acceleration_when_possible) |
| 221 | ReorderLayers(); |
[email protected] | ea1fc4b1 | 2011-06-16 17:49:04 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 224 | void View::RemoveChildView(View* view) { |
| 225 | DoRemoveChildView(view, true, true, false); |
| 226 | } |
| 227 | |
[email protected] | 77a0875 | 2011-06-03 00:00:08 | [diff] [blame] | 228 | void View::RemoveAllChildViews(bool delete_children) { |
[email protected] | f27ddd7 | 2011-06-09 23:13:26 | [diff] [blame] | 229 | while (!children_.empty()) |
| 230 | DoRemoveChildView(children_.front(), false, false, delete_children); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 231 | UpdateTooltip(); |
| 232 | } |
| 233 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 234 | bool View::Contains(const View* view) const { |
[email protected] | 280cddd | 2011-05-17 01:00:46 | [diff] [blame] | 235 | for (const View* v = view; v; v = v->parent_) { |
| 236 | if (v == this) |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 237 | return true; |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | int View::GetIndexOf(const View* view) const { |
[email protected] | 695e942e | 2011-06-06 15:15:51 | [diff] [blame] | 243 | Views::const_iterator i(std::find(children_.begin(), children_.end(), view)); |
[email protected] | 26b2cb72 | 2011-06-07 16:35:09 | [diff] [blame] | 244 | return i != children_.end() ? static_cast<int>(i - children_.begin()) : -1; |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 247 | // Size and disposition -------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 249 | void View::SetBounds(int x, int y, int width, int height) { |
| 250 | SetBoundsRect(gfx::Rect(x, y, std::max(0, width), std::max(0, height))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 253 | void View::SetBoundsRect(const gfx::Rect& bounds) { |
[email protected] | de8d613d | 2010-10-25 22:30:10 | [diff] [blame] | 254 | if (bounds == bounds_) { |
| 255 | if (needs_layout_) { |
| 256 | needs_layout_ = false; |
| 257 | Layout(); |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 258 | SchedulePaint(); |
[email protected] | de8d613d | 2010-10-25 22:30:10 | [diff] [blame] | 259 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | return; |
[email protected] | de8d613d | 2010-10-25 22:30:10 | [diff] [blame] | 261 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 263 | if (IsVisible()) { |
| 264 | // Paint where the view is currently. |
| 265 | SchedulePaintBoundsChanged( |
| 266 | bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME : |
| 267 | SCHEDULE_PAINT_SIZE_CHANGED); |
| 268 | } |
| 269 | |
[email protected] | 80f8b9f | 2008-10-16 18:17:47 | [diff] [blame] | 270 | gfx::Rect prev = bounds_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 271 | bounds_ = bounds; |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 272 | BoundsChanged(prev); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 275 | void View::SetSize(const gfx::Size& size) { |
| 276 | SetBounds(x(), y(), size.width(), size.height()); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 277 | } |
| 278 | |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 279 | void View::SetPosition(const gfx::Point& position) { |
| 280 | SetBounds(position.x(), position.y(), width(), height()); |
| 281 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 283 | void View::SetX(int x) { |
| 284 | SetBounds(x, y(), width(), height()); |
| 285 | } |
| 286 | |
| 287 | void View::SetY(int y) { |
| 288 | SetBounds(x(), y, width(), height()); |
| 289 | } |
| 290 | |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 291 | gfx::Rect View::GetContentsBounds() const { |
| 292 | gfx::Rect contents_bounds(GetLocalBounds()); |
| 293 | if (border_.get()) { |
| 294 | gfx::Insets insets; |
| 295 | border_->GetInsets(&insets); |
| 296 | contents_bounds.Inset(insets); |
| 297 | } |
| 298 | return contents_bounds; |
| 299 | } |
| 300 | |
| 301 | gfx::Rect View::GetLocalBounds() const { |
[email protected] | 27488c2 | 2011-10-25 02:45:21 | [diff] [blame] | 302 | return gfx::Rect(gfx::Point(), size()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 305 | gfx::Insets View::GetInsets() const { |
| 306 | gfx::Insets insets; |
| 307 | if (border_.get()) |
| 308 | border_->GetInsets(&insets); |
| 309 | return insets; |
| 310 | } |
| 311 | |
[email protected] | 4d35252 | 2011-05-31 16:24:27 | [diff] [blame] | 312 | gfx::Rect View::GetVisibleBounds() const { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 313 | if (!IsVisibleInRootView()) |
| 314 | return gfx::Rect(); |
| 315 | gfx::Rect vis_bounds(0, 0, width(), height()); |
| 316 | gfx::Rect ancestor_bounds; |
[email protected] | 4d35252 | 2011-05-31 16:24:27 | [diff] [blame] | 317 | const View* view = this; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 318 | ui::Transform transform; |
| 319 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 320 | while (view != NULL && !vis_bounds.IsEmpty()) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 321 | transform.ConcatTransform(view->GetTransform()); |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 322 | transform.ConcatTranslate(static_cast<float>(view->GetMirroredX()), |
| 323 | static_cast<float>(view->y())); |
| 324 | |
| 325 | vis_bounds = view->ConvertRectToParent(vis_bounds); |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 326 | const View* ancestor = view->parent_; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 327 | if (ancestor != NULL) { |
[email protected] | 4d35252 | 2011-05-31 16:24:27 | [diff] [blame] | 328 | ancestor_bounds.SetRect(0, 0, ancestor->width(), ancestor->height()); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 329 | vis_bounds = vis_bounds.Intersect(ancestor_bounds); |
| 330 | } else if (!view->GetWidget()) { |
| 331 | // If the view has no Widget, we're not visible. Return an empty rect. |
| 332 | return gfx::Rect(); |
| 333 | } |
| 334 | view = ancestor; |
| 335 | } |
| 336 | if (vis_bounds.IsEmpty()) |
| 337 | return vis_bounds; |
| 338 | // Convert back to this views coordinate system. |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 339 | transform.TransformRectReverse(&vis_bounds); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 340 | return vis_bounds; |
| 341 | } |
| 342 | |
| 343 | gfx::Rect View::GetScreenBounds() const { |
| 344 | gfx::Point origin; |
| 345 | View::ConvertPointToScreen(this, &origin); |
| 346 | return gfx::Rect(origin, size()); |
| 347 | } |
| 348 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 349 | gfx::Size View::GetPreferredSize() { |
| 350 | if (layout_manager_.get()) |
| 351 | return layout_manager_->GetPreferredSize(this); |
| 352 | return gfx::Size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | } |
| 354 | |
[email protected] | 754b008 | 2011-05-27 22:53:11 | [diff] [blame] | 355 | int View::GetBaseline() const { |
[email protected] | a136016 | 2009-11-30 21:19:07 | [diff] [blame] | 356 | return -1; |
| 357 | } |
| 358 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | void View::SizeToPreferredSize() { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 360 | gfx::Size prefsize = GetPreferredSize(); |
| 361 | if ((prefsize.width() != width()) || (prefsize.height() != height())) |
| 362 | SetBounds(x(), y(), prefsize.width(), prefsize.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 365 | gfx::Size View::GetMinimumSize() { |
| 366 | return GetPreferredSize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | int View::GetHeightForWidth(int w) { |
| 370 | if (layout_manager_.get()) |
| 371 | return layout_manager_->GetPreferredHeightForWidth(this, w); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 372 | return GetPreferredSize().height(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 6ed82bb8 | 2011-05-27 20:24:07 | [diff] [blame] | 375 | void View::SetVisible(bool visible) { |
[email protected] | 5fe3f3a | 2011-06-11 00:31:32 | [diff] [blame] | 376 | if (visible != visible_) { |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 377 | // If the View is currently visible, schedule paint to refresh parent. |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 378 | // TODO(beng): not sure we should be doing this if we have a layer. |
[email protected] | 5fe3f3a | 2011-06-11 00:31:32 | [diff] [blame] | 379 | if (visible_) |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 380 | SchedulePaint(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 381 | |
[email protected] | 5fe3f3a | 2011-06-11 00:31:32 | [diff] [blame] | 382 | visible_ = visible; |
[email protected] | 1d27aa53 | 2011-07-06 15:38:59 | [diff] [blame] | 383 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 384 | // This notifies all sub-views recursively. |
[email protected] | 5fe3f3a | 2011-06-11 00:31:32 | [diff] [blame] | 385 | PropagateVisibilityNotifications(this, visible_); |
[email protected] | 25ae9a1 | 2011-10-12 14:55:22 | [diff] [blame] | 386 | UpdateLayerVisibility(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 387 | |
| 388 | // If we are newly visible, schedule paint. |
[email protected] | 5fe3f3a | 2011-06-11 00:31:32 | [diff] [blame] | 389 | if (visible_) |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 390 | SchedulePaint(); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 391 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | 1646064 | 2011-03-04 23:15:53 | [diff] [blame] | 394 | bool View::IsVisible() const { |
[email protected] | 5fe3f3a | 2011-06-11 00:31:32 | [diff] [blame] | 395 | return visible_; |
[email protected] | 1646064 | 2011-03-04 23:15:53 | [diff] [blame] | 396 | } |
| 397 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 398 | bool View::IsVisibleInRootView() const { |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 399 | return IsVisible() && parent_ ? parent_->IsVisibleInRootView() : false; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | 6ed82bb8 | 2011-05-27 20:24:07 | [diff] [blame] | 402 | void View::SetEnabled(bool enabled) { |
| 403 | if (enabled != enabled_) { |
| 404 | enabled_ = enabled; |
| 405 | OnEnabledChanged(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | bool View::IsEnabled() const { |
| 410 | return enabled_; |
| 411 | } |
| 412 | |
[email protected] | 6ed82bb8 | 2011-05-27 20:24:07 | [diff] [blame] | 413 | void View::OnEnabledChanged() { |
| 414 | SchedulePaint(); |
| 415 | } |
| 416 | |
[email protected] | eb93b619b | 2011-02-15 22:53:19 | [diff] [blame] | 417 | // Transformations ------------------------------------------------------------- |
| 418 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 419 | const ui::Transform& View::GetTransform() const { |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 420 | static const ui::Transform* no_op = new ui::Transform; |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 421 | return layer() ? layer()->transform() : *no_op; |
[email protected] | eb93b619b | 2011-02-15 22:53:19 | [diff] [blame] | 422 | } |
| 423 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 424 | void View::SetTransform(const ui::Transform& transform) { |
| 425 | if (!transform.HasChange()) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 426 | if (layer()) { |
| 427 | layer_property_setter_->SetTransform(layer(), transform); |
| 428 | if (!paint_to_layer_) |
| 429 | DestroyLayer(); |
[email protected] | 40500ba | 2011-09-30 23:39:51 | [diff] [blame] | 430 | } else { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 431 | // Nothing. |
[email protected] | 40500ba | 2011-09-30 23:39:51 | [diff] [blame] | 432 | } |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 433 | } else { |
| 434 | if (!layer()) |
| 435 | CreateLayer(); |
| 436 | layer_property_setter_->SetTransform(layer(), transform); |
| 437 | layer()->ScheduleDraw(); |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 438 | } |
[email protected] | eb93b619b | 2011-02-15 22:53:19 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 441 | void View::SetPaintToLayer(bool paint_to_layer) { |
| 442 | paint_to_layer_ = paint_to_layer; |
| 443 | if (paint_to_layer_ && !layer()) { |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 444 | CreateLayer(); |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 445 | } else if (!paint_to_layer_ && layer()) { |
| 446 | DestroyLayer(); |
[email protected] | 7c4b1b9 | 2011-06-27 16:37:18 | [diff] [blame] | 447 | } |
[email protected] | 6b8b0f0 | 2011-06-02 22:04:02 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 450 | void View::SetLayerPropertySetter(LayerPropertySetter* setter) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 451 | DCHECK(layer()); |
| 452 | LayerPropertySetter* old_setter = layer_property_setter_.get(); |
| 453 | if (!layer() || (old_setter && old_setter == setter)) |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 454 | return; |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 455 | if (!setter) |
| 456 | setter = LayerPropertySetter::CreateDefaultSetter(); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 457 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 458 | if (old_setter) |
| 459 | old_setter->Uninstalled(layer()); |
| 460 | layer_property_setter_.reset(setter); |
| 461 | layer_property_setter_->Installed(layer()); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 464 | // RTL positioning ------------------------------------------------------------- |
| 465 | |
[email protected] | 9defbad | 2011-02-08 04:23:18 | [diff] [blame] | 466 | gfx::Rect View::GetMirroredBounds() const { |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 467 | gfx::Rect bounds(bounds_); |
[email protected] | 9defbad | 2011-02-08 04:23:18 | [diff] [blame] | 468 | bounds.set_x(GetMirroredX()); |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 469 | return bounds; |
| 470 | } |
| 471 | |
[email protected] | 9defbad | 2011-02-08 04:23:18 | [diff] [blame] | 472 | gfx::Point View::GetMirroredPosition() const { |
| 473 | return gfx::Point(GetMirroredX(), y()); |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | 9defbad | 2011-02-08 04:23:18 | [diff] [blame] | 476 | int View::GetMirroredX() const { |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 477 | return parent_ ? parent_->GetMirroredXForRect(bounds_) : x(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | 9defbad | 2011-02-08 04:23:18 | [diff] [blame] | 480 | int View::GetMirroredXForRect(const gfx::Rect& bounds) const { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 481 | return base::i18n::IsRTL() ? |
| 482 | (width() - bounds.x() - bounds.width()) : bounds.x(); |
| 483 | } |
| 484 | |
[email protected] | 9defbad | 2011-02-08 04:23:18 | [diff] [blame] | 485 | int View::GetMirroredXInView(int x) const { |
| 486 | return base::i18n::IsRTL() ? width() - x : x; |
| 487 | } |
| 488 | |
| 489 | int View::GetMirroredXWithWidthInView(int x, int w) const { |
| 490 | return base::i18n::IsRTL() ? width() - x - w : x; |
| 491 | } |
| 492 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 493 | // Layout ---------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | |
| 495 | void View::Layout() { |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 496 | needs_layout_ = false; |
| 497 | |
[email protected] | f861719 | 2010-07-05 07:57:10 | [diff] [blame] | 498 | // If we have a layout manager, let it handle the layout for us. |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 499 | if (layout_manager_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | layout_manager_->Layout(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 501 | |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 502 | // Make sure to propagate the Layout() call to any children that haven't |
| 503 | // received it yet through the layout manager and need to be laid out. This |
| 504 | // is needed for the case when the child requires a layout but its bounds |
| 505 | // weren't changed by the layout manager. If there is no layout manager, we |
| 506 | // just propagate the Layout() call down the hierarchy, so whoever receives |
| 507 | // the call can take appropriate action. |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 508 | for (int i = 0, count = child_count(); i < count; ++i) { |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 509 | View* child = child_at(i); |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 510 | if (child->needs_layout_ || !layout_manager_.get()) { |
| 511 | child->needs_layout_ = false; |
| 512 | child->Layout(); |
| 513 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 517 | void View::InvalidateLayout() { |
[email protected] | de8d613d | 2010-10-25 22:30:10 | [diff] [blame] | 518 | // Always invalidate up. This is needed to handle the case of us already being |
| 519 | // valid, but not our parent. |
[email protected] | 9ea053e | 2010-07-15 08:19:05 | [diff] [blame] | 520 | needs_layout_ = true; |
| 521 | if (parent_) |
| 522 | parent_->InvalidateLayout(); |
| 523 | } |
| 524 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | LayoutManager* View::GetLayoutManager() const { |
| 526 | return layout_manager_.get(); |
| 527 | } |
| 528 | |
| 529 | void View::SetLayoutManager(LayoutManager* layout_manager) { |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 530 | if (layout_manager_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | layout_manager_->Uninstalled(this); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 532 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 533 | layout_manager_.reset(layout_manager); |
[email protected] | 09fe949 | 2009-11-07 02:23:06 | [diff] [blame] | 534 | if (layout_manager_.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 535 | layout_manager_->Installed(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 536 | } |
| 537 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 538 | // Attributes ------------------------------------------------------------------ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 539 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 540 | std::string View::GetClassName() const { |
| 541 | return kViewClassName; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 542 | } |
| 543 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 544 | View* View::GetAncestorWithClassName(const std::string& name) { |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 545 | for (View* view = this; view; view = view->parent_) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 546 | if (view->GetClassName() == name) |
| 547 | return view; |
| 548 | } |
| 549 | return NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 550 | } |
| 551 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 552 | const View* View::GetViewByID(int id) const { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 553 | if (id == id_) |
| 554 | return const_cast<View*>(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 555 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 556 | for (int i = 0, count = child_count(); i < count; ++i) { |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 557 | const View* view = child_at(i)->GetViewByID(id); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 558 | if (view) |
| 559 | return view; |
| 560 | } |
| 561 | return NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 562 | } |
| 563 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 564 | View* View::GetViewByID(int id) { |
| 565 | return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id)); |
| 566 | } |
| 567 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 568 | void View::SetGroup(int gid) { |
| 569 | // Don't change the group id once it's set. |
| 570 | DCHECK(group_ == -1 || group_ == gid); |
| 571 | group_ = gid; |
| 572 | } |
| 573 | |
| 574 | int View::GetGroup() const { |
| 575 | return group_; |
| 576 | } |
| 577 | |
[email protected] | 1646064 | 2011-03-04 23:15:53 | [diff] [blame] | 578 | bool View::IsGroupFocusTraversable() const { |
| 579 | return true; |
| 580 | } |
| 581 | |
[email protected] | 48250ad | 2011-07-13 22:06:28 | [diff] [blame] | 582 | void View::GetViewsInGroup(int group, Views* views) { |
| 583 | if (group_ == group) |
| 584 | views->push_back(this); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 585 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 586 | for (int i = 0, count = child_count(); i < count; ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 587 | child_at(i)->GetViewsInGroup(group, views); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 588 | } |
| 589 | |
[email protected] | 48250ad | 2011-07-13 22:06:28 | [diff] [blame] | 590 | View* View::GetSelectedViewForGroup(int group) { |
[email protected] | 695e942e | 2011-06-06 15:15:51 | [diff] [blame] | 591 | Views views; |
[email protected] | 48250ad | 2011-07-13 22:06:28 | [diff] [blame] | 592 | GetWidget()->GetRootView()->GetViewsInGroup(group, &views); |
[email protected] | 6775e40a | 2011-03-04 21:03:47 | [diff] [blame] | 593 | return views.empty() ? NULL : views[0]; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // Coordinate conversion ------------------------------------------------------- |
| 597 | |
| 598 | // static |
| 599 | void View::ConvertPointToView(const View* src, |
| 600 | const View* dst, |
| 601 | gfx::Point* point) { |
| 602 | ConvertPointToView(src, dst, point, true); |
| 603 | } |
| 604 | |
| 605 | // static |
| 606 | void View::ConvertPointToWidget(const View* src, gfx::Point* p) { |
| 607 | DCHECK(src); |
| 608 | DCHECK(p); |
| 609 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 610 | src->ConvertPointForAncestor(NULL, p); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | // static |
| 614 | void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) { |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 615 | DCHECK(dest); |
| 616 | DCHECK(p); |
| 617 | |
| 618 | dest->ConvertPointFromAncestor(NULL, p); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // static |
| 622 | void View::ConvertPointToScreen(const View* src, gfx::Point* p) { |
| 623 | DCHECK(src); |
| 624 | DCHECK(p); |
| 625 | |
| 626 | // If the view is not connected to a tree, there's nothing we can do. |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 627 | const Widget* widget = src->GetWidget(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 628 | if (widget) { |
| 629 | ConvertPointToWidget(src, p); |
[email protected] | 642c224f6 | 2011-03-03 17:30:05 | [diff] [blame] | 630 | gfx::Rect r = widget->GetClientAreaScreenBounds(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 631 | p->SetPoint(p->x() + r.x(), p->y() + r.y()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
[email protected] | eb93b619b | 2011-02-15 22:53:19 | [diff] [blame] | 635 | gfx::Rect View::ConvertRectToParent(const gfx::Rect& rect) const { |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 636 | gfx::Rect x_rect = rect; |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 637 | GetTransform().TransformRect(&x_rect); |
[email protected] | a1f4eb5 | 2011-06-29 15:06:04 | [diff] [blame] | 638 | x_rect.Offset(GetMirroredPosition()); |
| 639 | return x_rect; |
| 640 | } |
| 641 | |
| 642 | gfx::Rect View::ConvertRectToWidget(const gfx::Rect& rect) const { |
| 643 | gfx::Rect x_rect = rect; |
| 644 | for (const View* v = this; v; v = v->parent_) |
| 645 | x_rect = v->ConvertRectToParent(x_rect); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 646 | return x_rect; |
| 647 | } |
| 648 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 649 | // Painting -------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 650 | |
[email protected] | 7907e59 | 2011-02-15 19:05:30 | [diff] [blame] | 651 | void View::SchedulePaint() { |
[email protected] | 0a119c9 | 2011-02-23 17:50:56 | [diff] [blame] | 652 | SchedulePaintInRect(GetLocalBounds()); |
[email protected] | 7907e59 | 2011-02-15 19:05:30 | [diff] [blame] | 653 | } |
| 654 | |
[email protected] | 0a119c9 | 2011-02-23 17:50:56 | [diff] [blame] | 655 | void View::SchedulePaintInRect(const gfx::Rect& rect) { |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 656 | if (!IsVisible() || !painting_enabled_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 657 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 658 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 659 | if (layer()) { |
| 660 | layer()->SchedulePaint(rect); |
| 661 | } else if (parent_) { |
| 662 | // Translate the requested paint rect to the parent's coordinate system |
| 663 | // then pass this notification up to the parent. |
| 664 | parent_->SchedulePaintInRect(ConvertRectToParent(rect)); |
| 665 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 666 | } |
| 667 | |
[email protected] | 0bcebb0 | 2011-02-16 03:37:04 | [diff] [blame] | 668 | void View::Paint(gfx::Canvas* canvas) { |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 669 | TRACE_EVENT0("View", "Paint"); |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 670 | |
| 671 | ScopedCanvas scoped_canvas(canvas); |
| 672 | |
| 673 | // Paint this View and its children, setting the clip rect to the bounds |
| 674 | // of this View and translating the origin to the local bounds' top left |
| 675 | // point. |
| 676 | // |
| 677 | // Note that the X (or left) position we pass to ClipRectInt takes into |
| 678 | // consideration whether or not the view uses a right-to-left layout so that |
| 679 | // we paint our view in its mirrored position if need be. |
[email protected] | 7fe2839 | 2011-10-27 00:16:05 | [diff] [blame] | 680 | if (!canvas->ClipRect(gfx::Rect(GetMirroredX(), y(), |
| 681 | width() - static_cast<int>(clip_x_), |
| 682 | height() - static_cast<int>(clip_y_)))) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 683 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 684 | } |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 685 | // Non-empty clip, translate the graphics such that 0,0 corresponds to |
| 686 | // where this view is located (related to its parent). |
[email protected] | 7fe2839 | 2011-10-27 00:16:05 | [diff] [blame] | 687 | canvas->Translate(GetMirroredPosition()); |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 688 | canvas->Transform(GetTransform()); |
[email protected] | f1785de | 2011-03-15 19:27:49 | [diff] [blame] | 689 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 690 | PaintCommon(canvas); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 693 | ThemeProvider* View::GetThemeProvider() const { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 694 | const Widget* widget = GetWidget(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 695 | return widget ? widget->GetThemeProvider() : NULL; |
| 696 | } |
| 697 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 698 | // Accelerated Painting -------------------------------------------------------- |
| 699 | |
| 700 | // static |
| 701 | void View::set_use_acceleration_when_possible(bool use) { |
| 702 | use_acceleration_when_possible = use; |
| 703 | } |
| 704 | |
[email protected] | d7e2ee9d | 2011-06-09 13:07:11 | [diff] [blame] | 705 | // static |
| 706 | bool View::get_use_acceleration_when_possible() { |
| 707 | return use_acceleration_when_possible; |
| 708 | } |
| 709 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 710 | // Input ----------------------------------------------------------------------- |
| 711 | |
[email protected] | e4607f0 | 2011-02-25 21:51:51 | [diff] [blame] | 712 | View* View::GetEventHandlerForPoint(const gfx::Point& point) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 713 | // Walk the child Views recursively looking for the View that most |
| 714 | // tightly encloses the specified point. |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 715 | for (int i = child_count() - 1; i >= 0; --i) { |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 716 | View* child = child_at(i); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 717 | if (!child->IsVisible()) |
| 718 | continue; |
| 719 | |
| 720 | gfx::Point point_in_child_coords(point); |
| 721 | View::ConvertPointToView(this, child, &point_in_child_coords); |
| 722 | if (child->HitTest(point_in_child_coords)) |
[email protected] | e4607f0 | 2011-02-25 21:51:51 | [diff] [blame] | 723 | return child->GetEventHandlerForPoint(point_in_child_coords); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 724 | } |
| 725 | return this; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 726 | } |
| 727 | |
[email protected] | 3446c06 | 2011-05-03 21:48:00 | [diff] [blame] | 728 | gfx::NativeCursor View::GetCursor(const MouseEvent& event) { |
[email protected] | 9e591cb | 2011-10-17 18:14:32 | [diff] [blame] | 729 | #if defined(OS_WIN) && !defined(USE_AURA) |
[email protected] | 5c30396 | 2011-04-27 03:48:14 | [diff] [blame] | 730 | static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); |
| 731 | return arrow; |
| 732 | #else |
[email protected] | 6a736234 | 2011-10-11 04:20:33 | [diff] [blame] | 733 | return gfx::kNullCursor; |
[email protected] | 5c30396 | 2011-04-27 03:48:14 | [diff] [blame] | 734 | #endif |
[email protected] | 9abf8dd6 | 2009-06-04 06:40:42 | [diff] [blame] | 735 | } |
| 736 | |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 737 | bool View::HitTest(const gfx::Point& l) const { |
[email protected] | 5c30396 | 2011-04-27 03:48:14 | [diff] [blame] | 738 | if (GetLocalBounds().Contains(l)) { |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 739 | if (HasHitTestMask()) { |
| 740 | gfx::Path mask; |
| 741 | GetHitTestMask(&mask); |
[email protected] | b7bf6b68 | 2011-09-30 18:05:23 | [diff] [blame] | 742 | #if defined(USE_AURA) |
[email protected] | af9c1fa3 | 2011-10-04 15:29:02 | [diff] [blame] | 743 | // TODO: should we use this every where? |
| 744 | SkRegion clip_region; |
| 745 | clip_region.setRect(0, 0, width(), height()); |
| 746 | SkRegion mask_region; |
| 747 | return mask_region.setPath(mask, clip_region) && |
| 748 | mask_region.contains(l.x(), l.y()); |
[email protected] | b7bf6b68 | 2011-09-30 18:05:23 | [diff] [blame] | 749 | #elif defined(OS_WIN) |
[email protected] | a684f12 | 2010-12-29 23:48:46 | [diff] [blame] | 750 | base::win::ScopedRegion rgn(mask.CreateNativeRegion()); |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 751 | return !!PtInRegion(rgn, l.x(), l.y()); |
[email protected] | 10a6e77b | 2009-12-31 01:03:52 | [diff] [blame] | 752 | #elif defined(TOOLKIT_USES_GTK) |
[email protected] | 9dd7e3d7 | 2011-01-20 18:27:06 | [diff] [blame] | 753 | ui::ScopedRegion rgn(mask.CreateNativeRegion()); |
[email protected] | a340697 | 2009-11-04 05:05:48 | [diff] [blame] | 754 | return gdk_region_point_in(rgn.Get(), l.x(), l.y()); |
[email protected] | a52ca467 | 2009-06-05 05:41:09 | [diff] [blame] | 755 | #endif |
| 756 | } |
| 757 | // No mask, but inside our bounds. |
| 758 | return true; |
| 759 | } |
| 760 | // Outside our bounds. |
| 761 | return false; |
| 762 | } |
| 763 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 764 | bool View::OnMousePressed(const MouseEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 765 | return false; |
| 766 | } |
| 767 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 768 | bool View::OnMouseDragged(const MouseEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 769 | return false; |
| 770 | } |
| 771 | |
[email protected] | 3433825 | 2011-03-29 00:39:05 | [diff] [blame] | 772 | void View::OnMouseReleased(const MouseEvent& event) { |
| 773 | } |
| 774 | |
| 775 | void View::OnMouseCaptureLost() { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 776 | } |
| 777 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 778 | void View::OnMouseMoved(const MouseEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 779 | } |
| 780 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 781 | void View::OnMouseEntered(const MouseEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 782 | } |
| 783 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 784 | void View::OnMouseExited(const MouseEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 785 | } |
| 786 | |
[email protected] | 6a4056b | 2011-06-14 22:09:17 | [diff] [blame] | 787 | ui::TouchStatus View::OnTouchEvent(const TouchEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 788 | DVLOG(1) << "visited the OnTouchEvent"; |
[email protected] | 6a4056b | 2011-06-14 22:09:17 | [diff] [blame] | 789 | return ui::TOUCH_STATUS_UNKNOWN; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 790 | } |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 791 | |
| 792 | void View::SetMouseHandler(View *new_mouse_handler) { |
| 793 | // It is valid for new_mouse_handler to be NULL |
| 794 | if (parent_) |
| 795 | parent_->SetMouseHandler(new_mouse_handler); |
| 796 | } |
| 797 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 798 | bool View::OnKeyPressed(const KeyEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 799 | return false; |
| 800 | } |
| 801 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 802 | bool View::OnKeyReleased(const KeyEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 803 | return false; |
| 804 | } |
| 805 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 806 | bool View::OnMouseWheel(const MouseWheelEvent& event) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 807 | return false; |
| 808 | } |
| 809 | |
[email protected] | 9fa3de1a | 2011-03-31 21:12:08 | [diff] [blame] | 810 | TextInputClient* View::GetTextInputClient() { |
| 811 | return NULL; |
| 812 | } |
| 813 | |
| 814 | InputMethod* View::GetInputMethod() { |
[email protected] | 6c8a431 | 2011-04-01 22:05:14 | [diff] [blame] | 815 | Widget* widget = GetWidget(); |
| 816 | return widget ? widget->GetInputMethod() : NULL; |
[email protected] | 9fa3de1a | 2011-03-31 21:12:08 | [diff] [blame] | 817 | } |
| 818 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 819 | // Accelerators ---------------------------------------------------------------- |
| 820 | |
| 821 | void View::AddAccelerator(const Accelerator& accelerator) { |
| 822 | if (!accelerators_.get()) |
| 823 | accelerators_.reset(new std::vector<Accelerator>()); |
| 824 | |
[email protected] | afed457 | 2011-06-09 18:00:11 | [diff] [blame] | 825 | DCHECK(std::find(accelerators_->begin(), accelerators_->end(), accelerator) == |
| 826 | accelerators_->end()) |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 827 | << "Registering the same accelerator multiple times"; |
| 828 | |
| 829 | accelerators_->push_back(accelerator); |
| 830 | RegisterPendingAccelerators(); |
| 831 | } |
| 832 | |
| 833 | void View::RemoveAccelerator(const Accelerator& accelerator) { |
[email protected] | afed457 | 2011-06-09 18:00:11 | [diff] [blame] | 834 | if (!accelerators_.get()) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 835 | NOTREACHED() << "Removing non-existing accelerator"; |
| 836 | return; |
| 837 | } |
| 838 | |
[email protected] | afed457 | 2011-06-09 18:00:11 | [diff] [blame] | 839 | std::vector<Accelerator>::iterator i( |
| 840 | std::find(accelerators_->begin(), accelerators_->end(), accelerator)); |
| 841 | if (i == accelerators_->end()) { |
| 842 | NOTREACHED() << "Removing non-existing accelerator"; |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | size_t index = i - accelerators_->begin(); |
| 847 | accelerators_->erase(i); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 848 | if (index >= registered_accelerator_count_) { |
| 849 | // The accelerator is not registered to FocusManager. |
| 850 | return; |
| 851 | } |
| 852 | --registered_accelerator_count_; |
| 853 | |
[email protected] | 72f1724 | 2011-02-18 21:05:55 | [diff] [blame] | 854 | // Providing we are attached to a Widget and registered with a focus manager, |
| 855 | // we should de-register from that focus manager now. |
| 856 | if (GetWidget() && accelerator_focus_manager_) |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 857 | accelerator_focus_manager_->UnregisterAccelerator(accelerator, this); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | void View::ResetAccelerators() { |
| 861 | if (accelerators_.get()) |
| 862 | UnregisterAccelerators(false); |
| 863 | } |
| 864 | |
[email protected] | 1646064 | 2011-03-04 23:15:53 | [diff] [blame] | 865 | bool View::AcceleratorPressed(const Accelerator& accelerator) { |
| 866 | return false; |
| 867 | } |
| 868 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 869 | // Focus ----------------------------------------------------------------------- |
| 870 | |
[email protected] | 0aaa528 | 2011-10-26 18:54:50 | [diff] [blame] | 871 | bool View::HasFocus() const { |
| 872 | const FocusManager* focus_manager = GetFocusManager(); |
| 873 | return focus_manager && (focus_manager->GetFocusedView() == this); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | View* View::GetNextFocusableView() { |
| 877 | return next_focusable_view_; |
| 878 | } |
| 879 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 880 | const View* View::GetNextFocusableView() const { |
| 881 | return next_focusable_view_; |
| 882 | } |
| 883 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 884 | View* View::GetPreviousFocusableView() { |
| 885 | return previous_focusable_view_; |
| 886 | } |
| 887 | |
| 888 | void View::SetNextFocusableView(View* view) { |
| 889 | view->previous_focusable_view_ = this; |
| 890 | next_focusable_view_ = view; |
| 891 | } |
| 892 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 893 | bool View::IsFocusableInRootView() const { |
| 894 | return IsFocusable() && IsVisibleInRootView(); |
| 895 | } |
| 896 | |
| 897 | bool View::IsAccessibilityFocusableInRootView() const { |
| 898 | return (focusable_ || accessibility_focusable_) && IsEnabled() && |
| 899 | IsVisibleInRootView(); |
| 900 | } |
| 901 | |
| 902 | FocusManager* View::GetFocusManager() { |
| 903 | Widget* widget = GetWidget(); |
| 904 | return widget ? widget->GetFocusManager() : NULL; |
| 905 | } |
| 906 | |
[email protected] | 0aaa528 | 2011-10-26 18:54:50 | [diff] [blame] | 907 | const FocusManager* View::GetFocusManager() const { |
| 908 | const Widget* widget = GetWidget(); |
| 909 | return widget ? widget->GetFocusManager() : NULL; |
| 910 | } |
| 911 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 912 | void View::RequestFocus() { |
[email protected] | 72f1724 | 2011-02-18 21:05:55 | [diff] [blame] | 913 | FocusManager* focus_manager = GetFocusManager(); |
[email protected] | 6b78e39f | 2011-08-22 23:57:32 | [diff] [blame] | 914 | if (focus_manager && IsFocusableInRootView()) |
[email protected] | 72f1724 | 2011-02-18 21:05:55 | [diff] [blame] | 915 | focus_manager->SetFocusedView(this); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 916 | } |
| 917 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 918 | bool View::SkipDefaultKeyEventProcessing(const KeyEvent& event) { |
[email protected] | 1646064 | 2011-03-04 23:15:53 | [diff] [blame] | 919 | return false; |
| 920 | } |
| 921 | |
| 922 | FocusTraversable* View::GetFocusTraversable() { |
| 923 | return NULL; |
| 924 | } |
| 925 | |
| 926 | FocusTraversable* View::GetPaneFocusTraversable() { |
| 927 | return NULL; |
| 928 | } |
| 929 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 930 | // Tooltips -------------------------------------------------------------------- |
| 931 | |
[email protected] | 0aaa528 | 2011-10-26 18:54:50 | [diff] [blame] | 932 | bool View::GetTooltipText(const gfx::Point& p, string16* tooltip) const { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 933 | return false; |
| 934 | } |
| 935 | |
[email protected] | 0aaa528 | 2011-10-26 18:54:50 | [diff] [blame] | 936 | bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) const { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 937 | return false; |
| 938 | } |
| 939 | |
| 940 | // Context menus --------------------------------------------------------------- |
| 941 | |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 942 | void View::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) { |
[email protected] | 042811c | 2008-10-31 21:31:34 | [diff] [blame] | 943 | if (!context_menu_controller_) |
| 944 | return; |
| 945 | |
[email protected] | 0933ab8 | 2011-03-04 04:57:58 | [diff] [blame] | 946 | context_menu_controller_->ShowContextMenuForView(this, p, is_mouse_gesture); |
[email protected] | 042811c | 2008-10-31 21:31:34 | [diff] [blame] | 947 | } |
| 948 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 949 | // Drag and drop --------------------------------------------------------------- |
| 950 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 951 | bool View::GetDropFormats( |
| 952 | int* formats, |
| 953 | std::set<OSExchangeData::CustomFormat>* custom_formats) { |
| 954 | return false; |
| 955 | } |
| 956 | |
| 957 | bool View::AreDropTypesRequired() { |
| 958 | return false; |
| 959 | } |
| 960 | |
| 961 | bool View::CanDrop(const OSExchangeData& data) { |
| 962 | // TODO(sky): when I finish up migration, this should default to true. |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | void View::OnDragEntered(const DropTargetEvent& event) { |
| 967 | } |
| 968 | |
| 969 | int View::OnDragUpdated(const DropTargetEvent& event) { |
| 970 | return ui::DragDropTypes::DRAG_NONE; |
| 971 | } |
| 972 | |
| 973 | void View::OnDragExited() { |
| 974 | } |
| 975 | |
| 976 | int View::OnPerformDrop(const DropTargetEvent& event) { |
| 977 | return ui::DragDropTypes::DRAG_NONE; |
| 978 | } |
| 979 | |
[email protected] | 72f1724 | 2011-02-18 21:05:55 | [diff] [blame] | 980 | void View::OnDragDone() { |
| 981 | } |
| 982 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 983 | // static |
| 984 | bool View::ExceededDragThreshold(int delta_x, int delta_y) { |
| 985 | return (abs(delta_x) > GetHorizontalDragThreshold() || |
| 986 | abs(delta_y) > GetVerticalDragThreshold()); |
| 987 | } |
| 988 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 989 | // Scrolling ------------------------------------------------------------------- |
| 990 | |
| 991 | void View::ScrollRectToVisible(const gfx::Rect& rect) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 992 | // We must take RTL UI mirroring into account when adjusting the position of |
| 993 | // the region. |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 994 | if (parent_) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 995 | gfx::Rect scroll_rect(rect); |
[email protected] | 9defbad | 2011-02-08 04:23:18 | [diff] [blame] | 996 | scroll_rect.Offset(GetMirroredX(), y()); |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 997 | parent_->ScrollRectToVisible(scroll_rect); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | int View::GetPageScrollIncrement(ScrollView* scroll_view, |
| 1002 | bool is_horizontal, bool is_positive) { |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | int View::GetLineScrollIncrement(ScrollView* scroll_view, |
| 1007 | bool is_horizontal, bool is_positive) { |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | //////////////////////////////////////////////////////////////////////////////// |
| 1012 | // View, protected: |
| 1013 | |
| 1014 | // Size and disposition -------------------------------------------------------- |
| 1015 | |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 1016 | void View::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 1017 | } |
| 1018 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1019 | void View::PreferredSizeChanged() { |
| 1020 | InvalidateLayout(); |
| 1021 | if (parent_) |
| 1022 | parent_->ChildPreferredSizeChanged(this); |
| 1023 | } |
| 1024 | |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1025 | bool View::NeedsNotificationWhenVisibleBoundsChange() const { |
| 1026 | return false; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1027 | } |
| 1028 | |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1029 | void View::OnVisibleBoundsChanged() { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | // Tree operations ------------------------------------------------------------- |
| 1033 | |
| 1034 | void View::ViewHierarchyChanged(bool is_add, View* parent, View* child) { |
| 1035 | } |
| 1036 | |
| 1037 | void View::VisibilityChanged(View* starting_from, bool is_visible) { |
| 1038 | } |
| 1039 | |
| 1040 | void View::NativeViewHierarchyChanged(bool attached, |
| 1041 | gfx::NativeView native_view, |
[email protected] | 1094607 | 2011-05-20 15:40:45 | [diff] [blame] | 1042 | internal::RootView* root_view) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1043 | FocusManager* focus_manager = GetFocusManager(); |
| 1044 | if (!accelerator_registration_delayed_ && |
| 1045 | accelerator_focus_manager_ && |
| 1046 | accelerator_focus_manager_ != focus_manager) { |
| 1047 | UnregisterAccelerators(true); |
| 1048 | accelerator_registration_delayed_ = true; |
| 1049 | } |
| 1050 | if (accelerator_registration_delayed_ && attached) { |
| 1051 | if (focus_manager) { |
| 1052 | RegisterPendingAccelerators(); |
| 1053 | accelerator_registration_delayed_ = false; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | // Painting -------------------------------------------------------------------- |
| 1059 | |
[email protected] | 0bcebb0 | 2011-02-16 03:37:04 | [diff] [blame] | 1060 | void View::PaintChildren(gfx::Canvas* canvas) { |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1061 | for (int i = 0, count = child_count(); i < count; ++i) |
[email protected] | 2360158 | 2011-09-28 13:32:18 | [diff] [blame] | 1062 | if (!child_at(i)->layer()) |
| 1063 | child_at(i)->Paint(canvas); |
[email protected] | 0bcebb0 | 2011-02-16 03:37:04 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | void View::OnPaint(gfx::Canvas* canvas) { |
| 1067 | OnPaintBackground(canvas); |
| 1068 | OnPaintFocusBorder(canvas); |
| 1069 | OnPaintBorder(canvas); |
| 1070 | } |
| 1071 | |
| 1072 | void View::OnPaintBackground(gfx::Canvas* canvas) { |
| 1073 | if (background_.get()) |
| 1074 | background_->Paint(canvas, this); |
| 1075 | } |
| 1076 | |
| 1077 | void View::OnPaintBorder(gfx::Canvas* canvas) { |
| 1078 | if (border_.get()) |
| 1079 | border_->Paint(*this, canvas); |
| 1080 | } |
| 1081 | |
| 1082 | void View::OnPaintFocusBorder(gfx::Canvas* canvas) { |
[email protected] | a1a9e7c8 | 2011-04-15 21:58:24 | [diff] [blame] | 1083 | if ((IsFocusable() || IsAccessibilityFocusableInRootView()) && HasFocus()) |
[email protected] | 27488c2 | 2011-10-25 02:45:21 | [diff] [blame] | 1084 | canvas->DrawFocusRect(GetLocalBounds()); |
[email protected] | 0bcebb0 | 2011-02-16 03:37:04 | [diff] [blame] | 1085 | } |
| 1086 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 1087 | // Accelerated Painting -------------------------------------------------------- |
| 1088 | |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 1089 | void View::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1090 | // This method should not have the side-effect of creating the layer. |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 1091 | if (layer()) |
| 1092 | layer()->SetFillsBoundsOpaquely(fills_bounds_opaquely); |
| 1093 | } |
| 1094 | |
[email protected] | 1cbbee3c | 2011-06-24 12:32:19 | [diff] [blame] | 1095 | bool View::SetExternalTexture(ui::Texture* texture) { |
[email protected] | 28cd2bb | 2011-09-19 21:04:19 | [diff] [blame] | 1096 | DCHECK(texture); |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1097 | SetPaintToLayer(true); |
[email protected] | 7c4b1b9 | 2011-06-27 16:37:18 | [diff] [blame] | 1098 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1099 | layer()->SetExternalTexture(texture); |
[email protected] | 1cbbee3c | 2011-06-24 12:32:19 | [diff] [blame] | 1100 | |
[email protected] | 3a1a0cc | 2011-08-10 14:36:42 | [diff] [blame] | 1101 | // Child views must not paint into the external texture. So make sure each |
| 1102 | // child view has its own layer to paint into. |
[email protected] | 28cd2bb | 2011-09-19 21:04:19 | [diff] [blame] | 1103 | for (Views::iterator i = children_.begin(); i != children_.end(); ++i) |
| 1104 | (*i)->SetPaintToLayer(true); |
[email protected] | 3a1a0cc | 2011-08-10 14:36:42 | [diff] [blame] | 1105 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1106 | SchedulePaintInRect(GetLocalBounds()); |
[email protected] | 1cbbee3c | 2011-06-24 12:32:19 | [diff] [blame] | 1107 | return true; |
| 1108 | } |
| 1109 | |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1110 | void View::CalculateOffsetToAncestorWithLayer(gfx::Point* offset, |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1111 | ui::Layer** layer_parent) { |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1112 | if (layer()) { |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1113 | if (layer_parent) |
| 1114 | *layer_parent = layer(); |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1115 | return; |
| 1116 | } |
| 1117 | if (!parent_) |
| 1118 | return; |
| 1119 | |
| 1120 | offset->Offset(x(), y()); |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1121 | parent_->CalculateOffsetToAncestorWithLayer(offset, layer_parent); |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1122 | } |
| 1123 | |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1124 | void View::MoveLayerToParent(ui::Layer* parent_layer, |
| 1125 | const gfx::Point& point) { |
| 1126 | gfx::Point local_point(point); |
| 1127 | if (parent_layer != layer()) |
| 1128 | local_point.Offset(x(), y()); |
| 1129 | if (layer() && parent_layer != layer()) { |
| 1130 | parent_layer->Add(layer()); |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1131 | layer()->SetBounds(gfx::Rect(local_point.x(), local_point.y(), |
| 1132 | width(), height())); |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1133 | } else { |
| 1134 | for (int i = 0, count = child_count(); i < count; ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1135 | child_at(i)->MoveLayerToParent(parent_layer, local_point); |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
[email protected] | 25ae9a1 | 2011-10-12 14:55:22 | [diff] [blame] | 1139 | void View::UpdateLayerVisibility() { |
| 1140 | if (!use_acceleration_when_possible) |
| 1141 | return; |
| 1142 | bool visible = IsVisible(); |
| 1143 | for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_) |
| 1144 | visible = v->IsVisible(); |
| 1145 | |
| 1146 | UpdateChildLayerVisibility(visible); |
| 1147 | } |
| 1148 | |
| 1149 | void View::UpdateChildLayerVisibility(bool ancestor_visible) { |
| 1150 | if (layer()) { |
| 1151 | layer()->SetVisible(ancestor_visible && IsVisible()); |
| 1152 | } else { |
| 1153 | for (int i = 0, count = child_count(); i < count; ++i) |
| 1154 | child_at(i)->UpdateChildLayerVisibility(ancestor_visible && IsVisible()); |
| 1155 | } |
| 1156 | } |
| 1157 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1158 | void View::UpdateChildLayerBounds(const gfx::Point& offset) { |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1159 | if (layer()) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1160 | layer_property_setter_->SetBounds(layer(), gfx::Rect(offset.x(), offset.y(), |
| 1161 | width(), height())); |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1162 | } else { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1163 | for (int i = 0, count = child_count(); i < count; ++i) { |
| 1164 | gfx::Point new_offset(offset.x() + child_at(i)->x(), |
| 1165 | offset.y() + child_at(i)->y()); |
| 1166 | child_at(i)->UpdateChildLayerBounds(new_offset); |
| 1167 | } |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1171 | void View::OnPaintLayer(gfx::Canvas* canvas) { |
[email protected] | 1d83353 | 2011-10-26 15:00:17 | [diff] [blame] | 1172 | if (!layer() || !layer()->fills_bounds_opaquely()) |
| 1173 | canvas->GetSkCanvas()->drawColor(SK_ColorBLACK, SkXfermode::kClear_Mode); |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1174 | PaintCommon(canvas); |
| 1175 | } |
| 1176 | |
[email protected] | 326828b | 2011-10-21 02:01:10 | [diff] [blame] | 1177 | void View::OnLayerAnimationEnded(const ui::Animation* animation) { |
| 1178 | } |
| 1179 | |
[email protected] | 9ef00a5 | 2011-10-08 11:53:35 | [diff] [blame] | 1180 | void View::ReorderLayers() { |
| 1181 | View* v = this; |
| 1182 | while (v && !v->layer()) |
| 1183 | v = v->parent(); |
| 1184 | |
| 1185 | // Forward to widget in case we're in a NativeWidgetView. |
| 1186 | if (!v) { |
| 1187 | if (GetWidget()) |
| 1188 | GetWidget()->ReorderLayers(); |
| 1189 | } else { |
| 1190 | for (Views::const_iterator i(v->children_.begin()); |
| 1191 | i != v->children_.end(); |
| 1192 | ++i) |
| 1193 | (*i)->ReorderChildLayers(v->layer()); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | void View::ReorderChildLayers(ui::Layer* parent_layer) { |
| 1198 | if (layer()) { |
| 1199 | DCHECK_EQ(parent_layer, layer()->parent()); |
| 1200 | parent_layer->MoveToFront(layer()); |
| 1201 | } else { |
| 1202 | for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i) |
| 1203 | (*i)->ReorderChildLayers(parent_layer); |
| 1204 | } |
| 1205 | } |
| 1206 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1207 | // Input ----------------------------------------------------------------------- |
| 1208 | |
| 1209 | bool View::HasHitTestMask() const { |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
| 1213 | void View::GetHitTestMask(gfx::Path* mask) const { |
| 1214 | DCHECK(mask); |
| 1215 | } |
| 1216 | |
| 1217 | // Focus ----------------------------------------------------------------------- |
| 1218 | |
| 1219 | bool View::IsFocusable() const { |
| 1220 | return focusable_ && IsEnabled() && IsVisible(); |
| 1221 | } |
| 1222 | |
[email protected] | 42013833 | 2011-02-24 00:59:52 | [diff] [blame] | 1223 | void View::OnFocus() { |
| 1224 | // TODO(beng): Investigate whether it's possible for us to move this to |
| 1225 | // Focus(). |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1226 | // By default, we clear the native focus. This ensures that no visible native |
| 1227 | // view as the focus and that we still receive keyboard inputs. |
| 1228 | FocusManager* focus_manager = GetFocusManager(); |
| 1229 | if (focus_manager) |
| 1230 | focus_manager->ClearNativeFocus(); |
| 1231 | |
[email protected] | 42013833 | 2011-02-24 00:59:52 | [diff] [blame] | 1232 | // TODO(beng): Investigate whether it's possible for us to move this to |
| 1233 | // Focus(). |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1234 | // Notify assistive technologies of the focus change. |
[email protected] | 79e549f | 2011-03-14 06:56:33 | [diff] [blame] | 1235 | GetWidget()->NotifyAccessibilityEvent( |
| 1236 | this, ui::AccessibilityTypes::EVENT_FOCUS, true); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1237 | } |
| 1238 | |
[email protected] | 42013833 | 2011-02-24 00:59:52 | [diff] [blame] | 1239 | void View::OnBlur() { |
| 1240 | } |
| 1241 | |
| 1242 | void View::Focus() { |
| 1243 | SchedulePaint(); |
| 1244 | OnFocus(); |
| 1245 | } |
| 1246 | |
| 1247 | void View::Blur() { |
| 1248 | SchedulePaint(); |
| 1249 | OnBlur(); |
| 1250 | } |
| 1251 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1252 | // Tooltips -------------------------------------------------------------------- |
| 1253 | |
| 1254 | void View::TooltipTextChanged() { |
| 1255 | Widget* widget = GetWidget(); |
[email protected] | 2463b91b | 2011-06-13 21:11:37 | [diff] [blame] | 1256 | // TooltipManager may be null if there is a problem creating it. |
[email protected] | 2f2b57b | 2011-06-16 17:21:23 | [diff] [blame] | 1257 | if (widget && widget->native_widget_private()->GetTooltipManager()) { |
| 1258 | widget->native_widget_private()->GetTooltipManager()-> |
| 1259 | TooltipTextChanged(this); |
| 1260 | } |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | // Context menus --------------------------------------------------------------- |
| 1264 | |
| 1265 | gfx::Point View::GetKeyboardContextMenuLocation() { |
| 1266 | gfx::Rect vis_bounds = GetVisibleBounds(); |
| 1267 | gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2, |
| 1268 | vis_bounds.y() + vis_bounds.height() / 2); |
| 1269 | ConvertPointToScreen(this, &screen_point); |
| 1270 | return screen_point; |
| 1271 | } |
| 1272 | |
| 1273 | // Drag and drop --------------------------------------------------------------- |
| 1274 | |
| 1275 | int View::GetDragOperations(const gfx::Point& press_pt) { |
| 1276 | return drag_controller_ ? |
[email protected] | c3fac08 | 2011-03-04 01:04:18 | [diff] [blame] | 1277 | drag_controller_->GetDragOperationsForView(this, press_pt) : |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1278 | ui::DragDropTypes::DRAG_NONE; |
| 1279 | } |
| 1280 | |
| 1281 | void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) { |
| 1282 | DCHECK(drag_controller_); |
[email protected] | c3fac08 | 2011-03-04 01:04:18 | [diff] [blame] | 1283 | drag_controller_->WriteDragDataForView(this, press_pt, data); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1284 | } |
| 1285 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1286 | bool View::InDrag() { |
[email protected] | 72f1724 | 2011-02-18 21:05:55 | [diff] [blame] | 1287 | Widget* widget = GetWidget(); |
[email protected] | 9861ae9 | 2011-03-03 20:15:03 | [diff] [blame] | 1288 | return widget ? widget->dragged_view() == this : false; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1289 | } |
| 1290 | |
[email protected] | 36de1cb | 2011-09-20 20:47:07 | [diff] [blame] | 1291 | // Debugging ------------------------------------------------------------------- |
| 1292 | |
| 1293 | #if !defined(NDEBUG) |
| 1294 | |
| 1295 | std::string View::PrintViewGraph(bool first) { |
| 1296 | return DoPrintViewGraph(first, this); |
| 1297 | } |
| 1298 | |
| 1299 | std::string View::DoPrintViewGraph(bool first, View* view_with_children) { |
| 1300 | // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19. |
| 1301 | const size_t kMaxPointerStringLength = 19; |
| 1302 | |
| 1303 | std::string result; |
| 1304 | |
| 1305 | if (first) |
| 1306 | result.append("digraph {\n"); |
| 1307 | |
| 1308 | // Node characteristics. |
| 1309 | char p[kMaxPointerStringLength]; |
| 1310 | |
| 1311 | size_t baseNameIndex = GetClassName().find_last_of('/'); |
| 1312 | if (baseNameIndex == std::string::npos) |
| 1313 | baseNameIndex = 0; |
| 1314 | else |
| 1315 | baseNameIndex++; |
| 1316 | |
| 1317 | char bounds_buffer[512]; |
| 1318 | |
| 1319 | // Information about current node. |
| 1320 | base::snprintf(p, arraysize(bounds_buffer), "%p", view_with_children); |
| 1321 | result.append(" N"); |
| 1322 | result.append(p+2); |
| 1323 | result.append(" [label=\""); |
| 1324 | |
| 1325 | result.append(GetClassName().substr(baseNameIndex).c_str()); |
| 1326 | |
| 1327 | base::snprintf(bounds_buffer, |
| 1328 | arraysize(bounds_buffer), |
| 1329 | "\\n bounds: (%d, %d), (%dx%d)", |
| 1330 | this->bounds().x(), |
| 1331 | this->bounds().y(), |
| 1332 | this->bounds().width(), |
| 1333 | this->bounds().height()); |
| 1334 | result.append(bounds_buffer); |
| 1335 | |
| 1336 | if (layer() && !layer()->hole_rect().IsEmpty()) { |
| 1337 | base::snprintf(bounds_buffer, |
| 1338 | arraysize(bounds_buffer), |
| 1339 | "\\n hole bounds: (%d, %d), (%dx%d)", |
| 1340 | layer()->hole_rect().x(), |
| 1341 | layer()->hole_rect().y(), |
| 1342 | layer()->hole_rect().width(), |
| 1343 | layer()->hole_rect().height()); |
| 1344 | result.append(bounds_buffer); |
| 1345 | } |
| 1346 | |
| 1347 | if (GetTransform().HasChange()) { |
| 1348 | gfx::Point translation; |
| 1349 | float rotation; |
| 1350 | gfx::Point3f scale; |
| 1351 | if (ui::InterpolatedTransform::FactorTRS(GetTransform(), |
| 1352 | &translation, |
| 1353 | &rotation, |
| 1354 | &scale)) { |
| 1355 | if (translation != gfx::Point(0, 0)) { |
| 1356 | base::snprintf(bounds_buffer, |
| 1357 | arraysize(bounds_buffer), |
| 1358 | "\\n translation: (%d, %d)", |
| 1359 | translation.x(), |
| 1360 | translation.y()); |
| 1361 | result.append(bounds_buffer); |
| 1362 | } |
| 1363 | |
| 1364 | if (fabs(rotation) > 1e-5) { |
| 1365 | base::snprintf(bounds_buffer, |
| 1366 | arraysize(bounds_buffer), |
| 1367 | "\\n rotation: %3.2f", rotation); |
| 1368 | result.append(bounds_buffer); |
| 1369 | } |
| 1370 | |
| 1371 | if (scale.AsPoint() != gfx::Point(0, 0)) { |
| 1372 | base::snprintf(bounds_buffer, |
| 1373 | arraysize(bounds_buffer), |
| 1374 | "\\n scale: (%2.4f, %2.4f)", |
| 1375 | scale.x(), |
| 1376 | scale.y()); |
| 1377 | result.append(bounds_buffer); |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | result.append("\""); |
| 1383 | if (!parent_) |
| 1384 | result.append(", shape=box"); |
| 1385 | if (layer()) { |
| 1386 | if (layer()->texture()) |
| 1387 | result.append(", color=green"); |
| 1388 | else |
| 1389 | result.append(", color=red"); |
| 1390 | |
| 1391 | if (layer()->fills_bounds_opaquely()) |
| 1392 | result.append(", style=filled"); |
| 1393 | } |
| 1394 | result.append("]\n"); |
| 1395 | |
| 1396 | // Link to parent. |
| 1397 | if (parent_) { |
| 1398 | char pp[kMaxPointerStringLength]; |
| 1399 | |
| 1400 | base::snprintf(pp, kMaxPointerStringLength, "%p", parent_); |
| 1401 | result.append(" N"); |
| 1402 | result.append(pp+2); |
| 1403 | result.append(" -> N"); |
| 1404 | result.append(p+2); |
| 1405 | result.append("\n"); |
| 1406 | } |
| 1407 | |
| 1408 | // Children. |
| 1409 | for (int i = 0, count = view_with_children->child_count(); i < count; ++i) |
| 1410 | result.append(view_with_children->child_at(i)->PrintViewGraph(false)); |
| 1411 | |
| 1412 | if (first) |
| 1413 | result.append("}\n"); |
| 1414 | |
| 1415 | return result; |
| 1416 | } |
| 1417 | |
| 1418 | #endif |
| 1419 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1420 | //////////////////////////////////////////////////////////////////////////////// |
| 1421 | // View, private: |
| 1422 | |
| 1423 | // DropInfo -------------------------------------------------------------------- |
| 1424 | |
| 1425 | void View::DragInfo::Reset() { |
| 1426 | possible_drag = false; |
| 1427 | start_pt = gfx::Point(); |
| 1428 | } |
| 1429 | |
| 1430 | void View::DragInfo::PossibleDrag(const gfx::Point& p) { |
| 1431 | possible_drag = true; |
| 1432 | start_pt = p; |
| 1433 | } |
| 1434 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1435 | // Painting -------------------------------------------------------------------- |
| 1436 | |
| 1437 | void View::SchedulePaintBoundsChanged(SchedulePaintType type) { |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1438 | // If we have a layer and the View's size did not change, we do not need to |
| 1439 | // schedule any paints since the layer will be redrawn at its new location |
| 1440 | // during the next Draw() cycle in the compositor. |
| 1441 | if (!layer() || type == SCHEDULE_PAINT_SIZE_CHANGED) { |
| 1442 | // Otherwise, if the size changes or we don't have a layer then we need to |
| 1443 | // use SchedulePaint to invalidate the area occupied by the View. |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1444 | SchedulePaint(); |
[email protected] | 3309710 | 2011-10-12 13:19:02 | [diff] [blame] | 1445 | } else if (parent_ && type == SCHEDULE_PAINT_SIZE_SAME) { |
| 1446 | // The compositor doesn't Draw() until something on screen changes, so |
| 1447 | // if our position changes but nothing is being animated on screen, then |
| 1448 | // tell the compositor to redraw the scene. We know layer() exists due to |
| 1449 | // the above if clause. |
| 1450 | layer()->ScheduleDraw(); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1454 | void View::PaintCommon(gfx::Canvas* canvas) { |
| 1455 | if (!IsVisible() || !painting_enabled_) |
| 1456 | return; |
| 1457 | |
| 1458 | { |
| 1459 | // If the View we are about to paint requested the canvas to be flipped, we |
| 1460 | // should change the transform appropriately. |
| 1461 | // The canvas mirroring is undone once the View is done painting so that we |
| 1462 | // don't pass the canvas with the mirrored transform to Views that didn't |
| 1463 | // request the canvas to be flipped. |
| 1464 | ScopedCanvas scoped(canvas); |
| 1465 | if (FlipCanvasOnPaintForRTLUI()) { |
[email protected] | 7fe2839 | 2011-10-27 00:16:05 | [diff] [blame] | 1466 | canvas->Translate(gfx::Point(width(), 0)); |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 1467 | canvas->ScaleInt(-1, 1); |
| 1468 | } |
| 1469 | |
| 1470 | OnPaint(canvas); |
| 1471 | } |
| 1472 | |
| 1473 | PaintChildren(canvas); |
| 1474 | } |
| 1475 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1476 | // Tree operations ------------------------------------------------------------- |
| 1477 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1478 | void View::DoRemoveChildView(View* view, |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1479 | bool update_focus_cycle, |
| 1480 | bool update_tool_tip, |
| 1481 | bool delete_removed_view) { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1482 | DCHECK(view); |
[email protected] | afed457 | 2011-06-09 18:00:11 | [diff] [blame] | 1483 | const Views::iterator i(std::find(children_.begin(), children_.end(), view)); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1484 | scoped_ptr<View> view_to_be_deleted; |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1485 | if (i != children_.end()) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1486 | if (update_focus_cycle) { |
| 1487 | // Let's remove the view from the focus traversal. |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1488 | View* next_focusable = view->next_focusable_view_; |
| 1489 | View* prev_focusable = view->previous_focusable_view_; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1490 | if (prev_focusable) |
| 1491 | prev_focusable->next_focusable_view_ = next_focusable; |
| 1492 | if (next_focusable) |
| 1493 | next_focusable->previous_focusable_view_ = prev_focusable; |
| 1494 | } |
| 1495 | |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1496 | if (GetWidget()) |
| 1497 | UnregisterChildrenForVisibleBoundsNotification(view); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1498 | view->PropagateRemoveNotifications(this); |
[email protected] | b5fe744 | 2011-05-26 02:42:43 | [diff] [blame] | 1499 | view->parent_ = NULL; |
[email protected] | 25ae9a1 | 2011-10-12 14:55:22 | [diff] [blame] | 1500 | view->UpdateLayerVisibility(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1501 | |
[email protected] | 3534e7a | 2011-05-26 17:44:03 | [diff] [blame] | 1502 | if (delete_removed_view && view->parent_owned()) |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1503 | view_to_be_deleted.reset(view); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1504 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1505 | children_.erase(i); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | if (update_tool_tip) |
| 1509 | UpdateTooltip(); |
| 1510 | |
| 1511 | if (layout_manager_.get()) |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1512 | layout_manager_->ViewRemoved(this, view); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1513 | } |
| 1514 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1515 | void View::PropagateRemoveNotifications(View* parent) { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1516 | for (int i = 0, count = child_count(); i < count; ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1517 | child_at(i)->PropagateRemoveNotifications(parent); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1518 | |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 1519 | for (View* v = this; v; v = v->parent_) |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1520 | v->ViewHierarchyChangedImpl(true, false, parent, this); |
| 1521 | } |
| 1522 | |
| 1523 | void View::PropagateAddNotifications(View* parent, View* child) { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1524 | for (int i = 0, count = child_count(); i < count; ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1525 | child_at(i)->PropagateAddNotifications(parent, child); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1526 | ViewHierarchyChangedImpl(true, true, parent, child); |
| 1527 | } |
| 1528 | |
| 1529 | void View::PropagateNativeViewHierarchyChanged(bool attached, |
| 1530 | gfx::NativeView native_view, |
[email protected] | 1094607 | 2011-05-20 15:40:45 | [diff] [blame] | 1531 | internal::RootView* root_view) { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1532 | for (int i = 0, count = child_count(); i < count; ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1533 | child_at(i)->PropagateNativeViewHierarchyChanged(attached, |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1534 | native_view, |
| 1535 | root_view); |
| 1536 | NativeViewHierarchyChanged(attached, native_view, root_view); |
| 1537 | } |
| 1538 | |
| 1539 | void View::ViewHierarchyChangedImpl(bool register_accelerators, |
| 1540 | bool is_add, |
| 1541 | View* parent, |
| 1542 | View* child) { |
| 1543 | if (register_accelerators) { |
| 1544 | if (is_add) { |
| 1545 | // If you get this registration, you are part of a subtree that has been |
| 1546 | // added to the view hierarchy. |
| 1547 | if (GetFocusManager()) { |
| 1548 | RegisterPendingAccelerators(); |
| 1549 | } else { |
| 1550 | // Delay accelerator registration until visible as we do not have |
| 1551 | // focus manager until then. |
| 1552 | accelerator_registration_delayed_ = true; |
| 1553 | } |
| 1554 | } else { |
| 1555 | if (child == this) |
| 1556 | UnregisterAccelerators(true); |
| 1557 | } |
| 1558 | } |
| 1559 | |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1560 | if (is_add && layer() && !layer()->parent()) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1561 | UpdateParentLayer(); |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1562 | } else if (!is_add && child == this) { |
| 1563 | // Make sure the layers beloning to the subtree rooted at |child| get |
| 1564 | // removed from layers that do not belong in the same subtree. |
| 1565 | OrphanLayers(); |
| 1566 | } |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1567 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1568 | ViewHierarchyChanged(is_add, parent, child); |
| 1569 | parent->needs_layout_ = true; |
| 1570 | } |
| 1571 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1572 | // Size and disposition -------------------------------------------------------- |
| 1573 | |
| 1574 | void View::PropagateVisibilityNotifications(View* start, bool is_visible) { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1575 | for (int i = 0, count = child_count(); i < count; ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1576 | child_at(i)->PropagateVisibilityNotifications(start, is_visible); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1577 | VisibilityChangedImpl(start, is_visible); |
| 1578 | } |
| 1579 | |
| 1580 | void View::VisibilityChangedImpl(View* starting_from, bool is_visible) { |
| 1581 | if (is_visible) |
| 1582 | RegisterPendingAccelerators(); |
| 1583 | else |
| 1584 | UnregisterAccelerators(true); |
| 1585 | VisibilityChanged(starting_from, is_visible); |
| 1586 | } |
| 1587 | |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 1588 | void View::BoundsChanged(const gfx::Rect& previous_bounds) { |
[email protected] | 19bebd0 | 2011-04-06 18:27:34 | [diff] [blame] | 1589 | if (IsVisible()) { |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1590 | // Paint the new bounds. |
| 1591 | SchedulePaintBoundsChanged( |
| 1592 | bounds_.size() == previous_bounds.size() ? SCHEDULE_PAINT_SIZE_SAME : |
| 1593 | SCHEDULE_PAINT_SIZE_CHANGED); |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1594 | } |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1595 | |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1596 | if (use_acceleration_when_possible) { |
| 1597 | if (layer()) { |
| 1598 | if (parent_) { |
[email protected] | e865b36 | 2011-07-01 17:09:46 | [diff] [blame] | 1599 | gfx::Point offset; |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1600 | parent_->CalculateOffsetToAncestorWithLayer(&offset, NULL); |
| 1601 | offset.Offset(x(), y()); |
| 1602 | layer_property_setter_->SetBounds(layer(), gfx::Rect(offset, size())); |
| 1603 | } else { |
| 1604 | layer_property_setter_->SetBounds(layer(), bounds_); |
[email protected] | ee01285 | 2011-06-24 02:46:20 | [diff] [blame] | 1605 | } |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1606 | // TODO(beng): this seems redundant with the SchedulePaint at the top of |
| 1607 | // this function. explore collapsing. |
| 1608 | if (previous_bounds.size() != bounds_.size() && |
| 1609 | !layer()->layer_updated_externally()) { |
| 1610 | // If our bounds have changed then we need to update the complete |
| 1611 | // texture. |
| 1612 | layer()->SchedulePaint(GetLocalBounds()); |
| 1613 | } |
| 1614 | } else { |
| 1615 | // If our bounds have changed, then any descendant layer bounds may |
| 1616 | // have changed. Update them accordingly. |
| 1617 | gfx::Point offset; |
| 1618 | CalculateOffsetToAncestorWithLayer(&offset, NULL); |
| 1619 | UpdateChildLayerBounds(offset); |
[email protected] | 19bebd0 | 2011-04-06 18:27:34 | [diff] [blame] | 1620 | } |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | OnBoundsChanged(previous_bounds); |
| 1624 | |
| 1625 | if (previous_bounds.size() != size()) { |
| 1626 | needs_layout_ = false; |
| 1627 | Layout(); |
| 1628 | } |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1629 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1630 | if (NeedsNotificationWhenVisibleBoundsChange()) |
[email protected] | 44e9d9c | 2011-04-14 21:04:06 | [diff] [blame] | 1631 | OnVisibleBoundsChanged(); |
[email protected] | 44e9d9c | 2011-04-14 21:04:06 | [diff] [blame] | 1632 | |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1633 | // Notify interested Views that visible bounds within the root view may have |
| 1634 | // changed. |
| 1635 | if (descendants_to_notify_.get()) { |
[email protected] | afed457 | 2011-06-09 18:00:11 | [diff] [blame] | 1636 | for (Views::iterator i(descendants_to_notify_->begin()); |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1637 | i != descendants_to_notify_->end(); ++i) { |
| 1638 | (*i)->OnVisibleBoundsChanged(); |
| 1639 | } |
| 1640 | } |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | // static |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1644 | void View::RegisterChildrenForVisibleBoundsNotification(View* view) { |
| 1645 | if (view->NeedsNotificationWhenVisibleBoundsChange()) |
| 1646 | view->RegisterForVisibleBoundsNotification(); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 1647 | for (int i = 0; i < view->child_count(); ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1648 | RegisterChildrenForVisibleBoundsNotification(view->child_at(i)); |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | // static |
| 1652 | void View::UnregisterChildrenForVisibleBoundsNotification(View* view) { |
| 1653 | if (view->NeedsNotificationWhenVisibleBoundsChange()) |
| 1654 | view->UnregisterForVisibleBoundsNotification(); |
| 1655 | for (int i = 0; i < view->child_count(); ++i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 1656 | UnregisterChildrenForVisibleBoundsNotification(view->child_at(i)); |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | void View::RegisterForVisibleBoundsNotification() { |
| 1660 | if (registered_for_visible_bounds_notification_) |
| 1661 | return; |
[email protected] | 44e9d9c | 2011-04-14 21:04:06 | [diff] [blame] | 1662 | |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1663 | registered_for_visible_bounds_notification_ = true; |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 1664 | for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_) |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1665 | ancestor->AddDescendantToNotify(this); |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | void View::UnregisterForVisibleBoundsNotification() { |
| 1669 | if (!registered_for_visible_bounds_notification_) |
| 1670 | return; |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 1671 | |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1672 | registered_for_visible_bounds_notification_ = false; |
[email protected] | 2fa942b | 2011-06-14 15:59:55 | [diff] [blame] | 1673 | for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_) |
[email protected] | 6260f9e | 2011-02-16 21:28:01 | [diff] [blame] | 1674 | ancestor->RemoveDescendantToNotify(this); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | void View::AddDescendantToNotify(View* view) { |
| 1678 | DCHECK(view); |
| 1679 | if (!descendants_to_notify_.get()) |
[email protected] | 695e942e | 2011-06-06 15:15:51 | [diff] [blame] | 1680 | descendants_to_notify_.reset(new Views); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1681 | descendants_to_notify_->push_back(view); |
| 1682 | } |
| 1683 | |
| 1684 | void View::RemoveDescendantToNotify(View* view) { |
| 1685 | DCHECK(view && descendants_to_notify_.get()); |
[email protected] | afed457 | 2011-06-09 18:00:11 | [diff] [blame] | 1686 | Views::iterator i(std::find( |
| 1687 | descendants_to_notify_->begin(), descendants_to_notify_->end(), view)); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1688 | DCHECK(i != descendants_to_notify_->end()); |
| 1689 | descendants_to_notify_->erase(i); |
| 1690 | if (descendants_to_notify_->empty()) |
| 1691 | descendants_to_notify_.reset(); |
| 1692 | } |
| 1693 | |
[email protected] | eb93b619b | 2011-02-15 22:53:19 | [diff] [blame] | 1694 | // Transformations ------------------------------------------------------------- |
| 1695 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 1696 | bool View::GetTransformRelativeTo(const View* ancestor, |
| 1697 | ui::Transform* transform) const { |
[email protected] | 6bb8b6c | 2011-05-19 22:09:05 | [diff] [blame] | 1698 | const View* p = this; |
| 1699 | |
| 1700 | while (p && p != ancestor) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1701 | transform->ConcatTransform(p->GetTransform()); |
[email protected] | 6bb8b6c | 2011-05-19 22:09:05 | [diff] [blame] | 1702 | transform->ConcatTranslate(static_cast<float>(p->GetMirroredX()), |
| 1703 | static_cast<float>(p->y())); |
| 1704 | |
| 1705 | p = p->parent_; |
[email protected] | c36b0bf | 2011-05-10 18:26:25 | [diff] [blame] | 1706 | } |
[email protected] | 6bb8b6c | 2011-05-19 22:09:05 | [diff] [blame] | 1707 | |
| 1708 | return p == ancestor; |
[email protected] | c36b0bf | 2011-05-10 18:26:25 | [diff] [blame] | 1709 | } |
| 1710 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1711 | // Coordinate conversion ------------------------------------------------------- |
| 1712 | |
| 1713 | // static |
| 1714 | void View::ConvertPointToView(const View* src, |
| 1715 | const View* dst, |
| 1716 | gfx::Point* point, |
| 1717 | bool try_other_direction) { |
| 1718 | // src can be NULL |
| 1719 | DCHECK(dst); |
| 1720 | DCHECK(point); |
| 1721 | |
[email protected] | 8647714 | 2011-08-19 19:06:15 | [diff] [blame] | 1722 | const Widget* src_widget = src ? src->GetWidget() : NULL ; |
| 1723 | const Widget* dst_widget = dst->GetWidget(); |
| 1724 | // If dest and src aren't in the same widget, try to convert the |
| 1725 | // point to the destination widget's coordinates first. |
| 1726 | // TODO(oshima|sadrul): Cleanup and consolidate conversion methods. |
| 1727 | if (Widget::IsPureViews() && src_widget && src_widget != dst_widget) { |
| 1728 | // convert to src_widget first. |
| 1729 | gfx::Point p = *point; |
| 1730 | src->ConvertPointForAncestor(src_widget->GetRootView(), &p); |
| 1731 | if (dst_widget->ConvertPointFromAncestor(src_widget, &p)) { |
| 1732 | // Convertion to destination widget's coordinates was successful. |
| 1733 | // Use destination's root as a source to convert the point further. |
| 1734 | src = dst_widget->GetRootView(); |
| 1735 | *point = p; |
| 1736 | } |
| 1737 | } |
| 1738 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 1739 | if (src == NULL || src->Contains(dst)) { |
| 1740 | dst->ConvertPointFromAncestor(src, point); |
| 1741 | if (!src) { |
[email protected] | 8647714 | 2011-08-19 19:06:15 | [diff] [blame] | 1742 | if (dst_widget) { |
| 1743 | gfx::Rect b = dst_widget->GetClientAreaScreenBounds(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1744 | point->SetPoint(point->x() - b.x(), point->y() - b.y()); |
| 1745 | } |
| 1746 | } |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 1747 | } else if (src && try_other_direction) { |
| 1748 | if (!src->ConvertPointForAncestor(dst, point)) { |
| 1749 | // |src| is not an ancestor of |dst|, and |dst| is not an ancestor of |
| 1750 | // |src| either. At this stage, |point| is in the widget's coordinate |
[email protected] | c069fb03 | 2011-03-09 02:26:54 | [diff] [blame] | 1751 | // system. So convert from the widget's to |dst|'s coordinate system now. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 1752 | ConvertPointFromWidget(dst, point); |
| 1753 | } |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1757 | bool View::ConvertPointForAncestor(const View* ancestor, |
| 1758 | gfx::Point* point) const { |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 1759 | ui::Transform trans; |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1760 | // TODO(sad): Have some way of caching the transformation results. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 1761 | bool result = GetTransformRelativeTo(ancestor, &trans); |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 1762 | gfx::Point3f p(*point); |
| 1763 | trans.TransformPoint(p); |
| 1764 | *point = p.AsPoint(); |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 1765 | return result; |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | bool View::ConvertPointFromAncestor(const View* ancestor, |
| 1769 | gfx::Point* point) const { |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 1770 | ui::Transform trans; |
| 1771 | bool result = GetTransformRelativeTo(ancestor, &trans); |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 1772 | gfx::Point3f p(*point); |
| 1773 | trans.TransformPointReverse(p); |
| 1774 | *point = p.AsPoint(); |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 1775 | return result; |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1776 | } |
| 1777 | |
[email protected] | c36b0bf | 2011-05-10 18:26:25 | [diff] [blame] | 1778 | // Accelerated painting -------------------------------------------------------- |
| 1779 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1780 | void View::CreateLayer() { |
[email protected] | 25ae9a1 | 2011-10-12 14:55:22 | [diff] [blame] | 1781 | // A new layer is being created for the view. So all the layers of the |
| 1782 | // sub-tree can inherit the visibility of the corresponding view. |
| 1783 | for (int i = 0, count = child_count(); i < count; ++i) |
| 1784 | child_at(i)->UpdateChildLayerVisibility(true); |
| 1785 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1786 | layer_.reset(new ui::Layer(NULL)); |
| 1787 | layer_->set_delegate(this); |
| 1788 | if (layer_property_setter_.get()) |
| 1789 | layer_property_setter_->Installed(layer()); |
| 1790 | else |
| 1791 | SetLayerPropertySetter(NULL); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1792 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1793 | UpdateParentLayers(); |
[email protected] | 25ae9a1 | 2011-10-12 14:55:22 | [diff] [blame] | 1794 | UpdateLayerVisibility(); |
[email protected] | 18da0fd | 2011-10-07 16:25:31 | [diff] [blame] | 1795 | |
| 1796 | // The new layer needs to be ordered in the layer tree according |
| 1797 | // to the view tree. Children of this layer were added in order |
| 1798 | // in UpdateParentLayers(). |
| 1799 | if (parent()) |
| 1800 | parent()->ReorderLayers(); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1801 | } |
| 1802 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1803 | void View::UpdateParentLayers() { |
| 1804 | // Attach all top-level un-parented layers. |
| 1805 | if (layer() && !layer()->parent()) { |
| 1806 | UpdateParentLayer(); |
| 1807 | } else { |
| 1808 | for (int i = 0, count = child_count(); i < count; ++i) |
| 1809 | child_at(i)->UpdateParentLayers(); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | void View::UpdateParentLayer() { |
[email protected] | 40500ba | 2011-09-30 23:39:51 | [diff] [blame] | 1814 | if (!layer()) |
| 1815 | return; |
| 1816 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1817 | ui::Layer* parent_layer = NULL; |
| 1818 | gfx::Point offset(x(), y()); |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1819 | |
| 1820 | // TODO(sad): The NULL check here for parent_ essentially is to check if this |
| 1821 | // is the RootView. Instead of doing this, this function should be made |
| 1822 | // virtual and overridden from the RootView. |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1823 | if (parent_) |
| 1824 | parent_->CalculateOffsetToAncestorWithLayer(&offset, &parent_layer); |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1825 | else if (!parent_ && GetWidget()) |
| 1826 | GetWidget()->CalculateOffsetToAncestorWithLayer(&offset, &parent_layer); |
[email protected] | 7c4b1b9 | 2011-06-27 16:37:18 | [diff] [blame] | 1827 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1828 | ReparentLayer(offset, parent_layer); |
| 1829 | } |
[email protected] | 6fb3c27 | 2011-09-29 20:36:29 | [diff] [blame] | 1830 | |
[email protected] | 06061b49 | 2011-10-03 21:48:49 | [diff] [blame] | 1831 | void View::OrphanLayers() { |
| 1832 | if (layer()) { |
| 1833 | if (layer()->parent()) |
| 1834 | layer()->parent()->Remove(layer()); |
| 1835 | |
| 1836 | // The layer belonging to this View has already been orphaned. It is not |
| 1837 | // necessary to orphan the child layers. |
| 1838 | return; |
| 1839 | } |
| 1840 | for (int i = 0, count = child_count(); i < count; ++i) |
| 1841 | child_at(i)->OrphanLayers(); |
| 1842 | } |
| 1843 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1844 | void View::ReparentLayer(const gfx::Point& offset, ui::Layer* parent_layer) { |
| 1845 | layer_->SetBounds(gfx::Rect(offset.x(), offset.y(), width(), height())); |
| 1846 | DCHECK_NE(layer(), parent_layer); |
| 1847 | if (parent_layer) |
| 1848 | parent_layer->Add(layer()); |
| 1849 | layer_->SchedulePaint(GetLocalBounds()); |
| 1850 | MoveLayerToParent(layer(), gfx::Point()); |
[email protected] | 40500ba | 2011-09-30 23:39:51 | [diff] [blame] | 1851 | } |
[email protected] | 6fb3c27 | 2011-09-29 20:36:29 | [diff] [blame] | 1852 | |
[email protected] | 40500ba | 2011-09-30 23:39:51 | [diff] [blame] | 1853 | void View::DestroyLayer() { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1854 | ui::Layer* new_parent = layer()->parent(); |
| 1855 | std::vector<ui::Layer*> children = layer()->children(); |
| 1856 | for (size_t i = 0; i < children.size(); ++i) { |
| 1857 | layer()->Remove(children[i]); |
| 1858 | if (new_parent) |
| 1859 | new_parent->Add(children[i]); |
| 1860 | } |
[email protected] | 40500ba | 2011-09-30 23:39:51 | [diff] [blame] | 1861 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1862 | if (layer_property_setter_.get()) |
| 1863 | layer_property_setter_->Uninstalled(layer()); |
| 1864 | |
| 1865 | layer_.reset(); |
| 1866 | |
[email protected] | 18da0fd | 2011-10-07 16:25:31 | [diff] [blame] | 1867 | if (new_parent) |
| 1868 | ReorderLayers(); |
| 1869 | |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1870 | gfx::Point offset; |
| 1871 | CalculateOffsetToAncestorWithLayer(&offset, NULL); |
| 1872 | UpdateChildLayerBounds(offset); |
| 1873 | |
| 1874 | SchedulePaint(); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1875 | } |
| 1876 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1877 | // Input ----------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1878 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1879 | bool View::ProcessMousePressed(const MouseEvent& event, DragInfo* drag_info) { |
[email protected] | 83548a4 | 2010-06-18 13:53:37 | [diff] [blame] | 1880 | const bool enabled = IsEnabled(); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1881 | int drag_operations = |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1882 | (enabled && event.IsOnlyLeftMouseButton() && HitTest(event.location())) ? |
| 1883 | GetDragOperations(event.location()) : 0; |
| 1884 | ContextMenuController* context_menu_controller = event.IsRightMouseButton() ? |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1885 | context_menu_controller_ : 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1886 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1887 | const bool result = OnMousePressed(event); |
[email protected] | c069fb03 | 2011-03-09 02:26:54 | [diff] [blame] | 1888 | // WARNING: we may have been deleted, don't use any View variables. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1889 | |
| 1890 | if (!enabled) |
| 1891 | return result; |
| 1892 | |
[email protected] | 02bae0f | 2011-01-19 20:22:00 | [diff] [blame] | 1893 | if (drag_operations != ui::DragDropTypes::DRAG_NONE) { |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1894 | drag_info->PossibleDrag(event.location()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1895 | return true; |
| 1896 | } |
| 1897 | return !!context_menu_controller || result; |
| 1898 | } |
| 1899 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1900 | bool View::ProcessMouseDragged(const MouseEvent& event, DragInfo* drag_info) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1901 | // Copy the field, that way if we're deleted after drag and drop no harm is |
| 1902 | // done. |
| 1903 | ContextMenuController* context_menu_controller = context_menu_controller_; |
| 1904 | const bool possible_drag = drag_info->possible_drag; |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1905 | if (possible_drag && ExceededDragThreshold( |
| 1906 | drag_info->start_pt.x() - event.x(), |
| 1907 | drag_info->start_pt.y() - event.y())) { |
[email protected] | b5f94de | 2009-12-04 07:59:00 | [diff] [blame] | 1908 | if (!drag_controller_ || |
[email protected] | c3fac08 | 2011-03-04 01:04:18 | [diff] [blame] | 1909 | drag_controller_->CanStartDragForView( |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1910 | this, drag_info->start_pt, event.location())) |
| 1911 | DoDrag(event, drag_info->start_pt); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1912 | } else { |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1913 | if (OnMouseDragged(event)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1914 | return true; |
| 1915 | // Fall through to return value based on context menu controller. |
| 1916 | } |
| 1917 | // WARNING: we may have been deleted. |
| 1918 | return (context_menu_controller != NULL) || possible_drag; |
| 1919 | } |
| 1920 | |
[email protected] | 3433825 | 2011-03-29 00:39:05 | [diff] [blame] | 1921 | void View::ProcessMouseReleased(const MouseEvent& event) { |
| 1922 | if (context_menu_controller_ && event.IsOnlyRightMouseButton()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1923 | // Assume that if there is a context menu controller we won't be deleted |
| 1924 | // from mouse released. |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1925 | gfx::Point location(event.location()); |
[email protected] | 3433825 | 2011-03-29 00:39:05 | [diff] [blame] | 1926 | OnMouseReleased(event); |
[email protected] | 464fdb3 | 2009-03-19 20:25:44 | [diff] [blame] | 1927 | if (HitTest(location)) { |
| 1928 | ConvertPointToScreen(this, &location); |
[email protected] | e9adf070 | 2010-03-08 23:34:07 | [diff] [blame] | 1929 | ShowContextMenu(location, true); |
[email protected] | 464fdb3 | 2009-03-19 20:25:44 | [diff] [blame] | 1930 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1931 | } else { |
[email protected] | 3433825 | 2011-03-29 00:39:05 | [diff] [blame] | 1932 | OnMouseReleased(event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1933 | } |
| 1934 | // WARNING: we may have been deleted. |
| 1935 | } |
| 1936 | |
[email protected] | 6a4056b | 2011-06-14 22:09:17 | [diff] [blame] | 1937 | ui::TouchStatus View::ProcessTouchEvent(const TouchEvent& event) { |
[email protected] | 18dab37 | 2011-10-03 21:21:44 | [diff] [blame] | 1938 | // TODO(rjkroege): Implement a grab scheme similar to as as is found in |
| 1939 | // MousePressed. |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 1940 | return OnTouchEvent(event); |
[email protected] | ad84c8f | 2010-09-08 14:06:10 | [diff] [blame] | 1941 | } |
[email protected] | ad84c8f | 2010-09-08 14:06:10 | [diff] [blame] | 1942 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 1943 | // Accelerators ---------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1944 | |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1945 | void View::RegisterPendingAccelerators() { |
| 1946 | if (!accelerators_.get() || |
| 1947 | registered_accelerator_count_ == accelerators_->size()) { |
| 1948 | // No accelerators are waiting for registration. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1949 | return; |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1950 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1951 | |
[email protected] | 72f1724 | 2011-02-18 21:05:55 | [diff] [blame] | 1952 | if (!GetWidget()) { |
| 1953 | // The view is not yet attached to a widget, defer registration until then. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1954 | return; |
| 1955 | } |
[email protected] | f3735c5d | 2009-03-19 17:26:23 | [diff] [blame] | 1956 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1957 | accelerator_focus_manager_ = GetFocusManager(); |
| 1958 | if (!accelerator_focus_manager_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1959 | // Some crash reports seem to show that we may get cases where we have no |
| 1960 | // focus manager (see bug #1291225). This should never be the case, just |
| 1961 | // making sure we don't crash. |
[email protected] | f8dce00 | 2009-09-10 21:07:04 | [diff] [blame] | 1962 | |
[email protected] | 2fc6672 | 2011-05-19 14:43:12 | [diff] [blame] | 1963 | // TODO(jcampan): This fails for a view under NativeWidgetGtk with |
| 1964 | // TYPE_CHILD. (see http://crbug.com/21335) reenable |
| 1965 | // NOTREACHED assertion and verify accelerators works as |
| 1966 | // expected. |
[email protected] | f8dce00 | 2009-09-10 21:07:04 | [diff] [blame] | 1967 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1968 | NOTREACHED(); |
[email protected] | f8dce00 | 2009-09-10 21:07:04 | [diff] [blame] | 1969 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1970 | return; |
| 1971 | } |
[email protected] | 37e6e61 | 2011-02-02 18:13:07 | [diff] [blame] | 1972 | // Only register accelerators if we are visible. |
| 1973 | if (!IsVisibleInRootView()) |
| 1974 | return; |
[email protected] | afed457 | 2011-06-09 18:00:11 | [diff] [blame] | 1975 | for (std::vector<Accelerator>::const_iterator i( |
| 1976 | accelerators_->begin() + registered_accelerator_count_); |
| 1977 | i != accelerators_->end(); ++i) { |
| 1978 | accelerator_focus_manager_->RegisterAccelerator(*i, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1979 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1980 | registered_accelerator_count_ = accelerators_->size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1981 | } |
| 1982 | |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1983 | void View::UnregisterAccelerators(bool leave_data_intact) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1984 | if (!accelerators_.get()) |
| 1985 | return; |
| 1986 | |
[email protected] | 72f1724 | 2011-02-18 21:05:55 | [diff] [blame] | 1987 | if (GetWidget()) { |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1988 | if (accelerator_focus_manager_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1989 | // We may not have a FocusManager if the window containing us is being |
| 1990 | // closed, in which case the FocusManager is being deleted so there is |
| 1991 | // nothing to unregister. |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1992 | accelerator_focus_manager_->UnregisterAccelerators(this); |
| 1993 | accelerator_focus_manager_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1994 | } |
[email protected] | bda9556c | 2010-01-07 00:55:16 | [diff] [blame] | 1995 | if (!leave_data_intact) { |
| 1996 | accelerators_->clear(); |
| 1997 | accelerators_.reset(); |
| 1998 | } |
[email protected] | 71421c3f | 2009-06-06 00:41:44 | [diff] [blame] | 1999 | registered_accelerator_count_ = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2003 | // Focus ----------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2004 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2005 | void View::InitFocusSiblings(View* v, int index) { |
[email protected] | 39a0f0c6 | 2011-06-09 16:27:18 | [diff] [blame] | 2006 | int count = child_count(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2007 | |
[email protected] | 39a0f0c6 | 2011-06-09 16:27:18 | [diff] [blame] | 2008 | if (count == 0) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2009 | v->next_focusable_view_ = NULL; |
| 2010 | v->previous_focusable_view_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2011 | } else { |
[email protected] | 39a0f0c6 | 2011-06-09 16:27:18 | [diff] [blame] | 2012 | if (index == count) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2013 | // We are inserting at the end, but the end of the child list may not be |
| 2014 | // the last focusable element. Let's try to find an element with no next |
| 2015 | // focusable element to link to. |
| 2016 | View* last_focusable_view = NULL; |
[email protected] | 695e942e | 2011-06-06 15:15:51 | [diff] [blame] | 2017 | for (Views::iterator i(children_.begin()); i != children_.end(); ++i) { |
| 2018 | if (!(*i)->next_focusable_view_) { |
| 2019 | last_focusable_view = *i; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2020 | break; |
| 2021 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2022 | } |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2023 | if (last_focusable_view == NULL) { |
| 2024 | // Hum... there is a cycle in the focus list. Let's just insert ourself |
| 2025 | // after the last child. |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 2026 | View* prev = children_[index - 1]; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2027 | v->previous_focusable_view_ = prev; |
| 2028 | v->next_focusable_view_ = prev->next_focusable_view_; |
| 2029 | prev->next_focusable_view_->previous_focusable_view_ = v; |
| 2030 | prev->next_focusable_view_ = v; |
| 2031 | } else { |
| 2032 | last_focusable_view->next_focusable_view_ = v; |
| 2033 | v->next_focusable_view_ = NULL; |
| 2034 | v->previous_focusable_view_ = last_focusable_view; |
| 2035 | } |
| 2036 | } else { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 2037 | View* prev = children_[index]->GetPreviousFocusableView(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2038 | v->previous_focusable_view_ = prev; |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 2039 | v->next_focusable_view_ = children_[index]; |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2040 | if (prev) |
| 2041 | prev->next_focusable_view_ = v; |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 2042 | children_[index]->previous_focusable_view_ = v; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2047 | // System events --------------------------------------------------------------- |
[email protected] | 96b667d | 2008-10-14 20:58:44 | [diff] [blame] | 2048 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2049 | void View::PropagateThemeChanged() { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 2050 | for (int i = child_count() - 1; i >= 0; --i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 2051 | child_at(i)->PropagateThemeChanged(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2052 | OnThemeChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2053 | } |
| 2054 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2055 | void View::PropagateLocaleChanged() { |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 2056 | for (int i = child_count() - 1; i >= 0; --i) |
[email protected] | fe84b91 | 2011-07-15 17:13:33 | [diff] [blame] | 2057 | child_at(i)->PropagateLocaleChanged(); |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2058 | OnLocaleChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2059 | } |
| 2060 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2061 | // Tooltips -------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2062 | |
| 2063 | void View::UpdateTooltip() { |
[email protected] | a0dde12 | 2008-11-21 20:51:20 | [diff] [blame] | 2064 | Widget* widget = GetWidget(); |
[email protected] | 9861ae9 | 2011-03-03 20:15:03 | [diff] [blame] | 2065 | // TODO(beng): The TooltipManager NULL check can be removed when we |
| 2066 | // consolidate Init() methods and make views_unittests Init() all |
| 2067 | // Widgets that it uses. |
[email protected] | 2f2b57b | 2011-06-16 17:21:23 | [diff] [blame] | 2068 | if (widget && widget->native_widget_private()->GetTooltipManager()) |
| 2069 | widget->native_widget_private()->GetTooltipManager()->UpdateTooltip(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2070 | } |
| 2071 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2072 | // Drag and drop --------------------------------------------------------------- |
| 2073 | |
[email protected] | 2c831b2 | 2011-03-14 23:17:20 | [diff] [blame] | 2074 | void View::DoDrag(const MouseEvent& event, const gfx::Point& press_pt) { |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2075 | int drag_operations = GetDragOperations(press_pt); |
| 2076 | if (drag_operations == ui::DragDropTypes::DRAG_NONE) |
| 2077 | return; |
| 2078 | |
| 2079 | OSExchangeData data; |
| 2080 | WriteDragData(press_pt, &data); |
| 2081 | |
| 2082 | // Message the RootView to do the drag and drop. That way if we're removed |
| 2083 | // the RootView can detect it and avoid calling us back. |
[email protected] | 9861ae9 | 2011-03-03 20:15:03 | [diff] [blame] | 2084 | GetWidget()->RunShellDrag(this, data, drag_operations); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2085 | } |
| 2086 | |
[email protected] | 2df3cc9 | 2011-02-05 00:38:59 | [diff] [blame] | 2087 | } // namespace views |