| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 61 } |
| 62 | 62 |
| 63 void Label::SetText(const base::string16& text) { | 63 void Label::SetText(const base::string16& text) { |
| 64 if (text != text_) | 64 if (text != text_) |
| 65 SetTextInternal(text); | 65 SetTextInternal(text); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void Label::SetTextInternal(const base::string16& text) { | 68 void Label::SetTextInternal(const base::string16& text) { |
| 69 text_ = text; | 69 text_ = text; |
| 70 | 70 |
| 71 if (is_obscured_) { | 71 if (obscured_) { |
| 72 size_t obscured_text_length = | 72 size_t obscured_text_length = |
| 73 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, text_.length())); | 73 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, text_.length())); |
| 74 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); | 74 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); |
| 75 } else { | 75 } else { |
| 76 layout_text_ = text_; | 76 layout_text_ = text_; |
| 77 } | 77 } |
| 78 | 78 |
| 79 ResetCachedSize(); | 79 ResetCachedSize(); |
| 80 PreferredSizeChanged(); | 80 PreferredSizeChanged(); |
| 81 SchedulePaint(); | 81 SchedulePaint(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 97 disabled_color_set_ = true; | 97 disabled_color_set_ = true; |
| 98 RecalculateColors(); | 98 RecalculateColors(); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void Label::SetBackgroundColor(SkColor color) { | 101 void Label::SetBackgroundColor(SkColor color) { |
| 102 background_color_ = color; | 102 background_color_ = color; |
| 103 background_color_set_ = true; | 103 background_color_set_ = true; |
| 104 RecalculateColors(); | 104 RecalculateColors(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void Label::SetShadows(const gfx::ShadowValues& shadows) { |
| 108 shadows_ = shadows; |
| 109 text_size_valid_ = false; |
| 110 } |
| 111 |
| 112 void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) { |
| 113 subpixel_rendering_enabled_ = subpixel_rendering_enabled; |
| 114 } |
| 115 |
| 107 void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { | 116 void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { |
| 108 // If the UI layout is right-to-left, flip the alignment direction. | 117 // If the UI layout is right-to-left, flip the alignment direction. |
| 109 if (base::i18n::IsRTL() && | 118 if (base::i18n::IsRTL() && |
| 110 (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) { | 119 (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) { |
| 111 alignment = (alignment == gfx::ALIGN_LEFT) ? | 120 alignment = (alignment == gfx::ALIGN_LEFT) ? |
| 112 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; | 121 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| 113 } | 122 } |
| 114 if (horizontal_alignment_ != alignment) { | 123 if (horizontal_alignment_ != alignment) { |
| 115 horizontal_alignment_ = alignment; | 124 horizontal_alignment_ = alignment; |
| 116 SchedulePaint(); | 125 SchedulePaint(); |
| 117 } | 126 } |
| 118 } | 127 } |
| 119 | 128 |
| 120 gfx::HorizontalAlignment Label::GetHorizontalAlignment() const { | 129 gfx::HorizontalAlignment Label::GetHorizontalAlignment() const { |
| 121 if (horizontal_alignment_ != gfx::ALIGN_TO_HEAD) | 130 if (horizontal_alignment_ != gfx::ALIGN_TO_HEAD) |
| 122 return horizontal_alignment_; | 131 return horizontal_alignment_; |
| 123 | 132 |
| 124 const base::i18n::TextDirection dir = | 133 const base::i18n::TextDirection dir = |
| 125 base::i18n::GetFirstStrongCharacterDirection(layout_text()); | 134 base::i18n::GetFirstStrongCharacterDirection(layout_text_); |
| 126 return dir == base::i18n::RIGHT_TO_LEFT ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; | 135 return dir == base::i18n::RIGHT_TO_LEFT ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| 127 } | 136 } |
| 128 | 137 |
| 129 void Label::SetLineHeight(int height) { | 138 void Label::SetLineHeight(int height) { |
| 130 if (height != line_height_) { | 139 if (height != line_height_) { |
| 131 line_height_ = height; | 140 line_height_ = height; |
| 132 ResetCachedSize(); | 141 ResetCachedSize(); |
| 133 PreferredSizeChanged(); | 142 PreferredSizeChanged(); |
| 134 SchedulePaint(); | 143 SchedulePaint(); |
| 135 } | 144 } |
| 136 } | 145 } |
| 137 | 146 |
| 138 void Label::SetMultiLine(bool multi_line) { | 147 void Label::SetMultiLine(bool multi_line) { |
| 139 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL || | 148 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL || |
| 140 elide_behavior_ == gfx::NO_ELIDE)); | 149 elide_behavior_ == gfx::NO_ELIDE)); |
| 141 if (multi_line != is_multi_line_) { | 150 if (multi_line != multi_line_) { |
| 142 is_multi_line_ = multi_line; | 151 multi_line_ = multi_line; |
| 143 ResetCachedSize(); | 152 ResetCachedSize(); |
| 144 PreferredSizeChanged(); | 153 PreferredSizeChanged(); |
| 145 SchedulePaint(); | 154 SchedulePaint(); |
| 146 } | 155 } |
| 147 } | 156 } |
| 148 | 157 |
| 149 void Label::SetObscured(bool obscured) { | 158 void Label::SetObscured(bool obscured) { |
| 150 if (obscured != is_obscured_) { | 159 if (obscured != obscured_) { |
| 151 is_obscured_ = obscured; | 160 obscured_ = obscured; |
| 152 SetTextInternal(text_); | 161 SetTextInternal(text_); |
| 153 } | 162 } |
| 154 } | 163 } |
| 155 | 164 |
| 156 void Label::SetAllowCharacterBreak(bool allow_character_break) { | 165 void Label::SetAllowCharacterBreak(bool allow_character_break) { |
| 157 if (allow_character_break != allow_character_break_) { | 166 if (allow_character_break != allow_character_break_) { |
| 158 allow_character_break_ = allow_character_break; | 167 allow_character_break_ = allow_character_break; |
| 159 ResetCachedSize(); | 168 ResetCachedSize(); |
| 160 PreferredSizeChanged(); | 169 PreferredSizeChanged(); |
| 161 SchedulePaint(); | 170 SchedulePaint(); |
| 162 } | 171 } |
| 163 } | 172 } |
| 164 | 173 |
| 165 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) { | 174 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) { |
| 166 DCHECK(!is_multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL || | 175 DCHECK(!multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL || |
| 167 elide_behavior_ == gfx::NO_ELIDE)); | 176 elide_behavior_ == gfx::NO_ELIDE)); |
| 168 if (elide_behavior != elide_behavior_) { | 177 if (elide_behavior != elide_behavior_) { |
| 169 elide_behavior_ = elide_behavior; | 178 elide_behavior_ = elide_behavior; |
| 170 ResetCachedSize(); | 179 ResetCachedSize(); |
| 171 PreferredSizeChanged(); | 180 PreferredSizeChanged(); |
| 172 SchedulePaint(); | 181 SchedulePaint(); |
| 173 } | 182 } |
| 174 } | 183 } |
| 175 | 184 |
| 176 void Label::SetTooltipText(const base::string16& tooltip_text) { | 185 void Label::SetTooltipText(const base::string16& tooltip_text) { |
| 177 tooltip_text_ = tooltip_text; | 186 tooltip_text_ = tooltip_text; |
| 178 } | 187 } |
| 179 | 188 |
| 180 void Label::SizeToFit(int max_width) { | 189 void Label::SizeToFit(int max_width) { |
| 181 DCHECK(is_multi_line_); | 190 DCHECK(multi_line_); |
| 182 | 191 |
| 183 std::vector<base::string16> lines; | 192 std::vector<base::string16> lines; |
| 184 base::SplitString(layout_text(), '\n', &lines); | 193 base::SplitString(layout_text_, '\n', &lines); |
| 185 | 194 |
| 186 int label_width = 0; | 195 int label_width = 0; |
| 187 for (std::vector<base::string16>::const_iterator iter = lines.begin(); | 196 for (std::vector<base::string16>::const_iterator iter = lines.begin(); |
| 188 iter != lines.end(); ++iter) { | 197 iter != lines.end(); ++iter) { |
| 189 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); | 198 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); |
| 190 } | 199 } |
| 191 | 200 |
| 192 label_width += GetInsets().width(); | 201 label_width += GetInsets().width(); |
| 193 | 202 |
| 194 if (max_width > 0) | 203 if (max_width > 0) |
| 195 label_width = std::min(label_width, max_width); | 204 label_width = std::min(label_width, max_width); |
| 196 | 205 |
| 197 SetBounds(x(), y(), label_width, 0); | 206 SetBounds(x(), y(), label_width, 0); |
| 198 SizeToPreferredSize(); | 207 SizeToPreferredSize(); |
| 199 } | 208 } |
| 200 | 209 |
| 210 const base::string16& Label::GetLayoutTextForTesting() const { |
| 211 return layout_text_; |
| 212 } |
| 213 |
| 201 gfx::Insets Label::GetInsets() const { | 214 gfx::Insets Label::GetInsets() const { |
| 202 gfx::Insets insets = View::GetInsets(); | 215 gfx::Insets insets = View::GetInsets(); |
| 203 if (focusable()) { | 216 if (focusable()) { |
| 204 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, | 217 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, |
| 205 kFocusBorderPadding, kFocusBorderPadding); | 218 kFocusBorderPadding, kFocusBorderPadding); |
| 206 } | 219 } |
| 207 return insets; | 220 return insets; |
| 208 } | 221 } |
| 209 | 222 |
| 210 int Label::GetBaseline() const { | 223 int Label::GetBaseline() const { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 234 gfx::Size size(gfx::GetStringWidth(base::string16(gfx::kEllipsisUTF16), | 247 gfx::Size size(gfx::GetStringWidth(base::string16(gfx::kEllipsisUTF16), |
| 235 font_list_), | 248 font_list_), |
| 236 font_list_.GetHeight()); | 249 font_list_.GetHeight()); |
| 237 size.SetToMin(text_size); // The actual text may be shorter than an ellipsis. | 250 size.SetToMin(text_size); // The actual text may be shorter than an ellipsis. |
| 238 gfx::Insets insets = GetInsets(); | 251 gfx::Insets insets = GetInsets(); |
| 239 size.Enlarge(insets.width(), insets.height()); | 252 size.Enlarge(insets.width(), insets.height()); |
| 240 return size; | 253 return size; |
| 241 } | 254 } |
| 242 | 255 |
| 243 int Label::GetHeightForWidth(int w) const { | 256 int Label::GetHeightForWidth(int w) const { |
| 244 if (!is_multi_line_) | 257 if (!multi_line_) |
| 245 return View::GetHeightForWidth(w); | 258 return View::GetHeightForWidth(w); |
| 246 | 259 |
| 247 w = std::max(0, w - GetInsets().width()); | 260 w = std::max(0, w - GetInsets().width()); |
| 248 | 261 |
| 249 for (size_t i = 0; i < cached_heights_.size(); ++i) { | 262 for (size_t i = 0; i < cached_heights_.size(); ++i) { |
| 250 const gfx::Size& s = cached_heights_[i]; | 263 const gfx::Size& s = cached_heights_[i]; |
| 251 if (s.width() == w) | 264 if (s.width() == w) |
| 252 return s.height() + GetInsets().height(); | 265 return s.height() + GetInsets().height(); |
| 253 } | 266 } |
| 254 | 267 |
| 255 int cache_width = w; | 268 int cache_width = w; |
| 256 | 269 |
| 257 int h = font_list_.GetHeight(); | 270 int h = font_list_.GetHeight(); |
| 258 const int flags = ComputeDrawStringFlags(); | 271 const int flags = ComputeDrawStringFlags(); |
| 259 gfx::Canvas::SizeStringInt( | 272 gfx::Canvas::SizeStringInt( |
| 260 layout_text(), font_list_, &w, &h, line_height_, flags); | 273 layout_text_, font_list_, &w, &h, line_height_, flags); |
| 261 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); | 274 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); |
| 262 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; | 275 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; |
| 263 return h + GetInsets().height(); | 276 return h + GetInsets().height(); |
| 264 } | 277 } |
| 265 | 278 |
| 266 const char* Label::GetClassName() const { | 279 const char* Label::GetClassName() const { |
| 267 return kViewClassName; | 280 return kViewClassName; |
| 268 } | 281 } |
| 269 | 282 |
| 270 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { | 283 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { |
| 271 // Bail out if the label does not contain the point. | |
| 272 // Note that HitTestPoint() cannot be used here as it uses | |
| 273 // Label::HitTestRect() to determine if the point hits the label; and | |
| 274 // Label::HitTestRect() always fails. Instead, default HitTestRect() | |
| 275 // implementation should be used. | |
| 276 if (!View::HitTestRect(gfx::Rect(point, gfx::Size(1, 1)))) | |
| 277 return NULL; | |
| 278 | |
| 279 if (tooltip_text_.empty() && !ShouldShowDefaultTooltip()) | 284 if (tooltip_text_.empty() && !ShouldShowDefaultTooltip()) |
| 280 return NULL; | 285 return NULL; |
| 281 | 286 |
| 282 return this; | 287 return HitTestPoint(point) ? this : NULL; |
| 283 } | 288 } |
| 284 | 289 |
| 285 bool Label::CanProcessEventsWithinSubtree() const { | 290 bool Label::CanProcessEventsWithinSubtree() const { |
| 286 // Send events to the parent view for handling. | 291 // Send events to the parent view for handling. |
| 287 return false; | 292 return false; |
| 288 } | 293 } |
| 289 | 294 |
| 295 void Label::GetAccessibleState(ui::AXViewState* state) { |
| 296 state->role = ui::AX_ROLE_STATIC_TEXT; |
| 297 state->AddStateFlag(ui::AX_STATE_READ_ONLY); |
| 298 state->name = layout_text_; |
| 299 } |
| 300 |
| 290 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { | 301 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { |
| 291 DCHECK(tooltip); | |
| 292 | |
| 293 // If a tooltip has been explicitly set, use it. | |
| 294 if (!tooltip_text_.empty()) { | 302 if (!tooltip_text_.empty()) { |
| 295 tooltip->assign(tooltip_text_); | 303 tooltip->assign(tooltip_text_); |
| 296 return true; | 304 return true; |
| 297 } | 305 } |
| 298 | 306 |
| 299 // Show the full text if the text does not fit. | |
| 300 if (ShouldShowDefaultTooltip()) { | 307 if (ShouldShowDefaultTooltip()) { |
| 301 *tooltip = layout_text(); | 308 *tooltip = layout_text_; |
| 302 return true; | 309 return true; |
| 303 } | 310 } |
| 304 | 311 |
| 305 return false; | 312 return false; |
| 306 } | 313 } |
| 307 | 314 |
| 308 void Label::GetAccessibleState(ui::AXViewState* state) { | |
| 309 state->role = ui::AX_ROLE_STATIC_TEXT; | |
| 310 state->AddStateFlag(ui::AX_STATE_READ_ONLY); | |
| 311 state->name = layout_text(); | |
| 312 } | |
| 313 | |
| 314 void Label::PaintText(gfx::Canvas* canvas, | 315 void Label::PaintText(gfx::Canvas* canvas, |
| 315 const base::string16& text, | 316 const base::string16& text, |
| 316 const gfx::Rect& text_bounds, | 317 const gfx::Rect& text_bounds, |
| 317 int flags) { | 318 int flags) { |
| 318 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; | 319 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; |
| 319 if (elide_behavior_ == gfx::FADE_TAIL) { | 320 if (elide_behavior_ == gfx::FADE_TAIL) { |
| 320 canvas->DrawFadedString(text, font_list_, color, text_bounds, flags); | 321 canvas->DrawFadedString(text, font_list_, color, text_bounds, flags); |
| 321 } else { | 322 } else { |
| 322 canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds, | 323 canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds, |
| 323 line_height_, flags, shadows_); | 324 line_height_, flags, shadows_); |
| 324 } | 325 } |
| 325 | 326 |
| 326 if (HasFocus()) { | 327 if (HasFocus()) { |
| 327 gfx::Rect focus_bounds = text_bounds; | 328 gfx::Rect focus_bounds = text_bounds; |
| 328 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | 329 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| 329 canvas->DrawFocusRect(focus_bounds); | 330 canvas->DrawFocusRect(focus_bounds); |
| 330 } | 331 } |
| 331 } | 332 } |
| 332 | 333 |
| 333 gfx::Size Label::GetTextSize() const { | 334 gfx::Size Label::GetTextSize() const { |
| 334 if (!text_size_valid_) { | 335 if (!text_size_valid_) { |
| 335 // For single-line strings, we supply the largest possible width, because | 336 // For single-line strings, we supply the largest possible width, because |
| 336 // while adding NO_ELLIPSIS to the flags works on Windows for forcing | 337 // while adding NO_ELLIPSIS to the flags works on Windows for forcing |
| 337 // SizeStringInt() to calculate the desired width, it doesn't seem to work | 338 // SizeStringInt() to calculate the desired width, it doesn't seem to work |
| 338 // on Linux. | 339 // on Linux. |
| 339 int w = is_multi_line_ ? | 340 int w = multi_line_ ? |
| 340 GetAvailableRect().width() : std::numeric_limits<int>::max(); | 341 GetAvailableRect().width() : std::numeric_limits<int>::max(); |
| 341 int h = font_list_.GetHeight(); | 342 int h = font_list_.GetHeight(); |
| 342 // For single-line strings, ignore the available width and calculate how | 343 // For single-line strings, ignore the available width and calculate how |
| 343 // wide the text wants to be. | 344 // wide the text wants to be. |
| 344 int flags = ComputeDrawStringFlags(); | 345 int flags = ComputeDrawStringFlags(); |
| 345 if (!is_multi_line_) | 346 if (!multi_line_) |
| 346 flags |= gfx::Canvas::NO_ELLIPSIS; | 347 flags |= gfx::Canvas::NO_ELLIPSIS; |
| 347 gfx::Canvas::SizeStringInt( | 348 gfx::Canvas::SizeStringInt( |
| 348 layout_text(), font_list_, &w, &h, line_height_, flags); | 349 layout_text_, font_list_, &w, &h, line_height_, flags); |
| 349 text_size_.SetSize(w, h); | 350 text_size_.SetSize(w, h); |
| 350 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows_); | 351 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows_); |
| 351 text_size_.Enlarge(shadow_margin.width(), shadow_margin.height()); | 352 text_size_.Enlarge(shadow_margin.width(), shadow_margin.height()); |
| 352 text_size_valid_ = true; | 353 text_size_valid_ = true; |
| 353 } | 354 } |
| 354 | 355 |
| 355 return text_size_; | 356 return text_size_; |
| 356 } | 357 } |
| 357 | 358 |
| 358 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 359 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 359 text_size_valid_ &= !is_multi_line_; | 360 text_size_valid_ &= !multi_line_; |
| 360 } | 361 } |
| 361 | 362 |
| 362 void Label::OnPaint(gfx::Canvas* canvas) { | 363 void Label::OnPaint(gfx::Canvas* canvas) { |
| 363 OnPaintBackground(canvas); | 364 OnPaintBackground(canvas); |
| 364 // We skip painting the focus border because it is being handled seperately by | 365 // We skip painting the focus border because it is being handled seperately by |
| 365 // some subclasses of Label. We do not want View's focus border painting to | 366 // some subclasses of Label. We do not want View's focus border painting to |
| 366 // interfere with that. | 367 // interfere with that. |
| 367 OnPaintBorder(canvas); | 368 OnPaintBorder(canvas); |
| 368 | 369 |
| 369 base::string16 paint_text; | 370 base::string16 paint_text; |
| 370 gfx::Rect text_bounds; | 371 gfx::Rect text_bounds; |
| 371 int flags = 0; | 372 int flags = 0; |
| 372 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 373 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 373 PaintText(canvas, paint_text, text_bounds, flags); | 374 PaintText(canvas, paint_text, text_bounds, flags); |
| 374 } | 375 } |
| 375 | 376 |
| 376 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 377 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 377 UpdateColorsFromTheme(theme); | 378 UpdateColorsFromTheme(theme); |
| 378 } | 379 } |
| 379 | 380 |
| 380 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { | 381 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { |
| 381 font_list_ = font_list; | 382 font_list_ = font_list; |
| 382 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; | 383 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; |
| 383 subpixel_rendering_enabled_ = true; | 384 subpixel_rendering_enabled_ = true; |
| 384 auto_color_readability_ = true; | 385 auto_color_readability_ = true; |
| 385 UpdateColorsFromTheme(ui::NativeTheme::instance()); | 386 UpdateColorsFromTheme(ui::NativeTheme::instance()); |
| 386 horizontal_alignment_ = gfx::ALIGN_CENTER; | 387 horizontal_alignment_ = gfx::ALIGN_CENTER; |
| 387 line_height_ = 0; | 388 line_height_ = 0; |
| 388 is_multi_line_ = false; | 389 multi_line_ = false; |
| 389 is_obscured_ = false; | 390 obscured_ = false; |
| 390 allow_character_break_ = false; | 391 allow_character_break_ = false; |
| 391 elide_behavior_ = gfx::ELIDE_TAIL; | 392 elide_behavior_ = gfx::ELIDE_TAIL; |
| 392 collapse_when_hidden_ = false; | 393 collapse_when_hidden_ = false; |
| 393 directionality_mode_ = gfx::DIRECTIONALITY_FROM_UI; | |
| 394 cached_heights_.resize(kCachedSizeLimit); | 394 cached_heights_.resize(kCachedSizeLimit); |
| 395 ResetCachedSize(); | 395 ResetCachedSize(); |
| 396 | 396 |
| 397 SetText(text); | 397 SetText(text); |
| 398 } | 398 } |
| 399 | 399 |
| 400 void Label::RecalculateColors() { | 400 void Label::RecalculateColors() { |
| 401 actual_enabled_color_ = auto_color_readability_ ? | 401 actual_enabled_color_ = auto_color_readability_ ? |
| 402 color_utils::GetReadableColor(requested_enabled_color_, | 402 color_utils::GetReadableColor(requested_enabled_color_, |
| 403 background_color_) : | 403 background_color_) : |
| (...skipping 28 matching lines...) Expand all Loading... |
| 432 return gfx::Rect(origin, text_size); | 432 return gfx::Rect(origin, text_size); |
| 433 } | 433 } |
| 434 | 434 |
| 435 int Label::ComputeDrawStringFlags() const { | 435 int Label::ComputeDrawStringFlags() const { |
| 436 int flags = 0; | 436 int flags = 0; |
| 437 | 437 |
| 438 // We can't use subpixel rendering if the background is non-opaque. | 438 // We can't use subpixel rendering if the background is non-opaque. |
| 439 if (SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_) | 439 if (SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_) |
| 440 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING; | 440 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING; |
| 441 | 441 |
| 442 if (directionality_mode_ == gfx::DIRECTIONALITY_FORCE_LTR) { | 442 base::i18n::TextDirection direction = |
| 443 base::i18n::GetFirstStrongCharacterDirection(layout_text_); |
| 444 if (direction == base::i18n::RIGHT_TO_LEFT) |
| 445 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; |
| 446 else |
| 443 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | 447 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; |
| 444 } else if (directionality_mode_ == gfx::DIRECTIONALITY_FORCE_RTL) { | |
| 445 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; | |
| 446 } else if (directionality_mode_ == gfx::DIRECTIONALITY_FROM_TEXT) { | |
| 447 base::i18n::TextDirection direction = | |
| 448 base::i18n::GetFirstStrongCharacterDirection(layout_text()); | |
| 449 if (direction == base::i18n::RIGHT_TO_LEFT) | |
| 450 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; | |
| 451 else | |
| 452 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
| 453 } | |
| 454 | 448 |
| 455 switch (GetHorizontalAlignment()) { | 449 switch (GetHorizontalAlignment()) { |
| 456 case gfx::ALIGN_LEFT: | 450 case gfx::ALIGN_LEFT: |
| 457 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; | 451 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
| 458 break; | 452 break; |
| 459 case gfx::ALIGN_CENTER: | 453 case gfx::ALIGN_CENTER: |
| 460 flags |= gfx::Canvas::TEXT_ALIGN_CENTER; | 454 flags |= gfx::Canvas::TEXT_ALIGN_CENTER; |
| 461 break; | 455 break; |
| 462 case gfx::ALIGN_RIGHT: | 456 case gfx::ALIGN_RIGHT: |
| 463 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; | 457 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 464 break; | 458 break; |
| 465 default: | 459 default: |
| 466 NOTREACHED(); | 460 NOTREACHED(); |
| 467 break; | 461 break; |
| 468 } | 462 } |
| 469 | 463 |
| 470 if (!is_multi_line_) | 464 if (!multi_line_) |
| 471 return flags; | 465 return flags; |
| 472 | 466 |
| 473 flags |= gfx::Canvas::MULTI_LINE; | 467 flags |= gfx::Canvas::MULTI_LINE; |
| 474 #if !defined(OS_WIN) | 468 #if !defined(OS_WIN) |
| 475 // Don't elide multiline labels on Linux. | 469 // Don't elide multiline labels on Linux. |
| 476 // Todo(davemoore): Do we depend on eliding multiline text? | 470 // Todo(davemoore): Do we depend on eliding multiline text? |
| 477 // Pango insists on limiting the number of lines to one if text is | 471 // Pango insists on limiting the number of lines to one if text is |
| 478 // elided. You can get around this if you can pass a maximum height | 472 // elided. You can get around this if you can pass a maximum height |
| 479 // but we don't currently have that data when we call the pango code. | 473 // but we don't currently have that data when we call the pango code. |
| 480 flags |= gfx::Canvas::NO_ELLIPSIS; | 474 flags |= gfx::Canvas::NO_ELLIPSIS; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 491 return bounds; | 485 return bounds; |
| 492 } | 486 } |
| 493 | 487 |
| 494 void Label::CalculateDrawStringParams(base::string16* paint_text, | 488 void Label::CalculateDrawStringParams(base::string16* paint_text, |
| 495 gfx::Rect* text_bounds, | 489 gfx::Rect* text_bounds, |
| 496 int* flags) const { | 490 int* flags) const { |
| 497 DCHECK(paint_text && text_bounds && flags); | 491 DCHECK(paint_text && text_bounds && flags); |
| 498 | 492 |
| 499 const bool forbid_ellipsis = elide_behavior_ == gfx::NO_ELIDE || | 493 const bool forbid_ellipsis = elide_behavior_ == gfx::NO_ELIDE || |
| 500 elide_behavior_ == gfx::FADE_TAIL; | 494 elide_behavior_ == gfx::FADE_TAIL; |
| 501 if (is_multi_line_ || forbid_ellipsis) { | 495 if (multi_line_ || forbid_ellipsis) { |
| 502 *paint_text = layout_text(); | 496 *paint_text = layout_text_; |
| 503 } else { | 497 } else { |
| 504 *paint_text = gfx::ElideText(layout_text(), font_list_, | 498 *paint_text = gfx::ElideText(layout_text_, font_list_, |
| 505 GetAvailableRect().width(), elide_behavior_); | 499 GetAvailableRect().width(), elide_behavior_); |
| 506 } | 500 } |
| 507 | 501 |
| 508 *text_bounds = GetTextBounds(); | 502 *text_bounds = GetTextBounds(); |
| 509 *flags = ComputeDrawStringFlags(); | 503 *flags = ComputeDrawStringFlags(); |
| 510 // TODO(msw): Elide multi-line text with ElideRectangleText instead. | 504 // TODO(msw): Elide multi-line text with ElideRectangleText instead. |
| 511 if (!is_multi_line_ || forbid_ellipsis) | 505 if (!multi_line_ || forbid_ellipsis) |
| 512 *flags |= gfx::Canvas::NO_ELLIPSIS; | 506 *flags |= gfx::Canvas::NO_ELLIPSIS; |
| 513 } | 507 } |
| 514 | 508 |
| 515 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { | 509 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
| 516 if (!enabled_color_set_) { | 510 if (!enabled_color_set_) { |
| 517 requested_enabled_color_ = theme->GetSystemColor( | 511 requested_enabled_color_ = theme->GetSystemColor( |
| 518 ui::NativeTheme::kColorId_LabelEnabledColor); | 512 ui::NativeTheme::kColorId_LabelEnabledColor); |
| 519 } | 513 } |
| 520 if (!disabled_color_set_) { | 514 if (!disabled_color_set_) { |
| 521 requested_disabled_color_ = theme->GetSystemColor( | 515 requested_disabled_color_ = theme->GetSystemColor( |
| 522 ui::NativeTheme::kColorId_LabelDisabledColor); | 516 ui::NativeTheme::kColorId_LabelDisabledColor); |
| 523 } | 517 } |
| 524 if (!background_color_set_) { | 518 if (!background_color_set_) { |
| 525 background_color_ = theme->GetSystemColor( | 519 background_color_ = theme->GetSystemColor( |
| 526 ui::NativeTheme::kColorId_LabelBackgroundColor); | 520 ui::NativeTheme::kColorId_LabelBackgroundColor); |
| 527 } | 521 } |
| 528 RecalculateColors(); | 522 RecalculateColors(); |
| 529 } | 523 } |
| 530 | 524 |
| 531 void Label::ResetCachedSize() { | 525 void Label::ResetCachedSize() { |
| 532 text_size_valid_ = false; | 526 text_size_valid_ = false; |
| 533 cached_heights_cursor_ = 0; | 527 cached_heights_cursor_ = 0; |
| 534 for (int i = 0; i < kCachedSizeLimit; ++i) | 528 for (int i = 0; i < kCachedSizeLimit; ++i) |
| 535 cached_heights_[i] = gfx::Size(); | 529 cached_heights_[i] = gfx::Size(); |
| 536 } | 530 } |
| 537 | 531 |
| 538 bool Label::ShouldShowDefaultTooltip() const { | 532 bool Label::ShouldShowDefaultTooltip() const { |
| 539 return !is_multi_line_ && !is_obscured_ && | 533 const gfx::Size text_size = GetTextSize(); |
| 540 gfx::GetStringWidth(layout_text(), font_list_) > | 534 const gfx::Size size = GetContentsBounds().size(); |
| 541 GetAvailableRect().width(); | 535 return !obscured() && (text_size.width() > size.width() || |
| 536 text_size.height() > size.height()); |
| 542 } | 537 } |
| 543 | 538 |
| 544 } // namespace views | 539 } // namespace views |
| OLD | NEW |