1 // Copyright 2014 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/ui/libgtk2ui/gtk2_border.h"
6
7 #include <gtk/gtk.h>
8
9 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
10 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
11 #include "chrome/browser/ui/libgtk2ui/native_theme_gtk2.h"
12 #include "third_party/skia/include/effects/SkLerpXfermode.h"
13 #include "ui/base/theme_provider.h"
14 #include "ui/gfx/animation/animation.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/image/image_skia_source.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/skia_util.h"
19 #include "ui/views/controls/button/label_button.h"
20 #include "ui/views/controls/button/label_button_border.h"
21 #include "ui/views/native_theme_delegate.h"
22
23 using views::Button;
24 using views::NativeThemeDelegate;
25
26 namespace libgtk2ui {
27
28 namespace {
29
30 const int kNumberOfFocusedStates = 2;
31
32 class ButtonImageSkiaSource : public gfx::ImageSkiaSource {
33 public:
ButtonImageSkiaSource(const Gtk2UI * gtk2_ui,const GtkStateType state,const bool focused,const gfx::Size & size)34 ButtonImageSkiaSource(const Gtk2UI* gtk2_ui,
35 const GtkStateType state,
36 const bool focused,
37 const gfx::Size& size)
38 : gtk2_ui_(gtk2_ui),
39 state_(state),
40 focused_(focused),
41 size_(size) {
42 }
43
~ButtonImageSkiaSource()44 virtual ~ButtonImageSkiaSource() {
45 }
46
GetImageForScale(float scale)47 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
48 int w = size_.width() * scale;
49 int h = size_.height() * scale;
50 return gfx::ImageSkiaRep(
51 gtk2_ui_->DrawGtkButtonBorder(state_, focused_, w, h), scale);
52 }
53
54 private:
55 const Gtk2UI* gtk2_ui_;
56 const GtkStateType state_;
57 const bool focused_;
58 const gfx::Size size_;
59
60 DISALLOW_COPY_AND_ASSIGN(ButtonImageSkiaSource);
61 };
62
63 } // namespace
64
Gtk2Border(Gtk2UI * gtk2_ui,views::LabelButton * owning_button,scoped_ptr<views::LabelButtonBorder> border)65 Gtk2Border::Gtk2Border(Gtk2UI* gtk2_ui,
66 views::LabelButton* owning_button,
67 scoped_ptr<views::LabelButtonBorder> border)
68 : gtk2_ui_(gtk2_ui),
69 owning_button_(owning_button),
70 border_(border.Pass()),
71 observer_manager_(this) {
72 observer_manager_.Add(NativeThemeGtk2::instance());
73 }
74
~Gtk2Border()75 Gtk2Border::~Gtk2Border() {
76 }
77
Paint(const views::View & view,gfx::Canvas * canvas)78 void Gtk2Border::Paint(const views::View& view, gfx::Canvas* canvas) {
79 DCHECK_EQ(&view, owning_button_);
80 const NativeThemeDelegate* native_theme_delegate = owning_button_;
81 gfx::Rect rect(native_theme_delegate->GetThemePaintRect());
82 ui::NativeTheme::ExtraParams extra;
83 ui::NativeTheme::State state = native_theme_delegate->GetThemeState(&extra);
84
85 const gfx::Animation* animation = native_theme_delegate->GetThemeAnimation();
86 if (animation && animation->is_animating()) {
87 // Linearly interpolate background and foreground painters during animation.
88 const SkRect sk_rect = gfx::RectToSkRect(rect);
89 canvas->sk_canvas()->saveLayer(&sk_rect, NULL);
90 state = native_theme_delegate->GetBackgroundThemeState(&extra);
91 PaintState(state, extra, rect, canvas);
92
93 SkPaint paint;
94 skia::RefPtr<SkXfermode> sk_lerp_xfer =
95 skia::AdoptRef(SkLerpXfermode::Create(animation->GetCurrentValue()));
96 paint.setXfermode(sk_lerp_xfer.get());
97 canvas->sk_canvas()->saveLayer(&sk_rect, &paint);
98 state = native_theme_delegate->GetForegroundThemeState(&extra);
99 PaintState(state, extra, rect, canvas);
100 canvas->sk_canvas()->restore();
101
102 canvas->sk_canvas()->restore();
103 } else {
104 PaintState(state, extra, rect, canvas);
105 }
106 }
107
GetInsets() const108 gfx::Insets Gtk2Border::GetInsets() const {
109 return border_->GetInsets();
110 }
111
GetMinimumSize() const112 gfx::Size Gtk2Border::GetMinimumSize() const {
113 return border_->GetMinimumSize();
114 }
115
OnNativeThemeUpdated(ui::NativeTheme * observed_theme)116 void Gtk2Border::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
117 DCHECK_EQ(observed_theme, NativeThemeGtk2::instance());
118 for (int i = 0; i < kNumberOfFocusedStates; ++i) {
119 for (int j = 0; j < views::Button::STATE_COUNT; ++j) {
120 button_images_[i][j] = gfx::ImageSkia();
121 }
122 }
123
124 // Our owning view must have its layout invalidated because the insets could
125 // have changed.
126 owning_button_->InvalidateLayout();
127 }
128
PaintState(const ui::NativeTheme::State state,const ui::NativeTheme::ExtraParams & extra,const gfx::Rect & rect,gfx::Canvas * canvas)129 void Gtk2Border::PaintState(const ui::NativeTheme::State state,
130 const ui::NativeTheme::ExtraParams& extra,
131 const gfx::Rect& rect,
132 gfx::Canvas* canvas) {
133 bool focused = extra.button.is_focused;
134 Button::ButtonState views_state = Button::GetButtonStateFrom(state);
135
136 if (border_->GetPainter(focused, views_state) ||
137 (focused && border_->GetPainter(false, views_state))) {
138 gfx::ImageSkia* image = &button_images_[focused][views_state];
139
140 if (image->isNull() || image->size() != rect.size()) {
141 GtkStateType gtk_state = GetGtkState(state);
142 *image = gfx::ImageSkia(
143 new ButtonImageSkiaSource(gtk2_ui_, gtk_state, focused, rect.size()),
144 rect.size());
145 }
146 canvas->DrawImageInt(*image, rect.x(), rect.y());
147 }
148 }
149
150 } // namespace libgtk2ui
151