1 /////////////////////////////////////////////////////////////////////////////
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 //-----------------------------------------------------------------------------
31 extern void wxapp_install_thread_wakeup();
32 extern void wxapp_uninstall_thread_wakeup();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 GdkAtom g_clipboardAtom
= 0;
40 GdkAtom g_targetsAtom
= 0;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 /* The contents of a selection are returned in a GtkSelectionData
47 structure. selection/target identify the request.
48 type specifies the type of the return; if length < 0, and
49 the data should be ignored. This structure has object semantics -
50 no fields should be modified directly, they should not be created
51 directly, and pointers to them should not be stored beyond the duration of
52 a callback. (If the last is changed, we'll need to add reference
55 struct _GtkSelectionData
67 //-----------------------------------------------------------------------------
68 // "selection_received" for targets
69 //-----------------------------------------------------------------------------
72 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
73 GtkSelectionData
*selection_data
,
74 #if (GTK_MINOR_VERSION > 0)
75 guint32
WXUNUSED(time
),
77 wxClipboard
*clipboard
)
81 clipboard
->m_waiting
= FALSE
;
85 if (selection_data
->length
<= 0)
87 clipboard
->m_waiting
= FALSE
;
91 /* make sure we got the data in the correct form */
92 if (selection_data
->type
!= GDK_SELECTION_TYPE_ATOM
)
94 clipboard
->m_waiting
= FALSE
;
99 wxDataFormat clip( selection_data->selection );
100 wxLogDebug( wxT("selection received for targets, clipboard %s"), clip.GetId().c_str() );
103 // the atoms we received, holding a list of targets (= formats)
104 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
106 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
108 wxDataFormat
format( atoms
[i
] );
111 wxLogDebug( wxT("selection received for targets, format %s"), format.GetId().c_str() );
114 if (format
== clipboard
->m_targetRequested
)
116 clipboard
->m_waiting
= FALSE
;
117 clipboard
->m_formatSupported
= TRUE
;
122 clipboard
->m_waiting
= FALSE
;
126 //-----------------------------------------------------------------------------
127 // "selection_received" for the actual data
128 //-----------------------------------------------------------------------------
131 selection_received( GtkWidget
*WXUNUSED(widget
),
132 GtkSelectionData
*selection_data
,
133 #if (GTK_MINOR_VERSION > 0)
134 guint32
WXUNUSED(time
),
136 wxClipboard
*clipboard
)
140 clipboard
->m_waiting
= FALSE
;
144 wxDataObject
*data_object
= clipboard
->m_receivedData
;
148 clipboard
->m_waiting
= FALSE
;
152 if (selection_data
->length
<= 0)
154 clipboard
->m_waiting
= FALSE
;
158 wxDataFormat
format( selection_data
->target
);
160 /* make sure we got the data in the correct format */
161 if (!data_object
->IsSupportedFormat( format
) )
163 clipboard
->m_waiting
= FALSE
;
167 /* make sure we got the data in the correct form (selection type).
168 if so, copy data to target object */
169 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
171 clipboard
->m_waiting
= FALSE
;
175 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
177 wxTheClipboard
->m_formatSupported
= TRUE
;
178 clipboard
->m_waiting
= FALSE
;
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
186 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
188 if (!wxTheClipboard
) return TRUE
;
190 if (event
->selection
== GDK_SELECTION_PRIMARY
)
192 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
195 if (event
->selection
== g_clipboardAtom
)
197 wxTheClipboard
->m_ownsClipboard
= FALSE
;
201 wxTheClipboard
->m_waiting
= FALSE
;
205 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
206 (!wxTheClipboard
->m_ownsClipboard
))
208 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
209 if (wxTheClipboard
->m_data
)
211 wxLogDebug( wxT("wxClipboard will get cleared" ) );
213 delete wxTheClipboard
->m_data
;
214 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
218 wxTheClipboard
->m_waiting
= FALSE
;
222 //-----------------------------------------------------------------------------
223 // selection handler for supplying data
224 //-----------------------------------------------------------------------------
227 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, 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 /* this will fail for composite formats */
240 if (format
.GetType() == wxDF_TEXT
)
242 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
243 wxString
text( text_object
->GetText() );
246 const wxWX2MBbuf s
= text
.mbc_str();
248 #else // more efficient in non-Unicode
249 const char *s
= text
.c_str();
250 int len
= (int) text
.Length();
252 gtk_selection_data_set(
254 GDK_SELECTION_TYPE_STRING
,
256 (unsigned char*) (const char*) s
,
262 int size
= data
->GetDataSize( format
);
264 if (size
== 0) return;
266 char *d
= new char[size
];
268 data
->GetDataHere( selection_data
->target
, (void*) d
);
270 gtk_selection_data_set(
272 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 */
342 wxapp_uninstall_thread_wakeup();
345 /* As we have data we also own the clipboard. Once we no longer own
346 it, clear_selection is called which will set m_data to zero */
347 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
351 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, 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
, GDK_CURRENT_TIME
);
362 while (m_waiting
) gtk_main_iteration();
368 m_data
= (wxDataObject
*) NULL
;
372 /* re-enable GUI threads */
373 wxapp_install_thread_wakeup();
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 wxLogDebug( wxT("wxClipboard now supports atom %s"), array
[i
].GetId().c_str() );
425 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
428 0 ); /* what is info ? */
433 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
435 GTK_SIGNAL_FUNC(selection_handler
),
439 /* disable GUI threads */
440 wxapp_uninstall_thread_wakeup();
443 /* Tell the world we offer clipboard data */
444 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
449 m_ownsPrimarySelection
= res
;
451 m_ownsClipboard
= res
;
454 /* re-enable GUI threads */
455 wxapp_install_thread_wakeup();
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_open
) return TRUE
;
478 /* store requested format to be asked for by callbacks */
479 m_targetRequested
= format
;
481 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
483 m_formatSupported
= FALSE
;
485 /* perform query. this will set m_formatSupported to
486 TRUE if m_targetRequested is supported.
487 alsom we have to wait for the "answer" from the
488 clipboard owner which is an asynchronous process.
489 therefore we set m_waiting = TRUE here and wait
490 until the callback "targets_selection_received"
495 gtk_selection_convert( m_targetsWidget
,
496 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
501 while (m_waiting
) gtk_main_iteration();
503 if (!m_formatSupported
) return FALSE
;
508 bool wxClipboard::GetData( wxDataObject
& data
)
510 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
512 /* get formats from wxDataObjects */
513 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
514 data
.GetAllFormats( array
);
516 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
518 wxDataFormat
format( array
[i
] );
520 wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format
.GetId().c_str() );
522 /* is data supported by clipboard ? */
524 /* store requested format to be asked for by callbacks */
525 m_targetRequested
= format
;
527 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
529 m_formatSupported
= FALSE
;
531 /* perform query. this will set m_formatSupported to
532 TRUE if m_targetRequested is supported.
533 also, we have to wait for the "answer" from the
534 clipboard owner which is an asynchronous process.
535 therefore we set m_waiting = TRUE here and wait
536 until the callback "targets_selection_received"
541 gtk_selection_convert( m_targetsWidget
,
542 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
547 while (m_waiting
) gtk_main_iteration();
549 if (!m_formatSupported
) continue;
551 /* store pointer to data object to be filled up by callbacks */
552 m_receivedData
= &data
;
554 /* store requested format to be asked for by callbacks */
555 m_targetRequested
= format
;
557 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
560 m_formatSupported
= FALSE
;
562 /* ask for clipboard contents. this will set
563 m_formatSupported to TRUE if m_targetRequested
565 also, we have to wait for the "answer" from the
566 clipboard owner which is an asynchronous process.
567 therefore we set m_waiting = TRUE here and wait
568 until the callback "targets_selection_received"
573 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
575 gtk_selection_convert( m_clipboardWidget
,
576 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
581 while (m_waiting
) gtk_main_iteration();
583 /* this is a true error as we checked for the presence of such data before */
584 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
591 wxLogDebug( wxT("wxClipboard::GetData: format not found") );