Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / button / label_button_border.cc
blobab7140821bf6eaea6684df6cff66330fa52850c4
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
3 // found in the LICENSE file.
5 #include "ui/views/controls/button/label_button_border.h"
7 #include "base/logging.h"
8 #include "third_party/skia/include/core/SkPaint.h"
9 #include "third_party/skia/include/effects/SkLerpXfermode.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/animation/animation.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/skia_util.h"
15 #include "ui/gfx/sys_color_change_listener.h"
16 #include "ui/native_theme/native_theme.h"
17 #include "ui/resources/grit/ui_resources.h"
18 #include "ui/views/border.h"
19 #include "ui/views/controls/button/label_button.h"
20 #include "ui/views/native_theme_delegate.h"
21 #include "ui/views/resources/grit/views_resources.h"
23 namespace views {
25 namespace {
27 // Insets for the unified button images. This assumes that the images
28 // are of a 9 grid, of 5x5 size each.
29 const int kButtonInsets = 5;
31 // The text-button hot and pushed image IDs; normal is unadorned by default.
32 const int kTextHoveredImages[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER);
33 const int kTextPressedImages[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED);
35 // A helper function to paint the appropriate broder images.
36 void PaintHelper(LabelButtonAssetBorder* border,
37 gfx::Canvas* canvas,
38 ui::NativeTheme::State state,
39 const gfx::Rect& rect,
40 const ui::NativeTheme::ExtraParams& extra) {
41 Painter* painter =
42 border->GetPainter(extra.button.is_focused,
43 Button::GetButtonStateFrom(state));
44 // Paint any corresponding unfocused painter if there is no focused painter.
45 if (!painter && extra.button.is_focused)
46 painter = border->GetPainter(false, Button::GetButtonStateFrom(state));
47 if (painter)
48 Painter::PaintPainterAt(canvas, painter, rect);
51 } // namespace
53 LabelButtonBorder::LabelButtonBorder() {}
54 LabelButtonBorder::~LabelButtonBorder() {}
56 bool LabelButtonBorder::PaintsButtonState(bool focused,
57 Button::ButtonState state) {
58 return false;
61 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) {}
63 gfx::Insets LabelButtonBorder::GetInsets() const {
64 return insets_;
67 gfx::Size LabelButtonBorder::GetMinimumSize() const {
68 return gfx::Size();
71 LabelButtonAssetBorder::LabelButtonAssetBorder(Button::ButtonStyle style) {
72 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
73 const gfx::Insets insets(kButtonInsets,
74 kButtonInsets,
75 kButtonInsets,
76 kButtonInsets);
78 set_insets(GetDefaultInsetsForStyle(style));
79 if (style == Button::STYLE_BUTTON) {
80 SetPainter(false, Button::STATE_NORMAL,
81 Painter::CreateImagePainter(
82 *rb.GetImageSkiaNamed(IDR_BUTTON_NORMAL), insets));
83 SetPainter(false, Button::STATE_HOVERED,
84 Painter::CreateImagePainter(
85 *rb.GetImageSkiaNamed(IDR_BUTTON_HOVER), insets));
86 SetPainter(false, Button::STATE_PRESSED,
87 Painter::CreateImagePainter(
88 *rb.GetImageSkiaNamed(IDR_BUTTON_PRESSED), insets));
89 SetPainter(false, Button::STATE_DISABLED,
90 Painter::CreateImagePainter(
91 *rb.GetImageSkiaNamed(IDR_BUTTON_DISABLED), insets));
92 SetPainter(true, Button::STATE_NORMAL,
93 Painter::CreateImagePainter(
94 *rb.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_NORMAL), insets));
95 SetPainter(true, Button::STATE_HOVERED,
96 Painter::CreateImagePainter(
97 *rb.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_HOVER), insets));
98 SetPainter(true, Button::STATE_PRESSED,
99 Painter::CreateImagePainter(
100 *rb.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_PRESSED), insets));
101 SetPainter(true, Button::STATE_DISABLED,
102 Painter::CreateImagePainter(
103 *rb.GetImageSkiaNamed(IDR_BUTTON_DISABLED), insets));
104 } else if (style == Button::STYLE_TEXTBUTTON) {
105 SetPainter(false, Button::STATE_HOVERED,
106 Painter::CreateImageGridPainter(kTextHoveredImages));
107 SetPainter(false, Button::STATE_PRESSED,
108 Painter::CreateImageGridPainter(kTextPressedImages));
112 LabelButtonAssetBorder::~LabelButtonAssetBorder() {}
114 // static
115 gfx::Insets LabelButtonAssetBorder::GetDefaultInsetsForStyle(
116 Button::ButtonStyle style) {
117 gfx::Insets insets;
118 if (style == Button::STYLE_BUTTON) {
119 insets = gfx::Insets(8, 13, 8, 13);
120 } else if (style == Button::STYLE_TEXTBUTTON) {
121 insets = gfx::Insets(5, 6, 5, 6);
122 } else {
123 NOTREACHED();
125 return insets;
128 bool LabelButtonAssetBorder::PaintsButtonState(bool focused,
129 Button::ButtonState state) {
130 // PaintHelper() above will paint the unfocused painter for a given state if
131 // the button is focused, but there is no focused painter.
132 return GetPainter(focused, state) || (focused && GetPainter(false, state));
135 void LabelButtonAssetBorder::Paint(const View& view, gfx::Canvas* canvas) {
136 const NativeThemeDelegate* native_theme_delegate =
137 static_cast<const LabelButton*>(&view);
138 gfx::Rect rect(native_theme_delegate->GetThemePaintRect());
139 ui::NativeTheme::ExtraParams extra;
140 const gfx::Animation* animation = native_theme_delegate->GetThemeAnimation();
141 ui::NativeTheme::State state = native_theme_delegate->GetThemeState(&extra);
143 if (animation && animation->is_animating()) {
144 // Linearly interpolate background and foreground painters during animation.
145 const SkRect sk_rect = gfx::RectToSkRect(rect);
146 canvas->sk_canvas()->saveLayer(&sk_rect, NULL);
147 state = native_theme_delegate->GetBackgroundThemeState(&extra);
148 PaintHelper(this, canvas, state, rect, extra);
150 SkPaint paint;
151 skia::RefPtr<SkXfermode> sk_lerp_xfer =
152 skia::AdoptRef(SkLerpXfermode::Create(animation->GetCurrentValue()));
153 paint.setXfermode(sk_lerp_xfer.get());
154 canvas->sk_canvas()->saveLayer(&sk_rect, &paint);
155 state = native_theme_delegate->GetForegroundThemeState(&extra);
156 PaintHelper(this, canvas, state, rect, extra);
157 canvas->sk_canvas()->restore();
159 canvas->sk_canvas()->restore();
160 } else {
161 PaintHelper(this, canvas, state, rect, extra);
165 gfx::Size LabelButtonAssetBorder::GetMinimumSize() const {
166 gfx::Size minimum_size;
167 for (int i = 0; i < 2; ++i) {
168 for (int j = 0; j < Button::STATE_COUNT; ++j) {
169 if (painters_[i][j])
170 minimum_size.SetToMax(painters_[i][j]->GetMinimumSize());
173 return minimum_size;
176 Painter* LabelButtonAssetBorder::GetPainter(bool focused,
177 Button::ButtonState state) {
178 return painters_[focused ? 1 : 0][state].get();
181 void LabelButtonAssetBorder::SetPainter(bool focused,
182 Button::ButtonState state,
183 Painter* painter) {
184 painters_[focused ? 1 : 0][state].reset(painter);
187 } // namespace views