| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ |
| 6 #define CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ | 6 #define CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include "chrome/browser/ui/gtk/gtk_theme_provider.h" |
| 10 #include <vector> | 10 // TODO(msw): remove this file once all includes have been updated. |
| 11 | |
| 12 #include "app/gtk_integers.h" | |
| 13 #include "app/gtk_signal.h" | |
| 14 #include "base/scoped_ptr.h" | |
| 15 #include "chrome/browser/prefs/pref_change_registrar.h" | |
| 16 #include "chrome/browser/gtk/owned_widget_gtk.h" | |
| 17 #include "chrome/browser/themes/browser_theme_provider.h" | |
| 18 #include "chrome/common/notification_observer.h" | |
| 19 #include "gfx/color_utils.h" | |
| 20 | |
| 21 class CairoCachedSurface; | |
| 22 class GtkSignalRegistrar; | |
| 23 class Profile; | |
| 24 | |
| 25 typedef struct _GdkDisplay GdkDisplay; | |
| 26 typedef struct _GdkEventExpose GdkEventExpose; | |
| 27 typedef struct _GdkPixbuf GdkPixbuf; | |
| 28 typedef struct _GtkIconSet GtkIconSet; | |
| 29 typedef struct _GtkStyle GtkStyle; | |
| 30 typedef struct _GtkWidget GtkWidget; | |
| 31 | |
| 32 // Specialization of BrowserThemeProvider which supplies system colors. | |
| 33 class GtkThemeProvider : public BrowserThemeProvider, | |
| 34 public NotificationObserver { | |
| 35 public: | |
| 36 // Returns GtkThemeProvider, casted from our superclass. | |
| 37 static GtkThemeProvider* GetFrom(Profile* profile); | |
| 38 | |
| 39 GtkThemeProvider(); | |
| 40 virtual ~GtkThemeProvider(); | |
| 41 | |
| 42 // Calls |observer|.Observe() for the browser theme with this provider as the | |
| 43 // source. | |
| 44 void InitThemesFor(NotificationObserver* observer); | |
| 45 | |
| 46 // Overridden from BrowserThemeProvider: | |
| 47 // | |
| 48 // Sets that we aren't using the system theme, then calls | |
| 49 // BrowserThemeProvider's implementation. | |
| 50 virtual void Init(Profile* profile); | |
| 51 virtual SkBitmap* GetBitmapNamed(int id) const; | |
| 52 virtual SkColor GetColor(int id) const; | |
| 53 virtual bool HasCustomImage(int id) const; | |
| 54 virtual void SetTheme(const Extension* extension); | |
| 55 virtual void UseDefaultTheme(); | |
| 56 virtual void SetNativeTheme(); | |
| 57 virtual bool UsingDefaultTheme(); | |
| 58 | |
| 59 // Overridden from NotificationObserver: | |
| 60 virtual void Observe(NotificationType type, | |
| 61 const NotificationSource& source, | |
| 62 const NotificationDetails& details); | |
| 63 | |
| 64 // Creates a GtkChromeButton instance, registered with this theme provider, | |
| 65 // with a "destroy" signal to remove it from our internal list when it goes | |
| 66 // away. | |
| 67 GtkWidget* BuildChromeButton(); | |
| 68 | |
| 69 // Creates a theme-aware vertical separator widget. | |
| 70 GtkWidget* CreateToolbarSeparator(); | |
| 71 | |
| 72 // Whether we should use the GTK system theme. | |
| 73 bool UseGtkTheme() const; | |
| 74 | |
| 75 // A wrapper around ThemeProvider::GetColor, transforming the result to a | |
| 76 // GdkColor. | |
| 77 GdkColor GetGdkColor(int id) const; | |
| 78 | |
| 79 // A weighted average between the text color and the background color of a | |
| 80 // label. Used for borders between GTK stuff and the webcontent. | |
| 81 GdkColor GetBorderColor() const; | |
| 82 | |
| 83 // Returns a set of icons tinted for different GtkStateTypes based on the | |
| 84 // label colors for the IDR resource |id|. | |
| 85 GtkIconSet* GetIconSetForId(int id) const; | |
| 86 | |
| 87 // This method returns the colors webkit will use for the scrollbars. When no | |
| 88 // colors are specified by the GTK+ theme, this function averages of the | |
| 89 // thumb part and of the track colors. | |
| 90 void GetScrollbarColors(GdkColor* thumb_active_color, | |
| 91 GdkColor* thumb_inactive_color, | |
| 92 GdkColor* track_color); | |
| 93 | |
| 94 // Expose the inner label. Only used for testing. | |
| 95 GtkWidget* fake_label() { return fake_label_.get(); } | |
| 96 | |
| 97 // Returns a CairoCachedSurface for a particular Display. CairoCachedSurfaces | |
| 98 // (hopefully) live on the X server, instead of the client so we don't have | |
| 99 // to send the image to the server on each expose. | |
| 100 CairoCachedSurface* GetSurfaceNamed(int id, GtkWidget* widget_on_display); | |
| 101 | |
| 102 // Same as above, but auto-mirrors the underlying pixbuf in RTL mode. | |
| 103 CairoCachedSurface* GetRTLEnabledSurfaceNamed(int id, | |
| 104 GtkWidget* widget_on_display); | |
| 105 | |
| 106 // Same as above, but gets the resource from the ResourceBundle instead of the | |
| 107 // BrowserThemeProvider. | |
| 108 // NOTE: Never call this with resource IDs that are ever passed to the above | |
| 109 // two functions! Depending on which call comes first, all callers will | |
| 110 // either get the themed or the unthemed version. | |
| 111 CairoCachedSurface* GetUnthemedSurfaceNamed(int id, | |
| 112 GtkWidget* widget_on_display); | |
| 113 | |
| 114 // Returns colors that we pass to webkit to match the system theme. | |
| 115 const SkColor& get_focus_ring_color() const { return focus_ring_color_; } | |
| 116 const SkColor& get_thumb_active_color() const { return thumb_active_color_; } | |
| 117 const SkColor& get_thumb_inactive_color() const { | |
| 118 return thumb_inactive_color_; | |
| 119 } | |
| 120 const SkColor& get_track_color() const { return track_color_; } | |
| 121 const SkColor& get_active_selection_bg_color() const { | |
| 122 return active_selection_bg_color_; | |
| 123 } | |
| 124 const SkColor& get_active_selection_fg_color() const { | |
| 125 return active_selection_fg_color_; | |
| 126 } | |
| 127 const SkColor& get_inactive_selection_bg_color() const { | |
| 128 return inactive_selection_bg_color_; | |
| 129 } | |
| 130 const SkColor& get_inactive_selection_fg_color() const { | |
| 131 return inactive_selection_fg_color_; | |
| 132 } | |
| 133 | |
| 134 // These functions do not add a ref to the returned pixbuf, and it should not | |
| 135 // be unreffed. If |native| is true, get the GTK_STOCK version of the icon. | |
| 136 static GdkPixbuf* GetFolderIcon(bool native); | |
| 137 static GdkPixbuf* GetDefaultFavicon(bool native); | |
| 138 | |
| 139 // Whether we use the GTK theme by default in the current desktop | |
| 140 // environment. Returns true when we GTK defaults to on. | |
| 141 static bool DefaultUsesSystemTheme(); | |
| 142 | |
| 143 private: | |
| 144 typedef std::map<int, SkColor> ColorMap; | |
| 145 typedef std::map<int, color_utils::HSL> TintMap; | |
| 146 typedef std::map<int, SkBitmap*> ImageCache; | |
| 147 typedef std::map<int, CairoCachedSurface*> CairoCachedSurfaceMap; | |
| 148 typedef std::map<GdkDisplay*, CairoCachedSurfaceMap> PerDisplaySurfaceMap; | |
| 149 | |
| 150 // Clears all the GTK color overrides. | |
| 151 virtual void ClearAllThemeData(); | |
| 152 | |
| 153 // Load theme data from preferences, possibly picking colors from GTK. | |
| 154 virtual void LoadThemePrefs(); | |
| 155 | |
| 156 // Let all the browser views know that themes have changed. | |
| 157 virtual void NotifyThemeChanged(const Extension* extension); | |
| 158 | |
| 159 // Additionally frees the CairoCachedSurfaces. | |
| 160 virtual void FreePlatformCaches(); | |
| 161 | |
| 162 // Extracts colors and tints from the GTK theme, both for the | |
| 163 // BrowserThemeProvider interface and the colors we send to webkit. | |
| 164 void LoadGtkValues(); | |
| 165 | |
| 166 // Reads in explicit theme frame colors from the ChromeGtkFrame style class | |
| 167 // or generates them per our fallback algorithm. | |
| 168 GdkColor BuildFrameColors(GtkStyle* frame_style); | |
| 169 | |
| 170 // Sets the values that we send to webkit to safe defaults. | |
| 171 void LoadDefaultValues(); | |
| 172 | |
| 173 // Builds all of the tinted menus images needed for custom buttons. This is | |
| 174 // always called on style-set even if we aren't using the gtk-theme because | |
| 175 // the menus are always rendered with gtk colors. | |
| 176 void RebuildMenuIconSets(); | |
| 177 | |
| 178 // Sets the underlying theme colors/tints from a GTK color. | |
| 179 void SetThemeColorFromGtk(int id, const GdkColor* color); | |
| 180 void SetThemeTintFromGtk(int id, const GdkColor* color); | |
| 181 | |
| 182 // Creates and returns a frame color, either using |gtk_base| verbatim if | |
| 183 // non-NULL, or tinting |base| with |tint|. Also sets |color_id| and | |
| 184 // |tint_id| to the returned color. | |
| 185 GdkColor BuildAndSetFrameColor(const GdkColor* base, | |
| 186 const GdkColor* gtk_base, | |
| 187 const color_utils::HSL& tint, | |
| 188 int color_id, | |
| 189 int tint_id); | |
| 190 | |
| 191 // Split out from FreePlatformCaches so it can be called in our destructor; | |
| 192 // FreePlatformCaches() is called from the BrowserThemeProvider's destructor, | |
| 193 // but by the time ~BrowserThemeProvider() is run, the vtable no longer | |
| 194 // points to GtkThemeProvider's version. | |
| 195 void FreePerDisplaySurfaces(PerDisplaySurfaceMap* per_display_map); | |
| 196 | |
| 197 // Frees all the created GtkIconSets we use for the chrome menu. | |
| 198 void FreeIconSets(); | |
| 199 | |
| 200 // Lazily generates each bitmap used in the gtk theme. | |
| 201 SkBitmap* GenerateGtkThemeBitmap(int id) const; | |
| 202 | |
| 203 // Creates a GTK+ version of IDR_THEME_FRAME. Instead of tinting, this | |
| 204 // creates a theme configurable gradient ending with |color_id| at the | |
| 205 // bottom, and |gradient_name| at the top if that color is specified in the | |
| 206 // theme. | |
| 207 SkBitmap* GenerateFrameImage(int color_id, | |
| 208 const char* gradient_name) const; | |
| 209 | |
| 210 // Takes the base frame image |base_id| and tints it with |tint_id|. | |
| 211 SkBitmap* GenerateTabImage(int base_id) const; | |
| 212 | |
| 213 // Tints an icon based on tint. | |
| 214 SkBitmap* GenerateTintedIcon(int base_id, color_utils::HSL tint) const; | |
| 215 | |
| 216 // Returns the tint for buttons that contrasts with the normal window | |
| 217 // background color. | |
| 218 void GetNormalButtonTintHSL(color_utils::HSL* tint) const; | |
| 219 | |
| 220 // Returns a tint that's the color of the current normal text in an entry. | |
| 221 void GetNormalEntryForegroundHSL(color_utils::HSL* tint) const; | |
| 222 | |
| 223 // Returns a tint that's the color of the current highlighted text in an | |
| 224 // entry. | |
| 225 void GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const; | |
| 226 | |
| 227 // Implements GetXXXSurfaceNamed(), given the appropriate pixbuf to use. | |
| 228 CairoCachedSurface* GetSurfaceNamedImpl(int id, | |
| 229 PerDisplaySurfaceMap* surface_map, | |
| 230 GdkPixbuf* pixbuf, | |
| 231 GtkWidget* widget_on_display); | |
| 232 | |
| 233 // Handles signal from GTK that our theme has been changed. | |
| 234 CHROMEGTK_CALLBACK_1(GtkThemeProvider, void, OnStyleSet, GtkStyle*); | |
| 235 | |
| 236 // A notification from the GtkChromeButton GObject destructor that we should | |
| 237 // remove it from our internal list. | |
| 238 CHROMEGTK_CALLBACK_0(GtkThemeProvider, void, OnDestroyChromeButton); | |
| 239 | |
| 240 CHROMEGTK_CALLBACK_1(GtkThemeProvider, gboolean, OnSeparatorExpose, | |
| 241 GdkEventExpose*); | |
| 242 | |
| 243 // Whether we should be using gtk rendering. | |
| 244 bool use_gtk_; | |
| 245 | |
| 246 // GtkWidgets that exist only so we can look at their properties (and take | |
| 247 // their colors). | |
| 248 GtkWidget* fake_window_; | |
| 249 GtkWidget* fake_frame_; | |
| 250 OwnedWidgetGtk fake_label_; | |
| 251 OwnedWidgetGtk fake_entry_; | |
| 252 OwnedWidgetGtk fake_menu_item_; | |
| 253 | |
| 254 // A list of all GtkChromeButton instances. We hold on to these to notify | |
| 255 // them of theme changes. | |
| 256 std::vector<GtkWidget*> chrome_buttons_; | |
| 257 | |
| 258 // Tracks all the signals we have connected to on various widgets. | |
| 259 scoped_ptr<GtkSignalRegistrar> signals_; | |
| 260 | |
| 261 // Tints and colors calculated by LoadGtkValues() that are given to the | |
| 262 // caller while |use_gtk_| is true. | |
| 263 ColorMap colors_; | |
| 264 TintMap tints_; | |
| 265 | |
| 266 // Colors used to tint certain icons. | |
| 267 color_utils::HSL button_tint_; | |
| 268 color_utils::HSL entry_tint_; | |
| 269 color_utils::HSL selected_entry_tint_; | |
| 270 | |
| 271 // Colors that we pass to WebKit. These are generated each time the theme | |
| 272 // changes. | |
| 273 SkColor focus_ring_color_; | |
| 274 SkColor thumb_active_color_; | |
| 275 SkColor thumb_inactive_color_; | |
| 276 SkColor track_color_; | |
| 277 SkColor active_selection_bg_color_; | |
| 278 SkColor active_selection_fg_color_; | |
| 279 SkColor inactive_selection_bg_color_; | |
| 280 SkColor inactive_selection_fg_color_; | |
| 281 | |
| 282 // A GtkIconSet that has the tinted icons for the NORMAL and PRELIGHT states | |
| 283 // of the IDR_FULLSCREEN_MENU_BUTTON tinted to the respective menu item label | |
| 284 // colors. | |
| 285 GtkIconSet* fullscreen_icon_set_; | |
| 286 | |
| 287 // Image cache of lazily created images, created when requested by | |
| 288 // GetBitmapNamed(). | |
| 289 mutable ImageCache gtk_images_; | |
| 290 | |
| 291 // Cairo surfaces for each GdkDisplay. | |
| 292 PerDisplaySurfaceMap per_display_surfaces_; | |
| 293 PerDisplaySurfaceMap per_display_unthemed_surfaces_; | |
| 294 | |
| 295 PrefChangeRegistrar registrar_; | |
| 296 | |
| 297 // This is a dummy widget that only exists so we have something to pass to | |
| 298 // gtk_widget_render_icon(). | |
| 299 static GtkWidget* icon_widget_; | |
| 300 | |
| 301 // The default folder icon and default bookmark icon for the GTK theme. | |
| 302 // These are static because the system can only have one theme at a time. | |
| 303 // They are cached when they are requested the first time, and cleared when | |
| 304 // the system theme changes. | |
| 305 static GdkPixbuf* default_folder_icon_; | |
| 306 static GdkPixbuf* default_bookmark_icon_; | |
| 307 }; | |
| 308 | 11 |
| 309 #endif // CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ | 12 #endif // CHROME_BROWSER_GTK_GTK_THEME_PROVIDER_H_ |
| OLD | NEW |