tooltip_manager.cc revision 3551c9c881056c480085172ff9840cab31610854
1ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// Use of this source code is governed by a BSD-style license that can be 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// found in the LICENSE file. 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com#include "ui/views/widget/tooltip_manager.h" 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 7ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#include <vector> 8ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com 9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com#include "base/strings/string_split.h" 10ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#include "base/strings/utf_string_conversions.h" 11ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#include "ui/base/text/text_elider.h" 126bf38b59c9de1cd35cd091cf8766117f4e1f66a0tomhudson@google.com#include "ui/gfx/font.h" 13ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com 14f93e717c7f7ca679a80acbfda6a34013ae1e2b8djunov@google.comnamespace { 155739d2c168819394502e20cbe6071979b9c1038cbsalomon@google.com 16ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com// Maximum number of characters we allow in a tooltip. 176e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.comconst size_t kMaxTooltipLength = 1024; 18f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com 1917b78946096265d80215a6c946286ecaa35ea7edepoger@google.com// Maximum number of lines we allow in the tooltip. 206fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.comconst size_t kMaxLines = 6; 210b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com 2216e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com} // namespace 236e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com 240b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.comnamespace views { 256fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com 266fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com// static 276fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.comvoid TooltipManager::TrimTooltipToFit(string16* text, 286fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com int* max_width, 296fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com int* line_count, 300b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com int x, 310b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com int y, 32515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org gfx::NativeView context) { 336150c2051e7cff869284de8d18928b070bf01096bsalomon@google.com *max_width = 0; 340b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com *line_count = 0; 350b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com 36f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com // Clamp the tooltip length to kMaxTooltipLength so that we don't 376177e6999d23a4268ffd98dedfb1da00e272a89brobertphillips@google.com // accidentally DOS the user with a mega tooltip. 386177e6999d23a4268ffd98dedfb1da00e272a89brobertphillips@google.com if (text->length() > kMaxTooltipLength) 396177e6999d23a4268ffd98dedfb1da00e272a89brobertphillips@google.com *text = text->substr(0, kMaxTooltipLength); 40bf2a46941e8fdebfcd24ea8f7184779021898225bsalomon@google.com 41ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com // Determine the available width for the tooltip. 4289ec61e33daa9cbac200d38f7c5bb8b88046999absalomon@google.com int available_width = GetMaxWidth(x, y, context); 43ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com 44 // Split the string into at most kMaxLines lines. 45 std::vector<string16> lines; 46 base::SplitString(*text, '\n', &lines); 47 if (lines.size() > kMaxLines) 48 lines.resize(kMaxLines); 49 *line_count = static_cast<int>(lines.size()); 50 51 // Format each line to fit. 52 gfx::Font font = GetDefaultFont(); 53 string16 result; 54 for (std::vector<string16>::iterator i = lines.begin(); i != lines.end(); 55 ++i) { 56 string16 elided_text = 57 ui::ElideText(*i, font, available_width, ui::ELIDE_AT_END); 58 *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); 59 if (!result.empty()) 60 result.push_back('\n'); 61 result.append(elided_text); 62 } 63 *text = result; 64} 65 66} // namespace views 67