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