]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dataobj.cpp | |
3 | // Purpose: wxDataObject class | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "dataobj.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/dataobj.h" | |
15 | #include "wx/app.h" | |
16 | #include "wx/debug.h" | |
17 | #include "wx/mstream.h" | |
18 | #include "wx/image.h" | |
19 | #include "wx/log.h" | |
20 | ||
21 | #include <gdk/gdk.h> | |
22 | ||
23 | //------------------------------------------------------------------------- | |
24 | // global data | |
25 | //------------------------------------------------------------------------- | |
26 | ||
27 | GdkAtom g_textAtom = 0; | |
28 | GdkAtom g_pngAtom = 0; | |
29 | GdkAtom g_fileAtom = 0; | |
30 | ||
31 | //------------------------------------------------------------------------- | |
32 | // wxDataFormat | |
33 | //------------------------------------------------------------------------- | |
34 | ||
35 | wxDataFormat::wxDataFormat() | |
36 | { | |
37 | // do *not* call PrepareFormats() from here for 2 reasons: | |
38 | // | |
39 | // 1. we will have time to do it later because some other Set function | |
40 | // must be called before we really need them | |
41 | // | |
42 | // 2. doing so prevents us from declaring global wxDataFormats because | |
43 | // calling PrepareFormats (and thus gdk_atom_intern) before GDK is | |
44 | // initialised will result in a crash | |
45 | m_type = wxDF_INVALID; | |
46 | m_format = (GdkAtom) 0; | |
47 | } | |
48 | ||
49 | wxDataFormat::wxDataFormat( wxDataFormatId type ) | |
50 | { | |
51 | PrepareFormats(); | |
52 | SetType( type ); | |
53 | } | |
54 | ||
55 | wxDataFormat::wxDataFormat( const wxChar *id ) | |
56 | { | |
57 | PrepareFormats(); | |
58 | SetId( id ); | |
59 | } | |
60 | ||
61 | wxDataFormat::wxDataFormat( const wxString &id ) | |
62 | { | |
63 | PrepareFormats(); | |
64 | SetId( id ); | |
65 | } | |
66 | ||
67 | wxDataFormat::wxDataFormat( NativeFormat format ) | |
68 | { | |
69 | PrepareFormats(); | |
70 | SetId( format ); | |
71 | } | |
72 | ||
73 | void wxDataFormat::SetType( wxDataFormatId type ) | |
74 | { | |
75 | PrepareFormats(); | |
76 | ||
77 | if (type == wxDF_UNICODETEXT) | |
78 | type = wxDF_TEXT; | |
79 | ||
80 | m_type = type; | |
81 | ||
82 | if (m_type == wxDF_TEXT) | |
83 | m_format = g_textAtom; | |
84 | else | |
85 | if (m_type == wxDF_BITMAP) | |
86 | m_format = g_pngAtom; | |
87 | else | |
88 | if (m_type == wxDF_FILENAME) | |
89 | m_format = g_fileAtom; | |
90 | else | |
91 | { | |
92 | wxFAIL_MSG( wxT("invalid dataformat") ); | |
93 | } | |
94 | } | |
95 | ||
96 | wxDataFormatId wxDataFormat::GetType() const | |
97 | { | |
98 | return m_type; | |
99 | } | |
100 | ||
101 | wxString wxDataFormat::GetId() const | |
102 | { | |
103 | wxString ret = wxString::FromAscii( gdk_atom_name( m_format ) ); | |
104 | return ret; | |
105 | } | |
106 | ||
107 | void wxDataFormat::SetId( NativeFormat format ) | |
108 | { | |
109 | PrepareFormats(); | |
110 | m_format = format; | |
111 | ||
112 | if (m_format == g_textAtom) | |
113 | m_type = wxDF_TEXT; | |
114 | else | |
115 | if (m_format == g_pngAtom) | |
116 | m_type = wxDF_BITMAP; | |
117 | else | |
118 | if (m_format == g_fileAtom) | |
119 | m_type = wxDF_FILENAME; | |
120 | else | |
121 | m_type = wxDF_PRIVATE; | |
122 | } | |
123 | ||
124 | void wxDataFormat::SetId( const wxChar *id ) | |
125 | { | |
126 | PrepareFormats(); | |
127 | m_type = wxDF_PRIVATE; | |
128 | wxString tmp( id ); | |
129 | m_format = gdk_atom_intern( (const char*) tmp.ToAscii(), FALSE ); | |
130 | } | |
131 | ||
132 | void wxDataFormat::PrepareFormats() | |
133 | { | |
134 | // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the | |
135 | // atoms STRING and file:ALL as the old code was, but normal X apps | |
136 | // use STRING for text selection when transfering the data via | |
137 | // clipboard, for example, so do use STRING for now (GNOME apps will | |
138 | // probably support STRING as well for compatibility anyhow), but use | |
139 | // text/uri-list for file dnd because compatibility is not important | |
140 | // here (with whom?) | |
141 | if (!g_textAtom) | |
142 | #if wxUSE_UNICODE | |
143 | g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE ); | |
144 | #else | |
145 | g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE ); | |
146 | #endif | |
147 | if (!g_pngAtom) | |
148 | g_pngAtom = gdk_atom_intern( "image/png", FALSE ); | |
149 | if (!g_fileAtom) | |
150 | g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE ); | |
151 | } | |
152 | ||
153 | //------------------------------------------------------------------------- | |
154 | // wxDataObject | |
155 | //------------------------------------------------------------------------- | |
156 | ||
157 | wxDataObject::wxDataObject() | |
158 | { | |
159 | } | |
160 | ||
161 | wxDataObject::~wxDataObject() | |
162 | { | |
163 | // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link | |
164 | } | |
165 | ||
166 | bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const | |
167 | { | |
168 | size_t nFormatCount = GetFormatCount(dir); | |
169 | if ( nFormatCount == 1 ) | |
170 | { | |
171 | return format == GetPreferredFormat(); | |
172 | } | |
173 | else | |
174 | { | |
175 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
176 | GetAllFormats(formats,dir); | |
177 | ||
178 | size_t n; | |
179 | for ( n = 0; n < nFormatCount; n++ ) | |
180 | { | |
181 | if ( formats[n] == format ) | |
182 | break; | |
183 | } | |
184 | ||
185 | delete [] formats; | |
186 | ||
187 | // found? | |
188 | return n < nFormatCount; | |
189 | } | |
190 | } | |
191 | ||
192 | // ---------------------------------------------------------------------------- | |
193 | // wxFileDataObject | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
196 | bool wxFileDataObject::GetDataHere(void *buf) const | |
197 | { | |
198 | wxString filenames; | |
199 | ||
200 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
201 | { | |
202 | filenames += m_filenames[i]; | |
203 | filenames += (wxChar) 0; | |
204 | } | |
205 | ||
206 | memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 ); | |
207 | ||
208 | return TRUE; | |
209 | } | |
210 | ||
211 | size_t wxFileDataObject::GetDataSize() const | |
212 | { | |
213 | size_t res = 0; | |
214 | ||
215 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
216 | { | |
217 | res += m_filenames[i].Len(); | |
218 | res += 1; | |
219 | } | |
220 | ||
221 | return res + 1; | |
222 | } | |
223 | ||
224 | bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf) | |
225 | { | |
226 | // VZ: old format | |
227 | #if 0 | |
228 | // filenames are stores as a string with #0 as deliminators | |
229 | const char *filenames = (const char*) buf; | |
230 | size_t pos = 0; | |
231 | for(;;) | |
232 | { | |
233 | if (filenames[0] == 0) | |
234 | break; | |
235 | if (pos >= size) | |
236 | break; | |
237 | wxString file( filenames ); // this returns the first file | |
238 | AddFile( file ); | |
239 | pos += file.Len()+1; | |
240 | filenames += file.Len()+1; | |
241 | } | |
242 | #else // 1 | |
243 | m_filenames.Empty(); | |
244 | ||
245 | // the text/uri-list format is a sequence of URIs (filenames prefixed by | |
246 | // "file:" as far as I see) delimited by "\r\n" of total length size | |
247 | // (I wonder what happens if the file has '\n' in its filename??) | |
248 | wxString filename; | |
249 | for ( const char *p = (const char *)buf; ; p++ ) | |
250 | { | |
251 | // some broken programs (testdnd GTK+ sample!) omit the trailing | |
252 | // "\r\n", so check for '\0' explicitly here instead of doing it in | |
253 | // the loop statement to account for it | |
254 | if ( (*p == '\r' && *(p+1) == '\n') || !*p ) | |
255 | { | |
256 | size_t lenPrefix = 5; // strlen("file:") | |
257 | if ( filename.Left(lenPrefix).MakeLower() == _T("file:") ) | |
258 | { | |
259 | // sometimes the syntax is "file:filename", sometimes it's | |
260 | // URL-like: "file://filename" - deal with both | |
261 | if ( filename[lenPrefix] == _T('/') && | |
262 | filename[lenPrefix + 1] == _T('/') ) | |
263 | { | |
264 | // skip the slashes | |
265 | lenPrefix += 2; | |
266 | } | |
267 | ||
268 | AddFile(filename.c_str() + lenPrefix); | |
269 | filename.Empty(); | |
270 | } | |
271 | else | |
272 | { | |
273 | wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"), | |
274 | filename.c_str()); | |
275 | } | |
276 | ||
277 | if ( !*p ) | |
278 | break; | |
279 | ||
280 | // skip '\r' | |
281 | p++; | |
282 | } | |
283 | else | |
284 | { | |
285 | filename += *p; | |
286 | } | |
287 | } | |
288 | #endif // 0/1 | |
289 | ||
290 | return TRUE; | |
291 | } | |
292 | ||
293 | void wxFileDataObject::AddFile( const wxString &filename ) | |
294 | { | |
295 | m_filenames.Add( filename ); | |
296 | } | |
297 | ||
298 | // ---------------------------------------------------------------------------- | |
299 | // wxBitmapDataObject | |
300 | // ---------------------------------------------------------------------------- | |
301 | ||
302 | wxBitmapDataObject::wxBitmapDataObject() | |
303 | { | |
304 | Init(); | |
305 | } | |
306 | ||
307 | wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) | |
308 | : wxBitmapDataObjectBase(bitmap) | |
309 | { | |
310 | Init(); | |
311 | ||
312 | DoConvertToPng(); | |
313 | } | |
314 | ||
315 | wxBitmapDataObject::~wxBitmapDataObject() | |
316 | { | |
317 | Clear(); | |
318 | } | |
319 | ||
320 | void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) | |
321 | { | |
322 | ClearAll(); | |
323 | ||
324 | wxBitmapDataObjectBase::SetBitmap(bitmap); | |
325 | ||
326 | DoConvertToPng(); | |
327 | } | |
328 | ||
329 | bool wxBitmapDataObject::GetDataHere(void *buf) const | |
330 | { | |
331 | if ( !m_pngSize ) | |
332 | { | |
333 | wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); | |
334 | ||
335 | return FALSE; | |
336 | } | |
337 | ||
338 | memcpy(buf, m_pngData, m_pngSize); | |
339 | ||
340 | return TRUE; | |
341 | } | |
342 | ||
343 | bool wxBitmapDataObject::SetData(size_t size, const void *buf) | |
344 | { | |
345 | Clear(); | |
346 | ||
347 | wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, | |
348 | FALSE, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); | |
349 | ||
350 | m_pngSize = size; | |
351 | m_pngData = malloc(m_pngSize); | |
352 | ||
353 | memcpy(m_pngData, buf, m_pngSize); | |
354 | ||
355 | wxMemoryInputStream mstream((char*) m_pngData, m_pngSize); | |
356 | wxImage image; | |
357 | if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) ) | |
358 | { | |
359 | return FALSE; | |
360 | } | |
361 | ||
362 | m_bitmap = wxBitmap(image); | |
363 | ||
364 | return m_bitmap.Ok(); | |
365 | } | |
366 | ||
367 | void wxBitmapDataObject::DoConvertToPng() | |
368 | { | |
369 | if ( !m_bitmap.Ok() ) | |
370 | return; | |
371 | ||
372 | wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, | |
373 | wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); | |
374 | ||
375 | wxImage image = m_bitmap.ConvertToImage(); | |
376 | ||
377 | wxCountingOutputStream count; | |
378 | image.SaveFile(count, wxBITMAP_TYPE_PNG); | |
379 | ||
380 | m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? | |
381 | m_pngData = malloc(m_pngSize); | |
382 | ||
383 | wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize); | |
384 | image.SaveFile(mstream, wxBITMAP_TYPE_PNG); | |
385 | } | |
386 | ||
387 |