/////////////////////////////////////////////////////////////////////////////
-// Name: bitmap.cpp
+// Name: src/cocoa/bitmap.cpp
// Purpose: wxBitmap
-// Author: AUTHOR
+// Author: David Elliott
// Modified by:
-// Created: ??/??/98
+// Created: 2003/07/19
// RCS-ID: $Id$
-// Copyright: (c) AUTHOR
+// Copyright: (c) 2003 David Elliott
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
-#pragma implementation "bitmap.h"
-#endif
-
-#include "wx/setup.h"
-#include "wx/utils.h"
-#include "wx/palette.h"
+#include "wx/wxprec.h"
+#ifndef WX_PRECOMP
+ #include "wx/log.h"
+ #include "wx/utils.h"
+ #include "wx/palette.h"
+ #include "wx/icon.h"
+#endif //WX_PRECOMP
#include "wx/bitmap.h"
-#include "wx/icon.h"
-#include "wx/log.h"
#include "wx/image.h"
+#include "wx/xpmdecod.h"
-#if !USE_SHARED_LIBRARIES
-IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
-IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
-#endif
+#include "wx/cocoa/autorelease.h"
+#include "wx/cocoa/string.h"
+
+#import <AppKit/NSBitmapImageRep.h>
+#import <AppKit/NSGraphics.h>
+
+// ========================================================================
+// wxBitmapRefData
+// ========================================================================
+class wxBitmapRefData: public wxGDIRefData
+{
+ friend class wxBitmap;
+public:
+ wxBitmapRefData();
+ wxBitmapRefData( const wxBitmapRefData& data );
+ virtual ~wxBitmapRefData();
+
+protected:
+ int m_width;
+ int m_height;
+ int m_depth;
+ bool m_ok;
+ int m_numColors;
+ wxPalette m_bitmapPalette;
+ int m_quality;
+ WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
+ wxMask *m_bitmapMask; // Optional mask
+};
+
+#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
wxBitmapRefData::wxBitmapRefData()
{
m_depth = 0;
m_quality = 0;
m_numColors = 0;
+ m_cocoaNSBitmapImageRep = nil;
m_bitmapMask = NULL;
}
+wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
+{
+ m_width = data.m_width;
+ m_height = data.m_height;
+ m_depth = data.m_depth;
+ m_ok = data.m_ok;
+ m_numColors = data.m_numColors;
+ m_bitmapPalette = data.m_bitmapPalette;
+ m_quality = data.m_quality;
+ m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
+ m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
+}
+
wxBitmapRefData::~wxBitmapRefData()
{
- /*
- * TODO: delete the bitmap data here.
- */
+ [m_cocoaNSBitmapImageRep release];
+ m_cocoaNSBitmapImageRep = NULL;
- if (m_bitmapMask)
- delete m_bitmapMask;
+ delete m_bitmapMask;
m_bitmapMask = NULL;
}
+// ========================================================================
+// wxBitmap
+// ========================================================================
+IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
+
wxBitmap::wxBitmap()
{
m_refData = NULL;
-
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
}
wxBitmap::~wxBitmap()
{
- if (wxTheBitmapList)
- wxTheBitmapList->DeleteObject(this);
}
wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
M_BITMAPDATA->m_numColors = 0;
/* TODO: create the bitmap from data */
-
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
}
wxBitmap::wxBitmap(int w, int h, int d)
{
(void)Create(w, h, d);
-
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
}
wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
{
(void) Create(data, type, width, height, depth);
-
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
}
wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
{
LoadFile(filename, type);
-
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
-}
-
-wxBitmap::wxBitmap(const wxImage& image, int depth)
-{
-}
-
-wxBitmap::wxBitmap(char **bits)
-{
-}
-
-/* TODO: maybe allow creation from XPM
-// Create from data
-wxBitmap::wxBitmap(const char **data)
-{
- (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
}
-*/
-bool wxBitmap::Create(int w, int h, int d)
+wxObjectRefData *wxBitmap::CreateRefData() const
{
- UnRef();
-
- m_refData = new wxBitmapRefData;
-
- M_BITMAPDATA->m_width = w;
- M_BITMAPDATA->m_height = h;
- M_BITMAPDATA->m_depth = d;
-
- /* TODO: create new bitmap */
-
- return M_BITMAPDATA->m_ok;
+ return new wxBitmapRefData;
}
-bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
+wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
{
- UnRef();
-
- m_refData = new wxBitmapRefData;
-
- wxBitmapHandler *handler = FindHandler(type);
-
- if ( handler == NULL ) {
- wxLogWarning("no bitmap handler for type %d defined.", type);
-
- return FALSE;
- }
-
- return handler->LoadFile(this, filename, type, -1, -1);
+ return new wxBitmapRefData(*(wxBitmapRefData*)data);
}
-bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
+WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
{
- UnRef();
-
- m_refData = new wxBitmapRefData;
-
- wxBitmapHandler *handler = FindHandler(type);
-
- if ( handler == NULL ) {
- wxLogWarning("no bitmap handler for type %d defined.", type);
-
- return FALSE;
- }
-
- return handler->Create(this, data, type, width, height, depth);
+ if(!M_BITMAPDATA)
+ return NULL;
+ return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
}
-bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
+void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
{
- wxBitmapHandler *handler = FindHandler(type);
-
- if ( handler == NULL ) {
- wxLogWarning("no bitmap handler for type %d defined.", type);
-
- return FALSE;
- }
-
- return handler->SaveFile(this, filename, type, palette);
+ if(!M_BITMAPDATA)
+ return;
+ // NOTE: No checking is done to make sure width/height agree
+ [bitmapImageRep retain];
+ [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
+ M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
}
void wxBitmap::SetWidth(int w)
M_BITMAPDATA->m_bitmapMask = mask ;
}
-/*
- * wxMask
- */
-
-wxMask::wxMask()
+bool wxBitmap::Ok() const
{
-/* TODO
- m_maskBitmap = 0;
-*/
+ return m_refData && M_BITMAPDATA->m_ok;
}
-// Construct a mask from a bitmap and a colour indicating
-// the transparent area
-wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
+wxPalette* wxBitmap::GetPalette() const
{
-/* TODO
- m_maskBitmap = 0;
-*/
- Create(bitmap, colour);
+ if(!m_refData)
+ return NULL;
+ return &M_BITMAPDATA->m_bitmapPalette;
}
-// Construct a mask from a bitmap and a palette index indicating
-// the transparent area
-wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
+wxMask* wxBitmap::GetMask() const
{
-/* TODO
- m_maskBitmap = 0;
-*/
-
- Create(bitmap, paletteIndex);
+ if(!m_refData)
+ return NULL;
+ return M_BITMAPDATA->m_bitmapMask;
}
-// Construct a mask from a mono bitmap (copies the bitmap).
-wxMask::wxMask(const wxBitmap& bitmap)
+int wxBitmap::GetDepth() const
{
-/* TODO
- m_maskBitmap = 0;
-*/
-
- Create(bitmap);
+ if(!m_refData)
+ return 0;
+ return M_BITMAPDATA->m_depth;
}
-wxMask::~wxMask()
+int wxBitmap::GetWidth() const
{
-// TODO: delete mask bitmap
+ if(!m_refData)
+ return 0;
+ return M_BITMAPDATA->m_width;
}
-// Create a mask from a mono bitmap (copies the bitmap).
-bool wxMask::Create(const wxBitmap& bitmap)
+int wxBitmap::GetHeight() const
{
-// TODO
- return FALSE;
+ if(!m_refData)
+ return 0;
+ return M_BITMAPDATA->m_height;
}
-// Create a mask from a bitmap and a palette index indicating
-// the transparent area
-bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
+bool wxBitmap::Create(int w, int h, int d)
{
-// TODO
- return FALSE;
+ UnRef();
+
+ m_refData = new wxBitmapRefData;
+
+ M_BITMAPDATA->m_width = w;
+ M_BITMAPDATA->m_height = h;
+ M_BITMAPDATA->m_depth = d;
+
+ /* TODO: create new bitmap */
+ M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
+ initWithBitmapDataPlanes: NULL
+ pixelsWide: w
+ pixelsHigh: h
+ bitsPerSample: 8
+ samplesPerPixel: 3
+ hasAlpha: NO
+ isPlanar: NO
+ colorSpaceName: NSCalibratedRGBColorSpace
+ bytesPerRow: 0
+ bitsPerPixel: 0];
+
+ wxLogDebug("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p", M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
+ M_BITMAPDATA->m_ok = true;
+ M_BITMAPDATA->m_numColors = 0;
+ M_BITMAPDATA->m_quality = 0;
+ M_BITMAPDATA->m_bitmapMask = NULL;
+
+ return M_BITMAPDATA->m_ok;
}
-// Create a mask from a bitmap and a colour indicating
-// the transparent area
-bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
+bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
{
-// TODO
- return FALSE;
-}
+ wxAutoNSAutoreleasePool pool;
+ UnRef();
-/*
- * wxBitmapHandler
- */
+ m_refData = new wxBitmapRefData;
-IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
+ NSBitmapImageRep *imageRep = [NSBitmapImageRep
+ imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
-wxBitmapHandler::~wxBitmapHandler()
-{
+ if(imageRep)
+ {
+ M_BITMAPDATA->m_width = [imageRep pixelsWide];
+ M_BITMAPDATA->m_height = [imageRep pixelsHigh];
+ M_BITMAPDATA->m_depth = 24; // FIXME
+ M_BITMAPDATA->m_ok = true;
+ M_BITMAPDATA->m_numColors = 0;
+ M_BITMAPDATA->m_quality = 0;
+ M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
+ M_BITMAPDATA->m_bitmapMask = NULL;
+ return true;
+ }
+ wxImage image;
+ if(!image.LoadFile(filename,type))
+ return false;
+ if(!image.Ok())
+ return false;
+ *this = wxBitmap(image);
+ return true;
}
-bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
+bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
{
- return FALSE;
+ UnRef();
+
+ m_refData = new wxBitmapRefData;
+
+ return false;
}
-bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type,
- int desiredWidth, int desiredHeight)
+bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
{
- return FALSE;
+ return false;
}
-bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
+bool wxBitmap::CopyFromIcon(const wxIcon& icno)
{
- return FALSE;
+ return false;
}
-/*
- * Standard handlers
- */
-
-/* TODO: bitmap handlers, a bit like this:
-class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
+wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
{
- DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
-public:
- inline wxBMPResourceHandler()
- {
- m_name = "Windows bitmap resource";
- m_extension = "";
- m_type = wxBITMAP_TYPE_BMP_RESOURCE;
- };
-
- virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
- int desiredWidth, int desiredHeight);
-};
-IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
-*/
+ return wxNullBitmap;
+}
-void wxBitmap::InitStandardHandlers()
+wxImage wxBitmap::ConvertToImage() const
{
-/* TODO: initialize all standard bitmap or derive class handlers here.
- AddHandler(new wxBMPResourceHandler);
- AddHandler(new wxBMPFileHandler);
- AddHandler(new wxXPMFileHandler);
- AddHandler(new wxXPMDataHandler);
- AddHandler(new wxICOResourceHandler);
- AddHandler(new wxICOFileHandler);
-*/
+ if(!M_BITMAPDATA->m_ok)
+ return wxImage(5,5)/*wxNullImage*/;
+ return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
}
-bool wxBitmap::CopyFromIcon(const wxIcon& icno)
+bool wxBitmap::CreateFromXpm(const char **xpm)
{
+#if wxUSE_IMAGE && wxUSE_XPM
+ UnRef();
+
+ wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
+
+ wxXPMDecoder decoder;
+ wxImage img = decoder.ReadData(xpm);
+ wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
+
+ *this = wxBitmap(img);
+ return true;
+#else
return false;
+#endif
}
-wxPalette* wxBitmap::GetPalette() const
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
{
- return NULL;
+ UnRef();
+
+ wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
+ wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
+
+ m_refData = new wxBitmapRefData();
+
+ M_BITMAPDATA->m_width = image.GetWidth();
+ M_BITMAPDATA->m_height = image.GetHeight();
+ NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
+ initWithBitmapDataPlanes: NULL
+ pixelsWide: image.GetWidth()
+ pixelsHigh: image.GetHeight()
+ bitsPerSample: 8
+ samplesPerPixel: 3
+ hasAlpha: NO
+ isPlanar: NO
+ colorSpaceName: NSCalibratedRGBColorSpace
+ bytesPerRow: 0
+ bitsPerPixel: 0];
+
+ const int numBytes = image.GetWidth()*image.GetHeight()*3;
+ memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
+ // TODO: Alpha and convert to desired depth
+ M_BITMAPDATA->m_depth = 24;
+ M_BITMAPDATA->m_ok = true;
+ M_BITMAPDATA->m_numColors = 0;
+ M_BITMAPDATA->m_quality = 0;
+ M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
+ M_BITMAPDATA->m_bitmapMask = NULL;
+ return true;
}
-wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
+// ========================================================================
+// wxMask
+// ========================================================================
+
+IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
+
+wxMask::wxMask()
{
- return wxNullBitmap;
+/* TODO
+ m_maskBitmap = 0;
+*/
}
-wxImage wxBitmap::ConvertToImage() const
+// Construct a mask from a bitmap and a colour indicating
+// the transparent area
+wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
{
- return wxNullImage;
+/* TODO
+ m_maskBitmap = 0;
+*/
+ Create(bitmap, colour);
}
-bool wxBitmap::Ok() const
+// Construct a mask from a bitmap and a palette index indicating
+// the transparent area
+wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
{
- return FALSE;
+/* TODO
+ m_maskBitmap = 0;
+*/
+
+ Create(bitmap, paletteIndex);
}
-wxMask* wxBitmap::GetMask() const
+// Construct a mask from a mono bitmap (copies the bitmap).
+wxMask::wxMask(const wxBitmap& bitmap)
{
- return NULL;
+/* TODO
+ m_maskBitmap = 0;
+*/
+
+ Create(bitmap);
}
-int wxBitmap::GetDepth() const
+wxMask::~wxMask()
{
- return 0;
+// TODO: delete mask bitmap
}
-int wxBitmap::GetWidth() const
+// Create a mask from a mono bitmap (copies the bitmap).
+bool wxMask::Create(const wxBitmap& bitmap)
{
- return 0;
+// TODO
+ return FALSE;
}
-int wxBitmap::GetHeight() const
+// Create a mask from a bitmap and a palette index indicating
+// the transparent area
+bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
{
- return 0;
+// TODO
+ return FALSE;
}
-
-bool wxBitmap::CreateFromXpm(const char **)
+// Create a mask from a bitmap and a colour indicating
+// the transparent area
+bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
{
- return false;
+// TODO
+ return FALSE;
}
-// vim:sts=4:sw=4:syn=cpp:et