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 //-----------------------------------------------------------------------------
83 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
84 GtkSelectionData
*selection_data
,
85 guint32
WXUNUSED(time
),
86 wxClipboard
*clipboard
)
88 if ( wxTheClipboard
&& selection_data
->length
> 0 )
90 // make sure we got the data in the correct form
91 GdkAtom type
= selection_data
->type
;
92 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
94 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
96 wxLogTrace( TRACE_CLIPBOARD
,
97 _T("got unsupported clipboard target") );
99 clipboard
->m_waiting
= FALSE
;
105 wxDataFormat
clip( selection_data
->selection
);
106 wxLogTrace( TRACE_CLIPBOARD
,
107 wxT("selection received for targets, clipboard %s"),
108 clip
.GetId().c_str() );
109 #endif // __WXDEBUG__
111 // the atoms we received, holding a list of targets (= formats)
112 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
114 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
116 wxDataFormat
format( atoms
[i
] );
118 wxLogTrace( TRACE_CLIPBOARD
,
119 wxT("selection received for targets, format %s"),
120 format
.GetId().c_str() );
122 // printf( "format %s requested %s\n",
123 // gdk_atom_name( atoms[i] ),
124 // gdk_atom_name( clipboard->m_targetRequested ) );
126 if (format
== clipboard
->m_targetRequested
)
128 clipboard
->m_waiting
= FALSE
;
129 clipboard
->m_formatSupported
= TRUE
;
135 clipboard
->m_waiting
= FALSE
;
139 //-----------------------------------------------------------------------------
140 // "selection_received" for the actual data
141 //-----------------------------------------------------------------------------
145 selection_received( GtkWidget
*WXUNUSED(widget
),
146 GtkSelectionData
*selection_data
,
147 guint32
WXUNUSED(time
),
148 wxClipboard
*clipboard
)
152 clipboard
->m_waiting
= FALSE
;
156 wxDataObject
*data_object
= clipboard
->m_receivedData
;
160 clipboard
->m_waiting
= FALSE
;
164 if (selection_data
->length
<= 0)
166 clipboard
->m_waiting
= FALSE
;
170 wxDataFormat
format( selection_data
->target
);
172 // make sure we got the data in the correct format
173 if (!data_object
->IsSupportedFormat( format
) )
175 clipboard
->m_waiting
= FALSE
;
180 This seems to cause problems somehow
181 // make sure we got the data in the correct form (selection type).
182 // if so, copy data to target object
183 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
185 clipboard
->m_waiting
= FALSE
;
190 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
192 wxTheClipboard
->m_formatSupported
= TRUE
;
193 clipboard
->m_waiting
= FALSE
;
197 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
203 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
205 if (!wxTheClipboard
) return TRUE
;
207 if (event
->selection
== GDK_SELECTION_PRIMARY
)
209 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
212 if (event
->selection
== g_clipboardAtom
)
214 wxTheClipboard
->m_ownsClipboard
= FALSE
;
218 wxTheClipboard
->m_waiting
= FALSE
;
222 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
223 (!wxTheClipboard
->m_ownsClipboard
))
225 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
226 if (wxTheClipboard
->m_data
)
228 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
230 delete wxTheClipboard
->m_data
;
231 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
235 wxTheClipboard
->m_waiting
= FALSE
;
240 //-----------------------------------------------------------------------------
241 // selection handler for supplying data
242 //-----------------------------------------------------------------------------
246 selection_handler( GtkWidget
*WXUNUSED(widget
),
247 GtkSelectionData
*selection_data
,
248 guint
WXUNUSED(info
),
249 guint
WXUNUSED(time
),
250 gpointer
WXUNUSED(data
) )
252 if (!wxTheClipboard
) return;
254 if (!wxTheClipboard
->m_data
) return;
256 wxDataObject
*data
= wxTheClipboard
->m_data
;
258 wxDataFormat
format( selection_data
->target
);
261 wxLogTrace(TRACE_CLIPBOARD
,
262 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s"),
263 format
.GetId().c_str(),
264 wxString::FromAscii(gdk_atom_name(selection_data
->target
)).c_str(),
265 wxString::FromAscii(gdk_atom_name(selection_data
->type
)).c_str(),
266 wxString::FromAscii(gdk_atom_name(selection_data
->selection
)).c_str()
270 if (!data
->IsSupportedFormat( format
)) return;
272 int size
= data
->GetDataSize( format
);
274 if (size
== 0) return;
276 void *d
= malloc(size
);
278 // Text data will be in UTF8 in Unicode mode.
279 data
->GetDataHere( selection_data
->target
, d
);
282 // NB: GTK+ requires special treatment of UTF8_STRING data, the text
283 // would show as UTF-8 data interpreted as latin1 (?) in other
284 // GTK+ apps if we used gtk_selection_data_set()
285 if (format
== wxDataFormat(wxDF_UNICODETEXT
))
287 gtk_selection_data_set_text(
295 gtk_selection_data_set(
297 GDK_SELECTION_TYPE_STRING
,
307 //-----------------------------------------------------------------------------
309 //-----------------------------------------------------------------------------
311 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
313 wxClipboard::wxClipboard()
318 m_ownsClipboard
= FALSE
;
319 m_ownsPrimarySelection
= FALSE
;
321 m_data
= (wxDataObject
*) NULL
;
322 m_receivedData
= (wxDataObject
*) NULL
;
324 /* we use m_targetsWidget to query what formats are available */
326 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
327 gtk_widget_realize( m_targetsWidget
);
329 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
330 "selection_received",
331 GTK_SIGNAL_FUNC( targets_selection_received
),
334 /* we use m_clipboardWidget to get and to offer data */
336 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
337 gtk_widget_realize( m_clipboardWidget
);
339 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
340 "selection_received",
341 GTK_SIGNAL_FUNC( selection_received
),
344 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
345 "selection_clear_event",
346 GTK_SIGNAL_FUNC( selection_clear_clip
),
349 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
350 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
352 m_formatSupported
= FALSE
;
353 m_targetRequested
= 0;
355 m_usePrimary
= FALSE
;
358 wxClipboard::~wxClipboard()
362 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
363 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
366 void wxClipboard::Clear()
371 /* disable GUI threads */
374 // As we have data we also own the clipboard. Once we no longer own
375 // it, clear_selection is called which will set m_data to zero
376 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
380 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
381 (guint32
) GDK_CURRENT_TIME
);
383 while (m_waiting
) gtk_main_iteration();
386 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
390 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
391 (guint32
) GDK_CURRENT_TIME
);
393 while (m_waiting
) gtk_main_iteration();
399 m_data
= (wxDataObject
*) NULL
;
403 /* re-enable GUI threads */
407 m_targetRequested
= 0;
408 m_formatSupported
= FALSE
;
411 bool wxClipboard::Open()
413 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
420 bool wxClipboard::SetData( wxDataObject
*data
)
422 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
424 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
428 return AddData( data
);
431 bool wxClipboard::AddData( wxDataObject
*data
)
433 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
435 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
437 // we can only store one wxDataObject
442 // get formats from wxDataObjects
443 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
444 m_data
->GetAllFormats( array
);
446 // primary selection or clipboard
447 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
451 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
453 wxLogTrace( TRACE_CLIPBOARD
,
454 wxT("wxClipboard now supports atom %s"),
455 array
[i
].GetId().c_str() );
457 // printf( "added %s\n",
458 // gdk_atom_name( array[i].GetFormatId() ) );
460 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
463 0 ); /* what is info ? */
468 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
470 GTK_SIGNAL_FUNC(selection_handler
),
474 /* disable GUI threads */
477 /* Tell the world we offer clipboard data */
478 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
480 (guint32
) GDK_CURRENT_TIME
));
483 m_ownsPrimarySelection
= res
;
485 m_ownsClipboard
= res
;
488 /* re-enable GUI threads */
494 void wxClipboard::Close()
496 wxCHECK_RET( m_open
, wxT("clipboard not open") );
501 bool wxClipboard::IsOpened() const
506 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
508 /* reentrance problems */
509 if (m_waiting
) return FALSE
;
511 /* store requested format to be asked for by callbacks */
512 m_targetRequested
= format
;
514 wxLogTrace( TRACE_CLIPBOARD
,
515 wxT("wxClipboard:IsSupported: requested format: %s"),
516 format
.GetId().c_str() );
518 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
520 m_formatSupported
= FALSE
;
522 /* perform query. this will set m_formatSupported to
523 TRUE if m_targetRequested is supported.
524 also, we have to wait for the "answer" from the
525 clipboard owner which is an asynchronous process.
526 therefore we set m_waiting = TRUE here and wait
527 until the callback "targets_selection_received"
532 gtk_selection_convert( m_targetsWidget
,
533 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
536 (guint32
) GDK_CURRENT_TIME
);
538 while (m_waiting
) gtk_main_iteration();
540 #if defined(__WXGTK20__) && wxUSE_UNICODE
541 if (!m_formatSupported
&& format
== wxDataFormat(wxDF_UNICODETEXT
))
543 // Another try with plain STRING format
544 extern GdkAtom g_altTextAtom
;
545 return IsSupported(g_altTextAtom
);
549 return m_formatSupported
;
552 bool wxClipboard::GetData( wxDataObject
& data
)
554 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
556 /* get formats from wxDataObjects */
557 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
558 data
.GetAllFormats( array
);
560 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
562 wxDataFormat
format( array
[i
] );
564 wxLogTrace( TRACE_CLIPBOARD
,
565 wxT("wxClipboard::GetData: requested format: %s"),
566 format
.GetId().c_str() );
568 /* is data supported by clipboard ? */
570 /* store requested format to be asked for by callbacks */
571 m_targetRequested
= format
;
573 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
575 m_formatSupported
= FALSE
;
577 /* perform query. this will set m_formatSupported to
578 TRUE if m_targetRequested is supported.
579 also, we have to wait for the "answer" from the
580 clipboard owner which is an asynchronous process.
581 therefore we set m_waiting = TRUE here and wait
582 until the callback "targets_selection_received"
587 gtk_selection_convert( m_targetsWidget
,
588 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
591 (guint32
) GDK_CURRENT_TIME
);
593 while (m_waiting
) gtk_main_iteration();
595 if (!m_formatSupported
) continue;
597 /* store pointer to data object to be filled up by callbacks */
598 m_receivedData
= &data
;
600 /* store requested format to be asked for by callbacks */
601 m_targetRequested
= format
;
603 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
606 m_formatSupported
= FALSE
;
608 /* ask for clipboard contents. this will set
609 m_formatSupported to TRUE if m_targetRequested
611 also, we have to wait for the "answer" from the
612 clipboard owner which is an asynchronous process.
613 therefore we set m_waiting = TRUE here and wait
614 until the callback "targets_selection_received"
619 wxLogTrace( TRACE_CLIPBOARD
,
620 wxT("wxClipboard::GetData: format found, start convert") );
622 gtk_selection_convert( m_clipboardWidget
,
623 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
626 (guint32
) GDK_CURRENT_TIME
);
628 while (m_waiting
) gtk_main_iteration();
630 /* this is a true error as we checked for the presence of such data before */
631 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
638 wxLogTrace( TRACE_CLIPBOARD
,
639 wxT("wxClipboard::GetData: format not found") );