return m_pictHandle ;
 }
 
-void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size);
-
-void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t WXUNUSED(size))
-{
-    wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
-
-    wxASSERT( data == membuf->GetData() ) ;
-
-    delete membuf ;
-}
-
 CGImageRef wxBitmapRefData::CreateCGImage() const
 {
     wxASSERT( m_ok ) ;
             int w = m_width ;
             int h = m_height ;
             CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
-            wxMemoryBuffer* membuf = NULL ;
+            wxMemoryBuffer membuf;
             
             if ( m_bitmapMask )
             {
                 alphaInfo = kCGImageAlphaFirst ;
-                membuf = new wxMemoryBuffer( imageSize ) ;
-                memcpy( membuf->GetData() , dataBuffer , imageSize ) ;
+                unsigned char *destalphastart = (unsigned char*) membuf.GetWriteBuf( imageSize ) ;
+                memcpy( destalphastart , dataBuffer , imageSize ) ;
                 unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ;
                 int maskrowbytes = m_bitmapMask->GetBytesPerRow() ;
-                unsigned char *destalphastart = (unsigned char *) membuf->GetData() ;
                 for ( int y = 0 ; y < h ; ++y , destalphastart += m_bytesPerRow, sourcemaskstart += maskrowbytes)
                 {
                     unsigned char *sourcemask = sourcemaskstart ;
                         *destalpha = 0xFF - *sourcemask ;
                     }
                 }
+                membuf.UngetWriteBuf( imageSize );
             }
             else
             {
 #endif
                 }
                 
-                membuf = new wxMemoryBuffer( m_memBuf ) ;
+                membuf = m_memBuf;
             }
             
             CGDataProviderRef dataProvider = NULL ;
             if ( m_depth == 1 )
             {
-                wxMemoryBuffer* maskBuf = new wxMemoryBuffer( m_width * m_height );
-                unsigned char * maskBufData = (unsigned char *) maskBuf->GetData();
-                unsigned char * bufData = (unsigned char *) membuf->GetData() ;
+                // TODO CHECK ALIGNMENT
+                wxMemoryBuffer maskBuf;
+                unsigned char * maskBufData = (unsigned char*) maskBuf.GetWriteBuf( m_width * m_height );
+                unsigned char * bufData = (unsigned char *) membuf.GetData() ;
                 // copy one color component
-                for( int i = 0 ; i < m_width * m_height ; ++i )
-                    maskBufData[i] = bufData[i*4+3];
+                size_t i = 0;
+                for( int y = 0 ; y < m_height ; bufData+= m_bytesPerRow, ++y )
+                {
+                    unsigned char *bufDataIter = bufData+3;
+                    for ( int x = 0 ; x < m_width ; bufDataIter += 4, ++x, ++i )
+                    {
+                        maskBufData[i] = *bufDataIter;
+                    }
+                }
+                maskBuf.UngetWriteBuf( m_width * m_height );
+                
                 dataProvider =
-                    CGDataProviderCreateWithData(
-                                                 maskBuf , (const void *) maskBufData , m_width * m_height,
-                                                 wxMacMemoryBufferReleaseProc );
-                // as we are now passing the mask buffer to the data provider, we have
-                // to release the membuf ourselves
-                delete membuf ;
+                    wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf );
                 
                 image = ::CGImageMaskCreate( w, h, 8, 8, m_width , dataProvider, NULL, false );
             }
             else
             {
                 CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
-                dataProvider =
-                    CGDataProviderCreateWithData(
-                                                 membuf , (const void *)membuf->GetData() , imageSize,
-                                                 wxMacMemoryBufferReleaseProc );
+                dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( membuf );
                 image =
                     ::CGImageCreate(
                                     w, h, 8 , 32 , m_bytesPerRow , colorSpace, alphaInfo ,
 
         return wxNullGraphicsFont;
 }
 
+//
+// CoreGraphics Helper Methods
+//
+
+// Data Providers and Consumers
+
+size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
+{
+    CFMutableDataRef data = (CFMutableDataRef) info;
+    if ( data )
+    {
+        CFDataAppendBytes( data, (const UInt8*) bytes, count );
+    }
+    return count;
+}
+
+void wxMacReleaseCFDataProviderCallback(void *info,
+                                      const void *WXUNUSED(data),
+                                      size_t WXUNUSED(count))
+{
+    if ( info )
+        CFRelease( (CFDataRef) info );
+}
+
+void wxMacReleaseCFDataConsumerCallback( void *info )
+{
+    if ( info )
+        CFRelease( (CFDataRef) info );
+}
+
+CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data )
+{
+    if ( data == NULL )
+        return NULL;
+
+    return CGDataProviderCreateWithCFData( data );
+}
+
+CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data )
+{
+    if ( data == NULL )
+        return NULL;
+
+    return CGDataConsumerCreateWithCFData( data );
+}
+
+void wxMacReleaseMemoryBufferProviderCallback(void *info, const void *data, size_t WXUNUSED(size))
+{
+    wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
+
+    wxASSERT( data == membuf->GetData() ) ;
+
+    delete membuf ;
+}
+
+CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf )
+{
+    wxMemoryBuffer* b = new wxMemoryBuffer( buf );
+    if ( b->GetDataLen() == 0 )
+        return NULL;
+        
+    return CGDataProviderCreateWithData( b , (const void *) b->GetData() , b->GetDataLen() ,
+                                                 wxMacReleaseMemoryBufferProviderCallback );
+}
\ No newline at end of file
 
 static const int IDM_WINDOWNEXT = 4004;
 static const int IDM_WINDOWTILEVERT = 4005;
 
+// others
+
+void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
+{
+#if 1 // TODO REMOVE
+    if ( inWindowRef )
+    {
+//        bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
+//        if ( inActivate != isHighlighted )
+#ifndef __LP64__
+        GrafPtr port ;
+        GetPort( &port ) ;
+        SetPortWindowPort( inWindowRef ) ;
+#endif
+        HiliteWindow( inWindowRef , inActivate ) ;
+        ControlRef control = NULL ;
+        ::GetRootControl( inWindowRef , &control ) ;
+        if ( control )
+        {
+            if ( inActivate )
+                ::ActivateControl( control ) ;
+            else
+                ::DeactivateControl( control ) ;
+        }
+#ifndef __LP64__
+        SetPort( port ) ;
+#endif
+    }
+#endif
+}
+
 // ----------------------------------------------------------------------------
 // Parent frame
 // ----------------------------------------------------------------------------
 
 
     CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
     m_data.reset(data);
-    CGDataConsumerRef dataConsumer = UMACGDataConsumerCreateWithCFData(data);
+    CGDataConsumerRef dataConsumer = wxMacCGDataConsumerCreateWithCFData(data);
     m_context = CGPDFContextCreate( dataConsumer, (width != 0 && height != 0) ? &r : NULL , NULL );
     CGDataConsumerRelease( dataConsumer );
     if ( m_context )
 
 void wxMetafileRefData::UpdateDocumentFromData()
 {
-    wxCFRef<CGDataProviderRef> provider(UMACGDataProviderCreateWithCFData(m_data));
+    wxCFRef<CGDataProviderRef> provider(wxMacCGDataProviderCreateWithCFData(m_data));
     m_pdfDoc.reset(CGPDFDocumentCreateWithProvider(provider));
     if ( m_pdfDoc != NULL )
     {
     Handle picHandle = (Handle) pictHandle;
     HLock(picHandle);
     CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) *picHandle, GetHandleSize(picHandle), kCFAllocatorNull);
-    wxCFRef<CGDataProviderRef> provider(UMACGDataProviderCreateWithCFData(data));
+    wxCFRef<CGDataProviderRef> provider(wxMacCGDataProviderCreateWithCFData(data));
     QDPictRef pictRef = QDPictCreateWithProvider(provider);
     CGRect rect = QDPictGetBounds(pictRef);
     m_refData = new wxMetafileRefData(wx_static_cast(int, rect.size.width),
 
         return ;
 
     Rect bounds ;
-    UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
+    GetRectInWindowCoords( &bounds );
 
     wxRect visRect = textctrl->MacGetClippedClientRect() ;
     Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
     SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
 
     // calculate the rectangles used by the control
-    UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
+    GetRectInWindowCoords( &bounds );
 
     m_txnControlBounds = bounds ;
     m_txnVisBounds = bounds ;
 
     }
 }
 
-ControlRef wxMacFindSubControl( wxTopLevelWindowMac* toplevelWindow, const Point& location , ControlRef superControl , ControlPartCode *outPart )
-{
-    if ( superControl )
-    {
-        UInt16 childrenCount = 0 ;
-        ControlHandle sibling ;
-        Rect r ;
-        OSStatus err = CountSubControls( superControl , &childrenCount ) ;
-        if ( err == errControlIsNotEmbedder )
-            return NULL ;
-
-        wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
-
-        for ( UInt16 i = childrenCount ; i >=1  ; --i )
-        {
-            err = GetIndexedSubControl( superControl , i , & sibling ) ;
-            if ( err == errControlIsNotEmbedder )
-                return NULL ;
-
-            wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
-            if ( IsControlVisible( sibling ) )
-            {
-                UMAGetControlBoundsInWindowCoords( sibling , &r ) ;
-                if ( MacPtInRect( location , &r ) )
-                {
-                    ControlHandle child = wxMacFindSubControl( toplevelWindow , location , sibling , outPart ) ;
-                    if ( child )
-                    {
-                        return child ;
-                    }
-                    else
-                    {
-                        Point testLocation = location ;
-
-                        if ( toplevelWindow )
-                        {
-                            testLocation.h -= r.left ;
-                            testLocation.v -= r.top ;
-                        }
-
-                        *outPart = TestControl( sibling , testLocation ) ;
-
-                        return sibling ;
-                    }
-                }
-            }
-        }
-    }
-
-    return NULL ;
-}
-
-
 #define NEW_CAPTURE_HANDLING 1
 
 pascal OSStatus
     if ( !wxTopLevelWindowBase::SetBackgroundColour(col) && m_hasBgCol )
         return false ;
     
-    if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) )
+    if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) || col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDocumentWindowBackground)) )
         SetThemeWindowBackground( (WindowRef) m_macWindow,  kThemeBrushDocumentWindowBackground, false ) ;
-    else if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) )
+    else if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) || col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive)) )
         SetThemeWindowBackground( (WindowRef) m_macWindow,  kThemeBrushDialogBackgroundActive, false ) ;
     // TODO BETTER THEME SUPPORT
     return true;
 
 #endif
 }
 
-// process manager
-long UMAGetProcessMode()
-{
-    OSErr err ;
-    ProcessInfoRec processinfo;
-    ProcessSerialNumber procno ;
-
-    procno.highLongOfPSN = 0 ;
-    procno.lowLongOfPSN = kCurrentProcess ;
-    processinfo.processInfoLength = sizeof(ProcessInfoRec);
-    processinfo.processName = NULL;
-#ifndef __LP64__
-    processinfo.processAppSpec = NULL;
-#endif
-
-    err = ::GetProcessInformation( &procno , &processinfo ) ;
-    wxASSERT( err == noErr ) ;
-
-    return processinfo.processMode ;
-}
-
-bool UMAGetProcessModeDoesActivateOnFGSwitch()
-{
-    return UMAGetProcessMode() & modeDoesActivateOnFGSwitch ;
-}
-
 // menu manager
 
 #if wxMAC_USE_COCOA == 0
 
 #endif
 
-// window manager
-
-#if wxMAC_USE_QUICKDRAW
-
-void UMAActivateControl( ControlRef inControl )
-{
-    ::ActivateControl( inControl ) ;
-}
-
-void UMADeactivateControl( ControlRef inControl )
-{
-    ::DeactivateControl( inControl ) ;
-}
-
-// others
-
-void UMAHighlightAndActivateWindow( WindowRef inWindowRef , bool inActivate )
-{
-#if 1 // TODO REMOVE
-    if ( inWindowRef )
-    {
-//        bool isHighlighted = IsWindowHighlited( inWindowRef ) ;
-//        if ( inActivate != isHighlighted )
-#ifndef __LP64__
-        GrafPtr port ;
-        GetPort( &port ) ;
-        SetPortWindowPort( inWindowRef ) ;
-#endif
-        HiliteWindow( inWindowRef , inActivate ) ;
-        ControlRef control = NULL ;
-        ::GetRootControl( inWindowRef , &control ) ;
-        if ( control )
-        {
-            if ( inActivate )
-                UMAActivateControl( control ) ;
-            else
-                UMADeactivateControl( control ) ;
-        }
-#ifndef __LP64__
-        SetPort( port ) ;
-#endif
-    }
-#endif
-}
-
-Rect * UMAGetControlBoundsInWindowCoords( ControlRef theControl, Rect *bounds )
-{
-    GetControlBounds( theControl , bounds ) ;
-    
-    WindowRef tlwref = GetControlOwner( theControl ) ;
-    
-    wxTopLevelWindowMac* tlwwx = wxFindWinFromMacWindow( tlwref ) ;
-    if ( tlwwx != NULL )
-    {
-        ControlRef rootControl = tlwwx->GetPeer()->GetControlRef() ;
-        HIPoint hiPoint = CGPointMake( 0 , 0 ) ;
-        HIViewConvertPoint( &hiPoint , HIViewGetSuperview(theControl) , rootControl ) ;
-        OffsetRect( bounds , (short) hiPoint.x , (short) hiPoint.y ) ;
-    }
-    
-    return bounds ;
-}
-
-#endif
-
-size_t UMAPutBytesCFRefCallback( void *info, const void *bytes, size_t count )
-{
-    CFMutableDataRef data = (CFMutableDataRef) info;
-    if ( data )
-    {
-        CFDataAppendBytes( data, (const UInt8*) bytes, count );
-    }
-    return count;
-}
-
-void UMAReleaseCFDataProviderCallback(void *info,
-                                      const void *WXUNUSED(data),
-                                      size_t WXUNUSED(count))
-{
-    if ( info )
-        CFRelease( (CFDataRef) info );
-}
-
-void UMAReleaseCFDataConsumerCallback( void *info )
-{
-    if ( info )
-        CFRelease( (CFDataRef) info );
-}
-
-CGDataProviderRef UMACGDataProviderCreateWithCFData( CFDataRef data )
-{
-    if ( data == NULL )
-        return NULL;
-
-    return CGDataProviderCreateWithCFData( data );
-}
-
-CGDataConsumerRef UMACGDataConsumerCreateWithCFData( CFMutableDataRef data )
-{
-    if ( data == NULL )
-        return NULL;
-
-    return CGDataConsumerCreateWithCFData( data );
-}
 #endif  // wxUSE_GUI
 
 
 void wxMacControl::GetRectInWindowCoords( Rect *r )
 {
-    UMAGetControlBoundsInWindowCoords( m_controlRef , r );
+    GetControlBounds( m_controlRef , r ) ;
+    
+    WindowRef tlwref = GetControlOwner( m_controlRef ) ;
+    
+    wxTopLevelWindowMac* tlwwx = wxFindWinFromMacWindow( tlwref ) ;
+    if ( tlwwx != NULL )
+    {
+        ControlRef rootControl = tlwwx->GetPeer()->GetControlRef() ;
+        HIPoint hiPoint = CGPointMake( 0 , 0 ) ;
+        HIViewConvertPoint( &hiPoint , HIViewGetSuperview(m_controlRef) , rootControl ) ;
+        OffsetRect( r , (short) hiPoint.x , (short) hiPoint.y ) ;
+    }
 }
 
 void wxMacControl::GetBestRect( Rect *r )