1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/clipbrd.cpp
3 // Purpose: wxClipboard implementation for wxGTK
4 // Author: Robert Roebling, Vadim Zeitlin
6 // Copyright: (c) 1998 Robert Roebling
7 // (c) 2007 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
24 #include "wx/clipbrd.h"
30 #include "wx/dataobj.h"
33 #include "wx/scopedarray.h"
34 #include "wx/scopeguard.h"
36 #include "wx/gtk/private.h"
38 typedef wxScopedArray
<wxDataFormat
> wxDataFormatArray
;
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 static GdkAtom g_clipboardAtom
= 0;
45 static GdkAtom g_targetsAtom
= 0;
46 static GdkAtom g_timestampAtom
= 0;
49 extern GdkAtom g_altTextAtom
;
52 // the trace mask we use with wxLogTrace() - call
53 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
54 // (there will be a *lot* of them!)
55 #define TRACE_CLIPBOARD _T("clipboard")
57 // ----------------------------------------------------------------------------
58 // wxClipboardSync: used to perform clipboard operations synchronously
59 // ----------------------------------------------------------------------------
61 // constructing this object on stack will wait wait until the latest clipboard
62 // operation is finished on block exit
64 // notice that there can be no more than one such object alive at any moment,
65 // i.e. reentrancies are not allowed
69 wxClipboardSync(wxClipboard
& clipboard
)
71 wxASSERT_MSG( !ms_clipboard
, _T("reentrancy in clipboard code") );
72 ms_clipboard
= &clipboard
;
78 wxTheApp
->YieldFor(wxEVT_CATEGORY_CLIPBOARD
);
81 // this method must be called by GTK+ callbacks to indicate that we got the
82 // result for our clipboard operation
83 static void OnDone(wxClipboard
* WXUNUSED_UNLESS_DEBUG(clipboard
))
85 wxASSERT_MSG( clipboard
== ms_clipboard
,
86 _T("got notification for alien clipboard") );
91 // this method should be called if it's possible that no async clipboard
92 // operation is currently in progress (like it can be the case when the
93 // clipboard is cleared but not because we asked about it), it should only
94 // be called if such situation is expected -- otherwise call OnDone() which
95 // would assert in this case
96 static void OnDoneIfInProgress(wxClipboard
*clipboard
)
103 static wxClipboard
*ms_clipboard
;
105 wxDECLARE_NO_COPY_CLASS(wxClipboardSync
);
108 wxClipboard
*wxClipboardSync::ms_clipboard
= NULL
;
110 // ============================================================================
111 // clipboard callbacks implementation
112 // ============================================================================
114 //-----------------------------------------------------------------------------
115 // "selection_received" for targets
116 //-----------------------------------------------------------------------------
120 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
121 GtkSelectionData
*selection_data
,
122 guint32
WXUNUSED(time
),
123 wxClipboard
*clipboard
)
128 wxON_BLOCK_EXIT1(wxClipboardSync::OnDone
, clipboard
);
130 if ( !selection_data
|| selection_data
->length
<= 0 )
133 // make sure we got the data in the correct form
134 GdkAtom type
= selection_data
->type
;
135 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
137 if ( strcmp(wxGtkString(gdk_atom_name(type
)), "TARGETS") != 0 )
139 wxLogTrace( TRACE_CLIPBOARD
,
140 _T("got unsupported clipboard target") );
147 // it's not really a format, of course, but we can reuse its GetId() method
148 // to format this atom as string
149 wxDataFormat
clip(selection_data
->selection
);
150 wxLogTrace( TRACE_CLIPBOARD
,
151 wxT("Received available formats for clipboard %s"),
152 clip
.GetId().c_str() );
153 #endif // __WXDEBUG__
155 // the atoms we received, holding a list of targets (= formats)
156 const GdkAtom
* const atoms
= (GdkAtom
*)selection_data
->data
;
157 for ( size_t i
= 0; i
< selection_data
->length
/sizeof(GdkAtom
); i
++ )
159 const wxDataFormat
format(atoms
[i
]);
161 wxLogTrace(TRACE_CLIPBOARD
, wxT("\t%s"), format
.GetId().c_str());
163 if ( clipboard
->GTKOnTargetReceived(format
) )
169 bool wxClipboard::GTKOnTargetReceived(const wxDataFormat
& format
)
171 if ( format
!= m_targetRequested
)
174 m_formatSupported
= true;
178 //-----------------------------------------------------------------------------
179 // "selection_received" for the actual data
180 //-----------------------------------------------------------------------------
184 selection_received( GtkWidget
*WXUNUSED(widget
),
185 GtkSelectionData
*selection_data
,
186 guint32
WXUNUSED(time
),
187 wxClipboard
*clipboard
)
192 wxON_BLOCK_EXIT1(wxClipboardSync::OnDone
, clipboard
);
194 if ( !selection_data
|| selection_data
->length
<= 0 )
197 clipboard
->GTKOnSelectionReceived(*selection_data
);
201 //-----------------------------------------------------------------------------
203 //-----------------------------------------------------------------------------
207 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
209 wxClipboard
* const clipboard
= wxTheClipboard
;
213 // notice the use of OnDoneIfInProgress() here instead of just OnDone():
214 // it's perfectly possible that we're receiving this notification from GTK+
215 // even though we hadn't cleared the clipboard ourselves but because
216 // another application (or even another window in the same program)
218 wxON_BLOCK_EXIT1(wxClipboardSync::OnDoneIfInProgress
, clipboard
);
220 wxClipboard::Kind kind
;
221 if (event
->selection
== GDK_SELECTION_PRIMARY
)
223 wxLogTrace(TRACE_CLIPBOARD
, wxT("Lost primary selection" ));
225 kind
= wxClipboard::Primary
;
227 else if (event
->selection
== g_clipboardAtom
)
229 wxLogTrace(TRACE_CLIPBOARD
, wxT("Lost clipboard" ));
231 kind
= wxClipboard::Clipboard
;
233 else // some other selection, we're not concerned
238 // the clipboard is no longer in our hands, we don't need data any more
239 clipboard
->GTKClearData(kind
);
245 //-----------------------------------------------------------------------------
246 // selection handler for supplying data
247 //-----------------------------------------------------------------------------
251 selection_handler( GtkWidget
*WXUNUSED(widget
),
252 GtkSelectionData
*selection_data
,
253 guint
WXUNUSED(info
),
254 guint
WXUNUSED(time
),
255 gpointer signal_data
)
257 wxClipboard
* const clipboard
= wxTheClipboard
;
261 wxDataObject
* const data
= clipboard
->GTKGetDataObject();
265 // ICCCM says that TIMESTAMP is a required atom.
266 // In particular, it satisfies Klipper, which polls
267 // TIMESTAMP to see if the clipboards content has changed.
268 // It shall return the time which was used to set the data.
269 if (selection_data
->target
== g_timestampAtom
)
271 guint timestamp
= GPOINTER_TO_UINT (signal_data
);
272 gtk_selection_data_set(selection_data
,
273 GDK_SELECTION_TYPE_INTEGER
,
275 (guchar
*)&(timestamp
),
277 wxLogTrace(TRACE_CLIPBOARD
,
278 _T("Clipboard TIMESTAMP requested, returning timestamp=%u"),
283 wxDataFormat
format( selection_data
->target
);
286 wxLogTrace(TRACE_CLIPBOARD
,
287 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"),
288 format
.GetId().c_str(),
289 wxString::FromAscii(wxGtkString(gdk_atom_name(selection_data
->target
))).c_str(),
290 wxString::FromAscii(wxGtkString(gdk_atom_name(selection_data
->type
))).c_str(),
291 wxString::FromAscii(wxGtkString(gdk_atom_name(selection_data
->selection
))).c_str(),
292 GPOINTER_TO_UINT( signal_data
)
294 #endif // __WXDEBUG__
296 if ( !data
->IsSupportedFormat( format
) )
299 int size
= data
->GetDataSize( format
);
303 wxCharBuffer
buf(size
- 1); // it adds 1 internally (for NUL)
305 // text data must be returned in UTF8 if format is wxDF_UNICODETEXT
306 if ( !data
->GetDataHere(format
, buf
.data()) )
309 // use UTF8_STRING format if requested in Unicode build but just plain
310 // STRING one in ANSI or if explicitly asked in Unicode
312 if (format
== wxDataFormat(wxDF_UNICODETEXT
))
314 gtk_selection_data_set_text(
316 (const gchar
*)buf
.data(),
320 #endif // wxUSE_UNICODE
322 gtk_selection_data_set(
324 GDK_SELECTION_TYPE_STRING
,
326 (const guchar
*)buf
.data(),
332 void wxClipboard::GTKOnSelectionReceived(const GtkSelectionData
& sel
)
334 wxCHECK_RET( m_receivedData
, _T("should be inside GetData()") );
336 const wxDataFormat
format(sel
.target
);
337 wxLogTrace(TRACE_CLIPBOARD
, _T("Received selection %s"),
338 format
.GetId().c_str());
340 if ( !m_receivedData
->IsSupportedFormat(format
) )
343 m_receivedData
->SetData(format
, sel
.length
, sel
.data
);
344 m_formatSupported
= true;
347 //-----------------------------------------------------------------------------
348 // asynchronous "selection_received" for targets
349 //-----------------------------------------------------------------------------
353 async_targets_selection_received( GtkWidget
*WXUNUSED(widget
),
354 GtkSelectionData
*selection_data
,
355 guint32
WXUNUSED(time
),
356 wxClipboard
*clipboard
)
358 if ( !clipboard
) // Assert?
361 if (!clipboard
->m_sink
)
364 wxClipboardEvent
*event
= new wxClipboardEvent(wxEVT_CLIPBOARD_CHANGED
);
365 event
->SetEventObject( clipboard
);
367 if ( !selection_data
|| selection_data
->length
<= 0 )
369 clipboard
->m_sink
->QueueEvent( event
);
370 clipboard
->m_sink
.Release();
374 // make sure we got the data in the correct form
375 GdkAtom type
= selection_data
->type
;
376 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
378 if ( strcmp(wxGtkString(gdk_atom_name(type
)), "TARGETS") != 0 )
380 wxLogTrace( TRACE_CLIPBOARD
,
381 _T("got unsupported clipboard target") );
383 clipboard
->m_sink
->QueueEvent( event
);
384 clipboard
->m_sink
.Release();
390 // it's not really a format, of course, but we can reuse its GetId() method
391 // to format this atom as string
392 wxDataFormat
clip(selection_data
->selection
);
393 wxLogTrace( TRACE_CLIPBOARD
,
394 wxT("Received available formats for clipboard %s"),
395 clip
.GetId().c_str() );
396 #endif // __WXDEBUG__
398 // the atoms we received, holding a list of targets (= formats)
399 const GdkAtom
* const atoms
= (GdkAtom
*)selection_data
->data
;
400 for ( size_t i
= 0; i
< selection_data
->length
/sizeof(GdkAtom
); i
++ )
402 const wxDataFormat
format(atoms
[i
]);
404 wxLogTrace(TRACE_CLIPBOARD
, wxT("\t%s"), format
.GetId().c_str());
406 event
->AddFormat( format
);
409 clipboard
->m_sink
->QueueEvent( event
);
410 clipboard
->m_sink
.Release();
414 // ============================================================================
415 // wxClipboard implementation
416 // ============================================================================
418 // ----------------------------------------------------------------------------
419 // wxClipboard ctor/dtor
420 // ----------------------------------------------------------------------------
422 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
424 wxClipboard::wxClipboard()
430 m_receivedData
= NULL
;
432 m_formatSupported
= false;
433 m_targetRequested
= 0;
435 // we use m_targetsWidget to query what formats are available
436 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
437 gtk_widget_realize( m_targetsWidget
);
439 g_signal_connect (m_targetsWidget
, "selection_received",
440 G_CALLBACK (targets_selection_received
), this);
442 // we use m_targetsWidgetAsync to query what formats are available asynchronously
443 m_targetsWidgetAsync
= gtk_window_new( GTK_WINDOW_POPUP
);
444 gtk_widget_realize( m_targetsWidgetAsync
);
446 g_signal_connect (m_targetsWidgetAsync
, "selection_received",
447 G_CALLBACK (async_targets_selection_received
), this);
449 // we use m_clipboardWidget to get and to offer data
450 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
451 gtk_widget_realize( m_clipboardWidget
);
453 g_signal_connect (m_clipboardWidget
, "selection_received",
454 G_CALLBACK (selection_received
), this);
456 g_signal_connect (m_clipboardWidget
, "selection_clear_event",
457 G_CALLBACK (selection_clear_clip
), NULL
);
459 // initialize atoms we use if not done yet
460 if ( !g_clipboardAtom
)
461 g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
462 if ( !g_targetsAtom
)
463 g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
464 if ( !g_timestampAtom
)
465 g_timestampAtom
= gdk_atom_intern ("TIMESTAMP", FALSE
);
468 wxClipboard::~wxClipboard()
472 gtk_widget_destroy( m_clipboardWidget
);
473 gtk_widget_destroy( m_targetsWidget
);
476 // ----------------------------------------------------------------------------
477 // wxClipboard helper functions
478 // ----------------------------------------------------------------------------
480 GdkAtom
wxClipboard::GTKGetClipboardAtom() const
482 return m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
486 void wxClipboard::GTKClearData(Kind kind
)
488 wxDataObject
*&data
= Data(kind
);
496 bool wxClipboard::SetSelectionOwner(bool set
)
498 bool rc
= gtk_selection_owner_set
500 set
? m_clipboardWidget
: NULL
,
501 GTKGetClipboardAtom(),
502 (guint32
)GDK_CURRENT_TIME
507 wxLogTrace(TRACE_CLIPBOARD
, _T("Failed to %sset selection owner"),
508 set
? _T("") : _T("un"));
514 void wxClipboard::AddSupportedTarget(GdkAtom atom
)
516 gtk_selection_add_target
519 GTKGetClipboardAtom(),
521 0 // info (same as client data) unused
525 bool wxClipboard::IsSupportedAsync(wxEvtHandler
*sink
)
528 return false; // currently busy, come back later
530 wxCHECK_MSG( sink
, false, wxT("no sink given") );
533 gtk_selection_convert( m_targetsWidgetAsync
,
534 GTKGetClipboardAtom(),
536 (guint32
) GDK_CURRENT_TIME
);
541 bool wxClipboard::DoIsSupported(const wxDataFormat
& format
)
543 wxCHECK_MSG( format
, false, wxT("invalid clipboard format") );
545 wxLogTrace(TRACE_CLIPBOARD
, wxT("Checking if format %s is available"),
546 format
.GetId().c_str());
548 // these variables will be used by our GTKOnTargetReceived()
549 m_targetRequested
= format
;
550 m_formatSupported
= false;
552 // block until m_formatSupported is set from targets_selection_received
555 wxClipboardSync
sync(*this);
557 gtk_selection_convert( m_targetsWidget
,
558 GTKGetClipboardAtom(),
560 (guint32
) GDK_CURRENT_TIME
);
563 return m_formatSupported
;
566 // ----------------------------------------------------------------------------
567 // wxClipboard public API implementation
568 // ----------------------------------------------------------------------------
570 void wxClipboard::Clear()
572 if ( gdk_selection_owner_get(GTKGetClipboardAtom()) ==
573 m_clipboardWidget
->window
)
575 wxClipboardSync
sync(*this);
577 // this will result in selection_clear_clip callback being called and
578 // it will free our data
579 SetSelectionOwner(false);
582 m_targetRequested
= 0;
583 m_formatSupported
= false;
586 bool wxClipboard::Open()
588 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
595 bool wxClipboard::SetData( wxDataObject
*data
)
597 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
599 wxCHECK_MSG( data
, false, wxT("data is invalid") );
603 return AddData( data
);
606 bool wxClipboard::AddData( wxDataObject
*data
)
608 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
610 wxCHECK_MSG( data
, false, wxT("data is invalid") );
612 // we can only store one wxDataObject so clear the old one
617 // get formats from wxDataObjects
618 const size_t count
= data
->GetFormatCount();
619 wxDataFormatArray
formats(new wxDataFormat
[count
]);
620 data
->GetAllFormats(formats
.get());
622 // always provide TIMESTAMP as a target, see comments in selection_handler
624 AddSupportedTarget(g_timestampAtom
);
626 for ( size_t i
= 0; i
< count
; i
++ )
628 const wxDataFormat
format(formats
[i
]);
630 wxLogTrace(TRACE_CLIPBOARD
, wxT("Adding support for %s"),
631 format
.GetId().c_str());
633 AddSupportedTarget(format
);
636 g_signal_connect (m_clipboardWidget
, "selection_get",
637 G_CALLBACK (selection_handler
),
638 GUINT_TO_POINTER (gtk_get_current_event_time()) );
640 // tell the world we offer clipboard data
641 return SetSelectionOwner();
644 void wxClipboard::Close()
646 wxCHECK_RET( m_open
, wxT("clipboard not open") );
651 bool wxClipboard::IsOpened() const
656 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
658 if ( DoIsSupported(format
) )
662 if ( format
== wxDF_UNICODETEXT
)
664 // also with plain STRING format
665 return DoIsSupported(g_altTextAtom
);
667 #endif // wxUSE_UNICODE
672 bool wxClipboard::GetData( wxDataObject
& data
)
674 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
676 // get all supported formats from wxDataObjects
677 const size_t count
= data
.GetFormatCount();
678 wxDataFormatArray
formats(new wxDataFormat
[count
]);
679 data
.GetAllFormats(formats
.get());
681 for ( size_t i
= 0; i
< count
; i
++ )
683 const wxDataFormat
format(formats
[i
]);
685 // is this format supported by clipboard ?
686 if ( !DoIsSupported(format
) )
689 wxLogTrace(TRACE_CLIPBOARD
, wxT("Requesting format %s"),
690 format
.GetId().c_str());
692 // these variables will be used by our GTKOnSelectionReceived()
693 m_receivedData
= &data
;
694 m_formatSupported
= false;
697 wxClipboardSync
sync(*this);
699 gtk_selection_convert(m_clipboardWidget
,
700 GTKGetClipboardAtom(),
702 (guint32
) GDK_CURRENT_TIME
);
703 } // wait until we get the results
706 Normally this is a true error as we checked for the presence of such
707 data before, but there are applications that may return an empty
708 string (e.g. Gnumeric-1.6.1 on Linux if an empty cell is copied)
709 which would produce a false error message here, so we check for the
710 size of the string first. With ANSI, GetDataSize returns an extra
711 value (for the closing null?), with unicode, the exact number of
712 tokens is given (that is more than 1 for non-ASCII characters)
713 (tested with Gnumeric-1.6.1 and OpenOffice.org-2.0.2)
716 if ( format
!= wxDF_UNICODETEXT
|| data
.GetDataSize(format
) > 0 )
718 if ( format
!= wxDF_TEXT
|| data
.GetDataSize(format
) > 1 )
719 #endif // UNICODE / !UNICODE
721 wxCHECK_MSG( m_formatSupported
, false,
722 wxT("error retrieving data from clipboard") );
728 wxLogTrace(TRACE_CLIPBOARD
, wxT("GetData(): format not found"));
733 #endif // wxUSE_CLIPBOARD