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

Unified Diff: chrome/browser/ui/libgtk2ui/gtk2_util.cc

Issue 18334003: Linux status icon for Ubuntu Unity (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/libgtk2ui/gtk2_util.cc
diff --git a/chrome/browser/ui/libgtk2ui/gtk2_util.cc b/chrome/browser/ui/libgtk2ui/gtk2_util.cc
index eec6b1a60d487c722e4392222a68c10543599e45..428881b927fe2a375ad13f0ae6c4a2c9aaabc7f0 100644
--- a/chrome/browser/ui/libgtk2ui/gtk2_util.cc
+++ b/chrome/browser/ui/libgtk2ui/gtk2_util.cc
@@ -12,6 +12,10 @@
#include "base/memory/scoped_ptr.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkUnPreMultiply.h"
+#include "ui/base/accelerators/accelerator.h"
+#include "ui/base/events/event_constants.h"
+#include "ui/base/keycodes/keyboard_code_conversion_x.cc"
#include "ui/gfx/canvas.h"
#include "ui/gfx/size.h"
@@ -36,6 +40,33 @@ void CommonInitFromCommandLine(const CommandLine& command_line,
}
}
+// Replaces all ampersands (as used in our grd files to indicate mnemonics)
+// to |target|, except ampersands appearing in pairs which are replaced by
+// a single ampersand. Any underscores get replaced with two underscores as
+// is needed by GTK.
+std::string ConvertAmpersandsTo(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 libgtk2ui {
@@ -117,4 +148,89 @@ const SkBitmap GdkPixbufToImageSkia(GdkPixbuf* pixbuf) {
return ret;
}
+GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap& bitmap) {
Elliot Glaysher 2013/07/10 22:55:03 For organizational purposes, please put this in sk
sidharthms 2013/07/11 05:15:12 Done.
+ if (bitmap.isNull())
+ return NULL;
+
+ SkAutoLockPixels lock_pixels(bitmap);
+
+ int width = bitmap.width();
+ int height = bitmap.height();
+
+ GdkPixbuf* pixbuf =
+ gdk_pixbuf_new(GDK_COLORSPACE_RGB, // The only colorspace gtk supports.
+ TRUE, // There is an alpha channel.
+ 8,
+ width,
+ height);
+
+ // SkBitmaps are premultiplied, we need to unpremultiply them.
+ const int kBytesPerPixel = 4;
+ uint8* divided = gdk_pixbuf_get_pixels(pixbuf);
+
+ for (int y = 0, i = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ uint32 pixel = bitmap.getAddr32(0, y)[x];
+
+ int alpha = SkColorGetA(pixel);
+ if (alpha != 0 && alpha != 255) {
+ SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel);
+ divided[i + 0] = SkColorGetR(unmultiplied);
+ divided[i + 1] = SkColorGetG(unmultiplied);
+ divided[i + 2] = SkColorGetB(unmultiplied);
+ divided[i + 3] = alpha;
+ } else {
+ divided[i + 0] = SkColorGetR(pixel);
+ divided[i + 1] = SkColorGetG(pixel);
+ divided[i + 2] = SkColorGetB(pixel);
+ divided[i + 3] = alpha;
+ }
+ i += kBytesPerPixel;
+ }
+ }
+
+ return pixbuf;
+}
+
+void SetAlwaysShowImage(GtkWidget* image_menu_item) {
+ gtk_image_menu_item_set_always_show_image(
+ GTK_IMAGE_MENU_ITEM(image_menu_item), TRUE);
+}
+
+std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) {
+ return ConvertAmpersandsTo(label, "_");
+}
+
+guint GetGdkKeyCodeForAccelerator(const ui::Accelerator& accelerator) {
+ // The second parameter is false because accelerator keys are expressed in
+ // terms of the non-shift-modified key.
+ return XKeysymForWindowsKeyCode(accelerator.key_code(), false);
+}
+
+GdkModifierType GetGdkModifierForAccelerator(
+ const ui::Accelerator& accelerator) {
+ int event_flag = accelerator.modifiers();
+ int modifier = 0;
+ if (event_flag & ui::EF_SHIFT_DOWN)
+ modifier |= GDK_SHIFT_MASK;
+ if (event_flag & ui::EF_CONTROL_DOWN)
+ modifier |= GDK_CONTROL_MASK;
+ if (event_flag & ui::EF_ALT_DOWN)
+ modifier |= GDK_MOD1_MASK;
+ return static_cast<GdkModifierType>(modifier);
+}
+
+int EventFlagsFromGdkState(guint state) {
+ int flags = ui::EF_NONE;
+ flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
+ flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
+ flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
+ flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : ui::EF_NONE;
+ flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_MOUSE_BUTTON : ui::EF_NONE;
+ flags |=
+ (state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_MOUSE_BUTTON : ui::EF_NONE;
+ flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_MOUSE_BUTTON : ui::EF_NONE;
+ return flags;
+}
+
} // namespace libgtk2ui

Powered by Google App Engine
This is Rietveld 408576698