OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "views/widget/tooltip_manager.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "app/gfx/text_elider.h" |
| 10 #include "views/screen.h" |
| 11 |
| 12 namespace views { |
| 13 |
| 14 // Maximum number of characters we allow in a tooltip. |
| 15 static const size_t kMaxTooltipLength = 1024; |
| 16 |
| 17 // Maximum number of lines we allow in the tooltip. |
| 18 static const size_t kMaxLines = 6; |
| 19 |
| 20 // Breaks |text| along line boundaries, placing each line of text into lines. |
| 21 static void SplitTooltipString(const std::wstring& text, |
| 22 std::vector<std::wstring>* lines) { |
| 23 size_t index = 0; |
| 24 size_t next_index; |
| 25 while ((next_index = text.find(TooltipManager::GetLineSeparator(), index)) |
| 26 != std::wstring::npos && lines->size() < kMaxLines) { |
| 27 lines->push_back(text.substr(index, next_index - index)); |
| 28 index = next_index + TooltipManager::GetLineSeparator().size(); |
| 29 } |
| 30 if (next_index != text.size() && lines->size() < kMaxLines) |
| 31 lines->push_back(text.substr(index, text.size() - index)); |
| 32 } |
| 33 |
| 34 // static |
| 35 int TooltipManager::GetMaxWidth(int x, int y) { |
| 36 gfx::Rect monitor_bounds = |
| 37 Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); |
| 38 // We don't want the tooltip to get too big, otherwise it looks wrong. |
| 39 return monitor_bounds.width() == 0 ? 400 : monitor_bounds.width() / 4; |
| 40 } |
| 41 |
| 42 // static |
| 43 void TooltipManager::TrimTooltipToFit(std::wstring* text, |
| 44 int* max_width, |
| 45 int* line_count, |
| 46 int x, |
| 47 int y) { |
| 48 *max_width = 0; |
| 49 *line_count = 0; |
| 50 |
| 51 // Clamp the tooltip length to kMaxTooltipLength so that we don't |
| 52 // accidentally DOS the user with a mega tooltip. |
| 53 if (text->length() > kMaxTooltipLength) |
| 54 *text = text->substr(0, kMaxTooltipLength); |
| 55 |
| 56 // Determine the available width for the tooltip. |
| 57 int available_width = GetMaxWidth(x, y); |
| 58 |
| 59 // Split the string. |
| 60 std::vector<std::wstring> lines; |
| 61 SplitTooltipString(*text, &lines); |
| 62 *line_count = static_cast<int>(lines.size()); |
| 63 |
| 64 // Format each line to fit. |
| 65 gfx::Font font = GetDefaultFont(); |
| 66 std::wstring result; |
| 67 for (std::vector<std::wstring>::iterator i = lines.begin(); i != lines.end(); |
| 68 ++i) { |
| 69 std::wstring elided_text = gfx::ElideText(*i, font, available_width); |
| 70 *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); |
| 71 if (i == lines.begin() && i + 1 == lines.end()) { |
| 72 *text = elided_text; |
| 73 return; |
| 74 } |
| 75 if (!result.empty()) |
| 76 result.append(GetLineSeparator()); |
| 77 result.append(elided_text); |
| 78 } |
| 79 *text = result; |
| 80 } |
| 81 |
| 82 } // namespace views |
OLD | NEW |