diff options
Diffstat (limited to 'gfx/gtk_util.cc')
-rw-r--r-- | gfx/gtk_util.cc | 61 |
1 files changed, 56 insertions, 5 deletions
diff --git a/gfx/gtk_util.cc b/gfx/gtk_util.cc index bbed191..c45e133 100644 --- a/gfx/gtk_util.cc +++ b/gfx/gtk_util.cc @@ -20,15 +20,38 @@ void FreePixels(guchar* pixels, gpointer data) { free(data); } +// Common implementation of ConvertAcceleratorsFromWindowsStyle() and +// RemoveWindowsStyleAccelerators(). +// Replaces all ampersands (as used in our grd files to indicate mnemonics) +// to |target|. Similarly any underscores get replaced with two underscores as +// is needed by pango. +std::string ConvertAmperstandsTo(const std::string& label, + const std::string& target) { + std::string ret; + ret.reserve(label.length() * 2); + for (size_t i = 0; i < label.length(); ++i) { + if ('_' == label[i]) { + ret.push_back('_'); + ret.push_back('_'); + } else if ('&' == label[i]) { + if (i + 1 < label.length() && '&' == label[i + 1]) { + ret.push_back('&'); + ++i; + } else { + ret.append(target); + } + } else { + ret.push_back(label[i]); + } + } + + return ret; +} + } // namespace namespace gfx { -const GdkColor kGdkWhite = GDK_COLOR_RGB(0xff, 0xff, 0xff); -const GdkColor kGdkGray = GDK_COLOR_RGB(0x7f, 0x7f, 0x7f); -const GdkColor kGdkBlack = GDK_COLOR_RGB(0x00, 0x00, 0x00); -const GdkColor kGdkGreen = GDK_COLOR_RGB(0x00, 0xff, 0x00); - GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap) { if (bitmap->isNull()) return NULL; @@ -104,4 +127,32 @@ double GetPangoResolution() { return resolution; } +std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { + return ConvertAmperstandsTo(label, "_"); +} + +std::string RemoveWindowsStyleAccelerators(const std::string& label) { + return ConvertAmperstandsTo(label, ""); +} + +uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) { + if (stride == 0) + stride = width * 4; + + uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride)); + + // We have to copy the pixels and swap from BGRA to RGBA. + for (int i = 0; i < height; ++i) { + for (int j = 0; j < width; ++j) { + int idx = i * stride + j * 4; + new_pixels[idx] = pixels[idx + 2]; + new_pixels[idx + 1] = pixels[idx + 1]; + new_pixels[idx + 2] = pixels[idx]; + new_pixels[idx + 3] = pixels[idx + 3]; + } + } + + return new_pixels; +} + } // namespace gfx |