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