]>
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 |
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 | ||
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") ); | |
88 | } | |
89 | } | |
90 | ||
91 | wxDataFormatId wxDataFormat::GetType() const | |
92 | { | |
2f1ae414 | 93 | return m_type; |
72e7876b SC |
94 | } |
95 | ||
96 | wxString wxDataFormat::GetId() const | |
97 | { | |
427ff662 SC |
98 | char text[5] ; |
99 | strncpy( text , (char*) m_format , 4 ) ; | |
100 | text[4] = 0 ; | |
101 | return wxString::FromAscii( text ) ; | |
72e7876b SC |
102 | } |
103 | ||
2f1ae414 | 104 | void wxDataFormat::SetId( NativeFormat format ) |
72e7876b | 105 | { |
2f1ae414 SC |
106 | m_format = format; |
107 | ||
427ff662 | 108 | if (m_format == kScrapFlavorTypeText) |
72e7876b SC |
109 | m_type = wxDF_TEXT; |
110 | else | |
427ff662 SC |
111 | if (m_format == kScrapFlavorTypeUnicode ) |
112 | m_type = wxDF_UNICODETEXT; | |
113 | else | |
114 | if (m_format == kScrapFlavorTypePicture) | |
72e7876b SC |
115 | m_type = wxDF_BITMAP; |
116 | else | |
a07c1212 | 117 | if (m_format == kDragFlavorTypeHFS ) |
72e7876b SC |
118 | m_type = wxDF_FILENAME; |
119 | else | |
120 | m_type = wxDF_PRIVATE; | |
72e7876b SC |
121 | } |
122 | ||
2f1ae414 | 123 | void wxDataFormat::SetId( const wxChar* zId ) |
72e7876b SC |
124 | { |
125 | wxString tmp(zId); | |
126 | ||
2f1ae414 SC |
127 | m_type = wxDF_PRIVATE; |
128 | m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); | |
72e7876b SC |
129 | } |
130 | ||
131 | //------------------------------------------------------------------------- | |
132 | // wxDataObject | |
133 | //------------------------------------------------------------------------- | |
134 | ||
135 | wxDataObject::wxDataObject() | |
136 | { | |
137 | } | |
138 | ||
139 | bool wxDataObject::IsSupportedFormat( | |
140 | const wxDataFormat& rFormat | |
141 | , Direction vDir | |
142 | ) const | |
143 | { | |
144 | size_t nFormatCount = GetFormatCount(vDir); | |
145 | ||
146 | if (nFormatCount == 1) | |
147 | { | |
148 | return rFormat == GetPreferredFormat(); | |
149 | } | |
150 | else | |
151 | { | |
e40298d5 | 152 | wxDataFormat* pFormats = new wxDataFormat[nFormatCount]; |
72e7876b SC |
153 | GetAllFormats( pFormats |
154 | ,vDir | |
155 | ); | |
156 | ||
157 | size_t n; | |
158 | ||
159 | for (n = 0; n < nFormatCount; n++) | |
160 | { | |
161 | if (pFormats[n] == rFormat) | |
162 | break; | |
163 | } | |
164 | ||
165 | delete [] pFormats; | |
166 | ||
167 | // found? | |
168 | return n < nFormatCount; | |
169 | } | |
170 | } | |
171 | ||
172 | // ---------------------------------------------------------------------------- | |
173 | // wxFileDataObject | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
176 | bool wxFileDataObject::GetDataHere( | |
177 | void* pBuf | |
178 | ) const | |
179 | { | |
180 | wxString sFilenames; | |
181 | ||
182 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
183 | { | |
184 | sFilenames += m_filenames[i]; | |
185 | sFilenames += (wxChar)0; | |
186 | } | |
187 | ||
188 | memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1); | |
189 | return TRUE; | |
190 | } | |
191 | ||
192 | size_t wxFileDataObject::GetDataSize() const | |
193 | { | |
e40298d5 | 194 | size_t nRes = 0; |
72e7876b SC |
195 | |
196 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
197 | { | |
198 | nRes += m_filenames[i].Len(); | |
199 | nRes += 1; | |
200 | } | |
201 | ||
202 | return nRes + 1; | |
203 | } | |
204 | ||
205 | bool wxFileDataObject::SetData( | |
206 | size_t WXUNUSED(nSize) | |
207 | , const void* pBuf | |
208 | ) | |
209 | { | |
a07c1212 | 210 | m_filenames.Empty(); |
72e7876b | 211 | |
427ff662 | 212 | AddFile(wxString::FromAscii((char*)pBuf)); |
72e7876b SC |
213 | |
214 | return TRUE; | |
215 | } | |
216 | ||
217 | void wxFileDataObject::AddFile( | |
218 | const wxString& rFilename | |
219 | ) | |
220 | { | |
221 | m_filenames.Add(rFilename); | |
222 | } | |
223 | ||
224 | // ---------------------------------------------------------------------------- | |
225 | // wxBitmapDataObject | |
226 | // ---------------------------------------------------------------------------- | |
227 | ||
228 | wxBitmapDataObject::wxBitmapDataObject() | |
229 | { | |
230 | Init(); | |
231 | } | |
232 | ||
233 | wxBitmapDataObject::wxBitmapDataObject( | |
e40298d5 | 234 | const wxBitmap& rBitmap |
72e7876b SC |
235 | ) |
236 | : wxBitmapDataObjectBase(rBitmap) | |
237 | { | |
238 | Init(); | |
2a391f87 SC |
239 | if ( m_bitmap.Ok() ) |
240 | { | |
e40298d5 JS |
241 | m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ; |
242 | } | |
72e7876b SC |
243 | } |
244 | ||
245 | wxBitmapDataObject::~wxBitmapDataObject() | |
246 | { | |
247 | Clear(); | |
248 | } | |
249 | ||
250 | void wxBitmapDataObject::SetBitmap( | |
251 | const wxBitmap& rBitmap | |
252 | ) | |
253 | { | |
2a391f87 | 254 | Clear(); |
72e7876b | 255 | wxBitmapDataObjectBase::SetBitmap(rBitmap); |
2a391f87 SC |
256 | if ( m_bitmap.Ok() ) |
257 | { | |
e40298d5 JS |
258 | m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ; |
259 | } | |
2a391f87 SC |
260 | } |
261 | ||
262 | void wxBitmapDataObject::Init() | |
263 | { | |
e40298d5 JS |
264 | m_pictHandle = NULL ; |
265 | m_pictCreated = false ; | |
2a391f87 SC |
266 | } |
267 | ||
268 | void wxBitmapDataObject::Clear() | |
269 | { | |
e40298d5 JS |
270 | if ( m_pictCreated && m_pictHandle ) |
271 | { | |
272 | KillPicture( (PicHandle) m_pictHandle ) ; | |
273 | } | |
274 | m_pictHandle = NULL ; | |
72e7876b SC |
275 | } |
276 | ||
277 | bool wxBitmapDataObject::GetDataHere( | |
278 | void* pBuf | |
279 | ) const | |
280 | { | |
2a391f87 | 281 | if (!m_pictHandle) |
72e7876b SC |
282 | { |
283 | wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); | |
284 | return FALSE; | |
285 | } | |
2a391f87 | 286 | memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle)); |
72e7876b SC |
287 | return TRUE; |
288 | } | |
289 | ||
2a391f87 SC |
290 | size_t wxBitmapDataObject::GetDataSize() const |
291 | { | |
e40298d5 | 292 | return GetHandleSize((Handle)m_pictHandle) ; |
2a391f87 SC |
293 | } |
294 | ||
72e7876b SC |
295 | bool wxBitmapDataObject::SetData( |
296 | size_t nSize | |
297 | , const void* pBuf | |
298 | ) | |
299 | { | |
300 | Clear(); | |
2a391f87 SC |
301 | PicHandle picHandle = (PicHandle) NewHandle( nSize ) ; |
302 | memcpy( *picHandle , pBuf , nSize ) ; | |
303 | m_pictHandle = picHandle ; | |
304 | m_pictCreated = false ; | |
305 | Rect frame = (**picHandle).picFrame ; | |
306 | ||
307 | m_bitmap.SetPict( picHandle ) ; | |
e40298d5 JS |
308 | m_bitmap.SetWidth( frame.right - frame.left ) ; |
309 | m_bitmap.SetHeight( frame.bottom - frame.top ) ; | |
72e7876b SC |
310 | return m_bitmap.Ok(); |
311 | } |