]>
Commit | Line | Data |
---|---|---|
6dddc146 DW |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
6dddc146 DW |
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 | |
6670f564 WS |
28 | #include "wx/defs.h" |
29 | #include "wx/intl.h" | |
30 | #include "wx/log.h" | |
6dddc146 | 31 | #endif |
6dddc146 | 32 | |
6670f564 WS |
33 | #if wxUSE_DATAOBJ |
34 | ||
6dddc146 | 35 | #include "wx/dataobj.h" |
e6ebb514 DW |
36 | #include "wx/mstream.h" |
37 | #include "wx/image.h" | |
6dddc146 DW |
38 | |
39 | #define INCL_DOS | |
40 | #include <os2.h> | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // functions | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
6dddc146 DW |
46 | // ---------------------------------------------------------------------------- |
47 | // wxDataFormat | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
4fd899b6 | 50 | wxString wxDataFormat::GetId() const |
6dddc146 | 51 | { |
0fba44b4 | 52 | wxChar zBuf[256]; |
4fd899b6 DW |
53 | wxString sRet; |
54 | ||
55 | ::WinQueryAtomName( ::WinQuerySystemAtomTable() | |
56 | ,m_uFormat | |
0fba44b4 | 57 | ,(PSZ)zBuf |
4fd899b6 DW |
58 | ,256 |
59 | ); | |
60 | sRet = zBuf; | |
61 | return sRet; | |
62 | } // end of wxDataFormat::GetId() | |
6dddc146 | 63 | |
4fd899b6 | 64 | void wxDataFormat::SetId ( |
6dddc146 DW |
65 | const wxChar* zId |
66 | ) | |
67 | { | |
4fd899b6 | 68 | m_uFormat = ::WinAddAtom( ::WinQuerySystemAtomTable() |
0fba44b4 | 69 | ,(PSZ)zId |
4fd899b6 DW |
70 | ); |
71 | } // end of wxDataFormat::SetId | |
6dddc146 | 72 | |
4fd899b6 | 73 | class CIDataObject |
6dddc146 | 74 | { |
4fd899b6 DW |
75 | public: |
76 | CIDataObject(wxDataObject* pDataObject); | |
77 | ~CIDataObject(); | |
78 | ||
79 | // | |
80 | // Operations on the DRAGITEM struct | |
81 | // | |
82 | bool GetData( const wxDataFormat& rFormat | |
83 | ,char* pzBuffer | |
84 | ,ULONG ulLen | |
85 | ); | |
86 | void GetDataHere( const wxDataFormat& rFormat | |
87 | ,char* pzBuffer | |
88 | ,ULONG ulLen | |
89 | ); | |
90 | void QueryGetData(const wxDataFormat& rFormat); | |
91 | void SetData( const wxDataFormat& rFormat | |
92 | ,char* pzBuffer | |
93 | ); | |
94 | private: | |
95 | wxDataObject* m_pDataObject; // pointer to C++ class we belong to | |
96 | DRAGITEM m_vDragItem; | |
97 | }; // end of CLASS CIDataObject | |
98 | ||
6670f564 WS |
99 | bool CIDataObject::GetData ( const wxDataFormat& rFormat, |
100 | char* pzBuffer, | |
101 | ULONG ulLen ) | |
6dddc146 | 102 | { |
4fd899b6 DW |
103 | QueryGetData(rFormat); |
104 | if (rFormat.GetType() == wxDF_INVALID) | |
105 | return FALSE; | |
6dddc146 | 106 | |
4fd899b6 DW |
107 | ULONG ulSize = m_pDataObject->GetDataSize(rFormat); |
108 | ||
109 | if (ulSize == 0) | |
110 | { | |
111 | // | |
112 | // It probably means that the method is just not implemented | |
113 | // | |
114 | return FALSE; | |
115 | } | |
116 | if (rFormat.GetType() == wxDF_PRIVATE) | |
6dddc146 | 117 | { |
4fd899b6 DW |
118 | // |
119 | // For custom formats, put the size with the data - alloc the | |
120 | // space for it | |
121 | // | |
122 | ulSize += sizeof(ULONG); | |
6dddc146 | 123 | } |
6dddc146 | 124 | |
4fd899b6 DW |
125 | if (ulSize > ulLen) // not enough room to copy |
126 | return FALSE; | |
6dddc146 | 127 | |
4fd899b6 DW |
128 | // |
129 | // Copy the data | |
130 | // | |
131 | GetDataHere( rFormat | |
132 | ,pzBuffer | |
133 | ,ulSize | |
134 | ); | |
6670f564 | 135 | return true; |
4fd899b6 | 136 | } // end of CIDataObject::GetData |
6dddc146 | 137 | |
4fd899b6 DW |
138 | void CIDataObject::GetDataHere( |
139 | const wxDataFormat& rFormat | |
140 | , char* pzBuffer | |
141 | , ULONG WXUNUSED(ulLen) | |
6dddc146 DW |
142 | ) |
143 | { | |
4fd899b6 DW |
144 | m_pDataObject->GetDataHere( rFormat |
145 | ,(void*)pzBuffer | |
146 | ); | |
147 | } // end of CIDataObject::GetDataHere | |
148 | ||
149 | void CIDataObject::QueryGetData ( | |
150 | const wxDataFormat& rFormat | |
6dddc146 DW |
151 | ) |
152 | { | |
4fd899b6 DW |
153 | m_pDataObject->IsSupportedFormat(rFormat); |
154 | } // end of CIDataObject::QueryGetData | |
6dddc146 | 155 | |
4fd899b6 DW |
156 | void CIDataObject::SetData ( |
157 | const wxDataFormat& rFormat | |
158 | , char* pzBuffer | |
159 | ) | |
6dddc146 | 160 | { |
322d45dd | 161 | ULONG ulSize = 0; |
4fd899b6 DW |
162 | |
163 | switch (rFormat.GetType()) | |
164 | { | |
165 | case wxDF_TEXT: | |
166 | case wxDF_OEMTEXT: | |
167 | case wxDF_FILENAME: | |
168 | case wxDF_HTML: | |
169 | ulSize = strlen((const char *)pzBuffer); | |
170 | break; | |
171 | ||
8681f66c | 172 | #if wxUSE_UNICODE |
4fd899b6 DW |
173 | case wxDF_UNICODETEXT: |
174 | ulSize = ::wcslen((const wchar_t *)pzBuffer); | |
175 | break; | |
8681f66c | 176 | #endif |
4fd899b6 DW |
177 | |
178 | case wxDF_BITMAP: | |
179 | case wxDF_METAFILE: | |
180 | case wxDF_ENHMETAFILE: | |
181 | case wxDF_TIFF: | |
182 | case wxDF_DIB: | |
183 | ulSize = 0; // pass via a handle | |
184 | break; | |
185 | ||
186 | ||
187 | case wxDF_SYLK: | |
188 | case wxDF_DIF: | |
189 | case wxDF_PALETTE: | |
190 | case wxDF_PENDATA: | |
191 | case wxDF_RIFF: | |
192 | case wxDF_WAVE: | |
193 | case wxDF_LOCALE: | |
194 | //PUNT | |
195 | break; | |
196 | ||
197 | case wxDF_PRIVATE: | |
198 | size_t* p = (size_t *)pzBuffer; | |
199 | ||
200 | ulSize = *p++; | |
201 | pzBuffer = (char*)p; | |
202 | break; | |
203 | } | |
204 | m_pDataObject->SetData( rFormat | |
205 | ,ulSize | |
206 | ,(void*)pzBuffer | |
207 | ); | |
208 | } // end of CIDataObject::SetData | |
6dddc146 DW |
209 | |
210 | //------------------------------------------------------------------------- | |
211 | // wxDataObject | |
212 | //------------------------------------------------------------------------- | |
213 | ||
4fd899b6 | 214 | wxDataObject::wxDataObject () |
6dddc146 | 215 | { |
4fd899b6 DW |
216 | m_pDataObject = new DRAGITEM; |
217 | } // end of wxDataObject::wxDataObject | |
6dddc146 | 218 | |
4fd899b6 | 219 | wxDataObject::~wxDataObject () |
6dddc146 | 220 | { |
4fd899b6 DW |
221 | delete m_pDataObject; |
222 | } // end of wxDataObject::~wxDataObject | |
6dddc146 DW |
223 | |
224 | // ---------------------------------------------------------------------------- | |
225 | // wxFileDataObject | |
226 | // ---------------------------------------------------------------------------- | |
227 | ||
6670f564 | 228 | bool wxFileDataObject::GetDataHere( void* pBuf ) const |
6dddc146 DW |
229 | { |
230 | wxString sFilenames; | |
231 | ||
232 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
233 | { | |
e6ebb514 DW |
234 | sFilenames += m_filenames[i]; |
235 | sFilenames += (wxChar)0; | |
6dddc146 DW |
236 | } |
237 | ||
e6ebb514 | 238 | memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1); |
6670f564 | 239 | return true; |
6dddc146 DW |
240 | } |
241 | ||
242 | size_t wxFileDataObject::GetDataSize() const | |
243 | { | |
244 | size_t nRes = 0; | |
245 | ||
246 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
247 | { | |
248 | nRes += m_filenames[i].Len(); | |
249 | nRes += 1; | |
250 | } | |
251 | ||
252 | return nRes + 1; | |
253 | } | |
254 | ||
6670f564 WS |
255 | bool wxFileDataObject::SetData( size_t WXUNUSED(nSize), |
256 | const void* pBuf ) | |
6dddc146 DW |
257 | { |
258 | /* TODO */ | |
259 | ||
6670f564 | 260 | wxString sFile((const wxChar *)pBuf); /* char, not wxChar */ |
6dddc146 DW |
261 | |
262 | AddFile(sFile); | |
263 | ||
6670f564 | 264 | return true; |
6dddc146 DW |
265 | } |
266 | ||
267 | void wxFileDataObject::AddFile( | |
268 | const wxString& rFilename | |
269 | ) | |
270 | { | |
271 | m_filenames.Add(rFilename); | |
272 | } | |
273 | ||
274 | // ---------------------------------------------------------------------------- | |
275 | // wxBitmapDataObject | |
276 | // ---------------------------------------------------------------------------- | |
277 | ||
278 | wxBitmapDataObject::wxBitmapDataObject() | |
279 | { | |
280 | Init(); | |
281 | } | |
282 | ||
283 | wxBitmapDataObject::wxBitmapDataObject( | |
284 | const wxBitmap& rBitmap | |
285 | ) | |
286 | : wxBitmapDataObjectBase(rBitmap) | |
287 | { | |
288 | Init(); | |
289 | ||
290 | DoConvertToPng(); | |
291 | } | |
292 | ||
293 | wxBitmapDataObject::~wxBitmapDataObject() | |
294 | { | |
295 | Clear(); | |
296 | } | |
297 | ||
6670f564 | 298 | void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap ) |
6dddc146 DW |
299 | { |
300 | ClearAll(); | |
301 | wxBitmapDataObjectBase::SetBitmap(rBitmap); | |
302 | DoConvertToPng(); | |
303 | } | |
304 | ||
6670f564 | 305 | bool wxBitmapDataObject::GetDataHere( void* pBuf ) const |
6dddc146 DW |
306 | { |
307 | if (!m_pngSize) | |
308 | { | |
309 | wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); | |
310 | return FALSE; | |
311 | } | |
312 | memcpy(pBuf, m_pngData, m_pngSize); | |
6670f564 | 313 | return true; |
6dddc146 DW |
314 | } |
315 | ||
316 | bool wxBitmapDataObject::SetData( | |
317 | size_t nSize | |
318 | , const void* pBuf | |
319 | ) | |
320 | { | |
321 | Clear(); | |
322 | m_pngSize = nSize; | |
323 | m_pngData = malloc(m_pngSize); | |
324 | ||
325 | memcpy(m_pngData, pBuf, m_pngSize); | |
326 | ||
7e99520b | 327 | #if wxUSE_STREAMS |
6dddc146 DW |
328 | wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize); |
329 | wxImage vImage; | |
330 | wxPNGHandler vHandler; | |
331 | ||
332 | if (!vHandler.LoadFile(&vImage, vMstream)) | |
333 | { | |
334 | return FALSE; | |
335 | } | |
336 | ||
3437f881 | 337 | m_bitmap = wxBitmap(vImage); |
7e99520b DW |
338 | #endif //wxUSE_STREAMS |
339 | ||
6dddc146 DW |
340 | return m_bitmap.Ok(); |
341 | } | |
342 | ||
343 | void wxBitmapDataObject::DoConvertToPng() | |
344 | { | |
345 | if (!m_bitmap.Ok()) | |
346 | return; | |
347 | ||
7e99520b | 348 | #if wxUSE_STREAMS |
3437f881 | 349 | wxImage vImage = m_bitmap.ConvertToImage(); |
6dddc146 DW |
350 | wxPNGHandler vHandler; |
351 | wxCountingOutputStream vCount; | |
352 | ||
e6ebb514 | 353 | vHandler.SaveFile(&vImage, vCount); |
6dddc146 DW |
354 | |
355 | m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ??? | |
356 | m_pngData = malloc(m_pngSize); | |
357 | ||
e6ebb514 DW |
358 | wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize); |
359 | ||
6dddc146 | 360 | vHandler.SaveFile(&vImage, vMstream ); |
7e99520b | 361 | #endif |
6dddc146 DW |
362 | } |
363 | ||
6670f564 | 364 | #endif // wxUSE_DATAOBJ |