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