OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 // kAutocompleteEditFontPixelSizeInPopup. They should be changed accordingly | 201 // kAutocompleteEditFontPixelSizeInPopup. They should be changed accordingly |
202 // if font size for autocomplete edit (in popup) change. | 202 // if font size for autocomplete edit (in popup) change. |
203 const int kAutocompleteVerticalMargin = 1; | 203 const int kAutocompleteVerticalMargin = 1; |
204 const int kAutocompleteVerticalMarginInPopup = 2; | 204 const int kAutocompleteVerticalMarginInPopup = 2; |
205 | 205 |
206 int GetEditFontPixelSize(bool popup_window_mode) { | 206 int GetEditFontPixelSize(bool popup_window_mode) { |
207 return popup_window_mode ? kAutocompleteEditFontPixelSizeInPopup : | 207 return popup_window_mode ? kAutocompleteEditFontPixelSizeInPopup : |
208 kAutocompleteEditFontPixelSize; | 208 kAutocompleteEditFontPixelSize; |
209 } | 209 } |
210 | 210 |
| 211 // Copies |selected_text| as text to the primary clipboard. If |write_url| is |
| 212 // true, this will also write |url| and |text| to the clipboard as a well-formed |
| 213 // URL. |
| 214 void DoCopy(const string16& selected_text, |
| 215 bool write_url, |
| 216 const GURL& url, |
| 217 const string16& text) { |
| 218 ui::Clipboard* cb = ui::Clipboard::GetForCurrentThread(); |
| 219 ui::ScopedClipboardWriter scw(cb, ui::Clipboard::BUFFER_STANDARD); |
| 220 scw.WriteText(selected_text); |
| 221 if (write_url) { |
| 222 BookmarkNodeData data; |
| 223 data.ReadFromTuple(url, text); |
| 224 data.WriteToClipboard(NULL); |
| 225 } |
| 226 } |
| 227 |
211 } // namespace | 228 } // namespace |
212 | 229 |
213 // static | 230 // static |
214 const char OmniboxViewViews::kViewClassName[] = "BrowserOmniboxViewViews"; | 231 const char OmniboxViewViews::kViewClassName[] = "BrowserOmniboxViewViews"; |
215 | 232 |
216 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, | 233 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, |
217 ToolbarModel* toolbar_model, | 234 ToolbarModel* toolbar_model, |
218 Profile* profile, | 235 Profile* profile, |
219 CommandUpdater* command_updater, | 236 CommandUpdater* command_updater, |
220 bool popup_window_mode, | 237 bool popup_window_mode, |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 ui::Range selection_range; | 778 ui::Range selection_range; |
762 textfield_->GetSelectedRange(&selection_range); | 779 textfield_->GetSelectedRange(&selection_range); |
763 ui::Clipboard* cb = ui::Clipboard::GetForCurrentThread(); | 780 ui::Clipboard* cb = ui::Clipboard::GetForCurrentThread(); |
764 string16 selected_text; | 781 string16 selected_text; |
765 cb->ReadText(ui::Clipboard::BUFFER_STANDARD, &selected_text); | 782 cb->ReadText(ui::Clipboard::BUFFER_STANDARD, &selected_text); |
766 const string16 text = textfield_->text(); | 783 const string16 text = textfield_->text(); |
767 GURL url; | 784 GURL url; |
768 bool write_url; | 785 bool write_url; |
769 model()->AdjustTextForCopy(selection_range.GetMin(), selected_text == text, | 786 model()->AdjustTextForCopy(selection_range.GetMin(), selected_text == text, |
770 &selected_text, &url, &write_url); | 787 &selected_text, &url, &write_url); |
771 ui::ScopedClipboardWriter scw(cb, ui::Clipboard::BUFFER_STANDARD); | 788 DoCopy(selected_text, write_url, url, text); |
772 scw.WriteText(selected_text); | |
773 if (write_url) { | |
774 BookmarkNodeData data; | |
775 data.ReadFromTuple(url, text); | |
776 data.WriteToClipboard(NULL); | |
777 } | |
778 } | 789 } |
779 | 790 |
780 void OmniboxViewViews::OnWriteDragData(ui::OSExchangeData* data) { | 791 void OmniboxViewViews::OnWriteDragData(ui::OSExchangeData* data) { |
781 ui::Range selection_range; | 792 ui::Range selection_range; |
782 textfield_->GetSelectedRange(&selection_range); | 793 textfield_->GetSelectedRange(&selection_range); |
783 string16 selected_text = textfield_->GetSelectedText(); | 794 string16 selected_text = textfield_->GetSelectedText(); |
784 const string16 text = textfield_->text(); | 795 const string16 text = textfield_->text(); |
785 GURL url; | 796 GURL url; |
786 bool write_url; | 797 bool write_url; |
787 model()->AdjustTextForCopy(selection_range.start(), selected_text == text, | 798 model()->AdjustTextForCopy(selection_range.start(), selected_text == text, |
788 &selected_text, &url, &write_url); | 799 &selected_text, &url, &write_url); |
789 data->SetString(selected_text); | 800 data->SetString(selected_text); |
790 if (write_url) | 801 if (write_url) |
791 data->SetURL(url, selected_text); | 802 data->SetURL(url, selected_text); |
792 } | 803 } |
793 | 804 |
794 void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) { | 805 void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) { |
795 // Minor note: We use IDC_ for command id here while the underlying textfield | 806 // Minor note: We use IDC_ for command id here while the underlying textfield |
796 // is using IDS_ for all its command ids. This is because views cannot depend | 807 // is using IDS_ for all its command ids. This is because views cannot depend |
797 // on IDC_ for now. | 808 // on IDC_ for now. |
798 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, | 809 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, |
799 IDS_EDIT_SEARCH_ENGINES); | 810 IDS_EDIT_SEARCH_ENGINES); |
800 | 811 |
| 812 if (chrome::search::IsInstantExtendedAPIEnabled( |
| 813 location_bar_view_->profile())) { |
| 814 int copy_position = menu_contents->GetIndexOfCommandId(IDS_APP_COPY); |
| 815 DCHECK(copy_position >= 0); |
| 816 menu_contents->InsertItemWithStringIdAt( |
| 817 copy_position + 1, IDC_COPY_URL, IDS_COPY_URL); |
| 818 } |
| 819 |
801 int paste_position = menu_contents->GetIndexOfCommandId(IDS_APP_PASTE); | 820 int paste_position = menu_contents->GetIndexOfCommandId(IDS_APP_PASTE); |
802 if (paste_position >= 0) | 821 DCHECK(paste_position >= 0); |
803 menu_contents->InsertItemWithStringIdAt( | 822 menu_contents->InsertItemWithStringIdAt( |
804 paste_position + 1, IDS_PASTE_AND_GO, IDS_PASTE_AND_GO); | 823 paste_position + 1, IDS_PASTE_AND_GO, IDS_PASTE_AND_GO); |
805 } | 824 } |
806 | 825 |
807 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { | 826 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { |
808 return (command_id == IDS_PASTE_AND_GO) ? | 827 if (command_id == IDS_PASTE_AND_GO) |
809 model()->CanPasteAndGo(GetClipboardText()) : | 828 return model()->CanPasteAndGo(GetClipboardText()); |
810 command_updater()->IsCommandEnabled(command_id); | 829 if (command_id == IDS_COPY_URL) { |
| 830 return toolbar_model()->WouldReplaceSearchURLWithSearchTerms() && |
| 831 !model()->user_input_in_progress(); |
| 832 } |
| 833 return command_updater()->IsCommandEnabled(command_id); |
811 } | 834 } |
812 | 835 |
813 bool OmniboxViewViews::IsItemForCommandIdDynamic(int command_id) const { | 836 bool OmniboxViewViews::IsItemForCommandIdDynamic(int command_id) const { |
814 return command_id == IDS_PASTE_AND_GO; | 837 return command_id == IDS_PASTE_AND_GO; |
815 } | 838 } |
816 | 839 |
817 string16 OmniboxViewViews::GetLabelForCommandId(int command_id) const { | 840 string16 OmniboxViewViews::GetLabelForCommandId(int command_id) const { |
818 if (command_id == IDS_PASTE_AND_GO) { | 841 if (command_id == IDS_PASTE_AND_GO) { |
819 return l10n_util::GetStringUTF16( | 842 return l10n_util::GetStringUTF16( |
820 model()->IsPasteAndSearch(GetClipboardText()) ? | 843 model()->IsPasteAndSearch(GetClipboardText()) ? |
821 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO); | 844 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO); |
822 } | 845 } |
823 | 846 |
824 return string16(); | 847 return string16(); |
825 } | 848 } |
826 | 849 |
827 void OmniboxViewViews::ExecuteCommand(int command_id) { | 850 void OmniboxViewViews::ExecuteCommand(int command_id) { |
828 if (command_id == IDS_PASTE_AND_GO) { | 851 if (command_id == IDS_PASTE_AND_GO) |
829 model()->PasteAndGo(GetClipboardText()); | 852 model()->PasteAndGo(GetClipboardText()); |
830 return; | 853 else if (command_id == IDS_COPY_URL) |
831 } | 854 CopyURL(); |
832 | 855 else |
833 command_updater()->ExecuteCommand(command_id); | 856 command_updater()->ExecuteCommand(command_id); |
834 } | 857 } |
835 | 858 |
836 #if defined(OS_CHROMEOS) | 859 #if defined(OS_CHROMEOS) |
837 void OmniboxViewViews::CandidateWindowOpened( | 860 void OmniboxViewViews::CandidateWindowOpened( |
838 chromeos::input_method::InputMethodManager* manager) { | 861 chromeos::input_method::InputMethodManager* manager) { |
839 ime_candidate_window_open_ = true; | 862 ime_candidate_window_open_ = true; |
840 CloseOmniboxPopup(); | 863 CloseOmniboxPopup(); |
841 } | 864 } |
842 | 865 |
843 void OmniboxViewViews::CandidateWindowClosed( | 866 void OmniboxViewViews::CandidateWindowClosed( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 const ui::Range& range) { | 921 const ui::Range& range) { |
899 if (text != GetText()) | 922 if (text != GetText()) |
900 textfield_->SetText(text); | 923 textfield_->SetText(text); |
901 textfield_->SelectRange(range); | 924 textfield_->SelectRange(range); |
902 } | 925 } |
903 | 926 |
904 string16 OmniboxViewViews::GetSelectedText() const { | 927 string16 OmniboxViewViews::GetSelectedText() const { |
905 // TODO(oshima): Support instant, IME. | 928 // TODO(oshima): Support instant, IME. |
906 return textfield_->GetSelectedText(); | 929 return textfield_->GetSelectedText(); |
907 } | 930 } |
| 931 |
| 932 void OmniboxViewViews::CopyURL() { |
| 933 const string16& text = toolbar_model()->GetText(false); |
| 934 DoCopy(text, true, toolbar_model()->GetURL(), text); |
| 935 } |
OLD | NEW |