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