OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/libgtkui/gtk_util.h" | 5 #include "chrome/browser/ui/libgtkui/gtk_util.h" |
6 | 6 |
7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
8 #include <gdk/gdk.h> | 8 #include <gdk/gdk.h> |
9 #include <gdk/gdkx.h> | 9 #include <gdk/gdkx.h> |
10 #include <gtk/gtk.h> | 10 #include <gtk/gtk.h> |
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 child_state = static_cast<GtkStateFlags>( | 416 child_state = static_cast<GtkStateFlags>( |
417 child_state | gtk_style_context_get_state(context)); | 417 child_state | gtk_style_context_get_state(context)); |
418 } | 418 } |
419 gtk_style_context_set_state(child_context, child_state); | 419 gtk_style_context_set_state(child_context, child_state); |
420 } | 420 } |
421 gtk_style_context_set_parent(child_context, context); | 421 gtk_style_context_set_parent(child_context, context); |
422 gtk_widget_path_unref(path); | 422 gtk_widget_path_unref(path); |
423 return child_context; | 423 return child_context; |
424 } | 424 } |
425 | 425 |
426 ScopedStyleContext GetStyleContextFromCss(const char* css_selector) { | 426 ScopedStyleContext GetStyleContextFromCss(const std::string& css_selector) { |
427 // Prepend a window node to the selector since all widgets must live | 427 // Prepend a window node to the selector since all widgets must live |
428 // in a window, but we don't want to specify that every time. | 428 // in a window, but we don't want to specify that every time. |
429 auto context = | 429 auto context = |
430 AppendCssNodeToStyleContext(nullptr, "GtkWindow#window.background"); | 430 AppendCssNodeToStyleContext(nullptr, "GtkWindow#window.background"); |
431 | 431 |
432 for (const auto& widget_type : | 432 for (const auto& widget_type : |
433 base::SplitString(css_selector, base::kWhitespaceASCII, | 433 base::SplitString(css_selector, base::kWhitespaceASCII, |
434 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) { | 434 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) { |
435 context = AppendCssNodeToStyleContext(context, widget_type); | 435 context = AppendCssNodeToStyleContext(context, widget_type); |
436 } | 436 } |
(...skipping 24 matching lines...) Expand all Loading... |
461 "border-radius: 0px;" | 461 "border-radius: 0px;" |
462 "border-style: none;" | 462 "border-style: none;" |
463 "box-shadow: none;" | 463 "box-shadow: none;" |
464 "}"); | 464 "}"); |
465 gfx::Size size(24, 24); | 465 gfx::Size size(24, 24); |
466 CairoSurface surface(size); | 466 CairoSurface surface(size); |
467 RenderBackground(size, surface.cairo(), context); | 467 RenderBackground(size, surface.cairo(), context); |
468 return surface.GetAveragePixelValue(false); | 468 return surface.GetAveragePixelValue(false); |
469 } | 469 } |
470 | 470 |
471 SkColor GetFgColor(const char* css_selector) { | 471 SkColor GetFgColor(const std::string& css_selector) { |
472 return GetFgColorFromStyleContext(GetStyleContextFromCss(css_selector)); | 472 return GetFgColorFromStyleContext(GetStyleContextFromCss(css_selector)); |
473 } | 473 } |
474 | 474 |
475 ScopedCssProvider GetCssProvider(const char* css) { | 475 ScopedCssProvider GetCssProvider(const std::string& css) { |
476 GtkCssProvider* provider = gtk_css_provider_new(); | 476 GtkCssProvider* provider = gtk_css_provider_new(); |
477 GError* error = nullptr; | 477 GError* error = nullptr; |
478 gtk_css_provider_load_from_data(provider, css, -1, &error); | 478 gtk_css_provider_load_from_data(provider, css.c_str(), -1, &error); |
479 DCHECK(!error); | 479 DCHECK(!error); |
480 return ScopedCssProvider(provider); | 480 return ScopedCssProvider(provider); |
481 } | 481 } |
482 | 482 |
483 void ApplyCssProviderToContext(GtkStyleContext* context, | 483 void ApplyCssProviderToContext(GtkStyleContext* context, |
484 GtkCssProvider* provider) { | 484 GtkCssProvider* provider) { |
485 while (context) { | 485 while (context) { |
486 gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), | 486 gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), |
487 G_MAXUINT); | 487 G_MAXUINT); |
488 context = gtk_style_context_get_parent(context); | 488 context = gtk_style_context_get_parent(context); |
489 } | 489 } |
490 } | 490 } |
491 | 491 |
492 void ApplyCssToContext(GtkStyleContext* context, const char* css) { | 492 void ApplyCssToContext(GtkStyleContext* context, const std::string& css) { |
493 auto provider = GetCssProvider(css); | 493 auto provider = GetCssProvider(css); |
494 ApplyCssProviderToContext(context, provider); | 494 ApplyCssProviderToContext(context, provider); |
495 } | 495 } |
496 | 496 |
497 void RenderBackground(const gfx::Size& size, | 497 void RenderBackground(const gfx::Size& size, |
498 cairo_t* cr, | 498 cairo_t* cr, |
499 GtkStyleContext* context) { | 499 GtkStyleContext* context) { |
500 if (!context) | 500 if (!context) |
501 return; | 501 return; |
502 RenderBackground(size, cr, gtk_style_context_get_parent(context)); | 502 RenderBackground(size, cr, gtk_style_context_get_parent(context)); |
503 gtk_render_background(context, cr, 0, 0, size.width(), size.height()); | 503 gtk_render_background(context, cr, 0, 0, size.width(), size.height()); |
504 } | 504 } |
505 | 505 |
506 SkColor GetBgColor(const char* css_selector) { | 506 SkColor GetBgColor(const std::string& css_selector) { |
507 return GetBgColorFromStyleContext(GetStyleContextFromCss(css_selector)); | 507 return GetBgColorFromStyleContext(GetStyleContextFromCss(css_selector)); |
508 } | 508 } |
509 | 509 |
510 SkColor GetBorderColor(const char* css_selector) { | 510 SkColor GetBorderColor(const std::string& css_selector) { |
511 // Borders have the same issue as backgrounds, due to the | 511 // Borders have the same issue as backgrounds, due to the |
512 // border-image property. | 512 // border-image property. |
513 auto context = GetStyleContextFromCss(css_selector); | 513 auto context = GetStyleContextFromCss(css_selector); |
514 gfx::Size size(24, 24); | 514 gfx::Size size(24, 24); |
515 CairoSurface surface(size); | 515 CairoSurface surface(size); |
516 gtk_render_frame(context, surface.cairo(), 0, 0, size.width(), size.height()); | 516 gtk_render_frame(context, surface.cairo(), 0, 0, size.width(), size.height()); |
517 return surface.GetAveragePixelValue(true); | 517 return surface.GetAveragePixelValue(true); |
518 } | 518 } |
519 | 519 |
520 SkColor GetSelectionBgColor(const char* css_selector) { | 520 SkColor GetSelectionBgColor(const std::string& css_selector) { |
521 auto context = GetStyleContextFromCss(css_selector); | 521 auto context = GetStyleContextFromCss(css_selector); |
522 if (GtkVersionCheck(3, 20)) | 522 if (GtkVersionCheck(3, 20)) |
523 return GetBgColorFromStyleContext(context); | 523 return GetBgColorFromStyleContext(context); |
524 // This is verbatim how Gtk gets the selection color on versions before 3.20. | 524 // This is verbatim how Gtk gets the selection color on versions before 3.20. |
525 GdkRGBA selection_color; | 525 GdkRGBA selection_color; |
526 G_GNUC_BEGIN_IGNORE_DEPRECATIONS; | 526 G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
527 gtk_style_context_get_background_color( | 527 gtk_style_context_get_background_color( |
528 context, gtk_style_context_get_state(context), &selection_color); | 528 context, gtk_style_context_get_state(context), &selection_color); |
529 G_GNUC_END_IGNORE_DEPRECATIONS; | 529 G_GNUC_END_IGNORE_DEPRECATIONS; |
530 return GdkRgbaToSkColor(selection_color); | 530 return GdkRgbaToSkColor(selection_color); |
531 } | 531 } |
532 | 532 |
533 SkColor GetSeparatorColor(const char* css_selector) { | 533 SkColor GetSeparatorColor(const std::string& css_selector) { |
534 if (!GtkVersionCheck(3, 20)) | 534 if (!GtkVersionCheck(3, 20)) |
535 return GetFgColor(css_selector); | 535 return GetFgColor(css_selector); |
536 | 536 |
537 auto context = GetStyleContextFromCss(css_selector); | 537 auto context = GetStyleContextFromCss(css_selector); |
538 int w = 1, h = 1; | 538 int w = 1, h = 1; |
539 gtk_style_context_get(context, gtk_style_context_get_state(context), | 539 gtk_style_context_get(context, gtk_style_context_get_state(context), |
540 "min-width", &w, "min-height", &h, nullptr); | 540 "min-width", &w, "min-height", &h, nullptr); |
541 GtkBorder border, padding; | 541 GtkBorder border, padding; |
542 GtkStateFlags state = gtk_style_context_get_state(context); | 542 GtkStateFlags state = gtk_style_context_get_state(context); |
543 gtk_style_context_get_border(context, state, &border); | 543 gtk_style_context_get_border(context, state, &border); |
(...skipping 12 matching lines...) Expand all Loading... |
556 } | 556 } |
557 | 557 |
558 CairoSurface surface(gfx::Size(w, h)); | 558 CairoSurface surface(gfx::Size(w, h)); |
559 gtk_render_background(context, surface.cairo(), 0, 0, w, h); | 559 gtk_render_background(context, surface.cairo(), 0, 0, w, h); |
560 gtk_render_frame(context, surface.cairo(), 0, 0, w, h); | 560 gtk_render_frame(context, surface.cairo(), 0, 0, w, h); |
561 return surface.GetAveragePixelValue(false); | 561 return surface.GetAveragePixelValue(false); |
562 } | 562 } |
563 #endif | 563 #endif |
564 | 564 |
565 } // namespace libgtkui | 565 } // namespace libgtkui |
OLD | NEW |