OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/label.h" | 5 #include "ui/views/controls/label.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <vector> | 10 #include <vector> |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 return prefsize; | 208 return prefsize; |
209 } | 209 } |
210 | 210 |
211 int Label::GetHeightForWidth(int w) { | 211 int Label::GetHeightForWidth(int w) { |
212 if (!is_multi_line_) | 212 if (!is_multi_line_) |
213 return View::GetHeightForWidth(w); | 213 return View::GetHeightForWidth(w); |
214 | 214 |
215 w = std::max(0, w - GetInsets().width()); | 215 w = std::max(0, w - GetInsets().width()); |
216 int h = font_.GetHeight(); | 216 int h = font_.GetHeight(); |
217 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, | 217 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, |
218 ComputeMultiLineFlags()); | 218 ComputeDrawStringFlags()); |
219 return h + GetInsets().height(); | 219 return h + GetInsets().height(); |
220 } | 220 } |
221 | 221 |
222 std::string Label::GetClassName() const { | 222 std::string Label::GetClassName() const { |
223 return kViewClassName; | 223 return kViewClassName; |
224 } | 224 } |
225 | 225 |
226 bool Label::HitTest(const gfx::Point& l) const { | 226 bool Label::HitTest(const gfx::Point& l) const { |
227 return false; | 227 return false; |
228 } | 228 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 if (!text_size_valid_) { | 283 if (!text_size_valid_) { |
284 // For single-line strings, we supply the largest possible width, because | 284 // For single-line strings, we supply the largest possible width, because |
285 // while adding NO_ELLIPSIS to the flags works on Windows for forcing | 285 // while adding NO_ELLIPSIS to the flags works on Windows for forcing |
286 // SizeStringInt() to calculate the desired width, it doesn't seem to work | 286 // SizeStringInt() to calculate the desired width, it doesn't seem to work |
287 // on Linux. | 287 // on Linux. |
288 int w = is_multi_line_ ? | 288 int w = is_multi_line_ ? |
289 GetAvailableRect().width() : std::numeric_limits<int>::max(); | 289 GetAvailableRect().width() : std::numeric_limits<int>::max(); |
290 int h = font_.GetHeight(); | 290 int h = font_.GetHeight(); |
291 // For single-line strings, ignore the available width and calculate how | 291 // For single-line strings, ignore the available width and calculate how |
292 // wide the text wants to be. | 292 // wide the text wants to be. |
293 int flags = ComputeMultiLineFlags(); | 293 int flags = ComputeDrawStringFlags(); |
294 if (!is_multi_line_) | 294 if (!is_multi_line_) |
295 flags |= gfx::Canvas::NO_ELLIPSIS; | 295 flags |= gfx::Canvas::NO_ELLIPSIS; |
296 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, flags); | 296 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, flags); |
297 text_size_.SetSize(w, h); | 297 text_size_.SetSize(w, h); |
298 text_size_valid_ = true; | 298 text_size_valid_ = true; |
299 } | 299 } |
300 | 300 |
301 return text_size_; | 301 return text_size_; |
302 } | 302 } |
303 | 303 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 break; | 419 break; |
420 default: | 420 default: |
421 NOTREACHED(); | 421 NOTREACHED(); |
422 break; | 422 break; |
423 } | 423 } |
424 text_origin.Offset(0, | 424 text_origin.Offset(0, |
425 std::max(0, (available_rect.height() - text_size.height())) / 2); | 425 std::max(0, (available_rect.height() - text_size.height())) / 2); |
426 return gfx::Rect(text_origin, text_size); | 426 return gfx::Rect(text_origin, text_size); |
427 } | 427 } |
428 | 428 |
429 int Label::ComputeMultiLineFlags() const { | 429 int Label::ComputeDrawStringFlags() const { |
| 430 int flags = 0; |
| 431 |
| 432 if (directionality_mode_ == AUTO_DETECT_DIRECTIONALITY) { |
| 433 base::i18n::TextDirection direction = |
| 434 base::i18n::GetFirstStrongCharacterDirection(GetText()); |
| 435 if (direction == base::i18n::RIGHT_TO_LEFT) |
| 436 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; |
| 437 else |
| 438 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; |
| 439 } |
| 440 |
430 if (!is_multi_line_) | 441 if (!is_multi_line_) |
431 return 0; | 442 return flags; |
432 | 443 |
433 int flags = gfx::Canvas::MULTI_LINE; | 444 flags |= gfx::Canvas::MULTI_LINE; |
434 #if !defined(OS_WIN) | 445 #if !defined(OS_WIN) |
435 // Don't elide multiline labels on Linux. | 446 // Don't elide multiline labels on Linux. |
436 // Todo(davemoore): Do we depend on eliding multiline text? | 447 // Todo(davemoore): Do we depend on eliding multiline text? |
437 // Pango insists on limiting the number of lines to one if text is | 448 // Pango insists on limiting the number of lines to one if text is |
438 // elided. You can get around this if you can pass a maximum height | 449 // elided. You can get around this if you can pass a maximum height |
439 // but we don't currently have that data when we call the pango code. | 450 // but we don't currently have that data when we call the pango code. |
440 flags |= gfx::Canvas::NO_ELLIPSIS; | 451 flags |= gfx::Canvas::NO_ELLIPSIS; |
441 #endif | 452 #endif |
442 if (allow_character_break_) | 453 if (allow_character_break_) |
443 flags |= gfx::Canvas::CHARACTER_BREAK; | 454 flags |= gfx::Canvas::CHARACTER_BREAK; |
444 switch (horiz_alignment_) { | 455 switch (horiz_alignment_) { |
445 case ALIGN_LEFT: | 456 case ALIGN_LEFT: |
446 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; | 457 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
447 break; | 458 break; |
448 case ALIGN_CENTER: | 459 case ALIGN_CENTER: |
449 flags |= gfx::Canvas::TEXT_ALIGN_CENTER; | 460 flags |= gfx::Canvas::TEXT_ALIGN_CENTER; |
450 break; | 461 break; |
451 case ALIGN_RIGHT: | 462 case ALIGN_RIGHT: |
452 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; | 463 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; |
453 break; | 464 break; |
454 } | 465 } |
| 466 |
455 return flags; | 467 return flags; |
456 } | 468 } |
457 | 469 |
458 gfx::Rect Label::GetAvailableRect() const { | 470 gfx::Rect Label::GetAvailableRect() const { |
459 gfx::Rect bounds(gfx::Point(), size()); | 471 gfx::Rect bounds(gfx::Point(), size()); |
460 gfx::Insets insets(GetInsets()); | 472 gfx::Insets insets(GetInsets()); |
461 bounds.Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); | 473 bounds.Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); |
462 return bounds; | 474 return bounds; |
463 } | 475 } |
464 | 476 |
(...skipping 20 matching lines...) Expand all Loading... |
485 *paint_text = base::i18n::GetDisplayStringInLTRDirectionality( | 497 *paint_text = base::i18n::GetDisplayStringInLTRDirectionality( |
486 *paint_text); | 498 *paint_text); |
487 } else if (elide_in_middle_) { | 499 } else if (elide_in_middle_) { |
488 *paint_text = ui::ElideText(text_, font_, GetAvailableRect().width(), | 500 *paint_text = ui::ElideText(text_, font_, GetAvailableRect().width(), |
489 ui::ELIDE_IN_MIDDLE); | 501 ui::ELIDE_IN_MIDDLE); |
490 } else { | 502 } else { |
491 *paint_text = text_; | 503 *paint_text = text_; |
492 } | 504 } |
493 | 505 |
494 *text_bounds = GetTextBounds(); | 506 *text_bounds = GetTextBounds(); |
495 *flags = ComputeMultiLineFlags(); | 507 *flags = ComputeDrawStringFlags(); |
496 | |
497 if (directionality_mode_ == AUTO_DETECT_DIRECTIONALITY) { | |
498 base::i18n::TextDirection direction = | |
499 base::i18n::GetFirstStrongCharacterDirection(GetText()); | |
500 if (direction == base::i18n::RIGHT_TO_LEFT) | |
501 *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; | |
502 else | |
503 *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
504 } | |
505 } | 508 } |
506 | 509 |
507 } // namespace views | 510 } // namespace views |
OLD | NEW |