| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #import "chrome/browser/cocoa/location_bar_view_mac.h" | 5 #import "chrome/browser/cocoa/location_bar_view_mac.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" |
| 7 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/app/chrome_dll_resource.h" | 11 #include "chrome/app/chrome_dll_resource.h" |
| 9 #include "chrome/browser/alternate_nav_url_fetcher.h" | 12 #include "chrome/browser/alternate_nav_url_fetcher.h" |
| 10 #import "chrome/browser/app_controller_mac.h" | 13 #import "chrome/browser/app_controller_mac.h" |
| 11 #import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" | 14 #import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" |
| 15 #import "chrome/browser/cocoa/autocomplete_text_field.h" |
| 16 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
| 12 #include "chrome/browser/command_updater.h" | 17 #include "chrome/browser/command_updater.h" |
| 18 #include "chrome/browser/profile.h" |
| 19 #include "chrome/browser/search_engines/template_url.h" |
| 20 #include "chrome/browser/search_engines/template_url_model.h" |
| 21 #include "grit/generated_resources.h" |
| 22 #include "grit/theme_resources.h" |
| 23 #include "skia/ext/skia_utils_mac.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 24 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 | 25 |
| 15 // TODO(shess): This code is mostly copied from the gtk | 26 // TODO(shess): This code is mostly copied from the gtk |
| 16 // implementation. Make sure it's all appropriate and flesh it out. | 27 // implementation. Make sure it's all appropriate and flesh it out. |
| 17 | 28 |
| 29 namespace { |
| 30 |
| 31 // Returns the short name for a keyword. |
| 32 // TODO(shess): Copied from views/location_bar_view.cc. Try to share |
| 33 // it. |
| 34 std::wstring GetKeywordName(Profile* profile, const std::wstring& keyword) { |
| 35 // Make sure the TemplateURL still exists. |
| 36 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLModel |
| 37 // to track changes to the model, this should become a DCHECK. |
| 38 const TemplateURL* template_url = |
| 39 profile->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword); |
| 40 if (template_url) |
| 41 return template_url->AdjustedShortNameForLocaleDirection(); |
| 42 return std::wstring(); |
| 43 } |
| 44 |
| 45 } // namespace |
| 46 |
| 18 LocationBarViewMac::LocationBarViewMac(AutocompleteTextField* field, | 47 LocationBarViewMac::LocationBarViewMac(AutocompleteTextField* field, |
| 19 CommandUpdater* command_updater, | 48 CommandUpdater* command_updater, |
| 20 ToolbarModel* toolbar_model, | 49 ToolbarModel* toolbar_model, |
| 21 Profile* profile) | 50 Profile* profile) |
| 22 : edit_view_(new AutocompleteEditViewMac(this, toolbar_model, profile, | 51 : edit_view_(new AutocompleteEditViewMac(this, toolbar_model, profile, |
| 23 command_updater, field)), | 52 command_updater, field)), |
| 24 command_updater_(command_updater), | 53 command_updater_(command_updater), |
| 25 disposition_(CURRENT_TAB), | 54 field_(field), |
| 26 transition_(PageTransition::TYPED) { | 55 disposition_(CURRENT_TAB), |
| 56 profile_(profile), |
| 57 transition_(PageTransition::TYPED) { |
| 27 } | 58 } |
| 28 | 59 |
| 29 LocationBarViewMac::~LocationBarViewMac() { | 60 LocationBarViewMac::~LocationBarViewMac() { |
| 30 // TODO(shess): Placeholder for omnibox changes. | 61 // TODO(shess): Placeholder for omnibox changes. |
| 31 } | 62 } |
| 32 | 63 |
| 33 std::wstring LocationBarViewMac::GetInputString() const { | 64 std::wstring LocationBarViewMac::GetInputString() const { |
| 34 return location_input_; | 65 return location_input_; |
| 35 } | 66 } |
| 36 | 67 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 if (fetcher->state() == AlternateNavURLFetcher::NOT_STARTED) { | 135 if (fetcher->state() == AlternateNavURLFetcher::NOT_STARTED) { |
| 105 // I'm not sure this should be reachable, but I'm not also sure enough | 136 // I'm not sure this should be reachable, but I'm not also sure enough |
| 106 // that it shouldn't to stick in a NOTREACHED(). In any case, this is | 137 // that it shouldn't to stick in a NOTREACHED(). In any case, this is |
| 107 // harmless; we can simply let the fetcher get deleted here and it will | 138 // harmless; we can simply let the fetcher get deleted here and it will |
| 108 // clean itself up properly. | 139 // clean itself up properly. |
| 109 } else { | 140 } else { |
| 110 fetcher.release(); // The navigation controller will delete the fetcher. | 141 fetcher.release(); // The navigation controller will delete the fetcher. |
| 111 } | 142 } |
| 112 } | 143 } |
| 113 | 144 |
| 145 void LocationBarViewMac::OnChangedImpl(AutocompleteTextField* field, |
| 146 const std::wstring& keyword, |
| 147 const std::wstring& short_name, |
| 148 const bool is_keyword_hint, |
| 149 const bool show_search_hint, |
| 150 NSImage* image) { |
| 151 AutocompleteTextFieldCell* cell = [field autocompleteTextFieldCell]; |
| 152 |
| 153 if (!keyword.empty() && !is_keyword_hint) { |
| 154 // Keyword search mode. The text will be like "Search Engine:". |
| 155 // "Engine" is a parameter to be replaced by text based on the |
| 156 // keyword. |
| 157 |
| 158 // TODO(shess): This needs to additionally support a minimized |
| 159 // version, to be used when the string below is too long. |
| 160 const std::wstring keyword_text( |
| 161 l10n_util::GetStringF(IDS_OMNIBOX_KEYWORD_TEXT, short_name)); |
| 162 [cell setKeywordString:base::SysWideToNSString(keyword_text)]; |
| 163 } else if (!keyword.empty() && is_keyword_hint) { |
| 164 // Keyword is a hint, like "Press [Tab] to search Engine". [Tab] |
| 165 // is a parameter to be replaced by an image. "Engine" is a |
| 166 // parameter to be replaced by text based on the keyword. |
| 167 std::vector<size_t> content_param_offsets; |
| 168 const std::wstring keyword_hint( |
| 169 l10n_util::GetStringF(IDS_OMNIBOX_KEYWORD_HINT, |
| 170 std::wstring(), short_name, |
| 171 &content_param_offsets)); |
| 172 |
| 173 // Should always be 2 offsets, see the comment in |
| 174 // location_bar_view.cc after IDS_OMNIBOX_KEYWORD_HINT fetch. |
| 175 DCHECK_EQ(content_param_offsets.size(), 2U); |
| 176 |
| 177 // Where to put the [TAB] image. |
| 178 const size_t split(content_param_offsets.front()); |
| 179 |
| 180 NSString* prefix = base::SysWideToNSString(keyword_hint.substr(0, split)); |
| 181 NSString* suffix = base::SysWideToNSString(keyword_hint.substr(split)); |
| 182 |
| 183 [cell setKeywordHintPrefix:prefix image:image suffix:suffix]; |
| 184 } else if (show_search_hint) { |
| 185 // Show a search hint right-justified in the field if there is no |
| 186 // keyword. |
| 187 const std::wstring hint(l10n_util::GetString(IDS_OMNIBOX_EMPTY_TEXT)); |
| 188 [cell setSearchHintString:base::SysWideToNSString(hint)]; |
| 189 } else { |
| 190 // Nothing interesting to show, plain old text field. |
| 191 [cell clearKeywordAndHint]; |
| 192 } |
| 193 |
| 194 // The field needs to re-layout if the visible decoration changed. |
| 195 [field resetFieldEditorFrameIfNeeded]; |
| 196 } |
| 197 |
| 114 void LocationBarViewMac::OnChanged() { | 198 void LocationBarViewMac::OnChanged() { |
| 115 // http://crbug.com/12285 | 199 // Unfortunately, the unit-test Profile doesn't have the right stuff |
| 200 // setup to do what GetKeywordName() needs to do. So do that out |
| 201 // here where we have a Profile and pass it into OnChangedImpl(). |
| 202 const std::wstring keyword(edit_view_->model()->keyword()); |
| 203 std::wstring short_name; |
| 204 if (!keyword.empty()) { |
| 205 short_name = GetKeywordName(profile_, keyword); |
| 206 } |
| 207 |
| 208 // TODO(shess): Implementation exported to a static so that it can |
| 209 // be unit tested without having to setup the entire object. This |
| 210 // makes me sad. I should fix that. |
| 211 OnChangedImpl(field_, |
| 212 keyword, |
| 213 short_name, |
| 214 edit_view_->model()->is_keyword_hint(), |
| 215 edit_view_->model()->show_search_hint(), |
| 216 GetTabButtonImage()); |
| 116 } | 217 } |
| 117 | 218 |
| 118 void LocationBarViewMac::OnInputInProgress(bool in_progress) { | 219 void LocationBarViewMac::OnInputInProgress(bool in_progress) { |
| 119 NOTIMPLEMENTED(); | 220 NOTIMPLEMENTED(); |
| 120 } | 221 } |
| 121 | 222 |
| 122 SkBitmap LocationBarViewMac::GetFavIcon() const { | 223 SkBitmap LocationBarViewMac::GetFavIcon() const { |
| 123 NOTIMPLEMENTED(); | 224 NOTIMPLEMENTED(); |
| 124 return SkBitmap(); | 225 return SkBitmap(); |
| 125 } | 226 } |
| 126 | 227 |
| 127 std::wstring LocationBarViewMac::GetTitle() const { | 228 std::wstring LocationBarViewMac::GetTitle() const { |
| 128 NOTIMPLEMENTED(); | 229 NOTIMPLEMENTED(); |
| 129 return std::wstring(); | 230 return std::wstring(); |
| 130 } | 231 } |
| 131 | 232 |
| 132 void LocationBarViewMac::Revert() { | 233 void LocationBarViewMac::Revert() { |
| 133 edit_view_->RevertAll(); | 234 edit_view_->RevertAll(); |
| 134 } | 235 } |
| 135 | 236 |
| 136 int LocationBarViewMac::PageActionVisibleCount() { | 237 int LocationBarViewMac::PageActionVisibleCount() { |
| 137 NOTIMPLEMENTED(); | 238 NOTIMPLEMENTED(); |
| 138 return -1; | 239 return -1; |
| 139 } | 240 } |
| 241 |
| 242 NSImage* LocationBarViewMac::GetTabButtonImage() { |
| 243 if (!tab_button_image_) { |
| 244 SkBitmap* skiaBitmap = ResourceBundle::GetSharedInstance(). |
| 245 GetBitmapNamed(IDR_LOCATION_BAR_KEYWORD_HINT_TAB); |
| 246 if (skiaBitmap) { |
| 247 tab_button_image_.reset([gfx::SkBitmapToNSImage(*skiaBitmap) retain]); |
| 248 } |
| 249 } |
| 250 return tab_button_image_; |
| 251 } |
| OLD | NEW |