1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "clipbrd.h"
14 #include "wx/clipbrd.h"
18 #include "wx/dataobj.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 GdkAtom g_clipboardAtom
= 0;
38 GdkAtom g_targetsAtom
= 0;
40 // the trace mask we use with wxLogTrace() - call
41 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
42 // (there will be a *lot* of them!)
43 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 /* The contents of a selection are returned in a GtkSelectionData
50 structure. selection/target identify the request.
51 type specifies the type of the return; if length < 0, and
52 the data should be ignored. This structure has object semantics -
53 no fields should be modified directly, they should not be created
54 directly, and pointers to them should not be stored beyond the duration of
55 a callback. (If the last is changed, we'll need to add reference
58 struct _GtkSelectionData
70 //-----------------------------------------------------------------------------
71 // "selection_received" for targets
72 //-----------------------------------------------------------------------------
75 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
76 GtkSelectionData
*selection_data
,
77 #if (GTK_MINOR_VERSION > 0)
78 guint32
WXUNUSED(time
),
80 wxClipboard
*clipboard
)
82 if ( wxTheClipboard
&& selection_data
->length
> 0 )
84 /* make sure we got the data in the correct form */
85 GdkAtom type
= selection_data
->type
;
86 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
88 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
90 wxLogTrace( TRACE_CLIPBOARD
,
91 _T("got unsupported clipboard target") );
93 clipboard
->m_waiting
= FALSE
;
99 wxDataFormat
clip( selection_data
->selection
);
100 wxLogTrace( TRACE_CLIPBOARD
,
101 wxT("selection received for targets, clipboard %s"),
102 clip
.GetId().c_str() );
103 #endif // __WXDEBUG__
105 // the atoms we received, holding a list of targets (= formats)
106 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
108 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
110 wxDataFormat
format( atoms
[i
] );
112 wxLogTrace( TRACE_CLIPBOARD
,
113 wxT("selection received for targets, format %s"),
114 format
.GetId().c_str() );
116 if (format
== clipboard
->m_targetRequested
)
118 clipboard
->m_waiting
= FALSE
;
119 clipboard
->m_formatSupported
= TRUE
;
125 clipboard
->m_waiting
= FALSE
;
128 //-----------------------------------------------------------------------------
129 // "selection_received" for the actual data
130 //-----------------------------------------------------------------------------
133 selection_received( GtkWidget
*WXUNUSED(widget
),
134 GtkSelectionData
*selection_data
,
135 #if (GTK_MINOR_VERSION > 0)
136 guint32
WXUNUSED(time
),
138 wxClipboard
*clipboard
)
142 clipboard
->m_waiting
= FALSE
;
146 wxDataObject
*data_object
= clipboard
->m_receivedData
;
150 clipboard
->m_waiting
= FALSE
;
154 if (selection_data
->length
<= 0)
156 clipboard
->m_waiting
= FALSE
;
160 wxDataFormat
format( selection_data
->target
);
162 /* make sure we got the data in the correct format */
163 if (!data_object
->IsSupportedFormat( format
) )
165 clipboard
->m_waiting
= FALSE
;
169 /* make sure we got the data in the correct form (selection type).
170 if so, copy data to target object */
171 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
173 clipboard
->m_waiting
= FALSE
;
177 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
179 wxTheClipboard
->m_formatSupported
= TRUE
;
180 clipboard
->m_waiting
= FALSE
;
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
188 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
190 if (!wxTheClipboard
) return TRUE
;
192 if (event
->selection
== GDK_SELECTION_PRIMARY
)
194 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
197 if (event
->selection
== g_clipboardAtom
)
199 wxTheClipboard
->m_ownsClipboard
= FALSE
;
203 wxTheClipboard
->m_waiting
= FALSE
;
207 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
208 (!wxTheClipboard
->m_ownsClipboard
))
210 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
211 if (wxTheClipboard
->m_data
)
213 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
215 delete wxTheClipboard
->m_data
;
216 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
220 wxTheClipboard
->m_waiting
= FALSE
;
224 //-----------------------------------------------------------------------------
225 // selection handler for supplying data
226 //-----------------------------------------------------------------------------
229 selection_handler( GtkWidget
*WXUNUSED(widget
),
230 GtkSelectionData
*selection_data
,
231 guint
WXUNUSED(info
),
232 guint
WXUNUSED(time
),
233 gpointer
WXUNUSED(data
) )
235 if (!wxTheClipboard
) return;
237 if (!wxTheClipboard
->m_data
) return;
239 wxDataObject
*data
= wxTheClipboard
->m_data
;
241 wxDataFormat
format( selection_data
->target
);
243 if (!data
->IsSupportedFormat( format
)) return;
245 int size
= data
->GetDataSize( format
);
247 if (size
== 0) return;
249 void *d
= malloc(size
);
251 data
->GetDataHere( selection_data
->target
, d
);
253 // transform Unicode text into multibyte before putting it on clipboard
255 if ( format
.GetType() == wxDF_TEXT
)
257 const wchar_t *wstr
= (const wchar_t *)d
;
258 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
259 char *str
= malloc(len
+ 1);
260 wxConvCurrent
->WC2MB(str
, wstr
, len
);
266 #endif // wxUSE_UNICODE
268 gtk_selection_data_set(
270 GDK_SELECTION_TYPE_STRING
,
278 //-----------------------------------------------------------------------------
280 //-----------------------------------------------------------------------------
282 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
284 wxClipboard::wxClipboard()
288 m_ownsClipboard
= FALSE
;
289 m_ownsPrimarySelection
= FALSE
;
291 m_data
= (wxDataObject
*) NULL
;
292 m_receivedData
= (wxDataObject
*) NULL
;
294 /* we use m_targetsWidget to query what formats are available */
296 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
297 gtk_widget_realize( m_targetsWidget
);
299 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
300 "selection_received",
301 GTK_SIGNAL_FUNC( targets_selection_received
),
304 /* we use m_clipboardWidget to get and to offer data */
306 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
307 gtk_widget_realize( m_clipboardWidget
);
309 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
310 "selection_received",
311 GTK_SIGNAL_FUNC( selection_received
),
314 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
315 "selection_clear_event",
316 GTK_SIGNAL_FUNC( selection_clear_clip
),
319 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
320 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
322 m_formatSupported
= FALSE
;
323 m_targetRequested
= 0;
325 m_usePrimary
= FALSE
;
328 wxClipboard::~wxClipboard()
332 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
333 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
336 void wxClipboard::Clear()
341 /* disable GUI threads */
344 /* As we have data we also own the clipboard. Once we no longer own
345 it, clear_selection is called which will set m_data to zero */
346 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
350 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
351 (guint32
) GDK_CURRENT_TIME
);
353 while (m_waiting
) gtk_main_iteration();
356 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
360 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
361 (guint32
) GDK_CURRENT_TIME
);
363 while (m_waiting
) gtk_main_iteration();
369 m_data
= (wxDataObject
*) NULL
;
373 /* re-enable GUI threads */
377 m_targetRequested
= 0;
378 m_formatSupported
= FALSE
;
381 bool wxClipboard::Open()
383 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
390 bool wxClipboard::SetData( wxDataObject
*data
)
392 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
394 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
398 return AddData( data
);
401 bool wxClipboard::AddData( wxDataObject
*data
)
403 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
405 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
407 /* we can only store one wxDataObject */
412 /* get formats from wxDataObjects */
413 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
414 m_data
->GetAllFormats( array
);
416 /* primary selection or clipboard */
417 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
421 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
423 wxLogTrace( TRACE_CLIPBOARD
,
424 wxT("wxClipboard now supports atom %s"),
425 array
[i
].GetId().c_str() );
427 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
430 0 ); /* what is info ? */
435 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
437 GTK_SIGNAL_FUNC(selection_handler
),
441 /* disable GUI threads */
444 /* Tell the world we offer clipboard data */
445 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
447 (guint32
) GDK_CURRENT_TIME
));
450 m_ownsPrimarySelection
= res
;
452 m_ownsClipboard
= res
;
455 /* re-enable GUI threads */
461 void wxClipboard::Close()
463 wxCHECK_RET( m_open
, wxT("clipboard not open") );
468 bool wxClipboard::IsOpened() const
473 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
475 /* reentrance problems */
476 if (m_waiting
) return FALSE
;
478 /* store requested format to be asked for by callbacks */
479 m_targetRequested
= format
;
482 wxLogTrace( TRACE_CLIPBOARD
,
483 wxT("wxClipboard:IsSupported: requested format: %s"),
484 format
.GetId().c_str() );
487 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
489 m_formatSupported
= FALSE
;
491 /* perform query. this will set m_formatSupported to
492 TRUE if m_targetRequested is supported.
493 also, we have to wait for the "answer" from the
494 clipboard owner which is an asynchronous process.
495 therefore we set m_waiting = TRUE here and wait
496 until the callback "targets_selection_received"
501 gtk_selection_convert( m_targetsWidget
,
502 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
505 (guint32
) GDK_CURRENT_TIME
);
507 while (m_waiting
) gtk_main_iteration();
509 if (!m_formatSupported
) return FALSE
;
514 bool wxClipboard::GetData( wxDataObject
& data
)
516 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
518 /* get formats from wxDataObjects */
519 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
520 data
.GetAllFormats( array
);
522 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
524 wxDataFormat
format( array
[i
] );
526 wxLogTrace( TRACE_CLIPBOARD
,
527 wxT("wxClipboard::GetData: requested format: %s"),
528 format
.GetId().c_str() );
530 /* is data supported by clipboard ? */
532 /* store requested format to be asked for by callbacks */
533 m_targetRequested
= format
;
535 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
537 m_formatSupported
= FALSE
;
539 /* perform query. this will set m_formatSupported to
540 TRUE if m_targetRequested is supported.
541 also, we have to wait for the "answer" from the
542 clipboard owner which is an asynchronous process.
543 therefore we set m_waiting = TRUE here and wait
544 until the callback "targets_selection_received"
549 gtk_selection_convert( m_targetsWidget
,
550 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
553 (guint32
) GDK_CURRENT_TIME
);
555 while (m_waiting
) gtk_main_iteration();
557 if (!m_formatSupported
) continue;
559 /* store pointer to data object to be filled up by callbacks */
560 m_receivedData
= &data
;
562 /* store requested format to be asked for by callbacks */
563 m_targetRequested
= format
;
565 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
568 m_formatSupported
= FALSE
;
570 /* ask for clipboard contents. this will set
571 m_formatSupported to TRUE if m_targetRequested
573 also, we have to wait for the "answer" from the
574 clipboard owner which is an asynchronous process.
575 therefore we set m_waiting = TRUE here and wait
576 until the callback "targets_selection_received"
581 wxLogTrace( TRACE_CLIPBOARD
,
582 wxT("wxClipboard::GetData: format found, start convert") );
584 gtk_selection_convert( m_clipboardWidget
,
585 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
588 (guint32
) GDK_CURRENT_TIME
);
590 while (m_waiting
) gtk_main_iteration();
592 /* this is a true error as we checked for the presence of such data before */
593 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
600 wxLogTrace( TRACE_CLIPBOARD
,
601 wxT("wxClipboard::GetData: format not found") );