]>
Commit | Line | Data |
---|---|---|
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 |
65571936 | 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" |
7f0c3a63 | 37 | #include <Scrap.h> |
72e7876b SC |
38 | |
39 | // ---------------------------------------------------------------------------- | |
40 | // functions | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // wxDataFormat | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | wxDataFormat::wxDataFormat() | |
48 | { | |
2f1ae414 SC |
49 | m_type = wxDF_INVALID; |
50 | m_format = 0; | |
72e7876b SC |
51 | } |
52 | ||
2f1ae414 | 53 | wxDataFormat::wxDataFormat( wxDataFormatId vType ) |
72e7876b | 54 | { |
72e7876b SC |
55 | SetType(vType); |
56 | } | |
57 | ||
2f1ae414 | 58 | wxDataFormat::wxDataFormat( const wxChar* zId) |
72e7876b | 59 | { |
72e7876b SC |
60 | SetId(zId); |
61 | } | |
62 | ||
2f1ae414 | 63 | wxDataFormat::wxDataFormat( const wxString& rId) |
72e7876b | 64 | { |
72e7876b SC |
65 | SetId(rId); |
66 | } | |
67 | ||
2f1ae414 | 68 | wxDataFormat::wxDataFormat( NativeFormat vFormat) |
72e7876b | 69 | { |
72e7876b SC |
70 | SetId(vFormat); |
71 | } | |
72 | ||
2f1ae414 | 73 | void 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 | { | |
87 | wxFAIL_MSG( wxT("invalid dataformat") ); | |
72e7876b | 88 | |
d8a01976 VZ |
89 | // this is '????' but it can't be used in the code because ??' is |
90 | // parsed as a trigraph! | |
91 | m_format = 0x3f3f3f3f; | |
63b8dd39 | 92 | } |
72e7876b SC |
93 | } |
94 | ||
95 | wxString wxDataFormat::GetId() const | |
96 | { | |
63b8dd39 VZ |
97 | // note that m_format is not a pointer to string, it *is* itself a 4 |
98 | // character string | |
99 | char text[5] ; | |
100 | strncpy( text , (char*) &m_format , 4 ) ; | |
101 | text[4] = 0 ; | |
102 | ||
427ff662 | 103 | return wxString::FromAscii( text ) ; |
72e7876b SC |
104 | } |
105 | ||
2f1ae414 | 106 | void wxDataFormat::SetId( NativeFormat format ) |
72e7876b | 107 | { |
2f1ae414 SC |
108 | m_format = format; |
109 | ||
427ff662 | 110 | if (m_format == kScrapFlavorTypeText) |
72e7876b | 111 | m_type = wxDF_TEXT; |
63b8dd39 | 112 | else if (m_format == kScrapFlavorTypeUnicode ) |
427ff662 | 113 | m_type = wxDF_UNICODETEXT; |
63b8dd39 | 114 | else if (m_format == kScrapFlavorTypePicture) |
72e7876b | 115 | m_type = wxDF_BITMAP; |
63b8dd39 | 116 | else if (m_format == kDragFlavorTypeHFS ) |
72e7876b SC |
117 | m_type = wxDF_FILENAME; |
118 | else | |
119 | m_type = wxDF_PRIVATE; | |
72e7876b SC |
120 | } |
121 | ||
2f1ae414 | 122 | void wxDataFormat::SetId( const wxChar* zId ) |
72e7876b | 123 | { |
2f1ae414 SC |
124 | m_type = wxDF_PRIVATE; |
125 | m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); | |
72e7876b SC |
126 | } |
127 | ||
128 | //------------------------------------------------------------------------- | |
129 | // wxDataObject | |
130 | //------------------------------------------------------------------------- | |
131 | ||
132 | wxDataObject::wxDataObject() | |
133 | { | |
134 | } | |
135 | ||
136 | bool wxDataObject::IsSupportedFormat( | |
137 | const wxDataFormat& rFormat | |
138 | , Direction vDir | |
139 | ) const | |
140 | { | |
141 | size_t nFormatCount = GetFormatCount(vDir); | |
142 | ||
143 | if (nFormatCount == 1) | |
144 | { | |
145 | return rFormat == GetPreferredFormat(); | |
146 | } | |
147 | else | |
148 | { | |
e40298d5 | 149 | wxDataFormat* pFormats = new wxDataFormat[nFormatCount]; |
72e7876b SC |
150 | GetAllFormats( pFormats |
151 | ,vDir | |
152 | ); | |
153 | ||
154 | size_t n; | |
155 | ||
156 | for (n = 0; n < nFormatCount; n++) | |
157 | { | |
158 | if (pFormats[n] == rFormat) | |
159 | break; | |
160 | } | |
161 | ||
162 | delete [] pFormats; | |
163 | ||
164 | // found? | |
165 | return n < nFormatCount; | |
166 | } | |
167 | } | |
168 | ||
25758297 SC |
169 | // ---------------------------------------------------------------------------- |
170 | // wxTextDataObject | |
171 | // ---------------------------------------------------------------------------- | |
172 | ||
173 | #if wxUSE_UNICODE | |
174 | void wxTextDataObject::GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir) const | |
175 | { | |
176 | *formats++ = wxDataFormat( wxDF_TEXT ); | |
177 | *formats = wxDataFormat( wxDF_UNICODETEXT ); | |
178 | } | |
179 | ||
180 | #endif | |
181 | ||
72e7876b SC |
182 | // ---------------------------------------------------------------------------- |
183 | // wxFileDataObject | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
186 | bool wxFileDataObject::GetDataHere( | |
187 | void* pBuf | |
188 | ) const | |
189 | { | |
190 | wxString sFilenames; | |
191 | ||
192 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
193 | { | |
194 | sFilenames += m_filenames[i]; | |
195 | sFilenames += (wxChar)0; | |
196 | } | |
197 | ||
198 | memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1); | |
199 | return TRUE; | |
200 | } | |
201 | ||
202 | size_t wxFileDataObject::GetDataSize() const | |
203 | { | |
e40298d5 | 204 | size_t nRes = 0; |
72e7876b SC |
205 | |
206 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
207 | { | |
208 | nRes += m_filenames[i].Len(); | |
209 | nRes += 1; | |
210 | } | |
211 | ||
212 | return nRes + 1; | |
213 | } | |
214 | ||
215 | bool wxFileDataObject::SetData( | |
216 | size_t WXUNUSED(nSize) | |
217 | , const void* pBuf | |
218 | ) | |
219 | { | |
a07c1212 | 220 | m_filenames.Empty(); |
72e7876b | 221 | |
ad05b188 SC |
222 | // only add if this is not an empty string |
223 | // we can therefore clear the list by just setting an empty string | |
224 | if ( (*(char*)pBuf) != 0 ) | |
225 | AddFile(wxString::FromAscii((char*)pBuf)); | |
72e7876b SC |
226 | |
227 | return TRUE; | |
228 | } | |
229 | ||
230 | void wxFileDataObject::AddFile( | |
231 | const wxString& rFilename | |
232 | ) | |
233 | { | |
234 | m_filenames.Add(rFilename); | |
235 | } | |
236 | ||
237 | // ---------------------------------------------------------------------------- | |
238 | // wxBitmapDataObject | |
239 | // ---------------------------------------------------------------------------- | |
240 | ||
241 | wxBitmapDataObject::wxBitmapDataObject() | |
242 | { | |
243 | Init(); | |
244 | } | |
245 | ||
246 | wxBitmapDataObject::wxBitmapDataObject( | |
e40298d5 | 247 | const wxBitmap& rBitmap |
72e7876b SC |
248 | ) |
249 | : wxBitmapDataObjectBase(rBitmap) | |
250 | { | |
251 | Init(); | |
2a391f87 SC |
252 | if ( m_bitmap.Ok() ) |
253 | { | |
e40298d5 JS |
254 | m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ; |
255 | } | |
72e7876b SC |
256 | } |
257 | ||
258 | wxBitmapDataObject::~wxBitmapDataObject() | |
259 | { | |
260 | Clear(); | |
261 | } | |
262 | ||
263 | void wxBitmapDataObject::SetBitmap( | |
264 | const wxBitmap& rBitmap | |
265 | ) | |
266 | { | |
2a391f87 | 267 | Clear(); |
72e7876b | 268 | wxBitmapDataObjectBase::SetBitmap(rBitmap); |
2a391f87 SC |
269 | if ( m_bitmap.Ok() ) |
270 | { | |
e40298d5 JS |
271 | m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ; |
272 | } | |
2a391f87 SC |
273 | } |
274 | ||
275 | void wxBitmapDataObject::Init() | |
276 | { | |
e40298d5 JS |
277 | m_pictHandle = NULL ; |
278 | m_pictCreated = false ; | |
2a391f87 SC |
279 | } |
280 | ||
281 | void wxBitmapDataObject::Clear() | |
282 | { | |
e40298d5 JS |
283 | if ( m_pictCreated && m_pictHandle ) |
284 | { | |
285 | KillPicture( (PicHandle) m_pictHandle ) ; | |
286 | } | |
287 | m_pictHandle = NULL ; | |
72e7876b SC |
288 | } |
289 | ||
290 | bool wxBitmapDataObject::GetDataHere( | |
291 | void* pBuf | |
292 | ) const | |
293 | { | |
2a391f87 | 294 | if (!m_pictHandle) |
72e7876b SC |
295 | { |
296 | wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); | |
297 | return FALSE; | |
298 | } | |
2a391f87 | 299 | memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle)); |
72e7876b SC |
300 | return TRUE; |
301 | } | |
302 | ||
2a391f87 SC |
303 | size_t wxBitmapDataObject::GetDataSize() const |
304 | { | |
e40298d5 | 305 | return GetHandleSize((Handle)m_pictHandle) ; |
2a391f87 SC |
306 | } |
307 | ||
72e7876b SC |
308 | bool wxBitmapDataObject::SetData( |
309 | size_t nSize | |
310 | , const void* pBuf | |
311 | ) | |
312 | { | |
313 | Clear(); | |
2a391f87 SC |
314 | PicHandle picHandle = (PicHandle) NewHandle( nSize ) ; |
315 | memcpy( *picHandle , pBuf , nSize ) ; | |
316 | m_pictHandle = picHandle ; | |
317 | m_pictCreated = false ; | |
318 | Rect frame = (**picHandle).picFrame ; | |
319 | ||
320 | m_bitmap.SetPict( picHandle ) ; | |
e40298d5 JS |
321 | m_bitmap.SetWidth( frame.right - frame.left ) ; |
322 | m_bitmap.SetHeight( frame.bottom - frame.top ) ; | |
72e7876b SC |
323 | return m_bitmap.Ok(); |
324 | } |