OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "app/text_elider.h" | 7 #include "app/text_elider.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 bool insert_ellipsis) { | 31 bool insert_ellipsis) { |
32 const std::wstring insert(insert_ellipsis ? kEllipsis : L""); | 32 const std::wstring insert(insert_ellipsis ? kEllipsis : L""); |
33 if (!cut_in_middle) | 33 if (!cut_in_middle) |
34 return text.substr(0, length) + insert; | 34 return text.substr(0, length) + insert; |
35 // We put the extra character, if any, before the cut. | 35 // We put the extra character, if any, before the cut. |
36 const size_t half_length = length / 2; | 36 const size_t half_length = length / 2; |
37 return text.substr(0, length - half_length) + insert + | 37 return text.substr(0, length - half_length) + insert + |
38 text.substr(text.length() - half_length, half_length); | 38 text.substr(text.length() - half_length, half_length); |
39 } | 39 } |
40 | 40 |
| 41 // TODO(tony): Get rid of wstrings. |
| 42 std::wstring GetDisplayStringInLTRDirectionality(const std::wstring& text) { |
| 43 return UTF16ToWide(base::i18n::GetDisplayStringInLTRDirectionality( |
| 44 WideToUTF16(text))); |
| 45 } |
| 46 |
41 } // namespace | 47 } // namespace |
42 | 48 |
43 namespace gfx { | 49 namespace gfx { |
44 | 50 |
45 // This function takes a GURL object and elides it. It returns a string | 51 // This function takes a GURL object and elides it. It returns a string |
46 // which composed of parts from subdomain, domain, path, filename and query. | 52 // which composed of parts from subdomain, domain, path, filename and query. |
47 // A "..." is added automatically at the end if the elided string is bigger | 53 // A "..." is added automatically at the end if the elided string is bigger |
48 // than the available pixel width. For available pixel width = 0, a formatted, | 54 // than the available pixel width. For available pixel width = 0, a formatted, |
49 // but un-elided, string is returned. | 55 // but un-elided, string is returned. |
50 // | 56 // |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 284 |
279 return ElideText(final_elided_url_string, font, available_pixel_width, false); | 285 return ElideText(final_elided_url_string, font, available_pixel_width, false); |
280 } | 286 } |
281 | 287 |
282 std::wstring ElideFilename(const FilePath& filename, | 288 std::wstring ElideFilename(const FilePath& filename, |
283 const gfx::Font& font, | 289 const gfx::Font& font, |
284 int available_pixel_width) { | 290 int available_pixel_width) { |
285 int full_width = font.GetStringWidth(filename.ToWStringHack()); | 291 int full_width = font.GetStringWidth(filename.ToWStringHack()); |
286 if (full_width <= available_pixel_width) { | 292 if (full_width <= available_pixel_width) { |
287 std::wstring elided_name = filename.ToWStringHack(); | 293 std::wstring elided_name = filename.ToWStringHack(); |
288 return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); | 294 return GetDisplayStringInLTRDirectionality(elided_name); |
289 } | 295 } |
290 | 296 |
291 #if defined(OS_WIN) | 297 #if defined(OS_WIN) |
292 std::wstring extension = filename.Extension(); | 298 std::wstring extension = filename.Extension(); |
293 #elif defined(OS_POSIX) | 299 #elif defined(OS_POSIX) |
294 std::wstring extension = base::SysNativeMBToWide(filename.Extension()); | 300 std::wstring extension = base::SysNativeMBToWide(filename.Extension()); |
295 #endif | 301 #endif |
296 std::wstring rootname = | 302 std::wstring rootname = |
297 filename.BaseName().RemoveExtension().ToWStringHack(); | 303 filename.BaseName().RemoveExtension().ToWStringHack(); |
298 | 304 |
299 if (rootname.empty() || extension.empty()) { | 305 if (rootname.empty() || extension.empty()) { |
300 std::wstring elided_name = ElideText(filename.ToWStringHack(), font, | 306 std::wstring elided_name = ElideText(filename.ToWStringHack(), font, |
301 available_pixel_width, false); | 307 available_pixel_width, false); |
302 return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); | 308 return GetDisplayStringInLTRDirectionality(elided_name); |
303 } | 309 } |
304 | 310 |
305 int ext_width = font.GetStringWidth(extension); | 311 int ext_width = font.GetStringWidth(extension); |
306 int root_width = font.GetStringWidth(rootname); | 312 int root_width = font.GetStringWidth(rootname); |
307 | 313 |
308 // We may have trimmed the path. | 314 // We may have trimmed the path. |
309 if (root_width + ext_width <= available_pixel_width) { | 315 if (root_width + ext_width <= available_pixel_width) { |
310 std::wstring elided_name = rootname + extension; | 316 std::wstring elided_name = rootname + extension; |
311 return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); | 317 return GetDisplayStringInLTRDirectionality(elided_name); |
312 } | 318 } |
313 | 319 |
314 int available_root_width = available_pixel_width - ext_width; | 320 int available_root_width = available_pixel_width - ext_width; |
315 std::wstring elided_name = | 321 std::wstring elided_name = |
316 ElideText(rootname, font, available_root_width, false); | 322 ElideText(rootname, font, available_root_width, false); |
317 elided_name += extension; | 323 elided_name += extension; |
318 return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name); | 324 return GetDisplayStringInLTRDirectionality(elided_name); |
319 } | 325 } |
320 | 326 |
321 // This function adds an ellipsis at the end of the text if the text | 327 // This function adds an ellipsis at the end of the text if the text |
322 // does not fit the given pixel width. | 328 // does not fit the given pixel width. |
323 std::wstring ElideText(const std::wstring& text, | 329 std::wstring ElideText(const std::wstring& text, |
324 const gfx::Font& font, | 330 const gfx::Font& font, |
325 int available_pixel_width, | 331 int available_pixel_width, |
326 bool elide_in_middle) { | 332 bool elide_in_middle) { |
327 if (text.empty()) | 333 if (text.empty()) |
328 return text; | 334 return text; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 string16 SortedDisplayURL::AfterHost() const { | 437 string16 SortedDisplayURL::AfterHost() const { |
432 size_t slash_index = display_url_.find(sort_host_, prefix_end_); | 438 size_t slash_index = display_url_.find(sort_host_, prefix_end_); |
433 if (slash_index == string16::npos) { | 439 if (slash_index == string16::npos) { |
434 NOTREACHED(); | 440 NOTREACHED(); |
435 return string16(); | 441 return string16(); |
436 } | 442 } |
437 return display_url_.substr(slash_index + sort_host_.length()); | 443 return display_url_.substr(slash_index + sort_host_.length()); |
438 } | 444 } |
439 | 445 |
440 } // namespace gfx | 446 } // namespace gfx |
OLD | NEW |