+#endif
+
+#ifdef __WXGTK3__
+GdkPixbuf* wxBitmap::GetPixbufNoMask() const
+{
+ wxCHECK_MSG(IsOk(), NULL, "invalid bitmap");
+
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ GdkPixbuf* pixbuf = bmpData->m_pixbufNoMask;
+ if (pixbuf)
+ return pixbuf;
+
+ const int w = bmpData->m_width;
+ const int h = bmpData->m_height;
+ if (bmpData->m_surface)
+ pixbuf = gdk_pixbuf_get_from_surface(bmpData->m_surface, 0, 0, w, h);
+ else
+ pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, bmpData->m_bpp == 32, 8, w, h);
+ bmpData->m_pixbufNoMask = pixbuf;
+ wxASSERT(bmpData->m_bpp == 32 || !gdk_pixbuf_get_has_alpha(bmpData->m_pixbufNoMask));
+
+ return pixbuf;
+}
+
+// helper to set up a simulated depth 1 surface
+static void SetSourceSurface1(const wxBitmapRefData* bmpData, cairo_t* cr, int x, int y, const wxColour* fg, const wxColour* bg)
+{
+ GdkPixbuf* pixbuf = gdk_pixbuf_copy(bmpData->m_pixbufNoMask);
+ const int w = bmpData->m_width;
+ const int h = bmpData->m_height;
+ const int stride = gdk_pixbuf_get_rowstride(pixbuf);
+ const int channels = gdk_pixbuf_get_n_channels(pixbuf);
+ guchar* dst = gdk_pixbuf_get_pixels(pixbuf);
+ guchar fg_r = 0, fg_g = 0, fg_b = 0;
+ if (fg && fg->IsOk())
+ {
+ fg_r = fg->Red();
+ fg_g = fg->Green();
+ fg_b = fg->Blue();
+ }
+ guchar bg_r = 255, bg_g = 255, bg_b = 255;
+ if (bg && bg->IsOk())
+ {
+ bg_r = bg->Red();
+ bg_g = bg->Green();
+ bg_b = bg->Blue();
+ }
+ for (int j = 0; j < h; j++, dst += stride)
+ {
+ guchar* d = dst;
+ for (int i = 0; i < w; i++, d += channels)
+ if (d[0])
+ {
+ d[0] = bg_r;
+ d[1] = bg_g;
+ d[2] = bg_b;
+ }
+ else
+ {
+ d[0] = fg_r;
+ d[1] = fg_g;
+ d[2] = fg_b;
+ }
+ }
+ gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
+ g_object_unref(pixbuf);
+}
+
+void wxBitmap::SetSourceSurface(cairo_t* cr, int x, int y, const wxColour* fg, const wxColour* bg) const
+{
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ if (bmpData->m_surface)
+ {
+ cairo_set_source_surface(cr, bmpData->m_surface, x, y);
+ return;
+ }
+ wxCHECK_RET(bmpData->m_pixbufNoMask, "no bitmap data");
+ if (bmpData->m_bpp == 1)
+ SetSourceSurface1(bmpData, cr, x, y, fg, bg);
+ else
+ {
+ gdk_cairo_set_source_pixbuf(cr, bmpData->m_pixbufNoMask, x, y);
+ cairo_pattern_get_surface(cairo_get_source(cr), &bmpData->m_surface);
+ cairo_surface_reference(bmpData->m_surface);
+ }
+}
+
+cairo_t* wxBitmap::CairoCreate() const
+{
+ wxCHECK_MSG(IsOk(), NULL, "invalid bitmap");
+
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ cairo_t* cr;
+ if (bmpData->m_surface)
+ cr = cairo_create(bmpData->m_surface);
+ else
+ {
+ GdkPixbuf* pixbuf = bmpData->m_pixbufNoMask;
+ const bool useAlpha = bmpData->m_bpp == 32 || (pixbuf && gdk_pixbuf_get_has_alpha(pixbuf));
+ bmpData->m_surface = cairo_image_surface_create(
+ useAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
+ bmpData->m_width, bmpData->m_height);
+ cr = cairo_create(bmpData->m_surface);
+ if (pixbuf)
+ {
+ gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
+ cairo_paint(cr);
+ cairo_set_source_rgb(cr, 0, 0, 0);
+ }
+ }
+ if (bmpData->m_pixbufNoMask)
+ {
+ g_object_unref(bmpData->m_pixbufNoMask);
+ bmpData->m_pixbufNoMask = NULL;
+ }
+ if (bmpData->m_pixbufMask)
+ {
+ g_object_unref(bmpData->m_pixbufMask);
+ bmpData->m_pixbufMask = NULL;
+ }
+ wxASSERT(cr && cairo_status(cr) == 0);
+ return cr;
+}
+
+void wxBitmap::Draw(cairo_t* cr, int x, int y, bool useMask, const wxColour* fg, const wxColour* bg) const
+{
+ wxCHECK_RET(IsOk(), "invalid bitmap");
+
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ SetSourceSurface(cr, x, y, fg, bg);
+ cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
+ cairo_surface_t* mask = NULL;
+ if (useMask && bmpData->m_mask)
+ mask = bmpData->m_mask->GetBitmap();
+ if (mask)
+ cairo_mask_surface(cr, mask, x, y);
+ else
+ cairo_paint(cr);
+}
+#endif