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

Side by Side 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 unified diff | Download patch
OLDNEW
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" 11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "skia/ext/platform_canvas.h" 13 #include "skia/ext/platform_canvas.h"
14 #include "third_party/skia/include/core/SkBitmap.h" 14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "third_party/skia/include/core/SkUnPreMultiply.h"
16 #include "ui/base/accelerators/accelerator.h"
17 #include "ui/base/events/event_constants.h"
18 #include "ui/base/keycodes/keyboard_code_conversion_x.cc"
15 #include "ui/gfx/canvas.h" 19 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/size.h" 20 #include "ui/gfx/size.h"
17 21
18 namespace { 22 namespace {
19 23
20 void CommonInitFromCommandLine(const CommandLine& command_line, 24 void CommonInitFromCommandLine(const CommandLine& command_line,
21 void (*init_func)(gint*, gchar***)) { 25 void (*init_func)(gint*, gchar***)) {
22 const std::vector<std::string>& args = command_line.argv(); 26 const std::vector<std::string>& args = command_line.argv();
23 int argc = args.size(); 27 int argc = args.size();
24 scoped_ptr<char *[]> argv(new char *[argc + 1]); 28 scoped_ptr<char *[]> argv(new char *[argc + 1]);
25 for (size_t i = 0; i < args.size(); ++i) { 29 for (size_t i = 0; i < args.size(); ++i) {
26 // TODO(piman@google.com): can gtk_init modify argv? Just being safe 30 // TODO(piman@google.com): can gtk_init modify argv? Just being safe
27 // here. 31 // here.
28 argv[i] = strdup(args[i].c_str()); 32 argv[i] = strdup(args[i].c_str());
29 } 33 }
30 argv[argc] = NULL; 34 argv[argc] = NULL;
31 char **argv_pointer = argv.get(); 35 char **argv_pointer = argv.get();
32 36
33 init_func(&argc, &argv_pointer); 37 init_func(&argc, &argv_pointer);
34 for (size_t i = 0; i < args.size(); ++i) { 38 for (size_t i = 0; i < args.size(); ++i) {
35 free(argv[i]); 39 free(argv[i]);
36 } 40 }
37 } 41 }
38 42
43 // Replaces all ampersands (as used in our grd files to indicate mnemonics)
44 // to |target|, except ampersands appearing in pairs which are replaced by
45 // a single ampersand. Any underscores get replaced with two underscores as
46 // is needed by GTK.
47 std::string ConvertAmpersandsTo(const std::string& label,
48 const std::string& target) {
49 std::string ret;
50 ret.reserve(label.length() * 2);
51 for (size_t i = 0; i < label.length(); ++i) {
52 if ('_' == label[i]) {
53 ret.push_back('_');
54 ret.push_back('_');
55 } else if ('&' == label[i]) {
56 if (i + 1 < label.length() && '&' == label[i + 1]) {
57 ret.push_back('&');
58 ++i;
59 } else {
60 ret.append(target);
61 }
62 } else {
63 ret.push_back(label[i]);
64 }
65 }
66
67 return ret;
68 }
69
39 } // namespace 70 } // namespace
40 71
41 namespace libgtk2ui { 72 namespace libgtk2ui {
42 73
43 void GtkInitFromCommandLine(const CommandLine& command_line) { 74 void GtkInitFromCommandLine(const CommandLine& command_line) {
44 CommonInitFromCommandLine(command_line, gtk_init); 75 CommonInitFromCommandLine(command_line, gtk_init);
45 } 76 }
46 77
47 // TODO(erg): This method was copied out of shell_integration_linux.cc. Because 78 // 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 79 // of how this library is structured as a stand alone .so, we can't call code
49 // from browser and above. 80 // from browser and above.
50 std::string GetDesktopName(base::Environment* env) { 81 std::string GetDesktopName(base::Environment* env) {
51 #if defined(GOOGLE_CHROME_BUILD) 82 #if defined(GOOGLE_CHROME_BUILD)
52 return "google-chrome.desktop"; 83 return "google-chrome.desktop";
53 #else // CHROMIUM_BUILD 84 #else // CHROMIUM_BUILD
54 // Allow $CHROME_DESKTOP to override the built-in value, so that development 85 // Allow $CHROME_DESKTOP to override the built-in value, so that development
55 // versions can set themselves as the default without interfering with 86 // versions can set themselves as the default without interfering with
56 // non-official, packaged versions using the built-in value. 87 // non-official, packaged versions using the built-in value.
57 std::string name; 88 std::string name;
58 if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty()) 89 if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty())
59 return name; 90 return name;
60 return "chromium-browser.desktop"; 91 return "chromium-browser.desktop";
61 #endif 92 #endif
62 } 93 }
63 94
64 const SkBitmap GdkPixbufToImageSkia(GdkPixbuf* pixbuf) { 95 const SkBitmap GdkPixbufToImageSkia(GdkPixbuf* pixbuf) {
sidharthms 2013/07/11 05:15:12 Should I move this one to skia_utils_gtk2 too?
Elliot Glaysher 2013/07/11 21:35:08 I totally missed this. Yes.
sidharthms 2013/07/12 01:12:15 Done.
65 // TODO(erg): What do we do in the case where the pixbuf fails these dchecks? 96 // TODO(erg): What do we do in the case where the pixbuf fails these dchecks?
66 // I would prefer to use our gtk based canvas, but that would require 97 // I would prefer to use our gtk based canvas, but that would require
67 // recompiling half of our skia extensions with gtk support, which we can't 98 // recompiling half of our skia extensions with gtk support, which we can't
68 // do in this build. 99 // do in this build.
69 DCHECK_EQ(GDK_COLORSPACE_RGB, gdk_pixbuf_get_colorspace(pixbuf)); 100 DCHECK_EQ(GDK_COLORSPACE_RGB, gdk_pixbuf_get_colorspace(pixbuf));
70 101
71 int n_channels = gdk_pixbuf_get_n_channels(pixbuf); 102 int n_channels = gdk_pixbuf_get_n_channels(pixbuf);
72 int w = gdk_pixbuf_get_width(pixbuf); 103 int w = gdk_pixbuf_get_width(pixbuf);
73 int h = gdk_pixbuf_get_height(pixbuf); 104 int h = gdk_pixbuf_get_height(pixbuf);
74 105
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 skia_data[y * w + x] = SkPreMultiplyARGB(255, red, green, blue); 141 skia_data[y * w + x] = SkPreMultiplyARGB(255, red, green, blue);
111 } 142 }
112 } 143 }
113 } else { 144 } else {
114 NOTREACHED(); 145 NOTREACHED();
115 } 146 }
116 147
117 return ret; 148 return ret;
118 } 149 }
119 150
151 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.
152 if (bitmap.isNull())
153 return NULL;
154
155 SkAutoLockPixels lock_pixels(bitmap);
156
157 int width = bitmap.width();
158 int height = bitmap.height();
159
160 GdkPixbuf* pixbuf =
161 gdk_pixbuf_new(GDK_COLORSPACE_RGB, // The only colorspace gtk supports.
162 TRUE, // There is an alpha channel.
163 8,
164 width,
165 height);
166
167 // SkBitmaps are premultiplied, we need to unpremultiply them.
168 const int kBytesPerPixel = 4;
169 uint8* divided = gdk_pixbuf_get_pixels(pixbuf);
170
171 for (int y = 0, i = 0; y < height; y++) {
172 for (int x = 0; x < width; x++) {
173 uint32 pixel = bitmap.getAddr32(0, y)[x];
174
175 int alpha = SkColorGetA(pixel);
176 if (alpha != 0 && alpha != 255) {
177 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel);
178 divided[i + 0] = SkColorGetR(unmultiplied);
179 divided[i + 1] = SkColorGetG(unmultiplied);
180 divided[i + 2] = SkColorGetB(unmultiplied);
181 divided[i + 3] = alpha;
182 } else {
183 divided[i + 0] = SkColorGetR(pixel);
184 divided[i + 1] = SkColorGetG(pixel);
185 divided[i + 2] = SkColorGetB(pixel);
186 divided[i + 3] = alpha;
187 }
188 i += kBytesPerPixel;
189 }
190 }
191
192 return pixbuf;
193 }
194
195 void SetAlwaysShowImage(GtkWidget* image_menu_item) {
196 gtk_image_menu_item_set_always_show_image(
197 GTK_IMAGE_MENU_ITEM(image_menu_item), TRUE);
198 }
199
200 std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) {
201 return ConvertAmpersandsTo(label, "_");
202 }
203
204 guint GetGdkKeyCodeForAccelerator(const ui::Accelerator& accelerator) {
205 // The second parameter is false because accelerator keys are expressed in
206 // terms of the non-shift-modified key.
207 return XKeysymForWindowsKeyCode(accelerator.key_code(), false);
208 }
209
210 GdkModifierType GetGdkModifierForAccelerator(
211 const ui::Accelerator& accelerator) {
212 int event_flag = accelerator.modifiers();
213 int modifier = 0;
214 if (event_flag & ui::EF_SHIFT_DOWN)
215 modifier |= GDK_SHIFT_MASK;
216 if (event_flag & ui::EF_CONTROL_DOWN)
217 modifier |= GDK_CONTROL_MASK;
218 if (event_flag & ui::EF_ALT_DOWN)
219 modifier |= GDK_MOD1_MASK;
220 return static_cast<GdkModifierType>(modifier);
221 }
222
223 int EventFlagsFromGdkState(guint state) {
224 int flags = ui::EF_NONE;
225 flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
226 flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
227 flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
228 flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : ui::EF_NONE;
229 flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_MOUSE_BUTTON : ui::EF_NONE;
230 flags |=
231 (state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_MOUSE_BUTTON : ui::EF_NONE;
232 flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_MOUSE_BUTTON : ui::EF_NONE;
233 return flags;
234 }
235
120 } // namespace libgtk2ui 236 } // namespace libgtk2ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698