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

Side by Side Diff: chrome/browser/ui/gtk/theme_service_gtk.cc

Issue 10337010: Revert r123782. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 7 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/gtk/theme_service_gtk.h"
6
7 #include <gtk/gtk.h>
8
9 #include <set>
10 #include <string>
11
12 #include "base/debug/trace_event.h"
13 #include "base/environment.h"
14 #include "base/nix/xdg_util.h"
15 #include "base/stl_util.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/themes/theme_service_factory.h"
19 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/gtk/chrome_gtk_frame.h"
22 #include "chrome/browser/ui/gtk/gtk_chrome_button.h"
23 #include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
24 #include "chrome/browser/ui/gtk/gtk_util.h"
25 #include "chrome/browser/ui/gtk/hover_controller_gtk.h"
26 #include "chrome/common/chrome_notification_types.h"
27 #include "chrome/common/pref_names.h"
28 #include "content/public/browser/notification_details.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/notification_source.h"
31 #include "grit/theme_resources.h"
32 #include "grit/theme_resources_standard.h"
33 #include "grit/ui_resources.h"
34 #include "third_party/skia/include/core/SkBitmap.h"
35 #include "third_party/skia/include/core/SkCanvas.h"
36 #include "third_party/skia/include/core/SkColor.h"
37 #include "third_party/skia/include/core/SkShader.h"
38 #include "ui/base/gtk/gtk_hig_constants.h"
39 #include "ui/base/gtk/gtk_signal_registrar.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/gfx/canvas.h"
42 #include "ui/gfx/color_utils.h"
43 #include "ui/gfx/gtk_util.h"
44 #include "ui/gfx/image/cairo_cached_surface.h"
45 #include "ui/gfx/image/image.h"
46 #include "ui/gfx/skbitmap_operations.h"
47 #include "ui/gfx/skia_util.h"
48 #include "ui/gfx/skia_utils_gtk.h"
49
50 namespace {
51
52 // The size of the rendered toolbar image.
53 const int kToolbarImageWidth = 64;
54 const int kToolbarImageHeight = 128;
55
56 // How much to tint the GTK+ color lighter at the top of the window.
57 const color_utils::HSL kGtkFrameShift = { -1, -1, 0.58 };
58
59 // How much to tint the GTK+ color when an explicit frame color hasn't been
60 // specified.
61 const color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 };
62
63 // Values used as the new luminance and saturation values in the inactive tab
64 // text color.
65 const double kDarkInactiveLuminance = 0.85;
66 const double kLightInactiveLuminance = 0.15;
67 const double kHeavyInactiveSaturation = 0.7;
68 const double kLightInactiveSaturation = 0.3;
69
70 // Number of times that the background color should be counted when trying to
71 // calculate the border color in GTK theme mode.
72 const int kBgWeight = 3;
73
74 // Padding to left, top and bottom of vertical separators.
75 const int kSeparatorPadding = 2;
76
77 // Default color for links on the NTP when the GTK+ theme doesn't define a
78 // link color. Constant taken from gtklinkbutton.c.
79 const GdkColor kDefaultLinkColor = { 0, 0, 0, 0xeeee };
80
81 // Middle color of the separator gradient.
82 const double kMidSeparatorColor[] =
83 { 194.0 / 255.0, 205.0 / 255.0, 212.0 / 212.0 };
84 // Top color of the separator gradient.
85 const double kTopSeparatorColor[] =
86 { 222.0 / 255.0, 234.0 / 255.0, 248.0 / 255.0 };
87
88 // A list of images that we provide while in gtk mode.
89 const int kThemeImages[] = {
90 IDR_THEME_TOOLBAR,
91 IDR_THEME_TAB_BACKGROUND,
92 IDR_THEME_TAB_BACKGROUND_INCOGNITO,
93 IDR_THEME_FRAME,
94 IDR_THEME_FRAME_INACTIVE,
95 IDR_THEME_FRAME_INCOGNITO,
96 IDR_THEME_FRAME_INCOGNITO_INACTIVE,
97 };
98
99 // A list of icons used in the autocomplete view that should be tinted to the
100 // current gtk theme selection color so they stand out against the GtkEntry's
101 // base color.
102 const int kAutocompleteImages[] = {
103 IDR_OMNIBOX_EXTENSION_APP,
104 IDR_OMNIBOX_HTTP,
105 IDR_OMNIBOX_HTTP_DARK,
106 IDR_OMNIBOX_HISTORY,
107 IDR_OMNIBOX_HISTORY_DARK,
108 IDR_OMNIBOX_SEARCH,
109 IDR_OMNIBOX_SEARCH_DARK,
110 IDR_OMNIBOX_STAR,
111 IDR_OMNIBOX_STAR_DARK,
112 IDR_OMNIBOX_TTS,
113 IDR_OMNIBOX_TTS_DARK,
114 IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON,
115 IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON,
116 };
117
118 bool IsOverridableImage(int id) {
119 CR_DEFINE_STATIC_LOCAL(std::set<int>, images, ());
120 if (images.empty()) {
121 images.insert(kThemeImages, kThemeImages + arraysize(kThemeImages));
122 images.insert(kAutocompleteImages,
123 kAutocompleteImages + arraysize(kAutocompleteImages));
124
125 const std::set<int>& buttons = ThemeService::GetTintableToolbarButtons();
126 images.insert(buttons.begin(), buttons.end());
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 = gfx::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(gfx::GdkColorToSkColor(text_color), &text_tint);
149
150 color_utils::HSL background_tint;
151 color_utils::SkColorToHSL(gfx::GdkColorToSkColor(background_color),
152 &background_tint);
153
154 // If the accent color is gray, then our normal HSL tomfoolery will bring out
155 // whatever color is oddly dominant (for example, in rgb space [125, 128,
156 // 125] will tint green instead of gray). Slight differences (+/-10 (4%) to
157 // all color components) should be interpreted as this color being gray and
158 // we should switch into a special grayscale mode.
159 int rb_diff = abs(SkColorGetR(accent_color) - SkColorGetB(accent_color));
160 int rg_diff = abs(SkColorGetR(accent_color) - SkColorGetG(accent_color));
161 int bg_diff = abs(SkColorGetB(accent_color) - SkColorGetG(accent_color));
162 if (rb_diff < 10 && rg_diff < 10 && bg_diff < 10) {
163 // Our accent is white/gray/black. Only the luminance of the accent color
164 // matters.
165 tint->h = -1;
166
167 // Use the saturation of the text.
168 tint->s = text_tint.s;
169
170 // Use the luminance of the accent color UNLESS there isn't enough
171 // luminance contrast between the accent color and the base color.
172 if (fabs(accent_tint.l - background_tint.l) > 0.3)
173 tint->l = accent_tint.l;
174 else
175 tint->l = text_tint.l;
176 } else {
177 // Our accent is a color.
178 tint->h = accent_tint.h;
179
180 // Don't modify the saturation; the amount of color doesn't matter.
181 tint->s = -1;
182
183 // If the text wants us to darken the icon, don't change the luminance (the
184 // icons are already dark enough). Otherwise, lighten the icon by no more
185 // than 0.9 since we don't want a pure-white icon even if the text is pure
186 // white.
187 if (text_tint.l < 0.5)
188 tint->l = -1;
189 else if (text_tint.l <= 0.9)
190 tint->l = text_tint.l;
191 else
192 tint->l = 0.9;
193 }
194 }
195
196
197 // Builds and tints the image with |id| to the GtkStateType |state| and
198 // places the result in |icon_set|.
199 void BuildIconFromIDRWithColor(int id,
200 GtkStyle* style,
201 GtkStateType state,
202 GtkIconSet* icon_set) {
203 SkColor color = gfx::GdkColorToSkColor(style->fg[state]);
204 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
205 SkBitmap original = *rb.GetBitmapNamed(id);
206
207 SkBitmap fill_color;
208 fill_color.setConfig(SkBitmap::kARGB_8888_Config,
209 original.width(), original.height(), 0);
210 fill_color.allocPixels();
211 fill_color.eraseColor(color);
212 SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(
213 fill_color, original);
214
215 GtkIconSource* icon = gtk_icon_source_new();
216 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&masked);
217 gtk_icon_source_set_pixbuf(icon, pixbuf);
218 g_object_unref(pixbuf);
219
220 gtk_icon_source_set_direction_wildcarded(icon, TRUE);
221 gtk_icon_source_set_size_wildcarded(icon, TRUE);
222
223 gtk_icon_source_set_state(icon, state);
224 // All fields default to wildcarding being on and setting a property doesn't
225 // turn off wildcarding. You need to do this yourself. This is stated once in
226 // the documentation in the gtk_icon_source_new() function, and no where else.
227 gtk_icon_source_set_state_wildcarded(
228 icon, state == GTK_STATE_NORMAL);
229
230 gtk_icon_set_add_source(icon_set, icon);
231 gtk_icon_source_free(icon);
232 }
233
234 // Applies an HSL shift to a GdkColor (instead of an SkColor)
235 void GdkColorHSLShift(const color_utils::HSL& shift, GdkColor* frame_color) {
236 SkColor shifted = color_utils::HSLShift(gfx::GdkColorToSkColor(*frame_color),
237 shift);
238 frame_color->pixel = 0;
239 frame_color->red = SkColorGetR(shifted) * ui::kSkiaToGDKMultiplier;
240 frame_color->green = SkColorGetG(shifted) * ui::kSkiaToGDKMultiplier;
241 frame_color->blue = SkColorGetB(shifted) * ui::kSkiaToGDKMultiplier;
242 }
243
244 } // namespace
245
246 GtkWidget* ThemeServiceGtk::icon_widget_ = NULL;
247 gfx::Image* ThemeServiceGtk::default_folder_icon_ = NULL;
248 gfx::Image* ThemeServiceGtk::default_bookmark_icon_ = NULL;
249
250 // static
251 ThemeServiceGtk* ThemeServiceGtk::GetFrom(Profile* profile) {
252 return static_cast<ThemeServiceGtk*>(
253 ThemeServiceFactory::GetForProfile(profile));
254 }
255
256 ThemeServiceGtk::ThemeServiceGtk()
257 : ThemeService(),
258 use_gtk_(false),
259 fake_window_(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
260 fake_frame_(chrome_gtk_frame_new()),
261 signals_(new ui::GtkSignalRegistrar),
262 fullscreen_icon_set_(NULL) {
263 fake_label_.Own(gtk_label_new(""));
264 fake_entry_.Own(gtk_entry_new());
265 fake_menu_item_.Own(gtk_menu_item_new());
266
267 // Only realized widgets receive style-set notifications, which we need to
268 // broadcast new theme images and colors. Only realized widgets have style
269 // properties, too, which we query for some colors.
270 gtk_widget_realize(fake_frame_);
271 gtk_widget_realize(fake_window_);
272 signals_->Connect(fake_frame_, "style-set",
273 G_CALLBACK(&OnStyleSetThunk), this);
274 }
275
276 ThemeServiceGtk::~ThemeServiceGtk() {
277 gtk_widget_destroy(fake_window_);
278 gtk_widget_destroy(fake_frame_);
279 fake_label_.Destroy();
280 fake_entry_.Destroy();
281 fake_menu_item_.Destroy();
282
283 FreeIconSets();
284
285 // We have to call this because FreePlatformCached() in ~ThemeService
286 // doesn't call the right virutal FreePlatformCaches.
287 FreePlatformCaches();
288 }
289
290 void ThemeServiceGtk::Init(Profile* profile) {
291 registrar_.Init(profile->GetPrefs());
292 registrar_.Add(prefs::kUsesSystemTheme, this);
293 use_gtk_ = profile->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
294 ThemeService::Init(profile);
295 }
296
297 SkBitmap* ThemeServiceGtk::GetBitmapNamed(int id) const {
298 // TODO(erg): Remove this const cast. The gfx::Image interface returns its
299 // images const. GetBitmapNamed() also should but doesn't and has a million
300 // callsites.
301 return const_cast<SkBitmap*>(GetImageNamed(id)->ToSkBitmap());
302 }
303
304 const gfx::Image* ThemeServiceGtk::GetImageNamed(int id) const {
305 // Try to get our cached version:
306 ImageCache::const_iterator it = gtk_images_.find(id);
307 if (it != gtk_images_.end())
308 return it->second;
309
310 if (use_gtk_ && IsOverridableImage(id)) {
311 gfx::Image* image = new gfx::Image(GenerateGtkThemeBitmap(id));
312 gtk_images_[id] = image;
313 return image;
314 }
315
316 return ThemeService::GetImageNamed(id);
317 }
318
319 SkColor ThemeServiceGtk::GetColor(int id) const {
320 if (use_gtk_) {
321 ColorMap::const_iterator it = colors_.find(id);
322 if (it != colors_.end())
323 return it->second;
324 }
325
326 return ThemeService::GetColor(id);
327 }
328
329 bool ThemeServiceGtk::HasCustomImage(int id) const {
330 if (use_gtk_)
331 return IsOverridableImage(id);
332
333 return ThemeService::HasCustomImage(id);
334 }
335
336 void ThemeServiceGtk::InitThemesFor(NotificationObserver* observer) {
337 observer->Observe(chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
338 content::Source<ThemeService>(this),
339 content::NotificationService::NoDetails());
340 }
341
342 void ThemeServiceGtk::SetTheme(const Extension* extension) {
343 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false);
344 LoadDefaultValues();
345 ThemeService::SetTheme(extension);
346 }
347
348 void ThemeServiceGtk::UseDefaultTheme() {
349 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, false);
350 LoadDefaultValues();
351 ThemeService::UseDefaultTheme();
352 }
353
354 void ThemeServiceGtk::SetNativeTheme() {
355 profile()->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, true);
356 ClearAllThemeData();
357 LoadGtkValues();
358 NotifyThemeChanged();
359 }
360
361 bool ThemeServiceGtk::UsingDefaultTheme() const {
362 return !use_gtk_ && ThemeService::UsingDefaultTheme();
363 }
364
365 bool ThemeServiceGtk::UsingNativeTheme() const {
366 return use_gtk_;
367 }
368
369 void ThemeServiceGtk::Observe(int type,
370 const content::NotificationSource& source,
371 const content::NotificationDetails& details) {
372 if ((type == chrome::NOTIFICATION_PREF_CHANGED) &&
373 (*content::Details<std::string>(details).ptr() ==
374 prefs::kUsesSystemTheme)) {
375 use_gtk_ = profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
376 } else {
377 ThemeService::Observe(type, source, details);
378 }
379 }
380
381 GtkWidget* ThemeServiceGtk::BuildChromeButton() {
382 GtkWidget* button = HoverControllerGtk::CreateChromeButton();
383 gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button), use_gtk_);
384 chrome_buttons_.push_back(button);
385
386 signals_->Connect(button, "destroy", G_CALLBACK(OnDestroyChromeButtonThunk),
387 this);
388 return button;
389 }
390
391 GtkWidget* ThemeServiceGtk::BuildChromeLinkButton(const std::string& text) {
392 GtkWidget* link_button = gtk_chrome_link_button_new(text.c_str());
393 gtk_chrome_link_button_set_use_gtk_theme(
394 GTK_CHROME_LINK_BUTTON(link_button),
395 use_gtk_);
396 link_buttons_.push_back(link_button);
397
398 signals_->Connect(link_button, "destroy",
399 G_CALLBACK(OnDestroyChromeLinkButtonThunk), this);
400
401 return link_button;
402 }
403
404 GtkWidget* ThemeServiceGtk::BuildLabel(const std::string& text,
405 GdkColor color) {
406 GtkWidget* label = gtk_label_new(text.empty() ? NULL : text.c_str());
407 if (!use_gtk_)
408 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &color);
409 labels_.insert(std::make_pair(label, color));
410
411 signals_->Connect(label, "destroy", G_CALLBACK(OnDestroyLabelThunk), this);
412
413 return label;
414 }
415
416 GtkWidget* ThemeServiceGtk::CreateToolbarSeparator() {
417 GtkWidget* separator = gtk_vseparator_new();
418 GtkWidget* alignment = gtk_alignment_new(0, 0, 1, 1);
419 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
420 kSeparatorPadding, kSeparatorPadding, kSeparatorPadding, 0);
421 gtk_container_add(GTK_CONTAINER(alignment), separator);
422
423 signals_->Connect(separator, "expose-event",
424 G_CALLBACK(OnSeparatorExposeThunk), this);
425 return alignment;
426 }
427
428 GdkColor ThemeServiceGtk::GetGdkColor(int id) const {
429 return gfx::SkColorToGdkColor(GetColor(id));
430 }
431
432 GdkColor ThemeServiceGtk::GetBorderColor() const {
433 GtkStyle* style = gtk_rc_get_style(fake_window_);
434
435 GdkColor text;
436 GdkColor bg;
437 if (use_gtk_) {
438 text = style->text[GTK_STATE_NORMAL];
439 bg = style->bg[GTK_STATE_NORMAL];
440 } else {
441 text = GetGdkColor(COLOR_BOOKMARK_TEXT);
442 bg = GetGdkColor(COLOR_TOOLBAR);
443 }
444
445 // Creates a weighted average between the text and base color where
446 // the base color counts more than once.
447 GdkColor color;
448 color.pixel = 0;
449 color.red = (text.red + (bg.red * kBgWeight)) / (1 + kBgWeight);
450 color.green = (text.green + (bg.green * kBgWeight)) / (1 + kBgWeight);
451 color.blue = (text.blue + (bg.blue * kBgWeight)) / (1 + kBgWeight);
452
453 return color;
454 }
455
456 GtkIconSet* ThemeServiceGtk::GetIconSetForId(int id) const {
457 if (id == IDR_FULLSCREEN_MENU_BUTTON)
458 return fullscreen_icon_set_;
459
460 return NULL;
461 }
462
463 void ThemeServiceGtk::GetScrollbarColors(GdkColor* thumb_active_color,
464 GdkColor* thumb_inactive_color,
465 GdkColor* track_color) {
466 const GdkColor* theme_thumb_active = NULL;
467 const GdkColor* theme_thumb_inactive = NULL;
468 const GdkColor* theme_trough_color = NULL;
469 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
470 "scrollbar-slider-prelight-color", &theme_thumb_active,
471 "scrollbar-slider-normal-color", &theme_thumb_inactive,
472 "scrollbar-trough-color", &theme_trough_color,
473 NULL);
474
475 // Ask the theme if the theme specifies all the scrollbar colors and short
476 // circuit the expensive painting/compositing if we have all of them.
477 if (theme_thumb_active && theme_thumb_inactive && theme_trough_color) {
478 *thumb_active_color = *theme_thumb_active;
479 *thumb_inactive_color = *theme_thumb_inactive;
480 *track_color = *theme_trough_color;
481 return;
482 }
483
484 // Create window containing scrollbar elements
485 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP);
486 GtkWidget* fixed = gtk_fixed_new();
487 GtkWidget* scrollbar = gtk_hscrollbar_new(NULL);
488 gtk_container_add(GTK_CONTAINER(window), fixed);
489 gtk_container_add(GTK_CONTAINER(fixed), scrollbar);
490 gtk_widget_realize(window);
491 gtk_widget_realize(scrollbar);
492
493 // Draw scrollbar thumb part and track into offscreen image
494 const int kWidth = 100;
495 const int kHeight = 20;
496 GtkStyle* style = gtk_rc_get_style(scrollbar);
497 GdkWindow* gdk_window = gtk_widget_get_window(window);
498 GdkPixmap* pm = gdk_pixmap_new(gdk_window, kWidth, kHeight, -1);
499 GdkRectangle rect = { 0, 0, kWidth, kHeight };
500 unsigned char data[3 * kWidth * kHeight];
501 for (int i = 0; i < 3; ++i) {
502 if (i < 2) {
503 // Thumb part
504 gtk_paint_slider(style, pm,
505 i == 0 ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
506 GTK_SHADOW_OUT, &rect, scrollbar, "slider", 0, 0,
507 kWidth, kHeight, GTK_ORIENTATION_HORIZONTAL);
508 } else {
509 // Track
510 gtk_paint_box(style, pm, GTK_STATE_ACTIVE, GTK_SHADOW_IN, &rect,
511 scrollbar, "trough-upper", 0, 0, kWidth, kHeight);
512 }
513 GdkPixbuf* pb = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB,
514 FALSE, 8, kWidth, kHeight,
515 3 * kWidth, 0, 0);
516 gdk_pixbuf_get_from_drawable(pb, pm, NULL, 0, 0, 0, 0, kWidth, kHeight);
517
518 // Sample pixels
519 int components[3] = { 0 };
520 for (int y = 2; y < kHeight - 2; ++y) {
521 for (int c = 0; c < 3; ++c) {
522 // Sample a vertical slice of pixels at about one-thirds from the
523 // left edge. This allows us to avoid any fixed graphics that might be
524 // located at the edges or in the center of the scrollbar.
525 // Each pixel is made up of a red, green, and blue component; taking up
526 // a total of three bytes.
527 components[c] += data[3 * (kWidth / 3 + y * kWidth) + c];
528 }
529 }
530 GdkColor* color = i == 0 ? thumb_active_color :
531 i == 1 ? thumb_inactive_color :
532 track_color;
533 color->pixel = 0;
534 // We sampled pixels across the full height of the image, ignoring a two
535 // pixel border. In some themes, the border has a completely different
536 // color which we do not want to factor into our average color computation.
537 //
538 // We now need to scale the colors from the 0..255 range, to the wider
539 // 0..65535 range, and we need to actually compute the average color; so,
540 // we divide by the total number of pixels in the sample.
541 color->red = components[0] * 65535 / (255 * (kHeight - 4));
542 color->green = components[1] * 65535 / (255 * (kHeight - 4));
543 color->blue = components[2] * 65535 / (255 * (kHeight - 4));
544
545 g_object_unref(pb);
546 }
547 g_object_unref(pm);
548
549 gtk_widget_destroy(window);
550
551 // Override any of the default colors with ones that were specified by the
552 // theme.
553 if (theme_thumb_active)
554 *thumb_active_color = *theme_thumb_active;
555
556 if (theme_thumb_inactive)
557 *thumb_inactive_color = *theme_thumb_inactive;
558
559 if (theme_trough_color)
560 *track_color = *theme_trough_color;
561 }
562
563 // static
564 gfx::Image* ThemeServiceGtk::GetFolderIcon(bool native) {
565 if (native) {
566 if (!icon_widget_)
567 icon_widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
568 // We never release our ref, so we will leak this on program shutdown.
569 if (!default_folder_icon_) {
570 GdkPixbuf* pixbuf = gtk_widget_render_icon(
571 icon_widget_, GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL);
572 if (pixbuf)
573 default_folder_icon_ = new gfx::Image(pixbuf);
574 }
575 if (default_folder_icon_)
576 return default_folder_icon_;
577 }
578
579 return &ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
580 IDR_BOOKMARK_BAR_FOLDER);
581 }
582
583 // static
584 gfx::Image* ThemeServiceGtk::GetDefaultFavicon(bool native) {
585 if (native) {
586 if (!icon_widget_)
587 icon_widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
588 // We never release our ref, so we will leak this on program shutdown.
589 if (!default_bookmark_icon_) {
590 GdkPixbuf* pixbuf = gtk_widget_render_icon(
591 icon_widget_, GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
592 if (pixbuf)
593 default_bookmark_icon_ = new gfx::Image(pixbuf);
594 }
595 if (default_bookmark_icon_)
596 return default_bookmark_icon_;
597 }
598
599 return &ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
600 IDR_DEFAULT_FAVICON);
601 }
602
603 // static
604 bool ThemeServiceGtk::DefaultUsesSystemTheme() {
605 scoped_ptr<base::Environment> env(base::Environment::Create());
606
607 switch (base::nix::GetDesktopEnvironment(env.get())) {
608 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
609 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
610 return true;
611 default:
612 return false;
613 }
614 }
615
616 void ThemeServiceGtk::ClearAllThemeData() {
617 colors_.clear();
618 tints_.clear();
619
620 ThemeService::ClearAllThemeData();
621 }
622
623 void ThemeServiceGtk::LoadThemePrefs() {
624 if (use_gtk_) {
625 LoadGtkValues();
626 } else {
627 LoadDefaultValues();
628 ThemeService::LoadThemePrefs();
629 }
630
631 RebuildMenuIconSets();
632 }
633
634 void ThemeServiceGtk::NotifyThemeChanged() {
635 ThemeService::NotifyThemeChanged();
636
637 // Notify all GtkChromeButtons of their new rendering mode:
638 for (std::vector<GtkWidget*>::iterator it = chrome_buttons_.begin();
639 it != chrome_buttons_.end(); ++it) {
640 gtk_chrome_button_set_use_gtk_rendering(
641 GTK_CHROME_BUTTON(*it), use_gtk_);
642 }
643
644 for (std::vector<GtkWidget*>::iterator it = link_buttons_.begin();
645 it != link_buttons_.end(); ++it) {
646 gtk_chrome_link_button_set_use_gtk_theme(
647 GTK_CHROME_LINK_BUTTON(*it), use_gtk_);
648 }
649
650 for (std::map<GtkWidget*, GdkColor>::iterator it = labels_.begin();
651 it != labels_.end(); ++it) {
652 const GdkColor* color = use_gtk_ ? NULL : &it->second;
653 gtk_util::SetLabelColor(it->first, color);
654 }
655
656 Browser* browser = BrowserList::GetLastActive();
657 if (browser && browser->window()) {
658 GtkWindow* window = browser->window()->GetNativeHandle();
659 gtk_util::SetDefaultWindowIcon(window);
660 gtk_util::SetWindowIcon(window, browser->profile());
661 }
662 }
663
664 void ThemeServiceGtk::FreePlatformCaches() {
665 ThemeService::FreePlatformCaches();
666 STLDeleteValues(&gtk_images_);
667 }
668
669 void ThemeServiceGtk::OnStyleSet(GtkWidget* widget,
670 GtkStyle* previous_style) {
671 gfx::Image* default_folder_icon = default_folder_icon_;
672 gfx::Image* default_bookmark_icon = default_bookmark_icon_;
673 default_folder_icon_ = NULL;
674 default_bookmark_icon_ = NULL;
675
676 if (profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme)) {
677 ClearAllThemeData();
678 LoadGtkValues();
679 NotifyThemeChanged();
680 }
681
682 RebuildMenuIconSets();
683
684 // Free the old icons only after the theme change notification has gone
685 // through.
686 if (default_folder_icon)
687 delete default_folder_icon;
688 if (default_bookmark_icon)
689 delete default_bookmark_icon;
690 }
691
692 void ThemeServiceGtk::LoadGtkValues() {
693 // Before we start setting images and values, we have to clear out old, stale
694 // values. (If we don't do this, we'll regress startup time in the case where
695 // someone installs a heavyweight theme, then goes back to GTK.)
696 profile()->GetPrefs()->ClearPref(prefs::kCurrentThemeImages);
697
698 GtkStyle* frame_style = gtk_rc_get_style(fake_frame_);
699
700 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
701 SetThemeColorFromGtk(ThemeService::COLOR_CONTROL_BACKGROUND,
702 &window_style->bg[GTK_STATE_NORMAL]);
703
704 GdkColor toolbar_color = window_style->bg[GTK_STATE_NORMAL];
705 SetThemeColorFromGtk(ThemeService::COLOR_TOOLBAR, &toolbar_color);
706
707 GdkColor button_color = window_style->bg[GTK_STATE_SELECTED];
708 SetThemeTintFromGtk(ThemeService::TINT_BUTTONS, &button_color);
709
710 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get());
711 GdkColor label_color = label_style->fg[GTK_STATE_NORMAL];
712 SetThemeColorFromGtk(ThemeService::COLOR_TAB_TEXT, &label_color);
713 SetThemeColorFromGtk(ThemeService::COLOR_BOOKMARK_TEXT, &label_color);
714
715 // Build the various icon tints.
716 GetNormalButtonTintHSL(&button_tint_);
717 GetNormalEntryForegroundHSL(&entry_tint_);
718 GetSelectedEntryForegroundHSL(&selected_entry_tint_);
719 GdkColor frame_color = BuildFrameColors(frame_style);
720
721 // The inactive frame color never occurs naturally in the theme, as it is a
722 // tinted version of |frame_color|. We generate another color based on the
723 // background tab color, with the lightness and saturation moved in the
724 // opposite direction. (We don't touch the hue, since there should be subtle
725 // hints of the color in the text.)
726 color_utils::HSL inactive_tab_text_hsl = tints_[TINT_BACKGROUND_TAB];
727 if (inactive_tab_text_hsl.l < 0.5)
728 inactive_tab_text_hsl.l = kDarkInactiveLuminance;
729 else
730 inactive_tab_text_hsl.l = kLightInactiveLuminance;
731
732 if (inactive_tab_text_hsl.s < 0.5)
733 inactive_tab_text_hsl.s = kHeavyInactiveSaturation;
734 else
735 inactive_tab_text_hsl.s = kLightInactiveSaturation;
736
737 colors_[ThemeService::COLOR_BACKGROUND_TAB_TEXT] =
738 color_utils::HSLToSkColor(inactive_tab_text_hsl, 255);
739
740 // We pick the text and background colors for the NTP out of the colors for a
741 // GtkEntry. We do this because GtkEntries background color is never the same
742 // as |toolbar_color|, is usually a white, and when it isn't a white,
743 // provides sufficient contrast to |toolbar_color|. Try this out with
744 // Darklooks, HighContrastInverse or ThinIce.
745 GtkStyle* entry_style = gtk_rc_get_style(fake_entry_.get());
746 GdkColor ntp_background = entry_style->base[GTK_STATE_NORMAL];
747 GdkColor ntp_foreground = entry_style->text[GTK_STATE_NORMAL];
748 SetThemeColorFromGtk(ThemeService::COLOR_NTP_BACKGROUND,
749 &ntp_background);
750 SetThemeColorFromGtk(ThemeService::COLOR_NTP_TEXT,
751 &ntp_foreground);
752
753 // The NTP header is the color that surrounds the current active thumbnail on
754 // the NTP, and acts as the border of the "Recent Links" box. It would be
755 // awesome if they were separated so we could use GetBorderColor() for the
756 // border around the "Recent Links" section, but matching the frame color is
757 // more important.
758 SetThemeColorFromGtk(ThemeService::COLOR_NTP_HEADER,
759 &frame_color);
760 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION,
761 &toolbar_color);
762 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION_TEXT,
763 &label_color);
764
765 // Override the link color if the theme provides it.
766 const GdkColor* link_color = NULL;
767 gtk_widget_style_get(GTK_WIDGET(fake_window_),
768 "link-color", &link_color, NULL);
769
770 bool is_default_link_color = false;
771 if (!link_color) {
772 link_color = &kDefaultLinkColor;
773 is_default_link_color = true;
774 }
775
776 SetThemeColorFromGtk(ThemeService::COLOR_NTP_LINK,
777 link_color);
778 SetThemeColorFromGtk(ThemeService::COLOR_NTP_LINK_UNDERLINE,
779 link_color);
780 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION_LINK,
781 link_color);
782 SetThemeColorFromGtk(ThemeService::COLOR_NTP_SECTION_LINK_UNDERLINE,
783 link_color);
784
785 if (link_color && !is_default_link_color)
786 gdk_color_free(const_cast<GdkColor*>(link_color));
787
788 // Generate the colors that we pass to WebKit.
789 focus_ring_color_ = gfx::GdkColorToSkColor(frame_color);
790 GdkColor thumb_active_color, thumb_inactive_color, track_color;
791 ThemeServiceGtk::GetScrollbarColors(&thumb_active_color,
792 &thumb_inactive_color,
793 &track_color);
794 thumb_active_color_ = gfx::GdkColorToSkColor(thumb_active_color);
795 thumb_inactive_color_ = gfx::GdkColorToSkColor(thumb_inactive_color);
796 track_color_ = gfx::GdkColorToSkColor(track_color);
797
798 // Some GTK themes only define the text selection colors on the GtkEntry
799 // class, so we need to use that for getting selection colors.
800 active_selection_bg_color_ =
801 gfx::GdkColorToSkColor(entry_style->base[GTK_STATE_SELECTED]);
802 active_selection_fg_color_ =
803 gfx::GdkColorToSkColor(entry_style->text[GTK_STATE_SELECTED]);
804 inactive_selection_bg_color_ =
805 gfx::GdkColorToSkColor(entry_style->base[GTK_STATE_ACTIVE]);
806 inactive_selection_fg_color_ =
807 gfx::GdkColorToSkColor(entry_style->text[GTK_STATE_ACTIVE]);
808 }
809
810 GdkColor ThemeServiceGtk::BuildFrameColors(GtkStyle* frame_style) {
811 GdkColor* theme_frame = NULL;
812 GdkColor* theme_inactive_frame = NULL;
813 GdkColor* theme_incognito_frame = NULL;
814 GdkColor* theme_incognito_inactive_frame = NULL;
815 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
816 "frame-color", &theme_frame,
817 "inactive-frame-color", &theme_inactive_frame,
818 "incognito-frame-color", &theme_incognito_frame,
819 "incognito-inactive-frame-color",
820 &theme_incognito_inactive_frame,
821 NULL);
822
823 GdkColor frame_color = BuildAndSetFrameColor(
824 &frame_style->bg[GTK_STATE_SELECTED],
825 theme_frame,
826 kDefaultFrameShift,
827 ThemeService::COLOR_FRAME,
828 ThemeService::TINT_FRAME);
829 if (theme_frame)
830 gdk_color_free(theme_frame);
831 SetThemeTintFromGtk(ThemeService::TINT_BACKGROUND_TAB, &frame_color);
832
833 BuildAndSetFrameColor(
834 &frame_style->bg[GTK_STATE_INSENSITIVE],
835 theme_inactive_frame,
836 kDefaultFrameShift,
837 ThemeService::COLOR_FRAME_INACTIVE,
838 ThemeService::TINT_FRAME_INACTIVE);
839 if (theme_inactive_frame)
840 gdk_color_free(theme_inactive_frame);
841
842 BuildAndSetFrameColor(
843 &frame_color,
844 theme_incognito_frame,
845 GetDefaultTint(ThemeService::TINT_FRAME_INCOGNITO),
846 ThemeService::COLOR_FRAME_INCOGNITO,
847 ThemeService::TINT_FRAME_INCOGNITO);
848 if (theme_incognito_frame)
849 gdk_color_free(theme_incognito_frame);
850
851 BuildAndSetFrameColor(
852 &frame_color,
853 theme_incognito_inactive_frame,
854 GetDefaultTint(ThemeService::TINT_FRAME_INCOGNITO_INACTIVE),
855 ThemeService::COLOR_FRAME_INCOGNITO_INACTIVE,
856 ThemeService::TINT_FRAME_INCOGNITO_INACTIVE);
857 if (theme_incognito_inactive_frame)
858 gdk_color_free(theme_incognito_inactive_frame);
859
860 return frame_color;
861 }
862
863 void ThemeServiceGtk::LoadDefaultValues() {
864 focus_ring_color_ = SkColorSetARGB(255, 229, 151, 0);
865 thumb_active_color_ = SkColorSetRGB(244, 244, 244);
866 thumb_inactive_color_ = SkColorSetRGB(234, 234, 234);
867 track_color_ = SkColorSetRGB(211, 211, 211);
868
869 active_selection_bg_color_ = SkColorSetRGB(30, 144, 255);
870 active_selection_fg_color_ = SK_ColorWHITE;
871 inactive_selection_bg_color_ = SkColorSetRGB(200, 200, 200);
872 inactive_selection_fg_color_ = SkColorSetRGB(50, 50, 50);
873 }
874
875 void ThemeServiceGtk::RebuildMenuIconSets() {
876 FreeIconSets();
877
878 GtkStyle* style = gtk_rc_get_style(fake_menu_item_.get());
879
880 fullscreen_icon_set_ = gtk_icon_set_new();
881 BuildIconFromIDRWithColor(IDR_FULLSCREEN_MENU_BUTTON,
882 style,
883 GTK_STATE_PRELIGHT,
884 fullscreen_icon_set_);
885 BuildIconFromIDRWithColor(IDR_FULLSCREEN_MENU_BUTTON,
886 style,
887 GTK_STATE_NORMAL,
888 fullscreen_icon_set_);
889 }
890
891 void ThemeServiceGtk::SetThemeColorFromGtk(int id, const GdkColor* color) {
892 colors_[id] = gfx::GdkColorToSkColor(*color);
893 }
894
895 void ThemeServiceGtk::SetThemeTintFromGtk(int id, const GdkColor* color) {
896 color_utils::HSL default_tint = GetDefaultTint(id);
897 color_utils::HSL hsl;
898 color_utils::SkColorToHSL(gfx::GdkColorToSkColor(*color), &hsl);
899
900 if (default_tint.s != -1)
901 hsl.s = default_tint.s;
902
903 if (default_tint.l != -1)
904 hsl.l = default_tint.l;
905
906 tints_[id] = hsl;
907 }
908
909 GdkColor ThemeServiceGtk::BuildAndSetFrameColor(const GdkColor* base,
910 const GdkColor* gtk_base,
911 const color_utils::HSL& tint,
912 int color_id,
913 int tint_id) {
914 GdkColor out_color = *base;
915 if (gtk_base) {
916 // The theme author specified a color to use, use it without modification.
917 out_color = *gtk_base;
918 } else {
919 // Tint the basic color since this is a heuristic color instead of one
920 // specified by the theme author.
921 GdkColorHSLShift(tint, &out_color);
922 }
923 SetThemeColorFromGtk(color_id, &out_color);
924 SetThemeTintFromGtk(tint_id, &out_color);
925
926 return out_color;
927 }
928
929 void ThemeServiceGtk::FreeIconSets() {
930 if (fullscreen_icon_set_) {
931 gtk_icon_set_unref(fullscreen_icon_set_);
932 fullscreen_icon_set_ = NULL;
933 }
934 }
935
936 SkBitmap* ThemeServiceGtk::GenerateGtkThemeBitmap(int id) const {
937 switch (id) {
938 case IDR_THEME_TOOLBAR: {
939 GtkStyle* style = gtk_rc_get_style(fake_window_);
940 GdkColor* color = &style->bg[GTK_STATE_NORMAL];
941 SkBitmap* bitmap = new SkBitmap;
942 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
943 kToolbarImageWidth, kToolbarImageHeight);
944 bitmap->allocPixels();
945 bitmap->eraseRGB(color->red >> 8, color->green >> 8, color->blue >> 8);
946 return bitmap;
947 }
948 case IDR_THEME_TAB_BACKGROUND:
949 return GenerateTabImage(IDR_THEME_FRAME);
950 case IDR_THEME_TAB_BACKGROUND_INCOGNITO:
951 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO);
952 case IDR_THEME_FRAME:
953 return GenerateFrameImage(ThemeService::COLOR_FRAME,
954 "frame-gradient-color");
955 case IDR_THEME_FRAME_INACTIVE:
956 return GenerateFrameImage(ThemeService::COLOR_FRAME_INACTIVE,
957 "inactive-frame-gradient-color");
958 case IDR_THEME_FRAME_INCOGNITO:
959 return GenerateFrameImage(ThemeService::COLOR_FRAME_INCOGNITO,
960 "incognito-frame-gradient-color");
961 case IDR_THEME_FRAME_INCOGNITO_INACTIVE: {
962 return GenerateFrameImage(
963 ThemeService::COLOR_FRAME_INCOGNITO_INACTIVE,
964 "incognito-inactive-frame-gradient-color");
965 }
966 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and
967 // instead should tint based on the foreground text entry color in GTK+
968 // mode because some themes that try to be dark *and* light have very
969 // different colors between the omnibox and the normal background area.
970 case IDR_OMNIBOX_EXTENSION_APP:
971 case IDR_OMNIBOX_HISTORY:
972 case IDR_OMNIBOX_HTTP:
973 case IDR_OMNIBOX_SEARCH:
974 case IDR_OMNIBOX_STAR:
975 case IDR_OMNIBOX_TTS:
976 case IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON:
977 case IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON: {
978 return GenerateTintedIcon(id, entry_tint_);
979 }
980 // In GTK mode, the dark versions of the omnibox icons only ever appear in
981 // the autocomplete popup and only against the current theme's GtkEntry
982 // base[GTK_STATE_SELECTED] color, so tint the icons so they won't collide
983 // with the selected color.
984 case IDR_OMNIBOX_EXTENSION_APP_DARK:
985 case IDR_OMNIBOX_HISTORY_DARK:
986 case IDR_OMNIBOX_HTTP_DARK:
987 case IDR_OMNIBOX_SEARCH_DARK:
988 case IDR_OMNIBOX_STAR_DARK:
989 case IDR_OMNIBOX_TTS_DARK: {
990 return GenerateTintedIcon(id, selected_entry_tint_);
991 }
992 default: {
993 return GenerateTintedIcon(id, button_tint_);
994 }
995 }
996 }
997
998 SkBitmap* ThemeServiceGtk::GenerateFrameImage(
999 int color_id,
1000 const char* gradient_name) const {
1001 // We use two colors: the main color (passed in) and a lightened version of
1002 // that color (which is supposed to match the light gradient at the top of
1003 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird).
1004 ColorMap::const_iterator it = colors_.find(color_id);
1005 DCHECK(it != colors_.end());
1006 SkColor base = it->second;
1007
1008 gfx::Canvas canvas(gfx::Size(kToolbarImageWidth, kToolbarImageHeight), true);
1009
1010 int gradient_size;
1011 GdkColor* gradient_top_color = NULL;
1012 gtk_widget_style_get(GTK_WIDGET(fake_frame_),
1013 "frame-gradient-size", &gradient_size,
1014 gradient_name, &gradient_top_color,
1015 NULL);
1016 if (gradient_size) {
1017 SkColor lighter = gradient_top_color ?
1018 gfx::GdkColorToSkColor(*gradient_top_color) :
1019 color_utils::HSLShift(base, kGtkFrameShift);
1020 if (gradient_top_color)
1021 gdk_color_free(gradient_top_color);
1022 SkShader* shader = gfx::CreateGradientShader(
1023 0, gradient_size, lighter, base);
1024 SkPaint paint;
1025 paint.setStyle(SkPaint::kFill_Style);
1026 paint.setAntiAlias(true);
1027 paint.setShader(shader);
1028 shader->unref();
1029
1030 canvas.DrawRect(gfx::Rect(0, 0, kToolbarImageWidth, gradient_size), paint);
1031 }
1032
1033 canvas.FillRect(gfx::Rect(0, gradient_size, kToolbarImageWidth,
1034 kToolbarImageHeight - gradient_size), base);
1035 return new SkBitmap(canvas.ExtractBitmap());
1036 }
1037
1038 SkBitmap* ThemeServiceGtk::GenerateTabImage(int base_id) const {
1039 SkBitmap* base_image = GetBitmapNamed(base_id);
1040 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap(
1041 *base_image, GetTint(ThemeService::TINT_BACKGROUND_TAB));
1042 return new SkBitmap(SkBitmapOperations::CreateTiledBitmap(
1043 bg_tint, 0, 0, bg_tint.width(), bg_tint.height()));
1044 }
1045
1046 SkBitmap* ThemeServiceGtk::GenerateTintedIcon(
1047 int base_id,
1048 const color_utils::HSL& tint) const {
1049 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
1050 return new SkBitmap(SkBitmapOperations::CreateHSLShiftedBitmap(
1051 *rb.GetBitmapNamed(base_id), tint));
1052 }
1053
1054 void ThemeServiceGtk::GetNormalButtonTintHSL(
1055 color_utils::HSL* tint) const {
1056 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
1057 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED];
1058 const GdkColor base_color = window_style->base[GTK_STATE_NORMAL];
1059
1060 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get());
1061 const GdkColor text_color = label_style->fg[GTK_STATE_NORMAL];
1062
1063 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint);
1064 }
1065
1066 void ThemeServiceGtk::GetNormalEntryForegroundHSL(
1067 color_utils::HSL* tint) const {
1068 GtkStyle* window_style = gtk_rc_get_style(fake_window_);
1069 const GdkColor accent_gdk_color = window_style->bg[GTK_STATE_SELECTED];
1070
1071 GtkStyle* style = gtk_rc_get_style(fake_entry_.get());
1072 const GdkColor text_color = style->text[GTK_STATE_NORMAL];
1073 const GdkColor base_color = style->base[GTK_STATE_NORMAL];
1074
1075 PickButtonTintFromColors(accent_gdk_color, text_color, base_color, tint);
1076 }
1077
1078 void ThemeServiceGtk::GetSelectedEntryForegroundHSL(
1079 color_utils::HSL* tint) const {
1080 // The simplest of all the tints. We just use the selected text in the entry
1081 // since the icons tinted this way will only be displayed against
1082 // base[GTK_STATE_SELECTED].
1083 GtkStyle* style = gtk_rc_get_style(fake_entry_.get());
1084 const GdkColor color = style->text[GTK_STATE_SELECTED];
1085 color_utils::SkColorToHSL(gfx::GdkColorToSkColor(color), tint);
1086 }
1087
1088 void ThemeServiceGtk::OnDestroyChromeButton(GtkWidget* button) {
1089 std::vector<GtkWidget*>::iterator it =
1090 find(chrome_buttons_.begin(), chrome_buttons_.end(), button);
1091 if (it != chrome_buttons_.end())
1092 chrome_buttons_.erase(it);
1093 }
1094
1095 void ThemeServiceGtk::OnDestroyChromeLinkButton(GtkWidget* button) {
1096 std::vector<GtkWidget*>::iterator it =
1097 find(link_buttons_.begin(), link_buttons_.end(), button);
1098 if (it != link_buttons_.end())
1099 link_buttons_.erase(it);
1100 }
1101
1102 void ThemeServiceGtk::OnDestroyLabel(GtkWidget* button) {
1103 std::map<GtkWidget*, GdkColor>::iterator it = labels_.find(button);
1104 if (it != labels_.end())
1105 labels_.erase(it);
1106 }
1107
1108 gboolean ThemeServiceGtk::OnSeparatorExpose(GtkWidget* widget,
1109 GdkEventExpose* event) {
1110 UNSHIPPED_TRACE_EVENT0("ui::gtk", "ThemeServiceGtk::OnSeparatorExpose");
1111 if (UsingNativeTheme())
1112 return FALSE;
1113
1114 cairo_t* cr = gdk_cairo_create(gtk_widget_get_window(widget));
1115 gdk_cairo_rectangle(cr, &event->area);
1116 cairo_clip(cr);
1117
1118 GdkColor bottom_color = GetGdkColor(ThemeService::COLOR_TOOLBAR);
1119 double bottom_color_rgb[] = {
1120 static_cast<double>(bottom_color.red / 257) / 255.0,
1121 static_cast<double>(bottom_color.green / 257) / 255.0,
1122 static_cast<double>(bottom_color.blue / 257) / 255.0, };
1123
1124 GtkAllocation allocation;
1125 gtk_widget_get_allocation(widget, &allocation);
1126
1127 cairo_pattern_t* pattern =
1128 cairo_pattern_create_linear(allocation.x, allocation.y,
1129 allocation.x,
1130 allocation.y + allocation.height);
1131 cairo_pattern_add_color_stop_rgb(
1132 pattern, 0.0,
1133 kTopSeparatorColor[0], kTopSeparatorColor[1], kTopSeparatorColor[2]);
1134 cairo_pattern_add_color_stop_rgb(
1135 pattern, 0.5,
1136 kMidSeparatorColor[0], kMidSeparatorColor[1], kMidSeparatorColor[2]);
1137 cairo_pattern_add_color_stop_rgb(
1138 pattern, 1.0,
1139 bottom_color_rgb[0], bottom_color_rgb[1], bottom_color_rgb[2]);
1140 cairo_set_source(cr, pattern);
1141
1142 double start_x = 0.5 + allocation.x;
1143 cairo_new_path(cr);
1144 cairo_set_line_width(cr, 1.0);
1145 cairo_move_to(cr, start_x, allocation.y);
1146 cairo_line_to(cr, start_x, allocation.y + allocation.height);
1147 cairo_stroke(cr);
1148 cairo_destroy(cr);
1149 cairo_pattern_destroy(pattern);
1150
1151 return TRUE;
1152 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/gtk/theme_service_gtk.h ('k') | chrome/browser/ui/gtk/theme_service_gtk_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698