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