From 968c951fe1f6aba8276d6585c452b89bfa95993d Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 23 Nov 2007 19:25:21 +0000 Subject: [PATCH] mac cleanup git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@50194 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/mac/carbon/bitmap.cpp | 107 +++++++++++++++++++++++++++++--- src/mac/carbon/cursor.cpp | 11 ++-- src/mac/carbon/dataobj.cpp | 2 +- src/mac/carbon/dataview.cpp | 6 +- src/mac/carbon/graphics.cpp | 4 +- src/mac/carbon/icon.cpp | 92 ++++++++++++++++----------- src/mac/carbon/listctrl_mac.cpp | 8 +-- src/mac/carbon/taskbar.cpp | 2 +- 8 files changed, 170 insertions(+), 62 deletions(-) diff --git a/src/mac/carbon/bitmap.cpp b/src/mac/carbon/bitmap.cpp index 0f3bdabdd1..2fdb2a0d65 100644 --- a/src/mac/carbon/bitmap.cpp +++ b/src/mac/carbon/bitmap.cpp @@ -43,6 +43,83 @@ IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) // under Quartz then content is transformed into a CGImageRef representing the same data // which can be transferred to the GPU by the OS for fast rendering +class WXDLLEXPORT wxBitmapRefData: public wxGDIRefData +{ + friend class WXDLLIMPEXP_FWD_CORE wxIcon; + friend class WXDLLIMPEXP_FWD_CORE wxCursor; +public: + wxBitmapRefData(int width , int height , int depth); + wxBitmapRefData(); + wxBitmapRefData(const wxBitmapRefData &tocopy); + + virtual ~wxBitmapRefData(); + + void Free(); + bool Ok() const { return IsOk(); } + bool IsOk() const { return m_ok; } + void SetOk( bool isOk) { m_ok = isOk; } + + void SetWidth( int width ) { m_width = width; } + void SetHeight( int height ) { m_height = height; } + void SetDepth( int depth ) { m_depth = depth; } + + int GetWidth() const { return m_width; } + int GetHeight() const { return m_height; } + int GetDepth() const { return m_depth; } + + void *GetRawAccess() const; + void *BeginRawAccess(); + void EndRawAccess(); + + bool HasAlpha() const { return m_hasAlpha; } + void UseAlpha( bool useAlpha ); + +public: +#if wxUSE_PALETTE + wxPalette m_bitmapPalette; +#endif // wxUSE_PALETTE + + wxMask * m_bitmapMask; // Optional mask + CGImageRef CreateCGImage() const; + + // returns true if the bitmap has a size that + // can be natively transferred into a true icon + // if no is returned GetIconRef will still produce + // an icon but it will be generated via a PICT and + // rescaled to 16 x 16 + bool HasNativeSize(); + + // caller should increase ref count if needed longer + // than the bitmap exists + IconRef GetIconRef(); + + // returns a Pict from the bitmap content + PicHandle GetPictHandle(); + + CGContextRef GetBitmapContext() const; + + int GetBytesPerRow() const { return m_bytesPerRow; } + private : + bool Create(int width , int height , int depth); + void Init(); + + int m_width; + int m_height; + int m_bytesPerRow; + int m_depth; + bool m_hasAlpha; + wxMemoryBuffer m_memBuf; + int m_rawAccessCount; + bool m_ok; + mutable CGImageRef m_cgImageRef; + + IconRef m_iconRef; + PicHandle m_pictHandle; + + CGContextRef m_hBitmap; +}; + + #define wxMAC_USE_PREMULTIPLIED_ALPHA 1 static const int kBestByteAlignement = 16; static const int kMaskBytesPerPixel = 1; @@ -52,7 +129,9 @@ static int GetBestBytesPerRow( int rawBytes ) return (((rawBytes)+kBestByteAlignement-1) & ~(kBestByteAlignement-1) ); } -#if wxUSE_BMPBUTTON +#if wxUSE_GUI + +// this is used for more controls than just the wxBitmap button, also for notebooks etc void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType ) { @@ -95,7 +174,7 @@ void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bi else if ( forceType == kControlContentCGImageRef ) { info->contentType = kControlContentCGImageRef ; - info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ; + info->u.imageRef = (CGImageRef) bmap->CreateCGImage() ; } else { @@ -112,7 +191,7 @@ CGImageRef wxMacCreateCGImageFromBitmap( const wxBitmap& bitmap ) wxBitmapRefData * bmap = bitmap.GetBitmapData() ; if ( bmap == NULL ) return NULL ; - return (CGImageRef) bmap->CGImageCreate(); + return (CGImageRef) bmap->CreateCGImage(); } void wxMacReleaseBitmapButton( ControlButtonContentInfo*info ) @@ -505,7 +584,7 @@ PicHandle wxBitmapRefData::GetPictHandle() { // QT does not correctly export the mask // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands - CGImageRef imageRef = CGImageCreate(); + CGImageRef imageRef = CreateCGImage(); err = GraphicsExportSetInputCGImage( exporter, imageRef ); err = GraphicsExportSetOutputHandle(exporter, (Handle)m_pictHandle); err = GraphicsExportDoExport(exporter, NULL); @@ -536,7 +615,7 @@ void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t WXUNUSED( delete membuf ; } -CGImageRef wxBitmapRefData::CGImageCreate() const +CGImageRef wxBitmapRefData::CreateCGImage() const { wxASSERT( m_ok ) ; wxASSERT( m_rawAccessCount >= 0 ) ; @@ -902,11 +981,25 @@ void wxBitmap::EndRawAccess() M_BITMAPDATA->EndRawAccess() ; } -WXCGIMAGEREF wxBitmap::CGImageCreate() const +CGImageRef wxBitmap::CreateCGImage() const { wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; - return M_BITMAPDATA->CGImageCreate() ; + return M_BITMAPDATA->CreateCGImage() ; +} + +IconRef wxBitmap::GetIconRef() const +{ + wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; + + return M_BITMAPDATA->GetIconRef() ; +} + +IconRef wxBitmap::CreateIconRef() const +{ + IconRef icon = GetIconRef(); + verify_noerr( AcquireIconRef(icon) ); + return icon; } wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const diff --git a/src/mac/carbon/cursor.cpp b/src/mac/carbon/cursor.cpp index 2dab20662a..e8f6425947 100644 --- a/src/mac/carbon/cursor.cpp +++ b/src/mac/carbon/cursor.cpp @@ -24,16 +24,15 @@ #include "wx/mac/private.h" -IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap) +IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject) -class WXDLLEXPORT wxCursorRefData: public wxBitmapRefData +class WXDLLEXPORT wxCursorRefData: public wxGDIRefData { - DECLARE_NO_COPY_CLASS(wxCursorRefData) - - friend class wxBitmap; friend class wxCursor; + DECLARE_NO_COPY_CLASS(wxCursorRefData) + public: wxCursorRefData(); virtual ~wxCursorRefData(); @@ -199,8 +198,6 @@ CursHandle wxGetStockCursor( int number ) wxCursorRefData::wxCursorRefData() { - SetWidth( 16 ); - SetHeight( 16 ); m_hCursor = NULL; #if wxMAC_USE_COCOA #else diff --git a/src/mac/carbon/dataobj.cpp b/src/mac/carbon/dataobj.cpp index 7a7a181d95..e7070a1e44 100644 --- a/src/mac/carbon/dataobj.cpp +++ b/src/mac/carbon/dataobj.cpp @@ -647,7 +647,7 @@ void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap ) wxBitmapDataObjectBase::SetBitmap( rBitmap ); if (m_bitmap.Ok()) { - CGImageRef cgImageRef = (CGImageRef) m_bitmap.CGImageCreate(); + CGImageRef cgImageRef = (CGImageRef) m_bitmap.CreateCGImage(); CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0); CGImageDestinationRef destination = CGImageDestinationCreateWithData( data , kUTTypeTIFF , 1 , NULL ); diff --git a/src/mac/carbon/dataview.cpp b/src/mac/carbon/dataview.cpp index 8cdb895a53..dc95fa24c1 100644 --- a/src/mac/carbon/dataview.cpp +++ b/src/mac/carbon/dataview.cpp @@ -169,7 +169,7 @@ static bool InitializeColumnDescription(DataBrowserListViewColumnDesc& columnDes columnDescription.headerBtnDesc.btnFontStyle.style = normal; columnDescription.headerBtnDesc.btnContentInfo.contentType = kControlContentIconRef; if (columnPtr->GetBitmap().Ok()) - columnDescription.headerBtnDesc.btnContentInfo.u.iconRef = columnPtr->GetBitmap().GetBitmapData()->GetIconRef(); + columnDescription.headerBtnDesc.btnContentInfo.u.iconRef = columnPtr->GetBitmap().GetIconRef(); // done: return true; } /* InitializeColumnDescription(DataBrowserListViewColumnDesc&, wxDataViewColumn const*, DataBrowserPropertyID, wxMacCFStringHolder const&) */ @@ -536,7 +536,7 @@ bool wxDataViewBitmapRenderer::Render(void) bitmap << this->GetValue(); if (bitmap.Ok()) - return (::SetDataBrowserItemDataIcon(this->GetDataReference(),bitmap.GetBitmapData()->GetIconRef()) == noErr); + return (::SetDataBrowserItemDataIcon(this->GetDataReference(),bitmap.GetIconRef()) == noErr); else return true; } /* if */ @@ -734,7 +734,7 @@ void wxDataViewColumn::SetBitmap(wxBitmap const& bitmap) wxCHECK_RET(macDataViewListCtrlPtr->GetHeaderDesc(this->GetPropertyID(),&headerDescription) == noErr,_("Could not get header description.")); if (this->GetBitmap().Ok()) - headerDescription.btnContentInfo.u.iconRef = this->GetBitmap().GetBitmapData()->GetIconRef(); + headerDescription.btnContentInfo.u.iconRef = this->GetBitmap().GetIconRef(); else headerDescription.btnContentInfo.u.iconRef = NULL; wxCHECK_RET(macDataViewListCtrlPtr->SetHeaderDesc(this->GetPropertyID(),&headerDescription) == noErr,_("Could not set icon.")); diff --git a/src/mac/carbon/graphics.cpp b/src/mac/carbon/graphics.cpp index 0f7889d7a9..d0f3507011 100644 --- a/src/mac/carbon/graphics.cpp +++ b/src/mac/carbon/graphics.cpp @@ -117,7 +117,7 @@ public : { wxASSERT( bmp && bmp->Ok() ); - Init( (CGImageRef) bmp->CGImageCreate() , transform ); + Init( (CGImageRef) bmp->CreateCGImage() , transform ); } // ImagePattern takes ownership of CGImageRef passed in @@ -1650,7 +1650,7 @@ void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDo { EnsureIsValid(); - CGImageRef image = (CGImageRef)( bmp.CGImageCreate() ); + CGImageRef image = (CGImageRef)( bmp.CreateCGImage() ); HIRect r = CGRectMake( x , y , w , h ); if ( bmp.GetDepth() == 1 ) { diff --git a/src/mac/carbon/icon.cpp b/src/mac/carbon/icon.cpp index 3457355e0c..2d7a1a16b2 100644 --- a/src/mac/carbon/icon.cpp +++ b/src/mac/carbon/icon.cpp @@ -23,6 +23,56 @@ IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap) #define M_ICONDATA ((wxIconRefData *)m_refData) +class WXDLLEXPORT wxIconRefData : public wxGDIRefData +{ +public: + wxIconRefData(); + wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight ); + virtual ~wxIconRefData() { Free(); } + + void Init(); + virtual void Free(); + + void SetWidth( int width ) { m_width = width; } + void SetHeight( int height ) { m_height = height; } + + int GetWidth() const { return m_width; } + int GetHeight() const { return m_height; } + + WXHICON GetHICON() const { return (WXHICON) m_iconRef; } + private : + IconRef m_iconRef; + int m_width; + int m_height; +}; + + +wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight ) +{ + m_iconRef = MAC_WXHICON( icon ) ; + + // Standard sizes + SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; + SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; +} + +void wxIconRefData::Init() +{ + m_iconRef = NULL ; +} + +void wxIconRefData::Free() +{ + if ( m_iconRef ) + { + ReleaseIconRef( m_iconRef ) ; + m_iconRef = NULL ; + } +} + +// +// +// wxIcon::wxIcon() { @@ -60,12 +110,7 @@ wxIcon::wxIcon(WXHICON icon, const wxSize& size) if (icon) AcquireIconRef( (IconRef) icon ) ; - m_refData = new wxIconRefData( icon ) ; - if ( (size.x != -1) && (size.y != -1) ) - { - M_ICONDATA->SetWidth( size.x ) ; - M_ICONDATA->SetHeight( size.y ) ; - } + m_refData = new wxIconRefData( icon, size.x, size.y ) ; } wxIcon::~wxIcon() @@ -180,7 +225,7 @@ bool wxIcon::LoadFile( } if ( iconRef ) { - m_refData = new wxIconRefData( (WXHICON) iconRef ) ; + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; return true ; } } @@ -191,7 +236,7 @@ bool wxIcon::LoadFile( verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ; if ( iconRef ) { - m_refData = new wxIconRefData( (WXHICON) iconRef ) ; + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; return true ; } @@ -244,35 +289,8 @@ void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) UnRef() ; // as the bitmap owns that ref, we have to acquire it as well - IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ; - AcquireIconRef( iconRef ) ; - - m_refData = new wxIconRefData( (WXHICON) iconRef ) ; - M_ICONDATA->SetWidth( bmp.GetWidth() ) ; - M_ICONDATA->SetHeight( bmp.GetHeight() ) ; -} - -wxIconRefData::wxIconRefData( WXHICON icon ) -{ - m_iconRef = MAC_WXHICON( icon ) ; - - // Standard sizes - SetWidth( 32 ) ; - SetHeight( 32 ) ; -} - -void wxIconRefData::Init() -{ - m_iconRef = NULL ; -} - -void wxIconRefData::Free() -{ - if ( m_iconRef ) - { - ReleaseIconRef( m_iconRef ) ; - m_iconRef = NULL ; - } + IconRef iconRef = bmp.CreateIconRef() ; + m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ; } IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) diff --git a/src/mac/carbon/listctrl_mac.cpp b/src/mac/carbon/listctrl_mac.cpp index 5e9a53567e..66046b29ca 100644 --- a/src/mac/carbon/listctrl_mac.cpp +++ b/src/mac/carbon/listctrl_mac.cpp @@ -980,13 +980,13 @@ bool wxListCtrl::SetColumn(int col, wxListItem& item) if (item.GetMask() & wxLIST_MASK_IMAGE && item.GetImage() != -1 ) { - columnDesc.btnContentInfo.contentType = kControlContentIconRef; wxImageList* imageList = GetImageList(wxIMAGE_LIST_SMALL); if (imageList && imageList->GetImageCount() > 0 ) { wxBitmap bmp = imageList->GetBitmap( item.GetImage() ); - IconRef icon = bmp.GetBitmapData()->GetIconRef(); + IconRef icon = bmp.GetIconRef(); columnDesc.btnContentInfo.u.iconRef = icon; + columnDesc.btnContentInfo.contentType = kControlContentIconRef; } } @@ -2754,7 +2754,7 @@ void wxMacDataBrowserListCtrlControl::DrawItem( wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL); if (imageList && imageList->GetImageCount() > 0){ wxBitmap bmp = imageList->GetBitmap(imgIndex); - IconRef icon = bmp.GetBitmapData()->GetIconRef(); + IconRef icon = bmp.GetIconRef(); CGContextSaveGState(context); CGContextTranslateCTM(context, 0,iconCGRect.origin.y + CGRectGetMaxY(iconCGRect)); @@ -2895,7 +2895,7 @@ OSStatus wxMacDataBrowserListCtrlControl::GetSetItemData(DataBrowserItemID itemI wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL); if (imageList && imageList->GetImageCount() > 0){ wxBitmap bmp = imageList->GetBitmap(imgIndex); - IconRef icon = bmp.GetBitmapData()->GetIconRef(); + IconRef icon = bmp.GetIconRef(); ::SetDataBrowserItemDataIcon(itemData, icon); } } diff --git a/src/mac/carbon/taskbar.cpp b/src/mac/carbon/taskbar.cpp index 6f12415372..9bb02f5124 100644 --- a/src/mac/carbon/taskbar.cpp +++ b/src/mac/carbon/taskbar.cpp @@ -380,7 +380,7 @@ bool wxDockTaskBarIcon::SetIcon(const wxIcon& icon, const wxString& WXUNUSED(too // get the CGImageRef for the wxBitmap: // OSX builds only, but then the dock only exists in OSX - CGImageRef pImage = (CGImageRef) bmp.CGImageCreate(); + CGImageRef pImage = (CGImageRef) bmp.CreateCGImage(); wxASSERT( pImage != NULL ); // actually set the dock image -- 2.45.2