Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / libgtk2ui / gtk2_ui.cc
blob29d8e1c3c007eaae9ed7723376a00f51e4de8e64
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
7 #include <math.h>
8 #include <set>
10 #include <pango/pango.h>
11 #include <X11/Xlib.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"
61 #endif
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.
75 #define __need_NULL
76 #include <stddef.h>
78 namespace libgtk2ui {
80 namespace {
82 struct GObjectDeleter {
83 void operator()(void* ptr) {
84 g_object_unref(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
113 // specified.
114 const color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 };
116 // Values used as the new luminance and saturation values in the inactive tab
117 // text color.
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.
133 // Default tints.
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
146 // down is Hard.
147 const int kThemeImages[] = {
148 IDR_THEME_TOOLBAR,
149 IDR_THEME_TAB_BACKGROUND,
150 IDR_THEME_TAB_BACKGROUND_DESKTOP,
151 IDR_THEME_TAB_BACKGROUND_INCOGNITO,
152 IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP,
153 IDR_FRAME,
154 IDR_FRAME_INACTIVE,
155 IDR_THEME_FRAME,
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
163 // base color.
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,
169 IDR_OMNIBOX_HTTP,
170 IDR_OMNIBOX_HTTP_DARK,
171 IDR_OMNIBOX_SEARCH,
172 IDR_OMNIBOX_SEARCH_DARK,
173 IDR_OMNIBOX_STAR,
174 IDR_OMNIBOX_STAR_DARK,
175 IDR_OMNIBOX_TTS,
176 IDR_OMNIBOX_TTS_DARK,
179 // This table converts button ids into a pair of gtk-stock id and state.
180 struct IDRGtkMapping {
181 int idr;
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
210 // dark on dark)
211 IDR_MENU_DROPARROW
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),
248 &text_tint);
250 color_utils::HSL background_tint;
251 color_utils::SkColorToHSL(GdkColorToSkColor(background_color),
252 &background_tint);
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
267 // matters.
268 tint->h = -1;
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;
277 else
278 tint->l = text_tint.l;
279 } else {
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.
284 tint->s = -1;
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
289 // white.
290 if (text_tint.l < 0.5)
291 tint->l = -1;
292 else if (text_tint.l <= 0.9)
293 tint->l = text_tint.l;
294 else
295 tint->l = 0.9;
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) {
312 switch (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;
325 default:
326 color_utils::HSL result = {-1, -1, -1};
327 return result;
331 // Returns a gfx::FontRenderParams corresponding to GTK's configuration.
332 gfx::FontRenderParams GetGtkFontRenderParams() {
333 GtkSettings* gtk_settings = gtk_settings_get_default();
334 CHECK(gtk_settings);
335 gint antialias = 0;
336 gint hinting = 0;
337 gchar* hint_style = NULL;
338 gchar* rgba = 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,
344 NULL);
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;
357 } else {
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;
372 } else {
373 LOG(WARNING) << "Unexpected gtk-xft-rgba \"" << rgba << "\"";
374 params.subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE;
377 g_free(hint_style);
378 g_free(rgba);
380 return params;
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();
392 CHECK(gtk_settings);
393 gint gtk_dpi = -1;
394 g_object_get(gtk_settings, "gtk-xft-dpi", &gtk_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
401 // point.
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.
410 return dpi / 72.0;
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;
422 default:
423 return views::LinuxUI::MIDDLE_CLICK_ACTION_LOWER;
427 } // namespace
429 Gtk2UI::Gtk2UI()
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);
455 LoadGtkValues();
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();
473 Gtk2UI::~Gtk2UI() {
474 gtk_widget_destroy(fake_window_);
475 gtk_widget_destroy(fake_frame_);
476 fake_label_.Destroy();
477 fake_entry_.Destroy();
479 ClearAllThemeData();
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())
486 return it->second;
488 if (IsOverridableImage(id)) {
489 gfx::Image image = gfx::Image(
490 gfx::ImageSkia::CreateFrom1xBitmap(GenerateGtkThemeBitmap(id)));
491 gtk_images_[id] = image;
492 return image;
495 return gfx::Image();
498 bool Gtk2UI::GetColor(int id, SkColor* color) const {
499 ColorMap::const_iterator it = colors_.find(id);
500 if (it != colors_.end()) {
501 *color = it->second;
502 return true;
505 return false;
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 {
525 return track_color_;
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,
560 NULL);
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:
586 return true;
587 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
588 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
589 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
590 return false;
592 // Unless GetDesktopEnvironment() badly misbehaves, this should never happen.
593 NOTREACHED();
594 return false;
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 {
608 return true;
611 scoped_ptr<views::StatusIconLinux> Gtk2UI::CreateLinuxStatusIcon(
612 const gfx::ImageSkia& image,
613 const base::string16& tool_tip) const {
614 if (AppIndicatorIcon::CouldOpen()) {
615 ++indicators_count;
616 return scoped_ptr<views::StatusIconLinux>(new AppIndicatorIcon(
617 base::StringPrintf("%s%d", kAppIndicatorIdPrefix, indicators_count),
618 image,
619 tool_tip));
620 } else {
621 return scoped_ptr<views::StatusIconLinux>(new Gtk2StatusIcon(
622 image, tool_tip));
626 gfx::Image Gtk2UI::GetIconForContentType(
627 const std::string& content_type,
628 int size) const {
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)));
642 if (!icon_info)
643 continue;
644 ScopedGdkPixbuf pixbuf(gtk_icon_info_load_icon(icon_info.get(), NULL));
645 if (!pixbuf)
646 continue;
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);
655 return gfx::Image();
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_,
671 trailing_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_,
690 trailing_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();
706 return params;
709 void Gtk2UI::GetDefaultFontDescription(
710 std::string* family_out,
711 int* size_pixels_out,
712 int* style_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,
760 NULL);
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);
772 return;
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) {
793 if (i < 2) {
794 // Thumb part
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);
799 } else {
800 // Track
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,
806 3 * kWidth, 0, 0);
807 gdk_pixbuf_get_from_drawable(pb, pm, NULL, 0, 0, 0, 0, kWidth, kHeight);
809 // Sample pixels
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 :
823 track_color;
824 color->pixel = 0;
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));
836 g_object_unref(pb);
838 g_object_unref(pm);
840 gtk_widget_destroy(window);
842 // Override any of the default colors with ones that were specified by the
843 // theme.
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;
901 else
902 inactive_tab_text_hsl.l = kLightInactiveLuminance;
904 if (inactive_tab_text_hsl.s < 0.5)
905 inactive_tab_text_hsl.s = kHeavyInactiveSaturation;
906 else
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,
921 &ntp_background);
922 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_TEXT,
923 &ntp_foreground);
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
929 // more important.
930 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_HEADER,
931 &frame_color);
932 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION,
933 &toolbar_color);
934 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_TEXT,
935 &label_color);
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;
943 if (!link_color) {
944 link_color = &kDefaultLinkColor;
945 is_default_link_color = true;
948 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK,
949 link_color);
950 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_LINK_UNDERLINE,
951 link_color);
952 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK,
953 link_color);
954 SetThemeColorFromGtk(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE,
955 link_color);
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,
966 &track_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,
994 NULL);
996 GdkColor frame_color = BuildAndSetFrameColor(
997 &frame_style->bg[GTK_STATE_SELECTED],
998 theme_frame,
999 kDefaultFrameShift,
1000 ThemeProperties::COLOR_FRAME,
1001 ThemeProperties::TINT_FRAME);
1002 if (theme_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,
1009 kDefaultFrameShift,
1010 ThemeProperties::COLOR_FRAME_INACTIVE,
1011 ThemeProperties::TINT_FRAME_INACTIVE);
1012 if (theme_inactive_frame)
1013 gdk_color_free(theme_inactive_frame);
1015 BuildAndSetFrameColor(
1016 &frame_color,
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(
1025 &frame_color,
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);
1033 return frame_color;
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;
1051 tints_[id] = hsl;
1054 GdkColor Gtk2UI::BuildAndSetFrameColor(const GdkColor* base,
1055 const GdkColor* gtk_base,
1056 const color_utils::HSL& tint,
1057 int color_id,
1058 int tint_id) {
1059 GdkColor out_color = *base;
1060 if (gtk_base) {
1061 // The theme author specified a color to use, use it without modification.
1062 out_color = *gtk_base;
1063 } else {
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);
1071 return out_color;
1074 SkBitmap Gtk2UI::GenerateGtkThemeBitmap(int id) const {
1075 switch (id) {
1076 case IDR_THEME_TOOLBAR: {
1077 GtkStyle* style = gtk_rc_get_style(fake_window_);
1078 GdkColor* color = &style->bg[GTK_STATE_NORMAL];
1079 SkBitmap bitmap;
1080 bitmap.allocN32Pixels(kToolbarImageWidth, kToolbarImageHeight);
1081 bitmap.eraseARGB(0xff, color->red >> 8, color->green >> 8,
1082 color->blue >> 8);
1083 return bitmap;
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);
1091 case IDR_FRAME:
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.
1133 case IDR_BACK:
1134 case IDR_BACK_D:
1135 case IDR_FORWARD:
1136 case IDR_FORWARD_D:
1137 case IDR_HOME:
1138 case IDR_RELOAD:
1139 case IDR_RELOAD_D:
1140 case IDR_STOP:
1141 case IDR_STOP_D: {
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);
1148 default: {
1149 return GenerateTintedIcon(id, button_tint_);
1153 return SkBitmap();
1156 SkBitmap Gtk2UI::GenerateFrameImage(
1157 int color_id,
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),
1167 1.0f, true);
1169 int gradient_size;
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,
1174 NULL);
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);
1181 SkPaint paint;
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(
1206 int base_id,
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;
1220 break;
1223 DCHECK(stock_id);
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);
1231 if (!icon_set)
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(
1236 icon_set,
1237 style,
1238 base::i18n::IsRTL() ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR,
1239 gtk_state,
1240 GTK_ICON_SIZE_SMALL_TOOLBAR,
1241 fake_frame_,
1242 NULL);
1244 if (!gdk_icon) {
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;
1251 SkBitmap retval;
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,
1262 false,
1263 false,
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));
1273 return retval;
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();
1281 SkBitmap retval;
1282 retval.allocN32Pixels(default_bitmap.width(), default_bitmap.height());
1283 retval.eraseColor(0);
1285 SkCanvas canvas(retval);
1286 SkBitmap border = DrawGtkButtonBorder(
1287 gtk_state,
1288 false,
1289 false,
1290 default_bitmap.width(),
1291 default_bitmap.height());
1292 canvas.drawBitmap(border, 0, 0);
1294 return retval;
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,
1329 bool focused,
1330 bool call_to_action,
1331 int width,
1332 int height) const {
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);
1343 if (call_to_action)
1344 GTK_WIDGET_SET_FLAGS(button, GTK_HAS_DEFAULT);
1346 if (focused) {
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));
1355 GdkPixmap* pixmap;
1357 // http://crbug.com/346740
1358 ANNOTATE_SCOPED_MEMORY_LEAK;
1359 pixmap = gtk_widget_get_snapshot(button, NULL);
1361 int w, h;
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
1367 // bits from X.
1368 GdkColormap* colormap = gdk_drawable_get_colormap(pixmap);
1369 GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable(NULL,
1370 GDK_DRAWABLE(pixmap),
1371 colormap,
1372 0, 0, 0, 0, w, h);
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);
1380 return border;
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
1389 // settings.
1390 gfx::FontRenderParamsQuery query;
1391 base::SplitString(pango_font_description_get_family(desc), ',',
1392 &query.families);
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;
1400 } else {
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();
1425 LoadGtkValues();
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;