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 "base/utf_string_conversions.h"
8#include "ui/gfx/canvas.h"
9#include "ui/gfx/color_utils.h"
10
11using views::Label;
12
13namespace chromeos {
14
15static const int kDefaultDropShadowSize = 2;
16
17// Default color is black.
18static const SkColor kDefaultColor = 0x000000;
19
20static const float kShadowOpacity = 0.2;
21
22DropShadowLabel::DropShadowLabel() : Label() {
23  Init();
24}
25
26void DropShadowLabel::Init() {
27  drop_shadow_size_ = kDefaultDropShadowSize;
28}
29
30void DropShadowLabel::SetDropShadowSize(int drop_shadow_size) {
31  if (drop_shadow_size != drop_shadow_size_) {
32    drop_shadow_size_ = drop_shadow_size;
33    invalidate_text_size();
34    SchedulePaint();
35  }
36}
37
38void DropShadowLabel::PaintText(gfx::Canvas* canvas,
39                                const std::wstring& text,
40                                const gfx::Rect& text_bounds,
41                                int flags) {
42  if (drop_shadow_size_ > 0) {
43    SkColor color = SkColorSetARGB(kShadowOpacity * SkColorGetA(GetColor()),
44                                   SkColorGetR(kDefaultColor),
45                                   SkColorGetG(kDefaultColor),
46                                   SkColorGetB(kDefaultColor));
47    for (int i = 0; i < drop_shadow_size_; i++) {
48      canvas->DrawStringInt(WideToUTF16Hack(text), font(), color,
49                            text_bounds.x() + i, text_bounds.y(),
50                            text_bounds.width(), text_bounds.height(), flags);
51      canvas->DrawStringInt(WideToUTF16Hack(text), font(), color,
52                            text_bounds.x() + i, text_bounds.y() + i,
53                            text_bounds.width(), text_bounds.height(), flags);
54      canvas->DrawStringInt(WideToUTF16Hack(text), font(), color,
55                            text_bounds.x(), text_bounds.y() + i,
56                            text_bounds.width(), text_bounds.height(), flags);
57    }
58  }
59
60  canvas->DrawStringInt(WideToUTF16Hack(text), font(), GetColor(),
61                        text_bounds.x(), text_bounds.y(),
62                        text_bounds.width(), text_bounds.height(), flags);
63
64  if (HasFocus() || paint_as_focused()) {
65    gfx::Rect focus_bounds = text_bounds;
66    focus_bounds.Inset(-Label::kFocusBorderPadding,
67                       -Label::kFocusBorderPadding);
68    canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(),
69                          focus_bounds.width(), focus_bounds.height());
70  }
71}
72
73gfx::Size DropShadowLabel::GetTextSize() const {
74  gfx::Size text_size = Label::GetTextSize();
75  text_size.SetSize(text_size.width() + drop_shadow_size_,
76                    text_size.height() + drop_shadow_size_);
77  return text_size;
78}
79
80}  // namespace chromeos
81