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