]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataobj.cpp
use gdk_fontset_load() instead of gdk_font_load() for better Japanese support (patch...
[wxWidgets.git] / src / gtk / dataobj.cpp
CommitLineData
8b53e5a2
RR
1///////////////////////////////////////////////////////////////////////////////
2// Name: dataobj.cpp
3// Purpose: wxDataObject class
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
3f480da3 7// Licence: wxWindows licence
8b53e5a2
RR
8///////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
3f480da3 11 #pragma implementation "dataobj.h"
8b53e5a2
RR
12#endif
13
14#include "wx/dataobj.h"
ab8884ac 15#include "wx/app.h"
0d2a2b60 16#include "wx/debug.h"
e2acb9ae
RR
17#include "wx/mstream.h"
18#include "wx/image.h"
1fb35ade 19#include "wx/log.h"
0d2a2b60 20
071a2d78 21#include <gdk/gdk.h>
0d2a2b60 22
d6086ea6
RR
23//-------------------------------------------------------------------------
24// global data
25//-------------------------------------------------------------------------
26
27GdkAtom g_textAtom = 0;
e2acb9ae 28GdkAtom g_pngAtom = 0;
1dd989e1 29GdkAtom g_fileAtom = 0;
d6086ea6 30
0d2a2b60
RR
31//-------------------------------------------------------------------------
32// wxDataFormat
33//-------------------------------------------------------------------------
34
cd5bf2a6 35wxDataFormat::wxDataFormat()
0d2a2b60 36{
4b3d29db
VZ
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
cd5bf2a6 45 m_type = wxDF_INVALID;
1dd989e1 46 m_format = (GdkAtom) 0;
cd5bf2a6
RR
47}
48
3f480da3 49wxDataFormat::wxDataFormat( wxDataFormatId type )
cd5bf2a6 50{
e2acb9ae 51 PrepareFormats();
cd5bf2a6 52 SetType( type );
0d2a2b60
RR
53}
54
b5be07d4 55wxDataFormat::wxDataFormat( const wxChar *id )
8a126fcc 56{
e2acb9ae 57 PrepareFormats();
8a126fcc
RR
58 SetId( id );
59}
60
0d2a2b60
RR
61wxDataFormat::wxDataFormat( const wxString &id )
62{
e2acb9ae 63 PrepareFormats();
cd5bf2a6 64 SetId( id );
0d2a2b60
RR
65}
66
1dd989e1 67wxDataFormat::wxDataFormat( NativeFormat format )
0d2a2b60 68{
e2acb9ae 69 PrepareFormats();
1dd989e1 70 SetId( format );
0d2a2b60
RR
71}
72
3f480da3 73void wxDataFormat::SetType( wxDataFormatId type )
cd5bf2a6 74{
4b3d29db 75 PrepareFormats();
ca11abde
RR
76
77 if (type == wxDF_UNICODETEXT)
78 type = wxDF_TEXT;
3f480da3 79
ca11abde
RR
80 m_type = type;
81
cd5bf2a6 82 if (m_type == wxDF_TEXT)
1dd989e1 83 m_format = g_textAtom;
cd5bf2a6
RR
84 else
85 if (m_type == wxDF_BITMAP)
1dd989e1 86 m_format = g_pngAtom;
cd5bf2a6
RR
87 else
88 if (m_type == wxDF_FILENAME)
1dd989e1 89 m_format = g_fileAtom;
cd5bf2a6
RR
90 else
91 {
223d09f6 92 wxFAIL_MSG( wxT("invalid dataformat") );
cd5bf2a6 93 }
cd5bf2a6 94}
3f480da3
VZ
95
96wxDataFormatId wxDataFormat::GetType() const
0d2a2b60
RR
97{
98 return m_type;
99}
100
101wxString wxDataFormat::GetId() const
102{
2e1d7104 103 wxString ret = wxString::FromAscii( gdk_atom_name( m_format ) );
1dd989e1 104 return ret;
0d2a2b60 105}
3f480da3 106
1dd989e1 107void wxDataFormat::SetId( NativeFormat format )
3f480da3 108{
4b3d29db 109 PrepareFormats();
1dd989e1 110 m_format = format;
3f480da3 111
1dd989e1
RR
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;
0d2a2b60 122}
3f480da3 123
1dd989e1 124void wxDataFormat::SetId( const wxChar *id )
0d2a2b60 125{
4b3d29db 126 PrepareFormats();
1dd989e1
RR
127 m_type = wxDF_PRIVATE;
128 wxString tmp( id );
2e1d7104 129 m_format = gdk_atom_intern( (const char*) tmp.ToAscii(), FALSE );
0d2a2b60 130}
3f480da3 131
1dd989e1 132void wxDataFormat::PrepareFormats()
0d2a2b60 133{
810b5e1f 134 // VZ: GNOME included in RedHat 6.1 uses the MIME types below and not the
74d38ad8
VZ
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?)
e1ee679c 141 if (!g_textAtom)
2e1d7104 142#if wxUSE_UNICODE
ca11abde 143 g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
2e1d7104 144#else
74d38ad8 145 g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
2e1d7104 146#endif
e1ee679c 147 if (!g_pngAtom)
1dd989e1 148 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
e1ee679c 149 if (!g_fileAtom)
810b5e1f 150 g_fileAtom = gdk_atom_intern( "text/uri-list", FALSE );
0d2a2b60 151}
8b53e5a2
RR
152
153//-------------------------------------------------------------------------
154// wxDataObject
155//-------------------------------------------------------------------------
156
0d2a2b60
RR
157wxDataObject::wxDataObject()
158{
0d2a2b60 159}
3f480da3 160
2b5f62a0
VZ
161wxDataObject::~wxDataObject()
162{
163 // dtor is empty but needed for Darwin and AIX -- otherwise it doesn't link
164}
165
b068c4e8 166bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
0d2a2b60 167{
b068c4e8 168 size_t nFormatCount = GetFormatCount(dir);
f2593d0d
RR
169 if ( nFormatCount == 1 )
170 {
1dd989e1
RR
171 return format == GetPreferredFormat();
172 }
f2593d0d
RR
173 else
174 {
1dd989e1 175 wxDataFormat *formats = new wxDataFormat[nFormatCount];
b068c4e8 176 GetAllFormats(formats,dir);
1dd989e1
RR
177
178 size_t n;
f2593d0d
RR
179 for ( n = 0; n < nFormatCount; n++ )
180 {
1dd989e1
RR
181 if ( formats[n] == format )
182 break;
183 }
0d2a2b60 184
1dd989e1 185 delete [] formats;
cd5bf2a6 186
1dd989e1
RR
187 // found?
188 return n < nFormatCount;
189 }
0d2a2b60
RR
190}
191
8b53e5a2
RR
192// ----------------------------------------------------------------------------
193// wxFileDataObject
194// ----------------------------------------------------------------------------
195
e1ee679c 196bool wxFileDataObject::GetDataHere(void *buf) const
0d2a2b60 197{
b068c4e8 198 wxString filenames;
4b3d29db 199
b068c4e8
RR
200 for (size_t i = 0; i < m_filenames.GetCount(); i++)
201 {
202 filenames += m_filenames[i];
4b3d29db 203 filenames += (wxChar) 0;
b068c4e8 204 }
4b3d29db 205
e1ee679c 206 memcpy( buf, filenames.mbc_str(), filenames.Len() + 1 );
0d2a2b60 207
e1ee679c 208 return TRUE;
0d2a2b60 209}
3f480da3 210
e1ee679c 211size_t wxFileDataObject::GetDataSize() const
0d2a2b60 212{
b068c4e8 213 size_t res = 0;
4b3d29db 214
b068c4e8
RR
215 for (size_t i = 0; i < m_filenames.GetCount(); i++)
216 {
217 res += m_filenames[i].Len();
4b3d29db 218 res += 1;
b068c4e8 219 }
4b3d29db 220
b068c4e8 221 return res + 1;
0d2a2b60 222}
3f480da3 223
7941ba11 224bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *buf)
0d2a2b60 225{
810b5e1f
VZ
226 // VZ: old format
227#if 0
2d68e1b4 228 // filenames are stores as a string with #0 as deliminators
2d68e1b4
RR
229 const char *filenames = (const char*) buf;
230 size_t pos = 0;
231 for(;;)
232 {
233 if (filenames[0] == 0)
810b5e1f
VZ
234 break;
235 if (pos >= size)
236 break;
2d68e1b4
RR
237 wxString file( filenames ); // this returns the first file
238 AddFile( file );
810b5e1f
VZ
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;
74d38ad8 249 for ( const char *p = (const char *)buf; ; p++ )
810b5e1f 250 {
74d38ad8
VZ
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 )
810b5e1f 255 {
74d38ad8 256 size_t lenPrefix = 5; // strlen("file:")
810b5e1f
VZ
257 if ( filename.Left(lenPrefix).MakeLower() == _T("file:") )
258 {
74d38ad8
VZ
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());
810b5e1f
VZ
275 }
276
74d38ad8
VZ
277 if ( !*p )
278 break;
810b5e1f
VZ
279
280 // skip '\r'
281 p++;
282 }
283 else
284 {
285 filename += *p;
286 }
2d68e1b4 287 }
810b5e1f 288#endif // 0/1
3f480da3 289
1dd989e1
RR
290 return TRUE;
291}
292
b068c4e8
RR
293void wxFileDataObject::AddFile( const wxString &filename )
294{
295 m_filenames.Add( filename );
296}
297
8b53e5a2
RR
298// ----------------------------------------------------------------------------
299// wxBitmapDataObject
300// ----------------------------------------------------------------------------
301
0d2a2b60
RR
302wxBitmapDataObject::wxBitmapDataObject()
303{
e1ee679c 304 Init();
0d2a2b60
RR
305}
306
307wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
e1ee679c 308 : wxBitmapDataObjectBase(bitmap)
0d2a2b60 309{
e1ee679c
VZ
310 Init();
311
e2acb9ae
RR
312 DoConvertToPng();
313}
314
315wxBitmapDataObject::~wxBitmapDataObject()
316{
e1ee679c 317 Clear();
0d2a2b60
RR
318}
319
320void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
321{
e1ee679c 322 ClearAll();
0d2a2b60 323
e1ee679c
VZ
324 wxBitmapDataObjectBase::SetBitmap(bitmap);
325
326 DoConvertToPng();
0d2a2b60
RR
327}
328
e1ee679c 329bool wxBitmapDataObject::GetDataHere(void *buf) const
0d2a2b60 330{
e1ee679c 331 if ( !m_pngSize )
1dd989e1 332 {
e1ee679c
VZ
333 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
334
335 return FALSE;
1dd989e1 336 }
0d2a2b60 337
e1ee679c
VZ
338 memcpy(buf, m_pngData, m_pngSize);
339
340 return TRUE;
e2acb9ae
RR
341}
342
e1ee679c 343bool wxBitmapDataObject::SetData(size_t size, const void *buf)
e2acb9ae 344{
e1ee679c
VZ
345 Clear();
346
bd3b7e09
VS
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
1dd989e1 350 m_pngSize = size;
e1ee679c
VZ
351 m_pngData = malloc(m_pngSize);
352
bd3b7e09 353 memcpy(m_pngData, buf, m_pngSize);
e1ee679c 354
bd3b7e09 355 wxMemoryInputStream mstream((char*) m_pngData, m_pngSize);
e2acb9ae 356 wxImage image;
bd3b7e09 357 if ( !image.LoadFile( mstream, wxBITMAP_TYPE_PNG ) )
e1ee679c
VZ
358 {
359 return FALSE;
360 }
361
bd3b7e09 362 m_bitmap = wxBitmap(image);
4b3d29db 363
b068c4e8 364 return m_bitmap.Ok();
e2acb9ae
RR
365}
366
367void wxBitmapDataObject::DoConvertToPng()
368{
bd3b7e09 369 if ( !m_bitmap.Ok() )
e1ee679c
VZ
370 return;
371
bd3b7e09
VS
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
368d59f0 375 wxImage image = m_bitmap.ConvertToImage();
e1ee679c 376
e2acb9ae 377 wxCountingOutputStream count;
bd3b7e09 378 image.SaveFile(count, wxBITMAP_TYPE_PNG);
e1ee679c 379
e2acb9ae 380 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
e1ee679c
VZ
381 m_pngData = malloc(m_pngSize);
382
bd3b7e09
VS
383 wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
384 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
0d2a2b60 385}
3f480da3 386
0d2a2b60 387