OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "views/widget/root_view.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "ui/base/accessibility/accessible_view_state.h" |
| 12 #include "ui/base/dragdrop/drag_drop_types.h" |
| 13 #include "ui/base/keycodes/keyboard_codes.h" |
| 14 #include "ui/gfx/canvas_skia.h" |
| 15 #include "ui/gfx/compositor/layer.h" |
| 16 #include "ui/views/focus/view_storage.h" |
| 17 #include "ui/views/layout/fill_layout.h" |
| 18 #include "ui/views/touchui/gesture_manager.h" |
| 19 #include "views/widget/widget.h" |
| 20 |
| 21 namespace views { |
| 22 namespace internal { |
| 23 |
| 24 // static |
| 25 const char RootView::kViewClassName[] = "views/RootView"; |
| 26 |
| 27 //////////////////////////////////////////////////////////////////////////////// |
| 28 // RootView, public: |
| 29 |
| 30 // Creation and lifetime ------------------------------------------------------- |
| 31 |
| 32 RootView::RootView(Widget* widget) |
| 33 : widget_(widget), |
| 34 mouse_pressed_handler_(NULL), |
| 35 mouse_move_handler_(NULL), |
| 36 last_click_handler_(NULL), |
| 37 explicit_mouse_handler_(false), |
| 38 last_mouse_event_flags_(0), |
| 39 last_mouse_event_x_(-1), |
| 40 last_mouse_event_y_(-1), |
| 41 gesture_manager_(GestureManager::GetInstance()), |
| 42 touch_pressed_handler_(NULL), |
| 43 ALLOW_THIS_IN_INITIALIZER_LIST(focus_search_(this, false, false)), |
| 44 focus_traversable_parent_(NULL), |
| 45 focus_traversable_parent_view_(NULL) { |
| 46 } |
| 47 |
| 48 RootView::~RootView() { |
| 49 // If we have children remove them explicitly so to make sure a remove |
| 50 // notification is sent for each one of them. |
| 51 if (has_children()) |
| 52 RemoveAllChildViews(true); |
| 53 } |
| 54 |
| 55 // Tree operations ------------------------------------------------------------- |
| 56 |
| 57 void RootView::SetContentsView(View* contents_view) { |
| 58 DCHECK(contents_view && GetWidget()->native_widget()) << |
| 59 "Can't be called until after the native widget is created!"; |
| 60 // The ContentsView must be set up _after_ the window is created so that its |
| 61 // Widget pointer is valid. |
| 62 SetLayoutManager(new FillLayout); |
| 63 if (has_children()) |
| 64 RemoveAllChildViews(true); |
| 65 AddChildView(contents_view); |
| 66 |
| 67 // Force a layout now, since the attached hierarchy won't be ready for the |
| 68 // containing window's bounds. Note that we call Layout directly rather than |
| 69 // calling the widget's size changed handler, since the RootView's bounds may |
| 70 // not have changed, which will cause the Layout not to be done otherwise. |
| 71 Layout(); |
| 72 } |
| 73 |
| 74 View* RootView::GetContentsView() { |
| 75 return child_count() > 0 ? child_at(0) : NULL; |
| 76 } |
| 77 |
| 78 void RootView::NotifyNativeViewHierarchyChanged(bool attached, |
| 79 gfx::NativeView native_view) { |
| 80 PropagateNativeViewHierarchyChanged(attached, native_view, this); |
| 81 } |
| 82 |
| 83 // Input ----------------------------------------------------------------------- |
| 84 |
| 85 bool RootView::OnKeyEvent(const KeyEvent& event) { |
| 86 bool consumed = false; |
| 87 |
| 88 View* v = GetFocusManager()->GetFocusedView(); |
| 89 // Special case to handle right-click context menus triggered by the |
| 90 // keyboard. |
| 91 if (v && v->IsEnabled() && ((event.key_code() == ui::VKEY_APPS) || |
| 92 (event.key_code() == ui::VKEY_F10 && event.IsShiftDown()))) { |
| 93 v->ShowContextMenu(v->GetKeyboardContextMenuLocation(), false); |
| 94 return true; |
| 95 } |
| 96 for (; v && v != this && !consumed; v = v->parent()) { |
| 97 consumed = (event.type() == ui::ET_KEY_PRESSED) ? |
| 98 v->OnKeyPressed(event) : v->OnKeyReleased(event); |
| 99 } |
| 100 return consumed; |
| 101 } |
| 102 |
| 103 // Focus ----------------------------------------------------------------------- |
| 104 |
| 105 void RootView::SetFocusTraversableParent(FocusTraversable* focus_traversable) { |
| 106 DCHECK(focus_traversable != this); |
| 107 focus_traversable_parent_ = focus_traversable; |
| 108 } |
| 109 |
| 110 void RootView::SetFocusTraversableParentView(View* view) { |
| 111 focus_traversable_parent_view_ = view; |
| 112 } |
| 113 |
| 114 // System events --------------------------------------------------------------- |
| 115 |
| 116 void RootView::ThemeChanged() { |
| 117 View::PropagateThemeChanged(); |
| 118 } |
| 119 |
| 120 void RootView::LocaleChanged() { |
| 121 View::PropagateLocaleChanged(); |
| 122 } |
| 123 |
| 124 //////////////////////////////////////////////////////////////////////////////// |
| 125 // RootView, FocusTraversable implementation: |
| 126 |
| 127 FocusSearch* RootView::GetFocusSearch() { |
| 128 return &focus_search_; |
| 129 } |
| 130 |
| 131 FocusTraversable* RootView::GetFocusTraversableParent() { |
| 132 return focus_traversable_parent_; |
| 133 } |
| 134 |
| 135 View* RootView::GetFocusTraversableParentView() { |
| 136 return focus_traversable_parent_view_; |
| 137 } |
| 138 |
| 139 //////////////////////////////////////////////////////////////////////////////// |
| 140 // RootView, View overrides: |
| 141 |
| 142 const Widget* RootView::GetWidget() const { |
| 143 return widget_; |
| 144 } |
| 145 |
| 146 Widget* RootView::GetWidget() { |
| 147 return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget()); |
| 148 } |
| 149 |
| 150 bool RootView::IsVisibleInRootView() const { |
| 151 return IsVisible(); |
| 152 } |
| 153 |
| 154 std::string RootView::GetClassName() const { |
| 155 return kViewClassName; |
| 156 } |
| 157 |
| 158 void RootView::SchedulePaintInRect(const gfx::Rect& rect) { |
| 159 if (layer()) { |
| 160 layer()->SchedulePaint(rect); |
| 161 } else { |
| 162 gfx::Rect xrect = ConvertRectToParent(rect); |
| 163 gfx::Rect invalid_rect = GetLocalBounds().Intersect(xrect); |
| 164 if (!invalid_rect.IsEmpty()) |
| 165 widget_->SchedulePaintInRect(invalid_rect); |
| 166 } |
| 167 } |
| 168 |
| 169 bool RootView::OnMousePressed(const MouseEvent& event) { |
| 170 MouseEvent e(event, this); |
| 171 UpdateCursor(e); |
| 172 SetMouseLocationAndFlags(e); |
| 173 |
| 174 // If mouse_pressed_handler_ is non null, we are currently processing |
| 175 // a pressed -> drag -> released session. In that case we send the |
| 176 // event to mouse_pressed_handler_ |
| 177 if (mouse_pressed_handler_) { |
| 178 MouseEvent mouse_pressed_event(e, this, mouse_pressed_handler_); |
| 179 drag_info.Reset(); |
| 180 mouse_pressed_handler_->ProcessMousePressed(mouse_pressed_event, |
| 181 &drag_info); |
| 182 return true; |
| 183 } |
| 184 DCHECK(!explicit_mouse_handler_); |
| 185 |
| 186 bool hit_disabled_view = false; |
| 187 // Walk up the tree until we find a view that wants the mouse event. |
| 188 for (mouse_pressed_handler_ = GetEventHandlerForPoint(e.location()); |
| 189 mouse_pressed_handler_ && (mouse_pressed_handler_ != this); |
| 190 mouse_pressed_handler_ = mouse_pressed_handler_->parent()) { |
| 191 if (!mouse_pressed_handler_->IsEnabled()) { |
| 192 // Disabled views should eat events instead of propagating them upwards. |
| 193 hit_disabled_view = true; |
| 194 break; |
| 195 } |
| 196 |
| 197 // See if this view wants to handle the mouse press. |
| 198 MouseEvent mouse_pressed_event(e, this, mouse_pressed_handler_); |
| 199 |
| 200 // Remove the double-click flag if the handler is different than the |
| 201 // one which got the first click part of the double-click. |
| 202 if (mouse_pressed_handler_ != last_click_handler_) |
| 203 mouse_pressed_event.set_flags(e.flags() & ~ui::EF_IS_DOUBLE_CLICK); |
| 204 |
| 205 drag_info.Reset(); |
| 206 bool handled = mouse_pressed_handler_->ProcessMousePressed( |
| 207 mouse_pressed_event, &drag_info); |
| 208 |
| 209 // The view could have removed itself from the tree when handling |
| 210 // OnMousePressed(). In this case, the removal notification will have |
| 211 // reset mouse_pressed_handler_ to NULL out from under us. Detect this |
| 212 // case and stop. (See comments in view.h.) |
| 213 // |
| 214 // NOTE: Don't return true here, because we don't want the frame to |
| 215 // forward future events to us when there's no handler. |
| 216 if (!mouse_pressed_handler_) |
| 217 break; |
| 218 |
| 219 // If the view handled the event, leave mouse_pressed_handler_ set and |
| 220 // return true, which will cause subsequent drag/release events to get |
| 221 // forwarded to that view. |
| 222 if (handled) { |
| 223 last_click_handler_ = mouse_pressed_handler_; |
| 224 return true; |
| 225 } |
| 226 } |
| 227 |
| 228 // Reset mouse_pressed_handler_ to indicate that no processing is occurring. |
| 229 mouse_pressed_handler_ = NULL; |
| 230 |
| 231 // In the event that a double-click is not handled after traversing the |
| 232 // entire hierarchy (even as a single-click when sent to a different view), |
| 233 // it must be marked as handled to avoid anything happening from default |
| 234 // processing if it the first click-part was handled by us. |
| 235 if (last_click_handler_ && e.flags() & ui::EF_IS_DOUBLE_CLICK) |
| 236 hit_disabled_view = true; |
| 237 |
| 238 last_click_handler_ = NULL; |
| 239 return hit_disabled_view; |
| 240 } |
| 241 |
| 242 bool RootView::OnMouseDragged(const MouseEvent& event) { |
| 243 MouseEvent e(event, this); |
| 244 UpdateCursor(e); |
| 245 |
| 246 if (mouse_pressed_handler_) { |
| 247 SetMouseLocationAndFlags(e); |
| 248 |
| 249 MouseEvent mouse_event(e, this, mouse_pressed_handler_); |
| 250 return mouse_pressed_handler_->ProcessMouseDragged(mouse_event, &drag_info); |
| 251 } |
| 252 return false; |
| 253 } |
| 254 |
| 255 void RootView::OnMouseReleased(const MouseEvent& event) { |
| 256 MouseEvent e(event, this); |
| 257 UpdateCursor(e); |
| 258 |
| 259 if (mouse_pressed_handler_) { |
| 260 MouseEvent mouse_released(e, this, mouse_pressed_handler_); |
| 261 // We allow the view to delete us from ProcessMouseReleased. As such, |
| 262 // configure state such that we're done first, then call View. |
| 263 View* mouse_pressed_handler = mouse_pressed_handler_; |
| 264 SetMouseHandler(NULL); |
| 265 mouse_pressed_handler->ProcessMouseReleased(mouse_released); |
| 266 // WARNING: we may have been deleted. |
| 267 } |
| 268 } |
| 269 |
| 270 void RootView::OnMouseCaptureLost() { |
| 271 if (mouse_pressed_handler_) { |
| 272 // Synthesize a release event for UpdateCursor. |
| 273 MouseEvent release_event(ui::ET_MOUSE_RELEASED, last_mouse_event_x_, |
| 274 last_mouse_event_y_, last_mouse_event_flags_); |
| 275 UpdateCursor(release_event); |
| 276 // We allow the view to delete us from OnMouseCaptureLost. As such, |
| 277 // configure state such that we're done first, then call View. |
| 278 View* mouse_pressed_handler = mouse_pressed_handler_; |
| 279 SetMouseHandler(NULL); |
| 280 mouse_pressed_handler->OnMouseCaptureLost(); |
| 281 // WARNING: we may have been deleted. |
| 282 } |
| 283 } |
| 284 |
| 285 void RootView::OnMouseMoved(const MouseEvent& event) { |
| 286 MouseEvent e(event, this); |
| 287 View* v = GetEventHandlerForPoint(e.location()); |
| 288 // Find the first enabled view, or the existing move handler, whichever comes |
| 289 // first. The check for the existing handler is because if a view becomes |
| 290 // disabled while handling moves, it's wrong to suddenly send ET_MOUSE_EXITED |
| 291 // and ET_MOUSE_ENTERED events, because the mouse hasn't actually exited yet. |
| 292 while (v && !v->IsEnabled() && (v != mouse_move_handler_)) |
| 293 v = v->parent(); |
| 294 if (v && v != this) { |
| 295 if (v != mouse_move_handler_) { |
| 296 if (mouse_move_handler_ != NULL) |
| 297 mouse_move_handler_->OnMouseExited(e); |
| 298 mouse_move_handler_ = v; |
| 299 MouseEvent entered_event(e, this, mouse_move_handler_); |
| 300 mouse_move_handler_->OnMouseEntered(entered_event); |
| 301 } |
| 302 MouseEvent moved_event(e, this, mouse_move_handler_); |
| 303 mouse_move_handler_->OnMouseMoved(moved_event); |
| 304 if (!(moved_event.flags() & ui::EF_IS_NON_CLIENT)) |
| 305 widget_->SetCursor(mouse_move_handler_->GetCursor(moved_event)); |
| 306 } else if (mouse_move_handler_ != NULL) { |
| 307 mouse_move_handler_->OnMouseExited(e); |
| 308 widget_->SetCursor(gfx::kNullCursor); |
| 309 } |
| 310 } |
| 311 |
| 312 void RootView::OnMouseExited(const MouseEvent& event) { |
| 313 if (mouse_move_handler_ != NULL) { |
| 314 mouse_move_handler_->OnMouseExited(event); |
| 315 mouse_move_handler_ = NULL; |
| 316 } |
| 317 } |
| 318 |
| 319 bool RootView::OnMouseWheel(const MouseWheelEvent& event) { |
| 320 MouseWheelEvent e(event, this); |
| 321 bool consumed = false; |
| 322 for (View* v = GetFocusManager()->GetFocusedView(); |
| 323 v && v != this && !consumed; v = v->parent()) |
| 324 consumed = v->OnMouseWheel(e); |
| 325 return consumed; |
| 326 } |
| 327 |
| 328 ui::TouchStatus RootView::OnTouchEvent(const TouchEvent& event) { |
| 329 TouchEvent e(event, this); |
| 330 |
| 331 // If touch_pressed_handler_ is non null, we are currently processing |
| 332 // a touch down on the screen situation. In that case we send the |
| 333 // event to touch_pressed_handler_ |
| 334 ui::TouchStatus status = ui::TOUCH_STATUS_UNKNOWN; |
| 335 |
| 336 if (touch_pressed_handler_) { |
| 337 TouchEvent touch_event(e, this, touch_pressed_handler_); |
| 338 status = touch_pressed_handler_->ProcessTouchEvent(touch_event); |
| 339 if (gesture_manager_->ProcessTouchEventForGesture(e, this, status)) |
| 340 status = ui::TOUCH_STATUS_SYNTH_MOUSE; |
| 341 if (status == ui::TOUCH_STATUS_END) |
| 342 touch_pressed_handler_ = NULL; |
| 343 return status; |
| 344 } |
| 345 |
| 346 // Walk up the tree until we find a view that wants the touch event. |
| 347 for (touch_pressed_handler_ = GetEventHandlerForPoint(e.location()); |
| 348 touch_pressed_handler_ && (touch_pressed_handler_ != this); |
| 349 touch_pressed_handler_ = touch_pressed_handler_->parent()) { |
| 350 if (!touch_pressed_handler_->IsEnabled()) { |
| 351 // Disabled views eat events but are treated as not handled by the |
| 352 // the GestureManager. |
| 353 status = ui::TOUCH_STATUS_UNKNOWN; |
| 354 break; |
| 355 } |
| 356 |
| 357 // See if this view wants to handle the touch |
| 358 TouchEvent touch_event(e, this, touch_pressed_handler_); |
| 359 status = touch_pressed_handler_->ProcessTouchEvent(touch_event); |
| 360 |
| 361 // The view could have removed itself from the tree when handling |
| 362 // OnTouchEvent(). So handle as per OnMousePressed. NB: we |
| 363 // assume that the RootView itself cannot be so removed. |
| 364 if (!touch_pressed_handler_) |
| 365 break; |
| 366 |
| 367 // The touch event wasn't processed. Go up the view hierarchy and dispatch |
| 368 // the touch event. |
| 369 if (status == ui::TOUCH_STATUS_UNKNOWN) |
| 370 continue; |
| 371 |
| 372 // If the touch didn't initiate a touch-sequence, then reset the touch event |
| 373 // handler. Otherwise, leave it set so that subsequent touch events are |
| 374 // dispatched to the same handler. |
| 375 if (status != ui::TOUCH_STATUS_START) |
| 376 touch_pressed_handler_ = NULL; |
| 377 |
| 378 if (gesture_manager_->ProcessTouchEventForGesture(e, this, status)) |
| 379 status = ui::TOUCH_STATUS_SYNTH_MOUSE; |
| 380 return status; |
| 381 } |
| 382 |
| 383 // Reset touch_pressed_handler_ to indicate that no processing is occurring. |
| 384 touch_pressed_handler_ = NULL; |
| 385 |
| 386 // Give the touch event to the gesture manager. |
| 387 if (gesture_manager_->ProcessTouchEventForGesture(e, this, status)) |
| 388 status = ui::TOUCH_STATUS_SYNTH_MOUSE; |
| 389 return status; |
| 390 } |
| 391 |
| 392 void RootView::SetMouseHandler(View *new_mh) { |
| 393 // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well. |
| 394 explicit_mouse_handler_ = (new_mh != NULL); |
| 395 mouse_pressed_handler_ = new_mh; |
| 396 } |
| 397 |
| 398 void RootView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 399 state->role = ui::AccessibilityTypes::ROLE_APPLICATION; |
| 400 } |
| 401 |
| 402 //////////////////////////////////////////////////////////////////////////////// |
| 403 // RootView, protected: |
| 404 |
| 405 void RootView::ViewHierarchyChanged(bool is_add, View* parent, View* child) { |
| 406 widget_->ViewHierarchyChanged(is_add, parent, child); |
| 407 |
| 408 if (!is_add) { |
| 409 if (!explicit_mouse_handler_ && mouse_pressed_handler_ == child) |
| 410 mouse_pressed_handler_ = NULL; |
| 411 if (mouse_move_handler_ == child) |
| 412 mouse_move_handler_ = NULL; |
| 413 if (touch_pressed_handler_ == child) |
| 414 touch_pressed_handler_ = NULL; |
| 415 } |
| 416 } |
| 417 |
| 418 void RootView::OnPaint(gfx::Canvas* canvas) { |
| 419 if (!layer() || !layer()->fills_bounds_opaquely()) |
| 420 canvas->GetSkCanvas()->drawColor(SK_ColorBLACK, SkXfermode::kClear_Mode); |
| 421 |
| 422 // TODO (pkotwicz): Remove this once we switch over to Aura desktop. |
| 423 // This is needed so that we can set the background behind the RWHV when the |
| 424 // RWHV is not visible. Not needed once there is a view between the RootView |
| 425 // and RWHV. |
| 426 View::OnPaint(canvas); |
| 427 } |
| 428 |
| 429 void RootView::CalculateOffsetToAncestorWithLayer(gfx::Point* offset, |
| 430 ui::Layer** layer_parent) { |
| 431 View::CalculateOffsetToAncestorWithLayer(offset, layer_parent); |
| 432 if (!layer()) |
| 433 widget_->CalculateOffsetToAncestorWithLayer(offset, layer_parent); |
| 434 } |
| 435 |
| 436 //////////////////////////////////////////////////////////////////////////////// |
| 437 // RootView, private: |
| 438 |
| 439 // Input ----------------------------------------------------------------------- |
| 440 |
| 441 void RootView::UpdateCursor(const MouseEvent& event) { |
| 442 if (!(event.flags() & ui::EF_IS_NON_CLIENT)) { |
| 443 View* v = GetEventHandlerForPoint(event.location()); |
| 444 widget_->SetCursor(v->GetCursor(MouseEvent(event, this, v))); |
| 445 } |
| 446 } |
| 447 |
| 448 void RootView::SetMouseLocationAndFlags(const MouseEvent& event) { |
| 449 last_mouse_event_flags_ = event.flags(); |
| 450 last_mouse_event_x_ = event.x(); |
| 451 last_mouse_event_y_ = event.y(); |
| 452 } |
| 453 |
| 454 } // namespace internal |
| 455 } // namespace views |
OLD | NEW |