| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 const ViewState view_state; | 98 const ViewState view_state; |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 // Returns a lazily initialized property bag accessor for saving our state in a | 101 // Returns a lazily initialized property bag accessor for saving our state in a |
| 102 // TabContents. | 102 // TabContents. |
| 103 PropertyAccessor<AutocompleteEditState>* GetStateAccessor() { | 103 PropertyAccessor<AutocompleteEditState>* GetStateAccessor() { |
| 104 static PropertyAccessor<AutocompleteEditState> state; | 104 static PropertyAccessor<AutocompleteEditState> state; |
| 105 return &state; | 105 return &state; |
| 106 } | 106 } |
| 107 | 107 |
| 108 // A convenience method for applying URL styles. |
| 109 void ApplyURLStyle(views::Textfield* textfield, |
| 110 size_t start, |
| 111 size_t end, |
| 112 SkColor color, |
| 113 bool strike) { |
| 114 gfx::StyleRange style; |
| 115 style.foreground = color; |
| 116 style.range = ui::Range(start, end); |
| 117 style.strike = strike; |
| 118 textfield->ApplyStyleRange(style); |
| 119 } |
| 120 |
| 108 const int kAutocompleteVerticalMargin = 4; | 121 const int kAutocompleteVerticalMargin = 4; |
| 109 | 122 |
| 110 // TODO(oshima): I'm currently using slightly different color than | 123 // TODO(oshima): I'm currently using slightly different color than |
| 111 // gtk/win omnibox so that I can tell which one is used from its color. | 124 // gtk/win omnibox so that I can tell which one is used from its color. |
| 112 // Fix this once we finish all features. | 125 // Fix this once we finish all features. |
| 113 const SkColor kFadedTextColor = SK_ColorGRAY; | 126 const SkColor kFadedTextColor = SK_ColorGRAY; |
| 114 const SkColor kNormalTextColor = SK_ColorBLACK; | 127 const SkColor kNormalTextColor = SK_ColorBLACK; |
| 115 const SkColor kSecureSchemeColor = SK_ColorGREEN; | 128 const SkColor kSecureSchemeColor = SK_ColorGREEN; |
| 116 const SkColor kSecurityErrorSchemeColor = SK_ColorRED; | 129 const SkColor kSecurityErrorSchemeColor = SK_ColorRED; |
| 117 | 130 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 128 bool popup_window_mode, | 141 bool popup_window_mode, |
| 129 const views::View* location_bar) | 142 const views::View* location_bar) |
| 130 : model_(new AutocompleteEditModel(this, controller, profile)), | 143 : model_(new AutocompleteEditModel(this, controller, profile)), |
| 131 popup_view_(CreatePopupView(profile, location_bar)), | 144 popup_view_(CreatePopupView(profile, location_bar)), |
| 132 controller_(controller), | 145 controller_(controller), |
| 133 toolbar_model_(toolbar_model), | 146 toolbar_model_(toolbar_model), |
| 134 command_updater_(command_updater), | 147 command_updater_(command_updater), |
| 135 popup_window_mode_(popup_window_mode), | 148 popup_window_mode_(popup_window_mode), |
| 136 security_level_(ToolbarModel::NONE), | 149 security_level_(ToolbarModel::NONE), |
| 137 ime_composing_before_change_(false), | 150 ime_composing_before_change_(false), |
| 138 delete_at_end_pressed_(false), | 151 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, | 152 set_border(views::Border::CreateEmptyBorder(kAutocompleteVerticalMargin, 0, |
| 144 kAutocompleteVerticalMargin, 0)); | 153 kAutocompleteVerticalMargin, 0)); |
| 145 } | 154 } |
| 146 | 155 |
| 147 OmniboxViewViews::~OmniboxViewViews() { | 156 OmniboxViewViews::~OmniboxViewViews() { |
| 148 NotificationService::current()->Notify( | 157 NotificationService::current()->Notify( |
| 149 chrome::NOTIFICATION_OMNIBOX_DESTROYED, Source<OmniboxViewViews>(this), | 158 chrome::NOTIFICATION_OMNIBOX_DESTROYED, Source<OmniboxViewViews>(this), |
| 150 NotificationService::NoDetails()); | 159 NotificationService::NoDetails()); |
| 151 // Explicitly teardown members which have a reference to us. Just to be safe | 160 // 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. | 161 // 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 | 625 |
| 617 //////////////////////////////////////////////////////////////////////////////// | 626 //////////////////////////////////////////////////////////////////////////////// |
| 618 // OmniboxViewViews, private: | 627 // OmniboxViewViews, private: |
| 619 | 628 |
| 620 size_t OmniboxViewViews::GetTextLength() const { | 629 size_t OmniboxViewViews::GetTextLength() const { |
| 621 // TODO(oshima): Support instant, IME. | 630 // TODO(oshima): Support instant, IME. |
| 622 return textfield_->text().length(); | 631 return textfield_->text().length(); |
| 623 } | 632 } |
| 624 | 633 |
| 625 void OmniboxViewViews::EmphasizeURLComponents() { | 634 void OmniboxViewViews::EmphasizeURLComponents() { |
| 626 InitTextStyles(); | |
| 627 // See whether the contents are a URL with a non-empty host portion, which we | 635 // 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 | 636 // 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 | 637 // 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 | 638 // 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 | 639 // be treated as a search or a navigation, and is the same method the Paste |
| 632 // And Go system uses. | 640 // And Go system uses. |
| 633 string16 text = GetText(); | 641 string16 text = GetText(); |
| 634 url_parse::Component scheme, host; | 642 url_parse::Component scheme, host; |
| 635 AutocompleteInput::ParseForEmphasizeComponents( | 643 AutocompleteInput::ParseForEmphasizeComponents( |
| 636 text, model_->GetDesiredTLD(), &scheme, &host); | 644 text, model_->GetDesiredTLD(), &scheme, &host); |
| 637 const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0); | 645 const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0); |
| 638 | 646 SkColor base_color = emphasize ? kFadedTextColor : kNormalTextColor; |
| 639 textfield_->ClearAllTextStyles(); | 647 ApplyURLStyle(textfield_, 0, text.length(), base_color, false); |
| 640 if (emphasize) { | 648 if (emphasize) |
| 641 textfield_->ApplyTextStyle(faded_text_style_, ui::Range(0, text.length())); | 649 ApplyURLStyle(textfield_, host.begin, host.end(), kNormalTextColor, false); |
| 642 textfield_->ApplyTextStyle(normal_text_style_, | |
| 643 ui::Range(host.begin, host.end())); | |
| 644 } else { | |
| 645 textfield_->ApplyTextStyle(normal_text_style_, ui::Range(0, text.length())); | |
| 646 } | |
| 647 // Emphasize the scheme for security UI display purposes (if necessary). | 650 // Emphasize the scheme for security UI display purposes (if necessary). |
| 648 if (!model_->user_input_in_progress() && scheme.is_nonempty() && | 651 if (!model_->user_input_in_progress() && scheme.is_nonempty() && |
| 649 (security_level_ != ToolbarModel::NONE)) { | 652 (security_level_ != ToolbarModel::NONE)) { |
| 650 ui::Range scheme_range(scheme.begin, scheme.end()); | 653 const size_t start = scheme.begin, end = scheme.end(); |
| 651 switch (security_level_) { | 654 switch (security_level_) { |
| 652 case ToolbarModel::SECURITY_ERROR: | 655 case ToolbarModel::SECURITY_ERROR: |
| 653 textfield_->ApplyTextStyle(security_error_scheme_style_, scheme_range); | 656 ApplyURLStyle(textfield_, start, end, kSecurityErrorSchemeColor, true); |
| 654 break; | 657 break; |
| 655 case ToolbarModel::SECURITY_WARNING: | 658 case ToolbarModel::SECURITY_WARNING: |
| 656 textfield_->ApplyTextStyle(faded_text_style_, scheme_range); | 659 ApplyURLStyle(textfield_, start, end, kFadedTextColor, false); |
| 657 break; | 660 break; |
| 658 case ToolbarModel::EV_SECURE: | 661 case ToolbarModel::EV_SECURE: |
| 659 case ToolbarModel::SECURE: | 662 case ToolbarModel::SECURE: |
| 660 textfield_->ApplyTextStyle(secure_scheme_style_, scheme_range); | 663 ApplyURLStyle(textfield_, start, end, kSecureSchemeColor, false); |
| 661 break; | 664 break; |
| 662 default: | 665 default: |
| 663 NOTREACHED() << "Unknown SecurityLevel:" << security_level_; | 666 NOTREACHED() << "Unknown SecurityLevel:" << security_level_; |
| 664 } | 667 } |
| 665 } | 668 } |
| 666 } | 669 } |
| 667 | 670 |
| 668 void OmniboxViewViews::TextChanged() { | 671 void OmniboxViewViews::TextChanged() { |
| 669 EmphasizeURLComponents(); | 672 EmphasizeURLComponents(); |
| 670 model_->OnChanged(); | 673 model_->OnChanged(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 691 Profile* profile, | 694 Profile* profile, |
| 692 const View* location_bar) { | 695 const View* location_bar) { |
| 693 #if defined(TOUCH_UI) | 696 #if defined(TOUCH_UI) |
| 694 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; | 697 typedef TouchAutocompletePopupContentsView AutocompleteContentsView; |
| 695 #else | 698 #else |
| 696 typedef AutocompletePopupContentsView AutocompleteContentsView; | 699 typedef AutocompletePopupContentsView AutocompleteContentsView; |
| 697 #endif | 700 #endif |
| 698 return new AutocompleteContentsView( | 701 return new AutocompleteContentsView( |
| 699 gfx::Font(), this, model_.get(), profile, location_bar); | 702 gfx::Font(), this, model_.get(), profile, location_bar); |
| 700 } | 703 } |
| 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 |