Chromium Code Reviews| 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 SelectRange(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] != '?')) { |
|
msw
2011/10/04 07:25:41
you can remove the braces for these conditional bl
xji
2011/10/05 01:23:42
Done.
| |
| 408 SetUserText(ASCIIToUTF16("?")); | 408 SetUserText(ASCIIToUTF16("?")); |
| 409 } else { | 409 } else { |
| 410 textfield_->SelectSelectionModel(gfx::SelectionModel(current_text.size(), | 410 SelectRange(current_text.size(), start + 1); |
| 411 start + 1)); | |
| 412 } | 411 } |
| 413 } | 412 } |
| 414 | 413 |
| 415 bool OmniboxViewViews::IsSelectAll() { | 414 bool OmniboxViewViews::IsSelectAll() { |
| 416 // TODO(oshima): IME support. | 415 // TODO(oshima): IME support. |
| 417 return textfield_->text() == textfield_->GetSelectedText(); | 416 return textfield_->text() == textfield_->GetSelectedText(); |
| 418 } | 417 } |
| 419 | 418 |
| 420 bool OmniboxViewViews::DeleteAtEndPressed() { | 419 bool OmniboxViewViews::DeleteAtEndPressed() { |
| 421 return delete_at_end_pressed_; | 420 return delete_at_end_pressed_; |
| 422 } | 421 } |
| 423 | 422 |
| 424 void OmniboxViewViews::GetSelectionBounds(string16::size_type* start, | 423 void OmniboxViewViews::GetSelectionBounds(string16::size_type* start, |
| 425 string16::size_type* end) { | 424 string16::size_type* end) { |
| 426 gfx::SelectionModel sel; | 425 ui::Range range; |
| 427 textfield_->GetSelectionModel(&sel); | 426 textfield_->GetSelectedRange(&range); |
| 428 *start = static_cast<size_t>(sel.selection_end()); | 427 *start = static_cast<size_t>(range.end()); |
| 429 *end = static_cast<size_t>(sel.selection_start()); | 428 *end = static_cast<size_t>(range.start()); |
| 430 } | 429 } |
| 431 | 430 |
| 432 void OmniboxViewViews::SelectAll(bool reversed) { | 431 void OmniboxViewViews::SelectAll(bool reversed) { |
| 433 if (reversed) | 432 if (reversed) |
| 434 textfield_->SelectSelectionModel(gfx::SelectionModel(GetTextLength(), 0)); | 433 SelectRange(GetTextLength(), 0); |
| 435 else | 434 else |
| 436 textfield_->SelectSelectionModel(gfx::SelectionModel(0, GetTextLength())); | 435 SelectRange(0, GetTextLength()); |
| 437 } | 436 } |
| 438 | 437 |
| 439 void OmniboxViewViews::RevertAll() { | 438 void OmniboxViewViews::RevertAll() { |
| 440 ClosePopup(); | 439 ClosePopup(); |
| 441 model_->Revert(); | 440 model_->Revert(); |
| 442 TextChanged(); | 441 TextChanged(); |
| 443 } | 442 } |
| 444 | 443 |
| 445 void OmniboxViewViews::UpdatePopup() { | 444 void OmniboxViewViews::UpdatePopup() { |
| 446 model_->SetInputInProgress(true); | 445 model_->SetInputInProgress(true); |
| 447 if (!model_->has_focus()) | 446 if (!model_->has_focus()) |
| 448 return; | 447 return; |
| 449 | 448 |
| 450 // Don't inline autocomplete when the caret/selection isn't at the end of | 449 // Don't inline autocomplete when the caret/selection isn't at the end of |
| 451 // the text, or in the middle of composition. | 450 // the text, or in the middle of composition. |
| 452 gfx::SelectionModel sel; | 451 ui::Range sel; |
| 453 textfield_->GetSelectionModel(&sel); | 452 textfield_->GetSelectedRange(&sel); |
| 454 size_t max_of_selection = std::max(sel.selection_start(), | |
| 455 sel.selection_end()); | |
| 456 bool no_inline_autocomplete = | 453 bool no_inline_autocomplete = |
| 457 max_of_selection < GetTextLength() || textfield_->IsIMEComposing(); | 454 sel.GetMax() < GetTextLength() || textfield_->IsIMEComposing(); |
| 458 | 455 |
| 459 bool is_sel_empty = (sel.selection_start() == sel.selection_end()); | 456 model_->StartAutocomplete(!sel.is_empty(), no_inline_autocomplete); |
| 460 model_->StartAutocomplete(!is_sel_empty, no_inline_autocomplete); | |
| 461 } | 457 } |
| 462 | 458 |
| 463 void OmniboxViewViews::ClosePopup() { | 459 void OmniboxViewViews::ClosePopup() { |
| 464 model_->StopAutocomplete(); | 460 model_->StopAutocomplete(); |
| 465 } | 461 } |
| 466 | 462 |
| 467 void OmniboxViewViews::SetFocus() { | 463 void OmniboxViewViews::SetFocus() { |
| 468 // In views-implementation, the focus is on textfield rather than OmniboxView. | 464 // In views-implementation, the focus is on textfield rather than OmniboxView. |
| 469 textfield_->RequestFocus(); | 465 textfield_->RequestFocus(); |
| 470 } | 466 } |
| 471 | 467 |
| 472 void OmniboxViewViews::OnTemporaryTextMaybeChanged( | 468 void OmniboxViewViews::OnTemporaryTextMaybeChanged( |
| 473 const string16& display_text, | 469 const string16& display_text, |
| 474 bool save_original_selection) { | 470 bool save_original_selection) { |
| 475 if (save_original_selection) { | 471 if (save_original_selection) |
| 476 gfx::SelectionModel sel; | 472 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 | 473 |
| 482 SetWindowTextAndCaretPos(display_text, display_text.length()); | 474 SetWindowTextAndCaretPos(display_text, display_text.length()); |
| 483 TextChanged(); | 475 TextChanged(); |
| 484 } | 476 } |
| 485 | 477 |
| 486 bool OmniboxViewViews::OnInlineAutocompleteTextMaybeChanged( | 478 bool OmniboxViewViews::OnInlineAutocompleteTextMaybeChanged( |
| 487 const string16& display_text, | 479 const string16& display_text, |
| 488 size_t user_text_length) { | 480 size_t user_text_length) { |
| 489 if (display_text == GetText()) | 481 if (display_text == GetText()) |
| 490 return false; | 482 return false; |
| 491 ui::Range range(display_text.size(), user_text_length); | 483 ui::Range range(display_text.size(), user_text_length); |
| 492 SetTextAndSelectedRange(display_text, range); | 484 SetTextAndSelectedRange(display_text, range); |
| 493 TextChanged(); | 485 TextChanged(); |
| 494 return true; | 486 return true; |
| 495 } | 487 } |
| 496 | 488 |
| 497 void OmniboxViewViews::OnRevertTemporaryText() { | 489 void OmniboxViewViews::OnRevertTemporaryText() { |
| 498 gfx::SelectionModel sel(saved_temporary_selection_.start(), | 490 textfield_->SelectRange(saved_temporary_selection_); |
| 499 saved_temporary_selection_.end()); | |
| 500 textfield_->SelectSelectionModel(sel); | |
| 501 TextChanged(); | 491 TextChanged(); |
| 502 } | 492 } |
| 503 | 493 |
| 504 void OmniboxViewViews::OnBeforePossibleChange() { | 494 void OmniboxViewViews::OnBeforePossibleChange() { |
| 505 // Record our state. | 495 // Record our state. |
| 506 text_before_change_ = GetText(); | 496 text_before_change_ = GetText(); |
| 507 gfx::SelectionModel sel; | 497 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(); | 498 ime_composing_before_change_ = textfield_->IsIMEComposing(); |
| 512 } | 499 } |
| 513 | 500 |
| 514 bool OmniboxViewViews::OnAfterPossibleChange() { | 501 bool OmniboxViewViews::OnAfterPossibleChange() { |
| 515 gfx::SelectionModel sel; | |
| 516 textfield_->GetSelectionModel(&sel); | |
| 517 ui::Range new_sel; | 502 ui::Range new_sel; |
| 518 new_sel.set_start(sel.selection_start()); | 503 textfield_->GetSelectedRange(&new_sel); |
| 519 new_sel.set_end(sel.selection_end()); | |
| 520 | 504 |
| 521 // See if the text or selection have changed since OnBeforePossibleChange(). | 505 // See if the text or selection have changed since OnBeforePossibleChange(). |
| 522 const string16 new_text = GetText(); | 506 const string16 new_text = GetText(); |
| 523 const bool text_changed = (new_text != text_before_change_) || | 507 const bool text_changed = (new_text != text_before_change_) || |
| 524 (ime_composing_before_change_ != textfield_->IsIMEComposing()); | 508 (ime_composing_before_change_ != textfield_->IsIMEComposing()); |
| 525 const bool selection_differs = | 509 const bool selection_differs = |
| 526 !((sel_before_change_.is_empty() && new_sel.is_empty()) || | 510 !((sel_before_change_.is_empty() && new_sel.is_empty()) || |
| 527 sel_before_change_.EqualsIgnoringDirection(new_sel)); | 511 sel_before_change_.EqualsIgnoringDirection(new_sel)); |
| 528 | 512 |
| 529 // When the user has deleted text, we don't allow inline autocomplete. Make | 513 // 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 | 674 |
| 691 void OmniboxViewViews::TextChanged() { | 675 void OmniboxViewViews::TextChanged() { |
| 692 EmphasizeURLComponents(); | 676 EmphasizeURLComponents(); |
| 693 model_->OnChanged(); | 677 model_->OnChanged(); |
| 694 } | 678 } |
| 695 | 679 |
| 696 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, | 680 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, |
| 697 const ui::Range& range) { | 681 const ui::Range& range) { |
| 698 if (text != GetText()) | 682 if (text != GetText()) |
| 699 textfield_->SetText(text); | 683 textfield_->SetText(text); |
| 700 textfield_->SelectSelectionModel(gfx::SelectionModel( | 684 textfield_->SelectRange(range); |
| 701 range.start(), range.end())); | |
| 702 } | 685 } |
| 703 | 686 |
| 704 string16 OmniboxViewViews::GetSelectedText() const { | 687 string16 OmniboxViewViews::GetSelectedText() const { |
| 705 // TODO(oshima): Support instant, IME. | 688 // TODO(oshima): Support instant, IME. |
| 706 return textfield_->GetSelectedText(); | 689 return textfield_->GetSelectedText(); |
| 707 } | 690 } |
| 708 | 691 |
| 692 void OmniboxViewViews::SelectRange(size_t caret, size_t end) { | |
| 693 const ui::Range range(caret, end); | |
| 694 textfield_->SelectRange(range); | |
|
msw
2011/10/04 07:25:41
You could optionally inline this without additiona
xji
2011/10/05 01:23:42
Removed this convient function.
It was there origi
| |
| 695 } | |
| 709 | 696 |
| 710 AutocompletePopupView* OmniboxViewViews::CreatePopupView( | 697 AutocompletePopupView* OmniboxViewViews::CreatePopupView( |
| 711 View* location_bar) { | 698 View* location_bar) { |
| 712 #if defined(TOUCH_UI) | 699 #if defined(TOUCH_UI) |
| 713 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; | 700 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; |
| 714 #else | 701 #else |
| 715 typedef AutocompletePopupContentsView AutocompleteContentsView; | 702 typedef AutocompletePopupContentsView AutocompleteContentsView; |
| 716 #endif | 703 #endif |
| 717 return new AutocompleteContentsView(gfx::Font(), this, model_.get(), | 704 return new AutocompleteContentsView(gfx::Font(), this, model_.get(), |
| 718 location_bar); | 705 location_bar); |
| 719 } | 706 } |
| OLD | NEW |