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