| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 <set> | |
| 10 | |
| 11 #include "app/gtk_signal_registrar.h" | |
| 12 #include "app/resource_bundle.h" | |
| 13 #include "base/environment.h" | |
| 14 #include "base/stl_util-inl.h" | |
| 15 #include "base/nix/xdg_util.h" | |
| 16 #include "chrome/browser/gtk/cairo_cached_surface.h" | |
| 17 #include "chrome/browser/gtk/chrome_gtk_frame.h" | |
| 18 #include "chrome/browser/gtk/gtk_chrome_button.h" | |
| 19 #include "chrome/browser/gtk/gtk_util.h" | |
| 20 #include "chrome/browser/gtk/hover_controller_gtk.h" | |
| 21 #include "chrome/browser/metrics/user_metrics.h" | |
| 22 #include "chrome/browser/prefs/pref_service.h" | |
| 23 #include "chrome/browser/profiles/profile.h" | |
| 24 #include "chrome/common/notification_details.h" | |
| 25 #include "chrome/common/notification_service.h" | |
| 26 #include "chrome/common/notification_source.h" | |
| 27 #include "chrome/common/notification_type.h" | |
| 28 #include "chrome/common/pref_names.h" | |
| 29 #include "gfx/canvas_skia.h" | |
| 30 #include "gfx/color_utils.h" | |
| 31 #include "gfx/gtk_util.h" | |
| 32 #include "gfx/skbitmap_operations.h" | |
| 33 #include "gfx/skia_util.h" | |
| 34 #include "gfx/skia_utils_gtk.h" | |
| 35 #include "grit/app_resources.h" | |
| 36 #include "grit/theme_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 | |
| 42 namespace { | |
| 43 | |
| 44 // The size of the rendered toolbar image. | |
| 45 const int kToolbarImageWidth = 64; | |
| 46 const int kToolbarImageHeight = 128; | |
| 47 | |
| 48 // How much to tint the GTK+ color lighter at the top of the window. | |
| 49 const color_utils::HSL kGtkFrameShift = { -1, -1, 0.58 }; | |
| 50 | |
| 51 // How much to tint the GTK+ color when an explicit frame color hasn't been | |
| 52 // specified. | |
| 53 const color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 }; | |
| 54 | |
| 55 // Values used as the new luminance and saturation values in the inactive tab | |
| 56 // text color. | |
| 57 const double kDarkInactiveLuminance = 0.85; | |
| 58 const double kLightInactiveLuminance = 0.15; | |
| 59 const double kHeavyInactiveSaturation = 0.7; | |
| 60 const double kLightInactiveSaturation = 0.3; | |
| 61 | |
| 62 // Number of times that the background color should be counted when trying to | |
| 63 // calculate the border color in GTK theme mode. | |
| 64 const int kBgWeight = 3; | |
| 65 | |
| 66 // Padding to left, top and bottom of vertical separators. | |
| 67 const int kSeparatorPadding = 2; | |
| 68 | |
| 69 // Default color for links on the NTP when the GTK+ theme doesn't define a | |
| 70 // link color. Constant taken from gtklinkbutton.c. | |
| 71 const GdkColor kDefaultLinkColor = { 0, 0, 0, 0xeeee }; | |
| 72 | |
| 73 // Middle color of the separator gradient. | |
| 74 const double kMidSeparatorColor[] = | |
| 75 { 194.0 / 255.0, 205.0 / 255.0, 212.0 / 212.0 }; | |
| 76 // Top color of the separator gradient. | |
| 77 const double kTopSeparatorColor[] = | |
| 78 { 222.0 / 255.0, 234.0 / 255.0, 248.0 / 255.0 }; | |
| 79 | |
| 80 // Converts a GdkColor to a SkColor. | |
| 81 SkColor GdkToSkColor(const GdkColor* color) { | |
| 82 return SkColorSetRGB(color->red >> 8, | |
| 83 color->green >> 8, | |
| 84 color->blue >> 8); | |
| 85 } | |
| 86 | |
| 87 // A list of images that we provide while in gtk mode. | |
| 88 const int kThemeImages[] = { | |
| 89 IDR_THEME_TOOLBAR, | |
| 90 IDR_THEME_TAB_BACKGROUND, | |
| 91 IDR_THEME_TAB_BACKGROUND_INCOGNITO, | |
| 92 IDR_THEME_FRAME, | |
| 93 IDR_THEME_FRAME_INACTIVE, | |
| 94 IDR_THEME_FRAME_INCOGNITO, | |
| 95 IDR_THEME_FRAME_INCOGNITO_INACTIVE, | |
| 96 }; | |
| 97 | |
| 98 // A list of icons used in the autocomplete view that should be tinted to the | |
| 99 // current gtk theme selection color so they stand out against the GtkEntry's | |
| 100 // base color. | |
| 101 const int kAutocompleteImages[] = { | |
| 102 IDR_OMNIBOX_HTTP, | |
| 103 IDR_OMNIBOX_HTTP_DARK, | |
| 104 IDR_OMNIBOX_HISTORY, | |
| 105 IDR_OMNIBOX_HISTORY_DARK, | |
| 106 IDR_OMNIBOX_SEARCH, | |
| 107 IDR_OMNIBOX_SEARCH_DARK, | |
| 108 IDR_OMNIBOX_MORE, | |
| 109 IDR_OMNIBOX_MORE_DARK, | |
| 110 IDR_OMNIBOX_STAR, | |
| 111 IDR_OMNIBOX_STAR_DARK, | |
| 112 IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON, | |
| 113 IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON | |
| 114 }; | |
| 115 | |
| 116 bool IsOverridableImage(int id) { | |
| 117 static std::set<int> images; | |
| 118 if (images.empty()) { | |
| 119 images.insert(kThemeImages, kThemeImages + arraysize(kThemeImages)); | |
| 120 images.insert(kAutocompleteImages, | |
| 121 kAutocompleteImages + arraysize(kAutocompleteImages)); | |
| 122 | |
| 123 const std::set<int>& buttons = | |
| 124 BrowserThemeProvider::GetTintableToolbarButtons(); | |
| 125 images.insert(buttons.begin(), buttons.end()); | |
| 126 } | |
| 127 | |
| 128 return images.count(id) > 0; | |
| 129 } | |
| 130 | |
| 131 // Picks a button tint from a set of background colors. While | |
| 132 // |accent_gdk_color| will usually be the same color through a theme, this | |
| 133 // function will get called with the normal GtkLabel |text_color|/GtkWindow | |
| 134 // |background_color| pair and the GtkEntry |text_color|/|background_color| | |
| 135 // pair. While 3/4 of the time the resulting tint will be the same, themes that | |
| 136 // have a dark window background (with light text) and a light text entry (with | |
| 137 // dark text) will get better icons with this separated out. | |
| 138 void PickButtonTintFromColors(const GdkColor& accent_gdk_color, | |
| 139 const GdkColor& text_color, | |
| 140 const GdkColor& background_color, | |
| 141 color_utils::HSL* tint) { | |
| 142 SkColor accent_color = GdkToSkColor(&accent_gdk_color); | |
| 143 color_utils::HSL accent_tint; | |
| 144 color_utils::SkColorToHSL(accent_color, &accent_tint); | |
| 145 | |
| 146 color_utils::HSL text_tint; | |
| 147 color_utils::SkColorToHSL(GdkToSkColor(&text_color), &text_tint); | |
| 148 | |
| 149 color_utils::HSL background_tint; | |
| 150 color_utils::SkColorToHSL(GdkToSkColor(&background_color), &background_tint); | |
| 151 | |
| 152 // If the accent color is gray, then our normal HSL tomfoolery will bring out | |
| 153 // whatever color is oddly dominant (for example, in rgb space [125, 128, | |
| 154 // 125] will tint green instead of gray). Slight differences (+/-10 (4%) to | |
| 155 // all color components) should be interpreted as this color being gray and | |
| 156 // we should switch into a special grayscale mode. | |
| 157 int rb_diff = abs(SkColorGetR(accent_color) - SkColorGetB(accent_color)); | |
| 158 int rg_diff = abs(SkColorGetR(accent_color) - SkColorGetG(accent_color)); | |
| 159 int bg_diff = abs(SkColorGetB(accent_color) - SkColorGetG(accent_color)); | |
| 160 if (rb_diff < 10 && rg_diff < 10 && bg_diff < 10) { | |
| 161 // Our accent is white/gray/black. Only the luminance of the accent color | |
| 162 // matters. | |
| 163 tint->h = -1; | |
| 164 | |
| 165 // Use the saturation of the text. | |
| 166 tint->s = text_tint.s; | |
| 167 | |
| 168 // Use the luminance of the accent color UNLESS there isn't enough | |
| 169 // luminance contrast between the accent color and the base color. | |
| 170 if (fabs(accent_tint.l - background_tint.l) > 0.3) | |
| 171 tint->l = accent_tint.l; | |
| 172 else | |
| 173 tint->l = text_tint.l; | |
| 174 } else { | |
| 175 // Our accent is a color. | |
| 176 tint->h = accent_tint.h; | |
| 177 | |
| 178 // Don't modify the saturation; the amount of color doesn't matter. | |
| 179 tint->s = -1; | |
| 180 | |
| 181 // If the text wants us to darken the icon, don't change the luminance (the | |
| 182 // icons are already dark enough). Otherwise, lighten the icon by no more | |
| 183 // than 0.9 since we don't want a pure-white icon even if the text is pure | |
| 184 // white. | |
| 185 if (text_tint.l < 0.5) | |
| 186 tint->l = -1; | |
| 187 else if (text_tint.l <= 0.9) | |
| 188 tint->l = text_tint.l; | |
| 189 else | |
| 190 tint->l = 0.9; | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 | |
| 195 // Builds and tints the image with |id| to the GtkStateType |state| and | |
| 196 // places the result in |icon_set|. | |
| 197 void BuildIconFromIDRWithColor(int id, | |
| 198 GtkStyle* style, | |
| 199 GtkStateType state, | |
| 200 GtkIconSet* icon_set) { | |
| 201 SkColor color = GdkToSkColor(&style->fg[state]); | |
| 202 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 203 SkBitmap original = *rb.GetBitmapNamed(id); | |
| 204 | |
| 205 SkBitmap fill_color; | |
| 206 fill_color.setConfig(SkBitmap::kARGB_8888_Config, | |
| 207 original.width(), original.height(), 0); | |
| 208 fill_color.allocPixels(); | |
| 209 fill_color.eraseColor(color); | |
| 210 SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap( | |
| 211 fill_color, original); | |
| 212 | |
| 213 GtkIconSource* icon = gtk_icon_source_new(); | |
| 214 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&masked); | |
| 215 gtk_icon_source_set_pixbuf(icon, pixbuf); | |
| 216 g_object_unref(pixbuf); | |
| 217 | |
| 218 gtk_icon_source_set_direction_wildcarded(icon, TRUE); | |
| 219 gtk_icon_source_set_size_wildcarded(icon, TRUE); | |
| 220 | |
| 221 gtk_icon_source_set_state(icon, state); | |
| 222 // All fields default to wildcarding being on and setting a property doesn't | |
| 223 // turn off wildcarding. You need to do this yourself. This is stated once in | |
| 224 // the documentation in the gtk_icon_source_new() function, and no where else. | |
| 225 gtk_icon_source_set_state_wildcarded( | |
| 226 icon, state == GTK_STATE_NORMAL); | |
| 227 | |
| 228 gtk_icon_set_add_source(icon_set, icon); | |
| 229 gtk_icon_source_free(icon); | |
| 230 } | |
| 231 | |
| 232 // Applies an HSL shift to a GdkColor (instead of an SkColor) | |
| 233 void GdkColorHSLShift(const color_utils::HSL& shift, GdkColor* frame_color) { | |
| 234 SkColor shifted = color_utils::HSLShift(GdkToSkColor(frame_color), shift); | |
| 235 frame_color->pixel = 0; | |
| 236 frame_color->red = SkColorGetR(shifted) * kSkiaToGDKMultiplier; | |
| 237 frame_color->green = SkColorGetG(shifted) * kSkiaToGDKMultiplier; | |
| 238 frame_color->blue = SkColorGetB(shifted) * kSkiaToGDKMultiplier; | |
| 239 } | |
| 240 | |
| 241 } // namespace | |
| 242 | |
| 243 GtkWidget* GtkThemeProvider::icon_widget_ = NULL; | |
| 244 GdkPixbuf* GtkThemeProvider::default_folder_icon_ = NULL; | |
| 245 GdkPixbuf* GtkThemeProvider::default_bookmark_icon_ = NULL; | |
| 246 | |
| 247 // static | |
| 248 GtkThemeProvider* GtkThemeProvider::GetFrom(Profile* profile) { | |
| 249 return static_cast<GtkThemeProvider*>(profile->GetThemeProvider()); | |
| 250 } | |
| 251 | |
| 252 GtkThemeProvider::GtkThemeProvider() | |
| 253 : BrowserThemeProvider(), | |
| 254 fake_window_(gtk_window_new(GTK_WINDOW_TOPLEVEL)), | |
| 255 fake_frame_(chrome_gtk_frame_new()), | |
| 256 signals_(new GtkSignalRegistrar), | |
| 257 fullscreen_icon_set_(NULL) { | |
| 258 fake_label_.Own(gtk_label_new("")); | |
| 259 fake_entry_.Own(gtk_entry_new()); | |
| 260 fake_menu_item_.Own(gtk_menu_item_new()); | |
| 261 | |
| 262 // Only realized widgets receive style-set notifications, which we need to | |
| 263 // broadcast new theme images and colors. Only realized widgets have style | |
| 264 // properties, too, which we query for some colors. | |
| 265 gtk_widget_realize(fake_frame_); | |
| 266 gtk_widget_realize(fake_window_); | |
| 267 signals_->Connect(fake_frame_, "style-set", | |
| 268 G_CALLBACK(&OnStyleSetThunk), this); | |
| 269 } | |
| 270 | |
| 271 GtkThemeProvider::~GtkThemeProvider() { | |
| 272 gtk_widget_destroy(fake_window_); | |
| 273 gtk_widget_destroy(fake_frame_); | |
| 274 fake_label_.Destroy(); | |
| 275 fake_entry_.Destroy(); | |
| 276 fake_menu_item_.Destroy(); | |
| 277 | |
| 278 FreeIconSets(); | |
| 279 | |
| 280 // We have to call this because FreePlatformCached() in ~BrowserThemeProvider | |
| 281 // doesn't call the right virutal FreePlatformCaches. | |
| 282 FreePlatformCaches(); | |
| 283 } | |
| 284 | |
| 285 void GtkThemeProvider::Init(Profile* profile) { | |
| 286 registrar_.Init(profile->GetPrefs()); | |
| 287 registrar_.Add(prefs::kUsesSystemTheme, this); | |
| 288 use_gtk_ = profile->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme); | |
| 289 | |
| 290 BrowserThemeProvider::Init(profile); | |
| 291 } | |
| 292 | |
| 293 SkBitmap* GtkThemeProvider::GetBitmapNamed(int id) const { | |
| 294 // Try to get our cached version: | |
| 295 ImageCache::const_iterator it = gtk_images_.find(id); | |
| 296 if (it != gtk_images_.end()) | |
| 297 return it->second; | |
| 298 | |
| 299 if (use_gtk_ && IsOverridableImage(id)) { | |
| 300 // We haven't built this image yet: | |
| 301 SkBitmap* bitmap = GenerateGtkThemeBitmap(id); | |
| 302 gtk_images_[id] = bitmap; | |
| 303 return bitmap; | |
| 304 } | |
| 305 | |
| 306 return BrowserThemeProvider::GetBitmapNamed(id); | |
| 307 } | |
| 308 | |
| 309 SkColor GtkThemeProvider::GetColor(int id) const { | |
| 310 if (use_gtk_) { | |
| 311 ColorMap::const_iterator it = colors_.find(id); | |
| 312 if (it != colors_.end()) | |
| 313 return it->second; | |
| 314 } | |
| 315 | |
| 316 return BrowserThemeProvider::GetColor(id); | |
| 317 } | |
| 318 | |
| 319 bool GtkThemeProvider::HasCustomImage(int id) const { | |
| 320 if (use_gtk_) | |
| 321 return IsOverridableImage(id); | |
| 322 | |
| 323 return BrowserThemeProvider::HasCustomImage(id); | |
| 324 } | |
| 325 | |
| 326 void GtkThemeProvider::InitThemesFor(NotificationObserver* observer) { | |
| 327 observer->Observe(NotificationType::BROWSER_THEME_CHANGED, | |
| 328 Source<ThemeProvider>(this), | |
| 329 NotificationService::NoDetails()); | |
| 330 } | |
| 331 | |
| 332 void GtkThemeProvider::SetTheme(const Extension* extension) { | |
| 333 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false); | |
| 334 LoadDefaultValues(); | |
| 335 BrowserThemeProvider::SetTheme(extension); | |
| 336 } | |
| 337 | |
| 338 void GtkThemeProvider::UseDefaultTheme() { | |
| 339 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false); | |
| 340 LoadDefaultValues(); | |
| 341 BrowserThemeProvider::UseDefaultTheme(); | |
| 342 } | |
| 343 | |
| 344 void GtkThemeProvider::SetNativeTheme() { | |
| 345 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, true); | |
| 346 ClearAllThemeData(); | |
| 347 LoadGtkValues(); | |
| 348 NotifyThemeChanged(NULL); | |
| 349 } | |
| 350 | |
| 351 bool GtkThemeProvider::UsingDefaultTheme() { | |
| 352 return !use_gtk_ && BrowserThemeProvider::UsingDefaultTheme(); | |
| 353 } | |
| 354 | |
| 355 void GtkThemeProvider::Observe(NotificationType type, | |
| 356 const NotificationSource& source, | |
| 357 const NotificationDetails& details) { | |
| 358 if ((type == NotificationType::PREF_CHANGED) && | |
| 359 (*Details<std::string>(details).ptr() == prefs::kUsesSystemTheme)) | |
| 360 use_gtk_ = profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme); | |
| 361 } | |
| 362 | |
| 363 GtkWidget* GtkThemeProvider::BuildChromeButton() { | |
| 364 GtkWidget* button = HoverControllerGtk::CreateChromeButton(); | |
| 365 gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button), use_gtk_); | |
| 366 chrome_buttons_.push_back(button); | |
| 367 | |
| 368 signals_->Connect(button, "destroy", G_CALLBACK(OnDestroyChromeButtonThunk), | |
| 369 this); | |
| 370 return button; | |
| 371 } | |
| 372 | |
| 373 GtkWidget* GtkThemeProvider::CreateToolbarSeparator() { | |
| 374 GtkWidget* separator = gtk_vseparator_new(); | |
| 375 GtkWidget* alignment = gtk_alignment_new(0, 0, 1, 1); | |
| 376 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), | |
| 377 kSeparatorPadding, kSeparatorPadding, kSeparatorPadding, 0); | |
| 378 gtk_container_add(GTK_CONTAINER(alignment), separator); | |
| 379 | |
| 380 signals_->Connect(separator, "expose-event", | |
| 381 G_CALLBACK(OnSeparatorExposeThunk), this); | |
| 382 return alignment; | |
| 383 } | |
| 384 | |
| 385 bool GtkThemeProvider::UseGtkTheme() const { | |
| 386 return use_gtk_; | |
| 387 } | |
| 388 | |
| 389 GdkColor GtkThemeProvider::GetGdkColor(int id) const { | |
| 390 return gfx::SkColorToGdkColor(GetColor(id)); | |
| 391 } | |
| 392 | |
| 393 GdkColor GtkThemeProvider::GetBorderColor() const { | |
| 394 GtkStyle* style = gtk_rc_get_style(fake_window_); | |
| 395 | |
| 396 GdkColor text; | |
| 397 GdkColor bg; | |
| 398 if (use_gtk_) { | |
| 399 text = style->text[GTK_STATE_NORMAL]; | |
| 400 bg = style->bg[GTK_STATE_NORMAL]; | |
| 401 } else { | |
| 402 text = GetGdkColor(COLOR_BOOKMARK_TEXT); | |
| 403 bg = GetGdkColor(COLOR_TOOLBAR); | |
| 404 } | |
| 405 | |
| 406 // Creates a weighted average between the text and base color where | |
| 407 // the base color counts more than once. | |
| 408 GdkColor color; | |
| 409 color.pixel = 0; | |
| 410 color.red = (text.red + (bg.red * kBgWeight)) / (1 + kBgWeight); | |
| 411 color.green = (text.green + (bg.green * kBgWeight)) / (1 + kBgWeight); | |
| 412 color.blue = (text.blue + (bg.blue * kBgWeight)) / (1 + kBgWeight); | |
| 413 | |
| 414 return color; | |
| 415 } | |
| 416 | |
| 417 GtkIconSet* GtkThemeProvider::GetIconSetForId(int id) const { | |
| 418 if (id == IDR_FULLSCREEN_MENU_BUTTON) | |
| 419 return fullscreen_icon_set_; | |
| 420 | |
| 421 return NULL; | |
| 422 } | |
| 423 | |
| 424 void GtkThemeProvider::GetScrollbarColors(GdkColor* thumb_active_color, | |
| 425 GdkColor* thumb_inactive_color, | |
| 426 GdkColor* track_color) { | |
| 427 const GdkColor* theme_thumb_active = NULL; | |
| 428 const GdkColor* theme_thumb_inactive = NULL; | |
| 429 const GdkColor* theme_trough_color = NULL; | |
| 430 gtk_widget_style_get(GTK_WIDGET(fake_frame_), | |
| 431 "scrollbar-slider-prelight-color", &theme_thumb_active, | |
| 432 "scrollbar-slider-normal-color", &theme_thumb_inactive, | |
| 433 "scrollbar-trough-color", &theme_trough_color, | |
| 434 NULL); | |
| 435 | |
| 436 // Ask the theme if the theme specifies all the scrollbar colors and short | |
| 437 // circuit the expensive painting/compositing if we have all of them. | |
| 438 if (theme_thumb_active && theme_thumb_inactive && theme_trough_color) { | |
| 439 *thumb_active_color = *theme_thumb_active; | |
| 440 *thumb_inactive_color = *theme_thumb_inactive; | |
| 441 *track_color = *theme_trough_color; | |
| 442 return; | |
| 443 } | |
| 444 | |
| 445 // Create window containing scrollbar elements | |
| 446 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP); | |
| 447 GtkWidget* fixed = gtk_fixed_new(); | |
| 448 GtkWidget* scrollbar = gtk_hscrollbar_new(NULL); | |
| 449 gtk_container_add(GTK_CONTAINER(window), fixed); | |
| 450 gtk_container_add(GTK_CONTAINER(fixed), scrollbar); | |
| 451 gtk_widget_realize(window); | |
| 452 gtk_widget_realize(scrollbar); | |
| 453 | |
| 454 // Draw scrollbar thumb part and track into offscreen image | |
| 455 const int kWidth = 100; | |
| 456 const int kHeight = 20; | |
| 457 GtkStyle* style = gtk_rc_get_style(scrollbar); | |
| 458 GdkPixmap* pm = gdk_pixmap_new(window->window, kWidth, kHeight, -1); | |
| 459 GdkRectangle rect = { 0, 0, kWidth, kHeight }; | |
| 460 unsigned char data[3 * kWidth * kHeight]; | |
| 461 for (int i = 0; i < 3; ++i) { | |
| 462 if (i < 2) { | |
| 463 // Thumb part | |
| 464 gtk_paint_slider(style, pm, | |
| 465 i == 0 ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL, | |
| 466 GTK_SHADOW_OUT, &rect, scrollbar, "slider", 0, 0, | |
| 467 kWidth, kHeight, GTK_ORIENTATION_HORIZONTAL); | |
| 468 } else { | |
| 469 // Track | |
| 470 gtk_paint_box(style, pm, GTK_STATE_ACTIVE, GTK_SHADOW_IN, &rect, | |
| 471 scrollbar, "trough-upper", 0, 0, kWidth, kHeight); | |
| 472 } | |
| 473 GdkPixbuf* pb = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, | |
| 474 FALSE, 8, kWidth, kHeight, | |
| 475 3 * kWidth, 0, 0); | |
| 476 gdk_pixbuf_get_from_drawable(pb, pm, NULL, 0, 0, 0, 0, kWidth, kHeight); | |
| 477 | |
| 478 // Sample pixels | |
| 479 int components[3] = { 0 }; | |
| 480 for (int y = 2; y < kHeight - 2; ++y) { | |
| 481 for (int c = 0; c < 3; ++c) { | |
| 482 // Sample a vertical slice of pixels at about one-thirds from the | |
| 483 // left edge. This allows us to avoid any fixed graphics that might be | |
| 484 // located at the edges or in the center of the scrollbar. | |
| 485 // Each pixel is made up of a red, green, and blue component; taking up | |
| 486 // a total of three bytes. | |
| 487 components[c] += data[3 * (kWidth / 3 + y * kWidth) + c]; | |
| 488 } | |
| 489 } | |
| 490 GdkColor* color = i == 0 ? thumb_active_color : | |
| 491 i == 1 ? thumb_inactive_color : | |
| 492 track_color; | |
| 493 color->pixel = 0; | |
| 494 // We sampled pixels across the full height of the image, ignoring a two | |
| 495 // pixel border. In some themes, the border has a completely different | |
| 496 // color which we do not want to factor into our average color computation. | |
| 497 // | |
| 498 // We now need to scale the colors from the 0..255 range, to the wider | |
| 499 // 0..65535 range, and we need to actually compute the average color; so, | |
| 500 // we divide by the total number of pixels in the sample. | |
| 501 color->red = components[0] * 65535 / (255 * (kHeight - 4)); | |
| 502 color->green = components[1] * 65535 / (255 * (kHeight - 4)); | |
| 503 color->blue = components[2] * 65535 / (255 * (kHeight - 4)); | |
| 504 | |
| 505 g_object_unref(pb); | |
| 506 } | |
| 507 g_object_unref(pm); | |
| 508 | |
| 509 gtk_widget_destroy(window); | |
| 510 | |
| 511 // Override any of the default colors with ones that were specified by the | |
| 512 // theme. | |
| 513 if (theme_thumb_active) | |
| 514 *thumb_active_color = *theme_thumb_active; | |
| 515 | |
| 516 if (theme_thumb_inactive) | |
| 517 *thumb_inactive_color = *theme_thumb_inactive; | |
| 518 | |
| 519 if (theme_trough_color) | |
| 520 *track_color = *theme_trough_color; | |
| 521 } | |
| 522 | |
| 523 CairoCachedSurface* GtkThemeProvider::GetSurfaceNamed( | |
| 524 int id, | |
| 525 GtkWidget* widget_on_display) { | |
| 526 return GetSurfaceNamedImpl(id, | |
| 527 &per_display_surfaces_, | |
| 528 GetPixbufNamed(id), | |
| 529 widget_on_display); | |
| 530 } | |
| 531 | |
| 532 CairoCachedSurface* GtkThemeProvider::GetRTLEnabledSurfaceNamed( | |
| 533 int id, | |
| 534 GtkWidget* widget_on_display) { | |
| 535 // We flip the sign of |id| when passing it to GetSurfaceNamedImpl() for the | |
| 536 // same reason that BrowserThemeProvider::GetPixbufImpl() does: so that if one | |
| 537 // location calls this function with a resource ID, and another place calls | |
| 538 // GetSurfaceNamed() with the same ID, they'll correctly get different | |
| 539 // surfaces in RTL mode. | |
| 540 return GetSurfaceNamedImpl(-id, | |
| 541 &per_display_surfaces_, | |
| 542 GetRTLEnabledPixbufNamed(id), | |
| 543 widget_on_display); | |
| 544 } | |
| 545 | |
| 546 CairoCachedSurface* GtkThemeProvider::GetUnthemedSurfaceNamed( | |
| 547 int id, | |
| 548 GtkWidget* widget_on_display) { | |
| 549 return GetSurfaceNamedImpl(id, | |
| 550 &per_display_unthemed_surfaces_, | |
| 551 ResourceBundle::GetSharedInstance().GetPixbufNamed(id), | |
| 552 widget_on_display); | |
| 553 } | |
| 554 | |
| 555 // static | |
| 556 GdkPixbuf* GtkThemeProvider::GetFolderIcon(bool native) { | |
| 557 if (native) { | |
| 558 if (!icon_widget_) | |
| 559 icon_widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 560 // We never release our ref, so we will leak this on program shutdown. | |
| 561 if (!default_folder_icon_) { | |
| 562 default_folder_icon_ = | |
| 563 gtk_widget_render_icon(icon_widget_, GTK_STOCK_DIRECTORY, | |
| 564 GTK_ICON_SIZE_MENU, NULL); | |
| 565 } | |
| 566 if (default_folder_icon_) | |
| 567 return default_folder_icon_; | |
| 568 } | |
| 569 | |
| 570 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 571 static GdkPixbuf* default_folder_icon_ = rb.GetPixbufNamed( | |
| 572 IDR_BOOKMARK_BAR_FOLDER); | |
| 573 return default_folder_icon_; | |
| 574 } | |
| 575 | |
| 576 // static | |
| 577 GdkPixbuf* GtkThemeProvider::GetDefaultFavicon(bool native) { | |
| 578 if (native) { | |
| 579 if (!icon_widget_) | |
| 580 icon_widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 581 // We never release our ref, so we will leak this on program shutdown. | |
| 582 if (!default_bookmark_icon_) { | |
| 583 default_bookmark_icon_ = | |
| 584 gtk_widget_render_icon(icon_widget_, GTK_STOCK_FILE, | |
| 585 GTK_ICON_SIZE_MENU, NULL); | |
| 586 } | |
| 587 if (default_bookmark_icon_) | |
| 588 return default_bookmark_icon_; | |
| 589 } | |
| 590 | |
| 591 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 592 static GdkPixbuf* default_bookmark_icon_ = rb.GetPixbufNamed( | |
| 593 IDR_DEFAULT_FAVICON); | |
| 594 return default_bookmark_icon_; | |
| 595 } | |
| 596 | |
| 597 // static | |
| 598 bool GtkThemeProvider::DefaultUsesSystemTheme() { | |
| 599 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 600 | |
| 601 switch (base::nix::GetDesktopEnvironment(env.get())) { | |
| 602 case base::nix::DESKTOP_ENVIRONMENT_GNOME: | |
| 603 case base::nix::DESKTOP_ENVIRONMENT_XFCE: | |
| 604 return true; | |
| 605 default: | |
| 606 return false; | |
| 607 } | |
| 608 } | |
| 609 | |
| 610 void GtkThemeProvider::ClearAllThemeData() { | |
| 611 colors_.clear(); | |
| 612 tints_.clear(); | |
| 613 | |
| 614 BrowserThemeProvider::ClearAllThemeData(); | |
| 615 } | |
| 616 | |
| 617 void GtkThemeProvider::LoadThemePrefs() { | |
| 618 if (use_gtk_) { | |
| 619 LoadGtkValues(); | |
| 620 } else { | |
| 621 LoadDefaultValues(); | |
| 622 BrowserThemeProvider::LoadThemePrefs(); | |
| 623 } | |
| 624 | |
| 625 RebuildMenuIconSets(); | |
| 626 } | |
| 627 | |
| 628 void GtkThemeProvider::NotifyThemeChanged(const Extension* extension) { | |
| 629 BrowserThemeProvider::NotifyThemeChanged(extension); | |
| 630 | |
| 631 // Notify all GtkChromeButtons of their new rendering mode: | |
| 632 for (std::vector<GtkWidget*>::iterator it = chrome_buttons_.begin(); | |
| 633 it != chrome_buttons_.end(); ++it) { | |
| 634 gtk_chrome_button_set_use_gtk_rendering( | |
| 635 GTK_CHROME_BUTTON(*it), use_gtk_); | |
| 636 } | |
| 637 } | |
| 638 | |
| 639 void GtkThemeProvider::FreePlatformCaches() { | |
| 640 BrowserThemeProvider::FreePlatformCaches(); | |
| 641 FreePerDisplaySurfaces(&per_display_surfaces_); | |
| 642 FreePerDisplaySurfaces(&per_display_unthemed_surfaces_); | |
| 643 STLDeleteValues(>k_images_); | |
| 644 } | |
| 645 | |
| 646 void GtkThemeProvider::OnStyleSet(GtkWidget* widget, | |
| 647 GtkStyle* previous_style) { | |
| 648 GdkPixbuf* default_folder_icon = default_folder_icon_; | |
| 649 GdkPixbuf* default_bookmark_icon = default_bookmark_icon_; | |
| 650 default_folder_icon_ = NULL; | |
| 651 default_bookmark_icon_ = NULL; | |
| 652 | |
| 653 if (profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme)) { | |
| 654 ClearAllThemeData(); | |
| 655 LoadGtkValues(); | |
| 656 NotifyThemeChanged(NULL); | |
| 657 } | |
| 658 | |
| 659 RebuildMenuIconSets(); | |
| 660 | |
| 661 // Free the old icons only after the theme change notification has gone | |
| 662 // through. | |
| 663 if (default_folder_icon) | |
| 664 g_object_unref(default_folder_icon); | |
| 665 if (default_bookmark_icon) | |
| 666 g_object_unref(default_bookmark_icon); | |
| 667 } | |
| 668 | |
| 669 void GtkThemeProvider::LoadGtkValues() { | |
| 670 // Before we start setting images and values, we have to clear out old, stale | |
| 671 // values. (If we don't do this, we'll regress startup time in the case where | |
| 672 // someone installs a heavyweight theme, then goes back to GTK.) | |
| 673 DictionaryValue* pref_images = | |
| 674 profile()->GetPrefs()->GetMutableDictionary(prefs::kCurrentThemeImages); | |
| 675 pref_images->Clear(); | |
| 676 | |
| 677 GtkStyle* frame_style = gtk_rc_get_style(fake_frame_); | |
| 678 | |
| 679 GtkStyle* window_style = gtk_rc_get_style(fake_window_); | |
| 680 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_CONTROL_BACKGROUND, | |
| 681 &window_style->bg[GTK_STATE_NORMAL]); | |
| 682 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_BUTTON_BACKGROUND, | |
| 683 &window_style->bg[GTK_STATE_NORMAL]); | |
| 684 | |
| 685 GdkColor toolbar_color = window_style->bg[GTK_STATE_NORMAL]; | |
| 686 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_TOOLBAR, &toolbar_color); | |
| 687 | |
| 688 GdkColor button_color = window_style->bg[GTK_STATE_SELECTED]; | |
| 689 SetThemeTintFromGtk(BrowserThemeProvider::TINT_BUTTONS, &button_color); | |
| 690 | |
| 691 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get()); | |
| 692 GdkColor label_color = label_style->fg[GTK_STATE_NORMAL]; | |
| 693 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_TAB_TEXT, &label_color); | |
| 694 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_BOOKMARK_TEXT, &label_color); | |
| 695 | |
| 696 // Build the various icon tints. | |
| 697 GetNormalButtonTintHSL(&button_tint_); | |
| 698 GetNormalEntryForegroundHSL(&entry_tint_); | |
| 699 GetSelectedEntryForegroundHSL(&selected_entry_tint_); | |
| 700 GdkColor frame_color = BuildFrameColors(frame_style); | |
| 701 | |
| 702 // The inactive frame color never occurs naturally in the theme, as it is a | |
| 703 // tinted version of |frame_color|. We generate another color based on the | |
| 704 // background tab color, with the lightness and saturation moved in the | |
| 705 // opposite direction. (We don't touch the hue, since there should be subtle | |
| 706 // hints of the color in the text.) | |
| 707 color_utils::HSL inactive_tab_text_hsl = tints_[TINT_BACKGROUND_TAB]; | |
| 708 if (inactive_tab_text_hsl.l < 0.5) | |
| 709 inactive_tab_text_hsl.l = kDarkInactiveLuminance; | |
| 710 else | |
| 711 inactive_tab_text_hsl.l = kLightInactiveLuminance; | |
| 712 | |
| 713 if (inactive_tab_text_hsl.s < 0.5) | |
| 714 inactive_tab_text_hsl.s = kHeavyInactiveSaturation; | |
| 715 else | |
| 716 inactive_tab_text_hsl.s = kLightInactiveSaturation; | |
| 717 | |
| 718 colors_[BrowserThemeProvider::COLOR_BACKGROUND_TAB_TEXT] = | |
| 719 color_utils::HSLToSkColor(inactive_tab_text_hsl, 255); | |
| 720 | |
| 721 // We pick the text and background colors for the NTP out of the colors for a | |
| 722 // GtkEntry. We do this because GtkEntries background color is never the same | |
| 723 // as |toolbar_color|, is usually a white, and when it isn't a white, | |
| 724 // provides sufficient contrast to |toolbar_color|. Try this out with | |
| 725 // Darklooks, HighContrastInverse or ThinIce. | |
| 726 GtkStyle* entry_style = gtk_rc_get_style(fake_entry_.get()); | |
| 727 GdkColor ntp_background = entry_style->base[GTK_STATE_NORMAL]; | |
| 728 GdkColor ntp_foreground = entry_style->text[GTK_STATE_NORMAL]; | |
| 729 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_BACKGROUND, | |
| 730 &ntp_background); | |
| 731 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_TEXT, | |
| 732 &ntp_foreground); | |
| 733 | |
| 734 // The NTP header is the color that surrounds the current active thumbnail on | |
| 735 // the NTP, and acts as the border of the "Recent Links" box. It would be | |
| 736 // awesome if they were separated so we could use GetBorderColor() for the | |
| 737 // border around the "Recent Links" section, but matching the frame color is | |
| 738 // more important. | |
| 739 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_HEADER, | |
| 740 &frame_color); | |
| 741 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_SECTION, | |
| 742 &toolbar_color); | |
| 743 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_SECTION_TEXT, | |
| 744 &label_color); | |
| 745 | |
| 746 // Override the link color if the theme provides it. | |
| 747 const GdkColor* link_color = NULL; | |
| 748 gtk_widget_style_get(GTK_WIDGET(fake_window_), | |
| 749 "link-color", &link_color, NULL); | |
| 750 if (!link_color) | |
| 751 link_color = &kDefaultLinkColor; | |
| 752 | |
| 753 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_LINK, | |
| 754 link_color); | |
| 755 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_LINK_UNDERLINE, | |
| 756 link_color); | |
| 757 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_SECTION_LINK, | |
| 758 link_color); | |
| 759 SetThemeColorFromGtk(BrowserThemeProvider::COLOR_NTP_SECTION_LINK_UNDERLINE, | |
| 760 link_color); | |
| 761 | |
| 762 // Generate the colors that we pass to WebKit. | |
| 763 focus_ring_color_ = GdkToSkColor(&frame_color); | |
| 764 GdkColor thumb_active_color, thumb_inactive_color, track_color; | |
| 765 GtkThemeProvider::GetScrollbarColors(&thumb_active_color, | |
| 766 &thumb_inactive_color, | |
| 767 &track_color); | |
| 768 thumb_active_color_ = GdkToSkColor(&thumb_active_color); | |
| 769 thumb_inactive_color_ = GdkToSkColor(&thumb_inactive_color); | |
| 770 track_color_ = GdkToSkColor(&track_color); | |
| 771 | |
| 772 // Some GTK themes only define the text selection colors on the GtkEntry | |
| 773 // class, so we need to use that for getting selection colors. | |
| 774 active_selection_bg_color_ = | |
| 775 GdkToSkColor(&entry_style->base[GTK_STATE_SELECTED]); | |
| 776 active_selection_fg_color_ = | |
| 777 GdkToSkColor(&entry_style->text[GTK_STATE_SELECTED]); | |
| 778 inactive_selection_bg_color_ = | |
| 779 GdkToSkColor(&entry_style->base[GTK_STATE_ACTIVE]); | |
| 780 inactive_selection_fg_color_ = | |
| 781 GdkToSkColor(&entry_style->text[GTK_STATE_ACTIVE]); | |
| 782 } | |
| 783 | |
| 784 GdkColor GtkThemeProvider::BuildFrameColors(GtkStyle* frame_style) { | |
| 785 const GdkColor* theme_frame = NULL; | |
| 786 const GdkColor* theme_inactive_frame = NULL; | |
| 787 const GdkColor* theme_incognito_frame = NULL; | |
| 788 const GdkColor* theme_incognito_inactive_frame = NULL; | |
| 789 gtk_widget_style_get(GTK_WIDGET(fake_frame_), | |
| 790 "frame-color", &theme_frame, | |
| 791 "inactive-frame-color", &theme_inactive_frame, | |
| 792 "incognito-frame-color", &theme_incognito_frame, | |
| 793 "incognito-inactive-frame-color", | |
| 794 &theme_incognito_inactive_frame, | |
| 795 NULL); | |
| 796 | |
| 797 GdkColor frame_color = BuildAndSetFrameColor( | |
| 798 &frame_style->bg[GTK_STATE_SELECTED], | |
| 799 theme_frame, | |
| 800 kDefaultFrameShift, | |
| 801 BrowserThemeProvider::COLOR_FRAME, | |
| 802 BrowserThemeProvider::TINT_FRAME); | |
| 803 SetThemeTintFromGtk(BrowserThemeProvider::TINT_BACKGROUND_TAB, &frame_color); | |
| 804 | |
| 805 BuildAndSetFrameColor( | |
| 806 &frame_style->bg[GTK_STATE_INSENSITIVE], | |
| 807 theme_inactive_frame, | |
| 808 kDefaultFrameShift, | |
| 809 BrowserThemeProvider::COLOR_FRAME_INACTIVE, | |
| 810 BrowserThemeProvider::TINT_FRAME_INACTIVE); | |
| 811 | |
| 812 BuildAndSetFrameColor( | |
| 813 &frame_color, | |
| 814 theme_incognito_frame, | |
| 815 GetDefaultTint(BrowserThemeProvider::TINT_FRAME_INCOGNITO), | |
| 816 BrowserThemeProvider::COLOR_FRAME_INCOGNITO, | |
| 817 BrowserThemeProvider::TINT_FRAME_INCOGNITO); | |
| 818 | |
| 819 BuildAndSetFrameColor( | |
| 820 &frame_color, | |
| 821 theme_incognito_inactive_frame, | |
| 822 GetDefaultTint(BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE), | |
| 823 BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE, | |
| 824 BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE); | |
| 825 | |
| 826 return frame_color; | |
| 827 } | |
| 828 | |
| 829 void GtkThemeProvider::LoadDefaultValues() { | |
| 830 focus_ring_color_ = SkColorSetARGB(255, 229, 151, 0); | |
| 831 thumb_active_color_ = SkColorSetRGB(244, 244, 244); | |
| 832 thumb_inactive_color_ = SkColorSetRGB(234, 234, 234); | |
| 833 track_color_ = SkColorSetRGB(211, 211, 211); | |
| 834 | |
| 835 active_selection_bg_color_ = SkColorSetRGB(30, 144, 255); | |
| 836 active_selection_fg_color_ = SK_ColorWHITE; | |
| 837 inactive_selection_bg_color_ = SkColorSetRGB(200, 200, 200); | |
| 838 inactive_selection_fg_color_ = SkColorSetRGB(50, 50, 50); | |
| 839 } | |
| 840 | |
| 841 void GtkThemeProvider::RebuildMenuIconSets() { | |
| 842 FreeIconSets(); | |
| 843 | |
| 844 GtkStyle* style = gtk_rc_get_style(fake_menu_item_.get()); | |
| 845 | |
| 846 fullscreen_icon_set_ = gtk_icon_set_new(); | |
| 847 BuildIconFromIDRWithColor(IDR_FULLSCREEN_MENU_BUTTON, | |
| 848 style, | |
| 849 GTK_STATE_PRELIGHT, | |
| 850 fullscreen_icon_set_); | |
| 851 BuildIconFromIDRWithColor(IDR_FULLSCREEN_MENU_BUTTON, | |
| 852 style, | |
| 853 GTK_STATE_NORMAL, | |
| 854 fullscreen_icon_set_); | |
| 855 } | |
| 856 | |
| 857 void GtkThemeProvider::SetThemeColorFromGtk(int id, const GdkColor* color) { | |
| 858 colors_[id] = GdkToSkColor(color); | |
| 859 } | |
| 860 | |
| 861 void GtkThemeProvider::SetThemeTintFromGtk(int id, const GdkColor* color) { | |
| 862 color_utils::HSL default_tint = GetDefaultTint(id); | |
| 863 color_utils::HSL hsl; | |
| 864 color_utils::SkColorToHSL(GdkToSkColor(color), &hsl); | |
| 865 | |
| 866 if (default_tint.s != -1) | |
| 867 hsl.s = default_tint.s; | |
| 868 | |
| 869 if (default_tint.l != -1) | |
| 870 hsl.l = default_tint.l; | |
| 871 | |
| 872 tints_[id] = hsl; | |
| 873 } | |
| 874 | |
| 875 GdkColor GtkThemeProvider::BuildAndSetFrameColor(const GdkColor* base, | |
| 876 const GdkColor* gtk_base, | |
| 877 const color_utils::HSL& tint, | |
| 878 int color_id, | |
| 879 int tint_id) { | |
| 880 GdkColor out_color = *base; | |
| 881 if (gtk_base) { | |
| 882 // The theme author specified a color to use, use it without modification. | |
| 883 out_color = *gtk_base; | |
| 884 } else { | |
| 885 // Tint the basic color since this is a heuristic color instead of one | |
| 886 // specified by the theme author. | |
| 887 GdkColorHSLShift(tint, &out_color); | |
| 888 } | |
| 889 SetThemeColorFromGtk(color_id, &out_color); | |
| 890 SetThemeTintFromGtk(tint_id, &out_color); | |
| 891 | |
| 892 return out_color; | |
| 893 } | |
| 894 | |
| 895 void GtkThemeProvider::FreePerDisplaySurfaces( | |
| 896 PerDisplaySurfaceMap* per_display_map) { | |
| 897 for (PerDisplaySurfaceMap::iterator it = per_display_map->begin(); | |
| 898 it != per_display_map->end(); ++it) { | |
| 899 for (CairoCachedSurfaceMap::iterator jt = it->second.begin(); | |
| 900 jt != it->second.end(); ++jt) { | |
| 901 delete jt->second; | |
| 902 } | |
| 903 } | |
| 904 per_display_map->clear(); | |
| 905 } | |
| 906 | |
| 907 void GtkThemeProvider::FreeIconSets() { | |
| 908 if (fullscreen_icon_set_) { | |
| 909 gtk_icon_set_unref(fullscreen_icon_set_); | |
| 910 fullscreen_icon_set_ = NULL; | |
| 911 } | |
| 912 } | |
| 913 | |
| 914 SkBitmap* GtkThemeProvider::GenerateGtkThemeBitmap(int id) const { | |
| 915 switch (id) { | |
| 916 case IDR_THEME_TOOLBAR: { | |
| 917 GtkStyle* style = gtk_rc_get_style(fake_window_); | |
| 918 GdkColor* color = &style->bg[GTK_STATE_NORMAL]; | |
| 919 SkBitmap* bitmap = new SkBitmap; | |
| 920 bitmap->setConfig(SkBitmap::kARGB_8888_Config, | |
| 921 kToolbarImageWidth, kToolbarImageHeight); | |
| 922 bitmap->allocPixels(); | |
| 923 bitmap->eraseRGB(color->red >> 8, color->green >> 8, color->blue >> 8); | |
| 924 return bitmap; | |
| 925 } | |
| 926 case IDR_THEME_TAB_BACKGROUND: | |
| 927 return GenerateTabImage(IDR_THEME_FRAME); | |
| 928 case IDR_THEME_TAB_BACKGROUND_INCOGNITO: | |
| 929 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO); | |
| 930 case IDR_THEME_FRAME: | |
| 931 return GenerateFrameImage(BrowserThemeProvider::COLOR_FRAME, | |
| 932 "frame-gradient-color"); | |
| 933 case IDR_THEME_FRAME_INACTIVE: | |
| 934 return GenerateFrameImage(BrowserThemeProvider::COLOR_FRAME_INACTIVE, | |
| 935 "inactive-frame-gradient-color"); | |
| 936 case IDR_THEME_FRAME_INCOGNITO: | |
| 937 return GenerateFrameImage(BrowserThemeProvider::COLOR_FRAME_INCOGNITO, | |
| 938 "incognito-frame-gradient-color"); | |
| 939 case IDR_THEME_FRAME_INCOGNITO_INACTIVE: { | |
| 940 return GenerateFrameImage( | |
| 941 BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE, | |
| 942 "incognito-inactive-frame-gradient-color"); | |
| 943 } | |
| 944 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and | |
| 945 // instead should tint based on the foreground text entry color in GTK+ | |
| 946 // mode because some themes that try to be dark *and* light have very | |
| 947 // different colors between the omnibox and the normal background area. | |
| 948 case IDR_OMNIBOX_HISTORY: | |
| 949 case IDR_OMNIBOX_HTTP: | |
| 950 case IDR_OMNIBOX_MORE: | |
| 951 case IDR_OMNIBOX_SEARCH: | |
| 952 case IDR_OMNIBOX_STAR: | |
| 953 case IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON: | |
| 954 case IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON: { | |
| 955 return GenerateTintedIcon(id, entry_tint_); | |
| 956 } | |
| 957 // In GTK mode, the dark versions of the omnibox icons only ever appear in | |
| 958 // the autocomplete popup and only against the current theme's GtkEntry | |
| 959 // base[GTK_STATE_SELECTED] color, so tint the icons so they won't collide | |
| 960 // with the selected color. | |
| 961 case IDR_OMNIBOX_HISTORY_DARK: | |
| 962 case IDR_OMNIBOX_HTTP_DARK: | |
| 963 case IDR_OMNIBOX_MORE_DARK: | |
| 964 case IDR_OMNIBOX_SEARCH_DARK: | |
| 965 case IDR_OMNIBOX_STAR_DARK: { | |
| 966 return GenerateTintedIcon(id, selected_entry_tint_); | |
| 967 } | |
| 968 default: { | |
| 969 return GenerateTintedIcon(id, button_tint_); | |
| 970 } | |
| 971 } | |
| 972 } | |
| 973 | |
| 974 SkBitmap* GtkThemeProvider::GenerateFrameImage( | |
| 975 int color_id, | |
| 976 const char* gradient_name) const { | |
| 977 // We use two colors: the main color (passed in) and a lightened version of | |
| 978 // that color (which is supposed to match the light gradient at the top of | |
| 979 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird). | |
| 980 ColorMap::const_iterator it = colors_.find(color_id); | |
| 981 DCHECK(it != colors_.end()); | |
| 982 SkColor base = it->second; | |
| 983 | |
| 984 gfx::CanvasSkia canvas(kToolbarImageWidth, kToolbarImageHeight, true); | |
| 985 | |
| 986 int gradient_size; | |
| 987 const GdkColor* gradient_top_color = NULL; | |
| 988 gtk_widget_style_get(GTK_WIDGET(fake_frame_), | |
| 989 "frame-gradient-size", &gradient_size, | |
| 990 gradient_name, &gradient_top_color, | |
| 991 NULL); | |
| 992 if (gradient_size) { | |
| 993 SkColor lighter = gradient_top_color ? GdkToSkColor(gradient_top_color) | |
| 994 : color_utils::HSLShift(base, kGtkFrameShift); | |
| 995 SkShader* shader = gfx::CreateGradientShader( | |
| 996 0, gradient_size, lighter, base); | |
| 997 SkPaint paint; | |
| 998 paint.setStyle(SkPaint::kFill_Style); | |
| 999 paint.setAntiAlias(true); | |
| 1000 paint.setShader(shader); | |
| 1001 shader->unref(); | |
| 1002 | |
| 1003 canvas.DrawRectInt(0, 0, kToolbarImageWidth, gradient_size, paint); | |
| 1004 } | |
| 1005 | |
| 1006 canvas.FillRectInt(base, 0, gradient_size, | |
| 1007 kToolbarImageWidth, | |
| 1008 kToolbarImageHeight - gradient_size); | |
| 1009 return new SkBitmap(canvas.ExtractBitmap()); | |
| 1010 } | |
| 1011 | |
| 1012 SkBitmap* GtkThemeProvider::GenerateTabImage(int base_id) const { | |
| 1013 SkBitmap* base_image = GetBitmapNamed(base_id); | |
| 1014 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap( | |
| 1015 *base_image, GetTint(BrowserThemeProvider::TINT_BACKGROUND_TAB)); | |
| 1016 return new SkBitmap(SkBitmapOperations::CreateTiledBitmap( | |
| 1017 bg_tint, 0, 0, bg_tint.width(), bg_tint.height())); | |
| 1018 } | |
| 1019 | |
| 1020 SkBitmap* GtkThemeProvider::GenerateTintedIcon(int base_id, | |
| 1021 color_utils::HSL tint) const { | |
| 1022 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 1023 scoped_ptr<SkBitmap> button(new SkBitmap(*rb.GetBitmapNamed(base_id))); | |
| 1024 return new SkBitmap(SkBitmapOperations::CreateHSLShiftedBitmap( | |
| 1025 *button, tint)); | |
| 1026 } | |
| 1027 | |
| 1028 void GtkThemeProvider::GetNormalButtonTintHSL( | |
| 1029 color_utils::HSL* tint) const { | |
| 1030 GtkStyle* window_style = gtk_rc_get_style(fake_window_); | |
| 1031 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED]; | |
| 1032 const GdkColor base_color = window_style->base[GTK_STATE_NORMAL]; | |
| 1033 | |
| 1034 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get()); | |
| 1035 const GdkColor text_color = label_style->fg[GTK_STATE_NORMAL]; | |
| 1036 | |
| 1037 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint); | |
| 1038 } | |
| 1039 | |
| 1040 void GtkThemeProvider::GetNormalEntryForegroundHSL( | |
| 1041 color_utils::HSL* tint) const { | |
| 1042 GtkStyle* window_style = gtk_rc_get_style(fake_window_); | |
| 1043 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED]; | |
| 1044 | |
| 1045 GtkStyle* style = gtk_rc_get_style(fake_entry_.get()); | |
| 1046 const GdkColor text_color = style->text[GTK_STATE_NORMAL]; | |
| 1047 const GdkColor base_color = style->base[GTK_STATE_NORMAL]; | |
| 1048 | |
| 1049 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint); | |
| 1050 } | |
| 1051 | |
| 1052 void GtkThemeProvider::GetSelectedEntryForegroundHSL( | |
| 1053 color_utils::HSL* tint) const { | |
| 1054 // The simplest of all the tints. We just use the selected text in the entry | |
| 1055 // since the icons tinted this way will only be displayed against | |
| 1056 // base[GTK_STATE_SELECTED]. | |
| 1057 GtkStyle* style = gtk_rc_get_style(fake_entry_.get()); | |
| 1058 const GdkColor color = style->text[GTK_STATE_SELECTED]; | |
| 1059 color_utils::SkColorToHSL(GdkToSkColor(&color), tint); | |
| 1060 } | |
| 1061 | |
| 1062 CairoCachedSurface* GtkThemeProvider::GetSurfaceNamedImpl( | |
| 1063 int id, | |
| 1064 PerDisplaySurfaceMap* display_surface_map, | |
| 1065 GdkPixbuf* pixbuf, | |
| 1066 GtkWidget* widget_on_display) { | |
| 1067 GdkDisplay* display = gtk_widget_get_display(widget_on_display); | |
| 1068 CairoCachedSurfaceMap& surface_map = (*display_surface_map)[display]; | |
| 1069 | |
| 1070 // Check to see if we already have the pixbuf in the cache. | |
| 1071 CairoCachedSurfaceMap::const_iterator found = surface_map.find(id); | |
| 1072 if (found != surface_map.end()) | |
| 1073 return found->second; | |
| 1074 | |
| 1075 CairoCachedSurface* surface = new CairoCachedSurface; | |
| 1076 surface->UsePixbuf(pixbuf); | |
| 1077 | |
| 1078 surface_map[id] = surface; | |
| 1079 | |
| 1080 return surface; | |
| 1081 } | |
| 1082 | |
| 1083 void GtkThemeProvider::OnDestroyChromeButton(GtkWidget* button) { | |
| 1084 std::vector<GtkWidget*>::iterator it = | |
| 1085 find(chrome_buttons_.begin(), chrome_buttons_.end(), button); | |
| 1086 if (it != chrome_buttons_.end()) | |
| 1087 chrome_buttons_.erase(it); | |
| 1088 } | |
| 1089 | |
| 1090 gboolean GtkThemeProvider::OnSeparatorExpose(GtkWidget* widget, | |
| 1091 GdkEventExpose* event) { | |
| 1092 if (UseGtkTheme()) | |
| 1093 return FALSE; | |
| 1094 | |
| 1095 cairo_t* cr = gdk_cairo_create(GDK_DRAWABLE(widget->window)); | |
| 1096 gdk_cairo_rectangle(cr, &event->area); | |
| 1097 cairo_clip(cr); | |
| 1098 | |
| 1099 GdkColor bottom_color = GetGdkColor(BrowserThemeProvider::COLOR_TOOLBAR); | |
| 1100 double bottom_color_rgb[] = { | |
| 1101 static_cast<double>(bottom_color.red / 257) / 255.0, | |
| 1102 static_cast<double>(bottom_color.green / 257) / 255.0, | |
| 1103 static_cast<double>(bottom_color.blue / 257) / 255.0, }; | |
| 1104 | |
| 1105 cairo_pattern_t* pattern = | |
| 1106 cairo_pattern_create_linear(widget->allocation.x, widget->allocation.y, | |
| 1107 widget->allocation.x, | |
| 1108 widget->allocation.y + | |
| 1109 widget->allocation.height); | |
| 1110 cairo_pattern_add_color_stop_rgb( | |
| 1111 pattern, 0.0, | |
| 1112 kTopSeparatorColor[0], kTopSeparatorColor[1], kTopSeparatorColor[2]); | |
| 1113 cairo_pattern_add_color_stop_rgb( | |
| 1114 pattern, 0.5, | |
| 1115 kMidSeparatorColor[0], kMidSeparatorColor[1], kMidSeparatorColor[2]); | |
| 1116 cairo_pattern_add_color_stop_rgb( | |
| 1117 pattern, 1.0, | |
| 1118 bottom_color_rgb[0], bottom_color_rgb[1], bottom_color_rgb[2]); | |
| 1119 cairo_set_source(cr, pattern); | |
| 1120 | |
| 1121 double start_x = 0.5 + widget->allocation.x; | |
| 1122 cairo_new_path(cr); | |
| 1123 cairo_set_line_width(cr, 1.0); | |
| 1124 cairo_move_to(cr, start_x, widget->allocation.y); | |
| 1125 cairo_line_to(cr, start_x, | |
| 1126 widget->allocation.y + widget->allocation.height); | |
| 1127 cairo_stroke(cr); | |
| 1128 cairo_destroy(cr); | |
| 1129 cairo_pattern_destroy(pattern); | |
| 1130 | |
| 1131 return TRUE; | |
| 1132 } | |
| OLD | NEW |