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
)
79 if ( wxTheClipboard
&& selection_data
->length
> 0 )
81 /* make sure we got the data in the correct form */
83 // VZ: I don't know what does this mean (and GTK+ authors apparently
84 // don't know either, Owen Taylor writes that "Motif seems to ask
85 // for TARGETS atom sometimes" (??)), but it seems that xterm
86 // (which is not a Motif app AFAIK) does this too, so it's
87 // absolutely essential to support this, otherwise we can't paste
89 GdkAtom type
= selection_data
->type
;
90 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
92 if ( strcmp(gdk_atom_name(type
), "TARGETS") != 0 )
94 // don't know what this is
95 clipboard
->m_waiting
= FALSE
;
98 //else: don't know what this is, but it seems to work in the same
99 // way as GDK_SELECTION_TYPE_ATOM does
101 //else: the data is the list of formats supported by the selection
104 wxDataFormat clip( selection_data->selection );
105 wxLogDebug( wxT("selection received for targets, clipboard %s"), clip.GetId().c_str() );
108 // the atoms we received, holding a list of targets (= formats)
109 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
111 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
113 wxDataFormat
format( atoms
[i
] );
116 wxLogDebug( wxT("selection received for targets, format %s"), format.GetId().c_str() );
119 if (format
== clipboard
->m_targetRequested
)
121 clipboard
->m_waiting
= FALSE
;
122 clipboard
->m_formatSupported
= TRUE
;
128 clipboard
->m_waiting
= FALSE
;
131 //-----------------------------------------------------------------------------
132 // "selection_received" for the actual data
133 //-----------------------------------------------------------------------------
136 selection_received( GtkWidget
*WXUNUSED(widget
),
137 GtkSelectionData
*selection_data
,
138 #if (GTK_MINOR_VERSION > 0)
139 guint32
WXUNUSED(time
),
141 wxClipboard
*clipboard
)
145 clipboard
->m_waiting
= FALSE
;
149 wxDataObject
*data_object
= clipboard
->m_receivedData
;
153 clipboard
->m_waiting
= FALSE
;
157 if (selection_data
->length
<= 0)
159 clipboard
->m_waiting
= FALSE
;
163 wxDataFormat
format( selection_data
->target
);
165 /* make sure we got the data in the correct format */
166 if (!data_object
->IsSupportedFormat( format
) )
168 clipboard
->m_waiting
= FALSE
;
172 /* make sure we got the data in the correct form (selection type).
173 if so, copy data to target object */
174 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
176 clipboard
->m_waiting
= FALSE
;
180 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
182 wxTheClipboard
->m_formatSupported
= TRUE
;
183 clipboard
->m_waiting
= FALSE
;
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
191 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
193 if (!wxTheClipboard
) return TRUE
;
195 if (event
->selection
== GDK_SELECTION_PRIMARY
)
197 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
200 if (event
->selection
== g_clipboardAtom
)
202 wxTheClipboard
->m_ownsClipboard
= FALSE
;
206 wxTheClipboard
->m_waiting
= FALSE
;
210 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
211 (!wxTheClipboard
->m_ownsClipboard
))
213 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
214 if (wxTheClipboard
->m_data
)
216 wxLogDebug( wxT("wxClipboard will get cleared" ) );
218 delete wxTheClipboard
->m_data
;
219 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
223 wxTheClipboard
->m_waiting
= FALSE
;
227 //-----------------------------------------------------------------------------
228 // selection handler for supplying data
229 //-----------------------------------------------------------------------------
232 selection_handler( GtkWidget
*WXUNUSED(widget
),
233 GtkSelectionData
*selection_data
,
234 guint
WXUNUSED(info
),
235 guint
WXUNUSED(time
),
236 gpointer
WXUNUSED(data
) )
238 if (!wxTheClipboard
) return;
240 if (!wxTheClipboard
->m_data
) return;
242 wxDataObject
*data
= wxTheClipboard
->m_data
;
244 wxDataFormat
format( selection_data
->target
);
246 if (!data
->IsSupportedFormat( format
)) return;
248 /* this will fail for composite formats */
249 if (format
.GetType() == wxDF_TEXT
)
251 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
252 wxString
text( text_object
->GetText() );
255 const wxWX2MBbuf s
= text
.mbc_str();
257 #else // more efficient in non-Unicode
258 const char *s
= text
.c_str();
259 int len
= (int) text
.Length();
261 gtk_selection_data_set(
263 GDK_SELECTION_TYPE_STRING
,
265 (unsigned char*) (const char*) s
,
271 int size
= data
->GetDataSize( format
);
273 if (size
== 0) return;
275 char *d
= new char[size
];
277 data
->GetDataHere( selection_data
->target
, (void*) d
);
279 gtk_selection_data_set(
281 GDK_SELECTION_TYPE_STRING
,
287 //-----------------------------------------------------------------------------
289 //-----------------------------------------------------------------------------
291 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
293 wxClipboard::wxClipboard()
297 m_ownsClipboard
= FALSE
;
298 m_ownsPrimarySelection
= FALSE
;
300 m_data
= (wxDataObject
*) NULL
;
301 m_receivedData
= (wxDataObject
*) NULL
;
303 /* we use m_targetsWidget to query what formats are available */
305 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
306 gtk_widget_realize( m_targetsWidget
);
308 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
309 "selection_received",
310 GTK_SIGNAL_FUNC( targets_selection_received
),
313 /* we use m_clipboardWidget to get and to offer data */
315 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
316 gtk_widget_realize( m_clipboardWidget
);
318 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
319 "selection_received",
320 GTK_SIGNAL_FUNC( selection_received
),
323 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
324 "selection_clear_event",
325 GTK_SIGNAL_FUNC( selection_clear_clip
),
328 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
329 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
331 m_formatSupported
= FALSE
;
332 m_targetRequested
= 0;
334 m_usePrimary
= FALSE
;
337 wxClipboard::~wxClipboard()
341 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
342 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
345 void wxClipboard::Clear()
350 /* disable GUI threads */
351 wxapp_uninstall_thread_wakeup();
354 /* As we have data we also own the clipboard. Once we no longer own
355 it, clear_selection is called which will set m_data to zero */
356 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
360 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
361 (guint32
) GDK_CURRENT_TIME
);
363 while (m_waiting
) gtk_main_iteration();
366 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
370 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
371 (guint32
) GDK_CURRENT_TIME
);
373 while (m_waiting
) gtk_main_iteration();
379 m_data
= (wxDataObject
*) NULL
;
383 /* re-enable GUI threads */
384 wxapp_install_thread_wakeup();
388 m_targetRequested
= 0;
389 m_formatSupported
= FALSE
;
392 bool wxClipboard::Open()
394 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
401 bool wxClipboard::SetData( wxDataObject
*data
)
403 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
405 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
409 return AddData( data
);
412 bool wxClipboard::AddData( wxDataObject
*data
)
414 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
416 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
418 /* we can only store one wxDataObject */
423 /* get formats from wxDataObjects */
424 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
425 m_data
->GetAllFormats( array
);
427 /* primary selection or clipboard */
428 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
432 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
434 wxLogDebug( wxT("wxClipboard now supports atom %s"), array
[i
].GetId().c_str() );
436 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
439 0 ); /* what is info ? */
444 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
446 GTK_SIGNAL_FUNC(selection_handler
),
450 /* disable GUI threads */
451 wxapp_uninstall_thread_wakeup();
454 /* Tell the world we offer clipboard data */
455 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
457 (guint32
) GDK_CURRENT_TIME
));
460 m_ownsPrimarySelection
= res
;
462 m_ownsClipboard
= res
;
465 /* re-enable GUI threads */
466 wxapp_install_thread_wakeup();
472 void wxClipboard::Close()
474 wxCHECK_RET( m_open
, wxT("clipboard not open") );
479 bool wxClipboard::IsOpened() const
484 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
486 /* reentrance problems */
487 if (m_open
) return TRUE
;
489 /* store requested format to be asked for by callbacks */
490 m_targetRequested
= format
;
492 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
494 m_formatSupported
= FALSE
;
496 /* perform query. this will set m_formatSupported to
497 TRUE if m_targetRequested is supported.
498 also, we have to wait for the "answer" from the
499 clipboard owner which is an asynchronous process.
500 therefore we set m_waiting = TRUE here and wait
501 until the callback "targets_selection_received"
506 gtk_selection_convert( m_targetsWidget
,
507 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
510 (guint32
) GDK_CURRENT_TIME
);
512 while (m_waiting
) gtk_main_iteration();
514 if (!m_formatSupported
) return FALSE
;
519 bool wxClipboard::GetData( wxDataObject
& data
)
521 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
523 /* get formats from wxDataObjects */
524 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
525 data
.GetAllFormats( array
);
527 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
529 wxDataFormat
format( array
[i
] );
531 wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format
.GetId().c_str() );
533 /* is data supported by clipboard ? */
535 /* store requested format to be asked for by callbacks */
536 m_targetRequested
= format
;
538 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
540 m_formatSupported
= FALSE
;
542 /* perform query. this will set m_formatSupported to
543 TRUE if m_targetRequested is supported.
544 also, we have to wait for the "answer" from the
545 clipboard owner which is an asynchronous process.
546 therefore we set m_waiting = TRUE here and wait
547 until the callback "targets_selection_received"
552 gtk_selection_convert( m_targetsWidget
,
553 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
556 (guint32
) GDK_CURRENT_TIME
);
558 while (m_waiting
) gtk_main_iteration();
560 if (!m_formatSupported
) continue;
562 /* store pointer to data object to be filled up by callbacks */
563 m_receivedData
= &data
;
565 /* store requested format to be asked for by callbacks */
566 m_targetRequested
= format
;
568 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
571 m_formatSupported
= FALSE
;
573 /* ask for clipboard contents. this will set
574 m_formatSupported to TRUE if m_targetRequested
576 also, we have to wait for the "answer" from the
577 clipboard owner which is an asynchronous process.
578 therefore we set m_waiting = TRUE here and wait
579 until the callback "targets_selection_received"
584 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
586 gtk_selection_convert( m_clipboardWidget
,
587 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
590 (guint32
) GDK_CURRENT_TIME
);
592 while (m_waiting
) gtk_main_iteration();
594 /* this is a true error as we checked for the presence of such data before */
595 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
602 wxLogDebug( wxT("wxClipboard::GetData: format not found") );