1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "clipbrd.h"
15 #include "wx/clipbrd.h"
19 #include "wx/dataobj.h"
23 #include "wx/x11/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 Atom g_clipboardAtom
= 0;
30 Atom g_targetsAtom
= 0;
32 // the trace mask we use with wxLogTrace() - call
33 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
34 // (there will be a *lot* of them!)
35 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 /* The contents of a selection are returned in a GtkSelectionData
42 structure. selection/target identify the request.
43 type specifies the type of the return; if length < 0, and
44 the data should be ignored. This structure has object semantics -
45 no fields should be modified directly, they should not be created
46 directly, and pointers to them should not be stored beyond the duration of
47 a callback. (If the last is changed, we'll need to add reference
50 struct _GtkSelectionData
62 //-----------------------------------------------------------------------------
63 // "selection_received" for targets
64 //-----------------------------------------------------------------------------
69 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
70 GtkSelectionData
*selection_data
,
71 #if (GTK_MINOR_VERSION > 0)
72 guint32
WXUNUSED(time
),
74 wxClipboard
*clipboard
)
76 if ( wxTheClipboard
&& selection_data
->length
> 0 )
78 /* make sure we got the data in the correct form */
79 GdkAtom type
= selection_data
->type
;
80 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
82 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
84 wxLogTrace( TRACE_CLIPBOARD
,
85 _T("got unsupported clipboard target") );
87 clipboard
->m_waiting
= FALSE
;
93 wxDataFormat
clip( selection_data
->selection
);
94 wxLogTrace( TRACE_CLIPBOARD
,
95 wxT("selection received for targets, clipboard %s"),
96 clip
.GetId().c_str() );
99 // the atoms we received, holding a list of targets (= formats)
100 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
102 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
104 wxDataFormat
format( atoms
[i
] );
106 wxLogTrace( TRACE_CLIPBOARD
,
107 wxT("selection received for targets, format %s"),
108 format
.GetId().c_str() );
110 if (format
== clipboard
->m_targetRequested
)
112 clipboard
->m_waiting
= FALSE
;
113 clipboard
->m_formatSupported
= TRUE
;
119 clipboard
->m_waiting
= FALSE
;
122 //-----------------------------------------------------------------------------
123 // "selection_received" for the actual data
124 //-----------------------------------------------------------------------------
127 selection_received( GtkWidget
*WXUNUSED(widget
),
128 GtkSelectionData
*selection_data
,
129 #if (GTK_MINOR_VERSION > 0)
130 guint32
WXUNUSED(time
),
132 wxClipboard
*clipboard
)
136 clipboard
->m_waiting
= FALSE
;
140 wxDataObject
*data_object
= clipboard
->m_receivedData
;
144 clipboard
->m_waiting
= FALSE
;
148 if (selection_data
->length
<= 0)
150 clipboard
->m_waiting
= FALSE
;
154 wxDataFormat
format( selection_data
->target
);
156 /* make sure we got the data in the correct format */
157 if (!data_object
->IsSupportedFormat( format
) )
159 clipboard
->m_waiting
= FALSE
;
163 /* make sure we got the data in the correct form (selection type).
164 if so, copy data to target object */
165 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
167 clipboard
->m_waiting
= FALSE
;
171 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
173 wxTheClipboard
->m_formatSupported
= TRUE
;
174 clipboard
->m_waiting
= FALSE
;
177 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
182 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
184 if (!wxTheClipboard
) return TRUE
;
186 if (event
->selection
== GDK_SELECTION_PRIMARY
)
188 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
191 if (event
->selection
== g_clipboardAtom
)
193 wxTheClipboard
->m_ownsClipboard
= FALSE
;
197 wxTheClipboard
->m_waiting
= FALSE
;
201 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
202 (!wxTheClipboard
->m_ownsClipboard
))
204 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
205 if (wxTheClipboard
->m_data
)
207 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
209 delete wxTheClipboard
->m_data
;
210 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
214 wxTheClipboard
->m_waiting
= FALSE
;
218 //-----------------------------------------------------------------------------
219 // selection handler for supplying data
220 //-----------------------------------------------------------------------------
223 selection_handler( GtkWidget
*WXUNUSED(widget
),
224 GtkSelectionData
*selection_data
,
225 guint
WXUNUSED(info
),
226 guint
WXUNUSED(time
),
227 gpointer
WXUNUSED(data
) )
229 if (!wxTheClipboard
) return;
231 if (!wxTheClipboard
->m_data
) return;
233 wxDataObject
*data
= wxTheClipboard
->m_data
;
235 wxDataFormat
format( selection_data
->target
);
237 if (!data
->IsSupportedFormat( format
)) return;
239 int size
= data
->GetDataSize( format
);
241 if (size
== 0) return;
243 void *d
= malloc(size
);
245 data
->GetDataHere( selection_data
->target
, d
);
247 // transform Unicode text into multibyte before putting it on clipboard
249 if ( format
.GetType() == wxDF_TEXT
)
251 const wchar_t *wstr
= (const wchar_t *)d
;
252 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
253 char *str
= malloc(len
+ 1);
254 wxConvCurrent
->WC2MB(str
, wstr
, len
);
260 #endif // wxUSE_UNICODE
262 gtk_selection_data_set(
264 GDK_SELECTION_TYPE_STRING
,
274 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------
278 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
280 wxClipboard::wxClipboard()
284 m_ownsClipboard
= FALSE
;
285 m_ownsPrimarySelection
= FALSE
;
287 m_data
= (wxDataObject
*) NULL
;
288 m_receivedData
= (wxDataObject
*) NULL
;
290 /* we use m_targetsWidget to query what formats are available */
292 /* we use m_clipboardWidget to get and to offer data */
294 if (!g_clipboardAtom
) g_clipboardAtom
= XInternAtom( (Display
*) wxGetDisplay(), "CLIPBOARD", False
);
295 if (!g_targetsAtom
) g_targetsAtom
= XInternAtom( (Display
*) wxGetDisplay(), "TARGETS", False
);
297 m_formatSupported
= FALSE
;
298 m_targetRequested
= 0;
300 m_usePrimary
= FALSE
;
303 wxClipboard::~wxClipboard()
307 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
308 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
311 void wxClipboard::Clear()
316 /* disable GUI threads */
319 /* As we have data we also own the clipboard. Once we no longer own
320 it, clear_selection is called which will set m_data to zero */
322 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
326 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
327 (guint32
) GDK_CURRENT_TIME
);
329 while (m_waiting
) gtk_main_iteration();
332 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
336 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
337 (guint32
) GDK_CURRENT_TIME
);
339 while (m_waiting
) gtk_main_iteration();
346 m_data
= (wxDataObject
*) NULL
;
350 /* re-enable GUI threads */
354 m_targetRequested
= 0;
355 m_formatSupported
= FALSE
;
358 bool wxClipboard::Open()
360 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
367 bool wxClipboard::SetData( wxDataObject
*data
)
369 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
371 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
375 return AddData( data
);
378 bool wxClipboard::AddData( wxDataObject
*data
)
380 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
382 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
384 /* we can only store one wxDataObject */
389 /* get formats from wxDataObjects */
390 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
391 m_data
->GetAllFormats( array
);
393 /* primary selection or clipboard */
394 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
398 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
400 wxLogTrace( TRACE_CLIPBOARD
,
401 wxT("wxClipboard now supports atom %s"),
402 array
[i
].GetId().c_str() );
405 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
408 0 ); /* what is info ? */
415 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
417 GTK_SIGNAL_FUNC(selection_handler
),
422 /* disable GUI threads */
427 /* Tell the world we offer clipboard data */
428 res
= (gtk_selection_owner_set( m_clipboardWidget
,
430 (guint32
) GDK_CURRENT_TIME
));
434 m_ownsPrimarySelection
= res
;
436 m_ownsClipboard
= res
;
439 /* re-enable GUI threads */
445 void wxClipboard::Close()
447 wxCHECK_RET( m_open
, wxT("clipboard not open") );
452 bool wxClipboard::IsOpened() const
457 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
459 /* reentrance problems */
460 if (m_waiting
) return FALSE
;
462 /* store requested format to be asked for by callbacks */
463 m_targetRequested
= format
;
466 wxLogTrace( TRACE_CLIPBOARD
,
467 wxT("wxClipboard:IsSupported: requested format: %s"),
468 format
.GetId().c_str() );
471 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
473 m_formatSupported
= FALSE
;
475 /* perform query. this will set m_formatSupported to
476 TRUE if m_targetRequested is supported.
477 also, we have to wait for the "answer" from the
478 clipboard owner which is an asynchronous process.
479 therefore we set m_waiting = TRUE here and wait
480 until the callback "targets_selection_received"
486 gtk_selection_convert( m_targetsWidget
,
487 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
490 (guint32
) GDK_CURRENT_TIME
);
492 while (m_waiting
) gtk_main_iteration();
495 if (!m_formatSupported
) return FALSE
;
500 bool wxClipboard::GetData( wxDataObject
& data
)
502 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
504 /* get formats from wxDataObjects */
505 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
506 data
.GetAllFormats( array
);
508 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
510 wxDataFormat
format( array
[i
] );
512 wxLogTrace( TRACE_CLIPBOARD
,
513 wxT("wxClipboard::GetData: requested format: %s"),
514 format
.GetId().c_str() );
516 /* is data supported by clipboard ? */
518 /* store requested format to be asked for by callbacks */
519 m_targetRequested
= format
;
521 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
523 m_formatSupported
= FALSE
;
525 /* perform query. this will set m_formatSupported to
526 TRUE if m_targetRequested is supported.
527 also, we have to wait for the "answer" from the
528 clipboard owner which is an asynchronous process.
529 therefore we set m_waiting = TRUE here and wait
530 until the callback "targets_selection_received"
536 gtk_selection_convert( m_targetsWidget
,
537 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
540 (guint32
) GDK_CURRENT_TIME
);
542 while (m_waiting
) gtk_main_iteration();
545 if (!m_formatSupported
) continue;
547 /* store pointer to data object to be filled up by callbacks */
548 m_receivedData
= &data
;
550 /* store requested format to be asked for by callbacks */
551 m_targetRequested
= format
;
553 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
556 m_formatSupported
= FALSE
;
558 /* ask for clipboard contents. this will set
559 m_formatSupported to TRUE if m_targetRequested
561 also, we have to wait for the "answer" from the
562 clipboard owner which is an asynchronous process.
563 therefore we set m_waiting = TRUE here and wait
564 until the callback "targets_selection_received"
569 wxLogTrace( TRACE_CLIPBOARD
,
570 wxT("wxClipboard::GetData: format found, start convert") );
573 gtk_selection_convert( m_clipboardWidget
,
574 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
577 (guint32
) GDK_CURRENT_TIME
);
579 while (m_waiting
) gtk_main_iteration();
582 /* this is a true error as we checked for the presence of such data before */
583 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
590 wxLogTrace( TRACE_CLIPBOARD
,
591 wxT("wxClipboard::GetData: format not found") );