1 // Copyright (c) 2012 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/ui/libgtk2ui/gtk2_ui.h" 6 7 #include <set> 8 9 #include "base/command_line.h" 10 #include "base/debug/leak_annotations.h" 11 #include "base/environment.h" 12 #include "base/i18n/rtl.h" 13 #include "base/logging.h" 14 #include "base/nix/mime_util_xdg.h" 15 #include "base/stl_util.h" 16 #include "base/strings/stringprintf.h" 17 #include "chrome/browser/themes/theme_properties.h" 18 #include "chrome/browser/ui/libgtk2ui/app_indicator_icon.h" 19 #include "chrome/browser/ui/libgtk2ui/chrome_gtk_frame.h" 20 #include "chrome/browser/ui/libgtk2ui/gtk2_border.h" 21 #include "chrome/browser/ui/libgtk2ui/gtk2_event_loop.h" 22 #include "chrome/browser/ui/libgtk2ui/gtk2_key_bindings_handler.h" 23 #include "chrome/browser/ui/libgtk2ui/gtk2_signal_registrar.h" 24 #include "chrome/browser/ui/libgtk2ui/gtk2_status_icon.h" 25 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h" 26 #include "chrome/browser/ui/libgtk2ui/native_theme_gtk2.h" 27 #include "chrome/browser/ui/libgtk2ui/print_dialog_gtk2.h" 28 #include "chrome/browser/ui/libgtk2ui/printing_gtk2_util.h" 29 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h" 30 #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h" 31 #include "chrome/browser/ui/libgtk2ui/unity_service.h" 32 #include "chrome/browser/ui/libgtk2ui/x11_input_method_context_impl_gtk2.h" 33 #include "grit/theme_resources.h" 34 #include "grit/ui_resources.h" 35 #include "printing/printing_context_linux.h" 36 #include "third_party/skia/include/core/SkBitmap.h" 37 #include "third_party/skia/include/core/SkCanvas.h" 38 #include "third_party/skia/include/core/SkColor.h" 39 #include "third_party/skia/include/core/SkShader.h" 40 #include "ui/base/resource/resource_bundle.h" 41 #include "ui/gfx/canvas.h" 42 #include "ui/gfx/image/image.h" 43 #include "ui/gfx/rect.h" 44 #include "ui/gfx/size.h" 45 #include "ui/gfx/skbitmap_operations.h" 46 #include "ui/gfx/skia_util.h" 47 #include "ui/views/controls/button/label_button.h" 48 #include "ui/views/controls/button/label_button_border.h" 49 #include "ui/views/linux_ui/window_button_order_observer.h" 50 51 #if defined(USE_GCONF) 52 #include "chrome/browser/ui/libgtk2ui/gconf_listener.h" 53 #endif 54 55 // A minimized port of GtkThemeService into something that can provide colors 56 // and images for aura. 57 // 58 // TODO(erg): There's still a lot that needs ported or done for the first time: 59 // 60 // - Render and inject the omnibox background. 61 // - Make sure to test with a light on dark theme, too. 62 63 namespace { 64 65 struct GObjectDeleter { 66 void operator()(void* ptr) { 67 g_object_unref(ptr); 68 } 69 }; 70 struct GtkIconInfoDeleter { 71 void operator()(GtkIconInfo* ptr) { 72 gtk_icon_info_free(ptr); 73 } 74 }; 75 typedef scoped_ptr<GIcon, GObjectDeleter> ScopedGIcon; 76 typedef scoped_ptr<GtkIconInfo, GtkIconInfoDeleter> ScopedGtkIconInfo; 77 typedef scoped_ptr<GdkPixbuf, GObjectDeleter> ScopedGdkPixbuf; 78 79 // Prefix for app indicator ids 80 const char kAppIndicatorIdPrefix[] = "chrome_app_indicator_"; 81 82 // Number of app indicators used (used as part of app-indicator id). 83 int indicators_count; 84 85 // The unknown content type. 86 const char* kUnknownContentType = "application/octet-stream"; 87 88 // The size of the rendered toolbar image. 89 const int kToolbarImageWidth = 64; 90 const int kToolbarImageHeight = 128; 91 92 // How much to tint the GTK+ color lighter at the top of the window. 93 const color_utils::HSL kGtkFrameShift = { -1, -1, 0.58 }; 94 95 // How much to tint the GTK+ color when an explicit frame color hasn't been 96 // specified. 97 const color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 }; 98 99 // Values used as the new luminance and saturation values in the inactive tab 100 // text color. 101 const double kDarkInactiveLuminance = 0.85; 102 const double kLightInactiveLuminance = 0.15; 103 const double kHeavyInactiveSaturation = 0.7; 104 const double kLightInactiveSaturation = 0.3; 105 106 // Default color for links on the NTP when the GTK+ theme doesn't define a 107 // link color. Constant taken from gtklinkbutton.c. 108 const GdkColor kDefaultLinkColor = { 0, 0, 0, 0xeeee }; 109 110 const int kSkiaToGDKMultiplier = 257; 111 112 // TODO(erg): ThemeService has a whole interface just for reading default 113 // constants. Figure out what to do with that more long term; for now, just 114 // copy the constants themselves here. 115 // 116 // Default tints. 117 const color_utils::HSL kDefaultTintButtons = { -1, -1, -1 }; 118 const color_utils::HSL kDefaultTintFrame = { -1, -1, -1 }; 119 const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f }; 120 const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f }; 121 const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f }; 122 const color_utils::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 }; 123 124 // A list of images that we provide while in gtk mode. 125 // 126 // TODO(erg): We list both the normal and *_DESKTOP versions of some of these 127 // images because in some contexts, we don't go through the 128 // chrome::MapThemeImage interface. That should be fixed, but tracking that 129 // down is Hard. 130 const int kThemeImages[] = { 131 IDR_THEME_TOOLBAR, 132 IDR_THEME_TAB_BACKGROUND, 133 IDR_THEME_TAB_BACKGROUND_DESKTOP, 134 IDR_THEME_TAB_BACKGROUND_INCOGNITO, 135 IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP, 136 IDR_FRAME, 137 IDR_FRAME_INACTIVE, 138 IDR_THEME_FRAME, 139 IDR_THEME_FRAME_INACTIVE, 140 IDR_THEME_FRAME_INCOGNITO, 141 IDR_THEME_FRAME_INCOGNITO_INACTIVE, 142 }; 143 144 // A list of icons used in the autocomplete view that should be tinted to the 145 // current gtk theme selection color so they stand out against the GtkEntry's 146 // base color. 147 // TODO(erg): Decide what to do about other icons that appear in the omnibox, 148 // e.g. content settings icons. 149 const int kAutocompleteImages[] = { 150 IDR_OMNIBOX_EXTENSION_APP, 151 IDR_OMNIBOX_HTTP, 152 IDR_OMNIBOX_HTTP_DARK, 153 IDR_OMNIBOX_SEARCH, 154 IDR_OMNIBOX_SEARCH_DARK, 155 IDR_OMNIBOX_STAR, 156 IDR_OMNIBOX_STAR_DARK, 157 IDR_OMNIBOX_TTS, 158 IDR_OMNIBOX_TTS_DARK, 159 }; 160 161 // This table converts button ids into a pair of gtk-stock id and state. 162 struct IDRGtkMapping { 163 int idr; 164 const char* stock_id; 165 GtkStateType gtk_state; 166 } const kGtkIcons[] = { 167 { IDR_BACK, GTK_STOCK_GO_BACK, GTK_STATE_NORMAL }, 168 { IDR_BACK_D, GTK_STOCK_GO_BACK, GTK_STATE_INSENSITIVE }, 169 { IDR_BACK_H, GTK_STOCK_GO_BACK, GTK_STATE_PRELIGHT }, 170 { IDR_BACK_P, GTK_STOCK_GO_BACK, GTK_STATE_ACTIVE }, 171 172 { IDR_FORWARD, GTK_STOCK_GO_FORWARD, GTK_STATE_NORMAL }, 173 { IDR_FORWARD_D, GTK_STOCK_GO_FORWARD, GTK_STATE_INSENSITIVE }, 174 { IDR_FORWARD_H, GTK_STOCK_GO_FORWARD, GTK_STATE_PRELIGHT }, 175 { IDR_FORWARD_P, GTK_STOCK_GO_FORWARD, GTK_STATE_ACTIVE }, 176 177 { IDR_HOME, GTK_STOCK_HOME, GTK_STATE_NORMAL }, 178 { IDR_HOME_H, GTK_STOCK_HOME, GTK_STATE_PRELIGHT }, 179 { IDR_HOME_P, GTK_STOCK_HOME, GTK_STATE_ACTIVE }, 180 181 { IDR_RELOAD, GTK_STOCK_REFRESH, GTK_STATE_NORMAL }, 182 { IDR_RELOAD_D, GTK_STOCK_REFRESH, GTK_STATE_INSENSITIVE }, 183 { IDR_RELOAD_H, GTK_STOCK_REFRESH, GTK_STATE_PRELIGHT }, 184 { IDR_RELOAD_P, GTK_STOCK_REFRESH, GTK_STATE_ACTIVE }, 185 186 { IDR_STOP, GTK_STOCK_STOP, GTK_STATE_NORMAL }, 187 { IDR_STOP_D, GTK_STOCK_STOP, GTK_STATE_INSENSITIVE }, 188 { IDR_STOP_H, GTK_STOCK_STOP, GTK_STATE_PRELIGHT }, 189 { IDR_STOP_P, GTK_STOCK_STOP, GTK_STATE_ACTIVE }, 190 }; 191 192 // The image resources that will be tinted by the 'button' tint value. 193 const int kOtherToolbarButtonIDs[] = { 194 IDR_TOOLBAR_BEZEL_HOVER, 195 IDR_TOOLBAR_BEZEL_PRESSED, 196 IDR_BROWSER_ACTION_H, 197 IDR_BROWSER_ACTION_P, 198 IDR_BROWSER_ACTIONS_OVERFLOW, 199 IDR_BROWSER_ACTIONS_OVERFLOW_H, 200 IDR_BROWSER_ACTIONS_OVERFLOW_P, 201 IDR_THROBBER, 202 IDR_THROBBER_WAITING, 203 IDR_THROBBER_LIGHT, 204 205 // TODO(erg): The dropdown arrow should be tinted because we're injecting 206 // various background GTK colors, but the code that accesses them needs to be 207 // modified so that they ask their ui::ThemeProvider instead of the 208 // ResourceBundle. (i.e. in a light on dark theme, the dropdown arrow will be 209 // dark on dark) 210 IDR_MENU_DROPARROW 211 }; 212 213 bool IsOverridableImage(int id) { 214 CR_DEFINE_STATIC_LOCAL(std::set<int>, images, ()); 215 if (images.empty()) { 216 images.insert(kThemeImages, kThemeImages + arraysize(kThemeImages)); 217 images.insert(kAutocompleteImages, 218 kAutocompleteImages + arraysize(kAutocompleteImages)); 219 220 for (unsigned int i = 0; i < arraysize(kGtkIcons); ++i) 221 images.insert(kGtkIcons[i].idr); 222 223 images.insert(kOtherToolbarButtonIDs, 224 kOtherToolbarButtonIDs + arraysize(kOtherToolbarButtonIDs)); 225 } 226 227 return images.count(id) > 0; 228 } 229 230 // Picks a button tint from a set of background colors. While 231 // |accent_gdk_color| will usually be the same color through a theme, this 232 // function will get called with the normal GtkLabel |text_color|/GtkWindow 233 // |background_color| pair and the GtkEntry |text_color|/|background_color| 234 // pair. While 3/4 of the time the resulting tint will be the same, themes that 235 // have a dark window background (with light text) and a light text entry (with 236 // dark text) will get better icons with this separated out. 237 void PickButtonTintFromColors(const GdkColor& accent_gdk_color, 238 const GdkColor& text_color, 239 const GdkColor& background_color, 240 color_utils::HSL* tint) { 241 SkColor accent_color = libgtk2ui::GdkColorToSkColor(accent_gdk_color); 242 color_utils::HSL accent_tint; 243 color_utils::SkColorToHSL(accent_color, &accent_tint); 244 245 color_utils::HSL text_tint; 246 color_utils::SkColorToHSL(libgtk2ui::GdkColorToSkColor(text_color), 247 &text_tint); 248 249 color_utils::HSL background_tint; 250 color_utils::SkColorToHSL(libgtk2ui::GdkColorToSkColor(background_color), 251 &background_tint); 252 253 // If the accent color is gray, then our normal HSL tomfoolery will bring out 254 // whatever color is oddly dominant (for example, in rgb space [125, 128, 255 // 125] will tint green instead of gray). Slight differences (+/-10 (4%) to 256 // all color components) should be interpreted as this color being gray and 257 // we should switch into a special grayscale mode. 258 int rb_diff = abs(static_cast<int>(SkColorGetR(accent_color)) - 259 static_cast<int>(SkColorGetB(accent_color))); 260 int rg_diff = abs(static_cast<int>(SkColorGetR(accent_color)) - 261 static_cast<int>(SkColorGetG(accent_color))); 262 int bg_diff = abs(static_cast<int>(SkColorGetB(accent_color)) - 263 static_cast<int>(SkColorGetG(accent_color))); 264 if (rb_diff < 10 && rg_diff < 10 && bg_diff < 10) { 265 // Our accent is white/gray/black. Only the luminance of the accent color 266 // matters. 267 tint->h = -1; 268 269 // Use the saturation of the text. 270 tint->s = text_tint.s; 271 272 // Use the luminance of the accent color UNLESS there isn't enough 273 // luminance contrast between the accent color and the base color. 274 if (fabs(accent_tint.l - background_tint.l) > 0.3) 275 tint->l = accent_tint.l; 276 else 277 tint->l = text_tint.l; 278 } else { 279 // Our accent is a color. 280 tint->h = accent_tint.h; 281 282 // Don't modify the saturation; the amount of color doesn't matter. 283 tint->s = -1; 284 285 // If the text wants us to darken the icon, don't change the luminance (the 286 // icons are already dark enough). Otherwise, lighten the icon by no more 287 // than 0.9 since we don't want a pure-white icon even if the text is pure 288 // white. 289 if (text_tint.l < 0.5) 290 tint->l = -1; 291 else if (text_tint.l <= 0.9) 292 tint->l = text_tint.l; 293 else 294 tint->l = 0.9; 295 } 296 } 297 298 // Applies an HSL shift to a GdkColor (instead of an SkColor) 299 void GdkColorHSLShift(const color_utils::HSL& shift, GdkColor* frame_color) { 300 SkColor shifted = color_utils::HSLShift( 301 libgtk2ui::GdkColorToSkColor(*frame_color), shift); 302 303 frame_color->pixel = 0; 304 frame_color->red = SkColorGetR(shifted) * kSkiaToGDKMultiplier; 305 frame_color->green = SkColorGetG(shifted) * kSkiaToGDKMultiplier; 306 frame_color->blue = SkColorGetB(shifted) * kSkiaToGDKMultiplier; 307 } 308 309 // Copied Default blah sections from ThemeService. 310 color_utils::HSL GetDefaultTint(int id) { 311 switch (id) { 312 case ThemeProperties::TINT_FRAME: 313 return kDefaultTintFrame; 314 case ThemeProperties::TINT_FRAME_INACTIVE: 315 return kDefaultTintFrameInactive; 316 case ThemeProperties::TINT_FRAME_INCOGNITO: 317 return kDefaultTintFrameIncognito; 318 case ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE: 319 return kDefaultTintFrameIncognitoInactive; 320 case ThemeProperties::TINT_BUTTONS: 321 return kDefaultTintButtons; 322 case ThemeProperties::TINT_BACKGROUND_TAB: 323 return kDefaultTintBackgroundTab; 324 default: 325 color_utils::HSL result = {-1, -1, -1}; 326 return result; 327 } 328 } 329 330 } // namespace 331 332 namespace libgtk2ui { 333 334 Gtk2UI::Gtk2UI() : middle_click_action_(MIDDLE_CLICK_ACTION_LOWER) { 335 GtkInitFromCommandLine(*CommandLine::ForCurrentProcess()); 336 } 337 338 void Gtk2UI::Initialize() { 339 signals_.reset(new Gtk2SignalRegistrar); 340 341 // Create our fake widgets. 342 fake_window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL); 343 fake_frame_ = chrome_gtk_frame_new(); 344 fake_label_.Own(gtk_label_new("")); 345 fake_entry_.Own(gtk_entry_new()); 346 347 // Only realized widgets receive style-set notifications, which we need to 348 // broadcast new theme images and colors. Only realized widgets have style 349 // properties, too, which we query for some colors. 350 gtk_widget_realize(fake_frame_); 351 gtk_widget_realize(fake_window_); 352 353 signals_->Connect(fake_frame_, "style-set", 354 G_CALLBACK(&OnStyleSetThunk), this); 355 356 LoadGtkValues(); 357 358 printing::PrintingContextLinux::SetCreatePrintDialogFunction( 359 &PrintDialogGtk2::CreatePrintDialog); 360 printing::PrintingContextLinux::SetPdfPaperSizeFunction( 361 &GetPdfPaperSizeDeviceUnitsGtk); 362 363 #if defined(USE_GCONF) 364 // We must build this after GTK gets initialized. 365 gconf_listener_.reset(new GConfListener(this)); 366 #endif // defined(USE_GCONF) 367 368 indicators_count = 0; 369 370 // Instantiate the singleton instance of Gtk2EventLoop. 371 Gtk2EventLoop::GetInstance(); 372 } 373 374 Gtk2UI::~Gtk2UI() { 375 gtk_widget_destroy(fake_window_); 376 gtk_widget_destroy(fake_frame_); 377 fake_label_.Destroy(); 378 fake_entry_.Destroy(); 379 380 ClearAllThemeData(); 381 } 382 383 gfx::Image Gtk2UI::GetThemeImageNamed(int id) const { 384 // Try to get our cached version: 385 ImageCache::const_iterator it = gtk_images_.find(id); 386 if (it != gtk_images_.end()) 387 return it->second; 388 389 if (IsOverridableImage(id)) { 390 gfx::Image image = gfx::Image( 391 gfx::ImageSkia::CreateFrom1xBitmap(GenerateGtkThemeBitmap(id))); 392 gtk_images_[id] = image; 393 return image; 394 } 395 396 return gfx::Image(); 397 } 398 399 bool Gtk2UI::GetColor(int id, SkColor* color) const { 400 ColorMap::const_iterator it = colors_.find(id); 401 if (it != colors_.end()) { 402 *color = it->second; 403 return true; 404 } 405 406 return false; 407 } 408 409 bool Gtk2UI::HasCustomImage(int id) const { 410 return IsOverridableImage(id); 411 } 412 413 SkColor Gtk2UI::GetFocusRingColor() const { 414 return focus_ring_color_; 415 } 416 417 SkColor Gtk2UI::GetThumbActiveColor() const { 418 return thumb_active_color_; 419 } 420 421 SkColor Gtk2UI::GetThumbInactiveColor() const { 422 return thumb_inactive_color_; 423 } 424 425 SkColor Gtk2UI::GetTrackColor() const { 426 return track_color_; 427 } 428 429 SkColor Gtk2UI::GetActiveSelectionBgColor() const { 430 return active_selection_bg_color_; 431 } 432 433 SkColor Gtk2UI::GetActiveSelectionFgColor() const { 434 return active_selection_fg_color_; 435 } 436 437 SkColor Gtk2UI::GetInactiveSelectionBgColor() const { 438 return inactive_selection_bg_color_; 439 } 440 441 SkColor Gtk2UI::GetInactiveSelectionFgColor() const { 442 return inactive_selection_fg_color_; 443 } 444 445 double Gtk2UI::GetCursorBlinkInterval() const { 446 // From http://library.gnome.org/devel/gtk/unstable/GtkSettings.html, this is 447 // the default value for gtk-cursor-blink-time. 448 static const gint kGtkDefaultCursorBlinkTime = 1200; 449 450 // Dividing GTK's cursor blink cycle time (in milliseconds) by this value 451 // yields an appropriate value for 452 // content::RendererPreferences::caret_blink_interval. This matches the 453 // logic in the WebKit GTK port. 454 static const double kGtkCursorBlinkCycleFactor = 2000.0; 455 456 gint cursor_blink_time = kGtkDefaultCursorBlinkTime; 457 gboolean cursor_blink = TRUE; 458 g_object_get(gtk_settings_get_default(), 459 "gtk-cursor-blink-time", &cursor_blink_time, 460 "gtk-cursor-blink", &cursor_blink, 461 NULL); 462 return cursor_blink ? (cursor_blink_time / kGtkCursorBlinkCycleFactor) : 0.0; 463 } 464 465 ui::NativeTheme* Gtk2UI::GetNativeTheme(aura::Window* window) const { 466 ui::NativeTheme* native_theme_override = NULL; 467 if (!native_theme_overrider_.is_null()) 468 native_theme_override = native_theme_overrider_.Run(window); 469 470 if (native_theme_override) 471 return native_theme_override; 472 473 return NativeThemeGtk2::instance(); 474 } 475 476 void Gtk2UI::SetNativeThemeOverride(const NativeThemeGetter& callback) { 477 native_theme_overrider_ = callback; 478 } 479 480 bool Gtk2UI::GetDefaultUsesSystemTheme() const { 481 scoped_ptr<base::Environment> env(base::Environment::Create()); 482 483 switch (base::nix::GetDesktopEnvironment(env.get())) { 484 case base::nix::DESKTOP_ENVIRONMENT_GNOME: 485 case base::nix::DESKTOP_ENVIRONMENT_UNITY: 486 case base::nix::DESKTOP_ENVIRONMENT_XFCE: 487 return true; 488 case base::nix::DESKTOP_ENVIRONMENT_KDE3: 489 case base::nix::DESKTOP_ENVIRONMENT_KDE4: 490 case base::nix::DESKTOP_ENVIRONMENT_OTHER: 491 return false; 492 } 493 // Unless GetDesktopEnvironment() badly misbehaves, this should never happen. 494 NOTREACHED(); 495 return false; 496 } 497 498 void Gtk2UI::SetDownloadCount(int count) const { 499 if (unity::IsRunning()) 500 unity::SetDownloadCount(count); 501 } 502 503 void Gtk2UI::SetProgressFraction(float percentage) const { 504 if (unity::IsRunning()) 505 unity::SetProgressFraction(percentage); 506 } 507 508 bool Gtk2UI::IsStatusIconSupported() const { 509 return true; 510 } 511 512 scoped_ptr<views::StatusIconLinux> Gtk2UI::CreateLinuxStatusIcon( 513 const gfx::ImageSkia& image, 514 const base::string16& tool_tip) const { 515 if (AppIndicatorIcon::CouldOpen()) { 516 ++indicators_count; 517 return scoped_ptr<views::StatusIconLinux>(new AppIndicatorIcon( 518 base::StringPrintf("%s%d", kAppIndicatorIdPrefix, indicators_count), 519 image, 520 tool_tip)); 521 } else { 522 return scoped_ptr<views::StatusIconLinux>(new Gtk2StatusIcon( 523 image, tool_tip)); 524 } 525 } 526 527 gfx::Image Gtk2UI::GetIconForContentType( 528 const std::string& content_type, 529 int size) const { 530 // This call doesn't take a reference. 531 GtkIconTheme* theme = gtk_icon_theme_get_default(); 532 533 std::string content_types[] = { 534 content_type, kUnknownContentType 535 }; 536 537 for (size_t i = 0; i < arraysize(content_types); ++i) { 538 ScopedGIcon icon(g_content_type_get_icon(content_types[i].c_str())); 539 ScopedGtkIconInfo icon_info( 540 gtk_icon_theme_lookup_by_gicon( 541 theme, icon.get(), size, 542 static_cast<GtkIconLookupFlags>(GTK_ICON_LOOKUP_FORCE_SIZE))); 543 if (!icon_info) 544 continue; 545 ScopedGdkPixbuf pixbuf(gtk_icon_info_load_icon(icon_info.get(), NULL)); 546 if (!pixbuf) 547 continue; 548 549 SkBitmap bitmap = GdkPixbufToImageSkia(pixbuf.get()); 550 DCHECK_EQ(size, bitmap.width()); 551 DCHECK_EQ(size, bitmap.height()); 552 gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); 553 image_skia.MakeThreadSafe(); 554 return gfx::Image(image_skia); 555 } 556 return gfx::Image(); 557 } 558 559 scoped_ptr<views::Border> Gtk2UI::CreateNativeBorder( 560 views::LabelButton* owning_button, 561 scoped_ptr<views::LabelButtonBorder> border) { 562 if (owning_button->GetNativeTheme() != NativeThemeGtk2::instance()) 563 return border.PassAs<views::Border>(); 564 565 return scoped_ptr<views::Border>( 566 new Gtk2Border(this, owning_button, border.Pass())); 567 } 568 569 void Gtk2UI::AddWindowButtonOrderObserver( 570 views::WindowButtonOrderObserver* observer) { 571 if (!leading_buttons_.empty() || !trailing_buttons_.empty()) { 572 observer->OnWindowButtonOrderingChange(leading_buttons_, 573 trailing_buttons_); 574 } 575 576 observer_list_.AddObserver(observer); 577 } 578 579 void Gtk2UI::RemoveWindowButtonOrderObserver( 580 views::WindowButtonOrderObserver* observer) { 581 observer_list_.RemoveObserver(observer); 582 } 583 584 void Gtk2UI::SetWindowButtonOrdering( 585 const std::vector<views::FrameButton>& leading_buttons, 586 const std::vector<views::FrameButton>& trailing_buttons) { 587 leading_buttons_ = leading_buttons; 588 trailing_buttons_ = trailing_buttons; 589 590 FOR_EACH_OBSERVER(views::WindowButtonOrderObserver, observer_list_, 591 OnWindowButtonOrderingChange(leading_buttons_, 592 trailing_buttons_)); 593 } 594 595 void Gtk2UI::SetNonClientMiddleClickAction(NonClientMiddleClickAction action) { 596 middle_click_action_ = action; 597 } 598 599 scoped_ptr<ui::LinuxInputMethodContext> Gtk2UI::CreateInputMethodContext( 600 ui::LinuxInputMethodContextDelegate* delegate) const { 601 return scoped_ptr<ui::LinuxInputMethodContext>( 602 new X11InputMethodContextImplGtk2(delegate)); 603 } 604 605 bool Gtk2UI::UseAntialiasing() const { 606 GtkSettings* gtk_settings = gtk_settings_get_default(); 607 CHECK(gtk_settings); 608 gint gtk_antialias = 0; 609 g_object_get(gtk_settings, 610 "gtk-xft-antialias", >k_antialias, 611 NULL); 612 return gtk_antialias != 0; 613 } 614 615 gfx::FontRenderParams::Hinting Gtk2UI::GetHintingStyle() const { 616 GtkSettings* gtk_settings = gtk_settings_get_default(); 617 CHECK(gtk_settings); 618 gfx::FontRenderParams::Hinting hinting = 619 gfx::FontRenderParams::HINTING_SLIGHT; 620 gint gtk_hinting = 0; 621 gchar* gtk_hint_style = NULL; 622 g_object_get(gtk_settings, 623 "gtk-xft-hinting", >k_hinting, 624 "gtk-xft-hintstyle", >k_hint_style, 625 NULL); 626 627 if (gtk_hint_style) { 628 if (gtk_hinting == 0 || strcmp(gtk_hint_style, "hintnone") == 0) 629 hinting = gfx::FontRenderParams::HINTING_NONE; 630 else if (strcmp(gtk_hint_style, "hintslight") == 0) 631 hinting = gfx::FontRenderParams::HINTING_SLIGHT; 632 else if (strcmp(gtk_hint_style, "hintmedium") == 0) 633 hinting = gfx::FontRenderParams::HINTING_MEDIUM; 634 else if (strcmp(gtk_hint_style, "hintfull") == 0) 635 hinting = gfx::FontRenderParams::HINTING_FULL; 636 637 g_free(gtk_hint_style); 638 } 639 640 return hinting; 641 } 642 643 gfx::FontRenderParams::SubpixelRendering 644 Gtk2UI::GetSubpixelRenderingStyle() const { 645 GtkSettings* gtk_settings = gtk_settings_get_default(); 646 CHECK(gtk_settings); 647 gfx::FontRenderParams::SubpixelRendering subpixel_rendering = 648 gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; 649 gchar* gtk_rgba = NULL; 650 g_object_get(gtk_settings, 651 "gtk-xft-rgba", >k_rgba, 652 NULL); 653 654 if (gtk_rgba) { 655 if (strcmp(gtk_rgba, "none") == 0) 656 subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; 657 else if (strcmp(gtk_rgba, "rgb") == 0) 658 subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_RGB; 659 else if (strcmp(gtk_rgba, "bgr") == 0) 660 subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_BGR; 661 else if (strcmp(gtk_rgba, "vrgb") == 0) 662 subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_VRGB; 663 else if (strcmp(gtk_rgba, "vbgr") == 0) 664 subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_VBGR; 665 666 g_free(gtk_rgba); 667 } 668 669 return subpixel_rendering; 670 } 671 672 std::string Gtk2UI::GetDefaultFontName() const { 673 GtkSettings* gtk_settings = gtk_settings_get_default(); 674 CHECK(gtk_settings); 675 676 std::string out_font_name = "sans 10"; 677 gchar* font_name = NULL; 678 g_object_get(gtk_settings, "gtk-font-name", &font_name, NULL); 679 680 if (font_name) { 681 out_font_name = std::string(font_name); 682 g_free(font_name); 683 } 684 685 return out_font_name; 686 } 687 688 ui::SelectFileDialog* Gtk2UI::CreateSelectFileDialog( 689 ui::SelectFileDialog::Listener* listener, 690 ui::SelectFilePolicy* policy) const { 691 return SelectFileDialogImpl::Create(listener, policy); 692 } 693 694 bool Gtk2UI::UnityIsRunning() { 695 return unity::IsRunning(); 696 } 697 698 views::LinuxUI::NonClientMiddleClickAction 699 Gtk2UI::GetNonClientMiddleClickAction() { 700 return middle_click_action_; 701 } 702 703 void Gtk2UI::NotifyWindowManagerStartupComplete() { 704 // TODO(port) Implement this using _NET_STARTUP_INFO_BEGIN/_NET_STARTUP_INFO 705 // from http://standards.freedesktop.org/startup-notification-spec/ instead. 706 gdk_notify_startup_complete(); 707 } 708 709 bool Gtk2UI::MatchEvent(const ui::Event& event, 710 std::vector<ui::TextEditCommandAuraLinux>* commands) { 711 // Ensure that we have a keyboard handler. 712 if (!key_bindings_handler_) 713 key_bindings_handler_.reset(new Gtk2KeyBindingsHandler); 714 715 return key_bindings_handler_->MatchEvent(event, commands); 716 } 717 718 void Gtk2UI::GetScrollbarColors(GdkColor* thumb_active_color, 719 GdkColor* thumb_inactive_color, 720 GdkColor* track_color) { 721 GdkColor* theme_thumb_active = NULL; 722 GdkColor* theme_thumb_inactive = NULL; 723 GdkColor* theme_trough_color = NULL; 724 gtk_widget_style_get(GTK_WIDGET(fake_frame_), 725 "scrollbar-slider-prelight-color", &theme_thumb_active, 726 "scrollbar-slider-normal-color", &theme_thumb_inactive, 727 "scrollbar-trough-color", &theme_trough_color, 728 NULL); 729 730 // Ask the theme if the theme specifies all the scrollbar colors and short 731 // circuit the expensive painting/compositing if we have all of them. 732 if (theme_thumb_active && theme_thumb_inactive && theme_trough_color) { 733 *thumb_active_color = *theme_thumb_active; 734 *thumb_inactive_color = *theme_thumb_inactive; 735 *track_color = *theme_trough_color; 736 737 gdk_color_free(theme_thumb_active); 738 gdk_color_free(theme_thumb_inactive); 739 gdk_color_free(theme_trough_color); 740 return; 741 } 742 743 // Create window containing scrollbar elements 744 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP); 745 GtkWidget* fixed = gtk_fixed_new(); 746 GtkWidget* scrollbar = gtk_hscrollbar_new(NULL); 747 gtk_container_add(GTK_CONTAINER(window), fixed); 748 gtk_container_add(GTK_CONTAINER(fixed), scrollbar); 749 gtk_widget_realize(window); 750 gtk_widget_realize(scrollbar); 751 752 // Draw scrollbar thumb part and track into offscreen image 753 const int kWidth = 100; 754 const int kHeight = 20; 755 GtkStyle* style = gtk_rc_get_style(scrollbar); 756 GdkWindow* gdk_window = gtk_widget_get_window(window); 757 GdkPixmap* pm = gdk_pixmap_new(gdk_window, kWidth, kHeight, -1); 758 GdkRectangle rect = { 0, 0, kWidth, kHeight }; 759 unsigned char data[3 * kWidth * kHeight]; 760 for (int i = 0; i < 3; ++i) { 761 if (i < 2) { 762 // Thumb part 763 gtk_paint_slider(style, pm, 764 i == 0 ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL, 765 GTK_SHADOW_OUT, &rect, scrollbar, "slider", 0, 0, 766 kWidth, kHeight, GTK_ORIENTATION_HORIZONTAL); 767 } else { 768 // Track 769 gtk_paint_box(style, pm, GTK_STATE_ACTIVE, GTK_SHADOW_IN, &rect, 770 scrollbar, "trough-upper", 0, 0, kWidth, kHeight); 771 } 772 GdkPixbuf* pb = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, 773 FALSE, 8, kWidth, kHeight, 774 3 * kWidth, 0, 0); 775 gdk_pixbuf_get_from_drawable(pb, pm, NULL, 0, 0, 0, 0, kWidth, kHeight); 776 777 // Sample pixels 778 int components[3] = { 0 }; 779 for (int y = 2; y < kHeight - 2; ++y) { 780 for (int c = 0; c < 3; ++c) { 781 // Sample a vertical slice of pixels at about one-thirds from the 782 // left edge. This allows us to avoid any fixed graphics that might be 783 // located at the edges or in the center of the scrollbar. 784 // Each pixel is made up of a red, green, and blue component; taking up 785 // a total of three bytes. 786 components[c] += data[3 * (kWidth / 3 + y * kWidth) + c]; 787 } 788 } 789 GdkColor* color = i == 0 ? thumb_active_color : 790 i == 1 ? thumb_inactive_color : 791 track_color; 792 color->pixel = 0; 793 // We sampled pixels across the full height of the image, ignoring a two 794 // pixel border. In some themes, the border has a completely different 795 // color which we do not want to factor into our average color computation. 796 // 797 // We now need to scale the colors from the 0..255 range, to the wider 798 // 0..65535 range, and we need to actually compute the average color; so, 799 // we divide by the total number of pixels in the sample. 800 color->red = components[0] * 65535 / (255 * (kHeight - 4)); 801 color->green = components[1] * 65535 / (255 * (kHeight - 4)); 802 color->blue = components[2] * 65535 / (255 * (kHeight - 4)); 803 804 g_object_unref(pb); 805 } 806 g_object_unref(pm); 807 808 gtk_widget_destroy(window); 809 810 // Override any of the default colors with ones that were specified by the 811 // theme. 812 if (theme_thumb_active) { 813 *thumb_active_color = *theme_thumb_active; 814 gdk_color_free(theme_thumb_active); 815 } 816 817 if (theme_thumb_inactive) { 818 *thumb_inactive_color = *theme_thumb_inactive; 819 gdk_color_free(theme_thumb_inactive); 820 } 821 822 if (theme_trough_color) { 823 *track_color = *theme_trough_color; 824 gdk_color_free(theme_trough_color); 825 } 826 } 827 828 void Gtk2UI::LoadGtkValues() { 829 // TODO(erg): GtkThemeService had a comment here about having to muck with 830 // the raw Prefs object to remove prefs::kCurrentThemeImages or else we'd 831 // regress startup time. Figure out how to do that when we can't access the 832 // prefs system from here. 833 834 GtkStyle* frame_style = gtk_rc_get_style(fake_frame_); 835 836 GtkStyle* window_style = gtk_rc_get_style(fake_window_); 837 SetThemeColorFromGtk(ThemeProperties::COLOR_CONTROL_BACKGROUND, 838 &window_style->bg[GTK_STATE_NORMAL]); 839 840 GdkColor toolbar_color = window_style->bg[GTK_STATE_NORMAL]; 841 SetThemeColorFromGtk(ThemeProperties::COLOR_TOOLBAR, &toolbar_color); 842 843 GdkColor button_color = window_style->bg[GTK_STATE_SELECTED]; 844 SetThemeTintFromGtk(ThemeProperties::TINT_BUTTONS, &button_color); 845 846 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get()); 847 GdkColor label_color = label_style->fg[GTK_STATE_NORMAL]; 848 SetThemeColorFromGtk(ThemeProperties::COLOR_TAB_TEXT, &label_color); 849 SetThemeColorFromGtk(ThemeProperties::COLOR_BOOKMARK_TEXT, &label_color); 850 SetThemeColorFromGtk(ThemeProperties::COLOR_STATUS_BAR_TEXT, &label_color); 851 852 // Build the various icon tints. 853 GetNormalButtonTintHSL(&button_tint_); 854 GetNormalEntryForegroundHSL(&entry_tint_); 855 GetSelectedEntryForegroundHSL(&selected_entry_tint_); 856 GdkColor frame_color = BuildFrameColors(frame_style); 857 858 // The inactive frame color never occurs naturally in the theme, as it is a 859 // tinted version of |frame_color|. We generate another color based on the 860 // background tab color, with the lightness and saturation moved in the 861 // opposite direction. (We don't touch the hue, since there should be subtle 862 // hints of the color in the text.) 863 color_utils::HSL inactive_tab_text_hsl = 864 tints_[ThemeProperties::TINT_BACKGROUND_TAB]; 865 if (inactive_tab_text_hsl.l < 0.5) 866 inactive_tab_text_hsl.l = kDarkInactiveLuminance; 867 else 868 inactive_tab_text_hsl.l = kLightInactiveLuminance; 869 870 if (inactive_tab_text_hsl.s < 0.5) 871 inactive_tab_text_hsl.s = kHeavyInactiveSaturation; 872 else 873 inactive_tab_text_hsl.s = kLightInactiveSaturation; 874 875 colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] = 876 color_utils::HSLToSkColor(inactive_tab_text_hsl, 255); 877 878 // We pick the text and background colors for the NTP out of the colors for a 879 // GtkEntry. We do this because GtkEntries background color is never the same 880 // as |toolbar_color|, is usually a white, and when it isn't a white, 881 // provides sufficient contrast to |toolbar_color|. Try this out with 882 // Darklooks, HighContrastInverse or ThinIce. 883 GtkStyle* entry_style = gtk_rc_get_style(fake_entry_.get()); 884 GdkColor ntp_background = entry_style->base[GTK_STATE_NORMAL]; 885 GdkColor ntp_foreground = entry_style->text[GTK_STATE_NORMAL]; 886 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_BACKGROUND, 887 &ntp_background); 888 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_TEXT, 889 &ntp_foreground); 890 891 // The NTP header is the color that surrounds the current active thumbnail on 892 // the NTP, and acts as the border of the "Recent Links" box. It would be 893 // awesome if they were separated so we could use GetBorderColor() for the 894 // border around the "Recent Links" section, but matching the frame color is 895 // more important. 896 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_HEADER, 897 &frame_color); 898 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION, 899 &toolbar_color); 900 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_TEXT, 901 &label_color); 902 903 // Override the link color if the theme provides it. 904 const GdkColor* link_color = NULL; 905 gtk_widget_style_get(GTK_WIDGET(fake_window_), 906 "link-color", &link_color, NULL); 907 908 bool is_default_link_color = false; 909 if (!link_color) { 910 link_color = &kDefaultLinkColor; 911 is_default_link_color = true; 912 } 913 914 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK, 915 link_color); 916 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK_UNDERLINE, 917 link_color); 918 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK, 919 link_color); 920 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE, 921 link_color); 922 923 if (!is_default_link_color) 924 gdk_color_free(const_cast<GdkColor*>(link_color)); 925 926 // Generate the colors that we pass to WebKit. 927 focus_ring_color_ = GdkColorToSkColor(frame_color); 928 929 GdkColor thumb_active_color, thumb_inactive_color, track_color; 930 Gtk2UI::GetScrollbarColors(&thumb_active_color, 931 &thumb_inactive_color, 932 &track_color); 933 thumb_active_color_ = GdkColorToSkColor(thumb_active_color); 934 thumb_inactive_color_ = GdkColorToSkColor(thumb_inactive_color); 935 track_color_ = GdkColorToSkColor(track_color); 936 937 // Some GTK themes only define the text selection colors on the GtkEntry 938 // class, so we need to use that for getting selection colors. 939 active_selection_bg_color_ = 940 GdkColorToSkColor(entry_style->base[GTK_STATE_SELECTED]); 941 active_selection_fg_color_ = 942 GdkColorToSkColor(entry_style->text[GTK_STATE_SELECTED]); 943 inactive_selection_bg_color_ = 944 GdkColorToSkColor(entry_style->base[GTK_STATE_ACTIVE]); 945 inactive_selection_fg_color_ = 946 GdkColorToSkColor(entry_style->text[GTK_STATE_ACTIVE]); 947 } 948 949 GdkColor Gtk2UI::BuildFrameColors(GtkStyle* frame_style) { 950 GdkColor* theme_frame = NULL; 951 GdkColor* theme_inactive_frame = NULL; 952 GdkColor* theme_incognito_frame = NULL; 953 GdkColor* theme_incognito_inactive_frame = NULL; 954 gtk_widget_style_get(GTK_WIDGET(fake_frame_), 955 "frame-color", &theme_frame, 956 "inactive-frame-color", &theme_inactive_frame, 957 "incognito-frame-color", &theme_incognito_frame, 958 "incognito-inactive-frame-color", 959 &theme_incognito_inactive_frame, 960 NULL); 961 962 GdkColor frame_color = BuildAndSetFrameColor( 963 &frame_style->bg[GTK_STATE_SELECTED], 964 theme_frame, 965 kDefaultFrameShift, 966 ThemeProperties::COLOR_FRAME, 967 ThemeProperties::TINT_FRAME); 968 if (theme_frame) 969 gdk_color_free(theme_frame); 970 SetThemeTintFromGtk(ThemeProperties::TINT_BACKGROUND_TAB, &frame_color); 971 972 BuildAndSetFrameColor( 973 &frame_style->bg[GTK_STATE_INSENSITIVE], 974 theme_inactive_frame, 975 kDefaultFrameShift, 976 ThemeProperties::COLOR_FRAME_INACTIVE, 977 ThemeProperties::TINT_FRAME_INACTIVE); 978 if (theme_inactive_frame) 979 gdk_color_free(theme_inactive_frame); 980 981 BuildAndSetFrameColor( 982 &frame_color, 983 theme_incognito_frame, 984 GetDefaultTint(ThemeProperties::TINT_FRAME_INCOGNITO), 985 ThemeProperties::COLOR_FRAME_INCOGNITO, 986 ThemeProperties::TINT_FRAME_INCOGNITO); 987 if (theme_incognito_frame) 988 gdk_color_free(theme_incognito_frame); 989 990 BuildAndSetFrameColor( 991 &frame_color, 992 theme_incognito_inactive_frame, 993 GetDefaultTint(ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE), 994 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE, 995 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE); 996 if (theme_incognito_inactive_frame) 997 gdk_color_free(theme_incognito_inactive_frame); 998 999 return frame_color; 1000 } 1001 1002 void Gtk2UI::SetThemeColorFromGtk(int id, const GdkColor* color) { 1003 colors_[id] = GdkColorToSkColor(*color); 1004 } 1005 1006 void Gtk2UI::SetThemeTintFromGtk(int id, const GdkColor* color) { 1007 color_utils::HSL default_tint = GetDefaultTint(id); 1008 color_utils::HSL hsl; 1009 color_utils::SkColorToHSL(GdkColorToSkColor(*color), &hsl); 1010 1011 if (default_tint.s != -1) 1012 hsl.s = default_tint.s; 1013 1014 if (default_tint.l != -1) 1015 hsl.l = default_tint.l; 1016 1017 tints_[id] = hsl; 1018 } 1019 1020 GdkColor Gtk2UI::BuildAndSetFrameColor(const GdkColor* base, 1021 const GdkColor* gtk_base, 1022 const color_utils::HSL& tint, 1023 int color_id, 1024 int tint_id) { 1025 GdkColor out_color = *base; 1026 if (gtk_base) { 1027 // The theme author specified a color to use, use it without modification. 1028 out_color = *gtk_base; 1029 } else { 1030 // Tint the basic color since this is a heuristic color instead of one 1031 // specified by the theme author. 1032 GdkColorHSLShift(tint, &out_color); 1033 } 1034 SetThemeColorFromGtk(color_id, &out_color); 1035 SetThemeTintFromGtk(tint_id, &out_color); 1036 1037 return out_color; 1038 } 1039 1040 SkBitmap Gtk2UI::GenerateGtkThemeBitmap(int id) const { 1041 switch (id) { 1042 case IDR_THEME_TOOLBAR: { 1043 GtkStyle* style = gtk_rc_get_style(fake_window_); 1044 GdkColor* color = &style->bg[GTK_STATE_NORMAL]; 1045 SkBitmap bitmap; 1046 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1047 kToolbarImageWidth, kToolbarImageHeight); 1048 bitmap.allocPixels(); 1049 bitmap.eraseARGB(0xff, color->red >> 8, color->green >> 8, 1050 color->blue >> 8); 1051 return bitmap; 1052 } 1053 case IDR_THEME_TAB_BACKGROUND: 1054 case IDR_THEME_TAB_BACKGROUND_DESKTOP: 1055 return GenerateTabImage(IDR_THEME_FRAME); 1056 case IDR_THEME_TAB_BACKGROUND_INCOGNITO: 1057 case IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP: 1058 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO); 1059 case IDR_FRAME: 1060 case IDR_THEME_FRAME: 1061 return GenerateFrameImage(ThemeProperties::COLOR_FRAME, 1062 "frame-gradient-color"); 1063 case IDR_FRAME_INACTIVE: 1064 case IDR_THEME_FRAME_INACTIVE: 1065 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INACTIVE, 1066 "inactive-frame-gradient-color"); 1067 case IDR_THEME_FRAME_INCOGNITO: 1068 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INCOGNITO, 1069 "incognito-frame-gradient-color"); 1070 case IDR_THEME_FRAME_INCOGNITO_INACTIVE: { 1071 return GenerateFrameImage( 1072 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE, 1073 "incognito-inactive-frame-gradient-color"); 1074 } 1075 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and 1076 // instead should tint based on the foreground text entry color in GTK+ 1077 // mode because some themes that try to be dark *and* light have very 1078 // different colors between the omnibox and the normal background area. 1079 // TODO(erg): Decide what to do about other icons that appear in the 1080 // omnibox, e.g. content settings icons. 1081 case IDR_OMNIBOX_EXTENSION_APP: 1082 case IDR_OMNIBOX_HTTP: 1083 case IDR_OMNIBOX_SEARCH: 1084 case IDR_OMNIBOX_STAR: 1085 case IDR_OMNIBOX_TTS: { 1086 return GenerateTintedIcon(id, entry_tint_); 1087 } 1088 // In GTK mode, the dark versions of the omnibox icons only ever appear in 1089 // the autocomplete popup and only against the current theme's GtkEntry 1090 // base[GTK_STATE_SELECTED] color, so tint the icons so they won't collide 1091 // with the selected color. 1092 case IDR_OMNIBOX_EXTENSION_APP_DARK: 1093 case IDR_OMNIBOX_HTTP_DARK: 1094 case IDR_OMNIBOX_SEARCH_DARK: 1095 case IDR_OMNIBOX_STAR_DARK: 1096 case IDR_OMNIBOX_TTS_DARK: { 1097 return GenerateTintedIcon(id, selected_entry_tint_); 1098 } 1099 // In GTK mode, we need to manually render several icons. 1100 case IDR_BACK: 1101 case IDR_BACK_D: 1102 case IDR_BACK_H: 1103 case IDR_BACK_P: 1104 case IDR_FORWARD: 1105 case IDR_FORWARD_D: 1106 case IDR_FORWARD_H: 1107 case IDR_FORWARD_P: 1108 case IDR_HOME: 1109 case IDR_HOME_H: 1110 case IDR_HOME_P: 1111 case IDR_RELOAD: 1112 case IDR_RELOAD_D: 1113 case IDR_RELOAD_H: 1114 case IDR_RELOAD_P: 1115 case IDR_STOP: 1116 case IDR_STOP_D: 1117 case IDR_STOP_H: 1118 case IDR_STOP_P: { 1119 return GenerateGTKIcon(id); 1120 } 1121 case IDR_TOOLBAR_BEZEL_HOVER: 1122 return GenerateToolbarBezel(GTK_STATE_PRELIGHT, IDR_TOOLBAR_BEZEL_HOVER); 1123 case IDR_TOOLBAR_BEZEL_PRESSED: 1124 return GenerateToolbarBezel(GTK_STATE_ACTIVE, IDR_TOOLBAR_BEZEL_PRESSED); 1125 case IDR_BROWSER_ACTION_H: 1126 return GenerateToolbarBezel(GTK_STATE_PRELIGHT, IDR_BROWSER_ACTION_H); 1127 case IDR_BROWSER_ACTION_P: 1128 return GenerateToolbarBezel(GTK_STATE_ACTIVE, IDR_BROWSER_ACTION_P); 1129 default: { 1130 return GenerateTintedIcon(id, button_tint_); 1131 } 1132 } 1133 1134 return SkBitmap(); 1135 } 1136 1137 SkBitmap Gtk2UI::GenerateFrameImage( 1138 int color_id, 1139 const char* gradient_name) const { 1140 // We use two colors: the main color (passed in) and a lightened version of 1141 // that color (which is supposed to match the light gradient at the top of 1142 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird). 1143 ColorMap::const_iterator it = colors_.find(color_id); 1144 DCHECK(it != colors_.end()); 1145 SkColor base = it->second; 1146 1147 gfx::Canvas canvas(gfx::Size(kToolbarImageWidth, kToolbarImageHeight), 1148 1.0f, true); 1149 1150 int gradient_size; 1151 GdkColor* gradient_top_color = NULL; 1152 gtk_widget_style_get(GTK_WIDGET(fake_frame_), 1153 "frame-gradient-size", &gradient_size, 1154 gradient_name, &gradient_top_color, 1155 NULL); 1156 if (gradient_size) { 1157 SkColor lighter = gradient_top_color ? 1158 GdkColorToSkColor(*gradient_top_color) : 1159 color_utils::HSLShift(base, kGtkFrameShift); 1160 if (gradient_top_color) 1161 gdk_color_free(gradient_top_color); 1162 skia::RefPtr<SkShader> shader = gfx::CreateGradientShader( 1163 0, gradient_size, lighter, base); 1164 SkPaint paint; 1165 paint.setStyle(SkPaint::kFill_Style); 1166 paint.setAntiAlias(true); 1167 paint.setShader(shader.get()); 1168 1169 canvas.DrawRect(gfx::Rect(0, 0, kToolbarImageWidth, gradient_size), paint); 1170 } 1171 1172 canvas.FillRect(gfx::Rect(0, gradient_size, kToolbarImageWidth, 1173 kToolbarImageHeight - gradient_size), base); 1174 return canvas.ExtractImageRep().sk_bitmap(); 1175 } 1176 1177 SkBitmap Gtk2UI::GenerateTabImage(int base_id) const { 1178 const SkBitmap* base_image = GetThemeImageNamed(base_id).ToSkBitmap(); 1179 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap( 1180 *base_image, GetDefaultTint(ThemeProperties::TINT_BACKGROUND_TAB)); 1181 return SkBitmapOperations::CreateTiledBitmap( 1182 bg_tint, 0, 0, bg_tint.width(), bg_tint.height()); 1183 } 1184 1185 SkBitmap Gtk2UI::GenerateTintedIcon( 1186 int base_id, 1187 const color_utils::HSL& tint) const { 1188 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1189 return SkBitmapOperations::CreateHSLShiftedBitmap( 1190 rb.GetImageNamed(base_id).AsBitmap(), tint); 1191 } 1192 1193 SkBitmap Gtk2UI::GenerateGTKIcon(int base_id) const { 1194 const char* stock_id = NULL; 1195 GtkStateType gtk_state = GTK_STATE_NORMAL; 1196 for (unsigned int i = 0; i < arraysize(kGtkIcons); ++i) { 1197 if (kGtkIcons[i].idr == base_id) { 1198 stock_id = kGtkIcons[i].stock_id; 1199 gtk_state = kGtkIcons[i].gtk_state; 1200 break; 1201 } 1202 } 1203 DCHECK(stock_id); 1204 1205 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1206 SkBitmap default_bitmap = rb.GetImageNamed(base_id).AsBitmap(); 1207 1208 gtk_widget_ensure_style(fake_frame_); 1209 GtkStyle* style = gtk_widget_get_style(fake_frame_); 1210 GtkIconSet* icon_set = gtk_style_lookup_icon_set(style, stock_id); 1211 if (!icon_set) 1212 return default_bitmap; 1213 1214 // Ask GTK to render the icon to a buffer, which we will steal from. 1215 GdkPixbuf* gdk_icon = gtk_icon_set_render_icon( 1216 icon_set, 1217 style, 1218 base::i18n::IsRTL() ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR, 1219 gtk_state, 1220 GTK_ICON_SIZE_SMALL_TOOLBAR, 1221 fake_frame_, 1222 NULL); 1223 1224 if (!gdk_icon) { 1225 // This can theoretically happen if an icon theme doesn't provide a 1226 // specific image. This should realistically never happen, but I bet there 1227 // are some theme authors who don't reliably provide all icons. 1228 return default_bitmap; 1229 } 1230 1231 SkBitmap retval; 1232 retval.setConfig(SkBitmap::kARGB_8888_Config, 1233 default_bitmap.width(), 1234 default_bitmap.height()); 1235 retval.allocPixels(); 1236 retval.eraseColor(0); 1237 1238 const SkBitmap icon = GdkPixbufToImageSkia(gdk_icon); 1239 g_object_unref(gdk_icon); 1240 1241 SkCanvas canvas(retval); 1242 1243 if (gtk_state == GTK_STATE_ACTIVE || gtk_state == GTK_STATE_PRELIGHT) { 1244 SkBitmap border = DrawGtkButtonBorder(gtk_state, 1245 false, 1246 default_bitmap.width(), 1247 default_bitmap.height()); 1248 canvas.drawBitmap(border, 0, 0); 1249 } 1250 1251 canvas.drawBitmap(icon, 1252 (default_bitmap.width() / 2) - (icon.width() / 2), 1253 (default_bitmap.height() / 2) - (icon.height() / 2)); 1254 1255 return retval; 1256 } 1257 1258 SkBitmap Gtk2UI::GenerateToolbarBezel(int gtk_state, int sizing_idr) const { 1259 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1260 SkBitmap default_bitmap = 1261 rb.GetImageNamed(sizing_idr).AsBitmap(); 1262 1263 SkBitmap retval; 1264 retval.setConfig(SkBitmap::kARGB_8888_Config, 1265 default_bitmap.width(), 1266 default_bitmap.height()); 1267 retval.allocPixels(); 1268 retval.eraseColor(0); 1269 1270 SkCanvas canvas(retval); 1271 SkBitmap border = DrawGtkButtonBorder( 1272 gtk_state, 1273 false, 1274 default_bitmap.width(), 1275 default_bitmap.height()); 1276 canvas.drawBitmap(border, 0, 0); 1277 1278 return retval; 1279 } 1280 1281 void Gtk2UI::GetNormalButtonTintHSL(color_utils::HSL* tint) const { 1282 GtkStyle* window_style = gtk_rc_get_style(fake_window_); 1283 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED]; 1284 const GdkColor base_color = window_style->base[GTK_STATE_NORMAL]; 1285 1286 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get()); 1287 const GdkColor text_color = label_style->fg[GTK_STATE_NORMAL]; 1288 1289 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint); 1290 } 1291 1292 void Gtk2UI::GetNormalEntryForegroundHSL(color_utils::HSL* tint) const { 1293 GtkStyle* window_style = gtk_rc_get_style(fake_window_); 1294 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED]; 1295 1296 GtkStyle* style = gtk_rc_get_style(fake_entry_.get()); 1297 const GdkColor text_color = style->text[GTK_STATE_NORMAL]; 1298 const GdkColor base_color = style->base[GTK_STATE_NORMAL]; 1299 1300 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint); 1301 } 1302 1303 void Gtk2UI::GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const { 1304 // The simplest of all the tints. We just use the selected text in the entry 1305 // since the icons tinted this way will only be displayed against 1306 // base[GTK_STATE_SELECTED]. 1307 GtkStyle* style = gtk_rc_get_style(fake_entry_.get()); 1308 const GdkColor color = style->text[GTK_STATE_SELECTED]; 1309 color_utils::SkColorToHSL(GdkColorToSkColor(color), tint); 1310 } 1311 1312 SkBitmap Gtk2UI::DrawGtkButtonBorder(int gtk_state, 1313 bool focused, 1314 int width, 1315 int height) const { 1316 // Create a temporary GTK button to snapshot 1317 GtkWidget* window = gtk_offscreen_window_new(); 1318 GtkWidget* button = gtk_button_new(); 1319 gtk_widget_set_size_request(button, width, height); 1320 gtk_container_add(GTK_CONTAINER(window), button); 1321 gtk_widget_realize(window); 1322 gtk_widget_realize(button); 1323 gtk_widget_show(button); 1324 gtk_widget_show(window); 1325 1326 if (focused) { 1327 // We can't just use gtk_widget_grab_focus() here because that sets 1328 // gtk_widget_is_focus(), but not gtk_widget_has_focus(), which is what the 1329 // GtkButton's paint checks. 1330 GTK_WIDGET_SET_FLAGS(button, GTK_HAS_FOCUS); 1331 } 1332 1333 gtk_widget_set_state(button, static_cast<GtkStateType>(gtk_state)); 1334 1335 GdkPixmap* pixmap; 1336 { 1337 // http://crbug.com/346740 1338 ANNOTATE_SCOPED_MEMORY_LEAK; 1339 pixmap = gtk_widget_get_snapshot(button, NULL); 1340 } 1341 int w, h; 1342 gdk_drawable_get_size(GDK_DRAWABLE(pixmap), &w, &h); 1343 DCHECK_EQ(w, width); 1344 DCHECK_EQ(h, height); 1345 1346 // We render the Pixmap to a Pixbuf. This can be slow, as we're scrapping 1347 // bits from X. 1348 GdkColormap* colormap = gdk_drawable_get_colormap(pixmap); 1349 GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable(NULL, 1350 GDK_DRAWABLE(pixmap), 1351 colormap, 1352 0, 0, 0, 0, w, h); 1353 1354 // Finally, we convert our pixbuf into a type we can use. 1355 SkBitmap border = GdkPixbufToImageSkia(pixbuf); 1356 g_object_unref(pixbuf); 1357 g_object_unref(pixmap); 1358 gtk_widget_destroy(window); 1359 1360 return border; 1361 } 1362 1363 void Gtk2UI::ClearAllThemeData() { 1364 gtk_images_.clear(); 1365 } 1366 1367 void Gtk2UI::OnStyleSet(GtkWidget* widget, GtkStyle* previous_style) { 1368 ClearAllThemeData(); 1369 LoadGtkValues(); 1370 NativeThemeGtk2::instance()->NotifyObservers(); 1371 } 1372 1373 } // namespace libgtk2ui 1374 1375 views::LinuxUI* BuildGtk2UI() { 1376 return new libgtk2ui::Gtk2UI; 1377 } 1378