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"
26 // Insets for the unified button images. This assumes that the images
27 // are of a 9 grid, of 5x5 size each.
28 const int kButtonInsets
= 5;
30 // The text-button hot and pushed image IDs; normal is unadorned by default.
31 const int kTextHoveredImages
[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER
);
32 const int kTextPressedImages
[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED
);
34 // A helper function to paint the appropriate broder images.
35 void PaintHelper(LabelButtonBorder
* border
,
37 ui::NativeTheme::State state
,
38 const gfx::Rect
& rect
,
39 const ui::NativeTheme::ExtraParams
& extra
) {
41 border
->GetPainter(extra
.button
.is_focused
,
42 Button::GetButtonStateFrom(state
));
43 // Paint any corresponding unfocused painter if there is no focused painter.
44 if (!painter
&& extra
.button
.is_focused
)
45 painter
= border
->GetPainter(false, Button::GetButtonStateFrom(state
));
47 Painter::PaintPainterAt(canvas
, painter
, rect
);
52 LabelButtonBorder::LabelButtonBorder(Button::ButtonStyle style
)
54 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
55 const gfx::Insets
insets(kButtonInsets
,
60 set_insets(GetDefaultInsetsForStyle(style_
));
61 if (style
== Button::STYLE_BUTTON
) {
62 SetPainter(false, Button::STATE_NORMAL
,
63 Painter::CreateImagePainter(
64 *rb
.GetImageSkiaNamed(IDR_BUTTON_NORMAL
), insets
));
65 SetPainter(false, Button::STATE_HOVERED
,
66 Painter::CreateImagePainter(
67 *rb
.GetImageSkiaNamed(IDR_BUTTON_HOVER
), insets
));
68 SetPainter(false, Button::STATE_PRESSED
,
69 Painter::CreateImagePainter(
70 *rb
.GetImageSkiaNamed(IDR_BUTTON_PRESSED
), insets
));
71 SetPainter(false, Button::STATE_DISABLED
,
72 Painter::CreateImagePainter(
73 *rb
.GetImageSkiaNamed(IDR_BUTTON_DISABLED
), insets
));
74 SetPainter(true, Button::STATE_NORMAL
,
75 Painter::CreateImagePainter(
76 *rb
.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_NORMAL
), insets
));
77 SetPainter(true, Button::STATE_HOVERED
,
78 Painter::CreateImagePainter(
79 *rb
.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_HOVER
), insets
));
80 SetPainter(true, Button::STATE_PRESSED
,
81 Painter::CreateImagePainter(
82 *rb
.GetImageSkiaNamed(IDR_BUTTON_FOCUSED_PRESSED
), insets
));
83 SetPainter(true, Button::STATE_DISABLED
,
84 Painter::CreateImagePainter(
85 *rb
.GetImageSkiaNamed(IDR_BUTTON_DISABLED
), insets
));
86 } else if (style
== Button::STYLE_TEXTBUTTON
) {
87 SetPainter(false, Button::STATE_HOVERED
,
88 Painter::CreateImageGridPainter(kTextHoveredImages
));
89 SetPainter(false, Button::STATE_PRESSED
,
90 Painter::CreateImageGridPainter(kTextPressedImages
));
94 LabelButtonBorder::~LabelButtonBorder() {}
97 gfx::Insets
LabelButtonBorder::GetDefaultInsetsForStyle(
98 Button::ButtonStyle style
) {
100 if (style
== Button::STYLE_BUTTON
) {
101 insets
= gfx::Insets(8, 13, 8, 13);
102 } else if (style
== Button::STYLE_TEXTBUTTON
) {
103 insets
= gfx::Insets(5, 6, 5, 6);
110 void LabelButtonBorder::Paint(const View
& view
, gfx::Canvas
* canvas
) {
111 const NativeThemeDelegate
* native_theme_delegate
=
112 static_cast<const LabelButton
*>(&view
);
113 gfx::Rect
rect(native_theme_delegate
->GetThemePaintRect());
114 ui::NativeTheme::ExtraParams extra
;
115 const gfx::Animation
* animation
= native_theme_delegate
->GetThemeAnimation();
116 ui::NativeTheme::State state
= native_theme_delegate
->GetThemeState(&extra
);
118 if (animation
&& animation
->is_animating()) {
119 // Linearly interpolate background and foreground painters during animation.
120 const SkRect sk_rect
= gfx::RectToSkRect(rect
);
121 canvas
->sk_canvas()->saveLayer(&sk_rect
, NULL
);
122 state
= native_theme_delegate
->GetBackgroundThemeState(&extra
);
123 PaintHelper(this, canvas
, state
, rect
, extra
);
126 skia::RefPtr
<SkXfermode
> sk_lerp_xfer
=
127 skia::AdoptRef(SkLerpXfermode::Create(animation
->GetCurrentValue()));
128 paint
.setXfermode(sk_lerp_xfer
.get());
129 canvas
->sk_canvas()->saveLayer(&sk_rect
, &paint
);
130 state
= native_theme_delegate
->GetForegroundThemeState(&extra
);
131 PaintHelper(this, canvas
, state
, rect
, extra
);
132 canvas
->sk_canvas()->restore();
134 canvas
->sk_canvas()->restore();
136 PaintHelper(this, canvas
, state
, rect
, extra
);
140 gfx::Insets
LabelButtonBorder::GetInsets() const {
144 gfx::Size
LabelButtonBorder::GetMinimumSize() const {
145 gfx::Size minimum_size
;
146 for (int i
= 0; i
< 2; ++i
) {
147 for (int j
= 0; j
< Button::STATE_COUNT
; ++j
) {
149 minimum_size
.SetToMax(painters_
[i
][j
]->GetMinimumSize());
155 Painter
* LabelButtonBorder::GetPainter(bool focused
,
156 Button::ButtonState state
) {
157 return painters_
[focused
? 1 : 0][state
].get();
160 void LabelButtonBorder::SetPainter(bool focused
,
161 Button::ButtonState state
,
163 painters_
[focused
? 1 : 0][state
].reset(painter
);