]>
Commit | Line | Data |
---|---|---|
72e7876b SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: os2/dataobj.cpp | |
3 | // Purpose: implementation of wx[I]DataObject class | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/21/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 David Webster | |
9 | // Licence: wxWindows license | |
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" |
72e7876b SC |
37 | |
38 | // ---------------------------------------------------------------------------- | |
39 | // functions | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // wxDataFormat | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | wxDataFormat::wxDataFormat() | |
47 | { | |
2f1ae414 SC |
48 | m_type = wxDF_INVALID; |
49 | m_format = 0; | |
72e7876b SC |
50 | } |
51 | ||
2f1ae414 | 52 | wxDataFormat::wxDataFormat( wxDataFormatId vType ) |
72e7876b | 53 | { |
72e7876b SC |
54 | SetType(vType); |
55 | } | |
56 | ||
2f1ae414 | 57 | wxDataFormat::wxDataFormat( const wxChar* zId) |
72e7876b | 58 | { |
72e7876b SC |
59 | SetId(zId); |
60 | } | |
61 | ||
2f1ae414 | 62 | wxDataFormat::wxDataFormat( const wxString& rId) |
72e7876b | 63 | { |
72e7876b SC |
64 | SetId(rId); |
65 | } | |
66 | ||
2f1ae414 | 67 | wxDataFormat::wxDataFormat( NativeFormat vFormat) |
72e7876b | 68 | { |
72e7876b SC |
69 | SetId(vFormat); |
70 | } | |
71 | ||
2f1ae414 | 72 | void wxDataFormat::SetType( wxDataFormatId Type ) |
72e7876b | 73 | { |
2f1ae414 SC |
74 | m_type = Type; |
75 | ||
76 | if (m_type == wxDF_TEXT) | |
77 | m_format = 'TEXT'; | |
78 | else if (m_type == wxDF_BITMAP || m_type == wxDF_METAFILE ) | |
79 | m_format = 'PICT'; | |
80 | else if (m_type == wxDF_FILENAME) | |
a07c1212 | 81 | m_format = kDragFlavorTypeHFS ; |
72e7876b SC |
82 | else |
83 | { | |
84 | wxFAIL_MSG( wxT("invalid dataformat") ); | |
85 | } | |
86 | } | |
87 | ||
88 | wxDataFormatId wxDataFormat::GetType() const | |
89 | { | |
2f1ae414 | 90 | return m_type; |
72e7876b SC |
91 | } |
92 | ||
93 | wxString wxDataFormat::GetId() const | |
94 | { | |
2f1ae414 | 95 | wxString sRet(""); // TODO: to name of ( m_format ) ); |
72e7876b SC |
96 | return sRet; |
97 | } | |
98 | ||
2f1ae414 | 99 | void wxDataFormat::SetId( NativeFormat format ) |
72e7876b | 100 | { |
2f1ae414 SC |
101 | m_format = format; |
102 | ||
103 | if (m_format == 'TEXT') | |
72e7876b SC |
104 | m_type = wxDF_TEXT; |
105 | else | |
2f1ae414 | 106 | if (m_format == 'PICT') |
72e7876b SC |
107 | m_type = wxDF_BITMAP; |
108 | else | |
a07c1212 | 109 | if (m_format == kDragFlavorTypeHFS ) |
72e7876b SC |
110 | m_type = wxDF_FILENAME; |
111 | else | |
112 | m_type = wxDF_PRIVATE; | |
72e7876b SC |
113 | } |
114 | ||
2f1ae414 | 115 | void wxDataFormat::SetId( const wxChar* zId ) |
72e7876b SC |
116 | { |
117 | wxString tmp(zId); | |
118 | ||
2f1ae414 SC |
119 | m_type = wxDF_PRIVATE; |
120 | m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); | |
72e7876b SC |
121 | } |
122 | ||
123 | //------------------------------------------------------------------------- | |
124 | // wxDataObject | |
125 | //------------------------------------------------------------------------- | |
126 | ||
127 | wxDataObject::wxDataObject() | |
128 | { | |
129 | } | |
130 | ||
131 | bool wxDataObject::IsSupportedFormat( | |
132 | const wxDataFormat& rFormat | |
133 | , Direction vDir | |
134 | ) const | |
135 | { | |
136 | size_t nFormatCount = GetFormatCount(vDir); | |
137 | ||
138 | if (nFormatCount == 1) | |
139 | { | |
140 | return rFormat == GetPreferredFormat(); | |
141 | } | |
142 | else | |
143 | { | |
144 | wxDataFormat* pFormats = new wxDataFormat[nFormatCount]; | |
145 | GetAllFormats( pFormats | |
146 | ,vDir | |
147 | ); | |
148 | ||
149 | size_t n; | |
150 | ||
151 | for (n = 0; n < nFormatCount; n++) | |
152 | { | |
153 | if (pFormats[n] == rFormat) | |
154 | break; | |
155 | } | |
156 | ||
157 | delete [] pFormats; | |
158 | ||
159 | // found? | |
160 | return n < nFormatCount; | |
161 | } | |
162 | } | |
163 | ||
164 | // ---------------------------------------------------------------------------- | |
165 | // wxFileDataObject | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
168 | bool wxFileDataObject::GetDataHere( | |
169 | void* pBuf | |
170 | ) const | |
171 | { | |
172 | wxString sFilenames; | |
173 | ||
174 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
175 | { | |
176 | sFilenames += m_filenames[i]; | |
177 | sFilenames += (wxChar)0; | |
178 | } | |
179 | ||
180 | memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1); | |
181 | return TRUE; | |
182 | } | |
183 | ||
184 | size_t wxFileDataObject::GetDataSize() const | |
185 | { | |
186 | size_t nRes = 0; | |
187 | ||
188 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
189 | { | |
190 | nRes += m_filenames[i].Len(); | |
191 | nRes += 1; | |
192 | } | |
193 | ||
194 | return nRes + 1; | |
195 | } | |
196 | ||
197 | bool wxFileDataObject::SetData( | |
198 | size_t WXUNUSED(nSize) | |
199 | , const void* pBuf | |
200 | ) | |
201 | { | |
a07c1212 | 202 | m_filenames.Empty(); |
72e7876b SC |
203 | |
204 | wxString sFile( (const char *)pBuf); /* char, not wxChar */ | |
205 | ||
206 | AddFile(sFile); | |
207 | ||
208 | return TRUE; | |
209 | } | |
210 | ||
211 | void wxFileDataObject::AddFile( | |
212 | const wxString& rFilename | |
213 | ) | |
214 | { | |
215 | m_filenames.Add(rFilename); | |
216 | } | |
217 | ||
218 | // ---------------------------------------------------------------------------- | |
219 | // wxBitmapDataObject | |
220 | // ---------------------------------------------------------------------------- | |
221 | ||
222 | wxBitmapDataObject::wxBitmapDataObject() | |
223 | { | |
224 | Init(); | |
225 | } | |
226 | ||
227 | wxBitmapDataObject::wxBitmapDataObject( | |
228 | const wxBitmap& rBitmap | |
229 | ) | |
230 | : wxBitmapDataObjectBase(rBitmap) | |
231 | { | |
232 | Init(); | |
2a391f87 SC |
233 | if ( m_bitmap.Ok() ) |
234 | { | |
235 | m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ; | |
236 | } | |
72e7876b SC |
237 | } |
238 | ||
239 | wxBitmapDataObject::~wxBitmapDataObject() | |
240 | { | |
241 | Clear(); | |
242 | } | |
243 | ||
244 | void wxBitmapDataObject::SetBitmap( | |
245 | const wxBitmap& rBitmap | |
246 | ) | |
247 | { | |
2a391f87 | 248 | Clear(); |
72e7876b | 249 | wxBitmapDataObjectBase::SetBitmap(rBitmap); |
2a391f87 SC |
250 | if ( m_bitmap.Ok() ) |
251 | { | |
252 | m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ; | |
253 | } | |
254 | } | |
255 | ||
256 | void wxBitmapDataObject::Init() | |
257 | { | |
258 | m_pictHandle = NULL ; | |
259 | m_pictCreated = false ; | |
260 | } | |
261 | ||
262 | void wxBitmapDataObject::Clear() | |
263 | { | |
264 | if ( m_pictCreated && m_pictHandle ) | |
265 | { | |
266 | KillPicture( (PicHandle) m_pictHandle ) ; | |
267 | } | |
268 | m_pictHandle = NULL ; | |
72e7876b SC |
269 | } |
270 | ||
271 | bool wxBitmapDataObject::GetDataHere( | |
272 | void* pBuf | |
273 | ) const | |
274 | { | |
2a391f87 | 275 | if (!m_pictHandle) |
72e7876b SC |
276 | { |
277 | wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); | |
278 | return FALSE; | |
279 | } | |
2a391f87 | 280 | memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle)); |
72e7876b SC |
281 | return TRUE; |
282 | } | |
283 | ||
2a391f87 SC |
284 | size_t wxBitmapDataObject::GetDataSize() const |
285 | { | |
286 | return GetHandleSize((Handle)m_pictHandle) ; | |
287 | } | |
288 | ||
72e7876b SC |
289 | bool wxBitmapDataObject::SetData( |
290 | size_t nSize | |
291 | , const void* pBuf | |
292 | ) | |
293 | { | |
294 | Clear(); | |
2a391f87 SC |
295 | PicHandle picHandle = (PicHandle) NewHandle( nSize ) ; |
296 | memcpy( *picHandle , pBuf , nSize ) ; | |
297 | m_pictHandle = picHandle ; | |
298 | m_pictCreated = false ; | |
299 | Rect frame = (**picHandle).picFrame ; | |
300 | ||
301 | m_bitmap.SetPict( picHandle ) ; | |
302 | m_bitmap.SetWidth( frame.right - frame.left ) ; | |
303 | m_bitmap.SetHeight( frame.bottom - frame.top ) ; | |
72e7876b SC |
304 | return m_bitmap.Ok(); |
305 | } |