blob: 76befd6661be937399eacb7a37ab859e3e59f917 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "chrome/browser/ui/gtk/gtk_theme_service.h"
6
7#include <gtk/gtk.h>
8
9#include <set>
10#include <string>
11
12#include "base/debug/trace_event.h"
13#include "base/environment.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000014#include "base/nix/mime_util_xdg.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015#include "base/nix/xdg_util.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000016#include "base/prefs/pref_service.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017#include "base/stl_util.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010018#include "chrome/browser/chrome_notification_types.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "chrome/browser/profiles/profile.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000020#include "chrome/browser/themes/theme_properties.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021#include "chrome/browser/themes/theme_service_factory.h"
22#include "chrome/browser/ui/browser.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000023#include "chrome/browser/ui/browser_iterator.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024#include "chrome/browser/ui/browser_window.h"
25#include "chrome/browser/ui/gtk/chrome_gtk_frame.h"
26#include "chrome/browser/ui/gtk/gtk_chrome_button.h"
27#include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
28#include "chrome/browser/ui/gtk/gtk_util.h"
29#include "chrome/browser/ui/gtk/hover_controller_gtk.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000030#include "chrome/common/extensions/extension.h"
31#include "chrome/common/pref_names.h"
32#include "content/public/browser/notification_details.h"
33#include "content/public/browser/notification_service.h"
34#include "content/public/browser/notification_source.h"
35#include "grit/theme_resources.h"
36#include "grit/ui_resources.h"
37#include "third_party/skia/include/core/SkBitmap.h"
38#include "third_party/skia/include/core/SkCanvas.h"
39#include "third_party/skia/include/core/SkColor.h"
40#include "third_party/skia/include/core/SkShader.h"
41#include "ui/base/gtk/gtk_hig_constants.h"
42#include "ui/base/gtk/gtk_signal_registrar.h"
43#include "ui/base/resource/resource_bundle.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000044#include "ui/base/theme_provider.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000045#include "ui/gfx/canvas.h"
46#include "ui/gfx/color_utils.h"
47#include "ui/gfx/gtk_util.h"
48#include "ui/gfx/image/cairo_cached_surface.h"
49#include "ui/gfx/image/image.h"
50#include "ui/gfx/skbitmap_operations.h"
51#include "ui/gfx/skia_util.h"
52#include "ui/gfx/skia_utils_gtk.h"
53
54namespace {
55
56// The size of the rendered toolbar image.
57const int kToolbarImageWidth = 64;
58const int kToolbarImageHeight = 128;
59
60// How much to tint the GTK+ color lighter at the top of the window.
61const color_utils::HSL kGtkFrameShift = { -1, -1, 0.58 };
62
63// How much to tint the GTK+ color when an explicit frame color hasn't been
64// specified.
65const color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 };
66
67// Values used as the new luminance and saturation values in the inactive tab
68// text color.
69const double kDarkInactiveLuminance = 0.85;
70const double kLightInactiveLuminance = 0.15;
71const double kHeavyInactiveSaturation = 0.7;
72const double kLightInactiveSaturation = 0.3;
73
74// Number of times that the background color should be counted when trying to
75// calculate the border color in GTK theme mode.
76const int kBgWeight = 3;
77
78// Padding to left, top and bottom of vertical separators.
79const int kSeparatorPadding = 2;
80
81// Default color for links on the NTP when the GTK+ theme doesn't define a
82// link color. Constant taken from gtklinkbutton.c.
83const GdkColor kDefaultLinkColor = { 0, 0, 0, 0xeeee };
84
85// Middle color of the separator gradient.
86const double kMidSeparatorColor[] =
87 { 194.0 / 255.0, 205.0 / 255.0, 212.0 / 212.0 };
88// Top color of the separator gradient.
89const double kTopSeparatorColor[] =
90 { 222.0 / 255.0, 234.0 / 255.0, 248.0 / 255.0 };
91
92// A list of images that we provide while in gtk mode.
93const int kThemeImages[] = {
94 IDR_THEME_TOOLBAR,
95 IDR_THEME_TAB_BACKGROUND,
96 IDR_THEME_TAB_BACKGROUND_INCOGNITO,
97 IDR_THEME_FRAME,
98 IDR_THEME_FRAME_INACTIVE,
99 IDR_THEME_FRAME_INCOGNITO,
100 IDR_THEME_FRAME_INCOGNITO_INACTIVE,
101};
102
103// A list of icons used in the autocomplete view that should be tinted to the
104// current gtk theme selection color so they stand out against the GtkEntry's
105// base color.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100106// TODO(erg): Decide what to do about other icons that appear in the omnibox,
107// e.g. content settings icons.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000108const int kAutocompleteImages[] = {
109 IDR_OMNIBOX_EXTENSION_APP,
110 IDR_OMNIBOX_HTTP,
111 IDR_OMNIBOX_HTTP_DARK,
112 IDR_OMNIBOX_SEARCH,
113 IDR_OMNIBOX_SEARCH_DARK,
114 IDR_OMNIBOX_STAR,
115 IDR_OMNIBOX_STAR_DARK,
116 IDR_OMNIBOX_TTS,
117 IDR_OMNIBOX_TTS_DARK,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000118};
119
120bool IsOverridableImage(int id) {
121 CR_DEFINE_STATIC_LOCAL(std::set<int>, images, ());
122 if (images.empty()) {
123 images.insert(kThemeImages, kThemeImages + arraysize(kThemeImages));
124 images.insert(kAutocompleteImages,
125 kAutocompleteImages + arraysize(kAutocompleteImages));
126
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000127 const std::set<int>& buttons =
128 ThemeProperties::GetTintableToolbarButtons();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000129 images.insert(buttons.begin(), buttons.end());
130 }
131
132 return images.count(id) > 0;
133}
134
135// Picks a button tint from a set of background colors. While
136// |accent_gdk_color| will usually be the same color through a theme, this
137// function will get called with the normal GtkLabel |text_color|/GtkWindow
138// |background_color| pair and the GtkEntry |text_color|/|background_color|
139// pair. While 3/4 of the time the resulting tint will be the same, themes that
140// have a dark window background (with light text) and a light text entry (with
141// dark text) will get better icons with this separated out.
142void PickButtonTintFromColors(const GdkColor& accent_gdk_color,
143 const GdkColor& text_color,
144 const GdkColor& background_color,
145 color_utils::HSL* tint) {
146 SkColor accent_color = gfx::GdkColorToSkColor(accent_gdk_color);
147 color_utils::HSL accent_tint;
148 color_utils::SkColorToHSL(accent_color, &accent_tint);
149
150 color_utils::HSL text_tint;
151 color_utils::SkColorToHSL(gfx::GdkColorToSkColor(text_color), &text_tint);
152
153 color_utils::HSL background_tint;
154 color_utils::SkColorToHSL(gfx::GdkColorToSkColor(background_color),
155 &background_tint);
156
157 // If the accent color is gray, then our normal HSL tomfoolery will bring out
158 // whatever color is oddly dominant (for example, in rgb space [125, 128,
159 // 125] will tint green instead of gray). Slight differences (+/-10 (4%) to
160 // all color components) should be interpreted as this color being gray and
161 // we should switch into a special grayscale mode.
162 int rb_diff = abs(SkColorGetR(accent_color) - SkColorGetB(accent_color));
163 int rg_diff = abs(SkColorGetR(accent_color) - SkColorGetG(accent_color));
164 int bg_diff = abs(SkColorGetB(accent_color) - SkColorGetG(accent_color));
165 if (rb_diff < 10 && rg_diff < 10 && bg_diff < 10) {
166 // Our accent is white/gray/black. Only the luminance of the accent color
167 // matters.
168 tint->h = -1;
169
170 // Use the saturation of the text.
171 tint->s = text_tint.s;
172
173 // Use the luminance of the accent color UNLESS there isn't enough
174 // luminance contrast between the accent color and the base color.
175 if (fabs(accent_tint.l - background_tint.l) > 0.3)
176 tint->l = accent_tint.l;
177 else
178 tint->l = text_tint.l;
179 } else {
180 // Our accent is a color.
181 tint->h = accent_tint.h;
182
183 // Don't modify the saturation; the amount of color doesn't matter.
184 tint->s = -1;
185
186 // If the text wants us to darken the icon, don't change the luminance (the
187 // icons are already dark enough). Otherwise, lighten the icon by no more
188 // than 0.9 since we don't want a pure-white icon even if the text is pure
189 // white.
190 if (text_tint.l < 0.5)
191 tint->l = -1;
192 else if (text_tint.l <= 0.9)
193 tint->l = text_tint.l;
194 else
195 tint->l = 0.9;
196 }
197}
198
199
200// Builds and tints the image with |id| to the GtkStateType |state| and
201// places the result in |icon_set|.
202void BuildIconFromIDRWithColor(int id,
203 GtkStyle* style,
204 GtkStateType state,
205 GtkIconSet* icon_set) {
206 SkColor color = gfx::GdkColorToSkColor(style->fg[state]);
207 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
208 SkBitmap original = rb.GetImageNamed(id).AsBitmap();
209
210 SkBitmap fill_color;
211 fill_color.setConfig(SkBitmap::kARGB_8888_Config,
212 original.width(), original.height(), 0);
213 fill_color.allocPixels();
214 fill_color.eraseColor(color);
215 SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(
216 fill_color, original);
217
218 GtkIconSource* icon = gtk_icon_source_new();
219 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(masked);
220 gtk_icon_source_set_pixbuf(icon, pixbuf);
221 g_object_unref(pixbuf);
222
223 gtk_icon_source_set_direction_wildcarded(icon, TRUE);
224 gtk_icon_source_set_size_wildcarded(icon, TRUE);
225
226 gtk_icon_source_set_state(icon, state);
227 // All fields default to wildcarding being on and setting a property doesn't
228 // turn off wildcarding. You need to do this yourself. This is stated once in
229 // the documentation in the gtk_icon_source_new() function, and no where else.
230 gtk_icon_source_set_state_wildcarded(
231 icon, state == GTK_STATE_NORMAL);
232
233 gtk_icon_set_add_source(icon_set, icon);
234 gtk_icon_source_free(icon);
235}
236
237// Applies an HSL shift to a GdkColor (instead of an SkColor)
238void GdkColorHSLShift(const color_utils::HSL& shift, GdkColor* frame_color) {
239 SkColor shifted = color_utils::HSLShift(gfx::GdkColorToSkColor(*frame_color),
240 shift);
241 frame_color->pixel = 0;
242 frame_color->red = SkColorGetR(shifted) * ui::kSkiaToGDKMultiplier;
243 frame_color->green = SkColorGetG(shifted) * ui::kSkiaToGDKMultiplier;
244 frame_color->blue = SkColorGetB(shifted) * ui::kSkiaToGDKMultiplier;
245}
246
247} // namespace
248
249GtkWidget* GtkThemeService::icon_widget_ = NULL;
250base::LazyInstance<gfx::Image> GtkThemeService::default_folder_icon_ =
251 LAZY_INSTANCE_INITIALIZER;
252base::LazyInstance<gfx::Image> GtkThemeService::default_bookmark_icon_ =
253 LAZY_INSTANCE_INITIALIZER;
254
255// static
256GtkThemeService* GtkThemeService::GetFrom(Profile* profile) {
257 return static_cast<GtkThemeService*>(
258 ThemeServiceFactory::GetForProfile(profile));
259}
260
261GtkThemeService::GtkThemeService()
262 : ThemeService(),
263 use_gtk_(false),
264 fake_window_(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
265 fake_frame_(chrome_gtk_frame_new()),
266 signals_(new ui::GtkSignalRegistrar),
267 fullscreen_icon_set_(NULL) {
268 fake_label_.Own(gtk_label_new(""));
269 fake_entry_.Own(gtk_entry_new());
270 fake_menu_item_.Own(gtk_menu_item_new());
271
272 // Only realized widgets receive style-set notifications, which we need to
273 // broadcast new theme images and colors. Only realized widgets have style
274 // properties, too, which we query for some colors.
275 gtk_widget_realize(fake_frame_);
276 gtk_widget_realize(fake_window_);
277 signals_->Connect(fake_frame_, "style-set",
278 G_CALLBACK(&OnStyleSetThunk), this);
279}
280
281GtkThemeService::~GtkThemeService() {
282 gtk_widget_destroy(fake_window_);
283 gtk_widget_destroy(fake_frame_);
284 fake_label_.Destroy();
285 fake_entry_.Destroy();
286 fake_menu_item_.Destroy();
287
288 FreeIconSets();
289
290 // We have to call this because FreePlatformCached() in ~ThemeService
291 // doesn't call the right virutal FreePlatformCaches.
292 FreePlatformCaches();
293}
294
295void GtkThemeService::Init(Profile* profile) {
296 registrar_.Init(profile->GetPrefs());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000297 registrar_.Add(prefs::kUsesSystemTheme,
298 base::Bind(&GtkThemeService::OnUsesSystemThemeChanged,
299 base::Unretained(this)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000300 ThemeService::Init(profile);
301}
302
303gfx::ImageSkia* GtkThemeService::GetImageSkiaNamed(int id) const {
304 // TODO(pkotwicz): Remove this const cast. The gfx::Image interface returns
305 // its images const. GetImageSkiaNamed() also should but has many callsites.
306 return const_cast<gfx::ImageSkia*>(GetImageNamed(id).ToImageSkia());
307}
308
309gfx::Image GtkThemeService::GetImageNamed(int id) const {
310 // Try to get our cached version:
311 ImageCache::const_iterator it = gtk_images_.find(id);
312 if (it != gtk_images_.end())
313 return *it->second;
314
315 if (use_gtk_ && IsOverridableImage(id)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000316 gfx::Image* image = new gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(
317 GenerateGtkThemeBitmap(id)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000318 gtk_images_[id] = image;
319 return *image;
320 }
321
322 return ThemeService::GetImageNamed(id);
323}
324
325SkColor GtkThemeService::GetColor(int id) const {
326 if (use_gtk_) {
327 ColorMap::const_iterator it = colors_.find(id);
328 if (it != colors_.end())
329 return it->second;
330 }
331
332 return ThemeService::GetColor(id);
333}
334
335bool GtkThemeService::HasCustomImage(int id) const {
336 if (use_gtk_)
337 return IsOverridableImage(id);
338
339 return ThemeService::HasCustomImage(id);
340}
341
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000342void GtkThemeService::InitThemesFor(content::NotificationObserver* observer) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000343 observer->Observe(chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
344 content::Source<ThemeService>(this),
345 content::NotificationService::NoDetails());
346}
347
348void GtkThemeService::SetTheme(const extensions::Extension* extension) {
349 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false);
350 LoadDefaultValues();
351 ThemeService::SetTheme(extension);
352}
353
354void GtkThemeService::UseDefaultTheme() {
355 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false);
Ben Murdoch558790d2013-07-30 15:19:42 +0100356 use_gtk_ = false;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000357 LoadDefaultValues();
358 ThemeService::UseDefaultTheme();
359}
360
361void GtkThemeService::SetNativeTheme() {
362 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, true);
Ben Murdoch558790d2013-07-30 15:19:42 +0100363 use_gtk_ = true;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000364 ClearAllThemeData();
365 LoadGtkValues();
366 NotifyThemeChanged();
367}
368
369bool GtkThemeService::UsingDefaultTheme() const {
370 return !use_gtk_ && ThemeService::UsingDefaultTheme();
371}
372
Ben Murdoch558790d2013-07-30 15:19:42 +0100373bool GtkThemeService::ShouldInitWithNativeTheme() const {
374 return profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
375}
376
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000377bool GtkThemeService::UsingNativeTheme() const {
378 return use_gtk_;
379}
380
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000381GtkWidget* GtkThemeService::BuildChromeButton() {
382 GtkWidget* button = HoverControllerGtk::CreateChromeButton();
383 gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button), use_gtk_);
384 chrome_buttons_.push_back(button);
385
386 signals_->Connect(button, "destroy", G_CALLBACK(OnDestroyChromeButtonThunk),
387 this);
388 return button;
389}
390
391GtkWidget* GtkThemeService::BuildChromeLinkButton(const std::string& text) {
392 GtkWidget* link_button = gtk_chrome_link_button_new(text.c_str());
393 gtk_chrome_link_button_set_use_gtk_theme(
394 GTK_CHROME_LINK_BUTTON(link_button),
395 use_gtk_);
396 link_buttons_.push_back(link_button);
397
398 signals_->Connect(link_button, "destroy",
399 G_CALLBACK(OnDestroyChromeLinkButtonThunk), this);
400
401 return link_button;
402}
403
404GtkWidget* GtkThemeService::BuildLabel(const std::string& text,
405 const GdkColor& color) {
406 GtkWidget* label = gtk_label_new(text.empty() ? NULL : text.c_str());
407 if (!use_gtk_)
408 gtk_util::SetLabelColor(label, &color);
409 labels_.insert(std::make_pair(label, color));
410
411 signals_->Connect(label, "destroy", G_CALLBACK(OnDestroyLabelThunk), this);
412
413 return label;
414}
415
416GtkWidget* GtkThemeService::CreateToolbarSeparator() {
417 GtkWidget* separator = gtk_vseparator_new();
418 GtkWidget* alignment = gtk_alignment_new(0, 0, 1, 1);
419 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
420 kSeparatorPadding, kSeparatorPadding, kSeparatorPadding, 0);
421 gtk_container_add(GTK_CONTAINER(alignment), separator);
422
423 signals_->Connect(separator, "expose-event",
424 G_CALLBACK(OnSeparatorExposeThunk), this);
425 return alignment;
426}
427
428GdkColor GtkThemeService::GetGdkColor(int id) const {
429 return gfx::SkColorToGdkColor(GetColor(id));
430}
431
432GdkColor GtkThemeService::GetBorderColor() const {
433 GtkStyle* style = gtk_rc_get_style(fake_window_);
434
435 GdkColor text;
436 GdkColor bg;
437 if (use_gtk_) {
438 text = style->text[GTK_STATE_NORMAL];
439 bg = style->bg[GTK_STATE_NORMAL];
440 } else {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000441 text = GetGdkColor(ThemeProperties::COLOR_BOOKMARK_TEXT);
442 bg = GetGdkColor(ThemeProperties::COLOR_TOOLBAR);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000443 }
444
445 // Creates a weighted average between the text and base color where
446 // the base color counts more than once.
447 GdkColor color;
448 color.pixel = 0;
449 color.red = (text.red + (bg.red * kBgWeight)) / (1 + kBgWeight);
450 color.green = (text.green + (bg.green * kBgWeight)) / (1 + kBgWeight);
451 color.blue = (text.blue + (bg.blue * kBgWeight)) / (1 + kBgWeight);
452
453 return color;
454}
455
456GtkIconSet* GtkThemeService::GetIconSetForId(int id) const {
457 if (id == IDR_FULLSCREEN_MENU_BUTTON)
458 return fullscreen_icon_set_;
459
460 return NULL;
461}
462
463void GtkThemeService::GetScrollbarColors(GdkColor* thumb_active_color,
464 GdkColor* thumb_inactive_color,
465 GdkColor* track_color) {
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100466 GdkColor* theme_thumb_active = NULL;
467 GdkColor* theme_thumb_inactive = NULL;
468 GdkColor* theme_trough_color = NULL;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000469 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
470 "scrollbar-slider-prelight-color", &theme_thumb_active,
471 "scrollbar-slider-normal-color", &theme_thumb_inactive,
472 "scrollbar-trough-color", &theme_trough_color,
473 NULL);
474
475 // Ask the theme if the theme specifies all the scrollbar colors and short
476 // circuit the expensive painting/compositing if we have all of them.
477 if (theme_thumb_active && theme_thumb_inactive && theme_trough_color) {
478 *thumb_active_color = *theme_thumb_active;
479 *thumb_inactive_color = *theme_thumb_inactive;
480 *track_color = *theme_trough_color;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100481 gdk_color_free(theme_thumb_active);
482 gdk_color_free(theme_thumb_inactive);
483 gdk_color_free(theme_trough_color);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000484 return;
485 }
486
487 // Create window containing scrollbar elements
488 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP);
489 GtkWidget* fixed = gtk_fixed_new();
490 GtkWidget* scrollbar = gtk_hscrollbar_new(NULL);
491 gtk_container_add(GTK_CONTAINER(window), fixed);
492 gtk_container_add(GTK_CONTAINER(fixed), scrollbar);
493 gtk_widget_realize(window);
494 gtk_widget_realize(scrollbar);
495
496 // Draw scrollbar thumb part and track into offscreen image
497 const int kWidth = 100;
498 const int kHeight = 20;
499 GtkStyle* style = gtk_rc_get_style(scrollbar);
500 GdkWindow* gdk_window = gtk_widget_get_window(window);
501 GdkPixmap* pm = gdk_pixmap_new(gdk_window, kWidth, kHeight, -1);
502 GdkRectangle rect = { 0, 0, kWidth, kHeight };
503 unsigned char data[3 * kWidth * kHeight];
504 for (int i = 0; i < 3; ++i) {
505 if (i < 2) {
506 // Thumb part
507 gtk_paint_slider(style, pm,
508 i == 0 ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
509 GTK_SHADOW_OUT, &rect, scrollbar, "slider", 0, 0,
510 kWidth, kHeight, GTK_ORIENTATION_HORIZONTAL);
511 } else {
512 // Track
513 gtk_paint_box(style, pm, GTK_STATE_ACTIVE, GTK_SHADOW_IN, &rect,
514 scrollbar, "trough-upper", 0, 0, kWidth, kHeight);
515 }
516 GdkPixbuf* pb = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB,
517 FALSE, 8, kWidth, kHeight,
518 3 * kWidth, 0, 0);
519 gdk_pixbuf_get_from_drawable(pb, pm, NULL, 0, 0, 0, 0, kWidth, kHeight);
520
521 // Sample pixels
522 int components[3] = { 0 };
523 for (int y = 2; y < kHeight - 2; ++y) {
524 for (int c = 0; c < 3; ++c) {
525 // Sample a vertical slice of pixels at about one-thirds from the
526 // left edge. This allows us to avoid any fixed graphics that might be
527 // located at the edges or in the center of the scrollbar.
528 // Each pixel is made up of a red, green, and blue component; taking up
529 // a total of three bytes.
530 components[c] += data[3 * (kWidth / 3 + y * kWidth) + c];
531 }
532 }
533 GdkColor* color = i == 0 ? thumb_active_color :
534 i == 1 ? thumb_inactive_color :
535 track_color;
536 color->pixel = 0;
537 // We sampled pixels across the full height of the image, ignoring a two
538 // pixel border. In some themes, the border has a completely different
539 // color which we do not want to factor into our average color computation.
540 //
541 // We now need to scale the colors from the 0..255 range, to the wider
542 // 0..65535 range, and we need to actually compute the average color; so,
543 // we divide by the total number of pixels in the sample.
544 color->red = components[0] * 65535 / (255 * (kHeight - 4));
545 color->green = components[1] * 65535 / (255 * (kHeight - 4));
546 color->blue = components[2] * 65535 / (255 * (kHeight - 4));
547
548 g_object_unref(pb);
549 }
550 g_object_unref(pm);
551
552 gtk_widget_destroy(window);
553
554 // Override any of the default colors with ones that were specified by the
555 // theme.
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100556 if (theme_thumb_active) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000557 *thumb_active_color = *theme_thumb_active;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100558 gdk_color_free(theme_thumb_active);
559 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000560
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100561 if (theme_thumb_inactive) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000562 *thumb_inactive_color = *theme_thumb_inactive;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100563 gdk_color_free(theme_thumb_inactive);
564 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000565
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100566 if (theme_trough_color) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000567 *track_color = *theme_trough_color;
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100568 gdk_color_free(theme_trough_color);
569 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000570}
571
572// static
573gfx::Image GtkThemeService::GetFolderIcon(bool native) {
574 if (native) {
575 if (!icon_widget_)
576 icon_widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
577
578 if (default_folder_icon_.Get().IsEmpty()) {
579 // This seems to leak.
580 GdkPixbuf* pixbuf = gtk_widget_render_icon(
581 icon_widget_, GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL);
582 if (pixbuf)
583 default_folder_icon_.Get() = gfx::Image(pixbuf);
584 }
585 if (!default_folder_icon_.Get().IsEmpty())
586 return default_folder_icon_.Get();
587 }
588
589 return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
590 IDR_BOOKMARK_BAR_FOLDER);
591}
592
593// static
594gfx::Image GtkThemeService::GetDefaultFavicon(bool native) {
595 if (native) {
596 if (!icon_widget_)
597 icon_widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
598
599 if (default_bookmark_icon_.Get().IsEmpty()) {
600 // This seems to leak.
601 GdkPixbuf* pixbuf = gtk_widget_render_icon(
602 icon_widget_, GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
603 if (pixbuf)
604 default_bookmark_icon_.Get() = gfx::Image(pixbuf);
605 }
606 if (!default_bookmark_icon_.Get().IsEmpty())
607 return default_bookmark_icon_.Get();
608 }
609
610 return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
611 IDR_DEFAULT_FAVICON);
612}
613
614// static
615bool GtkThemeService::DefaultUsesSystemTheme() {
616 scoped_ptr<base::Environment> env(base::Environment::Create());
617
618 switch (base::nix::GetDesktopEnvironment(env.get())) {
619 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
620 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
621 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
622 return true;
623 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
624 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
625 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
626 return false;
627 }
628 // Unless GetDesktopEnvironment() badly misbehaves, this should never happen.
629 NOTREACHED();
630 return false;
631}
632
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000633void GtkThemeService::LoadThemePrefs() {
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100634 // Initialize the values sent to webkit with the default values.
635 // ThemeService::LoadThemePrefs() will replace them with values for the native
636 // gtk theme if necessary.
637 LoadDefaultValues();
638
Ben Murdoch558790d2013-07-30 15:19:42 +0100639 // This takes care of calling SetNativeTheme() if necessary.
640 ThemeService::LoadThemePrefs();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000641
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000642 SetXDGIconTheme();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000643 RebuildMenuIconSets();
644}
645
646void GtkThemeService::NotifyThemeChanged() {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100647 if (!ready_)
648 return;
649
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000650 ThemeService::NotifyThemeChanged();
651
652 // Notify all GtkChromeButtons of their new rendering mode:
653 for (std::vector<GtkWidget*>::iterator it = chrome_buttons_.begin();
654 it != chrome_buttons_.end(); ++it) {
655 gtk_chrome_button_set_use_gtk_rendering(
656 GTK_CHROME_BUTTON(*it), use_gtk_);
657 }
658
659 for (std::vector<GtkWidget*>::iterator it = link_buttons_.begin();
660 it != link_buttons_.end(); ++it) {
661 gtk_chrome_link_button_set_use_gtk_theme(
662 GTK_CHROME_LINK_BUTTON(*it), use_gtk_);
663 }
664
665 for (std::map<GtkWidget*, GdkColor>::iterator it = labels_.begin();
666 it != labels_.end(); ++it) {
667 const GdkColor* color = use_gtk_ ? NULL : &it->second;
668 gtk_util::SetLabelColor(it->first, color);
669 }
670
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000671 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
672 Browser* browser = *it;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000673 if (!browser->window())
674 continue;
675 GtkWindow* window = browser->window()->GetNativeWindow();
676 gtk_util::SetDefaultWindowIcon(window);
677 gtk_util::SetWindowIcon(window, browser->profile());
678 }
679}
680
681void GtkThemeService::FreePlatformCaches() {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100682 colors_.clear();
683 tints_.clear();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000684 ThemeService::FreePlatformCaches();
685 STLDeleteValues(&gtk_images_);
686}
687
688void GtkThemeService::OnStyleSet(GtkWidget* widget,
689 GtkStyle* previous_style) {
690 default_folder_icon_.Get() = gfx::Image();
691 default_bookmark_icon_.Get() = gfx::Image();
692
693 if (profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme)) {
694 ClearAllThemeData();
695 LoadGtkValues();
696 NotifyThemeChanged();
697 }
698
699 RebuildMenuIconSets();
700}
701
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000702void GtkThemeService::SetXDGIconTheme() {
703 gchar* gtk_theme_name;
704 g_object_get(gtk_settings_get_default(),
705 "gtk-icon-theme-name",
706 &gtk_theme_name, NULL);
707 base::nix::SetIconThemeName(gtk_theme_name);
708 g_free(gtk_theme_name);
709}
710
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000711void GtkThemeService::LoadGtkValues() {
712 // Before we start setting images and values, we have to clear out old, stale
713 // values. (If we don't do this, we'll regress startup time in the case where
714 // someone installs a heavyweight theme, then goes back to GTK.)
715 profile()->GetPrefs()->ClearPref(prefs::kCurrentThemeImages);
716
717 GtkStyle* frame_style = gtk_rc_get_style(fake_frame_);
718
719 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000720 SetThemeColorFromGtk(ThemeProperties::COLOR_CONTROL_BACKGROUND,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000721 &window_style->bg[GTK_STATE_NORMAL]);
722
723 GdkColor toolbar_color = window_style->bg[GTK_STATE_NORMAL];
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000724 SetThemeColorFromGtk(ThemeProperties::COLOR_TOOLBAR, &toolbar_color);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000725
726 GdkColor button_color = window_style->bg[GTK_STATE_SELECTED];
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000727 SetThemeTintFromGtk(ThemeProperties::TINT_BUTTONS, &button_color);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000728
729 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get());
730 GdkColor label_color = label_style->fg[GTK_STATE_NORMAL];
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000731 SetThemeColorFromGtk(ThemeProperties::COLOR_TAB_TEXT, &label_color);
732 SetThemeColorFromGtk(ThemeProperties::COLOR_BOOKMARK_TEXT, &label_color);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000733
734 // Build the various icon tints.
735 GetNormalButtonTintHSL(&button_tint_);
736 GetNormalEntryForegroundHSL(&entry_tint_);
737 GetSelectedEntryForegroundHSL(&selected_entry_tint_);
738 GdkColor frame_color = BuildFrameColors(frame_style);
739
740 // The inactive frame color never occurs naturally in the theme, as it is a
741 // tinted version of |frame_color|. We generate another color based on the
742 // background tab color, with the lightness and saturation moved in the
743 // opposite direction. (We don't touch the hue, since there should be subtle
744 // hints of the color in the text.)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000745 color_utils::HSL inactive_tab_text_hsl =
746 tints_[ThemeProperties::TINT_BACKGROUND_TAB];
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000747 if (inactive_tab_text_hsl.l < 0.5)
748 inactive_tab_text_hsl.l = kDarkInactiveLuminance;
749 else
750 inactive_tab_text_hsl.l = kLightInactiveLuminance;
751
752 if (inactive_tab_text_hsl.s < 0.5)
753 inactive_tab_text_hsl.s = kHeavyInactiveSaturation;
754 else
755 inactive_tab_text_hsl.s = kLightInactiveSaturation;
756
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000757 colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] =
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000758 color_utils::HSLToSkColor(inactive_tab_text_hsl, 255);
759
760 // We pick the text and background colors for the NTP out of the colors for a
761 // GtkEntry. We do this because GtkEntries background color is never the same
762 // as |toolbar_color|, is usually a white, and when it isn't a white,
763 // provides sufficient contrast to |toolbar_color|. Try this out with
764 // Darklooks, HighContrastInverse or ThinIce.
765 GtkStyle* entry_style = gtk_rc_get_style(fake_entry_.get());
766 GdkColor ntp_background = entry_style->base[GTK_STATE_NORMAL];
767 GdkColor ntp_foreground = entry_style->text[GTK_STATE_NORMAL];
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000768 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_BACKGROUND,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000769 &ntp_background);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000770 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_TEXT,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000771 &ntp_foreground);
772
773 // The NTP header is the color that surrounds the current active thumbnail on
774 // the NTP, and acts as the border of the "Recent Links" box. It would be
775 // awesome if they were separated so we could use GetBorderColor() for the
776 // border around the "Recent Links" section, but matching the frame color is
777 // more important.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000778 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_HEADER,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000779 &frame_color);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000780 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000781 &toolbar_color);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000782 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_TEXT,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000783 &label_color);
784
785 // Override the link color if the theme provides it.
786 const GdkColor* link_color = NULL;
787 gtk_widget_style_get(GTK_WIDGET(fake_window_),
788 "link-color", &link_color, NULL);
789
790 bool is_default_link_color = false;
791 if (!link_color) {
792 link_color = &kDefaultLinkColor;
793 is_default_link_color = true;
794 }
795
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000796 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000797 link_color);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000798 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK_UNDERLINE,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000799 link_color);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000800 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000801 link_color);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000802 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000803 link_color);
804
805 if (!is_default_link_color)
806 gdk_color_free(const_cast<GdkColor*>(link_color));
807
808 // Generate the colors that we pass to WebKit.
809 focus_ring_color_ = gfx::GdkColorToSkColor(frame_color);
810 GdkColor thumb_active_color, thumb_inactive_color, track_color;
811 GtkThemeService::GetScrollbarColors(&thumb_active_color,
812 &thumb_inactive_color,
813 &track_color);
814 thumb_active_color_ = gfx::GdkColorToSkColor(thumb_active_color);
815 thumb_inactive_color_ = gfx::GdkColorToSkColor(thumb_inactive_color);
816 track_color_ = gfx::GdkColorToSkColor(track_color);
817
818 // Some GTK themes only define the text selection colors on the GtkEntry
819 // class, so we need to use that for getting selection colors.
820 active_selection_bg_color_ =
821 gfx::GdkColorToSkColor(entry_style->base[GTK_STATE_SELECTED]);
822 active_selection_fg_color_ =
823 gfx::GdkColorToSkColor(entry_style->text[GTK_STATE_SELECTED]);
824 inactive_selection_bg_color_ =
825 gfx::GdkColorToSkColor(entry_style->base[GTK_STATE_ACTIVE]);
826 inactive_selection_fg_color_ =
827 gfx::GdkColorToSkColor(entry_style->text[GTK_STATE_ACTIVE]);
828 location_bar_bg_color_ =
829 gfx::GdkColorToSkColor(entry_style->base[GTK_STATE_NORMAL]);
830 location_bar_text_color_ =
831 gfx::GdkColorToSkColor(entry_style->text[GTK_STATE_NORMAL]);
832}
833
834GdkColor GtkThemeService::BuildFrameColors(GtkStyle* frame_style) {
835 GdkColor* theme_frame = NULL;
836 GdkColor* theme_inactive_frame = NULL;
837 GdkColor* theme_incognito_frame = NULL;
838 GdkColor* theme_incognito_inactive_frame = NULL;
839 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
840 "frame-color", &theme_frame,
841 "inactive-frame-color", &theme_inactive_frame,
842 "incognito-frame-color", &theme_incognito_frame,
843 "incognito-inactive-frame-color",
844 &theme_incognito_inactive_frame,
845 NULL);
846
847 GdkColor frame_color = BuildAndSetFrameColor(
848 &frame_style->bg[GTK_STATE_SELECTED],
849 theme_frame,
850 kDefaultFrameShift,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000851 ThemeProperties::COLOR_FRAME,
852 ThemeProperties::TINT_FRAME);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000853 if (theme_frame)
854 gdk_color_free(theme_frame);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000855 SetThemeTintFromGtk(ThemeProperties::TINT_BACKGROUND_TAB, &frame_color);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000856
857 BuildAndSetFrameColor(
858 &frame_style->bg[GTK_STATE_INSENSITIVE],
859 theme_inactive_frame,
860 kDefaultFrameShift,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000861 ThemeProperties::COLOR_FRAME_INACTIVE,
862 ThemeProperties::TINT_FRAME_INACTIVE);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000863 if (theme_inactive_frame)
864 gdk_color_free(theme_inactive_frame);
865
866 BuildAndSetFrameColor(
867 &frame_color,
868 theme_incognito_frame,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000869 ThemeProperties::GetDefaultTint(
870 ThemeProperties::TINT_FRAME_INCOGNITO),
871 ThemeProperties::COLOR_FRAME_INCOGNITO,
872 ThemeProperties::TINT_FRAME_INCOGNITO);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000873 if (theme_incognito_frame)
874 gdk_color_free(theme_incognito_frame);
875
876 BuildAndSetFrameColor(
877 &frame_color,
878 theme_incognito_inactive_frame,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000879 ThemeProperties::GetDefaultTint(
880 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE),
881 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE,
882 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000883 if (theme_incognito_inactive_frame)
884 gdk_color_free(theme_incognito_inactive_frame);
885
886 return frame_color;
887}
888
889void GtkThemeService::LoadDefaultValues() {
890 focus_ring_color_ = SkColorSetARGB(255, 229, 151, 0);
891 thumb_active_color_ = SkColorSetRGB(244, 244, 244);
892 thumb_inactive_color_ = SkColorSetRGB(234, 234, 234);
893 track_color_ = SkColorSetRGB(211, 211, 211);
894
895 active_selection_bg_color_ = SkColorSetRGB(30, 144, 255);
896 active_selection_fg_color_ = SK_ColorWHITE;
897 inactive_selection_bg_color_ = SkColorSetRGB(200, 200, 200);
898 inactive_selection_fg_color_ = SkColorSetRGB(50, 50, 50);
899}
900
901void GtkThemeService::RebuildMenuIconSets() {
902 FreeIconSets();
903
904 GtkStyle* style = gtk_rc_get_style(fake_menu_item_.get());
905
906 fullscreen_icon_set_ = gtk_icon_set_new();
907 BuildIconFromIDRWithColor(IDR_FULLSCREEN_MENU_BUTTON,
908 style,
909 GTK_STATE_PRELIGHT,
910 fullscreen_icon_set_);
911 BuildIconFromIDRWithColor(IDR_FULLSCREEN_MENU_BUTTON,
912 style,
913 GTK_STATE_NORMAL,
914 fullscreen_icon_set_);
915}
916
917void GtkThemeService::SetThemeColorFromGtk(int id, const GdkColor* color) {
918 colors_[id] = gfx::GdkColorToSkColor(*color);
919}
920
921void GtkThemeService::SetThemeTintFromGtk(int id, const GdkColor* color) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000922 color_utils::HSL default_tint = ThemeProperties::GetDefaultTint(id);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000923 color_utils::HSL hsl;
924 color_utils::SkColorToHSL(gfx::GdkColorToSkColor(*color), &hsl);
925
926 if (default_tint.s != -1)
927 hsl.s = default_tint.s;
928
929 if (default_tint.l != -1)
930 hsl.l = default_tint.l;
931
932 tints_[id] = hsl;
933}
934
935GdkColor GtkThemeService::BuildAndSetFrameColor(const GdkColor* base,
936 const GdkColor* gtk_base,
937 const color_utils::HSL& tint,
938 int color_id,
939 int tint_id) {
940 GdkColor out_color = *base;
941 if (gtk_base) {
942 // The theme author specified a color to use, use it without modification.
943 out_color = *gtk_base;
944 } else {
945 // Tint the basic color since this is a heuristic color instead of one
946 // specified by the theme author.
947 GdkColorHSLShift(tint, &out_color);
948 }
949 SetThemeColorFromGtk(color_id, &out_color);
950 SetThemeTintFromGtk(tint_id, &out_color);
951
952 return out_color;
953}
954
955void GtkThemeService::FreeIconSets() {
956 if (fullscreen_icon_set_) {
957 gtk_icon_set_unref(fullscreen_icon_set_);
958 fullscreen_icon_set_ = NULL;
959 }
960}
961
962SkBitmap GtkThemeService::GenerateGtkThemeBitmap(int id) const {
963 switch (id) {
964 case IDR_THEME_TOOLBAR: {
965 GtkStyle* style = gtk_rc_get_style(fake_window_);
966 GdkColor* color = &style->bg[GTK_STATE_NORMAL];
967 SkBitmap bitmap;
968 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
969 kToolbarImageWidth, kToolbarImageHeight);
970 bitmap.allocPixels();
971 bitmap.eraseRGB(color->red >> 8, color->green >> 8, color->blue >> 8);
972 return bitmap;
973 }
974 case IDR_THEME_TAB_BACKGROUND:
975 return GenerateTabImage(IDR_THEME_FRAME);
976 case IDR_THEME_TAB_BACKGROUND_INCOGNITO:
977 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO);
978 case IDR_THEME_FRAME:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000979 return GenerateFrameImage(ThemeProperties::COLOR_FRAME,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000980 "frame-gradient-color");
981 case IDR_THEME_FRAME_INACTIVE:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000982 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INACTIVE,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000983 "inactive-frame-gradient-color");
984 case IDR_THEME_FRAME_INCOGNITO:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000985 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INCOGNITO,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000986 "incognito-frame-gradient-color");
987 case IDR_THEME_FRAME_INCOGNITO_INACTIVE: {
988 return GenerateFrameImage(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000989 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000990 "incognito-inactive-frame-gradient-color");
991 }
992 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and
993 // instead should tint based on the foreground text entry color in GTK+
994 // mode because some themes that try to be dark *and* light have very
995 // different colors between the omnibox and the normal background area.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100996 // TODO(erg): Decide what to do about other icons that appear in the
997 // omnibox, e.g. content settings icons.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000998 case IDR_OMNIBOX_EXTENSION_APP:
999 case IDR_OMNIBOX_HTTP:
1000 case IDR_OMNIBOX_SEARCH:
1001 case IDR_OMNIBOX_STAR:
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001002 case IDR_OMNIBOX_TTS: {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001003 return GenerateTintedIcon(id, entry_tint_);
1004 }
1005 // In GTK mode, the dark versions of the omnibox icons only ever appear in
1006 // the autocomplete popup and only against the current theme's GtkEntry
1007 // base[GTK_STATE_SELECTED] color, so tint the icons so they won't collide
1008 // with the selected color.
1009 case IDR_OMNIBOX_EXTENSION_APP_DARK:
1010 case IDR_OMNIBOX_HTTP_DARK:
1011 case IDR_OMNIBOX_SEARCH_DARK:
1012 case IDR_OMNIBOX_STAR_DARK:
1013 case IDR_OMNIBOX_TTS_DARK: {
1014 return GenerateTintedIcon(id, selected_entry_tint_);
1015 }
1016 default: {
1017 return GenerateTintedIcon(id, button_tint_);
1018 }
1019 }
1020}
1021
1022SkBitmap GtkThemeService::GenerateFrameImage(
1023 int color_id,
1024 const char* gradient_name) const {
1025 // We use two colors: the main color (passed in) and a lightened version of
1026 // that color (which is supposed to match the light gradient at the top of
1027 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird).
1028 ColorMap::const_iterator it = colors_.find(color_id);
1029 DCHECK(it != colors_.end());
1030 SkColor base = it->second;
1031
1032 gfx::Canvas canvas(gfx::Size(kToolbarImageWidth, kToolbarImageHeight),
1033 ui::SCALE_FACTOR_100P, true);
1034
1035 int gradient_size;
1036 GdkColor* gradient_top_color = NULL;
1037 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
1038 "frame-gradient-size", &gradient_size,
1039 gradient_name, &gradient_top_color,
1040 NULL);
1041 if (gradient_size) {
1042 SkColor lighter = gradient_top_color ?
1043 gfx::GdkColorToSkColor(*gradient_top_color) :
1044 color_utils::HSLShift(base, kGtkFrameShift);
1045 if (gradient_top_color)
1046 gdk_color_free(gradient_top_color);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001047 skia::RefPtr<SkShader> shader = gfx::CreateGradientShader(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001048 0, gradient_size, lighter, base);
1049 SkPaint paint;
1050 paint.setStyle(SkPaint::kFill_Style);
1051 paint.setAntiAlias(true);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001052 paint.setShader(shader.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001053
1054 canvas.DrawRect(gfx::Rect(0, 0, kToolbarImageWidth, gradient_size), paint);
1055 }
1056
1057 canvas.FillRect(gfx::Rect(0, gradient_size, kToolbarImageWidth,
1058 kToolbarImageHeight - gradient_size), base);
1059 return canvas.ExtractImageRep().sk_bitmap();
1060}
1061
1062SkBitmap GtkThemeService::GenerateTabImage(int base_id) const {
1063 SkBitmap base_image = GetImageNamed(base_id).AsBitmap();
1064 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001065 base_image, GetTint(ThemeProperties::TINT_BACKGROUND_TAB));
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001066 return SkBitmapOperations::CreateTiledBitmap(
1067 bg_tint, 0, 0, bg_tint.width(), bg_tint.height());
1068}
1069
1070SkBitmap GtkThemeService::GenerateTintedIcon(
1071 int base_id,
1072 const color_utils::HSL& tint) const {
1073 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
1074 return SkBitmapOperations::CreateHSLShiftedBitmap(
1075 rb.GetImageNamed(base_id).AsBitmap(), tint);
1076}
1077
1078void GtkThemeService::GetNormalButtonTintHSL(
1079 color_utils::HSL* tint) const {
1080 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
1081 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED];
1082 const GdkColor base_color = window_style->base[GTK_STATE_NORMAL];
1083
1084 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get());
1085 const GdkColor text_color = label_style->fg[GTK_STATE_NORMAL];
1086
1087 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint);
1088}
1089
1090void GtkThemeService::GetNormalEntryForegroundHSL(
1091 color_utils::HSL* tint) const {
1092 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
1093 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED];
1094
1095 GtkStyle* style = gtk_rc_get_style(fake_entry_.get());
1096 const GdkColor text_color = style->text[GTK_STATE_NORMAL];
1097 const GdkColor base_color = style->base[GTK_STATE_NORMAL];
1098
1099 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint);
1100}
1101
1102void GtkThemeService::GetSelectedEntryForegroundHSL(
1103 color_utils::HSL* tint) const {
1104 // The simplest of all the tints. We just use the selected text in the entry
1105 // since the icons tinted this way will only be displayed against
1106 // base[GTK_STATE_SELECTED].
1107 GtkStyle* style = gtk_rc_get_style(fake_entry_.get());
1108 const GdkColor color = style->text[GTK_STATE_SELECTED];
1109 color_utils::SkColorToHSL(gfx::GdkColorToSkColor(color), tint);
1110}
1111
1112void GtkThemeService::OnDestroyChromeButton(GtkWidget* button) {
1113 std::vector<GtkWidget*>::iterator it =
1114 find(chrome_buttons_.begin(), chrome_buttons_.end(), button);
1115 if (it != chrome_buttons_.end())
1116 chrome_buttons_.erase(it);
1117}
1118
1119void GtkThemeService::OnDestroyChromeLinkButton(GtkWidget* button) {
1120 std::vector<GtkWidget*>::iterator it =
1121 find(link_buttons_.begin(), link_buttons_.end(), button);
1122 if (it != link_buttons_.end())
1123 link_buttons_.erase(it);
1124}
1125
1126void GtkThemeService::OnDestroyLabel(GtkWidget* button) {
1127 std::map<GtkWidget*, GdkColor>::iterator it = labels_.find(button);
1128 if (it != labels_.end())
1129 labels_.erase(it);
1130}
1131
1132gboolean GtkThemeService::OnSeparatorExpose(GtkWidget* widget,
1133 GdkEventExpose* event) {
1134 UNSHIPPED_TRACE_EVENT0("ui::gtk", "GtkThemeService::OnSeparatorExpose");
1135 if (UsingNativeTheme())
1136 return FALSE;
1137
1138 cairo_t* cr = gdk_cairo_create(gtk_widget_get_window(widget));
1139 gdk_cairo_rectangle(cr, &event->area);
1140 cairo_clip(cr);
1141
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001142 GdkColor bottom_color = GetGdkColor(ThemeProperties::COLOR_TOOLBAR);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001143 double bottom_color_rgb[] = {
1144 static_cast<double>(bottom_color.red / 257) / 255.0,
1145 static_cast<double>(bottom_color.green / 257) / 255.0,
1146 static_cast<double>(bottom_color.blue / 257) / 255.0, };
1147
1148 GtkAllocation allocation;
1149 gtk_widget_get_allocation(widget, &allocation);
1150
1151 cairo_pattern_t* pattern =
1152 cairo_pattern_create_linear(allocation.x, allocation.y,
1153 allocation.x,
1154 allocation.y + allocation.height);
1155 cairo_pattern_add_color_stop_rgb(
1156 pattern, 0.0,
1157 kTopSeparatorColor[0], kTopSeparatorColor[1], kTopSeparatorColor[2]);
1158 cairo_pattern_add_color_stop_rgb(
1159 pattern, 0.5,
1160 kMidSeparatorColor[0], kMidSeparatorColor[1], kMidSeparatorColor[2]);
1161 cairo_pattern_add_color_stop_rgb(
1162 pattern, 1.0,
1163 bottom_color_rgb[0], bottom_color_rgb[1], bottom_color_rgb[2]);
1164 cairo_set_source(cr, pattern);
1165
1166 double start_x = 0.5 + allocation.x;
1167 cairo_new_path(cr);
1168 cairo_set_line_width(cr, 1.0);
1169 cairo_move_to(cr, start_x, allocation.y);
1170 cairo_line_to(cr, start_x, allocation.y + allocation.height);
1171 cairo_stroke(cr);
1172 cairo_destroy(cr);
1173 cairo_pattern_destroy(pattern);
1174
1175 return TRUE;
1176}
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001177
1178void GtkThemeService::OnUsesSystemThemeChanged() {
1179 use_gtk_ = profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
1180}