| OLD | NEW |
| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 int total_height = 0; | 252 int total_height = 0; |
| 253 // The width that was actually used. Guaranteed to be no larger than |width|. | 253 // The width that was actually used. Guaranteed to be no larger than |width|. |
| 254 int used_width = 0; | 254 int used_width = 0; |
| 255 | 255 |
| 256 base::string16 remaining_string = text_; | 256 base::string16 remaining_string = text_; |
| 257 StyleRanges::const_iterator current_range = style_ranges_.begin(); | 257 StyleRanges::const_iterator current_range = style_ranges_.begin(); |
| 258 | 258 |
| 259 // Iterate over the text, creating a bunch of labels and links and laying them | 259 // Iterate over the text, creating a bunch of labels and links and laying them |
| 260 // out in the appropriate positions. | 260 // out in the appropriate positions. |
| 261 while (!remaining_string.empty()) { | 261 while (!remaining_string.empty()) { |
| 262 // Don't put whitespace at beginning of a line with an exception for the | |
| 263 // first line (so the text's leading whitespace is respected). | |
| 264 if (x == 0 && line > 0) { | 262 if (x == 0 && line > 0) { |
| 265 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, | 263 if (remaining_string.front() == L'\n') { |
| 266 &remaining_string); | 264 // wrapped to the next line on \n, remove it. Other whitespaces, |
| 265 // eg, spaces to indent next line, are preserved. |
| 266 remaining_string.erase(0, 1); |
| 267 } else { |
| 268 // wrapped on whitespace character or characters in the middle of the |
| 269 // line - none of them are needed at the beginning of the next line |
| 270 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, |
| 271 &remaining_string); |
| 272 } |
| 267 } | 273 } |
| 268 | 274 |
| 269 gfx::Range range(gfx::Range::InvalidRange()); | 275 gfx::Range range(gfx::Range::InvalidRange()); |
| 270 if (current_range != style_ranges_.end()) | 276 if (current_range != style_ranges_.end()) |
| 271 range = current_range->range; | 277 range = current_range->range; |
| 272 | 278 |
| 273 const size_t position = text_.size() - remaining_string.size(); | 279 const size_t position = text_.size() - remaining_string.size(); |
| 274 | 280 |
| 275 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height); | 281 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height); |
| 276 std::vector<base::string16> substrings; | 282 std::vector<base::string16> substrings; |
| 277 gfx::FontList text_font_list = font_list_; | 283 gfx::FontList text_font_list = font_list_; |
| 278 // If the start of the remaining text is inside a styled range, the font | 284 // If the start of the remaining text is inside a styled range, the font |
| 279 // style may differ from the base font. The font specified by the range | 285 // style may differ from the base font. The font specified by the range |
| 280 // should be used when eliding text. | 286 // should be used when eliding text. |
| 281 if (position >= range.start()) { | 287 if (position >= range.start()) { |
| 282 text_font_list = | 288 text_font_list = |
| 283 text_font_list.Derive(0, current_range->style_info.font_style, | 289 text_font_list.Derive(0, current_range->style_info.font_style, |
| 284 current_range->style_info.weight); | 290 current_range->style_info.weight); |
| 285 } | 291 } |
| 286 gfx::ElideRectangleText(remaining_string, | 292 gfx::ElideRectangleText(remaining_string, |
| 287 text_font_list, | 293 text_font_list, |
| 288 chunk_bounds.width(), | 294 chunk_bounds.width(), |
| 289 chunk_bounds.height(), | 295 chunk_bounds.height(), |
| 290 gfx::WRAP_LONG_WORDS, | 296 gfx::WRAP_LONG_WORDS, |
| 291 &substrings); | 297 &substrings); |
| 292 | 298 |
| 293 if (substrings.empty() || substrings[0].empty()) { | 299 if (substrings.empty() || substrings[0].empty()) { |
| 294 // Nothing fits on this line. Start a new line. | 300 // Nothing fits on this line. Start a new line. |
| 295 // If x is 0, first line may have leading whitespace that doesn't fit in a | 301 // If x is 0, first line may have leading whitespace that doesn't fit in a |
| 296 // single line, so try trimming those. Otherwise there is no room for | 302 // single line, so try trimming those. When first remaining char is \n, |
| 297 // anything; abort. | 303 // otput it as empty line. Otherwise there is no room for anything; abort. |
| 298 if (x == 0) { | 304 if (x == 0) { |
| 299 if (line == 0) { | 305 if (line == 0) { |
| 300 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, | 306 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, |
| 301 &remaining_string); | 307 &remaining_string); |
| 302 continue; | 308 continue; |
| 309 } else if (!remaining_string.empty() && |
| 310 remaining_string.front() == L'\n') { |
| 311 line++; |
| 312 continue; |
| 303 } | 313 } |
| 304 break; | 314 break; |
| 305 } | 315 } |
| 306 | 316 |
| 307 x = 0; | 317 x = 0; |
| 308 line++; | 318 line++; |
| 309 continue; | 319 continue; |
| 310 } | 320 } |
| 311 | 321 |
| 312 base::string16 chunk = substrings[0]; | 322 base::string16 chunk = substrings[0]; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 383 |
| 374 remaining_string = remaining_string.substr(chunk.size()); | 384 remaining_string = remaining_string.substr(chunk.size()); |
| 375 } | 385 } |
| 376 | 386 |
| 377 DCHECK_LE(used_width, width); | 387 DCHECK_LE(used_width, width); |
| 378 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height); | 388 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height); |
| 379 return calculated_size_; | 389 return calculated_size_; |
| 380 } | 390 } |
| 381 | 391 |
| 382 } // namespace views | 392 } // namespace views |
| OLD | NEW |