+#include <X11/xpm.h>
+#else
+#include "wx/xpmdecod.h"
+#include "wx/wfstream.h"
+#endif
+#endif
+
+//-----------------------------------------------------------------------------
+// wxMask
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
+
+wxMask::wxMask()
+{
+ m_bitmap = NULL;
+ m_display = NULL;
+}
+
+wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
+{
+ m_bitmap = NULL;
+ Create( bitmap, colour );
+}
+
+wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
+{
+ m_bitmap = NULL;
+ Create( bitmap, paletteIndex );
+}
+
+wxMask::wxMask( const wxBitmap& bitmap )
+{
+ m_bitmap = NULL;
+ Create( bitmap );
+}
+
+wxMask::~wxMask()
+{
+ if (m_bitmap)
+ XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
+}
+
+bool wxMask::Create( const wxBitmap& bitmap,
+ const wxColour& colour )
+{
+#if !wxUSE_NANOX
+ if (m_bitmap)
+ {
+ XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
+ m_bitmap = NULL;
+ }
+
+ m_display = bitmap.GetDisplay();
+
+ wxImage image = bitmap.ConvertToImage();
+ if (!image.Ok()) return false;
+
+ m_display = bitmap.GetDisplay();
+
+ Display *xdisplay = (Display*) m_display;
+ int xscreen = DefaultScreen( xdisplay );
+ Window xroot = RootWindow( xdisplay, xscreen );
+
+ m_bitmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, image.GetWidth(), image.GetHeight(), 1 );
+ GC gc = XCreateGC( xdisplay, (Pixmap) m_bitmap, 0, NULL );
+
+ XSetForeground( xdisplay, gc, WhitePixel(xdisplay,xscreen) );
+ XSetFillStyle( xdisplay, gc, FillSolid );
+ XFillRectangle( xdisplay, (Pixmap) m_bitmap, gc, 0, 0, image.GetWidth(), image.GetHeight() );
+
+ unsigned char *data = image.GetData();
+ int index = 0;
+
+ unsigned char red = colour.Red();
+ unsigned char green = colour.Green();
+ unsigned char blue = colour.Blue();
+
+ int bpp = wxTheApp->GetVisualInfo(m_display)->m_visualDepth;
+
+ if (bpp == 15)
+ {
+ red &= 0xf8;
+ green &= 0xf8;
+ blue &= 0xf8;
+ } else
+ if (bpp == 16)
+ {
+ red &= 0xf8;
+ green &= 0xfc;
+ blue &= 0xf8;
+ } else
+ if (bpp == 12)
+ {
+ red &= 0xf0;
+ green &= 0xf0;
+ blue &= 0xf0;
+ }
+
+ XSetForeground( xdisplay, gc, BlackPixel(xdisplay,xscreen) );
+
+ int width = image.GetWidth();
+ int height = image.GetHeight();
+ for (int j = 0; j < height; j++)
+ {
+ int start_x = -1;
+ int i;
+ for (i = 0; i < width; i++)
+ {
+ if ((data[index] == red) &&
+ (data[index+1] == green) &&
+ (data[index+2] == blue))
+ {
+ if (start_x == -1)
+ start_x = i;
+ }
+ else
+ {
+ if (start_x != -1)
+ {
+ XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i-1, j );
+ start_x = -1;
+ }
+ }
+ index += 3;
+ }
+ if (start_x != -1)
+ XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i, j );
+ }
+
+ XFreeGC( xdisplay, gc );
+
+ return true;
+#else
+ return false;
+#endif
+ // wxUSE_NANOX
+}
+
+bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
+{
+ unsigned char r,g,b;
+ wxPalette *pal = bitmap.GetPalette();
+
+ wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
+
+ pal->GetRGB(paletteIndex, &r, &g, &b);
+
+ return Create(bitmap, wxColour(r, g, b));
+}
+
+bool wxMask::Create( const wxBitmap& bitmap )
+{
+#if !wxUSE_NANOX
+ if (m_bitmap)
+ {
+ XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
+ m_bitmap = NULL;
+ }
+
+ if (!bitmap.Ok()) return false;
+
+ wxCHECK_MSG( bitmap.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
+
+ m_display = bitmap.GetDisplay();
+
+ int xscreen = DefaultScreen( (Display*) m_display );
+ Window xroot = RootWindow( (Display*) m_display, xscreen );
+
+ m_bitmap = (WXPixmap) XCreatePixmap( (Display*) m_display, xroot, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
+
+ if (!m_bitmap) return false;
+
+ GC gc = XCreateGC( (Display*) m_display, (Pixmap) m_bitmap, 0, NULL );
+
+ XCopyPlane( (Display*) m_display, (Pixmap) bitmap.GetBitmap(), (Pixmap) m_bitmap,
+ gc, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), 0, 0, 1 );
+
+ XFreeGC( (Display*) m_display, gc );
+
+ return true;
+#else
+ return false;