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