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
,
352 (guint32
) GDK_CURRENT_TIME
);
354 while (m_waiting
) gtk_main_iteration();
357 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
361 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
362 (guint32
) GDK_CURRENT_TIME
);
364 while (m_waiting
) gtk_main_iteration();
370 m_data
= (wxDataObject
*) NULL
;
374 /* re-enable GUI threads */
375 wxapp_install_thread_wakeup();
379 m_targetRequested
= 0;
380 m_formatSupported
= FALSE
;
383 bool wxClipboard::Open()
385 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
392 bool wxClipboard::SetData( wxDataObject
*data
)
394 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
396 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
400 return AddData( data
);
403 bool wxClipboard::AddData( wxDataObject
*data
)
405 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
407 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
409 /* we can only store one wxDataObject */
414 /* get formats from wxDataObjects */
415 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
416 m_data
->GetAllFormats( array
);
418 /* primary selection or clipboard */
419 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
423 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
425 wxLogDebug( wxT("wxClipboard now supports atom %s"), 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 */
442 wxapp_uninstall_thread_wakeup();
445 /* Tell the world we offer clipboard data */
446 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
448 (guint32
) GDK_CURRENT_TIME
));
451 m_ownsPrimarySelection
= res
;
453 m_ownsClipboard
= res
;
456 /* re-enable GUI threads */
457 wxapp_install_thread_wakeup();
463 void wxClipboard::Close()
465 wxCHECK_RET( m_open
, wxT("clipboard not open") );
470 bool wxClipboard::IsOpened() const
475 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
477 /* reentrance problems */
478 if (m_open
) return TRUE
;
480 /* store requested format to be asked for by callbacks */
481 m_targetRequested
= format
;
483 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
485 m_formatSupported
= FALSE
;
487 /* perform query. this will set m_formatSupported to
488 TRUE if m_targetRequested is supported.
489 alsom we have to wait for the "answer" from the
490 clipboard owner which is an asynchronous process.
491 therefore we set m_waiting = TRUE here and wait
492 until the callback "targets_selection_received"
497 gtk_selection_convert( m_targetsWidget
,
498 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
501 (guint32
) GDK_CURRENT_TIME
);
503 while (m_waiting
) gtk_main_iteration();
505 if (!m_formatSupported
) return FALSE
;
510 bool wxClipboard::GetData( wxDataObject
& data
)
512 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
514 /* get formats from wxDataObjects */
515 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
516 data
.GetAllFormats( array
);
518 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
520 wxDataFormat
format( array
[i
] );
522 wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format
.GetId().c_str() );
524 /* is data supported by clipboard ? */
526 /* store requested format to be asked for by callbacks */
527 m_targetRequested
= format
;
529 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
531 m_formatSupported
= FALSE
;
533 /* perform query. this will set m_formatSupported to
534 TRUE if m_targetRequested is supported.
535 also, we have to wait for the "answer" from the
536 clipboard owner which is an asynchronous process.
537 therefore we set m_waiting = TRUE here and wait
538 until the callback "targets_selection_received"
543 gtk_selection_convert( m_targetsWidget
,
544 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
547 (guint32
) GDK_CURRENT_TIME
);
549 while (m_waiting
) gtk_main_iteration();
551 if (!m_formatSupported
) continue;
553 /* store pointer to data object to be filled up by callbacks */
554 m_receivedData
= &data
;
556 /* store requested format to be asked for by callbacks */
557 m_targetRequested
= format
;
559 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
562 m_formatSupported
= FALSE
;
564 /* ask for clipboard contents. this will set
565 m_formatSupported to TRUE if m_targetRequested
567 also, we have to wait for the "answer" from the
568 clipboard owner which is an asynchronous process.
569 therefore we set m_waiting = TRUE here and wait
570 until the callback "targets_selection_received"
575 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
577 gtk_selection_convert( m_clipboardWidget
,
578 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
581 (guint32
) GDK_CURRENT_TIME
);
583 while (m_waiting
) gtk_main_iteration();
585 /* this is a true error as we checked for the presence of such data before */
586 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
593 wxLogDebug( wxT("wxClipboard::GetData: format not found") );