| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/views/omnibox/omnibox_view_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 !event.IsControlDown()) { | 230 !event.IsControlDown()) { |
| 231 if (model_->is_keyword_hint()) { | 231 if (model_->is_keyword_hint()) { |
| 232 handled = model_->AcceptKeyword(); | 232 handled = model_->AcceptKeyword(); |
| 233 } else { | 233 } else { |
| 234 string16::size_type start = 0; | 234 string16::size_type start = 0; |
| 235 string16::size_type end = 0; | 235 string16::size_type end = 0; |
| 236 size_t length = GetTextLength(); | 236 size_t length = GetTextLength(); |
| 237 GetSelectionBounds(&start, &end); | 237 GetSelectionBounds(&start, &end); |
| 238 if (start != end || start < length) { | 238 if (start != end || start < length) { |
| 239 OnBeforePossibleChange(); | 239 OnBeforePossibleChange(); |
| 240 textfield_->SelectSelectionModel(gfx::SelectionModel(length, length)); | 240 textfield_->SelectRange(ui::Range(length, length)); |
| 241 OnAfterPossibleChange(); | 241 OnAfterPossibleChange(); |
| 242 handled = true; | 242 handled = true; |
| 243 } | 243 } |
| 244 | 244 |
| 245 // TODO(Oshima): handle instant | 245 // TODO(Oshima): handle instant |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 // TODO(oshima): page up & down | 248 // TODO(oshima): page up & down |
| 249 | 249 |
| 250 return handled; | 250 return handled; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 | 397 |
| 398 void OmniboxViewViews::SetWindowTextAndCaretPos(const string16& text, | 398 void OmniboxViewViews::SetWindowTextAndCaretPos(const string16& text, |
| 399 size_t caret_pos) { | 399 size_t caret_pos) { |
| 400 const ui::Range range(caret_pos, caret_pos); | 400 const ui::Range range(caret_pos, caret_pos); |
| 401 SetTextAndSelectedRange(text, range); | 401 SetTextAndSelectedRange(text, range); |
| 402 } | 402 } |
| 403 | 403 |
| 404 void OmniboxViewViews::SetForcedQuery() { | 404 void OmniboxViewViews::SetForcedQuery() { |
| 405 const string16 current_text(GetText()); | 405 const string16 current_text(GetText()); |
| 406 const size_t start = current_text.find_first_not_of(kWhitespaceUTF16); | 406 const size_t start = current_text.find_first_not_of(kWhitespaceUTF16); |
| 407 if (start == string16::npos || (current_text[start] != '?')) { | 407 if (start == string16::npos || (current_text[start] != '?')) |
| 408 SetUserText(ASCIIToUTF16("?")); | 408 SetUserText(ASCIIToUTF16("?")); |
| 409 } else { | 409 else |
| 410 textfield_->SelectSelectionModel(gfx::SelectionModel(current_text.size(), | 410 textfield_->SelectRange(ui::Range(current_text.size(), start + 1)); |
| 411 start + 1)); | |
| 412 } | |
| 413 } | 411 } |
| 414 | 412 |
| 415 bool OmniboxViewViews::IsSelectAll() { | 413 bool OmniboxViewViews::IsSelectAll() { |
| 416 // TODO(oshima): IME support. | 414 // TODO(oshima): IME support. |
| 417 return textfield_->text() == textfield_->GetSelectedText(); | 415 return textfield_->text() == textfield_->GetSelectedText(); |
| 418 } | 416 } |
| 419 | 417 |
| 420 bool OmniboxViewViews::DeleteAtEndPressed() { | 418 bool OmniboxViewViews::DeleteAtEndPressed() { |
| 421 return delete_at_end_pressed_; | 419 return delete_at_end_pressed_; |
| 422 } | 420 } |
| 423 | 421 |
| 424 void OmniboxViewViews::GetSelectionBounds(string16::size_type* start, | 422 void OmniboxViewViews::GetSelectionBounds(string16::size_type* start, |
| 425 string16::size_type* end) { | 423 string16::size_type* end) { |
| 426 gfx::SelectionModel sel; | 424 ui::Range range; |
| 427 textfield_->GetSelectionModel(&sel); | 425 textfield_->GetSelectedRange(&range); |
| 428 *start = static_cast<size_t>(sel.selection_end()); | 426 *start = static_cast<size_t>(range.end()); |
| 429 *end = static_cast<size_t>(sel.selection_start()); | 427 *end = static_cast<size_t>(range.start()); |
| 430 } | 428 } |
| 431 | 429 |
| 432 void OmniboxViewViews::SelectAll(bool reversed) { | 430 void OmniboxViewViews::SelectAll(bool reversed) { |
| 433 if (reversed) | 431 if (reversed) |
| 434 textfield_->SelectSelectionModel(gfx::SelectionModel(GetTextLength(), 0)); | 432 textfield_->SelectRange(ui::Range(GetTextLength(), 0)); |
| 435 else | 433 else |
| 436 textfield_->SelectSelectionModel(gfx::SelectionModel(0, GetTextLength())); | 434 textfield_->SelectRange(ui::Range(0, GetTextLength())); |
| 437 } | 435 } |
| 438 | 436 |
| 439 void OmniboxViewViews::RevertAll() { | 437 void OmniboxViewViews::RevertAll() { |
| 440 ClosePopup(); | 438 ClosePopup(); |
| 441 model_->Revert(); | 439 model_->Revert(); |
| 442 TextChanged(); | 440 TextChanged(); |
| 443 } | 441 } |
| 444 | 442 |
| 445 void OmniboxViewViews::UpdatePopup() { | 443 void OmniboxViewViews::UpdatePopup() { |
| 446 model_->SetInputInProgress(true); | 444 model_->SetInputInProgress(true); |
| 447 if (!model_->has_focus()) | 445 if (!model_->has_focus()) |
| 448 return; | 446 return; |
| 449 | 447 |
| 450 // Don't inline autocomplete when the caret/selection isn't at the end of | 448 // Don't inline autocomplete when the caret/selection isn't at the end of |
| 451 // the text, or in the middle of composition. | 449 // the text, or in the middle of composition. |
| 452 gfx::SelectionModel sel; | 450 ui::Range sel; |
| 453 textfield_->GetSelectionModel(&sel); | 451 textfield_->GetSelectedRange(&sel); |
| 454 size_t max_of_selection = std::max(sel.selection_start(), | |
| 455 sel.selection_end()); | |
| 456 bool no_inline_autocomplete = | 452 bool no_inline_autocomplete = |
| 457 max_of_selection < GetTextLength() || textfield_->IsIMEComposing(); | 453 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); |
| 458 | 454 |
| 459 bool is_sel_empty = (sel.selection_start() == sel.selection_end()); | 455 model_->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); |
| 460 model_->StartAutocomplete(!is_sel_empty, no_inline_autocomplete); | |
| 461 } | 456 } |
| 462 | 457 |
| 463 void OmniboxViewViews::ClosePopup() { | 458 void OmniboxViewViews::ClosePopup() { |
| 464 model_->StopAutocomplete(); | 459 model_->StopAutocomplete(); |
| 465 } | 460 } |
| 466 | 461 |
| 467 void OmniboxViewViews::SetFocus() { | 462 void OmniboxViewViews::SetFocus() { |
| 468 // In views-implementation, the focus is on textfield rather than OmniboxView. | 463 // In views-implementation, the focus is on textfield rather than OmniboxView. |
| 469 textfield_->RequestFocus(); | 464 textfield_->RequestFocus(); |
| 470 } | 465 } |
| 471 | 466 |
| 472 void OmniboxViewViews::OnTemporaryTextMaybeChanged( | 467 void OmniboxViewViews::OnTemporaryTextMaybeChanged( |
| 473 const string16& display_text, | 468 const string16& display_text, |
| 474 bool save_original_selection) { | 469 bool save_original_selection) { |
| 475 if (save_original_selection) { | 470 if (save_original_selection) |
| 476 gfx::SelectionModel sel; | 471 textfield_->GetSelectedRange(&saved_temporary_selection_); |
| 477 textfield_->GetSelectionModel(&sel); | |
| 478 saved_temporary_selection_.set_start(sel.selection_start()); | |
| 479 saved_temporary_selection_.set_end(sel.selection_end()); | |
| 480 } | |
| 481 | 472 |
| 482 SetWindowTextAndCaretPos(display_text, display_text.length()); | 473 SetWindowTextAndCaretPos(display_text, display_text.length()); |
| 483 TextChanged(); | 474 TextChanged(); |
| 484 } | 475 } |
| 485 | 476 |
| 486 bool OmniboxViewViews::OnInlineAutocompleteTextMaybeChanged( | 477 bool OmniboxViewViews::OnInlineAutocompleteTextMaybeChanged( |
| 487 const string16& display_text, | 478 const string16& display_text, |
| 488 size_t user_text_length) { | 479 size_t user_text_length) { |
| 489 if (display_text == GetText()) | 480 if (display_text == GetText()) |
| 490 return false; | 481 return false; |
| 491 ui::Range range(display_text.size(), user_text_length); | 482 ui::Range range(display_text.size(), user_text_length); |
| 492 SetTextAndSelectedRange(display_text, range); | 483 SetTextAndSelectedRange(display_text, range); |
| 493 TextChanged(); | 484 TextChanged(); |
| 494 return true; | 485 return true; |
| 495 } | 486 } |
| 496 | 487 |
| 497 void OmniboxViewViews::OnRevertTemporaryText() { | 488 void OmniboxViewViews::OnRevertTemporaryText() { |
| 498 gfx::SelectionModel sel(saved_temporary_selection_.start(), | 489 textfield_->SelectRange(saved_temporary_selection_); |
| 499 saved_temporary_selection_.end()); | |
| 500 textfield_->SelectSelectionModel(sel); | |
| 501 TextChanged(); | 490 TextChanged(); |
| 502 } | 491 } |
| 503 | 492 |
| 504 void OmniboxViewViews::OnBeforePossibleChange() { | 493 void OmniboxViewViews::OnBeforePossibleChange() { |
| 505 // Record our state. | 494 // Record our state. |
| 506 text_before_change_ = GetText(); | 495 text_before_change_ = GetText(); |
| 507 gfx::SelectionModel sel; | 496 textfield_->GetSelectedRange(&sel_before_change_); |
| 508 textfield_->GetSelectionModel(&sel); | |
| 509 sel_before_change_.set_start(sel.selection_start()); | |
| 510 sel_before_change_.set_end(sel.selection_end()); | |
| 511 ime_composing_before_change_ = textfield_->IsIMEComposing(); | 497 ime_composing_before_change_ = textfield_->IsIMEComposing(); |
| 512 } | 498 } |
| 513 | 499 |
| 514 bool OmniboxViewViews::OnAfterPossibleChange() { | 500 bool OmniboxViewViews::OnAfterPossibleChange() { |
| 515 gfx::SelectionModel sel; | |
| 516 textfield_->GetSelectionModel(&sel); | |
| 517 ui::Range new_sel; | 501 ui::Range new_sel; |
| 518 new_sel.set_start(sel.selection_start()); | 502 textfield_->GetSelectedRange(&new_sel); |
| 519 new_sel.set_end(sel.selection_end()); | |
| 520 | 503 |
| 521 // See if the text or selection have changed since OnBeforePossibleChange(). | 504 // See if the text or selection have changed since OnBeforePossibleChange(). |
| 522 const string16 new_text = GetText(); | 505 const string16 new_text = GetText(); |
| 523 const bool text_changed = (new_text != text_before_change_) || | 506 const bool text_changed = (new_text != text_before_change_) || |
| 524 (ime_composing_before_change_ != textfield_->IsIMEComposing()); | 507 (ime_composing_before_change_ != textfield_->IsIMEComposing()); |
| 525 const bool selection_differs = | 508 const bool selection_differs = |
| 526 !((sel_before_change_.is_empty() && new_sel.is_empty()) || | 509 !((sel_before_change_.is_empty() && new_sel.is_empty()) || |
| 527 sel_before_change_.EqualsIgnoringDirection(new_sel)); | 510 sel_before_change_.EqualsIgnoringDirection(new_sel)); |
| 528 | 511 |
| 529 // When the user has deleted text, we don't allow inline autocomplete. Make | 512 // When the user has deleted text, we don't allow inline autocomplete. Make |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 | 673 |
| 691 void OmniboxViewViews::TextChanged() { | 674 void OmniboxViewViews::TextChanged() { |
| 692 EmphasizeURLComponents(); | 675 EmphasizeURLComponents(); |
| 693 model_->OnChanged(); | 676 model_->OnChanged(); |
| 694 } | 677 } |
| 695 | 678 |
| 696 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, | 679 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, |
| 697 const ui::Range& range) { | 680 const ui::Range& range) { |
| 698 if (text != GetText()) | 681 if (text != GetText()) |
| 699 textfield_->SetText(text); | 682 textfield_->SetText(text); |
| 700 textfield_->SelectSelectionModel(gfx::SelectionModel( | 683 textfield_->SelectRange(range); |
| 701 range.start(), range.end())); | |
| 702 } | 684 } |
| 703 | 685 |
| 704 string16 OmniboxViewViews::GetSelectedText() const { | 686 string16 OmniboxViewViews::GetSelectedText() const { |
| 705 // TODO(oshima): Support instant, IME. | 687 // TODO(oshima): Support instant, IME. |
| 706 return textfield_->GetSelectedText(); | 688 return textfield_->GetSelectedText(); |
| 707 } | 689 } |
| 708 | 690 |
| 709 | |
| 710 AutocompletePopupView* OmniboxViewViews::CreatePopupView( | 691 AutocompletePopupView* OmniboxViewViews::CreatePopupView( |
| 711 View* location_bar) { | 692 View* location_bar) { |
| 712 #if defined(TOUCH_UI) | 693 #if defined(TOUCH_UI) |
| 713 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; | 694 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; |
| 714 #else | 695 #else |
| 715 typedef AutocompletePopupContentsView AutocompleteContentsView; | 696 typedef AutocompletePopupContentsView AutocompleteContentsView; |
| 716 #endif | 697 #endif |
| 717 return new AutocompleteContentsView(gfx::Font(), this, model_.get(), | 698 return new AutocompleteContentsView(gfx::Font(), this, model_.get(), |
| 718 location_bar); | 699 location_bar); |
| 719 } | 700 } |
| OLD | NEW |