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" |
| 11 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 11 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | 13 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 14 #include "chrome/browser/command_updater.h" | 14 #include "chrome/browser/command_updater.h" |
| 15 #include "chrome/browser/ui/views/autocomplete/autocomplete_popup_contents_view. h" | 15 #include "chrome/browser/ui/views/autocomplete/autocomplete_popup_contents_view. h" |
| 16 #include "chrome/browser/ui/views/autocomplete/touch_autocomplete_popup_contents _view.h" | 16 #include "chrome/browser/ui/views/autocomplete/touch_autocomplete_popup_contents _view.h" |
| 17 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 17 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "content/browser/tab_contents/tab_contents.h" | 19 #include "content/browser/tab_contents/tab_contents.h" |
| 20 #include "content/common/notification_service.h" | 20 #include "content/common/notification_service.h" |
| 21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 23 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 24 #include "third_party/skia/include/core/SkColor.h" | 24 #include "third_party/skia/include/core/SkColor.h" |
| 25 #include "ui/base/accessibility/accessible_view_state.h" | 25 #include "ui/base/accessibility/accessible_view_state.h" |
| 26 #include "ui/base/dragdrop/drag_drop_types.h" | 26 #include "ui/base/dragdrop/drag_drop_types.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
| 29 #include "ui/gfx/font.h" | 29 #include "ui/gfx/font.h" |
| 30 #include "ui/gfx/render_text.h" | |
| 30 #include "views/border.h" | 31 #include "views/border.h" |
| 31 #include "views/controls/textfield/text_style.h" | |
| 32 #include "views/controls/textfield/textfield.h" | 32 #include "views/controls/textfield/textfield.h" |
| 33 #include "views/layout/fill_layout.h" | 33 #include "views/layout/fill_layout.h" |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // Textfield for autocomplete that intercepts events that are necessary | 37 // Textfield for autocomplete that intercepts events that are necessary |
| 38 // for OmniboxViewViews. | 38 // for OmniboxViewViews. |
| 39 class AutocompleteTextfield : public views::Textfield { | 39 class AutocompleteTextfield : public views::Textfield { |
| 40 public: | 40 public: |
| 41 explicit AutocompleteTextfield(OmniboxViewViews* omnibox_view) | 41 explicit AutocompleteTextfield(OmniboxViewViews* omnibox_view) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 bool popup_window_mode, | 128 bool popup_window_mode, |
| 129 const views::View* location_bar) | 129 const views::View* location_bar) |
| 130 : model_(new AutocompleteEditModel(this, controller, profile)), | 130 : model_(new AutocompleteEditModel(this, controller, profile)), |
| 131 popup_view_(CreatePopupView(profile, location_bar)), | 131 popup_view_(CreatePopupView(profile, location_bar)), |
| 132 controller_(controller), | 132 controller_(controller), |
| 133 toolbar_model_(toolbar_model), | 133 toolbar_model_(toolbar_model), |
| 134 command_updater_(command_updater), | 134 command_updater_(command_updater), |
| 135 popup_window_mode_(popup_window_mode), | 135 popup_window_mode_(popup_window_mode), |
| 136 security_level_(ToolbarModel::NONE), | 136 security_level_(ToolbarModel::NONE), |
| 137 ime_composing_before_change_(false), | 137 ime_composing_before_change_(false), |
| 138 delete_at_end_pressed_(false), | 138 delete_at_end_pressed_(false) { |
| 139 faded_text_style_(NULL), | |
| 140 normal_text_style_(NULL), | |
| 141 security_error_scheme_style_(NULL), | |
| 142 secure_scheme_style_(NULL) { | |
| 143 set_border(views::Border::CreateEmptyBorder(kAutocompleteVerticalMargin, 0, | 139 set_border(views::Border::CreateEmptyBorder(kAutocompleteVerticalMargin, 0, |
| 144 kAutocompleteVerticalMargin, 0)); | 140 kAutocompleteVerticalMargin, 0)); |
| 145 } | 141 } |
| 146 | 142 |
| 147 OmniboxViewViews::~OmniboxViewViews() { | 143 OmniboxViewViews::~OmniboxViewViews() { |
| 148 NotificationService::current()->Notify( | 144 NotificationService::current()->Notify( |
| 149 chrome::NOTIFICATION_OMNIBOX_DESTROYED, Source<OmniboxViewViews>(this), | 145 chrome::NOTIFICATION_OMNIBOX_DESTROYED, Source<OmniboxViewViews>(this), |
| 150 NotificationService::NoDetails()); | 146 NotificationService::NoDetails()); |
| 151 // Explicitly teardown members which have a reference to us. Just to be safe | 147 // Explicitly teardown members which have a reference to us. Just to be safe |
| 152 // we want them to be destroyed before destroying any other internal state. | 148 // we want them to be destroyed before destroying any other internal state. |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 616 | 612 |
| 617 //////////////////////////////////////////////////////////////////////////////// | 613 //////////////////////////////////////////////////////////////////////////////// |
| 618 // OmniboxViewViews, private: | 614 // OmniboxViewViews, private: |
| 619 | 615 |
| 620 size_t OmniboxViewViews::GetTextLength() const { | 616 size_t OmniboxViewViews::GetTextLength() const { |
| 621 // TODO(oshima): Support instant, IME. | 617 // TODO(oshima): Support instant, IME. |
| 622 return textfield_->text().length(); | 618 return textfield_->text().length(); |
| 623 } | 619 } |
| 624 | 620 |
| 625 void OmniboxViewViews::EmphasizeURLComponents() { | 621 void OmniboxViewViews::EmphasizeURLComponents() { |
| 626 InitTextStyles(); | |
| 627 // See whether the contents are a URL with a non-empty host portion, which we | 622 // See whether the contents are a URL with a non-empty host portion, which we |
| 628 // should emphasize. To check for a URL, rather than using the type returned | 623 // should emphasize. To check for a URL, rather than using the type returned |
| 629 // by Parse(), ask the model, which will check the desired page transition for | 624 // by Parse(), ask the model, which will check the desired page transition for |
| 630 // this input. This can tell us whether an UNKNOWN input string is going to | 625 // this input. This can tell us whether an UNKNOWN input string is going to |
| 631 // be treated as a search or a navigation, and is the same method the Paste | 626 // be treated as a search or a navigation, and is the same method the Paste |
| 632 // And Go system uses. | 627 // And Go system uses. |
| 633 string16 text = GetText(); | 628 string16 text = GetText(); |
| 634 url_parse::Component scheme, host; | 629 url_parse::Component scheme, host; |
| 635 AutocompleteInput::ParseForEmphasizeComponents( | 630 AutocompleteInput::ParseForEmphasizeComponents( |
| 636 text, model_->GetDesiredTLD(), &scheme, &host); | 631 text, model_->GetDesiredTLD(), &scheme, &host); |
| 637 const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0); | 632 const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0); |
| 638 | 633 gfx::StyleRange base_style; |
| 639 textfield_->ClearAllTextStyles(); | 634 base_style.foreground = emphasize ? kFadedTextColor : kNormalTextColor; |
| 635 base_style.range = ui::Range(0, text.length()); | |
| 636 textfield_->ApplyStyleRange(base_style); | |
| 640 if (emphasize) { | 637 if (emphasize) { |
|
oshima
2011/07/23 09:51:12
While I understand what it's doing, it is now a bi
msw
2011/07/25 05:09:54
Done.
| |
| 641 textfield_->ApplyTextStyle(faded_text_style_, ui::Range(0, text.length())); | 638 gfx::StyleRange host_style; |
| 642 textfield_->ApplyTextStyle(normal_text_style_, | 639 host_style.foreground = kNormalTextColor; |
| 643 ui::Range(host.begin, host.end())); | 640 host_style.range = ui::Range(host.begin, host.end()); |
| 644 } else { | 641 textfield_->ApplyStyleRange(host_style); |
| 645 textfield_->ApplyTextStyle(normal_text_style_, ui::Range(0, text.length())); | |
| 646 } | 642 } |
| 647 // Emphasize the scheme for security UI display purposes (if necessary). | 643 // Emphasize the scheme for security UI display purposes (if necessary). |
| 648 if (!model_->user_input_in_progress() && scheme.is_nonempty() && | 644 if (!model_->user_input_in_progress() && scheme.is_nonempty() && |
| 649 (security_level_ != ToolbarModel::NONE)) { | 645 (security_level_ != ToolbarModel::NONE)) { |
| 650 ui::Range scheme_range(scheme.begin, scheme.end()); | 646 gfx::StyleRange scheme_style; |
| 647 scheme_style.range = ui::Range(scheme.begin, scheme.end()); | |
| 651 switch (security_level_) { | 648 switch (security_level_) { |
| 652 case ToolbarModel::SECURITY_ERROR: | 649 case ToolbarModel::SECURITY_ERROR: |
| 653 textfield_->ApplyTextStyle(security_error_scheme_style_, scheme_range); | 650 scheme_style.foreground = kSecurityErrorSchemeColor; |
| 651 scheme_style.strike = true; | |
| 654 break; | 652 break; |
| 655 case ToolbarModel::SECURITY_WARNING: | 653 case ToolbarModel::SECURITY_WARNING: |
| 656 textfield_->ApplyTextStyle(faded_text_style_, scheme_range); | 654 scheme_style.foreground = kFadedTextColor; |
| 657 break; | 655 break; |
| 658 case ToolbarModel::EV_SECURE: | 656 case ToolbarModel::EV_SECURE: |
| 659 case ToolbarModel::SECURE: | 657 case ToolbarModel::SECURE: |
| 660 textfield_->ApplyTextStyle(secure_scheme_style_, scheme_range); | 658 scheme_style.foreground = kSecureSchemeColor; |
| 661 break; | 659 break; |
| 662 default: | 660 default: |
| 663 NOTREACHED() << "Unknown SecurityLevel:" << security_level_; | 661 NOTREACHED() << "Unknown SecurityLevel:" << security_level_; |
| 664 } | 662 } |
| 663 textfield_->ApplyStyleRange(scheme_style); | |
| 665 } | 664 } |
| 666 } | 665 } |
| 667 | 666 |
| 668 void OmniboxViewViews::TextChanged() { | 667 void OmniboxViewViews::TextChanged() { |
| 669 EmphasizeURLComponents(); | 668 EmphasizeURLComponents(); |
| 670 model_->OnChanged(); | 669 model_->OnChanged(); |
| 671 } | 670 } |
| 672 | 671 |
| 673 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, | 672 void OmniboxViewViews::SetTextAndSelectedRange(const string16& text, |
| 674 const ui::Range& range) { | 673 const ui::Range& range) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 691 Profile* profile, | 690 Profile* profile, |
| 692 const View* location_bar) { | 691 const View* location_bar) { |
| 693 #if defined(TOUCH_UI) | 692 #if defined(TOUCH_UI) |
| 694 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; | 693 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; |
| 695 #else | 694 #else |
| 696 typedef AutocompletePopupContentsView AutocompleteContentsView; | 695 typedef AutocompletePopupContentsView AutocompleteContentsView; |
| 697 #endif | 696 #endif |
| 698 return new AutocompleteContentsView( | 697 return new AutocompleteContentsView( |
| 699 gfx::Font(), this, model_.get(), profile, location_bar); | 698 gfx::Font(), this, model_.get(), profile, location_bar); |
| 700 } | 699 } |
| 701 | |
| 702 void OmniboxViewViews::InitTextStyles() { | |
| 703 if (faded_text_style_) | |
| 704 return; | |
| 705 faded_text_style_ = textfield_->CreateTextStyle(); | |
| 706 normal_text_style_ = textfield_->CreateTextStyle(); | |
| 707 security_error_scheme_style_ = textfield_->CreateTextStyle(); | |
| 708 secure_scheme_style_ = textfield_->CreateTextStyle(); | |
| 709 | |
| 710 faded_text_style_->set_foreground(kFadedTextColor); | |
| 711 normal_text_style_->set_foreground(kNormalTextColor); | |
| 712 secure_scheme_style_->set_foreground(kSecureSchemeColor); | |
| 713 security_error_scheme_style_->set_foreground(kSecurityErrorSchemeColor); | |
| 714 security_error_scheme_style_->set_strike(true); | |
| 715 } | |
| OLD | NEW |