1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/gtk/gtk_theme_provider.h"
#include <gtk/gtk.h>
#include "base/gfx/gtk_util.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/profile.h"
#include "chrome/common/pref_names.h"
#include "grit/theme_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
namespace {
// The size of the rendered toolbar image.
const int kToolbarImageWidth = 64;
const int kToolbarImageHeight = 128;
} // namespace
GtkThemeProvider::GtkThemeProvider()
: BrowserThemeProvider(),
fake_window_(gtk_window_new(GTK_WINDOW_TOPLEVEL)) {
// Only realized widgets receive style-set notifications, which we need to
// broadcast new theme images and colors.
gtk_widget_realize(fake_window_);
g_signal_connect(fake_window_, "style-set", G_CALLBACK(&OnStyleSet), this);
}
GtkThemeProvider::~GtkThemeProvider() {
gtk_widget_destroy(fake_window_);
}
void GtkThemeProvider::SetTheme(Extension* extension) {
profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false);
BrowserThemeProvider::SetTheme(extension);
}
void GtkThemeProvider::UseDefaultTheme() {
profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false);
BrowserThemeProvider::UseDefaultTheme();
}
void GtkThemeProvider::SetNativeTheme() {
profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, true);
ClearAllThemeData();
LoadGtkValues();
NotifyThemeChanged();
}
// static
bool GtkThemeProvider::UseSystemThemeGraphics(Profile* profile) {
return profile->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
}
void GtkThemeProvider::LoadThemePrefs() {
if (profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme)) {
LoadGtkValues();
} else {
BrowserThemeProvider::LoadThemePrefs();
}
}
SkBitmap* GtkThemeProvider::LoadThemeBitmap(int id) {
if (id == IDR_THEME_TOOLBAR && UseSystemThemeGraphics(profile())) {
GtkStyle* style = gtk_rc_get_style(fake_window_);
GdkColor* color = &style->bg[GTK_STATE_NORMAL];
SkBitmap* bitmap = new SkBitmap;
bitmap->setConfig(SkBitmap::kARGB_8888_Config,
kToolbarImageWidth, kToolbarImageHeight);
bitmap->allocPixels();
bitmap->eraseRGB(color->red >> 8, color->green >> 8, color->blue >> 8);
return bitmap;
} else {
return BrowserThemeProvider::LoadThemeBitmap(id);
}
}
// static
void GtkThemeProvider::OnStyleSet(GtkWidget* widget,
GtkStyle* previous_style,
GtkThemeProvider* provider) {
if (provider->profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme)) {
provider->ClearAllThemeData();
provider->LoadGtkValues();
provider->NotifyThemeChanged();
}
}
void GtkThemeProvider::LoadGtkValues() {
GtkStyle* style = gtk_rc_get_style(fake_window_);
SetThemeColorFromGtk(themes::kColorFrame, &style->bg[GTK_STATE_SELECTED]);
// Skip COLOR_FRAME_INACTIVE and the incognito colors, as they will be
// autogenerated from tints.
SetThemeColorFromGtk(themes::kColorToolbar,
&style->bg[GTK_STATE_NORMAL]);
SetThemeColorFromGtk(themes::kColorTabText,
&style->text[GTK_STATE_NORMAL]);
SetThemeColorFromGtk(themes::kColorBackgroundTabText,
&style->text[GTK_STATE_NORMAL]);
SetThemeColorFromGtk(themes::kColorBookmarkText,
&style->text[GTK_STATE_NORMAL]);
SetThemeColorFromGtk(themes::kColorControlBackground,
&style->bg[GTK_STATE_NORMAL]);
SetThemeColorFromGtk(themes::kColorButtonBackground,
&style->bg[GTK_STATE_NORMAL]);
SetThemeTintFromGtk(themes::kTintButtons, &style->bg[GTK_STATE_SELECTED],
themes::kDefaultTintButtons);
SetThemeTintFromGtk(themes::kTintFrame, &style->bg[GTK_STATE_SELECTED],
themes::kDefaultTintFrame);
SetThemeTintFromGtk(themes::kTintFrameInactive,
&style->bg[GTK_STATE_SELECTED],
themes::kDefaultTintFrameInactive);
SetThemeTintFromGtk(themes::kTintFrameIncognito,
&style->bg[GTK_STATE_SELECTED],
themes::kDefaultTintFrameIncognito);
SetThemeTintFromGtk(themes::kTintFrameIncognitoInactive,
&style->bg[GTK_STATE_SELECTED],
themes::kDefaultTintFrameIncognitoInactive);
SetThemeTintFromGtk(themes::kTintBackgroundTab,
&style->bg[GTK_STATE_SELECTED],
themes::kDefaultTintBackgroundTab);
GenerateFrameColors();
GenerateFrameImages();
}
void GtkThemeProvider::SetThemeColorFromGtk(const char* id, GdkColor* color) {
SetColor(id, SkColorSetRGB(color->red >> 8,
color->green >> 8,
color->blue >> 8));
}
void GtkThemeProvider::SetThemeTintFromGtk(const char* id, GdkColor* color,
const skia::HSL& default_tint) {
skia::HSL hsl;
skia::SkColorToHSL(SkColorSetRGB((color->red >> 8),
(color->green >> 8),
(color->blue >> 8)), hsl);
if (default_tint.s != -1)
hsl.s = default_tint.s;
if (default_tint.l != -1)
hsl.l = default_tint.l;
SetTint(id, hsl);
}
GtkThemeProperties::GtkThemeProperties(Profile* profile)
: use_gtk_rendering(GtkThemeProvider::UseSystemThemeGraphics(profile)),
provider(profile->GetThemeProvider()) {
}
GdkColor GtkThemeProperties::GetGdkColor(int id) {
SkColor color = provider->GetColor(id);
GdkColor gdkcolor =
GDK_COLOR_RGB(SkColorGetR(color), SkColorGetG(color),
SkColorGetB(color));
return gdkcolor;
}
|