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