gtk_util.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/ui/gtk_util.h"
  5. #include <gdk/gdk.h>
  6. #include <gtk/gtk.h>
  7. #include <stdint.h>
  8. #include "base/macros/remove_parens.h"
  9. #include "base/strings/stringize_macros.h"
  10. #include "electron/electron_gtk_stubs.h"
  11. #include "third_party/skia/include/core/SkBitmap.h"
  12. #include "third_party/skia/include/core/SkColor.h"
  13. #include "third_party/skia/include/core/SkUnPreMultiply.h"
  14. // The following utilities are pulled from
  15. // https://source.chromium.org/chromium/chromium/src/+/main:ui/gtk/select_file_dialog_linux_gtk.cc;l=44-75;drc=a03ba4ca94f75531207c3ea832d6a605cde77394
  16. namespace gtk_util {
  17. namespace {
  18. const char* GtkGettext(const char* str) {
  19. // ex: "gtk30". GTK_MAJOR_VERSION is #defined as an int in parenthesis,
  20. // like (3), so use base macros to remove parenthesis and stringize it
  21. static const char kGettextDomain[] =
  22. "gtk" STRINGIZE(BASE_REMOVE_PARENS(GTK_MAJOR_VERSION)) "0";
  23. return g_dgettext(kGettextDomain, str);
  24. }
  25. } // namespace
  26. const char* GetCancelLabel() {
  27. static const char* cancel = GtkGettext("_Cancel");
  28. return cancel;
  29. }
  30. const char* GetOpenLabel() {
  31. static const char* open = GtkGettext("_Open");
  32. return open;
  33. }
  34. const char* GetSaveLabel() {
  35. static const char* save = GtkGettext("_Save");
  36. return save;
  37. }
  38. const char* GetOkLabel() {
  39. static const char* ok = GtkGettext("_Ok");
  40. return ok;
  41. }
  42. const char* GetNoLabel() {
  43. static const char* no = GtkGettext("_No");
  44. return no;
  45. }
  46. const char* GetYesLabel() {
  47. static const char* yes = GtkGettext("_Yes");
  48. return yes;
  49. }
  50. GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap& bitmap) {
  51. if (bitmap.isNull())
  52. return {};
  53. constexpr int kBytesPerPixel = 4;
  54. const auto [width, height] = bitmap.dimensions();
  55. std::vector<uint8_t> bytes;
  56. bytes.reserve(width * height * kBytesPerPixel);
  57. for (int y = 0; y < height; ++y) {
  58. for (int x = 0; x < width; ++x) {
  59. const SkColor pixel = bitmap.getColor(x, y);
  60. bytes.emplace_back(SkColorGetR(pixel));
  61. bytes.emplace_back(SkColorGetG(pixel));
  62. bytes.emplace_back(SkColorGetB(pixel));
  63. bytes.emplace_back(SkColorGetA(pixel));
  64. }
  65. }
  66. constexpr GdkColorspace kColorspace = GDK_COLORSPACE_RGB;
  67. constexpr gboolean kHasAlpha = true;
  68. constexpr int kBitsPerSample = 8;
  69. return gdk_pixbuf_new_from_bytes(
  70. g_bytes_new(std::data(bytes), std::size(bytes)), kColorspace, kHasAlpha,
  71. kBitsPerSample, width, height,
  72. gdk_pixbuf_calculate_rowstride(kColorspace, kHasAlpha, kBitsPerSample,
  73. width, height));
  74. }
  75. } // namespace gtk_util