| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/gtk_util.h" | 5 #include "ui/gfx/gtk_util.h" |
| 6 | 6 |
| 7 #include <gdk/gdk.h> | 7 #include <gdk/gdk.h> |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 std::map<GdkCursorType, GdkCursor*> cursor_cache_; | 49 std::map<GdkCursorType, GdkCursor*> cursor_cache_; |
| 50 | 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(GdkCursorCache); | 51 DISALLOW_COPY_AND_ASSIGN(GdkCursorCache); |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 void FreePixels(guchar* pixels, gpointer data) { | 54 void FreePixels(guchar* pixels, gpointer data) { |
| 55 free(data); | 55 free(data); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Common implementation of ConvertAcceleratorsFromWindowsStyle() and | |
| 59 // RemoveWindowsStyleAccelerators(). | |
| 60 // Replaces all ampersands (as used in our grd files to indicate mnemonics) | |
| 61 // to |target|. Similarly any underscores get replaced with two underscores as | |
| 62 // is needed by GTK. | |
| 63 std::string ConvertAmperstandsTo(const std::string& label, | |
| 64 const std::string& target) { | |
| 65 std::string ret; | |
| 66 ret.reserve(label.length() * 2); | |
| 67 for (size_t i = 0; i < label.length(); ++i) { | |
| 68 if ('_' == label[i]) { | |
| 69 ret.push_back('_'); | |
| 70 ret.push_back('_'); | |
| 71 } else if ('&' == label[i]) { | |
| 72 if (i + 1 < label.length() && '&' == label[i + 1]) { | |
| 73 ret.push_back('&'); | |
| 74 ++i; | |
| 75 } else { | |
| 76 ret.append(target); | |
| 77 } | |
| 78 } else { | |
| 79 ret.push_back(label[i]); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 return ret; | |
| 84 } | |
| 85 | |
| 86 } // namespace | 58 } // namespace |
| 87 | 59 |
| 88 namespace gfx { | 60 namespace gfx { |
| 89 | 61 |
| 90 void GtkInitFromCommandLine(const CommandLine& command_line) { | 62 void GtkInitFromCommandLine(const CommandLine& command_line) { |
| 91 const std::vector<std::string>& args = command_line.argv(); | 63 const std::vector<std::string>& args = command_line.argv(); |
| 92 int argc = args.size(); | 64 int argc = args.size(); |
| 93 scoped_array<char *> argv(new char *[argc + 1]); | 65 scoped_array<char *> argv(new char *[argc + 1]); |
| 94 for (size_t i = 0; i < args.size(); ++i) { | 66 for (size_t i = 0; i < args.size(); ++i) { |
| 95 // TODO(piman@google.com): can gtk_init modify argv? Just being safe | 67 // TODO(piman@google.com): can gtk_init modify argv? Just being safe |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 const std::vector<Rect>& cutouts) { | 129 const std::vector<Rect>& cutouts) { |
| 158 for (size_t i = 0; i < cutouts.size(); ++i) { | 130 for (size_t i = 0; i < cutouts.size(); ++i) { |
| 159 GdkRectangle rect = cutouts[i].ToGdkRectangle(); | 131 GdkRectangle rect = cutouts[i].ToGdkRectangle(); |
| 160 GdkRegion* rect_region = gdk_region_rectangle(&rect); | 132 GdkRegion* rect_region = gdk_region_rectangle(&rect); |
| 161 gdk_region_subtract(region, rect_region); | 133 gdk_region_subtract(region, rect_region); |
| 162 // TODO(deanm): It would be nice to be able to reuse the GdkRegion here. | 134 // TODO(deanm): It would be nice to be able to reuse the GdkRegion here. |
| 163 gdk_region_destroy(rect_region); | 135 gdk_region_destroy(rect_region); |
| 164 } | 136 } |
| 165 } | 137 } |
| 166 | 138 |
| 167 PangoContext* GetPangoContext() { | |
| 168 #if defined(USE_WAYLAND) | |
| 169 PangoFontMap* font_map = pango_cairo_font_map_get_default(); | |
| 170 PangoContext* default_context = pango_font_map_create_context(font_map); | |
| 171 #else | |
| 172 PangoContext* default_context = gdk_pango_context_get(); | |
| 173 #endif | |
| 174 return default_context; | |
| 175 } | |
| 176 | |
| 177 double GetPangoResolution() { | |
| 178 static double resolution; | |
| 179 static bool determined_resolution = false; | |
| 180 if (!determined_resolution) { | |
| 181 determined_resolution = true; | |
| 182 PangoContext* default_context = GetPangoContext(); | |
| 183 resolution = pango_cairo_context_get_resolution(default_context); | |
| 184 g_object_unref(default_context); | |
| 185 } | |
| 186 return resolution; | |
| 187 } | |
| 188 | |
| 189 GdkCursor* GetCursor(int type) { | 139 GdkCursor* GetCursor(int type) { |
| 190 static GdkCursorCache impl; | 140 static GdkCursorCache impl; |
| 191 return impl.GetCursorImpl(static_cast<GdkCursorType>(type)); | 141 return impl.GetCursorImpl(static_cast<GdkCursorType>(type)); |
| 192 } | 142 } |
| 193 | 143 |
| 194 std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { | 144 #if !defined(USE_WAYLAND) && !defined(USE_AURA) |
| 195 return ConvertAmperstandsTo(label, "_"); | 145 PangoContext* GetPangoContext() { |
| 146 return gdk_pango_context_get(); |
| 196 } | 147 } |
| 197 | 148 #endif |
| 198 std::string RemoveWindowsStyleAccelerators(const std::string& label) { | |
| 199 return ConvertAmperstandsTo(label, ""); | |
| 200 } | |
| 201 | |
| 202 uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) { | |
| 203 if (stride == 0) | |
| 204 stride = width * 4; | |
| 205 | |
| 206 uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride)); | |
| 207 | |
| 208 // We have to copy the pixels and swap from BGRA to RGBA. | |
| 209 for (int i = 0; i < height; ++i) { | |
| 210 for (int j = 0; j < width; ++j) { | |
| 211 int idx = i * stride + j * 4; | |
| 212 new_pixels[idx] = pixels[idx + 2]; | |
| 213 new_pixels[idx + 1] = pixels[idx + 1]; | |
| 214 new_pixels[idx + 2] = pixels[idx]; | |
| 215 new_pixels[idx + 3] = pixels[idx + 3]; | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 return new_pixels; | |
| 220 } | |
| 221 | 149 |
| 222 } // namespace gfx | 150 } // namespace gfx |
| OLD | NEW |