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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/gtk/nine_box.h" 6 7 #include "base/basictypes.h" 8 #include "base/i18n/rtl.h" 9 #include "base/logging.h" 10 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/gfx/gtk_util.h" 12 #include "ui/gfx/point.h" 13 14 namespace { 15 16 // Draw pixbuf |src| into |dst| at position (x, y). 17 void DrawPixbuf(cairo_t* cr, GdkPixbuf* src, int x, int y, double alpha) { 18 gdk_cairo_set_source_pixbuf(cr, src, x, y); 19 cairo_paint_with_alpha(cr, alpha); 20 } 21 22 // Tile pixbuf |src| across |cr| at |x|, |y| for |width| and |height|. 23 void TileImage(cairo_t* cr, GdkPixbuf* src, 24 int x, int y, int width, int height, double alpha) { 25 if (alpha == 1.0) { 26 gdk_cairo_set_source_pixbuf(cr, src, x, y); 27 cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REPEAT); 28 cairo_rectangle(cr, x, y, width, height); 29 cairo_fill(cr); 30 } else { 31 // Since there is no easy way to apply a mask to a fill operation, we create 32 // a secondary surface and tile into that, then paint it with |alpha|. 33 cairo_surface_t* surface = cairo_image_surface_create( 34 CAIRO_FORMAT_ARGB32, width, height); 35 cairo_t* tiled = cairo_create(surface); 36 gdk_cairo_set_source_pixbuf(tiled, src, 0, 0); 37 cairo_pattern_set_extend(cairo_get_source(tiled), CAIRO_EXTEND_REPEAT); 38 cairo_rectangle(tiled, 0, 0, width, height); 39 cairo_fill(tiled); 40 41 cairo_set_source_surface(cr, surface, x, y); 42 cairo_paint_with_alpha(cr, alpha); 43 44 cairo_destroy(tiled); 45 cairo_surface_destroy(surface); 46 } 47 } 48 49 } // namespace 50 51 NineBox::NineBox(int top_left, int top, int top_right, int left, int center, 52 int right, int bottom_left, int bottom, int bottom_right) 53 : unref_pixbufs_on_destroy_(false) { 54 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 55 images_[0] = top_left ? rb.GetPixbufNamed(top_left) : NULL; 56 images_[1] = top ? rb.GetPixbufNamed(top) : NULL; 57 images_[2] = top_right ? rb.GetPixbufNamed(top_right) : NULL; 58 images_[3] = left ? rb.GetPixbufNamed(left) : NULL; 59 images_[4] = center ? rb.GetPixbufNamed(center) : NULL; 60 images_[5] = right ? rb.GetPixbufNamed(right) : NULL; 61 images_[6] = bottom_left ? rb.GetPixbufNamed(bottom_left) : NULL; 62 images_[7] = bottom ? rb.GetPixbufNamed(bottom) : NULL; 63 images_[8] = bottom_right ? rb.GetPixbufNamed(bottom_right) : NULL; 64 } 65 66 NineBox::NineBox(int image, int top_margin, int bottom_margin, int left_margin, 67 int right_margin) 68 : unref_pixbufs_on_destroy_(true) { 69 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 70 GdkPixbuf* pixbuf = rb.GetPixbufNamed(image); 71 int width = gdk_pixbuf_get_width(pixbuf); 72 int height = gdk_pixbuf_get_height(pixbuf); 73 int inset_width = left_margin + right_margin; 74 int inset_height = top_margin + bottom_margin; 75 76 images_[0] = gdk_pixbuf_new_subpixbuf(pixbuf, 0, 0, left_margin, top_margin); 77 images_[1] = gdk_pixbuf_new_subpixbuf(pixbuf, left_margin, 0, 78 width - inset_width, top_margin); 79 images_[2] = gdk_pixbuf_new_subpixbuf(pixbuf, width - right_margin, 0, 80 right_margin, top_margin); 81 images_[3] = gdk_pixbuf_new_subpixbuf(pixbuf, 0, top_margin, 82 left_margin, height - inset_height); 83 images_[4] = gdk_pixbuf_new_subpixbuf(pixbuf, left_margin, top_margin, 84 width - inset_width, 85 height - inset_height); 86 images_[5] = gdk_pixbuf_new_subpixbuf(pixbuf, width - right_margin, 87 top_margin, right_margin, 88 height - inset_height); 89 images_[6] = gdk_pixbuf_new_subpixbuf(pixbuf, 0, height - bottom_margin, 90 left_margin, bottom_margin); 91 images_[7] = gdk_pixbuf_new_subpixbuf(pixbuf, left_margin, 92 height - bottom_margin, 93 width - inset_width, bottom_margin); 94 images_[8] = gdk_pixbuf_new_subpixbuf(pixbuf, width - right_margin, 95 height - bottom_margin, 96 right_margin, bottom_margin); 97 } 98 99 NineBox::~NineBox() { 100 if (unref_pixbufs_on_destroy_) { 101 for (int i = 0; i < 9; i++) { 102 g_object_unref(images_[i]); 103 } 104 } 105 } 106 107 void NineBox::RenderToWidget(GtkWidget* dst) const { 108 RenderToWidgetWithOpacity(dst, 1.0); 109 } 110 111 void NineBox::RenderToWidgetWithOpacity(GtkWidget* dst, double opacity) const { 112 int dst_width = dst->allocation.width; 113 int dst_height = dst->allocation.height; 114 115 // The upper-left and lower-right corners of the center square in the 116 // rendering of the ninebox. 117 int x1 = gdk_pixbuf_get_width(images_[0]); 118 int y1 = gdk_pixbuf_get_height(images_[0]); 119 int x2 = images_[2] ? dst_width - gdk_pixbuf_get_width(images_[2]) : x1; 120 int y2 = images_[6] ? dst_height - gdk_pixbuf_get_height(images_[6]) : y1; 121 // Paint nothing if there's not enough room. 122 if (x2 < x1 || y2 < y1) 123 return; 124 125 cairo_t* cr = gdk_cairo_create(GDK_DRAWABLE(dst->window)); 126 // For widgets that have their own window, the allocation (x,y) coordinates 127 // are GdkWindow relative. For other widgets, the coordinates are relative 128 // to their container. 129 if (GTK_WIDGET_NO_WINDOW(dst)) { 130 // Transform our cairo from window to widget coordinates. 131 cairo_translate(cr, dst->allocation.x, dst->allocation.y); 132 } 133 134 if (base::i18n::IsRTL()) { 135 cairo_translate(cr, dst_width, 0.0f); 136 cairo_scale(cr, -1.0f, 1.0f); 137 } 138 139 // Top row, center image is horizontally tiled. 140 if (images_[0]) 141 DrawPixbuf(cr, images_[0], 0, 0, opacity); 142 if (images_[1]) 143 TileImage(cr, images_[1], x1, 0, x2 - x1, y1, opacity); 144 if (images_[2]) 145 DrawPixbuf(cr, images_[2], x2, 0, opacity); 146 147 // Center row, all images are vertically tiled, center is horizontally tiled. 148 if (images_[3]) 149 TileImage(cr, images_[3], 0, y1, x1, y2 - y1, opacity); 150 if (images_[4]) 151 TileImage(cr, images_[4], x1, y1, x2 - x1, y2 - y1, opacity); 152 if (images_[5]) 153 TileImage(cr, images_[5], x2, y1, dst_width - x2, y2 - y1, opacity); 154 155 // Bottom row, center image is horizontally tiled. 156 if (images_[6]) 157 DrawPixbuf(cr, images_[6], 0, y2, opacity); 158 if (images_[7]) 159 TileImage(cr, images_[7], x1, y2, x2 - x1, dst_height - y2, opacity); 160 if (images_[8]) 161 DrawPixbuf(cr, images_[8], x2, y2, opacity); 162 163 cairo_destroy(cr); 164 } 165 166 void NineBox::RenderTopCenterStrip(cairo_t* cr, int x, int y, 167 int width) const { 168 const int height = gdk_pixbuf_get_height(images_[1]); 169 TileImage(cr, images_[1], x, y, width, height, 1.0); 170 } 171 172 void NineBox::ChangeWhiteToTransparent() { 173 for (int image_idx = 0; image_idx < 9; ++image_idx) { 174 GdkPixbuf* pixbuf = images_[image_idx]; 175 if (!pixbuf) 176 continue; 177 178 if (!gdk_pixbuf_get_has_alpha(pixbuf)) 179 continue; 180 181 guchar* pixels = gdk_pixbuf_get_pixels(pixbuf); 182 int rowstride = gdk_pixbuf_get_rowstride(pixbuf); 183 int width = gdk_pixbuf_get_width(pixbuf); 184 int height = gdk_pixbuf_get_height(pixbuf); 185 186 if (width * 4 > rowstride) { 187 NOTREACHED(); 188 continue; 189 } 190 191 for (int i = 0; i < height; ++i) { 192 for (int j = 0; j < width; ++j) { 193 guchar* pixel = &pixels[i * rowstride + j * 4]; 194 if (pixel[0] == 0xff && pixel[1] == 0xff && pixel[2] == 0xff) { 195 pixel[3] = 0; 196 } 197 } 198 } 199 } 200 } 201 202 void NineBox::ContourWidget(GtkWidget* widget) const { 203 int width = widget->allocation.width; 204 int height = widget->allocation.height; 205 int x1 = gdk_pixbuf_get_width(images_[0]); 206 int x2 = width - gdk_pixbuf_get_width(images_[2]); 207 208 // Paint the left and right sides. 209 GdkBitmap* mask = gdk_pixmap_new(NULL, width, height, 1); 210 gdk_pixbuf_render_threshold_alpha(images_[0], mask, 211 0, 0, 212 0, 0, -1, -1, 213 1); 214 gdk_pixbuf_render_threshold_alpha(images_[2], mask, 215 0, 0, 216 x2, 0, -1, -1, 217 1); 218 219 // Assume no transparency in the middle rectangle. 220 cairo_t* cr = gdk_cairo_create(mask); 221 cairo_rectangle(cr, x1, 0, x2 - x1, height); 222 cairo_fill(cr); 223 cairo_destroy(cr); 224 225 // Mask the widget's window's shape. 226 if (!base::i18n::IsRTL()) { 227 gtk_widget_shape_combine_mask(widget, mask, 0, 0); 228 } else { 229 GdkBitmap* flipped_mask = gdk_pixmap_new(NULL, width, height, 1); 230 cairo_t* flipped_cr = gdk_cairo_create(flipped_mask); 231 232 // Clear the target bitmap. 233 cairo_set_operator(flipped_cr, CAIRO_OPERATOR_CLEAR); 234 cairo_paint(flipped_cr); 235 236 // Apply flipping transformation. 237 cairo_translate(flipped_cr, width, 0.0f); 238 cairo_scale(flipped_cr, -1.0f, 1.0f); 239 240 // Paint the source bitmap onto the target. 241 cairo_set_operator(flipped_cr, CAIRO_OPERATOR_SOURCE); 242 gdk_cairo_set_source_pixmap(flipped_cr, mask, 0, 0); 243 cairo_paint(flipped_cr); 244 cairo_destroy(flipped_cr); 245 246 // Mask the widget. 247 gtk_widget_shape_combine_mask(widget, flipped_mask, 0, 0); 248 g_object_unref(flipped_mask); 249 } 250 251 g_object_unref(mask); 252 } 253