Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: chrome/browser/ui/libgtk2ui/gtk2_ui.cc

Issue 10545037: Aura/GTK integration: Create a small stub library that can talk to GTK. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sky nits Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
6
7 #include <set>
8
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "chrome/browser/themes/theme_service.h"
13 #include "chrome/browser/ui/libgtk2ui/chrome_gtk_frame.h"
14 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
15 #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
16 #include "grit/theme_resources.h"
17 #include "grit/theme_resources_standard.h"
18 #include "grit/ui_resources.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "third_party/skia/include/core/SkCanvas.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "third_party/skia/include/core/SkShader.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/image/image.h"
26 #include "ui/gfx/rect.h"
27 #include "ui/gfx/size.h"
28 #include "ui/gfx/skbitmap_operations.h"
29 #include "ui/gfx/skia_util.h"
30
31 // A minimized port of GtkThemeService into something that can provide colors
32 // and images for aura.
33 //
34 // TODO(erg): There's still a lot that needs ported or done for the first time:
35 //
36 // - Inject default favicon/folder icons into views somehow.
37 // - Inject GTK back/forward/reload/home items.
38 // - Render and inject the button overlay from the gtk theme.
39 // - Render and inject the omnibox background.
40 // - Listen for the "style-set" signal on |fake_frame_| and recreate theme
41 // colors and images.
42 // - Allow not using the theme.
43 // - Make sure to test with a light on dark theme, too.
44 // - Everything else that we're not doing.
45
46 namespace {
47
48 // The size of the rendered toolbar image.
49 const int kToolbarImageWidth = 64;
50 const int kToolbarImageHeight = 128;
51
52 // How much to tint the GTK+ color lighter at the top of the window.
53 const color_utils::HSL kGtkFrameShift = { -1, -1, 0.58 };
54
55 // How much to tint the GTK+ color when an explicit frame color hasn't been
56 // specified.
57 const color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 };
58
59 // Values used as the new luminance and saturation values in the inactive tab
60 // text color.
61 const double kDarkInactiveLuminance = 0.85;
62 const double kLightInactiveLuminance = 0.15;
63 const double kHeavyInactiveSaturation = 0.7;
64 const double kLightInactiveSaturation = 0.3;
65
66 // Default color for links on the NTP when the GTK+ theme doesn't define a
67 // link color. Constant taken from gtklinkbutton.c.
68 const GdkColor kDefaultLinkColor = { 0, 0, 0, 0xeeee };
69
70 const int kSkiaToGDKMultiplier = 257;
71
72
73 // TODO(erg): ThemeService has a whole interface just for reading default
74 // constants. Figure out what to do with that more long term; for now, just
75 // copy the constants themselves here.
76 //
77 // Default tints.
78 const color_utils::HSL kDefaultTintButtons = { -1, -1, -1 };
79 const color_utils::HSL kDefaultTintFrame = { -1, -1, -1 };
80 const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f };
81 const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f };
82 const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f };
83 const color_utils::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 };
84
85 // A list of images that we provide while in gtk mode.
86 const int kThemeImages[] = {
87 IDR_THEME_TOOLBAR,
88 IDR_THEME_TAB_BACKGROUND,
89 IDR_THEME_TAB_BACKGROUND_INCOGNITO,
90 IDR_THEME_FRAME,
91 IDR_THEME_FRAME_INACTIVE,
92 IDR_THEME_FRAME_INCOGNITO,
93 IDR_THEME_FRAME_INCOGNITO_INACTIVE,
94 };
95
96 // A list of icons used in the autocomplete view that should be tinted to the
97 // current gtk theme selection color so they stand out against the GtkEntry's
98 // base color.
99 const int kAutocompleteImages[] = {
100 IDR_OMNIBOX_EXTENSION_APP,
101 IDR_OMNIBOX_HTTP,
102 IDR_OMNIBOX_HTTP_DARK,
103 IDR_OMNIBOX_HISTORY,
104 IDR_OMNIBOX_HISTORY_DARK,
105 IDR_OMNIBOX_SEARCH,
106 IDR_OMNIBOX_SEARCH_DARK,
107 IDR_OMNIBOX_STAR,
108 IDR_OMNIBOX_STAR_DARK,
109 IDR_OMNIBOX_TTS,
110 IDR_OMNIBOX_TTS_DARK,
111 IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON,
112 IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON,
113 };
114
115 bool IsOverridableImage(int id) {
116 CR_DEFINE_STATIC_LOCAL(std::set<int>, images, ());
117 if (images.empty()) {
118 images.insert(kThemeImages, kThemeImages + arraysize(kThemeImages));
119 images.insert(kAutocompleteImages,
120 kAutocompleteImages + arraysize(kAutocompleteImages));
121
122 // TODO(erg): The original GtkThemeService called ThemeService::
123 // GetTintableToolbarButtons() here. Except that not all of the images
124 // returned are even used; for example, IDR_BACK should have never been
125 // rendered in the old gtk-theme mode. Recreate this list as I refine using
126 // a gtk theme on aura.
127 }
128
129 return images.count(id) > 0;
130 }
131
132 // Picks a button tint from a set of background colors. While
133 // |accent_gdk_color| will usually be the same color through a theme, this
134 // function will get called with the normal GtkLabel |text_color|/GtkWindow
135 // |background_color| pair and the GtkEntry |text_color|/|background_color|
136 // pair. While 3/4 of the time the resulting tint will be the same, themes that
137 // have a dark window background (with light text) and a light text entry (with
138 // dark text) will get better icons with this separated out.
139 void PickButtonTintFromColors(const GdkColor& accent_gdk_color,
140 const GdkColor& text_color,
141 const GdkColor& background_color,
142 color_utils::HSL* tint) {
143 SkColor accent_color = libgtk2ui::GdkColorToSkColor(accent_gdk_color);
144 color_utils::HSL accent_tint;
145 color_utils::SkColorToHSL(accent_color, &accent_tint);
146
147 color_utils::HSL text_tint;
148 color_utils::SkColorToHSL(libgtk2ui::GdkColorToSkColor(text_color),
149 &text_tint);
150
151 color_utils::HSL background_tint;
152 color_utils::SkColorToHSL(libgtk2ui::GdkColorToSkColor(background_color),
153 &background_tint);
154
155 // If the accent color is gray, then our normal HSL tomfoolery will bring out
156 // whatever color is oddly dominant (for example, in rgb space [125, 128,
157 // 125] will tint green instead of gray). Slight differences (+/-10 (4%) to
158 // all color components) should be interpreted as this color being gray and
159 // we should switch into a special grayscale mode.
160 int rb_diff = abs(SkColorGetR(accent_color) - SkColorGetB(accent_color));
161 int rg_diff = abs(SkColorGetR(accent_color) - SkColorGetG(accent_color));
162 int bg_diff = abs(SkColorGetB(accent_color) - SkColorGetG(accent_color));
163 if (rb_diff < 10 && rg_diff < 10 && bg_diff < 10) {
164 // Our accent is white/gray/black. Only the luminance of the accent color
165 // matters.
166 tint->h = -1;
167
168 // Use the saturation of the text.
169 tint->s = text_tint.s;
170
171 // Use the luminance of the accent color UNLESS there isn't enough
172 // luminance contrast between the accent color and the base color.
173 if (fabs(accent_tint.l - background_tint.l) > 0.3)
174 tint->l = accent_tint.l;
175 else
176 tint->l = text_tint.l;
177 } else {
178 // Our accent is a color.
179 tint->h = accent_tint.h;
180
181 // Don't modify the saturation; the amount of color doesn't matter.
182 tint->s = -1;
183
184 // If the text wants us to darken the icon, don't change the luminance (the
185 // icons are already dark enough). Otherwise, lighten the icon by no more
186 // than 0.9 since we don't want a pure-white icon even if the text is pure
187 // white.
188 if (text_tint.l < 0.5)
189 tint->l = -1;
190 else if (text_tint.l <= 0.9)
191 tint->l = text_tint.l;
192 else
193 tint->l = 0.9;
194 }
195 }
196
197 // Applies an HSL shift to a GdkColor (instead of an SkColor)
198 void GdkColorHSLShift(const color_utils::HSL& shift, GdkColor* frame_color) {
199 SkColor shifted = color_utils::HSLShift(
200 libgtk2ui::GdkColorToSkColor(*frame_color), shift);
201
202 frame_color->pixel = 0;
203 frame_color->red = SkColorGetR(shifted) * kSkiaToGDKMultiplier;
204 frame_color->green = SkColorGetG(shifted) * kSkiaToGDKMultiplier;
205 frame_color->blue = SkColorGetB(shifted) * kSkiaToGDKMultiplier;
206 }
207
208 // Copied Default blah sections from ThemeService.
209 color_utils::HSL GetDefaultTint(int id) {
210 switch (id) {
211 case ThemeService::TINT_FRAME:
212 return kDefaultTintFrame;
213 case ThemeService::TINT_FRAME_INACTIVE:
214 return kDefaultTintFrameInactive;
215 case ThemeService::TINT_FRAME_INCOGNITO:
216 return kDefaultTintFrameIncognito;
217 case ThemeService::TINT_FRAME_INCOGNITO_INACTIVE:
218 return kDefaultTintFrameIncognitoInactive;
219 case ThemeService::TINT_BUTTONS:
220 return kDefaultTintButtons;
221 case ThemeService::TINT_BACKGROUND_TAB:
222 return kDefaultTintBackgroundTab;
223 default:
224 color_utils::HSL result = {-1, -1, -1};
225 return result;
226 }
227 }
228
229 } // namespace
230
231 namespace libgtk2ui {
232
233 Gtk2UI::Gtk2UI() {
234 DLOG(ERROR) << "Activating the gtk2 component";
235 GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
236
237 // Create our fake widgets.
238 fake_window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
239 fake_frame_ = chrome_gtk_frame_new();
240 fake_label_.Own(gtk_label_new(""));
241 fake_entry_.Own(gtk_entry_new());
242 fake_menu_item_.Own(gtk_menu_item_new());
243
244 // Only realized widgets receive style-set notifications, which we need to
245 // broadcast new theme images and colors. Only realized widgets have style
246 // properties, too, which we query for some colors.
247 gtk_widget_realize(fake_frame_);
248 gtk_widget_realize(fake_window_);
249 // TODO: Also listen for "style-set" on the fake frame.
250
251 // TODO(erg): Be lazy about generating this data and connect it to the
252 // style-set signal handler.
253 LoadGtkValues();
254 }
255
256 Gtk2UI::~Gtk2UI() {
257 gtk_widget_destroy(fake_window_);
258 gtk_widget_destroy(fake_frame_);
259 fake_label_.Destroy();
260 fake_entry_.Destroy();
261 fake_menu_item_.Destroy();
262
263 ClearAllThemeData();
264 }
265
266 bool Gtk2UI::UseNativeTheme() const {
267 return true;
268 }
269
270 gfx::Image* Gtk2UI::GetThemeImageNamed(int id) const {
271 // Try to get our cached version:
272 ImageCache::const_iterator it = gtk_images_.find(id);
273 if (it != gtk_images_.end())
274 return it->second;
275
276 if (/*use_gtk_ && */ IsOverridableImage(id)) {
277 gfx::Image* image = new gfx::Image(GenerateGtkThemeBitmap(id));
278 gtk_images_[id] = image;
279 return image;
280 }
281
282 return NULL;
283 }
284
285 SkColor Gtk2UI::GetColor(int id) const {
286 ColorMap::const_iterator it = colors_.find(id);
287 DCHECK(it != colors_.end());
288 return it->second;
289 }
290
291 void Gtk2UI::GetScrollbarColors(GdkColor* thumb_active_color,
292 GdkColor* thumb_inactive_color,
293 GdkColor* track_color) {
294 const GdkColor* theme_thumb_active = NULL;
295 const GdkColor* theme_thumb_inactive = NULL;
296 const GdkColor* theme_trough_color = NULL;
297 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
298 "scrollbar-slider-prelight-color", &theme_thumb_active,
299 "scrollbar-slider-normal-color", &theme_thumb_inactive,
300 "scrollbar-trough-color", &theme_trough_color,
301 NULL);
302
303 // Ask the theme if the theme specifies all the scrollbar colors and short
304 // circuit the expensive painting/compositing if we have all of them.
305 if (theme_thumb_active && theme_thumb_inactive && theme_trough_color) {
306 *thumb_active_color = *theme_thumb_active;
307 *thumb_inactive_color = *theme_thumb_inactive;
308 *track_color = *theme_trough_color;
309 return;
310 }
311
312 // Create window containing scrollbar elements
313 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP);
314 GtkWidget* fixed = gtk_fixed_new();
315 GtkWidget* scrollbar = gtk_hscrollbar_new(NULL);
316 gtk_container_add(GTK_CONTAINER(window), fixed);
317 gtk_container_add(GTK_CONTAINER(fixed), scrollbar);
318 gtk_widget_realize(window);
319 gtk_widget_realize(scrollbar);
320
321 // Draw scrollbar thumb part and track into offscreen image
322 const int kWidth = 100;
323 const int kHeight = 20;
324 GtkStyle* style = gtk_rc_get_style(scrollbar);
325 GdkWindow* gdk_window = gtk_widget_get_window(window);
326 GdkPixmap* pm = gdk_pixmap_new(gdk_window, kWidth, kHeight, -1);
327 GdkRectangle rect = { 0, 0, kWidth, kHeight };
328 unsigned char data[3 * kWidth * kHeight];
329 for (int i = 0; i < 3; ++i) {
330 if (i < 2) {
331 // Thumb part
332 gtk_paint_slider(style, pm,
333 i == 0 ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
334 GTK_SHADOW_OUT, &rect, scrollbar, "slider", 0, 0,
335 kWidth, kHeight, GTK_ORIENTATION_HORIZONTAL);
336 } else {
337 // Track
338 gtk_paint_box(style, pm, GTK_STATE_ACTIVE, GTK_SHADOW_IN, &rect,
339 scrollbar, "trough-upper", 0, 0, kWidth, kHeight);
340 }
341 GdkPixbuf* pb = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB,
342 FALSE, 8, kWidth, kHeight,
343 3 * kWidth, 0, 0);
344 gdk_pixbuf_get_from_drawable(pb, pm, NULL, 0, 0, 0, 0, kWidth, kHeight);
345
346 // Sample pixels
347 int components[3] = { 0 };
348 for (int y = 2; y < kHeight - 2; ++y) {
349 for (int c = 0; c < 3; ++c) {
350 // Sample a vertical slice of pixels at about one-thirds from the
351 // left edge. This allows us to avoid any fixed graphics that might be
352 // located at the edges or in the center of the scrollbar.
353 // Each pixel is made up of a red, green, and blue component; taking up
354 // a total of three bytes.
355 components[c] += data[3 * (kWidth / 3 + y * kWidth) + c];
356 }
357 }
358 GdkColor* color = i == 0 ? thumb_active_color :
359 i == 1 ? thumb_inactive_color :
360 track_color;
361 color->pixel = 0;
362 // We sampled pixels across the full height of the image, ignoring a two
363 // pixel border. In some themes, the border has a completely different
364 // color which we do not want to factor into our average color computation.
365 //
366 // We now need to scale the colors from the 0..255 range, to the wider
367 // 0..65535 range, and we need to actually compute the average color; so,
368 // we divide by the total number of pixels in the sample.
369 color->red = components[0] * 65535 / (255 * (kHeight - 4));
370 color->green = components[1] * 65535 / (255 * (kHeight - 4));
371 color->blue = components[2] * 65535 / (255 * (kHeight - 4));
372
373 g_object_unref(pb);
374 }
375 g_object_unref(pm);
376
377 gtk_widget_destroy(window);
378
379 // Override any of the default colors with ones that were specified by the
380 // theme.
381 if (theme_thumb_active)
382 *thumb_active_color = *theme_thumb_active;
383
384 if (theme_thumb_inactive)
385 *thumb_inactive_color = *theme_thumb_inactive;
386
387 if (theme_trough_color)
388 *track_color = *theme_trough_color;
389 }
390
391 void Gtk2UI::LoadGtkValues() {
392 // TODO(erg): GtkThemeService had a comment here about having to muck with
393 // the raw Prefs object to remove prefs::kCurrentThemeImages or else we'd
394 // regress startup time. Figure out how to do that when we can't access the
395 // prefs system from here.
396
397 GtkStyle* frame_style = gtk_rc_get_style(fake_frame_);
398
399 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
400 SetThemeColorFromGtk(ThemeService::COLOR_CONTROL_BACKGROUND,
401 &window_style->bg[GTK_STATE_NORMAL]);
402
403 GdkColor toolbar_color = window_style->bg[GTK_STATE_NORMAL];
404 SetThemeColorFromGtk(ThemeService::COLOR_TOOLBAR, &toolbar_color);
405
406 GdkColor button_color = window_style->bg[GTK_STATE_SELECTED];
407 SetThemeTintFromGtk(ThemeService::TINT_BUTTONS, &button_color);
408
409 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get());
410 GdkColor label_color = label_style->fg[GTK_STATE_NORMAL];
411 SetThemeColorFromGtk(ThemeService::COLOR_TAB_TEXT, &label_color);
412 SetThemeColorFromGtk(ThemeService::COLOR_BOOKMARK_TEXT, &label_color);
413
414 // Build the various icon tints.
415 GetNormalButtonTintHSL(&button_tint_);
416 GetNormalEntryForegroundHSL(&entry_tint_);
417 GetSelectedEntryForegroundHSL(&selected_entry_tint_);
418 GdkColor frame_color = BuildFrameColors(frame_style);
419
420 // The inactive frame color never occurs naturally in the theme, as it is a
421 // tinted version of |frame_color|. We generate another color based on the
422 // background tab color, with the lightness and saturation moved in the
423 // opposite direction. (We don't touch the hue, since there should be subtle
424 // hints of the color in the text.)
425 color_utils::HSL inactive_tab_text_hsl =
426 tints_[ThemeService::TINT_BACKGROUND_TAB];
427 if (inactive_tab_text_hsl.l < 0.5)
428 inactive_tab_text_hsl.l = kDarkInactiveLuminance;
429 else
430 inactive_tab_text_hsl.l = kLightInactiveLuminance;
431
432 if (inactive_tab_text_hsl.s < 0.5)
433 inactive_tab_text_hsl.s = kHeavyInactiveSaturation;
434 else
435 inactive_tab_text_hsl.s = kLightInactiveSaturation;
436
437 colors_[ThemeService::COLOR_BACKGROUND_TAB_TEXT] =
438 color_utils::HSLToSkColor(inactive_tab_text_hsl, 255);
439
440 // We pick the text and background colors for the NTP out of the colors for a
441 // GtkEntry. We do this because GtkEntries background color is never the same
442 // as |toolbar_color|, is usually a white, and when it isn't a white,
443 // provides sufficient contrast to |toolbar_color|. Try this out with
444 // Darklooks, HighContrastInverse or ThinIce.
445 GtkStyle* entry_style = gtk_rc_get_style(fake_entry_.get());
446 GdkColor ntp_background = entry_style->base[GTK_STATE_NORMAL];
447 GdkColor ntp_foreground = entry_style->text[GTK_STATE_NORMAL];
448 SetThemeColorFromGtk(ThemeService::COLOR_NTP_BACKGROUND,
449 &ntp_background);
450 SetThemeColorFromGtk(ThemeService::COLOR_NTP_TEXT,
451 &ntp_foreground);
452
453 // The NTP header is the color that surrounds the current active thumbnail on
454 // the NTP, and acts as the border of the "Recent Links" box. It would be
455 // awesome if they were separated so we could use GetBorderColor() for the
456 // border around the "Recent Links" section, but matching the frame color is
457 // more important.
458 SetThemeColorFromGtk(ThemeService::COLOR_NTP_HEADER,
459 &frame_color);
460 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION,
461 &toolbar_color);
462 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION_TEXT,
463 &label_color);
464
465 // Override the link color if the theme provides it.
466 const GdkColor* link_color = NULL;
467 gtk_widget_style_get(GTK_WIDGET(fake_window_),
468 "link-color", &link_color, NULL);
469
470 bool is_default_link_color = false;
471 if (!link_color) {
472 link_color = &kDefaultLinkColor;
473 is_default_link_color = true;
474 }
475
476 SetThemeColorFromGtk(ThemeService::COLOR_NTP_LINK,
477 link_color);
478 SetThemeColorFromGtk(ThemeService::COLOR_NTP_LINK_UNDERLINE,
479 link_color);
480 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION_LINK,
481 link_color);
482 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION_LINK_UNDERLINE,
483 link_color);
484
485 if (!is_default_link_color)
486 gdk_color_free(const_cast<GdkColor*>(link_color));
487
488 // Generate the colors that we pass to WebKit.
489 focus_ring_color_ = GdkColorToSkColor(frame_color);
490
491 GdkColor thumb_active_color, thumb_inactive_color, track_color;
492 Gtk2UI::GetScrollbarColors(&thumb_active_color,
493 &thumb_inactive_color,
494 &track_color);
495 thumb_active_color_ = GdkColorToSkColor(thumb_active_color);
496 thumb_inactive_color_ = GdkColorToSkColor(thumb_inactive_color);
497 track_color_ = GdkColorToSkColor(track_color);
498
499 // Some GTK themes only define the text selection colors on the GtkEntry
500 // class, so we need to use that for getting selection colors.
501 active_selection_bg_color_ =
502 GdkColorToSkColor(entry_style->base[GTK_STATE_SELECTED]);
503 active_selection_fg_color_ =
504 GdkColorToSkColor(entry_style->text[GTK_STATE_SELECTED]);
505 inactive_selection_bg_color_ =
506 GdkColorToSkColor(entry_style->base[GTK_STATE_ACTIVE]);
507 inactive_selection_fg_color_ =
508 GdkColorToSkColor(entry_style->text[GTK_STATE_ACTIVE]);
509 }
510
511 GdkColor Gtk2UI::BuildFrameColors(GtkStyle* frame_style) {
512 GdkColor* theme_frame = NULL;
513 GdkColor* theme_inactive_frame = NULL;
514 GdkColor* theme_incognito_frame = NULL;
515 GdkColor* theme_incognito_inactive_frame = NULL;
516 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
517 "frame-color", &theme_frame,
518 "inactive-frame-color", &theme_inactive_frame,
519 "incognito-frame-color", &theme_incognito_frame,
520 "incognito-inactive-frame-color",
521 &theme_incognito_inactive_frame,
522 NULL);
523
524 GdkColor frame_color = BuildAndSetFrameColor(
525 &frame_style->bg[GTK_STATE_SELECTED],
526 theme_frame,
527 kDefaultFrameShift,
528 ThemeService::COLOR_FRAME,
529 ThemeService::TINT_FRAME);
530 if (theme_frame)
531 gdk_color_free(theme_frame);
532 SetThemeTintFromGtk(ThemeService::TINT_BACKGROUND_TAB, &frame_color);
533
534 BuildAndSetFrameColor(
535 &frame_style->bg[GTK_STATE_INSENSITIVE],
536 theme_inactive_frame,
537 kDefaultFrameShift,
538 ThemeService::COLOR_FRAME_INACTIVE,
539 ThemeService::TINT_FRAME_INACTIVE);
540 if (theme_inactive_frame)
541 gdk_color_free(theme_inactive_frame);
542
543 BuildAndSetFrameColor(
544 &frame_color,
545 theme_incognito_frame,
546 GetDefaultTint(ThemeService::TINT_FRAME_INCOGNITO),
547 ThemeService::COLOR_FRAME_INCOGNITO,
548 ThemeService::TINT_FRAME_INCOGNITO);
549 if (theme_incognito_frame)
550 gdk_color_free(theme_incognito_frame);
551
552 BuildAndSetFrameColor(
553 &frame_color,
554 theme_incognito_inactive_frame,
555 GetDefaultTint(ThemeService::TINT_FRAME_INCOGNITO_INACTIVE),
556 ThemeService::COLOR_FRAME_INCOGNITO_INACTIVE,
557 ThemeService::TINT_FRAME_INCOGNITO_INACTIVE);
558 if (theme_incognito_inactive_frame)
559 gdk_color_free(theme_incognito_inactive_frame);
560
561 return frame_color;
562 }
563
564 void Gtk2UI::SetThemeColorFromGtk(int id, const GdkColor* color) {
565 colors_[id] = GdkColorToSkColor(*color);
566 }
567
568 void Gtk2UI::SetThemeTintFromGtk(int id, const GdkColor* color) {
569 color_utils::HSL default_tint = GetDefaultTint(id);
570 color_utils::HSL hsl;
571 color_utils::SkColorToHSL(GdkColorToSkColor(*color), &hsl);
572
573 if (default_tint.s != -1)
574 hsl.s = default_tint.s;
575
576 if (default_tint.l != -1)
577 hsl.l = default_tint.l;
578
579 tints_[id] = hsl;
580 }
581
582 GdkColor Gtk2UI::BuildAndSetFrameColor(const GdkColor* base,
583 const GdkColor* gtk_base,
584 const color_utils::HSL& tint,
585 int color_id,
586 int tint_id) {
587 GdkColor out_color = *base;
588 if (gtk_base) {
589 // The theme author specified a color to use, use it without modification.
590 out_color = *gtk_base;
591 } else {
592 // Tint the basic color since this is a heuristic color instead of one
593 // specified by the theme author.
594 GdkColorHSLShift(tint, &out_color);
595 }
596 SetThemeColorFromGtk(color_id, &out_color);
597 SetThemeTintFromGtk(tint_id, &out_color);
598
599 return out_color;
600 }
601
602 SkBitmap Gtk2UI::GenerateGtkThemeBitmap(int id) const {
603 switch (id) {
604 case IDR_THEME_TOOLBAR: {
605 GtkStyle* style = gtk_rc_get_style(fake_window_);
606 GdkColor* color = &style->bg[GTK_STATE_NORMAL];
607 SkBitmap bitmap;
608 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
609 kToolbarImageWidth, kToolbarImageHeight);
610 bitmap.allocPixels();
611 bitmap.eraseRGB(color->red >> 8, color->green >> 8, color->blue >> 8);
612 return bitmap;
613 }
614 case IDR_THEME_TAB_BACKGROUND:
615 return GenerateTabImage(IDR_THEME_FRAME);
616 case IDR_THEME_TAB_BACKGROUND_INCOGNITO:
617 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO);
618 case IDR_THEME_FRAME:
619 return GenerateFrameImage(ThemeService::COLOR_FRAME,
620 "frame-gradient-color");
621 case IDR_THEME_FRAME_INACTIVE:
622 return GenerateFrameImage(ThemeService::COLOR_FRAME_INACTIVE,
623 "inactive-frame-gradient-color");
624 case IDR_THEME_FRAME_INCOGNITO:
625 return GenerateFrameImage(ThemeService::COLOR_FRAME_INCOGNITO,
626 "incognito-frame-gradient-color");
627 case IDR_THEME_FRAME_INCOGNITO_INACTIVE: {
628 return GenerateFrameImage(
629 ThemeService::COLOR_FRAME_INCOGNITO_INACTIVE,
630 "incognito-inactive-frame-gradient-color");
631 }
632 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and
633 // instead should tint based on the foreground text entry color in GTK+
634 // mode because some themes that try to be dark *and* light have very
635 // different colors between the omnibox and the normal background area.
636 case IDR_OMNIBOX_EXTENSION_APP:
637 case IDR_OMNIBOX_HISTORY:
638 case IDR_OMNIBOX_HTTP:
639 case IDR_OMNIBOX_SEARCH:
640 case IDR_OMNIBOX_STAR:
641 case IDR_OMNIBOX_TTS:
642 case IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON:
643 case IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON: {
644 return GenerateTintedIcon(id, entry_tint_);
645 }
646 // In GTK mode, the dark versions of the omnibox icons only ever appear in
647 // the autocomplete popup and only against the current theme's GtkEntry
648 // base[GTK_STATE_SELECTED] color, so tint the icons so they won't collide
649 // with the selected color.
650 case IDR_OMNIBOX_EXTENSION_APP_DARK:
651 case IDR_OMNIBOX_HISTORY_DARK:
652 case IDR_OMNIBOX_HTTP_DARK:
653 case IDR_OMNIBOX_SEARCH_DARK:
654 case IDR_OMNIBOX_STAR_DARK:
655 case IDR_OMNIBOX_TTS_DARK: {
656 return GenerateTintedIcon(id, selected_entry_tint_);
657 }
658 default: {
659 return GenerateTintedIcon(id, button_tint_);
660 }
661 }
662
663 return SkBitmap();
664 }
665
666 SkBitmap Gtk2UI::GenerateFrameImage(
667 int color_id,
668 const char* gradient_name) const {
669 // We use two colors: the main color (passed in) and a lightened version of
670 // that color (which is supposed to match the light gradient at the top of
671 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird).
672 ColorMap::const_iterator it = colors_.find(color_id);
673 DCHECK(it != colors_.end());
674 SkColor base = it->second;
675
676 gfx::Canvas canvas(gfx::Size(kToolbarImageWidth, kToolbarImageHeight), true);
677
678 int gradient_size;
679 GdkColor* gradient_top_color = NULL;
680 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
681 "frame-gradient-size", &gradient_size,
682 gradient_name, &gradient_top_color,
683 NULL);
684 if (gradient_size) {
685 SkColor lighter = gradient_top_color ?
686 GdkColorToSkColor(*gradient_top_color) :
687 color_utils::HSLShift(base, kGtkFrameShift);
688 if (gradient_top_color)
689 gdk_color_free(gradient_top_color);
690 SkShader* shader = gfx::CreateGradientShader(
691 0, gradient_size, lighter, base);
692 SkPaint paint;
693 paint.setStyle(SkPaint::kFill_Style);
694 paint.setAntiAlias(true);
695 paint.setShader(shader);
696 shader->unref();
697
698 canvas.DrawRect(gfx::Rect(0, 0, kToolbarImageWidth, gradient_size), paint);
699 }
700
701 canvas.FillRect(gfx::Rect(0, gradient_size, kToolbarImageWidth,
702 kToolbarImageHeight - gradient_size), base);
703 return canvas.ExtractBitmap();
704 }
705
706 SkBitmap Gtk2UI::GenerateTabImage(int base_id) const {
707 const SkBitmap* base_image = GetThemeImageNamed(base_id)->ToSkBitmap();
708 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap(
709 *base_image, GetDefaultTint(ThemeService::TINT_BACKGROUND_TAB));
710 return SkBitmapOperations::CreateTiledBitmap(
711 bg_tint, 0, 0, bg_tint.width(), bg_tint.height());
712 }
713
714 SkBitmap Gtk2UI::GenerateTintedIcon(
715 int base_id,
716 const color_utils::HSL& tint) const {
717 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
718 return SkBitmapOperations::CreateHSLShiftedBitmap(
719 *rb.GetBitmapNamed(base_id), tint);
720 }
721
722 void Gtk2UI::GetNormalButtonTintHSL(color_utils::HSL* tint) const {
723 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
724 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED];
725 const GdkColor base_color = window_style->base[GTK_STATE_NORMAL];
726
727 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get());
728 const GdkColor text_color = label_style->fg[GTK_STATE_NORMAL];
729
730 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint);
731 }
732
733 void Gtk2UI::GetNormalEntryForegroundHSL(color_utils::HSL* tint) const {
734 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
735 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED];
736
737 GtkStyle* style = gtk_rc_get_style(fake_entry_.get());
738 const GdkColor text_color = style->text[GTK_STATE_NORMAL];
739 const GdkColor base_color = style->base[GTK_STATE_NORMAL];
740
741 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint);
742 }
743
744 void Gtk2UI::GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const {
745 // The simplest of all the tints. We just use the selected text in the entry
746 // since the icons tinted this way will only be displayed against
747 // base[GTK_STATE_SELECTED].
748 GtkStyle* style = gtk_rc_get_style(fake_entry_.get());
749 const GdkColor color = style->text[GTK_STATE_SELECTED];
750 color_utils::SkColorToHSL(GdkColorToSkColor(color), tint);
751 }
752
753 void Gtk2UI::ClearAllThemeData() {
754 STLDeleteValues(&gtk_images_);
755 }
756
757 } // namespace libgtk2ui
758
759 ui::LinuxUI* BuildGtk2UI() {
760 return new libgtk2ui::Gtk2UI;
761 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698