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