Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: ui/views/controls/styled_label.cc

Issue 1140083002: Make StyledLabel wrap long words (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tests Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/controls/styled_label.h" 5 #include "ui/views/controls/styled_label.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // style may differ from the base font. The font specified by the range 274 // style may differ from the base font. The font specified by the range
275 // should be used when eliding text. 275 // should be used when eliding text.
276 if (position >= range.start()) { 276 if (position >= range.start()) {
277 text_font_list = text_font_list.DeriveWithStyle( 277 text_font_list = text_font_list.DeriveWithStyle(
278 current_range->style_info.font_style); 278 current_range->style_info.font_style);
279 } 279 }
280 gfx::ElideRectangleText(remaining_string, 280 gfx::ElideRectangleText(remaining_string,
281 text_font_list, 281 text_font_list,
282 chunk_bounds.width(), 282 chunk_bounds.width(),
283 chunk_bounds.height(), 283 chunk_bounds.height(),
284 gfx::IGNORE_LONG_WORDS, 284 gfx::WRAP_LONG_WORDS,
285 &substrings); 285 &substrings);
286 286
287 DCHECK(!substrings.empty()); 287 if (substrings.empty() || substrings[0].empty()) {
288 base::string16 chunk = substrings[0];
289 if (chunk.empty()) {
290 // Nothing fits on this line. Start a new line. 288 // Nothing fits on this line. Start a new line.
291 // If x is 0, first line may have leading whitespace that doesn't fit in a 289 // If x is 0, first line may have leading whitespace that doesn't fit in a
292 // single line, so try trimming those. Otherwise there is no room for 290 // single line, so try trimming those. Otherwise there is no room for
293 // anything; abort. 291 // anything; abort.
294 if (x == 0) { 292 if (x == 0) {
295 if (line == 0) { 293 if (line == 0) {
296 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, 294 base::TrimWhitespace(remaining_string, base::TRIM_LEADING,
297 &remaining_string); 295 &remaining_string);
298 continue; 296 continue;
299 } 297 }
300 break; 298 break;
301 } 299 }
302 300
303 x = 0; 301 x = 0;
304 line++; 302 line++;
305 continue; 303 continue;
306 } 304 }
307 305
306 base::string16 chunk = substrings[0];
307
308 scoped_ptr<Label> label; 308 scoped_ptr<Label> label;
309 if (position >= range.start()) { 309 if (position >= range.start()) {
310 const RangeStyleInfo& style_info = current_range->style_info; 310 const RangeStyleInfo& style_info = current_range->style_info;
311 311
312 if (style_info.disable_line_wrapping && chunk.size() < range.length() && 312 if (style_info.disable_line_wrapping && chunk.size() < range.length() &&
313 position == range.start() && x != 0) { 313 position == range.start() && x != 0) {
314 // If the chunk should not be wrapped, try to fit it entirely on the 314 // If the chunk should not be wrapped, try to fit it entirely on the
315 // next line. 315 // next line.
316 x = 0; 316 x = 0;
317 line++; 317 line++;
318 continue; 318 continue;
319 } 319 }
320 320
321 chunk = chunk.substr(0, std::min(chunk.size(), range.end() - position)); 321 if (chunk.size() > range.end() - position)
322 chunk = chunk.substr(0, range.end() - position);
322 323
323 label = CreateLabelRange(chunk, font_list_, style_info, this); 324 label = CreateLabelRange(chunk, font_list_, style_info, this);
324 325
325 if (style_info.is_link && !dry_run) 326 if (style_info.is_link && !dry_run)
326 link_targets_[label.get()] = range; 327 link_targets_[label.get()] = range;
327 328
328 if (position + chunk.size() >= range.end()) 329 if (position + chunk.size() >= range.end())
329 ++current_range; 330 ++current_range;
330 } else { 331 } else {
331 // This chunk is normal text. 332 // This chunk is normal text.
(...skipping 15 matching lines...) Expand all
347 label->SetBoundsRect(gfx::Rect( 348 label->SetBoundsRect(gfx::Rect(
348 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), 349 gfx::Point(GetInsets().left() + x - focus_border_insets.left(),
349 GetInsets().top() + line * line_height - 350 GetInsets().top() + line * line_height -
350 focus_border_insets.top()), 351 focus_border_insets.top()),
351 view_size)); 352 view_size));
352 AddChildView(label.release()); 353 AddChildView(label.release());
353 } 354 }
354 x += view_size.width() - focus_border_insets.width(); 355 x += view_size.width() - focus_border_insets.width();
355 used_width = std::max(used_width, x); 356 used_width = std::max(used_width, x);
356 357
358 // If |gfx::ElideRectangleText| returned more than one substring, that
359 // means the whole text did not fit into remaining line width, with text
360 // after |susbtring[0]| spilling into next line. If whole |substring[0]|
361 // was added to the current line (this may not be the case if part of the
362 // substring has different style), proceed to the next line.
363 if (substrings.size() > 1 && chunk.size() == substrings[0].size()) {
364 x = 0;
365 ++line;
366 }
367
357 remaining_string = remaining_string.substr(chunk.size()); 368 remaining_string = remaining_string.substr(chunk.size());
358 } 369 }
359 370
360 DCHECK_LE(used_width, width); 371 DCHECK_LE(used_width, width);
361 // The user-specified line height only applies to interline spacing, so the 372 // The user-specified line height only applies to interline spacing, so the
362 // final line's height is unaffected. 373 // final line's height is unaffected.
363 int total_height = line * line_height + 374 int total_height = line * line_height +
364 CalculateLineHeight(font_list_) + GetInsets().height(); 375 CalculateLineHeight(font_list_) + GetInsets().height();
365 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height); 376 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height);
366 return calculated_size_; 377 return calculated_size_;
367 } 378 }
368 379
369 } // namespace views 380 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/controls/styled_label_unittest.cc » ('j') | ui/views/controls/styled_label_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698