summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/gtk_theme_provider.cc
diff options
context:
space:
mode:
authorerg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-13 23:23:08 +0000
committererg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-13 23:23:08 +0000
commit5fdafb2f68bd61ef92d10194402348d1e5839015 (patch)
treefb8583216c733e2a2218501646cf8c72fec35bb1 /chrome/browser/gtk/gtk_theme_provider.cc
parent0735266658f996210b6f43142ce7f4c55f47ae13 (diff)
downloadchromium_src-5fdafb2f68bd61ef92d10194402348d1e5839015.zip
chromium_src-5fdafb2f68bd61ef92d10194402348d1e5839015.tar.gz
chromium_src-5fdafb2f68bd61ef92d10194402348d1e5839015.tar.bz2
GTK Themes: Refactored to use notifications instead of manual plumbing.
- Removes large amounts of plumbing because: - All GtkChromeButtons are constructed from GtkThemeProvider which keeps a reference to all live buttons and sends them theme change notifications. - CustomDrawButtons now subscribe themselves to the BROWSER_THEME_CHANGED notification; this gets rid of a LOT of plubming. - Removes the GtkThemeProperties struct; just pass the theme provider around. - Move all the constants from the themes namespace to class statics, per tony's suggestion Review URL: http://codereview.chromium.org/149547 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20561 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/gtk/gtk_theme_provider.cc')
-rw-r--r--chrome/browser/gtk/gtk_theme_provider.cc136
1 files changed, 101 insertions, 35 deletions
diff --git a/chrome/browser/gtk/gtk_theme_provider.cc b/chrome/browser/gtk/gtk_theme_provider.cc
index c42a038..91136db 100644
--- a/chrome/browser/gtk/gtk_theme_provider.cc
+++ b/chrome/browser/gtk/gtk_theme_provider.cc
@@ -9,7 +9,12 @@
#include "base/gfx/gtk_util.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/profile.h"
+#include "chrome/browser/gtk/gtk_chrome_button.h"
#include "chrome/common/pref_names.h"
+#include "chrome/common/notification_details.h"
+#include "chrome/common/notification_service.h"
+#include "chrome/common/notification_source.h"
+#include "chrome/common/notification_type.h"
#include "grit/theme_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
@@ -23,6 +28,11 @@ const int kToolbarImageHeight = 128;
} // namespace
+// static
+GtkThemeProvider* GtkThemeProvider::GetFrom(Profile* profile) {
+ return static_cast<GtkThemeProvider*>(profile->GetThemeProvider());
+}
+
GtkThemeProvider::GtkThemeProvider()
: BrowserThemeProvider(),
fake_window_(gtk_window_new(GTK_WINDOW_TOPLEVEL)) {
@@ -33,7 +43,28 @@ GtkThemeProvider::GtkThemeProvider()
}
GtkThemeProvider::~GtkThemeProvider() {
+ profile()->GetPrefs()->RemovePrefObserver(prefs::kUsesSystemTheme, this);
gtk_widget_destroy(fake_window_);
+
+ // Disconnect from the destroy signal of any redisual widgets in
+ // |chrome_buttons_|.
+ for (std::vector<GtkWidget*>::iterator it = chrome_buttons_.begin();
+ it != chrome_buttons_.end(); ++it) {
+ gtk_signal_disconnect_by_data(GTK_OBJECT(*it), this);
+ }
+}
+
+void GtkThemeProvider::Init(Profile* profile) {
+ profile->GetPrefs()->AddPrefObserver(prefs::kUsesSystemTheme, this);
+ use_gtk_ = profile->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
+
+ BrowserThemeProvider::Init(profile);
+}
+
+void GtkThemeProvider::InitThemesFor(NotificationObserver* observer) {
+ observer->Observe(NotificationType::BROWSER_THEME_CHANGED,
+ Source<ThemeProvider>(this),
+ NotificationService::NoDetails());
}
void GtkThemeProvider::SetTheme(Extension* extension) {
@@ -53,21 +84,60 @@ void GtkThemeProvider::SetNativeTheme() {
NotifyThemeChanged();
}
-// static
-bool GtkThemeProvider::UseSystemThemeGraphics(Profile* profile) {
- return profile->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
+void GtkThemeProvider::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ if (type == NotificationType::PREF_CHANGED) {
+ std::wstring key = *Details<std::wstring>(details).ptr();
+ if (key == prefs::kUsesSystemTheme) {
+ use_gtk_ = profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
+ }
+ }
+}
+
+GtkWidget* GtkThemeProvider::BuildChromeButton() {
+ GtkWidget* button = gtk_chrome_button_new();
+ gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button), use_gtk_);
+ chrome_buttons_.push_back(button);
+
+ g_signal_connect(button, "destroy", G_CALLBACK(OnDestroyChromeButton),
+ this);
+ return button;
+}
+
+bool GtkThemeProvider::UseGtkTheme() {
+ return use_gtk_;
+}
+
+GdkColor GtkThemeProvider::GetGdkColor(int id) {
+ SkColor color = GetColor(id);
+ GdkColor gdkcolor =
+ GDK_COLOR_RGB(SkColorGetR(color), SkColorGetG(color),
+ SkColorGetB(color));
+ return gdkcolor;
}
void GtkThemeProvider::LoadThemePrefs() {
- if (profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme)) {
+ if (use_gtk_) {
LoadGtkValues();
} else {
BrowserThemeProvider::LoadThemePrefs();
}
}
+void GtkThemeProvider::NotifyThemeChanged() {
+ BrowserThemeProvider::NotifyThemeChanged();
+
+ // Notify all constructored GtkChromeButtons of their new rendering mode:
+ for (std::vector<GtkWidget*>::iterator it = chrome_buttons_.begin();
+ it != chrome_buttons_.end(); ++it) {
+ gtk_chrome_button_set_use_gtk_rendering(
+ GTK_CHROME_BUTTON(*it), use_gtk_);
+ }
+}
+
SkBitmap* GtkThemeProvider::LoadThemeBitmap(int id) {
- if (id == IDR_THEME_TOOLBAR && UseSystemThemeGraphics(profile())) {
+ if (id == IDR_THEME_TOOLBAR && use_gtk_) {
GtkStyle* style = gtk_rc_get_style(fake_window_);
GdkColor* color = &style->bg[GTK_STATE_NORMAL];
SkBitmap* bitmap = new SkBitmap;
@@ -95,38 +165,38 @@ void GtkThemeProvider::OnStyleSet(GtkWidget* widget,
void GtkThemeProvider::LoadGtkValues() {
GtkStyle* style = gtk_rc_get_style(fake_window_);
- SetThemeColorFromGtk(themes::kColorFrame, &style->bg[GTK_STATE_SELECTED]);
+ SetThemeColorFromGtk(kColorFrame, &style->bg[GTK_STATE_SELECTED]);
// Skip COLOR_FRAME_INACTIVE and the incognito colors, as they will be
// autogenerated from tints.
- SetThemeColorFromGtk(themes::kColorToolbar,
+ SetThemeColorFromGtk(kColorToolbar,
&style->bg[GTK_STATE_NORMAL]);
- SetThemeColorFromGtk(themes::kColorTabText,
+ SetThemeColorFromGtk(kColorTabText,
&style->text[GTK_STATE_NORMAL]);
- SetThemeColorFromGtk(themes::kColorBackgroundTabText,
+ SetThemeColorFromGtk(kColorBackgroundTabText,
&style->text[GTK_STATE_NORMAL]);
- SetThemeColorFromGtk(themes::kColorBookmarkText,
+ SetThemeColorFromGtk(kColorBookmarkText,
&style->text[GTK_STATE_NORMAL]);
- SetThemeColorFromGtk(themes::kColorControlBackground,
+ SetThemeColorFromGtk(kColorControlBackground,
&style->bg[GTK_STATE_NORMAL]);
- SetThemeColorFromGtk(themes::kColorButtonBackground,
+ SetThemeColorFromGtk(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,
+ SetThemeTintFromGtk(kTintButtons, &style->bg[GTK_STATE_SELECTED],
+ kDefaultTintButtons);
+ SetThemeTintFromGtk(kTintFrame, &style->bg[GTK_STATE_SELECTED],
+ kDefaultTintFrame);
+ SetThemeTintFromGtk(kTintFrameInactive,
&style->bg[GTK_STATE_SELECTED],
- themes::kDefaultTintFrameInactive);
- SetThemeTintFromGtk(themes::kTintFrameIncognito,
+ kDefaultTintFrameInactive);
+ SetThemeTintFromGtk(kTintFrameIncognito,
&style->bg[GTK_STATE_SELECTED],
- themes::kDefaultTintFrameIncognito);
- SetThemeTintFromGtk(themes::kTintFrameIncognitoInactive,
+ kDefaultTintFrameIncognito);
+ SetThemeTintFromGtk(kTintFrameIncognitoInactive,
&style->bg[GTK_STATE_SELECTED],
- themes::kDefaultTintFrameIncognitoInactive);
- SetThemeTintFromGtk(themes::kTintBackgroundTab,
+ kDefaultTintFrameIncognitoInactive);
+ SetThemeTintFromGtk(kTintBackgroundTab,
&style->bg[GTK_STATE_SELECTED],
- themes::kDefaultTintBackgroundTab);
+ kDefaultTintBackgroundTab);
GenerateFrameColors();
GenerateFrameImages();
@@ -152,15 +222,11 @@ void GtkThemeProvider::SetThemeTintFromGtk(const char* id, GdkColor* color,
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;
+void GtkThemeProvider::OnDestroyChromeButton(GtkWidget* button,
+ GtkThemeProvider* provider) {
+ std::vector<GtkWidget*>::iterator it =
+ find(provider->chrome_buttons_.begin(), provider->chrome_buttons_.end(),
+ button);
+ if (it != provider->chrome_buttons_.end())
+ provider->chrome_buttons_.erase(it);
}