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"
9 #include <pango/pango.h>
11 #include "base/command_line.h"
12 #include "base/debug/leak_annotations.h"
13 #include "base/environment.h"
14 #include "base/i18n/rtl.h"
15 #include "base/logging.h"
16 #include "base/nix/mime_util_xdg.h"
17 #include "base/nix/xdg_util.h"
18 #include "base/stl_util.h"
19 #include "base/strings/string_split.h"
20 #include "base/strings/stringprintf.h"
21 #include "chrome/browser/themes/theme_properties.h"
22 #include "chrome/browser/ui/libgtk2ui/app_indicator_icon.h"
23 #include "chrome/browser/ui/libgtk2ui/chrome_gtk_frame.h"
24 #include "chrome/browser/ui/libgtk2ui/gtk2_border.h"
25 #include "chrome/browser/ui/libgtk2ui/gtk2_event_loop.h"
26 #include "chrome/browser/ui/libgtk2ui/gtk2_key_bindings_handler.h"
27 #include "chrome/browser/ui/libgtk2ui/gtk2_signal_registrar.h"
28 #include "chrome/browser/ui/libgtk2ui/gtk2_status_icon.h"
29 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
30 #include "chrome/browser/ui/libgtk2ui/native_theme_gtk2.h"
31 #include "chrome/browser/ui/libgtk2ui/print_dialog_gtk2.h"
32 #include "chrome/browser/ui/libgtk2ui/printing_gtk2_util.h"
33 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h"
34 #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
35 #include "chrome/browser/ui/libgtk2ui/unity_service.h"
36 #include "chrome/browser/ui/libgtk2ui/x11_input_method_context_impl_gtk2.h"
37 #include "grit/components_scaled_resources.h"
38 #include "grit/theme_resources.h"
39 #include "printing/printing_context_linux.h"
40 #include "third_party/skia/include/core/SkBitmap.h"
41 #include "third_party/skia/include/core/SkCanvas.h"
42 #include "third_party/skia/include/core/SkColor.h"
43 #include "third_party/skia/include/core/SkShader.h"
44 #include "ui/base/resource/resource_bundle.h"
45 #include "ui/gfx/canvas.h"
46 #include "ui/gfx/geometry/rect.h"
47 #include "ui/gfx/geometry/size.h"
48 #include "ui/gfx/image/image.h"
49 #include "ui/gfx/skbitmap_operations.h"
50 #include "ui/gfx/skia_util.h"
51 #include "ui/resources/grit/ui_resources.h"
52 #include "ui/views/controls/button/label_button.h"
53 #include "ui/views/controls/button/label_button_border.h"
54 #include "ui/views/linux_ui/window_button_order_observer.h"
56 #if defined(USE_GCONF)
57 #include "chrome/browser/ui/libgtk2ui/gconf_listener.h"
60 // A minimized port of GtkThemeService into something that can provide colors
61 // and images for aura.
63 // TODO(erg): There's still a lot that needs ported or done for the first time:
65 // - Render and inject the omnibox background.
66 // - Make sure to test with a light on dark theme, too.
68 // Work around a header bug:
69 // linux/debian_wheezy_i386-sysroot/usr/include/linux/stddef.h redefines NULL
70 // to 0, which breaks -Wsentinel. Get back the normal definition of NULL.
71 // TODO(thakis): Remove this once we update sysroots.
79 struct GObjectDeleter
{
80 void operator()(void* ptr
) {
84 struct GtkIconInfoDeleter
{
85 void operator()(GtkIconInfo
* ptr
) {
86 gtk_icon_info_free(ptr
);
89 typedef scoped_ptr
<GIcon
, GObjectDeleter
> ScopedGIcon
;
90 typedef scoped_ptr
<GtkIconInfo
, GtkIconInfoDeleter
> ScopedGtkIconInfo
;
91 typedef scoped_ptr
<GdkPixbuf
, GObjectDeleter
> ScopedGdkPixbuf
;
93 // Prefix for app indicator ids
94 const char kAppIndicatorIdPrefix
[] = "chrome_app_indicator_";
96 // Number of app indicators used (used as part of app-indicator id).
99 // The unknown content type.
100 const char* kUnknownContentType
= "application/octet-stream";
102 // The size of the rendered toolbar image.
103 const int kToolbarImageWidth
= 64;
104 const int kToolbarImageHeight
= 128;
106 // How much to tint the GTK+ color lighter at the top of the window.
107 const color_utils::HSL kGtkFrameShift
= { -1, -1, 0.58 };
109 // How much to tint the GTK+ color when an explicit frame color hasn't been
111 const color_utils::HSL kDefaultFrameShift
= { -1, -1, 0.4 };
113 // Values used as the new luminance and saturation values in the inactive tab
115 const double kDarkInactiveLuminance
= 0.85;
116 const double kLightInactiveLuminance
= 0.15;
117 const double kHeavyInactiveSaturation
= 0.7;
118 const double kLightInactiveSaturation
= 0.3;
120 // Default color for links on the NTP when the GTK+ theme doesn't define a
121 // link color. Constant taken from gtklinkbutton.c.
122 const GdkColor kDefaultLinkColor
= { 0, 0, 0, 0xeeee };
124 const int kSkiaToGDKMultiplier
= 257;
126 // TODO(erg): ThemeService has a whole interface just for reading default
127 // constants. Figure out what to do with that more long term; for now, just
128 // copy the constants themselves here.
131 const color_utils::HSL kDefaultTintButtons
= { -1, -1, -1 };
132 const color_utils::HSL kDefaultTintFrame
= { -1, -1, -1 };
133 const color_utils::HSL kDefaultTintFrameInactive
= { -1, -1, 0.75f
};
134 const color_utils::HSL kDefaultTintFrameIncognito
= { -1, 0.2f
, 0.35f
};
135 const color_utils::HSL kDefaultTintFrameIncognitoInactive
= { -1, 0.3f
, 0.6f
};
136 const color_utils::HSL kDefaultTintBackgroundTab
= { -1, 0.5, 0.75 };
138 // A list of images that we provide while in gtk mode.
140 // TODO(erg): We list both the normal and *_DESKTOP versions of some of these
141 // images because in some contexts, we don't go through the
142 // chrome::MapThemeImage interface. That should be fixed, but tracking that
144 const int kThemeImages
[] = {
146 IDR_THEME_TAB_BACKGROUND
,
147 IDR_THEME_TAB_BACKGROUND_DESKTOP
,
148 IDR_THEME_TAB_BACKGROUND_INCOGNITO
,
149 IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
,
153 IDR_THEME_FRAME_INACTIVE
,
154 IDR_THEME_FRAME_INCOGNITO
,
155 IDR_THEME_FRAME_INCOGNITO_INACTIVE
,
158 // A list of icons used in the autocomplete view that should be tinted to the
159 // current gtk theme selection color so they stand out against the GtkEntry's
161 // TODO(erg): Decide what to do about other icons that appear in the omnibox,
162 // e.g. content settings icons.
163 const int kAutocompleteImages
[] = {
164 IDR_OMNIBOX_CALCULATOR
,
165 IDR_OMNIBOX_EXTENSION_APP
,
167 IDR_OMNIBOX_HTTP_DARK
,
169 IDR_OMNIBOX_SEARCH_DARK
,
171 IDR_OMNIBOX_STAR_DARK
,
173 IDR_OMNIBOX_TTS_DARK
,
176 // This table converts button ids into a pair of gtk-stock id and state.
177 struct IDRGtkMapping
{
179 const char* stock_id
;
180 GtkStateType gtk_state
;
181 } const kGtkIcons
[] = {
182 { IDR_BACK
, GTK_STOCK_GO_BACK
, GTK_STATE_NORMAL
},
183 { IDR_BACK_D
, GTK_STOCK_GO_BACK
, GTK_STATE_INSENSITIVE
},
185 { IDR_FORWARD
, GTK_STOCK_GO_FORWARD
, GTK_STATE_NORMAL
},
186 { IDR_FORWARD_D
, GTK_STOCK_GO_FORWARD
, GTK_STATE_INSENSITIVE
},
188 { IDR_HOME
, GTK_STOCK_HOME
, GTK_STATE_NORMAL
},
190 { IDR_RELOAD
, GTK_STOCK_REFRESH
, GTK_STATE_NORMAL
},
191 { IDR_RELOAD_D
, GTK_STOCK_REFRESH
, GTK_STATE_INSENSITIVE
},
193 { IDR_STOP
, GTK_STOCK_STOP
, GTK_STATE_NORMAL
},
194 { IDR_STOP_D
, GTK_STOCK_STOP
, GTK_STATE_INSENSITIVE
},
197 // The image resources that will be tinted by the 'button' tint value.
198 const int kOtherToolbarButtonIDs
[] = {
199 IDR_TOOLBAR_BEZEL_HOVER
,
200 IDR_TOOLBAR_BEZEL_PRESSED
,
201 IDR_BROWSER_ACTIONS_OVERFLOW
,
203 IDR_THROBBER_WAITING
,
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 // Queries GTK for its font DPI setting and returns the number of pixels in a
385 double GetPixelsInPoint(float device_scale_factor
) {
386 GtkSettings
* gtk_settings
= gtk_settings_get_default();
389 g_object_get(gtk_settings
, "gtk-xft-dpi", >k_dpi
, NULL
);
391 // GTK multiplies the DPI by 1024 before storing it.
392 double dpi
= (gtk_dpi
> 0) ? gtk_dpi
/ 1024.0 : 96.0;
394 // Take device_scale_factor into account — if Chrome already scales the
395 // entire UI up by 2x, we should not also scale up.
396 dpi
/= device_scale_factor
;
398 // There are 72 points in an inch.
402 views::LinuxUI::NonClientMiddleClickAction
GetDefaultMiddleClickAction() {
403 scoped_ptr
<base::Environment
> env(base::Environment::Create());
404 switch (base::nix::GetDesktopEnvironment(env
.get())) {
405 case base::nix::DESKTOP_ENVIRONMENT_KDE4
:
406 // Starting with KDE 4.4, windows' titlebars can be dragged with the
407 // middle mouse button to create tab groups. We don't support that in
408 // Chrome, but at least avoid lowering windows in response to middle
409 // clicks to avoid surprising users who expect the KDE behavior.
410 return views::LinuxUI::MIDDLE_CLICK_ACTION_NONE
;
412 return views::LinuxUI::MIDDLE_CLICK_ACTION_LOWER
;
419 : default_font_size_pixels_(0),
420 default_font_style_(gfx::Font::NORMAL
),
421 middle_click_action_(GetDefaultMiddleClickAction()),
422 device_scale_factor_(1.0) {
423 GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess());
426 void Gtk2UI::Initialize() {
427 signals_
.reset(new Gtk2SignalRegistrar
);
429 // Create our fake widgets.
430 fake_window_
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
431 fake_frame_
= chrome_gtk_frame_new();
432 fake_label_
.Own(gtk_label_new(""));
433 fake_entry_
.Own(gtk_entry_new());
435 // Only realized widgets receive style-set notifications, which we need to
436 // broadcast new theme images and colors. Only realized widgets have style
437 // properties, too, which we query for some colors.
438 gtk_widget_realize(fake_frame_
);
439 gtk_widget_realize(fake_window_
);
441 signals_
->Connect(fake_frame_
, "style-set",
442 G_CALLBACK(&OnStyleSetThunk
), this);
446 printing::PrintingContextLinux::SetCreatePrintDialogFunction(
447 &PrintDialogGtk2::CreatePrintDialog
);
448 printing::PrintingContextLinux::SetPdfPaperSizeFunction(
449 &GetPdfPaperSizeDeviceUnitsGtk
);
451 #if defined(USE_GCONF)
452 // We must build this after GTK gets initialized.
453 gconf_listener_
.reset(new GConfListener(this));
454 #endif // defined(USE_GCONF)
456 indicators_count
= 0;
458 // Instantiate the singleton instance of Gtk2EventLoop.
459 Gtk2EventLoop::GetInstance();
463 gtk_widget_destroy(fake_window_
);
464 gtk_widget_destroy(fake_frame_
);
465 fake_label_
.Destroy();
466 fake_entry_
.Destroy();
471 gfx::Image
Gtk2UI::GetThemeImageNamed(int id
) const {
472 // Try to get our cached version:
473 ImageCache::const_iterator it
= gtk_images_
.find(id
);
474 if (it
!= gtk_images_
.end())
477 if (IsOverridableImage(id
)) {
478 gfx::Image image
= gfx::Image(
479 gfx::ImageSkia::CreateFrom1xBitmap(GenerateGtkThemeBitmap(id
)));
480 gtk_images_
[id
] = image
;
487 bool Gtk2UI::GetColor(int id
, SkColor
* color
) const {
488 ColorMap::const_iterator it
= colors_
.find(id
);
489 if (it
!= colors_
.end()) {
497 bool Gtk2UI::HasCustomImage(int id
) const {
498 return IsOverridableImage(id
);
501 SkColor
Gtk2UI::GetFocusRingColor() const {
502 return focus_ring_color_
;
505 SkColor
Gtk2UI::GetThumbActiveColor() const {
506 return thumb_active_color_
;
509 SkColor
Gtk2UI::GetThumbInactiveColor() const {
510 return thumb_inactive_color_
;
513 SkColor
Gtk2UI::GetTrackColor() const {
517 SkColor
Gtk2UI::GetActiveSelectionBgColor() const {
518 return active_selection_bg_color_
;
521 SkColor
Gtk2UI::GetActiveSelectionFgColor() const {
522 return active_selection_fg_color_
;
525 SkColor
Gtk2UI::GetInactiveSelectionBgColor() const {
526 return inactive_selection_bg_color_
;
529 SkColor
Gtk2UI::GetInactiveSelectionFgColor() const {
530 return inactive_selection_fg_color_
;
533 double Gtk2UI::GetCursorBlinkInterval() const {
534 // From http://library.gnome.org/devel/gtk/unstable/GtkSettings.html, this is
535 // the default value for gtk-cursor-blink-time.
536 static const gint kGtkDefaultCursorBlinkTime
= 1200;
538 // Dividing GTK's cursor blink cycle time (in milliseconds) by this value
539 // yields an appropriate value for
540 // content::RendererPreferences::caret_blink_interval. This matches the
541 // logic in the WebKit GTK port.
542 static const double kGtkCursorBlinkCycleFactor
= 2000.0;
544 gint cursor_blink_time
= kGtkDefaultCursorBlinkTime
;
545 gboolean cursor_blink
= TRUE
;
546 g_object_get(gtk_settings_get_default(),
547 "gtk-cursor-blink-time", &cursor_blink_time
,
548 "gtk-cursor-blink", &cursor_blink
,
550 return cursor_blink
? (cursor_blink_time
/ kGtkCursorBlinkCycleFactor
) : 0.0;
553 ui::NativeTheme
* Gtk2UI::GetNativeTheme(aura::Window
* window
) const {
554 ui::NativeTheme
* native_theme_override
= NULL
;
555 if (!native_theme_overrider_
.is_null())
556 native_theme_override
= native_theme_overrider_
.Run(window
);
558 if (native_theme_override
)
559 return native_theme_override
;
561 return NativeThemeGtk2::instance();
564 void Gtk2UI::SetNativeThemeOverride(const NativeThemeGetter
& callback
) {
565 native_theme_overrider_
= callback
;
568 bool Gtk2UI::GetDefaultUsesSystemTheme() const {
569 scoped_ptr
<base::Environment
> env(base::Environment::Create());
571 switch (base::nix::GetDesktopEnvironment(env
.get())) {
572 case base::nix::DESKTOP_ENVIRONMENT_GNOME
:
573 case base::nix::DESKTOP_ENVIRONMENT_UNITY
:
574 case base::nix::DESKTOP_ENVIRONMENT_XFCE
:
576 case base::nix::DESKTOP_ENVIRONMENT_KDE3
:
577 case base::nix::DESKTOP_ENVIRONMENT_KDE4
:
578 case base::nix::DESKTOP_ENVIRONMENT_OTHER
:
581 // Unless GetDesktopEnvironment() badly misbehaves, this should never happen.
586 void Gtk2UI::SetDownloadCount(int count
) const {
587 if (unity::IsRunning())
588 unity::SetDownloadCount(count
);
591 void Gtk2UI::SetProgressFraction(float percentage
) const {
592 if (unity::IsRunning())
593 unity::SetProgressFraction(percentage
);
596 bool Gtk2UI::IsStatusIconSupported() const {
600 scoped_ptr
<views::StatusIconLinux
> Gtk2UI::CreateLinuxStatusIcon(
601 const gfx::ImageSkia
& image
,
602 const base::string16
& tool_tip
) const {
603 if (AppIndicatorIcon::CouldOpen()) {
605 return scoped_ptr
<views::StatusIconLinux
>(new AppIndicatorIcon(
606 base::StringPrintf("%s%d", kAppIndicatorIdPrefix
, indicators_count
),
610 return scoped_ptr
<views::StatusIconLinux
>(new Gtk2StatusIcon(
615 gfx::Image
Gtk2UI::GetIconForContentType(
616 const std::string
& content_type
,
618 // This call doesn't take a reference.
619 GtkIconTheme
* theme
= gtk_icon_theme_get_default();
621 std::string content_types
[] = {
622 content_type
, kUnknownContentType
625 for (size_t i
= 0; i
< arraysize(content_types
); ++i
) {
626 ScopedGIcon
icon(g_content_type_get_icon(content_types
[i
].c_str()));
627 ScopedGtkIconInfo
icon_info(
628 gtk_icon_theme_lookup_by_gicon(
629 theme
, icon
.get(), size
,
630 static_cast<GtkIconLookupFlags
>(GTK_ICON_LOOKUP_FORCE_SIZE
)));
633 ScopedGdkPixbuf
pixbuf(gtk_icon_info_load_icon(icon_info
.get(), NULL
));
637 SkBitmap bitmap
= GdkPixbufToImageSkia(pixbuf
.get());
638 DCHECK_EQ(size
, bitmap
.width());
639 DCHECK_EQ(size
, bitmap
.height());
640 gfx::ImageSkia image_skia
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
641 image_skia
.MakeThreadSafe();
642 return gfx::Image(image_skia
);
647 scoped_ptr
<views::Border
> Gtk2UI::CreateNativeBorder(
648 views::LabelButton
* owning_button
,
649 scoped_ptr
<views::LabelButtonBorder
> border
) {
650 if (owning_button
->GetNativeTheme() != NativeThemeGtk2::instance())
651 return border
.Pass();
653 return make_scoped_ptr(new Gtk2Border(this, owning_button
, border
.Pass()));
656 void Gtk2UI::AddWindowButtonOrderObserver(
657 views::WindowButtonOrderObserver
* observer
) {
658 if (!leading_buttons_
.empty() || !trailing_buttons_
.empty()) {
659 observer
->OnWindowButtonOrderingChange(leading_buttons_
,
663 observer_list_
.AddObserver(observer
);
666 void Gtk2UI::RemoveWindowButtonOrderObserver(
667 views::WindowButtonOrderObserver
* observer
) {
668 observer_list_
.RemoveObserver(observer
);
671 void Gtk2UI::SetWindowButtonOrdering(
672 const std::vector
<views::FrameButton
>& leading_buttons
,
673 const std::vector
<views::FrameButton
>& trailing_buttons
) {
674 leading_buttons_
= leading_buttons
;
675 trailing_buttons_
= trailing_buttons
;
677 FOR_EACH_OBSERVER(views::WindowButtonOrderObserver
, observer_list_
,
678 OnWindowButtonOrderingChange(leading_buttons_
,
682 void Gtk2UI::SetNonClientMiddleClickAction(NonClientMiddleClickAction action
) {
683 middle_click_action_
= action
;
686 scoped_ptr
<ui::LinuxInputMethodContext
> Gtk2UI::CreateInputMethodContext(
687 ui::LinuxInputMethodContextDelegate
* delegate
) const {
688 return scoped_ptr
<ui::LinuxInputMethodContext
>(
689 new X11InputMethodContextImplGtk2(delegate
));
692 gfx::FontRenderParams
Gtk2UI::GetDefaultFontRenderParams() const {
693 static gfx::FontRenderParams params
= GetGtkFontRenderParams();
697 void Gtk2UI::GetDefaultFontDescription(
698 std::string
* family_out
,
699 int* size_pixels_out
,
701 gfx::FontRenderParams
* params_out
) const {
702 *family_out
= default_font_family_
;
703 *size_pixels_out
= default_font_size_pixels_
;
704 *style_out
= default_font_style_
;
705 *params_out
= default_font_render_params_
;
708 ui::SelectFileDialog
* Gtk2UI::CreateSelectFileDialog(
709 ui::SelectFileDialog::Listener
* listener
,
710 ui::SelectFilePolicy
* policy
) const {
711 return SelectFileDialogImpl::Create(listener
, policy
);
714 bool Gtk2UI::UnityIsRunning() {
715 return unity::IsRunning();
718 views::LinuxUI::NonClientMiddleClickAction
719 Gtk2UI::GetNonClientMiddleClickAction() {
720 return middle_click_action_
;
723 void Gtk2UI::NotifyWindowManagerStartupComplete() {
724 // TODO(port) Implement this using _NET_STARTUP_INFO_BEGIN/_NET_STARTUP_INFO
725 // from http://standards.freedesktop.org/startup-notification-spec/ instead.
726 gdk_notify_startup_complete();
729 bool Gtk2UI::MatchEvent(const ui::Event
& event
,
730 std::vector
<ui::TextEditCommandAuraLinux
>* commands
) {
731 // Ensure that we have a keyboard handler.
732 if (!key_bindings_handler_
)
733 key_bindings_handler_
.reset(new Gtk2KeyBindingsHandler
);
735 return key_bindings_handler_
->MatchEvent(event
, commands
);
738 void Gtk2UI::GetScrollbarColors(GdkColor
* thumb_active_color
,
739 GdkColor
* thumb_inactive_color
,
740 GdkColor
* track_color
) {
741 GdkColor
* theme_thumb_active
= NULL
;
742 GdkColor
* theme_thumb_inactive
= NULL
;
743 GdkColor
* theme_trough_color
= NULL
;
744 gtk_widget_style_get(GTK_WIDGET(fake_frame_
),
745 "scrollbar-slider-prelight-color", &theme_thumb_active
,
746 "scrollbar-slider-normal-color", &theme_thumb_inactive
,
747 "scrollbar-trough-color", &theme_trough_color
,
750 // Ask the theme if the theme specifies all the scrollbar colors and short
751 // circuit the expensive painting/compositing if we have all of them.
752 if (theme_thumb_active
&& theme_thumb_inactive
&& theme_trough_color
) {
753 *thumb_active_color
= *theme_thumb_active
;
754 *thumb_inactive_color
= *theme_thumb_inactive
;
755 *track_color
= *theme_trough_color
;
757 gdk_color_free(theme_thumb_active
);
758 gdk_color_free(theme_thumb_inactive
);
759 gdk_color_free(theme_trough_color
);
763 // Create window containing scrollbar elements
764 GtkWidget
* window
= gtk_window_new(GTK_WINDOW_POPUP
);
765 GtkWidget
* fixed
= gtk_fixed_new();
766 GtkWidget
* scrollbar
= gtk_hscrollbar_new(NULL
);
767 gtk_container_add(GTK_CONTAINER(window
), fixed
);
768 gtk_container_add(GTK_CONTAINER(fixed
), scrollbar
);
769 gtk_widget_realize(window
);
770 gtk_widget_realize(scrollbar
);
772 // Draw scrollbar thumb part and track into offscreen image
773 const int kWidth
= 100;
774 const int kHeight
= 20;
775 GtkStyle
* style
= gtk_rc_get_style(scrollbar
);
776 GdkWindow
* gdk_window
= gtk_widget_get_window(window
);
777 GdkPixmap
* pm
= gdk_pixmap_new(gdk_window
, kWidth
, kHeight
, -1);
778 GdkRectangle rect
= { 0, 0, kWidth
, kHeight
};
779 unsigned char data
[3 * kWidth
* kHeight
];
780 for (int i
= 0; i
< 3; ++i
) {
783 gtk_paint_slider(style
, pm
,
784 i
== 0 ? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
785 GTK_SHADOW_OUT
, &rect
, scrollbar
, "slider", 0, 0,
786 kWidth
, kHeight
, GTK_ORIENTATION_HORIZONTAL
);
789 gtk_paint_box(style
, pm
, GTK_STATE_ACTIVE
, GTK_SHADOW_IN
, &rect
,
790 scrollbar
, "trough-upper", 0, 0, kWidth
, kHeight
);
792 GdkPixbuf
* pb
= gdk_pixbuf_new_from_data(data
, GDK_COLORSPACE_RGB
,
793 FALSE
, 8, kWidth
, kHeight
,
795 gdk_pixbuf_get_from_drawable(pb
, pm
, NULL
, 0, 0, 0, 0, kWidth
, kHeight
);
798 int components
[3] = { 0 };
799 for (int y
= 2; y
< kHeight
- 2; ++y
) {
800 for (int c
= 0; c
< 3; ++c
) {
801 // Sample a vertical slice of pixels at about one-thirds from the
802 // left edge. This allows us to avoid any fixed graphics that might be
803 // located at the edges or in the center of the scrollbar.
804 // Each pixel is made up of a red, green, and blue component; taking up
805 // a total of three bytes.
806 components
[c
] += data
[3 * (kWidth
/ 3 + y
* kWidth
) + c
];
809 GdkColor
* color
= i
== 0 ? thumb_active_color
:
810 i
== 1 ? thumb_inactive_color
:
813 // We sampled pixels across the full height of the image, ignoring a two
814 // pixel border. In some themes, the border has a completely different
815 // color which we do not want to factor into our average color computation.
817 // We now need to scale the colors from the 0..255 range, to the wider
818 // 0..65535 range, and we need to actually compute the average color; so,
819 // we divide by the total number of pixels in the sample.
820 color
->red
= components
[0] * 65535 / (255 * (kHeight
- 4));
821 color
->green
= components
[1] * 65535 / (255 * (kHeight
- 4));
822 color
->blue
= components
[2] * 65535 / (255 * (kHeight
- 4));
828 gtk_widget_destroy(window
);
830 // Override any of the default colors with ones that were specified by the
832 if (theme_thumb_active
) {
833 *thumb_active_color
= *theme_thumb_active
;
834 gdk_color_free(theme_thumb_active
);
837 if (theme_thumb_inactive
) {
838 *thumb_inactive_color
= *theme_thumb_inactive
;
839 gdk_color_free(theme_thumb_inactive
);
842 if (theme_trough_color
) {
843 *track_color
= *theme_trough_color
;
844 gdk_color_free(theme_trough_color
);
848 void Gtk2UI::LoadGtkValues() {
849 // TODO(erg): GtkThemeService had a comment here about having to muck with
850 // the raw Prefs object to remove prefs::kCurrentThemeImages or else we'd
851 // regress startup time. Figure out how to do that when we can't access the
852 // prefs system from here.
854 GtkStyle
* frame_style
= gtk_rc_get_style(fake_frame_
);
856 GtkStyle
* window_style
= gtk_rc_get_style(fake_window_
);
857 SetThemeColorFromGtk(ThemeProperties::COLOR_CONTROL_BACKGROUND
,
858 &window_style
->bg
[GTK_STATE_NORMAL
]);
860 GdkColor toolbar_color
= window_style
->bg
[GTK_STATE_NORMAL
];
861 SetThemeColorFromGtk(ThemeProperties::COLOR_TOOLBAR
, &toolbar_color
);
863 GdkColor button_color
= window_style
->bg
[GTK_STATE_SELECTED
];
864 SetThemeTintFromGtk(ThemeProperties::TINT_BUTTONS
, &button_color
);
866 GtkStyle
* label_style
= gtk_rc_get_style(fake_label_
.get());
867 GdkColor label_color
= label_style
->fg
[GTK_STATE_NORMAL
];
868 SetThemeColorFromGtk(ThemeProperties::COLOR_TAB_TEXT
, &label_color
);
869 SetThemeColorFromGtk(ThemeProperties::COLOR_BOOKMARK_TEXT
, &label_color
);
870 SetThemeColorFromGtk(ThemeProperties::COLOR_STATUS_BAR_TEXT
, &label_color
);
872 UpdateDefaultFont(label_style
->font_desc
);
874 // Build the various icon tints.
875 GetNormalButtonTintHSL(&button_tint_
);
876 GetNormalEntryForegroundHSL(&entry_tint_
);
877 GetSelectedEntryForegroundHSL(&selected_entry_tint_
);
878 GdkColor frame_color
= BuildFrameColors(frame_style
);
880 // The inactive frame color never occurs naturally in the theme, as it is a
881 // tinted version of |frame_color|. We generate another color based on the
882 // background tab color, with the lightness and saturation moved in the
883 // opposite direction. (We don't touch the hue, since there should be subtle
884 // hints of the color in the text.)
885 color_utils::HSL inactive_tab_text_hsl
=
886 tints_
[ThemeProperties::TINT_BACKGROUND_TAB
];
887 if (inactive_tab_text_hsl
.l
< 0.5)
888 inactive_tab_text_hsl
.l
= kDarkInactiveLuminance
;
890 inactive_tab_text_hsl
.l
= kLightInactiveLuminance
;
892 if (inactive_tab_text_hsl
.s
< 0.5)
893 inactive_tab_text_hsl
.s
= kHeavyInactiveSaturation
;
895 inactive_tab_text_hsl
.s
= kLightInactiveSaturation
;
897 colors_
[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT
] =
898 color_utils::HSLToSkColor(inactive_tab_text_hsl
, 255);
900 // We pick the text and background colors for the NTP out of the colors for a
901 // GtkEntry. We do this because GtkEntries background color is never the same
902 // as |toolbar_color|, is usually a white, and when it isn't a white,
903 // provides sufficient contrast to |toolbar_color|. Try this out with
904 // Darklooks, HighContrastInverse or ThinIce.
905 GtkStyle
* entry_style
= gtk_rc_get_style(fake_entry_
.get());
906 GdkColor ntp_background
= entry_style
->base
[GTK_STATE_NORMAL
];
907 GdkColor ntp_foreground
= entry_style
->text
[GTK_STATE_NORMAL
];
908 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_BACKGROUND
,
910 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_TEXT
,
913 // The NTP header is the color that surrounds the current active thumbnail on
914 // the NTP, and acts as the border of the "Recent Links" box. It would be
915 // awesome if they were separated so we could use GetBorderColor() for the
916 // border around the "Recent Links" section, but matching the frame color is
918 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_HEADER
,
920 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION
,
922 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_TEXT
,
925 // Override the link color if the theme provides it.
926 const GdkColor
* link_color
= NULL
;
927 gtk_widget_style_get(GTK_WIDGET(fake_window_
),
928 "link-color", &link_color
, NULL
);
930 bool is_default_link_color
= false;
932 link_color
= &kDefaultLinkColor
;
933 is_default_link_color
= true;
936 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK
,
938 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK_UNDERLINE
,
940 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK
,
942 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
,
945 if (!is_default_link_color
)
946 gdk_color_free(const_cast<GdkColor
*>(link_color
));
948 // Generate the colors that we pass to WebKit.
949 focus_ring_color_
= GdkColorToSkColor(frame_color
);
951 GdkColor thumb_active_color
, thumb_inactive_color
, track_color
;
952 Gtk2UI::GetScrollbarColors(&thumb_active_color
,
953 &thumb_inactive_color
,
955 thumb_active_color_
= GdkColorToSkColor(thumb_active_color
);
956 thumb_inactive_color_
= GdkColorToSkColor(thumb_inactive_color
);
957 track_color_
= GdkColorToSkColor(track_color
);
959 // Some GTK themes only define the text selection colors on the GtkEntry
960 // class, so we need to use that for getting selection colors.
961 active_selection_bg_color_
=
962 GdkColorToSkColor(entry_style
->base
[GTK_STATE_SELECTED
]);
963 active_selection_fg_color_
=
964 GdkColorToSkColor(entry_style
->text
[GTK_STATE_SELECTED
]);
965 inactive_selection_bg_color_
=
966 GdkColorToSkColor(entry_style
->base
[GTK_STATE_ACTIVE
]);
967 inactive_selection_fg_color_
=
968 GdkColorToSkColor(entry_style
->text
[GTK_STATE_ACTIVE
]);
971 GdkColor
Gtk2UI::BuildFrameColors(GtkStyle
* frame_style
) {
972 GdkColor
* theme_frame
= NULL
;
973 GdkColor
* theme_inactive_frame
= NULL
;
974 GdkColor
* theme_incognito_frame
= NULL
;
975 GdkColor
* theme_incognito_inactive_frame
= NULL
;
976 gtk_widget_style_get(GTK_WIDGET(fake_frame_
),
977 "frame-color", &theme_frame
,
978 "inactive-frame-color", &theme_inactive_frame
,
979 "incognito-frame-color", &theme_incognito_frame
,
980 "incognito-inactive-frame-color",
981 &theme_incognito_inactive_frame
,
984 GdkColor frame_color
= BuildAndSetFrameColor(
985 &frame_style
->bg
[GTK_STATE_SELECTED
],
988 ThemeProperties::COLOR_FRAME
,
989 ThemeProperties::TINT_FRAME
);
991 gdk_color_free(theme_frame
);
992 SetThemeTintFromGtk(ThemeProperties::TINT_BACKGROUND_TAB
, &frame_color
);
994 BuildAndSetFrameColor(
995 &frame_style
->bg
[GTK_STATE_INSENSITIVE
],
996 theme_inactive_frame
,
998 ThemeProperties::COLOR_FRAME_INACTIVE
,
999 ThemeProperties::TINT_FRAME_INACTIVE
);
1000 if (theme_inactive_frame
)
1001 gdk_color_free(theme_inactive_frame
);
1003 BuildAndSetFrameColor(
1005 theme_incognito_frame
,
1006 GetDefaultTint(ThemeProperties::TINT_FRAME_INCOGNITO
),
1007 ThemeProperties::COLOR_FRAME_INCOGNITO
,
1008 ThemeProperties::TINT_FRAME_INCOGNITO
);
1009 if (theme_incognito_frame
)
1010 gdk_color_free(theme_incognito_frame
);
1012 BuildAndSetFrameColor(
1014 theme_incognito_inactive_frame
,
1015 GetDefaultTint(ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
),
1016 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
,
1017 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
);
1018 if (theme_incognito_inactive_frame
)
1019 gdk_color_free(theme_incognito_inactive_frame
);
1024 void Gtk2UI::SetThemeColorFromGtk(int id
, const GdkColor
* color
) {
1025 colors_
[id
] = GdkColorToSkColor(*color
);
1028 void Gtk2UI::SetThemeTintFromGtk(int id
, const GdkColor
* color
) {
1029 color_utils::HSL default_tint
= GetDefaultTint(id
);
1030 color_utils::HSL hsl
;
1031 color_utils::SkColorToHSL(GdkColorToSkColor(*color
), &hsl
);
1033 if (default_tint
.s
!= -1)
1034 hsl
.s
= default_tint
.s
;
1036 if (default_tint
.l
!= -1)
1037 hsl
.l
= default_tint
.l
;
1042 GdkColor
Gtk2UI::BuildAndSetFrameColor(const GdkColor
* base
,
1043 const GdkColor
* gtk_base
,
1044 const color_utils::HSL
& tint
,
1047 GdkColor out_color
= *base
;
1049 // The theme author specified a color to use, use it without modification.
1050 out_color
= *gtk_base
;
1052 // Tint the basic color since this is a heuristic color instead of one
1053 // specified by the theme author.
1054 GdkColorHSLShift(tint
, &out_color
);
1056 SetThemeColorFromGtk(color_id
, &out_color
);
1057 SetThemeTintFromGtk(tint_id
, &out_color
);
1062 SkBitmap
Gtk2UI::GenerateGtkThemeBitmap(int id
) const {
1064 case IDR_THEME_TOOLBAR
: {
1065 GtkStyle
* style
= gtk_rc_get_style(fake_window_
);
1066 GdkColor
* color
= &style
->bg
[GTK_STATE_NORMAL
];
1068 bitmap
.allocN32Pixels(kToolbarImageWidth
, kToolbarImageHeight
);
1069 bitmap
.eraseARGB(0xff, color
->red
>> 8, color
->green
>> 8,
1073 case IDR_THEME_TAB_BACKGROUND
:
1074 case IDR_THEME_TAB_BACKGROUND_DESKTOP
:
1075 return GenerateTabImage(IDR_THEME_FRAME
);
1076 case IDR_THEME_TAB_BACKGROUND_INCOGNITO
:
1077 case IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
:
1078 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO
);
1080 case IDR_THEME_FRAME
:
1081 return GenerateFrameImage(ThemeProperties::COLOR_FRAME
,
1082 "frame-gradient-color");
1083 case IDR_FRAME_INACTIVE
:
1084 case IDR_THEME_FRAME_INACTIVE
:
1085 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INACTIVE
,
1086 "inactive-frame-gradient-color");
1087 case IDR_THEME_FRAME_INCOGNITO
:
1088 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INCOGNITO
,
1089 "incognito-frame-gradient-color");
1090 case IDR_THEME_FRAME_INCOGNITO_INACTIVE
: {
1091 return GenerateFrameImage(
1092 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
,
1093 "incognito-inactive-frame-gradient-color");
1095 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and
1096 // instead should tint based on the foreground text entry color in GTK+
1097 // mode because some themes that try to be dark *and* light have very
1098 // different colors between the omnibox and the normal background area.
1099 // TODO(erg): Decide what to do about other icons that appear in the
1100 // omnibox, e.g. content settings icons.
1101 case IDR_OMNIBOX_CALCULATOR
:
1102 case IDR_OMNIBOX_EXTENSION_APP
:
1103 case IDR_OMNIBOX_HTTP
:
1104 case IDR_OMNIBOX_SEARCH
:
1105 case IDR_OMNIBOX_STAR
:
1106 case IDR_OMNIBOX_TTS
: {
1107 return GenerateTintedIcon(id
, entry_tint_
);
1109 // In GTK mode, the dark versions of the omnibox icons only ever appear in
1110 // the autocomplete popup and only against the current theme's GtkEntry
1111 // base[GTK_STATE_SELECTED] color, so tint the icons so they won't collide
1112 // with the selected color.
1113 case IDR_OMNIBOX_EXTENSION_APP_DARK
:
1114 case IDR_OMNIBOX_HTTP_DARK
:
1115 case IDR_OMNIBOX_SEARCH_DARK
:
1116 case IDR_OMNIBOX_STAR_DARK
:
1117 case IDR_OMNIBOX_TTS_DARK
: {
1118 return GenerateTintedIcon(id
, selected_entry_tint_
);
1120 // In GTK mode, we need to manually render several icons.
1130 return GenerateGTKIcon(id
);
1132 case IDR_TOOLBAR_BEZEL_HOVER
:
1133 return GenerateToolbarBezel(GTK_STATE_PRELIGHT
, IDR_TOOLBAR_BEZEL_HOVER
);
1134 case IDR_TOOLBAR_BEZEL_PRESSED
:
1135 return GenerateToolbarBezel(GTK_STATE_ACTIVE
, IDR_TOOLBAR_BEZEL_PRESSED
);
1137 return GenerateTintedIcon(id
, button_tint_
);
1144 SkBitmap
Gtk2UI::GenerateFrameImage(
1146 const char* gradient_name
) const {
1147 // We use two colors: the main color (passed in) and a lightened version of
1148 // that color (which is supposed to match the light gradient at the top of
1149 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird).
1150 ColorMap::const_iterator it
= colors_
.find(color_id
);
1151 DCHECK(it
!= colors_
.end());
1152 SkColor base
= it
->second
;
1154 gfx::Canvas
canvas(gfx::Size(kToolbarImageWidth
, kToolbarImageHeight
),
1158 GdkColor
* gradient_top_color
= NULL
;
1159 gtk_widget_style_get(GTK_WIDGET(fake_frame_
),
1160 "frame-gradient-size", &gradient_size
,
1161 gradient_name
, &gradient_top_color
,
1163 if (gradient_size
) {
1164 SkColor lighter
= gradient_top_color
?
1165 GdkColorToSkColor(*gradient_top_color
) :
1166 color_utils::HSLShift(base
, kGtkFrameShift
);
1167 skia::RefPtr
<SkShader
> shader
= gfx::CreateGradientShader(
1168 0, gradient_size
, lighter
, base
);
1170 paint
.setStyle(SkPaint::kFill_Style
);
1171 paint
.setAntiAlias(true);
1172 paint
.setShader(shader
.get());
1174 canvas
.DrawRect(gfx::Rect(0, 0, kToolbarImageWidth
, gradient_size
), paint
);
1177 if (gradient_top_color
)
1178 gdk_color_free(gradient_top_color
);
1180 canvas
.FillRect(gfx::Rect(0, gradient_size
, kToolbarImageWidth
,
1181 kToolbarImageHeight
- gradient_size
), base
);
1182 return canvas
.ExtractImageRep().sk_bitmap();
1185 SkBitmap
Gtk2UI::GenerateTabImage(int base_id
) const {
1186 const SkBitmap
* base_image
= GetThemeImageNamed(base_id
).ToSkBitmap();
1187 SkBitmap bg_tint
= SkBitmapOperations::CreateHSLShiftedBitmap(
1188 *base_image
, GetDefaultTint(ThemeProperties::TINT_BACKGROUND_TAB
));
1189 return SkBitmapOperations::CreateTiledBitmap(
1190 bg_tint
, 0, 0, bg_tint
.width(), bg_tint
.height());
1193 SkBitmap
Gtk2UI::GenerateTintedIcon(
1195 const color_utils::HSL
& tint
) const {
1196 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
1197 return SkBitmapOperations::CreateHSLShiftedBitmap(
1198 rb
.GetImageNamed(base_id
).AsBitmap(), tint
);
1201 SkBitmap
Gtk2UI::GenerateGTKIcon(int base_id
) const {
1202 const char* stock_id
= NULL
;
1203 GtkStateType gtk_state
= GTK_STATE_NORMAL
;
1204 for (unsigned int i
= 0; i
< arraysize(kGtkIcons
); ++i
) {
1205 if (kGtkIcons
[i
].idr
== base_id
) {
1206 stock_id
= kGtkIcons
[i
].stock_id
;
1207 gtk_state
= kGtkIcons
[i
].gtk_state
;
1213 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
1214 SkBitmap default_bitmap
= rb
.GetImageNamed(base_id
).AsBitmap();
1216 gtk_widget_ensure_style(fake_frame_
);
1217 GtkStyle
* style
= gtk_widget_get_style(fake_frame_
);
1218 GtkIconSet
* icon_set
= gtk_style_lookup_icon_set(style
, stock_id
);
1220 return default_bitmap
;
1222 // Ask GTK to render the icon to a buffer, which we will steal from.
1223 GdkPixbuf
* gdk_icon
= gtk_icon_set_render_icon(
1226 base::i18n::IsRTL() ? GTK_TEXT_DIR_RTL
: GTK_TEXT_DIR_LTR
,
1228 GTK_ICON_SIZE_SMALL_TOOLBAR
,
1233 // This can theoretically happen if an icon theme doesn't provide a
1234 // specific image. This should realistically never happen, but I bet there
1235 // are some theme authors who don't reliably provide all icons.
1236 return default_bitmap
;
1240 retval
.allocN32Pixels(default_bitmap
.width(), default_bitmap
.height());
1241 retval
.eraseColor(0);
1243 const SkBitmap icon
= GdkPixbufToImageSkia(gdk_icon
);
1244 g_object_unref(gdk_icon
);
1246 SkCanvas
canvas(retval
);
1248 if (gtk_state
== GTK_STATE_ACTIVE
|| gtk_state
== GTK_STATE_PRELIGHT
) {
1249 SkBitmap border
= DrawGtkButtonBorder(gtk_state
,
1252 default_bitmap
.width(),
1253 default_bitmap
.height());
1254 canvas
.drawBitmap(border
, 0, 0);
1257 canvas
.drawBitmap(icon
,
1258 (default_bitmap
.width() / 2) - (icon
.width() / 2),
1259 (default_bitmap
.height() / 2) - (icon
.height() / 2));
1264 SkBitmap
Gtk2UI::GenerateToolbarBezel(int gtk_state
, int sizing_idr
) const {
1265 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
1266 SkBitmap default_bitmap
=
1267 rb
.GetImageNamed(sizing_idr
).AsBitmap();
1270 retval
.allocN32Pixels(default_bitmap
.width(), default_bitmap
.height());
1271 retval
.eraseColor(0);
1273 SkCanvas
canvas(retval
);
1274 SkBitmap border
= DrawGtkButtonBorder(
1278 default_bitmap
.width(),
1279 default_bitmap
.height());
1280 canvas
.drawBitmap(border
, 0, 0);
1285 void Gtk2UI::GetNormalButtonTintHSL(color_utils::HSL
* tint
) const {
1286 GtkStyle
* window_style
= gtk_rc_get_style(fake_window_
);
1287 const GdkColor accent_gdk_color
= window_style
->bg
[GTK_STATE_SELECTED
];
1288 const GdkColor base_color
= window_style
->base
[GTK_STATE_NORMAL
];
1290 GtkStyle
* label_style
= gtk_rc_get_style(fake_label_
.get());
1291 const GdkColor text_color
= label_style
->fg
[GTK_STATE_NORMAL
];
1293 PickButtonTintFromColors(accent_gdk_color
, text_color
, base_color
, tint
);
1296 void Gtk2UI::GetNormalEntryForegroundHSL(color_utils::HSL
* tint
) const {
1297 GtkStyle
* window_style
= gtk_rc_get_style(fake_window_
);
1298 const GdkColor accent_gdk_color
= window_style
->bg
[GTK_STATE_SELECTED
];
1300 GtkStyle
* style
= gtk_rc_get_style(fake_entry_
.get());
1301 const GdkColor text_color
= style
->text
[GTK_STATE_NORMAL
];
1302 const GdkColor base_color
= style
->base
[GTK_STATE_NORMAL
];
1304 PickButtonTintFromColors(accent_gdk_color
, text_color
, base_color
, tint
);
1307 void Gtk2UI::GetSelectedEntryForegroundHSL(color_utils::HSL
* tint
) const {
1308 // The simplest of all the tints. We just use the selected text in the entry
1309 // since the icons tinted this way will only be displayed against
1310 // base[GTK_STATE_SELECTED].
1311 GtkStyle
* style
= gtk_rc_get_style(fake_entry_
.get());
1312 const GdkColor color
= style
->text
[GTK_STATE_SELECTED
];
1313 color_utils::SkColorToHSL(GdkColorToSkColor(color
), tint
);
1316 SkBitmap
Gtk2UI::DrawGtkButtonBorder(int gtk_state
,
1318 bool call_to_action
,
1321 // Create a temporary GTK button to snapshot
1322 GtkWidget
* window
= gtk_offscreen_window_new();
1323 GtkWidget
* button
= gtk_button_new();
1324 gtk_widget_set_size_request(button
, width
, height
);
1325 gtk_container_add(GTK_CONTAINER(window
), button
);
1326 gtk_widget_realize(window
);
1327 gtk_widget_realize(button
);
1328 gtk_widget_show(button
);
1329 gtk_widget_show(window
);
1332 GTK_WIDGET_SET_FLAGS(button
, GTK_HAS_DEFAULT
);
1335 // We can't just use gtk_widget_grab_focus() here because that sets
1336 // gtk_widget_is_focus(), but not gtk_widget_has_focus(), which is what the
1337 // GtkButton's paint checks.
1338 GTK_WIDGET_SET_FLAGS(button
, GTK_HAS_FOCUS
);
1341 gtk_widget_set_state(button
, static_cast<GtkStateType
>(gtk_state
));
1345 // http://crbug.com/346740
1346 ANNOTATE_SCOPED_MEMORY_LEAK
;
1347 pixmap
= gtk_widget_get_snapshot(button
, NULL
);
1350 gdk_drawable_get_size(GDK_DRAWABLE(pixmap
), &w
, &h
);
1351 DCHECK_EQ(w
, width
);
1352 DCHECK_EQ(h
, height
);
1354 // We render the Pixmap to a Pixbuf. This can be slow, as we're scrapping
1356 GdkColormap
* colormap
= gdk_drawable_get_colormap(pixmap
);
1357 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(NULL
,
1358 GDK_DRAWABLE(pixmap
),
1362 // Finally, we convert our pixbuf into a type we can use.
1363 SkBitmap border
= GdkPixbufToImageSkia(pixbuf
);
1364 g_object_unref(pixbuf
);
1365 g_object_unref(pixmap
);
1366 gtk_widget_destroy(window
);
1371 void Gtk2UI::ClearAllThemeData() {
1372 gtk_images_
.clear();
1375 void Gtk2UI::UpdateDefaultFont(const PangoFontDescription
* desc
) {
1376 // Use gfx::FontRenderParams to select a family and determine the rendering
1378 gfx::FontRenderParamsQuery query
;
1379 base::SplitString(pango_font_description_get_family(desc
), ',',
1382 if (pango_font_description_get_size_is_absolute(desc
)) {
1383 // If the size is absolute, it's specified in Pango units. There are
1384 // PANGO_SCALE Pango units in a device unit (pixel).
1385 const int size_pixels
= pango_font_description_get_size(desc
) / PANGO_SCALE
;
1386 default_font_size_pixels_
= size_pixels
;
1387 query
.pixel_size
= size_pixels
;
1389 // Non-absolute sizes are in points (again scaled by PANGO_SIZE).
1390 // Round the value when converting to pixels to match GTK's logic.
1391 const double size_points
= pango_font_description_get_size(desc
) /
1392 static_cast<double>(PANGO_SCALE
);
1393 default_font_size_pixels_
= static_cast<int>(
1394 GetPixelsInPoint(device_scale_factor_
) * size_points
+ 0.5);
1395 query
.point_size
= static_cast<int>(size_points
);
1398 query
.style
= gfx::Font::NORMAL
;
1399 // TODO(davemoore): Support weights other than bold?
1400 if (pango_font_description_get_weight(desc
) == PANGO_WEIGHT_BOLD
)
1401 query
.style
|= gfx::Font::BOLD
;
1402 // TODO(davemoore): What about PANGO_STYLE_OBLIQUE?
1403 if (pango_font_description_get_style(desc
) == PANGO_STYLE_ITALIC
)
1404 query
.style
|= gfx::Font::ITALIC
;
1406 default_font_render_params_
=
1407 gfx::GetFontRenderParams(query
, &default_font_family_
);
1408 default_font_style_
= query
.style
;
1411 void Gtk2UI::OnStyleSet(GtkWidget
* widget
, GtkStyle
* previous_style
) {
1412 ClearAllThemeData();
1414 NativeThemeGtk2::instance()->NotifyObservers();
1417 void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor
) {
1418 device_scale_factor_
= device_scale_factor
;
1419 GtkStyle
* label_style
= gtk_rc_get_style(fake_label_
.get());
1420 UpdateDefaultFont(label_style
->font_desc
);
1423 } // namespace libgtk2ui
1425 views::LinuxUI
* BuildGtk2UI() {
1426 return new libgtk2ui::Gtk2UI
;