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