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