| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/touchui/touch_selection_controller_impl.h" | 5 #include "ui/views/touchui/touch_selection_controller_impl.h" |
| 6 | 6 |
| 7 #include "base/time.h" | 7 #include "base/time.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "grit/ui_strings.h" | 9 #include "grit/ui_strings.h" |
| 10 #include "third_party/skia/include/effects/SkGradientShader.h" | 10 #include "third_party/skia/include/effects/SkGradientShader.h" |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 Layout(); | 316 Layout(); |
| 317 } | 317 } |
| 318 | 318 |
| 319 scoped_ptr<Widget> widget_; | 319 scoped_ptr<Widget> widget_; |
| 320 TouchSelectionControllerImpl* controller_; | 320 TouchSelectionControllerImpl* controller_; |
| 321 | 321 |
| 322 DISALLOW_COPY_AND_ASSIGN(TouchContextMenuView); | 322 DISALLOW_COPY_AND_ASSIGN(TouchContextMenuView); |
| 323 }; | 323 }; |
| 324 | 324 |
| 325 TouchSelectionControllerImpl::TouchSelectionControllerImpl( | 325 TouchSelectionControllerImpl::TouchSelectionControllerImpl( |
| 326 TouchSelectionClientView* client_view) | 326 ui::TouchSelectionClientView* client_view) |
| 327 : client_view_(client_view), | 327 : client_view_(client_view), |
| 328 selection_handle_1_(new SelectionHandleView(this)), | 328 selection_handle_1_(new SelectionHandleView(this)), |
| 329 selection_handle_2_(new SelectionHandleView(this)), | 329 selection_handle_2_(new SelectionHandleView(this)), |
| 330 context_menu_(new TouchContextMenuView(this)), | 330 context_menu_(new TouchContextMenuView(this)), |
| 331 dragging_handle_(NULL) { | 331 dragging_handle_(NULL) { |
| 332 } | 332 } |
| 333 | 333 |
| 334 TouchSelectionControllerImpl::~TouchSelectionControllerImpl() { | 334 TouchSelectionControllerImpl::~TouchSelectionControllerImpl() { |
| 335 } | 335 } |
| 336 | 336 |
| 337 void TouchSelectionControllerImpl::SelectionChanged(const gfx::Point& p1, | 337 void TouchSelectionControllerImpl::SelectionChanged(const gfx::Point& p1, |
| 338 const gfx::Point& p2) { | 338 const gfx::Point& p2) { |
| 339 gfx::Point screen_pos_1(p1); | 339 gfx::Point screen_pos_1(p1); |
| 340 View::ConvertPointToScreen(client_view_, &screen_pos_1); | 340 client_view_->ConvertPointToScreen(&screen_pos_1); |
| 341 gfx::Point screen_pos_2(p2); | 341 gfx::Point screen_pos_2(p2); |
| 342 View::ConvertPointToScreen(client_view_, &screen_pos_2); | 342 client_view_->ConvertPointToScreen(&screen_pos_2); |
| 343 | 343 |
| 344 if (dragging_handle_) { | 344 if (dragging_handle_) { |
| 345 // We need to reposition only the selection handle that is being dragged. | 345 // We need to reposition only the selection handle that is being dragged. |
| 346 // The other handle stays the same. Also, the selection handle being dragged | 346 // The other handle stays the same. Also, the selection handle being dragged |
| 347 // will always be at the end of selection, while the other handle will be at | 347 // will always be at the end of selection, while the other handle will be at |
| 348 // the start. | 348 // the start. |
| 349 dragging_handle_->SetScreenPosition(screen_pos_2); | 349 dragging_handle_->SetScreenPosition(screen_pos_2); |
| 350 } else { | 350 } else { |
| 351 UpdateContextMenu(p1, p2); | 351 UpdateContextMenu(p1, p2); |
| 352 | 352 |
| 353 // Check if there is any selection at all. | 353 // Check if there is any selection at all. |
| 354 if (IsEmptySelection(screen_pos_2, screen_pos_1)) { | 354 if (IsEmptySelection(screen_pos_2, screen_pos_1)) { |
| 355 selection_handle_1_->SetVisible(false); | 355 selection_handle_1_->SetVisible(false); |
| 356 selection_handle_2_->SetVisible(false); | 356 selection_handle_2_->SetVisible(false); |
| 357 return; | 357 return; |
| 358 } | 358 } |
| 359 | 359 |
| 360 if (client_view_->bounds().Contains(p1)) { | 360 if (client_view_->GetBounds().Contains(p1)) { |
| 361 selection_handle_1_->SetScreenPosition(screen_pos_1); | 361 selection_handle_1_->SetScreenPosition(screen_pos_1); |
| 362 selection_handle_1_->SetVisible(true); | 362 selection_handle_1_->SetVisible(true); |
| 363 } else { | 363 } else { |
| 364 selection_handle_1_->SetVisible(false); | 364 selection_handle_1_->SetVisible(false); |
| 365 } | 365 } |
| 366 | 366 |
| 367 if (client_view_->bounds().Contains(p2)) { | 367 if (client_view_->GetBounds().Contains(p2)) { |
| 368 selection_handle_2_->SetScreenPosition(screen_pos_2); | 368 selection_handle_2_->SetScreenPosition(screen_pos_2); |
| 369 selection_handle_2_->SetVisible(true); | 369 selection_handle_2_->SetVisible(true); |
| 370 } else { | 370 } else { |
| 371 selection_handle_2_->SetVisible(false); | 371 selection_handle_2_->SetVisible(false); |
| 372 } | 372 } |
| 373 } | 373 } |
| 374 } | 374 } |
| 375 | 375 |
| 376 void TouchSelectionControllerImpl::ClientViewLostFocus() { | 376 void TouchSelectionControllerImpl::ClientViewLostFocus() { |
| 377 selection_handle_1_->SetVisible(false); | 377 selection_handle_1_->SetVisible(false); |
| 378 selection_handle_2_->SetVisible(false); | 378 selection_handle_2_->SetVisible(false); |
| 379 HideContextMenu(); | 379 HideContextMenu(); |
| 380 } | 380 } |
| 381 | 381 |
| 382 void TouchSelectionControllerImpl::SelectionHandleDragged( | 382 void TouchSelectionControllerImpl::SelectionHandleDragged( |
| 383 const gfx::Point& drag_pos) { | 383 const gfx::Point& drag_pos) { |
| 384 // We do not want to show the context menu while dragging. | 384 // We do not want to show the context menu while dragging. |
| 385 HideContextMenu(); | 385 HideContextMenu(); |
| 386 context_menu_timer_.Start( | 386 context_menu_timer_.Start( |
| 387 FROM_HERE, | 387 FROM_HERE, |
| 388 base::TimeDelta::FromMilliseconds(kContextMenuTimoutMs), | 388 base::TimeDelta::FromMilliseconds(kContextMenuTimoutMs), |
| 389 this, | 389 this, |
| 390 &TouchSelectionControllerImpl::ContextMenuTimerFired); | 390 &TouchSelectionControllerImpl::ContextMenuTimerFired); |
| 391 | 391 |
| 392 if (client_view_->GetWidget()) { | 392 DCHECK(dragging_handle_); |
| 393 DCHECK(dragging_handle_); | 393 // Find the stationary selection handle. |
| 394 // Find the stationary selection handle. | 394 SelectionHandleView* fixed_handle = selection_handle_1_.get(); |
| 395 SelectionHandleView* fixed_handle = selection_handle_1_.get(); | 395 if (fixed_handle == dragging_handle_) |
| 396 if (fixed_handle == dragging_handle_) | 396 fixed_handle = selection_handle_2_.get(); |
| 397 fixed_handle = selection_handle_2_.get(); | |
| 398 | 397 |
| 399 // Find selection end points in client_view's coordinate system. | 398 // Find selection end points in client_view's coordinate system. |
| 400 gfx::Point p1(drag_pos.x() + kSelectionHandleRadius, drag_pos.y()); | 399 gfx::Point p1(drag_pos.x() + kSelectionHandleRadius, drag_pos.y()); |
| 401 ConvertPointToClientView(dragging_handle_, &p1); | 400 ConvertPointToClientView(dragging_handle_, &p1); |
| 402 | 401 |
| 403 gfx::Point p2(kSelectionHandleRadius, 0); | 402 gfx::Point p2(kSelectionHandleRadius, 0); |
| 404 ConvertPointToClientView(fixed_handle, &p2); | 403 ConvertPointToClientView(fixed_handle, &p2); |
| 405 | 404 |
| 406 // Instruct client_view to select the region between p1 and p2. The position | 405 // Instruct client_view to select the region between p1 and p2. The position |
| 407 // of |fixed_handle| is the start and that of |dragging_handle| is the end | 406 // of |fixed_handle| is the start and that of |dragging_handle| is the end |
| 408 // of selection. | 407 // of selection. |
| 409 client_view_->SelectRect(p2, p1); | 408 client_view_->SelectRect(p2, p1); |
| 410 } | |
| 411 } | 409 } |
| 412 | 410 |
| 413 void TouchSelectionControllerImpl::ConvertPointToClientView( | 411 void TouchSelectionControllerImpl::ConvertPointToClientView( |
| 414 SelectionHandleView* source, gfx::Point* point) { | 412 SelectionHandleView* source, gfx::Point* point) { |
| 415 View::ConvertPointToScreen(source, point); | 413 View::ConvertPointToScreen(source, point); |
| 416 gfx::Rect r = client_view_->GetWidget()->GetClientAreaBoundsInScreen(); | 414 client_view_->ConvertPointFromScreen(point); |
| 417 point->SetPoint(point->x() - r.x(), point->y() - r.y()); | |
| 418 View::ConvertPointFromWidget(client_view_, point); | |
| 419 } | 415 } |
| 420 | 416 |
| 421 bool TouchSelectionControllerImpl::IsCommandIdEnabled(int command_id) const { | 417 bool TouchSelectionControllerImpl::IsCommandIdEnabled(int command_id) const { |
| 422 return client_view_->IsCommandIdEnabled(command_id); | 418 return client_view_->IsCommandIdEnabled(command_id); |
| 423 } | 419 } |
| 424 | 420 |
| 425 void TouchSelectionControllerImpl::ExecuteCommand(int command_id) { | 421 void TouchSelectionControllerImpl::ExecuteCommand(int command_id) { |
| 426 HideContextMenu(); | 422 HideContextMenu(); |
| 427 client_view_->ExecuteCommand(command_id); | 423 client_view_->ExecuteCommand(command_id); |
| 428 } | 424 } |
| 429 | 425 |
| 430 void TouchSelectionControllerImpl::ContextMenuTimerFired() { | 426 void TouchSelectionControllerImpl::ContextMenuTimerFired() { |
| 431 // Get selection end points in client_view's space. | 427 // Get selection end points in client_view's space. |
| 432 gfx::Point p1(kSelectionHandleRadius, 0); | 428 gfx::Point p1(kSelectionHandleRadius, 0); |
| 433 ConvertPointToClientView(selection_handle_1_.get(), &p1); | 429 ConvertPointToClientView(selection_handle_1_.get(), &p1); |
| 434 gfx::Point p2(kSelectionHandleRadius, 0); | 430 gfx::Point p2(kSelectionHandleRadius, 0); |
| 435 ConvertPointToClientView(selection_handle_2_.get(), &p2); | 431 ConvertPointToClientView(selection_handle_2_.get(), &p2); |
| 436 | 432 |
| 437 // if selection is completely inside the view, we display the context menu | 433 // if selection is completely inside the view, we display the context menu |
| 438 // in the middle of the end points on the top. Else, we show the menu on the | 434 // in the middle of the end points on the top. Else, we show the menu on the |
| 439 // top border of the view in the center. | 435 // top border of the view in the center. |
| 440 gfx::Point menu_pos; | 436 gfx::Point menu_pos; |
| 441 if (client_view_->bounds().Contains(p1) && | 437 gfx::Rect client_bounds = client_view_->GetBounds(); |
| 442 client_view_->bounds().Contains(p2)) { | 438 if (client_bounds.Contains(p1) && client_bounds.Contains(p2)) { |
| 443 menu_pos.set_x((p1.x() + p2.x()) / 2); | 439 menu_pos.set_x((p1.x() + p2.x()) / 2); |
| 444 menu_pos.set_y(std::min(p1.y(), p2.y()) - kContextMenuVerticalOffset); | 440 menu_pos.set_y(std::min(p1.y(), p2.y()) - kContextMenuVerticalOffset); |
| 445 } else { | 441 } else { |
| 446 menu_pos.set_x(client_view_->x() + client_view_->width() / 2); | 442 menu_pos.set_x(client_bounds.x() + client_bounds.width() / 2); |
| 447 menu_pos.set_y(client_view_->y()); | 443 menu_pos.set_y(client_bounds.y()); |
| 448 } | 444 } |
| 449 | 445 |
| 450 View::ConvertPointToScreen(client_view_, &menu_pos); | 446 client_view_->ConvertPointToScreen(&menu_pos); |
| 451 | 447 |
| 452 context_menu_->SetScreenPosition(menu_pos); | 448 context_menu_->SetScreenPosition(menu_pos); |
| 453 context_menu_->SetVisible(true); | 449 context_menu_->SetVisible(true); |
| 454 } | 450 } |
| 455 | 451 |
| 456 void TouchSelectionControllerImpl::UpdateContextMenu(const gfx::Point& p1, | 452 void TouchSelectionControllerImpl::UpdateContextMenu(const gfx::Point& p1, |
| 457 const gfx::Point& p2) { | 453 const gfx::Point& p2) { |
| 458 // Hide context menu to be shown when the timer fires. | 454 // Hide context menu to be shown when the timer fires. |
| 459 HideContextMenu(); | 455 HideContextMenu(); |
| 460 | 456 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 482 } | 478 } |
| 483 | 479 |
| 484 bool TouchSelectionControllerImpl::IsSelectionHandle1Visible() { | 480 bool TouchSelectionControllerImpl::IsSelectionHandle1Visible() { |
| 485 return selection_handle_1_->visible(); | 481 return selection_handle_1_->visible(); |
| 486 } | 482 } |
| 487 | 483 |
| 488 bool TouchSelectionControllerImpl::IsSelectionHandle2Visible() { | 484 bool TouchSelectionControllerImpl::IsSelectionHandle2Visible() { |
| 489 return selection_handle_2_->visible(); | 485 return selection_handle_2_->visible(); |
| 490 } | 486 } |
| 491 | 487 |
| 488 } // namespace views |
| 489 |
| 490 namespace ui { |
| 491 |
| 492 TouchSelectionController* TouchSelectionController::create( | 492 TouchSelectionController* TouchSelectionController::create( |
| 493 TouchSelectionClientView* client_view) { | 493 TouchSelectionClientView* client_view) { |
| 494 return new TouchSelectionControllerImpl(client_view); | 494 return new views::TouchSelectionControllerImpl(client_view); |
| 495 } | 495 } |
| 496 | 496 |
| 497 } // namespace views | 497 } // namespace ui |
| OLD | NEW |