1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "clipbrd.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/clipbrd.h"
21 #include "wx/dataobj.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 GdkAtom g_clipboardAtom
= 0;
41 GdkAtom g_targetsAtom
= 0;
43 #if defined(__WXGTK20__) && wxUSE_UNICODE
44 extern GdkAtom g_altTextAtom
;
47 // the trace mask we use with wxLogTrace() - call
48 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
49 // (there will be a *lot* of them!)
50 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 /* The contents of a selection are returned in a GtkSelectionData
57 structure. selection/target identify the request.
58 type specifies the type of the return; if length < 0, and
59 the data should be ignored. This structure has object semantics -
60 no fields should be modified directly, they should not be created
61 directly, and pointers to them should not be stored beyond the duration of
62 a callback. (If the last is changed, we'll need to add reference
65 struct _GtkSelectionData
77 //-----------------------------------------------------------------------------
78 // "selection_received" for targets
79 //-----------------------------------------------------------------------------
82 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
83 GtkSelectionData
*selection_data
,
84 guint32
WXUNUSED(time
),
85 wxClipboard
*clipboard
)
87 if ( wxTheClipboard
&& selection_data
->length
> 0 )
89 // make sure we got the data in the correct form
90 GdkAtom type
= selection_data
->type
;
91 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
93 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
95 wxLogTrace( TRACE_CLIPBOARD
,
96 _T("got unsupported clipboard target") );
98 clipboard
->m_waiting
= FALSE
;
104 wxDataFormat
clip( selection_data
->selection
);
105 wxLogTrace( TRACE_CLIPBOARD
,
106 wxT("selection received for targets, clipboard %s"),
107 clip
.GetId().c_str() );
108 #endif // __WXDEBUG__
110 // the atoms we received, holding a list of targets (= formats)
111 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
113 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
115 wxDataFormat
format( atoms
[i
] );
117 wxLogTrace( TRACE_CLIPBOARD
,
118 wxT("selection received for targets, format %s"),
119 format
.GetId().c_str() );
121 // printf( "format %s requested %s\n",
122 // gdk_atom_name( atoms[i] ),
123 // gdk_atom_name( clipboard->m_targetRequested ) );
125 if (format
== clipboard
->m_targetRequested
)
127 clipboard
->m_waiting
= FALSE
;
128 clipboard
->m_formatSupported
= TRUE
;
134 clipboard
->m_waiting
= FALSE
;
137 //-----------------------------------------------------------------------------
138 // "selection_received" for the actual data
139 //-----------------------------------------------------------------------------
142 selection_received( GtkWidget
*WXUNUSED(widget
),
143 GtkSelectionData
*selection_data
,
144 guint32
WXUNUSED(time
),
145 wxClipboard
*clipboard
)
149 clipboard
->m_waiting
= FALSE
;
153 wxDataObject
*data_object
= clipboard
->m_receivedData
;
157 clipboard
->m_waiting
= FALSE
;
161 if (selection_data
->length
<= 0)
163 clipboard
->m_waiting
= FALSE
;
167 wxDataFormat
format( selection_data
->target
);
169 // make sure we got the data in the correct format
170 if (!data_object
->IsSupportedFormat( format
) )
172 clipboard
->m_waiting
= FALSE
;
177 This seems to cause problems somehow
178 // make sure we got the data in the correct form (selection type).
179 // if so, copy data to target object
180 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
182 clipboard
->m_waiting
= FALSE
;
187 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
189 wxTheClipboard
->m_formatSupported
= TRUE
;
190 clipboard
->m_waiting
= FALSE
;
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
198 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
200 if (!wxTheClipboard
) return TRUE
;
202 if (event
->selection
== GDK_SELECTION_PRIMARY
)
204 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
207 if (event
->selection
== g_clipboardAtom
)
209 wxTheClipboard
->m_ownsClipboard
= FALSE
;
213 wxTheClipboard
->m_waiting
= FALSE
;
217 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
218 (!wxTheClipboard
->m_ownsClipboard
))
220 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
221 if (wxTheClipboard
->m_data
)
223 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
225 delete wxTheClipboard
->m_data
;
226 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
230 wxTheClipboard
->m_waiting
= FALSE
;
234 //-----------------------------------------------------------------------------
235 // selection handler for supplying data
236 //-----------------------------------------------------------------------------
239 selection_handler( GtkWidget
*WXUNUSED(widget
),
240 GtkSelectionData
*selection_data
,
241 guint
WXUNUSED(info
),
242 guint
WXUNUSED(time
),
243 gpointer
WXUNUSED(data
) )
245 if (!wxTheClipboard
) return;
247 if (!wxTheClipboard
->m_data
) return;
249 wxDataObject
*data
= wxTheClipboard
->m_data
;
251 wxDataFormat
format( selection_data
->target
);
254 wxLogTrace(TRACE_CLIPBOARD
,
255 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s"),
256 format
.GetId().c_str(),
257 wxString::FromAscii(gdk_atom_name(selection_data
->target
)).c_str(),
258 wxString::FromAscii(gdk_atom_name(selection_data
->type
)).c_str(),
259 wxString::FromAscii(gdk_atom_name(selection_data
->selection
)).c_str()
263 if (!data
->IsSupportedFormat( format
)) return;
265 int size
= data
->GetDataSize( format
);
267 if (size
== 0) return;
269 void *d
= malloc(size
);
271 // Text data will be in UTF8 in Unicode mode.
272 data
->GetDataHere( selection_data
->target
, d
);
275 // NB: GTK+ requires special treatment of UTF8_STRING data, the text
276 // would show as UTF-8 data interpreted as latin1 (?) in other
277 // GTK+ apps if we used gtk_selection_data_set()
278 if (format
== wxDataFormat(wxDF_UNICODETEXT
))
280 gtk_selection_data_set_text(
288 gtk_selection_data_set(
290 GDK_SELECTION_TYPE_STRING
,
299 //-----------------------------------------------------------------------------
301 //-----------------------------------------------------------------------------
303 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
305 wxClipboard::wxClipboard()
310 m_ownsClipboard
= FALSE
;
311 m_ownsPrimarySelection
= FALSE
;
313 m_data
= (wxDataObject
*) NULL
;
314 m_receivedData
= (wxDataObject
*) NULL
;
316 /* we use m_targetsWidget to query what formats are available */
318 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
319 gtk_widget_realize( m_targetsWidget
);
321 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
322 "selection_received",
323 GTK_SIGNAL_FUNC( targets_selection_received
),
326 /* we use m_clipboardWidget to get and to offer data */
328 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
329 gtk_widget_realize( m_clipboardWidget
);
331 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
332 "selection_received",
333 GTK_SIGNAL_FUNC( selection_received
),
336 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
337 "selection_clear_event",
338 GTK_SIGNAL_FUNC( selection_clear_clip
),
341 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
342 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
344 m_formatSupported
= FALSE
;
345 m_targetRequested
= 0;
347 m_usePrimary
= FALSE
;
350 wxClipboard::~wxClipboard()
354 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
355 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
358 void wxClipboard::Clear()
363 /* disable GUI threads */
366 // As we have data we also own the clipboard. Once we no longer own
367 // it, clear_selection is called which will set m_data to zero
368 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
372 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
373 (guint32
) GDK_CURRENT_TIME
);
375 while (m_waiting
) gtk_main_iteration();
378 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
382 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
383 (guint32
) GDK_CURRENT_TIME
);
385 while (m_waiting
) gtk_main_iteration();
391 m_data
= (wxDataObject
*) NULL
;
395 /* re-enable GUI threads */
399 m_targetRequested
= 0;
400 m_formatSupported
= FALSE
;
403 bool wxClipboard::Open()
405 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
412 bool wxClipboard::SetData( wxDataObject
*data
)
414 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
416 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
420 return AddData( data
);
423 bool wxClipboard::AddData( wxDataObject
*data
)
425 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
427 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
429 // we can only store one wxDataObject
434 // get formats from wxDataObjects
435 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
436 m_data
->GetAllFormats( array
);
438 // primary selection or clipboard
439 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
443 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
445 wxLogTrace( TRACE_CLIPBOARD
,
446 wxT("wxClipboard now supports atom %s"),
447 array
[i
].GetId().c_str() );
449 // printf( "added %s\n",
450 // gdk_atom_name( array[i].GetFormatId() ) );
452 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
455 0 ); /* what is info ? */
460 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
462 GTK_SIGNAL_FUNC(selection_handler
),
466 /* disable GUI threads */
469 /* Tell the world we offer clipboard data */
470 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
472 (guint32
) GDK_CURRENT_TIME
));
475 m_ownsPrimarySelection
= res
;
477 m_ownsClipboard
= res
;
480 /* re-enable GUI threads */
486 void wxClipboard::Close()
488 wxCHECK_RET( m_open
, wxT("clipboard not open") );
493 bool wxClipboard::IsOpened() const
498 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
500 /* reentrance problems */
501 if (m_waiting
) return FALSE
;
503 /* store requested format to be asked for by callbacks */
504 m_targetRequested
= format
;
506 wxLogTrace( TRACE_CLIPBOARD
,
507 wxT("wxClipboard:IsSupported: requested format: %s"),
508 format
.GetId().c_str() );
510 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
512 m_formatSupported
= FALSE
;
514 /* perform query. this will set m_formatSupported to
515 TRUE if m_targetRequested is supported.
516 also, we have to wait for the "answer" from the
517 clipboard owner which is an asynchronous process.
518 therefore we set m_waiting = TRUE here and wait
519 until the callback "targets_selection_received"
524 gtk_selection_convert( m_targetsWidget
,
525 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
528 (guint32
) GDK_CURRENT_TIME
);
530 while (m_waiting
) gtk_main_iteration();
532 #if defined(__WXGTK20__) && wxUSE_UNICODE
533 if (!m_formatSupported
&& format
== wxDataFormat(wxDF_UNICODETEXT
))
535 // Another try with plain STRING format
536 extern GdkAtom g_altTextAtom
;
537 return IsSupported(g_altTextAtom
);
541 return m_formatSupported
;
544 bool wxClipboard::GetData( wxDataObject
& data
)
546 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
548 /* get formats from wxDataObjects */
549 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
550 data
.GetAllFormats( array
);
552 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
554 wxDataFormat
format( array
[i
] );
556 wxLogTrace( TRACE_CLIPBOARD
,
557 wxT("wxClipboard::GetData: requested format: %s"),
558 format
.GetId().c_str() );
560 /* is data supported by clipboard ? */
562 /* store requested format to be asked for by callbacks */
563 m_targetRequested
= format
;
565 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
567 m_formatSupported
= FALSE
;
569 /* perform query. this will set m_formatSupported to
570 TRUE if m_targetRequested is supported.
571 also, we have to wait for the "answer" from the
572 clipboard owner which is an asynchronous process.
573 therefore we set m_waiting = TRUE here and wait
574 until the callback "targets_selection_received"
579 gtk_selection_convert( m_targetsWidget
,
580 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
583 (guint32
) GDK_CURRENT_TIME
);
585 while (m_waiting
) gtk_main_iteration();
587 if (!m_formatSupported
) continue;
589 /* store pointer to data object to be filled up by callbacks */
590 m_receivedData
= &data
;
592 /* store requested format to be asked for by callbacks */
593 m_targetRequested
= format
;
595 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
598 m_formatSupported
= FALSE
;
600 /* ask for clipboard contents. this will set
601 m_formatSupported to TRUE if m_targetRequested
603 also, we have to wait for the "answer" from the
604 clipboard owner which is an asynchronous process.
605 therefore we set m_waiting = TRUE here and wait
606 until the callback "targets_selection_received"
611 wxLogTrace( TRACE_CLIPBOARD
,
612 wxT("wxClipboard::GetData: format found, start convert") );
614 gtk_selection_convert( m_clipboardWidget
,
615 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
618 (guint32
) GDK_CURRENT_TIME
);
620 while (m_waiting
) gtk_main_iteration();
622 /* this is a true error as we checked for the presence of such data before */
623 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
630 wxLogTrace( TRACE_CLIPBOARD
,
631 wxT("wxClipboard::GetData: format not found") );