| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "views/controls/label.h" | 5 #include "views/controls/label.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "app/gfx/chrome_canvas.h" | 9 #include "app/gfx/canvas.h" |
| 10 #include "app/gfx/chrome_font.h" | 10 #include "app/gfx/font.h" |
| 11 #include "app/gfx/insets.h" | 11 #include "app/gfx/insets.h" |
| 12 #include "app/gfx/text_elider.h" | 12 #include "app/gfx/text_elider.h" |
| 13 #include "app/l10n_util.h" | 13 #include "app/l10n_util.h" |
| 14 #include "app/resource_bundle.h" | 14 #include "app/resource_bundle.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "views/background.h" | 17 #include "views/background.h" |
| 18 | 18 |
| 19 namespace views { | 19 namespace views { |
| 20 | 20 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 // Return a size of (0, 0) if the label is not visible and if the | 61 // Return a size of (0, 0) if the label is not visible and if the |
| 62 // collapse_when_hidden_ flag is set. | 62 // collapse_when_hidden_ flag is set. |
| 63 // TODO(munjal): This logic probably belongs to the View class. But for now, | 63 // TODO(munjal): This logic probably belongs to the View class. But for now, |
| 64 // put it here since putting it in View class means all inheriting classes | 64 // put it here since putting it in View class means all inheriting classes |
| 65 // need ot respect the collapse_when_hidden_ flag. | 65 // need ot respect the collapse_when_hidden_ flag. |
| 66 if (!IsVisible() && collapse_when_hidden_) | 66 if (!IsVisible() && collapse_when_hidden_) |
| 67 return prefsize; | 67 return prefsize; |
| 68 | 68 |
| 69 if (is_multi_line_) { | 69 if (is_multi_line_) { |
| 70 int w = width(), h = 0; | 70 int w = width(), h = 0; |
| 71 ChromeCanvas::SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); | 71 gfx::Canvas::SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); |
| 72 prefsize.SetSize(w, h); | 72 prefsize.SetSize(w, h); |
| 73 } else { | 73 } else { |
| 74 prefsize = GetTextSize(); | 74 prefsize = GetTextSize(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 gfx::Insets insets = GetInsets(); | 77 gfx::Insets insets = GetInsets(); |
| 78 prefsize.Enlarge(insets.width(), insets.height()); | 78 prefsize.Enlarge(insets.width(), insets.height()); |
| 79 return prefsize; | 79 return prefsize; |
| 80 } | 80 } |
| 81 | 81 |
| 82 int Label::ComputeMultiLineFlags() { | 82 int Label::ComputeMultiLineFlags() { |
| 83 int flags = ChromeCanvas::MULTI_LINE; | 83 int flags = gfx::Canvas::MULTI_LINE; |
| 84 if (allow_character_break_) | 84 if (allow_character_break_) |
| 85 flags |= ChromeCanvas::CHARACTER_BREAK; | 85 flags |= gfx::Canvas::CHARACTER_BREAK; |
| 86 switch (horiz_alignment_) { | 86 switch (horiz_alignment_) { |
| 87 case ALIGN_LEFT: | 87 case ALIGN_LEFT: |
| 88 flags |= ChromeCanvas::TEXT_ALIGN_LEFT; | 88 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
| 89 break; | 89 break; |
| 90 case ALIGN_CENTER: | 90 case ALIGN_CENTER: |
| 91 flags |= ChromeCanvas::TEXT_ALIGN_CENTER; | 91 flags |= gfx::Canvas::TEXT_ALIGN_CENTER; |
| 92 break; | 92 break; |
| 93 case ALIGN_RIGHT: | 93 case ALIGN_RIGHT: |
| 94 flags |= ChromeCanvas::TEXT_ALIGN_RIGHT; | 94 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 95 break; | 95 break; |
| 96 } | 96 } |
| 97 return flags; | 97 return flags; |
| 98 } | 98 } |
| 99 | 99 |
| 100 void Label::CalculateDrawStringParams( | 100 void Label::CalculateDrawStringParams( |
| 101 std::wstring* paint_text, gfx::Rect* text_bounds, int* flags) { | 101 std::wstring* paint_text, gfx::Rect* text_bounds, int* flags) { |
| 102 DCHECK(paint_text && text_bounds && flags); | 102 DCHECK(paint_text && text_bounds && flags); |
| 103 | 103 |
| 104 if (url_set_) { | 104 if (url_set_) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 127 insets.top(), | 127 insets.top(), |
| 128 width() - insets.width(), | 128 width() - insets.width(), |
| 129 height() - insets.height()); | 129 height() - insets.height()); |
| 130 *flags = ComputeMultiLineFlags(); | 130 *flags = ComputeMultiLineFlags(); |
| 131 } else { | 131 } else { |
| 132 *text_bounds = GetTextBounds(); | 132 *text_bounds = GetTextBounds(); |
| 133 *flags = 0; | 133 *flags = 0; |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 void Label::Paint(ChromeCanvas* canvas) { | 137 void Label::Paint(gfx::Canvas* canvas) { |
| 138 PaintBackground(canvas); | 138 PaintBackground(canvas); |
| 139 std::wstring paint_text; | 139 std::wstring paint_text; |
| 140 gfx::Rect text_bounds; | 140 gfx::Rect text_bounds; |
| 141 int flags = 0; | 141 int flags = 0; |
| 142 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 142 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 143 canvas->DrawStringInt(paint_text, | 143 canvas->DrawStringInt(paint_text, |
| 144 font_, | 144 font_, |
| 145 color_, | 145 color_, |
| 146 text_bounds.x(), | 146 text_bounds.x(), |
| 147 text_bounds.y(), | 147 text_bounds.y(), |
| 148 text_bounds.width(), | 148 text_bounds.width(), |
| 149 text_bounds.height(), | 149 text_bounds.height(), |
| 150 flags); | 150 flags); |
| 151 | 151 |
| 152 // The focus border always hugs the text, regardless of the label's bounds. | 152 // The focus border always hugs the text, regardless of the label's bounds. |
| 153 if (HasFocus() || paint_as_focused_) { | 153 if (HasFocus() || paint_as_focused_) { |
| 154 int w = text_bounds.width(); | 154 int w = text_bounds.width(); |
| 155 int h = 0; | 155 int h = 0; |
| 156 // We explicitly OR in MULTI_LINE here since SizeStringInt seems to return | 156 // We explicitly OR in MULTI_LINE here since SizeStringInt seems to return |
| 157 // an incorrect height for single line text when the MULTI_LINE flag isn't | 157 // an incorrect height for single line text when the MULTI_LINE flag isn't |
| 158 // specified. o_O... | 158 // specified. o_O... |
| 159 ChromeCanvas::SizeStringInt(paint_text, font_, &w, &h, | 159 gfx::Canvas::SizeStringInt(paint_text, font_, &w, &h, |
| 160 flags | ChromeCanvas::MULTI_LINE); | 160 flags | gfx::Canvas::MULTI_LINE); |
| 161 gfx::Rect focus_rect = text_bounds; | 161 gfx::Rect focus_rect = text_bounds; |
| 162 focus_rect.set_width(w); | 162 focus_rect.set_width(w); |
| 163 focus_rect.set_height(h); | 163 focus_rect.set_height(h); |
| 164 focus_rect.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | 164 focus_rect.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| 165 // If the label is a single line of text, then the computed text bound | 165 // If the label is a single line of text, then the computed text bound |
| 166 // corresponds directly to the text being drawn and no mirroring is needed | 166 // corresponds directly to the text being drawn and no mirroring is needed |
| 167 // for the RTL case. For multiline text, the text bound is an estimation | 167 // for the RTL case. For multiline text, the text bound is an estimation |
| 168 // and is recomputed in ChromeCanvas::SizeStringInt(). For multiline text | 168 // and is recomputed in gfx::Canvas::SizeStringInt(). For multiline text |
| 169 // in RTL, we need to take mirroring into account when computing the focus | 169 // in RTL, we need to take mirroring into account when computing the focus |
| 170 // rectangle. | 170 // rectangle. |
| 171 int x = focus_rect.x(); | 171 int x = focus_rect.x(); |
| 172 if (flags & ChromeCanvas::MULTI_LINE) | 172 if (flags & gfx::Canvas::MULTI_LINE) |
| 173 x = MirroredLeftPointForRect(focus_rect); | 173 x = MirroredLeftPointForRect(focus_rect); |
| 174 canvas->DrawFocusRect(x, focus_rect.y(), focus_rect.width(), | 174 canvas->DrawFocusRect(x, focus_rect.y(), focus_rect.width(), |
| 175 focus_rect.height()); | 175 focus_rect.height()); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 void Label::PaintBackground(ChromeCanvas* canvas) { | 179 void Label::PaintBackground(gfx::Canvas* canvas) { |
| 180 const Background* bg = contains_mouse_ ? GetMouseOverBackground() : NULL; | 180 const Background* bg = contains_mouse_ ? GetMouseOverBackground() : NULL; |
| 181 if (!bg) | 181 if (!bg) |
| 182 bg = background(); | 182 bg = background(); |
| 183 if (bg) | 183 if (bg) |
| 184 bg->Paint(canvas, this); | 184 bg->Paint(canvas, this); |
| 185 } | 185 } |
| 186 | 186 |
| 187 void Label::SetFont(const gfx::Font& font) { | 187 void Label::SetFont(const gfx::Font& font) { |
| 188 font_ = font; | 188 font_ = font; |
| 189 text_size_valid_ = false; | 189 text_size_valid_ = false; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 if (text_size_valid_) | 232 if (text_size_valid_) |
| 233 return text_size_; | 233 return text_size_; |
| 234 return gfx::Size(); | 234 return gfx::Size(); |
| 235 } | 235 } |
| 236 | 236 |
| 237 int Label::GetHeightForWidth(int w) { | 237 int Label::GetHeightForWidth(int w) { |
| 238 if (is_multi_line_) { | 238 if (is_multi_line_) { |
| 239 gfx::Insets insets = GetInsets(); | 239 gfx::Insets insets = GetInsets(); |
| 240 w = std::max<int>(0, w - insets.width()); | 240 w = std::max<int>(0, w - insets.width()); |
| 241 int h = 0; | 241 int h = 0; |
| 242 ChromeCanvas cc(0, 0, true); | 242 gfx::Canvas cc(0, 0, true); |
| 243 cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); | 243 cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); |
| 244 return h + insets.height(); | 244 return h + insets.height(); |
| 245 } | 245 } |
| 246 | 246 |
| 247 return View::GetHeightForWidth(w); | 247 return View::GetHeightForWidth(w); |
| 248 } | 248 } |
| 249 | 249 |
| 250 std::string Label::GetClassName() const { | 250 std::string Label::GetClassName() const { |
| 251 return kViewClassName; | 251 return kViewClassName; |
| 252 } | 252 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 } | 444 } |
| 445 | 445 |
| 446 bool Label::GetAccessibleState(AccessibilityTypes::State* state) { | 446 bool Label::GetAccessibleState(AccessibilityTypes::State* state) { |
| 447 DCHECK(state); | 447 DCHECK(state); |
| 448 | 448 |
| 449 *state = AccessibilityTypes::STATE_READONLY; | 449 *state = AccessibilityTypes::STATE_READONLY; |
| 450 return true; | 450 return true; |
| 451 } | 451 } |
| 452 | 452 |
| 453 } // namespace views | 453 } // namespace views |
| OLD | NEW |