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 wxClipboard
*wxTheClipboard
= (wxClipboard
*) NULL
;
41 GdkAtom g_clipboardAtom
= 0;
42 GdkAtom g_targetsAtom
= 0;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 /* The contents of a selection are returned in a GtkSelectionData
49 structure. selection/target identify the request.
50 type specifies the type of the return; if length < 0, and
51 the data should be ignored. This structure has object semantics -
52 no fields should be modified directly, they should not be created
53 directly, and pointers to them should not be stored beyond the duration of
54 a callback. (If the last is changed, we'll need to add reference
57 struct _GtkSelectionData
69 //-----------------------------------------------------------------------------
70 // "selection_received" for targets
71 //-----------------------------------------------------------------------------
74 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
75 GtkSelectionData
*selection_data
,
76 #if (GTK_MINOR_VERSION > 0)
77 guint32
WXUNUSED(time
),
79 wxClipboard
*clipboard
)
83 clipboard
->m_waiting
= FALSE
;
87 if (selection_data
->length
<= 0)
89 clipboard
->m_waiting
= FALSE
;
93 /* make sure we got the data in the correct form */
94 if (selection_data
->type
!= GDK_SELECTION_TYPE_ATOM
)
96 clipboard
->m_waiting
= FALSE
;
100 // the atoms we received, holding a list of targets (= formats)
101 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
103 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
105 wxDataFormat
format( atoms
[i
] );
107 if (format
== clipboard
->m_targetRequested
)
109 clipboard
->m_waiting
= FALSE
;
110 clipboard
->m_formatSupported
= TRUE
;
115 clipboard
->m_waiting
= FALSE
;
119 //-----------------------------------------------------------------------------
120 // "selection_received" for the actual data
121 //-----------------------------------------------------------------------------
124 selection_received( GtkWidget
*WXUNUSED(widget
),
125 GtkSelectionData
*selection_data
,
126 #if (GTK_MINOR_VERSION > 0)
127 guint32
WXUNUSED(time
),
129 wxClipboard
*clipboard
)
133 clipboard
->m_waiting
= FALSE
;
137 wxDataObject
*data_object
= clipboard
->m_receivedData
;
141 clipboard
->m_waiting
= FALSE
;
145 if (selection_data
->length
<= 0)
147 clipboard
->m_waiting
= FALSE
;
151 wxDataFormat
format( selection_data
->target
);
153 /* make sure we got the data in the correct format */
154 if (!data_object
->IsSupportedFormat( format
) )
156 clipboard
->m_waiting
= FALSE
;
160 /* make sure we got the data in the correct form (selection type).
161 if so, copy data to target object */
162 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
164 clipboard
->m_waiting
= FALSE
;
168 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
170 wxTheClipboard
->m_formatSupported
= TRUE
;
171 clipboard
->m_waiting
= FALSE
;
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
179 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
181 if (!wxTheClipboard
) return TRUE
;
183 if (event
->selection
== GDK_SELECTION_PRIMARY
)
185 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
188 if (event
->selection
== g_clipboardAtom
)
190 wxTheClipboard
->m_ownsClipboard
= FALSE
;
194 wxTheClipboard
->m_waiting
= FALSE
;
198 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
199 (!wxTheClipboard
->m_ownsClipboard
))
201 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
202 if (wxTheClipboard
->m_data
)
204 delete wxTheClipboard
->m_data
;
205 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
209 wxTheClipboard
->m_waiting
= FALSE
;
213 //-----------------------------------------------------------------------------
214 // selection handler for supplying data
215 //-----------------------------------------------------------------------------
218 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
220 if (!wxTheClipboard
) return;
222 if (!wxTheClipboard
->m_data
) return;
224 wxDataObject
*data
= wxTheClipboard
->m_data
;
226 wxDataFormat
format( selection_data
->target
);
228 if (!data
->IsSupportedFormat( format
)) return;
230 /* this will fail for composite formats */
231 if (format
.GetType() == wxDF_TEXT
)
233 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
234 wxString
text( text_object
->GetText() );
237 const wxWX2MBbuf s
= text
.mbc_str();
239 #else // more efficient in non-Unicode
240 const char *s
= text
.c_str();
241 int len
= (int) text
.Length();
243 gtk_selection_data_set(
245 GDK_SELECTION_TYPE_STRING
,
247 (unsigned char*) (const char*) s
,
253 int size
= data
->GetDataSize( format
);
255 if (size
== 0) return;
257 char *d
= new char[size
];
259 data
->GetDataHere( selection_data
->target
, (void*) d
);
261 gtk_selection_data_set(
263 GDK_SELECTION_TYPE_STRING
,
269 //-----------------------------------------------------------------------------
271 //-----------------------------------------------------------------------------
273 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
275 wxClipboard::wxClipboard()
279 m_ownsClipboard
= FALSE
;
280 m_ownsPrimarySelection
= FALSE
;
282 m_data
= (wxDataObject
*) NULL
;
283 m_receivedData
= (wxDataObject
*) NULL
;
285 /* we use m_targetsWidget to query what formats are available */
287 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
288 gtk_widget_realize( m_targetsWidget
);
290 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
291 "selection_received",
292 GTK_SIGNAL_FUNC( targets_selection_received
),
295 /* we use m_clipboardWidget to get and to offer data */
297 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
298 gtk_widget_realize( m_clipboardWidget
);
300 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
301 "selection_received",
302 GTK_SIGNAL_FUNC( selection_received
),
305 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
306 "selection_clear_event",
307 GTK_SIGNAL_FUNC( selection_clear_clip
),
310 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
311 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
313 m_formatSupported
= FALSE
;
314 m_targetRequested
= 0;
316 m_usePrimary
= FALSE
;
319 wxClipboard::~wxClipboard()
323 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
324 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
327 void wxClipboard::Clear()
332 /* disable GUI threads */
333 wxapp_uninstall_thread_wakeup();
336 /* As we have data we also own the clipboard. Once we no longer own
337 it, clear_selection is called which will set m_data to zero */
338 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
342 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
344 while (m_waiting
) gtk_main_iteration();
347 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
351 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
353 while (m_waiting
) gtk_main_iteration();
359 m_data
= (wxDataObject
*) NULL
;
363 /* re-enable GUI threads */
364 wxapp_install_thread_wakeup();
368 m_targetRequested
= 0;
369 m_formatSupported
= FALSE
;
372 bool wxClipboard::Open()
374 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
381 bool wxClipboard::SetData( wxDataObject
*data
)
383 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
385 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
389 return AddData( data
);
392 bool wxClipboard::AddData( wxDataObject
*data
)
394 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
396 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
398 /* we can only store one wxDataObject */
403 /* This should happen automatically, but to be on the safe side */
404 m_ownsClipboard
= FALSE
;
405 m_ownsPrimarySelection
= FALSE
;
407 /* get formats from wxDataObjects */
408 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
409 m_data
->GetAllFormats( array
);
411 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
413 GdkAtom atom
= array
[i
];
414 wxLogDebug( wxT("Clipboard Supported atom %s"), gdk_atom_name( atom
) );
416 /* Add handlers if someone requests data. We currently always
417 offer data to the clipboard and the primary selection. Maybe
418 we should make that depend on the usePrimary flag */
420 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
421 GDK_SELECTION_PRIMARY
,
423 0 ); /* what is info ? */
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 if (!gtk_selection_owner_set( m_clipboardWidget
,
449 /* re-enable GUI threads */
450 wxapp_install_thread_wakeup();
454 m_ownsClipboard
= TRUE
;
456 if (!gtk_selection_owner_set( m_clipboardWidget
,
457 GDK_SELECTION_PRIMARY
,
461 /* re-enable GUI threads */
462 wxapp_install_thread_wakeup();
466 m_ownsPrimarySelection
= TRUE
;
469 /* re-enable GUI threads */
470 wxapp_install_thread_wakeup();
476 void wxClipboard::Close()
478 wxCHECK_RET( m_open
, wxT("clipboard not open") );
483 bool wxClipboard::IsOpened() const
488 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
490 /* reentrance problems */
491 if (m_open
) return TRUE
;
493 /* store requested format to be asked for by callbacks */
494 m_targetRequested
= format
;
496 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
498 m_formatSupported
= FALSE
;
500 /* perform query. this will set m_formatSupported to
501 TRUE if m_targetRequested is supported.
502 alsom we have to wait for the "answer" from the
503 clipboard owner which is an asynchronous process.
504 therefore we set m_waiting = TRUE here and wait
505 until the callback "targets_selection_received"
510 gtk_selection_convert( m_targetsWidget
,
511 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
516 while (m_waiting
) gtk_main_iteration();
518 if (!m_formatSupported
) return FALSE
;
523 bool wxClipboard::GetData( wxDataObject
& data
)
525 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
527 /* get formats from wxDataObjects */
528 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
529 data
.GetAllFormats( array
);
531 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
533 wxDataFormat
format( array
[i
] );
535 wxLogDebug( wxT("wxClipboard::GetData: request format %s"), format
.GetId().c_str() );
537 /* is data supported by clipboard ? */
538 if (!IsSupported( format
))
541 /* store pointer to data object to be filled up by callbacks */
542 m_receivedData
= &data
;
544 /* store requested format to be asked for by callbacks */
545 m_targetRequested
= format
;
547 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
550 m_formatSupported
= FALSE
;
552 /* ask for clipboard contents. this will set
553 m_formatSupported to TRUE if m_targetRequested
555 also, we have to wait for the "answer" from the
556 clipboard owner which is an asynchronous process.
557 therefore we set m_waiting = TRUE here and wait
558 until the callback "targets_selection_received"
563 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
565 gtk_selection_convert( m_clipboardWidget
,
566 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
571 while (m_waiting
) gtk_main_iteration();
573 /* this is a true error as we checked for the presence of such data before */
574 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );