]>
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 | bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const | |
162 | { | |
163 | size_t nFormatCount = GetFormatCount(dir); | |
164 | if ( nFormatCount == 1 ) | |
165 | { | |
166 | return format == GetPreferredFormat(); | |
167 | } | |
168 | else | |
169 | { | |
170 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
171 | GetAllFormats(formats,dir); | |
172 | ||
173 | size_t n; | |
174 | for ( n = 0; n < nFormatCount; n++ ) | |
175 | { | |
176 | if ( formats[n] == format ) | |
177 | break; | |
178 | } | |
179 | ||
180 | delete [] formats; | |
181 | ||
182 | // found? | |
183 | return n < nFormatCount; | |
184 | } | |
185 | } | |
186 | ||
187 | // ---------------------------------------------------------------------------- | |
188 | // wxFileDataObject | |
189 | // ---------------------------------------------------------------------------- | |
190 | ||
191 | bool wxFileDataObject::GetDataHere(void *buf) const | |
192 | { | |
193 | wxString filenames; | |
194 | ||
195 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
196 | { | |
197 | filenames += m_filenames[i]; | |
198 | filenames += (wxChar) 0; | |
199 | } | |
200 | ||
201 | memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 ); | |
202 | ||
203 | return TRUE; | |
204 | } | |
205 | ||
206 | size_t wxFileDataObject::GetDataSize() const | |
207 | { | |
208 | size_t res = 0; | |
209 | ||
210 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
211 | { | |
212 | res += m_filenames[i].Len(); | |
213 | res += 1; | |
214 | } | |
215 | ||
216 | return res + 1; | |
217 | } | |
218 | ||
219 | bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf) | |
220 | { | |
221 | // VZ: old format | |
222 | #if 0 | |
223 | // filenames are stores as a string with #0 as deliminators | |
224 | const char *filenames = (const char*) buf; | |
225 | size_t pos = 0; | |
226 | for(;;) | |
227 | { | |
228 | if (filenames[0] == 0) | |
229 | break; | |
230 | if (pos >= size) | |
231 | break; | |
232 | wxString file( filenames ); // this returns the first file | |
233 | AddFile( file ); | |
234 | pos += file.Len()+1; | |
235 | filenames += file.Len()+1; | |
236 | } | |
237 | #else // 1 | |
238 | m_filenames.Empty(); | |
239 | ||
240 | // the text/uri-list format is a sequence of URIs (filenames prefixed by | |
241 | // "file:" as far as I see) delimited by "\r\n" of total length size | |
242 | // (I wonder what happens if the file has '\n' in its filename??) | |
243 | wxString filename; | |
244 | for ( const char *p = (const char *)buf; ; p++ ) | |
245 | { | |
246 | // some broken programs (testdnd GTK+ sample!) omit the trailing | |
247 | // "\r\n", so check for '\0' explicitly here instead of doing it in | |
248 | // the loop statement to account for it | |
249 | if ( (*p == '\r' && *(p+1) == '\n') || !*p ) | |
250 | { | |
251 | size_t lenPrefix = 5; // strlen("file:") | |
252 | if ( filename.Left(lenPrefix).MakeLower() == _T("file:") ) | |
253 | { | |
254 | // sometimes the syntax is "file:filename", sometimes it's | |
255 | // URL-like: "file://filename" - deal with both | |
256 | if ( filename[lenPrefix] == _T('/') && | |
257 | filename[lenPrefix + 1] == _T('/') ) | |
258 | { | |
259 | // skip the slashes | |
260 | lenPrefix += 2; | |
261 | } | |
262 | ||
263 | AddFile(filename.c_str() + lenPrefix); | |
264 | filename.Empty(); | |
265 | } | |
266 | else | |
267 | { | |
268 | wxLogDebug(_T("Unsupported URI '%s' in wxFileDataObject"), | |
269 | filename.c_str()); | |
270 | } | |
271 | ||
272 | if ( !*p ) | |
273 | break; | |
274 | ||
275 | // skip '\r' | |
276 | p++; | |
277 | } | |
278 | else | |
279 | { | |
280 | filename += *p; | |
281 | } | |
282 | } | |
283 | #endif // 0/1 | |
284 | ||
285 | return TRUE; | |
286 | } | |
287 | ||
288 | void wxFileDataObject::AddFile( const wxString &filename ) | |
289 | { | |
290 | m_filenames.Add( filename ); | |
291 | } | |
292 | ||
293 | // ---------------------------------------------------------------------------- | |
294 | // wxBitmapDataObject | |
295 | // ---------------------------------------------------------------------------- | |
296 | ||
297 | wxBitmapDataObject::wxBitmapDataObject() | |
298 | { | |
299 | Init(); | |
300 | } | |
301 | ||
302 | wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) | |
303 | : wxBitmapDataObjectBase(bitmap) | |
304 | { | |
305 | Init(); | |
306 | ||
307 | DoConvertToPng(); | |
308 | } | |
309 | ||
310 | wxBitmapDataObject::~wxBitmapDataObject() | |
311 | { | |
312 | Clear(); | |
313 | } | |
314 | ||
315 | void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) | |
316 | { | |
317 | ClearAll(); | |
318 | ||
319 | wxBitmapDataObjectBase::SetBitmap(bitmap); | |
320 | ||
321 | DoConvertToPng(); | |
322 | } | |
323 | ||
324 | bool wxBitmapDataObject::GetDataHere(void *buf) const | |
325 | { | |
326 | if ( !m_pngSize ) | |
327 | { | |
328 | wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); | |
329 | ||
330 | return FALSE; | |
331 | } | |
332 | ||
333 | memcpy(buf, m_pngData, m_pngSize); | |
334 | ||
335 | return TRUE; | |
336 | } | |
337 | ||
338 | bool wxBitmapDataObject::SetData(size_t size, const void *buf) | |
339 | { | |
340 | Clear(); | |
341 | ||
342 | wxCHECK_MSG( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, | |
343 | FALSE, wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); | |
344 | ||
345 | m_pngSize = size; | |
346 | m_pngData = malloc(m_pngSize); | |
347 | ||
348 | memcpy(m_pngData, buf, m_pngSize); | |
349 | ||
350 | wxMemoryInputStream mstream((char*) m_pngData, m_pngSize); | |
351 | wxImage image; | |
352 | if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) ) | |
353 | { | |
354 | return FALSE; | |
355 | } | |
356 | ||
357 | m_bitmap = wxBitmap(image); | |
358 | ||
359 | return m_bitmap.Ok(); | |
360 | } | |
361 | ||
362 | void wxBitmapDataObject::DoConvertToPng() | |
363 | { | |
364 | if ( !m_bitmap.Ok() ) | |
365 | return; | |
366 | ||
367 | wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, | |
368 | wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); | |
369 | ||
370 | wxImage image = m_bitmap.ConvertToImage(); | |
371 | ||
372 | wxCountingOutputStream count; | |
373 | image.SaveFile(count, wxBITMAP_TYPE_PNG); | |
374 | ||
375 | m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? | |
376 | m_pngData = malloc(m_pngSize); | |
377 | ||
378 | wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize); | |
379 | image.SaveFile(mstream, wxBITMAP_TYPE_PNG); | |
380 | } | |
381 | ||
382 |