]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dataobj.cpp
   1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/mac/carbon/dataobj.cpp 
   3 // Purpose:     implementation of wxDataObject class 
   4 // Author:      Stefan Csomor 
   8 // Copyright:   (c) 1999 Stefan Csomor 
   9 // Licence:     wxWindows licence 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  17 #include "wx/dataobj.h" 
  22     #include "wx/dcmemory.h" 
  26 #include "wx/mstream.h" 
  27 #include "wx/metafile.h" 
  28 #include "wx/tokenzr.h" 
  30 #include "wx/mac/private.h" 
  37 // ---------------------------------------------------------------------------- 
  39 // ---------------------------------------------------------------------------- 
  41 wxDataFormat::wxDataFormat() 
  43     m_type 
= wxDF_INVALID
; 
  47 wxDataFormat::wxDataFormat( wxDataFormatId vType 
) 
  52 wxDataFormat::wxDataFormat( const wxString
& rId 
) 
  57 wxDataFormat::wxDataFormat( NativeFormat vFormat 
) 
  62 void wxDataFormat::SetType( wxDataFormatId dataType 
) 
  69         m_format 
= kScrapFlavorTypeText
; 
  72     case wxDF_UNICODETEXT
: 
  73         m_format 
= kScrapFlavorTypeUnicode
; 
  78         m_format 
= kScrapFlavorTypePicture
; 
  82         m_format 
= kDragFlavorTypeHFS
; 
  86        wxFAIL_MSG( wxT("invalid data format") ); 
  88        // NB: this translates to '????' ASCII but it can't be used in the code 
  89        // because '??' will get parsed as a trigraph! 
  90        m_format 
= 0x3f3f3f3f; 
  95 wxString 
wxDataFormat::GetId() const 
  97     wxCHECK_MSG( !IsStandard(), wxEmptyString
, 
  98                  wxT("name of predefined format cannot be retrieved") ); 
 103 void wxDataFormat::SetId( NativeFormat format 
) 
 109     case kScrapFlavorTypeText
: 
 113     case kScrapFlavorTypeUnicode
: 
 114         m_type 
= wxDF_UNICODETEXT
; 
 117     case kScrapFlavorTypePicture
: 
 118         m_type 
= wxDF_BITMAP
; 
 121     case kDragFlavorTypeHFS
: 
 122         m_type 
= wxDF_FILENAME
; 
 126         m_type 
= wxDF_PRIVATE
; 
 128         memcpy( text
, (const char*)&format
, 4 ); 
 130         m_id 
= wxString::FromAscii( text 
); 
 135 void wxDataFormat::SetId( const wxString
& zId 
) 
 137     m_type 
= wxDF_PRIVATE
; 
 142 bool wxDataFormat::operator==(const wxDataFormat
& format
) const 
 144     if (IsStandard() || format
.IsStandard()) 
 145         return (format
.m_type 
== m_type
); 
 147         return (m_id 
== format
.m_id
); 
 150 //------------------------------------------------------------------------- 
 152 //------------------------------------------------------------------------- 
 154 wxDataObject::wxDataObject() 
 158 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir 
) const 
 160     size_t nFormatCount 
= GetFormatCount( vDir 
); 
 163     if (nFormatCount 
== 1) 
 165         found 
= (rFormat 
== GetPreferredFormat()); 
 169         wxDataFormat 
*pFormats 
= new wxDataFormat
[nFormatCount
]; 
 170         GetAllFormats( pFormats
, vDir 
); 
 172         for (size_t n 
= 0; n 
< nFormatCount
; n
++) 
 174             if (pFormats
[n
] == rFormat
) 
 187 // ---------------------------------------------------------------------------- 
 189 // ---------------------------------------------------------------------------- 
 192 void wxTextDataObject::GetAllFormats( wxDataFormat 
*formats
, wxDataObjectBase::Direction dir 
) const 
 194     *formats
++ = wxDataFormat( wxDF_TEXT 
); 
 195     *formats 
= wxDataFormat( wxDF_UNICODETEXT 
); 
 199 // ---------------------------------------------------------------------------- 
 201 // ---------------------------------------------------------------------------- 
 203 void wxFileDataObject::GetFileNames( wxCharBuffer 
&buf 
) const 
 207     for (size_t i 
= 0; i 
< m_filenames
.GetCount(); i
++) 
 209         filenames 
+= m_filenames
[i
]; 
 210         filenames 
+= wxT('\n'); 
 213     buf 
= filenames
.fn_str(); 
 216 bool wxFileDataObject::GetDataHere( void *pBuf 
) const 
 225     buffLength 
= strlen( buf 
); 
 226     memcpy( pBuf
, (const char*)buf
, buffLength 
+ 1 ); 
 231 size_t wxFileDataObject::GetDataSize() const 
 237     buffLength 
= strlen( buf 
); 
 239     return buffLength 
+ 1; 
 242 bool wxFileDataObject::SetData( size_t nSize
, const void *pBuf 
) 
 247     filenames 
= wxString( (const char*)pBuf
, *wxConvFileName 
); 
 249     filenames 
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
))); 
 252     m_filenames 
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK 
); 
 257 void wxFileDataObject::AddFile( const wxString
& rFilename 
) 
 259     m_filenames
.Add( rFilename 
); 
 262 // ---------------------------------------------------------------------------- 
 263 // wxBitmapDataObject 
 264 // ---------------------------------------------------------------------------- 
 266 wxBitmapDataObject::wxBitmapDataObject() 
 271 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap 
) 
 272 : wxBitmapDataObjectBase( rBitmap 
) 
 278         m_pictHandle 
= m_bitmap
.GetBitmapData()->GetPictHandle(); 
 279         m_pictCreated 
= false; 
 283 wxBitmapDataObject::~wxBitmapDataObject() 
 288 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap 
) 
 291     wxBitmapDataObjectBase::SetBitmap( rBitmap 
); 
 294         m_pictHandle 
= m_bitmap
.GetBitmapData()->GetPictHandle(); 
 295         m_pictCreated 
= false; 
 299 void wxBitmapDataObject::Init() 
 302     m_pictCreated 
= false; 
 305 void wxBitmapDataObject::Clear() 
 307     if (m_pictHandle 
!= NULL
) 
 311             KillPicture( (PicHandle
)m_pictHandle 
); 
 316     m_pictCreated 
= false; 
 319 bool wxBitmapDataObject::GetDataHere( void *pBuf 
) const 
 321     if (m_pictHandle 
== NULL
) 
 323         wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); 
 330     memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle 
) ); 
 335 size_t wxBitmapDataObject::GetDataSize() const 
 337     if (m_pictHandle 
!= NULL
) 
 338         return GetHandleSize( (Handle
)m_pictHandle 
); 
 343 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf 
) 
 347     if ((pBuf 
== NULL
) || (nSize 
== 0)) 
 350     PicHandle picHandle 
= (PicHandle
)NewHandle( nSize 
); 
 351     memcpy( *picHandle
, pBuf
, nSize 
); 
 352     m_pictHandle 
= picHandle
; 
 354     // ownership is transferred to the bitmap 
 355     m_pictCreated 
= false; 
 358     wxMacGetPictureBounds( picHandle
, &frame 
); 
 361     mf
.SetHMETAFILE( (WXHMETAFILE
)m_pictHandle 
); 
 364     m_bitmap
.Create( frame
.right 
- frame
.left
, frame
.bottom 
- frame
.top 
); 
 365     mdc
.SelectObject( m_bitmap 
); 
 369     mdc
.SelectObject( wxNullBitmap 
); 
 372     return m_bitmap
.Ok();