drop_shadow_label.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/browser/chromeos/drop_shadow_label.h" 6 7#include "gfx/canvas.h" 8#include "gfx/color_utils.h" 9 10using views::Label; 11 12namespace chromeos { 13 14static const int kDefaultDropShadowSize = 2; 15 16// Default color is black. 17static const SkColor kDefaultColor = 0x000000; 18 19static const float kShadowOpacity = 0.2; 20 21DropShadowLabel::DropShadowLabel() : Label() { 22 Init(); 23} 24 25void DropShadowLabel::Init() { 26 drop_shadow_size_ = kDefaultDropShadowSize; 27} 28 29void DropShadowLabel::SetDropShadowSize(int drop_shadow_size) { 30 if (drop_shadow_size != drop_shadow_size_) { 31 drop_shadow_size_ = drop_shadow_size; 32 invalidate_text_size(); 33 SchedulePaint(); 34 } 35} 36 37void DropShadowLabel::PaintText(gfx::Canvas* canvas, 38 const std::wstring& text, 39 const gfx::Rect& text_bounds, 40 int flags) { 41 if (drop_shadow_size_ > 0) { 42 SkColor color = SkColorSetARGB(kShadowOpacity * SkColorGetA(GetColor()), 43 SkColorGetR(kDefaultColor), 44 SkColorGetG(kDefaultColor), 45 SkColorGetB(kDefaultColor)); 46 for (int i = 0; i < drop_shadow_size_; i++) { 47 canvas->DrawStringInt(text, font(), color, 48 text_bounds.x() + i, text_bounds.y(), 49 text_bounds.width(), text_bounds.height(), flags); 50 canvas->DrawStringInt(text, font(), color, 51 text_bounds.x() + i, text_bounds.y() + i, 52 text_bounds.width(), text_bounds.height(), flags); 53 canvas->DrawStringInt(text, font(), color, 54 text_bounds.x(), text_bounds.y() + i, 55 text_bounds.width(), text_bounds.height(), flags); 56 } 57 } 58 59 canvas->DrawStringInt(text, font(), GetColor(), 60 text_bounds.x(), text_bounds.y(), 61 text_bounds.width(), text_bounds.height(), flags); 62 63 if (HasFocus() || paint_as_focused()) { 64 gfx::Rect focus_bounds = text_bounds; 65 focus_bounds.Inset(-Label::kFocusBorderPadding, 66 -Label::kFocusBorderPadding); 67 canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(), 68 focus_bounds.width(), focus_bounds.height()); 69 } 70} 71 72gfx::Size DropShadowLabel::GetTextSize() const { 73 gfx::Size text_size = Label::GetTextSize(); 74 text_size.SetSize(text_size.width() + drop_shadow_size_, 75 text_size.height() + drop_shadow_size_); 76 return text_size; 77} 78 79} // namespace chromeos 80