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