]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dataobj.cpp
some != NULL checks
[wxWidgets.git] / src / mac / dataobj.cpp
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"
36
37 // ----------------------------------------------------------------------------
38 // functions
39 // ----------------------------------------------------------------------------
40
41 // ----------------------------------------------------------------------------
42 // wxDataFormat
43 // ----------------------------------------------------------------------------
44
45 wxDataFormat::wxDataFormat()
46 {
47 m_vType = wxDF_INVALID;
48 m_vFormat = 0;
49 }
50
51 wxDataFormat::wxDataFormat(
52 wxDataFormatId vType
53 )
54 {
55 PrepareFormats();
56 SetType(vType);
57 }
58
59 wxDataFormat::wxDataFormat(
60 const wxChar* zId
61 )
62 {
63 PrepareFormats();
64 SetId(zId);
65 }
66
67 wxDataFormat::wxDataFormat(
68 const wxString& rId
69 )
70 {
71 PrepareFormats();
72 SetId(rId);
73 }
74
75 wxDataFormat::wxDataFormat(
76 NativeFormat vFormat
77 )
78 {
79 PrepareFormats();
80 SetId(vFormat);
81 }
82
83 void wxDataFormat::SetType(
84 wxDataFormatId vType
85 )
86 {
87 m_vType = vType;
88
89 if (m_vType == wxDF_TEXT)
90 m_vFormat = 0;
91 else
92 if (m_vType == wxDF_BITMAP)
93 m_vFormat = 0;
94 else
95 if (m_vType == wxDF_FILENAME)
96 m_vFormat = 0;
97 else
98 {
99 wxFAIL_MSG( wxT("invalid dataformat") );
100 }
101 }
102
103 wxDataFormatId wxDataFormat::GetType() const
104 {
105 return m_vType;
106 }
107
108 wxString wxDataFormat::GetId() const
109 {
110 wxString sRet(""); // TODO: gdk_atom_name( m_format ) );
111 return sRet;
112 }
113
114 void wxDataFormat::SetId(
115 NativeFormat vFormat
116 )
117 {
118 m_vFormat = vFormat;
119 // TODO:
120 /*
121 if (m_format == g_textAtom)
122 m_type = wxDF_TEXT;
123 else
124 if (m_format == g_pngAtom)
125 m_type = wxDF_BITMAP;
126 else
127 if (m_format == g_fileAtom)
128 m_type = wxDF_FILENAME;
129 else
130 m_type = wxDF_PRIVATE;
131 */
132 }
133
134 void wxDataFormat::SetId(
135 const wxChar* zId
136 )
137 {
138 wxString tmp(zId);
139
140 m_vType = wxDF_PRIVATE;
141 m_vFormat = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
142 }
143
144 void wxDataFormat::PrepareFormats()
145 {
146 // TODO:
147 /*
148 if (!g_textAtom)
149 g_textAtom = gdk_atom_intern( "STRING", FALSE );
150 if (!g_pngAtom)
151 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
152 if (!g_fileAtom)
153 g_fileAtom = gdk_atom_intern( "file:ALL", FALSE );
154 */
155 }
156
157 //-------------------------------------------------------------------------
158 // wxDataObject
159 //-------------------------------------------------------------------------
160
161 wxDataObject::wxDataObject()
162 {
163 }
164
165 bool wxDataObject::IsSupportedFormat(
166 const wxDataFormat& rFormat
167 , Direction vDir
168 ) const
169 {
170 size_t nFormatCount = GetFormatCount(vDir);
171
172 if (nFormatCount == 1)
173 {
174 return rFormat == GetPreferredFormat();
175 }
176 else
177 {
178 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
179 GetAllFormats( pFormats
180 ,vDir
181 );
182
183 size_t n;
184
185 for (n = 0; n < nFormatCount; n++)
186 {
187 if (pFormats[n] == rFormat)
188 break;
189 }
190
191 delete [] pFormats;
192
193 // found?
194 return n < nFormatCount;
195 }
196 }
197
198 // ----------------------------------------------------------------------------
199 // wxFileDataObject
200 // ----------------------------------------------------------------------------
201
202 bool wxFileDataObject::GetDataHere(
203 void* pBuf
204 ) const
205 {
206 wxString sFilenames;
207
208 for (size_t i = 0; i < m_filenames.GetCount(); i++)
209 {
210 sFilenames += m_filenames[i];
211 sFilenames += (wxChar)0;
212 }
213
214 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
215 return TRUE;
216 }
217
218 size_t wxFileDataObject::GetDataSize() const
219 {
220 size_t nRes = 0;
221
222 for (size_t i = 0; i < m_filenames.GetCount(); i++)
223 {
224 nRes += m_filenames[i].Len();
225 nRes += 1;
226 }
227
228 return nRes + 1;
229 }
230
231 bool wxFileDataObject::SetData(
232 size_t WXUNUSED(nSize)
233 , const void* pBuf
234 )
235 {
236 /* TODO */
237
238 wxString sFile( (const char *)pBuf); /* char, not wxChar */
239
240 AddFile(sFile);
241
242 return TRUE;
243 }
244
245 void wxFileDataObject::AddFile(
246 const wxString& rFilename
247 )
248 {
249 m_filenames.Add(rFilename);
250 }
251
252 // ----------------------------------------------------------------------------
253 // wxBitmapDataObject
254 // ----------------------------------------------------------------------------
255
256 wxBitmapDataObject::wxBitmapDataObject()
257 {
258 Init();
259 }
260
261 wxBitmapDataObject::wxBitmapDataObject(
262 const wxBitmap& rBitmap
263 )
264 : wxBitmapDataObjectBase(rBitmap)
265 {
266 Init();
267
268 DoConvertToPng();
269 }
270
271 wxBitmapDataObject::~wxBitmapDataObject()
272 {
273 Clear();
274 }
275
276 void wxBitmapDataObject::SetBitmap(
277 const wxBitmap& rBitmap
278 )
279 {
280 ClearAll();
281 wxBitmapDataObjectBase::SetBitmap(rBitmap);
282 DoConvertToPng();
283 }
284
285 bool wxBitmapDataObject::GetDataHere(
286 void* pBuf
287 ) const
288 {
289 if (!m_pngSize)
290 {
291 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
292 return FALSE;
293 }
294 memcpy(pBuf, m_pngData, m_pngSize);
295 return TRUE;
296 }
297
298 bool wxBitmapDataObject::SetData(
299 size_t nSize
300 , const void* pBuf
301 )
302 {
303 Clear();
304 m_pngSize = nSize;
305 m_pngData = malloc(m_pngSize);
306
307 memcpy(m_pngData, pBuf, m_pngSize);
308
309 wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize);
310 wxImage vImage;
311 wxPNGHandler vHandler;
312
313 if (!vHandler.LoadFile(&vImage, vMstream))
314 {
315 return FALSE;
316 }
317
318 m_bitmap = vImage.ConvertToBitmap();
319 return m_bitmap.Ok();
320 }
321
322 void wxBitmapDataObject::DoConvertToPng()
323 {
324 if (!m_bitmap.Ok())
325 return;
326
327 wxImage vImage(m_bitmap);
328 wxPNGHandler vHandler;
329 wxCountingOutputStream vCount;
330
331 vHandler.SaveFile(&vImage, vCount);
332
333 m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ???
334 m_pngData = malloc(m_pngSize);
335
336 wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize);
337
338 vHandler.SaveFile(&vImage, vMstream );
339 }
340