]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dataobj.cpp
a hack to make copying double clicked words to clipboard work
[wxWidgets.git] / src / mac / carbon / dataobj.cpp
CommitLineData
72e7876b 1///////////////////////////////////////////////////////////////////////////////
427ff662
SC
2// Name: mac/dataobj.cpp
3// Purpose: implementation of wxDataObject class
4// Author: Stefan Csomor
72e7876b
SC
5// Modified by:
6// Created: 10/21/99
7// RCS-ID: $Id$
427ff662 8// Copyright: (c) 1999 Stefan Csomor
6aa89a22 9// Licence: wxWindows licence
72e7876b
SC
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "dataobj.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifndef WX_PRECOMP
28#include "wx/intl.h"
29#endif
30#include "wx/defs.h"
31
32#include "wx/log.h"
33#include "wx/dataobj.h"
34#include "wx/mstream.h"
35#include "wx/image.h"
76a5e5d2 36#include "wx/mac/private.h"
427ff662 37#include "Scrap.h"
72e7876b
SC
38
39// ----------------------------------------------------------------------------
40// functions
41// ----------------------------------------------------------------------------
42
43// ----------------------------------------------------------------------------
44// wxDataFormat
45// ----------------------------------------------------------------------------
46
47wxDataFormat::wxDataFormat()
48{
2f1ae414
SC
49 m_type = wxDF_INVALID;
50 m_format = 0;
72e7876b
SC
51}
52
2f1ae414 53wxDataFormat::wxDataFormat( wxDataFormatId vType )
72e7876b 54{
72e7876b
SC
55 SetType(vType);
56}
57
2f1ae414 58wxDataFormat::wxDataFormat( const wxChar* zId)
72e7876b 59{
72e7876b
SC
60 SetId(zId);
61}
62
2f1ae414 63wxDataFormat::wxDataFormat( const wxString& rId)
72e7876b 64{
72e7876b
SC
65 SetId(rId);
66}
67
2f1ae414 68wxDataFormat::wxDataFormat( NativeFormat vFormat)
72e7876b 69{
72e7876b
SC
70 SetId(vFormat);
71}
72
2f1ae414 73void wxDataFormat::SetType( wxDataFormatId Type )
72e7876b 74{
2f1ae414
SC
75 m_type = Type;
76
427ff662
SC
77 if (m_type == wxDF_TEXT )
78 m_format = kScrapFlavorTypeText;
79 else if (m_type == wxDF_UNICODETEXT )
80 m_format = kScrapFlavorTypeUnicode ;
2f1ae414 81 else if (m_type == wxDF_BITMAP || m_type == wxDF_METAFILE )
427ff662 82 m_format = kScrapFlavorTypePicture;
2f1ae414 83 else if (m_type == wxDF_FILENAME)
a07c1212 84 m_format = kDragFlavorTypeHFS ;
72e7876b
SC
85 else
86 {
493ac0b8 87 m_format = '????' ;
72e7876b
SC
88 wxFAIL_MSG( wxT("invalid dataformat") );
89 }
90}
91
92wxDataFormatId wxDataFormat::GetType() const
93{
2f1ae414 94 return m_type;
72e7876b
SC
95}
96
97wxString wxDataFormat::GetId() const
98{
427ff662 99 char text[5] ;
493ac0b8 100 strncpy( text , (char*) &m_format , 4 ) ;
427ff662
SC
101 text[4] = 0 ;
102 return wxString::FromAscii( text ) ;
72e7876b
SC
103}
104
2f1ae414 105void wxDataFormat::SetId( NativeFormat format )
72e7876b 106{
2f1ae414
SC
107 m_format = format;
108
427ff662 109 if (m_format == kScrapFlavorTypeText)
72e7876b
SC
110 m_type = wxDF_TEXT;
111 else
427ff662
SC
112 if (m_format == kScrapFlavorTypeUnicode )
113 m_type = wxDF_UNICODETEXT;
114 else
115 if (m_format == kScrapFlavorTypePicture)
72e7876b
SC
116 m_type = wxDF_BITMAP;
117 else
a07c1212 118 if (m_format == kDragFlavorTypeHFS )
72e7876b
SC
119 m_type = wxDF_FILENAME;
120 else
121 m_type = wxDF_PRIVATE;
72e7876b
SC
122}
123
2f1ae414 124void wxDataFormat::SetId( const wxChar* zId )
72e7876b
SC
125{
126 wxString tmp(zId);
127
2f1ae414
SC
128 m_type = wxDF_PRIVATE;
129 m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
72e7876b
SC
130}
131
132//-------------------------------------------------------------------------
133// wxDataObject
134//-------------------------------------------------------------------------
135
136wxDataObject::wxDataObject()
137{
138}
139
140bool wxDataObject::IsSupportedFormat(
141 const wxDataFormat& rFormat
142, Direction vDir
143) const
144{
145 size_t nFormatCount = GetFormatCount(vDir);
146
147 if (nFormatCount == 1)
148 {
149 return rFormat == GetPreferredFormat();
150 }
151 else
152 {
e40298d5 153 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
72e7876b
SC
154 GetAllFormats( pFormats
155 ,vDir
156 );
157
158 size_t n;
159
160 for (n = 0; n < nFormatCount; n++)
161 {
162 if (pFormats[n] == rFormat)
163 break;
164 }
165
166 delete [] pFormats;
167
168 // found?
169 return n < nFormatCount;
170 }
171}
172
173// ----------------------------------------------------------------------------
174// wxFileDataObject
175// ----------------------------------------------------------------------------
176
177bool wxFileDataObject::GetDataHere(
178 void* pBuf
179) const
180{
181 wxString sFilenames;
182
183 for (size_t i = 0; i < m_filenames.GetCount(); i++)
184 {
185 sFilenames += m_filenames[i];
186 sFilenames += (wxChar)0;
187 }
188
189 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
190 return TRUE;
191}
192
193size_t wxFileDataObject::GetDataSize() const
194{
e40298d5 195 size_t nRes = 0;
72e7876b
SC
196
197 for (size_t i = 0; i < m_filenames.GetCount(); i++)
198 {
199 nRes += m_filenames[i].Len();
200 nRes += 1;
201 }
202
203 return nRes + 1;
204}
205
206bool wxFileDataObject::SetData(
207 size_t WXUNUSED(nSize)
208, const void* pBuf
209)
210{
a07c1212 211 m_filenames.Empty();
72e7876b 212
427ff662 213 AddFile(wxString::FromAscii((char*)pBuf));
72e7876b
SC
214
215 return TRUE;
216}
217
218void wxFileDataObject::AddFile(
219 const wxString& rFilename
220)
221{
222 m_filenames.Add(rFilename);
223}
224
225// ----------------------------------------------------------------------------
226// wxBitmapDataObject
227// ----------------------------------------------------------------------------
228
229wxBitmapDataObject::wxBitmapDataObject()
230{
231 Init();
232}
233
234wxBitmapDataObject::wxBitmapDataObject(
e40298d5 235 const wxBitmap& rBitmap
72e7876b
SC
236)
237: wxBitmapDataObjectBase(rBitmap)
238{
239 Init();
2a391f87
SC
240 if ( m_bitmap.Ok() )
241 {
e40298d5
JS
242 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
243 }
72e7876b
SC
244}
245
246wxBitmapDataObject::~wxBitmapDataObject()
247{
248 Clear();
249}
250
251void wxBitmapDataObject::SetBitmap(
252 const wxBitmap& rBitmap
253)
254{
2a391f87 255 Clear();
72e7876b 256 wxBitmapDataObjectBase::SetBitmap(rBitmap);
2a391f87
SC
257 if ( m_bitmap.Ok() )
258 {
e40298d5
JS
259 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
260 }
2a391f87
SC
261}
262
263void wxBitmapDataObject::Init()
264{
e40298d5
JS
265 m_pictHandle = NULL ;
266 m_pictCreated = false ;
2a391f87
SC
267}
268
269void wxBitmapDataObject::Clear()
270{
e40298d5
JS
271 if ( m_pictCreated && m_pictHandle )
272 {
273 KillPicture( (PicHandle) m_pictHandle ) ;
274 }
275 m_pictHandle = NULL ;
72e7876b
SC
276}
277
278bool wxBitmapDataObject::GetDataHere(
279 void* pBuf
280) const
281{
2a391f87 282 if (!m_pictHandle)
72e7876b
SC
283 {
284 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
285 return FALSE;
286 }
2a391f87 287 memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle));
72e7876b
SC
288 return TRUE;
289}
290
2a391f87
SC
291size_t wxBitmapDataObject::GetDataSize() const
292{
e40298d5 293 return GetHandleSize((Handle)m_pictHandle) ;
2a391f87
SC
294}
295
72e7876b
SC
296bool wxBitmapDataObject::SetData(
297 size_t nSize
298, const void* pBuf
299)
300{
301 Clear();
2a391f87
SC
302 PicHandle picHandle = (PicHandle) NewHandle( nSize ) ;
303 memcpy( *picHandle , pBuf , nSize ) ;
304 m_pictHandle = picHandle ;
305 m_pictCreated = false ;
306 Rect frame = (**picHandle).picFrame ;
307
308 m_bitmap.SetPict( picHandle ) ;
e40298d5
JS
309 m_bitmap.SetWidth( frame.right - frame.left ) ;
310 m_bitmap.SetHeight( frame.bottom - frame.top ) ;
72e7876b
SC
311 return m_bitmap.Ok();
312}