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