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

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

Issue 14322007: Add line height setting to views::Label & use it for notifications. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « ui/views/controls/label.h ('k') | ui/views/controls/tree/tree_view.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 (alignment != gfx::ALIGN_CENTER)) { 114 (alignment != gfx::ALIGN_CENTER)) {
115 alignment = (alignment == gfx::ALIGN_LEFT) ? 115 alignment = (alignment == gfx::ALIGN_LEFT) ?
116 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; 116 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
117 } 117 }
118 if (horizontal_alignment_ != alignment) { 118 if (horizontal_alignment_ != alignment) {
119 horizontal_alignment_ = alignment; 119 horizontal_alignment_ = alignment;
120 SchedulePaint(); 120 SchedulePaint();
121 } 121 }
122 } 122 }
123 123
124 void Label::SetLineHeight(int height) {
125 if (height != line_height_) {
126 line_height_ = height;
127 ResetCachedSize();
128 PreferredSizeChanged();
129 SchedulePaint();
130 }
131 }
132
124 void Label::SetMultiLine(bool multi_line) { 133 void Label::SetMultiLine(bool multi_line) {
125 DCHECK(!multi_line || elide_behavior_ != ELIDE_IN_MIDDLE); 134 DCHECK(!multi_line || elide_behavior_ != ELIDE_IN_MIDDLE);
126 if (multi_line != is_multi_line_) { 135 if (multi_line != is_multi_line_) {
127 is_multi_line_ = multi_line; 136 is_multi_line_ = multi_line;
128 ResetCachedSize(); 137 ResetCachedSize();
129 PreferredSizeChanged(); 138 PreferredSizeChanged();
130 SchedulePaint(); 139 SchedulePaint();
131 } 140 }
132 } 141 }
133 142
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 228
220 for (size_t i = 0; i < cached_heights_.size(); ++i) { 229 for (size_t i = 0; i < cached_heights_.size(); ++i) {
221 const gfx::Size& s = cached_heights_[i]; 230 const gfx::Size& s = cached_heights_[i];
222 if (s.width() == w) 231 if (s.width() == w)
223 return s.height() + GetInsets().height(); 232 return s.height() + GetInsets().height();
224 } 233 }
225 234
226 int cache_width = w; 235 int cache_width = w;
227 236
228 int h = font_.GetHeight(); 237 int h = font_.GetHeight();
229 gfx::Canvas::SizeStringInt(text_, font_, &w, &h, ComputeDrawStringFlags()); 238 const int flags = ComputeDrawStringFlags();
239 gfx::Canvas::SizeStringInt(text_, font_, &w, &h, line_height_, flags);
230 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); 240 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h);
231 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; 241 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit;
232 return h + GetInsets().height(); 242 return h + GetInsets().height();
233 } 243 }
234 244
235 std::string Label::GetClassName() const { 245 std::string Label::GetClassName() const {
236 return kViewClassName; 246 return kViewClassName;
237 } 247 }
238 248
239 bool Label::HitTestRect(const gfx::Rect& rect) const { 249 bool Label::HitTestRect(const gfx::Rect& rect) const {
(...skipping 27 matching lines...) Expand all
267 void Label::PaintText(gfx::Canvas* canvas, 277 void Label::PaintText(gfx::Canvas* canvas,
268 const string16& text, 278 const string16& text,
269 const gfx::Rect& text_bounds, 279 const gfx::Rect& text_bounds,
270 int flags) { 280 int flags) {
271 gfx::ShadowValues shadows; 281 gfx::ShadowValues shadows;
272 if (has_shadow_) 282 if (has_shadow_)
273 shadows.push_back(gfx::ShadowValue(shadow_offset_, 0, 283 shadows.push_back(gfx::ShadowValue(shadow_offset_, 0,
274 enabled() ? enabled_shadow_color_ : disabled_shadow_color_)); 284 enabled() ? enabled_shadow_color_ : disabled_shadow_color_));
275 canvas->DrawStringWithShadows(text, font_, 285 canvas->DrawStringWithShadows(text, font_,
276 enabled() ? actual_enabled_color_ : actual_disabled_color_, 286 enabled() ? actual_enabled_color_ : actual_disabled_color_,
277 text_bounds, flags, shadows); 287 text_bounds, line_height_, flags, shadows);
278 288
279 if (HasFocus()) { 289 if (HasFocus()) {
280 gfx::Rect focus_bounds = text_bounds; 290 gfx::Rect focus_bounds = text_bounds;
281 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); 291 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
282 canvas->DrawFocusRect(focus_bounds); 292 canvas->DrawFocusRect(focus_bounds);
283 } 293 }
284 } 294 }
285 295
286 gfx::Size Label::GetTextSize() const { 296 gfx::Size Label::GetTextSize() const {
287 if (!text_size_valid_) { 297 if (!text_size_valid_) {
288 // For single-line strings, we supply the largest possible width, because 298 // For single-line strings, we supply the largest possible width, because
289 // while adding NO_ELLIPSIS to the flags works on Windows for forcing 299 // while adding NO_ELLIPSIS to the flags works on Windows for forcing
290 // SizeStringInt() to calculate the desired width, it doesn't seem to work 300 // SizeStringInt() to calculate the desired width, it doesn't seem to work
291 // on Linux. 301 // on Linux.
292 int w = is_multi_line_ ? 302 int w = is_multi_line_ ?
293 GetAvailableRect().width() : std::numeric_limits<int>::max(); 303 GetAvailableRect().width() : std::numeric_limits<int>::max();
294 int h = font_.GetHeight(); 304 int h = font_.GetHeight();
295 // For single-line strings, ignore the available width and calculate how 305 // For single-line strings, ignore the available width and calculate how
296 // wide the text wants to be. 306 // wide the text wants to be.
297 int flags = ComputeDrawStringFlags(); 307 int flags = ComputeDrawStringFlags();
298 if (!is_multi_line_) 308 if (!is_multi_line_)
299 flags |= gfx::Canvas::NO_ELLIPSIS; 309 flags |= gfx::Canvas::NO_ELLIPSIS;
300 gfx::Canvas::SizeStringInt(text_, font_, &w, &h, flags); 310 gfx::Canvas::SizeStringInt(text_, font_, &w, &h, line_height_, flags);
301 text_size_.SetSize(w, h); 311 text_size_.SetSize(w, h);
302 text_size_valid_ = true; 312 text_size_valid_ = true;
303 } 313 }
304 314
305 return text_size_; 315 return text_size_;
306 } 316 }
307 317
308 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { 318 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) {
309 text_size_valid_ &= !is_multi_line_; 319 text_size_valid_ &= !is_multi_line_;
310 } 320 }
(...skipping 20 matching lines...) Expand all
331 gfx::Font Label::GetDefaultFont() { 341 gfx::Font Label::GetDefaultFont() {
332 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); 342 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
333 } 343 }
334 344
335 void Label::Init(const string16& text, const gfx::Font& font) { 345 void Label::Init(const string16& text, const gfx::Font& font) {
336 font_ = font; 346 font_ = font;
337 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; 347 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false;
338 auto_color_readability_ = true; 348 auto_color_readability_ = true;
339 UpdateColorsFromTheme(ui::NativeTheme::instance()); 349 UpdateColorsFromTheme(ui::NativeTheme::instance());
340 horizontal_alignment_ = gfx::ALIGN_CENTER; 350 horizontal_alignment_ = gfx::ALIGN_CENTER;
351 line_height_ = 0;
341 is_multi_line_ = false; 352 is_multi_line_ = false;
342 allow_character_break_ = false; 353 allow_character_break_ = false;
343 elide_behavior_ = NO_ELIDE; 354 elide_behavior_ = NO_ELIDE;
344 collapse_when_hidden_ = false; 355 collapse_when_hidden_ = false;
345 directionality_mode_ = USE_UI_DIRECTIONALITY; 356 directionality_mode_ = USE_UI_DIRECTIONALITY;
346 has_focus_border_ = false; 357 has_focus_border_ = false;
347 enabled_shadow_color_ = 0; 358 enabled_shadow_color_ = 0;
348 disabled_shadow_color_ = 0; 359 disabled_shadow_color_ = 0;
349 shadow_offset_.SetPoint(1, 1); 360 shadow_offset_.SetPoint(1, 1);
350 has_shadow_ = false; 361 has_shadow_ = false;
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 } 497 }
487 498
488 void Label::ResetCachedSize() { 499 void Label::ResetCachedSize() {
489 text_size_valid_ = false; 500 text_size_valid_ = false;
490 cached_heights_cursor_ = 0; 501 cached_heights_cursor_ = 0;
491 for (int i = 0; i < kCachedSizeLimit; ++i) 502 for (int i = 0; i < kCachedSizeLimit; ++i)
492 cached_heights_[i] = gfx::Size(); 503 cached_heights_[i] = gfx::Size();
493 } 504 }
494 505
495 } // namespace views 506 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/label.h ('k') | ui/views/controls/tree/tree_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698