///////////////////////////////////////////////////////////////////////////// // Name: _bitmap.i // Purpose: SWIG interface for wxBitmap and wxMask // // Author: Robin Dunn // // Created: 7-July-1997 // RCS-ID: $Id$ // Copyright: (c) 2003 by Total Control Software // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// // Not a %module %{ #include %} //--------------------------------------------------------------------------- %{ #include static char** ConvertListOfStrings(PyObject* listOfStrings) { char** cArray = NULL; int count; if (!PyList_Check(listOfStrings)) { PyErr_SetString(PyExc_TypeError, "Expected a list of strings."); return NULL; } count = PyList_Size(listOfStrings); cArray = new char*[count]; for(int x=0; x wx.Bitmap converstions. :see: `wx.EmptyBitmap`, `wx.BitmapFromIcon`, `wx.BitmapFromImage`, `wx.BitmapFromXPMData`, `wx.BitmapFromBits`, `wx.BitmapFromBuffer`, `wx.BitmapFromBufferRGBA`, `wx.Image` "); MustHaveApp(wxBitmap); class wxBitmap : public wxGDIObject { public: DocCtorStr( wxBitmap(const wxString& name, wxBitmapType type=wxBITMAP_TYPE_ANY), "Loads a bitmap from a file.", " :param name: Name of the file to load the bitmap from. :param type: The type of image to expect. Can be one of the following constants (assuming that the neccessary `wx.Image` handlers are loaded): * wx.BITMAP_TYPE_ANY * wx.BITMAP_TYPE_BMP * wx.BITMAP_TYPE_ICO * wx.BITMAP_TYPE_CUR * wx.BITMAP_TYPE_XBM * wx.BITMAP_TYPE_XPM * wx.BITMAP_TYPE_TIF * wx.BITMAP_TYPE_GIF * wx.BITMAP_TYPE_PNG * wx.BITMAP_TYPE_JPEG * wx.BITMAP_TYPE_PNM * wx.BITMAP_TYPE_PCX * wx.BITMAP_TYPE_PICT * wx.BITMAP_TYPE_ICON * wx.BITMAP_TYPE_ANI * wx.BITMAP_TYPE_IFF :see: Alternate constructors `wx.EmptyBitmap`, `wx.BitmapFromIcon`, `wx.BitmapFromImage`, `wx.BitmapFromXPMData`, `wx.BitmapFromBits` "); ~wxBitmap(); DocCtorStrName( wxBitmap(int width, int height, int depth=-1), "Creates a new bitmap of the given size. A depth of -1 indicates the depth of the current screen or visual. Some platforms only support 1 for monochrome and -1 for the current display depth.", "", EmptyBitmap); DocCtorStrName( wxBitmap(const wxIcon& icon), "Create a new bitmap from a `wx.Icon` object.", "", BitmapFromIcon); DocCtorStrName( wxBitmap(const wxImage& image, int depth=-1), "Creates bitmap object from a `wx.Image`. This has to be done to actually display a `wx.Image` as you cannot draw an image directly on a window. The resulting bitmap will use the provided colour depth (or that of the current screen colour depth if depth is -1) which entails that a colour reduction may have to take place.", "", BitmapFromImage); %extend { %RenameDocCtor( BitmapFromXPMData, "Construct a Bitmap from a list of strings formatted as XPM data.", "", wxBitmap(PyObject* listOfStrings)) { char** cArray = NULL; wxBitmap* bmp; cArray = ConvertListOfStrings(listOfStrings); if (! cArray) return NULL; bmp = new wxBitmap(cArray); delete [] cArray; return bmp; } %RenameDocCtor( BitmapFromBits, "Creates a bitmap from an array of bits. You should only use this function for monochrome bitmaps (depth 1) in portable programs: in this case the bits parameter should contain an XBM image. For other bit depths, the behaviour is platform dependent.", "", wxBitmap(PyObject* bits, int width, int height, int depth=1 )) { char* buf; Py_ssize_t length; PyString_AsStringAndSize(bits, &buf, &length); return new wxBitmap(buf, width, height, depth); } } // wxGDIImage methods #ifdef __WXMSW__ long GetHandle(); %extend { void SetHandle(long handle) { self->SetHandle((WXHANDLE)handle); } } #endif bool Ok(); DocDeclStr( int , GetWidth(), "Gets the width of the bitmap in pixels.", ""); DocDeclStr( int , GetHeight(), "Gets the height of the bitmap in pixels.", ""); DocDeclStr( int , GetDepth(), "Gets the colour depth of the bitmap. A value of 1 indicates a monochrome bitmap.", ""); %extend { DocStr(GetSize, "Get the size of the bitmap.", ""); wxSize GetSize() { wxSize size(self->GetWidth(), self->GetHeight()); return size; } } DocDeclStr( virtual wxImage , ConvertToImage() const, "Creates a platform-independent image from a platform-dependent bitmap. This preserves mask information so that bitmaps and images can be converted back and forth without loss in that respect.", ""); DocDeclStr( virtual wxMask* , GetMask() const, "Gets the associated mask (if any) which may have been loaded from a file or explpicitly set for the bitmap. :see: `SetMask`, `wx.Mask` ", ""); // MSW only? wxBitmap GetMaskBitmap() const; %disownarg(wxMask*); DocDeclStr( virtual void , SetMask(wxMask* mask), "Sets the mask for this bitmap. :see: `GetMask`, `wx.Mask` ", ""); %cleardisown(wxMask*); %extend { DocStr(SetMaskColour, "Create a Mask based on a specified colour in the Bitmap.", ""); void SetMaskColour(const wxColour& colour) { wxMask *mask = new wxMask(*self, colour); self->SetMask(mask); } } DocDeclStr( virtual wxBitmap , GetSubBitmap(const wxRect& rect) const, "Returns a sub-bitmap of the current one as long as the rect belongs entirely to the bitmap. This function preserves bit depth and mask information.", ""); DocDeclStr( virtual bool , SaveFile(const wxString &name, wxBitmapType type, wxPalette *palette = NULL), "Saves a bitmap in the named file. See `__init__` for a description of the ``type`` parameter.", ""); DocDeclStr( virtual bool , LoadFile(const wxString &name, wxBitmapType type), "Loads a bitmap from a file. See `__init__` for a description of the ``type`` parameter.", ""); virtual wxPalette *GetPalette() const; #ifdef __WXMSW__ virtual void SetPalette(const wxPalette& palette); #endif virtual bool CopyFromIcon(const wxIcon& icon); DocDeclStr( virtual void , SetHeight(int height), "Set the height property (does not affect the existing bitmap data).", ""); DocDeclStr( virtual void , SetWidth(int width), "Set the width property (does not affect the existing bitmap data).", ""); DocDeclStr( virtual void , SetDepth(int depth), "Set the depth property (does not affect the existing bitmap data).", ""); %extend { DocStr(SetSize, "Set the bitmap size (does not affect the existing bitmap data).", ""); void SetSize(const wxSize& size) { self->SetWidth(size.x); self->SetHeight(size.y); } } #ifdef __WXMSW__ bool CopyFromCursor(const wxCursor& cursor); // WXWIN_COMPATIBILITY_2_4 #if 0 int GetQuality(); void SetQuality(int q); %pythoncode { GetQuality = wx._deprecated(GetQuality) } %pythoncode { SetQuality = wx._deprecated(SetQuality) } #endif #endif %pythoncode { def __nonzero__(self): return self.Ok() } %extend { bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : false; } bool __ne__(const wxBitmap* other) { return other ? (*self != *other) : true; } } }; //--------------------------------------------------------------------------- // Factory functions for creating wxBitmaps from Python buffer objects. They // use the Abstract Pixel API to be able to set RGB and A bytes directly into // the wxBitmap's pixel buffer. %{ // See http://tinyurl.com/e5adr for what premultiplying alpha means. It // appears to me that the other platforms are already doing it, so I'll just // automatically do it for wxMSW here. #ifdef __WXMSW__ #define wxPy_premultiply(p, a) ((p) * (a) / 256) #else #define wxPy_premultiply(p, a) (p) #endif %} %newobject _BitmapFromBufferAlpha; %newobject _BitmapFromBuffer; %inline %{ wxBitmap* _BitmapFromBufferAlpha(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE) { if (DATASIZE != width*height*3) { wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); return NULL; } if (ALPHASIZE != width*height) { wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size."); return NULL; } wxBitmap* bmp = new wxBitmap(width, height, 32); wxAlphaPixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height)); if (! pixels) { // raise an exception... wxPyErr_SetString(PyExc_RuntimeError, "Failed to gain raw access to bitmap data."); return NULL; } pixels.UseAlpha(); wxAlphaPixelData::Iterator p(pixels); for (int y=0; y