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