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"
10 #include <pango/pango.h>
13 #include "base/command_line.h"
14 #include "base/debug/leak_annotations.h"
15 #include "base/environment.h"
16 #include "base/i18n/rtl.h"
17 #include "base/logging.h"
18 #include "base/nix/mime_util_xdg.h"
19 #include "base/nix/xdg_util.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_split.h"
22 #include "base/strings/stringprintf.h"
23 #include "chrome/browser/themes/theme_properties.h"
24 #include "chrome/browser/ui/libgtk2ui/app_indicator_icon.h"
25 #include "chrome/browser/ui/libgtk2ui/chrome_gtk_frame.h"
26 #include "chrome/browser/ui/libgtk2ui/gtk2_border.h"
27 #include "chrome/browser/ui/libgtk2ui/gtk2_event_loop.h"
28 #include "chrome/browser/ui/libgtk2ui/gtk2_key_bindings_handler.h"
29 #include "chrome/browser/ui/libgtk2ui/gtk2_signal_registrar.h"
30 #include "chrome/browser/ui/libgtk2ui/gtk2_status_icon.h"
31 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
32 #include "chrome/browser/ui/libgtk2ui/native_theme_gtk2.h"
33 #include "chrome/browser/ui/libgtk2ui/print_dialog_gtk2.h"
34 #include "chrome/browser/ui/libgtk2ui/printing_gtk2_util.h"
35 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h"
36 #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
37 #include "chrome/browser/ui/libgtk2ui/unity_service.h"
38 #include "chrome/browser/ui/libgtk2ui/x11_input_method_context_impl_gtk2.h"
39 #include "grit/components_scaled_resources.h"
40 #include "grit/theme_resources.h"
41 #include "printing/printing_context_linux.h"
42 #include "third_party/skia/include/core/SkBitmap.h"
43 #include "third_party/skia/include/core/SkCanvas.h"
44 #include "third_party/skia/include/core/SkColor.h"
45 #include "third_party/skia/include/core/SkShader.h"
46 #include "ui/base/resource/resource_bundle.h"
47 #include "ui/gfx/canvas.h"
48 #include "ui/gfx/geometry/rect.h"
49 #include "ui/gfx/geometry/size.h"
50 #include "ui/gfx/image/image.h"
51 #include "ui/gfx/skbitmap_operations.h"
52 #include "ui/gfx/skia_util.h"
53 #include "ui/gfx/x/x11_types.h"
54 #include "ui/resources/grit/ui_resources.h"
55 #include "ui/views/controls/button/label_button.h"
56 #include "ui/views/controls/button/label_button_border.h"
57 #include "ui/views/linux_ui/window_button_order_observer.h"
59 #if defined(USE_GCONF)
60 #include "chrome/browser/ui/libgtk2ui/gconf_listener.h"
63 // A minimized port of GtkThemeService into something that can provide colors
64 // and images for aura.
66 // TODO(erg): There's still a lot that needs ported or done for the first time:
68 // - Render and inject the omnibox background.
69 // - Make sure to test with a light on dark theme, too.
71 // Work around a header bug:
72 // linux/debian_wheezy_i386-sysroot/usr/include/linux/stddef.h redefines NULL
73 // to 0, which breaks -Wsentinel. Get back the normal definition of NULL.
74 // TODO(thakis): Remove this once we update sysroots.
82 struct GObjectDeleter
{
83 void operator()(void* ptr
) {
87 struct GtkIconInfoDeleter
{
88 void operator()(GtkIconInfo
* ptr
) {
89 gtk_icon_info_free(ptr
);
92 typedef scoped_ptr
<GIcon
, GObjectDeleter
> ScopedGIcon
;
93 typedef scoped_ptr
<GtkIconInfo
, GtkIconInfoDeleter
> ScopedGtkIconInfo
;
94 typedef scoped_ptr
<GdkPixbuf
, GObjectDeleter
> ScopedGdkPixbuf
;
96 // Prefix for app indicator ids
97 const char kAppIndicatorIdPrefix
[] = "chrome_app_indicator_";
99 // Number of app indicators used (used as part of app-indicator id).
100 int indicators_count
;
102 // The unknown content type.
103 const char* kUnknownContentType
= "application/octet-stream";
105 // The size of the rendered toolbar image.
106 const int kToolbarImageWidth
= 64;
107 const int kToolbarImageHeight
= 128;
109 // How much to tint the GTK+ color lighter at the top of the window.
110 const color_utils::HSL kGtkFrameShift
= { -1, -1, 0.58 };
112 // How much to tint the GTK+ color when an explicit frame color hasn't been
114 const color_utils::HSL kDefaultFrameShift
= { -1, -1, 0.4 };
116 // Values used as the new luminance and saturation values in the inactive tab
118 const double kDarkInactiveLuminance
= 0.85;
119 const double kLightInactiveLuminance
= 0.15;
120 const double kHeavyInactiveSaturation
= 0.7;
121 const double kLightInactiveSaturation
= 0.3;
123 // Default color for links on the NTP when the GTK+ theme doesn't define a
124 // link color. Constant taken from gtklinkbutton.c.
125 const GdkColor kDefaultLinkColor
= { 0, 0, 0, 0xeeee };
127 const int kSkiaToGDKMultiplier
= 257;
129 // TODO(erg): ThemeService has a whole interface just for reading default
130 // constants. Figure out what to do with that more long term; for now, just
131 // copy the constants themselves here.
134 const color_utils::HSL kDefaultTintButtons
= { -1, -1, -1 };
135 const color_utils::HSL kDefaultTintFrame
= { -1, -1, -1 };
136 const color_utils::HSL kDefaultTintFrameInactive
= { -1, -1, 0.75f
};
137 const color_utils::HSL kDefaultTintFrameIncognito
= { -1, 0.2f
, 0.35f
};
138 const color_utils::HSL kDefaultTintFrameIncognitoInactive
= { -1, 0.3f
, 0.6f
};
139 const color_utils::HSL kDefaultTintBackgroundTab
= { -1, 0.5, 0.75 };
141 // A list of images that we provide while in gtk mode.
143 // TODO(erg): We list both the normal and *_DESKTOP versions of some of these
144 // images because in some contexts, we don't go through the
145 // chrome::MapThemeImage interface. That should be fixed, but tracking that
147 const int kThemeImages
[] = {
149 IDR_THEME_TAB_BACKGROUND
,
150 IDR_THEME_TAB_BACKGROUND_DESKTOP
,
151 IDR_THEME_TAB_BACKGROUND_INCOGNITO
,
152 IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
,
156 IDR_THEME_FRAME_INACTIVE
,
157 IDR_THEME_FRAME_INCOGNITO
,
158 IDR_THEME_FRAME_INCOGNITO_INACTIVE
,
161 // A list of icons used in the autocomplete view that should be tinted to the
162 // current gtk theme selection color so they stand out against the GtkEntry's
164 // TODO(erg): Decide what to do about other icons that appear in the omnibox,
165 // e.g. content settings icons.
166 const int kAutocompleteImages
[] = {
167 IDR_OMNIBOX_CALCULATOR
,
168 IDR_OMNIBOX_EXTENSION_APP
,
170 IDR_OMNIBOX_HTTP_DARK
,
172 IDR_OMNIBOX_SEARCH_DARK
,
174 IDR_OMNIBOX_STAR_DARK
,
176 IDR_OMNIBOX_TTS_DARK
,
179 // This table converts button ids into a pair of gtk-stock id and state.
180 struct IDRGtkMapping
{
182 const char* stock_id
;
183 GtkStateType gtk_state
;
184 } const kGtkIcons
[] = {
185 { IDR_BACK
, GTK_STOCK_GO_BACK
, GTK_STATE_NORMAL
},
186 { IDR_BACK_D
, GTK_STOCK_GO_BACK
, GTK_STATE_INSENSITIVE
},
188 { IDR_FORWARD
, GTK_STOCK_GO_FORWARD
, GTK_STATE_NORMAL
},
189 { IDR_FORWARD_D
, GTK_STOCK_GO_FORWARD
, GTK_STATE_INSENSITIVE
},
191 { IDR_HOME
, GTK_STOCK_HOME
, GTK_STATE_NORMAL
},
193 { IDR_RELOAD
, GTK_STOCK_REFRESH
, GTK_STATE_NORMAL
},
194 { IDR_RELOAD_D
, GTK_STOCK_REFRESH
, GTK_STATE_INSENSITIVE
},
196 { IDR_STOP
, GTK_STOCK_STOP
, GTK_STATE_NORMAL
},
197 { IDR_STOP_D
, GTK_STOCK_STOP
, GTK_STATE_INSENSITIVE
},
200 // The image resources that will be tinted by the 'button' tint value.
201 const int kOtherToolbarButtonIDs
[] = {
202 IDR_TOOLBAR_BEZEL_HOVER
,
203 IDR_TOOLBAR_BEZEL_PRESSED
,
204 IDR_BROWSER_ACTIONS_OVERFLOW
,
206 // TODO(erg): The dropdown arrow should be tinted because we're injecting
207 // various background GTK colors, but the code that accesses them needs to be
208 // modified so that they ask their ui::ThemeProvider instead of the
209 // ResourceBundle. (i.e. in a light on dark theme, the dropdown arrow will be
214 bool IsOverridableImage(int id
) {
215 CR_DEFINE_STATIC_LOCAL(std::set
<int>, images
, ());
216 if (images
.empty()) {
217 images
.insert(kThemeImages
, kThemeImages
+ arraysize(kThemeImages
));
218 images
.insert(kAutocompleteImages
,
219 kAutocompleteImages
+ arraysize(kAutocompleteImages
));
221 for (unsigned int i
= 0; i
< arraysize(kGtkIcons
); ++i
)
222 images
.insert(kGtkIcons
[i
].idr
);
224 images
.insert(kOtherToolbarButtonIDs
,
225 kOtherToolbarButtonIDs
+ arraysize(kOtherToolbarButtonIDs
));
228 return images
.count(id
) > 0;
231 // Picks a button tint from a set of background colors. While
232 // |accent_gdk_color| will usually be the same color through a theme, this
233 // function will get called with the normal GtkLabel |text_color|/GtkWindow
234 // |background_color| pair and the GtkEntry |text_color|/|background_color|
235 // pair. While 3/4 of the time the resulting tint will be the same, themes that
236 // have a dark window background (with light text) and a light text entry (with
237 // dark text) will get better icons with this separated out.
238 void PickButtonTintFromColors(const GdkColor
& accent_gdk_color
,
239 const GdkColor
& text_color
,
240 const GdkColor
& background_color
,
241 color_utils::HSL
* tint
) {
242 SkColor accent_color
= GdkColorToSkColor(accent_gdk_color
);
243 color_utils::HSL accent_tint
;
244 color_utils::SkColorToHSL(accent_color
, &accent_tint
);
246 color_utils::HSL text_tint
;
247 color_utils::SkColorToHSL(GdkColorToSkColor(text_color
),
250 color_utils::HSL background_tint
;
251 color_utils::SkColorToHSL(GdkColorToSkColor(background_color
),
254 // If the accent color is gray, then our normal HSL tomfoolery will bring out
255 // whatever color is oddly dominant (for example, in rgb space [125, 128,
256 // 125] will tint green instead of gray). Slight differences (+/-10 (4%) to
257 // all color components) should be interpreted as this color being gray and
258 // we should switch into a special grayscale mode.
259 int rb_diff
= abs(static_cast<int>(SkColorGetR(accent_color
)) -
260 static_cast<int>(SkColorGetB(accent_color
)));
261 int rg_diff
= abs(static_cast<int>(SkColorGetR(accent_color
)) -
262 static_cast<int>(SkColorGetG(accent_color
)));
263 int bg_diff
= abs(static_cast<int>(SkColorGetB(accent_color
)) -
264 static_cast<int>(SkColorGetG(accent_color
)));
265 if (rb_diff
< 10 && rg_diff
< 10 && bg_diff
< 10) {
266 // Our accent is white/gray/black. Only the luminance of the accent color
270 // Use the saturation of the text.
271 tint
->s
= text_tint
.s
;
273 // Use the luminance of the accent color UNLESS there isn't enough
274 // luminance contrast between the accent color and the base color.
275 if (fabs(accent_tint
.l
- background_tint
.l
) > 0.3)
276 tint
->l
= accent_tint
.l
;
278 tint
->l
= text_tint
.l
;
280 // Our accent is a color.
281 tint
->h
= accent_tint
.h
;
283 // Don't modify the saturation; the amount of color doesn't matter.
286 // If the text wants us to darken the icon, don't change the luminance (the
287 // icons are already dark enough). Otherwise, lighten the icon by no more
288 // than 0.9 since we don't want a pure-white icon even if the text is pure
290 if (text_tint
.l
< 0.5)
292 else if (text_tint
.l
<= 0.9)
293 tint
->l
= text_tint
.l
;
299 // Applies an HSL shift to a GdkColor (instead of an SkColor)
300 void GdkColorHSLShift(const color_utils::HSL
& shift
, GdkColor
* frame_color
) {
301 SkColor shifted
= color_utils::HSLShift(
302 GdkColorToSkColor(*frame_color
), shift
);
304 frame_color
->pixel
= 0;
305 frame_color
->red
= SkColorGetR(shifted
) * kSkiaToGDKMultiplier
;
306 frame_color
->green
= SkColorGetG(shifted
) * kSkiaToGDKMultiplier
;
307 frame_color
->blue
= SkColorGetB(shifted
) * kSkiaToGDKMultiplier
;
310 // Copied Default blah sections from ThemeService.
311 color_utils::HSL
GetDefaultTint(int id
) {
313 case ThemeProperties::TINT_FRAME
:
314 return kDefaultTintFrame
;
315 case ThemeProperties::TINT_FRAME_INACTIVE
:
316 return kDefaultTintFrameInactive
;
317 case ThemeProperties::TINT_FRAME_INCOGNITO
:
318 return kDefaultTintFrameIncognito
;
319 case ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
:
320 return kDefaultTintFrameIncognitoInactive
;
321 case ThemeProperties::TINT_BUTTONS
:
322 return kDefaultTintButtons
;
323 case ThemeProperties::TINT_BACKGROUND_TAB
:
324 return kDefaultTintBackgroundTab
;
326 color_utils::HSL result
= {-1, -1, -1};
331 // Returns a gfx::FontRenderParams corresponding to GTK's configuration.
332 gfx::FontRenderParams
GetGtkFontRenderParams() {
333 GtkSettings
* gtk_settings
= gtk_settings_get_default();
337 gchar
* hint_style
= NULL
;
339 g_object_get(gtk_settings
,
340 "gtk-xft-antialias", &antialias
,
341 "gtk-xft-hinting", &hinting
,
342 "gtk-xft-hintstyle", &hint_style
,
343 "gtk-xft-rgba", &rgba
,
346 gfx::FontRenderParams params
;
347 params
.antialiasing
= antialias
!= 0;
349 if (hinting
== 0 || !hint_style
|| strcmp(hint_style
, "hintnone") == 0) {
350 params
.hinting
= gfx::FontRenderParams::HINTING_NONE
;
351 } else if (strcmp(hint_style
, "hintslight") == 0) {
352 params
.hinting
= gfx::FontRenderParams::HINTING_SLIGHT
;
353 } else if (strcmp(hint_style
, "hintmedium") == 0) {
354 params
.hinting
= gfx::FontRenderParams::HINTING_MEDIUM
;
355 } else if (strcmp(hint_style
, "hintfull") == 0) {
356 params
.hinting
= gfx::FontRenderParams::HINTING_FULL
;
358 LOG(WARNING
) << "Unexpected gtk-xft-hintstyle \"" << hint_style
<< "\"";
359 params
.hinting
= gfx::FontRenderParams::HINTING_NONE
;
362 if (!rgba
|| strcmp(rgba
, "none") == 0) {
363 params
.subpixel_rendering
= gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE
;
364 } else if (strcmp(rgba
, "rgb") == 0) {
365 params
.subpixel_rendering
= gfx::FontRenderParams::SUBPIXEL_RENDERING_RGB
;
366 } else if (strcmp(rgba
, "bgr") == 0) {
367 params
.subpixel_rendering
= gfx::FontRenderParams::SUBPIXEL_RENDERING_BGR
;
368 } else if (strcmp(rgba
, "vrgb") == 0) {
369 params
.subpixel_rendering
= gfx::FontRenderParams::SUBPIXEL_RENDERING_VRGB
;
370 } else if (strcmp(rgba
, "vbgr") == 0) {
371 params
.subpixel_rendering
= gfx::FontRenderParams::SUBPIXEL_RENDERING_VBGR
;
373 LOG(WARNING
) << "Unexpected gtk-xft-rgba \"" << rgba
<< "\"";
374 params
.subpixel_rendering
= gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE
;
383 double GetBaseDPI() {
384 XDisplay
* xdisplay
= gfx::GetXDisplay();
385 int xscreen
= DefaultScreen(xdisplay
);
386 return (DisplayHeight(xdisplay
, xscreen
) * 25.4) /
387 DisplayHeightMM(xdisplay
, xscreen
);
390 double GetFontDPI() {
391 GtkSettings
* gtk_settings
= gtk_settings_get_default();
394 g_object_get(gtk_settings
, "gtk-xft-dpi", >k_dpi
, NULL
);
396 // GTK multiplies the DPI by 1024 before storing it.
397 return (gtk_dpi
> 0) ? gtk_dpi
/ 1024.0 : GetBaseDPI();
400 // Queries GTK for its font DPI setting and returns the number of pixels in a
402 double GetPixelsInPoint(float device_scale_factor
) {
403 double dpi
= GetFontDPI();
405 // Take device_scale_factor into account — if Chrome already scales the
406 // entire UI up by 2x, we should not also scale up.
407 dpi
/= device_scale_factor
;
409 // There are 72 points in an inch.
413 views::LinuxUI::NonClientMiddleClickAction
GetDefaultMiddleClickAction() {
414 scoped_ptr
<base::Environment
> env(base::Environment::Create());
415 switch (base::nix::GetDesktopEnvironment(env
.get())) {
416 case base::nix::DESKTOP_ENVIRONMENT_KDE4
:
417 // Starting with KDE 4.4, windows' titlebars can be dragged with the
418 // middle mouse button to create tab groups. We don't support that in
419 // Chrome, but at least avoid lowering windows in response to middle
420 // clicks to avoid surprising users who expect the KDE behavior.
421 return views::LinuxUI::MIDDLE_CLICK_ACTION_NONE
;
423 return views::LinuxUI::MIDDLE_CLICK_ACTION_LOWER
;
430 : default_font_size_pixels_(0),
431 default_font_style_(gfx::Font::NORMAL
),
432 middle_click_action_(GetDefaultMiddleClickAction()),
433 device_scale_factor_(1.0) {
434 GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess());
437 void Gtk2UI::Initialize() {
438 signals_
.reset(new Gtk2SignalRegistrar
);
440 // Create our fake widgets.
441 fake_window_
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
442 fake_frame_
= chrome_gtk_frame_new();
443 fake_label_
.Own(gtk_label_new(""));
444 fake_entry_
.Own(gtk_entry_new());
446 // Only realized widgets receive style-set notifications, which we need to
447 // broadcast new theme images and colors. Only realized widgets have style
448 // properties, too, which we query for some colors.
449 gtk_widget_realize(fake_frame_
);
450 gtk_widget_realize(fake_window_
);
452 signals_
->Connect(fake_frame_
, "style-set",
453 G_CALLBACK(&OnStyleSetThunk
), this);
457 printing::PrintingContextLinux::SetCreatePrintDialogFunction(
458 &PrintDialogGtk2::CreatePrintDialog
);
459 printing::PrintingContextLinux::SetPdfPaperSizeFunction(
460 &GetPdfPaperSizeDeviceUnitsGtk
);
462 #if defined(USE_GCONF)
463 // We must build this after GTK gets initialized.
464 gconf_listener_
.reset(new GConfListener(this));
465 #endif // defined(USE_GCONF)
467 indicators_count
= 0;
469 // Instantiate the singleton instance of Gtk2EventLoop.
470 Gtk2EventLoop::GetInstance();
474 gtk_widget_destroy(fake_window_
);
475 gtk_widget_destroy(fake_frame_
);
476 fake_label_
.Destroy();
477 fake_entry_
.Destroy();
482 gfx::Image
Gtk2UI::GetThemeImageNamed(int id
) const {
483 // Try to get our cached version:
484 ImageCache::const_iterator it
= gtk_images_
.find(id
);
485 if (it
!= gtk_images_
.end())
488 if (IsOverridableImage(id
)) {
489 gfx::Image image
= gfx::Image(
490 gfx::ImageSkia::CreateFrom1xBitmap(GenerateGtkThemeBitmap(id
)));
491 gtk_images_
[id
] = image
;
498 bool Gtk2UI::GetColor(int id
, SkColor
* color
) const {
499 ColorMap::const_iterator it
= colors_
.find(id
);
500 if (it
!= colors_
.end()) {
508 bool Gtk2UI::HasCustomImage(int id
) const {
509 return IsOverridableImage(id
);
512 SkColor
Gtk2UI::GetFocusRingColor() const {
513 return focus_ring_color_
;
516 SkColor
Gtk2UI::GetThumbActiveColor() const {
517 return thumb_active_color_
;
520 SkColor
Gtk2UI::GetThumbInactiveColor() const {
521 return thumb_inactive_color_
;
524 SkColor
Gtk2UI::GetTrackColor() const {
528 SkColor
Gtk2UI::GetActiveSelectionBgColor() const {
529 return active_selection_bg_color_
;
532 SkColor
Gtk2UI::GetActiveSelectionFgColor() const {
533 return active_selection_fg_color_
;
536 SkColor
Gtk2UI::GetInactiveSelectionBgColor() const {
537 return inactive_selection_bg_color_
;
540 SkColor
Gtk2UI::GetInactiveSelectionFgColor() const {
541 return inactive_selection_fg_color_
;
544 double Gtk2UI::GetCursorBlinkInterval() const {
545 // From http://library.gnome.org/devel/gtk/unstable/GtkSettings.html, this is
546 // the default value for gtk-cursor-blink-time.
547 static const gint kGtkDefaultCursorBlinkTime
= 1200;
549 // Dividing GTK's cursor blink cycle time (in milliseconds) by this value
550 // yields an appropriate value for
551 // content::RendererPreferences::caret_blink_interval. This matches the
552 // logic in the WebKit GTK port.
553 static const double kGtkCursorBlinkCycleFactor
= 2000.0;
555 gint cursor_blink_time
= kGtkDefaultCursorBlinkTime
;
556 gboolean cursor_blink
= TRUE
;
557 g_object_get(gtk_settings_get_default(),
558 "gtk-cursor-blink-time", &cursor_blink_time
,
559 "gtk-cursor-blink", &cursor_blink
,
561 return cursor_blink
? (cursor_blink_time
/ kGtkCursorBlinkCycleFactor
) : 0.0;
564 ui::NativeTheme
* Gtk2UI::GetNativeTheme(aura::Window
* window
) const {
565 ui::NativeTheme
* native_theme_override
= NULL
;
566 if (!native_theme_overrider_
.is_null())
567 native_theme_override
= native_theme_overrider_
.Run(window
);
569 if (native_theme_override
)
570 return native_theme_override
;
572 return NativeThemeGtk2::instance();
575 void Gtk2UI::SetNativeThemeOverride(const NativeThemeGetter
& callback
) {
576 native_theme_overrider_
= callback
;
579 bool Gtk2UI::GetDefaultUsesSystemTheme() const {
580 scoped_ptr
<base::Environment
> env(base::Environment::Create());
582 switch (base::nix::GetDesktopEnvironment(env
.get())) {
583 case base::nix::DESKTOP_ENVIRONMENT_GNOME
:
584 case base::nix::DESKTOP_ENVIRONMENT_UNITY
:
585 case base::nix::DESKTOP_ENVIRONMENT_XFCE
:
587 case base::nix::DESKTOP_ENVIRONMENT_KDE3
:
588 case base::nix::DESKTOP_ENVIRONMENT_KDE4
:
589 case base::nix::DESKTOP_ENVIRONMENT_OTHER
:
592 // Unless GetDesktopEnvironment() badly misbehaves, this should never happen.
597 void Gtk2UI::SetDownloadCount(int count
) const {
598 if (unity::IsRunning())
599 unity::SetDownloadCount(count
);
602 void Gtk2UI::SetProgressFraction(float percentage
) const {
603 if (unity::IsRunning())
604 unity::SetProgressFraction(percentage
);
607 bool Gtk2UI::IsStatusIconSupported() const {
611 scoped_ptr
<views::StatusIconLinux
> Gtk2UI::CreateLinuxStatusIcon(
612 const gfx::ImageSkia
& image
,
613 const base::string16
& tool_tip
) const {
614 if (AppIndicatorIcon::CouldOpen()) {
616 return scoped_ptr
<views::StatusIconLinux
>(new AppIndicatorIcon(
617 base::StringPrintf("%s%d", kAppIndicatorIdPrefix
, indicators_count
),
621 return scoped_ptr
<views::StatusIconLinux
>(new Gtk2StatusIcon(
626 gfx::Image
Gtk2UI::GetIconForContentType(
627 const std::string
& content_type
,
629 // This call doesn't take a reference.
630 GtkIconTheme
* theme
= gtk_icon_theme_get_default();
632 std::string content_types
[] = {
633 content_type
, kUnknownContentType
636 for (size_t i
= 0; i
< arraysize(content_types
); ++i
) {
637 ScopedGIcon
icon(g_content_type_get_icon(content_types
[i
].c_str()));
638 ScopedGtkIconInfo
icon_info(
639 gtk_icon_theme_lookup_by_gicon(
640 theme
, icon
.get(), size
,
641 static_cast<GtkIconLookupFlags
>(GTK_ICON_LOOKUP_FORCE_SIZE
)));
644 ScopedGdkPixbuf
pixbuf(gtk_icon_info_load_icon(icon_info
.get(), NULL
));
648 SkBitmap bitmap
= GdkPixbufToImageSkia(pixbuf
.get());
649 DCHECK_EQ(size
, bitmap
.width());
650 DCHECK_EQ(size
, bitmap
.height());
651 gfx::ImageSkia image_skia
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
652 image_skia
.MakeThreadSafe();
653 return gfx::Image(image_skia
);
658 scoped_ptr
<views::Border
> Gtk2UI::CreateNativeBorder(
659 views::LabelButton
* owning_button
,
660 scoped_ptr
<views::LabelButtonBorder
> border
) {
661 if (owning_button
->GetNativeTheme() != NativeThemeGtk2::instance())
662 return border
.Pass();
664 return make_scoped_ptr(new Gtk2Border(this, owning_button
, border
.Pass()));
667 void Gtk2UI::AddWindowButtonOrderObserver(
668 views::WindowButtonOrderObserver
* observer
) {
669 if (!leading_buttons_
.empty() || !trailing_buttons_
.empty()) {
670 observer
->OnWindowButtonOrderingChange(leading_buttons_
,
674 observer_list_
.AddObserver(observer
);
677 void Gtk2UI::RemoveWindowButtonOrderObserver(
678 views::WindowButtonOrderObserver
* observer
) {
679 observer_list_
.RemoveObserver(observer
);
682 void Gtk2UI::SetWindowButtonOrdering(
683 const std::vector
<views::FrameButton
>& leading_buttons
,
684 const std::vector
<views::FrameButton
>& trailing_buttons
) {
685 leading_buttons_
= leading_buttons
;
686 trailing_buttons_
= trailing_buttons
;
688 FOR_EACH_OBSERVER(views::WindowButtonOrderObserver
, observer_list_
,
689 OnWindowButtonOrderingChange(leading_buttons_
,
693 void Gtk2UI::SetNonClientMiddleClickAction(NonClientMiddleClickAction action
) {
694 middle_click_action_
= action
;
697 scoped_ptr
<ui::LinuxInputMethodContext
> Gtk2UI::CreateInputMethodContext(
698 ui::LinuxInputMethodContextDelegate
* delegate
,
699 bool is_simple
) const {
700 return scoped_ptr
<ui::LinuxInputMethodContext
>(
701 new X11InputMethodContextImplGtk2(delegate
, is_simple
));
704 gfx::FontRenderParams
Gtk2UI::GetDefaultFontRenderParams() const {
705 static gfx::FontRenderParams params
= GetGtkFontRenderParams();
709 void Gtk2UI::GetDefaultFontDescription(
710 std::string
* family_out
,
711 int* size_pixels_out
,
713 gfx::FontRenderParams
* params_out
) const {
714 *family_out
= default_font_family_
;
715 *size_pixels_out
= default_font_size_pixels_
;
716 *style_out
= default_font_style_
;
717 *params_out
= default_font_render_params_
;
720 ui::SelectFileDialog
* Gtk2UI::CreateSelectFileDialog(
721 ui::SelectFileDialog::Listener
* listener
,
722 ui::SelectFilePolicy
* policy
) const {
723 return SelectFileDialogImpl::Create(listener
, policy
);
726 bool Gtk2UI::UnityIsRunning() {
727 return unity::IsRunning();
730 views::LinuxUI::NonClientMiddleClickAction
731 Gtk2UI::GetNonClientMiddleClickAction() {
732 return middle_click_action_
;
735 void Gtk2UI::NotifyWindowManagerStartupComplete() {
736 // TODO(port) Implement this using _NET_STARTUP_INFO_BEGIN/_NET_STARTUP_INFO
737 // from http://standards.freedesktop.org/startup-notification-spec/ instead.
738 gdk_notify_startup_complete();
741 bool Gtk2UI::MatchEvent(const ui::Event
& event
,
742 std::vector
<ui::TextEditCommandAuraLinux
>* commands
) {
743 // Ensure that we have a keyboard handler.
744 if (!key_bindings_handler_
)
745 key_bindings_handler_
.reset(new Gtk2KeyBindingsHandler
);
747 return key_bindings_handler_
->MatchEvent(event
, commands
);
750 void Gtk2UI::GetScrollbarColors(GdkColor
* thumb_active_color
,
751 GdkColor
* thumb_inactive_color
,
752 GdkColor
* track_color
) {
753 GdkColor
* theme_thumb_active
= NULL
;
754 GdkColor
* theme_thumb_inactive
= NULL
;
755 GdkColor
* theme_trough_color
= NULL
;
756 gtk_widget_style_get(GTK_WIDGET(fake_frame_
),
757 "scrollbar-slider-prelight-color", &theme_thumb_active
,
758 "scrollbar-slider-normal-color", &theme_thumb_inactive
,
759 "scrollbar-trough-color", &theme_trough_color
,
762 // Ask the theme if the theme specifies all the scrollbar colors and short
763 // circuit the expensive painting/compositing if we have all of them.
764 if (theme_thumb_active
&& theme_thumb_inactive
&& theme_trough_color
) {
765 *thumb_active_color
= *theme_thumb_active
;
766 *thumb_inactive_color
= *theme_thumb_inactive
;
767 *track_color
= *theme_trough_color
;
769 gdk_color_free(theme_thumb_active
);
770 gdk_color_free(theme_thumb_inactive
);
771 gdk_color_free(theme_trough_color
);
775 // Create window containing scrollbar elements
776 GtkWidget
* window
= gtk_window_new(GTK_WINDOW_POPUP
);
777 GtkWidget
* fixed
= gtk_fixed_new();
778 GtkWidget
* scrollbar
= gtk_hscrollbar_new(NULL
);
779 gtk_container_add(GTK_CONTAINER(window
), fixed
);
780 gtk_container_add(GTK_CONTAINER(fixed
), scrollbar
);
781 gtk_widget_realize(window
);
782 gtk_widget_realize(scrollbar
);
784 // Draw scrollbar thumb part and track into offscreen image
785 const int kWidth
= 100;
786 const int kHeight
= 20;
787 GtkStyle
* style
= gtk_rc_get_style(scrollbar
);
788 GdkWindow
* gdk_window
= gtk_widget_get_window(window
);
789 GdkPixmap
* pm
= gdk_pixmap_new(gdk_window
, kWidth
, kHeight
, -1);
790 GdkRectangle rect
= { 0, 0, kWidth
, kHeight
};
791 unsigned char data
[3 * kWidth
* kHeight
];
792 for (int i
= 0; i
< 3; ++i
) {
795 gtk_paint_slider(style
, pm
,
796 i
== 0 ? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
797 GTK_SHADOW_OUT
, &rect
, scrollbar
, "slider", 0, 0,
798 kWidth
, kHeight
, GTK_ORIENTATION_HORIZONTAL
);
801 gtk_paint_box(style
, pm
, GTK_STATE_ACTIVE
, GTK_SHADOW_IN
, &rect
,
802 scrollbar
, "trough-upper", 0, 0, kWidth
, kHeight
);
804 GdkPixbuf
* pb
= gdk_pixbuf_new_from_data(data
, GDK_COLORSPACE_RGB
,
805 FALSE
, 8, kWidth
, kHeight
,
807 gdk_pixbuf_get_from_drawable(pb
, pm
, NULL
, 0, 0, 0, 0, kWidth
, kHeight
);
810 int components
[3] = { 0 };
811 for (int y
= 2; y
< kHeight
- 2; ++y
) {
812 for (int c
= 0; c
< 3; ++c
) {
813 // Sample a vertical slice of pixels at about one-thirds from the
814 // left edge. This allows us to avoid any fixed graphics that might be
815 // located at the edges or in the center of the scrollbar.
816 // Each pixel is made up of a red, green, and blue component; taking up
817 // a total of three bytes.
818 components
[c
] += data
[3 * (kWidth
/ 3 + y
* kWidth
) + c
];
821 GdkColor
* color
= i
== 0 ? thumb_active_color
:
822 i
== 1 ? thumb_inactive_color
:
825 // We sampled pixels across the full height of the image, ignoring a two
826 // pixel border. In some themes, the border has a completely different
827 // color which we do not want to factor into our average color computation.
829 // We now need to scale the colors from the 0..255 range, to the wider
830 // 0..65535 range, and we need to actually compute the average color; so,
831 // we divide by the total number of pixels in the sample.
832 color
->red
= components
[0] * 65535 / (255 * (kHeight
- 4));
833 color
->green
= components
[1] * 65535 / (255 * (kHeight
- 4));
834 color
->blue
= components
[2] * 65535 / (255 * (kHeight
- 4));
840 gtk_widget_destroy(window
);
842 // Override any of the default colors with ones that were specified by the
844 if (theme_thumb_active
) {
845 *thumb_active_color
= *theme_thumb_active
;
846 gdk_color_free(theme_thumb_active
);
849 if (theme_thumb_inactive
) {
850 *thumb_inactive_color
= *theme_thumb_inactive
;
851 gdk_color_free(theme_thumb_inactive
);
854 if (theme_trough_color
) {
855 *track_color
= *theme_trough_color
;
856 gdk_color_free(theme_trough_color
);
860 void Gtk2UI::LoadGtkValues() {
861 // TODO(erg): GtkThemeService had a comment here about having to muck with
862 // the raw Prefs object to remove prefs::kCurrentThemeImages or else we'd
863 // regress startup time. Figure out how to do that when we can't access the
864 // prefs system from here.
866 GtkStyle
* frame_style
= gtk_rc_get_style(fake_frame_
);
868 GtkStyle
* window_style
= gtk_rc_get_style(fake_window_
);
869 SetThemeColorFromGtk(ThemeProperties::COLOR_CONTROL_BACKGROUND
,
870 &window_style
->bg
[GTK_STATE_NORMAL
]);
872 GdkColor toolbar_color
= window_style
->bg
[GTK_STATE_NORMAL
];
873 SetThemeColorFromGtk(ThemeProperties::COLOR_TOOLBAR
, &toolbar_color
);
875 GdkColor button_color
= window_style
->bg
[GTK_STATE_SELECTED
];
876 SetThemeTintFromGtk(ThemeProperties::TINT_BUTTONS
, &button_color
);
878 GtkStyle
* label_style
= gtk_rc_get_style(fake_label_
.get());
879 GdkColor label_color
= label_style
->fg
[GTK_STATE_NORMAL
];
880 SetThemeColorFromGtk(ThemeProperties::COLOR_TAB_TEXT
, &label_color
);
881 SetThemeColorFromGtk(ThemeProperties::COLOR_BOOKMARK_TEXT
, &label_color
);
882 SetThemeColorFromGtk(ThemeProperties::COLOR_STATUS_BAR_TEXT
, &label_color
);
884 UpdateDefaultFont(label_style
->font_desc
);
886 // Build the various icon tints.
887 GetNormalButtonTintHSL(&button_tint_
);
888 GetNormalEntryForegroundHSL(&entry_tint_
);
889 GetSelectedEntryForegroundHSL(&selected_entry_tint_
);
890 GdkColor frame_color
= BuildFrameColors(frame_style
);
892 // The inactive frame color never occurs naturally in the theme, as it is a
893 // tinted version of |frame_color|. We generate another color based on the
894 // background tab color, with the lightness and saturation moved in the
895 // opposite direction. (We don't touch the hue, since there should be subtle
896 // hints of the color in the text.)
897 color_utils::HSL inactive_tab_text_hsl
=
898 tints_
[ThemeProperties::TINT_BACKGROUND_TAB
];
899 if (inactive_tab_text_hsl
.l
< 0.5)
900 inactive_tab_text_hsl
.l
= kDarkInactiveLuminance
;
902 inactive_tab_text_hsl
.l
= kLightInactiveLuminance
;
904 if (inactive_tab_text_hsl
.s
< 0.5)
905 inactive_tab_text_hsl
.s
= kHeavyInactiveSaturation
;
907 inactive_tab_text_hsl
.s
= kLightInactiveSaturation
;
909 colors_
[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT
] =
910 color_utils::HSLToSkColor(inactive_tab_text_hsl
, 255);
912 // We pick the text and background colors for the NTP out of the colors for a
913 // GtkEntry. We do this because GtkEntries background color is never the same
914 // as |toolbar_color|, is usually a white, and when it isn't a white,
915 // provides sufficient contrast to |toolbar_color|. Try this out with
916 // Darklooks, HighContrastInverse or ThinIce.
917 GtkStyle
* entry_style
= gtk_rc_get_style(fake_entry_
.get());
918 GdkColor ntp_background
= entry_style
->base
[GTK_STATE_NORMAL
];
919 GdkColor ntp_foreground
= entry_style
->text
[GTK_STATE_NORMAL
];
920 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_BACKGROUND
,
922 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_TEXT
,
925 // The NTP header is the color that surrounds the current active thumbnail on
926 // the NTP, and acts as the border of the "Recent Links" box. It would be
927 // awesome if they were separated so we could use GetBorderColor() for the
928 // border around the "Recent Links" section, but matching the frame color is
930 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_HEADER
,
932 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION
,
934 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_TEXT
,
937 // Override the link color if the theme provides it.
938 const GdkColor
* link_color
= NULL
;
939 gtk_widget_style_get(GTK_WIDGET(fake_window_
),
940 "link-color", &link_color
, NULL
);
942 bool is_default_link_color
= false;
944 link_color
= &kDefaultLinkColor
;
945 is_default_link_color
= true;
948 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK
,
950 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK_UNDERLINE
,
952 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK
,
954 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
,
957 if (!is_default_link_color
)
958 gdk_color_free(const_cast<GdkColor
*>(link_color
));
960 // Generate the colors that we pass to WebKit.
961 focus_ring_color_
= GdkColorToSkColor(frame_color
);
963 GdkColor thumb_active_color
, thumb_inactive_color
, track_color
;
964 Gtk2UI::GetScrollbarColors(&thumb_active_color
,
965 &thumb_inactive_color
,
967 thumb_active_color_
= GdkColorToSkColor(thumb_active_color
);
968 thumb_inactive_color_
= GdkColorToSkColor(thumb_inactive_color
);
969 track_color_
= GdkColorToSkColor(track_color
);
971 // Some GTK themes only define the text selection colors on the GtkEntry
972 // class, so we need to use that for getting selection colors.
973 active_selection_bg_color_
=
974 GdkColorToSkColor(entry_style
->base
[GTK_STATE_SELECTED
]);
975 active_selection_fg_color_
=
976 GdkColorToSkColor(entry_style
->text
[GTK_STATE_SELECTED
]);
977 inactive_selection_bg_color_
=
978 GdkColorToSkColor(entry_style
->base
[GTK_STATE_ACTIVE
]);
979 inactive_selection_fg_color_
=
980 GdkColorToSkColor(entry_style
->text
[GTK_STATE_ACTIVE
]);
983 GdkColor
Gtk2UI::BuildFrameColors(GtkStyle
* frame_style
) {
984 GdkColor
* theme_frame
= NULL
;
985 GdkColor
* theme_inactive_frame
= NULL
;
986 GdkColor
* theme_incognito_frame
= NULL
;
987 GdkColor
* theme_incognito_inactive_frame
= NULL
;
988 gtk_widget_style_get(GTK_WIDGET(fake_frame_
),
989 "frame-color", &theme_frame
,
990 "inactive-frame-color", &theme_inactive_frame
,
991 "incognito-frame-color", &theme_incognito_frame
,
992 "incognito-inactive-frame-color",
993 &theme_incognito_inactive_frame
,
996 GdkColor frame_color
= BuildAndSetFrameColor(
997 &frame_style
->bg
[GTK_STATE_SELECTED
],
1000 ThemeProperties::COLOR_FRAME
,
1001 ThemeProperties::TINT_FRAME
);
1003 gdk_color_free(theme_frame
);
1004 SetThemeTintFromGtk(ThemeProperties::TINT_BACKGROUND_TAB
, &frame_color
);
1006 BuildAndSetFrameColor(
1007 &frame_style
->bg
[GTK_STATE_INSENSITIVE
],
1008 theme_inactive_frame
,
1010 ThemeProperties::COLOR_FRAME_INACTIVE
,
1011 ThemeProperties::TINT_FRAME_INACTIVE
);
1012 if (theme_inactive_frame
)
1013 gdk_color_free(theme_inactive_frame
);
1015 BuildAndSetFrameColor(
1017 theme_incognito_frame
,
1018 GetDefaultTint(ThemeProperties::TINT_FRAME_INCOGNITO
),
1019 ThemeProperties::COLOR_FRAME_INCOGNITO
,
1020 ThemeProperties::TINT_FRAME_INCOGNITO
);
1021 if (theme_incognito_frame
)
1022 gdk_color_free(theme_incognito_frame
);
1024 BuildAndSetFrameColor(
1026 theme_incognito_inactive_frame
,
1027 GetDefaultTint(ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
),
1028 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
,
1029 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
);
1030 if (theme_incognito_inactive_frame
)
1031 gdk_color_free(theme_incognito_inactive_frame
);
1036 void Gtk2UI::SetThemeColorFromGtk(int id
, const GdkColor
* color
) {
1037 colors_
[id
] = GdkColorToSkColor(*color
);
1040 void Gtk2UI::SetThemeTintFromGtk(int id
, const GdkColor
* color
) {
1041 color_utils::HSL default_tint
= GetDefaultTint(id
);
1042 color_utils::HSL hsl
;
1043 color_utils::SkColorToHSL(GdkColorToSkColor(*color
), &hsl
);
1045 if (default_tint
.s
!= -1)
1046 hsl
.s
= default_tint
.s
;
1048 if (default_tint
.l
!= -1)
1049 hsl
.l
= default_tint
.l
;
1054 GdkColor
Gtk2UI::BuildAndSetFrameColor(const GdkColor
* base
,
1055 const GdkColor
* gtk_base
,
1056 const color_utils::HSL
& tint
,
1059 GdkColor out_color
= *base
;
1061 // The theme author specified a color to use, use it without modification.
1062 out_color
= *gtk_base
;
1064 // Tint the basic color since this is a heuristic color instead of one
1065 // specified by the theme author.
1066 GdkColorHSLShift(tint
, &out_color
);
1068 SetThemeColorFromGtk(color_id
, &out_color
);
1069 SetThemeTintFromGtk(tint_id
, &out_color
);
1074 SkBitmap
Gtk2UI::GenerateGtkThemeBitmap(int id
) const {
1076 case IDR_THEME_TOOLBAR
: {
1077 GtkStyle
* style
= gtk_rc_get_style(fake_window_
);
1078 GdkColor
* color
= &style
->bg
[GTK_STATE_NORMAL
];
1080 bitmap
.allocN32Pixels(kToolbarImageWidth
, kToolbarImageHeight
);
1081 bitmap
.eraseARGB(0xff, color
->red
>> 8, color
->green
>> 8,
1085 case IDR_THEME_TAB_BACKGROUND
:
1086 case IDR_THEME_TAB_BACKGROUND_DESKTOP
:
1087 return GenerateTabImage(IDR_THEME_FRAME
);
1088 case IDR_THEME_TAB_BACKGROUND_INCOGNITO
:
1089 case IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
:
1090 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO
);
1092 case IDR_THEME_FRAME
:
1093 return GenerateFrameImage(ThemeProperties::COLOR_FRAME
,
1094 "frame-gradient-color");
1095 case IDR_FRAME_INACTIVE
:
1096 case IDR_THEME_FRAME_INACTIVE
:
1097 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INACTIVE
,
1098 "inactive-frame-gradient-color");
1099 case IDR_THEME_FRAME_INCOGNITO
:
1100 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INCOGNITO
,
1101 "incognito-frame-gradient-color");
1102 case IDR_THEME_FRAME_INCOGNITO_INACTIVE
: {
1103 return GenerateFrameImage(
1104 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
,
1105 "incognito-inactive-frame-gradient-color");
1107 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and
1108 // instead should tint based on the foreground text entry color in GTK+
1109 // mode because some themes that try to be dark *and* light have very
1110 // different colors between the omnibox and the normal background area.
1111 // TODO(erg): Decide what to do about other icons that appear in the
1112 // omnibox, e.g. content settings icons.
1113 case IDR_OMNIBOX_CALCULATOR
:
1114 case IDR_OMNIBOX_EXTENSION_APP
:
1115 case IDR_OMNIBOX_HTTP
:
1116 case IDR_OMNIBOX_SEARCH
:
1117 case IDR_OMNIBOX_STAR
:
1118 case IDR_OMNIBOX_TTS
: {
1119 return GenerateTintedIcon(id
, entry_tint_
);
1121 // In GTK mode, the dark versions of the omnibox icons only ever appear in
1122 // the autocomplete popup and only against the current theme's GtkEntry
1123 // base[GTK_STATE_SELECTED] color, so tint the icons so they won't collide
1124 // with the selected color.
1125 case IDR_OMNIBOX_EXTENSION_APP_DARK
:
1126 case IDR_OMNIBOX_HTTP_DARK
:
1127 case IDR_OMNIBOX_SEARCH_DARK
:
1128 case IDR_OMNIBOX_STAR_DARK
:
1129 case IDR_OMNIBOX_TTS_DARK
: {
1130 return GenerateTintedIcon(id
, selected_entry_tint_
);
1132 // In GTK mode, we need to manually render several icons.
1142 return GenerateGTKIcon(id
);
1144 case IDR_TOOLBAR_BEZEL_HOVER
:
1145 return GenerateToolbarBezel(GTK_STATE_PRELIGHT
, IDR_TOOLBAR_BEZEL_HOVER
);
1146 case IDR_TOOLBAR_BEZEL_PRESSED
:
1147 return GenerateToolbarBezel(GTK_STATE_ACTIVE
, IDR_TOOLBAR_BEZEL_PRESSED
);
1149 return GenerateTintedIcon(id
, button_tint_
);
1156 SkBitmap
Gtk2UI::GenerateFrameImage(
1158 const char* gradient_name
) const {
1159 // We use two colors: the main color (passed in) and a lightened version of
1160 // that color (which is supposed to match the light gradient at the top of
1161 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird).
1162 ColorMap::const_iterator it
= colors_
.find(color_id
);
1163 DCHECK(it
!= colors_
.end());
1164 SkColor base
= it
->second
;
1166 gfx::Canvas
canvas(gfx::Size(kToolbarImageWidth
, kToolbarImageHeight
),
1170 GdkColor
* gradient_top_color
= NULL
;
1171 gtk_widget_style_get(GTK_WIDGET(fake_frame_
),
1172 "frame-gradient-size", &gradient_size
,
1173 gradient_name
, &gradient_top_color
,
1175 if (gradient_size
) {
1176 SkColor lighter
= gradient_top_color
?
1177 GdkColorToSkColor(*gradient_top_color
) :
1178 color_utils::HSLShift(base
, kGtkFrameShift
);
1179 skia::RefPtr
<SkShader
> shader
= gfx::CreateGradientShader(
1180 0, gradient_size
, lighter
, base
);
1182 paint
.setStyle(SkPaint::kFill_Style
);
1183 paint
.setAntiAlias(true);
1184 paint
.setShader(shader
.get());
1186 canvas
.DrawRect(gfx::Rect(0, 0, kToolbarImageWidth
, gradient_size
), paint
);
1189 if (gradient_top_color
)
1190 gdk_color_free(gradient_top_color
);
1192 canvas
.FillRect(gfx::Rect(0, gradient_size
, kToolbarImageWidth
,
1193 kToolbarImageHeight
- gradient_size
), base
);
1194 return canvas
.ExtractImageRep().sk_bitmap();
1197 SkBitmap
Gtk2UI::GenerateTabImage(int base_id
) const {
1198 const SkBitmap
* base_image
= GetThemeImageNamed(base_id
).ToSkBitmap();
1199 SkBitmap bg_tint
= SkBitmapOperations::CreateHSLShiftedBitmap(
1200 *base_image
, GetDefaultTint(ThemeProperties::TINT_BACKGROUND_TAB
));
1201 return SkBitmapOperations::CreateTiledBitmap(
1202 bg_tint
, 0, 0, bg_tint
.width(), bg_tint
.height());
1205 SkBitmap
Gtk2UI::GenerateTintedIcon(
1207 const color_utils::HSL
& tint
) const {
1208 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
1209 return SkBitmapOperations::CreateHSLShiftedBitmap(
1210 rb
.GetImageNamed(base_id
).AsBitmap(), tint
);
1213 SkBitmap
Gtk2UI::GenerateGTKIcon(int base_id
) const {
1214 const char* stock_id
= NULL
;
1215 GtkStateType gtk_state
= GTK_STATE_NORMAL
;
1216 for (unsigned int i
= 0; i
< arraysize(kGtkIcons
); ++i
) {
1217 if (kGtkIcons
[i
].idr
== base_id
) {
1218 stock_id
= kGtkIcons
[i
].stock_id
;
1219 gtk_state
= kGtkIcons
[i
].gtk_state
;
1225 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
1226 SkBitmap default_bitmap
= rb
.GetImageNamed(base_id
).AsBitmap();
1228 gtk_widget_ensure_style(fake_frame_
);
1229 GtkStyle
* style
= gtk_widget_get_style(fake_frame_
);
1230 GtkIconSet
* icon_set
= gtk_style_lookup_icon_set(style
, stock_id
);
1232 return default_bitmap
;
1234 // Ask GTK to render the icon to a buffer, which we will steal from.
1235 GdkPixbuf
* gdk_icon
= gtk_icon_set_render_icon(
1238 base::i18n::IsRTL() ? GTK_TEXT_DIR_RTL
: GTK_TEXT_DIR_LTR
,
1240 GTK_ICON_SIZE_SMALL_TOOLBAR
,
1245 // This can theoretically happen if an icon theme doesn't provide a
1246 // specific image. This should realistically never happen, but I bet there
1247 // are some theme authors who don't reliably provide all icons.
1248 return default_bitmap
;
1252 retval
.allocN32Pixels(default_bitmap
.width(), default_bitmap
.height());
1253 retval
.eraseColor(0);
1255 const SkBitmap icon
= GdkPixbufToImageSkia(gdk_icon
);
1256 g_object_unref(gdk_icon
);
1258 SkCanvas
canvas(retval
);
1260 if (gtk_state
== GTK_STATE_ACTIVE
|| gtk_state
== GTK_STATE_PRELIGHT
) {
1261 SkBitmap border
= DrawGtkButtonBorder(gtk_state
,
1264 default_bitmap
.width(),
1265 default_bitmap
.height());
1266 canvas
.drawBitmap(border
, 0, 0);
1269 canvas
.drawBitmap(icon
,
1270 (default_bitmap
.width() / 2) - (icon
.width() / 2),
1271 (default_bitmap
.height() / 2) - (icon
.height() / 2));
1276 SkBitmap
Gtk2UI::GenerateToolbarBezel(int gtk_state
, int sizing_idr
) const {
1277 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
1278 SkBitmap default_bitmap
=
1279 rb
.GetImageNamed(sizing_idr
).AsBitmap();
1282 retval
.allocN32Pixels(default_bitmap
.width(), default_bitmap
.height());
1283 retval
.eraseColor(0);
1285 SkCanvas
canvas(retval
);
1286 SkBitmap border
= DrawGtkButtonBorder(
1290 default_bitmap
.width(),
1291 default_bitmap
.height());
1292 canvas
.drawBitmap(border
, 0, 0);
1297 void Gtk2UI::GetNormalButtonTintHSL(color_utils::HSL
* tint
) const {
1298 GtkStyle
* window_style
= gtk_rc_get_style(fake_window_
);
1299 const GdkColor accent_gdk_color
= window_style
->bg
[GTK_STATE_SELECTED
];
1300 const GdkColor base_color
= window_style
->base
[GTK_STATE_NORMAL
];
1302 GtkStyle
* label_style
= gtk_rc_get_style(fake_label_
.get());
1303 const GdkColor text_color
= label_style
->fg
[GTK_STATE_NORMAL
];
1305 PickButtonTintFromColors(accent_gdk_color
, text_color
, base_color
, tint
);
1308 void Gtk2UI::GetNormalEntryForegroundHSL(color_utils::HSL
* tint
) const {
1309 GtkStyle
* window_style
= gtk_rc_get_style(fake_window_
);
1310 const GdkColor accent_gdk_color
= window_style
->bg
[GTK_STATE_SELECTED
];
1312 GtkStyle
* style
= gtk_rc_get_style(fake_entry_
.get());
1313 const GdkColor text_color
= style
->text
[GTK_STATE_NORMAL
];
1314 const GdkColor base_color
= style
->base
[GTK_STATE_NORMAL
];
1316 PickButtonTintFromColors(accent_gdk_color
, text_color
, base_color
, tint
);
1319 void Gtk2UI::GetSelectedEntryForegroundHSL(color_utils::HSL
* tint
) const {
1320 // The simplest of all the tints. We just use the selected text in the entry
1321 // since the icons tinted this way will only be displayed against
1322 // base[GTK_STATE_SELECTED].
1323 GtkStyle
* style
= gtk_rc_get_style(fake_entry_
.get());
1324 const GdkColor color
= style
->text
[GTK_STATE_SELECTED
];
1325 color_utils::SkColorToHSL(GdkColorToSkColor(color
), tint
);
1328 SkBitmap
Gtk2UI::DrawGtkButtonBorder(int gtk_state
,
1330 bool call_to_action
,
1333 // Create a temporary GTK button to snapshot
1334 GtkWidget
* window
= gtk_offscreen_window_new();
1335 GtkWidget
* button
= gtk_button_new();
1336 gtk_widget_set_size_request(button
, width
, height
);
1337 gtk_container_add(GTK_CONTAINER(window
), button
);
1338 gtk_widget_realize(window
);
1339 gtk_widget_realize(button
);
1340 gtk_widget_show(button
);
1341 gtk_widget_show(window
);
1344 GTK_WIDGET_SET_FLAGS(button
, GTK_HAS_DEFAULT
);
1347 // We can't just use gtk_widget_grab_focus() here because that sets
1348 // gtk_widget_is_focus(), but not gtk_widget_has_focus(), which is what the
1349 // GtkButton's paint checks.
1350 GTK_WIDGET_SET_FLAGS(button
, GTK_HAS_FOCUS
);
1353 gtk_widget_set_state(button
, static_cast<GtkStateType
>(gtk_state
));
1357 // http://crbug.com/346740
1358 ANNOTATE_SCOPED_MEMORY_LEAK
;
1359 pixmap
= gtk_widget_get_snapshot(button
, NULL
);
1362 gdk_drawable_get_size(GDK_DRAWABLE(pixmap
), &w
, &h
);
1363 DCHECK_EQ(w
, width
);
1364 DCHECK_EQ(h
, height
);
1366 // We render the Pixmap to a Pixbuf. This can be slow, as we're scrapping
1368 GdkColormap
* colormap
= gdk_drawable_get_colormap(pixmap
);
1369 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(NULL
,
1370 GDK_DRAWABLE(pixmap
),
1374 // Finally, we convert our pixbuf into a type we can use.
1375 SkBitmap border
= GdkPixbufToImageSkia(pixbuf
);
1376 g_object_unref(pixbuf
);
1377 g_object_unref(pixmap
);
1378 gtk_widget_destroy(window
);
1383 void Gtk2UI::ClearAllThemeData() {
1384 gtk_images_
.clear();
1387 void Gtk2UI::UpdateDefaultFont(const PangoFontDescription
* desc
) {
1388 // Use gfx::FontRenderParams to select a family and determine the rendering
1390 gfx::FontRenderParamsQuery query
;
1391 base::SplitString(pango_font_description_get_family(desc
), ',',
1394 if (pango_font_description_get_size_is_absolute(desc
)) {
1395 // If the size is absolute, it's specified in Pango units. There are
1396 // PANGO_SCALE Pango units in a device unit (pixel).
1397 const int size_pixels
= pango_font_description_get_size(desc
) / PANGO_SCALE
;
1398 default_font_size_pixels_
= size_pixels
;
1399 query
.pixel_size
= size_pixels
;
1401 // Non-absolute sizes are in points (again scaled by PANGO_SIZE).
1402 // Round the value when converting to pixels to match GTK's logic.
1403 const double size_points
= pango_font_description_get_size(desc
) /
1404 static_cast<double>(PANGO_SCALE
);
1405 default_font_size_pixels_
= static_cast<int>(
1406 GetPixelsInPoint(device_scale_factor_
) * size_points
+ 0.5);
1407 query
.point_size
= static_cast<int>(size_points
);
1410 query
.style
= gfx::Font::NORMAL
;
1411 // TODO(davemoore): Support weights other than bold?
1412 if (pango_font_description_get_weight(desc
) == PANGO_WEIGHT_BOLD
)
1413 query
.style
|= gfx::Font::BOLD
;
1414 // TODO(davemoore): What about PANGO_STYLE_OBLIQUE?
1415 if (pango_font_description_get_style(desc
) == PANGO_STYLE_ITALIC
)
1416 query
.style
|= gfx::Font::ITALIC
;
1418 default_font_render_params_
=
1419 gfx::GetFontRenderParams(query
, &default_font_family_
);
1420 default_font_style_
= query
.style
;
1423 void Gtk2UI::OnStyleSet(GtkWidget
* widget
, GtkStyle
* previous_style
) {
1424 ClearAllThemeData();
1426 NativeThemeGtk2::instance()->NotifyObservers();
1429 void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor
) {
1430 device_scale_factor_
= device_scale_factor
;
1431 GtkStyle
* label_style
= gtk_rc_get_style(fake_label_
.get());
1432 UpdateDefaultFont(label_style
->font_desc
);
1435 float Gtk2UI::GetDeviceScaleFactor() const {
1436 float scale
= GetFontDPI() / GetBaseDPI();
1437 // Round to 1 decimal, e.g. to 1.4.
1438 return roundf(scale
* 10) / 10;
1441 } // namespace libgtk2ui
1443 views::LinuxUI
* BuildGtk2UI() {
1444 return new libgtk2ui::Gtk2UI
;