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 "ui/views/widget/tooltip_manager.h" | 5 #include "ui/views/widget/tooltip_manager.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "ui/base/text/text_elider.h" | 11 #include "ui/base/text/text_elider.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Maximum number of characters we allow in a tooltip. | 15 // Maximum number of characters we allow in a tooltip. |
16 const size_t kMaxTooltipLength = 1024; | 16 const size_t kMaxTooltipLength = 1024; |
17 | 17 |
18 // Maximum number of lines we allow in the tooltip. | 18 // Maximum number of lines we allow in the tooltip. |
19 const size_t kMaxLines = 6; | 19 const size_t kMaxLines = 6; |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 namespace views { | 23 namespace views { |
24 | 24 |
25 // static | 25 // static |
26 void TooltipManager::TrimTooltipToFit(string16* text, | 26 void TooltipManager::TrimTooltipToFit(string16* text, |
27 int* max_width, | 27 int* max_width, |
28 int* line_count, | 28 int* line_count, |
29 int x, | 29 int x, |
30 int y) { | 30 int y, |
| 31 gfx::NativeView context) { |
31 *max_width = 0; | 32 *max_width = 0; |
32 *line_count = 0; | 33 *line_count = 0; |
33 | 34 |
34 // Clamp the tooltip length to kMaxTooltipLength so that we don't | 35 // Clamp the tooltip length to kMaxTooltipLength so that we don't |
35 // accidentally DOS the user with a mega tooltip. | 36 // accidentally DOS the user with a mega tooltip. |
36 if (text->length() > kMaxTooltipLength) | 37 if (text->length() > kMaxTooltipLength) |
37 *text = text->substr(0, kMaxTooltipLength); | 38 *text = text->substr(0, kMaxTooltipLength); |
38 | 39 |
39 // Determine the available width for the tooltip. | 40 // Determine the available width for the tooltip. |
40 int available_width = GetMaxWidth(x, y); | 41 int available_width = GetMaxWidth(x, y, context); |
41 | 42 |
42 // Split the string into at most kMaxLines lines. | 43 // Split the string into at most kMaxLines lines. |
43 std::vector<string16> lines; | 44 std::vector<string16> lines; |
44 base::SplitString(*text, '\n', &lines); | 45 base::SplitString(*text, '\n', &lines); |
45 if (lines.size() > kMaxLines) | 46 if (lines.size() > kMaxLines) |
46 lines.resize(kMaxLines); | 47 lines.resize(kMaxLines); |
47 *line_count = static_cast<int>(lines.size()); | 48 *line_count = static_cast<int>(lines.size()); |
48 | 49 |
49 // Format each line to fit. | 50 // Format each line to fit. |
50 gfx::Font font = GetDefaultFont(); | 51 gfx::Font font = GetDefaultFont(); |
51 string16 result; | 52 string16 result; |
52 for (std::vector<string16>::iterator i = lines.begin(); i != lines.end(); | 53 for (std::vector<string16>::iterator i = lines.begin(); i != lines.end(); |
53 ++i) { | 54 ++i) { |
54 string16 elided_text = | 55 string16 elided_text = |
55 ui::ElideText(*i, font, available_width, ui::ELIDE_AT_END); | 56 ui::ElideText(*i, font, available_width, ui::ELIDE_AT_END); |
56 *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); | 57 *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); |
57 if (!result.empty()) | 58 if (!result.empty()) |
58 result.push_back('\n'); | 59 result.push_back('\n'); |
59 result.append(elided_text); | 60 result.append(elided_text); |
60 } | 61 } |
61 *text = result; | 62 *text = result; |
62 } | 63 } |
63 | 64 |
64 } // namespace views | 65 } // namespace views |
OLD | NEW |