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