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/libgtk2ui/gtk2_util.h" | 5 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "skia/ext/platform_canvas.h" | 12 #include "ui/base/accelerators/accelerator.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "ui/base/events/event_constants.h" |
15 #include "ui/gfx/canvas.h" | 14 #include "ui/base/keycodes/keyboard_code_conversion_x.cc" |
Mark Seaborn
2013/07/27 04:38:20
This should be ".h", not ".cc"!
I just got the fo
sidharthms
2013/07/27 07:50:24
Oh! I'm not sure how it got through the commit que
Mark Seaborn
2013/07/27 16:28:44
It's OK, I've prepared a fix: https://codereview.c
| |
16 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 void CommonInitFromCommandLine(const CommandLine& command_line, | 19 void CommonInitFromCommandLine(const CommandLine& command_line, |
21 void (*init_func)(gint*, gchar***)) { | 20 void (*init_func)(gint*, gchar***)) { |
22 const std::vector<std::string>& args = command_line.argv(); | 21 const std::vector<std::string>& args = command_line.argv(); |
23 int argc = args.size(); | 22 int argc = args.size(); |
24 scoped_ptr<char *[]> argv(new char *[argc + 1]); | 23 scoped_ptr<char *[]> argv(new char *[argc + 1]); |
25 for (size_t i = 0; i < args.size(); ++i) { | 24 for (size_t i = 0; i < args.size(); ++i) { |
26 // TODO(piman@google.com): can gtk_init modify argv? Just being safe | 25 // TODO(piman@google.com): can gtk_init modify argv? Just being safe |
27 // here. | 26 // here. |
28 argv[i] = strdup(args[i].c_str()); | 27 argv[i] = strdup(args[i].c_str()); |
29 } | 28 } |
30 argv[argc] = NULL; | 29 argv[argc] = NULL; |
31 char **argv_pointer = argv.get(); | 30 char **argv_pointer = argv.get(); |
32 | 31 |
33 init_func(&argc, &argv_pointer); | 32 init_func(&argc, &argv_pointer); |
34 for (size_t i = 0; i < args.size(); ++i) { | 33 for (size_t i = 0; i < args.size(); ++i) { |
35 free(argv[i]); | 34 free(argv[i]); |
36 } | 35 } |
37 } | 36 } |
38 | 37 |
38 // Replaces all ampersands (as used in our grd files to indicate mnemonics) | |
39 // to |target|, except ampersands appearing in pairs which are replaced by | |
40 // a single ampersand. Any underscores get replaced with two underscores as | |
41 // is needed by GTK. | |
42 std::string ConvertAmpersandsTo(const std::string& label, | |
43 const std::string& target) { | |
44 std::string ret; | |
45 ret.reserve(label.length() * 2); | |
46 for (size_t i = 0; i < label.length(); ++i) { | |
47 if ('_' == label[i]) { | |
48 ret.push_back('_'); | |
49 ret.push_back('_'); | |
50 } else if ('&' == label[i]) { | |
51 if (i + 1 < label.length() && '&' == label[i + 1]) { | |
52 ret.push_back('&'); | |
53 ++i; | |
54 } else { | |
55 ret.append(target); | |
56 } | |
57 } else { | |
58 ret.push_back(label[i]); | |
59 } | |
60 } | |
61 | |
62 return ret; | |
63 } | |
64 | |
39 } // namespace | 65 } // namespace |
40 | 66 |
41 namespace libgtk2ui { | 67 namespace libgtk2ui { |
42 | 68 |
43 void GtkInitFromCommandLine(const CommandLine& command_line) { | 69 void GtkInitFromCommandLine(const CommandLine& command_line) { |
44 CommonInitFromCommandLine(command_line, gtk_init); | 70 CommonInitFromCommandLine(command_line, gtk_init); |
45 } | 71 } |
46 | 72 |
47 // TODO(erg): This method was copied out of shell_integration_linux.cc. Because | 73 // TODO(erg): This method was copied out of shell_integration_linux.cc. Because |
48 // of how this library is structured as a stand alone .so, we can't call code | 74 // of how this library is structured as a stand alone .so, we can't call code |
49 // from browser and above. | 75 // from browser and above. |
50 std::string GetDesktopName(base::Environment* env) { | 76 std::string GetDesktopName(base::Environment* env) { |
51 #if defined(GOOGLE_CHROME_BUILD) | 77 #if defined(GOOGLE_CHROME_BUILD) |
52 return "google-chrome.desktop"; | 78 return "google-chrome.desktop"; |
53 #else // CHROMIUM_BUILD | 79 #else // CHROMIUM_BUILD |
54 // Allow $CHROME_DESKTOP to override the built-in value, so that development | 80 // Allow $CHROME_DESKTOP to override the built-in value, so that development |
55 // versions can set themselves as the default without interfering with | 81 // versions can set themselves as the default without interfering with |
56 // non-official, packaged versions using the built-in value. | 82 // non-official, packaged versions using the built-in value. |
57 std::string name; | 83 std::string name; |
58 if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty()) | 84 if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty()) |
59 return name; | 85 return name; |
60 return "chromium-browser.desktop"; | 86 return "chromium-browser.desktop"; |
61 #endif | 87 #endif |
62 } | 88 } |
63 | 89 |
64 const SkBitmap GdkPixbufToImageSkia(GdkPixbuf* pixbuf) { | 90 void SetAlwaysShowImage(GtkWidget* image_menu_item) { |
65 // TODO(erg): What do we do in the case where the pixbuf fails these dchecks? | 91 gtk_image_menu_item_set_always_show_image( |
66 // I would prefer to use our gtk based canvas, but that would require | 92 GTK_IMAGE_MENU_ITEM(image_menu_item), TRUE); |
67 // recompiling half of our skia extensions with gtk support, which we can't | 93 } |
68 // do in this build. | |
69 DCHECK_EQ(GDK_COLORSPACE_RGB, gdk_pixbuf_get_colorspace(pixbuf)); | |
70 | 94 |
71 int n_channels = gdk_pixbuf_get_n_channels(pixbuf); | 95 std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { |
72 int w = gdk_pixbuf_get_width(pixbuf); | 96 return ConvertAmpersandsTo(label, "_"); |
73 int h = gdk_pixbuf_get_height(pixbuf); | 97 } |
74 | 98 |
75 SkBitmap ret; | 99 guint GetGdkKeyCodeForAccelerator(const ui::Accelerator& accelerator) { |
76 ret.setConfig(SkBitmap::kARGB_8888_Config, w, h); | 100 // The second parameter is false because accelerator keys are expressed in |
77 ret.allocPixels(); | 101 // terms of the non-shift-modified key. |
78 ret.eraseColor(0); | 102 return XKeysymForWindowsKeyCode(accelerator.key_code(), false); |
103 } | |
79 | 104 |
80 uint32_t* skia_data = static_cast<uint32_t*>(ret.getAddr(0, 0)); | 105 GdkModifierType GetGdkModifierForAccelerator( |
106 const ui::Accelerator& accelerator) { | |
107 int event_flag = accelerator.modifiers(); | |
108 int modifier = 0; | |
109 if (event_flag & ui::EF_SHIFT_DOWN) | |
110 modifier |= GDK_SHIFT_MASK; | |
111 if (event_flag & ui::EF_CONTROL_DOWN) | |
112 modifier |= GDK_CONTROL_MASK; | |
113 if (event_flag & ui::EF_ALT_DOWN) | |
114 modifier |= GDK_MOD1_MASK; | |
115 return static_cast<GdkModifierType>(modifier); | |
116 } | |
81 | 117 |
82 if (n_channels == 4) { | 118 int EventFlagsFromGdkState(guint state) { |
83 int total_length = w * h; | 119 int flags = ui::EF_NONE; |
84 guchar* gdk_pixels = gdk_pixbuf_get_pixels(pixbuf); | 120 flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE; |
85 | 121 flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : ui::EF_NONE; |
86 // Now here's the trick: we need to convert the gdk data (which is RGBA and | 122 flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : ui::EF_NONE; |
87 // isn't premultiplied) to skia (which can be anything and premultiplied). | 123 flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : ui::EF_NONE; |
88 for (int i = 0; i < total_length; ++i, gdk_pixels += 4) { | 124 flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_MOUSE_BUTTON : ui::EF_NONE; |
89 const unsigned char& red = gdk_pixels[0]; | 125 flags |= |
90 const unsigned char& green = gdk_pixels[1]; | 126 (state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_MOUSE_BUTTON : ui::EF_NONE; |
91 const unsigned char& blue = gdk_pixels[2]; | 127 flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_MOUSE_BUTTON : ui::EF_NONE; |
92 const unsigned char& alpha = gdk_pixels[3]; | 128 return flags; |
93 | |
94 skia_data[i] = SkPreMultiplyARGB(alpha, red, green, blue); | |
95 } | |
96 } else if (n_channels == 3) { | |
97 // Because GDK makes rowstrides word aligned, we need to do something a bit | |
98 // more complex when a pixel isn't perfectly a word of memory. | |
99 int rowstride = gdk_pixbuf_get_rowstride(pixbuf); | |
100 guchar* gdk_pixels = gdk_pixbuf_get_pixels(pixbuf); | |
101 for (int y = 0; y < h; ++y) { | |
102 int row = y * rowstride; | |
103 | |
104 for (int x = 0; x < w; ++x) { | |
105 guchar* pixel = gdk_pixels + row + (x * 3); | |
106 const unsigned char& red = pixel[0]; | |
107 const unsigned char& green = pixel[1]; | |
108 const unsigned char& blue = pixel[2]; | |
109 | |
110 skia_data[y * w + x] = SkPreMultiplyARGB(255, red, green, blue); | |
111 } | |
112 } | |
113 } else { | |
114 NOTREACHED(); | |
115 } | |
116 | |
117 return ret; | |
118 } | 129 } |
119 | 130 |
120 } // namespace libgtk2ui | 131 } // namespace libgtk2ui |
OLD | NEW |