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